Lock escalation

On this page 7

Lock escalation is the process by which a database engine replaces many fine-grained locks, typically row or page locks, with a single coarser lock on a partition or an entire table. In database performance work, it matters because it reduces lock memory and lock manager overhead while sharply reducing concurrency for every other session that needs the same object.

Escalation is an automatic, engine-driven decision rather than an application request. It is most closely associated with Microsoft SQL Server and IBM Db2, which escalate when lock counts or lock memory cross configured thresholds. Engines such as PostgreSQL and Oracle Database are designed to avoid it, which makes the concept essential when comparing locking behavior across platforms.

How lock escalation works

Every lock a transaction holds consumes an entry in the lock manager’s memory structure. A statement that updates millions of rows would otherwise need millions of entries, so the engine monitors how many locks a single transaction holds on one object and, past a threshold, attempts to acquire one table or partition lock in a compatible mode.

If the coarse lock is granted, the engine releases the fine-grained locks it replaces, freeing memory immediately. If another session holds an incompatible lock on the same object, the escalation attempt fails, the transaction keeps its row locks and continues, and the engine retries escalation later, for example after a further batch of locks is acquired.

The lock mode is preserved during escalation: shared row locks become a shared table lock, and exclusive row locks become an exclusive table lock. Intent locks on higher levels of the hierarchy, such as intent exclusive on the table, are what make the upgrade possible without scanning every row lock held by other sessions.

Escalation triggers and thresholds

Escalation is triggered by lock count per statement or transaction or by total lock memory pressure, and the exact values differ by engine. The following table summarizes the commonly documented triggers and the controls that influence them.

EngineTypical triggerEscalation targetMain controls
SQL ServerAbout 5,000 locks on one object, or lock memory pressureTable or partitionLOCK_ESCALATION table option, trace flags 1211 and 1224
Db2 for Linux, UNIX and WindowsApplication exceeds MAXLOCKS share of LOCKLISTTableLOCKLIST and MAXLOCKS parameters
Db2 for z/OSLocks per table space exceed LOCKMAXTable space or tableLOCKMAX and NUMLKTS settings
PostgreSQLNo row lock escalationNot applicableRow locks stored in tuple headers
Oracle DatabaseNo lock escalationNot applicableRow locks stored in data blocks
Escalation behavior in major database engines

In SQL Server, the 5,000-lock threshold applies to a single reference to an object within one statement, not to the whole transaction, which is why batched updates of 4,000 rows usually avoid escalation. The LOCK_ESCALATION option accepts TABLE, AUTO for partition-level escalation, or DISABLE.

Lock escalation is often confused with lock contention, lock conversion and deadlocks, although each describes a different phenomenon. The comparison below clarifies where escalation sits among these performance terms.

ConceptWhat changesTypical causeEffect
Lock escalationGranularity rises from rows to tableToo many locks held by one transactionLower memory use, less concurrency
Lock conversionMode changes on the same resourceShared lock upgraded to exclusiveMay wait or deadlock
Lock contentionSessions wait on one anotherIncompatible locks on hot resourcesHigher latency, blocking chains
DeadlockCycle of waits is broken by a victimOpposite lock acquisition orderOne transaction is rolled back
Escalation and neighboring locking concepts

The relationship between these concepts is causal: escalation frequently creates contention, because a table lock blocks sessions that would otherwise have touched unrelated rows. It can also increase deadlock risk when two escalating transactions each wait for the other to release a coarse lock.

Diagnosing and preventing escalation

Escalation becomes a problem when blocking appears suddenly during large writes while normal OLTP traffic runs on the same table. In SQL Server it can be observed through the lock_escalation extended event and the escalation counters in sys.dm_db_index_operational_stats; in Db2, escalation messages appear in the diagnostic log.

The table below lists proven mitigation techniques, ordered roughly from least to most invasive.

TechniqueHow it helpsTrade-off
Batch large modificationsKeeps each statement below the lock thresholdMore round trips and longer total runtime
Add supporting indexesFewer rows scanned and locked per statementExtra write and storage cost
Partition-level escalationCoarse lock covers one partition onlyRequires a partitioned table
Row versioning isolationReaders stop taking shared locksVersion store overhead in tempdb
Disable escalation per tableEngine keeps fine-grained locksRisk of lock memory exhaustion
Mitigation options for unwanted escalation

Disabling escalation is a last resort rather than a default. When lock memory runs out, the engine returns out-of-lock errors to applications, which is usually worse than a short period of table-level blocking.

Common misconceptions about lock escalation

  • Escalation is not always harmful: for maintenance jobs that run alone, a single table lock is faster and cheaper than millions of row locks.
  • Row locks never escalate to page locks in SQL Server; escalation goes directly to the table or partition level.
  • Lowering isolation does not remove writer locks: exclusive locks for updates are still taken under read committed or snapshot isolation.
  • A failed escalation is silent: the transaction continues with row locks, so the absence of a table lock does not prove that escalation was never attempted.

Put simply

Imagine a library that hands out a reserved sign for every book someone is using. If one reader takes thousands of books from a single shelf, the library runs out of signs. So it closes the whole shelf with one sign instead.

That saves effort, but nobody else can borrow from that shelf until the reader is done. Lock escalation is exactly this choice inside a database: less bookkeeping in exchange for more waiting by other users.

In practice

An online retailer runs a nightly archive job on SQL Server that deletes order lines older than three years from a 400-million-row table. One night the job starts late and overlaps with the morning order peak. The single DELETE statement passes about 5,000 row locks within seconds, escalates to an exclusive table lock, and checkout requests begin to time out.

The database team rewrites the job to delete in batches of 2,000 rows using a supporting index on the order date, with a short pause between batches. Each batch stays below the escalation threshold and commits quickly, so ordinary inserts and updates continue in parallel.

The archive job now takes 40 minutes instead of 12, but checkout blocking disappears, and the escalation counters for the table remain at zero across the following weeks.