Concept

Transaction

Group of SQL statements treated as a single atomic unit: either every change commits or every change rolls back.

A transaction is a unit of work bracketed by BEGIN and COMMIT (or ROLLBACK). PostgreSQL guarantees ACID properties for the changes inside it: atomicity, consistency, isolation, and durability. Every individual statement runs in its own implicit transaction unless one is already open.

Inside a transaction, your changes are visible to your own queries but not to other sessions until commit. If any statement raises an error, the entire transaction is aborted — subsequent statements fail until you ROLLBACK. Savepoints (SAVEPOINT, ROLLBACK TO) let you partially undo while keeping the surrounding transaction alive.

Hold transactions for as little time as possible. A long-running transaction prevents VACUUM from reclaiming dead tuples from rows committed after it began, which leads to bloat across the database.