MySQL 8.0.40
Source Code Documentation
trx0roll.cc File Reference

Transaction rollback. More...

#include <sys/types.h>
#include "clone0clone.h"
#include "dict0dd.h"
#include "fsp0fsp.h"
#include "ha_prototypes.h"
#include "lock0lock.h"
#include "mach0data.h"
#include "os0thread-create.h"
#include "pars0pars.h"
#include "que0que.h"
#include "read0read.h"
#include "row0mysql.h"
#include "row0undo.h"
#include "sql_thd_internal_api.h"
#include "srv0mon.h"
#include "srv0start.h"
#include "trx0rec.h"
#include "trx0roll.h"
#include "trx0rseg.h"
#include "trx0sys.h"
#include "trx0trx.h"
#include "trx0undo.h"
#include "usr0sess.h"
#include "current_thd.h"

Functions

static void trx_rollback_finish (trx_t *trx)
 Finishes a transaction rollback. More...
 
static void trx_rollback_to_savepoint_low (trx_t *trx, trx_savept_t *savept)
 Rollback a transaction used in MySQL. More...
 
dberr_t trx_rollback_to_savepoint (trx_t *trx, trx_savept_t *savept)
 Rollback a transaction to a given savepoint or do a complete rollback. More...
 
static dberr_t trx_rollback_for_mysql_low (trx_t *trx)
 Rollback a transaction used in MySQL. More...
 
static dberr_t trx_rollback_low (trx_t *trx)
 Rollback a transaction used in MySQL. More...
 
dberr_t trx_rollback_for_mysql (trx_t *trx)
 Rollback a transaction used in MySQL. More...
 
dberr_t trx_rollback_last_sql_stat_for_mysql (trx_t *trx)
 Rollback the latest SQL statement for MySQL. More...
 
static trx_named_savept_ttrx_savepoint_find (trx_t *trx, const char *name)
 Search for a savepoint using name. More...
 
static void trx_roll_savepoint_free (trx_t *trx, trx_named_savept_t *savep)
 Frees a single savepoint struct. More...
 
void trx_roll_savepoints_free (trx_t *trx, trx_named_savept_t *savep)
 Frees savepoint structs starting from savep. More...
 
static dberr_t trx_rollback_to_savepoint_for_mysql_low (trx_t *trx, trx_named_savept_t *savep, int64_t *mysql_binlog_cache_pos)
 Rolls back a transaction back to a named savepoint. More...
 
dberr_t trx_rollback_to_savepoint_for_mysql (trx_t *trx, const char *savepoint_name, int64_t *mysql_binlog_cache_pos)
 Rolls back a transaction back to a named savepoint. More...
 
dberr_t trx_savepoint_for_mysql (trx_t *trx, const char *savepoint_name, int64_t binlog_cache_pos)
 Creates a named savepoint. More...
 
dberr_t trx_release_savepoint_for_mysql (trx_t *trx, const char *savepoint_name)
 Releases only the named savepoint. More...
 
bool trx_is_recv (const trx_t *trx)
 Determines if this transaction is rolling back an incomplete transaction in crash recovery. More...
 
trx_savept_t trx_savept_take (trx_t *trx)
 Returns a transaction savepoint taken at this point in time. More...
 
static void trx_rollback_active (trx_t *trx)
 Roll back an active transaction. More...
 
static bool trx_rollback_or_clean_resurrected (trx_t *trx, bool all)
 Rollback or clean up any resurrected incomplete transactions. More...
 
void trx_rollback_or_clean_recovered (bool all)
 Rollback or clean up any incomplete transactions which were encountered in crash recovery. More...
 
void trx_recovery_rollback (THD *thd)
 Rollback or clean up any incomplete transactions which were encountered in crash recovery. More...
 
void trx_recovery_rollback_thread ()
 Rollback or clean up any incomplete transactions which were encountered in crash recovery. More...
 
static void trx_roll_try_truncate (trx_t *trx, trx_undo_ptr_t *undo_ptr)
 Tries truncate the undo logs. More...
 
static const page_ttrx_roll_pop_top_rec (trx_t *trx, trx_undo_t *undo, mtr_t *mtr, uint32_t *undo_offset)
 Pops the topmost undo log record in a single undo log and updates the info about the topmost record in the undo log memory struct. More...
 
static trx_undo_rec_ttrx_roll_pop_top_rec_of_trx_low (trx_t *trx, trx_undo_ptr_t *undo_ptr, undo_no_t limit, roll_ptr_t *roll_ptr, mem_heap_t *heap)
 Pops the topmost record when the two undo logs of a transaction are seen as a single stack of records ordered by their undo numbers. More...
 
