What is a deadlock?
A deadlock occurs when transactions form a cycle of lock waits, so none can continue. The database stops one transaction to break the cycle.
How a deadlock forms
Section titled “How a deadlock forms”Consider two transactions that change the same rows in a different order:
| Transaction | Lock already held | Lock requested next |
|---|---|---|
| A | Row 1 | Row 2 |
| B | Row 2 | Row 1 |
A needs B to release row 2, while B needs A to release row 1. A single query waiting for another transaction is a lock wait; it becomes a deadlock when the waits form a cycle.
Recognize the error
Section titled “Recognize the error”PostgreSQL reports 40P01; MySQL and MariaDB report error 1213.
TableAI identifies these as Deadlock Detected.
See SQLSTATE for the difference between an SQLSTATE and a database error number.
Use the database-specific recovery guide before retrying:
For current blocking activity, follow diagnose database performance. For a query that waits or times out, use slow query troubleshooting.