Error Reference › SQL / Database
SQL / Database

Deadlock found

Two transactions each held a lock the other needed, so the database killed one to break the tie.

What it looks like

ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

What does "Deadlock found" mean?

Returned by the database (InnoDB in MySQL; PostgreSQL has its own “deadlock detected”) when two transactions each wait for a lock the other holds. The database picks one to roll back so the other can finish — the rolled-back one simply needs running again.

What causes "Deadlock found"?

  • Transactions updating the same rows in different orders.
  • Long transactions holding locks.
  • High write concurrency.

How do I confirm the cause?

  1. In MySQL, run SHOW ENGINE INNODB STATUS and read the LATEST DETECTED DEADLOCK section — it shows both queries and the locks involved.
  2. Look for code paths that update the same tables or rows in different orders.
  3. Check for long transactions that hold locks while doing slow work such as API calls.

How do I fix "Deadlock found"?

  • Always access rows in a consistent order.
  • Keep transactions short.
  • Retry the deadlocked transaction (it's expected under load).

How do I stop it happening again?

  • Access tables and rows in the same order in every transaction, and keep transactions short.
  • Wrap write transactions in a small retry loop — occasional deadlocks are normal under heavy load.
  • Index the columns used in UPDATE and DELETE conditions, so each statement locks fewer rows.
Got a different error? Paste it into the Explain This Error tool → Identifies the family and the fix — runs locally, nothing uploaded.

Related errors