extended events for deadlock

  • I've setup a deadlock monitor using extended events like this.

    CREATE EVENT SESSION [deadlock] ON SERVER

    ADD EVENT sqlserver.lock_deadlock(

    ACTION(package0.process_id,sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_id,sqlserver.database_name,sqlserver.plan_handle)

    WHERE ([sqlserver].[database_name]=N'Db1' OR [sqlserver].[database_name]=N'Db2' OR [sqlserver].[database_name]=N'DB3')),

    ADD EVENT sqlserver.lock_deadlock_chain(

    ACTION(package0.process_id,sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_id,sqlserver.database_name,sqlserver.plan_handle)

    WHERE ([sqlserver].[database_name]=N'Db1' OR [sqlserver].[database_name]=N'Db2' OR [sqlserver].[database_name]=N'DB3')),

    ADD EVENT sqlserver.xml_deadlock_report(

    ACTION(package0.process_id,sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_id,sqlserver.database_name,sqlserver.plan_handle)

    WHERE ([sqlserver].[database_name]=N'Db1' OR [sqlserver].[database_name]=N'Db2' OR [sqlserver].[database_name]=N'DB3'))

    ADD TARGET package0.event_file(SET filename=N'E:\SQL Install\MSSQL11.MSSQLSERVER\MSSQL\Log\deadlock_monitor.xel')

    WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=ON)

    GO

    Deadlock happened couple of days ago. I'm trying to determine the cause of deadlocks. What script should I use to pull that information to see what objects/processes caused deadlock?

  • You could view it in the GUI. Go to management->extended events->session->Deadlock. right-click on package0.event_file and choose "View Target Data"

    here is a basic query. I am sure someone will add something much better.

    SELECT *, CAST(event_data AS XML) AS 'event_data_XML'

    FROM sys.fn_xe_file_target_read_file('E:\SQL Install\MSSQL11.MSSQLSERVER\MSSQL\Log\deadlock_monitor.xel', NULL, NULL, NULL) where object_name = 'xml_deadlock_report'

    Bob
    -----------------------------------------------------------------------------
    How to post to get the best help[/url]

  • Thanks. How do I see the query that was run to cause the deadlock?

  • This was removed by the editor as SPAM

  • Robert klimes (10/6/2014)


    You could view it in the GUI. Go to management->extended events->session->Deadlock. right-click on package0.event_file and choose "View Target Data"

    here is a basic query. I am sure someone will add something much better.

    SELECT *, CAST(event_data AS XML) AS 'event_data_XML'

    FROM sys.fn_xe_file_target_read_file('E:\SQL Install\MSSQL11.MSSQLSERVER\MSSQL\Log\deadlock_monitor.xel', NULL, NULL, NULL) where object_name = 'xml_deadlock_report'

    If you open the xml column from the above query, it will open in another window. There is lots of info about the transactions that took part in the deadlock.

    Igor Micev,My blog: www.igormicev.com

  • At the top of the xml there are tags <victim-list>. here is an example

    <victim-list>

    <victimProcess id="process207c1d468" />

    </victim-list>

    the victim process id(s) are the processes that got killed due to the deadlock. Find the Process id in the <process-list> section and in that process id the input buffer will show query that was killed.

    <process id="process207c1d468" taskpriority="0" logused="144" waitresource="RID: 7:1:325:19" waittime="659" ownerId="4329139" transactionname="user_transaction" lasttranstarted="2014-10-07T08:53:23.443" XDES="0x231981000" lockMode="U" schedulerid="2" kpid="11032" status="suspended" spid="55" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2014-10-07T08:53:40.113" lastbatchcompleted="2014-10-07T08:53:23.530" lastattention="2014-10-07T08:42:54.587" clientapp="Microsoft SQL Server Management Studio - Query" hostname="ITS1019777" hostpid="9416" loginname="ADMIN\rklimes" isolationlevel="read committed (2)" xactid="4329139" currentdb="7" lockTimeout="4294967295" clientoption1="671090784" clientoption2="390200">

    <executionStack>

    <frame procname="adhoc" line="1" stmtstart="58" stmtend="152" sqlhandle="0x02000000c1450b241d05fed07cbd918b838c810dd57b293c0000000000000000000000000000000000000000">

    unknown </frame>

    <frame procname="adhoc" line="1" stmtend="94" sqlhandle="0x020000004f5d8600c681164acba4aa957a0615c5021a07ab0000000000000000000000000000000000000000">

    unknown </frame>

    </executionStack>

    <inputbuf>

    update deadlock1

    set val = 'Two'

    where id = 20 </inputbuf>

    </process>

    the other process in the process list will be the one that succeeded.

    <process id="process207c1d848" taskpriority="0" logused="144" waitresource="RID: 7:1:327:19" waittime="7511" ownerId="4329151" transactionname="user_transaction" lasttranstarted="2014-10-07T08:53:27.893" XDES="0x23167b000" lockMode="U" schedulerid="2" kpid="10128" status="suspended" spid="53" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2014-10-07T08:53:33.260" lastbatchcompleted="2014-10-07T08:53:27.990" lastattention="2014-10-07T08:43:59.500" clientapp="Microsoft SQL Server Management Studio - Query" hostname="ITS1019777" hostpid="9416" loginname="ADMIN\rklimes" isolationlevel="read committed (2)" xactid="4329151" currentdb="7" lockTimeout="4294967295" clientoption1="671090784" clientoption2="390200">

    <executionStack>

    <frame procname="adhoc" line="1" stmtstart="58" stmtend="152" sqlhandle="0x02000000cc61311e914cc8e7d6441c7b2a0f1b42e42f89b50000000000000000000000000000000000000000">

    unknown </frame>

    <frame procname="adhoc" line="1" stmtend="94" sqlhandle="0x02000000228bbc0f7c4eb2b6fda3f323542a1d043e28550a0000000000000000000000000000000000000000">

    unknown </frame>

    </executionStack>

    <inputbuf>

    update deadlock2

    set val = 'Two'

    where id = 20 </inputbuf>

    </process>

    further down in the <resource-list>, it will show which objects were involved in the deadlock

    <resource-list>

    <ridlock fileid="1" pageid="325" dbid="7" objectname="test.dbo.deadlock1" id="lock2066b1e00" mode="X" associatedObjectId="72057594040745984">

    <owner-list>

    <owner id="process207c1d848" mode="X" />

    </owner-list>

    <waiter-list>

    <waiter id="process207c1d468" mode="U" requestType="wait" />

    </waiter-list>

    </ridlock>

    <ridlock fileid="1" pageid="327" dbid="7" objectname="test.dbo.deadlock2" id="lock2066b1300" mode="X" associatedObjectId="72057594040811520">

    <owner-list>

    <owner id="process207c1d468" mode="X" />

    </owner-list>

    <waiter-list>

    <waiter id="process207c1d848" mode="U" requestType="wait" />

    </waiter-list>

    </ridlock>

    </resource-list>

    here is the entire deadlock report

    <event name="xml_deadlock_report" package="sqlserver" timestamp="2014-10-07T13:53:40.772Z">

    <data name="xml_report">

    <value>

    <deadlock>

    <victim-list>

    <victimProcess id="process207c1d468" />

    </victim-list>

    <process-list>

    <process id="process207c1d468" taskpriority="0" logused="144" waitresource="RID: 7:1:325:19" waittime="659" ownerId="4329139" transactionname="user_transaction" lasttranstarted="2014-10-07T08:53:23.443" XDES="0x231981000" lockMode="U" schedulerid="2" kpid="11032" status="suspended" spid="55" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2014-10-07T08:53:40.113" lastbatchcompleted="2014-10-07T08:53:23.530" lastattention="2014-10-07T08:42:54.587" clientapp="Microsoft SQL Server Management Studio - Query" hostname="ITS1019777" hostpid="9416" loginname="ADMIN\rklimes" isolationlevel="read committed (2)" xactid="4329139" currentdb="7" lockTimeout="4294967295" clientoption1="671090784" clientoption2="390200">

    <executionStack>

    <frame procname="adhoc" line="1" stmtstart="58" stmtend="152" sqlhandle="0x02000000c1450b241d05fed07cbd918b838c810dd57b293c0000000000000000000000000000000000000000">

    unknown </frame>

    <frame procname="adhoc" line="1" stmtend="94" sqlhandle="0x020000004f5d8600c681164acba4aa957a0615c5021a07ab0000000000000000000000000000000000000000">

    unknown </frame>

    </executionStack>

    <inputbuf>

    update deadlock1

    set val = 'Two'

    where id = 20 </inputbuf>

    </process>

    <process id="process207c1d848" taskpriority="0" logused="144" waitresource="RID: 7:1:327:19" waittime="7511" ownerId="4329151" transactionname="user_transaction" lasttranstarted="2014-10-07T08:53:27.893" XDES="0x23167b000" lockMode="U" schedulerid="2" kpid="10128" status="suspended" spid="53" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2014-10-07T08:53:33.260" lastbatchcompleted="2014-10-07T08:53:27.990" lastattention="2014-10-07T08:43:59.500" clientapp="Microsoft SQL Server Management Studio - Query" hostname="ITS1019777" hostpid="9416" loginname="ADMIN\rklimes" isolationlevel="read committed (2)" xactid="4329151" currentdb="7" lockTimeout="4294967295" clientoption1="671090784" clientoption2="390200">

    <executionStack>

    <frame procname="adhoc" line="1" stmtstart="58" stmtend="152" sqlhandle="0x02000000cc61311e914cc8e7d6441c7b2a0f1b42e42f89b50000000000000000000000000000000000000000">

    unknown </frame>

    <frame procname="adhoc" line="1" stmtend="94" sqlhandle="0x02000000228bbc0f7c4eb2b6fda3f323542a1d043e28550a0000000000000000000000000000000000000000">

    unknown </frame>

    </executionStack>

    <inputbuf>

    update deadlock2

    set val = 'Two'

    where id = 20 </inputbuf>

    </process>

    </process-list>

    <resource-list>

    <ridlock fileid="1" pageid="325" dbid="7" objectname="test.dbo.deadlock1" id="lock2066b1e00" mode="X" associatedObjectId="72057594040745984">

    <owner-list>

    <owner id="process207c1d848" mode="X" />

    </owner-list>

    <waiter-list>

    <waiter id="process207c1d468" mode="U" requestType="wait" />

    </waiter-list>

    </ridlock>

    <ridlock fileid="1" pageid="327" dbid="7" objectname="test.dbo.deadlock2" id="lock2066b1300" mode="X" associatedObjectId="72057594040811520">

    <owner-list>

    <owner id="process207c1d468" mode="X" />

    </owner-list>

    <waiter-list>

    <waiter id="process207c1d848" mode="U" requestType="wait" />

    </waiter-list>

    </ridlock>

    </resource-list>

    </deadlock>

    </value>

    </data>

    <action name="plan_handle" package="sqlserver">

    <value>0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</value>

    </action>

    <action name="database_name" package="sqlserver">

    <value>master</value>

    </action>

    <action name="database_id" package="sqlserver">

    <value>1</value>

    </action>

    <action name="client_hostname" package="sqlserver">

    <value />

    </action>

    <action name="client_app_name" package="sqlserver">

    <value />

    </action>

    <action name="process_id" package="package0">

    <value>6968</value>

    </action>

    </event>

    Bob
    -----------------------------------------------------------------------------
    How to post to get the best help[/url]

  • My XML looks like this and can't decipher. I do get the the object id to tell me which table. But can't tell for sure what query was running at the time and which process got killed and which one succeeded during the dead lock.

    <event name="lock_deadlock" package="sqlserver" timestamp="2014-09-27T19:59:20.473Z">

    <data name="resource_type">

    <value>5</value>

    <text>OBJECT</text>

    </data>

    <data name="mode">

    <value>1</value>

    <text>SCH_S</text>

    </data>

    <data name="owner_type">

    <value>1</value>

    <text>Transaction</text>

    </data>

    <data name="transaction_id">

    <value>8784892</value>

    </data>

    <data name="database_id">

    <value>13</value>

    </data>

    <data name="lockspace_workspace_id">

    <value>0x00000010c8849e50</value>

    </data>

    <data name="lockspace_sub_id">

    <value>1</value>

    </data>

    <data name="lockspace_nest_id">

    <value>0</value>

    </data>

    <data name="resource_0">

    <value>1195151303</value>

    </data>

    <data name="resource_1">

    <value>0</value>

    </data>

    <data name="resource_2">

    <value>0</value>

    </data>

    <data name="deadlock_id">

    <value>8772</value>

    </data>

    <data name="object_id">

    <value>1195151303</value>

    </data>

    <data name="associated_object_id">

    <value>1195151303</value>

    </data>

    <data name="duration">

    <value>245281000</value>

    </data>

    <data name="resource_description">

    <value />

    </data>

    <data name="database_name">

    <value />

    </data>

    <action name="plan_handle" package="sqlserver">

    <value>06000d004597b6073068328b0900000001000000000000000000000000000000000000000000000000000000</value>

    </action>

    <action name="database_name" package="sqlserver">

    <value>DB1</value>

    </action>

    <action name="database_id" package="sqlserver">

    <value>13</value>

    </action>

    <action name="client_hostname" package="sqlserver">

    <value>DIBS856W7LT</value>

    </action>

    <action name="client_app_name" package="sqlserver">

    <value>Microsoft SQL Server Management Studio</value>

    </action>

    <action name="process_id" package="package0">

    <value>7668</value>

    </action>

    </event>

  • You can add the following event to the session

    ADD EVENT sqlserver.blocked_process_report(

    ACTION(package0.process_id,sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_id,sqlserver.database_name,sqlserver.plan_handle)

    WHERE ([sqlserver].[database_name]=N'Db1' OR [sqlserver].[database_name]=N'Db2' OR [sqlserver].[database_name]=N'DB3')),

    and then see if you have more details.

    Igor Micev,My blog: www.igormicev.com

  • the xml you posted is from the lock_deadlock event. you need the xml from the xml_deadlock_report event.

    Bob
    -----------------------------------------------------------------------------
    How to post to get the best help[/url]

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply