Locking A Row That Is Not There
Seat holds are created like this, at READ COMMITTED. seat_holds has no unique constraint, and there is a permanent row in seats for every seat.
BEGIN;
SELECT 1 FROM seat_holds WHERE seat_id = 42 FOR UPDATE;
-- 0 rows
INSERT INTO seat_holds (seat_id, user_id, held_until)
VALUES (42, $1, now() + interval '10 minutes');
COMMIT;
Two requests for seat 42 arrive three milliseconds apart and both succeed. Which changes actually prevent the double hold? More than one applies.
Sign in to answer questions and track your progress
Sign In