Skip to content

Instantly share code, notes, and snippets.

@ismasan
Created December 5, 2024 14:24
Show Gist options
  • Save ismasan/07dcd892da7b9206c9cdead49f52eacd to your computer and use it in GitHub Desktop.
Save ismasan/07dcd892da7b9206c9cdead49f52eacd to your computer and use it in GitHub Desktop.
Postgres-based job queue using FOR UPDATE SKIP LOCKED
WITH next_job AS (
SELECT
id,
type,
payload,
created_at
FROM jobs
WHERE created_at <= ?
ORDER BY created_at
FOR UPDATE SKIP LOCKED
LIMIT 1
)
DELETE FROM jobs
WHERE id IN (SELECT id FROM next_job)
RETURNING *;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment