MySQL 9.1.0
Source Code Documentation
trx0purge.cc File Reference

Purge old versions. More...

#include <sys/types.h>
#include <new>
#include <unordered_map>
#include "clone0api.h"
#include "clone0clone.h"
#include "dict0dd.h"
#include "fil0fil.h"
#include "fsp0fsp.h"
#include "fsp0sysspace.h"
#include "fsp0types.h"
#include "fut0fut.h"
#include "ha_prototypes.h"
#include "log0buf.h"
#include "log0chkp.h"
#include "mach0data.h"
#include "mtr0log.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "os0thread.h"
#include "que0que.h"
#include "read0read.h"
#include "row0purge.h"
#include "row0upd.h"
#include "srv0mon.h"
#include "srv0srv.h"
#include "srv0start.h"
#include "sync0sync.h"
#include "trx0purge.h"
#include "trx0rec.h"
#include "trx0roll.h"
#include "trx0rseg.h"
#include "trx0trx.h"
#include "ut0math.h"

Classes

struct  Purge_groups_t
 

Namespaces

namespace  undo
 

Functions

static que_ttrx_purge_graph_build (trx_t *trx, ulint n_purge_threads)
 Builds a purge 'query' graph. More...
 
void trx_purge_sys_mem_create ()
 Initialize in-memory purge structures. More...
 
void trx_purge_sys_initialize (uint32_t n_purge_threads, purge_pq_t *purge_queue)
 Creates the global purge system control structure and inits the history mutex. More...
 
void trx_purge_sys_close ()
 Frees the global purge system control structure. More...
 
void trx_purge_add_update_undo_to_history (trx_t *trx, trx_undo_ptr_t *undo_ptr, page_t *undo_page, bool update_rseg_history_len, ulint n_added_logs, mtr_t *mtr)
 Adds the update undo log as the first log in the history list. More...
 
static void trx_purge_remove_log_hdr (trx_rsegf_t *rseg_hdr, trx_ulogf_t *log_hdr, mtr_t *mtr)
 Remove an rseg header from the history list. More...
 
static void trx_purge_free_segment (trx_rseg_t *rseg, fil_addr_t hdr_addr, bool noredo)
 Frees a rollback segment which is in the history list. More...
 
static void trx_purge_truncate_rseg_history (trx_rseg_t *rseg, const purge_iter_t *limit)
 Removes unnecessary history data from a rollback segment. More...
 
void undo::init_space_id_bank ()
 Initialize the undo tablespace space_id bank which is a lock free repository for information about the space IDs used for undo tablespaces. More...
 
void undo::use_space_id (space_id_t space_id)
 Note that the undo space number for a space ID is being used. More...
 
void undo::unuse_space_id (space_id_t space_id)
 Mark an undo number associated with a given space_id as unused and available to be reused. More...
 
space_id_t undo::next_space_id (space_id_t space_id, space_id_t space_num)
 Given a valid undo space_id or SPACE_UNKNOWN, return the next space_id for the given space number. More...
 
space_id_t undo::next_space_id (space_id_t space_id)
 Given a valid undo space_id, return the next space_id for that space number. More...
 
space_id_t undo::use_next_space_id (space_id_t space_num)
 Mark that the given undo space number is being used and return the next available space_id for that space number. More...
 
space_id_t undo::get_next_available_space_num ()
 Return the next available undo space ID to be used for a new explicit undo tablespaces. More...
 
char * undo::make_space_name (space_id_t space_id)
 Build a standard undo tablespace name from a space_id. More...
 
char * undo::make_file_name (space_id_t space_id)
 Build a standard undo tablespace file name from a space_id. More...
 
void undo::inject_crash (const char *injection_point_name)
 Inject a crash if a certain SET GLOBAL DEBUG has been set. More...
 
dberr_t undo::start_logging (Tablespace *undo_space)
 Create the truncate log file. More...
 
void undo::done_logging (space_id_t space_num)
 Mark completion of undo truncate action by writing magic number to the log file and then removing it from the disk. More...
 
bool undo::is_active_truncate_log_present (space_id_t space_num)
 Check if TRUNCATE_DDL_LOG file exist. More...
 
void undo::add_space_to_construction_list (space_id_t space_id)
 Add undo tablespace to s_under_construction vector. More...
 
void undo::clear_construction_list ()
 Clear the s_under_construction vector. More...
 
