InnoDB Undo Log

The undo log is where InnoDB records a row's previous value when it changes the row. It serves two purposes. It restores rows when a transaction rolls back, and it rebuilds an old version of a row when another transaction reads from a past point in time (consistent read). The table always holds only the latest version. Earlier versions are rebuilt by following the undo records that the row's DB_ROLL_PTR points to.

Key Specifications

ItemValue
Storage locationUndo tablespaces (undo_001, undo_002; two by default since 8.0), and the global temporary tablespace for temporary tables
StructureUndo tablespace → rollback segments (128 by default, innodb_rollback_segments) → undo segments → undo logs
Record typesInsert undo (discarded right at commit), update undo (kept until MVCC no longer needs it)
CleanupPurge threads (innodb_purge_threads, default 4)
Accumulated amountHistory list length (the number of committed transactions not yet purged)
Shrinking the tablespaceinnodb_undo_log_truncate (on by default in 8.0), when innodb_max_undo_log_size is exceeded
Adding and dropping undo tablespacesCREATE UNDO TABLESPACE, DROP UNDO TABLESPACE since 8.0.14

How It Works

Figure. Row version chain and read view

flowchart LR
  R["Row in the table<br/>balance 300K, DB_TRX_ID 120"] -- "DB_ROLL_PTR" --> U1["Undo record<br/>balance 400K, TRX 100"]
  U1 -- "previous pointer" --> U2["Undo record<br/>balance 500K, TRX 73"]
  V["Read view<br/>active list: 100, 120"] -. "120 and 100 are not visible" .-> U2

Diagram explanation: The Row in the table holds the latest value and the number of the transaction that last changed it (DB_TRX_ID 120). Following DB_ROLL_PTR leads to the Undo record of the immediately preceding version, which in turn leads to the version before that. A transaction doing a consistent read creates a Read view when it starts (Repeatable Read) or at each statement (Read Committed), and records the list of transactions that were active at that moment. If the row's transaction number is not visible, the read walks back along the chain to find a visible version (the 500K created by TRX 73).

Purge. The purge thread removes the update undo of a committed transaction once no read view older than it remains. DELETE also only marks the row as deleted, and the purge does the actual removal.

Long transactions. If a transaction stays open for a long time (especially a session that only runs SELECT under Repeatable Read and never commits), no undo created after its read view can be removed. The history list length keeps growing, the undo tablespace grows, and reads by other transactions slow down because they follow long chains.

Configuration and Inspection

-- history list length (TRANSACTIONS section)
SHOW ENGINE INNODB STATUS;
SELECT count FROM information_schema.INNODB_METRICS WHERE name = 'trx_rseg_history_len';

-- Long-open transactions
SELECT trx_id, trx_started, TIMESTAMPDIFF(SECOND, trx_started, NOW()) AS age_s, trx_mysql_thread_id, trx_query
FROM information_schema.INNODB_TRX ORDER BY trx_started LIMIT 10;

SELECT TABLESPACE_NAME, FILE_NAME FROM information_schema.FILES WHERE FILE_TYPE = 'UNDO LOG';
SHOW VARIABLES LIKE 'innodb_undo%';

Comparison with Other DBMSs

InnoDBPostgreSQLOracleSQL Server
Location of old versionsUndo tablespaceInside the table (dead rows)Undo tablespacetempdb version store (PVS with ADR)
CleanupPurgeVACUUMOverwritten after the retention periodBackground cleanup
When old versions are goneThe chain just gets longer and reads continueNot applicableORA-01555Error when tempdb fills up

In the Articles

NOTE The undo tablespace layout and defaults differ by version. Before 5.7, undo lived in the system tablespace (ibdata1) and could not be shrunk.

Related pages