Deadlock in SQL SERVER 2014 - sql-server

I am experiencing sporadic deadlock behaviour when my application tries to execute any DROP TABLE statement against my database.
I tried capturing the deadlock graph and this is the output from deadlock xml
<deadlock>
<victim-list>
<victimProcess id="process2bddc28" />
</victim-list>
<process-list>
<process id="process2bddc28" taskpriority="0" logused="0" waitresource="KEY: 6:281474978938880 (85262e079cd2)" waittime="1900" ownerId="76493296" transactionname="user_transaction" lasttranstarted="2016-02-22T05:58:15.867" XDES="0x621cb38e0" lockMode="S" schedulerid="6" kpid="10748" status="suspended" spid="119" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2016-02-22T05:58:16.080" lastbatchcompleted="2016-02-22T05:58:16.080" lastattention="1900-01-01T00:00:00.080" hostname="DEV01" hostpid="13420" isolationlevel="read committed (2)" xactid="76493296" currentdb="6" lockTimeout="4294967295" clientoption1="671090784" clientoption2="128056">
<executionStack>
<frame procname="mssqlsystemresource.sys.sp_columns_100" line="138" stmtstart="12020" stmtend="17532" sqlhandle="0x0300ff7f78a964e13656e600e7a5000001000000000000000000000000000000000000000000000000000000">
</frame>
</executionStack>
<inputbuf>Proc [Database Id = 32767 Object Id = -513496712]</inputbuf>
</process>
<process id="process45c8ca8" taskpriority="0" logused="6748" waitresource="KEY: 6:562949956108288 (5db581fe7a0b)" waittime="1900" ownerId="76493663" transactionname="user_transaction" lasttranstarted="2016-02-22T05:58:16.087" XDES="0x16aeded90" lockMode="X" schedulerid="1" kpid="18268" status="suspended" spid="129" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2016-02-22T05:58:16.090" lastbatchcompleted="2016-02-22T05:58:16.090" lastattention="1900-01-01T00:00:00.090" hostname="DEV01" hostpid="20128" isolationlevel="read committed (2)" xactid="76493663" currentdb="6" lockTimeout="4294967295" clientoption1="671090784" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" sqlhandle="0x01000600702bfd0bd083047f0600000000000000000000000000000000000000000000000000000000000000">
DROP TABLE "dbo"."USER_TABLE_1" </frame>
</executionStack>
<inputbuf>
DROP TABLE "dbo"."USER_TABLE_1" </inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="281474978938880" dbid="6" objectname="DEV_01.sys.sysschobjs" indexname="clst" id="lock35af27b80" mode="X" associatedObjectId="281474978938880">
<owner-list><owner id="process45c8ca8" mode="X" /></owner-list>
<waiter-list><waiter id="process2bddc28" mode="S" requestType="wait" /></waiter-list>
</keylock>
<keylock hobtid="562949956108288" dbid="6" objectname="DEV_01.sys.syscolpars" indexname="nc" id="lock453c34600" mode="U" associatedObjectId="562949956108288">
<owner-list><owner id="process2bddc28" mode="S" /></owner-list>
<waiter-list><waiter id="process45c8ca8" mode="X" requestType="convert" /></waiter-list>
</keylock>
</resource-list>
</deadlock>
This behaviour was not seen in earlier versions of SQL Server. Any thoughts on this?

Related

Does after insert trigger fire on update?

