FULL AI ANALYSIS

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

Generated Oct 19, 2026, 9:33:36 AM

Analysis 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.

Possible 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%

MySQL bug in MDL handling for this minor version (edge case)

10%

Suggested fixes

1

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.

2

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.

3

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.

4

Diagnostic step 4

If ALTER must complete, schedule it for a maintenance window and use pt-online-schema-change or gh-ost to avoid long MDL holds.

5

Diagnostic step 5

If replication already diverged or you cannot safely kill DDL, rebuild replica from a fresh backup taken after primary is in the desired schema state and resume replication.