RCSI and Snapshot Isolation
SQL Server is lock-based by default, so reading a row that is being modified means waiting until the writing transaction ends. When you turn on row versioning, each modification copies the old version to the version store, and readers take no locks and read the version that matches their own point in time. There are two options. RCSI (READ_COMMITTED_SNAPSHOT) changes the existing Read Committed behavior to a statement-level snapshot, and snapshot isolation (ALLOW_SNAPSHOT_ISOLATION) enables the SNAPSHOT isolation level, which gives a transaction-level snapshot.
Key Specs
| Item | RCSI | Snapshot isolation |
|---|---|---|
| Option | READ_COMMITTED_SNAPSHOT ON | ALLOW_SNAPSHOT_ISOLATION ON |
| How to use | Once on, it applies to all of Read Committed immediately (no code change) | SET TRANSACTION ISOLATION LEVEL SNAPSHOT in the session |
| Snapshot time | Per statement | The transaction's first data access |
| Write conflict | Waits on a lock (same as ordinary Read Committed) | Modifying a row that another transaction modified first raises error 3960 and rolls back the transaction |
| Azure SQL Database default | On | On |
| SQL Server default | Off | Off |
| Item | Value |
|---|---|
| Version store | tempdb. With ADR on, the persistent version store (PVS) inside the user database |
| Per-row information | A 14-byte version tag (transaction sequence number and previous-version pointer). Added to rows modified after the option is turned on |
| Cleanup | A background task periodically removes versions that are no longer needed (about every minute) |
How It Works
Figure. Copying the old version to the version store on modification
flowchart LR
A["T1: UPDATE balance 500K → 400K"] --> B["Table row: 400K<br/>14-byte tag holds previous-version pointer"]
A --> C["Version store: 500K<br/>tempdb or PVS"]
B -- "pointer" --> C
D["T2: SELECT (started before T1 commit)"] --> B
B -- "change T1 made that did not exist at T2's time" --> C
C --> E["T2 reads 500K, no lock wait"]
Diagram explanation: When T1 modifies the row, the table row takes the new value 400K, and the old value 500K is copied to the version store. The row's 14-byte tag points to that previous version. When T2 reads the row, if the row's version is newer than its own point in time, it follows the pointer and reads the version from its own point in time. So T2 does not wait for T1's X lock.
Caution when turning on RCSI. Some code behaves differently once reads stop waiting. For example, if "read, check, then modify" logic relied on lock waits, it can now read an old value and move on, so use the UPDLOCK hint or the READCOMMITTEDLOCK hint in such places. The ALTER DATABASE that turns on RCSI finishes only when no other connections exist (WITH ROLLBACK IMMEDIATE).
When the version store grows. A long-open snapshot transaction or a long query prevents deleting versions created after its point in time, so tempdb (or the PVS) keeps growing. On a system with many updates, allow generous tempdb space and I/O.
Azure SQL Database. RCSI and snapshot isolation are both on by default and ADR is always on, so an application moved from on-premises SQL Server reads old versions instead of waiting on locks.
Configuration and Checks
SELECT name, is_read_committed_snapshot_on, snapshot_isolation_state_desc, is_accelerated_database_recovery_on
FROM sys.databases;
ALTER DATABASE tune SET READ_COMMITTED_SNAPSHOT ON WITH ROLLBACK IMMEDIATE;
ALTER DATABASE tune SET ALLOW_SNAPSHOT_ISOLATION ON;
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
BEGIN TRANSACTION;
SELECT balance FROM dbo.account WHERE id = 1;
COMMIT;
-- Version store usage by database
SELECT DB_NAME(database_id) AS db, reserved_space_kb / 1024 AS reserved_mb
FROM sys.dm_tran_version_store_space_usage;
-- Long-open snapshot transactions
SELECT session_id, transaction_id, elapsed_time_seconds
FROM sys.dm_tran_active_snapshot_database_transactions ORDER BY elapsed_time_seconds DESC;
Comparison with Other DBMSs
| SQL Server | PostgreSQL | InnoDB | Oracle | |
|---|---|---|---|---|
| Old version location | tempdb version store or PVS | Inside the table (cleaned by VACUUM) | Undo log | Undo tablespace |
| Turning on and off | Database option | Always MVCC | Always MVCC | Always MVCC |
| Default Read Committed | Lock-based (Azure SQL uses RCSI) | Statement-level snapshot | — (the default is Repeatable Read) | Statement-level snapshot |
| Write conflict in a transaction snapshot | Error 3960 | Serialization failure error | Modifies the latest row | ORA-08177 (Serializable) |
In the Articles
- 7.5 The Idea of MVCC: Show Readers an Old Version
- 7.7 Where to Keep Old Versions
- 7.8 Isolation Levels: When to Take the Snapshot
- 7.10 Differences Between DBMSs and Phenomena You Meet in Practice
NOTE The version store location and defaults can vary by version, ADR setting, and Azure service.