dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/mysql/#33
Script #33
mysql
Lockingread-onlyMin Version: 5.7

Long-Open InnoDB Transactions in MySQL

Lists open transactions by age, locked rows and last query. Finds those blocking purge and bloating the history list.

#innodb#transaction#history-list
mysql · sql
SELECT trx_id, trx_state, trx_started,
       TIMESTAMPDIFF(SECOND, trx_started, NOW()) AS age_sec,
       trx_rows_locked, trx_rows_modified,
       trx_mysql_thread_id AS thread_id,
       LEFT(trx_query, 200) AS query
FROM information_schema.innodb_trx
ORDER BY trx_started;

SELECT name, count FROM information_schema.innodb_metrics WHERE name = 'trx_rseg_history_len';
Operational Notes & Remediation Guidance

Long-running open transactions retain undo logs, preventing purge threads from cleaning undo history.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 5.7).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.