Isolation Levels
An isolation level in SQL is a setting that decides how much one transaction can see of other transactions running at the same time. It is used to balance correctness against speed: stronger levels prevent more surprises, but make transactions wait for or retry each other more often. The SQL standard defines four levels by which read phenomena they allow. In this lesson, we will meet each phenomenon on a two-session timeline, learn the four levels and what PostgreSQL actually does at each, and set the level for a transaction.
In One Minute
- The four standard levels: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE.
- PostgreSQL's default is READ COMMITTED: each statement sees data committed before it started.
- REPEATABLE READ gives the whole transaction one snapshot; SERIALIZABLE makes concurrent transactions behave as if they ran one at a time.
- Stronger levels can fail with a serialization error; the program must retry.
- Set it with
BEGIN ISOLATION LEVEL ...orSET TRANSACTION ISOLATION LEVEL ....
Setting the Level
BEGIN ISOLATION LEVEL REPEATABLE READ;
-- or, as the first statement of a transaction:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;SHOW transaction_isolation;
BEGIN ISOLATION LEVEL REPEATABLE READ;
SHOW transaction_isolation;
COMMIT; transaction_isolation
-----------------------
read committed
(1 row)
BEGIN
transaction_isolation
-----------------------
repeatable read
(1 row)
COMMITExplanation: Outside a transaction, the default is read committed. Inside, BEGIN ISOLATION LEVEL changed it for that transaction only. The phenomena below need two sessions running at once, which Try it can't do; the timelines are drawings of PostgreSQL's documented behavior.
The Read Phenomena
| Phenomenon | What happens |
|---|---|
| Dirty read | Seeing another transaction's changes before it commits (it might roll back) |
| Non-repeatable read | Reading the same row twice in one transaction and getting different values |
| Phantom read | Running the same query twice and getting new or missing rows |
| Lost update | Two transactions read a value, both change it, and one change overwrites the other |
| Write skew | Two transactions each check a rule, then each make a change that's fine alone but breaks the rule together |
A non-repeatable read, at READ COMMITTED: session A reads SQL Basics' fee ($400.00), session B changes it to $425.00 and commits, and A reads it again in the same transaction, now $425.00. A report computed by A from both reads mixes old and new values.
At REPEATABLE READ, A's second read still returns $400.00: the whole transaction sees the snapshot taken at its first statement.
The Four Levels
| Level | Dirty read | Non-repeatable read | Phantom | Serialization anomalies |
|---|---|---|---|---|
| READ UNCOMMITTED | Allowed by the standard; not in PostgreSQL | Possible | Possible | Possible |
| READ COMMITTED (PostgreSQL default) | Not possible | Possible | Possible | Possible |
| REPEATABLE READ | Not possible | Not possible | Not in PostgreSQL | Possible |
| SERIALIZABLE | Not possible | Not possible | Not possible | Not possible |
PostgreSQL is stricter than the standard requires: READ UNCOMMITTED behaves exactly like READ COMMITTED, so dirty reads never happen, and REPEATABLE READ also prevents phantoms. Lost updates and write skew are the anomalies to watch at READ COMMITTED.
Lost Updates and Write Skew
A lost update: Navika's study total is 45 minutes. Two sessions each read 45, one adds 30 and writes 75, the other adds 60 and writes 105. The final value is 105; the 30 minutes are lost. At READ COMMITTED, PostgreSQL lets both writes happen (the second waits for the first, then overwrites it). At REPEATABLE READ, the second writer fails with could not serialize access due to concurrent update and must retry. Locks and Deadlocks shows two simpler fixes.
Write skew: a rule says at least one teacher must be on call. Two teachers are on call; each, in their own transaction, checks "someone else is on call" and signs off. Each change is fine alone; together they leave nobody on call. Only SERIALIZABLE detects this, failing one transaction with a serialization error.
Defaults in Other Databases
| Database | Default level | Note |
|---|---|---|
| PostgreSQL | READ COMMITTED | MVCC snapshots; no dirty reads at any level |
| MySQL (InnoDB) | REPEATABLE READ | Uses gap locks to limit phantoms |
| SQL Server | READ COMMITTED | Uses locks by default; READ_COMMITTED_SNAPSHOT switches to snapshots |
| Oracle | READ COMMITTED | Also offers SERIALIZABLE (snapshot-based) |
Tabs showing each database's version are coming to this course.
Common Mistakes
Assuming the default prevents lost updates. READ COMMITTED doesn't; use an atomic update, a lock, or a stronger level.
Raising the level without retry logic. Serialization failures are expected at higher levels.
Expecting dirty reads in PostgreSQL. READ UNCOMMITTED behaves as READ COMMITTED.
Long transactions at REPEATABLE READ. They hold an old snapshot, which delays cleanup (see MVCC).
Interview Corner
What are the four isolation levels?
READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ and SERIALIZABLE, from weakest to strongest, defined by which read phenomena they allow.
What's the difference between a non-repeatable read and a phantom read?
A non-repeatable read is the same row returning different values; a phantom read is the same query returning a different set of rows because rows were inserted or deleted.
What is PostgreSQL's default isolation level?
READ COMMITTED. Each statement sees data committed before that statement began.
Practice
Challenge: Start a transaction at SERIALIZABLE, show the level, read SQL Basics' fee, and commit.
Show solution
BEGIN ISOLATION LEVEL SERIALIZABLE;
SHOW transaction_isolation;
SELECT title, fee_usd FROM courses WHERE course_id = 4;
COMMIT;BEGIN
transaction_isolation
-----------------------
serializable
(1 row)
title | fee_usd
------------+---------
SQL Basics | 400.00
(1 row)
COMMITAlone, a serializable transaction behaves like any other. The difference only appears when another transaction touches the same data at the same time, and one of them would then be asked to retry.
Key Takeaways
| You want | Use |
|---|---|
| The default | Nothing: READ COMMITTED |
| One snapshot for the whole transaction | BEGIN ISOLATION LEVEL REPEATABLE READ |
| As if transactions ran one at a time | BEGIN ISOLATION LEVEL SERIALIZABLE, plus retries |
| See the current level | SHOW transaction_isolation; |
Was this tutorial helpful?