trx_undo_rec_ttrx_roll_pop_top_rec_of_trx (trx_t *trx, undo_no_t limit, roll_ptr_t *roll_ptr, mem_heap_t *heap)
 Get next undo log record from redo and noredo rollback segments. More...
 
static que_ttrx_roll_graph_build (trx_t *trx, bool partial_rollback)
 Builds an undo 'query' graph for a transaction. More...
 
static que_thr_ttrx_rollback_start (trx_t *trx, ib_id_t roll_limit, bool partial_rollback)
 Starts a rollback operation, creates the UNDO graph that will do the actual undo operation. More...
 
roll_node_troll_node_create (mem_heap_t *heap)
 Creates a rollback command node struct. More...
 
que_thr_ttrx_rollback_step (que_thr_t *thr)
 Performs an execution step for a rollback command node in a query graph. More...
 

Variables

static const ulint TRX_ROLL_TRUNC_THRESHOLD = 1
 This many pages must be undone before a truncate is tried within rollback. More...
 
static const trx_ttrx_roll_crash_recv_trx = nullptr
 In crash recovery, the current trx to be rolled back; NULL otherwise. More...
 
static undo_no_t trx_roll_max_undo_no
 In crash recovery we set this to the undo n:o of the current trx to be rolled back. More...
 
static ulint trx_roll_progress_printed_pct
 Auxiliary variable which tells the previous progress % we printed. More...
 

Detailed Description

Transaction rollback.

Created 3/26/1996 Heikki Tuuri

Function Documentation

◆ roll_node_create()

roll_node_t * roll_node_create ( mem_heap_t heap)

Creates a rollback command node struct.

Returns
own: rollback node struct
Parameters
heapin: mem heap where created

◆ trx_is_recv()

bool trx_is_recv ( const trx_t trx)

Determines if this transaction is rolling back an incomplete transaction in crash recovery.

Returns
true if trx is an incomplete transaction that is being rolled back in crash recovery
Parameters
trxin: transaction

◆ trx_recovery_rollback()

void trx_recovery_rollback ( THD thd)

Rollback or clean up any incomplete transactions which were encountered in crash recovery.

If the transaction already was committed, then we clean up a possible insert undo log. If the transaction was not yet committed, then we roll it back. Note: this is done in a background thread.

◆ trx_recovery_rollback_thread()

void trx_recovery_rollback_thread ( )

Rollback or clean up any incomplete transactions which were encountered in crash recovery.

If the transaction already was committed, then we clean up a possible insert undo log. If the transaction was not yet committed, then we roll it back. Note: this is done in a background thread.

◆ trx_release_savepoint_for_mysql()

dberr_t trx_release_savepoint_for_mysql ( trx_t trx,
const char *  savepoint_name 
)

Releases only the named savepoint.

Releases a named savepoint.

Savepoints which were set after this savepoint are left as is.

Returns
if no savepoint of the name found then DB_NO_SAVEPOINT, otherwise DB_SUCCESS
Parameters
trxin: transaction handle
savepoint_namein: savepoint name

◆ trx_roll_graph_build()

static que_t * trx_roll_graph_build ( trx_t trx,
bool  partial_rollback 
)
static

Builds an undo 'query' graph for a transaction.

The actual rollback is performed by executing this query graph like a query subprocedure call. The reply about the completion of the rollback will be sent by this graph.

Parameters
[in,out]trxtransaction
[in]partial_rollbacktrue if partial rollback
Returns
the query graph

◆ trx_roll_pop_top_rec()

static const page_t * trx_roll_pop_top_rec ( trx_t trx,
trx_undo_t undo,
mtr_t mtr,
uint32_t *  undo_offset 
)
static

Pops the topmost undo log record in a single undo log and updates the info about the topmost record in the undo log memory struct.

Parameters
[in]trxtransaction
[in]undoundo log
[in]mtrmtr
[out]undo_offsetoffset of undo record in the page
Returns
Undo page where undo log record resides, the page s-latched

◆ trx_roll_pop_top_rec_of_trx()

trx_undo_rec_t * trx_roll_pop_top_rec_of_trx ( trx_t trx,
undo_no_t  limit,
roll_ptr_t roll_ptr,
mem_heap_t heap 
)

Get next undo log record from redo and noredo rollback segments.

Returns
undo log record copied to heap, NULL if none left, or if the undo number of the top record would be less than the limit
Parameters
trxin: transaction
limitin: least undo number we need
roll_ptrout: roll pointer to undo record
heapin: memory heap where copied

