Skip to content
UniCircle
MediumConceptualTransactions

Transactions and isolation levels in PostgreSQL

Question PG-TXN-006

How do you use transactions in PostgreSQL? What isolation levels does it offer and what anomalies does each prevent?

Show the explanation

A transaction groups statements so they succeed or fail together (atomicity). Use BEGIN … COMMIT, or ROLLBACK to undo; SAVEPOINT lets you roll back part of one.

SQL
begin;
update accounts set balance = balance - 500 where id = 1;
update accounts set balance = balance + 500 where id = 2;
commit;           -- both changes become visible together, or neither does

PostgreSQL uses MVCC: readers see a snapshot and do not block writers, and writers do not block readers.

LevelPreventsNotes
Read Committed (default)dirty readseach statement sees data committed before it started
Repeatable Read+ non-repeatable reads, phantomsthe whole transaction sees one snapshot; may fail with a serialization error on conflicting updates
Serializable+ all anomalies (e.g. write skew)behaves as if transactions ran one at a time; the application must retry on serialization failures

Read Uncommitted is accepted but behaves as Read Committed — PostgreSQL never shows uncommitted data. For a "read then update" pattern under Read Committed, lock the row with SELECT … FOR UPDATE or do the change in one statement (set balance = balance - 500).

Original UniCircle question. Last reviewed 22 Oct 2026.