質問

I'm investigating a deadlock and used the following query (which I got from Jonathan Kehayias's Pluralsight course on deadlocks) to extract information from extended events regarding the deadlock:

SELECT  
    XEvent.query('data[@name="xml_report"]/value/deadlock') AS deadlock_graph 
FROM    (SELECT CAST([target_data] AS XML) AS TargetData 
         FROM sys.dm_xe_session_targets AS st 
         INNER JOIN sys.dm_xe_sessions AS s  
            ON [s].[address] = [st].[event_session_address] 
         WHERE [s].[name] = N'system_health' 
           AND [st].[target_name] = N'ring_buffer') AS Data 
CROSS APPLY TargetData.nodes ('RingBufferTarget/event[@name="xml_deadlock_report"]') AS XEventData (XEvent); 

Strangely that gave me a 1-row, 1-column resultset, containing an empty value. After a bit of digging I found out that it was due to the deadlock graph portion of the XML document that gets returned by the inner query wasn't valid XML. It looked like this:

malformed XML

As you can see the angle brackets have all been replaced by their HTML encoding. I don't understand how this happened because all I'm doing is querying the database directly from SSMS.

Anyway, once I realised the problem I was able to replace all the erroneous values with the proper angle brackets and my XML was valid, and I was able to examine the deadlock. I find this very strange though, does anyone know why the deadlock graph is coming back like this?

I should note that the rest of the XML (i.e. the rest of the events other than the deadlock graph) came back perfectly formed as you can see here: enter image description here

Here's my version info:

Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (X64) Jun 28 2012 08:36:30 Copyright (c) Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 6.1 (Build 7601: Service Pack 1) (Hypervisor)

Anyone have an idea as t what is going on? I find it rather peculiar.

thanks in advance

Jamie

役に立ちましたか?

解決

Jamie, I believe that's standard for XML wrapped within XML. This query works (courtesy of Kehayias)

SELECT CAST (
    REPLACE (
        REPLACE (
            XEventData.XEvent.value ('(data/value)[1]', 'varchar(max)'),
            '<victim-list>', '<deadlock><victim-list>'),
        '<process-list>', '</victim-list><process-list>')
    AS XML) AS DeadlockGraph
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 [name] = 'system_health') AS Data
CROSS APPLY TargetData.nodes ('//RingBufferTarget/event') AS XEventData (XEvent)
    WHERE XEventData.XEvent.value('@name', 'varchar(4000)') = 'xml_deadlock_report';

他のヒント

A straight copy/paste of Michael's code above caused an error:

Msg 9436, Level 16, State 1, Line 1

XML parsing: line 5, character 15, end tag does not match start tag

So I fiddled about a bit to get this:

SELECT CAST (
            XEventData.XEvent.value ('(data/value)[1]', 'varchar(max)')
    AS XML) AS DeadlockGraph
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 [name] = 'system_health') AS Data
CROSS APPLY TargetData.nodes ('//RingBufferTarget/event') AS XEventData (XEvent)
    WHERE XEventData.XEvent.value('@name', 'varchar(4000)') = 'xml_deadlock_report';

which seems to work fine.

HTH

JT

Here is another version (for SQL Server 2008R2 (SP2)):

SELECT CAST(CAST(st.target_data AS xml).query('RingBufferTarget/event[@name="xml_deadlock_report"]/data/value/text()').value('.', 'nvarchar(MAX)') AS xml)
FROM sys.dm_xe_session_targets st
    INNER JOIN sys.dm_xe_sessions s 
        ON st.event_session_address = s.address
WHERE s.name = 'system_health' AND st.target_name = 'ring_buffer'
FOR XML PATH('deadlock-list');
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top