1 /*****************************************************************************
2 
3 Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2017, 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/trx0rec.h
22 Transaction undo log record
23 
24 Created 3/26/1996 Heikki Tuuri
25 *******************************************************/
26 
27 #ifndef trx0rec_h
28 #define trx0rec_h
29 
30 #include "trx0types.h"
31 #include "row0types.h"
32 #include "mtr0mtr.h"
33 #include "rem0types.h"
34 #include "page0types.h"
35 #include "row0log.h"
36 #include "que0types.h"
37 
38 /***********************************************************************//**
39 Copies the undo record to the heap.
40 @return own: copy of undo log record */
41 UNIV_INLINE
42 trx_undo_rec_t*
43 trx_undo_rec_copy(
44 /*==============*/
45 	const trx_undo_rec_t*	undo_rec,	/*!< in: undo log record */
46 	mem_heap_t*		heap);		/*!< in: heap where copied */
47 /**********************************************************************//**
48 Reads the undo log record type.
49 @return record type */
50 UNIV_INLINE
51 ulint
52 trx_undo_rec_get_type(
53 /*==================*/
54 	const trx_undo_rec_t*	undo_rec);	/*!< in: undo log record */
55 /**********************************************************************//**
56 Reads the undo log record number.
57 @return undo no */
58 UNIV_INLINE
59 undo_no_t
60 trx_undo_rec_get_undo_no(
61 /*=====================*/
62 	const trx_undo_rec_t*	undo_rec);	/*!< in: undo log record */
63 
64 /**********************************************************************//**
65 Returns the start of the undo record data area. */
66 #define trx_undo_rec_get_ptr(undo_rec, undo_no)		\
67 	((undo_rec) + trx_undo_rec_get_offset(undo_no))
68 
69 /**********************************************************************//**
70 Reads from an undo log record the general parameters.
71 @return remaining part of undo log record after reading these values */
72 byte*
73 trx_undo_rec_get_pars(
74 /*==================*/
75 	trx_undo_rec_t*	undo_rec,	/*!< in: undo log record */
76 	ulint*		type,		/*!< out: undo record type:
77 					TRX_UNDO_INSERT_REC, ... */
78 	ulint*		cmpl_info,	/*!< out: compiler info, relevant only
79 					for update type records */
80 	bool*		updated_extern,	/*!< out: true if we updated an
81 					externally stored fild */
82 	undo_no_t*	undo_no,	/*!< out: undo log record number */
83 	table_id_t*	table_id)	/*!< out: table id */
84 	MY_ATTRIBUTE((nonnull));
85 /*******************************************************************//**
86 Builds a row reference from an undo log record.
87 @return pointer to remaining part of undo record */
88 byte*
89 trx_undo_rec_get_row_ref(
90 /*=====================*/
91 	byte*		ptr,	/*!< in: remaining part of a copy of an undo log
92 				record, at the start of the row reference;
93 				NOTE that this copy of the undo log record must
94 				be preserved as long as the row reference is
95 				used, as we do NOT copy the data in the
96 				record! */
97 	dict_index_t*	index,	/*!< in: clustered index */
98 	const dtuple_t**ref,	/*!< out, own: row reference */
99 	mem_heap_t*	heap);	/*!< in: memory heap from which the memory
100 				needed is allocated */
101 /**********************************************************************//**
102 Reads from an undo log update record the system field values of the old
103 version.
104 @return remaining part of undo log record after reading these values */
105 byte*
106 trx_undo_update_rec_get_sys_cols(
107 /*=============================*/
108 	const byte*	ptr,		/*!< in: remaining part of undo
109 					log record after reading
110 					general parameters */
111 	trx_id_t*	trx_id,		/*!< out: trx id */
112 	roll_ptr_t*	roll_ptr,	/*!< out: roll ptr */
113 	byte*		info_bits);	/*!< out: info bits state */
114 /*******************************************************************//**
115 Builds an update vector based on a remaining part of an undo log record.
116 @return remaining part of the record, NULL if an error detected, which
117 means that the record is corrupted */
118 byte*
119 trx_undo_update_rec_get_update(
120 /*===========================*/
121 	const byte*	ptr,	/*!< in: remaining part in update undo log
122 				record, after reading the row reference
123 				NOTE that this copy of the undo log record must
124 				be preserved as long as the update vector is
125 				used, as we do NOT copy the data in the
126 				record! */
127 	dict_index_t*	index,	/*!< in: clustered index */
128 	ulint		type,	/*!< in: TRX_UNDO_UPD_EXIST_REC,
129 				TRX_UNDO_UPD_DEL_REC, or
130 				TRX_UNDO_DEL_MARK_REC; in the last case,
131 				only trx id and roll ptr fields are added to
132 				the update vector */
133 	trx_id_t	trx_id,	/*!< in: transaction id from this undorecord */
134 	roll_ptr_t	roll_ptr,/*!< in: roll pointer from this undo record */
135 	byte		info_bits,/*!< in: info bits from this undo record */
136 	mem_heap_t*	heap,	/*!< in: memory heap from which the memory
137 				needed is allocated */
138 	upd_t**		upd);	/*!< out, own: update vector */
139 /*******************************************************************//**
140 Builds a partial row from an update undo log record, for purge.
141 It contains the columns which occur as ordering in any index of the table.
142 Any missing columns are indicated by col->mtype == DATA_MISSING.
143 @return pointer to remaining part of undo record */
144 byte*
145 trx_undo_rec_get_partial_row(
146 /*=========================*/
147 	const byte*	ptr,	/*!< in: remaining part in update undo log
148 				record of a suitable type, at the start of
149 				the stored index columns;
150 				NOTE that this copy of the undo log record must
151 				be preserved as long as the partial row is
152 				used, as we do NOT copy the data in the
153 				record! */
154 	dict_index_t*	index,	/*!< in: clustered index */
155 	const upd_t*	update,	/*!< in: updated columns */
156 	dtuple_t**	row,	/*!< out, own: partial row */
157 	ibool		ignore_prefix, /*!< in: flag to indicate if we
158 				expect blob prefixes in undo. Used
159 				only in the assertion. */
160 	mem_heap_t*	heap)	/*!< in: memory heap from which the memory
161 				needed is allocated */
162 	MY_ATTRIBUTE((nonnull, warn_unused_result));
163 /** Report a RENAME TABLE operation.
164 @param[in,out]	trx	transaction
165 @param[in]	table	table that is being renamed
166 @return	DB_SUCCESS or error code */
167 dberr_t trx_undo_report_rename(trx_t* trx, const dict_table_t* table)
168 	MY_ATTRIBUTE((nonnull, warn_unused_result));
169 /***********************************************************************//**
170 Writes information to an undo log about an insert, update, or a delete marking
171 of a clustered index record. This information is used in a rollback of the
172 transaction and in consistent reads that must look to the history of this
173 transaction.
174 @return DB_SUCCESS or error code */
175 dberr_t
176 trx_undo_report_row_operation(
177 /*==========================*/
178 	que_thr_t*	thr,		/*!< in: query thread */
179 	dict_index_t*	index,		/*!< in: clustered index */
180 	const dtuple_t*	clust_entry,	/*!< in: in the case of an insert,
181 					index entry to insert into the
182 					clustered index; in updates,
183 					may contain a clustered index
184 					record tuple that also contains
185 					virtual columns of the table;
186 					otherwise, NULL */
187 	const upd_t*	update,		/*!< in: in the case of an update,
188 					the update vector, otherwise NULL */
189 	ulint		cmpl_info,	/*!< in: compiler info on secondary
190 					index updates */
191 	const rec_t*	rec,		/*!< in: case of an update or delete
192 					marking, the record in the clustered
193 					index; NULL if insert */
194 	const rec_offs*	offsets,	/*!< in: rec_get_offsets(rec) */
195 	roll_ptr_t*	roll_ptr)	/*!< out: DB_ROLL_PTR to the
196 					undo log record */
197 	MY_ATTRIBUTE((nonnull(1,2,8), warn_unused_result));
198 
199 /** status bit used for trx_undo_prev_version_build() */
200 
201 /** TRX_UNDO_PREV_IN_PURGE tells trx_undo_prev_version_build() that it
202 is being called purge view and we would like to get the purge record
203 even it is in the purge view (in normal case, it will return without
204 fetching the purge record */
205 #define		TRX_UNDO_PREV_IN_PURGE		0x1
206 
207 /** This tells trx_undo_prev_version_build() to fetch the old value in
208 the undo log (which is the after image for an update) */
209 #define		TRX_UNDO_GET_OLD_V_VALUE	0x2
210 
211 /*******************************************************************//**
212 Build a previous version of a clustered index record. The caller must
213 hold a latch on the index page of the clustered index record.
214 @retval true if previous version was built, or if it was an insert
215 or the table has been rebuilt
216 @retval false if the previous version is earlier than purge_view,
217 which means that it may have been removed */
218 bool
219 trx_undo_prev_version_build(
220 /*========================*/
221 	const rec_t*	index_rec,/*!< in: clustered index record in the
222 				index tree */
223 	mtr_t*		index_mtr,/*!< in: mtr which contains the latch to
224 				index_rec page and purge_view */
225 	const rec_t*	rec,	/*!< in: version of a clustered index record */
226 	dict_index_t*	index,	/*!< in: clustered index */
227 	rec_offs*	offsets,/*!< in/out: rec_get_offsets(rec, index) */
228 	mem_heap_t*	heap,	/*!< in: memory heap from which the memory
229 				needed is allocated */
230 	rec_t**		old_vers,/*!< out, own: previous version, or NULL if
231 				rec is the first inserted version, or if
232 				history data has been deleted */
233 	mem_heap_t*	v_heap,	/* !< in: memory heap used to create vrow
234 				dtuple if it is not yet created. This heap
235 				diffs from "heap" above in that it could be
236 				prebuilt->old_vers_heap for selection */
237 	dtuple_t**	vrow,	/*!< out: virtual column info, if any */
238 	ulint		v_status);
239 				/*!< in: status determine if it is going
240 				into this function by purge thread or not.
241 				And if we read "after image" of undo log */
242 
243 /** Read from an undo log record a non-virtual column value.
244 @param[in,out]	ptr		pointer to remaining part of the undo record
245 @param[in,out]	field		stored field
246 @param[in,out]	len		length of the field, or UNIV_SQL_NULL
247 @param[in,out]	orig_len	original length of the locally stored part
248 of an externally stored column, or 0
249 @return remaining part of undo log record after reading these values */
250 byte *trx_undo_rec_get_col_val(const byte *ptr, const byte **field,
251                                uint32_t *len, uint32_t *orig_len);
252 
253 /** Read virtual column value from undo log
254 @param[in]	table		the table
255 @param[in]	ptr		undo log pointer
256 @param[in,out]	row		the dtuple to fill
257 @param[in]	in_purge	whether this is called by purge */
258 void
259 trx_undo_read_v_cols(
260 	const dict_table_t*	table,
261 	const byte*		ptr,
262 	dtuple_t*		row,
263 	bool			in_purge);
264 
265 /** Read virtual column index from undo log if the undo log contains such
266 info, and verify the column is still indexed, and output its position
267 @param[in]	table		the table
268 @param[in]	ptr		undo log pointer
269 @param[in]	first_v_col	if this is the first virtual column, which
270 				has the version marker
271 @param[in,out]	is_undo_log	his function is used to parse both undo log,
272 				and online log for virtual columns. So
273 				check to see if this is undo log
274 @param[out]	field_no	the column number, or FIL_NULL if not indexed
275 @return remaining part of undo log record after reading these values */
276 const byte*
277 trx_undo_read_v_idx(
278 	const dict_table_t*	table,
279 	const byte*		ptr,
280 	bool			first_v_col,
281 	bool*			is_undo_log,
282 	uint32_t*		field_no);
283 
284 /* Types of an undo log record: these have to be smaller than 16, as the
285 compilation info multiplied by 16 is ORed to this value in an undo log
286 record */
287 
288 #define	TRX_UNDO_RENAME_TABLE	9	/*!< RENAME TABLE */
289 #define	TRX_UNDO_INSERT_METADATA 10	/*!< insert a metadata
290 					pseudo-record for instant ALTER */
291 #define	TRX_UNDO_INSERT_REC	11	/* fresh insert into clustered index */
292 #define	TRX_UNDO_UPD_EXIST_REC	12	/* update of a non-delete-marked
293 					record */
294 #define	TRX_UNDO_UPD_DEL_REC	13	/* update of a delete marked record to
295 					a not delete marked record; also the
296 					fields of the record can change */
297 #define	TRX_UNDO_DEL_MARK_REC	14	/* delete marking of a record; fields
298 					do not change */
299 #define	TRX_UNDO_CMPL_INFO_MULT	16U	/* compilation info is multiplied by
300 					this and ORed to the type above */
301 #define	TRX_UNDO_UPD_EXTERN	128U	/* This bit can be ORed to type_cmpl
302 					to denote that we updated external
303 					storage fields: used by purge to
304 					free the external storage */
305 
306 /** The search tuple corresponding to TRX_UNDO_INSERT_METADATA */
307 extern const dtuple_t trx_undo_metadata;
308 
309 /** Read the table id from an undo log record.
310 @param[in]      rec        Undo log record
311 @return table id stored as a part of undo log record */
trx_undo_rec_get_table_id(const trx_undo_rec_t * rec)312 inline table_id_t trx_undo_rec_get_table_id(const trx_undo_rec_t *rec)
313 {
314   rec+= 3;
315   mach_read_next_much_compressed(&rec);
316   return mach_read_next_much_compressed(&rec);
317 }
318 
319 #include "trx0rec.inl"
320 
321 #endif /* trx0rec_h */
322