Pcornel
Databases · Transactions, MVCC & Locking

Deadlocks

Cheat sheet · https://pcornel.dk/topic.php?topic=6175

Back to the Topic

Answer

A deadlock is two or more transactions each waiting for a lock the other holds; the database detects it and aborts one as the victim.

Remember

Access tables and rows in a consistent order, keep transactions short and index foreign keys. Applications must retry the victim transaction.

First checks

  • SHOW ENGINE INNODB STATUS — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -S <server> -Q "SELECT @@VERSION" — SQL Server reachable and version

Commands

SHOW ENGINE INNODB STATUS\G

Verify in this order

T1 locks A wants B ↔ T2 locks B wants A → detector aborts one

Where it usually breaks

Large batch updates in random order deadlock with each other.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=6175

Confirmation

Confirm action

Continue?