DEADLOCKS IN THE DATABASE
Two UPDATEs, opposite order, and the detector picks a victim
Two transactions each update two rows, taking a row lock on each until COMMIT. T1 holds row 1 and waits for row 2. T2 holds row 2 and waits for row 1. What happens next?
An UPDATE takes an exclusive lock on the row it changes and holds it until COMMIT. Two transfers between the same accounts in opposite directions each lock their first row, then wait for the other’s. That is the lock-order deadlock from threads, with rows as the locks.
The database watches for it. After a short wait (PostgreSQL’s deadlock_timeout is 1 s; InnoDB checks at once) a detector looks for a cycle in the wait-for graph and aborts one transaction, the victim. The other proceeds. Your application gets an error and must retry. A wait with no cycle is not a deadlock, and the detector leaves it alone.
-- T1: transfer 10 from 1 to 2 -- T2: transfer 25 from 2 to 1
BEGIN; BEGIN;
UPDATE accounts SET balance = balance - 10 WHERE id = 1;
UPDATE accounts SET balance = balance - 25 WHERE id = 2;
UPDATE accounts SET balance = balance + 10 WHERE id = 2; -- waits for T2
UPDATE accounts SET balance = balance + 25 WHERE id = 1; -- waits for T1
-- ERROR 40P01 deadlock detected (one of them, after deadlock_timeout)-- Touch rows in one global order (by primary key), in every code path.
BEGIN;
UPDATE accounts SET balance = balance + d1 WHERE id = least(a, b);
UPDATE accounts SET balance = balance + d2 WHERE id = greatest(a, b);
COMMIT;
-- Or take every lock first, in key order, then do the work:
SELECT * FROM accounts WHERE id IN (a, b) ORDER BY id FOR UPDATE;
-- Either way: still catch 40P01 / 1213 and retry. Some other path will deadlock you. Watch the WAIT-FOR GRAPH. In DEADLOCK the second wait closes the cycle and the strip turns red until the detector aborts T2. In SLOW, NOT DEADLOCKED there is only one edge: the detector runs, finds nothing, and T2 waits past the 2000 ms budget anyway. Both fixes run the same opposite-direction transfers with no cycle and no abort.
T1 updates row 1 then row 2. T2 updates row 2 then row 1. Interleave their statements so each one is waiting for a row the other holds.
GOAL: a cycle appears in the wait-for graph
Two transactions transfer money between the same two rows in opposite directions at the same time. Rewrite transfer so it can never deadlock. The grader interleaves them at every lock and checks balances.
Transfers, inventory moves, any job that updates several rows of the same table from different code paths. Symptoms: PostgreSQL ERROR: deadlock detected (SQLSTATE 40P01), MySQL error 1213, and a SHOW ENGINE INNODB STATUS section titled LATEST DETECTED DEADLOCK.
“Two transactions deadlock in production. How do you find and fix it?” Read the deadlock report for the two statements and the rows, explain the opposite-order cycle, fix it with a consistent key order or SELECT … ORDER BY id FOR UPDATE, and add a bounded retry for the error you can never fully rule out. Contrast it with a plain lock wait, which the detector will not touch.
USED IN
Chapters that build on this lesson.