skip to content

What does optimized locking in SQL Server 2025 change about how UPDATE and DELETE hold locks, and what must a database have enabled first?

level: seniorimportance: should knowfreq 35%

answer

  1. one lock per transaction, not per row
  2. a lock on the transaction ID
  3. qualify first, lock afterwards
  4. a recovery feature is the prerequisite

basics

~20 s

Optimized locking replaces row and page locks held until commit with one exclusive lock on the transaction ID, releasing each row lock once the row is changed. It requires accelerated database recovery; with READ_COMMITTED_SNAPSHOT, rows also qualify without U locks.

solid answer

~50 s

Optimized locking is off by default in SQL Server 2025 and is enabled per database with `ALTER DATABASE ... SET OPTIMIZED_LOCKING = ON`, which first requires accelerated database recovery (ADR). It has two parts. **Transaction ID (TID) locking** stamps each modified row with the writer's TID; the writer normally still takes row and page locks but releases each one as soon as the row is modified, holding only one `X` lock on its `XACT` resource until commit, while other writers wait for it with an `S` lock on that TID. **Lock after qualification (LAQ)** works only with `READ_COMMITTED_SNAPSHOT ON` at `READ COMMITTED`: the `WHERE` predicate is evaluated against the latest committed version without `U` locks, and only qualifying rows are locked. The results are far less lock memory, far rarer escalation and fewer deadlocks, plus one behaviour change worth testing.

code

sql · 10 lines
sql
ALTER DATABASE Shop SET ACCELERATED_DATABASE_RECOVERY = ON WITH ROLLBACK IMMEDIATE;
ALTER DATABASE Shop SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;
ALTER DATABASE Shop SET OPTIMIZED_LOCKING = ON WITH ROLLBACK IMMEDIATE;

SELECT name,
       is_accelerated_database_recovery_on,
       is_read_committed_snapshot_on,
       is_optimized_locking_on
FROM sys.databases
WHERE name = N'Shop';

go deeper

for a junior

Remember that SQL Server 2025 can hold a single lock per writing transaction until commit, instead of one per modified row, and that it is switched on per database.

for a middle

Explain TID locking and lock after qualification separately, and which of them needs READ_COMMITTED_SNAPSHOT and which needs accelerated database recovery.

for a senior

Show you would test for the LAQ behaviour change, recognise LCK_M_S_XACT waits and xactlock deadlock resources, and find the hints that stop statements benefiting.

for a principal

Weigh enabling ADR, RCSI and optimized locking together as a 2025 upgrade step against the cost of testing every read-then-write path that relied on blocking.

