7.5 The Idea of MVCC: Showing Readers an Earlier Version

The limitation of the lock approach was that "reads block writes". The approach that removes this limitation is MVCC (Multi-Version Concurrency Control).

The principle is this. If a transaction is modifying a row, a transaction that tries to read that row is shown the value from before the modification. The writing transaction creates a new value and the reading transaction sees the old one, so neither has to wait for the other. To make this possible, when the database modifies a row it does not erase the old value but creates a new version and keeps it together with the old one. Multiple versions of one row exist at the same time. In Section 1.4 we saw PostgreSQL appending a new row on UPDATE, and that was exactly the act of creating a new version.

So which of the many versions should a reading transaction see? The criterion is the snapshot. When a transaction (or statement) starts, the database takes "what is committed at this moment" as the reference. Each version carries the number of the transaction that created it, so the reader picks "the most recent version among those created by transactions committed as of my snapshot". Versions created by transactions that started after my snapshot, or by transactions not yet committed, are treated as nonexistent.

Snapshots and Visibility Checks

What the database copies when it creates a snapshot is not data. A snapshot is a small record made of a few transaction numbers. In Section 4.1 we said every transaction is assigned a number. At the moment a snapshot is created, the database records two things: the largest transaction number issued so far, and the list of transaction numbers still in progress at that moment.

Each version of a row carries two numbers: the number of the transaction that created that version, and the number of the transaction that deleted it (or replaced it with a new version). In PostgreSQL these are called xmin and xmax. Whether a version is visible in my snapshot is determined by the following rules.

  • Does the creating transaction appear committed in my snapshot? It is visible only if its number is smaller than the snapshot's maximum number, it is not in the in-progress list, and it is recorded as committed in the status table.
  • If there is a deleting transaction, does it also appear committed in my snapshot? If it does, this version has already been deleted and is not visible.

Let us look at the transfer example. When T1 (number 100) changes Hong Gildong's balance from 500,000 to 400,000, "deleting transaction = 100" is written on the 500,000 version, and "creating transaction = 100" is written on the 400,000 version.

VersionCreating transactionDeleting transactionQuery started while T1 is in progress (in-progress list includes 100)Query started after T1 commits
500,00073100Visible. 100 has not committed yet, so it is regarded as not deletedNot visible
400,000100—Not visible. 100 is in progressVisible

Thanks to these rules, a query can see data that fits its own snapshot by merely comparing numbers, without taking any locks. And since the status table must be consulted to judge "appears committed", the transaction status records seen in Section 4.1 are a prerequisite of MVCC. InnoDB and Oracle use the same principle, with different terminology. InnoDB keeps the number of the transaction that last modified each row and follows the Undo records to find the version matching its own snapshot (read view), while Oracle compares the SCN recorded in the block with the SCN at which its own query started.

Figure 7-5. MVCC snapshots

flowchart LR
  subgraph ROW ["Versions of Hong Gildong's row"]
    V1["Version 1: 500K<br/>xmin 73, xmax 100"]
    V2["Version 2: 400K<br/>xmin 100, no xmax"]
    V1 -->|"UPDATE by T1 (number 100)"| V2
  end
  Q1["Query A<br/>started before T1 committed<br/>in-progress list includes 100"]
  Q2["Query B<br/>started after T1 committed<br/>100 is committed"]
  Q1 -->|"reads 500K"| V1
  Q2 -->|"reads 400K"| V2

The middle box shows the two versions that coexist for Hong Gildong's row. Version 1 (500K) was created by transaction 73, and when T1 (100) did the UPDATE, 100 was written into its xmax. Version 2 (400K) was created by T1, so its xmin is 100.

  1. Query A started while T1 was still in progress, so 100 is in the snapshot's in-progress list. Version 2 is not visible because its creating transaction (100) does not appear committed. Version 1 is visible because its deleting transaction (100) does not appear committed, so it is treated as not yet deleted. Query A reads 500K.
  2. Query B started after T1 committed, so 100 is a committed transaction. Version 1 is not visible because it was deleted by a committed transaction, and Version 2 is visible because it was created by a committed transaction. Query B reads 400K.

Even when reading the same row at the same moment, the two queries choose different versions according to their own snapshots, and neither waits for T1's lock. Even after T1 commits, Query A keeps seeing 500K as long as it uses its original snapshot.

Let us revisit the total query T2 of Section 7.1. When T2 started, T1 was in progress, so T1's changes are not visible in T2's snapshot. T2 reads the 500K version from Hong Gildong's row and the 200K version from Kim Cheolsu's row. It does not matter if T1 commits in between. T2 sees data based on its own snapshot until the end. The total is correct, and T1 never waited for an instant. This is the sentence that summarizes MVCC: "reads do not block writes, and writes do not block reads".

Write-Write Conflicts

One thing, however, does not change. Writes still conflict with writes. If T1 and T3 try to modify the same row concurrently, both cannot create a new version. So an MVCC database also uses exclusive locks for writes. T3 waits until T1 ends and then recomputes on top of the 400K version T1 created. MVCC did not eliminate locks; it narrowed the range where locks are needed to "between writes".

Then, if T1 commits after T3 has waited, what should T3 see? T3's snapshot does not contain T1's change, so in principle it should see the 500K version, but writing on top of that would be a Lost Update. Here the behavior diverges by isolation level.

  • Under Read Committed, after T3 obtains the lock it rereads the latest committed version of the row (400K), reevaluates the WHERE condition, and if the condition still holds, writes on top of it. For UPDATE ... SET balance = balance - 200000, 200,000 is subtracted from 400,000, giving 200,000 won. Both withdrawals are reflected.
  • Under Repeatable Read and above (snapshot isolation), the rule "a row committed by another transaction after my snapshot cannot be modified" is applied, and T3 fails with a serialization error. This is the first-committer-wins rule. The application must retry the transaction from the beginning.

Either way, "reading an old version and overwriting on top of it" does not happen. This protection, however, works only when the database reads and writes within a single UPDATE statement. In the pattern of Section 7.1, where the application reads with SELECT, computes, and writes with a separate UPDATE, the second UPDATE executes regardless of the "value that was read", so the database cannot know about the conflict. The means of filling this gap is the topic of the next section.