I am trying to understand what is causing this deadlock.
The update command in the first process is the result of the trigger from the insert command. So I am trying to understand if the insert trigger fire on update?
<deadlock>
<victim-list>
<victimProcess id="process1fb169468" />
</victim-list>
<process-list>
<process id="process1fb169468" taskpriority="0" logused="2044" waitresource="KEY: 5:72057598307401728 (c73677424644)" waittime="61674" ownerId="34455794" transactionname="user_transaction" lasttranstarted="2022-07-11T23:24:01.387" XDES="0x56bee18e0" lockMode="U" schedulerid="6" kpid="9800" status="suspended" spid="94" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2022-07-11T23:24:01.387" lastbatchcompleted="2022-07-11T23:24:01.387" lastattention="2022-07-11T22:02:36.350" clientapp="Vizion WMS" hostname="AGROXAWORK02" hostpid="10484" loginname="vizion" isolationlevel="read committed (2)" xactid="34455794" currentdb="5" currentdbname="WMSData" lockTimeout="4294967295" clientoption1="671219744" clientoption2="128056">
<executionStack>
<frame procname="WMSData.dbo.UpdateDes" line="5" stmtstart="154" stmtend="298" sqlhandle="0x030005000caa6e4b5b56dd0089a6000000000000000000000000000000000000000000000000000000000000">
update inventory set designation = 'FROZEN' where designation <> 'FROZEN </frame>
<frame procname="WMSData.dbo.PostReceipt" line="101" stmtstart="7960" stmtend="10026" sqlhandle="0x0300050071b68341909a680037ae000001000000000000000000000000000000000000000000000000000000">
INSERT INTO [wmsdata].[dbo].[Inventory]
([Customer],[Product],[Row],[Rack],[Slot],[Pallets],[Cases],[Net],[ProductDate],[ReceiverNumber],[LotNumber],[ReceiveDate],[StorThru],[Status],[PalletNumber],[CustomerPallet],[StartTime],[StopTime],[BornOnDate],[EnteredBy],[ImportOrManual],[FullPalletQty],[PalletQtyReceived],[CustomerReference],lpnumber,fcgpallet,designation,mark,invoptional1,invoptional2,invoptional3,invoptional4,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,rsrate)
select [Customer],[Product],[Row],[Rack],[Slot],[Pallets],[Cases],[Net],[ProductDate],[Receiver],[LotNumber],convert(char(12),getdate(),101) as [ReceiveDate],[StorThru],[Status],[PalletNumber],[CustomerPallet],[StartTime],[StopTime],convert(char(12),getdate(),101) as [BornOnDate],[EnteredBy],[ImportOrManual],[FullPalletQty],cases as [PalletQtyReceived], #r, lpnumber, fcgpallet,designation,mark,invoptional1,invoptional2,invoptional3,invoptional4,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,rsrate
from [wmsdata].[dbo].receipts with (nolock) where palletnum </frame>
<frame procname="adhoc" line="1" sqlhandle="0x0100050098f50238607f6f0b0500000000000000000000000000000000000000000000000000000000000000">
exec dbo.postreceipt '149496','RDAY' </frame>
</executionStack>
<inputbuf>
exec dbo.postreceipt '149496','RDAY' </inputbuf>
</process>
<process id="process46811bc28" taskpriority="0" logused="22480" waitresource="KEY: 5:72057598307401728 (771c8a402149)" waittime="4364" ownerId="34439550" transactionname="user_transaction" lasttranstarted="2022-07-11T23:22:34.483" XDES="0x5a043d770" lockMode="U" schedulerid="1" kpid="10752" status="suspended" spid="59" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2022-07-11T23:22:34.497" lastbatchcompleted="2022-07-11T23:22:33.330" lastattention="2022-07-11T23:17:04.210" clientapp="Vizion WMS" hostname="AGROXAWORK02" hostpid="12144" loginname="vizion" isolationlevel="read committed (2)" xactid="34439550" currentdb="5" currentdbname="WMSData" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="WMSData.dbo.PostShipment" line="161" stmtstart="16962" stmtend="17036" sqlhandle="0x0300050069a66001dd5e640021ac000001000000000000000000000000000000000000000000000000000000">
delete from inventory where cases <= </frame>
<frame procname="adhoc" line="1" sqlhandle="0x01000500f2278100b05d31790100000000000000000000000000000000000000000000000000000000000000">
exec Postshipment '257208', 'JJOHNSON' </frame>
</executionStack>
<inputbuf>
exec Postshipment '257208', 'JJOHNSON' </inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="72057598307401728" dbid="5" objectname="WMSData.dbo.Inventory" indexname="PK_Inventory" id="lock4e671e880" mode="X" associatedObjectId="72057598307401728">
<owner-list>
<owner id="process46811bc28" mode="X" />
</owner-list>
<waiter-list>
<waiter id="process1fb169468" mode="U" requestType="wait" />
</waiter-list>
</keylock>
<keylock hobtid="72057598307401728" dbid="5" objectname="WMSData.dbo.Inventory" indexname="PK_Inventory" id="lock52ec3d280" mode="X" associatedObjectId="72057598307401728">
<owner-list>
<owner id="process1fb169468" mode="X" />
</owner-list>
<waiter-list>
<waiter id="process46811bc28" mode="U" requestType="wait" />
</waiter-list>
</keylock>
</resource-list>
</deadlock>

How to solve deadlock detected by sql server?

