{"id":529,"date":"2016-02-22T08:02:13","date_gmt":"2016-02-22T14:02:13","guid":{"rendered":"http:\/\/www.sqlfreelancer.com\/blog\/?p=529"},"modified":"2016-02-22T08:02:13","modified_gmt":"2016-02-22T14:02:13","slug":"capturing-deadlocks-with-extended-events","status":"publish","type":"post","link":"https:\/\/www.sqlfreelancer.com\/blog\/capturing-deadlocks-with-extended-events\/","title":{"rendered":"Capturing Deadlocks with Extended Events"},"content":{"rendered":"<p>I\u2019ve been noticing a lot of deadlocks on my server. What\u2019s the best way to track down the queries so I can fix the problem?<\/p>\n<p>There are a few different ways that you can capture deadlock information. You can setup a trace flag (1222) to write the deadlock information to the error log, setup a Profiler trace to capture the deadlock graph, or setup an Extended Event to capture all sorts of information.<\/p>\n<p>I\u2019m going to focus on setting up an Extended Event in this post since MS continues to say Profiler will not be released in future versions. Extended Events are the future so why not start using them now?<\/p>\n<p>In SSMS, drill down to Management, Extended Events. Right click on Sessions and click New Session Wizard:<\/p>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-1.png\" rel=\"attachment wp-att-530\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-530\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-1.png\" alt=\"Deadlocks with Extended Events 1\" width=\"288\" height=\"246\" \/><\/a><\/p>\n<p>Click next on the Introduction screen and give the Session a name. I\u2019m going to name this session Deadlocks:<\/p>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-2.png\" rel=\"attachment wp-att-531\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-531\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-2.png\" alt=\"Deadlocks with Extended Events 2\" width=\"624\" height=\"296\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-2.png 624w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-2-300x142.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<p>Click next. On the Choose Template screen you can choose a predefined template (like Profiler) or you can create your own events by choosing \u201cDo not use a template\u201d. For this post, let\u2019s create our own:<\/p>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-3.png\" rel=\"attachment wp-att-532\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-532\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-3.png\" alt=\"Deadlocks with Extended Events 3\" width=\"624\" height=\"398\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-3.png 624w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-3-300x191.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<p>Click next and you\u2019ll see hundreds of events (like Profiler). We only want to capture deadlock data so let\u2019s scroll down to the very bottom and choose xml_deadlock_report. Click on the event and click the right arrow to move it into the Selected Events box:<\/p>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-4.png\" rel=\"attachment wp-att-533\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-533\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-4.png\" alt=\"Deadlocks with Extended Events 4\" width=\"624\" height=\"382\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-4.png 624w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-4-300x184.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<p>You can choose other events if needed, but for the simplicity of this post I\u2019m just going to use this one. Click next. The Capture Global Fields page allows us to select what fields we want to capture. These are unique to each event selected. For this example, I\u2019ll choose the following fields:<\/p>\n<ul>\n<li>Callstack<\/li>\n<li>Client_app_name<\/li>\n<li>Client_hostname<\/li>\n<li>Database_id<\/li>\n<li>Database_name<\/li>\n<li>Plan_handle<\/li>\n<li>Process_id<\/li>\n<li>Sql_text<\/li>\n<li>Transaction_id<\/li>\n<li>Transaction_sequence<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-5.png\" rel=\"attachment wp-att-534\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-534\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-5.png\" alt=\"Deadlocks with Extended Events 5\" width=\"624\" height=\"584\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-5.png 624w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-5-300x281.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<p>Click next. On this page you can apply filters if needed. I\u2019ll setup a filter so that I only capture data from the RollTide database. There are hundreds of different filters that can be configured so that you don\u2019t pull back data that is not needed:<\/p>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-6.png\" rel=\"attachment wp-att-535\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-535\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-6.png\" alt=\"Deadlocks with Extended Events 6\" width=\"624\" height=\"245\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-6.png 624w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-6-300x118.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<p>Click next to the Session Data Storage page. This page allows you to save data to a file or work with only the most recent data. I don\u2019t want to keep thousands upon thousands of events so I\u2019ll choose \u201cWork with only the most recent data\u201d<\/p>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-7.png\" rel=\"attachment wp-att-536\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-536\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-7.png\" alt=\"Deadlocks with Extended Events 7\" width=\"624\" height=\"438\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-7.png 624w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-7-300x211.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<p>The next page summarizes all the options we have selected. You can also script this session if you need to create it on other servers or save it for later. Click Finish to create the new session.<\/p>\n<p>The last page allows you to start the session immediately and watch live data. For this post, I\u2019ll choose both:<\/p>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-8-1.png\" rel=\"attachment wp-att-538\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-538\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-8-1.png\" alt=\"Deadlocks with Extended Events 8\" width=\"624\" height=\"443\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-8-1.png 624w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-8-1-300x213.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<p>You should see the new session under Extended Events and the Live Data tab should appear:<\/p>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-9.png\" rel=\"attachment wp-att-539\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-539\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-9.png\" alt=\"Deadlocks with Extended Events 9\" width=\"258\" height=\"227\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-10.png\" rel=\"attachment wp-att-540\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-540\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-10.png\" alt=\"Deadlocks with Extended Events 10\" width=\"567\" height=\"603\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-10.png 567w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-10-282x300.png 282w\" sizes=\"auto, (max-width: 567px) 100vw, 567px\" \/><\/a><\/p>\n<p>Once a deadlock occurs it should show the deadlock in the Live Data window:<\/p>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-11.png\" rel=\"attachment wp-att-541\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-541\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-11.png\" alt=\"Deadlocks with Extended Events 11\" width=\"489\" height=\"463\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-11.png 489w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-11-300x284.png 300w\" sizes=\"auto, (max-width: 489px) 100vw, 489px\" \/><\/a><\/p>\n<p>This view shows all of the fields we selected including the XML report. If you click on the Deadlock tab, you\u2019ll see the graph:<\/p>\n<p><a href=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-12.png\" rel=\"attachment wp-att-542\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-542\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-12.png\" alt=\"Deadlocks with Extended Events 12\" width=\"624\" height=\"332\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-12.png 624w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2016\/02\/Deadlocks-with-Extended-Events-12-300x160.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/a><\/p>\n<p>You can also use this query to see detailed information including the Deadlock graph and Event XML<\/p>\n<pre>SELECT\r\nDATEADD(mi, DATEDIFF(mi, GETUTCDATE(), CURRENT_TIMESTAMP), DeadlockEventXML.value('(event\/@timestamp)[1]', 'datetime2')) AS [EventTime],\r\nDeadlockEventXML.value('(\/\/process[@id[\/\/victim-list\/victimProcess[1]\/@id]]\/@hostname)[1]', 'nvarchar(max)') AS HostName,\r\nDeadlockEventXML.value('(\/\/process[@id[\/\/victim-list\/victimProcess[1]\/@id]]\/@clientapp)[1]', 'nvarchar(max)') AS ClientApp,\r\nDB_NAME(DeadlockEventXML.value('(\/\/process[@id[\/\/victim-list\/victimProcess[1]\/@id]]\/@currentdb)[1]', 'nvarchar(max)')) AS [DatabaseName],\r\nDeadlockEventXML.value('(\/\/process[@id[\/\/victim-list\/victimProcess[1]\/@id]]\/@transactionname)[1]', 'nvarchar(max)') AS VictimTransactionName,\r\nDeadlockEventXML.value('(\/\/process[@id[\/\/victim-list\/victimProcess[1]\/@id]]\/@isolationlevel)[1]', 'nvarchar(max)') AS IsolationLevel,\r\nDeadlockEventXML.query('(event\/data[@name=\"xml_report\"]\/value\/deadlock)[1]') AS DeadLockGraph,\r\nDeadlockEventXML\r\nFROM\r\n(\r\nSELECT\r\nXEvent.query('.') AS DeadlockEventXML,\r\nData.TargetData\r\nFROM\r\n(\r\nSELECT\r\nCAST(target_data AS XML) AS TargetData\r\nFROM sys.dm_xe_session_targets st\r\nJOIN sys.dm_xe_sessions s ON s.address = st.event_session_address\r\nWHERE s.name = 'Deadlocks' AND\r\nst.target_name = 'ring_buffer'\r\n) AS Data\r\nCROSS APPLY TargetData.nodes('RingBufferTarget\/event[@name=\"xml_deadlock_report\"]') AS XEventData(XEvent)\r\n) AS DeadlockInfo <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve been noticing a lot of deadlocks on my server. What\u2019s the best way to track down the queries so I can fix the problem? There are a few different ways that you can capture deadlock information. You can setup a trace flag (1222) to write the deadlock information to the error log, setup a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,25],"tags":[107,105],"class_list":["post-529","post","type-post","status-publish","format-standard","hentry","category-extended-events","category-monitoring","tag-extended-events","tag-monitoring"],"_links":{"self":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/comments?post=529"}],"version-history":[{"count":2,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/529\/revisions"}],"predecessor-version":[{"id":544,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/529\/revisions\/544"}],"wp:attachment":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}