# 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.

Subject: SQL | Lesson 33 of 44 | 7 min read
Interactive lesson: https://codewithanimation.in/sql/isolation-levels
Previous: [Transactions & ACID](https://codewithanimation.in/sql/transactions-and-acid.md) | Next: [Locks & Deadlocks](https://codewithanimation.in/sql/locks-and-deadlocks.md)

## 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.

## Common mistake: Serialization errors mean retry

At Repeatable Read, B's snapshot still shows the old row, but the row has changed since. B cannot update it, so PostgreSQL stops with `ERROR: could not serialize access due to concurrent update` (SQLSTATE 40001). Your application must catch it, roll back and run the whole transaction again.

**Serializable** is the strictest level. It works like Repeatable Read, and it also watches for patterns that could not have happened if the transactions had run one after another. If it finds one, it fails a transaction with `could not serialize access due to read/write dependencies among transactions`, again SQLSTATE 40001. The same rule applies: retry.

A stricter level is not always better. It gives stronger guarantees and costs more retries. Choose the weakest level that is still correct for the work you do.

## Tip: Handle 40001 once, in one place

Wrap your transactions in a small helper that catches SQLSTATE 40001 and retries a few times. Then you can use Repeatable Read or Serializable wherever correctness needs it.

## Try it: 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.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/isolation-levels

## Check yourself

### Question 1

Session B has updated a row but has not committed. Can session A, at any PostgreSQL isolation level, see B's change?

- A. No, PostgreSQL never allows dirty reads
- B. Yes, at Read Uncommitted
- C. Yes, at Read Committed

Correct answer: A

Uncommitted changes are never visible to other sessions in PostgreSQL.

- Why not B: PostgreSQL accepts Read Uncommitted, but it behaves exactly like Read Committed.
- Why not C: Read Committed only shows committed data.

### Question 2

Session 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?

- A. The same value as the first read
- B. The new value that B committed
- C. An error

Correct answer: A

Repeatable Read uses the snapshot taken at the first query, so a repeated read gives the same answer.

- Why not B: That is Read Committed. Repeatable Read keeps one snapshot for the whole transaction.
- Why not C: Reading never fails here. Errors appear when you try to update a row that has changed.

### Question 3

A transaction at Repeatable Read fails with SQLSTATE 40001. What should the application do?

- A. Roll back and retry the whole transaction
- B. Ignore the error and continue
- C. Switch to Read Uncommitted

Correct answer: A

The manual says applications using this level must be prepared to retry transactions that fail with a serialization failure.

- Why not B: The transaction is aborted. Nothing more can run in it.
- Why not C: That does not exist as a separate behaviour in PostgreSQL, and it would not fix the conflict.

## 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.
