Prevent Overlapping Reservations with PostgreSQL Exclusion Constraints
A reservation service has to stop two bookings from claiming the same room at the same time, even when requests arrive together.…
MySQL can stall a statement for several reasons. The server might be checking or changing a table definition, a table-level lock might be blocking access to the table, or InnoDB might be waiting for locks on index records (entries in an index that help InnoDB locate rows) or ranges to clear. Each lock protects a different resource. When a wait names a table, another transaction has not necessarily locked every row in it.
People use the phrase "server-level lock" for more than one mechanism. MySQL's server layer manages metadata locks, which protect table definitions and their concurrent use, along with table locks, which control access to a table. InnoDB handles row-level work with intention locks, which announce that a transaction plans to lock rows, and with locks on index records and the gaps between them. Once you identify which lock is waiting, you can find the blocking transaction and see why statements that look unrelated are stuck.
MySQL's internal locking documentation describes table-level locking for storage engines such as MyISAM, MEMORY, and MERGE. InnoDB takes a different approach and locks index records and ranges when it changes data. Separate from both, MySQL's server layer uses metadata locks (MDL) to coordinate access to object definitions like table schemas, without locking any table rows.
| Mechanism | What it protects | Typical contention |
|---|---|---|
| Metadata lock (MDL) | A database object's definition and concurrent use | A transaction using a table delays DDL, or pending DDL delays later statements |
| Server-layer table lock | Access to a table, particularly for engines with table-level locking or explicit LOCK TABLES requests |
A session requesting a table lock waits for a conflicting table lock to clear |
| InnoDB intention lock | Signals that a transaction intends to lock rows in a table | InnoDB coordinates row locks with requests for a full-table lock |
| InnoDB record, gap, or next-key lock | Index records and, for some lock types, the spaces between records | A statement changing an indexed record or inserting into a locked range waits |
MySQL uses other locking mechanisms as well, though these four account for most waits you will see. Even when a table-level lock and a metadata lock both reference the same table, they protect different things.
MySQL acquires a metadata lock whenever a statement touches an object and can hold that lock until the transaction ends. Because of that, a transaction that only read a table can still block a schema change, even though it holds no InnoDB row locks that conflict with other reads.
In production, this pileup often starts when a transaction uses a table and stays open. Another session runs ALTER TABLE and waits for the first transaction's metadata lock to clear. If that ALTER TABLE needs an exclusive metadata lock, MySQL queues later statements touching the same table behind it. To your application, the whole table looks locked up, even though the original transaction holds no table-wide data lock. MySQL documents this queuing behavior under metadata locking and online DDL concurrency.
Calling a schema change "online" does not guarantee it will skip metadata waits. Even an online DDL statement can still need an exclusive metadata lock when it updates the table definition. If you see a process-list state such as Waiting for table metadata lock, look at MDL first. Query performance_schema.metadata_locks to see granted and pending requests on that object, then inspect the transactions holding the granted locks. A session can look idle from the client's side and still hold a metadata lock because its transaction has not committed or rolled back.
InnoDB's "row locks" apply to index records, so the index InnoDB picks to find rows shapes what it locks. A statement that updates only a handful of rows can still lock many index records, because InnoDB may lock every record it scans while evaluating the statement. When InnoDB scans a wide range because the table lacks a suitable index, it can block many other transactions. MySQL explains these rules in its pages on InnoDB locking and locks set by SQL statements.
A record lock blocks conflicting changes to a single index record. A gap lock protects the interval between index records, mainly to stop other transactions from inserting into that space. A next-key lock combines a record lock with a gap lock on the interval immediately before that record.
Take an index with the values 10, 11, 13, and 20. If a transaction holds a next-key lock on 13, it also locks the gap between 11 and 13. Another transaction trying to insert into that gap will wait, even though it touches no existing row, because the lock covers that index range.
Both your search condition and your isolation level determine which locks InnoDB takes. When InnoDB looks up one exact value through a unique index (which allows only one row per indexed value), it can lock only the matching index record and leave the gap before it open. When InnoDB searches an interval, or uses an index that allows duplicate values, it can lock ranges with gap or next-key locks. Under the default REPEATABLE READ isolation level, InnoDB uses next-key locking for searches and index scans to stop phantom rows, which are new rows that appear inside a range a transaction already read. Under READ COMMITTED, InnoDB disables gap locking for searches and index scans except for foreign-key and duplicate-key checks. Record locks still apply, and switching isolation levels also changes how transactions read data.
A plain consistent SELECT normally takes no InnoDB row locks. Locking reads like SELECT ... FOR UPDATE or SELECT ... FOR SHARE, along with data-changing statements such as UPDATE and DELETE, do acquire locks. SERIALIZABLE is the exception, where plain reads also take locks.
InnoDB sets table-level intention shared (IS) and intention exclusive (IX) locks to signal that a transaction plans to lock rows inside the table. These locks do not block normal row-level work. Instead, InnoDB uses them to coordinate row locks with full-table lock requests such as LOCK TABLES ... WRITE. Seeing an IX lock on a table does not mean every row in that table is blocked.
Explicit table locks work differently. MySQL sets table locks when a session runs LOCK TABLES, and whether InnoDB participates depends on your transaction mode and configuration. InnoDB also uses narrower table-level locks, such as AUTO-INC locks under certain innodb_autoinc_lock_mode settings. Rather than assuming InnoDB never takes table-level locks, check what a specific lock protects.
Start by checking the waiting session's state and current statement. SHOW FULL PROCESSLIST can show a state like Waiting for table metadata lock, which points straight to metadata contention. For an MDL wait, query performance_schema.metadata_locks to find the pending request and the sessions holding granted locks on the same object. Next, check whether those sessions still have open transactions, because the statement currently visible in a session might not be the one that first acquired the lock.
When a statement waits on an InnoDB data lock, query Performance Schema's data_lock_waits table to match the waiting transaction with the blocking one, and check data_locks for the table, index, and lock mode. MySQL explains how these tables connect in its reference for data_lock_waits. Read those lock details alongside the SQL statement, index definitions, and execution plan. A range scan or an unsuitable index often explains why InnoDB locked records or ranges well beyond the rows the statement changes.
Look at transaction age alongside statement duration. InnoDB holds its locks until a transaction commits or rolls back, and MySQL can hold metadata locks on objects used in that transaction for the same duration. An earlier statement in a long-running transaction may be holding the blocking lock while the transaction's current statement looks harmless.
Treat diagnostic output as a quick snapshot. MySQL warns that data_locks, data_lock_waits, transaction data, and process-list output can change rapidly and may not match when you query them together. Grab the relevant state quickly and cross-check it against the transaction and statement context.
In MySQL 8.4, innodb_lock_wait_timeout defaults to 50 seconds and applies to InnoDB row-lock waits. MySQL table-lock waits happen outside InnoDB, so that timeout does not govern them. Even if a statement stalls for about 50 seconds, you cannot identify the locking subsystem from the duration alone; check the wait state and lock instrumentation instead. See the InnoDB parameter documentation for details.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com