7.4 Lock Escalation: When Row Locks Become a Table Lock
Row locks have a cost. Each lock uses tens to hundreds of bytes of lock manager memory. This is no problem for a transfer that modifies one row, but a bulk job that says "change the status of last month's 10 million transactions in one go" must take 10 million row locks. If a lock is 100 bytes, that is 1 GB. And taking 10 million locks one by one and releasing them one by one later also takes time.
So some DBMSs, when the row locks a transaction holds on one table exceed a threshold, release all those row locks and replace them with a single table lock. This is lock escalation. Millions of row X locks become a single table X lock. Memory usage drops sharply, and the burden on the lock manager disappears.
Figure 7-4. Before and after lock escalation
flowchart LR
subgraph BEFORE ["Before escalation"]
BT["account table: batch holds IX"]
BR1["Row 1: X"]
BR2["Row 2: X"]
BRN["... thousands of rows: X"]
BT --> BR1
BT --> BR2
BT --> BRN
end
subgraph AFTER ["After escalation"]
AT["account table: batch holds X<br/>no row locks"]
end
BRN -->|"row lock count exceeds threshold"| AT
O1["Another transaction<br/>accesses a row the batch has not modified"]
O1 -.->|"table IS or IX: compatible with IX, proceeds"| BT
O1 -.->|"table IS or IX: incompatible with X, waits"| AT
The left box is before escalation. The bulk job (batch) holds IX on the table and one X per modified row. The lock manager must record all of these row locks in memory.
- As the batch modifies more rows and the row lock count passes the threshold, the database releases all the row X locks and takes a single X on the table (right box). The locks the lock manager must record shrink to one.
- The "another transaction" below tries to read or modify a row the batch has not touched. Before escalation, it can take IS or IX on the table (compatible with IX), and since it does not overlap the batch at the row level, it proceeds as is.
- After escalation, the X on the table is incompatible with both IS and IX, so even a transaction accessing a row unrelated to the batch waits at the table level.
The difference between the two boxes is the heart of this section. Memory shrinks, but the range other transactions can access shrinks from "every row the batch has not modified" to "nothing".
This conversion comes at a large price. Table X, in the compatibility matrix of Section 7.3, coexists with nothing. From the moment escalation occurs, every transaction that tries to read or modify a row this transaction has not modified is blocked too. A job that modifies only last month's transactions out of 10 million rows also blocks the screens that query this month's transactions. This is one cause of the practical complaint that "unrelated screens freeze every time the nightly batch runs". If, at the moment escalation is attempted, another transaction holds an incompatible lock on the same table (for example, X on a different row), the escalation fails and the row locks continue to be held. In other words, escalation happens only "when it can", so the same batch shows up as an inconsistent phenomenon: some days it blocks everything and other days it does not.
Differences Among DBMSs
Whether lock escalation is needed depends on the design of "where locks are recorded".
| DBMS | Escalation | Reason and behavior |
|---|---|---|
| SQL Server / Azure SQL | Yes | Locks are recorded in separate lock manager memory. When one statement takes about 5,000 locks on one table, it attempts to convert to a table (or partition) lock. It also attempts this when lock memory use nears its limit. Controlled per table with the LOCK_ESCALATION option (TABLE, AUTO, DISABLE) |
| MySQL (InnoDB) | No | Row locks are compressed and recorded as a per-page bitmap, so memory grows little as the row count grows. Instead, if lock memory is exhausted, the transaction fails with an error |
| Oracle | No | Row locks are recorded inside the block that contains the row (the ITL area). No separate lock manager memory is used, so there is no burden that scales with the row count |
| PostgreSQL | No | Row locks are recorded in the row header (tuple header). The in-memory lock table is used only for table-level locks, so it is independent of the row count |
Learn more — SQL Server Locks and Lock Escalation, InnoDB Gap Locks and Next-Key Locks, Oracle TM and TX Locks, PostgreSQL Lock Modes
Table 7-3. Lock escalation support.
NOTE Thresholds and option names may differ by version.
Oracle and PostgreSQL do not escalate because they have no need to. Row locks are not kept in a separate in-memory table but are recorded directly inside the page that contains the row, so even holding 10 million rows does not grow the lock manager's memory. In exchange, finding out "who holds this row" requires reading that page, and the cost is paid elsewhere, for example by not clearing the lock marks page by page at commit but leaving it to a transaction that later touches the page to clean up. Neither is superior; they are designs that pay the same cost in different places.
Practical Countermeasures
In the SQL Server family, escalation usually causes trouble when a bulk job shares a table with an online service. There are three directions of response.
- Split the work into small pieces. Instead of modifying 10 million rows in one transaction, commit in batches of a few thousand. Each transaction's lock count stays below the threshold so escalation does not occur, and the long-transaction problem of Section 4.5 is avoided as well. This is the most common and safest method.
- Control escalation per table. For a partitioned table, make it escalate only to the partition rather than the whole table, and in special cases turn escalation off. Note, however, that turning it off increases lock memory accordingly, so check the limit.
- Move the read side to MVCC. With RCSI on, queries take no row locks and read old versions, so even when escalation places table X, queries are not blocked. Other writes are still blocked.
A DBMS without escalation does not make bulk jobs safe. The locks do not turn into a table lock, but it is the same that 10 million row locks are held until commit, and other transactions that try to modify those rows wait just as long. This is why response 1, splitting the work into small pieces, works in every DBMS.