skip to content

In MySQL's InnoDB, the History list length in SHOW ENGINE INNODB STATUS keeps climbing for hours; what does it measure, and how do you diagnose and fix it?

level: seniorimportance: should knowfreq 45%

answer

  1. undo waiting to be purged
  2. purge cannot pass the oldest read view
  3. an idle open transaction
  4. INNODB_TRX ordered by trx_started
  5. innodb_max_purge_lag throttles DML

basics

~20 s

History list length counts committed undo logs that purge has not processed. It climbs when a long-open transaction pins old versions or purge falls behind; find the oldest transaction in INNODB_TRX, end it, and let purge catch up.

solid answer

~40 s

InnoDB keeps old row versions in undo logs inside undo tablespaces. When a transaction commits, its update undo goes onto the **history list**, and background **purge threads** process it, physically removing delete-marked rows and freeing undo pages. Purge cannot discard undo that any open read view might still need, so one long-running or forgotten transaction, a `mysqldump --single-transaction` during heavy writes or an `autocommit=0` session that ran a `SELECT` and went idle, makes `History list length` climb. Find the culprit in `information_schema.INNODB_TRX` ordered by `trx_started`, and commit or kill it. Then purge catches up and `innodb_undo_log_truncate` shrinks oversized undo tablespaces. If writes simply outrun purge, `innodb_max_purge_lag` (0, off, by default) throttles DML, and `innodb_purge_threads`, whose 8.4 default is 1 on 16 or fewer logical CPUs and 4 otherwise, can be raised at restart.

code

sql · 9 lines
sql
SELECT trx_id, trx_mysql_thread_id, trx_state, trx_started,
       TIMESTAMPDIFF(SECOND, trx_started, NOW()) AS age_seconds
FROM information_schema.INNODB_TRX
ORDER BY trx_started
LIMIT 5;

SELECT NAME, COUNT
FROM information_schema.INNODB_METRICS
WHERE NAME = 'trx_rseg_history_len';

go deeper

for a junior

Recall that InnoDB keeps old row versions in undo logs and that a background purge removes them once nothing needs them.

for a middle

Explain insert versus update undo, what the history list holds, and why the oldest open read view limits how far purge can go.

for a senior

Run the diagnosis: trend History list length, find the oldest transaction in INNODB_TRX, end it, and know which purge and undo settings to touch and which need a restart.

for a principal

Set the policy that prevents recurrence: alerting on transaction age and history length, and where long dumps and reports are allowed to run.

