Deadlock found when trying to get lock; try restarting transaction
Two transactions waited on each other’s locks; the database killed one so the other could continue.
Meaning
Deadlocks are normal in busy OLTP systems. The database picks a victim and rolls it back (MySQL 1213, PostgreSQL 40P01, SQL Server 1205). Applications should retry the victim transaction — and reduce how often it happens.
Common causes
- Transactions updating the same rows in different orders
- Long transactions holding locks
- Missing indexes causing range/gap locks on many rows
- Bulk updates competing with OLTP writes
⚡ Quick fix
- Retry the transaction (with small backoff) on deadlock errors
- Access tables/rows in a consistent order
- Keep transactions short
- Add indexes so updates lock fewer rows
Detailed fix by platform
MySQL
SHOW ENGINE INNODB STATUS\G→ LATEST DETECTED DEADLOCK shows both queries.
PostgreSQL
- Enable
log_lock_waitsand check the server log for the deadlock detail.
How to diagnose
- Queries — Which two statements deadlocked?
- Order — Do they lock rows in different orders?
- Indexes — Are WHERE clauses indexed?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026