One query doing nothing but pg_sleep cut my test queue from 3,992 jobs a second to 852 in 45 seconds. Nothing errored and nothing was blocked: each worker was reading 1,551 buffers to find its next job instead of 12.
Most guides to a Postgres job queue stop at FOR UPDATE SKIP LOCKED and a partial index. That part takes ten minutes. The hard parts are which claim model you pick, what MVCC does to the head of your index, and a commit-time lock in NOTIFY that PostgreSQL 19 still takes. I ran each of them on PostgreSQL 18.6 (Docker on a 2-vCPU, 2 GiB colima VM, default config, pgbench), so the absolute numbers are small. The ratios are the point.
How Does FOR UPDATE SKIP LOCKED Work in a Postgres Job Queue?
The test table is the usual shape, with one partial index that only holds runnable jobs:
CREATE TABLE jobs (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
queue text NOT NULL DEFAULT 'default',
state text NOT NULL DEFAULT 'available',
priority smallint NOT NULL DEFAULT 1,
scheduled_at timestamptz NOT NULL DEFAULT now(),
attempt int NOT NULL DEFAULT 0,
lease_until timestamptz,
args jsonb NOT NULL DEFAULT '{}'
);
CREATE INDEX jobs_fetch ON jobs (queue, priority, scheduled_at, id)
WHERE state = 'available';
A worker claims the first row nobody else has locked:
SELECT id FROM jobs
WHERE state = 'available' AND queue = 'default' AND scheduled_at <= now()
ORDER BY priority, scheduled_at, id
LIMIT 10
FOR NO KEY UPDATE SKIP LOCKED;
The plan is Limit → LockRows → Index Scan using jobs_fetch, and on a freshly vacuumed table it touched 10 buffers for 10 rows. state = 'available' still appears as a Filter even though the index predicate already implies it: locking queries keep the qual so Postgres can recheck a row that changed while it waited.
The docs are blunt that skipping rows "provides an inconsistent view of the data". For a queue that is the feature. With 8 workers each holding the lock through a 10 ms job, SKIP LOCKED ran 478 jobs a second. The same script with plain FOR NO KEY UPDATE ran 56, because every worker queued behind whoever held the head row.
Two things follow. First, there is no FIFO guarantee: job 1,001 can finish before job 1,000. If two jobs touch the same account, order them yourself with an advisory lock or by routing on account ID, as I described for outbox relays. Second, the lock strength matters if anything has a foreign key to jobs. FOR UPDATE conflicts with the KEY SHARE lock a child insert takes, so writing an attempt-log row that references a claimed job will wait. FOR NO KEY UPDATE does not. pg-boss's source has a comment about exactly this on its queue table.
Lease vs Held Transaction: Which Claim Model Is Safer?
There are two ways to own a job.
Held transaction.BEGIN, claim with SKIP LOCKED, do the work, mark it done, COMMIT. If the worker dies, the transaction rolls back and the row is available again. No sweeper, no expiry tuning.
Lease. Claim and commit in one statement, run the job outside any transaction, then complete it in a second one:
WITH next AS (
SELECT id FROM jobs
WHERE state = 'available' AND queue = 'default' AND scheduled_at <= now()
ORDER BY priority, scheduled_at, id
LIMIT 1
FOR NO KEY UPDATE SKIP LOCKED
)
UPDATE jobs j
SET state = 'running', attempt = j.attempt + 1,
lease_until = now() + interval '5 minutes'
FROM next WHERE j.id = next.id
RETURNING j.id, j.attempt;
-- later, after the side effect
UPDATE jobs SET state = 'completed', lease_until = NULL
WHERE id = $1 AND attempt = $2; -- fencing: a rescued, re-run job wins
On trivial jobs the two are close once there is any concurrency:
| Workers | Held transaction (jobs/s) | Lease (jobs/s) |
|---|---|---|
| 1 | 4,594 | 2,596 |
| 8 | 8,635 | 8,701 |
| 32 | 8,997 | 7,916 |
The difference that matters is not speed. A held-transaction worker that claims a row has been assigned a transaction ID, and Postgres cannot clean up any row version deleted after that ID until it commits. I left one worker idle in its transaction (as if waiting on a slow bank API) while 8 others ran the lease workload for 10 seconds. VACUUM VERBOSE then reported 59,130 dead tuples "not yet removable", and throughput was 2,956 jobs a second against 8,167 without the idle worker. pg_stat_activity showed a backend_xid and no backend_xmin, so tools that only watch for old snapshots will miss it.
My position, for payments specifically: use leases, as every library in the comparison below already does. Payout, refund and webhook jobs call PSPs and banks, and those calls take anything from 200 ms to a 30-second timeout. Holding a database transaction across a network call is the bug, whatever the queue. The lease costs you two things you should be building anyway: an expiry sweeper, and idempotency on the downstream call, because a lease that expires mid-call means a second worker will make it again. Send the job ID plus attempt as the PSP idempotency key and check attempt on completion, as above. The same consumer-side discipline is in my inbox pattern guide.
Why Does a Postgres Job Queue Slow Down Over Time?
Every claim moves a row out of the partial index's predicate, but the old index entry stays until something removes it. Those entries pile up at the exact place every worker starts scanning: the head of the queue.
When a dead entry is dead to every transaction, the first index scan to visit it marks it killed and later scans skip the heap visit. They still read the leaf page. After a 45-second baseline run with 600,000 queued jobs, a single claim touched 583 buffers and took 0.82 ms. After a manual VACUUM the same query touched 12 buffers and took 0.049 ms.
If anything holds an old snapshot, the entries cannot be killed at all, and every claim visits the heap for each one. I started SELECT pg_sleep(600) in another session and reran the same workload:
| Seconds | 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 |
|---|---|---|---|---|---|---|---|---|---|
| Jobs/s | 3,992 | 1,996 | 1,543 | 1,310 | 1,139 | 1,051 | 957 | 895 | 852 |
Latency went from 2.0 ms to 9.4 ms. A lone claim at the end read 1,551 buffers. Brandur Leach described this failure on Heroku Postgres in 2015, with 247,311 dead row versions that could not be removed. PostgreSQL 14's bottom-up B-tree deletion doesn't rescue you here, because it can only remove entries already dead to everyone, which is exactly what an old snapshot prevents.
One result surprised me. The same pg_sleep run in a different database on the same server held nothing back: 0 tuples not removable, 8,167 jobs a second. For ordinary tables, the cleanup horizon only counts backends connected to the same database. Replication slots and hot_standby_feedback from a replica still apply cluster-wide, so a long report on a standby can do this to your primary.
The guardrails: set transaction_timeout (PostgreSQL 17+) and idle_in_transaction_session_timeout on every application role, and alert on the age of backend_xmin and backend_xid in pg_stat_activity and of xmin in pg_replication_slots.
How to Tune Autovacuum for a Postgres Queue Table
Autovacuum fires at 50 dead tuples plus 20% of the table. For a 600,000-row queue that is about 120,000 dead tuples, and every claim walks the leaf pages their index entries occupy. PostgreSQL 18 added autovacuum_vacuum_max_threshold, but its default is 100 million, which no queue table reaches. Set it per table instead:
ALTER TABLE jobs SET (
autovacuum_vacuum_scale_factor = 0,
autovacuum_vacuum_threshold = 5000,
autovacuum_vacuum_cost_delay = 0
);
Don't expect HOT updates to save you. Of 542,508 updates in my baseline run, n_tup_hot_upd was 0. A column used in an index predicate counts as indexed, so every change to state writes a new heap tuple and a new primary-key entry, whatever the fillfactor. The cheapest row is one that isn't there. Delete completed jobs, or partition by day and TRUNCATE old partitions, rather than keeping millions of completed rows in the same heap your workers scan.
Does LISTEN/NOTIFY Scale for a Postgres Job Queue?
Waking workers with NOTIFY looks free. It isn't. At commit, any transaction that issued NOTIFY takes a heavyweight lock on object 0 of pg_database, shared by every database on the server, and holds it until the commit is done. That forces those commits through one at a time.
Recall.ai hit this in March 2025 with tens of thousands of concurrent writers, logging lock waits around 1,000 ms. Their post was updated in May 2026 to say a Postgres commit had removed the bottleneck. That work shipped in 19.0 on 19 November 2026, and the release notes describe it as waking only the backends listening on a channel. The REL_19_0 tag's async.c still takes the lock in PreCommit_Notify, with the comment "Serialize writers by acquiring a special lock that we hold till after commit". DBOS reached the same conclusion in July.
My enqueue test, 16 clients inserting one job per transaction:
| Enqueue transaction | Jobs/s, run 1 | Jobs/s, run 2 |
|---|---|---|
INSERT only | 29,234 | 29,757 |
INSERT + NOTIFY jobs_available | 10,814 | 10,876 |
That is a 63% drop on a VM whose fsync is cheap. On real disks, where group commit does more of the work, I'd expect the gap to widen. Poll every 100 ms to 1 s instead, or send one debounced NOTIFY from a single process rather than one per enqueue.
River vs Graphile Worker vs pg-boss vs Oban vs pgmq
I read the claim SQL in each library's current release. All of them use FOR UPDATE ... SKIP LOCKED, none use FOR NO KEY UPDATE on the job row, and every one commits the claim before running the job. Nobody ships the held-transaction model. They differ on defaults, and the defaults decide how long a payout sits stuck after a worker dies.
| Library | Stuck-job recovery | Default attempts, backoff | Wake-up | Completed jobs |
|---|---|---|---|---|
| River 0.51 (Go) | Rescuer after 1 h | 25, errors⁴ s ±10% | NOTIFY + 1 s poll | Kept 24 h |
| Graphile Worker 0.18 (Node) | locked_at older than 4 h, hard-coded | 25, e^min(n,10) s | NOTIFY + 2 s poll | Deleted at once |
| pg-boss 12.40 (Node) | Expires after 15 min | 2 retries, no delay | 2 s poll, NOTIFY opt-in | Kept 7 days |
| Oban 2.24 (Elixir) | Lifeline after 60 min, opt-in | 20, 15 + 2ⁿ s | NOTIFY + 1 s stager | Forever unless Pruner is on |
| Solid Queue 1.7 (Rails) | Heartbeat lost for 5 min, then marked failed | Active Job retry_on only | 0.1 s poll | 1 day |
| pgmq 1.13 (extension) | Visibility timeout you pass per read | None built in | Poll, NOTIFY opt-in | Until delete() or archive() |
Four defaults catch payment teams out. Graphile Worker will not hand a crashed worker's job to anyone else for four hours, a long wait for a refund. River's JobTimeout defaults to one minute, shorter than some bank APIs take on a bad day. pg-boss retries twice with no delay, so a ten-second PSP outage exhausts it. And Solid Queue, the Rails 8 default, installs into a separate queue database, so perform_later inside your payment transaction is not atomic with it until you point it at the main database. Its jobs held by a process that dies are marked failed, not retried.
Postgres Job Queue vs SQS for Payment Jobs in 2026
The case for Postgres rests on one property SQS cannot give you: the job commits or rolls back with the business write. Insert the payout row and its send_payout job in one transaction and there is no window where one exists without the other. To get that with SQS you need an outbox table, which is a Postgres queue plus a relay.
| Postgres lease queue | Amazon SQS standard | |
|---|---|---|
| Enqueue atomic with the business write | Yes, same transaction | No, needs an outbox |
| Delivery | At least once (lease expiry) | At least once |
| Claim timeout | Your lease_until | Visibility timeout: 30 s default, 12 h max |
| Retention | Until you delete it | 4 days default, 14 days max |
| Payload | Keep it to IDs | 1 MiB max since August 2025 |
| Ordering | None under SKIP LOCKED | None (FIFO queues order per group) |
| What scale costs you | WAL, vacuum and IO on your primary | Per request |
Both are at-least-once, so the idempotency work is identical. SQS's 12-hour cap counts from first receipt and extending the timeout doesn't reset it, which matters for slow batch payouts.
My view: start on Postgres and stay there longer than the "use a real queue" advice suggests. 100 million payouts a year averages about 3 a second. Graphile Worker's own docs only suggest looking elsewhere above 5,000 jobs a second, and Oban measured 17,699 jobs a second on a 10-core laptop back in 2022. What should move you is blast radius, not throughput. When queue churn shows up in your ledger's p99 latency, through WAL volume or vacuum IO, move the queue to its own cluster first and to SQS or Kafka second.
What This Means for Payments Engineers
1. Pick leases for anything that calls out. Fence completion on attempt, and pass job_id:attempt as the downstream idempotency key.
2. Index the claim, not the table. One partial index matching your WHERE and ORDER BY, with state in the predicate. Check the plan says LockRows over an index scan.
3. Use FOR NO KEY UPDATE if any table has a foreign key to jobs.
4. Tune autovacuum per table with a fixed threshold, and delete or partition completed jobs.
5. Cap transaction lifetimes with transaction_timeout and idle_in_transaction_session_timeout, and alert on backend_xid age as well as backend_xmin.
6. Keep reporting off the queue's database, and keep hot_standby_feedback off replicas that run long queries.
7. Don't NOTIFY per enqueue. Poll, or debounce.
I write about the database plumbing under payment systems at Tom Wang. The benchmarks above are small-machine numbers, so rerun the scripts on your own hardware before you quote them in a design review.