Can MySQL online DDL with LOCK=NONE still stall application queries during the final metadata-lock cutover?
0 reputation · 29 Apr 2023, 22:21 UTC
0 reputation · 29 Apr 2023, 22:21 UTC
We are planning a schema migration for a small MySQL 8.0 application and want to avoid visible downtime. The plan uses ALTER TABLE with ALGORITHM=INPLACE and LOCK=NONE, documented to permit concurrent DML for most of the operation. However, MySQL still requires a brief exclusive metadata lock at the start and end of the DDL to install the new table definition. If a long-running transaction or query holds a metadata lock when the ALTER reaches its final phase, the DDL waits, and application queries queue behind it, creating a potential traffic stall.
The unresolved decision is whether native online DDL is acceptable for this workload, or whether a trigger-based tool such as pt-online-schema-change or gh-ost is necessary to avoid the final metadata-lock cutover entirely. Not every ALTER supports INPLACE, so some changes would fail immediately if the algorithm is forced, while others might silently rebuild if not checked.
Which factor most influences the severity of the final metadata-lock stall: the duration of existing transactions or the rate of incoming queries? For a small application with modest traffic, is native online DDL generally sufficient, or does the risk of a queue behind the final lock make an external tool the safer default? If an ALTER operation does not support INPLACE, how can that limitation be detected before attempting the change in production?
35775 reputation · 30 Apr 2023, 00:27 UTC
The decisive factor in whether the final metadata-lock cutover stalls application queries is the duration of existing transactions, not the rate of incoming queries. MySQL's ALTER TABLE ... ALGORITHM=INPLACE LOCK=NONE permits concurrent DML throughout the bulk of the operation, but it still requires a brief exclusive metadata lock (MDL) at the start to validate the new definition and at the final cutover phase to install it. If any session holds an MDL on the table - typically from a long-running transaction, backup, or analytics query - the cutover blocks until that session releases its lock. Incoming query volume only determines how long the resulting queue persists after the block occurs; it does not prevent the block itself.
For a small application with modest traffic and no long-running transactions, native online DDL with LOCK=NONE is generally sufficient. The final exclusive MDL window is typically under one second, which produces a noticeable but brief pause rather than a sustained stall. However, if any session maintains an MDL across the DDL's execution - common in workloads with ad-hoc reports, scheduled backups, or replication lag - the cutover can wait indefinitely, and queued queries accumulate, creating visible traffic disruption.
Before running the change in production, verify that the intended ALGORITHM=INPLACE is supported for the specific table alteration. Some column or type changes silently fall back to a COPY-style rebuild if INPLACE is unavailable, especially without LOCK=NONE. Detect this by checking SHOW CREATE TABLE for the Engine and Algorithm fields, or by querying the Performance Schema for existing MDL holders:
SELECT * FROM performance_schema.metadata_locks WHERE object_schema = 'your_db' AND object_name = 'your_table';
If the query returns rows, those sessions hold an MDL and will block the cutover. Additionally, run a test ALTER TABLE on a staging copy with a held MDL and monitor the Performance Schema to observe the blocking behavior in a controlled environment.
One remaining diagnostic detail: does your workload include any long-running transactions or recurring queries that typically hold an MDL on the target table? Answering this determines whether the risk of a cutover stall is low enough for native DDL or whether an external online-schema-change tool should be the default.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.