• SQLKnowItAll (2/27/2013)


    When I look at Jonathan's Extended Event Session Explorer (We are on 2008 R2) I DO see object names for the deadlocks. Any thoughts?

    I've always had to look things up by HobtId, depending on whether the object is a heap or a btree

    I usually prefer to get the output with one line per deadlocked process sort of like this:

    WITH log_deadlocks

    AS (

    SELECT CONVERT( xml, XEventData.XEvent.value('(data/value)[1]', 'varchar(max)')) as DeadlockGraph

    ,xEventData.xEvent.value('@timestamp', 'datetime') StartDate

    FROM (

    select CAST(target_data as xml) as TargetData

    from sys.dm_xe_session_targets st

    join sys.dm_xe_sessions s

    on s.address = st.event_session_address

    where s.name = 'system_health'

    ) AS Data

    CROSS APPLY TargetData.nodes ('//RingBufferTarget/event') AS XEventData (XEvent)

    WHERE XEventData.XEvent.value('@name', 'varchar(400)') = 'xml_deadlock_report'

    )

    SELECT

    DeadlockTime = StartDate

    ,ObjectDbName = DB_NAME( isnull(Reslist.KeyLock.value('@dbid', 'int'), Reslist2.RidLock.value('@dbid', 'int') ) )

    ,ObjectName = (

    select quotename(s.name) + '.' + quotename(o.name)

    from sys.partitions p with (nolock)

    join sys.objects o with (nolock)

    on o.object_id = p.object_id

    join sys.schemas s with (nolock)

    on s.schema_id = o.schema_id

    where p.hobt_id = isnull(

    Reslist.KeyLock.value('@associatedObjectId', 'bigint') -- lock is on an index

    ,Reslist2.RidLock.value('@associatedObjectId', 'bigint') -- lock is on a heap

    )

    )

    ,Spid = ProList.Process.value('@spid', 'int')

    ,Terminated = case when ProList.Process.value('@lastattention', 'datetime') is not null then 'Y' else 'N' end

    ,LastTranStarted = ProList.Process.value('@lasttranstarted', 'datetime')

    ,LoginName = ProList.Process.value('@loginname', 'sysname')

    ,IsolationLevel = ProList.Process.value('@isolationlevel', 'nvarchar(400)')

    ,InputBuf = ProList.Process.value('./inputbuf[1]', 'varchar(max)')

    ,InputBufLine = ProList.Process.value('./executionStack[1]/frame[1]/@line', 'int')

    ,SessionDbName = DB_NAME( ProList.Process.value('@currentdb', 'int') )

    ,HostName = ProList.Process.value('@hostname', 'nvarchar(400)')

    ,ClientApp = ProList.Process.value('@clientapp', 'nvarchar(400)')

    ,DeadlockGraph

    FROM Log_Deadlocks

    -- One row for each <process> node in <process-list>

    CROSS APPLY DeadlockGraph.nodes('//process-list/process') ProList(Process)

    -- Grab the first <keylock> from <resource-list> for an index lock

    OUTER APPLY DeadlockGraph.nodes('//resource-list/keylock[1]') ResList(KeyLock)

    -- Grab the first <ridlock> from <resource-list> for a heap lock

    OUTER APPLY DeadlockGraph.nodes('//resource-list/ridlock[1]') ResList2(RidLock)