1/*****************************************************************************
2
3Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
4Copyright (c) 2015, 2018, MariaDB Corporation.
5
6This program is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free Software
8Foundation; version 2 of the License.
9
10This program is distributed in the hope that it will be useful, but WITHOUT
11ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program; if not, write to the Free Software Foundation, Inc.,
1651 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17
18*****************************************************************************/
19
20/**************************************************//**
21@file include/trx0roll.h
22Transaction rollback
23
24Created 3/26/1996 Heikki Tuuri
25*******************************************************/
26
27#ifndef trx0roll_h
28#define trx0roll_h
29
30#include "univ.i"
31#include "trx0trx.h"
32#include "trx0types.h"
33#include "mtr0mtr.h"
34#include "trx0sys.h"
35
36extern bool trx_rollback_is_active;
37extern const trx_t* trx_roll_crash_recv_trx;
38
39/*******************************************************************//**
40Determines if this transaction is rolling back an incomplete transaction
41in crash recovery.
42@return TRUE if trx is an incomplete transaction that is being rolled
43back in crash recovery */
44ibool
45trx_is_recv(
46/*========*/
47 const trx_t* trx); /*!< in: transaction */
48/*******************************************************************//**
49Returns a transaction savepoint taken at this point in time.
50@return savepoint */
51trx_savept_t
52trx_savept_take(
53/*============*/
54 trx_t* trx); /*!< in: transaction */
55
56/** Get the last undo log record of a transaction (for rollback).
57@param[in,out] trx transaction
58@param[out] roll_ptr DB_ROLL_PTR to the undo record
59@param[in,out] heap memory heap for allocation
60@return undo log record copied to heap
61@retval NULL if none left or the roll_limit (savepoint) was reached */
62trx_undo_rec_t*
63trx_roll_pop_top_rec_of_trx(trx_t* trx, roll_ptr_t* roll_ptr, mem_heap_t* heap)
64 MY_ATTRIBUTE((nonnull, warn_unused_result));
65
66/** Report progress when rolling back a row of a recovered transaction. */
67void trx_roll_report_progress();
68/*******************************************************************//**
69Rollback or clean up any incomplete transactions which were
70encountered in crash recovery. If the transaction already was
71committed, then we clean up a possible insert undo log. If the
72transaction was not yet committed, then we roll it back.
73@param all true=roll back all recovered active transactions;
74false=roll back any incomplete dictionary transaction */
75void
76trx_rollback_recovered(bool all);
77/*******************************************************************//**
78Rollback or clean up any incomplete transactions which were
79encountered in crash recovery. If the transaction already was
80committed, then we clean up a possible insert undo log. If the
81transaction was not yet committed, then we roll it back.
82Note: this is done in a background thread.
83@return a dummy parameter */
84extern "C"
85os_thread_ret_t
86DECLARE_THREAD(trx_rollback_all_recovered)(void*);
87/*********************************************************************//**
88Creates a rollback command node struct.
89@return own: rollback node struct */
90roll_node_t*
91roll_node_create(
92/*=============*/
93 mem_heap_t* heap); /*!< in: mem heap where created */
94/***********************************************************//**
95Performs an execution step for a rollback command node in a query graph.
96@return query thread to run next, or NULL */
97que_thr_t*
98trx_rollback_step(
99/*==============*/
100 que_thr_t* thr); /*!< in: query thread */
101/*******************************************************************//**
102Rollback a transaction used in MySQL.
103@return error code or DB_SUCCESS */
104dberr_t
105trx_rollback_for_mysql(
106/*===================*/
107 trx_t* trx) /*!< in/out: transaction */
108 MY_ATTRIBUTE((nonnull));
109/*******************************************************************//**
110Rollback the latest SQL statement for MySQL.
111@return error code or DB_SUCCESS */
112dberr_t
113trx_rollback_last_sql_stat_for_mysql(
114/*=================================*/
115 trx_t* trx) /*!< in/out: transaction */
116 MY_ATTRIBUTE((nonnull));
117/*******************************************************************//**
118Rollback a transaction to a given savepoint or do a complete rollback.
119@return error code or DB_SUCCESS */
120dberr_t
121trx_rollback_to_savepoint(
122/*======================*/
123 trx_t* trx, /*!< in: transaction handle */
124 trx_savept_t* savept) /*!< in: pointer to savepoint undo number, if
125 partial rollback requested, or NULL for
126 complete rollback */
127 MY_ATTRIBUTE((nonnull(1)));
128/*******************************************************************//**
129Rolls back a transaction back to a named savepoint. Modifications after the
130savepoint are undone but InnoDB does NOT release the corresponding locks
131which are stored in memory. If a lock is 'implicit', that is, a new inserted
132row holds a lock where the lock information is carried by the trx id stored in
133the row, these locks are naturally released in the rollback. Savepoints which
134were set after this savepoint are deleted.
135@return if no savepoint of the name found then DB_NO_SAVEPOINT,
136otherwise DB_SUCCESS */
137dberr_t
138trx_rollback_to_savepoint_for_mysql(
139/*================================*/
140 trx_t* trx, /*!< in: transaction handle */
141 const char* savepoint_name, /*!< in: savepoint name */
142 int64_t* mysql_binlog_cache_pos) /*!< out: the MySQL binlog cache
143 position corresponding to this
144 savepoint; MySQL needs this
145 information to remove the
146 binlog entries of the queries
147 executed after the savepoint */
148 MY_ATTRIBUTE((nonnull, warn_unused_result));
149/*******************************************************************//**
150Creates a named savepoint. If the transaction is not yet started, starts it.
151If there is already a savepoint of the same name, this call erases that old
152savepoint and replaces it with a new. Savepoints are deleted in a transaction
153commit or rollback.
154@return always DB_SUCCESS */
155dberr_t
156trx_savepoint_for_mysql(
157/*====================*/
158 trx_t* trx, /*!< in: transaction handle */
159 const char* savepoint_name, /*!< in: savepoint name */
160 int64_t binlog_cache_pos) /*!< in: MySQL binlog cache
161 position corresponding to this
162 connection at the time of the
163 savepoint */
164 MY_ATTRIBUTE((nonnull));
165/*******************************************************************//**
166Releases a named savepoint. Savepoints which
167were set after this savepoint are deleted.
168@return if no savepoint of the name found then DB_NO_SAVEPOINT,
169otherwise DB_SUCCESS */
170dberr_t
171trx_release_savepoint_for_mysql(
172/*============================*/
173 trx_t* trx, /*!< in: transaction handle */
174 const char* savepoint_name) /*!< in: savepoint name */
175 MY_ATTRIBUTE((nonnull, warn_unused_result));
176/*******************************************************************//**
177Frees savepoint structs starting from savep. */
178void
179trx_roll_savepoints_free(
180/*=====================*/
181 trx_t* trx, /*!< in: transaction handle */
182 trx_named_savept_t* savep); /*!< in: free all savepoints > this one;
183 if this is NULL, free all savepoints
184 of trx */
185/** Rollback node states */
186enum roll_node_state {
187 ROLL_NODE_NONE = 0, /*!< Unknown state */
188 ROLL_NODE_SEND, /*!< about to send a rollback signal to
189 the transaction */
190 ROLL_NODE_WAIT /*!< rollback signal sent to the
191 transaction, waiting for completion */
192};
193
194/** Rollback command node in a query graph */
195struct roll_node_t{
196 que_common_t common; /*!< node type: QUE_NODE_ROLLBACK */
197 enum roll_node_state state; /*!< node execution state */
198 bool partial;/*!< TRUE if we want a partial
199 rollback */
200 trx_savept_t savept; /*!< savepoint to which to
201 roll back, in the case of a
202 partial rollback */
203 que_thr_t* undo_thr;/*!< undo query graph */
204};
205
206/** A savepoint set with SQL's "SAVEPOINT savepoint_id" command */
207struct trx_named_savept_t{
208 char* name; /*!< savepoint name */
209 trx_savept_t savept; /*!< the undo number corresponding to
210 the savepoint */
211 int64_t mysql_binlog_cache_pos;
212 /*!< the MySQL binlog cache position
213 corresponding to this savepoint, not
214 defined if the MySQL binlogging is not
215 enabled */
216 UT_LIST_NODE_T(trx_named_savept_t)
217 trx_savepoints; /*!< the list of savepoints of a
218 transaction */
219};
220
221#endif
222