Skip to content
Databasesintermediate

How do database isolation levels work?

Updated Nov 30, 20263 min readBased on official documentation
How do database isolation levels work?

In short

Isolation levels control how much a database transaction can see of other transactions running at the same time. Stricter levels prevent more problems, such as dirty reads and phantom reads, but can slow the system down. PostgreSQL supports all four standard levels, though it implements only three of them internally.

What isolation means

A transaction is a group of database statements that succeed or fail together. Many transactions often run at the same time. Isolation is the rule that decides how much one transaction can see of the changes made by the others while they are still running.

The SQL standard defines four isolation levels: Read Uncommitted, Read Committed, Repeatable Read and Serializable. Each level forbids a certain set of problems. The main problems are a dirty read, where you read data another transaction has written but not yet committed; a nonrepeatable read, where re-reading a row gives a different value because someone else changed and committed it; a phantom read, where re-running a query returns a different set of rows; and a serialization anomaly, where the final result could not have come from running the transactions one after another.

The four levels

The strictest level is Serializable. It promises that running transactions at the same time gives the same result as running them one at a time in some order. Because of that promise, none of the problems above can happen at this level.

The other three levels allow some problems. Read Uncommitted allows all of them in the standard. Read Committed prevents dirty reads but allows the rest. Repeatable Read prevents dirty and nonrepeatable reads but still allows serialization anomalies. A table in the documentation lists exactly which problems each level allows, and it is a good idea to check it when you choose a level.

How PostgreSQL handles them

PostgreSQL accepts all four level names, but it only implements three distinct behaviors. Its Read Uncommitted mode works exactly like Read Committed. The reason is that this is the only sensible way to fit the standard levels onto PostgreSQL's multiversion design, where each transaction sees a snapshot of the data.

PostgreSQL's Repeatable Read also does not allow phantom reads, which the standard permits at that level. Stronger guarantees than the standard requires are acceptable, so this is allowed.

Read Committed is the default. Inside a Read Committed transaction, each SELECT sees only data committed before that query started. Two SELECT statements in the same transaction can therefore return different results if another transaction commits in between.

A simple example

Suppose you want to move 100 units from one bank account to another. You can wrap both updates in one transaction:

BEGIN; UPDATE accounts SET balance = balance + 100.00 WHERE acctnum = 12345; UPDATE accounts SET balance = balance - 100.00 WHERE acctnum = 7534; COMMIT;

In Read Committed mode this works well, because each statement changes a row it has chosen by a fixed key. Complex search conditions are a different story. The documentation shows that a DELETE with a WHERE condition can skip a row if another transaction changes the value while the delete is running. For such cases, a stricter level or explicit locking may be safer.

To set the level for a transaction, use the SET TRANSACTION command right after BEGIN, for example SET TRANSACTION ISOLATION LEVEL SERIALIZABLE.

Common mistakes

A common mistake is assuming that a transaction sees one frozen view of the database. In Read Committed, every statement gets a fresh snapshot, so the view can change between statements. Another mistake is using Serializable everywhere without thinking. Stricter levels can cause more waiting or failed transactions that the application must retry.

Sequences are also special. Changes to a sequence, which is the counter behind a serial column, are visible to other transactions at once and are not rolled back if the transaction aborts. So a gap in ID numbers is normal and does not mean data was lost.

Choosing a level

Start with the default Read Committed for most simple work. Move to Repeatable Read or Serializable when a transaction reads data and then makes decisions based on it, such as checking a balance before withdrawing money. Stricter levels protect correctness but cost some speed, so pick the lowest level that prevents the problems your code cannot tolerate.

FAQ

What is the default isolation level in PostgreSQL?

The default is Read Committed. Each statement sees only data that was committed before the statement started.

Does PostgreSQL really have a Read Uncommitted level?

You can request it, but PostgreSQL treats it the same as Read Committed. Uncommitted data is never visible to other transactions.

What is a phantom read?

A phantom read happens when running the same query again returns a different set of rows because another transaction committed new matching rows in between.

How do I change the isolation level of a transaction?

Use the SET TRANSACTION command at the start of the transaction, before running any queries in it.

Sources

This page is based on the official documentation below. Read it for full detail.

Isolation levels — official documentation ↗

Isolation levels in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.