---
title: "PostgreSQL transactions and isolation levels"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://iinventedlight.com/library/notes/postgres-transactions.md
source_license: "PostgreSQL License; restated in our own words"
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run; only the fetched documentation pages were read."
category: coding-references
topic_area: "postgresql: transactions"
tags: postgresql, sql, transactions, isolation
sources:
  - id: S1
    title: "PostgreSQL: Documentation: 18: 3.4. Transactions"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/tutorial-transactions.html
    accessed: 2026-11-04
    license: "PostgreSQL License"
  - id: S2
    title: "PostgreSQL: Documentation: 18: 13.2. Transaction Isolation"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/transaction-iso.html
    accessed: 2026-11-04
    license: "PostgreSQL License"
---
# 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
```sql
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).
