r/SQLServer • u/SkullLeader • 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?
•
u/AutoModerator Nov 30 '25
After your question has been solved /u/SkullLeader, please reply to the helpful user's comment with the phrase "Solution verified".
This will not only award a point to the contributor for their assistance but also update the post's flair to "Solved".
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/dbrownems Microsoft Employee Nov 30 '25 edited Dec 01 '25
First, this:
>Eventually (hours later) SQL server choses one to kill which frees the deadlock.
Isn't the expected deadlock behavior. SQL Server should detect and resolve deadlocks in seconds, not minutes or hours. Are you actually getting deadlock graphs in the system_health session?
You may have two blocking issues. An hours-long lock held by a session, blocking other sessions but not causing a deadlock, and then eventually this session wakes up, gets into a real deadlock and gets killed.
>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?
Yes, sp_getapplock is a good way to avoid deadlocks.
>My main concern here is that if, for any reason, an instance of the proc fails to call sp_releaseapplock
Always use `@LockOwner=Transaction` and `SET XACT_ABORT=ON` and errors will rollback the transaction which will release the lock.
8
u/LredF Nov 30 '25
I used to be part of a team that had to take over an app from a company we bought. They had a huge 400 column table that many columns can be updated. We broke that shit up into several tables, added PKs, FKs, indexes and forced every process that did updates to using the PK.
The users were thrilled. A bogged down app was flying.
4
u/Anlarb 1 Nov 30 '25
What is the nature of the updates (can they be broken up into batches?)
RCSI might help too, give it a try in a test environment.
2
u/B1zmark 1 Dec 01 '25
I won't get into the politics because the solution is 100% "Fix the code". But there are some thing you can do to that will be suboptimal generally, but could lead to better performance under the scenario you're in.
-Change the Clustered index on the tables being locked on most often - make it a random (or pseudo random) field. This disperses data but only really works on large tables, or tables were rows are very large and page count is high.
-Use "WITH ROWLOCK" wherever you can on the offending SP, but this might not work if other queries are blocking it.
-Change the tables to have no lock escalation (similar to ROWLOCK hint) but this hammers TempDB so make sure that it has enough space and is on a sufficiently fast disk.
To be clear, apart from changing the clustered index, these options aren't great generally. And changing the CI is also a totally situational thing - because in an OLTP system it means your hotspot pages aren't going to be in memory and performance on the front end might suffer in general because of that.
2
u/Commercial-Trash-606 Dec 01 '25
Before you get into technicalities of how to troubleshoot deadlocks or micromanage locking, let's look at the obvious. Are the indexes ready to locate the rows UPDATE needs easily? Are you doing things where you read from table, do a long-running process and then write back into the same table....in one transaction? etc.
What's your transaction isolation level? Do you use (nolock) or dirty read when that's good enough?
How's the hardware side of things? Enough IOPS? Enough RAM?
I don't know your system, obviously, but there are basic, basic, things you can do to massively improve the situation, I bet.
2
u/No_Resolution_9252 Nov 30 '25
avoiding deadlocks is a pretty large area.
Few common items:
1
u/Dry_Author8849 Nov 30 '25
I don't think sp_getapplock will help here. Analyze the lock chain.
Use set lock_timeout. In your case, it will be the fastest fix.
Cheers!
1
u/MerlinTrashMan Nov 30 '25
How many rows are being updated in each statement? It sounds counter intuitive but if the update has the potential to update multiple non-sequential pages then the procs will actually run faster if they get an entire table lock. In situations like these, I find using the standard
begin tran, begin try, update with tablock, commit, end try, begin catch, capture error data, rollback, end catch raise error with captured data
is significantly more reliable and performant then the applock style you are using.
1
u/SirGreybush 1 Nov 30 '25
Also look into how the indexe(s) are created if you have lock issues within the same table, often happens in an OLTP + ERP setting, because indexes use pages, and more than one record can be within the same page, so a Lock on record A can affect a lock on record H.
I've previously used RedGate bought tools with monitoring, it really helped. My customers usually have the budget and I'm a consultant.
However as an employee, where often you cannot ask for a budget, look at DBAtools.io
When the DBA clears out a lock, he probably looks at the current lock(s), picks one and does a KILL #SPID then asks, is the issue resolved?
Semaphore locking I'd say was popular a very long time ago, back when we didn't have page or row lock mechanisms.
Know that even with a select statement WITH NO LOCK specified, it can prevent a transaction requiring an exclusive lock on a table from occurring and entering a wait state.
If you modify the indexe(s) to be by row, rather than by page, the size will be larger on disk, and more IO will be required, however it will drastically reduce the problem.
Finally, check to make sure there is no Triggers on that or those tables, they are often a source of pain. Triggers are usually not required, they are meant as a work-around.
That said, it is possible to temporarily disable them, do some work, reenable them.
1
u/PaulPhxAz 1 Nov 30 '25
I don't think there's enough information here. If you're explicitely locking ... and you're running into deadlocks, then I think your flow is wrong in general. IE, if you have a competing process trying to get the lock, it should just be waiting and letting someone else run. The total run-time should be the exact same as if you had one process running them in a non-competing series.
Can you change the process:
* Instead of running directly, add a row in a MyProcessLog table with Completed = 0, Created = Now
* Agent that runs every minute or sooner and picks the next one off the table and runs it ( it could even loop and run up to 10 in a single series ).
Now you have a single process executing in a series. Remove any "locking".
But I'll give some general advice:
See if you can chunk the data into smaller updates.
Remove foreign keys.
Do explicit updlock/halocks.
Prep all your data outside of your transaction into a temp table and then do the update from that indexed temp table.
In SQL Agent make a job that checks what's locked, and send alerts if they go past X minutes ( you have to keep track yourself in a small table )
1
u/Admirable_Writer_373 Dec 01 '25
My guess is there’s another process that’s quite similar in the tables it’s updating but not in the same order, and that is actually at the bottom of this cluster. So, gather more information
1
u/Commercial-Trash-606 Dec 03 '25
How about a rewrite? Instead of letting different threads update against the table whenever they feel like, you make them write into "update candidate" table first, and then a very frequent job would act on that single table in single thread? Basically a variation of queueing technique?
1
u/Commercial-Trash-606 Dec 03 '25
If you do go ahead with micro-managing the locks/retries, one way that never leaves garbage behind (locks and throws away the key walk away), is to rewrite the proc doing update as follows: (a) is the at-mark.
DECLARE (a)first try_failed bit = 0
BEGIN TRY
.... do your update and stuff
END TRY
BEGIN CATCH
SET (a) first_try_failed = 1
END CATCH
if ( (a)first_try_failed = 1)
BEGIN
WAITFOR DELAY ... (however long you want to wait)
... try your update again
END
If you implement the deadlock victim problem workaround like this, you never leave semaphore or lock in undesirable state. Every stored proc call now is patient enough to do two tries instead of blowing up on the first time being chosen as the deadlock victim. You can define the (a)second_try_failed bit and do it one more time, so that the stored proc is patient enough to try 3 times before calling it quits, and when it does, do so gracefully and not a blow-up.
That's what I'd do in your scenario. You are correct to worry about leaving locks forever accidentally. It does happen and then nothing works.
1
u/Informal_Pace9237 Nov 30 '25
How much time does your update SQL run. Is your update atomic? Does your update column have an index on them or they are part of an index? Is there a possibility you can queue these updates?
I think you are using named locks. In most databases named locks are released automatically after commit or rollback. Is it not happening in your situation?
Regarding avoidance of deadlocks there are multiple ways to attempt but all will depend on my questions above.
-1
u/Simple_Brilliant_491 Dec 01 '25
I just launched a new tuning tool called AI SQL Tuner (AI-powered SQL Server Tuning Made Simple - AI SQL Tuner). It will look at the recent deadlocks in the system health session and sends the results to AI to identify the code it involved. It then grabs the code and sends that to AI to provide recommended fixes. You can see an example of the results here: Dirty Deadlocks, Found Dirt Cheap: Fix SQL Server Deadlocks Fast - AI SQL Tuner. Here is the code it uses to get the recent deadlocks (same place that https://www.reddit.com/user/dbrownems/ mentioned):
SELECT TOP (20)
timestamp_utc,
CAST(event_data AS XML) AS DeadlockGraph,
CAST(event_data AS VARCHAR(MAX)) AS DeadlockData
FROM sys.fn_xe_file_target_read_file('system_health*.xel', NULL, NULL, NULL)
WHERE object_name = 'xml_deadlock_report'
ORDER BY timestamp_utc DESC;
The software also has options for code review and index tuning that you and your DBA may benefit from. It is free to use with SQL Developer, $100 for an annual subscription to use with SQL Standard and $200/year if you have Enterprise.
(I know of lot of SQL folks are skeptical of AI, but if you give it data it needs, with the right prompts and options, and use the newer reasoning models like GPT-5 it can work really well. See https://aisqltuner.com/2025/11/25/4-tips-for-using-ai-with-sql-server/ for more details.)
0
u/[deleted] Nov 30 '25
[deleted] — view deleted comment