Several workers poll a PostgreSQL table for jobs. Two workers sometimes try to run the same job, and polling slows down as completed jobs accumulate. Correctness and performance need separate evidence: first explain who can claim a row, then learn how the database finds it.
Claim work under an explicit transaction boundary
1. Why is selecting the first ready job not a claim?
Two sessions can both see the same ready row before either updates it. Lock a candidate and update its state in the same transaction. The example uses one PostgreSQL statement, which can run as its own short transaction in autocommit. The worker should not start external work before the claim commits.
WITH candidate AS (
SELECT id FROM jobs
WHERE status = 'ready'
ORDER BY created_at, id
LIMIT 1
FOR UPDATE SKIP LOCKED
)
UPDATE jobs AS job
SET status = 'running'
FROM candidate
WHERE job.id = candidate.id
RETURNING job.id;
2. What does SKIP LOCKED give up?
Instead of waiting for a locked candidate, a worker can take another one. That is useful for consumers of a queue-like table but does not guarantee strict FIFO ordering: another worker may be claiming the oldest job while this worker moves on. An empty result can mean there is no immediately claimable row, not that the table contains no ready jobs. This is not a suitable general-purpose read strategy for reports.
3. What if the worker crashes after it commits the claim?
Row locks end with the transaction; the committed running status remains. Add a recovery contract, such as a stored lease deadline and claim token. A worker finishing after its lease expires should not overwrite a newer claimant's result. The sample statement only demonstrates claiming and does not implement leases. If work has external side effects, a database row lock is not an exactly-once guarantee for those effects.
Find the work without scanning it all
4. Why might a partial index fit this query?
A partial index on (created_at, id) WHERE status = 'ready' can serve this ordering while excluding completed jobs. PostgreSQL must be able to infer that the query implies the predicate at planning time; a generic prepared plan with status = $1 cannot assume that every execution requests ready jobs. The index also changes as jobs enter and leave the ready state, so inspect its write cost under queue churn.
5. What can EXPLAIN ANALYZE prove?
It runs the statement and reports what happened on that execution. Check whether the estimated row counts align with observed rows, how many loops each node ran, and where buffers are hit or read. Try fixtures with different ready-to-completed ratios. Use an isolated dataset for an updating statement; a plan captured without worker contention cannot tell you how much contention production workers will see.
Practice in two sessions
- Hold a lock on the oldest row and predict the result of a second session's claim.
- Compare the plan before and after the partial index. Name one reason a small table might still favor a sequential scan.
- Describe crash recovery without assuming jobs execute exactly once.
For SQL result shape rather than queue locking, read the joins and windows walkthrough. Use Mentaro's free practice demo to practice explaining the transaction boundary aloud.