r/SQLServer • • Nov 30 '25

Question Deadlock avoidance techniques?

Long story short, we have a stored proc that does an UPDATE on a specific table. Our job scheduler can be running numerous instances of this proc at the same time. We are seeing deadlocks occur because these UPDATEs are causing page level locks on the table being updated and of course numerous instances are each acquiring page locks needed by the other instances. Eventually (hours later) SQL server choses one to kill which frees the deadlock. Ok in the sense that we can just rerun the killed instance, but really bad because each job needs to rerun every few minutes, so holding things up for hours causes huge issues for us.

In our proc, would using sp_getapplock prior to executing the UPDATE and then using sp_releaseapplock right after the UPDATE completes be a good way to mitigate the issue we are seeing? Something like the below, but we might make several attempts to obtain the lock a few seconds apart before giving up and calling RAISERROR.

DECLARE u/result INT;

EXEC u/result = sp_getapplock

u/Resource = 'MySemaphore',

u/LockMode = 'Exclusive',

u/LockOwner = 'Session',

u/LockTimeout = 1000; -- ms

IF u/result < 0

RAISERROR('Failed to acquire semaphore', 16, 1);

ELSE
BEGIN

<our UPDATE>

END

EXEC sp_releaseapplock u/Resource = 'MySemaphore', u/LockOwner = 'Session';

My main concern here is that if, for any reason, an instance of the proc fails to call sp_releaseapplock we'd be in worse shape than we are currently, because now (I think) we need to get a DBA involved to go and manually clear out the lock that was created, while all instances of the proc that get run in the meantime fail to acquire the lock and so do not do this UPDATE. Is there some way to guarantee that sp_releaseapplock will be called no matter what?

Are there any other approaches to avoiding these deadlocks that might be better?

10 Upvotes

32 comments sorted by

View all comments

Show parent comment

1

u/aModernSage Nov 30 '25

I would suggest thinking of this as "the app expects a single object, with all columns present" - not "the app expects a single table". Just to add the comment: The column order should hopefully be irrelevant to an app as well. Relying on column ordinals can be buggy, so best to just avoid it whenever possible

I'd also recommend looking into synonyms. That SQL Server object can be very helpful while breaking a large table object down into smaller units, as commentor had mentioned.