7.9 Cleaning Up Old Versions: Vacuum, Purge, and Long Transactions

Old versions cannot be kept forever. When no transaction that can see a particular old version remains, that version is no longer needed by anyone and must be removed. The name of this cleanup work differs by DBMS.

  • PostgreSQL's VACUUM: scans the table for old rows that no transaction can see (dead rows, Section 1.4) and marks their space as reusable. autovacuum performs it automatically in the background.
  • InnoDB's purge: deletes records that are no longer needed from the undo area and physically removes rows marked as deleted. Background threads perform it.
  • Oracle: because the undo area is circular, old records are naturally overwritten. It is a matter of configuring "how long to keep them" rather than a separate cleanup job.
  • SQL Server / Azure SQL: a background task periodically cleans the version store in tempdb.

There is one criterion for cleanup. Only versions older than the snapshot of the oldest transaction currently in progress can be removed, because that transaction may still be looking at them. This explains the third risk in Section 4.5. If one transaction stays open for three hours, none of the old versions created across the entire database during those three hours can be cleaned. It does not matter if that transaction never read those rows. The database keeps every version based only on the possibility that "it might read them."

Figure 7-8. The Span in Which a Long Transaction Blocks Cleanup

block-beta
  columns 5
  tm["time"] h0["before -3h"] h1["-3h -> -2h"] h2["-2h -> -1h"] h3["-1h -> now"]
  tl["T_long"] space bar["T_long still open"]:3
  sn["snapshot"] space snap["oldest active snapshot (T_long)"]:3
  hz["horizon"] space hzn["cleanup horizon: VACUUM / purge stops here"]:3
  ob["old versions"] ok["o o o · can be cleaned"] no["x x x x x x x x x · cannot be cleaned"]:3
  class tm,h0,h1,h2,h3 muted
  class tl,sn,hz,ob plain
  class bar slot
  class snap,hzn meta
  class ok data
  class no warn

The horizontal axis is time, and the right end (now) is the present. A transaction T_long that started three hours ago (-3h) has been open until now without committing or rolling back.

  1. The snapshot created when T_long started is the oldest snapshot among the transactions currently in progress. The cleanup horizon (-3h) is that point, and cleanup work (VACUUM, purge, and so on) can remove only versions older than this boundary.
  2. In the before -3h column, old versions (o) replaced before three hours ago are invisible to every in-progress snapshot, so they can be cleaned.
  3. In the columns after -3h, none of the old versions (x) created by UPDATEs and DELETEs across the entire database during the three hours can be cleaned, because in T_long's snapshot those versions may still be the "current value." This holds even if T_long never read those rows.
  4. As time passes, x keeps piling up to the right, but the cleanup boundary stays fixed at -3h until T_long ends. When T_long ends, the boundary moves to the point of the next oldest snapshot, and only then can the accumulated versions be removed.

The result appears as a different symptom in each DBMS. In PostgreSQL, tables and indexes bloat, queries slow down skipping dead rows, and in severe cases there is even a risk of exhausting transaction IDs. In InnoDB, the undo area grows and a metric called history list length surges, and queries reading old snapshots slow down following long undo chains. In Oracle, undo runs short and long queries fail with "snapshot too old." In SQL Server, tempdb grows.

PostgreSQL has one more risk unique to long transactions. Transaction IDs are 32-bit integers, so the limit is about 4.2 billion, and the number keeps increasing and then wraps around to the beginning. Section 7.5 said that the visibility decision is a "comparison of the size of numbers," so when the number wraps around, the "past" appears to be the "future" and every decision is inverted. To prevent this, PostgreSQL replaces the transaction ID of sufficiently old versions with a special marker meaning "the permanent past, visible to all transactions." This operation is called freeze and is performed by VACUUM. But freeze is subject to the same constraint as cleanup: only versions older than the oldest in-progress transaction can be frozen. If a long transaction blocks freeze while the number approaches the limit, PostgreSQL refuses new transactions and stops to prevent data corruption. This is one of the failures operators fear most, and it is why the "age of the oldest transaction ID" is on the monitoring list. InnoDB, which uses 64-bit numbers, Oracle, which uses SCNs, and SQL Server do not have this problem.

The symptoms differ, but the remedy is the same: find the long-open transaction and close it. The most common cause is the "application that opens a transaction on a connection with autocommit off and leaves it unattended" seen in Section 4.4, and the second is running a long report query on the production database at Repeatable Read. What an operator should check periodically is "when did the longest-open transaction start?"