Several workers grabbing the same job? Use SKIP LOCKED in PostgreSQL

With FOR UPDATE SKIP LOCKED, each worker takes a row nobody has locked and skips locked rows instead of waiting. That turns a plain PostgreSQL table into a job queue.

Published 3 min read#Web development#Developer tools

You have a jobs table and three workers reading from it, and suddenly the same email goes out twice, or the same order is counted twice. The usual cause is that the workers read the same row at the same moment. By the end of this article you will know how to make each worker take a job nobody else holds, with one SQL statement.

Why do workers pick the same job?

Workers pick the same job because each one reads the first pending row before another one has changed its status. This is the common shape:

SELECT id FROM jobs WHERE status = 'pending' ORDER BY id LIMIT 1;
-- the worker then runs:
UPDATE jobs SET status = 'running' WHERE id = 42;

There is a moment between the read and the update, and any other worker that reads during that moment finds the same row.

What is FOR UPDATE?

FOR UPDATE is a locking clause in SELECT, a part that locks rows as it gets them from the table. When it sits at the top level of the query, the locked rows are exactly the rows the query returns.

On its own it causes a second problem: with no extra option, the statement waits for other transactions to finish when a row is locked. So the second worker reaches the same row and waits, and the workers line up behind each other instead of working side by side.

What does SKIP LOCKED do?

SKIP LOCKED makes SELECT skip any row it cannot lock immediately, instead of waiting.

PostgreSQL's SELECT documentation says this option can be used to avoid lock contention when multiple consumers access a queue-like table.

BEGIN;

SELECT id, payload
FROM jobs
WHERE status = 'pending'
ORDER BY id
LIMIT 1
FOR UPDATE SKIP LOCKED;

-- do the work, then:
UPDATE jobs SET status = 'done' WHERE id = 42;

COMMIT;

The first worker locks row 42. The second skips it and takes the next one, and the third takes the one after that. None of them waits, and no two take the same row.

Can you take a job and change its status in one statement?

Yes. Put the SELECT inside an UPDATE, so one statement picks the row, locks it, changes its status and returns it:

UPDATE jobs
SET status = 'running', started_at = now()
WHERE id = (
  SELECT id FROM jobs
  WHERE status = 'pending'
  ORDER BY id
  LIMIT 1
  FOR UPDATE SKIP LOCKED
)
RETURNING id, payload;

If no row comes back, there are no free jobs right now, and the worker waits a little and asks again. To keep the query fast as the table grows, a partial index on rows where status = 'pending' helps, and you can see the difference with EXPLAIN ANALYZE.

What is the difference between SKIP LOCKED and NOWAIT?

The difference is that NOWAIT reports an error if it cannot lock a row immediately, while SKIP LOCKED skips the row and carries on. According to the same documentation, both stop the statement from waiting for other transactions to commit.

Option

When the row is locked

Fits

No option

Waits until the lock is released

Updating one specific row

NOWAIT

Reports an error at once

When you want to know the row is busy

SKIP LOCKED

Skips it and takes another

A queue with several workers

When should you not use it?

Do not use it for any query that needs a complete view of the data. The documentation is clear: skipping locked rows gives an inconsistent view, so it is not suitable for general purpose work like reports and totals.

The same documentation makes one more point: SKIP LOCKED applies only to row-level locks, and the ROW SHARE table-level lock is still taken in the ordinary way. So the option does not skip a lock on the table itself, and you should watch for any command that locks the table while it runs, like a plain index build (details in the article on building an index without a lock).

Where do you start today?

Open the code that pulls jobs in your project and check whether the read and the update are two separate statements. If they are, replace them with one UPDATE with FOR UPDATE SKIP LOCKED, and start a second worker next to the first. Does each one now take a different job?

FAQ

Should I use SKIP LOCKED for a report or a dashboard?

No. The PostgreSQL docs say that skipping locked rows gives an inconsistent view of the data, so it suits a queue, not general work.

If I want the statement to fail instead of skipping the row, what do I use?

NOWAIT. According to the PostgreSQL docs, the statement reports an error instead of waiting if it cannot lock a selected row immediately.

Does SKIP LOCKED lock the whole table?

No. It applies only to row-level locks, and the ROW SHARE table-level lock is still taken in the ordinary way.

Share

More articles