skip to content

In SQL Server, what triggers lock escalation, what does it escalate to, and which of ROWLOCK, LOCK_ESCALATION or batching actually prevents it?

level: seniorimportance: should knowfreq 55%

answer

  1. a per-statement lock count
  2. checked every 1,250 new locks
  3. table level, never page level
  4. a table option and smaller batches

basics

~20 s

When a statement holds 5,000 locks on one table reference or lock memory passes a threshold, SQL Server swaps them for one table or partition lock, never a page lock. ROWLOCK doesn't prevent it; smaller batches or LOCK_ESCALATION do.

solid answer

~50 s

SQL Server checks for escalation every 1,250 new locks and escalates when one statement holds at least 5,000 locks on a single reference to a table or index, so 3,000 in each of two indexes doesn't count, or when lock memory passes its threshold (24 percent of engine memory with the default `locks` setting). The row or key locks become one table lock, or a partition (HoBT) lock on a partitioned table set to `LOCK_ESCALATION = AUTO`; it never stops at page level. If another session holds an incompatible table lock, the attempt fails and the statement keeps its fine-grained locks. `ROWLOCK` only sets the starting granularity and doesn't prevent escalation. What works: batch large DML well below the threshold, index so the statement touches fewer rows, `ALTER TABLE ... SET (LOCK_ESCALATION = DISABLE)` for one table, or SQL Server 2025's optimized locking, which releases row locks as it goes.

code

sql · 10 lines
sql
DECLARE @deleted int;

WHILE @deleted IS NULL OR @deleted > 0
BEGIN
    DELETE TOP (500)
    FROM dbo.audit_log
    WHERE logged_at < '2026-01-01';

    SET @deleted = @@ROWCOUNT;
END;

go deeper

for a junior

Remember that SQL Server can replace many row locks with one table lock, and that this is why one big DELETE can block everyone else.

for a middle

Explain the 5,000-locks-per-statement-per-reference rule, the 1,250-lock check, and that escalation goes to the table or partition, never to pages.

for a senior

Show you can confirm escalation with the lock_escalation event or OBJECT-level waits and fix it with batching, indexing or a per-table LOCK_ESCALATION setting, not ROWLOCK.

for a principal

Weigh per-table LOCK_ESCALATION and batch-size conventions against instance-wide trace flags, and whether moving to optimized locking in 2025 retires the problem for the fleet.

