advancedTop 30 Scenario-Based Questions

How would you implement a job queue in PostgreSQL supporting multiple workers without losing or duplicating jobs?

CREATE TABLE job_queue (id BIGSERIAL PRIMARY KEY, payload JSONB, status VARCHAR(20) DEFAULT 'PENDING', created_at TIMESTAMP DEFAULT NOW(), started_at TIMESTAMP, worker_id TEXT); Worker claim query: BEGIN; SELECT id, payload FROM job_queue WHERE status = 'PENDING' ORDER BY created_at LIMIT 1 FOR UPDATE SKIP LOCKED; -- Only if row found: UPDATE job_queue SET status = 'PROCESSING', started_at = NOW()

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to A table has duplicate email rows due to a data quality issue. How do you delete duplicates keeping only the most recent?← Back to all SQL questions