Isolation Levels

Updated · 7 min read
hardPostgreSQL 18sqlisolation-levelsread-committedserializableconcurrencyinterview

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 ... or SET TRANSACTION ISOLATION LEVEL ....

Setting the Level

SQL
BEGIN ISOLATION LEVEL REPEATABLE READ;
-- or, as the first statement of a transaction:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SQL▶ Try it
SHOW transaction_isolation;
 
BEGIN ISOLATION LEVEL REPEATABLE READ;
SHOW transaction_isolation;
COMMIT;
Output
 transaction_isolation
-----------------------
 read committed
(1 row)
 
BEGIN
 
 transaction_isolation
-----------------------
 repeatable read
(1 row)
 
COMMIT

Explanation: 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

PhenomenonWhat happens
Dirty readSeeing another transaction's changes before it commits (it might roll back)
Non-repeatable readReading the same row twice in one transaction and getting different values
Phantom readRunning the same query twice and getting new or missing rows
Lost updateTwo transactions read a value, both change it, and one change overwrites the other
Write skewTwo 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.

A reads 400.00; B updates to 425.00 and commits; A reads again and gets 425.00 at READ COMMITTED, 400.00 at REPEATABLE READ

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

LevelDirty readNon-repeatable readPhantomSerialization anomalies
READ UNCOMMITTEDAllowed by the standard; not in PostgreSQLPossiblePossiblePossible
READ COMMITTED (PostgreSQL default)Not possiblePossiblePossiblePossible
REPEATABLE READNot possibleNot possibleNot in PostgreSQLPossible
SERIALIZABLENot possibleNot possibleNot possibleNot 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.

Each transaction sees one other teacher on call and signs off; together they leave none. SERIALIZABLE fails one of them

Defaults in Other Databases

DatabaseDefault levelNote
PostgreSQLREAD COMMITTEDMVCC snapshots; no dirty reads at any level
MySQL (InnoDB)REPEATABLE READUses gap locks to limit phantoms
SQL ServerREAD COMMITTEDUses locks by default; READ_COMMITTED_SNAPSHOT switches to snapshots
OracleREAD COMMITTEDAlso offers SERIALIZABLE (snapshot-based)

Tabs showing each database's version are coming to this course.

What Each Level Prevents in PostgreSQL

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
SQL▶ Try it
BEGIN ISOLATION LEVEL SERIALIZABLE;
SHOW transaction_isolation;
SELECT title, fee_usd FROM courses WHERE course_id = 4;
COMMIT;
Output
BEGIN
 
 transaction_isolation
-----------------------
 serializable
(1 row)
 
   title    | fee_usd
------------+---------
 SQL Basics |  400.00
(1 row)
 
COMMIT

Alone, 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.

Quick check: Isolation levels

3 questions
1What is PostgreSQL's default isolation level?
2Within one transaction, a row is read twice and returns different values. What is this?
3Which level prevents write skew?

Key Takeaways

You wantUse
The defaultNothing: READ COMMITTED
One snapshot for the whole transactionBEGIN ISOLATION LEVEL REPEATABLE READ
As if transactions ran one at a timeBEGIN ISOLATION LEVEL SERIALIZABLE, plus retries
See the current levelSHOW transaction_isolation;

Was this tutorial helpful?

Lesson 76 of 115