The Lock Manager in SQL Server is responsible for controlling concurrent access to data — making sure multiple users/sessions don't corrupt each other's work when reading or writing at the same time.
Here's what it
does:
Core job —
Grant, Track, and Release locks
- When a session wants to read or modify data, it asks
the Lock Manager for permission
- Lock Manager decides: can this session proceed, or
must it wait?
- Once done, it releases the lock so others can proceed
|
Lock Type |
When used |
|
Shared (S) |
Reading data — multiple readers
allowed simultaneously |
|
Exclusive (X) |
Writing/modifying — blocks everyone
else |
|
Update (U) |
About to modify — prevents deadlocks
during read-then-write |
|
Intent (IS/IX) |
Signals intention to lock at a lower
level (row inside a page) |
|
Schema (Sch) |
Table structure changes (ALTER
TABLE) |
Lock granularity levels it controls Row → Page → Table → Database — it picks the right level based on how much data is touched.
Deadlock
detection The Lock Manager also runs a background deadlock monitor
that detects when two sessions are waiting on each other indefinitely, then
kills the one with the lower cost to roll back (the deadlock victim).
Lock
escalation When too many fine-grained locks (row/page) accumulate, Lock
Manager automatically escalates them to a single table lock to save memory —
this can cause blocking if not managed properly.
- Missing indexes → full table scans → longer lock hold
time → more blocking
- Proper indexes → faster queries → locks released
quicker → better concurrency
- That's exactly why the Lock Manager sits inside the
Storage Engine — index and lock behaviour are tightly coupled.
Lock
lifecycle with transactions: Every query goes through the same cycle —
request a lock → Lock Manager checks compatibility → granted (proceed) or
queued (wait) → execute → commit/rollback → locks released. The "No"
path (waiting) is where blocking happens.
How to diagnose blocking issues — the most common real-world problem the Lock Manager causes:
Three
blocking scenarios and how to fix them:
|
Scenario |
Detect with |
Fix |
|
Regular blocking |
sys.dm_exec_requests — check
blocking_session_id |
Add indexes, shorten transactions,
use RCSI |
|
Deadlock |
system_health Extended Event —
deadlock graph |
Consistent table access order,
covering indexes |
|
Lock escalation |
Lock:Escalation Extended Event |
Covering index reduces rows scanned,
table partitioning |
The golden rule connecting all three: fewer rows scanned = fewer locks held = less blocking. Indexes are your #1 tool against Lock Manager problems.
No comments:
Post a Comment