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:
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:
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';