skip to content

In SQL Server, where do you find the deadlock graph behind an error 1205 that already happened, and how do you read it to find the cause?

level: seniorimportance: should knowfreq 58%

answer

  1. a session that is always running
  2. one XML event per deadlock
  3. victims, processes, resources
  4. owner-list against waiter-list

basics

~20 s

The default system_health Extended Events session records every deadlock as an xml_deadlock_report event. Its graph names the victim, each process's statement and, in resource-list, who owns and who waits for each lock, so you can trace the cycle.

solid answer

~50 s

No trace has to be running in advance: the `system_health` Extended Events session is on by default and captures `xml_deadlock_report`, so you read its `event_file` or `ring_buffer` target, and SSMS draws the graph. The XML has three parts. `victim-list` names the process SQL Server rolled back; `process-list` gives each process's statement, isolation level, `waitresource` and `lockMode`; `resource-list` shows each locked resource with an `owner-list` and a `waiter-list`. Read the resources as a cycle: an `UPDATE` owns `X` on a clustered-index key and waits for `X` on a nonclustered key; a `SELECT` owns `S` on that nonclustered key and waits for `S` on the clustered key. That is the classic key-lookup deadlock between a reader and an update of an indexed column, fixed by a covering index or by `READ_COMMITTED_SNAPSHOT`, so the reader takes no shared locks.

code

sql · 5 lines
sql
SELECT timestamp_utc,
       CAST(event_data AS xml) AS deadlock_report
FROM sys.fn_xe_file_target_read_file('system_health*.xel', NULL, NULL, NULL)
WHERE object_name = N'xml_deadlock_report'
ORDER BY timestamp_utc DESC;

go deeper

for a junior

Remember that error 1205 means your transaction was chosen as a deadlock victim and rolled back, and that it has to be retried.

for a middle

Know that system_health captures xml_deadlock_report by default and what victim-list, process-list and resource-list each contain.

for a senior

Show you can walk the owner and waiter lists into a cycle, name the shape, such as a key-lookup deadlock, and choose the fix that removes it rather than changing who loses.

for a principal

Decide what the team standardises: retry handling for 1205 in the data layer, a routine review of system_health deadlocks, and how far RCSI would cut reader-writer deadlocks.

## Where SQL Server records deadlocks A **lock monitor** thread searches for deadlock cycles, every 5 seconds by default and as often as every 100 milliseconds while deadlocks keep occurring. When it finds one, it picks a **victim**, ends that session's batch, rolls back its transaction and returns **error 1205**: "Your transaction (process ID #...) was deadlocked on {lock | communication buffer | thread} resources with another process and has been chosen as the deadlock victim. Rerun your transaction." The evidence is already captured: - The **`system_health`** Extended Events session is created and started by default, and it records every detected deadlock as an **`xml_deadlock_report`** event containing the full deadlock graph. - You read it from the session's `event_file` target with `sys.fn_xe_file_target_read_file`, or from its `ring_buffer` target, which holds less history. SSMS can open the event and draw the graph. - Trace flags 1204 and 1222 write deadlock details to the error log instead, and the Profiler deadlock-graph event is the older route; `xml_deadlock_report` is the recommended one. ## The three parts of the graph The XML has three child elements under `deadlock`: - **`victim-list`**: the `id` of the process that was rolled back. - **`process-list`**: one `process` per participant, with its `spid`, `isolationlevel`, `waitresource` (for example `KEY: 5:72057594214350848 (...)`), the `lockMode` it was waiting for, `logused`, and an `executionStack` and `inputbuf` showing the procedure, line and statement text. - **`resource-list`**: one element per resource in the cycle, such as `keylock` with `objectname` and `indexname`, each holding an `owner-list` (who holds it, in what mode) and a `waiter-list` (who wants it, in what mode). ## Reading a cycle: a worked example A checkout procedure runs `UPDATE dbo.orders SET status = 'paid' WHERE order_id = @id`. A dashboard procedure runs `SELECT status, total FROM dbo.orders WHERE status = @s`, which seeks a nonclustered index on `status` and does a key lookup into the clustered index for `total`. Both run at lock-based `READ COMMITTED`. A trimmed graph looks like this: ```xml <resource-list> <keylock objectname="Shop.dbo.orders" indexname="PK_orders" mode="X"> <owner-list><owner id="processA" mode="X" /></owner-list> <waiter-list><waiter id="processB" mode="S" requestType="wait" /></waiter-list> </keylock> <keylock objectname="Shop.dbo.orders" indexname="ix_orders_status" mode="S"> <owner-list><owner id="processB" mode="S" /></owner-list> <waiter-list><waiter id="processA" mode="X" requestType="wait" /></waiter-list> </keylock> </resource-list> ``` Read it in four steps: 1. Pair each **owner** with the **waiter** on the other resource. Process A (the `UPDATE`) owns `X` on the clustered key and waits for `X` on the `ix_orders_status` key. 2. Process B (the `SELECT`) owns `S` on that `ix_orders_status` key and waits for `S` on the clustered key A holds. That closes the cycle. 3. Ask **why** each needs the second resource. A changed `status`, a column in the nonclustered index, so it must update that index's entry too. B found the row through the nonclustered index and needs the clustered row for `total`. 4. Check `victim-list` and `isolationlevel` to see who lost and under which rules. ## How SQL Server picks the victim - The session with the lower **`DEADLOCK_PRIORITY`** loses. It accepts `LOW`, `NORMAL` (the default, 0), `HIGH`, or an integer from -10 to 10. - At equal priority, the transaction **least expensive to roll back** loses; at equal cost the choice is random. - A task that is already rolling back is never chosen. Changing priority decides who loses. It does not remove the cycle. ## Fixes by cycle shape | Shape in the graph | Fix that removes the cycle | |---|---| | Reader holds `S` on a nonclustered key, waits for the clustered key | Cover the query (`INCLUDE` the looked-up columns) so the reader never visits the clustered index, or turn on `READ_COMMITTED_SNAPSHOT` so readers take no `S` locks | | Two writers lock the same tables in opposite order | Make every code path touch objects in the same order | | Long transactions holding locks across user interaction | Shorten them and keep each in one batch | | A transaction at `REPEATABLE READ` or `SERIALIZABLE` without need | Use a lower level or row versioning | Whatever the fix, the application still needs a handler for 1205 that pauses briefly, ideally for a randomised interval, and retries the whole transaction, because rolled-back work does not come back on its own. After applying a fix, keep running the same `system_health` query: the cycle disappearing from new reports is the confirmation, and a different shape in `resource-list` means a different pair of statements to examine next.

  • How does SQL Server choose the deadlock victim, and how can a session influence that choice?
    The session with the lower `DEADLOCK_PRIORITY` is chosen; `SET DEADLOCK_PRIORITY` takes `LOW`, `NORMAL` (0, the default), `HIGH` or an integer from -10 to 10. At equal priority SQL Server picks the transaction least expensive to roll back, and picks randomly if cost is equal too. A transaction already rolling back is never chosen.
  • How quickly does SQL Server notice a deadlock?
    Its lock monitor searches for cycles every 5 seconds by default. After it finds deadlocks, the interval drops toward 100 milliseconds depending on how often they occur, and the first lock waits after a detection trigger an immediate search. When deadlocks stop, the interval drifts back to 5 seconds.

  • You have to enable trace flag 1222 before a deadlock to see its graph.
  • SQL Server always kills the session that started its transaction last.
  • Error 1205 rolls back only the failed statement, so the transaction can carry on.
  • A plain SELECT can never be part of a deadlock because it only reads.
  • Raising DEADLOCK_PRIORITY on the important session fixes the deadlock.