All glossary terms

Transaction isolation levels

What are the transaction isolation levels in PostgreSQL?

The isolation level decides how much a transaction sees of concurrent transactions' changes. PostgreSQL defaults to Read Committed and also offers Repeatable Read and Serializable.

The SQL standard defines four isolation levels according to which anomalies they allow: dirty reads, nonrepeatable reads, phantom reads and serialization anomalies. PostgreSQL accepts all four level names, but Read Uncommitted behaves like Read Committed, so in practice there are three: Read Committed, Repeatable Read and Serializable.

Read Committed, the default, gives each statement a fresh snapshot, so two queries in the same transaction can see different data. Repeatable Read uses one snapshot for the whole transaction. Serializable adds checks so that concurrent transactions behave as if they had run one at a time. At the two stricter levels a transaction can fail with a serialization error, and the application should retry it.

Example

The example shows the server's default level, then starts a Repeatable Read transaction and confirms it with SHOW transaction_isolation. Every read inside that transaction uses the same snapshot.

CREATE TABLE stock (sku text PRIMARY KEY, qty int);
INSERT INTO stock VALUES ('A-1', 5);

SHOW default_transaction_isolation;

BEGIN ISOLATION LEVEL REPEATABLE READ;
SHOW transaction_isolation;
SELECT qty FROM stock WHERE sku = 'A-1';
COMMIT;

Plain SQL for stock PostgreSQL 16 or 17, no extensions needed. Run it in an empty database you can throw away.

Sources

Official PostgreSQL documentation (in English).

Related terms