I'm facing a deadlock problem, the sql server detects it and return an exception because it chose a process as a victim. I would like to avoid this problem, I tried to change the isolation level to read uncommitted and also add indexes but this did not resolve the problems, here is report that describe the deadlock problem:
<deadlock>
<victim-list>
<victimProcess id="process133b1e188" />
</victim-list>
<process-list>
<process id="process133b1e188" taskpriority="0" logused="5236" waitresource="RID: 18:1:3545:209" waittime="1939" ownerId="13564317" transactionname="implicit_transaction" lasttranstarted="2020-12-18T18:15:14.250" XDES="0x1a064a3a8" lockMode="U" schedulerid="2" kpid="23832" status="suspended" spid="71" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2020-12-18T18:15:14.280" lastbatchcompleted="2020-12-18T18:15:14.280" lastattention="1900-01-01T00:00:00.280" clientapp="jTDS" hostname="med" hostpid="123" loginname="med" isolationlevel="read uncommitted (1)" xactid="13564317" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="462" stmtend="592" sqlhandle="0x030012003d326d4a3bf62b0195ac000001000000000000000000000000000000000000000000000000000000"> UPDATE DEP SET name WHERE id = 2 </frame>
</executionStack>
</process>
<process id="process133b1e558" taskpriority="0" logused="30956" waitresource="KEY: 18:72057766865403904 (8c1a6a11df48)" waittime="1957" ownerId="13563835" transactionname="implicit_transaction" lasttranstarted="2020-12-18T18:15:14.113" XDES="0x1a064b668" lockMode="S" schedulerid="2" kpid="32596" status="suspended" spid="68" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2020-12-18T18:15:14.263" lastbatchcompleted="2020-12-18T18:15:14.263" lastattention="1900-01-01T00:00:00.263" clientapp="jTDS" hostname="med" hostpid="123" loginname="med" isolationlevel="read committed (2)" xactid="13563835" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="24" sqlhandle="0x0200000064dbc4257a329ecd9ef9f0056fe48ce548b17d2e0000000000000000000000000000000000000000">
SELECT * FROM Worker w WHERE (w.id = #P0 ) </frame>
</executionStack>
</process>
</process-list>
<resource-list>
<ridlock fileid="1" pageid="3545" dbid="18" objectname="DEP" id="lock185869680" mode="X" associatedObjectId="72057766864551936">
<owner-list>
<owner id="process133b1e558" mode="X" />
</owner-list>
<waiter-list>
<waiter id="process133b1e188" mode="U" requestType="wait" />
</waiter-list>
</ridlock>
<keylock hobtid="72057766865403904" dbid="18" objectname="Worker" indexname="worker_id" id="lock17d9fdb80" mode="X" associatedObjectId="72057766865403904">
<owner-list>
<owner id="process133b1e188" mode="X" />
</owner-list>
<waiter-list>
<waiter id="process133b1e558" mode="S" requestType="wait" />
</waiter-list>
</keylock>
</resource-list>
</deadlock>
Try reading about:
set deadlock priority high
You should use high or 10 for the session you don't want to be the victim.
And I would say isolation level read uncommitted is not a good choice.

SQL Server: Transaction deadlock on SQL with PK in WHERE clause

We have a Java application that uses a table AU_PROCESSQUEUE for following operations:
INSERT new job
UPDATE job status to READY | RUNNING
UPDATE associated workflow information for that job
DELETE the job once it is completed
Table DDL
CREATE TABLE AU_PROCESSQUEUE (
AU_JOB_ID varchar(80) NOT NULL ,
AU_PROCESSFLOW_ID varchar (50) NULL ,
AU_PROCESSFLOW_NAME varchar (64) NULL ,
AU_STATUS varchar (30) NULL ,
AU_SUBJECT varbinary (Max) NULL ,
AU_PROCESSFLOW_OBJECTADDRESS varchar (350) NULL ,
AU_JOB_METADATA varbinary(Max) NULL ,
AU_QUEUE_TIME datetime NULL ,
AU_START_TIME datetime NULL ,
AU_END_TIME datetime NULL ,
AU_INSTANCE_ID varchar (100) NULL ,
AU_PRIORITY numeric(18, 2) NULL ,
AU_NODE_NAME varchar (50) NULL ,
AU_MESSAGE varchar (2000) NULL ,
CONSTRAINT PK_AU_PROCESSQUEUE PRIMARY KEY(AU_JOB_ID)) ;
INDEXES
CREATE NONCLUSTERED INDEX [IX_AU_PROCESSQUEUE_AU_STATUS_AU_NODE_NAME]
ON [AdeptiaBackend].[dbo].[AU_PROCESSQUEUE] (
[AU_STATUS] ASC,
[AU_NODE_NAME] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = ON,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
CREATE NONCLUSTERED INDEX [IX_AU_PROCESSQUEUE_AUQUEUETIME]
ON [AdeptiaBackend].[dbo].[AU_PROCESSQUEUE] (
[AU_QUEUE_TIME] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = ON,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
There is a SELECT query that keeps on executing on this table periodically to load the QUEUED jobs ordered by queued time
Select * from AU_PROCESSQUEUE (NOLOCK)
WHERE (AU_STATUS = 'Queued' OR AU_STATUS = 'Ready To Resume')
and
( AU_NODE_NAME='localhost' OR AU_NODE_NAME IS NULL )
ORDER BY AU_QUEUE_TIME LIMIT 200
The Java application only process a fixed number of jobs at a time (defined as job concurrency say 60), so essentially, there are 60 different threads where following queries are being executed in parallel:
Update AU_PROCESSQUEUE
SET AU_NODE_NAME=#P0 ,
AU_INSTANCE_ID=#P1,
AU_PROCESSFLOW_OBJECTADDRESS=#P2,
AU_STATUS=#P3,
AU_START_TIME=#P4
WHERE AU_JOB_ID=#P5
DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0
Issue
Under high job concurrency, say 60, we are seeing transactions being deadlocked in SQL Server. Here is the deadlock trace:
<deadlock-list>
<deadlock victim="process48d914ca8">
<process-list>
<process id="process4cdda84e8" taskpriority="0" logused="0" waitresource="KEY: 18:72057594055098368 (666003c8ddef)" waittime="34201" ownerId="1603282" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:31.663" XDES="0x4cdff7770" lockMode="U" schedulerid="1" kpid="3892" status="suspended" spid="122" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:31.663" lastbatchcompleted="2018-01-09T10:12:31.663" lastattention="1900-01-01T00:00:00.663" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-UOEQMBQP08C" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1603282" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process48d914ca8" taskpriority="0" logused="0" waitresource="KEY: 18:72057594055098368 (666003c8ddef)" waittime="35028" ownerId="1603137" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:31.107" XDES="0x488c743b0" lockMode="U" schedulerid="4" kpid="2232" status="suspended" spid="286" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:31.107" lastbatchcompleted="2018-01-09T10:12:31.107" lastattention="1900-01-01T00:00:00.107" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-UOEQMBQP08C" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1603137" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process4c809dc28" taskpriority="0" logused="0" waitresource="PAGE: 18:1:3620 " waittime="33435" ownerId="1609771" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:57.623" XDES="0x48d905770" lockMode="IU" schedulerid="4" kpid="1712" status="suspended" spid="289" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:57.623" lastbatchcompleted="2018-01-09T10:12:57.620" lastattention="1900-01-01T00:00:00.620" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-UOEQMBQP08C" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1609771" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process4c7853468" taskpriority="0" logused="0" waitresource="PAGE: 18:1:4688 " waittime="33460" ownerId="1609641" transactionname="UPDATE" lasttranstarted="2018-01-09T10:12:57.237" XDES="0x4c77783b0" lockMode="U" schedulerid="2" kpid="5660" status="suspended" spid="326" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:57.237" lastbatchcompleted="2018-01-09T10:10:20.090" lastattention="1900-01-01T00:00:00.090" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-UOEQMBQP08C" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1609641" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="220" stmtend="522" sqlhandle="0x0200000086a2ea1594021304e59bc20f346ab6be8a632b340000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000),#P1 nvarchar(4000),#P2 nvarchar(4000),#P3 nvarchar(4000),#P4 datetime2,#P5 nvarchar(4000))Update AU_PROCESSQUEUE SET AU_NODE_NAME=#P0 , AU_INSTANCE_ID=#P1, AU_PROCESSFLOW_OBJECTADDRESS=#P2, AU_STATUS=#P3, AU_START_TIME=#P4 WHERE AU_JOB_ID=#P5 </inputbuf>
</process>
<process id="process4bd316ca8" taskpriority="0" logused="0" waitresource="KEY: 18:72057594055098368 (666003c8ddef)" waittime="35028" ownerId="1609113" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:55.737" XDES="0x48d66a3b0" lockMode="U" schedulerid="4" kpid="6312" status="suspended" spid="336" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:55.733" lastbatchcompleted="2018-01-09T10:12:55.733" lastattention="1900-01-01T00:00:00.733" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-7EFMLLQ3PKJ" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1609113" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process4bd316108" taskpriority="0" logused="0" waitresource="KEY: 18:72057594055098368 (666003c8ddef)" waittime="35028" ownerId="1607808" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:51.910" XDES="0x4d1e4a6d0" lockMode="U" schedulerid="4" kpid="9144" status="suspended" spid="266" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:51.910" lastbatchcompleted="2018-01-09T10:12:51.910" lastattention="1900-01-01T00:00:00.910" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-UOEQMBQP08C" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1607808" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process4bfc37468" taskpriority="0" logused="0" waitresource="KEY: 18:72057594055098368 (666003c8ddef)" waittime="35029" ownerId="1604201" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:36.360" XDES="0x4b89623b0" lockMode="U" schedulerid="4" kpid="2704" status="suspended" spid="535" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:36.360" lastbatchcompleted="2018-01-09T10:12:36.360" lastattention="1900-01-01T00:00:00.360" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-7EFMLLQ3PKJ" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1604201" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process4bd317468" taskpriority="0" logused="0" waitresource="KEY: 18:72057594055098368 (666003c8ddef)" waittime="35029" ownerId="1603983" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:35.400" XDES="0x47ff6c540" lockMode="U" schedulerid="4" kpid="3508" status="suspended" spid="160" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:35.400" lastbatchcompleted="2018-01-09T10:12:35.400" lastattention="1900-01-01T00:00:00.400" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-UOEQMBQP08C" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1603983" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
<process id="process4bd3168c8" taskpriority="0" logused="0" waitresource="PAGE: 18:1:3620 " waittime="8339" ownerId="1598759" transactionname="DELETE" lasttranstarted="2018-01-09T10:12:16.410" XDES="0x491786d90" lockMode="IU" schedulerid="4" kpid="6692" status="suspended" spid="350" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2018-01-09T10:12:16.410" lastbatchcompleted="2018-01-09T10:12:16.407" lastattention="1900-01-01T00:00:00.407" clientapp="Microsoft JDBC Driver for SQL Server" hostname="WIN-7EFMLLQ3PKJ" hostpid="0" loginname="sa" isolationlevel="read committed (2)" xactid="1598759" currentdb="18" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="40" stmtend="132" sqlhandle="0x0200000063980b37a86688199d73e7920178af18c95d90030000000000000000000000000000000000000000">
unknown </frame>
<frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 nvarchar(4000))DELETE from AU_PROCESSQUEUE WHERE AU_JOB_ID=#P0 </inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="72057594055098368" dbid="18" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" indexname="IX_AU_PROCESSQUEUE_AUQUEUETIME" id="lock4cfec4f80" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process48d914ca8" mode="U" requestType="wait"/>
</owner-list>
<waiter-list>
<waiter id="process4cdda84e8" mode="U" requestType="wait"/>
</waiter-list>
</keylock>
<keylock hobtid="72057594055098368" dbid="18" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" indexname="IX_AU_PROCESSQUEUE_AUQUEUETIME" id="lock4cfec4f80" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process4bd3168c8" mode="U"/>
</owner-list>
<waiter-list>
<waiter id="process48d914ca8" mode="U" requestType="wait"/>
</waiter-list>
</keylock>
<pagelock fileid="1" pageid="3620" dbid="18" subresource="FULL" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" id="lock4af764580" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process4c7853468" mode="U"/>
</owner-list>
<waiter-list>
<waiter id="process4c809dc28" mode="IU" requestType="wait"/>
</waiter-list>
</pagelock>
<pagelock fileid="1" pageid="4688" dbid="18" subresource="FULL" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" id="lock4cfe98600" mode="IU" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process4cdda84e8" mode="IU"/>
<owner id="process4bfc37468" mode="IU"/>
<owner id="process4bd317468" mode="IU"/>
<owner id="process48d914ca8" mode="IU"/>
</owner-list>
<waiter-list>
<waiter id="process4c7853468" mode="U" requestType="wait"/>
</waiter-list>
</pagelock>
<keylock hobtid="72057594055098368" dbid="18" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" indexname="IX_AU_PROCESSQUEUE_AUQUEUETIME" id="lock4cfec4f80" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process48d914ca8" mode="U" requestType="wait"/>
</owner-list>
<waiter-list>
<waiter id="process4bd316ca8" mode="U" requestType="wait"/>
</waiter-list>
</keylock>
<keylock hobtid="72057594055098368" dbid="18" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" indexname="IX_AU_PROCESSQUEUE_AUQUEUETIME" id="lock4cfec4f80" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process48d914ca8" mode="U" requestType="wait"/>
</owner-list>
<waiter-list>
<waiter id="process4bd316108" mode="U" requestType="wait"/>
</waiter-list>
</keylock>
<keylock hobtid="72057594055098368" dbid="18" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" indexname="IX_AU_PROCESSQUEUE_AUQUEUETIME" id="lock4cfec4f80" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process48d914ca8" mode="U" requestType="wait"/>
</owner-list>
<waiter-list>
<waiter id="process4bfc37468" mode="U" requestType="wait"/>
</waiter-list>
</keylock>
<keylock hobtid="72057594055098368" dbid="18" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" indexname="IX_AU_PROCESSQUEUE_AUQUEUETIME" id="lock4cfec4f80" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process48d914ca8" mode="U" requestType="wait"/>
</owner-list>
<waiter-list>
<waiter id="process4bd317468" mode="U" requestType="wait"/>
</waiter-list>
</keylock>
<pagelock fileid="1" pageid="3620" dbid="18" subresource="FULL" objectname="topco_cluster_bk.dbo.AU_PROCESSQUEUE" id="lock4af764580" mode="U" associatedObjectId="72057594055098368">
<owner-list>
<owner id="process4c809dc28" mode="IU" requestType="wait"/>
</owner-list>
<waiter-list>
<waiter id="process4bd3168c8" mode="IU" requestType="wait"/>
</waiter-list>
</pagelock>
</resource-list>
</deadlock>
</deadlock-list>
Questions
Why am I seeing a deadlock when both the UPDATE and DELETE query is being executed with PK in the WHERE clause? Isn't updating and deleting on PK considered the best scenario?
Will it help if I change my PK (column AU_JOB_ID) to INT (or BIGINT) instead of VARCHAR?
Will it help if I change status column (AU_STATUS) to refer lookup codes - that will change the column from VARCHAR to INT and help in faster comparison and search?
I am not an expert on database and SQL Server, so any help shall be appreciated. Thanks in advance!
Stepping back, it's going to be tough to tweak and troubleshoot your way through this problem, even if you were "an expert on database and SQL Server". The basic problem the three different data structures that need to be updated whenever you update or delete: the clustered index and two non-clustered indexes.
When using a table as a queue, you either need to keep the table structure extremely simplistic, or use Service Broker Queues, which is SQL Server's native in-database queuing feature.
So do what you would do in Java, synchronize access to your shared data. In SQL Server you use a transaction and an Application Lock to ensure that a session has brief, exclusive access to run a bit of code.
You seem to be focused on where the locks are occurring rather that what is causing them.
If the select was not taking locks then the update and delete should not have a problem.

SQL Server deadlock - with S and IX lock mode

Recently we encountered deadlock issues when more users using the system, below is the log details, appreciate if someone can provide guides on how to resolve this:
How S or IX lock mode causing the deadlock?
I am thinking probably to improve the query time for the select statement, so that it could release the lock sooner.
<TextData>
<deadlock-list>
<deadlock victim="process6956d8">
<process-list>
<process id="process6956d8" taskpriority="0" logused="0" waitresource="PAGE: 8:1:6923" waittime="1840" ownerId="67336413" transactionname="implicit_transaction" lasttranstarted="2013-07-08T16:22:34.897" XDES="0x124272700" lockMode="S" schedulerid="2" kpid="5472" status="suspended" spid="64" sbid="0" ecid="0" priority="0" transcount="1" lastbatchstarted="2013-07-08T16:22:34.900" lastbatchcompleted="2013-07-08T16:22:34.900" clientapp="Microsoft SQL Server JDBC Driver" hostname="VIRTIPH-MAS001" hostpid="0" loginname="prtcmt_user" isolationlevel="read committed (2)" xactid="67336413" currentdb="8" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="adhoc" line="1" sqlhandle="0x02000000ec69351d69911b1227018da1e1e67e60256cd8be">
select top 290 workplan0_.id as id112_, workplan0_.request_mode as request2_112_, workplan0_.cmt_status_id as cmt3_112_, workplan0_.requestType_rd_id as requestT4_112_, workplan0_.requested_dt as requested5_112_, workplan0_.generic_desc as generic6_112_, workplan0_.brand_desc as brand7_112_, workplan0_.strength as strength112_, workplan0_.prod_form_name as prod9_112_, workplan0_.pack_desc as pack10_112_, workplan0_.inv_uom_desc as inv11_112_, workplan0_.pack_uom_desc as pack12_112_, workplan0_.requestType_name as request13_112_, workplan0_.site_name as site14_112_, workplan0_.priority as priority112_, workplan0_.source as source112_, workplan0_.cmt_status_code as cmt17_112_, workplan0_.prev_cmt_status_code as prev18_112_, workplan0_.swmd_prod_id as swmd19_112_, workplan0_.hppl_lookslike_id as hppl20_112_, workplan0_.amt_id as amt21_112_, workplan0_.locked_dt as locked22_112_ from ipharm_workPlanView workplan0_ where workplan0_.requestType_rd_id=6 and workplan0_.cmt_status_id=33 order by workplan0_.generic_des </frame>
<frame procname="unknown" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
select top 290 workplan0_.id as id112_, workplan0_.request_mode as request2_112_, workplan0_.cmt_status_id as cmt3_112_, workplan0_.requestType_rd_id as requestT4_112_, workplan0_.requested_dt as requested5_112_, workplan0_.generic_desc as generic6_112_, workplan0_.brand_desc as brand7_112_, workplan0_.strength as strength112_, workplan0_.prod_form_name as prod9_112_, workplan0_.pack_desc as pack10_112_, workplan0_.inv_uom_desc as inv11_112_, workplan0_.pack_uom_desc as pack12_112_, workplan0_.requestType_name as request13_112_, workplan0_.site_name as site14_112_, workplan0_.priority as priority112_, workplan0_.source as source112_, workplan0_.cmt_status_code as cmt17_112_, workplan0_.prev_cmt_status_code as prev18_112_, workplan0_.swmd_prod_id as swmd19_112_, workplan0_.hppl_lookslike_id as hppl20_112_, workplan0_.amt_id as amt21_112_, workplan0_.locked_dt as locked22_112_ from ipharm_workPlanView workplan0_ where workplan0_.requestType_rd_id=6 and workplan0_.cmt_status_id=33 order by workplan0_.generic_de </inputbuf>
</process>
<process id="processc7c5ec58" taskpriority="0" logused="9904" waitresource="PAGE: 8:1:6922" waittime="936" ownerId="67336189" transactionname="implicit_transaction" lasttranstarted="2013-07-08T16:22:33.930" XDES="0x123635380" lockMode="IX" schedulerid="1" kpid="5232" status="suspended" spid="61" sbid="0" ecid="0" priority="0" transcount="2" lastbatchstarted="2013-07-08T16:22:35.930" lastbatchcompleted="2013-07-08T16:22:35.927" clientapp="Microsoft SQL Server JDBC Driver" hostname="VIRTIPH-MAS001" hostpid="0" loginname="prtcmt_user" isolationlevel="read committed (2)" xactid="67336189" currentdb="8" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058">
<executionStack>
<frame procname="adhoc" line="1" stmtstart="890" sqlhandle="0x02000000a89737254800790c60bb4d0fa72b9640012d8592">
update requestedProduct set version=#P0, create_user=#P1, create_dt=#P2, last_update_user=#P3, last_update_dt=#P4, requested_by=#P5, requested_dt=#P6, requested_byName=#P7, requested_byTitle=#P8, status_id=#P9, status_by=#P10, status_date=#P11, cmt_status_id=#P12, prev_cmt_status_id=#P13, prod_id=#P14, source_prod_id=#P15, hppl_lookslike_id=#P16, amt_id=#P17, fred_code=#P18, source=#P19, swmd_prod_id=#P20, locked_dt=#P21, locked_by=#P22, request_mode=#P23, original_request_mode=#P24, site_rd_id=#P25, priority_rd_id=#P26, submitted_priority_rd_id=#P27, requestType_rd_id=#P28, comment=#P29 where id=#P30 and version=#P31 </frame>
<frame procname="unknown" line="1" sqlhandle="0x000000000000000000000000000000000000000000000000">
unknown </frame>
</executionStack>
<inputbuf>
(#P0 int,#P1 nvarchar(4000),#P2 datetime,#P3 nvarchar(4000),#P4 datetime,#P5 bigint,#P6 datetime,#P7 nvarchar(4000),#P8 nvarchar(4000),#P9 bigint,#P10 bigint,#P11 datetime,#P12 bigint,#P13 bigint,#P14 int,#P15 int,#P16 nvarchar(4000),#P17 nvarchar(4000),#P18 nvarchar(4000),#P19 nvarchar(4000),#P20 varchar(8000),#P21 datetime,#P22 bigint,#P23 int,#P24 int,#P25 bigint,#P26 bigint,#P27 bigint,#P28 bigint,#P29 varchar(8000),#P30 bigint,#P31 int)update requestedProduct set version=#P0, create_user=#P1, create_dt=#P2, last_update_user=#P3, last_update_dt=#P4, requested_by=#P5, requested_dt=#P6, requested_byName=#P7, requested_byTitle=#P8, status_id=#P9, status_by=#P10, status_date=#P11, cmt_status_id=#P12, prev_cmt_status_id=#P13, prod_id=#P14, source_prod_id=#P15, hppl_lookslike_id=#P16, amt_id=#P17, fred_code=#P18, source=#P19, swmd_prod_id=#P20, locked_dt=#P21, locked_by=#P22, request_mode=#P23, original_request_mode=#P24, site_rd_id=#P25, priority_rd_id=#P26, submitted_priority_rd_id=#P27, requestType_r </inputbuf>
</process>
</process-list>
<resource-list>
<pagelock fileid="1" pageid="6923" dbid="8" objectname="MYDB.dbo.requestedProduct" id="lockedec4680" mode="IX" associatedObjectId="72057594227261440">
<owner-list>
<owner id="processc7c5ec58" mode="IX" />
</owner-list>
<waiter-list>
<waiter id="process6956d8" mode="S" requestType="wait" />
</waiter-list>
</pagelock>
<pagelock fileid="1" pageid="6922" dbid="8" objectname="MYDB.dbo.requestedProduct" id="lockfa82e880" mode="SIU" associatedObjectId="72057594227261440">
<owner-list>
<owner id="process6956d8" mode="S" />
</owner-list>
<waiter-list>
<waiter id="processc7c5ec58" mode="IX" requestType="convert" />
</waiter-list>
</pagelock>
</resource-list>
</deadlock>
</deadlock-list>
</TextData>)

Delete Stored Proc Deadlock in Sql Server

I am having the following deadlock in SQL Server 2005 with a specific delete stored proc and I can't figure out what I need to do to remedy it.
<deadlock-list>
<deadlock victim="processf3a868">
<process-list>
<process id="processcae718" taskpriority="0" logused="0" waitresource="KEY: 7:72057594340311040 (b50041b389fe)" waittime="62" ownerId="1678098" transactionguid="0x950057256032d14db6a2c553a39a8279" transactionname="user_transaction" lasttranstarted="2010-05-26T13:45:23.517" XDES="0x8306c370" lockMode="RangeS-U" schedulerid="1" kpid="2432" status="suspended" spid="59" sbid="0" ecid="0" priority="0" transcount="2" lastbatchstarted="2010-05-26T13:45:23.717" lastbatchcompleted="2010-05-26T13:45:23.717" clientapp=".Net SqlClient Data Provider" hostname="DEVELOPER01" hostpid="28104" loginname="DEVELOPER01\ServiceUser" isolationlevel="serializable (4)" xactid="1678098" currentdb="7" lockTimeout="4294967295" clientoption1="673185824" clientoption2="128056">
<executionStack>
<frame procname="DB.dbo.sp_DeleteSecuritiesRecords" line="13" stmtstart="708" stmtend="918" sqlhandle="0x030007008b6b662229b10c014f9d00000100000000000000">
DELETE FROM tSecuritiesRecords WHERE [FilingID] = #filingID AND [AccountID] = #accountID </frame>
</executionStack>
<inputbuf>
Proc [Database Id = 7 Object Id = 577137547] </inputbuf>
</process>
<process id="processf3a868" taskpriority="0" logused="0" waitresource="KEY: 7:72057594340311040 (4f00409af90f)" waittime="93" ownerId="1678019" transactionguid="0xb716547a8f7fdd40b342e5db6b3699fb" transactionname="user_transaction" lasttranstarted="2010-05-26T13:45:21.543" XDES="0x92617130" lockMode="X" schedulerid="3" kpid="13108" status="suspended" spid="57" sbid="0" ecid="0" priority="0" transcount="2" lastbatchstarted="2010-05-26T13:45:23.717" lastbatchcompleted="2010-05-26T13:45:23.717" clientapp=".Net SqlClient Data Provider" hostname="DEVELOPER01" hostpid="28104" loginname="DEVELOPER01\ServiceUser" isolationlevel="serializable (4)" xactid="1678019" currentdb="7" lockTimeout="4294967295" clientoption1="673185824" clientoption2="128056">
<executionStack>
<frame procname="DB.dbo.sp_DeleteSecuritiesRecords" line="13" stmtstart="708" stmtend="918" sqlhandle="0x030007008b6b662229b10c014f9d00000100000000000000">
DELETE FROM tSecuritiesRecords WHERE [FilingID] = #filingID AND [AccountID] = #accountID </frame>
</executionStack>
<inputbuf>
Proc [Database Id = 7 Object Id = 577137547] </inputbuf>
</process>
</process-list>
<resource-list>
<keylock hobtid="72057594340311040" dbid="7" objectname="DB.dbo.tSecuritiesRecords" indexname="PK_tTransactions" id="lock82416380" mode="RangeS-U" associatedObjectId="72057594340311040">
<owner-list>
<owner id="processf3a868" mode="RangeS-U"/>
</owner-list>
<waiter-list>
<waiter id="processcae718" mode="RangeS-U" requestType="convert"/>
</waiter-list>
</keylock>
<keylock hobtid="72057594340311040" dbid="7" objectname="DB.dbo.tSecuritiesRecords" indexname="PK_tTransactions" id="lock825fd380" mode="RangeS-U" associatedObjectId="72057594340311040">
<owner-list>
<owner id="processcae718" mode="RangeS-S"/>
</owner-list>
<waiter-list>
<waiter id="processf3a868" mode="X" requestType="convert"/>
</waiter-list>
</keylock>
</resource-list>
</deadlock>
</deadlock-list>
You're missing an index:
create index idxSecuritiesAccountFiling
on tSecuritiesRecords (AccountID, FillingID);
Dues to the lack of index both DELETEs have to do range scans and acquire serializable locks to stabilize the DELETE operation. When they turn around and try to convert the S locks into X locks, they deadlock. With an index in place, the range locks acquired would not overlap.

Resources