SQL Server Locks and Lock Escalation

At the default isolation level (lock-based Read Committed), SQL Server takes shared locks (S) even for reads and exclusive locks (X) for writes. Locks apply at several granularities: row (key or RID), page, table (or partition), and database. On the larger granularities, it places intent locks (IS, IU, IX) that announce what is locked below. Every lock is recorded in the lock manager in memory, so when locks pile up, SQL Server replaces many row locks with a single table lock to save memory (lock escalation).

Key Specs

ItemValue
Basic modesS (shared), U (update), X (exclusive)
Intent locksIS, IU, IX, SIX, SIU, UIX
Schema locksSch-S (pins the schema during compilation and queries), Sch-M (DDL, conflicts with all locks)
Key-range locksIn Serializable, placed on gaps between index keys to prevent phantoms (RangeS-S, RangeI-N, etc.)
Lock granularityRID and KEY (row), PAGE, OBJECT (table), HOBT and partition, DATABASE, etc.
Memory per lockAbout 100 bytes
Escalation thresholdAbout 5,000 locks by one statement on one object (table, index, or partition), or lock memory use approaching its limit
Escalation targetTable (default TABLE) or partition (AUTO)
DisableLOCK_ESCALATION = DISABLE per table, trace flags 1211 and 1224 (whole instance)

How It Works

U locks. UPDATE holds a U lock while it looks for the target row and converts it to X when it actually modifies the row. U is compatible with S but not with another U, which prevents the deadlock where two statements that want to modify the same row both hold S and wait for each other's X conversion.

Figure. Lock escalation

flowchart LR
  A["UPDATE takes row locks one by one<br/>table IX, row X"] --> B{"About 5,000 locks on one object"}
  B -- "No" --> A
  B -- "Yes" --> C{"Does another session hold an<br/>incompatible lock on that table"}
  C -- "No" --> D["Replace with one table X<br/>and release the row locks"]
  C -- "Yes" --> E["Escalation fails, row locks kept<br/>retry after every 1,250 more locks"]

Diagram notes: As a statement keeps adding row locks and reaches about 5,000 on one object, it tries to convert them to a table (or partition) lock. If another session holds an incompatible lock such as IX on the same table, the attempt fails, the statement continues with its row locks, and it retries after taking a certain number of additional locks. If it succeeds, the whole table is locked and even row-level work by other sessions is blocked, which causes a sudden drop in concurrency during a bulk update.

Avoiding escalation. Commit bulk updates in chunks of a few thousand rows, add an index to reduce the number of rows scanned (that is, locks taken), or, for a partitioned table, use LOCK_ESCALATION = AUTO so that escalation goes only to the partition level. For waits on the read side, turning on RCSI cuts them substantially because reads no longer take S locks.

Lock hints. WITH (UPDLOCK) takes a U lock while reading and blocks other FOR UPDATE-style reads, and HOLDLOCK keeps locks until the end of the transaction like Serializable. READPAST skips locked rows (queue processing), and NOWAIT raises error 1222 immediately instead of waiting. SET LOCK_TIMEOUT sets the wait limit.

Configuration and Checks

-- Locks currently held and waits
SELECT request_session_id, resource_type, resource_description, request_mode, request_status
FROM sys.dm_tran_locks WHERE resource_database_id = DB_ID();

-- Who is blocking whom
SELECT session_id, blocking_session_id, wait_type, wait_time, wait_resource
FROM sys.dm_exec_requests WHERE blocking_session_id <> 0;

ALTER TABLE dbo.orders SET (LOCK_ESCALATION = AUTO);
SELECT name, lock_escalation_desc FROM sys.tables;

-- Fetch one unlocked job from a queue
SELECT TOP (1) id FROM dbo.jobs WITH (UPDLOCK, READPAST, ROWLOCK) WHERE status = 'ready' ORDER BY id;

Comparison with Other DBMSs

SQL ServerPostgreSQLInnoDBOracle
Where row locks are recordedLock manager memoryRow header (tuple header)Per-page bitmap (lock memory)ITL in the block
EscalationYesNoNoNo
Does a default read take locksYes (with RCSI off)NoNoNo
Phantom preventionKey-range locks (Serializable)Snapshot, SSIGap locks, next-key locksSnapshot

In the Articles

NOTE The escalation threshold and lock memory size can vary with the version.

Related pages