Fill the blank
Let several workers each claim a different pending job without waiting on each other.
CREATE TABLE jobs (
id bigserial PRIMARY KEY,
state text NOT NULL DEFAULT 'pending'
);
INSERT INTO jobs (state) VALUES ('pending'), ('pending');
BEGIN;
SELECT id FROM jobs
WHERE state = 'pending'
ORDER BY id
LIMIT 1
FOR UPDATE ____;
COMMIT;Hint 1 of 1
You want to move past rows other workers hold.
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A SKIP LOCKED
Correct. A second worker skips the row this one holds and takes the next.
FOR UPDATE locks the chosen row, and SKIP LOCKED tells other workers to pass over rows already locked instead of waiting. Each worker ends up with a different job.
Why not the others?
- B
NOWAIT. Fails with an error instead of moving on to the next job. - C
OF jobs. Names the table to lock; workers still queue on the same row. - D
SHARE. Not valid here, and a shared lock lets two workers claim the same job.