Two PostgreSQL sessions issue SELECT ... FOR UPDATE on overlapping but different individual rows of the same table, in opposite orders. Both then try to lock the other's row. What happens at the engine level?
-- Session A | -- Session B
BEGIN; | BEGIN;
SELECT ... WHERE id=1 |
FOR UPDATE; | SELECT ... WHERE id=2
| FOR UPDATE;
SELECT ... WHERE id=2 |
FOR UPDATE; -- waits | SELECT ... WHERE id=1
| FOR UPDATE; -- waits