Isolation Levels and SSI
This page explains how each isolation level is implemented, how SQL dialects map onto the four internal levels, and how the Serializable level detects write skew with a serializable snapshot isolation (SSI) tracker. The MVCC machinery the levels are built on is described in MVCC.
Internal levels
IsolationLevel |
Read bound | Commit-time validation | SSI tracking |
|---|---|---|---|
ReadUncommitted |
Latest committed (snapshot_read_ts() == None) |
None | No |
ReadCommitted |
Latest committed; one statement snapshot per statement | None | No |
SnapshotIsolation |
start_ts - 1 |
Write-write (first committer wins) | No |
Serializable |
start_ts - 1 |
Write-write plus read-write, then SSI cycle check | Yes |
(source: src/transaction/mvcc.rs, IsolationLevel, Transaction::snapshot_read_ts, TransactionManager::validate)
ReadUncommitted behaves the same as ReadCommitted. Another transaction's writes sit in that transaction's private write buffer until commit, so there is no path by which a dirty value can be read. (source: src/transaction/mvcc.rs, Transaction::record_write)
The Rust Default for IsolationLevel is SnapshotIsolation. This default applies to Rust callers of TransactionManager::begin_default. It is not the SQL default. (source: src/transaction/mvcc.rs, impl Default for IsolationLevel, begin_default)
SQL defaults and names
| Path | Default for BEGIN
|
Source |
|---|---|---|
| PostgreSQL and other non-MySQL SQL sessions | Read Committed |
src/query/engine.rs, begin_transaction (pending_isolation_level defaults to Read Committed) |
| MySQL sessions |
mysql_session_isolation, default SnapshotIsolation (REPEATABLE READ) |
src/query/engine.rs, begin_isolation
|
| MongoDB multi-document transactions | SnapshotIsolation |
src/protocol/mongodb/commands.rs |
Parsing and display:
| SQL text | Internal level |
SHOW transaction_isolation text |
|---|---|---|
READ UNCOMMITTED |
ReadUncommitted |
read uncommitted |
READ COMMITTED |
ReadCommitted |
read committed |
REPEATABLE READ |
SnapshotIsolation |
repeatable read |
SERIALIZABLE |
Serializable |
serializable |
(source: src/query/engine.rs, isolation_level_from_sql, parse_isolation_level_str, isolation_level_guc_name)
isolation_level_from_sql matches whole words and returns None for anything else, so SET TRANSACTION ISOLATION LEVEL SNAPSHOT_PLEASE and SET transaction_isolation = 'banana' are errors. Trailing modes such as , READ ONLY are accepted. Tested by unknown_isolation_level_is_refused and test_begin_isolation_level_serializable_syntax in src/query/engine.rs tests.
SET TRANSACTION ISOLATION LEVEL issued while a transaction is open changes the open transaction in place through Transaction::set_isolation, which also registers the transaction with, or retires it from, the SSI tracker. (source: src/query/engine.rs, apply_isolation_to_current_txn; src/transaction/mvcc.rs, Transaction::set_isolation)
begin_transaction treats a pending level of Read Committed as "not set". In a MySQL session, a pending READ COMMITTED therefore falls through to the session isolation. (source: src/query/engine.rs, begin_transaction, begin_isolation) Not verified: no test was found for an explicit SET TRANSACTION ISOLATION LEVEL READ COMMITTED followed by BEGIN in a MySQL session.
Read Committed
Each statement takes a statement snapshot (see Statement snapshots), so one statement sees one consistent cut even while autocommit batches are landing. No conflict check runs at commit. Lost updates are prevented by row locks taken by UPDATE and DELETE, not by validation. Write skew is allowed, as in PostgreSQL. (source: src/transaction/mvcc.rs, statement_snapshot, validate; src/query/engine.rs, acquire_row_lock)
Tested by test_read_committed_write_skew_both_commit, test_concurrent_update_same_row_no_lost_updates_or_vanish and sum_over_concurrent_transfers_sees_whole_transactions in src/query/engine.rs tests.
Snapshot Isolation (REPEATABLE READ)
Reads see the snapshot at start_ts - 1 plus the transaction's own buffered writes. At commit, check_write_conflicts rejects the transaction if any key it wrote has a recent_writes entry from another transaction with a commit timestamp at or after its start_ts. The error is Error::TransactionConflict, which PostgreSQL clients see as SQLSTATE 40001. (source: src/transaction/mvcc.rs, check_write_conflicts)
In PostgreSQL dialect, the engine also checks each row it is about to update or delete after the row lock is granted. check_serializable_row reads the key at the snapshot and compares it with the latest version. If they differ, a transaction that committed after the snapshot changed the row, and the statement fails at once with 40001 "could not serialize access due to concurrent update". The check is skipped when current_read_writes is set or when the transaction has no snapshot bound. (source: src/query/engine.rs, check_serializable_row)
Tested by repeatable_read_update_of_concurrently_changed_row_is_40001 in src/query/engine.rs tests and test_write_write_conflict in src/transaction/mvcc.rs tests.
MySQL and SQL Server
For MySQL and SQL Server sessions at REPEATABLE READ or SERIALIZABLE, mark_mysql_txn sets current_read_writes = true. Reads still use the snapshot, but writes act on the latest version under row locks. A second writer waits for the first, then writes on top of its result. Commit-time validation, the SSI check and check_serializable_row are all skipped. This follows InnoDB, where a blocked writer never gets a serialization failure. (source: src/query/engine.rs, mark_mysql_txn; src/transaction/mvcc.rs, commit_validate_only)
Tested by test_mysql_sessions_share_write_conflict_detection in src/protocol/mysql/mod.rs.
Serializable
Serializable adds two checks to Snapshot Isolation:
-
check_read_write_conflictsrejects the commit if any key in the read set was written by a transaction that committed at or afterstart_ts. - Before validation,
SsiTracker::check_cycleslooks for a dangerous structure around the committing transaction.
(source: src/transaction/mvcc.rs, commit_validate_only, check_read_write_conflicts)
What feeds the SSI tracker
- Writes:
Transaction::record_writepushes each key to the tracker when the transaction is Serializable. - Reads: the engine's table-scan paths call
TransactionManager::ssi_record_readfor every storage key returned by the scan, after merging the transaction's own buffered writes.
(source: src/transaction/mvcc.rs, Transaction::record_write, ssi_record_read; src/query/engine.rs, table scan paths that call ssi_record_read)
Only Serializable transactions are registered. Read Committed and Snapshot Isolation transactions pay no SSI cost. Tested by test_read_committed_not_registered in src/transaction/ssi.rs.
Data structure
SsiTracker holds one parking_lot::Mutex over SsiInner:
| Field | Type | Meaning |
|---|---|---|
active |
HashMap<TxnId, TxnSsiState> |
Registered Serializable transactions in flight |
committed |
VecDeque<(TxnId, TxnSsiState)> |
The last SSI_COMMITTED_WINDOW (128) committed Serializable transactions |
seq |
u64 |
Commit sequence counter |
Each TxnSsiState has read_set and write_set (HashSet<Vec<u8>>), start_seq (the value of seq at registration) and commit_seq. (source: src/transaction/ssi.rs, SsiTracker, SSI_COMMITTED_WINDOW)
Algorithm
At commit of T, check_cycles(T) looks for T as the pivot of T2 -rw-> T -rw-> T3:
- In-edge: some other transaction T2, either active or committed with
commit_seq >= T.start_seq, whoseread_setintersectsT.write_set. If none exists, T commits. - Out-edge: some other transaction T3 that has already committed with
commit_seq > T.start_seq, whosewrite_setintersectsT.read_set. - If both exist, T fails with "could not serialize access due to concurrent update", which maps to
Error::TransactionConflictand SQLSTATE 40001.
The out-edge counts only a T3 that has already committed. While T3 is still running, T commits, and T3 is the one rejected when it tries to commit. In a plain write skew between two transactions, the first committer wins and the second gets 40001. (source: src/transaction/ssi.rs, SsiTracker::check_cycles)
After a successful commit, retire_committed moves the transaction into the committed window with the next commit_seq, dropping the oldest entry when the window holds 128. retire_aborted removes an aborted transaction without a window entry. (source: src/transaction/ssi.rs, retire_committed, retire_aborted)
Tests in src/transaction/ssi.rs: test_no_conflict_single_tx, test_write_skew_detected, test_no_write_skew_different_keys, test_committed_window_overlap, test_committed_window_used, test_read_committed_not_registered, test_retire_aborted_cleans_up. End to end: test_ssi_write_skew_detected in src/query/engine.rs tests.
Limits of the SSI implementation
-
Key-exact, not predicate. The read set holds the keys a scan returned, not the predicate. An insert of a new key that would have matched an earlier scan does not create an edge, so phantom anomalies are not detected. The module comment says this. (source:
src/transaction/ssi.rs, module comment andsets_intersect) - Bounded window. Only 128 committed Serializable transactions are kept. A committed T3 that has been evicted can no longer supply an out-edge. Not verified: no test exercises eviction under load.
- False positives. Two transactions that touch the same key without a real dependency can still be aborted. Clients must retry 40001.
- Single global mutex. Every Serializable read and write takes the tracker mutex.
Summary of anomalies
| Anomaly | Read Committed | Snapshot Isolation | Serializable |
|---|---|---|---|
| Dirty read | Prevented | Prevented | Prevented |
| Lost update (PG dialect) | Prevented by row locks | 40001 | 40001 |
| Non-repeatable read | Possible | Prevented | Prevented |
| Write skew on the same keys | Possible | Possible | Detected (40001) |
| Phantom from a new key | Possible | Possible | Not detected |
(source: src/transaction/mvcc.rs, validate; src/transaction/ssi.rs, check_cycles; src/query/engine.rs, check_serializable_row, acquire_row_lock)