MySQL Locks, Blocking and Deadlocks
One transaction holds a row lock, another waits and dies with ERROR 1205. Then two transactions take the same rows in opposite order and InnoDB kills one with ERROR 1213 - and its report names both, the rows and the index.
Performance Guide 35 of 45 Advanced
- OSUbuntu 26.04 LTS
- MySQL8.4.10-0ubuntu0.26.04.1
- Test data400,000 rows, 2 core / 4 GB host
- TimeAbout 19 min
REPEATABLE READ is the MySQL default and is stricter than most databases' READ COMMITTED default. It holds more locks for longer, which makes both failures here more likely than on PostgreSQL or Oracle.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| db-a01 | 192.168.0.81 | Ubuntu 26.04 LTS | Primary / Source / Replica Set Member 1 | 2 Core | 4 GB | 50 GB |
This guide includes
Use this when a statement is hanging, or dying with ERROR 1205. This matters because blocking and deadlock are different things - one waits, the other is chosen and killed - and finding who blocks whom has to happen while it is still happening.
- reading the three settings that govern all of it
- blocking one transaction behind another, across two live connections
- finding who is blocking whom from a third session, while the wait is still live
- causing a real deadlock, which is not the same thing as blocking
- reading InnoDB's own account of the most recent one
Before you start
- The
customerstable. - Two concurrent connections - the transcript backgrounds them with
&.
-
Read the three settings that govern all of it
-
Block one transaction behind another
-
Find out who is blocking whom, while it is happening
-
Cause a real deadlock
-
Read InnoDB's own account of it
Official sources
- MySQL 8.4 Reference Manual - Optimizing Queries with EXPLAIN
- MySQL 8.4 Reference Manual - Obtaining Execution Plan Information
- MySQL 8.4 Reference Manual - Multiple-Column Indexes
- MySQL 8.4 Reference Manual - Skip Scan Range Access
- MySQL 8.4 Reference Manual - The Slow Query Log
- MySQL 8.4 Reference Manual - InnoDB Locking