Skip to content

Instantly share code, notes, and snippets.

@up1
Created January 18, 2026 09:00
Show Gist options
  • Select an option

  • Save up1/4782f5471c938cec8ca1108d750d6156 to your computer and use it in GitHub Desktop.

Select an option

Save up1/4782f5471c938cec8ca1108d750d6156 to your computer and use it in GitHub Desktop.
Postgresql :: Skip Locked
WITH cte AS (
SELECT id FROM tasks WHERE status = 'pending' ORDER BY created_at ASC LIMIT 1 FOR UPDATE SKIP LOCKED
)
UPDATE tasks SET status = 'in_progress'
FROM cte
WHERE tasks.id = cte.id
RETURNING tasks.*;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment