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
uuidwith UUIDv7 generated by the workhold runtime; secret claim tokens are random UUIDv4. - State codes use
smallintplus CHECK constraints and application mappings. - Instants use workhold-generated
timestamptz; counters and generations useintegerunless an aggregate requiresbigint. - Task
priorityusessmallintand initially accepts only0;available_atis a non-nulltimestamptzand initially equals the workhold store enqueue time. - Names and diagnostic codes use
textplus explicit length and format CHECKs. - Attempt failures use a bounded machine-readable
failure_code textand an optional cappedfailure_detail text. - Opaque payload is size-limited
jsonboutside 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.