Skip to content

Commit 85efc9c

Browse files
authored
Merge branch 'main' into rw-1009-trace-flags
2 parents 5209aba + 63ae2c8 commit 85efc9c

8 files changed

Lines changed: 632 additions & 651 deletions

File tree

azure-sql/database/analyze-prevent-deadlocks.md

Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
---
22
title: Analyze and Prevent Deadlocks
33
titleSuffix: Azure SQL Database
4-
description: Learn how to analyze deadlocks and prevent them from reoccurring in Azure SQL Database and Fabric SQL database.
4+
description: Learn how to analyze deadlocks and prevent them from reoccurring in Azure SQL Database and SQL database in Fabric.
55
author: rwestMSFT
66
ms.author: randolphwest
77
ms.reviewer: mathoma, dfurman, wiassaf
8-
ms.date: 09/23/2025
8+
ms.date: 10/09/2025
99
ms.service: azure-sql-database
1010
ms.subservice: performance
1111
ms.topic: troubleshooting-general
12-
monikerRange: "=azuresql || =azuresql-db || =fabricsql"
1312
ms.custom:
1413
- ignite-2024
1514
- sfi-image-nochange
15+
monikerRange: "=azuresql || =azuresql-db || =fabricsql"
1616
---
1717

18-
# Analyze and prevent deadlocks in Azure SQL Database and Fabric SQL database
18+
# Analyze and prevent deadlocks in Azure SQL Database and SQL database in Fabric
1919

2020
[!INCLUDE [appliesto-sqldb-fabricsqldb](../includes/appliesto-sqldb-fabricsqldb.md)]
2121

