The consuming API wraps PgQ’s lower-level primitives (Documentation Index
Fetch the complete documentation index at: https://mintlify.com/NikolayS/PgQue/llms.txt
Use this file to discover all available pages before exploring further.
next_batch, get_batch_events, finish_batch, event_retry) into a concise receive → process → ack (or nack) loop. All functions in this section require the pgque_reader role. Note that pgque_writer does not inherit pgque_reader — applications that both produce and consume must hold both roles explicitly.
Snapshot rule.
pgque.send → pgque.ticker → pgque.receive must each run in their own committed transactions. The ticker’s snapshot must be taken after send commits, and receive only sees what committed before it. The same rule applies to pgque.maint_retry_events → pgque.ticker → pgque.receive. Pool-based drivers (Go pgxpool, TypeScript pg.Pool) satisfy this automatically via autocommit. Python’s non-autocommit default requires explicit commit boundaries; the high-level Python Consumer handles this internally.pgque.message composite type
pgque.receive returns a set of pgque.message rows. pgque.nack consumes the same composite.
Event id (
ev_id). The stable identifier for this event across retries.Batch containing this message. Pass this to
ack or nack.Event type (
ev_type). May be null; 'default' for events sent without an explicit type.Event data (
ev_data). Cast to jsonb for JSON access: msg.payload::jsonb.NULL on first delivery; incremented by one on each retry redelivery.Original event insertion time (
ev_time).ev_extra1. May be null.ev_extra2. May be null.ev_extra3. May be null.ev_extra4. May be null.Functions
pgque.receive(queue text, consumer text, max_return int default 100) → setof pgque.message
Pulls the next batch for consumer on queue and streams up to max_return messages. max_return must be >= 1; passing 0 or a negative value raises an error. Returns an empty set if no batch is available.
Grant: pgque_reader. Source: sql/pgque-api/receive.sql.
Name of the queue to consume from.
Name of the registered consumer. The consumer must have been subscribed with
pgque.subscribe or pgque.register_consumer.Maximum number of message rows returned to the caller. Must be >= 1.
pgque.ack(batch_id bigint) → integer
Closes the batch and advances the consumer position. Modern alias for pgque.finish_batch. Returns 1 on success, 0 if the batch was not found.
Grant: pgque_reader. Source: sql/pgque-api/receive.sql.
The
batch_id returned by pgque.receive. Closes the batch and moves the consumer cursor past it.pgque.nack(batch_id bigint, msg pgque.message, retry_after interval default '60 seconds', reason text default null) → integer
Negative-acknowledges one message from the active batch. Schedules it for redelivery or routes it to the dead letter queue when retries are exhausted.
Only msg.msg_id (together with batch_id) is used from the composite — type, payload, retry_count, created_at, and the extra* fields are ignored. nack re-queries the canonical event from the active batch and uses those server-side values for all decisions and writes.
Routing logic:
- If the canonical
ev_retryis below the queue’smax_retries, re-queues the event afterretry_after(viapgque.event_retry). - If
ev_retry >= max_retries, routes the event topgque.dead_letter(viapgque.event_dead). This routing is idempotent: repeated calls for the same terminal message produce exactly one DLQ row. - If
msg.msg_idis not present in the active batch — including aNULLmsg_id or a msg_id from a different batch — raisesmsg_id % not found in batch %.
pgque_reader. Source: sql/pgque-api/receive.sql.
The batch id from the active receive.
The full message row returned by
pgque.receive. Only msg.msg_id is read; other fields are ignored.How long to wait before redelivering the message. Ignored when the message is being routed to the DLQ.
Optional failure reason stored in
pgque.dead_letter.dl_reason when the message is routed to the DLQ.nack schedules a per-event retry (or DLQ routing), but it does not advance the consumer cursor. Always call ack(batch_id) after nacking all failed messages in a batch to finalize it and allow the consumer to proceed.pgque.subscribe(queue text, consumer text) → integer
Registers consumer on queue. Modern alias for pgque.register_consumer. Returns 1 on new registration, 0 if already registered.
Grant: pgque_reader. Source: sql/pgque-api/send.sql.
Name of the queue to subscribe to.
Name to give this consumer. Free-form text; used as the cursor identity.
pgque.unsubscribe(queue text, consumer text) → integer
Removes the consumer and its retry-queue entries from queue. Modern alias for pgque.unregister_consumer. Returns the number of subscriptions removed.
Grant: pgque_reader. Source: sql/pgque-api/send.sql.
Name of the queue.
Name of the consumer to remove.
