Transactions: one boundary, several isolation stories
Follow one transaction from BEGIN to COMMIT, then see what concurrent reads may observe at named isolation levels. Transactions and indexes · Databases.
Mira moves value from one named account to another. A crash after the debit would be a very bad place to discover that the two updates were never one unit. Transactions give the work a boundary; isolation decides what nearby sessions may see while it runs.
Put the boundary around the whole change
Start with two small rows: Mira's travel fund and Nila's study fund. The amounts are symbolic values in a finite teaching schedule. The first scene uses a nonlocking PostgreSQL READ COMMITTED read under multiversion concurrency control (MVCC). The observer reads committed row versions, while the writer can see its own uncommitted changes. The second session only observes; it never alters either row. Other engines and locking reads need their own rules.
One transfer, one transaction boundary
Read the steps as text
- BEGIN opens one transaction on the client connection; the observer still has the committed starting view.
- The debit is staged inside the open unit. Under the stated nonlocking PostgreSQL READ COMMITTED/MVCC read, the observer still has the prior committed view.
- The credit joins the same unit. Both rows are now staged, but the observer still waits for the unit's commit boundary.
- COMMIT closes the unit. The two staged changes become one committed result eligible for later reads.
- The boundary explains atomicity; the validity rule still has to be encoded, and durability follows a successful commit under the engine's settings.
BEGIN opens a multi-statement unit on one connection. The debit and credit are staged inside that unit, so another session does not get a committed half-transfer. COMMIT makes the unit's result eligible for later reads and completes the transaction. With autocommit, each statement is its own transaction when no explicit block surrounds it.
That is atomicity: all of this unit commits together or the unit rolls back. Consistency is a separate question: the schema or application must encode a rule such as “both account rows exist and the transfer is allowed,” and the transaction must preserve it. A transaction does not invent a business rule merely by having a name. Durability concerns a successful commit surviving recovery under the engine's durability settings. Isolation concerns what concurrent sessions can observe.
BEGIN;
UPDATE accounts SET state = 'debit staged' WHERE name = 'Mira';
UPDATE accounts SET state = 'credit staged' WHERE name = 'Nila';
COMMIT;Keep good work while undoing one branch
An open transaction can mark a savepoint before a risky statement. ROLLBACK TO SAVEPOINT discards work after that mark and leaves earlier work and the outer transaction open. It is not a nested committed transaction.
Roll back to a mark, keep the outer unit
Read the steps as text
- The client begins one outer transaction and stages the allowed transfer before marking a risky adjustment.
- SAVEPOINT mark records an internal undo position; the outer transaction remains open.
- The adjustment violates the named rule in this finite schedule, so only the work after mark is rejected.
- ROLLBACK TO mark removes the rejected adjustment while keeping the earlier transfer and the outer unit open.
- The outer transaction can now commit the retained work; a savepoint never published a nested result by itself.
The transfer schedule makes a useful distinction: a full ROLLBACK cancels the whole open unit, while a savepoint rollback removes only the later adjustment. The connection still owns the outer transaction until COMMIT or a full rollback.
Name the read you are worried about
Isolation minima are easiest to remember as phenomena. A dirty read sees another transaction's uncommitted write. A nonrepeatable read repeats a row read after another transaction commits a changed value. A phantom read reruns a predicate and finds that its qualifying row set changed. A serialization anomaly is a successfully committed result that cannot match any one-at-a-time order.
The SQL-standard level names describe minimum protection. READ UNCOMMITTED permits all of those changes; READ COMMITTED blocks dirty reads but can allow nonrepeatable and phantom reads; REPEATABLE READ blocks dirty and nonrepeatable reads while the standard still permits phantoms; SERIALIZABLE promises serial effect for successful commits. The schedule below shows the three read phenomena before we compare real engines.
Three read phenomena, three different surprises
Read the steps as text
- Session A and Session B share named rows. The schedule starts with three possible surprises, not with a vendor-specific promise.
- A has written but not committed. If B reads that value, B has seen work that may still disappear.
- B now commits a changed value between A's two ordinary reads. A's second read no longer repeats the first row value.
- A reruns a predicate and finds a new qualifying row committed by B. The row set changed even though no earlier row was edited.
- Dirty, nonrepeatable and phantom reads describe different observations; isolation names which minimum protections apply.
The table is a contract floor, not a promise that every engine implements only that floor. PostgreSQL maps READ UNCOMMITTED to READ COMMITTED, and its REPEATABLE READ also prevents phantoms. InnoDB defaults to REPEATABLE READ, but its ordinary consistent reads and locking reads do not use the same view. Read the query kind and the named engine before predicting a result.
Snapshot timing belongs to the engine
PostgreSQL's default READ COMMITTED takes a snapshot at the start of each ordinary SELECT, so a later statement in one transaction can see a concurrent commit that an earlier statement missed. PostgreSQL REPEATABLE READ keeps the transaction snapshot from its first relevant statement and is stronger than the SQL minimum for phantoms.
With InnoDB's default REPEATABLE READ and plain START TRANSACTION (without WITH CONSISTENT SNAPSHOT), the first consistent read establishes the ordinary read snapshot. InnoDB READ COMMITTED uses a fresh snapshot for each consistent read. A transaction's own writes are visible to its own session; “uncommitted work is invisible” describes other sessions' ordinary reads.
When ordinary reads set the view
Read the steps as text
- BEGIN creates a transaction context, but the read view depends on the engine and the kind of read that follows.
- With plain START TRANSACTION and no WITH CONSISTENT SNAPSHOT, the first consistent read sets the InnoDB repeatable-read view.
- A later ordinary read can refresh under READ COMMITTED or keep the earlier view under repeatable read, depending on the named engine row.
- Snapshot timing is engine-specific: name the engine, isolation level and ordinary or locking read before you generalize.
Those are named examples, not a universal BEGIN rule. A read that already has a snapshot can keep its earlier view after commit; a fresh eligible read can observe the committed change. Locking reads and data-changing statements follow engine-specific rules that this opener leaves for a deeper lesson.
Retry the whole serializable attempt
Serializable execution can reject a concurrent attempt. In PostgreSQL, a serialization failure means the transaction did not commit; the application must end that attempt and retry the whole transaction from a fresh read. Do not reuse decisions from a transaction that later aborted. InnoDB deadlocks also need whole-transaction retry, while a default lock-wait timeout has a different statement-level rollback scope.
Serializable work can fail and retry
Read the steps as text
- Ada and Bo each read the same roster rule: at least one doctor must remain on call. Both begin under the named SERIALIZABLE model.
- Each attempt makes its own decision from the shared snapshot. Ada's write reaches the commit check first; Bo's conflicting attempt cannot also commit as if it ran alone.
- Ada commits one departure; the database rejects Bo's stale concurrent attempt with a serialization failure, so Bo's first transaction is over without a commit.
- Bo starts a fresh attempt and rereads the roster. The retry sees Ada off call, so it keeps Bo on call and preserves the named rule.
- Serializable success has a serial effect; a failed attempt contributes no commit and must be retried from fresh reads.
The final scenario is a small write-skew witness: two named doctors both see that the other is on call, then each tries to leave. The rows and event order are hand-authored assumptions under the named SERIALIZABLE model. They illustrate why a retry is a new read-and-check attempt; they are not a database simulator.
Recap
- Atomicity groups changes; consistency depends on encoded constraints and invariants; durability follows a successful commit.
- Isolation names minimum protections, while PostgreSQL and InnoDB document stronger or different behavior for particular read types.
- Snapshot timing and serialization failure are engine facts: retry the whole failed serializable transaction from fresh reads.
A connection is part of the boundary
The transaction lives on a database session or connection. Code using a pool must keep the same connection from BEGIN through COMMIT or ROLLBACK; returning it early does not move the transaction to the next checkout. PostgreSQL can leave a failed transaction in an aborted state until rollback or savepoint rollback. InnoDB normally rolls back a deadlocked transaction, while its default lock-wait timeout rolls back only the waiting statement unless configuration changes that scope.
If you want a syntax refresher, SQL basics is optional reading. The hard prerequisite here is keys and constraints, because the consistency discussion depends on rules that the schema actually declares.
For a DSA connection, Queue Fundamentals gives you a compact way to trace ordered actions; keep queue operations separate from transaction isolation.