What is a transaction?
A transaction groups database work into a unit that can commit or roll back. The PostgreSQL 18 transaction tutorial documents this contract. ACID terminology is in Concepts: what is a transaction?.
How to read the demos
The Transcripts below are generated from Scenarios with separate PostgreSQL connections. Their footers identify the executed server version, currently PostgreSQL 18.6. Assertions establish the displayed outcomes for these schedules, not every possible schedule. Manual links identify Documented contracts; marked derivations identify Entailed guarantees. See the methodology for the runner and generation process.
Atomicity, demonstrated
A credits bob, then attempts to debit 150 from alice, whose balance is only 100. The debit violates the table's CHECK constraint. After full ROLLBACK, the Scenario asserts both original balances. B's plain SELECT also asserts that A's uncommitted credit was not visible at READ COMMITTED.
A transfers 150 from alice to bob. Crediting bob works fine…
A> BEGIN ISOLATION LEVEL READ COMMITTED;
BEGIN
A> UPDATE accounts SET balance = balance + 150 WHERE owner = 'bob';
UPDATE 1
B> SELECT balance FROM accounts WHERE owner = 'bob'; -- B can't see A's uncommitted credit
balance
---------
50
(1 row)…but debiting alice violates the CHECK constraint — she only has 100.
A> UPDATE accounts SET balance = balance - 150 WHERE owner = 'alice'; -- check_violation
ERROR: 23514: new row for relation "accounts" violates check constraint "accounts_balance_check"This run uses full ROLLBACK after the CHECK violation. Bob's earlier credit is undone along with the failed transfer.
A> ROLLBACK;
ROLLBACK
B> SELECT owner, balance FROM accounts ORDER BY id;
owner | balance
-------+---------
alice | 100
bob | 50
(2 rows)Verified against PostgreSQL 18.6 · Run it yourself · Scenario source
This statement error leaves the explicit transaction failed. Full ROLLBACK discards the earlier credit; a savepoint established before a statement error can instead permit partial recovery. Error scope depends on whether a transaction block is open and which error occurred, as the next lesson explains.
This atomicity claim concerns transactional table changes. It does not include external effects or sequence counters. The isolation manual's sequence warning documents that sequence changes are visible immediately and are not rolled back.