DEV Community

Cover image for My 8 Postgres queue workers processed jobs one at a time (86 jobs/s) until I added SKIP LOCKED (671 jobs/s)
Muhammad Adnan
Muhammad Adnan

Posted on

My 8 Postgres queue workers processed jobs one at a time (86 jobs/s) until I added SKIP LOCKED (671 jobs/s)

A jobs table is the simplest job queue there is, and plenty of backends run on one. Each worker claims the oldest pending job, works on it, marks it done:

BEGIN;
UPDATE jobs SET status = 'done', worker = $1, picked_at = clock_timestamp()
 WHERE id = (SELECT id FROM jobs
              WHERE status = 'pending'
              ORDER BY id
              LIMIT 1
              FOR UPDATE);
-- ... do the work (send the email, call the API) ...
COMMIT;
Enter fullscreen mode Exit fullscreen mode

FOR UPDATE is there so two workers can't take the same job. It does that. I measured what else it does.

The setup

PostgreSQL 16, a jobs table with 100,000 pending jobs and a partial index on pending ones. 8 workers (pgbench clients) run the transaction above in a loop for 30 seconds. Each job "works" for 10 ms inside the transaction. Then the same with one change: FOR UPDATE SKIP LOCKED. The scripts and full output are in the measurement folder for this post.

FOR UPDATE FOR UPDATE SKIP LOCKED
Jobs done in 30 s 2,594 20,132
Jobs per second 86 671
Average time per claim-and-finish 92.8 ms 11.9 ms
Fewest jobs done by one worker 1 2,512
Most jobs done by one worker 481 2,521

With 10 ms of work per job, 8 workers in parallel can finish at most about 800 jobs a second. SKIP LOCKED got 671. Plain FOR UPDATE got 86, which is what one worker working alone would do. Seven of the eight workers spent their time waiting.

Why the workers queue up

Every worker asks for the same thing: the pending job with the lowest id. The first worker locks it. The other seven find the same row, try to lock it, and wait for the first transaction to commit. When it does, the row isn't pending any more, so Postgres re-checks the condition, moves on to the next job, and now the waiting workers race for that one. One wins, the rest wait again.

So the workers line up behind each other's transactions, and the 10 ms of work happens one job at a time. Adding workers doesn't help; it just makes the line longer. And it isn't fair: in this run one worker finished 1 job in 30 seconds while another finished 481, because which waiter gets the lock next is down to timing.

The fix: skip rows other workers have locked

UPDATE jobs SET status = 'done', worker = $1, picked_at = clock_timestamp()
 WHERE id = (SELECT id FROM jobs
              WHERE status = 'pending'
              ORDER BY id
              LIMIT 1
              FOR UPDATE SKIP LOCKED);
Enter fullscreen mode Exit fullscreen mode

With SKIP LOCKED, a worker that finds a locked row doesn't wait; it takes the next unlocked one. Each worker gets its own job straight away, the 10 ms of work happens in parallel, and every worker did between 2,512 and 2,521 jobs.

Details that matter:

  • SKIP LOCKED gives up strict order. Jobs are started roughly oldest first, not exactly. If two jobs must run in sequence (two updates to the same account), handle that explicitly, for example with one queue per account or an advisory lock on the account id.
  • Keep the transaction short. Long work inside the transaction holds the row lock and, as in one of my earlier measurements, stops VACUUM cleaning up behind it. For slow jobs, claim the job in one short transaction (status = 'running', picked_at = now()), do the work outside it, and mark it done in another. Then add a sweeper that resets jobs stuck in running past a timeout, since a crashed worker will never finish them.
  • Index the pending jobs. A partial index like CREATE INDEX ON jobs (id) WHERE status = 'pending' keeps the "find the oldest pending job" lookup small as done jobs pile up.

How to spot it

  • Adding queue workers doesn't increase throughput.
  • pg_stat_activity shows several workers with wait_event_type = 'Lock' (wait_event = 'transactionid'), all waiting on the same transaction.
  • Some workers process far fewer jobs than others.
SELECT pid, wait_event_type, wait_event, now() - xact_start AS waiting_for, left(query, 60)
FROM pg_stat_activity
WHERE wait_event_type = 'Lock';
Enter fullscreen mode Exit fullscreen mode

Checklist

  • Queue claims use FOR UPDATE SKIP LOCKED.
  • Work that takes more than a few milliseconds runs outside the claiming transaction, with a timeout sweeper for crashed workers.
  • Jobs that must run in order are grouped explicitly, not left to ORDER BY id.
  • A partial index covers the pending jobs.

More measurements like this

I measure one Postgres behavior like this every few days. My Claude Code skills pack for TypeScript + GraphQL + Postgres backends turns lessons like these into rules Claude follows while it writes code: safe migrations, slow-query profiling, cursor pagination, resolver auth and more, plus a starter backend tested in CI against a real Postgres.

Top comments (0)