bool undo::is_under_construction (space_id_t space_id)
 Is an undo tablespace under construction at the moment. More...
 
void undo::set_active (space_id_t space_id)
 Set an undo tablespace active. More...
 
bool undo::is_active (space_id_t space_id, bool get_latch=true)
 
static bool trx_purge_mark_undo_for_truncate (size_t truncate_count)
 Decide if an undo truncation needs to be done at this time. More...
 
static bool trx_purge_check_if_marked_undo_is_empty ()
 Iterate over selected UNDO tablespace and check if all the rsegs that resides in the tablespace have been freed. More...
 
static bool trx_purge_truncate_marked_undo_low (space_id_t space_num, std::string space_name)
 Truncate the marked undo tablespace. More...
 
static bool trx_purge_truncate_marked_undo ()
 Truncate the marked undo tablespace. More...
 
static void trx_purge_truncate_history (purge_iter_t *limit, const ReadView *view)
 Removes unnecessary history data from rollback segments. More...
 
static void trx_purge_truncate_undo_spaces ()
 Select an undo tablespace to truncate, make sure it is empty of undo logs, then finally truncate it. More...
 
static void trx_purge_rseg_get_next_history_log (trx_rseg_t *rseg, ulint *n_pages_handled)
 Updates the last not yet purged history log info in rseg when we have purged a whole undo log. More...
 
static void trx_purge_read_undo_rec (trx_purge_t *purge_sys, const page_size_t &page_size)
 Position the purge sys "iterator" on the undo record to use for purging. More...
 
static void trx_purge_choose_next_log (void)
 Chooses the next undo log to purge and updates the info in purge_sys. More...
 
static trx_undo_rec_ttrx_purge_get_next_rec (ulint *n_pages_handled, mem_heap_t *heap)
 Gets the next record to purge and updates the info in the purge system. More...
 
static trx_undo_rec_ttrx_purge_fetch_next_rec (trx_id_t *modifier_trx_id, roll_ptr_t *roll_ptr, ulint *n_pages_handled, mem_heap_t *heap)
 Fetches the next undo log record from the history list to purge. More...
 
static ulint trx_purge_attach_undo_recs (const ulint n_purge_threads, ulint batch_size)
 This function runs a purge batch. More...
 
static ulint trx_purge_dml_delay (void)
 Calculate the DML delay required. More...
 
static void trx_purge_wait_for_workers_to_complete ()
 Wait for pending purge jobs to complete. More...
 
static void trx_purge_truncate (void)
 Remove old historical changes from the rollback segments. More...
 
ulint trx_purge (ulint n_purge_threads, ulint batch_size, bool truncate)
 This function runs a purge batch. More...
 
purge_state_t trx_purge_state (void)
 Get the purge state. More...
 
void trx_purge_stop (void)
 Stop purge and wait for it to stop, move to PURGE_STATE_STOP. More...
 
void trx_purge_run (void)
 Resume purge, move to PURGE_STATE_RUN. More...
 

Variables

ulong srv_max_purge_lag = 0
 Maximum allowable purge history length. More...
 
ulong srv_max_purge_lag_delay = 0
 Max DML user threads delay in micro-seconds. More...
 
trx_purge_tpurge_sys = nullptr
 The global data structure coordinating a purge. More...
 
static constexpr int64_t PURGE_CHECK_UNDO_TRUNCATE_DELAY_IN_MS = 10
 Wait for a short delay between checks. More...
 
bool srv_purge_view_update_only_debug
 
static trx_undo_rec_t trx_purge_ignore_rec
 A sentinel undo record used as a return value when we have a whole undo log which can be skipped by purge. More...
 

Detailed Description

Purge old versions.

Created 3/26/1996 Heikki Tuuri

Function Documentation

◆ trx_purge()

ulint trx_purge ( ulint  n_purge_threads,
ulint  batch_size,
bool  truncate 
)

This function runs a purge batch.

Returns
number of undo log pages handled in the batch
Parameters
n_purge_threadsin: number of purge tasks to submit to the work queue
batch_sizein: the maximum number of records to purge in one batch
truncatein: truncate history if true

◆ trx_purge_add_update_undo_to_history()

void trx_purge_add_update_undo_to_history ( trx_t trx,
trx_undo_ptr_t undo_ptr,
page_t undo_page,
bool  update_rseg_history_len,
ulint  n_added_logs,
mtr_t mtr 
)

Adds the update undo log as the first log in the history list.

in: mtr

