How It Works: SQL Server Locking WAIT_WITH_LOW_PRIORITY

Published Feb 06 2022 07:34 AM 1,078 Views
Microsoft

 

Move from: bobsql.com

 

I received a question from Jonathan as he read over prior posts on locking and I cannot take credit for the question nor the answer, just being the middle man for this transaction.

 

Question:

“The way I have understood lock partitioning and a regular index rebuild to interact, is that the final SCH-M lock for the object is taken by acquiring it across all of the lock partitions, in ascending order to avoid any deadlock conditions. More precisely I understood that it does not even attempt to acquire the SCH-M in lock partition 4 say, until it has acquired it successfully in lock partition 3.  Once the SCH-M is acquired in the final partition it's good to go. 

If we attempt the same with low priority waits for the index rebuild ... on a non-partitioned system it seems straightforward, if my SCH-M request is blocked by an existing SCH-S, and a new SCH-S request arrives, it is not blocked by me.  If we apply the same behavior on each lock partition in ascending order, then at the point where I am trying to acquire SCH-M in the final partition, that would mean I already have acquired it in all the lower partitions, and I would be blocking processes on every other scheduler.

 

Do you know how this works?” - Jonathan Kehayias

 

The developer’s (Panagiotis) answer:

 

The goal of WAIT_AT_LOW_PRIORITY is to avoid blocking any other requests while waiting for a lock.  In the case of lock partitioning, the lock might be acquired on a few partitions and then end up waiting on partition 4 because there is a conflicting lock being held there.  If we simply waited with low priority on partition 4, we would not block any new requests on partition 4, but since we are holding locks on earlier partitions the user requests would be blocked.  Based on that, when WAIT_AT_LOW_PRIORITY is used, we wait with low priority on the first partition and if acquired we attempt to lock all other partitions without waiting.  If we can’t take the lock on partition 4, we will unlock all earlier partitions to eliminate blocking and start waiting with low priority on 4.  Once the lock on partition 4 is acquired, we follow the same process of acquiring the next partitions without waiting, cycling back to partition 0 once we hit the last partition.

Bob Dorr

