Learning navigation

Code with Animation

Isolation Levels

See what two sessions can and cannot see of each other's work at Read Committed and Repeatable Read, and what a serialization error asks you to do.

SQL7 min readConcept 33 of 44

How much of other people's work do you see?

Many sessions use a database at the same time. An isolation level decides what one transaction can see of the changes other transactions make while it is running.

PostgreSQL has three working levels: Read Committed (the default), Repeatable Read and Serializable. A fourth, Read Uncommitted, is accepted but behaves exactly like Read Committed. At every level, you never see another session's uncommitted changes: PostgreSQL has no dirty reads.

Same query, two answers

A report that reads a balance and then reads it again can get two different numbers if someone commits in between. Whether that is allowed, and what happens when two sessions change the same row, is decided by the level you choose.

Read Committed and Repeatable Read

At Read Committed, every statement sees a fresh snapshot of the data that was committed when that statement began. Session A reads Alice's balance (100), session B updates it to 150 and commits, and A's next read in the same transaction returns 150. That is a non-repeatable read. A new row that appears between two counts is called a phantom.

At Repeatable Read, one snapshot is taken at the first query and used for the whole transaction. A's second read still returns 100, and a second count does not include rows inserted by others. In PostgreSQL, Repeatable Read has no phantoms.

Two sessions adding to the same row: B waits for A. If A commits, Read Committed re-reads the newest version of the row and applies B's change on top: Alice ends at 130, and no update is lost.

Replay each story at both levels

Pick a story and a level, then press Next statement. The step where the two levels disagree is marked.

A: BEGIN ISOLATION LEVEL READ COMMITTED;
A: SELECT balance FROM accounts WHERE name = 'Alice';
B: BEGIN;
B: UPDATE accounts SET balance = 150 WHERE name = 'Alice';
A: SELECT balance FROM accounts WHERE name = 'Alice';
B: COMMIT;
A: SELECT balance FROM accounts WHERE name = 'Alice';
A: COMMIT;
What each statement returned (0 of 8)
sessiontextresulttext

Pick a story and a level, then press Next statement. Session A is the one that reads or updates twice; session B is the other person.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1Session B has updated a row but has not committed. Can session A, at any PostgreSQL isolation level, see B's change?
  2. 2Session A reads a balance twice in one transaction, and B commits a change in between. At Repeatable Read, what does A's second read return?
  3. 3A transaction at Repeatable Read fails with SQLSTATE 40001. What should the application do?

Remember this

  • Read Committed is the default: every statement sees data committed when it began, so a repeated read can change.
  • Repeatable Read keeps one snapshot for the whole transaction. In PostgreSQL it also has no phantoms.
  • PostgreSQL never shows uncommitted changes. Read Uncommitted behaves like Read Committed.
  • Serialization failures (SQLSTATE 40001) mean: roll back and retry the transaction.

Done with this concept?

Mark it complete to track your progress. No login needed.