The idea · Lab 2.7.3A naive queue query — SELECT the oldest queued row, mark it processing — has an obvious race: two workers running that same query at nearly the same instant can both select the same row before either has marked it taken. Adding FOR UPDATE fixes the double-pick but introduces a new problem: the second worker now waits, blocked, for the first worker's entire job to finish, single-filing what was supposed to be parallel work. SKIP LOCKED is the missing piece: a worker whose candidate row is already locked by someone else does not wait for it at all — it simply moves on to the next available row, as if the locked one did not exist.
This lab proves the no-collision guarantee directly: one worker holds a job mid-processing while a second, genuinely concurrent worker asks for work at the same moment, and picks a completely different job with no wait and no error. Then it contrasts this with FOR UPDATE NOWAIT, which fails loudly instead of skipping quietly — a different tool for a different situation.
The Full Dequeue Pattern
BEGIN;
SELECT id FROM jobs WHERE status = 'queued' ORDER BY id FOR UPDATE SKIP LOCKED LIMIT 1;
-- ... do the actual work here ...
UPDATE jobs SET status = 'done' WHERE id = <that id>;
COMMIT;
Everything from the SELECT to the final UPDATE lives in one transaction: if the worker crashes mid-job, the row lock (and the whole transaction) is released automatically on disconnect, and the job goes back to being genuinely queued for the next worker to pick up — no job is ever silently lost.
SKIP LOCKED vs NOWAIT
-- SKIP LOCKED: silently move past a row someone else already has
SELECT id FROM jobs WHERE status = 'queued' ORDER BY id FOR UPDATE SKIP LOCKED LIMIT 1;
-- NOWAIT: fail loudly and immediately if the row is already locked
SELECT id FROM jobs WHERE status = 'queued' ORDER BY id FOR UPDATE NOWAIT LIMIT 1;
Both are non-blocking — neither one ever makes a session wait the way plain FOR UPDATE would. The difference is what happens next: SKIP LOCKED quietly tries a different row, which is exactly what a job-queue worker wants. NOWAIT raises an error and aborts the transaction, which is the right behavior when a lock conflict means something is genuinely wrong and should stop the caller rather than being silently routed around.
Key concepts
FOR UPDATE SKIP LOCKED
Modifies a row-locking SELECT to silently exclude any row that another session already has locked, rather than waiting for it or erroring. Combined with LIMIT 1 and an ORDER BY, it turns "give me one thing to work on" into a query that never collides with any other session running the identical query at the same moment.
FOR UPDATE NOWAIT
Also non-blocking, but with the opposite reaction to a lock conflict: it immediately raises could not obtain lock on row and aborts the transaction, rather than trying a different row. Appropriate when encountering a lock at all is itself the meaningful signal — for example, enforcing that only one process may touch a specific row, with any conflict treated as an error condition to surface.
What to remember
- FOR UPDATE SKIP LOCKED silently excludes rows another session already has locked, rather than waiting for them or erroring
- A worker using SKIP LOCKED never waits at all — a locked row is invisible to it, not a source of delay
- The SELECT, the real work, and the final status UPDATE all belong in one transaction, so the row's lock lasts exactly as long as the claim is valid
- FOR UPDATE NOWAIT is non-blocking like SKIP LOCKED, but reacts to a lock conflict with a hard error instead of trying a different row
- Choose SKIP LOCKED when a lock conflict is routine and expected (a job queue); choose NOWAIT when a lock conflict is itself a meaningful event that should stop the caller