Module 5 · Transactions and concurrency control
Episode 36 Deadlocks
▶ This video is coming to YouTube soon.
Two bank transfers lock rows in opposite orders and wait for each other forever. This episode builds the waits-for graph, shows how Postgres detects the cycle after deadlock_timeout and aborts one transaction, animates wait-die and wound-wait, and fixes it for good with consistent lock ordering. Proof: a real deadlock in Postgres with its DETAIL lines and SQLSTATE 40P01, then the ordered version committing.
Chapters
- 0:00Intro
- 0:15Two transfers, opposite directions
- 0:37A cycle in the waits-for graph
- 1:31A deadlock needs all four
- 1:57Detection: search after a timeout
- 2:43Wait-die: the young die
- 3:29Wound-wait: the old wound
- 4:06Four ways to deal with deadlocks
- 4:33The fix: lock rows in a consistent order
- 5:10The code: lock in id order, retry on 40P01
- 5:35Proof: a real deadlock, then none
- 7:11Recap
- 7:38Next: Timestamp Ordering and OCC