%3CLINGO-SUB%20id%3D%22lingo-sub-3122713%22%20slang%3D%22en-US%22%3EHow%20It%20Works%3A%20SQL%20Server%20Locking%20WAIT_WITH_LOW_PRIORITY%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3122713%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22WordSection1%22%3E%0A%3CH6%20class%3D%22MsoNormal%22%20id%3D%22toc-hId-1762583519%22%20id%3D%22toc-hId-1762583556%22%3EMove%20from%3A%20bobsql.com%3C%2FH6%3E%0A%3CP%20class%3D%22MsoNormal%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22MsoNormal%22%3EI%20received%20a%20question%20from%20Jonathan%20as%20he%20read%20over%20prior%20posts%20on%20locking%20and%26nbsp%3BI%20cannot%20take%20credit%20for%20the%20question%20nor%20the%20answer%2C%20just%20being%20the%20middle%20man%20for%20this%20transaction.%3C%2FP%3E%0A%3CP%20class%3D%22MsoNormal%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22MsoNormal%22%3E%3CSTRONG%3EQuestion%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%20class%3D%22MsoNormal%22%20style%3D%22margin-left%3A%20.5in%3B%22%3E%3CI%3E%E2%80%9CThe%20way%20I%20have%20understood%20lock%20partitioning%20and%20a%20regular%20index%20rebuild%20to%20interact%2C%20is%20that%20the%20final%20SCH-M%20lock%20for%20the%20object%20is%20taken%20by%20acquiring%20it%20across%20all%20of%20the%20lock%20partitions%2C%20in%20ascending%20order%20to%20avoid%20any%20deadlock%20conditions.%20More%20precisely%20I%20understood%20that%20it%20does%20not%20even%20attempt%20to%20acquire%20the%20SCH-M%20in%20lock%20partition%204%20say%2C%20until%20it%20has%20acquired%20it%20successfully%20in%20lock%20partition%203.%26nbsp%3B%20Once%20the%20SCH-M%20is%20acquired%20in%20the%20final%20partition%20it's%20good%20to%20go.%26nbsp%3B%3C%2FI%3E%3C%2FP%3E%0A%3CP%20class%3D%22MsoNormal%22%20style%3D%22margin-left%3A%20.5in%3B%22%3E%3CI%3EIf%20we%20attempt%20the%20same%20with%20low%20priority%20waits%20for%20the%20index%20rebuild%20...%20on%20a%20non-partitioned%20system%20it%20seems%20straightforward%2C%20if%20my%20SCH-M%20request%20is%20blocked%20by%20an%20existing%20SCH-S%2C%20and%20a%20new%20SCH-S%20request%20arrives%2C%20it%20is%20not%20blocked%20by%20me.%26nbsp%3B%20If%20we%20apply%20the%20same%20behavior%20on%20each%20lock%20partition%20in%20ascending%20order%2C%20then%20at%20the%20point%20where%20I%20am%20trying%20to%20acquire%20SCH-M%20in%20the%20final%20partition%2C%20that%20would%20mean%20I%20already%20have%20acquired%20it%20in%20all%20the%20lower%20partitions%2C%20and%20I%20would%20be%20blocking%20processes%20on%20every%20other%20scheduler.%3C%2FI%3E%3C%2FP%3E%0A%3CP%20class%3D%22MsoNormal%22%20style%3D%22margin-left%3A%20.5in%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22MsoNormal%22%20style%3D%22margin-left%3A%20.5in%3B%22%3E%3CI%3EDo%20you%20know%20how%20this%20works%3F%E2%80%9D%20-%20Jonathan%20Kehayias%3C%2FI%3E%3C%2FP%3E%0A%3CP%20class%3D%22MsoNormal%22%20style%3D%22margin-left%3A%20.5in%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22MsoNormal%22%3E%3CSTRONG%3EThe%20developer%E2%80%99s%20(Panagiotis)%20answer%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%20class%3D%22MsoNormal%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22MsoNormal%22%3EThe%20goal%20of%20WAIT_AT_LOW_PRIORITY%20is%20to%20avoid%20blocking%20any%20other%20requests%20while%20waiting%20for%20a%20lock.%26nbsp%3B%20In%20the%20case%20of%20lock%20partitioning%2C%20the%20lock%20might%20be%20acquired%20on%20a%20few%20partitions%20and%20then%20end%20up%20waiting%20on%20partition%204%20because%20there%20is%20a%20conflicting%20lock%20being%20held%20there.%26nbsp%3B%20If%20we%20simply%20waited%20with%20low%20priority%20on%20partition%204%2C%20we%20would%20not%20block%20any%20new%20requests%20on%20partition%204%2C%20but%20since%20we%20are%20holding%20locks%20on%20earlier%20partitions%20the%20user%20requests%20would%20be%20blocked.%26nbsp%3B%20Based%20on%20that%2C%20when%20WAIT_AT_LOW_PRIORITY%20is%20used%2C%20we%20wait%20with%20low%20priority%20on%20the%20first%20partition%20and%20if%20acquired%20we%20attempt%20to%20lock%20all%20other%20partitions%20without%20waiting.%26nbsp%3B%20If%20we%20can%E2%80%99t%20take%20the%20lock%20on%20partition%204%2C%20we%20will%20unlock%20all%20earlier%20partitions%20to%20eliminate%20blocking%20and%20start%20waiting%20with%20low%20priority%20on%204.%26nbsp%3B%20Once%20the%20lock%20on%20partition%204%20is%20acquired%2C%20we%20follow%20the%20same%20process%20of%20acquiring%20the%20next%20partitions%20without%20waiting%2C%20cycling%20back%20to%20partition%200%20once%20we%20hit%20the%20last%20partition.%3C%2FP%3E%0A%3CP%20class%3D%22MsoNormal%22%3E%3CSPAN%20style%3D%22color%3A%20white%3B%22%3EBob%20Dorr%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-3122713%22%20slang%3D%22en-US%22%3E%3CP%3EHow%20the%20low%20priority%20lock%20setting%20works%20in%20SQL%20Server.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3122713%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBobSQL%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Feb 06 2022 07:33 AM
Updated by: