MVCC and Tuple Visibility

Topics Covered

Row Versions Instead of Locks

The problem MVCC solves

Every update creates a new version

Readers do not block writers

Writers still block writers

Version chains

Comparing with undo based designs

What a row version carries

Seeing versions for yourself

Why this matters operationally

MVCC and DDL

Sequences sit outside MVCC

PostgreSQL lets readers and writers work on the same rows at the same time without blocking each other. A long report can scan a table while thousands of updates change it, and the report sees one consistent picture of the data as it was when it started. The mechanism is multiversion concurrency control, MVCC: instead of changing rows in place and locking them against readers, Postgres keeps multiple versions of each row and lets every transaction decide which version it is allowed to see. This lesson explains how those versions are labeled, how a transaction decides what it sees, and what the design costs. Almost every operational behavior of Postgres, from vacuum to bloat to wraparound, follows from the choices described here.

The problem MVCC solves

Without multiversioning, a database that wants readers to see consistent data must make them wait for writers. A reader scanning a table would take shared locks, and a writer would need exclusive locks, so a long report would block updates for its whole duration, and a long update would block every read. Early databases worked this way and suffered exactly these stalls. The alternative, letting readers see changes as they happen, gives inconsistent results, such as a sum over accounts that counts a transfer twice or not at all. MVCC avoids both by letting the reader see an older version while the writer creates a new one.

Every update creates a new version

In Postgres, an UPDATE never modifies a row in place. It writes a complete new version of the row, a new tuple, and marks the old tuple as superseded by setting its xmax to the updating transaction. A DELETE writes no new tuple but sets xmax on the existing one. An INSERT writes a tuple whose xmin is the inserting transaction. The old versions stay in the table until no transaction could possibly need them, at which point vacuum removes them. At any moment, a single logical row may therefore have several physical versions in the heap, each labeled with the transactions that created and deleted it.

An update stamps the old tuple with its xid and writes a new tuple. A reader that started earlier keeps reading the old version.

Readers do not block writers

Because each transaction reads the versions that were current for it, a reader never needs to lock rows against writers, and a writer never needs to wait for readers to finish. A plain SELECT takes no row locks at all. The only lock it holds is a light relation level lock that conflicts with operations such as dropping the table. This is the central promise of MVCC and the reason long analytical queries can run on a busy transactional database without stopping it, as long as the cost of keeping old versions around is understood and managed.

Writers still block writers

MVCC does not remove all waiting. Two transactions that try to update the same row cannot both create the next version, since the row history must remain a single chain. The second updater finds the row already has an xmax from an in progress transaction and waits for that transaction to finish. If the first commits, the second, in Read Committed, re-reads the newest version and applies its update to that, while in stricter isolation levels it fails with a serialization error. Writers also block on explicit row locks taken by SELECT FOR UPDATE. Lock waits are covered in detail later in the course.

A second writer to the same row waits for the first. After the first commits, the second re-reads the newest version in Read Committed.

Version chains

Each old tuple points to its successor through the ctid field in its header. When a transaction follows a row through an update, for example when an index scan finds the old version and needs the current one, it follows these ctid links forward. Index entries point to specific tuple versions, not to logical rows, so a normal update also inserts new entries in every index, pointing at the new tuple. The heap only tuple optimization, covered in a later lesson, avoids those index entries when the new version fits on the same page and no indexed column changed.

Comparing with undo based designs

Oracle, MySQL InnoDB and SQL Server use a different approach. They update rows in place and write the previous values to an undo log, and a reader that needs an older version reconstructs it from the undo log. Old versions therefore live outside the table, and the table itself does not bloat. Postgres keeps old versions in the table itself, which makes rollback instant, since nothing needs to be undone, and makes reading old versions cheap, but it means tables accumulate dead tuples that vacuum must remove, and updates write a whole new row rather than a small change. Neither design is free; they put the cost in different places.

What a row version carries

The header fields that matter for visibility are xmin, the transaction that created the version, xmax, the transaction that deleted or locked it or zero, cmin and cmax, command counters within the creating or deleting transaction, sharing a single field, and the infomask bits, which cache commit status and record whether xmax is a lock or a real delete. These are all the data Postgres needs to decide whether a given transaction should see this version. The rest of this lesson builds up the rules from these fields and the transaction state stored elsewhere.

Seeing versions for yourself

The system columns xmin, xmax and ctid can be selected like ordinary columns, which makes MVCC easy to observe. Updating a row and selecting it again shows a new ctid and a new xmin. Running the same query in a second session that started a repeatable read transaction before the update shows the old values. The pageinspect extension goes further, showing every tuple on a page including dead ones, with their infomask flags decoded. Spending a few minutes with these tools is the fastest way to make the abstract rules in this lesson concrete.

Why this matters operationally

The version based design explains a long list of operational behaviors. Tables grow under updates even when the row count is constant, because each update leaves a dead version. Long transactions cause bloat, because their snapshots keep old versions alive. Vacuum exists to remove dead versions and update the maps that make scans fast. Transaction identifiers must eventually be frozen, because they are stored in every tuple and are only 32 bits wide. Each of these gets its own lesson, and each starts from the fact that rows are versions stamped with transaction identifiers.

MVCC and DDL

MVCC covers data, not schema. System catalogs are themselves tables with row versions, but most schema changes take strong locks because the rest of the system caches catalog information and cannot safely see two definitions of a table at once. ALTER TABLE, for example, usually needs an access exclusive lock that waits for every running query on the table, including plain SELECTs, and blocks new ones while it waits. This is the main exception to readers never waiting, and it is why the migrations lesson treats lock timeouts and lock queues as the core of safe schema changes. TRUNCATE is another exception: it is not MVCC safe, so a Repeatable Read transaction that started earlier can see the table as empty.

Sequences sit outside MVCC

Sequence values are not transactional. nextval increments the sequence immediately and the increment is never rolled back, so that concurrent transactions never wait for each other to commit before getting a value. Aborted transactions therefore leave gaps, and values can appear to commit out of order. Applications that need gapless numbering, such as invoice numbers, must use a separate counter row updated under a lock, accepting that it serializes those transactions.

Think in versions

An update is a new tuple plus an xmax on the old one, a delete is just an xmax, and readers choose a version instead of waiting. Writers to the same row still wait. Use the xmin, xmax and ctid system columns and pageinspect to watch it happen.