Removes the update undo log segment from the rseg slot if it is too big for reuse.

Parameters
trxin: transaction
undo_ptrin/out: update undo log.
undo_pagein: update undo log header page, x-latched
update_rseg_history_lenin: if true: update rseg history len else skip updating it.
n_added_logsin: number of logs added
mtrin: mtr

◆ trx_purge_attach_undo_recs()

static ulint trx_purge_attach_undo_recs ( const ulint  n_purge_threads,
ulint  batch_size 
)
static

This function runs a purge batch.

Parameters
[in]n_purge_threadsnumber of purge threads
[in]batch_sizenumber of pages to purge
Returns
number of undo log pages handled in the batch

◆ trx_purge_check_if_marked_undo_is_empty()

static bool trx_purge_check_if_marked_undo_is_empty ( )
static

Iterate over selected UNDO tablespace and check if all the rsegs that resides in the tablespace have been freed.

◆ trx_purge_choose_next_log()

static void trx_purge_choose_next_log ( void  )
static

Chooses the next undo log to purge and updates the info in purge_sys.

This function is used to initialize purge_sys when the next record to purge is not known, and also to update the purge system info on the next record when purge has handled the whole undo log for a transaction.

◆ trx_purge_dml_delay()

static ulint trx_purge_dml_delay ( void  )
static

Calculate the DML delay required.

Returns
delay in microseconds or ULINT_MAX

◆ trx_purge_fetch_next_rec()

static trx_undo_rec_t * trx_purge_fetch_next_rec ( trx_id_t modifier_trx_id,
roll_ptr_t roll_ptr,
ulint n_pages_handled,
mem_heap_t heap 
)
static

Fetches the next undo log record from the history list to purge.

It must be released with the corresponding release function.

Returns
copy of an undo log record or pointer to trx_purge_ignore_rec, if the whole undo log can skipped in purge; NULL if none left
Parameters
modifier_trx_idout: modifier trx id. this is the trx that created the undo record.
roll_ptrout: roll pointer to undo record
n_pages_handledin/out: number of UNDO log pages handled
heapin: memory heap where copied

◆ trx_purge_free_segment()

static void trx_purge_free_segment ( trx_rseg_t rseg,
fil_addr_t  hdr_addr,
bool  noredo 
)
static

Frees a rollback segment which is in the history list.

Removes the rseg hdr from the history list.

Parameters
[in,out]rsegrollback segment
[in]hdr_addrfile address of log_hdr
[in]noredoskip redo logging.

◆ trx_purge_get_next_rec()

static trx_undo_rec_t * trx_purge_get_next_rec ( ulint n_pages_handled,
mem_heap_t heap 
)
static

Gets the next record to purge and updates the info in the purge system.

Returns
copy of an undo log record or pointer to the dummy undo log record
Parameters
n_pages_handledin/out: number of UNDO pages handled
heapin: memory heap where copied

◆ trx_purge_graph_build()

static que_t * trx_purge_graph_build ( trx_t trx,
ulint  n_purge_threads 
)
static

Builds a purge 'query' graph.

The actual purge is performed by executing this query graph.

Parameters
[in]trxtransaction
[in]n_purge_threadsnumber of purge threads
Returns
own: the query graph

◆ trx_purge_mark_undo_for_truncate()

static bool trx_purge_mark_undo_for_truncate ( size_t  truncate_count)
static

Decide if an undo truncation needs to be done at this time.

If an undo tablespace is already marked, return that so that this truncation will get finished. If none is marked, iterate over all the UNDO tablespaces and check if any qualify to be truncated. Normal operation; Choose the marked space. If none are marked, choose an explicitly inactive space. If none exist, check if conditions allow implicit truncation. If conditions allow, choose an implicitly inactive space. If none exist and no space has been truncated yet, look for an undo space that is too big. Fast shutdown; Do not truncate. This routine is not called. Slow shutdown; Choose the marked space. If none are marked, choose an explicitly inactive space. If none exist, check if conditions allow implicit truncation. If conditions allow, ignoring the previous truncate count, look for an undo space that is too big.

Parameters
[in]truncate_countnumber of times this is called in a loop
Returns
true if an undo tablespace was marked for truncate.

◆ trx_purge_read_undo_rec()

static void trx_purge_read_undo_rec ( trx_purge_t purge_sys,
const page_size_t page_size 
)
static

Position the purge sys "iterator" on the undo record to use for purging.

