Sub topic 10
Snapshot Isolation and Repeatable Read
Assume you know basic SQL and can use BEGIN, COMMIT, and ROLLBACK. This chapter focuses on how a transaction gets a consistent view of rows while other transactions change the database.
The snapshot model
Snapshot isolation uses multiversion concurrency control (MVCC). MVCC keeps multiple committed versions of a row. When a transaction reads, the database chooses versions that were committed before the transaction's snapshot.
A transaction can therefore continue reading an older version while another transaction updates the current version. Ordinary reads usually do not block ordinary writes.
Under snapshot isolation, both reads in Transaction A use the same snapshot. Transaction A does not see Transaction B's later commit.
This gives repeatable reads for ordinary snapshot-based queries:
- A row read once keeps the same visible value.
- A later query does not see commits that occurred after the snapshot.
- A concurrent update to the same row may cause a conflict when the transaction writes.
Repeatable read in PostgreSQL
PostgreSQL's REPEATABLE READ uses one transaction-wide snapshot. The following example requires two database sessions.
-- Session A
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance integer NOT NULL
);
INSERT INTO accounts VALUES (1, 100);
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT balance FROM accounts WHERE id = 1;
-- 100
-- Pause Session A here.-- Session B
BEGIN;
UPDATE accounts
SET balance = 50
WHERE id = 1;
COMMIT;-- Session A
SELECT balance FROM accounts WHERE id = 1;
-- Still 100
COMMIT;Session A keeps its original snapshot even though Session B committed. A new transaction started after Session B commits would see 50.
Snapshot isolation is not always serializable
A stable snapshot prevents many anomalies, but it does not make every transaction appear to run alone. In particular, it can allow write skew.
Suppose a rule says that at least one doctor must remain on duty. Two transactions can both read that two doctors are on duty. One transaction turns Alice off duty, and the other turns Bob off duty. They update different rows, so neither sees a direct row conflict. Both can commit, leaving no doctor on duty.
Repeatable read and phantom rows
A phantom is a row that appears or disappears from a repeated range query. The SQL standard permits phantoms at REPEATABLE READ, but many MVCC implementations provide stronger behavior for snapshot reads.
For example, a PostgreSQL repeatable-read transaction keeps the same view of:
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SELECT count(*)
FROM orders
WHERE status = 'pending';
-- Other transactions may insert matching orders here.
SELECT count(*)
FROM orders
WHERE status = 'pending';
-- Same snapshot count
COMMIT;This does not mean every operation is non-blocking. A locking read such as SELECT ... FOR UPDATE follows different rules and may wait for another transaction's row lock.
| Isolation choice | Ordinary snapshot reads | Same-row write conflict | Cross-row write skew |
|---|---|---|---|
| Snapshot-style repeatable read | Stable transaction view | Detected by many engines | May be allowed |
| Serializable | Stable serializable view | Detected or blocked | Prevented, often by aborting one transaction |
Choosing between repeatable read and serializable
Use repeatable read when a transaction needs a consistent report or must make decisions from a stable view, and your application can tolerate snapshot isolation's limits.
Use SERIALIZABLE when correctness depends on a predicate or rule spanning multiple rows. Serializable execution can reduce concurrency and can abort transactions with a serialization failure. The application must retry the complete transaction.
Exercise
Transaction A uses PostgreSQL REPEATABLE READ. Transaction B commits an update after A's first SELECT. What does A's second ordinary SELECT see, and what anomaly can still occur?
Show answer
A sees the value from its original snapshot. Write skew can still occur when concurrent transactions read the same rule but update different rows.