Notifications and polling contract
Source commit cbff27b8a34e1384384018680e46c20d5c610cc9.
SQL 0.1.1 emits PostgreSQL notifications as hints that a consumer should inspect stored work. Notifications contain no messages, payloads, or acknowledgement state. Correctness depends on queue queries and polling, not on observing every notification.
Channels and timing
| Channel | When emitted |
|---|---|
pmq:t:<topic> | Every publication, whether or not it has a live subscriber. |
pmq:q:<queue> | Non-final nack, release, and replay that moves at least one DLQ entry. |
pmq:q:<queue> for grouped work | Ack, final nack, final-attempt maintenance retirement, or deletion of an unsettled grouped delivery, which can unblock a successor. |
All payloads are empty. PostgreSQL delivers notifications on commit and can coalesce identical notifications within one transaction. One notification therefore does not correspond to one message.
Claims, visibility extension, queue-lifetime extension, ungrouped acknowledgements, topology declarations/deletions, and general cleanup do not provide a complete state-change notification stream. A consumer cannot rely on queue deletion immediately producing an explicit delete notification.
A custom listener
Use a dedicated session connection. LISTEN state does not survive reassignment through a transaction-mode pooler. Complete the subscription transaction before relying on it, then query queue state to close the startup race.
A receive loop should:
- Establish and commit LISTEN subscriptions for the topic and queue channels.
- Fetch eligible deliveries until its local capacity is full or the queue returns no work.
- Wait for a notification, the next eligible time hint, or the fallback polling deadline.
- Query again after any wake-up, handling empty results normally.
- Reestablish subscriptions after listener reconnection and query state again.
This contract is what SDK consumers implement; normal applications should configure their polling interval rather than write another listener.
Next visible time
get_next_visible_time(queue) returns the earliest visibility deadline among unsettled rows below the attempt limit and at the head of their group. It includes lease deadlines of processing heads. NULL means there is no matching candidate; a timestamp in the past means the hint is already due.
The hint is not a reservation. Another consumer may claim the row before this client wakes, and concurrent changes can make the result stale immediately. Use a bounded wait and fetch again. Do not spin continuously on a past hint when contention or another consumer prevents a claim.
The function returns NULL for a missing queue and does not validate Exclusive expiry. Actual consumption establishes whether the subscription remains usable. It cannot replace a queue-loss check.
Time-based work needs polling
No publisher is required to stay connected until a delayed message's deadline. A scheduled delivery is stored at publication; consumers must wake for time-based eligibility even if no new notification arrives at that instant.
Likewise, a crashed worker's lease can expire without a new publish notification. Polling and next-visible-time hints recover this work. Exhausted final attempts still require fast maintenance before FIFO successors can advance.
SDK defaults differ: Go and Rust normally cap fallback checks at ten seconds, TypeScript at one second. See SDK behavior before tuning. Shorter polling can reduce missed-notification latency while increasing empty database calls.
Use SQL API conventions for ownership and Using SQL for the basic claim transaction.