## SQL Server's lock hierarchy in brief SQL Server locks resources at several levels of one hierarchy. The ones that matter for escalation are: - `RID` (a row in a heap) and `KEY` (a row in a B-tree index), the finest grain; - `PAGE`, an 8 KB data or index page; - `HoBT`, one heap or B-tree, which on a partitioned table means one partition; - `TABLE` (shown as an `OBJECT` resource in `sys.dm_tran_locks`), the whole table with all its indexes. Before taking a row lock, a transaction takes **intent locks** (`IS`, `IX`) on the page and table above it, so a later request for a whole-table lock can see at once that someone is working inside. Every lock costs memory, roughly 100 bytes each, and a statement that touches a million rows can hold a million locks. **Lock escalation** is SQL Server trading those many fine locks for one coarse lock. ## When escalation fires Escalation is attempted when the table's `LOCK_ESCALATION` option has not disabled it and one of these holds: 1. A single Transact-SQL statement has acquired at least **5,000 locks** on a single non-partitioned table or index. 2. A single statement has acquired at least 5,000 locks on one **partition**, and the table is set to `LOCK_ESCALATION = AUTO`. 3. Lock memory across the instance passes its threshold: **24 percent** of the memory used by the Database Engine when the `locks` configuration option is at its default of 0, or 40 percent of the configured value (less under memory pressure) otherwise. The details trip people up. The count is **per statement and per reference**: 3,000 locks in one index plus 3,000 in another index of the same table does not trigger it, nor does a self-join whose two references each hold 3,000. The engine checks at every **1,250** newly acquired locks. And if an attempt fails because another session holds an incompatible lock on the table, the statement does not wait for the table lock: it keeps acquiring row, key or page locks and tries again at later checkpoints. ## What it escalates to Escalation always goes to the **table** (or, with `AUTO` on a partitioned table, the partition) and **never to page locks**. The new lock is strong enough for the most restrictive lock it replaces: if the session had `X` row locks from an `UPDATE` and `S` locks from a later `SELECT` on the same table, its table-level `IX` becomes a table `X` lock and every row, page and intent lock below it is released. That is the production symptom. A purge that escalates to a table `X` lock blocks every other session that touches the table, including single-row inserts, which then wait with `LCK_M_IX` or similar lock waits until the purge commits. ## The nightly purge, done safely A single `DELETE FROM dbo.audit_log WHERE logged_at < '2026-01-01'` that removes hundreds of thousands of rows will cross 5,000 locks almost immediately. Deleting in small batches, each committed on its own, keeps every statement far below the threshold and lets other sessions in between batches (the code example shows the loop). ## What prevents it and what doesn't | Approach | Prevents escalation? | Notes | |---|---|---| | `WITH (ROWLOCK)` | No | Changes only the initial granularity; escalation still applies | | Batches of a few hundred rows | Yes | Each statement stays under 5,000 locks; commit each batch | | A better index | Often | Fewer rows touched means fewer locks; large key-lookup plans can take thousands | | `LOCK_ESCALATION = AUTO` | Partitioned tables only | Escalates to the partition; on a non-partitioned table it still goes to `TABLE` | | `LOCK_ESCALATION = DISABLE` | In most cases | Per table; some table locks are still taken, such as a serializable scan of a heap | | Trace flags 1211 and 1224 | Instance-wide | Lose the memory protection escalation gives; risk error 1204 | | Optimized locking (2025) | Far rarer | Row locks are released as each row is modified; needs accelerated database recovery | `TABLE` is the default `LOCK_ESCALATION` value, and `sys.tables.lock_escalation_desc` shows each table's setting. ## Detecting it - The `lock_escalation` Extended Events event records each escalation with its statement; a histogram target per database is a cheap way to see where they happen. - Blocked sessions waiting on an `OBJECT` resource rather than a `KEY` or `PAGE` in `sys.dm_tran_locks` point at a table-level lock. - In SQL Server 2025, the `locking_stats` event reports aggregate escalation counts per database every few minutes, whether or not optimized locking is enabled. Disabling escalation without fixing the statement only moves the problem: lock memory keeps growing, and when it runs out SQL Server raises **error 1204** ("cannot obtain a LOCK resource at this time"), which stops the statement and rolls back its transaction.

  • In SQL Server, what happens when a lock escalation attempt fails?
    Escalation needs a table lock compatible with what other sessions hold. If another session holds an incompatible lock on the table, even an `IX` lock from a one-row update, the attempt fails. The statement does not wait for the table lock; it carries on with row, key or page locks and tries escalating again at later 1,250-lock checkpoints.
  • Why are trace flags 1211 and 1224 a poor fix for escalation on one table in SQL Server?
    They act on the whole instance, not one table, and they remove the protection escalation gives lock memory. If lock structures keep growing, any query can fail with error 1204, which stops the statement and rolls back its transaction. `LOCK_ESCALATION` on the one table, or smaller batches, targets the real problem.

  • Escalation fires once a whole transaction holds 5,000 locks across all its tables.
  • Adding WITH (ROWLOCK) stops SQL Server from escalating.
  • SQL Server escalates row locks to page locks first, then to the table.
  • LOCK_ESCALATION = AUTO escalates to partitions on any table, partitioned or not.
  • A failed escalation makes the statement wait until the table lock is granted.

Assumes SQL Server 2025, whose optimized locking (new in 2025, off by default) makes escalation far less likely; without it the thresholds described here apply.