Overview

By default SQL Server uses locks to give transactions isolation (locking-and-mvcc). A transaction that needs a lock held by another one blocks (waits). Two transactions that wait for each other form a deadlock, and the engine kills one of them.

Core concepts

  • Lock modes: shared (S), exclusive (X), update (U), intent (IS, IX, SIX), schema (Sch-S, Sch-M), key-range locks under SERIALIZABLE. S is compatible with S; X is compatible with nothing.
  • Granularity: row (KEY or RID), PAGE, OBJECT. Intent locks on the higher levels announce finer locks. Lock escalation converts many row/page locks to a table lock (roughly after 5,000 locks held by one statement on one object).
  • Blocking is normal and brief. It becomes an incident when a head blocker holds locks for a long time (open transaction, slow client, missing index causing wide scans).
  • Deadlock: the lock monitor checks for cycles every few seconds and chooses a victim (lowest DEADLOCK_PRIORITY, then least log used). The victim gets error 1205 and its transaction is rolled back.
  • Blocking is described by wait_type LCK_M_* (for example LCK_M_S, LCK_M_U, LCK_M_X, LCK_M_IX) in sqlserver-wait-stats.

Worked example

Blocking (executed on SQL Server 2022, two sessions): session A updates a row and holds the transaction open; session B reads the same row under the default READ COMMITTED.

-- session A
BEGIN TRAN; UPDATE dbo.Acct SET bal = bal - 10 WHERE id = 1;  -- keep open
 
-- session B (blocks)
SELECT bal FROM dbo.Acct WHERE id = 1;
 
-- session C
SELECT r.session_id, r.blocking_session_id, r.wait_type, r.wait_resource, r.command
FROM sys.dm_exec_requests r WHERE r.blocking_session_id > 0;
-- 78 | 52 | LCK_M_S | KEY: 5:72057594046513152 (8194443284a0) | SELECT

sys.dm_tran_locks showed session 52 holding KEY X, PAGE IX, OBJECT IX and session 78 waiting for KEY S. The reader waited 8 seconds. With READ_COMMITTED_SNAPSHOT ON the same read returned immediately (sqlserver-isolation-rcsi).

Deadlock (executed): A updates row 1 then row 2; B updates row 2 then row 1, with a 4-second pause between statements. Result: A committed; B received

Msg 1205, Level 13: Transaction (Process ID 52) was deadlocked on lock resources with another process
and has been chosen as the deadlock victim. Rerun the transaction.

Find deadlock graphs in the always-on system_health Extended Events session (event xml_deadlock_report), or capture them with your own session.

Pitfalls and production notes

  • Access objects in a consistent order across code paths; that alone prevents most deadlocks.
  • Keep transactions short; never wait for user input or remote calls inside one.
  • Missing indexes widen scans and lock more rows; index the predicates of UPDATE/DELETE.
  • Applications must retry on 1205 (and on 1222, lock timeout, when configured).
  • Avoid the NOLOCK hint: it permits dirty, duplicated or missing rows. Use RCSI or snapshot isolation instead.
  • KILL <session> rolls back, which can take as long as the work done; check sys.dm_exec_requests.percent_complete.
  • Find the head blocker with sys.dm_os_waiting_tasks / sys.dm_exec_requests; sp_whoisactive (community) is the usual tool.

Practice questions

  1. Why does a long-running SELECT block an UPDATE under READ COMMITTED but not under RCSI?
  2. What does lock escalation protect against, and what does it cost?
  3. Design a retry loop for error 1205.

References

  • Microsoft Learn, Transaction locking and row versioning guide; Deadlocks guide

locking-and-mvcc · isolation-levels · sqlserver-isolation-rcsi · sqlserver-wait-stats