◆ trx_roll_pop_top_rec_of_trx_low()

static trx_undo_rec_t * trx_roll_pop_top_rec_of_trx_low ( trx_t trx,
trx_undo_ptr_t undo_ptr,
undo_no_t  limit,
roll_ptr_t roll_ptr,
mem_heap_t heap 
)
static

Pops the topmost record when the two undo logs of a transaction are seen as a single stack of records ordered by their undo numbers.

Returns
undo log record copied to heap, NULL if none left, or if the undo number of the top record would be less than the limit
Parameters
trxin/out: transaction
undo_ptrin: rollback segment to look for next undo log record.
limitin: least undo number we need
roll_ptrout: roll pointer to undo record
heapin/out: memory heap where copied

◆ trx_roll_savepoint_free()

static void trx_roll_savepoint_free ( trx_t trx,
trx_named_savept_t savep 
)
static

Frees a single savepoint struct.

Parameters
trxin: transaction handle
savepin: savepoint to free

◆ trx_roll_savepoints_free()

void trx_roll_savepoints_free ( trx_t trx,
trx_named_savept_t savep 
)

Frees savepoint structs starting from savep.

Parameters
[in]trxTransaction handle
[in]savepFree all savepoints starting with this savepoint i, if savep is nullptr free all save points

◆ trx_roll_try_truncate()

static void trx_roll_try_truncate ( trx_t trx,
trx_undo_ptr_t undo_ptr 
)
static

Tries truncate the undo logs.

Parameters
trxin/out: transaction
undo_ptrin: rollback segment to look for next undo log record.

◆ trx_rollback_active()

static void trx_rollback_active ( trx_t trx)
static

Roll back an active transaction.

Parameters
trxin/out: transaction

◆ trx_rollback_finish()

static void trx_rollback_finish ( trx_t trx)
static

Finishes a transaction rollback.

in: transaction

Parameters
trxin: transaction

◆ trx_rollback_for_mysql()

dberr_t trx_rollback_for_mysql ( trx_t trx)

Rollback a transaction used in MySQL.

Returns
error code or DB_SUCCESS
Parameters
trxin/out: transaction

◆ trx_rollback_for_mysql_low()

static dberr_t trx_rollback_for_mysql_low ( trx_t trx)
static

Rollback a transaction used in MySQL.

Returns
error code or DB_SUCCESS
Parameters
trxin/out: transaction

◆ trx_rollback_last_sql_stat_for_mysql()

dberr_t trx_rollback_last_sql_stat_for_mysql ( trx_t trx)

Rollback the latest SQL statement for MySQL.

Returns
error code or DB_SUCCESS
Parameters
trxin/out: transaction

◆ trx_rollback_low()

static dberr_t trx_rollback_low ( trx_t trx)
static

Rollback a transaction used in MySQL.

Parameters
[in,out]trxtransaction
Returns
error code or DB_SUCCESS

◆ trx_rollback_or_clean_recovered()

void trx_rollback_or_clean_recovered ( bool  all)

Rollback or clean up any incomplete transactions which were encountered in crash recovery.

If the transaction already was committed, then we clean up a possible insert undo log. If the transaction was not yet committed, then we roll it back.

Parameters
allin: false=roll back dictionary transactions; true=roll back all non-PREPARED transactions

◆ trx_rollback_or_clean_resurrected()

static bool trx_rollback_or_clean_resurrected ( trx_t trx,
bool  all 
)
static

Rollback or clean up any resurrected incomplete transactions.

It assumes that the caller holds the trx_sys_t::mutex and it will release the lock if it does a clean up or rollback.

Returns
true if the transaction was cleaned up or rolled back and trx_sys->mutex was released.
Parameters
trxin: transaction to rollback or clean
allin: false=roll back dictionary transactions; true=roll back all non-PREPARED transactions

◆ trx_rollback_start()

static que_thr_t * trx_rollback_start ( trx_t trx,
ib_id_t  roll_limit,
bool  partial_rollback 
)
static

Starts a rollback operation, creates the UNDO graph that will do the actual undo operation.

Parameters
[in]trxtransaction
[in]roll_limitrollback to undo no (for partial undo), 0 if we are rolling back the entire transaction
[in]partial_rollbacktrue if partial rollback
Returns
query graph thread that will perform the UNDO operations.

◆ trx_rollback_step()

que_thr_t * trx_rollback_step ( que_thr_t thr)

