Debian Codegraph @

storage/innobase/row/row0upd.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1996, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work 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 either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file row/row0upd.cc
29 Update of a row
30
31 Created 12/27/1996 Heikki Tuuri
32 *******************************************************/
33
34 #include <sys/types.h>
35
36 #include "dict0dict.h"
37 #include "ha_prototypes.h"
38
39 #include "my_byteorder.h"
40 #include "my_dbug.h"
41
42 #include "rem0rec.h"
43 #include "row0upd.h"
44 #include "trx0undo.h"
45 #ifndef UNIV_HOTBACKUP
46 #include <algorithm>
47
48 #include <debug_sync.h>
49 #include "btr0btr.h"
50 #include "btr0cur.h"
51 #include "buf0lru.h"
52 #include "current_thd.h"
53 #include "dict0boot.h"
54 #include "dict0crea.h"
55 #include "eval0eval.h"
56 #include "fts0fts.h"
57 #include "fts0types.h"
58 #include "lob0lob.h"
59 #include "lock0lock.h"
60 #include "log0chkp.h"
61 #include "mach0data.h"
62 #include "pars0sym.h"
63 #include "que0que.h"
64 #include "rem0cmp.h"
65 #endif /* !UNIV_HOTBACKUP */
66 #include "row0ext.h"
67 #include "row0ins.h"
68 #include "row0log.h"
69 #ifndef UNIV_HOTBACKUP
70 #include "fts0fts.h"
71 #include "fts0types.h"
72 #include "row0row.h"
73 #include "row0sel.h"
74 #include "trx0rec.h"
75 #endif /* !UNIV_HOTBACKUP */
76 #include <algorithm>
77 #include "lob0lob.h"
78 #ifndef UNIV_HOTBACKUP
79 #include "current_thd.h"
80 #include "dict0dd.h"
81 #endif /* !UNIV_HOTBACKUP */
82
83 #ifndef UNIV_HOTBACKUP
84 /* What kind of latch and lock can we assume when the control comes to
85 -------------------------------------------------------------------
86 an update node?
87 --------------
88 Efficiency of massive updates would require keeping an x-latch on a
89 clustered index page through many updates, and not setting an explicit
90 x-lock on clustered index records, as they anyway will get an implicit
91 x-lock when they are updated. A problem is that the read nodes in the
92 graph should know that they must keep the latch when passing the control
93 up to the update node, and not set any record lock on the record which
94 will be updated. Another problem occurs if the execution is stopped,
95 as the kernel switches to another query thread, or the transaction must
96 wait for a lock. Then we should be able to release the latch and, maybe,
97 acquire an explicit x-lock on the record.
98 Because this seems too complicated, we conclude that the less
99 efficient solution of releasing all the latches when the control is
100 transferred to another node, and acquiring explicit x-locks, is better. */
101
102 /* How is a delete performed? If there is a delete without an
103 explicit cursor, i.e., a searched delete, there are at least
104 two different situations:
105 the implicit select cursor may run on (1) the clustered index or
106 on (2) a secondary index. The delete is performed by setting
107 the delete bit in the record and substituting the id of the
108 deleting transaction for the original trx id, and substituting a
109 new roll ptr for previous roll ptr. The old trx id and roll ptr
110 are saved in the undo log record. Thus, no physical changes occur
111 in the index tree structure at the time of the delete. Only
112 when the undo log is purged, the index records will be physically
113 deleted from the index trees.
114
115 The query graph executing a searched delete would consist of
116 a delete node which has as a subtree a select subgraph.
117 The select subgraph should return a (persistent) cursor
118 in the clustered index, placed on page which is x-latched.
119 The delete node should look for all secondary index records for
120 this clustered index entry and mark them as deleted. When is
121 the x-latch freed? The most efficient way for performing a
122 searched delete is obviously to keep the x-latch for several
123 steps of query graph execution. */
124
125 /*************************************************************************
126 IMPORTANT NOTE: Any operation that generates redo MUST check that there
127 is enough space in the redo log before for that operation. This is
128 done by calling log_free_check(). The reason for checking the
129 availability of the redo log space before the start of the operation is
130 that we MUST not hold any synchronization objects when performing the
131 check.
132 If you make a change in this module make sure that no codepath is
133 introduced where a call to log_free_check() is bypassed. */
134
135 static_assert(DATA_TRX_ID + 1 == DATA_ROLL_PTR, "DATA_TRX_ID invalid value!");
136
137 /** Checks if an update vector changes some of the first ordering fields of an
138 index record. This is only used in foreign key checks and we can assume
139 that index does not contain column prefixes.
140 @return true if changes */
141 static bool row_upd_changes_first_fields_binary(
142 dtuple_t *entry, /*!< in: old value of index entry */
143 dict_index_t *index, /*!< in: index of entry */
144 const upd_t *update, /*!< in: update vector for the row */
145 ulint n); /*!< in: how many first fields to check */
146
147 /** Checks if index currently is mentioned as a referenced index in a foreign
148 key constraint.
149
150 NOTE that since we do not hold dict_operation_lock when leaving the
151 function, it may be that the referencing table has been dropped when
152 we leave this function: this function is only for heuristic use!
153
154 @return true if referenced */
155 static bool row_upd_index_is_referenced(dict_index_t *index) /*!< in: index */
156 {
157 dict_table_t *table = index->table;
158 bool is_referenced = false;
159
160 if (table->referenced_set.empty()) {
161 return false;
162 }
163
164 dict_foreign_set::iterator it =
165 std::find_if(table->referenced_set.begin(), table->referenced_set.end(),
166 dict_foreign_with_index(index));
167
168 is_referenced = (it != table->referenced_set.end());
169
170 return is_referenced;
171 }
172
173 /** Checks if possible foreign key constraints hold after a delete of the record
174 under pcur.
175
176 NOTE that this function will temporarily commit mtr and lose the
177 pcur position!
178
179 @return DB_SUCCESS or an error code */
180 [[nodiscard]] static dberr_t row_upd_check_references_constraints(
181 upd_node_t *node, /*!< in: row update node */
182 btr_pcur_t *pcur, /*!< in: cursor positioned on a record; NOTE: the
183 cursor position is lost in this function! */
184 dict_table_t *table, /*!< in: table in question */
185 dict_index_t *index, /*!< in: index of the cursor */
186 ulint *offsets, /*!< in/out: rec_get_offsets(pcur.rec, index) */
187 que_thr_t *thr, /*!< in: query thread */
188 mtr_t *mtr) /*!< in: mtr */
189 {
190 dict_foreign_t *foreign;
191 mem_heap_t *heap;
192 dtuple_t *entry;
193 trx_t *trx;
194 const rec_t *rec;
195 dberr_t err;
196
197 DBUG_TRACE;
198
199 /* TODO: NEWDD: WL#6049 Ignore FK on DD system tables for now */
200 if (table->is_dd_table) {
201 return DB_SUCCESS;
202 }
203
204 if (table->referenced_set.empty()) {
205 return DB_SUCCESS;
206 }
207
208 trx = thr_get_trx(thr);
209
210 rec = pcur->get_rec();
211 ut_ad(rec_offs_validate(rec, index, offsets));
212
213 heap = mem_heap_create(500, UT_LOCATION_HERE);
214
215 entry = row_rec_to_index_entry(rec, index, offsets, heap);
216
217 mtr_commit(mtr);
218
219 DEBUG_SYNC_C("foreign_constraint_check_for_update");
220
221 mtr_start(mtr);
222
223 for (dict_foreign_set::iterator it = table->referenced_set.begin();
224 it != table->referenced_set.end(); ++it) {
225 foreign = *it;
226
227 /* Note that we may have an update which updates the index
228 record, but does NOT update the first fields which are
229 referenced in a foreign key constraint. Then the update does
230 NOT break the constraint. */
231
232 if (foreign->referenced_index == index &&
233 (node->is_delete ||
234 row_upd_changes_first_fields_binary(entry, index, node->update,
235 foreign->n_fields))) {
236 dict_table_t *foreign_table = foreign->foreign_table;
237
238 dict_table_t *ref_table = nullptr;
239
240 if (foreign_table == nullptr) {
241 MDL_ticket *mdl;
242
243 ref_table = dd_table_open_on_name(trx->mysql_thd, &mdl,
244 foreign->foreign_table_name_lookup,
245 false, DICT_ERR_IGNORE_NONE);
246 }
247
248 /* NOTE that if the thread ends up waiting for a lock
249 we will release dict_operation_lock temporarily!
250 But the counter on the table protects 'foreign' from
251 being dropped while the check is running. */
252 if (foreign_table) {
253 foreign_table->n_foreign_key_checks_running.fetch_add(1);
254 }
255
256 err = row_ins_check_foreign_constraint(false, foreign, table, entry, thr);
257
258 if (foreign_table) {
259 foreign_table->n_foreign_key_checks_running.fetch_sub(1);
260 }
261
262 if (ref_table != nullptr) {
263 dict_table_close(ref_table, false, false);
264 }
265
266 if (err != DB_SUCCESS) {
267 goto func_exit;
268 }
269 }
270 }
271
272 err = DB_SUCCESS;
273
274 func_exit:
275 mem_heap_free(heap);
276
277 DEBUG_SYNC_C("foreign_constraint_check_for_update_done");
278 return err;
279 }
280
281 /** Creates an update node for a query graph.
282 @return own: update node */
283 upd_node_t *upd_node_create(mem_heap_t *heap) /*!< in: mem heap where created */
284 {
285 upd_node_t *node;
286
287 node = static_cast<upd_node_t *>(mem_heap_zalloc(heap, sizeof(upd_node_t)));
288
289 node->common.type = QUE_NODE_UPDATE;
290 node->state = UPD_NODE_UPDATE_CLUSTERED;
291 node->heap = mem_heap_create(128, UT_LOCATION_HERE);
292 node->magic_n = UPD_NODE_MAGIC_N;
293
294 return (node);
295 }
296 #endif /* !UNIV_HOTBACKUP */
297
298 /** Updates the trx id and roll ptr field in a clustered index record in
299 database recovery.
300 @param[in,out] rec Record
301 @param[in,out] page_zip Compressed page, or null
302 @param[in] offsets Array returned by rec_get_offsets()
303 @param[in] pos Trx_id position in rec
304 @param[in] trx_id Transaction id
305 @param[in] roll_ptr Roll ptr of the undo log record */
306 void row_upd_rec_sys_fields_in_recovery(rec_t *rec, page_zip_des_t *page_zip,
307 const ulint *offsets, ulint pos,
308 trx_id_t trx_id, roll_ptr_t roll_ptr) {
309 ut_ad(rec_offs_validate(rec, nullptr, offsets));
310
311 if (page_zip) {
312 page_zip_write_trx_id_and_roll_ptr(page_zip, rec, offsets, pos, trx_id,
313 roll_ptr);
314 } else {
315 byte *field;
316 ulint len;
317
318 field = rec_get_nth_field(nullptr, rec, offsets, pos, &len);
319 ut_ad(len == DATA_TRX_ID_LEN);
320 trx_write_trx_id(field, trx_id);
321 trx_write_roll_ptr(field + DATA_TRX_ID_LEN, roll_ptr);
322 }
323 }
324
325 #ifndef UNIV_HOTBACKUP
326 /** Sets the trx id or roll ptr field of a clustered index entry.
327 @param[in,out] entry Index entry, where the memory buffers for sys fields are
328 already allocated: the function just copies the new values to them
329 @param[in] index Clustered index
330 @param[in] type Data_trx_id or data_roll_ptr
331 @param[in] val Value to write */
332 void row_upd_index_entry_sys_field(dtuple_t *entry, dict_index_t *index,
333 ulint type, uint64_t val) {
334 dfield_t *dfield;
335 byte *field;
336 ulint pos;
337
338 ut_ad(index->is_clustered());
339
340 pos = index->get_sys_col_pos(type);
341
342 dfield = dtuple_get_nth_field(entry, pos);
343 field = static_cast<byte *>(dfield_get_data(dfield));
344
345 if (type == DATA_TRX_ID) {
346 ut_ad(val > 0);
347 trx_write_trx_id(field, val);
348 } else {
349 ut_ad(type == DATA_ROLL_PTR);
350 trx_write_roll_ptr(field, val);
351 }
352 }
353
354 /** Returns true if row update changes size of some field in index or if some
355 field to be updated is stored externally in rec or update.
356 @return true if the update changes the size of some field in index or
357 the field is external in rec or update */
358 bool row_upd_changes_field_size_or_external(
359 const dict_index_t *index, /*!< in: index */
360 const ulint *offsets, /*!< in: rec_get_offsets(rec, index) */
361 const upd_t *update) /*!< in: update vector */
362 {
363 const upd_field_t *upd_field;
364 const dfield_t *new_val;
365 ulint old_len;
366 ulint new_len;
367 ulint n_fields;
368 ulint i;
369
370 ut_ad(rec_offs_validate(nullptr, index, offsets));
371 ut_ad(!index->table->skip_alter_undo);
372
373 n_fields = upd_get_n_fields(update);
374
375 for (i = 0; i < n_fields; i++) {
376 upd_field = upd_get_nth_field(update, i);
377
378 /* We should ignore virtual field updates (updates to materialized
379 virtual columns are not considered to be virtual field updates). */
380 if (upd_field->is_virtual()) {
381 continue;
382 }
383
384 new_val = &(upd_field->new_val);
385 new_len = dfield_get_len(new_val);
386
387 if (dfield_is_null(new_val) && !rec_offs_comp(offsets)) {
388 /* A bug fixed on Dec 31st, 2004: we looked at the
389 SQL NULL size from the wrong field! We may backport
390 this fix also to 4.0. The merge to 5.0 will be made
391 manually immediately after we commit this to 4.1. */
392
393 new_len = index->get_col(upd_field->field_no)->get_null_size(0);
394 }
395
396 uint32_t field_no = upd_field->field_no;
397
398 old_len = rec_offs_nth_size(index, offsets, field_no);
399
400 if (rec_offs_comp(offsets)) {
401 if (rec_offs_nth_sql_null(index, offsets, field_no)) {
402 /* Note that in the compact table format,
403 for a variable length field, an SQL NULL
404 will use zero bytes in the offset array
405 at the start of the physical record, but
406 a zero-length value (empty string) will
407 use one byte! Thus, we cannot use
408 update-in-place if we update an SQL NULL
409 varchar to an empty string! */
410
411 old_len = UNIV_SQL_NULL;
412 } else if (rec_offs_nth_default(index, offsets, field_no)) {
413 /* This will force to do pessimistic update,
414 since the default value is not inlined,
415 so any update to it will extend the record. */
416 old_len = UNIV_SQL_ADD_COL_DEFAULT;
417 }
418 } else {
419 /* REDUNDANT row format, if it updates the field with
420 not inlined default value, do it in pessimistic way */
421 if (rec_offs_nth_default(index, offsets, field_no)) {
422 old_len = UNIV_SQL_ADD_COL_DEFAULT;
423 }
424 }
425
426 if (dfield_is_ext(new_val) || old_len != new_len ||
427 rec_offs_nth_extern(index, offsets, upd_field->field_no)) {
428 return true;
429 }
430 }
431
432 return false;
433 }
434
435 /** Returns true if row update contains disowned external fields.
436 @return true if the update contains disowned external fields. */
437 bool row_upd_changes_disowned_external(
438 const upd_t *update) /*!< in: update vector */
439 {
440 const upd_field_t *upd_field;
441 const dfield_t *new_val;
442 ulint n_fields;
443 ulint i;
444
445 n_fields = upd_get_n_fields(update);
446
447 for (i = 0; i < n_fields; i++) {
448 upd_field = upd_get_nth_field(update, i);
449 new_val = &(upd_field->new_val);
450
451 if (!dfield_is_ext(new_val)) {
452 continue;
453 }
454
455 ut_ad(dfield_get_len(new_val) >= BTR_EXTERN_FIELD_REF_SIZE);
456
457 lob::ref_t ref(new_val->blobref());
458
459 if (!ref.is_owner()) {
460 return (true);
461 }
462 }
463
464 return (false);
465 }
466 #endif /* !UNIV_HOTBACKUP */
467
468 /** Replaces the new column values stored in the update vector to the
469 record given. No field size changes are allowed. This function is
470 usually invoked on a clustered index. The only use case for a
471 secondary index is row_ins_sec_index_entry_by_modify() or its
472 counterpart in ibuf_insert_to_index_page(). */
473 void row_upd_rec_in_place(
474 rec_t *rec, /*!< in/out: record where replaced */
475 const dict_index_t *index, /*!< in: the index the record belongs to */
476 const ulint *offsets, /*!< in: array returned by rec_get_offsets() */
477 const upd_t *update, /*!< in: update vector */
478 page_zip_des_t *page_zip) /*!< in: compressed page with enough space
479 available, or NULL */
480 {
481 const upd_field_t *upd_field;
482 const dfield_t *new_val;
483 ulint n_fields;
484 ulint i;
485
486 ut_ad(rec_offs_validate(rec, index, offsets));
487 ut_ad(!index->table->skip_alter_undo);
488 ut_d(update->validate_for_index(index));
489
490 if (rec_offs_comp(offsets)) {
491 /* Keep the INSTANT/VERSION bit of prepared physical record */
492 const bool is_instant = rec_get_instant_flag_new(rec);
493 const bool is_versioned = rec_new_is_versioned(rec);
494
495 /* Set the info_bits from the update vector */
496 rec_set_info_bits_new(rec, update->info_bits);
497
498 /* Set the INSTANT/VERSION bit from the values kept */
499 if (is_versioned) {
500 rec_new_set_versioned(rec);
501 } else if (is_instant) {
502 ut_ad(index->table->has_instant_cols());
503 ut_ad(!rec_new_is_versioned(rec));
504 rec_new_set_instant(rec);
505 } else {
506 rec_new_reset_instant_version(rec);
507 }
508
509 /* Only one of the bit (INSTANT or VERSION) could be set */
510 ut_a(!(rec_get_instant_flag_new(rec) && rec_new_is_versioned(rec)));
511 } else {
512 /* INSTANT bit is irrelevant for the record in Redundant format */
513 bool is_versioned = rec_old_is_versioned(rec);
514 rec_set_info_bits_old(rec, update->info_bits);
515 if (is_versioned) {
516 rec_old_set_versioned(rec, true);
517 } else {
518 rec_old_set_versioned(rec, false);
519 }
520 }
521
522 n_fields = upd_get_n_fields(update);
523
524 for (i = 0; i < n_fields; i++) {
525 upd_field = upd_get_nth_field(update, i);
526
527 /* No need to update virtual columns for non-virtual index */
528 if (upd_field->is_virtual()) {
529 continue;
530 }
531
532 uint32_t field_no = upd_field->field_no;
533 new_val = &(upd_field->new_val);
534 ut_ad(!dfield_is_ext(new_val) ==
535 !rec_offs_nth_extern(index, offsets, field_no));
536
537 /* Updating default value for instantly added columns must not be done
538 in-place. See also row_upd_changes_field_size_or_external() */
539 ut_ad(!rec_offs_nth_default(index, offsets, field_no));
540 rec_set_nth_field(index, rec, offsets, field_no, dfield_get_data(new_val),
541 dfield_get_len(new_val));
542 }
543
544 if (page_zip) {
545 page_zip_write_rec(page_zip, rec, index, offsets, 0);
546 }
547 }
548
549 #ifndef UNIV_HOTBACKUP
550 byte *row_upd_write_sys_vals_to_log(dict_index_t *index, trx_id_t trx_id,
551 roll_ptr_t roll_ptr, byte *log_ptr,
552 mtr_t *mtr [[maybe_unused]]) {
553 ut_ad(index->is_clustered());
554 ut_ad(mtr);
555
556 log_ptr +=
557 mach_write_compressed(log_ptr, index->get_sys_col_pos(DATA_TRX_ID));
558
559 trx_write_roll_ptr(log_ptr, roll_ptr);
560 log_ptr += DATA_ROLL_PTR_LEN;
561
562 log_ptr += mach_u64_write_compressed(log_ptr, trx_id);
563
564 return (log_ptr);
565 }
566 #endif /* !UNIV_HOTBACKUP */
567
568 /** Parses the log data of system field values.
569 @return log data end or NULL */
570 byte *row_upd_parse_sys_vals(const byte *ptr, /*!< in: buffer */
571 const byte *end_ptr, /*!< in: buffer end */
572 ulint *pos, /*!< out: TRX_ID position in record */
573 trx_id_t *trx_id, /*!< out: trx id */
574 roll_ptr_t *roll_ptr) /*!< out: roll ptr */
575 {
576 *pos = mach_parse_compressed(&ptr, end_ptr);
577
578 if (ptr == nullptr) {
579 return (nullptr);
580 }
581
582 if (end_ptr < ptr + DATA_ROLL_PTR_LEN) {
583 return (nullptr);
584 }
585
586 *roll_ptr = trx_read_roll_ptr(ptr);
587 ptr += DATA_ROLL_PTR_LEN;
588
589 *trx_id = mach_u64_parse_compressed(&ptr, end_ptr);
590
591 return (const_cast<byte *>(ptr));
592 }
593
594 #ifndef UNIV_HOTBACKUP
595 void row_upd_index_write_log(dict_index_t *index, const upd_t *update,
596 byte *log_ptr, mtr_t *mtr) {
597 const upd_field_t *upd_field;
598 const dfield_t *new_val;
599 ulint len;
600 ulint n_fields;
601 ulint log_fields = 0;
602 byte *buf_end;
603 ulint i;
604
605 n_fields = upd_get_n_fields(update);
606
607 buf_end = log_ptr + MLOG_BUF_MARGIN;
608
609 mach_write_to_1(log_ptr, update->info_bits);
610 log_ptr++;
611
612 for (i = 0; i < n_fields; i++) {
613 upd_field = upd_get_nth_field(update, i);
614
615 /* No need to log virtual field updates (updates to virtual columns
616 which are not materialized in given index), since in this case
617 row_upd_rec_inplace() won't apply this kind of redo log. */
618 if (upd_field->is_virtual()) {
619 ut_ad(index->is_clustered());
620 continue;
621 }
622
623 ++log_fields;
624 }
625
626 log_ptr += mach_write_compressed(log_ptr, log_fields);
627
628 for (i = 0; i < n_fields; i++) {
629 static_assert(MLOG_BUF_MARGIN > 30, "MLOG_BUF_MARGIN <= 30");
630
631 if (log_ptr + 30 > buf_end) {
632 mlog_close(mtr, log_ptr);
633
634 bool success = mlog_open(mtr, MLOG_BUF_MARGIN, log_ptr);
635 ut_a(success);
636 buf_end = log_ptr + MLOG_BUF_MARGIN;
637 }
638
639 upd_field = upd_get_nth_field(update, i);
640
641 if (upd_field->is_virtual()) {
642 ut_ad(index->is_clustered());
643 continue;
644 }
645
646 new_val = &(upd_field->new_val);
647
648 len = dfield_get_len(new_val);
649
650 log_ptr += mach_write_compressed(log_ptr, upd_field->field_no);
651 log_ptr += mach_write_compressed(log_ptr, len);
652
653 if (len != UNIV_SQL_NULL) {
654 if (log_ptr + len < buf_end) {
655 memcpy(log_ptr, dfield_get_data(new_val), len);
656
657 log_ptr += len;
658 } else {
659 mlog_close(mtr, log_ptr);
660
661 mlog_catenate_string(mtr, static_cast<byte *>(dfield_get_data(new_val)),
662 len);
663
664 bool success = mlog_open(mtr, MLOG_BUF_MARGIN, log_ptr);
665 ut_a(success);
666 buf_end = log_ptr + MLOG_BUF_MARGIN;
667 }
668 }
669 }
670
671 mlog_close(mtr, log_ptr);
672 }
673 #endif /* !UNIV_HOTBACKUP */
674
675 byte *row_upd_index_parse(const byte *ptr, const byte *end_ptr,
676 mem_heap_t *heap, upd_t **update_out,
677 dict_index_t *index) {
678 upd_t *update;
679 upd_field_t *upd_field;
680 dfield_t *new_val;
681 ulint len;
682 ulint n_fields;
683 ulint info_bits;
684 ulint i;
685
686 if (end_ptr < ptr + 1) {
687 return (nullptr);
688 }
689
690 info_bits = mach_read_from_1(ptr);
691 ptr++;
692 n_fields = mach_parse_compressed(&ptr, end_ptr);
693
694 if (ptr == nullptr) {
695 return (nullptr);
696 }
697
698 update = upd_create(n_fields, heap);
699 update->info_bits = info_bits;
700
701 for (i = 0; i < n_fields; i++) {
702 ulint field_no;
703 upd_field = upd_get_nth_field(update, i);
704 new_val = &(upd_field->new_val);
705
706 field_no = mach_parse_compressed(&ptr, end_ptr);
707
708 if (ptr == nullptr) {
709 return (nullptr);
710 }
711
712 /* In some previous versions for materialized virtual fields the
713 field_no contained a special value being the virtual column number +
714 REC_MAX_N_FIELDS instead of actual field position.
715 TODO: Remove in 10.x, as after 9.5.0, 8.4.7 we no longer produce
716 updates in this format. */
717 if (field_no >= REC_MAX_N_FIELDS) {
718 auto vcol_no = field_no - REC_MAX_N_FIELDS;
719 field_no = index->get_col_pos(vcol_no, true, true);
720 }
721
722 upd_field->field_no = field_no;
723
724 len = mach_parse_compressed(&ptr, end_ptr);
725
726 if (ptr == nullptr) {
727 return (nullptr);
728 }
729
730 if (len != UNIV_SQL_NULL) {
731 if (end_ptr < ptr + len) {
732 return (nullptr);
733 }
734
735 dfield_set_data(new_val, mem_heap_dup(heap, ptr, len), len);
736 ptr += len;
737 } else {
738 dfield_set_null(new_val);
739 }
740 }
741
742 ut_d(update->validate());
743 *update_out = update;
744
745 return const_cast<byte *>(ptr);
746 }
747
748 #ifndef UNIV_HOTBACKUP
749 /** Builds an update vector from those fields which in a secondary index entry
750 differ from a record that has the equal ordering fields. NOTE: we compare
751 the fields as binary strings!
752 @return own: update vector of differing fields */
753 upd_t *row_upd_build_sec_rec_difference_binary(
754 const rec_t *rec, /*!< in: secondary index record */
755 dict_index_t *index, /*!< in: index */
756 const ulint *offsets, /*!< in: rec_get_offsets(rec, index) */
757 const dtuple_t *entry, /*!< in: entry to insert */
758 mem_heap_t *heap) /*!< in: memory heap from which allocated */
759 {
760 const dfield_t *dfield;
761 const byte *data;
762 ulint len;
763 upd_t *update;
764 ulint n_diff;
765 ulint i;
766
767 /* This function is used only for a secondary index */
768 ut_a(!index->is_clustered());
769 ut_ad(rec_offs_validate(rec, index, offsets));
770 ut_ad(rec_offs_n_fields(offsets) == dtuple_get_n_fields(entry));
771 ut_ad(!rec_offs_any_extern(offsets));
772 ut_ad(!index->table->skip_alter_undo);
773
774 update = upd_create(dtuple_get_n_fields(entry), heap);
775
776 n_diff = 0;
777
778 for (i = 0; i < dtuple_get_n_fields(entry); i++) {
779 data = rec_get_nth_field(index, rec, offsets, i, &len);
780
781 dfield = dtuple_get_nth_field(entry, i);
782
783 /* NOTE that it may be that len != dfield_get_len(dfield) if we
784 are updating in a character set and collation where strings of
785 different length can be equal in an alphabetical comparison,
786 and also in the case where we have a column prefix index
787 and the last characters in the index field are spaces; the
788 latter case probably caused the assertion failures reported at
789 row0upd.cc line 713 in versions 4.0.14 - 4.0.16. */
790
791 /* NOTE: we compare the fields as binary strings!
792 (No collation) */
793
794 if (!dfield_data_is_binary_equal(dfield, len, data)) {
795 upd_field_t *const upd_field = upd_get_nth_field(update, n_diff);
796
797 dfield_copy(&(upd_field->new_val), dfield);
798
799 upd_field_set_field_no(upd_field, i, index);
800
801 n_diff++;
802 }
803 }
804
805 update->n_fields = n_diff;
806 ut_d(update->validate_for_index(index));
807
808 return (update);
809 }
810
811 /** Builds an update vector from those fields, excluding the roll ptr and
812 trx id fields, which in an index entry differ from a record that has
813 the equal ordering fields. NOTE: we compare the fields as binary strings!
814 @param[in] index clustered index
815 @param[in] entry clustered index entry to insert
816 @param[in] rec clustered index record
817 @param[in] offsets rec_get_offsets(rec,index), or NULL
818 @param[in] no_sys skip the system columns
819 DB_TRX_ID and DB_ROLL_PTR
820 @param[in] trx transaction (for diagnostics),
821 or NULL
822 @param[in] heap memory heap from which allocated
823 @param[in] mysql_table NULL, or mysql table object when
824 user thread invokes dml
825 @param[out] error error number in case of failure
826 @return own: update vector of differing fields, excluding roll ptr and
827 trx id */
828 upd_t *row_upd_build_difference_binary(dict_index_t *index,
829 const dtuple_t *entry, const rec_t *rec,
830 const ulint *offsets, bool no_sys,
831 trx_t *trx, mem_heap_t *heap,
832 TABLE *mysql_table, dberr_t *error) {
833 ut_d(entry->validate_for_index(index));
834 upd_field_t *upd_field;
835 dfield_t *dfield;
836 const byte *data;
837 ulint len;
838 upd_t *update;
839 ulint n_diff;
840 ulint trx_id_pos;
841 ulint i;
842 ulint offsets_[REC_OFFS_NORMAL_SIZE];
843 const ulint n_fld = dtuple_get_n_fields(entry);
844 const ulint n_v_fld = dtuple_get_n_v_fields(entry);
845 rec_offs_init(offsets_);
846 const dict_table_t *const table = index->table;
847
848 /* This function is used only for a clustered index */
849 ut_a(index->is_clustered());
850 ut_ad(!table->skip_alter_undo);
851
852 update = upd_create(n_fld + n_v_fld, heap);
853 update->table = table;
854
855 n_diff = 0;
856
857 trx_id_pos = index->get_sys_col_pos(DATA_TRX_ID);
858 ut_ad(table->is_intrinsic() ||
859 (index->get_sys_col_pos(DATA_ROLL_PTR) == trx_id_pos + 1));
860
861 if (!offsets) {
862 offsets = rec_get_offsets(rec, index, offsets_, ULINT_UNDEFINED,
863 UT_LOCATION_HERE, &heap);
864 } else {
865 ut_ad(rec_offs_validate(rec, index, offsets));
866 }
867
868 for (i = 0; i < n_fld; i++) {
869 data = rec_get_nth_field_instant(rec, offsets, i, index, &len);
870
871 dfield = dtuple_get_nth_field(entry, i);
872
873 /* NOTE: we compare the fields as binary strings!
874 (No collation) */
875 if (no_sys) {
876 /* TRX_ID */
877 if (i == trx_id_pos) {
878 continue;
879 }
880
881 /* DB_ROLL_PTR */
882 if (i == trx_id_pos + 1 && !table->is_intrinsic()) {
883 continue;
884 }
885 }
886
887 if (!dfield_is_ext(dfield) != !rec_offs_nth_extern(index, offsets, i) ||
888 !dfield_data_is_binary_equal(dfield, len, data)) {
889 upd_field = upd_get_nth_field(update, n_diff);
890
891 dfield_copy(&(upd_field->new_val), dfield);
892
893 #ifdef UNIV_DEBUG
894 if (index->has_row_versions()) {
895 upd_field->field_phy_pos = index->get_field(i)->col->get_col_phy_pos();
896 }
897 #endif
898 upd_field_set_field_no(upd_field, i, index);
899
900 n_diff++;
901 }
902 }
903
904 /* Check the virtual columns updates. Even if there is no non-virtual
905 column (base columns) change, we will still need to build the
906 indexed virtual column value so that undo log would log them (
907 for purge/mvcc purpose) */
908 if (n_v_fld > 0) {
909 row_ext_t *ext;
910 mem_heap_t *v_heap = nullptr;
911 THD *thd;
912
913 if (trx == nullptr) {
914 thd = current_thd;
915 } else {
916 thd = trx->mysql_thd;
917 }
918
919 ut_ad(!update->old_vrow);
920
921 for (i = 0; i < n_v_fld; i++) {
922 const dict_v_col_t *col = dict_table_get_nth_v_col(table, i);
923
924 if (!col->m_col.ord_part) {
925 continue;
926 }
927
928 if (update->old_vrow == nullptr) {
929 update->old_vrow = row_build(ROW_COPY_POINTERS, index, rec, offsets,
930 table, nullptr, nullptr, &ext, heap);
931 }
932
933 dfield = dtuple_get_nth_v_field(entry, i);
934
935 const dfield_t *const vfield = innobase_get_computed_value(
936 update->old_vrow, col, table, &v_heap, heap, thd, mysql_table);
937
938 if (vfield == nullptr) {
939 *error = DB_COMPUTE_VALUE_FAILED;
940 return nullptr;
941 }
942
943 if (!dfield_data_is_binary_equal(
944 dfield, vfield->len, static_cast<const byte *>(vfield->data))) {
945 upd_field = upd_get_nth_field(update, n_diff);
946
947 upd_field->old_v_val = static_cast<dfield_t *>(
948 mem_heap_alloc(heap, sizeof *upd_field->old_v_val));
949
950 dfield_copy(upd_field->old_v_val, vfield);
951
952 dfield_copy(&(upd_field->new_val), dfield);
953
954 upd_field_set_v_field_no(upd_field, i, index);
955
956 n_diff++;
957 }
958 }
959
960 if (v_heap) {
961 mem_heap_free(v_heap);
962 }
963 }
964
965 update->n_fields = n_diff;
966 ut_d(update->validate_for_index(index));
967
968 return update;
969 }
970 /** Fetch a prefix of an externally stored column.
971 This is similar to row_ext_lookup(), but the row_ext_t holds the old values
972 of the column and must not be poisoned with the new values.
973 @param[in] clust_index the clustered index.
974 @param[in] data 'internally' stored part of the field
975 containing also the reference to the external part
976 @param[in] local_len length of data, in bytes
977 @param[in] page_size BLOB page size
978 @param[in,out] len input - length of prefix to fetch; output:
979 fetched length of the prefix
980 @param[in] is_sdi true for SDI indexes
981 @param[in,out] heap heap where to allocate
982 @return BLOB prefix */
983 static byte *row_upd_ext_fetch(dict_index_t *clust_index, const byte *data,
984 ulint local_len, const page_size_t &page_size,
985 ulint *len,
986 IF_DEBUG(bool is_sdi, ) mem_heap_t *heap) {
987 byte *buf = static_cast<byte *>(mem_heap_alloc(heap, *len));
988
989 *len = lob::btr_copy_externally_stored_field_prefix_func(
990 nullptr, clust_index, buf, *len, page_size, data,
991 IF_DEBUG(is_sdi, ) local_len);
992
993 /* We should never update records containing a half-deleted BLOB. */
994 ut_a(*len);
995
996 return (buf);
997 }
998
999 /** Replaces the new column value stored in the update vector in
1000 the given index entry field.
1001 @param[in] index index dictionary object.
1002 @param[in,out] dfield data field of the index entry
1003 @param[in] field index field
1004 @param[in] col field->col
1005 @param[in] uf update field
1006 @param[in,out] heap memory heap for allocating and copying
1007 the new value
1008 @param[in] is_sdi true for SDI indexes
1009 @param[in] page_size page size */
1010 static void row_upd_index_replace_new_col_val_func(
1011 const dict_index_t *index, dfield_t *dfield, const dict_field_t *field,
1012 const dict_col_t *col, const upd_field_t *uf, mem_heap_t *heap,
1013 IF_DEBUG(bool is_sdi, ) const page_size_t &page_size) {
1014 DBUG_TRACE;
1015
1016 ulint len;
1017 const byte *data;
1018
1019 dfield_copy_data(dfield, &uf->new_val);
1020
1021 if (dfield_is_null(dfield)) {
1022 return;
1023 }
1024
1025 len = dfield_get_len(dfield);
1026 data = static_cast<const byte *>(dfield_get_data(dfield));
1027
1028 if (field->prefix_len > 0) {
1029 auto fetch_ext = dfield_is_ext(dfield) &&
1030 len < (ulint)field->prefix_len + BTR_EXTERN_FIELD_REF_SIZE;
1031
1032 if (fetch_ext) {
1033 ulint l = len;
1034
1035 len = field->prefix_len;
1036
1037 data = row_upd_ext_fetch(index->table->first_index(), data, l, page_size,
1038 &len, IF_DEBUG(is_sdi, ) heap);
1039 }
1040
1041 len =
1042 dtype_get_at_most_n_mbchars(col->prtype, col->mbminmaxlen,
1043 field->prefix_len, len, (const char *)data);
1044
1045 dfield_set_data(dfield, data, len);
1046
1047 if (!fetch_ext) {
1048 dfield_dup(dfield, heap);
1049 }
1050
1051 return;
1052 }
1053
1054 switch (uf->orig_len) {
1055 byte *buf;
1056 case BTR_EXTERN_FIELD_REF_SIZE:
1057 /* Restore the original locally stored
1058 part of the column. In the undo log,
1059 InnoDB writes a longer prefix of externally
1060 stored columns, so that column prefixes
1061 in secondary indexes can be reconstructed. */
1062 dfield_set_data(dfield, data + len - BTR_EXTERN_FIELD_REF_SIZE,
1063 BTR_EXTERN_FIELD_REF_SIZE);
1064 dfield_set_ext(dfield);
1065 [[fallthrough]];
1066 case 0:
1067 if (dfield_is_multi_value(dfield)) {
1068 dfield_multi_value_dup(dfield, heap);
1069 } else {
1070 dfield_dup(dfield, heap);
1071 }
1072 break;
1073 default:
1074 /* Reconstruct the original locally
1075 stored part of the column. The data
1076 will have to be copied. */
1077 ut_a(uf->orig_len > BTR_EXTERN_FIELD_REF_SIZE);
1078 buf = static_cast<byte *>(mem_heap_alloc(heap, uf->orig_len));
1079
1080 /* Copy the locally stored prefix. */
1081 memcpy(buf, data, uf->orig_len - BTR_EXTERN_FIELD_REF_SIZE);
1082
1083 /* Copy the BLOB pointer. */
1084 memcpy(buf + uf->orig_len - BTR_EXTERN_FIELD_REF_SIZE,
1085 data + len - BTR_EXTERN_FIELD_REF_SIZE, BTR_EXTERN_FIELD_REF_SIZE);
1086
1087 dfield_set_data(dfield, buf, uf->orig_len);
1088 dfield_set_ext(dfield);
1089 break;
1090 }
1091 }
1092
1093 static inline void row_upd_index_replace_new_col_val(
1094 const dict_index_t *index, dfield_t *dfield, const dict_field_t *field,
1095 const dict_col_t *col, const upd_field_t *uf, mem_heap_t *heap,
1096 bool is_sdi [[maybe_unused]], const page_size_t &page_size) {
1097 row_upd_index_replace_new_col_val_func(index, dfield, field, col, uf, heap,
1098 IF_DEBUG(is_sdi, ) page_size);
1099 }
1100
1101 /** Replaces the new column values stored in the update vector to the index
1102 entry given.
1103 @param[in,out] entry Index entry where replaced; the clustered index record
1104 must be covered by a lock or a page latch to prevent deletion [rollback or
1105 purge]
1106 @param[in] index Index; note that this may also be a non-clustered index
1107 @param[in] update An update vector built for the index so that the field
1108 number in an upd_field is the index position
1109 @param[in] order_only If true, limit the replacement to ordering fields of
1110 index; note that this does not work for non-clustered indexes.
1111 @param[in] heap Memory heap for allocating and copying the new values */
1112 void row_upd_index_replace_new_col_vals_index_pos(dtuple_t *entry,
1113 const dict_index_t *index,
1114 const upd_t *update,
1115 bool order_only,
1116 mem_heap_t *heap) {
1117 DBUG_TRACE;
1118 ut_ad(index);
1119 ut_ad(!index->table->skip_alter_undo);
1120 ut_d(update->validate_for_index(index));
1121
1122 dtuple_set_info_bits(entry, update->info_bits);
1123
1124 const ulint n_fields = order_only ? dict_index_get_n_unique(index)
1125 : dict_index_get_n_fields(index);
1126 for (ulint field_index = 0; field_index < n_fields; field_index++) {
1127 const dict_field_t *field = index->get_field(field_index);
1128 const dict_col_t *col = field->col;
1129
1130 if (col->is_instant_dropped()) {
1131 dfield_t *field = dtuple_get_nth_field(entry, field_index);
1132 field->reset();
1133 continue;
1134 }
1135
1136 if (auto uf = upd_get_field_by_field_no(update, field_index, false); uf) {
1137 upd_field_t *tmp = const_cast<upd_field_t *>(uf);
1138 dfield_t *dfield = dtuple_get_nth_field(entry, field_index);
1139 tmp->ext_in_old = dfield_is_ext(dfield);
1140
1141 dfield_copy(&tmp->old_val, dfield);
1142
1143 const auto &page_size = dict_table_page_size(index->table);
1144 row_upd_index_replace_new_col_val(index, dfield, field, col, uf, heap,
1145 dict_index_is_sdi(index), page_size);
1146 }
1147 }
1148 ut_d(entry->validate_for_index(index));
1149 }
1150
1151 /** Replaces the new column values stored in the update vector to the index
1152 entry given.
1153 @param[in,out] entry Index entry where replaced; the clustered index record must
1154 be covered by a lock or a page latch to prevent deletion (rollback or purge)
1155 @param[in] index Index; note that this may also be a non-clustered index
1156 @param[in] update An update vector built for the clustered index so that the
1157 field number in an upd_field is the clustered index position
1158 @param[in] heap Memory heap for allocating and copying the new values */
1159 void row_upd_index_replace_new_col_vals(dtuple_t *entry,
1160 const dict_index_t *index,
1161 const upd_t *update, mem_heap_t *heap) {
1162 ulint i;
1163 const dict_index_t *clust_index = index->table->first_index();
1164 const page_size_t &page_size = dict_table_page_size(index->table);
1165
1166 ut_ad(!index->table->skip_alter_undo);
1167
1168 dtuple_set_info_bits(entry, update->info_bits);
1169
1170 for (i = 0; i < dict_index_get_n_fields(index); i++) {
1171 const dict_field_t *field;
1172 const dict_col_t *col;
1173 const upd_field_t *uf;
1174
1175 field = index->get_field(i);
1176 col = field->col;
1177 if (col->is_virtual()) {
1178 const dict_v_col_t *vcol = reinterpret_cast<const dict_v_col_t *>(col);
1179
1180 uf = upd_get_field_by_field_no(update, vcol->v_pos, true);
1181 } else {
1182 uf = upd_get_field_by_field_no(
1183 update, dict_col_get_clust_pos(col, clust_index), false);
1184 }
1185
1186 if (uf) {
1187 row_upd_index_replace_new_col_val(index, dtuple_get_nth_field(entry, i),
1188 field, col, uf, heap,
1189 dict_index_is_sdi(index), page_size);
1190 }
1191 }
1192 }
1193
1194 /** Replaces the virtual column values stored in the update vector.
1195 @param[in,out] row row whose column to be set
1196 @param[in] field data to set
1197 @param[in] len data length
1198 @param[in] vcol virtual column info */
1199 static void row_upd_set_vcol_data(dtuple_t *row, const byte *field, ulint len,
1200 dict_v_col_t *vcol) {
1201 dfield_t *dfield = dtuple_get_nth_v_field(row, vcol->v_pos);
1202
1203 if (dfield_get_type(dfield)->mtype == DATA_MISSING) {
1204 vcol->m_col.copy_type(dfield_get_type(dfield));
1205
1206 dfield_set_data(dfield, field, len);
1207 }
1208 }
1209
1210 /** Replaces the virtual column values stored in a dtuple with that of
1211 a update vector.
1212 @param[in,out] row dtuple whose column to be updated
1213 @param[in] table table
1214 @param[in] update an update vector built for the clustered index
1215 @param[in] upd_new update to new or old value
1216 @param[in,out] undo_row undo row (if needs to be updated)
1217 @param[in] ptr remaining part in update undo log */
1218 void row_upd_replace_vcol(dtuple_t *row, const dict_table_t *table,
1219 const upd_t *update, bool upd_new, dtuple_t *undo_row,
1220 const byte *ptr) {
1221 ut_ad(!table->skip_alter_undo);
1222
1223 const auto n_cols = dtuple_get_n_v_fields(row);
1224 for (ulint col_no = 0; col_no < n_cols; col_no++) {
1225 dfield_t *dfield;
1226
1227 const dict_v_col_t *col = dict_table_get_nth_v_col(table, col_no);
1228
1229 /* If there is no index on the column, do not bother for
1230 value update */
1231 if (!col->m_col.ord_part) {
1232 const dict_index_t *clust_index = table->first_index();
1233
1234 /* Skip the column if there is no online alter
1235 table in progress or it is not being indexed
1236 in new table */
1237 if (!dict_index_is_online_ddl(clust_index) ||
1238 !row_log_col_is_indexed(clust_index, col_no)) {
1239 continue;
1240 }
1241 }
1242
1243 dfield = dtuple_get_nth_v_field(row, col_no);
1244
1245 const upd_field_t *const upd_field =
1246 upd_get_field_by_field_no(update, col->v_pos, true);
1247 if (upd_field) {
1248 if (upd_new) {
1249 dfield_copy_data(dfield, &upd_field->new_val);
1250 } else {
1251 dfield_copy_data(dfield, upd_field->old_v_val);
1252 }
1253
1254 dfield_get_type(dfield)->mtype = upd_field->new_val.type.mtype;
1255 dfield_get_type(dfield)->prtype = upd_field->new_val.type.prtype;
1256 dfield_get_type(dfield)->mbminmaxlen =
1257 upd_field->new_val.type.mbminmaxlen;
1258 }
1259 }
1260
1261 bool first_v_col = true;
1262 bool is_undo_log = true;
1263
1264 /* We will read those unchanged (but indexed) virtual columns in */
1265 if (ptr != nullptr) {
1266 const byte *end_ptr;
1267
1268 end_ptr = ptr + mach_read_from_2(ptr);
1269 ptr += 2;
1270
1271 while (ptr != end_ptr) {
1272 const byte *field;
1273 ulint field_no;
1274 ulint len;
1275 ulint orig_len;
1276 bool is_v;
1277 dict_v_col_t *vcol = nullptr;
1278 dfield_t *dfield = nullptr;
1279 dfield_t read_field;
1280
1281 field_no = mach_read_next_compressed(&ptr);
1282
1283 is_v = (field_no >= REC_MAX_N_FIELDS);
1284
1285 if (is_v) {
1286 ptr = trx_undo_read_v_idx(table, ptr, first_v_col, &is_undo_log,
1287 &field_no);
1288 first_v_col = false;
1289 if (field_no != ULINT_UNDEFINED) {
1290 vcol = dict_table_get_nth_v_col(table, field_no);
1291 dfield = dtuple_get_nth_v_field(row, vcol->v_pos);
1292 }
1293 }
1294
1295 if ((is_v && vcol != nullptr && vcol->m_col.is_multi_value()) ||
1296 trx_undo_rec_is_multi_value(ptr)) {
1297 ut_ad(is_v);
1298 ut_ad(vcol != nullptr || field_no == ULINT_UNDEFINED);
1299 ptr = trx_undo_rec_get_multi_value(ptr, &read_field, update->heap);
1300
1301 if (dfield_get_type(dfield)->mtype == DATA_MISSING) {
1302 dfield_copy_data(dfield, &read_field);
1303 }
1304
1305 vcol->m_col.copy_type(dfield_get_type(dfield));
1306
1307 if (undo_row) {
1308 dfield_t *undo_field = dtuple_get_nth_v_field(undo_row, vcol->v_pos);
1309 if (dfield_get_type(undo_field)->mtype == DATA_MISSING) {
1310 vcol->m_col.copy_type(dfield_get_type(undo_field));
1311 dfield_copy_data(undo_field, &read_field);
1312 }
1313 }
1314
1315 continue;
1316 } else {
1317 ptr = trx_undo_rec_get_col_val(ptr, &field, &len, &orig_len);
1318 }
1319
1320 if (field_no == ULINT_UNDEFINED) {
1321 ut_ad(is_v);
1322 continue;
1323 }
1324
1325 if (is_v) {
1326 row_upd_set_vcol_data(row, field, len, vcol);
1327
1328 if (undo_row) {
1329 row_upd_set_vcol_data(undo_row, field, len, vcol);
1330 }
1331 }
1332 ut_ad(ptr <= end_ptr);
1333 }
1334 }
1335 }
1336
1337 void row_upd_replace(dtuple_t *row, row_ext_t **ext, const dict_index_t *index,
1338 const upd_t *update, mem_heap_t *heap) {
1339 ulint col_no;
1340 ulint i;
1341 ulint n_cols;
1342 ulint n_ext_cols;
1343 ulint *ext_cols;
1344 const dict_table_t *table;
1345
1346 ut_ad(row);
1347 ut_ad(ext);
1348 ut_ad(index);
1349 ut_ad(index->is_clustered());
1350 ut_ad(update);
1351 ut_ad(heap);
1352 ut_d(update->validate_for_index(index));
1353 ut_ad(!index->table->skip_alter_undo);
1354
1355 n_cols = dtuple_get_n_fields(row);
1356 table = index->table;
1357 ut_ad(n_cols == table->get_n_cols());
1358
1359 ext_cols =
1360 static_cast<ulint *>(mem_heap_alloc(heap, n_cols * sizeof *ext_cols));
1361
1362 n_ext_cols = 0;
1363
1364 dtuple_set_info_bits(row, update->info_bits);
1365
1366 for (col_no = 0; col_no < n_cols; col_no++) {
1367 const dict_col_t *col = table->get_col(col_no);
1368 const ulint clust_pos = dict_col_get_clust_pos(col, index);
1369 dfield_t *dfield;
1370
1371 if (UNIV_UNLIKELY(clust_pos == ULINT_UNDEFINED)) {
1372 continue;
1373 }
1374
1375 dfield = dtuple_get_nth_field(row, col_no);
1376
1377 for (i = 0; i < upd_get_n_fields(update); i++) {
1378 const upd_field_t *upd_field = upd_get_nth_field(update, i);
1379
1380 if (upd_field->field_no != clust_pos || upd_field->is_virtual()) {
1381 continue;
1382 }
1383
1384 dfield_copy_data(dfield, &upd_field->new_val);
1385 break;
1386 }
1387
1388 if (dfield_is_ext(dfield) && col->ord_part) {
1389 ext_cols[n_ext_cols++] = col_no;
1390 }
1391 }
1392
1393 if (n_ext_cols) {
1394 *ext = row_ext_create(index, n_ext_cols, ext_cols, table->flags, row,
1395 dict_index_is_sdi(index), heap);
1396 } else {
1397 *ext = nullptr;
1398 }
1399
1400 row_upd_replace_vcol(row, table, update, true, nullptr, nullptr);
1401 ut_d(row->validate_for_index(index));
1402 }
1403
1404 bool row_upd_changes_ord_field_binary_func(dict_index_t *index,
1405 const upd_t *update,
1406 IF_DEBUG(const que_thr_t *thr, )
1407 const dtuple_t *row,
1408 const row_ext_t *ext,
1409 bool *non_mv_upd, ulint flag) {
1410 ulint n_unique;
1411 ulint i;
1412 const dict_index_t *clust_index;
1413 bool changes = false;
1414
1415 ut_ad(index);
1416 ut_ad(update);
1417
1418 ut_ad(!index->table->skip_alter_undo);
1419 ut_ad(non_mv_upd == nullptr || index->is_multi_value());
1420
1421 if (non_mv_upd != nullptr) {
1422 *non_mv_upd = false;
1423 }
1424
1425 n_unique = dict_index_get_n_unique(index);
1426
1427 clust_index = index->table->first_index();
1428
1429 for (i = 0; i < n_unique; i++) {
1430 const dict_field_t *ind_field;
1431 const dict_col_t *col;
1432 ulint col_no;
1433 const upd_field_t *upd_field;
1434 const dfield_t *dfield;
1435 dfield_t dfield_ext;
1436 ulint dfield_len;
1437 const byte *buf;
1438 const dict_v_col_t *vcol = nullptr;
1439
1440 ind_field = index->get_field(i);
1441 col = ind_field->col;
1442 col_no = dict_col_get_no(col);
1443
1444 if (col->is_virtual()) {
1445 vcol = reinterpret_cast<const dict_v_col_t *>(col);
1446
1447 upd_field = upd_get_field_by_field_no(update, vcol->v_pos, true);
1448 } else {
1449 upd_field = upd_get_field_by_field_no(
1450 update, dict_col_get_clust_pos(col, clust_index), false);
1451 }
1452
1453 if (upd_field == nullptr) {
1454 continue;
1455 }
1456
1457 if (row == nullptr) {
1458 ut_ad(ext == nullptr);
1459 return (true);
1460 }
1461
1462 if (col->is_virtual()) {
1463 dfield = dtuple_get_nth_v_field(row, vcol->v_pos);
1464 } else {
1465 dfield = dtuple_get_nth_field(row, col_no);
1466 }
1467
1468 /* For spatial index update, since the different geometry
1469 data could generate same MBR, so, if the new index entry is
1470 same as old entry, which means the MBR is not changed, we
1471 don't need to do anything. */
1472 if (dict_index_is_spatial(index) && i == 0) {
1473 double mbr1[SPDIMS * 2];
1474 double mbr2[SPDIMS * 2];
1475 rtr_mbr_t *old_mbr;
1476 rtr_mbr_t *new_mbr;
1477 uchar *dptr = nullptr;
1478 ulint flen = 0;
1479 ulint dlen = 0;
1480 mem_heap_t *temp_heap = nullptr;
1481 const dfield_t *new_field = &upd_field->new_val;
1482
1483 const page_size_t page_size = (ext != nullptr)
1484 ? ext->page_size
1485 : dict_table_page_size(index->table);
1486
1487 ut_ad(dfield->data != nullptr && dfield->len > GEO_DATA_HEADER_SIZE);
1488 ut_ad(col->get_spatial_status() != SPATIAL_NONE);
1489
1490 /* Get the old mbr. */
1491 if (dfield_is_ext(dfield)) {
1492 /* For off-page stored data, we
1493 need to read the whole field data. */
1494 flen = dfield_get_len(dfield);
1495 dptr = static_cast<byte *>(dfield_get_data(dfield));
1496 temp_heap = mem_heap_create(100, UT_LOCATION_HERE);
1497
1498 const dict_index_t *clust_index =
1499 (ext == nullptr ? index->table->first_index() : ext->index);
1500 dptr = lob::btr_copy_externally_stored_field(
1501 nullptr, clust_index, &dlen, nullptr, dptr, page_size, flen, false,
1502 temp_heap);
1503 } else {
1504 dptr = static_cast<uchar *>(dfield->data);
1505 dlen = dfield->len;
1506 }
1507
1508 uint32_t srid;
1509
1510 get_mbr_from_store(index->rtr_srs.get(), dptr, static_cast<uint>(dlen),
1511 SPDIMS, mbr1, &srid);
1512 old_mbr = reinterpret_cast<rtr_mbr_t *>(mbr1);
1513
1514 /* Get the new mbr. */
1515 if (dfield_is_ext(new_field)) {
1516 if (flag == ROW_BUILD_FOR_UNDO &&
1517 dict_table_has_atomic_blobs(index->table)) {
1518 /* For undo, and the table is
1519 Barrcuda, we need to skip the
1520 prefix data. */
1521 flen = BTR_EXTERN_FIELD_REF_SIZE;
1522 ut_ad(dfield_get_len(new_field) >= BTR_EXTERN_FIELD_REF_SIZE);
1523 dptr = static_cast<byte *>(dfield_get_data(new_field)) +
1524 dfield_get_len(new_field) - BTR_EXTERN_FIELD_REF_SIZE;
1525 } else {
1526 flen = dfield_get_len(new_field);
1527 dptr = static_cast<byte *>(dfield_get_data(new_field));
1528 }
1529
1530 if (temp_heap == nullptr) {
1531 temp_heap = mem_heap_create(100, UT_LOCATION_HERE);
1532 }
1533
1534 const dict_index_t *clust_index =
1535 (ext == nullptr ? index->table->first_index() : ext->index);
1536 dptr = lob::btr_copy_externally_stored_field(
1537 nullptr, clust_index, &dlen, nullptr, dptr, page_size, flen,
1538 dict_table_is_sdi(index->table->id), temp_heap);
1539 } else {
1540 dptr = static_cast<uchar *>(upd_field->new_val.data);
1541 dlen = upd_field->new_val.len;
1542 }
1543
1544 uint32_t new_srid;
1545 get_mbr_from_store(index->rtr_srs.get(), dptr, static_cast<uint>(dlen),
1546 SPDIMS, mbr2, &new_srid);
1547
1548 new_mbr = reinterpret_cast<rtr_mbr_t *>(mbr2);
1549
1550 if (new_srid != srid) {
1551 return (true);
1552 }
1553
1554 if (temp_heap) {
1555 mem_heap_free(temp_heap);
1556 }
1557
1558 /* We use mbr_equal_physically() because we would like to skip
1559 Updation of Spatial Index only when the existing MBR in Spatial
1560 Index matches physically to the MBR of the new geometry.
1561 Else it might cause issues later during searching of record, because
1562 cmp_geometry_field() does physical comparison.*/
1563 if (!mbr_equal_physically(old_mbr, new_mbr)) {
1564 return (true);
1565 } else {
1566 continue;
1567 }
1568 }
1569
1570 /* This treatment of column prefix indexes is loosely
1571 based on row_build_index_entry(). */
1572
1573 if (UNIV_LIKELY(ind_field->prefix_len == 0) || dfield_is_null(dfield)) {
1574 /* do nothing special */
1575 } else if (ext) {
1576 /* Silence a compiler warning without
1577 silencing a Valgrind error. */
1578 dfield_len = 0;
1579 UNIV_MEM_INVALID(&dfield_len, sizeof dfield_len);
1580 /* See if the column is stored externally. */
1581 buf = row_ext_lookup(ext, col_no, &dfield_len);
1582
1583 ut_ad(col->ord_part);
1584
1585 if (UNIV_LIKELY_NULL(buf)) {
1586 if (UNIV_UNLIKELY(buf == field_ref_zero)) {
1587 /* The externally stored field
1588 was not written yet. This
1589 record should only be seen by
1590 trx_rollback_or_clean_all_recovered(),
1591 when the server had crashed before
1592 storing the field. */
1593 ut_ad(thr == nullptr || thr->graph->trx->is_recovered);
1594 ut_ad(thr == nullptr || trx_is_recv(thr->graph->trx));
1595 ut_ad(non_mv_upd == nullptr);
1596
1597 return (true);
1598 }
1599
1600 goto copy_dfield;
1601 }
1602 } else if (dfield_is_ext(dfield)) {
1603 dfield_len = dfield_get_len(dfield);
1604 ut_a(dfield_len > BTR_EXTERN_FIELD_REF_SIZE);
1605 dfield_len -= BTR_EXTERN_FIELD_REF_SIZE;
1606 ut_a(index->is_clustered() || ind_field->prefix_len <= dfield_len);
1607
1608 buf = static_cast<byte *>(dfield_get_data(dfield));
1609 copy_dfield:
1610 ut_a(dfield_len > 0);
1611 dfield_copy(&dfield_ext, dfield);
1612 dfield_set_data(&dfield_ext, buf, dfield_len);
1613 dfield = &dfield_ext;
1614 }
1615
1616 if (!dfield_datas_are_binary_equal(dfield, &upd_field->new_val,
1617 ind_field->prefix_len)) {
1618 changes = true;
1619
1620 if (non_mv_upd == nullptr || !dfield_is_multi_value(dfield)) {
1621 if (non_mv_upd != nullptr) {
1622 *non_mv_upd = true;
1623 }
1624 break;
1625 }
1626 }
1627 }
1628
1629 return changes;
1630 }
1631
1632 /** Checks if an update vector changes an ordering field of an index record.
1633 NOTE: we compare the fields as binary strings!
1634 @return true if update vector may change an ordering field in an index
1635 record */
1636 bool row_upd_changes_some_index_ord_field_binary(
1637 const dict_table_t *table, /*!< in: table */
1638 const upd_t *update) /*!< in: update vector for the row */
1639 {
1640 const upd_field_t *upd_field;
1641 const dict_index_t *index;
1642 ulint i;
1643
1644 index = table->first_index();
1645
1646 for (i = 0; i < upd_get_n_fields(update); i++) {
1647 upd_field = upd_get_nth_field(update, i);
1648
1649 if (upd_field->is_virtual()) {
1650 if (dict_table_get_nth_v_col(index->table, upd_field->field_no)
1651 ->m_col.ord_part) {
1652 return true;
1653 }
1654 } else {
1655 if (index->get_field(upd_field->field_no)->col->ord_part) {
1656 return true;
1657 }
1658 }
1659 }
1660
1661 return false;
1662 }
1663
1664 /** Checks if an FTS Doc ID column is affected by an UPDATE.
1665 @return whether the Doc ID column is changed */
1666 bool row_upd_changes_doc_id(dict_table_t *table, /*!< in: table */
1667 upd_field_t *upd_field) /*!< in: field to check */
1668 {
1669 ulint col_no;
1670 dict_index_t *clust_index;
1671 fts_t *fts = table->fts;
1672
1673 ut_ad(!table->skip_alter_undo);
1674
1675 clust_index = table->first_index();
1676
1677 /* Convert from index-specific column number to table-global
1678 column number. */
1679 col_no = clust_index->get_col_no(upd_field->field_no);
1680
1681 return (col_no == fts->doc_col);
1682 }
1683
1684 ulint row_upd_changes_fts_column(dict_table_t *table, upd_field_t *upd_field) {
1685 ulint col_no;
1686 dict_index_t *clust_index;
1687 fts_t *fts = table->fts;
1688
1689 ut_ad(!table->skip_alter_undo);
1690
1691 if (upd_field->is_virtual()) {
1692 col_no = upd_field->field_no;
1693 return (dict_table_is_fts_column(fts->indexes, col_no, true));
1694 } else {
1695 clust_index = table->first_index();
1696
1697 /* Convert from index-specific column number to table-global
1698 column number. */
1699 col_no = clust_index->get_col_no(upd_field->field_no);
1700 return (dict_table_is_fts_column(fts->indexes, col_no, false));
1701 }
1702 }
1703
1704 /** Checks if an update vector changes some of the first ordering fields of an
1705 index record. This is only used in foreign key checks and we can assume
1706 that index does not contain column prefixes.
1707 @return true if changes */
1708 static bool row_upd_changes_first_fields_binary(
1709 dtuple_t *entry, /*!< in: index entry */
1710 dict_index_t *index, /*!< in: index of entry */
1711 const upd_t *update, /*!< in: update vector for the row */
1712 ulint n) /*!< in: how many first fields to check */
1713 {
1714 ulint n_upd_fields;
1715 ulint i, j;
1716 dict_index_t *clust_index;
1717
1718 ut_ad(update && index);
1719 ut_ad(n <= dict_index_get_n_fields(index));
1720
1721 n_upd_fields = upd_get_n_fields(update);
1722 clust_index = index->table->first_index();
1723
1724 for (i = 0; i < n; i++) {
1725 const dict_field_t *ind_field;
1726 const dict_col_t *col;
1727 ulint col_pos;
1728
1729 ind_field = index->get_field(i);
1730 col = ind_field->col;
1731 col_pos = dict_col_get_clust_pos(col, clust_index);
1732
1733 ut_a(ind_field->prefix_len == 0);
1734 /* Multi-value index doesn't support FK */
1735 ut_ad(!col->is_multi_value());
1736
1737 for (j = 0; j < n_upd_fields; j++) {
1738 const upd_field_t *upd_field = upd_get_nth_field(update, j);
1739
1740 if (col_pos == upd_field->field_no &&
1741 !dfield_datas_are_binary_equal(dtuple_get_nth_field(entry, i),
1742 &upd_field->new_val, 0)) {
1743 return true;
1744 }
1745 }
1746 }
1747
1748 return false;
1749 }
1750
1751 /** Copies the column values from a record.
1752 @param[in] rec record in a clustered index
1753 @param[in] offsets array returned by rec_get_offsets()
1754 @param[in] index clustered index where record resides
1755 @param[in] column first column in a column list, or nullptr */
1756 static inline void row_upd_copy_columns(rec_t *rec, const ulint *offsets,
1757 const dict_index_t *index,
1758 sym_node_t *column) {
1759 const byte *data;
1760 ulint len;
1761
1762 ut_ad(index->is_clustered());
1763
1764 while (column) {
1765 data = rec_get_nth_field_instant(
1766 rec, offsets, column->field_nos[SYM_CLUST_FIELD_NO], index, &len);
1767 eval_node_copy_and_alloc_val(column, data, len);
1768
1769 column = UT_LIST_GET_NEXT(col_var_list, column);
1770 }
1771 }
1772
1773 /** Calculates the new values for fields to update. Note that
1774 row_upd_copy_columns must have been called first. */
1775 static inline void row_upd_eval_new_vals(
1776 upd_t *update) /*!< in/out: update vector */
1777 {
1778 que_node_t *exp;
1779 upd_field_t *upd_field;
1780 ulint n_fields;
1781 ulint i;
1782
1783 n_fields = upd_get_n_fields(update);
1784
1785 for (i = 0; i < n_fields; i++) {
1786 upd_field = upd_get_nth_field(update, i);
1787
1788 exp = upd_field->exp;
1789
1790 eval_exp(exp);
1791
1792 dfield_copy_data(&(upd_field->new_val), que_node_get_val(exp));
1793 }
1794 }
1795
1796 /** Copies the data pointed to by the new values for virtual fields to update
1797 @param[in,out] update an update vector */
1798 static void row_upd_dup_v_new_vals(upd_t *update) {
1799 ut_ad(update != nullptr);
1800
1801 for (ulint j = 0; j < upd_get_n_fields(update); j++) {
1802 upd_field_t *upd_field = upd_get_nth_field(update, j);
1803 if (upd_field->new_val.type.is_virtual()) {
1804 if (dfield_is_multi_value(&upd_field->new_val)) {
1805 dfield_multi_value_dup(&upd_field->new_val, update->heap);
1806 } else {
1807 dfield_dup(&upd_field->new_val, update->heap);
1808 }
1809 }
1810 }
1811 }
1812
1813 /** Stores to the heap the virtual columns that need for any indexes
1814 @param[in,out] node row update node
1815 @param[in,out] update an update vector if it is update
1816 @param[in] thd mysql thread handle
1817 @param[in,out] mysql_table mysql table object */
1818 static void row_upd_store_v_row(upd_node_t *node, upd_t *update, THD *thd,
1819 TABLE *mysql_table) {
1820 mem_heap_t *heap = nullptr;
1821 const dict_table_t *const table = node->table;
1822 bool new_val_v_cols_dup = false;
1823 const ulint n_upd = update ? upd_get_n_fields(update) : 0;
1824
1825 for (ulint col_no = 0; col_no < dict_table_get_n_v_cols(node->table);
1826 col_no++) {
1827 const dict_v_col_t *col = dict_table_get_nth_v_col(node->table, col_no);
1828
1829 if (col->m_col.ord_part) {
1830 dfield_t *dfield = dtuple_get_nth_v_field(node->row, col_no);
1831 ulint i = 0;
1832
1833 /* Check if the value is already in update vector */
1834 for (i = 0; i < n_upd; i++) {
1835 const upd_field_t *upd_field = upd_get_nth_field(update, i);
1836 if (!(upd_field->new_val.type.prtype & DATA_VIRTUAL) ||
1837 upd_field->field_no != col->v_pos) {
1838 continue;
1839 }
1840
1841 dfield_copy_data(dfield, upd_field->old_v_val);
1842 if (dfield_is_multi_value(dfield)) {
1843 dfield_multi_value_dup(dfield, node->heap);
1844 } else {
1845 dfield_dup(dfield, node->heap);
1846 }
1847 break;
1848 }
1849
1850 /* Not updated */
1851 if (i >= n_upd) {
1852 /* If this is an update, then the value
1853 should be in update->old_vrow */
1854 if (update) {
1855 if (update->old_vrow == nullptr) {
1856 /* This only happens in
1857 cascade update. And virtual
1858 column can't be affected,
1859 so it is Ok to set it to NULL */
1860 dfield_set_null(dfield);
1861 } else {
1862 dfield_t *vfield = dtuple_get_nth_v_field(update->old_vrow, col_no);
1863 dfield_copy_data(dfield, vfield);
1864 if (dfield_is_multi_value(dfield)) {
1865 dfield_multi_value_dup(dfield, node->heap);
1866 } else {
1867 dfield_dup(dfield, node->heap);
1868 }
1869 if (dfield_is_null(dfield)) {
1870 if (!new_val_v_cols_dup) {
1871 row_upd_dup_v_new_vals(update);
1872 new_val_v_cols_dup = true;
1873 }
1874 innobase_get_computed_value(node->row, col, table, &heap,
1875 node->heap, thd, mysql_table);
1876 }
1877 }
1878 } else {
1879 /* Need to compute, this happens when
1880 deleting row */
1881 innobase_get_computed_value(node->row, col, table, &heap, node->heap,
1882 thd, mysql_table);
1883 }
1884 }
1885 }
1886 }
1887
1888 if (heap) {
1889 mem_heap_free(heap);
1890 }
1891 }
1892
1893 void row_upd_store_row(upd_node_t *node, THD *thd, TABLE *mysql_table) {
1894 dict_index_t *clust_index;
1895 rec_t *rec;
1896 mem_heap_t *heap = nullptr;
1897 row_ext_t **ext;
1898 ulint offsets_[REC_OFFS_NORMAL_SIZE];
1899 const ulint *offsets;
1900 rec_offs_init(offsets_);
1901
1902 ut_ad(node->pcur->m_latch_mode != BTR_NO_LATCHES);
1903
1904 if (node->row != nullptr) {
1905 mem_heap_empty(node->heap);
1906 }
1907
1908 clust_index = node->table->first_index();
1909
1910 rec = node->pcur->get_rec();
1911
1912 offsets = rec_get_offsets(rec, clust_index, offsets_, ULINT_UNDEFINED,
1913 UT_LOCATION_HERE, &heap);
1914
1915 if (dict_table_has_atomic_blobs(node->table)) {
1916 /* There is no prefix of externally stored columns in
1917 the clustered index record. Build a cache of column
1918 prefixes. */
1919 ext = &node->ext;
1920 } else {
1921 /* REDUNDANT and COMPACT formats store a local
1922 768-byte prefix of each externally stored column.
1923 No cache is needed. */
1924 ext = nullptr;
1925 node->ext = nullptr;
1926 }
1927
1928 node->row = row_build(ROW_COPY_DATA, clust_index, rec, offsets, nullptr,
1929 nullptr, nullptr, ext, node->heap);
1930
1931 if (node->table->n_v_cols) {
1932 row_upd_store_v_row(node, node->is_delete ? nullptr : node->update, thd,
1933 mysql_table);
1934 }
1935
1936 if (node->is_delete) {
1937 node->upd_row = nullptr;
1938 node->upd_ext = nullptr;
1939 } else {
1940 node->upd_row = dtuple_copy(node->row, node->heap);
1941 row_upd_replace(node->upd_row, &node->upd_ext, clust_index, node->update,
1942 node->heap);
1943 }
1944
1945 if (UNIV_LIKELY_NULL(heap)) {
1946 mem_heap_free(heap);
1947 }
1948 }
1949
1950 /** Print a MBR data from disk */
1951 static void srv_mbr_print(const byte *data) {
1952 double a, b, c, d;
1953 a = mach_double_read(data);
1954 data += sizeof(double);
1955 b = mach_double_read(data);
1956 data += sizeof(double);
1957 c = mach_double_read(data);
1958 data += sizeof(double);
1959 d = mach_double_read(data);
1960
1961 ib::info(ER_IB_MSG_1043) << "GIS MBR INFO: " << a << " and " << b << ", " << c
1962 << ", " << d << "\n";
1963 }
1964
1965 /** Delete mark a secondary index entry of a row, when the index
1966 is built on multi-value field
1967 @param[in] index the multi-value index
1968 @param[in] entry the entry to handle on the index
1969 @param[in] thr query thread
1970 @return DB_SUCCESS on success, otherwise error code */
1971 static inline dberr_t row_upd_del_one_multi_sec_index_entry(dict_index_t *index,
1972 dtuple_t *entry,
1973 que_thr_t *thr) {
1974 mtr_t mtr;
1975 btr_pcur_t pcur;
1976 btr_cur_t *btr_cur;
1977 const rec_t *rec;
1978 ulint mode;
1979 ulint flags = 0;
1980 dberr_t err = DB_SUCCESS;
1981 enum row_search_result search_result;
1982 ut_d(trx_t *trx = thr_get_trx(thr));
1983
1984 ut_ad(trx->id != 0);
1985 ut_ad(!index->table->is_intrinsic());
1986 ut_ad(!row_upd_index_is_referenced(index));
1987 ut_ad(index->is_committed());
1988 ut_ad(!dict_index_is_online_ddl(index));
1989
1990 DBUG_EXECUTE_IF("delete_one_multi_sec_index_entry_lock_wait", {
1991 static uint16_t count = 0;
1992 if (index->is_multi_value()) {
1993 ++count;
1994 }
1995 if (count == 2) {
1996 count = 0;
1997 return DB_LOCK_WAIT;
1998 }
1999 });
2000
2001 mtr_start(&mtr);
2002 if (index->table->is_temporary()) {
2003 flags |= BTR_NO_LOCKING_FLAG;
2004 mtr.set_log_mode(MTR_LOG_NO_REDO);
2005 }
2006
2007 /* Set the query thread, so that ibuf_insert_low() will be
2008 able to invoke thd_get_trx(). */
2009 pcur.get_btr_cur()->thr = thr;
2010
2011 mode = (index->table->is_temporary()) ? BTR_MODIFY_LEAF
2012 : BTR_MODIFY_LEAF | BTR_DELETE_MARK;
2013
2014 search_result = row_search_index_entry(index, entry, mode, &pcur, &mtr);
2015
2016 btr_cur = pcur.get_btr_cur();
2017
2018 rec = btr_cur_get_rec(btr_cur);
2019
2020 switch (search_result) {
2021 case ROW_NOT_DELETED_REF: /* should only occur for BTR_DELETE */
2022 ut_error;
2023 break;
2024 case ROW_BUFFERED:
2025 /* Entry was delete marked already. */
2026 break;
2027
2028 case ROW_NOT_FOUND:
2029 ib::error(ER_IB_MSG_1044)
2030 << "Record in index " << index->name << " of table "
2031 << index->table->name << " was not found on update: " << *entry
2032 << " at: " << rec_index_print(rec, index);
2033 #ifdef UNIV_DEBUG
2034 mtr_commit(&mtr);
2035 mtr_start(&mtr);
2036 ut_ad(btr_validate_index(index, nullptr, false));
2037 ut_d(ut_error);
2038 #endif /* UNIV_DEBUG */
2039 ut_o(break);
2040 case ROW_FOUND:
2041 ut_ad(err == DB_SUCCESS);
2042
2043 /* Delete mark the old index record; it can already be
2044 delete marked if we return after a lock wait in
2045 row_ins_sec_index_entry() afterwards */
2046 if (!rec_get_deleted_flag(rec, dict_table_is_comp(index->table))) {
2047 err = btr_cur_del_mark_set_sec_rec(flags, btr_cur, true, thr, &mtr);
2048 if (err != DB_SUCCESS) {
2049 break;
2050 }
2051 }
2052
2053 ut_ad(err == DB_SUCCESS);
2054 break;
2055 }
2056
2057 pcur.close();
2058 mtr_commit(&mtr);
2059
2060 return err;
2061 }
2062
2063 /** Updates secondary index entries of a row, when the index is built on
2064 multi-value field.
2065 @param[in,out] node row update node
2066 @param[in] thr query thread
2067 @param[in] non_mv_upd true if any non-multi-value field on the index
2068 gets updated too
2069 @return DB_SUCCESS if operation successfully completed, else error
2070 code or DB_LOCK_WAIT */
2071 [[nodiscard]] static dberr_t row_upd_multi_sec_index_entry(upd_node_t *node,
2072 que_thr_t *thr,
2073 bool non_mv_upd) {
2074 mem_heap_t *heap;
2075 dict_index_t *index;
2076 dberr_t err = DB_SUCCESS;
2077 ut_d(trx_t *trx = thr_get_trx(thr));
2078
2079 ut_ad(trx->id != 0);
2080
2081 index = node->index;
2082
2083 ut_ad(!dict_index_is_spatial(index));
2084 ut_ad(!index->table->is_intrinsic());
2085 ut_ad(!row_upd_index_is_referenced(index));
2086 ut_ad(index->is_committed());
2087 ut_ad(!dict_index_is_online_ddl(index));
2088
2089 heap = mem_heap_create(1024, UT_LOCATION_HERE);
2090
2091 /* Once this is non-zero, it means the delete phase has been done, and
2092 it only got blocked by locking wait during update phase. */
2093 if (node->upd_multi_val_pos == 0) {
2094 /* Check every multi-value data, if it's indexed on current index,
2095 delete all old values from the index. */
2096 Multi_value_entry_builder_normal mv_entry_builder(
2097 node->row, node->ext, index, heap, true, !non_mv_upd);
2098 for (dtuple_t *entry = mv_entry_builder.begin(node->del_multi_val_pos);
2099 entry != nullptr; entry = mv_entry_builder.next()) {
2100 err = row_upd_del_one_multi_sec_index_entry(index, entry, thr);
2101 if (err != DB_SUCCESS) {
2102 node->del_multi_val_pos = mv_entry_builder.last_multi_value_position();
2103 goto func_exit;
2104 }
2105 }
2106
2107 node->del_multi_val_pos = 0;
2108 }
2109
2110 {
2111 Multi_value_entry_builder_normal mv_entry_builder(
2112 node->upd_row, node->upd_ext, index, heap, true, !non_mv_upd);
2113 for (dtuple_t *entry = mv_entry_builder.begin(node->upd_multi_val_pos);
2114 entry != nullptr; entry = mv_entry_builder.next()) {
2115 err = row_ins_sec_index_entry(index, entry, thr, false);
2116
2117 if (err != DB_SUCCESS) {
2118 node->upd_multi_val_pos = mv_entry_builder.last_multi_value_position();
2119 goto func_exit;
2120 }
2121 }
2122
2123 node->upd_multi_val_pos = 0;
2124 }
2125
2126 func_exit:
2127 mem_heap_free(heap);
2128 return err;
2129 }
2130
2131 /** Updates a secondary index entry of a row.
2132 @param[in] node row update node
2133 @param[in] old_entry the old entry to search, or nullptr then it
2134 has to be created in this function
2135 @param[in] thr query thread
2136 @return DB_SUCCESS if operation successfully completed, else error
2137 code or DB_LOCK_WAIT */
2138 [[nodiscard]] static dberr_t row_upd_sec_index_entry_low(upd_node_t *node,
2139 dtuple_t *old_entry,
2140 que_thr_t *thr) {
2141 mtr_t mtr;
2142 const rec_t *rec;
2143 btr_pcur_t pcur;
2144 mem_heap_t *heap;
2145 dtuple_t *entry = old_entry;
2146 dict_index_t *index;
2147 btr_cur_t *btr_cur;
2148 dberr_t err = DB_SUCCESS;
2149 trx_t *trx = thr_get_trx(thr);
2150 ulint mode;
2151 ulint flags = 0;
2152 enum row_search_result search_result;
2153
2154 ut_ad(trx->id != 0);
2155
2156 index = node->index;
2157
2158 DBUG_EXECUTE_IF("row_upd_sec_index_entry_lock_wait", {
2159 static uint16_t count = 0;
2160 if (index->is_multi_value()) {
2161 ++count;
2162 }
2163 if (count == 2) {
2164 count = 0;
2165 return (DB_LOCK_WAIT);
2166 }
2167 });
2168
2169 auto referenced = row_upd_index_is_referenced(index);
2170
2171 heap = mem_heap_create(1024, UT_LOCATION_HERE);
2172
2173 if (!node->is_delete && dict_index_is_spatial(index) &&
2174 index->srid_is_valid) {
2175 const dict_col_t *col = index->get_field(0)->col;
2176 ulint col_no = dict_col_get_no(col);
2177 const dfield_t *dfield = dtuple_get_nth_field(node->upd_row, col_no);
2178 uchar *dptr = static_cast<uchar *>(dfield_get_data(dfield));
2179 uint32_t srid = uint4korr(dptr);
2180 if (index->srid != srid) {
2181 return DB_CANT_CREATE_GEOMETRY_OBJECT;
2182 }
2183 }
2184
2185 /* Build old index entry */
2186 if (entry == nullptr) {
2187 entry = row_build_index_entry(node->row, node->ext, index, heap);
2188 ut_a(entry);
2189 }
2190
2191 if (!index->table->is_intrinsic()) {
2192 log_free_check();
2193 }
2194
2195 ut_ad(trx_can_be_handled_by_current_thread(trx));
2196 DEBUG_SYNC(trx->mysql_thd, "before_row_upd_sec_index_entry");
2197
2198 mtr_start(&mtr);
2199
2200 /* Disable REDO logging as lifetime of temp-tables is limited to
2201 server or connection lifetime and so REDO information is not needed
2202 on restart for recovery.
2203 Disable locking as temp-tables are not shared across connection. */
2204 if (index->table->is_temporary()) {
2205 flags |= BTR_NO_LOCKING_FLAG;
2206 mtr.set_log_mode(MTR_LOG_NO_REDO);
2207
2208 if (index->table->is_intrinsic()) {
2209 flags |= BTR_NO_UNDO_LOG_FLAG;
2210 }
2211 }
2212
2213 if (!index->is_committed()) {
2214 /* The index->online_status may change if the index is
2215 or was being created online, but not committed yet. It
2216 is protected by index->lock. */
2217
2218 mtr_s_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE);
2219
2220 switch (dict_index_get_online_status(index)) {
2221 case ONLINE_INDEX_COMPLETE:
2222 /* This is a normal index. Do not log anything.
2223 Perform the update on the index tree directly. */
2224 break;
2225 case ONLINE_INDEX_CREATION:
2226 /* Log a DELETE and optionally INSERT. */
2227 row_log_online_op(index, entry, 0);
2228
2229 if (!node->is_delete) {
2230 mem_heap_empty(heap);
2231 entry =
2232 row_build_index_entry(node->upd_row, node->upd_ext, index, heap);
2233 ut_a(entry);
2234 row_log_online_op(index, entry, trx->id);
2235 }
2236 [[fallthrough]];
2237 case ONLINE_INDEX_ABORTED:
2238 case ONLINE_INDEX_ABORTED_DROPPED:
2239 mtr_commit(&mtr);
2240 goto func_exit;
2241 }
2242
2243 /* We can only buffer delete-mark operations if there
2244 are no foreign key constraints referring to the index.
2245 Change buffering is disabled for temporary tables and
2246 spatial index. */
2247 mode = (referenced || index->table->is_temporary() ||
2248 dict_index_is_spatial(index))
2249 ? BTR_MODIFY_LEAF | BTR_ALREADY_S_LATCHED
2250 : BTR_MODIFY_LEAF | BTR_ALREADY_S_LATCHED | BTR_DELETE_MARK;
2251 } else {
2252 /* For secondary indexes,
2253 index->online_status==ONLINE_INDEX_COMPLETE if
2254 index->is_committed(). */
2255 ut_ad(!dict_index_is_online_ddl(index));
2256
2257 /* We can only buffer delete-mark operations if there
2258 are no foreign key constraints referring to the index.
2259 Change buffering is disabled for temporary tables and
2260 spatial index. */
2261 mode = (referenced || index->table->is_temporary() ||
2262 dict_index_is_spatial(index))
2263 ? BTR_MODIFY_LEAF
2264 : BTR_MODIFY_LEAF | BTR_DELETE_MARK;
2265 }
2266
2267 if (dict_index_is_spatial(index)) {
2268 ut_ad(mode & BTR_MODIFY_LEAF);
2269 mode |= BTR_RTREE_DELETE_MARK;
2270 }
2271
2272 /* Set the query thread, so that ibuf_insert_low() will be
2273 able to invoke thd_get_trx(). */
2274 pcur.get_btr_cur()->thr = thr;
2275
2276 search_result = row_search_index_entry(index, entry, mode, &pcur, &mtr);
2277
2278 btr_cur = pcur.get_btr_cur();
2279
2280 rec = btr_cur_get_rec(btr_cur);
2281
2282 switch (search_result) {
2283 case ROW_NOT_DELETED_REF: /* should only occur for BTR_DELETE */
2284 ut_error;
2285 break;
2286 case ROW_BUFFERED:
2287 /* Entry was delete marked already. */
2288 break;
2289
2290 case ROW_NOT_FOUND:
2291 if (!index->is_committed()) {
2292 /* When online CREATE INDEX copied the update
2293 that we already made to the clustered index,
2294 and completed the secondary index creation
2295 before we got here, the old secondary index
2296 record would not exist. The CREATE INDEX
2297 should be waiting for a MySQL meta-data lock
2298 upgrade at least until this UPDATE returns.
2299 After that point, set_committed(true) would be
2300 invoked by commit_inplace_alter_table(). */
2301 break;
2302 }
2303
2304 if (dict_index_is_spatial(index) && btr_cur->rtr_info->fd_del) {
2305 /* We found the record, but a delete marked */
2306 break;
2307 }
2308
2309 ib::error(ER_IB_MSG_1044)
2310 << "Record in index " << index->name << " of table "
2311 << index->table->name << " was not found on update: " << *entry
2312 << " at: " << rec_index_print(rec, index);
2313
2314 if (dict_index_is_spatial(index)) {
2315 srv_mbr_print((unsigned char *)entry->fields[0].data);
2316 }
2317
2318 #ifdef UNIV_DEBUG
2319 mtr_commit(&mtr);
2320 mtr_start(&mtr);
2321 ut_ad(btr_validate_index(index, nullptr, false));
2322 ut_d(ut_error);
2323 #endif /* UNIV_DEBUG */
2324 ut_o(break);
2325 case ROW_FOUND:
2326 ut_ad(err == DB_SUCCESS);
2327
2328 /* Delete mark the old index record; it can already be
2329 delete marked if we return after a lock wait in
2330 row_ins_sec_index_entry() below */
2331 if (!rec_get_deleted_flag(rec, dict_table_is_comp(index->table))) {
2332 err = btr_cur_del_mark_set_sec_rec(flags, btr_cur, true, thr, &mtr);
2333 if (err != DB_SUCCESS) {
2334 break;
2335 }
2336 }
2337
2338 ut_ad(err == DB_SUCCESS);
2339
2340 if (referenced) {
2341 ulint *offsets;
2342
2343 offsets = rec_get_offsets(rec, index, nullptr, ULINT_UNDEFINED,
2344 UT_LOCATION_HERE, &heap);
2345
2346 /* NOTE that the following call loses
2347 the position of pcur ! */
2348 err = row_upd_check_references_constraints(node, &pcur, index->table,
2349 index, offsets, thr, &mtr);
2350 }
2351 break;
2352 }
2353
2354 pcur.close();
2355 mtr_commit(&mtr);
2356
2357 if (node->is_delete || err != DB_SUCCESS) {
2358 goto func_exit;
2359 }
2360
2361 mem_heap_empty(heap);
2362
2363 /* Build a new index entry */
2364 entry = row_build_index_entry(node->upd_row, node->upd_ext, index, heap);
2365 ut_a(entry);
2366
2367 /* Insert new index entry */
2368 err = row_ins_sec_index_entry(index, entry, thr, false);
2369
2370 func_exit:
2371 mem_heap_free(heap);
2372
2373 return err;
2374 }
2375
2376 /** Updates a secondary index entry of a row.
2377 @param[in] node row update node
2378 @param[in] thr query thread
2379 @return DB_SUCCESS if operation successfully completed, else error
2380 code or DB_LOCK_WAIT */
2381 [[nodiscard]] static inline dberr_t row_upd_sec_index_entry(upd_node_t *node,
2382 que_thr_t *thr) {
2383 return row_upd_sec_index_entry_low(node, nullptr, thr);
2384 }
2385
2386 /** Delete secondary index entries of a row, when the index is built on
2387 multi-value field.
2388 @param[in,out] node row update node
2389 @param[in] thr query thread
2390 @return DB_SUCCESS if operation successfully completed, else error
2391 code or DB_LOCK_WAIT */
2392 [[nodiscard]] static inline dberr_t row_upd_del_multi_sec_index_entry(
2393 upd_node_t *node, que_thr_t *thr) {
2394 mem_heap_t *heap;
2395 dberr_t err = DB_SUCCESS;
2396 heap = mem_heap_create(1024, UT_LOCATION_HERE);
2397
2398 ut_d(trx_t *trx = thr_get_trx(thr));
2399
2400 ut_ad(trx->id != 0);
2401 ut_ad(!node->index->table->is_intrinsic());
2402 ut_ad(!row_upd_index_is_referenced(node->index));
2403 ut_ad(node->index->is_committed());
2404 ut_ad(!dict_index_is_online_ddl(node->index));
2405
2406 {
2407 Multi_value_entry_builder_normal mv_entry_builder(
2408 node->row, node->ext, node->index, heap, true, false);
2409
2410 for (dtuple_t *entry = mv_entry_builder.begin(node->del_multi_val_pos);
2411 entry != nullptr; entry = mv_entry_builder.next()) {
2412 err = row_upd_sec_index_entry_low(node, entry, thr);
2413
2414 if (err != DB_SUCCESS) {
2415 node->del_multi_val_pos = mv_entry_builder.last_multi_value_position();
2416 goto func_exit;
2417 }
2418 }
2419
2420 node->del_multi_val_pos = 0;
2421 ut_ad(node->upd_multi_val_pos == 0);
2422 }
2423
2424 func_exit:
2425 mem_heap_free(heap);
2426
2427 return err;
2428 }
2429
2430 /** Updates the secondary index record if it is changed in the row update or
2431 deletes it if this is a delete.
2432 @return DB_SUCCESS if operation successfully completed, else error
2433 code or DB_LOCK_WAIT */
2434 [[nodiscard]] static dberr_t row_upd_sec_step(
2435 upd_node_t *node, /*!< in: row update node */
2436 que_thr_t *thr) /*!< in: query thread */
2437 {
2438 ut_ad((node->state == UPD_NODE_UPDATE_ALL_SEC) ||
2439 (node->state == UPD_NODE_UPDATE_SOME_SEC));
2440 ut_ad(!node->index->is_clustered());
2441
2442 bool non_mv_upd = false;
2443
2444 if (node->state == UPD_NODE_UPDATE_ALL_SEC ||
2445 row_upd_changes_ord_field_binary(
2446 node->index, node->update, thr, node->row, node->ext,
2447 (node->index->is_multi_value() ? &non_mv_upd : nullptr))) {
2448 if (node->index->is_multi_value()) {
2449 if (node->is_delete) {
2450 return (row_upd_del_multi_sec_index_entry(node, thr));
2451 } else {
2452 return (row_upd_multi_sec_index_entry(
2453 node, thr, (node->state == UPD_NODE_UPDATE_ALL_SEC || non_mv_upd)));
2454 }
2455 } else {
2456 return (row_upd_sec_index_entry(node, thr));
2457 }
2458 }
2459
2460 return DB_SUCCESS;
2461 }
2462
2463 /** Mark non-updated off-page columns inherited when the primary key is
2464 updated. We must mark them as inherited in entry, so that they are not
2465 freed in a rollback. A limited version of this function used to be
2466 called btr_cur_mark_dtuple_inherited_extern().
2467 @param[in] index Index describing record's fields.
2468 @param[in] rec old record, or NULL
2469 @param[in] offsets rec_get_offsets(rec), or NULL
2470 @param[in,out] entry updated entry to be inserted into the clustered index
2471 @param[in] update update vector
2472 @return whether any columns were inherited */
2473 static bool row_upd_clust_rec_by_insert_inherit_func(
2474 const dict_index_t *index, const rec_t *rec,
2475 IF_DEBUG(const ulint *offsets, ) dtuple_t *entry, const upd_t *update) {
2476 bool inherit = false;
2477 ulint i;
2478
2479 ut_ad(!rec == !offsets);
2480 ut_ad(!rec || rec_offs_any_extern(offsets));
2481
2482 for (i = 0; i < dtuple_get_n_fields(entry); i++) {
2483 dfield_t *dfield = dtuple_get_nth_field(entry, i);
2484 ulint len;
2485
2486 ut_ad(!offsets ||
2487 !rec_offs_nth_extern(index, offsets, i) == !dfield_is_ext(dfield) ||
2488 upd_get_field_by_field_no(update, i, false));
2489 if (!dfield_is_ext(dfield) || upd_get_field_by_field_no(update, i, false)) {
2490 continue;
2491 }
2492
2493 lob::ref_t ref(dfield->blobref());
2494
2495 #ifdef UNIV_DEBUG
2496 if (UNIV_LIKELY(rec != nullptr)) {
2497 (void)rec_get_nth_field(index, rec, offsets, i, &len);
2498 ut_ad(len == dfield_get_len(dfield));
2499 ut_ad(len != UNIV_SQL_NULL);
2500 ut_ad(len >= BTR_EXTERN_FIELD_REF_SIZE);
2501
2502 /* The pointer must not be zero. */
2503 ut_ad(!ref.is_null());
2504
2505 /* The BLOB must be owned. */
2506 ut_ad(ref.is_owner());
2507 }
2508 #endif /* UNIV_DEBUG */
2509
2510 len = dfield_get_len(dfield);
2511 ut_a(len != UNIV_SQL_NULL);
2512 ut_a(len >= BTR_EXTERN_FIELD_REF_SIZE);
2513
2514 /* The pointer must not be zero. */
2515 ut_a(!ref.is_null());
2516
2517 /* The BLOB must be owned, unless we are resuming from
2518 a lock wait and we already had disowned the BLOB. */
2519 ut_a(rec == nullptr || ref.is_owner());
2520
2521 ref.set_owner(true, nullptr);
2522 ref.set_inherited(true, nullptr);
2523
2524 /* The BTR_EXTERN_INHERITED_FLAG only matters in
2525 rollback of a fresh insert (insert_undo log).
2526 Purge (operating on update_undo log) will always free
2527 the extern fields of a delete-marked row. */
2528
2529 inherit = true;
2530 }
2531
2532 return inherit;
2533 }
2534
2535 static inline bool row_upd_clust_rec_by_insert_inherit(
2536 const dict_index_t *index, const rec_t *rec,
2537 const ulint *offsets [[maybe_unused]], dtuple_t *entry,
2538 const upd_t *update) {
2539 return row_upd_clust_rec_by_insert_inherit_func(
2540 index, rec, IF_DEBUG(offsets, ) entry, update);
2541 }
2542
2543 /** Marks the clustered index record deleted and inserts the updated version
2544 of the record to the index. This function should be used when the ordering
2545 fields of the clustered index record change. This should be quite rare in
2546 database applications.
2547 @return DB_SUCCESS if operation successfully completed, else error
2548 code or DB_LOCK_WAIT */
2549 [[nodiscard]] static dberr_t row_upd_clust_rec_by_insert(
2550 ulint flags, /*!< in: undo logging and locking flags */
2551 upd_node_t *node, /*!< in/out: row update node */
2552 dict_index_t *index, /*!< in: clustered index of the record */
2553 que_thr_t *thr, /*!< in: query thread */
2554 bool referenced, /*!< in: true if index may be referenced in
2555 a foreign key constraint */
2556 mtr_t *mtr) /*!< in/out: mtr; gets committed here */
2557 {
2558 mem_heap_t *heap;
2559 btr_pcur_t *pcur;
2560 btr_cur_t *btr_cur;
2561 trx_t *trx;
2562 dict_table_t *table;
2563 dtuple_t *entry;
2564 dberr_t err;
2565 rec_t *rec;
2566 ulint *offsets = nullptr;
2567
2568 ut_ad(node);
2569 ut_ad(index->is_clustered());
2570
2571 trx = thr_get_trx(thr);
2572 table = node->table;
2573 pcur = node->pcur;
2574 btr_cur = pcur->get_btr_cur();
2575
2576 heap = mem_heap_create(100, UT_LOCATION_HERE);
2577
2578 entry = row_build_index_entry_low(node->upd_row, node->upd_ext, index, heap,
2579 ROW_BUILD_FOR_INSERT);
2580 ut_ad(dtuple_get_info_bits(entry) == 0);
2581
2582 row_upd_index_entry_sys_field(entry, index, DATA_TRX_ID, trx->id);
2583
2584 switch (node->state) {
2585 default:
2586 ut_error;
2587 case UPD_NODE_INSERT_CLUSTERED:
2588 /* A lock wait occurred in row_ins_clust_index_entry() in
2589 the previous invocation of this function. */
2590 row_upd_clust_rec_by_insert_inherit(index, nullptr, nullptr, entry,
2591 node->update);
2592 break;
2593 case UPD_NODE_UPDATE_CLUSTERED:
2594 /* This is the first invocation of the function where
2595 we update the primary key. Delete-mark the old record
2596 in the clustered index and prepare to insert a new entry. */
2597 rec = btr_cur_get_rec(btr_cur);
2598 offsets = rec_get_offsets(rec, index, nullptr, ULINT_UNDEFINED,
2599 UT_LOCATION_HERE, &heap);
2600 ut_ad(page_rec_is_user_rec(rec));
2601
2602 if (rec_get_deleted_flag(rec, rec_offs_comp(offsets))) {
2603 /* If the clustered index record is already delete
2604 marked, then we are here after a DB_LOCK_WAIT.
2605 Skip delete marking clustered index and disowning
2606 its blobs. */
2607 ut_ad(rec_get_trx_id(rec, index) == trx->id);
2608 ut_ad(!trx_undo_roll_ptr_is_insert(
2609 row_get_rec_roll_ptr(rec, index, offsets)));
2610 goto check_fk;
2611 }
2612
2613 err =
2614 btr_cur_del_mark_set_clust_rec(flags, btr_cur_get_block(btr_cur), rec,
2615 index, offsets, thr, node->row, mtr);
2616 if (err != DB_SUCCESS) {
2617 err_exit:
2618 mtr_commit(mtr);
2619 mem_heap_free(heap);
2620 return (err);
2621 }
2622
2623 /* If the the new row inherits externally stored
2624 fields (off-page columns a.k.a. BLOBs) from the
2625 delete-marked old record, mark them disowned by the
2626 old record and owned by the new entry. */
2627
2628 if (rec_offs_any_extern(offsets)) {
2629 if (row_upd_clust_rec_by_insert_inherit(index, rec, offsets, entry,
2630 node->update)) {
2631 /* The blobs are disowned here, expecting the
2632 insert down below to inherit them. But if the
2633 insert fails, then this disown will be undone
2634 when the operation is rolled back. */
2635
2636 lob::BtrContext btr_ctx(mtr, pcur, index, rec, offsets,
2637 btr_cur_get_block(btr_cur));
2638
2639 btr_ctx.disown_inherited_fields(node->update);
2640 }
2641 }
2642 check_fk:
2643 if (referenced) {
2644 /* NOTE that the following call loses
2645 the position of pcur ! */
2646
2647 err = row_upd_check_references_constraints(node, pcur, table, index,
2648 offsets, thr, mtr);
2649
2650 if (err != DB_SUCCESS) {
2651 goto err_exit;
2652 }
2653 }
2654 }
2655
2656 mtr_commit(mtr);
2657
2658 err = row_ins_clust_index_entry(index, entry, thr, false);
2659 node->state = UPD_NODE_INSERT_CLUSTERED;
2660
2661 mem_heap_free(heap);
2662
2663 return (err);
2664 }
2665
2666 /** Get the new autoinc counter from the update vector when there is
2667 an autoinc field defined in this table.
2668 @param[in] update update vector for the clustered index
2669 @param[in] autoinc_field_no autoinc field's order in clustered index
2670 @return the new counter if we find it in the update vector, otherwise 0.
2671 We don't mind that the new counter happens to be 0, we just care about
2672 non-zero counters. */
2673 uint64_t row_upd_get_new_autoinc_counter(const upd_t *update,
2674 ulint autoinc_field_no) {
2675 ulint n_fields = update->n_fields;
2676 const dfield_t *field = nullptr;
2677
2678 for (ulint i = 0; i < n_fields; ++i) {
2679 const upd_field_t *upd_field = upd_get_nth_field(update, i);
2680
2681 if (upd_field->field_no == autoinc_field_no && !upd_field->is_virtual()) {
2682 /* We should double check the field to see if this
2683 is a virtual column, which is on virtual index
2684 instead of clustered index */
2685 field = &upd_field->new_val;
2686 break;
2687 }
2688 }
2689
2690 if (field != nullptr) {
2691 return (row_parse_int_from_field(field));
2692 }
2693
2694 return (0);
2695 }
2696
2697 /** If the table has autoinc column and the counter is updated to
2698 some bigger value, we need to log the new autoinc counter. We will
2699 use the given mtr to do logging for performance reasons.
2700 @param[in] node Row update node
2701 @param[in,out] mtr Mini-transaction
2702 @return true if auto increment needs to be persisted to DD table buffer. */
2703 static bool row_upd_check_autoinc_counter(const upd_node_t *node, mtr_t *mtr) {
2704 dict_table_t *table = node->table;
2705
2706 if (!dict_table_has_autoinc_col(table) || table->is_temporary() ||
2707 node->row == nullptr) {
2708 return false;
2709 }
2710
2711 /* If the node->row hasn't been prepared, there must
2712 no order field change and autoinc field should keep
2713 as is. Otherwise, we need to check if autoinc field
2714 would be changed to a bigger number. */
2715 uint64_t new_counter;
2716
2717 new_counter =
2718 row_upd_get_new_autoinc_counter(node->update, table->autoinc_field_no);
2719
2720 if (new_counter == 0) {
2721 return false;
2722 }
2723
2724 uint64_t old_counter;
2725 const dict_index_t *index;
2726
2727 index = table->first_index();
2728
2729 /* The autoinc field order in row is not the
2730 same as in clustered index, we need to get
2731 the column number in the table instead. */
2732 old_counter = row_get_autoinc_counter(
2733 node->row, index->get_col_no(table->autoinc_field_no));
2734
2735 bool persist_autoinc = false;
2736
2737 /* We just check if the updated counter is bigger than
2738 the old one, which may result in more redo logs, since
2739 this is safer than checking with the counter in table
2740 object. */
2741 if (new_counter > old_counter) {
2742 persist_autoinc = dict_table_autoinc_log(table, new_counter, mtr);
2743 }
2744
2745 return persist_autoinc;
2746 }
2747
2748 void upd_t::append(const upd_field_t &field) {
2749 if (static_cast<size_t>(n_fields) < n_capacity) {
2750 fields[n_fields++] = field;
2751 return;
2752 }
2753
2754 /*
2755 Extend the fields array to accommodate new field. Size is doubled due to the
2756 following reasons::
2757 1. We want to avoid frequent memory reallocations
2758 2. We cannot allocate lot of space upfront as upd_create is called in
2759 frequent code path, and append is called rarely
2760 */
2761 n_capacity = n_capacity * 2;
2762 upd_field_t *temp_fields = static_cast<upd_field_t *>(
2763 mem_heap_zalloc(heap, sizeof(upd_field_t) * n_capacity));
2764 ut_ad(temp_fields);
2765
2766 memcpy(temp_fields, fields, sizeof(upd_field_t) * n_fields);
2767 temp_fields[n_fields++] = field;
2768
2769 /* Fields array is allocated on heap and will be freed with the heap */
2770 fields = temp_fields;
2771 }
2772
2773 /** Updates a clustered index record of a row when the ordering fields do
2774 not change.
2775 @param[in] flags undo logging and locking flags
2776 @param[in] node row update node
2777 @param[in] index clustered index
2778 @param[in] offsets rec_get_offsets() on node->pcur
2779 @param[in,out] offsets_heap memory heap, can be emptied
2780 @param[in] thr query thread
2781 @param[in] mtr mtr; gets committed here
2782 @return DB_SUCCESS if operation successfully completed, else error
2783 code or DB_LOCK_WAIT */
2784 [[nodiscard]] static dberr_t row_upd_clust_rec(ulint flags, upd_node_t *node,
2785 dict_index_t *index,
2786 ulint *offsets,
2787 mem_heap_t **offsets_heap,
2788 que_thr_t *thr, mtr_t *mtr) {
2789 mem_heap_t *heap = nullptr;
2790 big_rec_t *big_rec = nullptr;
2791 btr_pcur_t *pcur;
2792 btr_cur_t *btr_cur;
2793 dberr_t err = DB_SUCCESS;
2794 bool persist_autoinc = false;
2795 bool is_old_or_new_rec_extern = false;
2796 const dtuple_t *rebuilt_old_pk = nullptr;
2797 trx_id_t trx_id = thr_get_trx(thr)->id;
2798 trx_t *trx = thr_get_trx(thr);
2799
2800 ut_ad(node);
2801 ut_ad(index->is_clustered());
2802 ut_ad(!thr_get_trx(thr)->in_rollback);
2803
2804 pcur = node->pcur;
2805 btr_cur = pcur->get_btr_cur();
2806
2807 ut_ad(btr_cur->index == index);
2808 ut_ad(!rec_get_deleted_flag(btr_cur_get_rec(btr_cur),
2809 dict_table_is_comp(index->table)));
2810 ut_ad(rec_offs_validate(btr_cur_get_rec(btr_cur), index, offsets));
2811
2812 if (dict_index_is_online_ddl(index)) {
2813 is_old_or_new_rec_extern = rec_offs_any_extern(offsets);
2814 rebuilt_old_pk = row_log_table_get_pk(btr_cur_get_rec(btr_cur), index,
2815 offsets, nullptr, &heap);
2816 if (row_log_table_get_error(index) == DB_INDEX_CORRUPT) {
2817 err = DB_CORRUPTION;
2818
2819 mtr->commit();
2820 goto func_exit;
2821 }
2822 }
2823
2824 /* Check and log if necessary at the beginning, to prevent any
2825 further potential deadlock */
2826 persist_autoinc = row_upd_check_autoinc_counter(node, mtr);
2827
2828 /* Try optimistic updating of the record, keeping changes within
2829 the page; we do not check locks because we assume the x-lock on the
2830 record to update */
2831
2832 if (node->cmpl_info & UPD_NODE_NO_SIZE_CHANGE) {
2833 err = btr_cur_update_in_place(flags | BTR_NO_LOCKING_FLAG, btr_cur, offsets,
2834 node->update, node->cmpl_info, thr,
2835 thr_get_trx(thr)->id, mtr);
2836 } else {
2837 err = btr_cur_optimistic_update(
2838 flags | BTR_NO_LOCKING_FLAG, btr_cur, &offsets, offsets_heap,
2839 node->update, node->cmpl_info, thr, thr_get_trx(thr)->id, mtr);
2840 }
2841
2842 if (err == DB_SUCCESS) {
2843 goto success;
2844 }
2845
2846 mtr->commit();
2847
2848 if (buf_LRU_buf_pool_running_out()) {
2849 err = DB_LOCK_TABLE_FULL;
2850 goto func_exit;
2851 }
2852 /* We may have to modify the tree structure: do a pessimistic descent
2853 down the index tree */
2854
2855 mtr->start();
2856
2857 /* Disable REDO logging as lifetime of temp-tables is limited to
2858 server or connection lifetime and so REDO information is not needed
2859 on restart for recovery.
2860 Disable locking as temp-tables are not shared across connection. */
2861 if (index->table->is_temporary()) {
2862 flags |= BTR_NO_LOCKING_FLAG;
2863 mtr->set_log_mode(MTR_LOG_NO_REDO);
2864
2865 if (index->table->is_intrinsic()) {
2866 flags |= BTR_NO_UNDO_LOG_FLAG;
2867 }
2868 }
2869
2870 /* NOTE: this transaction has an s-lock or x-lock on the record and
2871 therefore other transactions cannot modify the record when we have no
2872 latch on the page. In addition, we assume that other query threads of
2873 the same transaction do not modify the record in the meantime.
2874 Therefore we can assert that the restoration of the cursor succeeds. */
2875
2876 ut_a(pcur->restore_position(BTR_MODIFY_TREE, mtr, UT_LOCATION_HERE));
2877
2878 ut_ad(
2879 !rec_get_deleted_flag(pcur->get_rec(), dict_table_is_comp(index->table)));
2880
2881 if (!heap) {
2882 heap = mem_heap_create(1024, UT_LOCATION_HERE);
2883 }
2884
2885 err = btr_cur_pessimistic_update(
2886 flags | BTR_NO_LOCKING_FLAG | BTR_KEEP_POS_FLAG, btr_cur, &offsets,
2887 offsets_heap, heap, &big_rec, node->update, node->cmpl_info, thr, trx_id,
2888 trx->undo_no, mtr);
2889 if (big_rec) {
2890 ut_a(err == DB_SUCCESS);
2891
2892 DEBUG_SYNC_C("before_row_upd_extern");
2893 err = lob::btr_store_big_rec_extern_fields(
2894 trx, pcur, node->update, offsets, big_rec, mtr, lob::OPCODE_UPDATE);
2895 DEBUG_SYNC_C("after_row_upd_extern");
2896 }
2897
2898 if (err == DB_SUCCESS) {
2899 success:
2900 if (dict_index_is_online_ddl(index)) {
2901 dtuple_t *new_v_row = nullptr;
2902 dtuple_t *old_v_row = nullptr;
2903
2904 /* In case UPDATE modifies extern BLOB and makes it fit within record
2905 after above update, we still need old virtual col */
2906 is_old_or_new_rec_extern |= rec_offs_any_extern(offsets);
2907
2908 if (!(node->cmpl_info & UPD_NODE_NO_ORD_CHANGE)) {
2909 new_v_row = node->upd_row;
2910 old_v_row = node->update->old_vrow;
2911 } else if (is_old_or_new_rec_extern) {
2912 /* Row log treats UPDATE on extern BLOB as DELETE + INSERT. This
2913 requires virtual col info. Since no change in virtual col, new value is
2914 same as old */
2915 old_v_row = node->update->old_vrow;
2916 new_v_row = old_v_row;
2917 }
2918
2919 row_log_table_update(btr_cur_get_rec(btr_cur), index, offsets,
2920 rebuilt_old_pk, new_v_row, old_v_row);
2921 }
2922 }
2923
2924 mtr->commit();
2925
2926 func_exit:
2927 if (heap) {
2928 mem_heap_free(heap);
2929 }
2930
2931 if (big_rec) {
2932 dtuple_big_rec_free(big_rec);
2933 }
2934
2935 /* Persist auto increment value to DD buffer table if requested. Do it after
2936 closing the mini transaction and releasing latches. */
2937 if (persist_autoinc) {
2938 dict_table_persist_to_dd_table_buffer(node->table);
2939 }
2940
2941 return err;
2942 }
2943
2944 /** Delete marks a clustered index record.
2945 @return DB_SUCCESS if operation successfully completed, else error code */
2946 [[nodiscard]] static dberr_t row_upd_del_mark_clust_rec(
2947 ulint flags, /*!< in: undo logging and locking flags */
2948 upd_node_t *node, /*!< in: row update node */
2949 dict_index_t *index, /*!< in: clustered index */
2950 ulint *offsets, /*!< in/out: rec_get_offsets() for the
2951 record under the cursor */
2952 que_thr_t *thr, /*!< in: query thread */
2953 bool referenced,
2954 /*!< in: true if index may be referenced in
2955 a foreign key constraint */
2956 mtr_t *mtr) /*!< in: mtr; gets committed here */
2957 {
2958 btr_pcur_t *pcur;
2959 btr_cur_t *btr_cur;
2960 dberr_t err;
2961
2962 ut_ad(node);
2963 ut_ad(index->is_clustered());
2964 ut_ad(node->is_delete);
2965
2966 pcur = node->pcur;
2967 btr_cur = pcur->get_btr_cur();
2968
2969 /* Store row because we have to build also the secondary index
2970 entries */
2971
2972 row_upd_store_row(node, thr_get_trx(thr)->mysql_thd,
2973 thr->prebuilt ? thr->prebuilt->m_mysql_table : nullptr);
2974
2975 /* Mark the clustered index record deleted; we do not have to check
2976 locks, because we assume that we have an x-lock on the record */
2977
2978 err = btr_cur_del_mark_set_clust_rec(flags, btr_cur_get_block(btr_cur),
2979 btr_cur_get_rec(btr_cur), index, offsets,
2980 thr, node->row, mtr);
2981 if (err == DB_SUCCESS && referenced) {
2982 /* NOTE that the following call loses the position of pcur ! */
2983
2984 err = row_upd_check_references_constraints(node, pcur, index->table, index,
2985 offsets, thr, mtr);
2986 }
2987
2988 mtr_commit(mtr);
2989
2990 return (err);
2991 }
2992
2993 /** Updates the clustered index record.
2994 @return DB_SUCCESS if operation successfully completed, DB_LOCK_WAIT
2995 in case of a lock wait, else error code */
2996 [[nodiscard]] static dberr_t row_upd_clust_step(
2997 upd_node_t *node, /*!< in: row update node */
2998 que_thr_t *const thr) /*!< in: query thread */
2999 {
3000 dict_index_t *index;
3001 btr_pcur_t *pcur;
3002 dberr_t err;
3003 mtr_t mtr;
3004 rec_t *rec;
3005 mem_heap_t *heap = nullptr;
3006 ulint offsets_[REC_OFFS_NORMAL_SIZE];
3007 ulint *offsets;
3008 ulint flags = 0;
3009 trx_t *const trx = thr_get_trx(thr);
3010 rec_offs_init(offsets_);
3011
3012 index = node->table->first_index();
3013
3014 auto referenced = row_upd_index_is_referenced(index);
3015
3016 pcur = node->pcur;
3017
3018 /* We have to restore the cursor to its position */
3019
3020 mtr_start(&mtr);
3021
3022 /* Disable REDO logging as lifetime of temp-tables is limited to
3023 server or connection lifetime and so REDO information is not needed
3024 on restart for recovery.
3025 Disable locking as temp-tables are not shared across connection. */
3026 if (index->table->is_temporary()) {
3027 flags |= BTR_NO_LOCKING_FLAG;
3028 mtr.set_log_mode(MTR_LOG_NO_REDO);
3029
3030 if (index->table->is_intrinsic()) {
3031 flags |= BTR_NO_UNDO_LOG_FLAG;
3032 }
3033 }
3034
3035 /* If the restoration does not succeed, then the same
3036 transaction has deleted the record on which the cursor was,
3037 and that is an SQL error. If the restoration succeeds, it may
3038 still be that the same transaction has successively deleted
3039 and inserted a record with the same ordering fields, but in
3040 that case we know that the transaction has at least an
3041 implicit x-lock on the record. */
3042
3043 ut_a(pcur->m_rel_pos == BTR_PCUR_ON);
3044
3045 ulint mode;
3046
3047 ut_ad(trx_can_be_handled_by_current_thread(trx));
3048 DEBUG_SYNC(trx->mysql_thd, "innodb_row_upd_clust_step_enter");
3049
3050 if (dict_index_is_online_ddl(index)) {
3051 ut_ad(node->table->id != DICT_INDEXES_ID);
3052 mode = BTR_MODIFY_LEAF | BTR_ALREADY_S_LATCHED;
3053 mtr_s_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE);
3054 } else {
3055 mode = BTR_MODIFY_LEAF;
3056 }
3057
3058 auto success = pcur->restore_position(mode, &mtr, UT_LOCATION_HERE);
3059
3060 if (!success) {
3061 err = DB_RECORD_NOT_FOUND;
3062
3063 mtr_commit(&mtr);
3064
3065 return (err);
3066 }
3067
3068 rec = pcur->get_rec();
3069 offsets = rec_get_offsets(rec, index, offsets_, ULINT_UNDEFINED,
3070 UT_LOCATION_HERE, &heap);
3071
3072 if (!node->has_clust_rec_x_lock) {
3073 err = lock_clust_rec_modify_check_and_lock(flags, pcur->get_block(), rec,
3074 index, offsets, thr);
3075 if (err != DB_SUCCESS) {
3076 mtr_commit(&mtr);
3077 goto exit_func;
3078 }
3079 }
3080
3081 ut_ad(lock_trx_has_rec_x_lock(thr, index->table, pcur->get_block(),
3082 page_rec_get_heap_no(rec)));
3083
3084 /* NOTE: the following function calls will also commit mtr */
3085
3086 if (node->is_delete) {
3087 err = row_upd_del_mark_clust_rec(flags, node, index, offsets, thr,
3088 referenced, &mtr);
3089
3090 if (err == DB_SUCCESS) {
3091 node->state = UPD_NODE_UPDATE_ALL_SEC;
3092 node->index = index->next();
3093 }
3094
3095 goto exit_func;
3096 }
3097
3098 /* If the update is made for MySQL, we already have the update vector
3099 ready, else we have to do some evaluation: */
3100
3101 if (UNIV_UNLIKELY(!node->in_mysql_interface)) {
3102 /* Copy the necessary columns from clust_rec and calculate the
3103 new values to set */
3104 row_upd_copy_columns(rec, offsets, index, UT_LIST_GET_FIRST(node->columns));
3105 row_upd_eval_new_vals(node->update);
3106 }
3107
3108 if (node->cmpl_info & UPD_NODE_NO_ORD_CHANGE) {
3109 err = row_upd_clust_rec(flags, node, index, offsets, &heap, thr, &mtr);
3110 goto exit_func;
3111 }
3112
3113 row_upd_store_row(node, trx->mysql_thd,
3114 thr->prebuilt ? thr->prebuilt->m_mysql_table : nullptr);
3115
3116 if (row_upd_changes_ord_field_binary(index, node->update, thr, node->row,
3117 node->ext, nullptr)) {
3118 /* Update causes an ordering field (ordering fields within
3119 the B-tree) of the clustered index record to change: perform
3120 the update by delete marking and inserting.
3121
3122 TODO! What to do to the 'Halloween problem', where an update
3123 moves the record forward in index so that it is again
3124 updated when the cursor arrives there? Solution: the
3125 read operation must check the undo record undo number when
3126 choosing records to update. MySQL solves now the problem
3127 externally! */
3128
3129 err =
3130 row_upd_clust_rec_by_insert(flags, node, index, thr, referenced, &mtr);
3131
3132 if (err != DB_SUCCESS) {
3133 goto exit_func;
3134 }
3135
3136 node->state = UPD_NODE_UPDATE_ALL_SEC;
3137 } else {
3138 err = row_upd_clust_rec(flags, node, index, offsets, &heap, thr, &mtr);
3139
3140 if (err != DB_SUCCESS) {
3141 goto exit_func;
3142 }
3143
3144 node->state = UPD_NODE_UPDATE_SOME_SEC;
3145 }
3146
3147 node->index = index->next();
3148
3149 exit_func:
3150 if (heap) {
3151 mem_heap_free(heap);
3152 }
3153 return (err);
3154 }
3155
3156 /** Updates the affected index records of a row. When the control is transferred
3157 to this node, we assume that we have a persistent cursor which was on a
3158 record, and the position of the cursor is stored in the cursor.
3159 @return DB_SUCCESS if operation successfully completed, else error
3160 code or DB_LOCK_WAIT */
3161 static dberr_t row_upd(upd_node_t *node, /*!< in: row update node */
3162 que_thr_t *thr) /*!< in: query thread */
3163 {
3164 dberr_t err = DB_SUCCESS;
3165 DBUG_TRACE;
3166
3167 ut_ad(node != nullptr);
3168 ut_ad(thr != nullptr);
3169 ut_ad(!thr_get_trx(thr)->in_rollback);
3170 ut_ad(!node->table->skip_alter_undo);
3171
3172 DBUG_PRINT("row_upd", ("table: %s", node->table->name.m_name));
3173 DBUG_PRINT("row_upd", ("info bits in update vector: 0x%lx",
3174 node->update ? node->update->info_bits : 0));
3175 DBUG_PRINT("row_upd",
3176 ("foreign_id: %s", node->foreign ? node->foreign->id : "NULL"));
3177
3178 if (UNIV_LIKELY(node->in_mysql_interface)) {
3179 /* We do not get the cmpl_info value from the MySQL
3180 interpreter: we must calculate it on the fly: */
3181
3182 if (node->is_delete || row_upd_changes_some_index_ord_field_binary(
3183 node->table, node->update)) {
3184 node->cmpl_info = 0;
3185 } else {
3186 node->cmpl_info = UPD_NODE_NO_ORD_CHANGE;
3187 }
3188 }
3189
3190 switch (node->state) {
3191 case UPD_NODE_UPDATE_CLUSTERED:
3192 case UPD_NODE_INSERT_CLUSTERED:
3193 if (!node->table->is_intrinsic()) {
3194 log_free_check();
3195 }
3196 err = row_upd_clust_step(node, thr);
3197
3198 if (err != DB_SUCCESS) {
3199 return err;
3200 }
3201 }
3202
3203 ut_ad(trx_can_be_handled_by_current_thread(thr_get_trx(thr)));
3204 DEBUG_SYNC(thr_get_trx(thr)->mysql_thd, "after_row_upd_clust");
3205
3206 if (node->index == nullptr ||
3207 (!node->is_delete && (node->cmpl_info & UPD_NODE_NO_ORD_CHANGE))) {
3208 return DB_SUCCESS;
3209 }
3210
3211 DBUG_EXECUTE_IF("row_upd_skip_sec", node->index = nullptr;);
3212
3213 do {
3214 /* Skip corrupted index */
3215 dict_table_skip_corrupt_index(node->index);
3216
3217 if (!node->index) {
3218 break;
3219 }
3220
3221 if (node->index->type != DICT_FTS) {
3222 err = row_upd_sec_step(node, thr);
3223
3224 if (err != DB_SUCCESS) {
3225 return err;
3226 }
3227 }
3228
3229 node->index = node->index->next();
3230 } while (node->index != nullptr);
3231
3232 ut_ad(err == DB_SUCCESS);
3233
3234 /* Do some cleanup */
3235
3236 if (node->row != nullptr) {
3237 node->row = nullptr;
3238 node->ext = nullptr;
3239 node->upd_row = nullptr;
3240 node->upd_ext = nullptr;
3241 mem_heap_empty(node->heap);
3242 }
3243
3244 node->state = UPD_NODE_UPDATE_CLUSTERED;
3245
3246 return err;
3247 }
3248
3249 /** Updates a row in a table. This is a high-level function used in SQL
3250 execution graphs.
3251 @return query thread to run next or NULL */
3252 que_thr_t *row_upd_step(que_thr_t *thr) /*!< in: query thread */
3253 {
3254 upd_node_t *node;
3255 sel_node_t *sel_node;
3256 que_node_t *parent;
3257 dberr_t err = DB_SUCCESS;
3258 trx_t *trx;
3259 DBUG_TRACE;
3260
3261 ut_ad(thr);
3262
3263 trx = thr_get_trx(thr);
3264
3265 trx_start_if_not_started_xa(trx, true, UT_LOCATION_HERE);
3266
3267 node = static_cast<upd_node_t *>(thr->run_node);
3268
3269 sel_node = node->select;
3270
3271 parent = que_node_get_parent(node);
3272
3273 ut_ad(que_node_get_type(node) == QUE_NODE_UPDATE);
3274
3275 if (thr->prev_node == parent) {
3276 node->state = UPD_NODE_SET_IX_LOCK;
3277 }
3278
3279 if (node->state == UPD_NODE_SET_IX_LOCK) {
3280 if (!node->has_clust_rec_x_lock) {
3281 /* It may be that the current session has not yet
3282 started its transaction, or it has been committed: */
3283
3284 err = lock_table(0, node->table, LOCK_IX, thr);
3285
3286 if (err != DB_SUCCESS) {
3287 goto error_handling;
3288 }
3289 }
3290
3291 node->state = UPD_NODE_UPDATE_CLUSTERED;
3292
3293 if (node->searched_update) {
3294 /* Reset the cursor */
3295 sel_node->state = SEL_NODE_OPEN;
3296
3297 /* Fetch a row to update */
3298
3299 thr->run_node = sel_node;
3300
3301 return thr;
3302 }
3303 }
3304
3305 /* sel_node is NULL if we are in the MySQL interface */
3306
3307 if (sel_node && (sel_node->state != SEL_NODE_FETCH)) {
3308 if (!node->searched_update) {
3309 /* An explicit cursor should be positioned on a row
3310 to update */
3311
3312 ut_error;
3313 }
3314
3315 ut_ad(sel_node->state == SEL_NODE_NO_MORE_ROWS);
3316
3317 /* No more rows to update, or the select node performed the
3318 updates directly in-place */
3319
3320 thr->run_node = parent;
3321
3322 return thr;
3323 }
3324
3325 /* DO THE CHECKS OF THE CONSISTENCY CONSTRAINTS HERE */
3326
3327 err = row_upd(node, thr);
3328
3329 error_handling:
3330 trx->error_state = err;
3331
3332 if (err != DB_SUCCESS) {
3333 return nullptr;
3334 }
3335
3336 /* DO THE TRIGGER ACTIONS HERE */
3337
3338 if (node->searched_update) {
3339 /* Fetch next row to update */
3340
3341 thr->run_node = sel_node;
3342 } else {
3343 /* It was an explicit cursor update */
3344
3345 thr->run_node = parent;
3346 }
3347
3348 node->state = UPD_NODE_UPDATE_CLUSTERED;
3349
3350 return thr;
3351 }
3352
3353 std::ostream &upd_field_t::print(std::ostream &out) const {
3354 out << "[upd_field_t: field_no=" << field_no << ", orig_len=" << orig_len
3355 << ", old_val=" << old_val << ", new_val=" << new_val
3356 << ", ext_in_old=" << ext_in_old;
3357
3358 if (lob_diffs != nullptr) {
3359 for (auto iter = lob_diffs->begin(); iter != lob_diffs->end(); ++iter) {
3360 out << *iter;
3361 }
3362 }
3363
3364 out << "]";
3365
3366 return out;
3367 }
3368
3369 std::ostream &upd_t::print(std::ostream &out) const {
3370 out << "[upd_t: n_fields=" << n_fields << ", ";
3371 for (ulint i = 0; i < n_fields; ++i) {
3372 out << fields[i];
3373 print_puvect(out, &fields[i]);
3374 }
3375 out << "]";
3376 return out;
3377 }
3378
3379 std::ostream &print_binary_diff(std::ostream &out, const Binary_diff *bdiff,
3380 const dict_table_t *table, const Field *field,
3381 bool print_old) {
3382 ulint field_no = 0;
3383 if (table != nullptr) {
3384 dict_col_t *col = table->get_col(field->field_index());
3385 field_no = dict_col_get_clust_pos(col, table->first_index());
3386 }
3387
3388 const char *to = bdiff->new_data(const_cast<Field *>(field));
3389 size_t len = bdiff->length();
3390
3391 out << "[Binary_diff: field_index=" << field->field_index()
3392 << ", field_no=" << field_no << ", offset=" << bdiff->offset()
3393 << ", length=" << len << ", new_data=" << PrintBuffer(to, len) << "]";
3394
3395 if (print_old) {
3396 const char *from = bdiff->old_data(const_cast<Field *>(field));
3397 out << ", old_data=" << PrintBuffer(from, len);
3398 }
3399 out << "]";
3400
3401 return (out);
3402 }
3403
3404 std::ostream &print_binary_diff(std::ostream &out, const Binary_diff *bdiff,
3405 Field *fld) {
3406 const char *to = bdiff->new_data(fld);
3407 size_t len = bdiff->length();
3408
3409 out << "[Binary_diff: field_index=" << fld->field_index()
3410 << ", offset=" << bdiff->offset() << ", length=" << bdiff->length()
3411 << ", new_data=" << PrintBuffer(to, len) << "]";
3412 return (out);
3413 }
3414
3415 std::ostream &upd_t::print_puvect(std::ostream &out, upd_field_t *uf) const {
3416 if (!is_partially_updated(uf->field_no)) {
3417 return (out);
3418 }
3419
3420 Field *fld = uf->mysql_field;
3421
3422 const Binary_diff_vector *dv = mysql_table->get_binary_diffs(fld);
3423
3424 for (Binary_diff_vector::const_iterator iter = dv->begin(); iter != dv->end();
3425 ++iter) {
3426 const Binary_diff *bdiff = iter;
3427 print_binary_diff(out, bdiff, table, fld, true);
3428 }
3429
3430 return (out);
3431 }
3432
3433 const upd_field_t *upd_t::get_field_by_field_no(
3434 ulint field_no, const dict_index_t *index) const {
3435 ut_ad(!(index->is_clustered() &&
3436 index->get_field(field_no)->col->is_virtual()));
3437
3438 return upd_get_field_by_field_no(this, field_no, false);
3439 }
3440
3441 /** Check if the given field number is partially updated.
3442 @param[in] field_no the field number.
3443 @return true if partially updated, false otherwise. */
3444 bool upd_t::is_partially_updated(ulint field_no) const {
3445 if (mysql_table == nullptr || !mysql_table->has_binary_diff_columns()) {
3446 return false;
3447 }
3448
3449 const upd_field_t *const uf =
3450 get_field_by_field_no(field_no, table->first_index());
3451
3452 if (uf == nullptr || uf->mysql_field == nullptr) {
3453 return false;
3454 }
3455
3456 ut_ad(mysql_table == uf->mysql_field->table);
3457
3458 if (!mysql_table->is_binary_diff_enabled(uf->mysql_field)) {
3459 return false;
3460 }
3461
3462 if (dict_table_has_atomic_blobs(table)) {
3463 return true;
3464 }
3465
3466 #ifdef UNIV_DEBUG
3467 rec_format_t format = dict_tf_get_rec_format(table->flags);
3468 ut_ad(format == REC_FORMAT_REDUNDANT || format == REC_FORMAT_COMPACT);
3469 #endif /* UNIV_DEBUG */
3470
3471 /* In compact and redundant row format, partially updating the LOB prefix
3472 is not yet supported. */
3473
3474 const Binary_diff_vector *bdiff_vector =
3475 get_binary_diff_by_field_no(field_no);
3476
3477 for (Binary_diff_vector::const_iterator iter = bdiff_vector->begin();
3478 iter != bdiff_vector->end(); ++iter) {
3479 const Binary_diff *bdiff = iter;
3480
3481 if (bdiff->offset() < DICT_ANTELOPE_MAX_INDEX_COL_LEN) {
3482 return false;
3483 }
3484 }
3485
3486 return true;
3487 }
3488
3489 const Binary_diff_vector *upd_t::get_binary_diff_by_field_no(
3490 ulint field_no) const {
3491 ut_ad(table != nullptr);
3492
3493 const upd_field_t *const uf =
3494 get_field_by_field_no(field_no, table->first_index());
3495 ut_ad(uf != nullptr);
3496
3497 Field *fld = uf->mysql_field;
3498
3499 if (fld == nullptr) {
3500 return nullptr;
3501 }
3502
3503 return mysql_table->get_binary_diffs(fld);
3504 }
3505 #endif /* !UNIV_HOTBACKUP */