Sub topic 9
Isolation levels as snapshot timing
This chapter assumes basic SQL and transactions. You should know BEGIN, COMMIT, and ROLLBACK, and you should be able to open two psql sessions connected to the same database.
An isolation level controls how a transaction observes changes made by other transactions. In PostgreSQL, the key difference is when a transaction takes its snapshot. A snapshot is the set of committed row versions visible to a query.
Read Committed: one snapshot per statement
Read Committed is PostgreSQL's default isolation level. Each SQL statement takes a new snapshot when it starts. A statement sees changes committed before that snapshot was taken.
Therefore, two SELECT statements in one transaction can return different results:
-- Session A
BEGIN ISOLATION LEVEL READ COMMITTED;
SELECT balance FROM accounts WHERE id = 1;
-- Suppose this returns 100.
-- Session B, after Session A's first SELECT
UPDATE accounts
SET balance = balance + 10
WHERE id = 1;
COMMIT;
-- Session A
SELECT balance FROM accounts WHERE id = 1;
-- This can return 110.
COMMIT;Read Committed prevents a statement from seeing an uncommitted change. However, the transaction can experience nonrepeatable reads, where the same query returns a different value later. A repeated search can also find new rows. These effects are often acceptable for short, independent statements.
Repeatable Read: one snapshot per transaction
Repeatable Read takes a snapshot for the transaction's first query. Later queries continue using that view, so repeated reads are stable.
-- Session A
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT balance FROM accounts WHERE id = 1;
-- Suppose this returns 100.
-- Session B changes and commits the same row.
-- Session A
SELECT balance FROM accounts WHERE id = 1;
-- Still returns 100.
COMMIT;PostgreSQL's Repeatable Read is based on snapshot isolation. It does more than provide stable reads. If Session A tries to update a row that Session B changed after Session A's snapshot, PostgreSQL raises a serialization failure instead of silently overwriting the newer version.
-- Session A
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT balance FROM accounts WHERE id = 1;
-- Session B:
-- UPDATE accounts SET balance = balance + 10 WHERE id = 1;
-- COMMIT;
-- Session A:
UPDATE accounts SET balance = balance - 5 WHERE id = 1;
-- ERROR: could not serialize access due to concurrent update
ROLLBACK;Applications using Repeatable Read must retry the whole transaction after this error.
Serializable: stable reads plus dependency checks
Serializable is the strongest isolation level. PostgreSQL still uses snapshots, but Serializable Snapshot Isolation also tracks dangerous read and write dependencies. If the combined behavior could not have occurred in any serial order, PostgreSQL aborts one transaction with a serialization failure.
| Isolation level | Snapshot timing | Typical conflict result |
|---|---|---|
| Read Committed | One per statement | A statement may wait, then use the newer row |
| Repeatable Read | One per transaction | Concurrent update can cause serialization failure |
| Serializable | One per transaction plus dependency tracking | Additional serialization failures are possible |
Serializable does not make transactions immune to errors. It moves some concurrency decisions from application logic into the database. The application must detect serialization failures, roll back, and retry the complete unit of work.
Choosing an isolation level
Use Read Committed when each statement can make its own decision and short-lived changes in the surrounding data are acceptable. Use Repeatable Read when several queries must share one consistent view. Use Serializable when correctness requires the result to match some serial ordering, and the application can retry transactions.
Exercise
Session A uses Repeatable Read and reads a balance of 100. Session B then commits an update to that row. What should Session A expect when it updates the same row?
Show answer
PostgreSQL can raise a serialization failure because the row changed after Session A's snapshot. Session A must roll back and retry the complete transaction.