Select For Update Skip Locked
Claim a batch of jobs from a queue by locking only unlocked rows, letting concurrent workers drain the queue without blocking each other.
Code
WITH claimed AS (
SELECT id
FROM jobs
WHERE status = 'pending'
ORDER BY created_at
LIMIT 10
FOR UPDATE SKIP LOCKED
)
UPDATE jobs
SET status = 'processing', started_at = now()
WHERE id IN (SELECT id FROM claimed)
RETURNING id, payload;Report a problem with this exampleOpens a pre-filled issue on GitHub.