InnoDB Gap Locks and Next-Key Locks

InnoDB row locks attach to index records, not to the rows themselves. InnoDB can also lock the gap between two records. A locked gap stops other transactions from inserting new rows into it, so reading the same range again returns no new rows (phantom prevention). Under Repeatable Read, the default isolation level, locking reads (SELECT ... FOR UPDATE, UPDATE, DELETE) use next-key locks, which lock a record together with the gap before it.

Key Specifications

LockWhat it locksPurpose
Record lockOne index recordModifying a row, or finding one row through a unique index
Gap lockThe gap between records (not the records themselves)Blocks inserts into that gap. Gap locks do not conflict with each other
Next-key lockA record + the gap before itDefault for range searches under Repeatable Read
Insert intention lockThe gap an insert targets (a kind of gap lock)Concurrent inserts at different positions in the same gap are allowed; waits if a gap lock exists
Table intention lockIS and IX on the tableMarks the table before row locks are taken
ItemValue
Lock wait limitinnodb_lock_wait_timeout, default 50 seconds. When exceeded, only the statement fails (the transaction remains)
DeadlockOn detection, rolls back one of the transactions (innodb_deadlock_detect, on by default)
Recording methodOne bitmap per page records the lock state of multiple records on that page
EscalationNone

How It Works

Figure. Records and gaps in an index

block-beta
  columns 9
  g1["(−∞, 10)"] r1["10"] g2["(10, 20)"] r2["20"] g3["(20, 30)"] r3["30"] g4["(30, 40)"] r4["40"] g5["(40, +∞)"]
  class r1,r4 data
  class g1,g4,g5 free
  class g2,r2,g3,r3 hot

Diagram description: The index holds the keys 10, 20, 30, and 40, with a gap between each pair. Under Repeatable Read, when WHERE id BETWEEN 15 AND 30 FOR UPDATE runs, InnoDB takes a next-key lock (record + the gap before it) on every record it passes while looking for matching records. The shaded cells (10, 20), 20, (20, 30), and 30 are locked, and checking the next record can also lock the gap (30, 40). Meanwhile, other transactions wait instead of inserting 15 or 25 into these gaps.

One row through a unique index. If every column of a unique index is matched with equals (=) and the row exists, InnoDB takes only a record lock and leaves the gap unlocked. If the value does not exist, InnoDB locks the gap where it would be.

Read Committed. At the Read Committed level, searches and index scans take no gap locks (they remain for foreign keys and duplicate checks). In exchange, phantoms can appear, and locks on rows that do not match the condition are released early. Services with frequent gap-lock waits and deadlocks often switch to Read Committed.

No index. If the condition column has no index, InnoDB scans the entire clustered index and locks every record and gap, which effectively locks the whole table.

Why there is no escalation. Because one bitmap records the locks of many records on a page, lock memory grows slowly even as the number of locked rows increases. For that reason, InnoDB never converts row locks into a table lock. Instead, when lock memory reaches the buffer pool limit, the transaction fails with an error (The total number of locks exceeds the lock table size).

Configuration and Inspection

-- Locks currently held (from 8.0)
SELECT ENGINE_TRANSACTION_ID AS trx, OBJECT_NAME, INDEX_NAME, LOCK_TYPE, LOCK_MODE, LOCK_STATUS, LOCK_DATA
FROM performance_schema.data_locks;
-- LOCK_MODE: X (next-key), X,REC_NOT_GAP (record only), X,GAP (gap), X,GAP,INSERT_INTENTION

-- Who is waiting for whom
SELECT * FROM sys.innodb_lock_waits;

-- Last deadlock (LATEST DETECTED DEADLOCK section)
SHOW ENGINE INNODB STATUS;

SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
SET SESSION innodb_lock_wait_timeout = 5;

Comparison with Other DBMSs

InnoDBPostgreSQLOracleSQL Server
Phantom preventionGap locks (default under Repeatable Read)Snapshot (Repeatable Read), SSI (Serializable)Snapshot (Serializable)Key-range locks (Serializable)
Where row locks are recordedPer-page bitmap (lock memory)Tuple headerITL in the blockLock manager memory
EscalationNoneNoneNoneTo a table lock at about 5,000 locks

In the Articles

NOTE Which gaps get locked can vary with the execution plan, the indexes, and the version. Check the actual locks with performance_schema.data_locks.

Related pages