MySQL InnoDB Transaction Commit: Undo Segment State Changes and Cleanup Process
The article explains how InnoDB handles transaction commit by modifying Insert and Update Undo segment states, generating a transaction commit number, adding rollback segments to the purge queue, and finally cleaning or caching Undo segments based on their usage and state.
1. Modify Insert Undo Segment State
During transaction commit, InnoDB checks whether the transaction has allocated an Insert Undo segment for a regular or temporary table and updates its state to either TRX_UNDO_CACHED if the segment can be reused, or TRX_UNDO_TO_FREE otherwise. If the segment manages only one Undo page and the used space is less than three‑quarters, it is marked TRX_UNDO_CACHED ; otherwise it is marked TRX_UNDO_TO_FREE for later release.
2. Generate Transaction Commit Number
When a transaction has an Update Undo segment, a commit number is generated from trx_sys->next_trx_id_or_no . The same counter provides both the transaction ID (at start) and the commit number (at commit), ensuring the commit number is always greater than the ID. The commit number is stored in the transaction object's no field and the transaction is appended to trx_sys->serialisation_list , marking it as a pending commit.
3. Add Rollback Segment to Purge Queue
If the transaction has an Update Undo segment, its owning rollback segment is added to the purge queue. Both regular‑table and temporary‑table rollback segments are packaged into a TrxUndoRsegs object, which holds an array m_rsegs of the involved segments and the commit number in m_trx_no . The packaged object is then enqueued for purge.
4. Process Update Undo Segment
4.1 Undo Segment State
Update Undo segments are treated similarly to Insert Undo segments: if they manage a single page with less than 75% usage, they become TRX_UNDO_CACHED ; otherwise they are set to TRX_UNDO_TO_PURGE to await purge.
4.2 Undo Log Page Count
For segments marked TRX_UNDO_TO_PURGE , the Undo slot in the rollback segment header is set to 4294967295 (FIL_NULL), breaking the link. The number of pages managed by the segment is added to the rollback segment header’s TRX_RSEG_HISTORY_SIZE , representing pages that will no longer receive new Undo logs.
4.3 Undo Log Group
Each new Undo log group created by the committing transaction is inserted at the head of the rollback segment’s history list, and trx_sys->rseg_history_len is incremented. If the length exceeds a threshold, a sleeping purge thread is awakened. The commit number is recorded in both the rollback segment header ( TRX_RSEG_MAX_TRX_NO ) and the log group header ( TRX_UNDO_TRX_NO ). If the group contains no delete or overflow‑field logs, its TRX_UNDO_DEL_MARKS flag is cleared to avoid unnecessary physical deletions.
4.4 Undo Segment List
After processing, the Update Undo segment is removed from the rollback segment’s update_undo_list . If its state is TRX_UNDO_CACHED , it is added to update_undo_cached for reuse; if TRX_UNDO_TO_PURGE , its memory object is freed (the underlying Undo pages are reclaimed only after purge).
5. Clean Insert Undo Segment
The Insert Undo segment is first removed from insert_undo_list . If its state is TRX_UNDO_CACHED , it is placed at the head of insert_undo_cached . If the state is TRX_UNDO_TO_FREE , both the segment and its managed pages are released, followed by freeing the segment’s memory object.
6. Summary
Modify Insert Undo segment state to TRX_UNDO_CACHED or TRX_UNDO_TO_FREE .
Generate a transaction commit number.
Add rollback segments that manage Update Undo segments to the purge queue.
Remove Update Undo segments from update_undo_list and cache or purge them based on their state.
Remove Insert Undo segments from insert_undo_list and cache or free them accordingly.
Aikesheng Open Source Community
The Aikesheng Open Source Community provides stable, enterprise‑grade MySQL open‑source tools and services, releases a premium open‑source component each year (1024), and continuously operates and maintains them.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.