{"id":49,"date":"2013-11-13T00:00:00","date_gmt":"2013-01-01T00:00:00","guid":{"rendered":"https:\/\/www.sqlhammer.com\/blog\/deadlocks-querying-extended-events"},"modified":"2026-03-18T21:50:37","modified_gmt":"2026-03-18T21:50:37","slug":"deadlocks-querying-extended-events","status":"publish","type":"post","link":"https:\/\/sqlhammer.com\/index.php\/2013\/11\/13\/deadlocks-querying-extended-events\/","title":{"rendered":"Background"},"content":{"rendered":"<h1>Background<\/h1>\n<p>The days of restarting SQL Service with trace flags 1204 or 1222 (<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms178104(v=sql.105\">MSDN Detecting and Ending Deadlocks<\/a>.aspx)) or being forced to start up a profiler trace are no more.<\/p>\n<p>Let\u2019s face it. There are times when someone comes up to you and says, \u201chey we got a handful of deadlocks an hour ago can you tell me how it happened?\u201d Now, if you are practicing pro-active DBA methods then you might already have this information but if you are left with the deer in the head-lights look then these queries might be for you.<\/p>\n<p>Extended Events was new with SQL Server 2008 and by default captures xml deadlock reports. For information about Extended Events see Pinal Dave\u2019s post <a href=\"http:\/\/blog.sqlauthority.com\/2010\/03\/29\/sql-server-introduction-to-extended-events-finding-long-running-queries\/\">here<\/a>\u00a0or you can read the white paper <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd822788.aspx\">here<\/a>.<\/p>\n<h1>The Code<\/h1>\n<p>The comments on the queries below are fairly self explanatory. Running the first query will return a single record with a lot of improperly formatted xml. This query is given because it is very fast. All you need to do then is paste it into notepad and search for xml_deadlock_report to find the deadlock graph xml. I provided the bottom query for use when you come across the sql handles in the xml to identify the queries that were being run.<\/p>\n<p>The middle query is the best of the bunch here but can be very slow. This one will return a row per deadlock graph in properly formatted xml. Must easier to work with.<\/p>\n<p>In both cases the most recent deadlock events will be at the bottom of the result set or the bottom of the xml code block copied out from query one.<\/p>\n<p><code>-- This query is fast but the xml is not formatted properly so you<\/p>\n<p>-- need to paste the result in notepad and search for xml_deadlock_report.<\/p>\n<p>select CAST(target_data as xml) as TargetData<\/p>\n<p>from sys.dm_xe_session_targets st<\/p>\n<p>join sys.dm_xe_sessions s on s.address = st.event_session_address<\/p>\n<p>where name = 'system_health'<\/code><\/p>\n<p><code>-- This query gives you a record per deadlock graph with xml corrected<\/p>\n<p>-- but this query is VERY slow. There is no server performance impact<\/p>\n<p>-- but you should expect this query to run for 30 mins +<\/p>\n<p>select XEventData.XEvent.value('(data\/value)[1]', 'varchar(max)') as DeadlockGraph<\/p>\n<p>FROM<\/p>\n<p>(select CAST(target_data as xml) as TargetData<\/p>\n<p>from sys.dm_xe_session_targets st<\/p>\n<p>join sys.dm_xe_sessions s on s.address = st.event_session_address<\/p>\n<p>where name = 'system_health') AS Data<\/p>\n<p>CROSS APPLY TargetData.nodes ('\/\/RingBufferTarget\/event') AS XEventData (XEvent)<\/p>\n<p>where XEventData.XEvent.value('@name', 'varchar(4000)') = 'xml_deadlock_report'<\/code><\/p>\n<p><code>\/<em>For viewing the sql handles<\/em>\/<\/p>\n<p>SELECT * FROM sys.dm_exec_sql_text(0x03001a00ea93ed5bed752b0150a100000100000000000000);<\/code><\/p>\n<p><strong>Credits:<\/strong> (edited \u2013 2013-02-13)<\/p>\n<p>Many thanks to\u00a0<a href=\"http:\/\/www.sqlservercentral.com\/Authors\/Articles\/Jonathan_Kehayias\/244648\/\">Jonathan Kehayias<\/a>\u00a0for authoring these scripts and providing an informative article <a href=\"http:\/\/www.sqlservercentral.com\/articles\/deadlock\/65658\/\">here <\/a>on <a href=\"http:\/\/www.sqlservercentral.com\/\">sqlservercentral.com<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Background The days of restarting SQL Service with trace flags 1204 or 1222 (MSDN Detecting and Ending Deadlocks.aspx)) or being forced to start up a profiler trace are no more. Let\u2019s face it. There are times when someone comes up to you and says, \u201chey we got a handful of deadlocks an hour ago can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":1,"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":312,"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/posts\/49\/revisions\/312"}],"wp:attachment":[{"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlhammer.com\/index.php\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}