Reference · Cheat sheet

InnoDB locking & deadlocks

The one-page compressed essence: locks sit on index records; writes and locking-reads take them; cycles deadlock.

Lesson: 0006Context: InnoDB, MySQL 8.0+

The one idea

Locks are on index records

InnoDB locks index records, always. Which rows lock depends on which index the query walks — so an unindexed WHERE locks everything it scans.

The three lock types

LockCoversStops
recordone index recordupdate/delete of that row
gapgap between records (or before first / after last)inserts into the gap
next-keyrecord + gap before itboth — default for scans in RR

Who takes what

plain SELECTno locks — MVCC snapshot (0005)
SELECT … FOR SHARES (shared) — others can read, not write
SELECT … FOR UPDATEX (exclusive) — as if updating
UPDATE / DELETEX (exclusive) on rows walked

Deadlocks

A holds row1, wants row2 ┐ ├─ cycle → InnoDB detects → rolls back a VICTIM B holds row2, wants row1 ┘ victim gets ERROR 1213 (ER_LOCK_DEADLOCK)

Diagnose live locks (8.0)

SELECT * FROM performance_schema.data_locks; -- who holds what SELECT * FROM performance_schema.data_lock_waits; -- who waits on whom SHOW ENGINE INNODB STATUS\G -- last deadlock + transactions

Source: MySQL 8.0 Manual — InnoDB Locking · Deadlocks in InnoDB · All lessons