| title | Tutorial |
|---|---|
| description | A hands-on PgQue walkthrough — install, send, receive, retry, dead-letter, idempotency, partition keys, and health checks. |
A guided, hands-on walkthrough. With psql access to a Postgres 14–19 database
and about fifteen minutes, you can follow every step end to end. You will type
SQL, see what comes back, and build an intuition for how PgQue moves messages
through an order-processing queue.
By the end you will have a working orders queue with a processor consumer,
a happy-path delivery, a retry that reappears after a delay, a message driven
all the way to the dead-letter queue, a health check, and a retry-safe keyed
event consumed through a leased partition slot.
Prerequisites: Postgres 14–19, a database to install into, and psql (the tutorial uses \i and \gset, which are psql meta-commands). pg_cron is recommended for production but not required here — this tutorial drives the ticker by hand, so it works on any managed or self-managed Postgres.
Each step shows the exact SQL and the expected output. Your msg_id / batch_id / ev_new numbers will differ from the examples — each pgque.force_next_tick call advances the event sequence by a large amount, so exact numeric output depends on when you call it. Treat the numbers as illustrative. Where transaction boundaries matter — and they matter, because PgQue is snapshot-based — the text calls that out.
For reproducible output, run psql with --no-psqlrc and PAGER=cat. This
tutorial follows the main branch development build; start from the cloned
repo so \i devel/sql/pgque.sql resolves. For the stable build, use the
latest release's tagged tutorial.
cd /path/to/pgque
PAGER=cat psql --no-psqlrc -d mydbFor vocabulary — "event", "batch", "tick", "rotation" — see the concepts glossary.
PgQue is a single SQL file. Install it inside a transaction so a failure leaves no half-built schema behind:
begin;
\i devel/sql/pgque.sql
commit;Verify the install by asking for the version:
select pgque.version(); version
---------
<installed development version>
(1 row)
The install creates the pgque schema, three roles (pgque_reader,
pgque_writer, pgque_admin), and every function you call in the rest of this
tutorial. The roles are siblings, not parent and child: pgque_writer produces
(send, send_idem, send_batch); pgque_reader owns the normal and
partitioned consume APIs; pgque_admin is a member of both.
If you are following along as the install owner, you need no extra grants — skip the next snippet. In production you grant the roles to your own app roles. An app that both produces and consumes needs both roles:
-- produce and consume:
grant pgque_reader, pgque_writer to app_orders;
-- produce only:
grant pgque_writer to app_webhook;
-- consume only (dashboard, metrics):
grant pgque_reader to metrics;See Installation & operations for the full role and grant details.
A queue is a named, shared event log. A consumer is a named cursor into that log. Any number of producers can write to the same queue concurrently, and any number of consumers can subscribe — each sees every event through its own cursor, independently (fan-out by default).
select pgque.create_queue('orders');
select pgque.subscribe('orders', 'processor'); create_queue
--------------
1
(1 row)
subscribe
-----------
1
(1 row)
create_queue returns 1 when it created the queue, 0 if it already existed — the call is idempotent. subscribe is the modern alias for PgQ's register_consumer.
Send one event to the queue. The jsonb overload validates and canonicalizes the payload:
select pgque.send('orders', '{"order_id": 42, "total": 99.95}'::jsonb); send
------
1
(1 row)
That return value is the event id — unique within the queue. pgque.send also accepts a raw text payload for formats you encode yourself (protobuf, msgpack, XML). Note that an untyped literal like '{"x":1}' without the ::jsonb cast resolves to the text overload and is stored verbatim, with no JSON validation. This tutorial stays on jsonb; see the reference for the full overload rules.
Now try to pull that event back out:
select * from pgque.receive('orders', 'processor', 100); msg_id | batch_id | type | payload | retry_count | created_at | ...
--------+----------+------+---------+-------------+------------+-----
(0 rows)
Zero rows. This surprises every first-time user, and it is not an error. Here is why.
PgQue is tick-based, not row-claiming. Producers append events, but consumers do not see rows directly — they see batches. A batch is the set of events between two ticks. Until a tick happens, there is no batch boundary, so pgque.receive has nothing to return and reports zero rows.
In normal operation a scheduler (pg_cron or an external loop) checks whether a tick is due. PgQue's default scheduler checks every 100 ms; each queue's thresholds decide whether to create a tick. In this tutorial you have not started a scheduler, so no tick has run since queue creation. That is the next step.
For demos and tests, pgque.force_next_tick lets one queue bypass the tick thresholds. It does not create a tick by itself — it only advances the queue's event sequence past the tick threshold so that the next ticker run is guaranteed to materialize a tick. You still have to call pgque.ticker() afterwards:
select pgque.force_next_tick('orders');
select pgque.ticker(); force_next_tick
-----------------
1
(1 row)
ticker
--------
1
(1 row)
force_next_tick returns the current last tick id (the queue was seeded with tick 1 by create_queue). ticker() returns the number of queues it ticked.
Each statement above runs in its own transaction. This is required, not stylistic. PgQue marks batch boundaries with a
pg_snapshot: the ticker captures a snapshot, andreceiveonly returns events whosesendcommitted before that snapshot. An event sent in the same transaction as the tick is still in-progress at snapshot time and is excluded from the batch. Wrappingsend+force_next_tick+ticker+receivein onebegin/commitreturns zero rows. In psql autocommit, eachselectis already its own transaction — just do not wrap them.
Now receive again:
select * from pgque.receive('orders', 'processor', 100); msg_id | batch_id | type | payload | retry_count | created_at
--------+----------+---------+----------------------------------+-------------+------------------------
1 | 1 | default | {"total": 99.95, "order_id": 42} | | 2026-06-02 10:00:00+00
(1 row)
The event is back. retry_count is null because this is the first delivery attempt. The batch_id is the value you need for the next step. (The jsonb overload stored a canonical form, so the keys come back ordered by length then alphabetically — "total" before "order_id" — rather than in the order you typed them.)
In production you never call force_next_tick: pg_cron runs the ticker continuously, or an external worker loop calls pgque.ticker() on its own cadence. force_next_tick exists precisely for the situation here — advancing one queue on demand without waiting on the tick thresholds.
A batch stays assigned to its consumer until the consumer calls ack. Until then the same batch is returned on every receive — the cursor has not moved.
This tutorial batch contains one event, already inspected above. Capture its batch_id and ack it. In psql, \gset saves a single-row result into a variable:
select batch_id from pgque.receive('orders', 'processor', 100) limit 1 \gset
select pgque.ack(:batch_id);For a real batch, process every returned message before acknowledging; do not copy the limit 1 selection as a processing loop. max_return is a complete-batch safety ceiling, not pagination. If receive raises an overflow error, roll back and retry with a sufficient ceiling within your resource budget; never acknowledge the failed receive.
ack
-----
1
(1 row)
Or, if you already saw batch_id = 1, call it directly with select pgque.ack(1);.
ack is the modern alias for PgQ's finish_batch. It finalizes the batch and advances the consumer cursor past it. Confirm there is nothing left:
select * from pgque.receive('orders', 'processor', 100);(0 rows)
Every row pgque.receive returns is a pgque.message composite: msg_id (PgQ's ev_id), batch_id, type, payload (text — cast to jsonb for JSON access), retry_count (null on first delivery, an integer after a retry), created_at, and four free-form extra1..4 text columns. The modern receive/ack/nack surface reduces cleanly to PgQ primitives; see the reference.
Consumers sometimes fail. nack handles that: it schedules the message for redelivery after a delay you choose, and routes the message to the dead-letter queue once it has exhausted its retries. By default a message retries up to 5 times before the DLQ. To reach the DLQ quickly in step 8, lower the ceiling first:
select pgque.set_queue_config('orders', 'max_retries', '2');The parameter is max_retries; set_queue_config prepends queue_ for you.
Send another order, tick, and receive — four separate transactions in psql autocommit:
select pgque.send('orders', '{"order_id": 43, "total": 10.00}'::jsonb);
select pgque.force_next_tick('orders');
select pgque.ticker();
select * from pgque.receive('orders', 'processor', 100); msg_id | batch_id | type | payload | retry_count | created_at
--------+----------+---------+----------------------------------+-------------+------------------------
2 | 2 | default | {"total": 10.00, "order_id": 43} | | 2026-06-02 10:01:00+00
(1 row)
Now pretend the handler failed. nack takes the full pgque.message row. Note that nack and ack are both needed on the same batch — they are not alternatives. nack per-event schedules the retry (or routes to the DLQ); ack per-batch finalizes the batch and advances the cursor. Without the ack, the consumer never moves past this batch. A do block that receives, nacks, and acks in one place is the natural pattern:
do $$
declare
v_msg pgque.message;
begin
select * into v_msg from pgque.receive('orders', 'processor', 1) limit 1;
perform pgque.nack(v_msg.batch_id, v_msg, '0 seconds'::interval, 'simulated failure');
perform pgque.ack(v_msg.batch_id);
end $$;The event is now in PgQ's retry queue, with a redelivery time set by the interval you passed (here '0 seconds', so it is due immediately; in real code you would pass a backoff like '60 seconds'). Moving a due event back into the main stream is a separate maintenance step, pgque.maint_retry_events(). After that, the next tick makes it visible again:
select pgque.maint_retry_events();
select pgque.force_next_tick('orders');
select pgque.ticker();
select * from pgque.receive('orders', 'processor', 100); msg_id | batch_id | type | payload | retry_count | created_at
--------+----------+---------+----------------------------------+-------------+------------------------
2 | 3 | default | {"total": 10.00, "order_id": 43} | 1 | 2026-06-02 10:01:00+00
(1 row)
Same msg_id = 2, new batch_id = 3, and retry_count = 1 — that is the redelivery. (In production, pgque.start() schedules maint_retry_events on its own cadence and you never call it by hand.)
nack routes a message to the dead-letter queue when its stored retry count has reached the ceiling — specifically when coalesce(ev_retry, 0) >= max_retries. Walk the arithmetic with max_retries = 2:
- First nack: the event's
retry_countis null, socoalesce(null, 0) = 0.0 >= 2is false → retry. The retry bumps the stored count to1. (This is the nack you did in step 7.) - Second nack:
retry_count = 1.1 >= 2is false → retry. The stored count becomes2. - Third nack:
retry_count = 2.2 >= 2is true → the message goes topgque.dead_letterinstead of the retry queue.
You have done the first nack. Run the block below twice to do the second and third. The first iteration redelivers with retry_count = 2; the second iteration sees 2 >= 2 and dead-letters the message:
do $$
declare
v_msg pgque.message;
begin
select * into v_msg from pgque.receive('orders', 'processor', 1) limit 1;
perform pgque.nack(v_msg.batch_id, v_msg, '0 seconds'::interval, 'still failing');
perform pgque.ack(v_msg.batch_id);
end $$;
select pgque.maint_retry_events();
select pgque.force_next_tick('orders');
select pgque.ticker();After the second iteration, receive returns nothing — the event has left the main queue for pgque.dead_letter. Inspect it:
select dl_id, dl_reason, ev_id, ev_retry, ev_data
from pgque.dlq_inspect('orders'); dl_id | dl_reason | ev_id | ev_retry | ev_data
-------+---------------+-------+----------+----------------------------------
1 | still failing | 2 | 2 | {"total": 10.00, "order_id": 43}
(1 row)
From here you have two options. Once the upstream bug is fixed, replay the event onto the queue — it re-enters with a fresh event id and is delivered on the next tick, and the DLQ row is removed:
select pgque.dlq_replay(1);Or empty the DLQ. dlq_purge deletes rows older than the interval you pass; '0 seconds' clears everything for the queue (the default is '30 days'):
select pgque.dlq_purge('orders', '0 seconds'::interval);Two observability functions report queue and consumer health; status() rolls them up.
select queue_name, ticker_lag, ev_new, last_tick_id
from pgque.get_queue_info('orders'); queue_name | ticker_lag | ev_new | last_tick_id
------------+--------------+--------+--------------
orders | 00:00:03.412 | 0 | 7
(1 row)
ticker_lag is the wall time since the last tick. If it grows without bound, the ticker is not running.
select queue_name, consumer_name, lag, last_seen, pending_events
from pgque.get_consumer_info('orders', 'processor'); queue_name | consumer_name | lag | last_seen | pending_events
------------+---------------+--------------+--------------+----------------
orders | processor | 00:00:03.510 | 00:00:01.500 | 0
(1 row)
lag is the age of the tick at the consumer's last finished position, not the time since its last ack. last_seen measures time since recorded consumer activity, such as opening or finishing a batch. Polling when no batch is available does not refresh it.
pending_events estimates the ticked backlog after the consumer's finished position. It subtracts that position's saved event sequence from the latest tick's sequence. It does not count events waiting for the next tick, and it is not an exact unread-row count: forced ticks and sequence gaps can inflate it. A zero value does not prove that no message is waiting. The queue's ev_new is the sequence-based estimate for activity after the latest tick.
These lag values are illustrative. With no intervening tick in this manual session, the consumer's position cannot be newer than the queue's latest tick; its lag is therefore at least the earlier queue-lag reading.
Interpret health against traffic and scheduler settings. An active queue with default thresholds normally ticks within about 3 seconds, plus scheduler delay. A healthy idle queue can back off toward ticker_idle_period (1 minute by default), so its ticker and consumer lag can exceed a few seconds. In this manual tutorial, lag grows until you drive the ticker again. For production alerts, compare cursor progress and backlog over time; see Monitoring.
select * from pgque.status(); component | status | detail
--------------+-------------+----------------------------------------------------------
postgresql | info | PostgreSQL 17.2 on ...
pgque | info | <installed development version>
scheduler | manual | ticker_job_id=NULL, maint_job_id=NULL, tick_period_ms=100 (10.00 ticks/sec)
ticker | stopped | not scheduled (tick_period_ms=100)
maintenance | stopped | not scheduled
pg_cron | unavailable | use pgque.start_timetable() for pg_timetable, or call ...
pg_timetable | unavailable | run pg_timetable against this database, then ...
queues | info | 1 queues configured
consumers | info | 1 active subscriptions
(9 rows)
status() is the one-stop health check. Here it reflects manual ticking: with no scheduler, the scheduler row reads manual and the ticker/maintenance rows read stopped. The displayed rate is calculated from the configured period; it is not a measurement of created ticks. With pg_cron installed and pgque.start() run, the scheduler row would show pg_cron with job ids and the ticker/maintenance rows would flip to scheduled.
The basic consumer above sees the whole stream. This final exercise combines the two SQL-first scaling features: producer idempotency prevents a retry from appending twice, while a partition key routes the accepted event to one leased slot. Events for that key follow event-ID order within each snapshot window. This is not global producer or commit order: a transaction that commits late with a lower event ID can appear in a later window.
Create every slot before producing. The atomic setup gives all four slots the same starting tick:
select pgque.create_queue('keyed_orders');
select pgque.subscribe_partitioned(
queue_name := 'keyed_orders',
consumer := 'workers',
n := 4
);Send the same logical effect twice with the same idempotency key:
select * from pgque.send_idem(
queue_name := 'keyed_orders',
type_name := 'order.created',
payload := '{"order_id": 99}'::jsonb,
idem_key := 'tutorial:order-99:create:v1',
ttl := interval '1 hour',
partition_key := 'customer-42'
);
select * from pgque.send_idem(
queue_name := 'keyed_orders',
type_name := 'order.created',
payload := '{"order_id": 99}'::jsonb,
idem_key := 'tutorial:order-99:create:v1',
ttl := interval '1 hour',
partition_key := 'customer-42'
);The first row has deduped = false; the second has deduped = true and the
same event_id. The TTL is a deduplication window for this effect, not a rate
limit. Different work needs a different key.
Calculate the destination slot, claim it with a process-instance worker id, then tick and receive. These are still separate psql autocommit transactions:
select ((hashtextextended('customer-42', 0) % 4 + 4) % 4)::int as customer_slot \gset
select pgque.claim_slot(
queue_name := 'keyed_orders',
consumer := 'workers',
slot := :customer_slot,
worker := 'tutorial-worker',
ttl := interval '2 minutes'
);
select pgque.force_next_tick('keyed_orders');
select pgque.ticker();
select msg_id, batch_id, type, payload, extra1 as partition_key
from pgque.receive_partitioned(
queue_name := 'keyed_orders',
consumer := 'workers',
slot := :customer_slot,
n := 4,
worker := 'tutorial-worker',
max_return := 500
);One row returns, not two. Capture and ack the same batch, then release the slot at the batch boundary:
select batch_id
from pgque.receive_partitioned(
queue_name := 'keyed_orders',
consumer := 'workers',
slot := :customer_slot,
n := 4,
worker := 'tutorial-worker',
max_return := 500
)
limit 1 \gset
select pgque.ack_partitioned(
queue_name := 'keyed_orders',
consumer := 'workers',
slot := :customer_slot,
n := 4,
worker := 'tutorial-worker',
batch_id := :batch_id
);
select pgque.release_slot(
queue_name := 'keyed_orders',
consumer := 'workers',
slot := :customer_slot,
worker := 'tutorial-worker'
);Production workers repeat that lifecycle across every slot. A stopped slot pins rotation, and each slot scans the full stream, so four slots mean roughly four times the reads. Leases use transactional rows and work through transaction-mode poolers, but the first-party clients do not yet implement the claim/rebalance loop. See Partition keys and Producer idempotency before deploying this pattern.
You have driven the ticker by hand all the way through. In production, select pgque.start(); schedules pg_cron to run the ticker and maintenance jobs. The default 100 ms interval is a check cadence, not a delivery guarantee. Queue thresholds still default to 500 events or 3 seconds since the last tick. The historical roughly 52 ms median used lowered thresholds and a fast-polling consumer, not default queue settings. See Latency and tick tuning before choosing settings for your workload.
To tear down the queue you built here:
select pgque.drop_queue('orders', true); -- administratively destroys the queue and subscriptionsTo remove PgQue entirely (drops the pgque schema; leaves the roles), see Installation & operations.
Next:
- Installation & operations —
pg_cronsetup, the production ticker cadence, roles, and teardown. - Latency & tick tuning — how
tick_period_mstrades latency against overhead. - Reference — public SQL signatures, return types, behavior, and role grants.
- Examples — patterns: fan-out, exactly-once consumption, batch loading, recurring jobs.
- Concepts — glossary of event, batch, tick, rotation, and the consumer loop.
- Monitoring — health metrics and what to alert on.
- Producer idempotency — key scope, TTL behavior, maintenance, and failure handling.
- Partition keys — worker leases, fencing, poolers, monitoring, read amplification, and recovery.