Use it when applications report deadlock errors.
Deadlocks
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.
1. Deadlock details: PG log (log_lock_waits), SHOW ENGINE INNODB STATUS LATEST DETECTED DEADLOCK, SQL Server system_health XE.
2. Which statements and objects?
3. Inconsistent lock order?
4. Missing index causing wide locks?
5. Add retry logic.
T1 locks A wants B ↔ T2 locks B wants A → detector aborts one
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Reproduce with two sessions updating rows in opposite order. Add consistent ordering (for example by primary key) and confirm the errors stop.
Step 2 Finding and preventing deadlocks Verify boundary
| Database | Where to look |
|---|---|
| MySQL | SHOW ENGINE INNODB STATUS\G → LATEST DETECTED DEADLOCK |
| PostgreSQL | Log (deadlock detected), log_lock_waits = on |
| SQL Server | system_health Extended Events (xml_deadlock_report) |
Prevent by accessing tables/rows in the same order, keeping transactions short, and indexing so updates lock fewer rows. Applications must catch the deadlock error and retry.
Step 3 Common failures Narrow the fault
Large batch updates in random order deadlock with each other. Missing indexes on foreign keys cause extra locking. Swallowing the error without retry loses the work.
Access tables and rows in a consistent order, keep transactions short and index foreign keys. Applications must retry the victim transaction.
SHOW ENGINE INNODB STATUS— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
The lock manager builds a wait-for graph and aborts a victim when it finds a cycle. Missing indexes make updates lock more rows than needed, raising deadlock chances. Foreign key checks also take locks on parent rows.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
A minimal deadlock
| Time | Session A | Session B |
|---|---|---|
| 1 | UPDATE accounts SET … WHERE id = 1; (locks row 1) | |
| 2 | UPDATE accounts SET … WHERE id = 2; (locks row 2) | |
| 3 | UPDATE … WHERE id = 2; → waits for B | |
| 4 | UPDATE … WHERE id = 1; → cycle → one session is rolled back |
The database picks a victim, rolls it back and returns an error; the other transaction continues. The victim's work is not lost if the application retries it.
Reading the InnoDB report
SHOW ENGINE INNODB STATUS\G
-- LATEST DETECTED DEADLOCK
-- *** (1) TRANSACTION … UPDATE accounts SET … WHERE id = 2
-- *** (1) HOLDS THE LOCK(S): … index PRIMARY of table `bank`.`accounts` … lock_mode X locks rec but not gap
-- *** (1) WAITING FOR THIS LOCK TO BE GRANTED: …
-- *** WE ROLL BACK TRANSACTION (2)
Note which index each lock is on: locks on a secondary index or locks gap before rec point at missing or non-unique indexes making the statement lock more rows than intended. Set innodb_print_all_deadlocks = ON to log every deadlock to the error log instead of only the latest.
PostgreSQL equivalent
The server log shows ERROR: deadlock detected with both processes' statements (DETAIL: Process 1234 waits for ShareLock on transaction …). log_lock_waits = on also logs waits longer than deadlock_timeout (1 s), which helps find the long waits that precede deadlocks.
Prevention that works
Access rows and tables in one consistent order (for example sort IDs before updating a batch), keep transactions short, index the columns in WHERE of UPDATE/DELETE, and retry on error 1213 / SQLSTATE 40P01.
Common mistakes
- Accessing tables in different orders in different transactions.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.