SQL Server lock modes (S, U, X, IS, IX, SIX, Sch-S, Sch-M), the compatibility matrix, row, page and table granularity, the lock hierarchy, lock escalation thresholds and how to control it.
SQL Server locks resources in a hierarchy: rows (keys or RIDs), pages, and objects. Reads under read committed take shared (S) locks, writes take exclusive (X) locks held until commit, and updates first take an update (U) lock while searching so two updaters can't deadlock converting S to X. Before locking a row, SQL Server puts intent locks (IS, IX) on the page and table so a table-level request can see conflicts cheaply. S is compatible with S; X is compatible with nothing.
Lock escalation swaps many row or page locks for one table (or partition) lock when one statement holds about 5,000 locks on an object, or under memory pressure. It saves memory but can block the whole table, which is the trade-off.
"SQL Server uses locks to provide isolation. The main modes are shared for reading, exclusive for writing, and update, which is taken while an UPDATE or DELETE is finding rows. The U lock exists to avoid a classic deadlock: if two sessions both read a row with S and then both try to convert to X, each waits for the other. Only one session can hold U on a resource, so the second one waits early instead.
Locks are taken at different granularities: a key in an index or a RID in a heap, a page, or the whole object. To make that efficient there are intent locks: before I take an X lock on a row, I take IX on the page and on the table. Then if someone wants a table-level S lock, the engine sees the IX at table level and knows there's a conflict without checking every row. Schema locks are separate: queries take Sch-S, and an ALTER TABLE needs Sch-M, which is why an online index operation or migration can queue behind a long query and then block everyone behind it.
Lock escalation is the engine trading thousands of fine locks for one coarse lock. When a single statement holds about 5,000 locks on one table, it tries to escalate to a table lock, or to a partition lock if the table is set to
LOCK_ESCALATION = AUTO. It never escalates to page. The mistake I see is a batch job that deletes 100,000 rows in one statement, escalates to a table X lock, and blocks the whole order table for a minute. The fix is batching under the threshold, or the right index so fewer rows are touched, not a hint. Locks are cheap until they're not, and escalation is usually where they stop being cheap."
| Mode | Taken by | Notes |
|---|---|---|
| S (shared) | SELECT under read committed or higher | Released after the read under read committed; held to commit under repeatable read and serializable |
| U (update) | UPDATE/DELETE while locating rows; UPDLOCK hint |
Converts to X when the row qualifies |
| X (exclusive) | INSERT, UPDATE, DELETE | Held until the transaction ends, at every isolation level |
| IS / IX / SIX | Parent page and table of a finer lock | Make coarse-grained conflict checks cheap |
| Sch-S / Sch-M | Queries / DDL | Sch-M is incompatible with everything, including Sch-S |
| Key-range | Serializable range scans | Prevents phantoms |
| Requested \ Held | IS | S | U | IX | X |
|---|---|---|---|---|---|
| IS | yes | yes | yes | yes | no |
| S | yes | yes | yes | no | no |
| U | yes | yes | no | no | no |
| IX | yes | no | no | yes | no |
| X | no | no | no | no | no |
A nightly purge deleted about [200,000] audit rows in one statement; it escalated to a table X lock and API writes to that table timed out for [90] seconds. Rewriting it as a loop deleting 4,000 rows per transaction kept it below the escalation threshold, and the timeouts went to zero while the purge took only slightly longer.
Checks that the candidate understands the lock hierarchy and escalation well enough to explain real blocking.
Green flags
sys.dm_tran_locks or LCK_M_* waitsRed flags
NOLOCK as the fixLocks are in-memory structures owned by the lock manager. Each has a resource (database, object, page, key hash, RID), a mode and an owner (usually the transaction). Requests that conflict queue in FIFO order; a waiting session shows LCK_M_* waits, for example LCK_M_S or LCK_M_X.
DATABASE (S, held by every connection)
└─ OBJECT dbo.Orders IX
└─ PAGE 1:4402 IX
└─ KEY (8194443284a0) X
Without intent locks, a request for a table lock would have to scan every row lock to detect conflicts. With them, a table-level request only checks the table-level entry: IX means "someone is writing below me", so a table S request must wait.
Session A: S on row -> wants X (waits for B's S)
Session B: S on row -> wants X (waits for A's S) => deadlock
With U: A takes U, B requests U and waits, A converts to X, commits, then B continues. That is why UPDLOCK is the standard hint for read-then-update patterns.
The engine picks row or page locks per statement based on estimates; it can also start with page locks for large scans. ALLOW_ROW_LOCKS and ALLOW_PAGE_LOCKS index options can restrict this, rarely a good idea.
ALTER TABLE ... SET (LOCK_ESCALATION = AUTO) on a partitioned table. DISABLE prevents most escalation.Optimized locking changes the picture: with transaction ID (TID) locking, a writer holds one lock on its transaction id instead of keeping row locks until commit, and lock after qualification (LAQ) evaluates predicates against the latest committed version before taking a lock. It requires Accelerated Database Recovery, and LAQ requires RCSI. It is enabled in Azure SQL Database and is opt-in per database on SQL Server 2025. It greatly reduces lock memory and escalation.
SELECT request_session_id, resource_type, resource_description, request_mode, request_status
FROM sys.dm_tran_locks
WHERE resource_database_id = DB_ID();
The lock_escalation Extended Events event records each escalation.
sys.dm_tran_locks to see the X key lock and IX page and table locks.LOCK_ESCALATION = DISABLE and compare lock counts.HOLDLOCK, then fix it with UPDLOCK.Observe lock modes and escalation, then avoid it by batching.
-- Window 1: hold a write lock and look at the hierarchy
BEGIN TRAN;
UPDATE dbo.Orders SET Status = 'Paid' WHERE OrderId = 42;
SELECT resource_type, resource_description, request_mode, request_status
FROM sys.dm_tran_locks
WHERE request_session_id = @@SPID;
-- KEY X, PAGE IX, OBJECT IX
ROLLBACK;
-- A purge that would escalate to a table X lock
-- DELETE dbo.AuditLog WHERE CreatedAt < DATEADD(day, -90, SYSUTCDATETIME());
-- Batched purge: each statement stays under ~5,000 locks and commits quickly
DECLARE @cutoff datetime2 = DATEADD(day, -90, SYSUTCDATETIME()), @rows int = 1;
WHILE @rows > 0
BEGIN
DELETE TOP (4000) dbo.AuditLog WHERE CreatedAt < @cutoff; -- needs an index on CreatedAt
SET @rows = @@ROWCOUNT;
END
-- Partitioned table: escalate to the partition, not the whole table
ALTER TABLE dbo.AuditLog SET (LOCK_ESCALATION = AUTO);
sys.dm_tran_locks. Why it is wrong: An IX on a table explains why a table-level request is blocked. Instead: Read the full hierarchy.HOLDLOCK alone for read-then-update. Why it is wrong: Two sessions both get S and deadlock converting to X. Instead: UPDLOCK, HOLDLOCK together.WAIT_AT_LOW_PRIORITY for online index operations and deploy at quiet times.Locking answers become strong when they explain why each mode exists: U for conversion deadlocks, intent locks for cheap hierarchy checks, escalation for memory. In practice, most locking problems are scans that lock more rows than needed, so the first fix is an index, the second is a shorter transaction, and hints come last. On Azure SQL Database, optimized locking and RCSI already remove much of the classic pain, so ask which engine and settings are in play before prescribing.
| Control | When it fits | Cost | When not to |
|---|---|---|---|
| Batching DML | Purges, backfills | Longer total run time | Work that must be one atomic change |
LOCK_ESCALATION = AUTO |
Partitioned tables with partition-aligned work | Partition-level deadlocks possible | Non-partitioned tables (no effect) |
UPDLOCK hint |
Read-then-update in one transaction | Serialises updaters of the same row | Pure reads |
| Optimized locking | Azure SQL / SQL Server 2025 with ADR and RCSI | Needs ADR (persistent version store) | Code relying on blocking semantics of read committed with locks |
Blocking is sessions waiting for locks another session holds. I find the head blocker by following blocking_session_id in sys.dm_exec_requests to the session that isn't blocked itself, or with sp_WhoIsActive @find_block_leaders = 1. Then I check what it is: a running query, or a sleeping session with open_transaction_count > 0, which means the app opened a transaction and never finished it.
Typical .NET causes: transactions left open around slow work or HTTP calls, scans caused by missing indexes locking far more rows than needed, lock escalation from big batches, and leaked connections. Mitigation may be KILL, but the fix is indexing, shorter transactions and RCSI so readers don't wait on writers.
When you delete a parent row, SQL Server must check every child table that references it. If the child's foreign key column has no index, each check is a table scan, and the delete's locks are held for the whole long statement. A large delete also escalates to a table lock after about 5,000 locks, blocking everyone.
I'd confirm it from the actual plan (a scan of the child under the FK check, huge logical reads) and the blocking chain (sys.dm_exec_requests, sp_WhoIsActive). The fix: index the FK columns, delete in small batches with short transactions, and avoid wide cascades. Separately, constraints created WITH NOCHECK are untrusted, so the optimiser cannot use them; re-validate with WITH CHECK CHECK CONSTRAINT once the data is clean.
Key-range locks protect the gap between index keys so no other session can insert a row that would change the result of a range read. They appear only under SERIALIZABLE (or the HOLDLOCK hint). Modes such as RangeS-S, RangeS-U and RangeI-N combine a range part and a key part. They prevent phantoms and make upserts safe, but they need a suitable index (otherwise far more is locked), increase blocking, and are a common cause of deadlocks.
Last updated 23 Nov 2026.