## What the history list is InnoDB is a multi-version engine. When a row is updated, the new version is written in place in the clustered index and the information needed to rebuild the old version is written to an **undo log** in a **rollback segment**, which lives in an **undo tablespace**. A delete only sets a delete-mark on the row. Two kinds of undo exist: - **insert undo**, needed only for rollback and discarded at commit; - **update undo**, needed for rollback *and* for consistent reads by other transactions, so it outlives the commit. Committed transactions' update undo logs are kept on the **history list**. **Purge** works through that list in the background: it physically removes delete-marked rows and their index records, and frees undo pages it no longer needs. The value `History list length` in the `TRANSACTIONS` section of `SHOW ENGINE INNODB STATUS` is the purge lag. It is typically low, usually under a few thousand. ## Why it climbs Purge may only discard undo that no open **read view** could still need. Under InnoDB's default `REPEATABLE READ`, a transaction's snapshot is fixed at its first consistent read, and every row version committed after that moment must stay reconstructable until the transaction ends. So the history list grows when: 1. **A transaction stays open for a long time**, even a read-only one. Typical culprits are a `mysqldump --single-transaction` running while heavy DML continues, a session with `autocommit=0` that ran a `SELECT` and never issued `COMMIT` or `ROLLBACK`, or an hours-long report. 2. **Writes outrun purge**, for instance a write-heavy workload concentrated on one table, which a single purge thread processes. The symptoms build slowly: undo tablespaces (`undo_001` and `undo_002` by default) grow, delete-marked rows and secondary index entries pile up so scans and lookups touch more garbage, and consistent reads walk longer version chains. ## Diagnosing - Watch `History list length` over time, or graph the `trx_rseg_history_len` counter in `INFORMATION_SCHEMA.INNODB_METRICS`, which is enabled by default. - Find the oldest transactions in `INFORMATION_SCHEMA.INNODB_TRX`, ordered by `trx_started`. `trx_mysql_thread_id` maps each to a connection in the process list. - Check undo tablespace sizes through `INFORMATION_SCHEMA.FILES` where `FILE_TYPE` is `UNDO LOG`. ## Fixing it 1. **End the pinning transaction.** Have its owner commit, or `KILL` the connection; if it had written a lot, its rollback can itself take a while. Once its read view is gone, purge catches up on its own. 2. **Let undo shrink.** With `innodb_undo_log_truncate=ON` (the default), an undo tablespace larger than `innodb_max_undo_log_size` (1 GiB by default) is taken offline, emptied by purge and truncated back to 16 MB. Automatic truncation needs at least two active undo tablespaces, which a default instance has; `CREATE UNDO TABLESPACE ... ADD DATAFILE 'name.ibu'` adds more. 3. **If writes outrun purge**, give purge more room or slow the writers: - `innodb_purge_threads` is the maximum number of purge threads; it is not dynamic. **MySQL 8.4 changed its default** to 1 when the server has 16 or fewer logical processors and 4 otherwise (8.0 used 4). More threads help when DML spreads across many tables, and contend with each other when it is concentrated on a few. - `innodb_max_purge_lag`, `0` by default meaning no limit, delays `INSERT`, `UPDATE` and `DELETE` once the purge lag exceeds it; `innodb_max_purge_lag_delay`, in microseconds, caps that delay. - `innodb_purge_batch_size`, default 300 undo log pages, is meant for advanced tuning and is usually left alone. | Setting | Default | Dynamic | |---|---|---| | `innodb_purge_threads` | 1 if logical CPUs are 16 or fewer, otherwise 4 | No | | `innodb_max_purge_lag` | 0 (no limit) | Yes | | `innodb_max_purge_lag_delay` | 0 (microseconds) | Yes | | `innodb_undo_log_truncate` | `ON` | Yes | | `innodb_max_undo_log_size` | 1073741824 bytes | Yes | ## Preventing it - Keep transactions short and make sure error paths `COMMIT` or `ROLLBACK`. - Alert on `trx_rseg_history_len` and on any transaction in `INNODB_TRX` older than a threshold. - Run long consistent dumps and reports where their snapshot does not pin the primary's purge, such as a replica. Why a long snapshot pins old versions is the general MVCC horizon story; what is InnoDB's own is where those versions live, the status line and metric that expose them, and the purge and truncation settings that drain them.

  • Why can a read-only transaction make the History list length grow?
    Under `REPEATABLE READ`, a consistent read sees the snapshot fixed at the transaction's first read, so InnoDB must keep every row version committed after that point reconstructable from undo until the transaction ends. Purge cannot free that undo, however little the transaction itself wrote. Committing or closing the session releases the read view.
  • How does innodb_max_purge_lag slow writers, and why is it off by default?
    Once the purge lag exceeds `innodb_max_purge_lag`, InnoDB adds a delay to `INSERT`, `UPDATE` and `DELETE` operations, calculated from how far the lag is over the threshold and capped by `innodb_max_purge_lag_delay`. It trades write latency for purge progress, so it is off (0) by default and is worth setting only for a workload where purge keeps falling behind.

  • History list length counts transactions waiting for row locks.
  • Only transactions that modify data can stop purge from progressing.
  • Purge removes undo as soon as the writing transaction commits.
  • innodb_purge_threads can be raised at runtime to fix a backlog immediately.
  • Undo tablespaces never shrink, so a rebuild is the only cure for growth.

Assumes MySQL 8.4, which changed the innodb_purge_threads default from 4 to 1 on servers with 16 or fewer logical processors.