7.8 Isolation Levels: When the Snapshot Is Taken
What data a transaction sees depends on when the snapshot is created. This is the isolation level. The SQL standard defines four levels, each of which allows or prevents particular anomalies. The reference anomalies are the three seen in Section 7.1: Dirty Read, Non-repeatable Read, and Phantom Read. The SQL standard defines isolation levels by how many of these three it prevents. Lost Update is not in the standard's isolation level table; the reason and the countermeasures are covered in Sections 7.6 and 7.10.
| Isolation level | Dirty Read | Non-repeatable Read | Phantom Read |
|---|---|---|---|
| Read Uncommitted | Possible | Possible | Possible |
| Read Committed | Prevented | Possible | Possible |
| Repeatable Read | Prevented | Prevented | Possible (by the standard) |
| Serializable | Prevented | Prevented | Prevented |
Table 7-5. Isolation levels and anomalies. This is the minimum the standard requires; the actual behavior of each product is described in the text.
Reading this table again from the MVCC perspective makes the difference between the two levels simple.
Read Committed creates a new snapshot for each statement. Each statement sees what was committed up to the moment it starts. So there is no Dirty Read, but the first and second statements in a transaction use different snapshots, so changes committed in between are visible to the second statement. This is why a Non-repeatable Read occurs.
Repeatable Read creates the snapshot once when the transaction starts and uses it to the end. All statements in the transaction see the same snapshot, so reading the same row any number of times returns the same value. Because the snapshot itself is fixed in an MVCC implementation of Repeatable Read, it in practice prevents most of the Phantoms that the standard allows. PostgreSQL's Repeatable Read is like this.
Figure 7-7. Snapshot Timing in Read Committed and Repeatable Read
sequenceDiagram
participant RC as Transaction A (Read Committed)
participant H as Hong Gildong row
participant W as T1 (transfer)
participant RR as Transaction B (Repeatable Read)
Note over RR: Transaction starts, snapshot S0 created
Note over RC: Statement 1 starts, snapshot S1 created
RC->>H: Statement 1, read against S1
H-->>RC: 500,000
RR->>H: Statement 1, read against S0
H-->>RR: 500,000
W->>H: Change 500,000 to 400,000 and COMMIT
Note over RC: Statement 2 starts, new snapshot S2 created
RC->>H: Statement 2, read against S2
H-->>RC: 400,000
RR->>H: Statement 2, still read against S0
H-->>RR: 500,000
The diagram places two transactions that read the same row twice side by side, differing only in isolation level. A on the left is Read Committed, B on the right is Repeatable Read, and T1 in the middle modifies Hong Gildong's balance and commits between the two reads.
- B creates snapshot S0 once, when the transaction starts. A creates snapshot S1 not at transaction start but when statement 1 starts.
- On the first read, both see 500,000, because T1 has not yet committed.
- T1 changes the value to 400,000 and commits.
- A's statement 2 creates a new snapshot S2. At the time of S2, T1 is committed, so 400,000 is visible. The value changed within the same transaction, so this is a Non-repeatable Read.
- B's statement 2 does not create a new snapshot and uses S0 as is. S0 records T1 as in progress, so the 400,000 version created by T1 is not visible, and B sees 500,000 again.
The entire difference between the two isolation levels is that A has two snapshot marks (Notes), one per statement, while B has only one, at the start of the transaction.
Serializable is the strongest level. It guarantees that even if transactions actually ran concurrently, the result is the same as "running them one at a time in some order." It detects even subtle conflicts that snapshots alone cannot catch (where two transactions write based on each other's read values) and fails one of them. In return for the stronger guarantee, some transactions are rolled back with a "serialization failure" error, and the application must have retry logic.
A classic case showing why Serializable is needed is Write Skew. Two transactions modify different rows, but the condition each one read is broken by the other's write. Restated with the bank example: Hong Gildong has two accounts (A and B), and there is a rule that "withdrawal is allowed if the sum of the two balances is 0 or more." With a sum of 1,000,000 won, two withdrawals arrive at the same time.
| Time | Withdrawal T1 (1,000,000 from A) | Withdrawal T3 (1,000,000 from B) |
|---|---|---|
| 1 | Reads the A+B sum: 1,000,000 | |
| 2 | Reads the A+B sum: 1,000,000 | |
| 3 | Condition passes, withdraws 1,000,000 from A, COMMIT | |
| 4 | Condition passes, withdraws 1,000,000 from B, COMMIT |
sequenceDiagram
participant T1 as T1 (withdraw from A)
participant A as Account A row
participant B as Account B row
participant T3 as T3 (withdraw from B)
T1->>A: Read A and B against the snapshot
T3->>B: Read A and B against the snapshot
Note over T1,T3: Both see a sum of 1,000,000 and pass the condition
T1->>A: Withdraw 1,000,000 from A, COMMIT
T3->>B: Withdraw 1,000,000 from B, COMMIT
Note over A,B: Different rows, so no write conflict. Sum is −1,000,000
T1 modifies only the account A row, and T3 only the account B row. Both transactions read both A and B in their own snapshots and confirmed a sum of 1,000,000, but because the rows they write are different, neither row locks nor the first-committer-wins check detect a conflict. Each decision was correct within its own snapshot, but combining the results written by the other breaks the rule.
The two transactions modified different rows, so there is no write conflict, and the sum of 1,000,000 won that each read from its own snapshot was accurate at that point. Repeatable Read (snapshot isolation) lets both pass. Yet the result is a sum of −1,000,000 won, a rule violation. Unlike Lost Update, no write was lost, and unlike Phantom, no rows were added. It is a case where "a write to another row was made based on a value that was read, and that basis was simultaneously broken by another transaction's write."
Write Skew cannot be prevented by snapshots alone. There are two ways to prevent it. One is to take locks on the rows being read with FOR UPDATE so that the second transaction stops at the read stage (Section 7.6); the other is to use Serializable. PostgreSQL's Serializable (SSI, Serializable Snapshot Isolation) takes no additional locks but tracks the dependencies of "who read what and wrote what," and when such a pattern is detected it fails one transaction with a serialization error. InnoDB's Serializable converts every plain SELECT into a read that takes a shared lock, and SQL Server's Serializable uses range locks. The names are the same, but the cost and behavior differ, so check each product.
Read Uncommitted is the level that allows Dirty Reads, and in an MVCC database it is effectively meaningless. Showing an old version costs almost nothing, so there is no reason to show uncommitted values. PostgreSQL behaves as Read Committed even if Read Uncommitted is requested, and Oracle does not provide this level.
Raising the isolation level raises correctness and lowers concurrency. Thanks to MVCC, however, that cost is much smaller than in the lock-based era, because a read query using Repeatable Read does not block writes. The cost comes mainly from having to keep old versions longer. If a transaction uses its start-time snapshot to the end, every old version created after that point must be kept until that transaction ends. This is the subject of the next section.