Parameters
[in,out]purge_syspurge instance
[in]page_sizepage size

◆ trx_purge_remove_log_hdr()

static void trx_purge_remove_log_hdr ( trx_rsegf_t rseg_hdr,
trx_ulogf_t log_hdr,
mtr_t mtr 
)
static

Remove an rseg header from the history list.

Parameters
[in,out]rseg_hdrRollback segment header
[in]log_hdrUndo log segment header
[in,out]mtrMini-transaction.

◆ trx_purge_rseg_get_next_history_log()

static void trx_purge_rseg_get_next_history_log ( trx_rseg_t rseg,
ulint n_pages_handled 
)
static

Updates the last not yet purged history log info in rseg when we have purged a whole undo log.

Advances also purge_sys->purge_trx_no past the purged log.

Parameters
rsegin: rollback segment
n_pages_handledin/out: number of UNDO pages handled

◆ trx_purge_run()

void trx_purge_run ( void  )

Resume purge, move to PURGE_STATE_RUN.

◆ trx_purge_state()

purge_state_t trx_purge_state ( void  )

Get the purge state.

Returns
purge state.

◆ trx_purge_stop()

void trx_purge_stop ( void  )

Stop purge and wait for it to stop, move to PURGE_STATE_STOP.

◆ trx_purge_sys_close()

void trx_purge_sys_close ( void  )

Frees the global purge system control structure.

◆ trx_purge_sys_initialize()

void trx_purge_sys_initialize ( uint32_t  n_purge_threads,
purge_pq_t purge_queue 
)

Creates the global purge system control structure and inits the history mutex.

Parameters
[in]n_purge_threadsnumber of purge threads
[in,out]purge_queueUNDO log min binary heap

◆ trx_purge_sys_mem_create()

void trx_purge_sys_mem_create ( )

Initialize in-memory purge structures.

◆ trx_purge_truncate()

static void trx_purge_truncate ( void  )
static

Remove old historical changes from the rollback segments.

◆ trx_purge_truncate_history()

static void trx_purge_truncate_history ( purge_iter_t limit,
const ReadView view 
)
static

Removes unnecessary history data from rollback segments.

NOTE that when this function is called, the caller must not have any latches on undo log pages!

Parameters
[in]limitTruncate limit
[in]viewPurge view

◆ trx_purge_truncate_marked_undo()

static bool trx_purge_truncate_marked_undo ( )
static

Truncate the marked undo tablespace.

This wrapper does initial preparation and handles cleanup.

Returns
true for success, false for failure

◆ trx_purge_truncate_marked_undo_low()

static bool trx_purge_truncate_marked_undo_low ( space_id_t  space_num,
std::string  space_name 
)
static

Truncate the marked undo tablespace.

This layer handles locking of marked_rsegs and undo::spaces.

Returns
true for success, false for failure

◆ trx_purge_truncate_rseg_history()

static void trx_purge_truncate_rseg_history ( trx_rseg_t rseg,
const purge_iter_t limit 
)
static

Removes unnecessary history data from a rollback segment.

Parameters
rsegin: rollback segment
limitin: truncate offset

◆ trx_purge_truncate_undo_spaces()

static void trx_purge_truncate_undo_spaces ( )
static

Select an undo tablespace to truncate, make sure it is empty of undo logs, then finally truncate it.

◆ trx_purge_wait_for_workers_to_complete()

static void trx_purge_wait_for_workers_to_complete ( )
static

Wait for pending purge jobs to complete.

Variable Documentation

◆ PURGE_CHECK_UNDO_TRUNCATE_DELAY_IN_MS

constexpr int64_t PURGE_CHECK_UNDO_TRUNCATE_DELAY_IN_MS = 10
staticconstexpr

Wait for a short delay between checks.

◆ purge_sys

trx_purge_t* purge_sys = nullptr

The global data structure coordinating a purge.

◆ srv_max_purge_lag

ulong srv_max_purge_lag = 0

Maximum allowable purge history length.

<=0 means 'infinite'.

◆ srv_max_purge_lag_delay

ulong srv_max_purge_lag_delay = 0

Max DML user threads delay in micro-seconds.

◆ srv_purge_view_update_only_debug

bool srv_purge_view_update_only_debug

◆ trx_purge_ignore_rec

trx_undo_rec_t trx_purge_ignore_rec
static

A sentinel undo record used as a return value when we have a whole undo log which can be skipped by purge.