PgQue · Design Brief
Within one queue, events that share a partition key are consumed in order by a single worker at a time; events with different keys are consumed in parallel. Order within a key, parallelism across keys — Kafka's partition model, achieved by routing, with no per-event locks and no new mutable state.
PgQue is an ordered, immutable log, not a job queue. The motivating
workload (a multi-tenant storage service evaluating PgQue to replace pg-boss) emits
millions of file-lifecycle events — FileCreated,
FileDeleted, FileOverwritten. They must be processed
in order per tenant, but order across tenants does not matter.
One in-order consumer can't keep up; naive multi-worker consumption breaks per-tenant order.
Per-key affinity + FIFO. Every event of key K maps to one slot hashtextextended(K,0) % N; within it, non-retried events of K arrive in ev_id order, never to another slot.
Single processor per key. At most one worker holds an unacked event for K at any instant.
Failure boundary. pause: no later K event until the failed one is acked or dead-lettered. skip (v0.1 default): at-least-once, order not held after a failure.
In · v0.1
send()-sourced eventshashtextextended(key,0) % N affinityskip failure policy (default)Deferred / out
pause policy — specified, ships v0.2| ID | Decision | Choice (v0.2) |
|---|---|---|
| D1 | Where the key lives | ev_extra1, send()-sourced queues only |
| D2 | Failure policy | skip default; pause ships v0.2 |
| D3 | Slot count N | Fixed, persisted & enforced per (queue, consumer) |
| D4 | Assignment function | hashtextextended(key,0) % N (version-stable) |
| D5 | State budget | No new table; pause derives from existing retry_queue |
| D6 | Producer signature | send(queue, type, payload, partition_key =>) |
| D7 | Slot & single-owner | slot = consumer "<c>#k/N"; receive lock |
Review round 1 re-grounded the mechanism: cooperative consumers hand out disjoint tick windows (not a hash-filtered shared batch), so slots are independent subscriptions, not a coop overlay. The retry path preserves ev_id and changes ev_txid — so order breaks across a retry, which is exactly what the pause boundary (G3) handles.
Open for round 2: the exact retry_queue predicate that rebuilds a slot's
blocked-key set after a crash (the crux of crash-safe pause), and whether
N× read amplification at target throughput justifies a single-reader/dispatch
optimization in v0.1.