## The problem it targets Without optimized locking, an `UPDATE` of 1,000 rows at SQL Server's default `READ COMMITTED` takes 1,000 exclusive (`X`) key or row locks, plus intent locks on pages, and holds them all until the transaction ends. Many locks mean lock memory, a high chance of crossing the 5,000-lock **escalation** threshold, and long windows in which other writers queue. Before modifying, each candidate row is also read under an update (`U`) lock, so two `UPDATE`s that will change different rows can still block each other while they scan. **Optimized locking**, new in SQL Server 2025, attacks both. It is not available in SQL Server 2022 or earlier, and in 2025 it is **off by default** and enabled per database. ## Transaction ID (TID) locking When row versioning or accelerated database recovery is in use, every row carries the **transaction ID** of the last transaction that modified it. TID locking uses that stamp: 1. The modifying transaction holds one `X` lock on its own TID, shown as an `XACT` resource in `sys.dm_tran_locks`. 2. It still takes `X` row and `IX` page locks to modify each row, but **releases each one as soon as that row is modified**. 3. Another transaction that needs one of those rows takes an `S` lock on the TID stamped on the row, which waits until the first transaction commits or rolls back. With RCSI and LAQ in use, SQL Server can skip the short page lock for inserts into heaps, and both the row and page locks for many updates, when no `REPEATABLE READ` or `SERIALIZABLE` reader needs the row stable, protecting the change with an exclusive page latch instead; this is called skip index locks. So an open transaction that updated three rows shows a single `XACT` lock in `sys.dm_tran_locks` instead of one `IX` page lock and three `X` key locks. Escalation becomes far less likely, though it can still occur. ## Lock after qualification (LAQ) LAQ needs `READ_COMMITTED_SNAPSHOT ON` and a statement running at `READ COMMITTED`. The predicate of an `UPDATE` or `DELETE` is evaluated against the **latest committed version** of each row with no lock at all. Rows that don't qualify are skipped; a qualifying row gets a short `X` lock while it is modified. Two sessions updating different rows of the same table therefore don't block each other. Because no `U` lock protects a row between qualification and modification, another transaction may change it in between. SQL Server then waits for that transaction and **requalifies** the predicate. Where the plan uses an operator that can't requalify, the statement is internally restarted without LAQ and the `lock_after_qual_stmt_abort` event fires; heuristics switch LAQ off for a plan, or the database, if restarts waste too much work. LAQ is **not used**: - with `UPDLOCK`, `READCOMMITTEDLOCK`, `XLOCK` or `HOLDLOCK` hints, or at an isolation level other than `READ COMMITTED`; - on a table with a columnstore index, in `MERGE`, or with variable assignment; - with an `OUTPUT` clause that returns a result set or fills a table variable; - when more than one seek or scan reads the rows being modified. ## Turning it on | Requirement | Why | |---|---| | Accelerated database recovery enabled first | Optimized locking builds on it; ADR can't be disabled while optimized locking is on | | `READ_COMMITTED_SNAPSHOT ON` (recommended) | Without it, TID locking works but LAQ doesn't | | No other connections while switching | Same rule as the RCSI switch; `WITH ROLLBACK IMMEDIATE` is accepted | `DATABASEPROPERTYEX(DB_NAME(), 'IsOptimizedLockingOn')` returns 1, 0 or `NULL` (not available), and `sys.databases.is_optimized_locking_on` shows it per database. It does not apply to `tempdb` or temporary tables, and it changes only the row and page locks of DML statements; schema locks are untouched. ## A behaviour change worth testing A table `dbo.tickets` holds one row with `id = 1` and `state = 1`. Session 1 runs `UPDATE dbo.tickets SET state = 2 WHERE id = 1` and has not committed. Session 2 runs `UPDATE dbo.tickets SET state = 3 WHERE state = 2`. - **Without LAQ**, session 2 blocks, then sees session 1's committed `state = 2`, qualifies the row and sets it to 3. - **With LAQ**, session 2 evaluates `state = 2` against the latest committed version, where `state` is still 1, skips the row without blocking, and the final value is 2. Code that relies on one transaction's update waiting for and then acting on another's should use a stricter isolation level or a locking hint such as `READCOMMITTEDLOCK`, accepting that it gives up part of the benefit. ## Diagnosing it - Waits on the TID appear as `LCK_M_S_XACT_READ`, `LCK_M_S_XACT_MODIFY` or, rarely, `LCK_M_S_XACT` in `sys.dm_os_wait_stats`. - Deadlock reports show `xactlock` resources in `resource-list`. - The `locking_stats` event aggregates escalations and LAQ usage per database every few minutes.

  • With optimized locking and READ_COMMITTED_SNAPSHOT both on in SQL Server 2025, how can an UPDATE end with a different result than before?
    Lock after qualification evaluates the predicate against the latest committed version instead of waiting. If another transaction has an uncommitted change that would make the row qualify, the `UPDATE` skips the row rather than blocking and then updating it. Code that relied on that wait needs `READCOMMITTEDLOCK` or a stricter isolation level.
  • Does optimized locking in SQL Server 2025 help a workload that uses UPDLOCK or HOLDLOCK hints everywhere?
    Much less. The hints are still honoured, so SQL Server takes the row or page locks they ask for and holds them to the end of the transaction, and lock after qualification isn't used for those statements. A hint on one table doesn't disable optimized locking for the other tables in the query.

  • Optimized locking is switched on by default in SQL Server 2025.
  • With optimized locking, two transactions can modify the same row at once.
  • Lock after qualification works without READ_COMMITTED_SNAPSHOT.
  • Optimized locking can be enabled without accelerated database recovery.
  • Optimized locking also removes schema locks, so DDL stops blocking queries.

Assumes SQL Server 2025, the first SQL Server release with optimized locking; it is off by default there and absent from 2022 and earlier.