One transaction, two different snapshots
MVCC, Read Committed and consistent reports
Atomicity does not freeze concurrent data. Read Committed and Repeatable Read choose snapshots at different boundaries.
An API builds a report with two queries: count the orders, then sum their amounts. Wrapping both in BEGIN and COMMIT does not automatically make them observe one database state.
Transactional atomicity concerns changes saved together. Snapshot visibility concerns which committed versions a reader sees.
MVCC does not copy the entire database per reader
PostgreSQL's Multiversion Concurrency Control lets reads select visible data versions according to a snapshot. A reader can see an earlier version while another transaction updates it.
This supports concurrency, not the absence of locks. DDL and explicitly locking reads need separate consideration.
Read Committed takes a new snapshot per command
An ordinary SELECT at PostgreSQL's default isolation level sees other transactions' data committed before the command begins, plus its own transaction's earlier changes.
Suppose the initial orders are 40 and 60:
| Step | Reader A | Writer B | |---|---|---| | 1 | BEGIN at Read Committed | | | 2 | COUNT returns 2 | | | 3 | | Inserts an order for 50; COMMIT | | 4 | SUM returns 150 | | | 5 | COMMIT | |
“Two orders totaling 150” combines two valid snapshots into one inconsistent report. It is not a dirty read: the new order committed before the second SELECT.
Repeatable Read changes that boundary
In PostgreSQL, Repeatable Read establishes its snapshot at the first non-transaction-control statement, not necessarily BEGIN itself. In this example the first query fixes the view; the second sees two orders totaling 100.
Use your actual schema. Both queries must share the same connection and transaction. Naming a TypeScript function generateReport does not make two independent pool connections share a transaction.
READ ONLY states access intent; it does not itself select a fixed snapshot. Read-only Read Committed remains possible.
A single query may be simpler
For this ordinary query, both aggregates observe the same statement snapshot. Check whether one query expresses the report before holding a transaction across several application calls.
A consistent snapshot can still be stale
Repeatable Read alone does not enforce every business invariant under concurrent writes. Serializable is stronger and may fail a transaction with serialization_failure. Retry the entire transaction, while treating external side effects separately as in idempotency.
BEGIN is neither an automatic lock on previously read rows nor SELECT FOR UPDATE.
Check yourself
A begins Repeatable Read. B inserts and commits. Only then does A execute its first relevant statement. Is B's order visible? Yes, assuming no earlier statement established A's snapshot.
For a report, specify when the snapshot is chosen. For inventory or another business invariant, additionally specify how competing writes are coordinated.