Core Concepts

Isolation Levels and MVCC

Trace what overlapping transactions can read and why a stable snapshot can still allow an unsafe write.

Free~17 min

Two reads in one transaction can see different values if another transaction commits a change between them. The isolation level determines which changes are visible and how the database handles conflicting work. A report may need the same view throughout. Enforcing a rule across several rows can require a stronger guarantee.

Decide whether a second read can change

A snapshot is a read view of the database. The operator, reader R, queries model pool P3 twice in one transaction. The first query sees 1 out of 2 slots used. Between the queries, the gateway, writer W, fills the last slot and commits. The isolation level decides whether R sees that commit on the second read.

At PostgreSQL's default Read Committed level, each ordinary SELECT starts with its own snapshot. R first reads version V1, with one slot used. The second query starts after W commits, so it reads V2, with two slots used. R also sees the earlier writes it made in its own transaction. It cannot see W's write before W commits. Seeing another transaction's uncommitted write would be a dirty read. PostgreSQL treats Read Uncommitted as Read Committed and prevents dirty reads at both levels. PostgreSQL Read Committed

At PostgreSQL's Repeatable Read level, R keeps the snapshot it took at its first query. More precisely, the snapshot starts at the first statement that does database work, such as a read or write. BEGIN alone does not take it. R's second query in this example still sees V1. A reader whose snapshot starts after W commits can see V2. PostgreSQL Repeatable Read

A changed value on a second read of one row is a nonrepeatable read. A changed set of rows matching a repeated filter is a phantom read. At Read Committed, a second query for pools with free slots might omit P3 because W filled it. PostgreSQL Repeatable Read prevents both changes from other transactions within the same snapshot. Check the database's guarantee when moving this design to a different engine. PostgreSQL isolation guarantees

Keep the two row versions

PostgreSQL uses multiversion concurrency control (MVCC) to give readers those views. W's update creates a new version of the P3 row. The old version remains available to transactions with older snapshots. After W commits, R's Repeatable Read snapshot can still read V1 while a new query can read V2. An ordinary reader and writer can overlap this way without the reader locking the row against updates. PostgreSQL MVCC introduction

PostgreSQL can reclaim space from row versions no active snapshot still needs. Its cleanup process, vacuum, does that work. A long-running snapshot on a busy table can keep old versions from being reclaimed while newer updates create more of them. Keep the read and processing inside the transaction only as long as they need the same database view. PostgreSQL routine vacuuming

A stable view can still lead to unsafe writes

Imagine a maintenance screen for pool P3. It has two worker records, A and B. The application allows an operator to disable a worker only if another worker will remain enabled. Each operator runs a transaction that checks the other worker, then disables its own target.

Transaction A reads that worker B is enabled. Transaction B reads that worker A is enabled. They read before either commits. Each updates a different row, so at PostgreSQL Repeatable Read both can commit. The final state has both workers disabled. This is write skew. Each write made sense in its own snapshot, but the pair breaks a rule spanning multiple rows. PostgreSQL on snapshot isolation and business rules

At Serializable isolation, the successful transactions must have an effect that could result from running them one at a time. In the example, either order would make the second transaction see that its target is the last enabled worker. PostgreSQL can reject one operation with a serialization failure. If B retries after A commits, B can now see that A is disabled and leave the remaining worker enabled. This protection depends on the relevant transactions running at Serializable and performing the checks that preserve the rule. PostgreSQL Serializable

Restart decisions after a conflict

At PostgreSQL Repeatable Read, a transaction can successfully read V1 and still fail if it later tries to update or lock the P3 row after W commits V2. PostgreSQL reports a serialization failure in this case. At Read Committed, a simple update or locking read can wait for the other transaction and then recheck its condition on the updated row. PostgreSQL isolation and conflicting writes

When PostgreSQL reports a serialization failure, retry the complete transaction. Reread the rows and redo the decisions that picked the writes. Repeating the last update with an old decision would carry the conflict into the new attempt. Keep external work scheduled after commit or make it safe to repeat so a retry does not repeat an irreversible effect. PostgreSQL serialization failure handling

Lock the row every decision reads

For the ChatGPT walkthrough's admission check, we know which row holds the pool's used count. Every caller locks that capacity row before checking and reserving a slot. The lock makes competing admissions take turns at Read Committed. A stable view is useful when we need a report whose reads fit together. A row lock is useful when we know the exact record that competing writers must consult before deciding. Serializable can protect a rule across rows when those decisions use the relevant reads and the application can retry conflicts.

See also

  • PostgreSQL shows how snapshots and vacuum fit into the rest of its storage engine.
  • Consistency Models covers what clients see when data lives on multiple replicas.
  • Idempotency covers retries at an API boundary.
  • ACID Transactions follows the whole commit and rollback boundary for a turn and its reservations.
  • Locking Strategies compares protecting a decision first with checking it when writing.

Used in

  • ChatGPT checks shared capacity under a lock so two admissions cannot claim the last slot based on the same earlier read.
  • URL Shortener lets the database settle competing alias writes even when both requests initially saw the code as available.

Done reading? Mark it so it sticks in your dashboard.

Discussion