This topic describes how to use the lock analysis feature of Database Autonomy Service (DAS) to perform deadlock analysis on your databases. It covers two modes: recent deadlock analysis and full deadlock analysis.
The data in the figures in this topic, including thread IDs and SQL statements, is for demonstration purposes only. When you perform the operations, use your actual data and parameters.
Prerequisites
-
The target instance must use one of the following engines:
-
Recent deadlock analysis:
-
RDS MySQL
-
Self-managed MySQL
-
PolarDB for MySQL
-
PolarDB-X 2.0
-
-
Full deadlock analysis:
-
RDS MySQL
-
PolarDB for MySQL
-
-
-
The target instance must be connected to DAS with a connection status of Normal Access. For more information, see Connect a database instance to DAS.
-
Alibaba Cloud Managed Services (formerly DAS Economy Edition) must be enabled.
Important-
Alibaba Cloud Managed Services is available only in specific regions. For more information, see Supported databases and regions by edition.
-
To enable this service, see Manage Alibaba Cloud Managed Services.
-
Recent deadlock analysis
-
Parameter settings: The instance must have the
innodb_deadlock_detectparameter enabled.NoteThis parameter is typically enabled by default. To confirm this, run
SHOW VARIABLES LIKE 'innodb_deadlock_detect'. Note that you cannot change the value of this parameter for some instance types.-
To modify instance parameters:
-
For RDS MySQL, see Set instance parameters.
-
For PolarDB for MySQL, see Set cluster and node parameters.
-
-
-
Analyze data: Analyze the most recent deadlock log returned by the
SHOW ENGINE INNODB STATUScommand.
If multiple deadlocks have occurred, each analysis captures only the most recent one and saves its data.
Procedure
Log on to the DAS console.
In the navigation pane on the left, click .
Find the target instance and click the instance ID to open the instance details page.
-
In the left-side navigation pane, click Lock Analysis. On the Lock Analysis page, select the Recent Deadlock Analysis tab.
-
Click Create Analysis to start a recent deadlock analysis.
After the analysis is complete, the results table on the Recent Deadlock Analysis tab shows the generation time, occurrence time, and whether a deadlock was found. Click View Details to see detailed information about the deadlock.
View details
-
Select a time period to view the results of analysis tasks triggered within that period.
-
In the list of analysis tasks for recent deadlock analysis, click View Details in the Details column.
-
A panel appears, displaying the deadlock details in three tabs:
-
View: Displays a visual graph of the deadlock relationship, including nodes for the transactions and record locks involved. Arrows indicating lock granted and waiting show the lock possession and waiting relationships, forming a deadlock cycle. Rolled-back transactions are marked as "Rolled back". Click a transaction node to expand its details, such as Session ID, thread ID, request type, transaction ID, involved table, waiting lock information, and the name of the index on the waiting lock.
-
List: This tab displays a side-by-side comparison of the two deadlocked transactions, with rolled-back transactions marked. The table rows include Session ID, thread ID, request type, transaction ID, involved table, waiting lock (index name and lock type, such as X locks rec but not gap waiting), held lock (index name and lock type), and transaction SQL. At the bottom, you can use the thread ID or transaction ID to search for related transaction details in SQL Explorer and Audit.
-
Log: Displays the raw deadlock log details from the
LATEST DETECTED DEADLOCKsection of theSHOW ENGINE INNODB STATUSoutput. This includes the time the deadlock occurred, the SQL statements of the involved transactions, held lock information (HOLDS THE LOCK), and waiting lock information (WAITING FOR THIS LOCK TO BE GRANTED).
-
Analyze with SQL Explorer and Audit
-
In the left-side navigation pane for the target instance, click SQL Explorer and Audit. On the Audit page, click Enable Advanced Query.
-
Select a time range that includes when the deadlock occurred and enter the thread ID from the recent deadlock analysis to query the SQL statements executed by that transaction.
-
For Transaction 1 in the example, the query results show that the thread executed three SQL statements:
update students set name=UUID() where id = 2(execution time: 2,938.377 ms, lock wait time: 1 ms),update students set name=UUID() where id = 3(execution time: 0.002 ms), andbegin. The unusually long execution time of the first update statement indicates a lock wait. -
For Transaction 2 (rolled back): On the Audit page, set the Time range, set the Thread ID to
195662, and then click Query. The log list shows the SQL execution record for Transaction 2:update students set name=UUID() where id = 3failed with error code 1213 (deadlock) and took 1.40 ms, with a lock wait time of 1.152 ms.update students set name=UUID() where id = 2succeeded and took 0.20 ms.beginsucceeded and took 0.18 ms.
-
Full deadlock analysis
-
You must enable the
innodb_deadlock_detectandinnodb_print_all_deadlocksparameters. For an RDS MySQL instance, you must set thelog_error_verbosityparameter to 3.NoteThe
innodb_deadlock_detectparameter is usually enabled by default. You can runSHOW VARIABLES LIKE 'innodb_deadlock_detect'to confirm this. Note that you cannot change the value of this parameter for some instance types.-
To modify instance parameters:
-
For RDS MySQL, see Set instance parameters.
-
For PolarDB for MySQL, see Set cluster and node parameters.
-
-
-
Data source: This feature parses the deadlock information printed in the instance's error log every hour to generate deadlock count trends and compile the details of each deadlock.
-
Cooldown period: If Alibaba Cloud Managed Services is enabled and DAS detects that the required parameters are not enabled, DAS enters a 12-hour cooldown period and does not run analysis tasks. If you enable the required parameters during the cooldown period, DAS waits for the period to end before rescheduling the analysis. For example:
-
An analysis task at 11:12 detects that a required parameter is not enabled and initiates a 12-hour cooldown. After 23:12, full deadlock analysis reschedules the task. If the parameter is still not enabled, another 12-hour cooldown begins. If you enable the parameter before 23:12, the cooldown ends at 23:12. Afterward, the analysis runs hourly, provided the required parameter and Alibaba Cloud Managed Services remain enabled for the instance.
-
Analysis of deadlocks that include the message "TOO DEEP OR LONG SEARCH IN THE LOCK TABLE WAITS-FOR GRAPH, WE WILL ROLL BACK FOLLOWING TRANSACTION" is not supported.
Procedure
Log on to the DAS console.
In the navigation pane on the left, click .
Find the target instance and click the instance ID to open the instance details page.
-
In the left-side navigation pane, click Lock Analysis. On the Lock Analysis page, select the Full Deadlock Analysis tab.
-
Each hour, full deadlock analysis automatically analyzes the instance's error logs, counts the deadlocks, and plots the data on the Deadlock Trend chart at the top of the page. Select a time period to view the deadlock trend for that period.
The top of the page provides quick-select buttons for Last 1 Day, Last 3 Days, and Last 7 Days, as well as a custom date range selector. Click the View button to refresh the trend chart. Below the chart is a list of deadlock events that shows the Deadlock Time for each. Click View Details for a specific event to see more information.
View details
-
In the deadlock list of the full deadlock analysis, click View Details in the Details column.
-
A panel appears, displaying the deadlock details in three tabs: View, List, and Log. The Log tab contains the error log content related to the deadlock. The View and List tabs are the same as those in recent deadlock analysis. For more information, see Recent deadlock analysis - View details.
Analyze with SQL Explorer and Audit
The procedure is the same as for recent deadlock analysis. For more information, see Recent deadlock analysis - Analyze with SQL Explorer and Audit.