message TEXT, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, completed_at TIMESTAMPTZ NULL ); CREATE INDEX idx_job_created_at ON job (created_at); CREATE INDEX idx_job_completed_at ON job (completed_at);
completed_at is NULL ORDER BY created_at LIMIT 1 FOR UPDATE SKIP LOCKED; UPDATE job SET completed_at = $1 WHERE id = $2; COMMIT; すでに行ロックが獲得されている行は 解除待ちせずに飛ばす