Databases

MySQL replica SQL thread stalls with 'Waiting for table metadata lock' after direct ALTER TABLE on primary during peak writes

Environment

Environment

Primary: MySQL 8.0.27 on Ubuntu 20.04 (systemd), InnoDB, binlog_format=ROW; Replica: MySQL 8.0.27 on Debian 11. Replication via asynchronous binlog/relay. A nightly maintenance job issues ALTER TABLE ... (non-ALGORITHM=INPLACE) on a high-write table during business hours.

Symptoms

Symptoms

Replica SQL thread shows Slave_IO_Running=Yes, Slave_SQL_Running=No. SHOW PROCESSLIST on primary shows ALTER TABLE connection holding MDL lock; many other sessions report 'Waiting for table metadata lock' and DML queuing. Application latency spikes and new writes pile up on primary.

Observations

Observations

performance_schema.metadata_locks identifies ALTER TABLE statement (connection 2345) as lock owner; multiple short transactions stuck in 'Waiting for table metadata lock' for same table. Replica relay log entries for that table apply but SQL thread halts where it reaches DDL/DML conflict. No obvious replication error lines in error log.

Already attempted

Already attempted

Tried KILLing the ALTER connection — orchestration recreated and resumed the DDL; stopped and started the replica SQL thread but it immediately blocked at same position; considered 'slave_skip_errors' but rejected due to data-loss risk. No pt-online-schema-change or gh-ost was used.

Question

Question

What's the safe way to unstick replication without risking data inconsistency? How to prevent this class of outage during online DDL in future?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1mo ago• Trouble ID

Most likely causes

Direct ALTER TABLE using copy algorithm on primary holding metadata lock during peak writes 90%
Replica SQL thread encountering DDL/DML ordering that requires table metadata access while primary holds MDL 55%
Long-running transactions or open cursors on primary causing MDL to be retained longer than expected 30%

Summary

Replica SQL thread blocks when it needs to apply statements that conflict with a primary-side metadata lock held by a concurrent ALTER TABLE. A non-online DDL (ALGORITHM=COPY) grabs MDL and prevents other sessions from accessing table metadata; replicas, replaying events, then block. Immediate unblocking options include stopping or killing the DDL on primary (risky if automated), or temporarily pausing replication and rebuilding the replica if required. Long-term fixes: perform online schema changes (pt-online-schema-change or gh-ost), schedule DDL during low-write windows, use ALGORITHM=INPLACE when supported, or use logical/rolling migration approaches to avoid long MDL holds.

Suggested Fixes

Diagnostic step 1
Identify the exact connection holding the MDL on the primary: SELECT * FROM performance_schema.metadata_locks and SHOW PROCESSLIST; note connection id.
Diagnostic step 2
If the ALTER was accidental or can be interrupted, coordinate with the app team and KILL the ALTER connection on primary; verify table integrity after termination and check binary log position.
Diagnostic step 3
On replicas: STOP SLAVE SQL_THREAD, inspect relay log position and pending event for the blocked table; do not skip errors blindly.

Continue troubleshooting

Matched from the title, description and tags