PostgreSQL transactions and isolation levels
Short answer
Wrap related statements in BEGIN; ... COMMIT; so they succeed or fail as one all-or-nothing unit. Use ROLLBACK to discard them. PostgreSQL's default isolation level is Read Committed. [S1][S2]
Example
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE name = 'a';
UPDATE accounts SET balance = balance + 100 WHERE name = 'b';
COMMIT;
Isolation in brief
Levels differ in which anomalies they prevent, such as dirty reads, non-repeatable reads and phantom reads. [S2] Raise the level with SET TRANSACTION ISOLATION LEVEL ... only when you need it, and be ready to retry on serialization failures.
Claims
- id: C1 claim: A transaction bundles steps into one all-or-nothing operation. source: S1 evidence: "it bundles multiple steps into a single, all-or-nothing operation"
- id: C2 claim: Transactions are set up with BEGIN and COMMIT. source: S1 evidence: "surrounding the SQL commands of the transaction with BEGIN and COMMIT commands"
- id: C3 claim: Read Committed is the default isolation level. source: S2 evidence: "Read Committed is the default isolation level in PostgreSQL."
- id: C4 claim: A dirty read reads uncommitted data from another transaction. source: S2 evidence: "A transaction reads data written by a concurrent uncommitted transaction."
Sources
- [S1] PostgreSQL: Documentation: 18: 3.4. Transactions. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/tutorial-transactions.html . License: PostgreSQL License. Accessed 2026-11-04.
- [S2] PostgreSQL: Documentation: 18: 13.2. Transaction Isolation. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/transaction-iso.html . License: PostgreSQL License. Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: 3.4. Transactions, PostgreSQL License; PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: 13.2. Transaction Isolation, PostgreSQL License.
Cite this
iinventedlight. 2026. "PostgreSQL transactions and isolation levels." iinventedlight Library, iil:note:428gfz9t, version of 2026-11-04. https://iinventedlight.com/library/notes/postgres-transactions/. License: CC-BY-4.0.
@misc{iil_note_428gfz9t,
author = {{iinventedlight}},
title = {{PostgreSQL transactions and isolation levels}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/postgres-transactions/},
note = {Permanent ID iil:note:428gfz9t. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/postgres-transactions.md.