Databases · Senior · Question 11 of 75

How can you build a job queue on PostgreSQL that many workers poll safely?

SELECT id FROM jobs
WHERE status = 'queued'
ORDER BY id
FOR UPDATE SKIP LOCKED
LIMIT 1;

Explanation

Libraries like pg-boss and graphile-worker use this. It works well for moderate throughput and keeps jobs transactional with your data.

Further reading

Problem with a question

Databases · Senior · question 11

Test yourself

Take the Databases · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz