0%
PostgreSQL Deep Dive: MVCC, Vacuum, Plans, and Replication
Architecture and Storage
MVCC and Vacuum
Indexes and Query Planning
Isolation Levels in Postgres
Isolation levels define what a transaction can see of other transactions that run at the same time. The SQL standard names four levels by the anomalies they forbid, but Postgres implements them with MVCC snapshots, which gives its levels behavior that differs in important ways from the textbook and from other databases. Read Committed is the default and the level most applications run at, so understanding exactly what it guarantees, and what it does not, is the first step. This section covers how Read Committed uses snapshots, how concurrent updates are handled and the anomalies it allows.
A new snapshot per statement
Under Read Committed, each statement takes a fresh snapshot when it starts. It sees every transaction that committed before that moment and none that committed after. Within one statement, the view is consistent: a long SELECT never sees half of another transaction. But two statements in the same transaction can see different data, because a transaction may commit between them. A report that runs a count and then a sum in the same transaction may find that the numbers do not agree, since rows were added in between.
What Read Committed prevents
Read Committed prevents dirty reads: a transaction never sees changes from another transaction that has not committed. In Postgres this is automatic, since uncommitted row versions are invisible to every snapshot. Postgres accepts the level name Read Uncommitted for compatibility but treats it exactly as Read Committed, so dirty reads are impossible at every level. This differs from databases that implement Read Uncommitted with genuine dirty reads for performance reasons.
Concurrent updates under Read Committed
When an UPDATE or DELETE under Read Committed finds a target row that another in progress transaction has modified, it waits for that transaction. If the other transaction rolls back, the waiting statement proceeds with the original row. If it commits, the waiting statement follows the update chain to the newest version, reevaluates its WHERE condition on that version, and, if it still matches, applies its change to the new version. This is called EvalPlanQual, and it is why UPDATE balance equals balance plus 10 never loses an increment under concurrency.
The surprise in EvalPlanQual
The recheck applies only to the row being updated, and the rest of the statement still uses its original snapshot. Consider UPDATE items SET rank equals rank plus 1 WHERE rank greater than or equal to 5, running while another transaction changes ranks. The statement may find rows that no longer match after the recheck and skip them, and it never finds rows that started matching only after its snapshot, so the result can be a mix of the old and new states that neither transaction intended. For single row updates with simple conditions this rarely matters; for complex multi row updates that depend on each other, it can.
Lost updates in application code
The safety of UPDATE balance equals balance plus 10 depends on doing the arithmetic in the database. If the application reads the balance with a SELECT, computes the new value in code and writes it back with UPDATE balance equals the computed value, two concurrent transactions can both read 100, both compute 110 and both write 110, losing one increment. Read Committed does not prevent this lost update, because the UPDATE simply overwrites the row. Using SELECT FOR UPDATE for the read, or putting the arithmetic in the UPDATE, closes the gap.
Nonrepeatable reads and phantoms
Because each statement sees a new snapshot, a row read twice in the same transaction may have changed between the reads, a nonrepeatable read, and a query run twice with the same condition may return new rows the second time, a phantom. These are allowed by the standard at Read Committed. Code that reads a value, makes a decision and then reads related data assuming nothing changed is exposed to them, and the fix is either locking the rows read or running at a stronger isolation level.
Why Read Committed is the default
Read Committed never raises serialization errors, so applications need no retry logic for ordinary conflicts, and it does not hold old snapshots for the whole transaction, which keeps vacuum effective for long transactions with many short statements. For the many workloads that consist of short transactions with single statement reads and atomic updates, it provides the right guarantees at the lowest cost, which is why it is the default and the choice of most applications.
Statement level consistency in functions
A statement that calls functions sees one snapshot for the outer statement, but functions written in PL/pgSQL that run their own queries take a new snapshot for each of those queries when declared VOLATILE, the default. A volatile function called once per row may therefore see different data for different rows of the same outer statement. Functions declared STABLE or IMMUTABLE use the snapshot of the calling statement, which gives consistent results and also lets the planner optimize calls. Declaring function volatility correctly matters for both correctness and performance.
Read Committed and long reports
A long reporting transaction made of many queries under Read Committed sees a moving target: totals from one query may not match details from the next. If consistency across queries matters, the report should run under Repeatable Read, which takes one snapshot for the whole transaction, or as a single query. Running on a replica does not change this, since Read Committed semantics apply there too, with snapshots taken from the replayed state.
Choosing the default per role or transaction
The default isolation level is set by default_transaction_isolation, which can be configured globally, per database, per role or per session. Individual transactions choose with BEGIN ISOLATION LEVEL REPEATABLE READ or SET TRANSACTION. A common setup keeps Read Committed as the default and runs specific operations, such as reports or critical balance checks, at stronger levels explicitly. Setting a stronger default for a role used by a reporting tool is a simple way to give all its queries consistent snapshots.
Snapshots and the transaction id
A snapshot records the range of transaction ids that had started, the list of those still in progress, and the next id to be assigned. A row version is visible if its creating transaction committed before the snapshot and its deleting transaction, if any, did not. Under Read Committed a new snapshot is built for every statement, which costs a little CPU each time and is one reason very high rates of tiny statements benefit from fewer, larger statements.
Read Committed and SELECT FOR UPDATE
SELECT FOR UPDATE under Read Committed uses the same recheck logic as UPDATE. If a selected row was changed by a concurrent transaction that committed, the locking read returns the newest version if it still matches the condition, and skips it otherwise. This means a FOR UPDATE query can return a row whose values differ from what a plain SELECT in the same statement snapshot would show, which is exactly what read modify write code needs: the locked value is the current one, not a stale one.
Isolation inside a single statement
Even within one statement, Read Committed provides a consistent snapshot for reads, so a join or aggregate never mixes states. Writes made by the statement itself are not visible to its own scans, which prevents problems such as an UPDATE repeatedly finding the rows it just created. Common table expressions that modify data run concurrently with the main query on the same snapshot, so the main query does not see their changes, a frequent surprise when chaining data modifying CTEs.
Autocommit statements
A statement run outside an explicit transaction is its own transaction, so under autocommit Read Committed and Repeatable Read behave the same for it. The differences appear only in multi statement transactions.
Read Committed gives each statement a fresh snapshot, prevents dirty reads, rechecks updated rows against their newest version, and allows nonrepeatable reads, phantoms and lost updates in read then write code. Do arithmetic in UPDATE statements or lock rows you read before writing.