Facing a deadlock in SQL server database and can see deadlock entry in SQL logs. How the log entries can be used to find the reason for this deadlock?
Deadlock information is captured by the system_health
Extended Events trace by default. No need to turn on additional trace flags.
Information from the xml_deadlock
event can be viewed from SSMS Object Explorer (Management-->Extended Events-->Sessions--system_health) or with T-SQL. Below is an example query to get the deadlock xml from the file target. You can also save the deadlock xml to a file with an xdl
extension and open the file in SSMS for a graphical view of the deadlock.
--get xml_deadlock_report from system_health session file target
WITH
--get full path to current system_health trace file
CurrentSystemHealthTraceFile AS (
SELECT CAST(target_data AS xml).value('(/EventFileTarget/File/@name)[1]', 'varchar(255)') AS FileName
FROM sys.dm_xe_session_targets
WHERE
target_name = 'event_file'
AND CAST(target_data AS xml).value('(/EventFileTarget/File/@name)[1]', 'varchar(255)') LIKE '%\system[_]health%'
)
--get trace folder name and add base name of system_health trace file with wildcard
, BaseSystemHealthFileName AS (
SELECT
REVERSE(SUBSTRING(REVERSE(FileName), CHARINDEX(N'\', REVERSE(FileName)), 255)) + N'system_health*.xel' AS FileNamePattern
FROM CurrentSystemHealthTraceFile
)
--get xml_deadlock_report events from all system_health trace files
, DeadLockReports AS (
SELECT CAST(event_data AS xml) AS event_data
FROM BaseSystemHealthFileName
CROSS APPLY sys.fn_xe_file_target_read_file ( FileNamePattern, NULL, NULL, NULL) AS xed
WHERE xed.object_name like 'xml_deadlock_report'
)
--display 10 most recent deadlocks
SELECT TOP 10
DATEADD(hour, DATEDIFF(hour, SYSUTCDATETIME(), SYSDATETIME()), event_data.value('(/event/@timestamp)[1]', 'datetime2')) AS LocalTime
, event_data AS DeadlockReport
FROM DeadLockReports
ORDER BY LocalTime ASC;
This code will display the error log which contains the query that creates the deadlock. IF OBJECT_ID('tempdb.dbo.ErrorLog') IS Not Null BEGIN DROP TABLE tempdb.dbo.ErrorLog END
CREATE TABLE tempdb.dbo.ErrorLog
(Id int IDENTITY (1, 1) NOT NULL,
logdate DATETIME, procInfo VARCHAR(10),
ERRORLOG VARCHAR(MAX))
-- insert the actual data from the Error log into our newly created table.
INSERT INTO tempdb.dbo.ErrorLog
EXEC master.dbo.sp_readerrorlog
declare @sql nvarchar(max)
set @sql='select logdate, procInfo, ERRORLOG from tempdb.dbo.ErrorLog
where Id >= (select top 1 id from tempdb.dbo.ErrorLog WHERE ERRORLOG Like
''%deadlock-list%'' order by id desc)'
select @SQL
Deadlock information can be captured in the SQL Server Error Log or by using Profiler / Server Side Trace.
You can turn on each of these separately or turn them on together.
To turn these on you can issue the following commands in a query window or you can add these as startup parameters. If these are turned on from a query window, the next time SQL Server starts these trace flags will not be active, so if you always want to capture this data the startup parameters is the best option.
DBCC TRACEON (1204, -1)
DBCC TRACEON (1222, -1)
Please refer to the following Links for more details