How does a database like PostgreSQL resolve this?
T1: UPDATE acct SET bal = 0 WHERE id = 1; T2: UPDATE acct SET bal = 0 WHERE id = 2; T1: UPDATE acct SET bal = 0 WHERE id = 2; -- waits T2: UPDATE acct SET bal = 0 WHERE id = 1; -- waits
- Both commit; the last write wins
- It merges the two transactions
- It aborts one of them as victim
- Both wait forever until restart
Each holds a lock the other needs: a deadlock. The detector cancels one transaction so the other can proceed.
Play SQL & Databases ▸