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