@@ -118,17 +118,17 @@ Learn more about each of these approaches in the [Prevent a deadlock from reoccu
118118

119119
In this article, we use the `AdventureWorksLT` sample database to set up alerts for deadlocks, cause an example deadlock, analyze the deadlock graph for the example deadlock, and test changes to prevent the deadlock from reoccurring.
120120

121-
We use the [SQL Server Management Studio](/sql/ssms/download-sql-server-management-studio-ssms) (SSMS) client in this article, as it contains functionality to display deadlock graphs in an interactive visual mode. You can use other clients such as [Azure Data Studio](/azure-data-studio/download-azure-data-studio), the [MSSQL extension for Visual Studio Code](/sql/tools/visual-studio-code-extensions/mssql/mssql-extension-visual-studio-code), [sqlcmd](/sql/tools/sqlcmd/sqlcmd-utility), or your favorite T-SQL querying tool to follow along with the examples, but you might only be able to view deadlock graphs as XML.
121+
We use the [SQL Server Management Studio](/ssms/install/install) (SSMS) client in this article, as it contains functionality to display deadlock graphs in an interactive visual mode. You can use other clients such as the [MSSQL extension for Visual Studio Code](/sql/tools/visual-studio-code-extensions/mssql/mssql-extension-visual-studio-code), [sqlcmd](/sql/tools/sqlcmd/sqlcmd-utility), or your favorite Transact-SQL querying tool to follow along with the examples, but you might only be able to view deadlock graphs as XML.
122122

123123
### Create the AdventureWorksLT database
124124

125125
To follow along with the examples, create a new database in Azure SQL Database and select **Sample** data as the **Data source**.
126126

127-
For detailed instructions on how to create `AdventureWorksLT` with the Azure portal, Azure CLI, or PowerShell, select the approach of your choice in [Quickstart: Create an Azure SQL Database single database](single-database-create-quickstart.md).
127+
For detailed instructions on how to create `AdventureWorksLT` with the Azure portal, Azure CLI, or PowerShell, select the approach of your choice in [Quickstart: Create a single database in Azure SQL Database](single-database-create-quickstart.md).
128128

129129
### Set up deadlock alerts in the Azure portal
130130

131-
To set up alerts for deadlock events, follow the steps in the article [Create alerts for Azure SQL Database using the Azure portal](alerts-create.md).
131+
To set up alerts for deadlock events, follow the steps in the article [Create alerts for Azure SQL Database and Azure Synapse Analytics using the Azure portal](alerts-create.md).
132132

133133
Select **Deadlocks** as the signal name for the alert. Configure the **Action group** to notify you using the method of your choice, such as the **Email/SMS/Push/Voice** action type.
134134

@@ -184,7 +184,7 @@ To create an XEvents session that writes to an event file target, we:
184184
To configure an Azure Storage container, first create or select an existing Azure Storage account, then create the container. Generate a Shared Access Signature (SAS) token for the container. This section describes completing this process in the Azure portal.
185185

186186
> [!NOTE]
187-
> If you wish to create and configure the Azure Storage blob container with PowerShell, see [Event File target code for extended events in Azure SQL Database and SQL database in Fabric](xevent-code-event-file.md). Alternately, you might find it convenient to [Use Azure Storage Explorer](#use-azure-storage-explorer) to create and configure the Azure Storage blob container instead of using the Azure portal.
187+
> If you wish to create and configure the Azure Storage blob container with PowerShell, see [Create an event session with an event_file target in Azure Storage](xevent-code-event-file.md). Alternately, you might find it convenient to [Use Azure Storage Explorer](#use-azure-storage-explorer) to create and configure the Azure Storage blob container instead of using the Azure portal.
188188
189189
#### Create or select an Azure Storage account
190190

@@ -206,7 +206,7 @@ From the storage account page in the Azure portal:
206206
1. Select **+ Container** to create a new container. The New container pane appears.
207207
1. Enter a name for the container under **Name**.
208208
1. Select **Create**.
209-
1. Select the container from the list after it is created.
209+
1. Select the container from the list after it's created.
210210

211211
#### Create a shared access token
212212

@@ -294,69 +294,9 @@ GO
294294

295295
---
296296

297-
## Cause a deadlock in AdventureWorksLT
298-
299-
> [!NOTE]
300-
> This example works in the `AdventureWorksLT` database with the default schema and data when RCSI is enabled. See [Create the AdventureWorksLT database](#create-the-adventureworkslt-database) for instructions to create the database.
301-
302-
To cause a deadlock, you need to connect two sessions to the `AdventureWorksLT` database. We refer to these sessions as **Session A** and **Session B**.
303-
304-
In **Session A**, run the following Transact-SQL. This code begins an [explicit transaction](/sql/relational-databases/sql-server-transaction-locking-and-row-versioning-guide#starting-transactions) and runs a single statement that updates the `SalesLT.Product` table. To do this, the transaction acquires an [update (U) lock](/sql/relational-databases/sql-server-transaction-locking-and-row-versioning-guide#behavior-when-modifying-data) on one row on table `SalesLT.Product` which is converted to an exclusive (X) lock. We leave the transaction open.
305-
306-
```sql
307-
BEGIN TRANSACTION;
308-
309-
UPDATE SalesLT.Product
310-
SET SellEndDate = SellEndDate + 1
311-
WHERE Color = 'Red';
312-
```
313-
314-
Now, in **Session B**, run the following Transact-SQL. This code doesn't explicitly begin a transaction. Instead, it operates in [autocommit transaction mode](/sql/relational-databases/sql-server-transaction-locking-and-row-versioning-guide#starting-transactions). This statement updates the `SalesLT.ProductDescription` table. The update takes out an update (U) lock on 72 rows on the `SalesLT.ProductDescription` table. The query joins to other tables, including the `SalesLT.Product` table.
315-
316-
```sql
317-
UPDATE SalesLT.ProductDescription
318-
SET Description = Description
319-
FROM SalesLT.ProductDescription AS pd
320-
INNER JOIN SalesLT.ProductModelProductDescription AS pmpd
321-
ON pd.ProductDescriptionID = pmpd.ProductDescriptionID
322-
INNER JOIN SalesLT.ProductModel AS pm
323-
ON pmpd.ProductModelID = pm.ProductModelID
324-
INNER JOIN SalesLT.Product AS p
325-
ON pm.ProductModelID = p.ProductModelID
326-
WHERE p.Color = 'Silver';
327-
```
328-
329-
To complete this update, **Session B** needs a shared (S) lock on rows on the table `SalesLT.Product`, including the row that is locked by **Session A**. **Session B** is blocked on `SalesLT.Product`.
330-
331-
Return to **Session A**. Run the following Transact-SQL statement. This runs a second `UPDATE` statement as part of the open transaction.
332-
333-
```sql
334-
UPDATE SalesLT.ProductDescription
335-
SET Description = Description
336-
FROM SalesLT.ProductDescription AS pd
337-
INNER JOIN SalesLT.ProductModelProductDescription AS pmpd
338-
ON pd.ProductDescriptionID = pmpd.ProductDescriptionID
339-
INNER JOIN SalesLT.ProductModel AS pm
340-
ON pmpd.ProductModelID = pm.ProductModelID
341-
INNER JOIN SalesLT.Product AS p
342-
ON pm.ProductModelID = p.ProductModelID
343-
WHERE p.Color = 'Red';
344-
```
345-
346-
The second update statement in **Session A** is blocked by **Session B** on the `SalesLT.ProductDescription`.
347-
348-
**Session A** and **Session B** are now mutually blocking one another. Neither transaction can proceed, as they each need a resource that is locked by the other.
349-
350-
After a few seconds, the deadlock monitor identifies that the transactions in **Session A** and **Session B** are mutually blocking one another, and that neither can make progress. You should see a deadlock occur, with **Session A** chosen as the deadlock victim. An error message appears in **Session A** with text similar to the following:
351-
352-
```output
353-
Msg 1205, Level 13, State 51, Line 7
354-
Transaction (Process ID 91) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
355-
```
356-
357-
**Session B** completes successfully.
297+
## Cause a deadlock
358298

359-
If you [set up deadlock alerts in the Azure portal](#set-up-deadlock-alerts-in-the-azure-portal), you should receive a notification shortly after the deadlock occurs.
299+
Because [optimized locking](/sql/relational-databases/performance/optimized-locking) is always enabled in Azure SQL Database and SQL database in Fabric, deadlocks are less likely. For more information, and for an example of a deadlock that can occur with optimized locking, see [Optimized locking and deadlocks](/sql/relational-databases/sql-server-deadlocks-guide#optimized-locking-and-deadlocks).
360300

361301
## View deadlock graphs from an XEvents session
362302

0 commit comments

Comments
 (0)