7.7 Where Old Versions Are Stored
To implement MVCC, old versions must be kept somewhere. Where they are kept differs by DBMS, and this difference produces the differences in behavior you encounter in operations. There are three approaches.
PostgreSQL: in the table itself. As seen in Section 1.4, an UPDATE adds a new row to the same table page (or another page) and leaves the old row in place, marked only with "which transaction deleted it." All versions sit side by side in the table, so the structure is simple and rollback is fast, because it is enough to treat the new version as nonexistent. The cost is that old versions occupy table space, so the table grows (bloat) and VACUUM is needed to clean it up. In addition, because indexes point to the row's address (Section 3.5), every index must be updated each time a row is created at a new location.
InnoDB and Oracle: in a separate undo area. An UPDATE overwrites the table's row in place with the new value, and copies the pre-overwrite value into an undo area (the undo log in InnoDB, the undo tablespace in Oracle). A query that needs an old version starts from the current row in the table and follows the undo records backward to reconstruct the past value that matches its snapshot. The table always holds only the latest version, which keeps it clean, and there is less index updating. The cost is that queries reading an old snapshot must follow a long undo chain and become slow, and if the undo area is reused first, the query fails. Oracle's well-known "snapshot too old" error is this case. This is the structure behind the statement in Section 5.6 that undo information is used for both rollback and concurrent queries.
SQL Server and Azure SQL: in a version store. The default behavior is the traditional lock-based approach, but turning on snapshot isolation or RCSI (Read Committed Snapshot Isolation) switches it to MVCC. Old versions are then copied into the version store in tempdb. This is the case described in Section 1.4, where SQL Server modifies in place but leaves the old value in the version store. Structurally it is close to the InnoDB and Oracle approach, but it differs in that it can be turned on and off.
Figure 7-6. Comparison of Old Version Locations
flowchart LR
subgraph PG ["PostgreSQL"]
PGT["Table page"]
PGO["Old version: 500,000<br/>xmax marks the deleting transaction"]
PGN["New version: 400,000"]
PGT --- PGO
PGT --- PGN
end
subgraph IO ["InnoDB · Oracle"]
IOT["Table: latest version 400,000<br/>overwritten in place"]
IOU["Undo area: old value 500,000"]
IOT -->|"Follows undo records backward"| IOU
end
subgraph SS ["SQL Server · Azure SQL<br/>snapshot isolation or RCSI"]
SST["Table: latest version 400,000<br/>overwritten in place"]
SSV["tempdb version store: old value 500,000"]
SST -->|"References old version"| SSV
end
The three boxes show side by side where the same change, updating Hong Gildong's balance from 500,000 to 400,000, is left in each DBMS immediately afterward.
- PostgreSQL (left). The table page holds both the old version (500,000) and the new version (400,000). The old version is not erased; only the number of the transaction that replaced it is written to
xmax. A query picks, among the versions in the table, the one visible to its snapshot. Because the old version occupies table space, VACUUM must clean it up later. - InnoDB and Oracle (middle). The table holds only the latest version (400,000). Before the UPDATE overwrote the row in place, it copied the old value (500,000) into the undo area. A query that needs the old version starts from the current row in the table and follows the undo records backward in the direction of the arrow to reconstruct the past value.
- SQL Server and Azure SQL (right). This is the picture when snapshot isolation or RCSI is on. The table is modified in place and holds only the latest version, and the old value is copied into the version store in tempdb. The structure is similar to the middle one, except that the old version lives in tempdb rather than in the user database.
Whether old versions are inside the table (left) or in a separate area outside it (middle and right) determines the advantages and costs in the table below.
| Approach | Representative DBMS | Advantage | Cost |
|---|---|---|---|
| Versions kept in the table | PostgreSQL | Fast rollback, simple structure | Table bloat, VACUUM required, index update overhead |
| Separate undo area | InnoDB, Oracle | Table keeps only the latest version | Long queries slow down, queries fail when undo runs short |
| Version store (tempdb) | SQL Server, Azure SQL | Can be enabled only when needed | tempdb space and I/O overhead |
Learn more — VACUUM, InnoDB undo log, Oracle undo, RCSI and snapshot isolation
Table 7-4. Comparison of old version storage approaches.
It is hard to say that one approach is superior. The three pay the same cost at different times. PostgreSQL has a small cost at write time and pays a cleanup cost later, while the undo approach pays a copy cost at write time and keeps the table clean. What an operator needs to know is "where does my DBMS keep old versions," because what to monitor depends on the answer.
How much do indexes know? Indexes are a point that is often overlooked with respect to old versions. Chapter 3 said an index holds "a value and a row address." An index entry carries no information about which transactions can see that row. So in an MVCC database, a query that uses an index must find the address in the index and then read the actual row version in the table to check visibility. Section 3.5 introduced the covering index lookup that "finishes by reading only the index," but in PostgreSQL this lookup does not hold as is, because it cannot be known whether the value in the index belongs to the version visible in my snapshot. For this, PostgreSQL keeps a visibility map, which records per page the fact that "all rows in this page are visible to all transactions," and skips the table visit only for pages with that mark (index-only scan). VACUUM updates this mark, so when VACUUM falls behind, covering index lookups also become slower. InnoDB also records the last-modifying transaction number in secondary index pages, so in many cases it can judge visibility from the index alone, and Oracle has an SCN in index blocks as well, so it handles consistent reads at the index level.
NOTE How visibility is judged at the index level may differ by product and version.