Two customers read the same booking availability while another request changes it. A report continues running while orders are updated. PostgreSQL needs rules for what each operation can see and how competing changes are resolved. Those rules affect application correctness as well as performance.
Multiversion concurrency control, or MVCC, is central to that behavior. Understanding snapshots, isolation levels, and row-version cleanup helps a team explain surprising reads, design transaction retries, and investigate growing tables. It also makes clear why avoiding unnecessary blocking does not remove the need for constraints and careful transaction boundaries.
The key ideas
- A snapshot defines visibility; the isolation level determines when snapshots change.
- Concurrent writes still need conflict handling and business invariants.
- Keep transactions bounded and monitor vacuum as part of normal operation.
Think in visible row versions
PostgreSQL uses a multiversion model so a statement can see a consistent snapshot while other transactions change data. Updates and deletes can leave older row versions that remain needed by existing snapshots. An ordinary read can therefore see an earlier committed value without forcing the updating transaction to wait for that reader.
Consider an order whose status changes from pending to paid. A statement that already has its snapshot may still see pending, while a later operation can see paid after the update commits. That is a visibility rule, not proof of a stale application cache. When investigating an unexpected value, record the transaction boundaries, isolation level, and commit order alongside the query.
Reference: PostgreSQL: introduction to MVCC; PostgreSQL: routine vacuuming
Distinguish statement snapshots from transaction snapshots
PostgreSQL defaults to Read Committed: each statement takes a new snapshot. Repeatable Read keeps the snapshot established by the first non-transaction-control statement. Both levels show the transaction's own changes.
Try two sessions: A begins and reads a pending order; B updates it to paid and commits; A reads again. Compare the outcomes below using a fresh transaction and reset data for each isolation level. Include this exercise in your team's transaction review so the expected visibility is explicit.
| Event | Session A: Read Committed | Session A: Repeatable Read |
|---|---|---|
| A first reads | pending | pending |
| B commits paid; A reads again | paid | pending |
| A commits; a new transaction reads | paid | paid |
Reference: PostgreSQL: transaction isolation levels
Protect business rules when writers compete
A stable snapshot does not automatically prevent every business anomaly. If two requests both check that one slot is available and then independently insert a booking, the read alone does not enforce the booking rule. Express invariants using suitable constraints and transaction logic, or serialize access to the resource where that design is appropriate. Test the actual conflicting operations.
Serializable isolation can reject a transaction with a serialization failure to prevent an outcome inconsistent with serial execution. Retry the entire transaction when that happens. Keep retries bounded and coordinate external effects, such as payments, so repeating SQL work does not blindly repeat a charge.
Reference: PostgreSQL: transaction isolation levels
Separate snapshot visibility from lock waiting
MVCC does not mean PostgreSQL has no locks. Conflicting row updates can wait, SELECT FOR UPDATE deliberately takes row locks, and table locks used by some schema operations can block reads or writes. Investigate what a session is waiting for before describing every slow transaction as an isolation problem.
For a balance adjustment or booking change, decide what must be protected and acquire required locks in a consistent order where feasible. Include deadlock handling and failure cleanup in the design. Avoid holding a transaction open while waiting for a customer action or a slow external API. A transaction with little CPU activity can still retain locks and disrupt other requests.
Reference: PostgreSQL: explicit locking and deadlocks
Treat vacuum as the maintenance partner of MVCC
Older row versions cannot be removed while still needed for visibility. Vacuum makes reusable space available when versions are no longer needed, and autovacuum performs routine maintenance. Long-running transactions and retained snapshots can delay cleanup. Diagnose the cause of retained versions before assuming the answer is to vacuum more aggressively.
Standard VACUUM generally makes space reusable within the table rather than returning all that space to the operating system. VACUUM FULL rewrites the table and requires an exclusive lock, so it is a separate operational decision. Keep autovacuum enabled and monitor its progress, dead-row estimates, transaction age, and workload behavior. Maintenance also protects against transaction ID wraparound; it deserves routine attention.
Reference: PostgreSQL: routine vacuuming
Make transaction behavior part of the operating review
For each important write workflow, document the isolation level, invariant, expected conflicts, and retry policy. Review connection cleanup and idle-in-transaction sessions, especially around exceptions and worker failures. For long reports, consider how snapshot duration interacts with maintenance and whether the business can accept a different reporting arrangement.
Validate correctness with deliberately overlapping transactions, not only single-user examples. Observe transaction duration, lock waits, aborted transactions, and table growth after release. MVCC gives PostgreSQL a useful foundation for concurrent work; clear application boundaries and regular maintenance make that foundation dependable in a growing product.
Sources & further reading
The guidance above combines our engineering recommendations with the official references below. Service features and platform requirements can change; check the current documentation when implementing.