Find MySQL Metadata Lock Waits and Blocking Sessions
Joins performance_schema.metadata_locks with threads to expose granted and pending lock holders causing DDL stalls, including sleeping sessions invisible to row-lock monitors.
SELECT m.OBJECT_TYPE, m.OBJECT_SCHEMA, m.OBJECT_NAME, m.LOCK_TYPE, m.LOCK_DURATION, m.LOCK_STATUS, t.PROCESSLIST_ID AS conn_id, t.PROCESSLIST_USER AS user, t.PROCESSLIST_HOST AS host, t.PROCESSLIST_COMMAND AS command, t.PROCESSLIST_STATE AS state, t.PROCESSLIST_INFO AS query_info
FROM performance_schema.metadata_locks m
JOIN performance_schema.threads t ON m.OWNER_THREAD_ID = t.THREAD_ID
WHERE m.OBJECT_TYPE = 'TABLE'
AND m.OBJECT_SCHEMA NOT IN ('performance_schema', 'information_schema', 'mysql', 'sys')
ORDER BY m.OBJECT_SCHEMA, m.OBJECT_NAME, m.LOCK_STATUS;Requires performance_schema metadata locking enabled (default in 8.0/8.4). Filters user tables and reveals granted SHARED_READ locks held by idle/sleeping transactions blocking incoming EXCLUSIVE DDL locks.
- This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
- Prior to executing on production systems under high load, verify the target database version (Min. Version: 5.7).
- Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.
Engineering Triad: Related Runbooks & Incident Response
Which production incidents and emergency drills execute this script as a diagnosis step?
SQL Server Blocking Chain: Find and Resolve the Head Blocker
Finds the head blocker, detects the open transaction and ends it safely. Covers RCSI and threshold settings to prevent recurrence.
SQL Server Deadlock Investigation: From system_health Graph to Root Cause
Analyzes error 1205 using the deadlock graph, distinguishes victim from the lock cycle and prevents recurrence through access-order and index fixes.
Other MYSQL Diagnostic Scripts
View All (100)Find Long-Running Queries in MySQL
Lists active queries running longer than 60 seconds from the processlist. Fast diagnosis for slowdowns and lock pile-ups.
Find MySQL InnoDB Lock Waits and the Blocking Query
Uses the sys schema to show waiting and blocking sessions with lock modes. Resolves who is holding up whom in a single query.
List the Largest Tables and Disk Usage in MySQL
Ranks tables by data, index and free space size. Core input for capacity planning and archiving decisions.
Detect Unused Indexes in MySQL
Uses sys.schema_unused_indexes to list indexes that were never used. Helps safely drop indexes that slow down writes.