1 /*****************************************************************************
2 
3 Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation.  The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24 
25 *****************************************************************************/
26 
27 /**************************************************//**
28 @file include/row0upd.h
29 Update of a row
30 
31 Created 12/27/1996 Heikki Tuuri
32 *******************************************************/
33 
34 #ifndef row0upd_h
35 #define row0upd_h
36 
37 #include "univ.i"
38 #include "data0data.h"
39 #include "row0types.h"
40 #include "btr0types.h"
41 #include "dict0types.h"
42 #include "trx0types.h"
43 
44 #ifndef UNIV_HOTBACKUP
45 # include "btr0pcur.h"
46 # include "que0types.h"
47 # include "pars0types.h"
48 #endif /* !UNIV_HOTBACKUP */
49 
50 /*********************************************************************//**
51 Creates an update vector object.
52 @return	own: update vector object */
53 UNIV_INLINE
54 upd_t*
55 upd_create(
56 /*=======*/
57 	ulint		n,	/*!< in: number of fields */
58 	mem_heap_t*	heap);	/*!< in: heap from which memory allocated */
59 /*********************************************************************//**
60 Returns the number of fields in the update vector == number of columns
61 to be updated by an update vector.
62 @return	number of fields */
63 UNIV_INLINE
64 ulint
65 upd_get_n_fields(
66 /*=============*/
67 	const upd_t*	update);	/*!< in: update vector */
68 #ifdef UNIV_DEBUG
69 /*********************************************************************//**
70 Returns the nth field of an update vector.
71 @return	update vector field */
72 UNIV_INLINE
73 upd_field_t*
74 upd_get_nth_field(
75 /*==============*/
76 	const upd_t*	update,	/*!< in: update vector */
77 	ulint		n);	/*!< in: field position in update vector */
78 #else
79 # define upd_get_nth_field(update, n) ((update)->fields + (n))
80 #endif
81 #ifndef UNIV_HOTBACKUP
82 /*********************************************************************//**
83 Sets an index field number to be updated by an update vector field. */
84 UNIV_INLINE
85 void
86 upd_field_set_field_no(
87 /*===================*/
88 	upd_field_t*	upd_field,	/*!< in: update vector field */
89 	ulint		field_no,	/*!< in: field number in a clustered
90 					index */
91 	dict_index_t*	index,		/*!< in: index */
92 	trx_t*		trx);		/*!< in: transaction */
93 /*********************************************************************//**
94 Returns a field of an update vector by field_no.
95 @return	update vector field, or NULL */
96 UNIV_INLINE
97 const upd_field_t*
98 upd_get_field_by_field_no(
99 /*======================*/
100 	const upd_t*	update,	/*!< in: update vector */
101 	ulint		no)	/*!< in: field_no */
102 	MY_ATTRIBUTE((nonnull, pure));
103 /*********************************************************************//**
104 Writes into the redo log the values of trx id and roll ptr and enough info
105 to determine their positions within a clustered index record.
106 @return	new pointer to mlog */
107 UNIV_INTERN
108 byte*
109 row_upd_write_sys_vals_to_log(
110 /*==========================*/
111 	dict_index_t*	index,	/*!< in: clustered index */
112 	trx_id_t	trx_id,	/*!< in: transaction id */
113 	roll_ptr_t	roll_ptr,/*!< in: roll ptr of the undo log record */
114 	byte*		log_ptr,/*!< pointer to a buffer of size > 20 opened
115 				in mlog */
116 	mtr_t*		mtr);	/*!< in: mtr */
117 /*********************************************************************//**
118 Updates the trx id and roll ptr field in a clustered index record when
119 a row is updated or marked deleted. */
120 UNIV_INLINE
121 void
122 row_upd_rec_sys_fields(
123 /*===================*/
124 	rec_t*		rec,	/*!< in/out: record */
125 	page_zip_des_t*	page_zip,/*!< in/out: compressed page whose
126 				uncompressed part will be updated, or NULL */
127 	dict_index_t*	index,	/*!< in: clustered index */
128 	const ulint*	offsets,/*!< in: rec_get_offsets(rec, index) */
129 	const trx_t*	trx,	/*!< in: transaction */
130 	roll_ptr_t	roll_ptr);/*!< in: roll ptr of the undo log record,
131 				  can be 0 during IMPORT */
132 /*********************************************************************//**
133 Sets the trx id or roll ptr field of a clustered index entry. */
134 UNIV_INTERN
135 void
136 row_upd_index_entry_sys_field(
137 /*==========================*/
138 	dtuple_t*	entry,	/*!< in/out: index entry, where the memory
139 				buffers for sys fields are already allocated:
140 				the function just copies the new values to
141 				them */
142 	dict_index_t*	index,	/*!< in: clustered index */
143 	ulint		type,	/*!< in: DATA_TRX_ID or DATA_ROLL_PTR */
144 	ib_uint64_t	val);	/*!< in: value to write */
145 /*********************************************************************//**
146 Creates an update node for a query graph.
147 @return	own: update node */
148 UNIV_INTERN
149 upd_node_t*
150 upd_node_create(
151 /*============*/
152 	mem_heap_t*	heap);	/*!< in: mem heap where created */
153 /***********************************************************//**
154 Writes to the redo log the new values of the fields occurring in the index. */
155 UNIV_INTERN
156 void
157 row_upd_index_write_log(
158 /*====================*/
159 	const upd_t*	update,	/*!< in: update vector */
160 	byte*		log_ptr,/*!< in: pointer to mlog buffer: must
161 				contain at least MLOG_BUF_MARGIN bytes
162 				of free space; the buffer is closed
163 				within this function */
164 	mtr_t*		mtr);	/*!< in: mtr into whose log to write */
165 /***********************************************************//**
166 Returns TRUE if row update changes size of some field in index or if some
167 field to be updated is stored externally in rec or update.
168 @return TRUE if the update changes the size of some field in index or
169 the field is external in rec or update */
170 UNIV_INTERN
171 ibool
172 row_upd_changes_field_size_or_external(
173 /*===================================*/
174 	dict_index_t*	index,	/*!< in: index */
175 	const ulint*	offsets,/*!< in: rec_get_offsets(rec, index) */
176 	const upd_t*	update);/*!< in: update vector */
177 /***********************************************************//**
178 Returns true if row update contains disowned external fields.
179 @return true if the update contains disowned external fields. */
180 UNIV_INTERN
181 bool
182 row_upd_changes_disowned_external(
183 /*==============================*/
184 	const upd_t*	update)	/*!< in: update vector */
185 	MY_ATTRIBUTE((nonnull, warn_unused_result));
186 #endif /* !UNIV_HOTBACKUP */
187 /***********************************************************//**
188 Replaces the new column values stored in the update vector to the
189 record given. No field size changes are allowed. This function is
190 usually invoked on a clustered index. The only use case for a
191 secondary index is row_ins_sec_index_entry_by_modify() or its
192 counterpart in ibuf_insert_to_index_page(). */
193 UNIV_INTERN
194 void
195 row_upd_rec_in_place(
196 /*=================*/
197 	rec_t*		rec,	/*!< in/out: record where replaced */
198 	dict_index_t*	index,	/*!< in: the index the record belongs to */
199 	const ulint*	offsets,/*!< in: array returned by rec_get_offsets() */
200 	const upd_t*	update,	/*!< in: update vector */
201 	page_zip_des_t*	page_zip);/*!< in: compressed page with enough space
202 				available, or NULL */
203 #ifndef UNIV_HOTBACKUP
204 /***************************************************************//**
205 Builds an update vector from those fields which in a secondary index entry
206 differ from a record that has the equal ordering fields. NOTE: we compare
207 the fields as binary strings!
208 @return	own: update vector of differing fields */
209 UNIV_INTERN
210 upd_t*
211 row_upd_build_sec_rec_difference_binary(
212 /*====================================*/
213 	const rec_t*	rec,	/*!< in: secondary index record */
214 	dict_index_t*	index,	/*!< in: index */
215 	const ulint*	offsets,/*!< in: rec_get_offsets(rec, index) */
216 	const dtuple_t*	entry,	/*!< in: entry to insert */
217 	mem_heap_t*	heap)	/*!< in: memory heap from which allocated */
218 	MY_ATTRIBUTE((warn_unused_result, nonnull));
219 /***************************************************************//**
220 Builds an update vector from those fields, excluding the roll ptr and
221 trx id fields, which in an index entry differ from a record that has
222 the equal ordering fields. NOTE: we compare the fields as binary strings!
223 @return own: update vector of differing fields, excluding roll ptr and
224 trx id */
225 UNIV_INTERN
226 const upd_t*
227 row_upd_build_difference_binary(
228 /*============================*/
229 	dict_index_t*	index,	/*!< in: clustered index */
230 	const dtuple_t*	entry,	/*!< in: entry to insert */
231 	const rec_t*	rec,	/*!< in: clustered index record */
232 	const ulint*	offsets,/*!< in: rec_get_offsets(rec,index), or NULL */
233 	bool		no_sys,	/*!< in: skip the system columns
234 				DB_TRX_ID and DB_ROLL_PTR */
235 	trx_t*		trx,	/*!< in: transaction (for diagnostics),
236 				or NULL */
237 	mem_heap_t*	heap)	/*!< in: memory heap from which allocated */
238 	MY_ATTRIBUTE((nonnull(1,2,3,7), warn_unused_result));
239 /***********************************************************//**
240 Replaces the new column values stored in the update vector to the index entry
241 given. */
242 UNIV_INTERN
243 void
244 row_upd_index_replace_new_col_vals_index_pos(
245 /*=========================================*/
246 	dtuple_t*	entry,	/*!< in/out: index entry where replaced;
247 				the clustered index record must be
248 				covered by a lock or a page latch to
249 				prevent deletion (rollback or purge) */
250 	dict_index_t*	index,	/*!< in: index; NOTE that this may also be a
251 				non-clustered index */
252 	const upd_t*	update,	/*!< in: an update vector built for the index so
253 				that the field number in an upd_field is the
254 				index position */
255 	ibool		order_only,
256 				/*!< in: if TRUE, limit the replacement to
257 				ordering fields of index; note that this
258 				does not work for non-clustered indexes. */
259 	mem_heap_t*	heap);	/*!< in: memory heap for allocating and
260 				copying the new values */
261 /***********************************************************//**
262 Replaces the new column values stored in the update vector to the index entry
263 given. */
264 UNIV_INTERN
265 void
266 row_upd_index_replace_new_col_vals(
267 /*===============================*/
268 	dtuple_t*	entry,	/*!< in/out: index entry where replaced;
269 				the clustered index record must be
270 				covered by a lock or a page latch to
271 				prevent deletion (rollback or purge) */
272 	dict_index_t*	index,	/*!< in: index; NOTE that this may also be a
273 				non-clustered index */
274 	const upd_t*	update,	/*!< in: an update vector built for the
275 				CLUSTERED index so that the field number in
276 				an upd_field is the clustered index position */
277 	mem_heap_t*	heap)	/*!< in: memory heap for allocating and
278 				copying the new values */
279 	MY_ATTRIBUTE((nonnull));
280 /***********************************************************//**
281 Replaces the new column values stored in the update vector. */
282 UNIV_INTERN
283 void
284 row_upd_replace(
285 /*============*/
286 	dtuple_t*		row,	/*!< in/out: row where replaced,
287 					indexed by col_no;
288 					the clustered index record must be
289 					covered by a lock or a page latch to
290 					prevent deletion (rollback or purge) */
291 	row_ext_t**		ext,	/*!< out, own: NULL, or externally
292 					stored column prefixes */
293 	const dict_index_t*	index,	/*!< in: clustered index */
294 	const upd_t*		update,	/*!< in: an update vector built for the
295 					clustered index */
296 	mem_heap_t*		heap);	/*!< in: memory heap */
297 /***********************************************************//**
298 Checks if an update vector changes an ordering field of an index record.
299 
300 This function is fast if the update vector is short or the number of ordering
301 fields in the index is small. Otherwise, this can be quadratic.
302 NOTE: we compare the fields as binary strings!
303 @return TRUE if update vector changes an ordering field in the index record */
304 UNIV_INTERN
305 ibool
306 row_upd_changes_ord_field_binary_func(
307 /*==================================*/
308 	dict_index_t*	index,	/*!< in: index of the record */
309 	const upd_t*	update,	/*!< in: update vector for the row; NOTE: the
310 				field numbers in this MUST be clustered index
311 				positions! */
312 #ifdef UNIV_DEBUG
313 	const que_thr_t*thr,	/*!< in: query thread */
314 #endif /* UNIV_DEBUG */
315 	const dtuple_t*	row,	/*!< in: old value of row, or NULL if the
316 				row and the data values in update are not
317 				known when this function is called, e.g., at
318 				compile time */
319 	const row_ext_t*ext)	/*!< NULL, or prefixes of the externally
320 				stored columns in the old row */
321 	MY_ATTRIBUTE((warn_unused_result));
322 #ifdef UNIV_DEBUG
323 # define row_upd_changes_ord_field_binary(index,update,thr,row,ext)	\
324 	row_upd_changes_ord_field_binary_func(index,update,thr,row,ext)
325 #else /* UNIV_DEBUG */
326 # define row_upd_changes_ord_field_binary(index,update,thr,row,ext)	\
327 	row_upd_changes_ord_field_binary_func(index,update,row,ext)
328 #endif /* UNIV_DEBUG */
329 /***********************************************************//**
330 Checks if an FTS indexed column is affected by an UPDATE.
331 @return offset within fts_t::indexes if FTS indexed column updated else
332 ULINT_UNDEFINED */
333 UNIV_INTERN
334 ulint
335 row_upd_changes_fts_column(
336 /*=======================*/
337 	dict_table_t*	table,		/*!< in: table */
338 	upd_field_t*	upd_field);	/*!< in: field to check */
339 /***********************************************************//**
340 Checks if an FTS Doc ID column is affected by an UPDATE.
341 @return whether Doc ID column is affected */
342 UNIV_INTERN
343 bool
344 row_upd_changes_doc_id(
345 /*===================*/
346 	dict_table_t*	table,		/*!< in: table */
347 	upd_field_t*	upd_field)	/*!< in: field to check */
348 	MY_ATTRIBUTE((nonnull, warn_unused_result));
349 /***********************************************************//**
350 Checks if an update vector changes an ordering field of an index record.
351 This function is fast if the update vector is short or the number of ordering
352 fields in the index is small. Otherwise, this can be quadratic.
353 NOTE: we compare the fields as binary strings!
354 @return TRUE if update vector may change an ordering field in an index
355 record */
356 UNIV_INTERN
357 ibool
358 row_upd_changes_some_index_ord_field_binary(
359 /*========================================*/
360 	const dict_table_t*	table,	/*!< in: table */
361 	const upd_t*		update);/*!< in: update vector for the row */
362 /***********************************************************//**
363 Updates a row in a table. This is a high-level function used
364 in SQL execution graphs.
365 @return	query thread to run next or NULL */
366 UNIV_INTERN
367 que_thr_t*
368 row_upd_step(
369 /*=========*/
370 	que_thr_t*	thr);	/*!< in: query thread */
371 #endif /* !UNIV_HOTBACKUP */
372 /*********************************************************************//**
373 Parses the log data of system field values.
374 @return	log data end or NULL */
375 UNIV_INTERN
376 byte*
377 row_upd_parse_sys_vals(
378 /*===================*/
379 	byte*		ptr,	/*!< in: buffer */
380 	byte*		end_ptr,/*!< in: buffer end */
381 	ulint*		pos,	/*!< out: TRX_ID position in record */
382 	trx_id_t*	trx_id,	/*!< out: trx id */
383 	roll_ptr_t*	roll_ptr);/*!< out: roll ptr */
384 /*********************************************************************//**
385 Updates the trx id and roll ptr field in a clustered index record in database
386 recovery. */
387 UNIV_INTERN
388 void
389 row_upd_rec_sys_fields_in_recovery(
390 /*===============================*/
391 	rec_t*		rec,	/*!< in/out: record */
392 	page_zip_des_t*	page_zip,/*!< in/out: compressed page, or NULL */
393 	const ulint*	offsets,/*!< in: array returned by rec_get_offsets() */
394 	ulint		pos,	/*!< in: TRX_ID position in rec */
395 	trx_id_t	trx_id,	/*!< in: transaction id */
396 	roll_ptr_t	roll_ptr);/*!< in: roll ptr of the undo log record */
397 /*********************************************************************//**
398 Parses the log data written by row_upd_index_write_log.
399 @return	log data end or NULL */
400 UNIV_INTERN
401 byte*
402 row_upd_index_parse(
403 /*================*/
404 	byte*		ptr,	/*!< in: buffer */
405 	byte*		end_ptr,/*!< in: buffer end */
406 	mem_heap_t*	heap,	/*!< in: memory heap where update vector is
407 				built */
408 	upd_t**		update_out);/*!< out: update vector */
409 
410 
411 /* Update vector field */
412 struct upd_field_t{
413 	unsigned	field_no:16;	/*!< field number in an index, usually
414 					the clustered index, but in updating
415 					a secondary index record in btr0cur.cc
416 					this is the position in the secondary
417 					index */
418 #ifndef UNIV_HOTBACKUP
419 	unsigned	orig_len:16;	/*!< original length of the locally
420 					stored part of an externally stored
421 					column, or 0 */
422 	que_node_t*	exp;		/*!< expression for calculating a new
423 					value: it refers to column values and
424 					constants in the symbol table of the
425 					query graph */
426 #endif /* !UNIV_HOTBACKUP */
427 	dfield_t	new_val;	/*!< new value for the column */
428 };
429 
430 /* Update vector structure */
431 struct upd_t{
432 	ulint		info_bits;	/*!< new value of info bits to record;
433 					default is 0 */
434 	ulint		n_fields;	/*!< number of update fields */
435 	upd_field_t*	fields;		/*!< array of update fields */
436 };
437 
438 #ifndef UNIV_HOTBACKUP
439 /* Update node structure which also implements the delete operation
440 of a row */
441 
442 struct upd_node_t{
443 	que_common_t	common;	/*!< node type: QUE_NODE_UPDATE */
444 	ibool		is_delete;/* TRUE if delete, FALSE if update */
445 	ibool		searched_update;
446 				/* TRUE if searched update, FALSE if
447 				positioned */
448 	ibool		in_mysql_interface;
449 				/* TRUE if the update node was created
450 				for the MySQL interface */
451 	dict_foreign_t*	foreign;/* NULL or pointer to a foreign key
452 				constraint if this update node is used in
453 				doing an ON DELETE or ON UPDATE operation */
454 	upd_node_t*	cascade_node;/* NULL or an update node template which
455 				is used to implement ON DELETE/UPDATE CASCADE
456 				or ... SET NULL for foreign keys */
457 	mem_heap_t*	cascade_heap;/* NULL or a mem heap where the cascade
458 				node is created */
459 	sel_node_t*	select;	/*!< query graph subtree implementing a base
460 				table cursor: the rows returned will be
461 				updated */
462 	btr_pcur_t*	pcur;	/*!< persistent cursor placed on the clustered
463 				index record which should be updated or
464 				deleted; the cursor is stored in the graph
465 				of 'select' field above, except in the case
466 				of the MySQL interface */
467 	dict_table_t*	table;	/*!< table where updated */
468 	upd_t*		update;	/*!< update vector for the row */
469 	ulint		update_n_fields;
470 				/* when this struct is used to implement
471 				a cascade operation for foreign keys, we store
472 				here the size of the buffer allocated for use
473 				as the update vector */
474 	sym_node_list_t	columns;/* symbol table nodes for the columns
475 				to retrieve from the table */
476 	ibool		has_clust_rec_x_lock;
477 				/* TRUE if the select which retrieves the
478 				records to update already sets an x-lock on
479 				the clustered record; note that it must always
480 				set at least an s-lock */
481 	ulint		cmpl_info;/* information extracted during query
482 				compilation; speeds up execution:
483 				UPD_NODE_NO_ORD_CHANGE and
484 				UPD_NODE_NO_SIZE_CHANGE, ORed */
485 	/*----------------------*/
486 	/* Local storage for this graph node */
487 	ulint		state;	/*!< node execution state */
488 	dict_index_t*	index;	/*!< NULL, or the next index whose record should
489 				be updated */
490 	dtuple_t*	row;	/*!< NULL, or a copy (also fields copied to
491 				heap) of the row to update; this must be reset
492 				to NULL after a successful update */
493 	row_ext_t*	ext;	/*!< NULL, or prefixes of the externally
494 				stored columns in the old row */
495 	dtuple_t*	upd_row;/* NULL, or a copy of the updated row */
496 	row_ext_t*	upd_ext;/* NULL, or prefixes of the externally
497 				stored columns in upd_row */
498 	mem_heap_t*	heap;	/*!< memory heap used as auxiliary storage;
499 				this must be emptied after a successful
500 				update */
501 	/*----------------------*/
502 	sym_node_t*	table_sym;/* table node in symbol table */
503 	que_node_t*	col_assign_list;
504 				/* column assignment list */
505 	ulint		magic_n;
506 };
507 
508 #define	UPD_NODE_MAGIC_N	1579975
509 
510 /* Node execution states */
511 #define UPD_NODE_SET_IX_LOCK	   1	/* execution came to the node from
512 					a node above and if the field
513 					has_clust_rec_x_lock is FALSE, we
514 					should set an intention x-lock on
515 					the table */
516 #define UPD_NODE_UPDATE_CLUSTERED  2	/* clustered index record should be
517 					updated */
518 #define UPD_NODE_INSERT_CLUSTERED  3	/* clustered index record should be
519 					inserted, old record is already delete
520 					marked */
521 #define UPD_NODE_INSERT_BLOB	   4	/* clustered index record should be
522 					inserted, old record is already
523 					delete-marked; non-updated BLOBs
524 					should be inherited by the new record
525 					and disowned by the old record */
526 #define UPD_NODE_UPDATE_ALL_SEC	   5	/* an ordering field of the clustered
527 					index record was changed, or this is
528 					a delete operation: should update
529 					all the secondary index records */
530 #define UPD_NODE_UPDATE_SOME_SEC   6	/* secondary index entries should be
531 					looked at and updated if an ordering
532 					field changed */
533 
534 /* Compilation info flags: these must fit within 3 bits; see trx0rec.h */
535 #define UPD_NODE_NO_ORD_CHANGE	1	/* no secondary index record will be
536 					changed in the update and no ordering
537 					field of the clustered index */
538 #define UPD_NODE_NO_SIZE_CHANGE	2	/* no record field size will be
539 					changed in the update */
540 
541 #endif /* !UNIV_HOTBACKUP */
542 
543 #ifndef UNIV_NONINL
544 #include "row0upd.ic"
545 #endif
546 
547 #endif
548