When I get the deadlock graph xml in SQL Server, it shows me an executionStack for each process, e.g. (I have removed most of the attributes):
<process>
<executionStack>
<frame>INSERT INTO MYTABLE</frame>
<frame>INSERT INTO MYTABLE</frame>
</executionStack>
<inputbuf>INSERT INTO MYTABLE</inputbuf>
</process>
I know the list of frames does not contain all the frames in the transaction, but does it contain all the frames that are implicated in the deadlock, so I do not need to look any further? Or do I need to also examine the preceding sql statements in the same transaction to get a complete picture as to the cause of the deadlock?
Edit: to clarify further, the following can deadlock itself if run in two separate spids:
select * from mytable where column = @arg
delete from mytable where column = @arg
Will the deadlock graph show the select statement as well as the delete statement, or only the delete statement because that was the last statement in the transaction? Without seeing the select statement too, it is difficult to identify the correct fix (e.g., updlock hint on the select).
You need to examine the preceding sql statements in the same transaction.
The deadlock graph just shows you the call stack for the statements executing when the deadlock occurred. The statements that actually acquired the deadlocked resources may not be present. This is easily demonstrated as follows.
Connection 1
Connection 2 (Execute quickly after Conn 1)
Deadlock Graph
The assignments to column
foothat acquired the locks in the first place aren’t shown.(NB: Without the
GOstatement to make the statements separate batches you would in this case see the offending statement but in general the lock acquisition might occur further down the call stack so the statement that actually acquired the locks still wouldn’t be shown)