Chapter 07 · Transactions, Locking, Row Versioning, Isolation, and Deadlocks

Version Store, tempdb Impact, Long Transactions, and Snapshot Cleanup

Trace version retention, tempdb pressure, long snapshot transactions, cleanup, and ADR Persistent Version Store differences with supported DMVs.

Advanced120–155 minutesVersion-store observation labSQL Server 2025 CU7 · compatibility 170VIEW SERVER PERFORMANCE STATE for full DMV evidenceLast reviewed: August 2026

Learning outcomes

A long-running ServiceHub report holds a versioned snapshot while writers continue changing rows. tempdb grows and an operator says “snapshot leaks tempdb.” That explanation is incomplete: older versions remain because active transactions can still need them, and when Accelerated Database Recovery (ADR) is enabled, the Persistent Version Store (PVS) changes where versions live. The correct investigation begins with configuration and transaction age.

01

Explain why committed row versions may remain needed.

02

Monitor traditional tempdb version-store space.

03

Identify active snapshot transactions and their age.

04

Distinguish tempdb version store from ADR PVS.

05

Fix retention causes before shrinking files or killing sessions.

Permissions

On SQL Server 2022+, full visibility into several performance DMVs requires VIEW SERVER PERFORMANCE STATE. Do not grant broad production privileges only to run a tutorial; use an authorized observer.

1. Version cleanup is constrained by active readers

Writers create older row images when versioning mechanisms need them. Commit does not automatically make every old image disposable: an active versioned transaction might still need a prior state. Cleanup can proceed only when versions are no longer required, and cleanup itself is asynchronous.

sql · identify which storage story applies
SELECT name,is_read_committed_snapshot_on,       snapshot_isolation_state_desc,is_accelerated_database_recovery_onFROM sys.databases WHERE name=N'ServiceHubLab';GO

2. Traditional version-store pressure is measurable in tempdb

sql · aggregate tempdb version-store usage
SELECT DB_NAME(database_id) AS database_name,       reserved_page_count,reserved_space_kbFROM sys.dm_tran_version_store_space_usageWHERE database_id=DB_ID(N'ServiceHubLab');GO

Microsoft documents this DMV as an efficient per-database aggregate of traditional version-store space in tempdb. It does not identify one culprit query. Sample before/during/after and correlate with active transactions and workload.

3. Create a bounded retention experiment

sql · Session A — hold a snapshot open
ALTER DATABASE ServiceHubLab SET ALLOW_SNAPSHOT_ISOLATION ON;GOUSE ServiceHubLab;SET TRANSACTION ISOLATION LEVEL SNAPSHOT;BEGIN TRANSACTION;SELECT * FROM lab07.Inventory ORDER BY part_id;-- Leave this transaction open while Session B runs bounded updates.
sql · Session B — create a small controlled version chain
DECLARE @i int=0;WHILE @i<200BEGIN  UPDATE lab07.Inventory  SET on_hand=CASE WHEN part_id=10 THEN 5+(@i%2) ELSE on_hand END  WHERE part_id=10;  SET @i+=1;END;GO

Do not expect a universal kilobyte result: page allocation and current state vary. Record your local delta. The lab is intentionally bounded so it cannot generate unbounded storage pressure.

sql · find long-running versioned transactions
SELECT session_id,transaction_id,transaction_sequence_num,       elapsed_time_seconds,max_version_chain_traversed,average_version_chain_traversedFROM sys.dm_tran_active_snapshot_database_transactionsORDER BY elapsed_time_seconds DESC;GO

4. ADR means versions can live in the user database PVS

ADR uses a Persistent Version Store in the user database for version-based recovery and has its own cleaner. When ADR is enabled, the old blanket statement “all row versions are in tempdb” is wrong. Observe is_accelerated_database_recovery_on and, when ON, use the supported PVS DMV for the exact engine build.

sql · ADR/PVS observation path
SELECT name,is_accelerated_database_recovery_onFROM sys.databases WHERE name=N'ServiceHubLab';GO-- If ADR is ON, inspect the supported schema on this exact build:-- SELECT * FROM sys.dm_tran_persistent_version_store_stats-- WHERE database_id=DB_ID(N'ServiceHubLab');

This chapter does not enable ADR merely to make a screenshot. ADR is a database recovery design choice; benchmark it in a dedicated disposable database if you need direct PVS experimentation.

5. Fix the retention mechanism, not the file size symptom

An old snapshot can keep versions relevant. Ending that transaction can make them eligible for cleanup, but space may not drop immediately because cleanup is asynchronous. Repeated tempdb shrinking is therefore not a causal fix. Killing a production session can trigger rollback and application failure and should be an incident decision with owner approval.

Wrong approach

“Version store is large; shrink tempdb every hour.” Shrinking attacks allocated file size rather than the transaction/configuration behavior retaining versions.

6. Cleanup and production monitoring

sql · end the experiment
IF @@TRANCOUNT>0 ROLLBACK TRANSACTION;SET TRANSACTION ISOLATION LEVEL READ COMMITTED;GO-- After all lab sessions end, restore the recorded ALLOW_SNAPSHOT_ISOLATION value.ALTER DATABASE ServiceHubLab SET ALLOW_SNAPSHOT_ISOLATION OFF;GO

For production, trend version-store/PVS space, oldest active versioned transaction, transaction duration, tempdb free space, user-database PVS growth when ADR is ON, and workload changes. One isolated DMV sample does not establish causation.

Check your understanding

  1. Why can versions remain after a writer commits?
  2. What does sys.dm_tran_version_store_space_usage report?
  3. What does ADR change?
  4. Does ending a snapshot instantly guarantee space reclamation?
  5. Why is repeated shrinking a poor first response?
Review the answers

An active versioned transaction may still need older row images.

Aggregated traditional tempdb version-store space by database.

It uses a Persistent Version Store in the user database and different recovery/cleanup mechanics.

No; cleanup is asynchronous.

It treats file size rather than the transaction/version-retention cause.

Authoritative references

Keep knowledge open

Help the academy stay free and grow.

If these tutorials save you time, a small donation supports new lessons, technical review, diagrams, examples, and long-term maintenance.

ETHEthereum / ERC-20 only
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0

Send only Ethereum or ERC-20 compatible assets to this address.