Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

Deadlocks

Direct 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.

When to use this

Use it when applications report deadlock errors.

First 30 seconds

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.

Operational verification

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

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it when applications report deadlock errors.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

3 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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
DatabaseWhere to look
MySQLSHOW ENGINE INNODB STATUS\G → LATEST DETECTED DEADLOCK
PostgreSQLLog (deadlock detected), log_lock_waits = on
SQL Serversystem_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.

Key points

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

Key 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
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

TimeSession ASession B
1UPDATE accounts SET … WHERE id = 1; (locks row 1)
2UPDATE accounts SET … WHERE id = 2; (locks row 2)
3UPDATE … WHERE id = 2; → waits for B
4UPDATE … 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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
Terminology layer

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.

Aliases that resolve here
deadlocksdatabase deadlockinnodb status deadlock
Confirmation

Confirm action

Continue?