Next-key lock

A next-key lock combines a record lock on an index entry with a gap lock on the gap before that entry. In REPEATABLE READ, range and non-unique locking operations use next-key locks to prevent other transactions from inserting matching rows into the scanned range.

Minimal example

sql
SELECT * FROM orders WHERE restaurant_id = 3 AND status = 'PLACED' FOR UPDATE;

Learn this concept

Lesson 3.8 · Isolation levels and anomalies
Prerequisite conceptsIsolation level