Troubleshooting slow queries, long-running statements, and lock waits
Find a slow or long-running routed statement in a database cluster group for Aurora and the member cluster session behind it. Then find the lock or transaction that blocks it, and end a statement or a session when you must. Use this page when one routed statement is slow, waits on a lock, or fails with a lock wait timeout. If every routed session is stuck, see Every client stalls or slows down first.
The routing layer forwards a routed statement and doesn't run it. The statement runs on one instance of one member cluster, so you find that instance and debug the statement there. After you find the right member cluster, you tune a statement as on any Aurora DB cluster. The checks on this page are read-only and use Aurora MySQL views and statements. Ending a statement or a session is a separate step that you choose to run.
Before you begin
Prepare the following before you run the checks on this page.
-
Collect the time window, the statement, and the global connection ID of an affected session, as described in Before you start: what to collect. Open each direct session on a member cluster as read-only, with statement and lock time limits, as that section shows.
-
Make sure that you can reach every instance endpoint of the member cluster, and that
performance_schemais turned on for each instance. For the member cluster user that the checks need, see The global connection ID.
A query is slow
Symptom. A routed statement takes a long time, or the latency of the application rises for one table or one routing key value. Or you find a slow statement on a member cluster, in its statement digests, Performance Insights, or slow query log, under a cluster name that your application didn't write.
Check. Find the statement at the routing endpoint, and then on the member cluster and the instance that runs it. The process list names the member cluster of a statement, not the instance. Each instance has its own performance_schema, so read the writer or the reader that ran the statement.
To find a slow statement and the instance that runs it
-
At the routing endpoint, while the statement runs, read its row in the routing layer's process list (
router_processlist). The member cluster alias column names the member cluster of the connection. -
If the statement has finished, find its member cluster from the mapping rows of the router name that it named. At the routing endpoint, read
dbclustergroup_sys.data_mappings. Then, on that member cluster (direct), read its statement digests. -
On each instance endpoint of that member cluster (direct), find the member cluster session that carries the global connection ID, as shown in Tracing a session. The cluster endpoint reaches only the writer. Don't pin a session to find an instance, because a pin binds a session to a member cluster, not to an instance.
-
If the member cluster session shows the command
Sleep, it's idle and runs no statement. The time goes elsewhere, such as a lock wait. For more information, see Lock waits and long transactions on a member cluster. -
If you started from a statement on a member cluster (direct), read the connection attributes of its session. If
_rds_connection_sourcedoesn't name the routing layer, a direct connection runs the statement. Tune or stop it as on any Aurora MySQL DB cluster. Otherwise, find its session at the routing endpoint from_rds_router_connection_id, or from the statement's trace comment, as shown in Tracing a session or statement across the group.
On Aurora MySQL, the checks are the following statements. Get your own session's global connection ID with SELECT CONNECTION_ID(), or another session's from the ID column of router_processlist. Then filter the process list with the number as a literal. An expression in a filter on the system tables, such as WHERE id = CONNECTION_ID(), is refused with ERROR 1235: Only literal values are allowed. No expressions, subqueries, or variables.
-- Step 1. Runs at: routing endpoint. Your own session's global connection ID. SELECT CONNECTION_ID(); -- Step 1. Runs at: routing endpoint. One session, with the ID as a literal. SELECT * FROM dbclustergroup_sys.router_processlist WHERE id =ROUTER_CONNECTION_ID; -- Step 2. Runs at: routing endpoint. The mapping rows of a router table name. SELECT * FROM dbclustergroup_sys.data_mappings WHERE (router_db_name, router_table_name) = ('ROUTER_DB', 'ROUTER_TABLE'); -- Step 2. Runs at: member cluster endpoint (direct). The statements that took the most time. SELECT SCHEMA_NAME, COUNT_STAR AS execs, SUM_TIMER_WAIT DIV 1000000000 AS total_ms, AVG_TIMER_WAIT DIV 1000000 AS avg_us, SUM_ROWS_EXAMINED, LEFT(DIGEST_TEXT, 120) AS digest_text FROM performance_schema.events_statements_summary_by_digest WHERE SCHEMA_NAME IS NOT NULL ORDER BY SUM_TIMER_WAIT DESC LIMIT 25; -- Step 5. Runs at: the instance endpoint where you found the statement (direct). -- The connection source and the global connection ID of one member cluster session. SELECT t.PROCESSLIST_ID AS pid, src.ATTR_VALUE AS connection_source, rid.ATTR_VALUE AS router_connection_id, LEFT(t.PROCESSLIST_INFO, 150) AS current_sql FROM performance_schema.threads t LEFT JOIN performance_schema.session_connect_attrs src ON src.PROCESSLIST_ID = t.PROCESSLIST_ID AND src.ATTR_NAME = '_rds_connection_source' LEFT JOIN performance_schema.session_connect_attrs rid ON rid.PROCESSLIST_ID = t.PROCESSLIST_ID AND rid.ATTR_NAME = '_rds_router_connection_id' WHERE t.PROCESSLIST_ID =MEMBER_PROCESS_ID;
The digests use cluster names. Map them back to router names with data_mappings before you compare member clusters, because the same table has a different cluster name on each member cluster.
Cause. A long transaction holds the lock, or the plan or the member cluster's load is slow. If the statement is slow on one member cluster only, with the same plan elsewhere, the cause is that member cluster's load.
Fix. On that instance (direct), tune the statement as on any Aurora DB cluster. For more information, see Monitoring DB load with Amazon CloudWatch Database Insights on Amazon Aurora. EXPLAIN at the routing endpoint returns the member cluster's plan. To compare the instances of one member cluster, run EXPLAIN with the cluster names on each instance endpoint (direct). If the statement has finished, debug it on the member cluster endpoint (direct). If one member cluster is busier than the others, see Troubleshooting uneven load across member clusters. If connecting is slow rather than the statement, see Connecting fails, is slow, or drops.
A long-running statement or an idle session holds locks
Symptom. A routed statement runs much longer than you expect, or a session runs past its timeout. Or a session sits idle in router_processlist while other statements wait on it.
Check. Match the session at the routing endpoint with its member cluster session by the _rds_router_connection_id connection attribute. Find the instance and the member cluster session, follow a lock wait to its blocker, and measure transaction age, as shown in Tracing a session or statement across the group. For a session that is idle inside an open transaction, find the member cluster that holds the transaction on the member cluster side.
To match a routed session with the member cluster that holds its transaction
-
At the routing endpoint, get the session's global connection ID, and read its row in
router_processlistwith the ID as a literal. -
On the writer of each member cluster (direct), list the open transactions, oldest first, with the global connection ID of each. The member cluster whose list has the session's ID holds its open transaction.
-
To go the other way, on the member cluster (direct), read the session's
_rds_router_connection_id. Then, at the routing endpoint, read that ID inrouter_processlist, one literal ID for each statement.
On Aurora MySQL, the checks are the following statements.
-- Step 1. Runs at: routing endpoint. Your own ID, or take another session's from the ID column. SELECT CONNECTION_ID(); SELECT * FROM dbclustergroup_sys.router_processlist WHERE id =ROUTER_CONNECTION_ID; -- Step 2. Runs at: member cluster endpoint (direct), on each member cluster's writer. -- Open transactions, oldest first, with the global connection ID of each. SELECT x.trx_mysql_thread_id AS pid, x.trx_started, TIMESTAMPDIFF(SECOND, x.trx_started, NOW()) AS trx_age_s, rid.ATTR_VALUE AS router_connection_id, LEFT(x.trx_query, 120) AS current_sql FROM information_schema.innodb_trx x LEFT JOIN performance_schema.session_connect_attrs rid ON rid.PROCESSLIST_ID = x.trx_mysql_thread_id AND rid.ATTR_NAME = '_rds_router_connection_id' ORDER BY x.trx_started LIMIT 50; -- Step 3. Runs at: the instance endpoint that holds the session (direct). SELECT ATTR_VALUE AS router_connection_id FROM performance_schema.session_connect_attrs WHERE PROCESSLIST_ID =MEMBER_PROCESS_IDAND ATTR_NAME = '_rds_router_connection_id';
An idle session's trx_query is NULL. To read what it ran, see Lock waits and long transactions on a member cluster.
Cause. The application keeps a transaction open between statements, or a statement runs longer than its timeout allows.
Fix. Instead of ending a session, fix the application so that it commits or rolls back. To stop a statement or end a session, use the following statements. Don't end the service's own sessions (rdsadmin, event_scheduler, and rds_aurora_* users). After you end a session, confirm that the session and its locks are gone on every member cluster that it reached.
At the routing endpoint, KILL QUERY cancels the running statement and keeps the session, and KILL CONNECTION ends the session and its member cluster connections. Use the session's global connection ID from router_processlist, or from SELECT CONNECTION_ID() in that session. To end another router user's connection, you need dbclustergroup_connection_admin.
-- Runs at: routing endpoint. KILL QUERYROUTER_CONNECTION_ID; KILL CONNECTIONROUTER_CONNECTION_ID;
After KILL QUERY, the statement fails with error 1317 Query execution was interrupted, and the session and its open transaction stay open. If the client paused in the middle of reading a result, it gets 1317 when it reads again. To end such a read at once, end the session. After KILL CONNECTION, the session's open transaction rolls back, and the client's next statement gets error 2013.
At the routing endpoint, KILL takes the global connection ID from router_processlist, and it reaches the session whichever part of the routing layer holds it. Error 1094 Unknown thread id means that no part of the routing layer holds the session, so it has already ended. If its locks remain on a member cluster, find the member cluster session by _rds_router_connection_id and run KILL on it (direct).
To stop only the running statement from the member cluster side, run KILL QUERY with the member cluster session's process ID on the instance that holds it (direct).
On a member cluster (direct), KILL or MEMBER_PROCESS_IDCALL mysql.rds_kill( can affect the whole routed session, not only its work on that member cluster. While that session has an open transaction, a running statement, or prepared statements on it, the routing layer doesn't open a replacement member cluster session and ends the whole session. An open transaction rolls back, the session's member cluster sessions close on every member cluster, and the client's next statement gets error MEMBER_PROCESS_ID)1927 or 2013. For a pinned session, the client gets error 1053 when no statement was running, or a lost connection (2013) if a statement was running. A member cluster session that was idle, with none of those, can be replaced at the session's next statement on that member cluster. To stop only the running statement, use KILL QUERY . The statement then fails with error MEMBER_PROCESS_ID1317, and the session and its open transaction stay open.
-- Runs at: the instance endpoint that holds the session (direct). KILL QUERYMEMBER_PROCESS_ID;
For the error codes, see Error codes for database cluster groups for Aurora.
Lock waits and long transactions on a member cluster
Symptom. Routed statements on some keys hang, and then fail with a lock wait timeout. On Aurora MySQL, a lock wait timeout returns 1205 Lock wait timeout exceeded. Or a schema change on a member cluster waits on a metadata lock.
Check. A routed statement runs on one instance of one member cluster, and its lock and transaction state is on that member cluster's writer, not in the routing layer. Writes and locking reads run on the writer, so connect to the member cluster endpoint (direct). Build the blocking chain there, and then map each end back to its session at the routing endpoint.
To find the blocker of a lock wait on Aurora MySQL
-
At the routing endpoint, find the member cluster that holds the contended rows from the mapping rows in
dbclustergroup_sys.data_mappings. For a scaleout table, use the routing function of the key. -
On that member cluster's writer (direct), open a read-only session, and count the lock waits. If they number in the thousands, skip the next step and read only the oldest transactions, because the blockers are among them.
-
On the writer (direct), build the blocking chain, longest waits first, with both ends joined to
_rds_router_connection_id. Follow each waiter to its blocker until you reach a session that doesn't wait. A loop is a deadlock. -
On the writer (direct), read the oldest open transactions. Measure transaction age from
trx_started. -
For a blocker that shows no current statement, read its recent statements on the writer (direct). It's idle inside an open transaction, between statements.
-
For a metadata lock, read who holds it and who waits on the member cluster (direct).
-
At the routing endpoint, read each global connection ID from the chain in
router_processlist, one literal ID for each statement.
On Aurora MySQL, the checks are the following statements.
-- Step 1. Runs at: routing endpoint. The member cluster of a router table name. SELECT * FROM dbclustergroup_sys.data_mappings WHERE (router_db_name, router_table_name) = ('ROUTER_DB', 'ROUTER_TABLE'); -- Step 2. Runs at: member cluster endpoint (direct), writer. Read-only, with time limits. SET SESSION transaction_read_only = ON, max_execution_time = 60000, lock_wait_timeout = 5, innodb_lock_wait_timeout = 5; SELECT COUNT(*) AS lock_waits FROM performance_schema.data_lock_waits; -- Step 3. Runs at: member cluster endpoint (direct), writer. The blocking chain, longest waits first. SELECT rt.PROCESSLIST_ID AS req_pid, ra.ATTR_VALUE AS req_router_id, bt.PROCESSLIST_ID AS blk_pid, ba.ATTR_VALUE AS blk_router_id, rl.OBJECT_SCHEMA, rl.OBJECT_NAME, rl.INDEX_NAME, bl.LOCK_MODE AS blk_mode, bl.LOCK_DATA, TIMESTAMPDIFF(SECOND, rx.trx_wait_started, NOW()) AS wait_s, LEFT(rx.trx_query, 120) AS waiting_sql FROM performance_schema.data_lock_waits w JOIN performance_schema.threads rt ON rt.THREAD_ID = w.REQUESTING_THREAD_ID JOIN performance_schema.threads bt ON bt.THREAD_ID = w.BLOCKING_THREAD_ID LEFT JOIN performance_schema.session_connect_attrs ra ON ra.PROCESSLIST_ID = rt.PROCESSLIST_ID AND ra.ATTR_NAME = '_rds_router_connection_id' LEFT JOIN performance_schema.session_connect_attrs ba ON ba.PROCESSLIST_ID = bt.PROCESSLIST_ID AND ba.ATTR_NAME = '_rds_router_connection_id' JOIN performance_schema.data_locks rl ON rl.ENGINE_LOCK_ID = w.REQUESTING_ENGINE_LOCK_ID JOIN performance_schema.data_locks bl ON bl.ENGINE_LOCK_ID = w.BLOCKING_ENGINE_LOCK_ID LEFT JOIN information_schema.innodb_trx rx ON rx.trx_id = w.REQUESTING_ENGINE_TRANSACTION_ID ORDER BY wait_s DESC LIMIT 100; -- Step 4. Runs at: member cluster endpoint (direct), writer. The 20 oldest transactions, no joins. SELECT trx_mysql_thread_id AS pid, trx_started, trx_state, LEFT(trx_query, 120) AS current_sql FROM information_schema.innodb_trx ORDER BY trx_started LIMIT 20; -- Step 5. Runs at: member cluster endpoint (direct), writer. Current and recent statements of one session. SELECT 'current' AS src, c.EVENT_ID, LEFT(c.SQL_TEXT, 150) AS sql_text, c.TIMER_WAIT DIV 1000000 AS latency_us FROM performance_schema.events_statements_current c JOIN performance_schema.threads t ON t.THREAD_ID = c.THREAD_ID WHERE t.PROCESSLIST_ID =MEMBER_PROCESS_IDUNION ALL SELECT 'history', h.EVENT_ID, LEFT(h.SQL_TEXT, 150), h.TIMER_WAIT DIV 1000000 FROM performance_schema.events_statements_history h JOIN performance_schema.threads t ON t.THREAD_ID = h.THREAD_ID WHERE t.PROCESSLIST_ID =MEMBER_PROCESS_IDORDER BY EVENT_ID DESC LIMIT 50; -- Step 6. Runs at: member cluster endpoint (direct). PENDING rows wait; GRANTED rows on the same object hold. SELECT t.PROCESSLIST_ID AS pid, t.PROCESSLIST_USER AS user, m.OBJECT_SCHEMA, m.OBJECT_NAME, m.LOCK_TYPE, m.LOCK_DURATION, m.LOCK_STATUS, LEFT(t.PROCESSLIST_INFO, 120) AS current_sql FROM performance_schema.metadata_locks m JOIN performance_schema.threads t ON t.THREAD_ID = m.OWNER_THREAD_ID WHERE m.OBJECT_TYPE = 'TABLE' ORDER BY m.OBJECT_SCHEMA, m.OBJECT_NAME, m.LOCK_STATUS LIMIT 200; -- Step 7. Runs at: routing endpoint. One literal ID for each statement. SELECT * FROM dbclustergroup_sys.router_processlist WHERE id =ROUTER_CONNECTION_ID;
The statement history holds only the last performance_schema_events_statements_history_size statements of each session, so it can miss the start of a long transaction. Statement text in the history is cut at performance_schema_max_sql_text_length characters.
Cause. A long transaction holds the lock. A blocker whose blk_router_id is NULL isn't a routed session. It's a direct connection, such as a schema tool, or one of the service's own sessions. A direct connection that holds a lock is a common cause of waits. A waiter that reaches the member cluster's innodb_lock_wait_timeout fails on its own with error 1205, and the blocking transaction keeps its locks until it ends.
Fix. Fix the application so that it ends the long transaction. To decide whether to wait, fix the application, or end a session, compare each waiter's wait_s with innodb_lock_wait_timeout on the member cluster (direct). To end a routed session, see A long-running statement or an idle session holds locks. Handle a direct connection that blocks on the member cluster. For schema tools, see Rolling out schema changes.
-- Runs at: member cluster endpoint (direct), writer. SELECT @@innodb_lock_wait_timeout;
Related pages
The following pages cover related tasks and reference.