Skip to content

007. PostgreSQL type policy

Status: Accepted
Date: 2026-09-18
Scope: workhold logical schema and public identifiers

Context

workhold tables are write-heavy and hold a high row count. Parser v1 used domain-specific text fields and mutable JSON on hot rows. Native PostgreSQL ENUM, random UUID primary keys, and unbounded JSON would raise the cost of evolution, indexes, or WAL.

Decision

  • Internal relation keys use bigint GENERATED ALWAYS AS IDENTITY.
  • Public task, event, and claim IDs use native uuid with UUIDv7 generated by the workhold runtime; secret claim tokens are random UUIDv4.
  • State codes use smallint plus CHECK constraints and application mappings.
  • Instants use workhold-generated timestamptz; counters and generations use integer unless an aggregate requires bigint.
  • Task priority uses smallint and initially accepts only 0; available_at is a non-null timestamptz and initially equals the workhold store enqueue time.
  • Names and diagnostic codes use text plus explicit length and format CHECKs.
  • Attempt failures use a bounded machine-readable failure_code text and an optional capped failure_detail text.
  • Opaque payload is size-limited jsonb outside the frequently updated lease row; a payload GIN index is not created.
  • Idempotency and request fingerprints are 32-byte SHA-256 bytea.

Alternatives considered

Option Why not chosen
UUIDv4 as every PK/FK Larger random indexes and references on hot and history tables
PostgreSQL ENUM Deleting or renaming states complicates rolling schema evolution
varchar(n) or citext everywhere No storage gain; canonical lowercase and CHECKs are explicit
Unbounded JSONB on task lease row TOAST/WAL amplification on the hot-row lifecycle

Consequences

Positive: Compact joins and indexes, portable public IDs, an evolvable state machine, and bounded payload cost without PostgreSQL extensions.

Negative / trade-offs: Public and internal IDs coexist; UUIDv7 generation is required in the tested runtime; smallint states require a canonical mapping.

Follow-up: Column catalog — docs/03-reference/02-storage-contract.md. Client available_at and non-zero priority are reserved; enabling them is separate ADRs after Phase 11 / 12, without silent CHECK relaxation.

References