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()
This is a Pro chapter
Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.
How would you implement a job queue in PostgreSQL supporting multiple workers without losing or duplicating jobs?