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.…
When you run a MySQL schema change such as ALTER TABLE and MySQL pauses it even though nobody is changing the table’s rows, another open transaction may be holding a metadata lock. MySQL uses a metadata lock (MDL) to protect a database object, such as a table, while a session or transaction uses it. MySQL keeps a transaction’s lock on a table until the client commits or rolls back that transaction, so even a read-only transaction can block your schema change (DDL) on that table.
InnoDB uses row locks to control access to specific records or index ranges, which is separate from metadata locking. When you diagnose an MDL wait, you look for the session running the waiting DDL and the session holding the conflicting metadata lock, then you check whether the blocking client can finish its transaction safely.
MySQL uses metadata locks to coordinate access to database objects and prevent anyone from altering a table’s definition while another session is using it. In MySQL 8.4, when a client accesses a table inside a transaction, MySQL holds that metadata lock until the transaction ends. As the Metadata Locking documentation notes, a client holds these locks even after running a SELECT on either a transactional or a nontransactional table.
Suppose app.orders exists and two clients connect to the same MySQL server.
Session A starts a transaction, reads one row from the table, and leaves the transaction open:
START TRANSACTION;
SELECT * FROM app.orders LIMIT 1;
-- Leave the transaction open.
Session B tries to alter the table definition:
ALTER TABLE app.orders ADD COLUMN review_flag TINYINT;
-- Waits while Session A's transaction remains open.
Because Session A read app.orders and has not ended its transaction, MySQL cannot grant Session B the metadata lock it needs. That lock conflicts with the one Session A already holds, so Session B waits. When Session A finishes:
COMMIT;
MySQL releases Session A’s transaction lock, and Session B proceeds if no other session is blocking it. If Session A runs ROLLBACK instead, MySQL also ends the transaction and releases the lock. In autocommit mode, MySQL normally ends a statement’s transaction as soon as the statement finishes, so you only see this kind of wait when a client leaves a transaction open. When Session B runs DDL, MySQL implicitly commits any active transaction in Session B itself, as documented under statements that cause an implicit commit, though it never commits a separate blocking session.
Start with MySQL 8.4’s sys.schema_table_lock_waits view. It lists sessions waiting for table metadata locks and identifies the sessions blocking them, along with the object name, wait duration, accounts, and suggested KILL statements. Run:
SELECT *
FROM sys.schema_table_lock_waits;
Match the waiting query and object name to the DDL you are investigating, then use the blocking connection ID and account to find out who owns the other session. The view’s documentation describes each column and the generated commands. Before you run a suggested KILL command, find out what that connection is doing and what interrupting or rolling back its work would mean for the application.
For more detail, query Performance Schema’s metadata_locks table, which shows the lock requests MySQL has granted and the ones still waiting. Join each lock owner to performance_schema.threads so you can map each lock record to a MySQL connection:
SELECT ml.OBJECT_TYPE, ml.OBJECT_SCHEMA, ml.OBJECT_NAME,
ml.LOCK_TYPE, ml.LOCK_DURATION, ml.LOCK_STATUS,
t.PROCESSLIST_ID, t.PROCESSLIST_USER,
t.PROCESSLIST_COMMAND, t.PROCESSLIST_INFO
FROM performance_schema.metadata_locks AS ml
LEFT JOIN performance_schema.threads AS t
ON t.THREAD_ID = ml.OWNER_THREAD_ID
WHERE ml.OBJECT_SCHEMA = 'app'
AND ml.OBJECT_NAME = 'orders'
ORDER BY ml.LOCK_STATUS, t.PROCESSLIST_ID;
A status of PENDING means MySQL has not granted that request yet, while GRANTED means the session currently holds the lock. Match the pending DDL request with the granted locks on the same object, then inspect those sessions. The metadata_locks documentation explains these records and the instrumentation behind them.
A connection that appears as Sleep in SHOW PROCESSLIST is idle right now, yet it can still have an open transaction. Don’t rule it out as a blocker based on that status alone. You can check sys.processlist for connection, statement, and transaction details, and use the metadata-lock wait records to confirm which session is blocking the DDL. In unusual cases, MySQL keeps metadata locks for a prepared XA transaction even after the client disconnects or the server restarts. Those locks stay in place until someone runs XA COMMIT or XA ROLLBACK, according to the MySQL metadata-locking rules.
InnoDB uses row locks to protect records or index ranges, whereas MySQL uses metadata locks to protect database object definitions. The InnoDB locking documentation covers shared and exclusive record locks as well as gap locks, which protect the spaces between index records. Because these locks serve a different purpose from MDL, a client can block a table definition change without holding a conflicting row lock or modifying a single row.
If your DDL session waits while InnoDB row-lock diagnostics show no relevant conflict, check the metadata-lock views. Another session can still be blocking the schema change even when InnoDB reports no row-lock waits.
Even when you run an online or in-place ALTER TABLE, MySQL still needs metadata locks at certain stages. Although MySQL allows concurrent data changes during most of an in-place online ALTER TABLE, the MySQL 8.4 documentation notes that MySQL waits for relevant open transactions before finishing. In the final phase, MySQL must acquire an exclusive metadata lock to update the table definition. The exact algorithm and locking behavior depend on the specific statement and the storage engine.
Plan for a possible metadata-lock wait even when your ALTER TABLE supports online execution or specifies LOCK=NONE. Before you start the change, check for open transactions that have touched the target table, and watch sys.schema_table_lock_waits while the statement runs. MySQL’s online DDL documentation explains these constraints, though it does not set a single timeout value that fits every statement or deployment.
Keep your transactions short. Once a client finishes accessing a table, have it commit or roll back before waiting on unrelated work such as user input or a long-running job. That way, MySQL holds the transaction’s metadata lock for as little time as possible.
Before you deploy a schema change, check for long-running or idle sessions that have touched the target table. A process list only shows what sessions are doing right now, and it does not tell you by itself whether an earlier transaction has ended. If your DDL session starts waiting, use the metadata-lock views to find the blocking session, then ask the service owner or operator to finish or roll back that transaction if it is safe to do so. You can either cancel the waiting DDL statement or terminate the blocking connection, and both choices interrupt the work in the affected session, so decide based on what that session is doing.
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