Performs an execution step for a rollback command node in a query graph.

Returns
query thread to run next, or NULL
Parameters
thrin: query thread

◆ trx_rollback_to_savepoint()

dberr_t trx_rollback_to_savepoint ( trx_t trx,
trx_savept_t savept 
)

Rollback a transaction to a given savepoint or do a complete rollback.

Returns
error code or DB_SUCCESS
Parameters
trxin: transaction handle
saveptin: pointer to savepoint undo number, if partial rollback requested, or NULL for complete rollback

◆ trx_rollback_to_savepoint_for_mysql()

dberr_t trx_rollback_to_savepoint_for_mysql ( trx_t trx,
const char *  savepoint_name,
int64_t *  mysql_binlog_cache_pos 
)

Rolls back a transaction back to a named savepoint.

Modifications after the savepoint are undone but InnoDB does NOT release the corresponding locks which are stored in memory. If a lock is 'implicit', that is, a new inserted row holds a lock where the lock information is carried by the trx id stored in the row, these locks are naturally released in the rollback. Savepoints which were set after this savepoint are deleted.

Returns
if no savepoint of the name found then DB_NO_SAVEPOINT, otherwise DB_SUCCESS
Parameters
trxin: transaction handle
savepoint_namein: savepoint name
mysql_binlog_cache_posout: the MySQL binlog cache position corresponding to this savepoint; MySQL needs this information to remove the binlog entries of the queries executed after the savepoint

◆ trx_rollback_to_savepoint_for_mysql_low()

static dberr_t trx_rollback_to_savepoint_for_mysql_low ( trx_t trx,
trx_named_savept_t savep,
int64_t *  mysql_binlog_cache_pos 
)
static

Rolls back a transaction back to a named savepoint.

Modifications after the savepoint are undone but InnoDB does NOT release the corresponding locks which are stored in memory. If a lock is 'implicit', that is, a new inserted row holds a lock where the lock information is carried by the trx id stored in the row, these locks are naturally released in the rollback. Savepoints which were set after this savepoint are deleted.

Returns
if no savepoint of the name found then DB_NO_SAVEPOINT, otherwise DB_SUCCESS
Parameters
trxin/out: transaction
savepin/out: savepoint
mysql_binlog_cache_posout: the MySQL binlog cache position corresponding to this savepoint; MySQL needs this information to remove the binlog entries of the queries executed after the savepoint

◆ trx_rollback_to_savepoint_low()

static void trx_rollback_to_savepoint_low ( trx_t trx,
trx_savept_t savept 
)
static

Rollback a transaction used in MySQL.

Parameters
trxin: transaction handle
saveptin: pointer to savepoint undo number, if partial rollback requested, or NULL for complete rollback

◆ trx_savepoint_find()

static trx_named_savept_t * trx_savepoint_find ( trx_t trx,
const char *  name 
)
static

Search for a savepoint using name.

Returns
savepoint if found else NULL
Parameters
trxin: transaction
namein: savepoint name

◆ trx_savepoint_for_mysql()

dberr_t trx_savepoint_for_mysql ( trx_t trx,
const char *  savepoint_name,
int64_t  binlog_cache_pos 
)

Creates a named savepoint.

If the transaction is not yet started, starts it. If there is already a savepoint of the same name, this call erases that old savepoint and replaces it with a new. Savepoints are deleted in a transaction commit or rollback.

Returns
always DB_SUCCESS
Parameters
trxin: transaction handle
savepoint_namein: savepoint name
binlog_cache_posin: MySQL binlog cache position corresponding to this connection at the time of the savepoint

◆ trx_savept_take()

trx_savept_t trx_savept_take ( trx_t trx)

Returns a transaction savepoint taken at this point in time.

Returns
savepoint
Parameters
trxin: transaction

Variable Documentation

◆ trx_roll_crash_recv_trx

const trx_t* trx_roll_crash_recv_trx = nullptr
static

In crash recovery, the current trx to be rolled back; NULL otherwise.

◆ trx_roll_max_undo_no

undo_no_t trx_roll_max_undo_no
static

In crash recovery we set this to the undo n:o of the current trx to be rolled back.

Then we can print how many % the rollback has progressed.

◆ trx_roll_progress_printed_pct

ulint trx_roll_progress_printed_pct
static

Auxiliary variable which tells the previous progress % we printed.

◆ TRX_ROLL_TRUNC_THRESHOLD

const ulint TRX_ROLL_TRUNC_THRESHOLD = 1
static

This many pages must be undone before a truncate is tried within rollback.