Debian Codegraph @

storage/innobase/trx/trx0rec.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 trx/trx0rec.cc
29 Transaction undo log record
30
31 Created 3/26/1996 Heikki Tuuri
32 *******************************************************/
33
34 #include "trx0rec.h"
35
36 #include <sys/types.h>
37
38 #include "fsp0fsp.h"
39 #include "mach0data.h"
40 #include "mtr0log.h"
41 #include "trx0undo.h"
42 #include "ut0dbg.h"
43 #ifndef UNIV_HOTBACKUP
44 #include "dict0dict.h"
45 #include "fsp0sysspace.h"
46 #include "lob0index.h"
47 #include "lob0inf.h"
48 #include "lob0lob.h"
49 #include "que0que.h"
50 #include "read0read.h"
51 #include "row0ext.h"
52 #include "row0mysql.h"
53 #include "row0row.h"
54 #include "row0upd.h"
55 #include "trx0purge.h"
56 #include "trx0rseg.h"
57 #include "ut0mem.h"
58
59 #include "my_dbug.h"
60
61 namespace dd {
62 class Spatial_reference_system;
63 }
64
65 /*=========== UNDO LOG RECORD CREATION AND DECODING ====================*/
66
67 /** Writes the mtr log entry of the inserted undo log record on the undo log
68 page. */
69 static inline void trx_undof_page_add_undo_rec_log(
70 page_t *undo_page, /*!< in: undo log page */
71 ulint old_free, /*!< in: start offset of the inserted entry */
72 ulint new_free, /*!< in: end offset of the entry */
73 mtr_t *mtr) /*!< in: mtr */
74 {
75 byte *log_ptr = nullptr;
76 const byte *log_end;
77 ulint len;
78
79 if (!mlog_open(mtr, 11 + 13 + MLOG_BUF_MARGIN, log_ptr)) {
80 return;
81 }
82
83 log_end = &log_ptr[11 + 13 + MLOG_BUF_MARGIN];
84 log_ptr = mlog_write_initial_log_record_fast(undo_page, MLOG_UNDO_INSERT,
85 log_ptr, mtr);
86 len = new_free - old_free - 4;
87
88 mach_write_to_2(log_ptr, len);
89 log_ptr += 2;
90
91 if (log_ptr + len <= log_end) {
92 memcpy(log_ptr, undo_page + old_free + 2, len);
93 mlog_close(mtr, log_ptr + len);
94 } else {
95 mlog_close(mtr, log_ptr);
96 mlog_catenate_string(mtr, undo_page + old_free + 2, len);
97 }
98 }
99 #endif /* !UNIV_HOTBACKUP */
100
101 /** Parses a redo log record of adding an undo log record.
102 @return end of log record or NULL */
103 byte *trx_undo_parse_add_undo_rec(byte *ptr, /*!< in: buffer */
104 byte *end_ptr, /*!< in: buffer end */
105 page_t *page) /*!< in: page or NULL */
106 {
107 ulint len;
108 byte *rec;
109 ulint first_free;
110
111 if (end_ptr < ptr + 2) {
112 return (nullptr);
113 }
114
115 len = mach_read_from_2(ptr);
116 ptr += 2;
117
118 if (end_ptr < ptr + len) {
119 return (nullptr);
120 }
121
122 if (page == nullptr) {
123 return (ptr + len);
124 }
125
126 first_free = mach_read_from_2(page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE);
127 rec = page + first_free;
128
129 mach_write_to_2(rec, first_free + 4 + len);
130 mach_write_to_2(rec + 2 + len, first_free);
131
132 mach_write_to_2(page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE,
133 first_free + 4 + len);
134 ut_memcpy(rec + 2, ptr, len);
135
136 return (ptr + len);
137 }
138
139 #ifndef UNIV_HOTBACKUP
140 /** Calculates the free space left for extending an undo log record.
141 @return bytes left */
142 static inline ulint trx_undo_left(const page_t *page, /*!< in: undo log page */
143 const byte *ptr) /*!< in: pointer to page */
144 {
145 /* The '- 10' is a safety margin, in case we have some small
146 calculation error below */
147
148 #ifdef UNIV_DEBUG
149 ut_ad(ptr >= page);
150 size_t diff = ptr - page;
151 size_t max_free = UNIV_PAGE_SIZE - 10 - FIL_PAGE_DATA_END;
152 ut_ad(diff < UNIV_PAGE_SIZE);
153 ut_ad(diff <= max_free);
154 #endif /* UNIV_DEBUG */
155
156 return (UNIV_PAGE_SIZE - (ptr - page) - 10 - FIL_PAGE_DATA_END);
157 }
158
159 size_t trx_undo_max_free_space() {
160 /* Starting from an empty undo page. The following calculation is based
161 on what free space is got from trx_undo_reuse_cached(), trx_undo_create()
162 and trx_undo_left(). Current simplified free_space would be
163 UNIV_PAGE_SIZE - 290. */
164 size_t free_space =
165 UNIV_PAGE_SIZE - (TRX_UNDO_SEG_HDR + TRX_UNDO_SEG_HDR_SIZE +
166 TRX_UNDO_LOG_XA_HDR_SIZE + FIL_PAGE_DATA_END + 10);
167
168 /* Undo number, table id, undo log type and pointer to next.
169 Also refer to the beginning of trx_undo_page_report_insert() */
170 free_space -= (11 + 11 + 1 + 2);
171
172 /* For simplification, the max record length should be
173 UNIV_PAGE_SIZE - 290 - 25 = UNIV_PAGE_SIZE - 315. */
174
175 return (free_space);
176 }
177
178 /** Set the next and previous pointers in the undo page for the undo record
179 that was written to ptr. Update the first free value by the number of bytes
180 written for this undo record.
181 @return offset of the inserted entry on the page if succeeded, 0 if fail */
182 static ulint trx_undo_page_set_next_prev_and_add(
183 page_t *undo_page, /*!< in/out: undo log page */
184 byte *ptr, /*!< in: ptr up to where data has been
185 written on this undo page. */
186 mtr_t *mtr) /*!< in: mtr */
187 {
188 ulint first_free; /*!< offset within undo_page */
189 ulint end_of_rec; /*!< offset within undo_page */
190 byte *ptr_to_first_free;
191 /* pointer within undo_page
192 that points to the next free
193 offset value within undo_page.*/
194
195 ut_ad(ptr > undo_page);
196 ut_ad(ptr < undo_page + UNIV_PAGE_SIZE);
197
198 if (UNIV_UNLIKELY(trx_undo_left(undo_page, ptr) < 2)) {
199 return (0);
200 }
201
202 ptr_to_first_free = undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE;
203
204 first_free = mach_read_from_2(ptr_to_first_free);
205
206 /* Write offset of the previous undo log record */
207 mach_write_to_2(ptr, first_free);
208 ptr += 2;
209
210 end_of_rec = ptr - undo_page;
211
212 /* Write offset of the next undo log record */
213 mach_write_to_2(undo_page + first_free, end_of_rec);
214
215 /* Update the offset to first free undo record */
216 mach_write_to_2(ptr_to_first_free, end_of_rec);
217
218 /* Write this log entry to the UNDO log */
219 trx_undof_page_add_undo_rec_log(undo_page, first_free, end_of_rec, mtr);
220
221 return (first_free);
222 }
223
224 /** Virtual column undo log version. To distinguish it from a length value
225 in 5.7.8 undo log, it starts with 0xF1 */
226 static const ulint VIRTUAL_COL_UNDO_FORMAT_1 = 0xF1;
227
228 /** Decide if the following undo log record is a multi-value virtual column
229 @param[in] undo_rec undo log record
230 @return true if this is a multi-value virtual column log, otherwise false */
231 bool trx_undo_rec_is_multi_value(const byte *undo_rec) {
232 return (Multi_value_logger::is_multi_value_log(undo_rec));
233 }
234
235 /** Write virtual column index info (index id and column position in index)
236 to the undo log
237 @param[in,out] undo_page undo log page
238 @param[in] table the table
239 @param[in] pos the virtual column position
240 @param[in] ptr undo log record being written
241 @param[in] first_v_col whether this is the first virtual column
242 which could start with a version marker
243 @return new undo log pointer */
244 static byte *trx_undo_log_v_idx(page_t *undo_page, const dict_table_t *table,
245 ulint pos, byte *ptr, bool first_v_col) {
246 ut_ad(pos < table->n_v_def);
247 dict_v_col_t *vcol = dict_table_get_nth_v_col(table, pos);
248
249 ulint n_idx = vcol->v_indexes->size();
250 byte *old_ptr;
251
252 ut_ad(n_idx > 0);
253
254 /* Size to reserve, max (11 + 5) bytes for each index id and position, plus
255 5 bytes for num of indexes, 2 bytes for write total length.
256 1 byte for undo log record format version marker */
257 ulint size = n_idx * (11 + 5) + 5 + 2 + (first_v_col ? 1 : 0);
258
259 if (trx_undo_left(undo_page, ptr) < size) {
260 return (nullptr);
261 }
262
263 if (first_v_col) {
264 /* write the version marker */
265 mach_write_to_1(ptr, VIRTUAL_COL_UNDO_FORMAT_1);
266
267 ptr += 1;
268 }
269
270 old_ptr = ptr;
271
272 ptr += 2;
273
274 ptr += mach_write_compressed(ptr, n_idx);
275
276 dict_v_idx_list::iterator it;
277
278 for (it = vcol->v_indexes->begin(); it != vcol->v_indexes->end(); ++it) {
279 dict_v_idx_t v_index = *it;
280
281 ptr += mach_u64_write_much_compressed(ptr, v_index.index->id);
282
283 ptr += mach_write_compressed(ptr, v_index.nth_field);
284 }
285
286 mach_write_to_2(old_ptr, ptr - old_ptr);
287
288 return (ptr);
289 }
290
291 /** Read virtual column index from undo log, and verify the column is still
292 indexed, and return its position
293 @param[in] table the table
294 @param[in] ptr undo log pointer
295 @param[out] col_pos the column number or ULINT_UNDEFINED
296 if the column is not indexed any more
297 @return remaining part of undo log record after reading these values */
298 static const byte *trx_undo_read_v_idx_low(const dict_table_t *table,
299 const byte *ptr, ulint *col_pos) {
300 ulint len = mach_read_from_2(ptr);
301 const byte *old_ptr = ptr;
302
303 *col_pos = ULINT_UNDEFINED;
304
305 ptr += 2;
306
307 ulint num_idx = mach_read_next_compressed(&ptr);
308
309 ut_ad(num_idx > 0);
310
311 const dict_index_t *clust_index = table->first_index();
312
313 for (ulint i = 0; i < num_idx; i++) {
314 space_index_t id = mach_read_next_much_compressed(&ptr);
315 ulint pos = mach_read_next_compressed(&ptr);
316 const dict_index_t *index = clust_index->next();
317
318 while (index != nullptr) {
319 /* Return if we find a matching index.
320 TODO: in the future, it might be worth to add
321 checks on other indexes */
322 if (index->id == id) {
323 const dict_col_t *col = index->get_col(pos);
324 ut_ad(col->is_virtual());
325 const dict_v_col_t *vcol = reinterpret_cast<const dict_v_col_t *>(col);
326 *col_pos = vcol->v_pos;
327 return (old_ptr + len);
328 }
329
330 index = index->next();
331 }
332 }
333
334 return (old_ptr + len);
335 }
336
337 /** Read virtual column index from undo log or online log if the log
338 contains such info, and in the undo log case, verify the column is
339 still indexed, and output its position
340 @param[in] table the table
341 @param[in] ptr undo log pointer
342 @param[in] first_v_col if this is the first virtual column, which
343 has the version marker
344 @param[in,out] is_undo_log this function is used to parse both undo log,
345 and online log for virtual columns. So
346 check to see if this is undo log. When
347 first_v_col is true, is_undo_log is output,
348 when first_v_col is false, is_undo_log is input
349 @param[in,out] field_no the column number
350 @return remaining part of undo log record after reading these values */
351 const byte *trx_undo_read_v_idx(const dict_table_t *table, const byte *ptr,
352 bool first_v_col, bool *is_undo_log,
353 ulint *field_no) {
354 /* Version marker only put on the first virtual column */
355 if (first_v_col) {
356 /* Undo log has the virtual undo log marker */
357 *is_undo_log = (mach_read_from_1(ptr) == VIRTUAL_COL_UNDO_FORMAT_1);
358
359 if (*is_undo_log) {
360 ptr += 1;
361 }
362 }
363
364 if (*is_undo_log) {
365 ptr = trx_undo_read_v_idx_low(table, ptr, field_no);
366 } else {
367 *field_no -= REC_MAX_N_FIELDS;
368 }
369
370 return (ptr);
371 }
372
373 /** Store the multi-value column information for undo log
374 @param[in,out] undo_page undo page to store the information
375 @param[in] vfield multi-value field information
376 @param[in,out] ptr pointer where to store the information
377 @return true if stored successfully, false if space is not enough */
378 static bool trx_undo_store_multi_value(page_t *undo_page,
379 const dfield_t *vfield, byte **ptr) {
380 ut_ad(vfield != nullptr);
381
382 Multi_value_logger mv_logger(
383 static_cast<multi_value_data *>(dfield_get_data(vfield)),
384 dfield_get_len(vfield));
385 uint32_t log_len = mv_logger.get_log_len(false);
386
387 if (trx_undo_left(undo_page, *ptr) < log_len) {
388 return (false);
389 }
390
391 mv_logger.log(ptr);
392
393 return (true);
394 }
395
396 /** Reports in the undo log of an insert of virtual columns.
397 @param[in] undo_page undo log page
398 @param[in] table the table
399 @param[in] row dtuple contains the virtual columns
400 @param[in,out] ptr log ptr
401 @return true if write goes well, false if out of space */
402 static bool trx_undo_report_insert_virtual(page_t *undo_page,
403 dict_table_t *table,
404 const dtuple_t *row, byte **ptr) {
405 byte *start = *ptr;
406 bool first_v_col = true;
407
408 if (trx_undo_left(undo_page, *ptr) < 2) {
409 return (false);
410 }
411
412 /* Reserve 2 bytes to write the number
413 of bytes the stored fields take in this
414 undo record */
415 *ptr += 2;
416
417 for (ulint col_no = 0; col_no < dict_table_get_n_v_cols(table); col_no++) {
418 dfield_t *vfield = nullptr;
419
420 const dict_v_col_t *col = dict_table_get_nth_v_col(table, col_no);
421
422 if (col->m_col.ord_part) {
423 /* make sure enough space to write the length */
424 if (trx_undo_left(undo_page, *ptr) < 5) {
425 return (false);
426 }
427
428 ulint pos = col_no;
429 pos += REC_MAX_N_FIELDS;
430 *ptr += mach_write_compressed(*ptr, pos);
431
432 *ptr = trx_undo_log_v_idx(undo_page, table, col_no, *ptr, first_v_col);
433 first_v_col = false;
434
435 if (*ptr == nullptr) {
436 return (false);
437 }
438
439 vfield = dtuple_get_nth_v_field(row, col->v_pos);
440
441 ulint flen = vfield->len;
442
443 if (col->m_col.is_multi_value()) {
444 bool suc = trx_undo_store_multi_value(undo_page, vfield, ptr);
445
446 if (!suc) {
447 return (false);
448 }
449 } else if (flen != UNIV_SQL_NULL) {
450 ulint max_len = dict_max_v_field_len_store_undo(table, col_no);
451
452 if (flen > max_len) {
453 flen = max_len;
454 }
455
456 if (trx_undo_left(undo_page, *ptr) < flen + 5) {
457 return (false);
458 }
459 *ptr += mach_write_compressed(*ptr, flen);
460
461 ut_memcpy(*ptr, vfield->data, flen);
462 *ptr += flen;
463 } else {
464 if (trx_undo_left(undo_page, *ptr) < 5) {
465 return (false);
466 }
467
468 *ptr += mach_write_compressed(*ptr, flen);
469 }
470 }
471 }
472
473 /* Always mark the end of the log with 2 bytes length field */
474 mach_write_to_2(start, *ptr - start);
475
476 return (true);
477 }
478
479 /** Reports in the undo log of an insert of a clustered index record.
480 @return offset of the inserted entry on the page if succeed, 0 if fail */
481 static ulint trx_undo_page_report_insert(
482 page_t *undo_page, /*!< in: undo log page */
483 trx_t *trx, /*!< in: transaction */
484 dict_index_t *index, /*!< in: clustered index */
485 const dtuple_t *clust_entry, /*!< in: index entry which will be
486 inserted to the clustered index */
487 mtr_t *mtr) /*!< in: mtr */
488 {
489 ulint first_free;
490 byte *ptr;
491 ulint i;
492
493 ut_ad(index->is_clustered());
494 ut_ad(mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_TYPE) ==
495 TRX_UNDO_INSERT);
496
497 first_free =
498 mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE);
499 ptr = undo_page + first_free;
500
501 ut_ad(first_free <= UNIV_PAGE_SIZE);
502
503 if (trx_undo_left(undo_page, ptr) < 2 + 1 + 11 + 11) {
504 /* Not enough space for writing the general parameters */
505
506 return (0);
507 }
508
509 /* Reserve 2 bytes for the pointer to the next undo log record */
510 ptr += 2;
511
512 /* Store first some general parameters to the undo log */
513 *ptr++ = TRX_UNDO_INSERT_REC;
514 ptr += mach_u64_write_much_compressed(ptr, trx->undo_no);
515 ptr += mach_u64_write_much_compressed(ptr, index->table->id);
516 /*----------------------------------------*/
517 /* Store then the fields required to uniquely determine the record
518 to be inserted in the clustered index */
519
520 for (i = 0; i < dict_index_get_n_unique(index); i++) {
521 const dfield_t *field = dtuple_get_nth_field(clust_entry, i);
522 ulint flen = dfield_get_len(field);
523
524 if (trx_undo_left(undo_page, ptr) < 5) {
525 return (0);
526 }
527
528 ptr += mach_write_compressed(ptr, flen);
529
530 if (flen != UNIV_SQL_NULL && flen != 0) {
531 if (trx_undo_left(undo_page, ptr) < flen) {
532 return (0);
533 }
534
535 ut_memcpy(ptr, dfield_get_data(field), flen);
536 ptr += flen;
537 }
538 }
539
540 if (index->table->n_v_cols) {
541 if (!trx_undo_report_insert_virtual(undo_page, index->table, clust_entry,
542 &ptr)) {
543 return (0);
544 }
545 }
546
547 return (trx_undo_page_set_next_prev_and_add(undo_page, ptr, mtr));
548 }
549
550 /** Reads from an undo log record the general parameters.
551 @return remaining part of undo log record after reading these values */
552 byte *trx_undo_rec_get_pars(
553 trx_undo_rec_t *undo_rec, /*!< in: undo log record */
554 ulint *type, /*!< out: undo record type:
555 TRX_UNDO_INSERT_REC, ... */
556 ulint *cmpl_info, /*!< out: compiler info, relevant only
557 for update type records */
558 bool *updated_extern, /*!< out: true if we updated an
559 externally stored fild */
560 undo_no_t *undo_no, /*!< out: undo log record number */
561 table_id_t *table_id, /*!< out: table id */
562 type_cmpl_t &type_cmpl) /*!< out: type compilation info */
563 {
564 const byte *ptr;
565
566 ptr = undo_rec + 2;
567 ptr = type_cmpl.read(ptr);
568
569 *updated_extern = type_cmpl.is_lob_updated();
570 *type = type_cmpl.type_info();
571 *cmpl_info = type_cmpl.cmpl_info();
572
573 if (type_cmpl.is_lob_undo()) {
574 /* Reading the new 1-byte undo record flag. */
575 uint8_t undo_rec_flags = 0x00;
576
577 undo_rec_flags = mach_read_from_1(ptr);
578 ptr++;
579
580 ut_a(undo_rec_flags == 0x00);
581 }
582
583 *undo_no = mach_read_next_much_compressed(&ptr);
584 *table_id = mach_read_next_much_compressed(&ptr);
585
586 return (const_cast<byte *>(ptr));
587 }
588
589 /** Reads from an undo log record the table ID
590 @param[in] undo_rec Undo log record
591 @return the table ID */
592 table_id_t trx_undo_rec_get_table_id(const trx_undo_rec_t *undo_rec) {
593 const byte *ptr = undo_rec + 2;
594 uint8_t type_cmpl = mach_read_from_1(ptr);
595
596 const bool blob_undo = type_cmpl & TRX_UNDO_MODIFY_BLOB;
597
598 if (blob_undo) {
599 /* The next record offset takes 2 bytes + 1 byte for
600 type_cmpl flag + 1 byte for the new flag. Total 4 bytes.
601 The new flag is currently unused and is available for
602 future use. */
603 ptr = undo_rec + 4;
604 } else {
605 ptr = undo_rec + 3;
606 }
607
608 /* Skip the UNDO number */
609 mach_read_next_much_compressed(&ptr);
610
611 /* Read the table ID */
612 return (mach_read_next_much_compressed(&ptr));
613 }
614
615 /** Read from an undo log record of a multi-value virtual column.
616 @param[in] ptr pointer to remaining part of the undo record
617 @param[in,out] field stored field, nullptr if the col is no longer
618 indexed or existing, in the latter case,
619 this function will only skip the log
620 @param[in,out] heap memory heap
621 @return remaining part of undo log record after reading these values */
622 const byte *trx_undo_rec_get_multi_value(const byte *ptr, dfield_t *field,
623 mem_heap_t *heap) {
624 if (field == nullptr) {
625 return (ptr + Multi_value_logger::read_log_len(ptr));
626 }
627
628 return (Multi_value_logger::read(ptr, field, heap));
629 }
630
631 /** Read from an undo log record a non-virtual column value.
632 @param[in,out] ptr pointer to remaining part of the undo record
633 @param[in,out] field stored field
634 @param[in,out] len length of the field, or UNIV_SQL_NULL
635 @param[in,out] orig_len original length of the locally stored part
636 of an externally stored column, or 0
637 @return remaining part of undo log record after reading these values */
638 byte *trx_undo_rec_get_col_val(const byte *ptr, const byte **field, ulint *len,
639 ulint *orig_len) {
640 *len = mach_read_next_compressed(&ptr);
641 *orig_len = 0;
642
643 switch (*len) {
644 case UNIV_SQL_NULL:
645 *field = nullptr;
646 break;
647 case UNIV_EXTERN_STORAGE_FIELD:
648 *orig_len = mach_read_next_compressed(&ptr);
649 *len = mach_read_next_compressed(&ptr);
650 *field = ptr;
651 ptr += *len & ~SPATIAL_STATUS_MASK;
652
653 ut_ad(*orig_len >= BTR_EXTERN_FIELD_REF_SIZE);
654 ut_ad(*len > *orig_len);
655 /* @see dtuple_convert_big_rec() */
656 ut_ad(*len >= BTR_EXTERN_FIELD_REF_SIZE);
657
658 /* we do not have access to index->table here
659 ut_ad(dict_table_has_atomic_blobs(index->table)
660 || *len >= col->max_prefix
661 + BTR_EXTERN_FIELD_REF_SIZE);
662 */
663
664 *len += UNIV_EXTERN_STORAGE_FIELD;
665 break;
666 default:
667 *field = ptr;
668 if (*len >= UNIV_EXTERN_STORAGE_FIELD) {
669 ptr += (*len - UNIV_EXTERN_STORAGE_FIELD) & ~SPATIAL_STATUS_MASK;
670 } else {
671 ptr += *len;
672 }
673 }
674
675 return (const_cast<byte *>(ptr));
676 }
677
678 /** Builds a row reference from an undo log record.
679 @return pointer to remaining part of undo record */
680 byte *trx_undo_rec_get_row_ref(
681 byte *ptr, /*!< in: remaining part of a copy of an undo log
682 record, at the start of the row reference;
683 NOTE that this copy of the undo log record must
684 be preserved as long as the row reference is
685 used, as we do NOT copy the data in the
686 record! */
687 dict_index_t *index, /*!< in: clustered index */
688 dtuple_t **ref, /*!< out, own: row reference */
689 mem_heap_t *heap) /*!< in: memory heap from which the memory
690 needed is allocated */
691 {
692 ulint ref_len;
693 ulint i;
694
695 ut_ad(index && ptr && ref && heap);
696 ut_a(index->is_clustered());
697
698 ref_len = dict_index_get_n_unique(index);
699
700 *ref = dtuple_create(heap, ref_len);
701
702 dict_index_copy_types(*ref, index, ref_len);
703
704 for (i = 0; i < ref_len; i++) {
705 dfield_t *dfield;
706 const byte *field;
707 ulint len;
708 ulint orig_len;
709
710 dfield = dtuple_get_nth_field(*ref, i);
711
712 ptr = trx_undo_rec_get_col_val(ptr, &field, &len, &orig_len);
713
714 dfield_set_data(dfield, field, len);
715 }
716
717 return (ptr);
718 }
719
720 /** Skips a row reference from an undo log record.
721 @return pointer to remaining part of undo record */
722 static byte *trx_undo_rec_skip_row_ref(
723 byte *ptr, /*!< in: remaining part in update undo log
724 record, at the start of the row reference */
725 const dict_index_t *index) /*!< in: clustered index */
726 {
727 ulint ref_len;
728 ulint i;
729
730 ut_ad(index && ptr);
731 ut_a(index->is_clustered());
732
733 ref_len = dict_index_get_n_unique(index);
734
735 for (i = 0; i < ref_len; i++) {
736 const byte *field;
737 ulint len;
738 ulint orig_len;
739
740 ptr = trx_undo_rec_get_col_val(ptr, &field, &len, &orig_len);
741 }
742
743 return (ptr);
744 }
745
746 /** Fetch a prefix of an externally stored column, for writing to the undo
747 log of an update or delete marking of a clustered index record.
748 @param[in] trx transaction object
749 @param[in] index the clustered index object
750 @param[out] ext_buf buffer to hold the prefix data and BLOB pointer
751 @param[in] prefix_len prefix size to store in the undo log
752 @param[in] page_size page size
753 @param[in] field an externally stored column
754 @param[in] is_sdi true for SDI indexes
755 @param[in,out] len input: length of field; output: used length of
756 ext_buf
757 @return ext_buf */
758 static byte *trx_undo_page_fetch_ext(trx_t *trx, dict_index_t *index,
759 byte *ext_buf, ulint prefix_len,
760 const page_size_t &page_size,
761 const byte *field,
762 IF_DEBUG(bool is_sdi, ) ulint *len) {
763 /* Fetch the BLOB. */
764 ulint ext_len = lob::btr_copy_externally_stored_field_prefix_func(
765 trx, index, ext_buf, prefix_len, page_size, field,
766 IF_DEBUG(is_sdi, ) * len);
767
768 #ifdef UNIV_DEBUG
769 if (ext_len == 0) {
770 byte *field_ref = const_cast<byte *>(field) + (*len) - lob::ref_t::SIZE;
771 lob::ref_t ref(field_ref);
772 lob::ref_mem_t ref_mem;
773 ref.parse(ref_mem);
774 lob::print(trx, index, std::cout, ref, true);
775 }
776 #endif /* UNIV_DEBUG */
777
778 /* BLOBs should always be nonempty. */
779 ut_a(ext_len > 0);
780 /* Append the BLOB pointer to the prefix. */
781 memcpy(ext_buf + ext_len, field + *len - BTR_EXTERN_FIELD_REF_SIZE,
782 BTR_EXTERN_FIELD_REF_SIZE);
783 *len = ext_len + BTR_EXTERN_FIELD_REF_SIZE;
784 return (ext_buf);
785 }
786
787 /** Writes to the undo log a prefix of an externally stored column.
788 @param[in] trx transaction object
789 @param[in] index the clustered index object
790 @param[out] ptr undo log position, at least 15 bytes must be
791 available
792 @param[out] ext_buf a buffer of DICT_MAX_FIELD_LEN_BY_FORMAT()
793 size, or NULL when should not fetch a longer
794 prefix
795 @param[in] prefix_len prefix size to store in the undo log
796 @param[in] page_size page size
797 @param[in,out] field the locally stored part of the externally
798 stored column
799 @param[in,out] len length of field, in bytes
800 @param[in] is_sdi true for SDI indexes
801 @param[in] spatial_status whether the column is used by spatial index or
802 regular index
803 @return undo log position */
804 static byte *trx_undo_page_report_modify_ext_func(
805 trx_t *trx, dict_index_t *index, byte *ptr, byte *ext_buf, ulint prefix_len,
806 const page_size_t &page_size, const byte **field, ulint *len,
807 IF_DEBUG(bool is_sdi, ) spatial_status_t spatial_status) {
808 ulint spatial_len = 0;
809
810 switch (spatial_status) {
811 case SPATIAL_UNKNOWN:
812 case SPATIAL_NONE:
813 break;
814
815 case SPATIAL_MIXED:
816 case SPATIAL_ONLY:
817 spatial_len = DATA_MBR_LEN;
818 break;
819 }
820
821 /* Encode spatial status into length. */
822 spatial_len |= spatial_status << SPATIAL_STATUS_SHIFT;
823
824 if (spatial_status == SPATIAL_ONLY) {
825 /* If the column is only used by gis index, log its
826 MBR is enough.*/
827 ptr += mach_write_compressed(ptr, UNIV_EXTERN_STORAGE_FIELD + spatial_len);
828
829 return (ptr);
830 }
831
832 if (ext_buf) {
833 ut_a(prefix_len > 0);
834
835 /* If an ordering column is externally stored, we will
836 have to store a longer prefix of the field. In this
837 case, write to the log a marker followed by the
838 original length and the real length of the field. */
839 ptr += mach_write_compressed(ptr, UNIV_EXTERN_STORAGE_FIELD);
840
841 ptr += mach_write_compressed(ptr, *len);
842
843 *field = trx_undo_page_fetch_ext(trx, index, ext_buf, prefix_len, page_size,
844 *field, IF_DEBUG(is_sdi, ) len);
845
846 ptr += mach_write_compressed(ptr, *len + spatial_len);
847 } else {
848 ptr += mach_write_compressed(
849 ptr, UNIV_EXTERN_STORAGE_FIELD + *len + spatial_len);
850 }
851
852 return (ptr);
853 }
854
855 static inline byte *trx_undo_page_report_modify_ext(
856 trx_t *trx, dict_index_t *index, byte *ptr, byte *ext_buf, ulint prefix_len,
857 const page_size_t &page_size, const byte **field, ulint *len,
858 bool is_sdi [[maybe_unused]], spatial_status_t spatial_status) {
859 return trx_undo_page_report_modify_ext_func(
860 trx, index, ptr, ext_buf, prefix_len, page_size, field, len,
861 IF_DEBUG(is_sdi, ) spatial_status);
862 }
863
864 /** Get MBR from a Geometry column stored externally
865 @param[in] trx transaction object
866 @param[in] index the clustered index object
867 @param[out] mbr MBR to fill
868 @param[in] page_size table pagesize
869 @param[in] field field contain the geometry data
870 @param[in,out] len length of field, in bytes
871 @param[in] srs Spatial reference system of R-tree.
872 */
873 static void trx_undo_get_mbr_from_ext(trx_t *trx, dict_index_t *index,
874 double *mbr, const page_size_t &page_size,
875 const byte *field, ulint *len,
876 const dd::Spatial_reference_system *srs) {
877 uchar *dptr = nullptr;
878 ulint dlen;
879 mem_heap_t *heap = mem_heap_create(100, UT_LOCATION_HERE);
880
881 dptr = lob::btr_copy_externally_stored_field(
882 trx, index, &dlen, nullptr, field, page_size, *len, false, heap);
883
884 if (dlen <= GEO_DATA_HEADER_SIZE) {
885 for (uint i = 0; i < SPDIMS; ++i) {
886 mbr[i * 2] = DBL_MAX;
887 mbr[i * 2 + 1] = -DBL_MAX;
888 }
889 } else {
890 get_mbr_from_store(srs, dptr, static_cast<uint>(dlen), SPDIMS, mbr,
891 nullptr);
892 }
893
894 mem_heap_free(heap);
895 }
896
897 static const byte *trx_undo_read_blob_update(const byte *undo_ptr,
898 upd_field_t *uf,
899 lob::undo_vers_t *lob_undo) {
900 DBUG_TRACE;
901
902 /* Read one byte of flags. */
903 uint8_t flag = *undo_ptr;
904 ut_a(flag == 0x00);
905 undo_ptr++;
906
907 const ulint field_no = uf->field_no;
908
909 /* Read the size of the vector. */
910 ulint N = mach_read_next_compressed(&undo_ptr);
911
912 if (N == 0) {
913 return undo_ptr;
914 }
915
916 /* Read the LOB first page number*/
917 uf->lob_first_page_no = mach_read_next_compressed(&undo_ptr);
918 uf->lob_version = mach_read_next_compressed(&undo_ptr);
919 uf->last_trx_id = mach_read_next_compressed(&undo_ptr);
920 uf->last_undo_no = mach_read_next_compressed(&undo_ptr);
921
922 for (size_t i = 0; i < N; ++i) {
923 Lob_diff lob_diff(uf->heap);
924 lob::undo_seq_t *lob_seq = nullptr;
925 lob::undo_data_t lob_undo_data;
926
927 if (lob_undo != nullptr) {
928 lob_seq = lob_undo->get_undo_sequence(field_no);
929 }
930
931 /* Read the offset. */
932 undo_ptr = lob_diff.read_offset(undo_ptr);
933 lob_undo_data.m_offset = lob_diff.m_offset;
934
935 /* Read the length. */
936 undo_ptr = lob_diff.read_length(undo_ptr);
937
938 /* Read the old data. */
939 lob_diff.set_old_data(undo_ptr);
940
941 /* Copy the data only if the lob_undo is not null. */
942 if (lob_seq != nullptr) {
943 undo_ptr = lob_undo_data.copy_old_data(undo_ptr, lob_diff.m_length);
944 } else {
945 undo_ptr += lob_diff.m_length;
946 }
947
948 lob_undo_data.m_version = uf->lob_version;
949 lob_undo_data.m_page_no = uf->lob_first_page_no;
950
951 if (lob_seq != nullptr) {
952 lob_seq->m_field_no = field_no;
953 lob_seq->push_back(lob_undo_data);
954 }
955
956 /* Read the number of LOB index entries modified. */
957 ulint n_entry = mach_read_next_compressed(&undo_ptr);
958
959 ut_ad(n_entry == 1 || n_entry == 2);
960
961 for (size_t i = 0; i < n_entry; ++i) {
962 lob_index_diff_t idx_diff;
963
964 /* Read the modifier trx id of the LOB index entry. */
965 idx_diff.m_modifier_trxid = mach_read_next_compressed(&undo_ptr);
966
967 /* Write the modifier trx undo_no of the LOB index entry. */
968 idx_diff.m_modifier_undo_no = mach_read_next_compressed(&undo_ptr);
969
970 lob_diff.m_idx_diffs->push_back(idx_diff);
971 }
972
973 uf->push_lob_diff(lob_diff);
974 DBUG_LOG("lob", lob_diff);
975 }
976
977 return undo_ptr;
978 }
979
980 /** Write the partial update information about LOBs to the undo log record.
981 @param[in] undo_page the undo page
982 @param[in] index the clustered index where LOBs are modified.
983 @param[in] undo_ptr the location within undo page where next
984 part of undo record is to be written.
985 @param[in] field the LOB data
986 @param[in] flen length of LOB data in bytes
987 @param[in] update the update vector containing partial update
988 information on LOBs.
989 @param[in] fld the field to which the LOB belongs.
990 @param[in] mtr the mini-transaction context.
991 @return the undo record pointer where new data can be written.
992 @return nullptr when there is not enough space in undo page. */
993 static byte *trx_undo_report_blob_update(page_t *undo_page, dict_index_t *index,
994 byte *undo_ptr, const byte *field,
995 ulint flen, const upd_t *update,
996 const upd_field_t *fld, mtr_t *mtr) {
997 DBUG_TRACE;
998
999 /* Access the LOB reference object. */
1000 byte *field_ref = const_cast<byte *>(field) + flen - lob::ref_t::SIZE;
1001
1002 lob::ref_t ref(field_ref);
1003
1004 /* Check if enough space for flag and vector length. */
1005 if (trx_undo_left(undo_page, undo_ptr) < 6) {
1006 return nullptr;
1007 }
1008
1009 /* Write one byte of flags. */
1010 *undo_ptr = 0x00;
1011 undo_ptr++;
1012
1013 if (fld == nullptr || update == nullptr) {
1014 /* Write the size of the vector as 0. */
1015 undo_ptr += mach_write_compressed(undo_ptr, 0);
1016 return undo_ptr;
1017 }
1018
1019 /* Find the Binary_diff object */
1020 const Binary_diff_vector *bdiff_v =
1021 update->get_binary_diff_by_field_no(fld->field_no);
1022
1023 if (bdiff_v == nullptr || !update->is_partially_updated(fld->field_no)) {
1024 /* Write the size of the vector as 0. */
1025 undo_ptr += mach_write_compressed(undo_ptr, 0);
1026 return undo_ptr;
1027 }
1028
1029 const ulint bytes_changed = upd_t::get_total_modified_bytes(*bdiff_v);
1030
1031 /* Whether the update to the LOB can be considered as a small change. */
1032 const bool small_change =
1033 (bytes_changed <= lob::ref_t::LOB_SMALL_CHANGE_THRESHOLD);
1034
1035 if (!small_change) {
1036 /* This is not a small change. So write the size of the vector as
1037 0 and bailout. */
1038 undo_ptr += mach_write_compressed(undo_ptr, 0);
1039 return undo_ptr;
1040 }
1041
1042 const page_size_t page_size = dict_table_page_size(index->table);
1043 if (page_size.is_compressed()) {
1044 /* This is compressed LOB. Not yet supporting. */
1045 undo_ptr += mach_write_compressed(undo_ptr, 0);
1046 return undo_ptr;
1047 }
1048
1049 trx_id_t last_trx_id;
1050 undo_no_t last_undo_no;
1051 ulint lob_version;
1052 page_type_t f_page_type;
1053
1054 /* Obtain LOB info. */
1055 lob::get_info(ref, index, lob_version, last_trx_id, last_undo_no, f_page_type,
1056 mtr);
1057
1058 /* Only the page type FIL_PAGE_TYPE_LOB_FIRST is supported here. */
1059 if (f_page_type != FIL_PAGE_TYPE_LOB_FIRST) {
1060 undo_ptr += mach_write_compressed(undo_ptr, 0);
1061 return undo_ptr;
1062 }
1063
1064 /* Only for small changes to the BLOB, we do regular undo logging. */
1065 size_t N = bdiff_v->size();
1066
1067 /* Write the size of the vector. */
1068 undo_ptr += mach_write_compressed(undo_ptr, N);
1069
1070 if (N == 0) {
1071 return undo_ptr;
1072 }
1073
1074 /* Check if there is enough space for lob_version, last_trx_id
1075 and last_undo_no. */
1076 if (trx_undo_left(undo_page, undo_ptr) < 20) {
1077 return nullptr;
1078 }
1079
1080 /* Write the LOB first page number*/
1081 undo_ptr += mach_write_compressed(undo_ptr, ref.page_no());
1082
1083 /* Write the lob version number */
1084 undo_ptr += mach_write_compressed(undo_ptr, lob_version);
1085
1086 /* Write the last trx id */
1087 undo_ptr += mach_write_compressed(undo_ptr, last_trx_id);
1088
1089 /* Write the last undo_no */
1090 undo_ptr += mach_write_compressed(undo_ptr, last_undo_no);
1091
1092 for (size_t i = 0; i < N; ++i) {
1093 const Binary_diff &bdiff = bdiff_v->at(i);
1094
1095 if (trx_undo_left(undo_page, undo_ptr) < 10) {
1096 return nullptr;
1097 }
1098
1099 /* Write the offset. */
1100 undo_ptr += mach_write_compressed(undo_ptr, bdiff.offset());
1101
1102 /* Write the length. */
1103 undo_ptr += mach_write_compressed(undo_ptr, bdiff.length());
1104
1105 if (trx_undo_left(undo_page, undo_ptr) < bdiff.length()) {
1106 return nullptr;
1107 }
1108
1109 /* Write the old data. */
1110 ut_memcpy(undo_ptr, bdiff.old_data(fld->mysql_field), bdiff.length());
1111 undo_ptr += bdiff.length();
1112
1113 lob::List_iem_t entries;
1114
1115 /* Find the affected LOB index entries. */
1116 lob::get_affected_index_entries(ref, index, bdiff, entries, mtr);
1117
1118 ulint n_entry = entries.size();
1119
1120 ut_ad(n_entry == 1 || n_entry == 2);
1121
1122 /* Check if there is enough space for n_entry */
1123 if (trx_undo_left(undo_page, undo_ptr) < 5) {
1124 return nullptr;
1125 }
1126
1127 /* Write the number of LOB index entries modified. */
1128 undo_ptr += mach_write_compressed(undo_ptr, n_entry);
1129
1130 for (lob::List_iem_t::iterator iter = entries.begin();
1131 iter != entries.end(); ++iter) {
1132 if (trx_undo_left(undo_page, undo_ptr) < 10) {
1133 return nullptr;
1134 }
1135
1136 /* Write the modifier trx id of the LOB index entry. */
1137 undo_ptr += mach_write_compressed(undo_ptr, iter->m_trx_id_modifier);
1138
1139 /* Write the modifier trx undo_no of the LOB index entry. */
1140 undo_ptr += mach_write_compressed(undo_ptr, iter->m_undo_no_modifier);
1141 }
1142 }
1143
1144 return undo_ptr;
1145 }
1146
1147 /**********************************************************************/ /**
1148 Reports in the undo log of an update or delete marking of a clustered index
1149 record.
1150 @return byte offset of the inserted undo log entry on the page if
1151 succeed, 0 if fail */
1152 static ulint trx_undo_page_report_modify(
1153 /*========================*/
1154 page_t *undo_page, /*!< in: undo log page */
1155 trx_t *trx, /*!< in: transaction */
1156 dict_index_t *index, /*!< in: clustered index where update or
1157 delete marking is done */
1158 const rec_t *rec, /*!< in: clustered index record which
1159 has NOT yet been modified */
1160 const ulint *offsets, /*!< in: rec_get_offsets(rec, index) */
1161 const upd_t *update, /*!< in: update vector which tells the
1162 columns to be updated; in the case of
1163 a delete, this should be set to NULL */
1164 ulint cmpl_info, /*!< in: compiler info on secondary
1165 index updates */
1166 const dtuple_t *row, /*!< in: clustered index row contains
1167 virtual column info */
1168 mtr_t *mtr) /*!< in: mtr */
1169 {
1170 DBUG_TRACE;
1171
1172 dict_table_t *table;
1173 ulint first_free;
1174 byte *ptr;
1175 const byte *field;
1176 ulint flen;
1177 ulint col_no;
1178 ulint type_cmpl;
1179 byte *type_cmpl_ptr;
1180 ulint i;
1181 trx_id_t trx_id;
1182 trx_undo_ptr_t *undo_ptr;
1183 bool ignore_prefix = false;
1184 byte ext_buf[REC_VERSION_56_MAX_INDEX_COL_LEN + BTR_EXTERN_FIELD_REF_SIZE];
1185 bool first_v_col = true;
1186
1187 ut_a(index->is_clustered());
1188 ut_ad(rec_offs_validate(rec, index, offsets));
1189 ut_ad(mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_TYPE) ==
1190 TRX_UNDO_UPDATE);
1191 table = index->table;
1192
1193 /* If table instance is temporary then select noredo rseg as changes
1194 to undo logs don't need REDO logging given that they are not
1195 restored on restart as corresponding object doesn't exist on restart.*/
1196 undo_ptr =
1197 index->table->is_temporary() ? &trx->rsegs.m_noredo : &trx->rsegs.m_redo;
1198
1199 first_free =
1200 mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE);
1201 ptr = undo_page + first_free;
1202
1203 ut_ad(first_free <= UNIV_PAGE_SIZE);
1204
1205 if (trx_undo_left(undo_page, ptr) < 50) {
1206 /* NOTE: the value 50 must be big enough so that the general
1207 fields written below fit on the undo log page */
1208
1209 return 0;
1210 }
1211
1212 /* Reserve 2 bytes for the pointer to the next undo log record */
1213 ptr += 2;
1214
1215 /* Store first some general parameters to the undo log */
1216
1217 if (!update) {
1218 ut_ad(!rec_get_deleted_flag(rec, dict_table_is_comp(table)));
1219 type_cmpl = TRX_UNDO_DEL_MARK_REC;
1220 } else if (rec_get_deleted_flag(rec, dict_table_is_comp(table))) {
1221 type_cmpl = TRX_UNDO_UPD_DEL_REC;
1222 /* We are about to update a delete marked record.
1223 We don't typically need the prefix in this case unless
1224 the delete marking is done by the same transaction
1225 (which we check below). */
1226 ignore_prefix = true;
1227 } else {
1228 type_cmpl = TRX_UNDO_UPD_EXIST_REC;
1229 }
1230
1231 type_cmpl |= cmpl_info * TRX_UNDO_CMPL_INFO_MULT;
1232 type_cmpl_ptr = ptr;
1233
1234 *ptr++ = (byte)type_cmpl;
1235
1236 /* Introducing a change in undo log format. */
1237 *type_cmpl_ptr |= TRX_UNDO_MODIFY_BLOB;
1238
1239 /* Introducing a new 1-byte flag. */
1240 *ptr++ = 0x00;
1241
1242 ptr += mach_u64_write_much_compressed(ptr, trx->undo_no);
1243
1244 ptr += mach_u64_write_much_compressed(ptr, table->id);
1245
1246 /*----------------------------------------*/
1247 /* Store the state of the info bits */
1248
1249 *ptr++ = (byte)rec_get_info_bits(rec, dict_table_is_comp(table));
1250
1251 /* Store the values of the system columns */
1252 field = rec_get_nth_field(nullptr, rec, offsets,
1253 index->get_sys_col_pos(DATA_TRX_ID), &flen);
1254 ut_ad(flen == DATA_TRX_ID_LEN);
1255
1256 trx_id = trx_read_trx_id(field);
1257
1258 /* If it is an update of a delete marked record, then we are
1259 allowed to ignore blob prefixes if the delete marking was done
1260 by some other trx as it must have committed by now for us to
1261 allow an over-write. */
1262 if (ignore_prefix) {
1263 ignore_prefix = (trx_id != trx->id);
1264 }
1265 ptr += mach_u64_write_compressed(ptr, trx_id);
1266
1267 field = rec_get_nth_field(nullptr, rec, offsets,
1268 index->get_sys_col_pos(DATA_ROLL_PTR), &flen);
1269 ut_ad(flen == DATA_ROLL_PTR_LEN);
1270
1271 ptr += mach_u64_write_compressed(ptr, trx_read_roll_ptr(field));
1272
1273 /*----------------------------------------*/
1274 /* Store then the fields required to uniquely determine the
1275 record which will be modified in the clustered index */
1276
1277 for (i = 0; i < dict_index_get_n_unique(index); i++) {
1278 field = rec_get_nth_field(index, rec, offsets, i, &flen);
1279
1280 /* The ordering columns must not be stored externally. */
1281 ut_ad(!rec_offs_nth_extern(index, offsets, i));
1282 ut_ad(!rec_offs_nth_default(index, offsets, i));
1283 ut_ad(index->get_col(i)->ord_part);
1284
1285 if (trx_undo_left(undo_page, ptr) < 5) {
1286 return 0;
1287 }
1288
1289 ptr += mach_write_compressed(ptr, flen);
1290
1291 if (flen != UNIV_SQL_NULL) {
1292 if (trx_undo_left(undo_page, ptr) < flen) {
1293 return 0;
1294 }
1295
1296 ut_memcpy(ptr, field, flen);
1297 ptr += flen;
1298 }
1299 }
1300
1301 /*----------------------------------------*/
1302 /* Save to the undo log the old values of the columns to be updated. */
1303
1304 if (update) {
1305 if (trx_undo_left(undo_page, ptr) < 5) {
1306 return 0;
1307 }
1308
1309 ulint n_updated = upd_get_n_fields(update);
1310
1311 /* If this is an online update while an inplace alter table
1312 is in progress and the table has virtual column, we will
1313 need to double check if there are any non-indexed columns
1314 being registered in update vector in case they will be indexed
1315 in new table */
1316 if (dict_index_is_online_ddl(index) && index->table->n_v_cols > 0) {
1317 for (i = 0; i < upd_get_n_fields(update); i++) {
1318 const upd_field_t *fld = upd_get_nth_field(update, i);
1319 ulint pos = fld->field_no;
1320
1321 /* These columns must not have an index
1322 on them */
1323 if (fld->is_virtual() &&
1324 dict_table_get_nth_v_col(table, pos)->v_indexes->empty()) {
1325 n_updated--;
1326 }
1327 }
1328 }
1329
1330 ptr += mach_write_compressed(ptr, n_updated);
1331
1332 for (i = 0; i < upd_get_n_fields(update); i++) {
1333 const upd_field_t *fld = upd_get_nth_field(update, i);
1334
1335 bool is_virtual = fld->is_virtual();
1336 bool is_multi_val = upd_fld_is_multi_value_col(fld);
1337 ulint max_v_log_len = 0;
1338
1339 ulint pos = fld->field_no;
1340
1341 /* Write field number to undo log */
1342 if (trx_undo_left(undo_page, ptr) < 5) {
1343 return 0;
1344 }
1345
1346 if (is_virtual) {
1347 /* Skip the non-indexed column, during
1348 an online alter table */
1349 if (dict_index_is_online_ddl(index) &&
1350 dict_table_get_nth_v_col(table, pos)->v_indexes->empty()) {
1351 continue;
1352 }
1353
1354 /* add REC_MAX_N_FIELDS to mark this
1355 is a virtual col */
1356 pos += REC_MAX_N_FIELDS;
1357 }
1358
1359 if (index->has_row_versions() && !is_virtual) {
1360 /* Write physical position of field in UNDO */
1361 auto phy_pos = index->get_field(pos)->col->get_col_phy_pos();
1362 ut_ad(phy_pos == fld->field_phy_pos);
1363 ut_ad(!index->get_field(pos)->col->is_instant_dropped());
1364 ptr += mach_write_compressed(ptr, phy_pos);
1365 } else {
1366 ptr += mach_write_compressed(ptr, pos);
1367 }
1368
1369 /* Save the old value of field */
1370 if (is_virtual) {
1371 ut_ad(fld->field_no < table->n_v_def);
1372
1373 ptr = trx_undo_log_v_idx(undo_page, table, fld->field_no, ptr,
1374 first_v_col);
1375 if (ptr == nullptr) {
1376 return 0;
1377 }
1378 first_v_col = false;
1379
1380 max_v_log_len = dict_max_v_field_len_store_undo(table, fld->field_no);
1381
1382 field = static_cast<byte *>(fld->old_v_val->data);
1383 flen = fld->old_v_val->len;
1384
1385 /* Only log sufficient bytes for index
1386 record update */
1387 if (flen != UNIV_SQL_NULL) {
1388 flen = std::min(flen, max_v_log_len);
1389 }
1390 } else {
1391 field = rec_get_nth_field_instant(rec, offsets, pos, index, &flen);
1392 }
1393
1394 if (trx_undo_left(undo_page, ptr) < 15) {
1395 return 0;
1396 }
1397
1398 if (!is_virtual && rec_offs_nth_extern(index, offsets, pos)) {
1399 ut_ad(!is_multi_val);
1400 const dict_col_t *col = index->get_col(pos);
1401 ulint prefix_len = dict_max_field_len_store_undo(table, col);
1402
1403 ut_ad(prefix_len + BTR_EXTERN_FIELD_REF_SIZE <= sizeof ext_buf);
1404
1405 ptr = trx_undo_page_report_modify_ext(
1406 trx, index, ptr,
1407 col->ord_part && !ignore_prefix &&
1408 flen < REC_ANTELOPE_MAX_INDEX_COL_LEN
1409 ? ext_buf
1410 : nullptr,
1411 prefix_len, dict_table_page_size(table), &field, &flen,
1412 dict_table_is_sdi(table->id), SPATIAL_UNKNOWN);
1413
1414 /* Notify purge that it eventually has to
1415 free the old externally stored field */
1416
1417 undo_ptr->update_undo->del_marks = true;
1418
1419 *type_cmpl_ptr |= TRX_UNDO_UPD_EXTERN;
1420 } else if (!is_multi_val) {
1421 ptr += mach_write_compressed(ptr, flen);
1422 }
1423
1424 if (is_multi_val) {
1425 bool suc = trx_undo_store_multi_value(undo_page, fld->old_v_val, &ptr);
1426 if (!suc) {
1427 return 0;
1428 }
1429 } else if (flen != UNIV_SQL_NULL) {
1430 if (trx_undo_left(undo_page, ptr) < flen) {
1431 return 0;
1432 }
1433
1434 ut_memcpy(ptr, field, flen);
1435 ptr += flen;
1436
1437 if (!is_virtual && rec_offs_nth_extern(index, offsets, pos)) {
1438 ptr = trx_undo_report_blob_update(undo_page, index, ptr, field, flen,
1439 update, fld, mtr);
1440
1441 if (ptr == nullptr) {
1442 return 0;
1443 }
1444 }
1445 }
1446
1447 /* Also record the new value for virtual column */
1448 if (is_virtual) {
1449 field = static_cast<byte *>(fld->new_val.data);
1450 flen = fld->new_val.len;
1451 if (flen != UNIV_SQL_NULL) {
1452 flen = std::min(flen, max_v_log_len);
1453 }
1454
1455 if (trx_undo_left(undo_page, ptr) < 15) {
1456 return 0;
1457 }
1458
1459 if (is_multi_val) {
1460 bool suc = trx_undo_store_multi_value(undo_page, &fld->new_val, &ptr);
1461 if (!suc) {
1462 return 0;
1463 }
1464 } else {
1465 ptr += mach_write_compressed(ptr, flen);
1466
1467 if (flen != UNIV_SQL_NULL) {
1468 if (trx_undo_left(undo_page, ptr) < flen) {
1469 return 0;
1470 }
1471
1472 ut_memcpy(ptr, field, flen);
1473 ptr += flen;
1474 }
1475 }
1476 }
1477 }
1478 }
1479
1480 /* Reset the first_v_col, so to put the virtual column undo
1481 version marker again, when we log all the indexed columns */
1482 first_v_col = true;
1483
1484 /*----------------------------------------*/
1485 /* In the case of a delete marking, and also in the case of an update
1486 where any ordering field of any index changes, store the values of all
1487 columns which occur as ordering fields in any index. This info is used
1488 in the purge of old versions where we use it to build and search the
1489 delete marked index records, to look if we can remove them from the
1490 index tree. Note that starting from 4.0.14 also externally stored
1491 fields can be ordering in some index. Starting from 5.2, we no longer
1492 store REC_MAX_INDEX_COL_LEN first bytes to the undo log record,
1493 but we can construct the column prefix fields in the index by
1494 fetching the first page of the BLOB that is pointed to by the
1495 clustered index. This works also in crash recovery, because all pages
1496 (including BLOBs) are recovered before anything is rolled back. */
1497
1498 if (!update || !(cmpl_info & UPD_NODE_NO_ORD_CHANGE)) {
1499 byte *old_ptr = ptr;
1500 double mbr[SPDIMS * 2];
1501 mem_heap_t *row_heap = nullptr;
1502
1503 undo_ptr->update_undo->del_marks = true;
1504
1505 if (trx_undo_left(undo_page, ptr) < 5) {
1506 return 0;
1507 }
1508
1509 /* Reserve 2 bytes to write the number of bytes the stored
1510 fields take in this undo record */
1511
1512 ptr += 2;
1513
1514 for (col_no = 0; col_no < table->get_n_cols(); col_no++) {
1515 const dict_col_t *col = table->get_col(col_no);
1516
1517 if (col->ord_part) {
1518 ulint pos;
1519 spatial_status_t spatial_status;
1520
1521 spatial_status = SPATIAL_NONE;
1522
1523 /* Write field number to undo log */
1524 if (trx_undo_left(undo_page, ptr) < 5 + 15) {
1525 return 0;
1526 }
1527
1528 pos = index->get_col_pos(col_no);
1529 if (index->has_row_versions()) {
1530 /* Write physical position of field in UNDO */
1531 ut_ad(!col->is_virtual());
1532 ut_ad(!col->is_instant_dropped());
1533
1534 auto phy_pos = col->get_col_phy_pos();
1535 ut_ad(phy_pos < REC_MAX_N_FIELDS);
1536
1537 ptr += mach_write_compressed(ptr, phy_pos);
1538 } else {
1539 ptr += mach_write_compressed(ptr, pos);
1540 }
1541
1542 /* Save the old value of field */
1543 field = rec_get_nth_field_instant(rec, offsets, pos, index, &flen);
1544
1545 if (rec_offs_nth_extern(index, offsets, pos)) {
1546 const dict_col_t *col = index->get_col(pos);
1547 ulint prefix_len = dict_max_field_len_store_undo(table, col);
1548
1549 ut_a(prefix_len < sizeof ext_buf);
1550
1551 spatial_status = col->get_spatial_status();
1552
1553 /* If there is a spatial index on it,
1554 log its MBR */
1555 if (spatial_status != SPATIAL_NONE) {
1556 ut_ad(DATA_GEOMETRY_MTYPE(col->mtype));
1557
1558 trx_undo_get_mbr_from_ext(trx, index, mbr,
1559 dict_table_page_size(table), field, &flen,
1560 index->rtr_srs.get());
1561 }
1562
1563 ptr = trx_undo_page_report_modify_ext(
1564 trx, index, ptr,
1565 flen < REC_ANTELOPE_MAX_INDEX_COL_LEN && !ignore_prefix ? ext_buf
1566 : nullptr,
1567 prefix_len, dict_table_page_size(table), &field, &flen,
1568 dict_table_is_sdi(table->id), spatial_status);
1569 } else {
1570 ptr += mach_write_compressed(ptr, flen);
1571 }
1572
1573 if (flen != UNIV_SQL_NULL && spatial_status != SPATIAL_ONLY) {
1574 if (trx_undo_left(undo_page, ptr) < flen) {
1575 return 0;
1576 }
1577
1578 ut_memcpy(ptr, field, flen);
1579 ptr += flen;
1580 }
1581
1582 if (spatial_status != SPATIAL_NONE) {
1583 if (trx_undo_left(undo_page, ptr) < DATA_MBR_LEN) {
1584 return 0;
1585 }
1586
1587 for (uint i = 0; i < SPDIMS * 2; i++) {
1588 mach_double_write(ptr, mbr[i]);
1589 ptr += sizeof(double);
1590 }
1591 }
1592 }
1593 }
1594
1595 for (col_no = 0; col_no < dict_table_get_n_v_cols(table); col_no++) {
1596 dfield_t *vfield = nullptr;
1597
1598 const dict_v_col_t *col = dict_table_get_nth_v_col(table, col_no);
1599
1600 if (col->m_col.ord_part) {
1601 ulint pos = col_no;
1602 ulint max_v_log_len = dict_max_v_field_len_store_undo(table, pos);
1603
1604 /* Write field number to undo log.
1605 Make sure there is enough space in log */
1606 if (trx_undo_left(undo_page, ptr) < 5) {
1607 return 0;
1608 }
1609
1610 pos += REC_MAX_N_FIELDS;
1611 ptr += mach_write_compressed(ptr, pos);
1612
1613 ut_ad(col_no < table->n_v_def);
1614 ptr = trx_undo_log_v_idx(undo_page, table, col_no, ptr, first_v_col);
1615 first_v_col = false;
1616
1617 if (!ptr) {
1618 return 0;
1619 }
1620
1621 if (update) {
1622 ut_ad(!row);
1623 if (update->old_vrow == nullptr) {
1624 /* This only happens in cascade update. And virtual column can't be
1625 affected, so it is Ok to set it to NULL */
1626 flen = UNIV_SQL_NULL;
1627 } else {
1628 vfield = dtuple_get_nth_v_field(update->old_vrow, col->v_pos);
1629 }
1630 } else if (row) {
1631 vfield = dtuple_get_nth_v_field(row, col->v_pos);
1632 } else {
1633 ut_d(ut_error);
1634 }
1635
1636 if (vfield) {
1637 field = static_cast<byte *>(vfield->data);
1638 flen = vfield->len;
1639 } else {
1640 ut_ad(flen == UNIV_SQL_NULL);
1641 }
1642
1643 /* Prepare to write the field length and field data */
1644 if (flen != UNIV_SQL_NULL) {
1645 flen = std::min(flen, max_v_log_len);
1646
1647 if (trx_undo_left(undo_page, ptr) < 5 + flen) {
1648 return 0;
1649 }
1650 } else if (trx_undo_left(undo_page, ptr) < 5) {
1651 return 0;
1652 }
1653
1654 if (col->m_col.is_multi_value()) {
1655 dfield_t multi_value_field;
1656 if (vfield == nullptr) {
1657 dfield_set_null(&multi_value_field);
1658 vfield = &multi_value_field;
1659 }
1660 bool suc = trx_undo_store_multi_value(undo_page, vfield, &ptr);
1661 if (!suc) {
1662 return 0;
1663 }
1664 } else {
1665 ptr += mach_write_compressed(ptr, flen);
1666
1667 if (flen != UNIV_SQL_NULL) {
1668 ut_memcpy(ptr, field, flen);
1669 ptr += flen;
1670 }
1671 }
1672 }
1673 }
1674
1675 mach_write_to_2(old_ptr, ptr - old_ptr);
1676
1677 if (row_heap) {
1678 mem_heap_free(row_heap);
1679 }
1680 }
1681
1682 /*----------------------------------------*/
1683 /* Write pointers to the previous and the next undo log records */
1684 if (trx_undo_left(undo_page, ptr) < 2) {
1685 return 0;
1686 }
1687
1688 mach_write_to_2(ptr, first_free);
1689 ptr += 2;
1690 mach_write_to_2(undo_page + first_free, ptr - undo_page);
1691
1692 mach_write_to_2(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE,
1693 ptr - undo_page);
1694
1695 /* Write to the REDO log about this change in the UNDO log */
1696
1697 trx_undof_page_add_undo_rec_log(undo_page, first_free, ptr - undo_page, mtr);
1698 return first_free;
1699 }
1700
1701 /** Reads from an undo log update record the system field values of the old
1702 version.
1703 @return remaining part of undo log record after reading these values */
1704 byte *trx_undo_update_rec_get_sys_cols(
1705 const byte *ptr, /*!< in: remaining part of undo
1706 log record after reading
1707 general parameters */
1708 trx_id_t *trx_id, /*!< out: trx id */
1709 roll_ptr_t *roll_ptr, /*!< out: roll ptr */
1710 ulint *info_bits) /*!< out: info bits state */
1711 {
1712 /* Read the state of the info bits */
1713 *info_bits = mach_read_from_1(ptr);
1714 ptr += 1;
1715
1716 /* Read the values of the system columns */
1717
1718 *trx_id = mach_u64_read_next_compressed(&ptr);
1719 *roll_ptr = mach_u64_read_next_compressed(&ptr);
1720
1721 return (const_cast<byte *>(ptr));
1722 }
1723
1724 byte *trx_undo_update_rec_get_update(const byte *ptr, const dict_index_t *index,
1725 ulint type, trx_id_t trx_id,
1726 roll_ptr_t roll_ptr, ulint info_bits,
1727 mem_heap_t *heap, upd_t **upd,
1728 lob::undo_vers_t *lob_undo,
1729 type_cmpl_t &type_cmpl) {
1730 DBUG_TRACE;
1731
1732 upd_field_t *upd_field;
1733 upd_t *update;
1734 ulint n_fields;
1735 byte *buf;
1736 ulint i;
1737 bool first_v_col = true;
1738 bool is_undo_log = true;
1739 ulint n_skip_field = 0;
1740
1741 ut_a(index->is_clustered());
1742
1743 if (type != TRX_UNDO_DEL_MARK_REC) {
1744 n_fields = mach_read_next_compressed(&ptr);
1745 } else {
1746 n_fields = 0;
1747 }
1748
1749 update = upd_create(n_fields + 2, heap);
1750
1751 update->table = index->table;
1752
1753 update->info_bits = info_bits;
1754
1755 /* Store first trx id and roll ptr to update vector */
1756
1757 upd_field = upd_get_nth_field(update, n_fields);
1758
1759 buf = static_cast<byte *>(mem_heap_alloc(heap, DATA_TRX_ID_LEN));
1760
1761 trx_write_trx_id(buf, trx_id);
1762
1763 auto const trx_id_col = index->table->get_sys_col(DATA_TRX_ID);
1764 upd_field_set_field_no(upd_field, dict_col_get_clust_pos(trx_id_col, index),
1765 index);
1766 dfield_set_data(&(upd_field->new_val), buf, DATA_TRX_ID_LEN);
1767 trx_id_col->copy_type(dfield_get_type(&upd_field->new_val));
1768
1769 upd_field = upd_get_nth_field(update, n_fields + 1);
1770
1771 buf = static_cast<byte *>(mem_heap_alloc(heap, DATA_ROLL_PTR_LEN));
1772
1773 trx_write_roll_ptr(buf, roll_ptr);
1774
1775 auto const data_roll_ptr_col = index->table->get_sys_col(DATA_ROLL_PTR);
1776 upd_field_set_field_no(
1777 upd_field, dict_col_get_clust_pos(data_roll_ptr_col, index), index);
1778 dfield_set_data(&(upd_field->new_val), buf, DATA_ROLL_PTR_LEN);
1779 data_roll_ptr_col->copy_type(dfield_get_type(&upd_field->new_val));
1780
1781 /* Store then the updated ordinary columns to the update vector */
1782
1783 for (i = 0; i < n_fields; i++) {
1784 const byte *field;
1785 ulint len;
1786 ulint field_no;
1787 ulint orig_len;
1788 bool is_virtual;
1789 dict_v_col_t *vcol = nullptr;
1790
1791 field_no = mach_read_next_compressed(&ptr);
1792
1793 is_virtual = (field_no >= REC_MAX_N_FIELDS);
1794
1795 if (is_virtual) {
1796 /* If new version, we need to check index list to figure
1797 out the correct virtual column position */
1798 ptr = trx_undo_read_v_idx(index->table, ptr, first_v_col, &is_undo_log,
1799 &field_no);
1800 first_v_col = false;
1801 } else if (field_no >= dict_index_get_n_fields(index)) {
1802 ib::error(ER_IB_MSG_1184)
1803 << "Trying to access update undo rec"
1804 " field "
1805 << field_no << " in index " << index->name << " of table "
1806 << index->table->name << " but index has only "
1807 << dict_index_get_n_fields(index) << " fields " << BUG_REPORT_MSG
1808 << ". Run also CHECK TABLE " << index->table->name
1809 << "."
1810 " n_fields = "
1811 << n_fields << ", i = " << i << ", ptr " << ptr;
1812
1813 ut_d(ut_error);
1814 ut_o(*upd = nullptr);
1815 ut_o(return nullptr);
1816 }
1817
1818 upd_field = upd_get_nth_field(update, i);
1819
1820 if (is_virtual) {
1821 /* This column could be dropped or no longer indexed */
1822 if (field_no == ULINT_UNDEFINED) {
1823 /* Mark this is no longer needed */
1824 upd_field->field_no = REC_MAX_N_FIELDS;
1825
1826 if (trx_undo_rec_is_multi_value(ptr)) {
1827 ptr = trx_undo_rec_get_multi_value(ptr, nullptr, heap);
1828 ut_ad(trx_undo_rec_is_multi_value(ptr));
1829 ptr = trx_undo_rec_get_multi_value(ptr, nullptr, heap);
1830 } else {
1831 ptr = trx_undo_rec_get_col_val(ptr, &field, &len, &orig_len);
1832 ptr = trx_undo_rec_get_col_val(ptr, &field, &len, &orig_len);
1833 }
1834 n_skip_field++;
1835 continue;
1836 } else {
1837 vcol = dict_table_get_nth_v_col(index->table, field_no);
1838 }
1839
1840 upd_field_set_v_field_no(upd_field, field_no, index);
1841 } else {
1842 if (index->has_row_versions()) {
1843 auto log_pos = index->fields_array[field_no];
1844 upd_field_set_field_no(upd_field, log_pos, index);
1845 IF_DEBUG(upd_field->field_phy_pos = field_no;)
1846 } else {
1847 upd_field_set_field_no(upd_field, field_no, index);
1848 }
1849 }
1850
1851 if (vcol != nullptr && vcol->m_col.is_multi_value()) {
1852 ptr = trx_undo_rec_get_multi_value(ptr, &upd_field->new_val, heap);
1853 } else {
1854 ptr = trx_undo_rec_get_col_val(ptr, &field, &len, &orig_len);
1855
1856 upd_field->orig_len = orig_len;
1857
1858 if (len == UNIV_SQL_NULL) {
1859 dfield_set_null(&upd_field->new_val);
1860 } else if (len < UNIV_EXTERN_STORAGE_FIELD) {
1861 dfield_set_data(&upd_field->new_val, field, len);
1862 } else {
1863 len -= UNIV_EXTERN_STORAGE_FIELD;
1864
1865 dfield_set_data(&upd_field->new_val, field, len);
1866 dfield_set_ext(&upd_field->new_val);
1867
1868 if (type_cmpl.is_lob_undo() && type_cmpl.is_lob_updated()) {
1869 /* Read the partial update on LOB */
1870 ptr = trx_undo_read_blob_update(ptr, upd_field, lob_undo);
1871 }
1872 }
1873 }
1874
1875 if (is_virtual) {
1876 upd_field->old_v_val = static_cast<dfield_t *>(
1877 mem_heap_zalloc(heap, sizeof *upd_field->old_v_val));
1878
1879 if (vcol != nullptr && vcol->m_col.is_multi_value()) {
1880 ptr = trx_undo_rec_get_multi_value(ptr, upd_field->old_v_val, heap);
1881 } else {
1882 ptr = trx_undo_rec_get_col_val(ptr, &field, &len, &orig_len);
1883 if (len == UNIV_SQL_NULL) {
1884 dfield_set_null(upd_field->old_v_val);
1885 } else if (len < UNIV_EXTERN_STORAGE_FIELD) {
1886 dfield_set_data(upd_field->old_v_val, field, len);
1887 } else {
1888 ut_d(ut_error);
1889 }
1890 }
1891 }
1892 }
1893
1894 /* In rare scenario, we could have skipped virtual column (as they
1895 are dropped. We will regenerate a update vector and skip them */
1896 if (n_skip_field > 0) {
1897 ulint n = 0;
1898 ut_ad(n_skip_field <= n_fields);
1899
1900 upd_t *new_update = upd_create(n_fields + 2 - n_skip_field, heap);
1901
1902 for (i = 0; i < n_fields + 2; i++) {
1903 upd_field = upd_get_nth_field(update, i);
1904
1905 if (upd_field->field_no == REC_MAX_N_FIELDS) {
1906 continue;
1907 }
1908
1909 upd_field_t *new_upd_field = upd_get_nth_field(new_update, n);
1910 *new_upd_field = *upd_field;
1911 n++;
1912 }
1913 ut_ad(n == n_fields + 2 - n_skip_field);
1914 *upd = new_update;
1915 } else {
1916 *upd = update;
1917 }
1918
1919 return const_cast<byte *>(ptr);
1920 }
1921
1922 /** Builds a partial row from an update undo log record, for purge.
1923 It contains the columns which occur as ordering in any index of the table.
1924 Any missing columns are indicated by col->mtype == DATA_MISSING.
1925 @return pointer to remaining part of undo record */
1926 byte *trx_undo_rec_get_partial_row(
1927 const byte *ptr, /*!< in: remaining part in update undo log
1928 record of a suitable type, at the start of
1929 the stored index columns;
1930 NOTE that this copy of the undo log record must
1931 be preserved as long as the partial row is
1932 used, as we do NOT copy the data in the
1933 record! */
1934 dict_index_t *index, /*!< in: clustered index */
1935 dtuple_t **row, /*!< out, own: partial row */
1936 bool ignore_prefix, /*!< in: flag to indicate if we
1937 expect blob prefixes in undo. Used
1938 only in the assertion. */
1939 mem_heap_t *heap) /*!< in: memory heap from which the memory
1940 needed is allocated */
1941 {
1942 const byte *end_ptr;
1943 bool first_v_col = true;
1944 bool is_undo_log = true;
1945
1946 ut_ad(index);
1947 ut_ad(ptr);
1948 ut_ad(row);
1949 ut_ad(heap);
1950 ut_ad(index->is_clustered());
1951
1952 *row = dtuple_create_with_vcol(heap, index->table->get_n_cols(),
1953 dict_table_get_n_v_cols(index->table));
1954
1955 /* Mark all columns in the row uninitialized, so that
1956 we can distinguish missing fields from fields that are SQL NULL. */
1957 for (ulint i = 0; i < index->table->get_n_cols(); i++) {
1958 dfield_get_type(dtuple_get_nth_field(*row, i))->mtype = DATA_MISSING;
1959 /* In case a multi-value field checking read uninitialized value */
1960 dfield_get_type(dtuple_get_nth_field(*row, i))->prtype = 0;
1961 }
1962
1963 dtuple_init_v_fld(*row);
1964
1965 end_ptr = ptr + mach_read_from_2(ptr);
1966 ptr += 2;
1967
1968 while (ptr != end_ptr) {
1969 dfield_t *dfield = nullptr;
1970 const byte *field;
1971 ulint field_no = ULINT_UNDEFINED;
1972 const dict_col_t *col = nullptr;
1973 ulint col_no;
1974 ulint len;
1975 ulint orig_len;
1976 bool is_virtual;
1977 dict_v_col_t *vcol = nullptr;
1978
1979 field_no = mach_read_next_compressed(&ptr);
1980
1981 is_virtual = (field_no >= REC_MAX_N_FIELDS);
1982
1983 if (is_virtual) {
1984 ptr = trx_undo_read_v_idx(index->table, ptr, first_v_col, &is_undo_log,
1985 &field_no);
1986 first_v_col = false;
1987 if (field_no != ULINT_UNDEFINED) {
1988 vcol = dict_table_get_nth_v_col(index->table, field_no);
1989 col = &vcol->m_col;
1990 col_no = dict_col_get_no(col);
1991 dfield = dtuple_get_nth_v_field(*row, vcol->v_pos);
1992 vcol->m_col.copy_type(dfield_get_type(dfield));
1993 }
1994 }
1995
1996 if ((vcol != nullptr && vcol->m_col.is_multi_value()) ||
1997 trx_undo_rec_is_multi_value(ptr)) {
1998 ut_ad(is_virtual);
1999 ut_ad(vcol != nullptr || field_no == ULINT_UNDEFINED);
2000 ut_ad(dfield != nullptr || field_no == ULINT_UNDEFINED);
2001 ptr = trx_undo_rec_get_multi_value(ptr, dfield, heap);
2002 continue;
2003 } else {
2004 ptr = trx_undo_rec_get_col_val(ptr, &field, &len, &orig_len);
2005 }
2006
2007 /* This column could be dropped or no longer indexed */
2008 if (field_no == ULINT_UNDEFINED) {
2009 ut_ad(is_virtual);
2010 continue;
2011 }
2012
2013 if (!is_virtual) {
2014 if (index->has_row_versions()) {
2015 /* This field_no is physical pos */
2016 col = index->get_physical_field(field_no)->col;
2017 } else {
2018 col = index->get_col(field_no);
2019 }
2020
2021 /* This column shouldn't be dropped unless index on this column is
2022 dropped. */
2023 ut_ad(!col->is_instant_dropped() || !col->ord_part);
2024 if (col->is_instant_dropped()) {
2025 continue;
2026 }
2027 col_no = dict_col_get_no(col);
2028 dfield = dtuple_get_nth_field(*row, col_no);
2029 index->table->get_col(col_no)->copy_type(dfield_get_type(dfield));
2030 }
2031
2032 dfield_set_data(dfield, field, len);
2033
2034 if (len != UNIV_SQL_NULL && len >= UNIV_EXTERN_STORAGE_FIELD) {
2035 spatial_status_t spatial_status;
2036
2037 /* Decode spatial status. */
2038 spatial_status = static_cast<spatial_status_t>(
2039 (len & SPATIAL_STATUS_MASK) >> SPATIAL_STATUS_SHIFT);
2040 len &= ~SPATIAL_STATUS_MASK;
2041
2042 /* Keep compatible with 5.7.9 format. */
2043 if (spatial_status == SPATIAL_UNKNOWN) {
2044 spatial_status = col->get_spatial_status();
2045 }
2046
2047 switch (spatial_status) {
2048 case SPATIAL_ONLY:
2049 ut_ad(len - UNIV_EXTERN_STORAGE_FIELD == DATA_MBR_LEN);
2050 dfield_set_len(dfield, len - UNIV_EXTERN_STORAGE_FIELD);
2051 break;
2052
2053 case SPATIAL_MIXED:
2054 dfield_set_len(dfield,
2055 len - UNIV_EXTERN_STORAGE_FIELD - DATA_MBR_LEN);
2056 break;
2057
2058 case SPATIAL_NONE:
2059 dfield_set_len(dfield, len - UNIV_EXTERN_STORAGE_FIELD);
2060 break;
2061
2062 case SPATIAL_UNKNOWN:
2063 ut_d(ut_error);
2064 ut_o(break);
2065 }
2066
2067 dfield_set_ext(dfield);
2068 dfield_set_spatial_status(dfield, spatial_status);
2069
2070 /* If the prefix of this column is indexed,
2071 ensure that enough prefix is stored in the
2072 undo log record. */
2073 if (!ignore_prefix && col->ord_part && spatial_status != SPATIAL_ONLY) {
2074 ut_a(dfield_get_len(dfield) >= BTR_EXTERN_FIELD_REF_SIZE);
2075 ut_a(dict_table_has_atomic_blobs(index->table) ||
2076 dfield_get_len(dfield) >=
2077 REC_ANTELOPE_MAX_INDEX_COL_LEN + BTR_EXTERN_FIELD_REF_SIZE);
2078 }
2079 }
2080 }
2081
2082 return (const_cast<byte *>(ptr));
2083 }
2084 #endif /* !UNIV_HOTBACKUP */
2085
2086 /** Erases the unused undo log page end.
2087 @return true if the page contained something, false if it was empty */
2088 static bool trx_undo_erase_page_end(
2089 page_t *undo_page, /*!< in/out: undo page whose end to erase */
2090 mtr_t *mtr) /*!< in/out: mini-transaction */
2091 {
2092 ulint first_free;
2093
2094 first_free =
2095 mach_read_from_2(undo_page + TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_FREE);
2096 memset(undo_page + first_free, 0xff,
2097 (UNIV_PAGE_SIZE - FIL_PAGE_DATA_END) - first_free);
2098
2099 mlog_write_initial_log_record(undo_page, MLOG_UNDO_ERASE_END, mtr);
2100 return (first_free != TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_HDR_SIZE);
2101 }
2102
2103 byte *trx_undo_parse_erase_page_end(byte *ptr, byte *end_ptr [[maybe_unused]],
2104 page_t *page, mtr_t *mtr) {
2105 if (page == nullptr) {
2106 return (ptr);
2107 }
2108
2109 trx_undo_erase_page_end(page, mtr);
2110
2111 return (ptr);
2112 }
2113
2114 #ifndef UNIV_HOTBACKUP
2115 /** Writes information to an undo log about an insert, update, or a delete
2116 marking of a clustered index record. This information is used in a rollback of
2117 the transaction and in consistent reads that must look to the history of this
2118 transaction.
2119 @return DB_SUCCESS or error code */
2120 dberr_t trx_undo_report_row_operation(
2121 ulint flags, /*!< in: if BTR_NO_UNDO_LOG_FLAG bit is
2122 set, does nothing */
2123 ulint op_type, /*!< in: TRX_UNDO_INSERT_OP or
2124 TRX_UNDO_MODIFY_OP */
2125 que_thr_t *thr, /*!< in: query thread */
2126 dict_index_t *index, /*!< in: clustered index */
2127 const dtuple_t *clust_entry, /*!< in: in the case of an insert,
2128 index entry to insert into the
2129 clustered index, otherwise NULL */
2130 const upd_t *update, /*!< in: in the case of an update,
2131 the update vector, otherwise NULL */
2132 ulint cmpl_info, /*!< in: compiler info on secondary
2133 index updates */
2134 const rec_t *rec, /*!< in: in case of an update or delete
2135 marking, the record in the clustered
2136 index, otherwise NULL */
2137 const ulint *offsets, /*!< in: rec_get_offsets(rec) */
2138 roll_ptr_t *roll_ptr) /*!< out: rollback pointer to the
2139 inserted undo log record,
2140 0 if BTR_NO_UNDO_LOG
2141 flag was specified */
2142 {
2143 trx_t *trx;
2144 trx_undo_t *undo;
2145 page_no_t page_no;
2146 buf_block_t *undo_block;
2147 trx_undo_ptr_t *undo_ptr;
2148 mtr_t mtr;
2149 dberr_t err = DB_SUCCESS;
2150 #ifdef UNIV_DEBUG
2151 int loop_count = 0;
2152 #endif /* UNIV_DEBUG */
2153
2154 ut_a(index->is_clustered());
2155 ut_ad(!rec || rec_offs_validate(rec, index, offsets));
2156
2157 if (flags & BTR_NO_UNDO_LOG_FLAG) {
2158 *roll_ptr = 0;
2159
2160 return (DB_SUCCESS);
2161 }
2162
2163 ut_ad(thr);
2164 ut_ad(!srv_read_only_mode);
2165 ut_ad((op_type != TRX_UNDO_INSERT_OP) || (clust_entry && !update && !rec));
2166
2167 trx = thr_get_trx(thr);
2168
2169 bool is_temp_table = index->table->is_temporary();
2170
2171 /* Temporary tables do not go into INFORMATION_SCHEMA.TABLES,
2172 so do not bother adding it to the list of modified tables by
2173 the transaction - this list is only used for maintaining
2174 INFORMATION_SCHEMA.TABLES.UPDATE_TIME. */
2175 if (!is_temp_table) {
2176 trx->mod_tables.insert(index->table);
2177 }
2178
2179 /* If trx is read-only then only temp-tables can be written. */
2180 ut_ad(!trx->read_only || is_temp_table);
2181
2182 /* If this is a temp-table then we assign temporary rseg. */
2183 if (is_temp_table && trx->rsegs.m_noredo.rseg == nullptr) {
2184 trx_assign_rseg_temp(trx);
2185 }
2186
2187 mtr_start(&mtr);
2188
2189 if (is_temp_table) {
2190 /* If object is temporary, disable REDO logging that
2191 is done to track changes done to UNDO logs. This is
2192 feasible given that temporary tables and temporary
2193 undo logs are not restored on restart. */
2194 undo_ptr = &trx->rsegs.m_noredo;
2195 mtr.set_log_mode(MTR_LOG_NO_REDO);
2196 } else {
2197 undo_ptr = &trx->rsegs.m_redo;
2198 }
2199
2200 mutex_enter(&trx->undo_mutex);
2201
2202 #ifdef UNIV_DEBUG
2203 if (srv_inject_too_many_concurrent_trxs) {
2204 err = DB_TOO_MANY_CONCURRENT_TRXS;
2205 goto err_exit;
2206 }
2207 #endif /* UNIV_DEBUG */
2208
2209 switch (op_type) {
2210 case TRX_UNDO_INSERT_OP:
2211 undo = undo_ptr->insert_undo;
2212
2213 if (undo == nullptr) {
2214 err = trx_undo_assign_undo(trx, undo_ptr, TRX_UNDO_INSERT);
2215 undo = undo_ptr->insert_undo;
2216
2217 if (undo == nullptr) {
2218 /* Did not succeed */
2219 ut_ad(err != DB_SUCCESS);
2220 goto err_exit;
2221 }
2222 }
2223
2224 ut_ad(err == DB_SUCCESS);
2225 break;
2226 default:
2227 ut_ad(op_type == TRX_UNDO_MODIFY_OP);
2228
2229 undo = undo_ptr->update_undo;
2230
2231 if (undo == nullptr) {
2232 err = trx_undo_assign_undo(trx, undo_ptr, TRX_UNDO_UPDATE);
2233 undo = undo_ptr->update_undo;
2234
2235 if (undo == nullptr) {
2236 /* Did not succeed */
2237 ut_ad(err != DB_SUCCESS);
2238 goto err_exit;
2239 }
2240 }
2241
2242 ut_ad(err == DB_SUCCESS);
2243 break;
2244 }
2245
2246 page_no = undo->last_page_no;
2247 undo_block = buf_page_get_gen(page_id_t(undo->space, page_no),
2248 undo->page_size, RW_X_LATCH, undo->guess_block,
2249 Page_fetch::NORMAL, UT_LOCATION_HERE, &mtr);
2250
2251 buf_block_dbg_add_level(undo_block, SYNC_TRX_UNDO_PAGE);
2252
2253 do {
2254 page_t *undo_page;
2255 ulint offset;
2256
2257 undo_page = buf_block_get_frame(undo_block);
2258 ut_ad(page_no == undo_block->page.id.page_no());
2259
2260 switch (op_type) {
2261 case TRX_UNDO_INSERT_OP:
2262 offset = trx_undo_page_report_insert(undo_page, trx, index, clust_entry,
2263 &mtr);
2264 break;
2265 default:
2266 ut_ad(op_type == TRX_UNDO_MODIFY_OP);
2267 offset =
2268 trx_undo_page_report_modify(undo_page, trx, index, rec, offsets,
2269 update, cmpl_info, clust_entry, &mtr);
2270 }
2271
2272 if (UNIV_UNLIKELY(offset == 0)) {
2273 /* The record did not fit on the page. We erase the
2274 end segment of the undo log page and write a log
2275 record of it: this is to ensure that in the debug
2276 version the replicate page constructed using the log
2277 records stays identical to the original page */
2278
2279 if (!trx_undo_erase_page_end(undo_page, &mtr)) {
2280 /* The record did not fit on an empty
2281 undo page. Discard the freshly allocated
2282 page and return an error. */
2283
2284 /* When we remove a page from an undo
2285 log, this is analogous to a
2286 pessimistic insert in a B-tree, and we
2287 must reserve the counterpart of the
2288 tree latch, which is the rseg
2289 mutex. We must commit the mini-transaction
2290 first, because it may be holding lower-level
2291 latches, such as SYNC_FSP and SYNC_FSP_PAGE. */
2292
2293 mtr_commit(&mtr);
2294 mtr_start(&mtr);
2295
2296 if (index->table->is_temporary()) {
2297 mtr.set_log_mode(MTR_LOG_NO_REDO);
2298 }
2299
2300 undo_ptr->rseg->latch();
2301 trx_undo_free_last_page(trx, undo, &mtr);
2302 undo_ptr->rseg->unlatch();
2303
2304 err = DB_UNDO_RECORD_TOO_BIG;
2305 goto err_exit;
2306 }
2307
2308 mtr_commit(&mtr);
2309 } else {
2310 /* Success */
2311 undo->guess_block = undo_block;
2312 mtr_commit(&mtr);
2313
2314 undo->empty = false;
2315 undo->top_page_no = page_no;
2316 undo->top_offset = offset;
2317 undo->top_undo_no = trx->undo_no;
2318
2319 trx->undo_no++;
2320 trx->undo_rseg_space = undo_ptr->rseg->space_id;
2321
2322 mutex_exit(&trx->undo_mutex);
2323
2324 *roll_ptr =
2325 trx_undo_build_roll_ptr(op_type == TRX_UNDO_INSERT_OP,
2326 undo_ptr->rseg->space_id, page_no, offset);
2327 return (DB_SUCCESS);
2328 }
2329
2330 ut_ad(page_no == undo->last_page_no);
2331
2332 /* We have to extend the undo log by one page */
2333
2334 ut_ad(++loop_count < 2);
2335
2336 mtr_start(&mtr);
2337
2338 if (index->table->is_temporary()) {
2339 mtr.set_log_mode(MTR_LOG_NO_REDO);
2340 }
2341
2342 /* When we add a page to an undo log, this is analogous to
2343 a pessimistic insert in a B-tree, and we must reserve the
2344 counterpart of the tree latch, which is the rseg mutex. */
2345
2346 undo_ptr->rseg->latch();
2347 undo_block = trx_undo_add_page(trx, undo, undo_ptr, &mtr);
2348 undo_ptr->rseg->unlatch();
2349
2350 page_no = undo->last_page_no;
2351
2352 DBUG_EXECUTE_IF("ib_err_ins_undo_page_add_failure", undo_block = nullptr;);
2353 } while (undo_block != nullptr);
2354
2355 ib_errf(
2356 trx->mysql_thd, IB_LOG_LEVEL_ERROR, ER_INNODB_UNDO_LOG_FULL,
2357 "No more space left over in %s tablespace for allocating UNDO"
2358 " log pages. Please add new data file to the tablespace or"
2359 " check if filesystem is full or enable auto-extension for"
2360 " the tablespace",
2361 ((undo->space == TRX_SYS_SPACE)
2362 ? "system"
2363 : ((fsp_is_system_temporary(undo->space)) ? "temporary" : "undo")));
2364
2365 /* Did not succeed: out of space */
2366 err = DB_OUT_OF_FILE_SPACE;
2367
2368 err_exit:
2369 mutex_exit(&trx->undo_mutex);
2370 mtr_commit(&mtr);
2371 return (err);
2372 }
2373
2374 /*============== BUILDING PREVIOUS VERSION OF A RECORD ===============*/
2375
2376 /** Copies an undo record to heap. This function can be called if we know that
2377 the undo log record exists.
2378 @return own: copy of the record */
2379 [[nodiscard]] static trx_undo_rec_t *trx_undo_get_undo_rec_low(
2380 roll_ptr_t roll_ptr, /*!< in: roll pointer to record */
2381 mem_heap_t *heap, /*!< in: memory heap where copied */
2382 bool is_temp) /*!< in: true if temp undo rec. */
2383 {
2384 trx_undo_rec_t *undo_rec;
2385 ulint rseg_id;
2386 space_id_t space_id;
2387 page_no_t page_no;
2388 ulint offset;
2389 const page_t *undo_page;
2390 bool is_insert;
2391 mtr_t mtr;
2392
2393 trx_undo_decode_roll_ptr(roll_ptr, &is_insert, &rseg_id, &page_no, &offset);
2394 space_id = trx_rseg_id_to_space_id(rseg_id, is_temp);
2395
2396 bool found;
2397 const page_size_t &page_size = fil_space_get_page_size(space_id, &found);
2398 ut_ad(found);
2399
2400 mtr_start(&mtr);
2401
2402 undo_page = trx_undo_page_get_s_latched(page_id_t(space_id, page_no),
2403 page_size, &mtr);
2404
2405 undo_rec = trx_undo_rec_copy(undo_page, static_cast<uint32_t>(offset), heap);
2406
2407 mtr_commit(&mtr);
2408
2409 return (undo_rec);
2410 }
2411
2412 /** Copies an undo record to heap.
2413 @param[in] roll_ptr roll pointer to record
2414 @param[in] trx_id id of the trx that generated
2415 the roll pointer: it points to an
2416 undo log of this transaction
2417 @param[in] heap memory heap where copied
2418 @param[in] is_temp true if temporary, no-redo rseg.
2419 @param[in] name table name
2420 @param[out] undo_rec own: copy of the record
2421 @retval true if the undo log has been
2422 truncated and we cannot fetch the old version
2423 @retval false if the undo log record is available
2424 NOTE: the caller must have latches on the clustered index page. */
2425 [[nodiscard]] static bool trx_undo_get_undo_rec(roll_ptr_t roll_ptr,
2426 trx_id_t trx_id,
2427 mem_heap_t *heap, bool is_temp,
2428 const table_name_t &name,
2429 trx_undo_rec_t **undo_rec) {
2430 bool missing_history;
2431
2432 rw_lock_s_lock(&purge_sys->latch, UT_LOCATION_HERE);
2433
2434 missing_history = purge_sys->view.changes_visible(trx_id, name);
2435 if (!missing_history) {
2436 *undo_rec = trx_undo_get_undo_rec_low(roll_ptr, heap, is_temp);
2437 }
2438
2439 rw_lock_s_unlock(&purge_sys->latch);
2440
2441 return (missing_history);
2442 }
2443
2444 #ifdef UNIV_DEBUG
2445 #define ATTRIB_USED_ONLY_IN_DEBUG
2446 #else /* UNIV_DEBUG */
2447 #define ATTRIB_USED_ONLY_IN_DEBUG [[maybe_unused]]
2448 #endif /* UNIV_DEBUG */
2449
2450 bool trx_undo_prev_version_build(
2451 const rec_t *index_rec ATTRIB_USED_ONLY_IN_DEBUG,
2452 mtr_t *index_mtr ATTRIB_USED_ONLY_IN_DEBUG, const rec_t *rec,
2453 const dict_index_t *const index, ulint *offsets, mem_heap_t *heap,
2454 rec_t **old_vers, mem_heap_t *v_heap, const dtuple_t **vrow, ulint v_status,
2455 lob::undo_vers_t *lob_undo) {
2456 DBUG_TRACE;
2457
2458 trx_undo_rec_t *undo_rec = nullptr;
2459 dtuple_t *entry;
2460 trx_id_t rec_trx_id;
2461 ulint type;
2462 undo_no_t undo_no;
2463 table_id_t table_id;
2464 trx_id_t trx_id;
2465 roll_ptr_t roll_ptr;
2466 upd_t *update = nullptr;
2467 byte *ptr;
2468 ulint info_bits;
2469 ulint cmpl_info;
2470 bool dummy_extern;
2471 byte *buf;
2472
2473 ut_ad(!rw_lock_own(&purge_sys->latch, RW_LOCK_S));
2474 ut_ad(mtr_memo_contains_page(index_mtr, index_rec, MTR_MEMO_PAGE_S_FIX) ||
2475 mtr_memo_contains_page(index_mtr, index_rec, MTR_MEMO_PAGE_X_FIX));
2476 ut_ad(rec_offs_validate(rec, index, offsets));
2477 ut_a(index->is_clustered());
2478
2479 roll_ptr = row_get_rec_roll_ptr(rec, index, offsets);
2480
2481 *old_vers = nullptr;
2482
2483 if (trx_undo_roll_ptr_is_insert(roll_ptr)) {
2484 /* The record rec is the first inserted version */
2485 return true;
2486 }
2487
2488 rec_trx_id = row_get_rec_trx_id(rec, index, offsets);
2489
2490 /* REDO rollback segments are used only for non-temporary objects.
2491 For temporary objects NON-REDO rollback segments are used. */
2492 bool is_temp = index->table->is_temporary();
2493
2494 ut_ad(!index->table->skip_alter_undo);
2495
2496 if (trx_undo_get_undo_rec(roll_ptr, rec_trx_id, heap, is_temp,
2497 index->table->name, &undo_rec)) {
2498 if (v_status & TRX_UNDO_PREV_IN_PURGE) {
2499 /* We are fetching the record being purged */
2500 undo_rec = trx_undo_get_undo_rec_low(roll_ptr, heap, is_temp);
2501 } else {
2502 /* The undo record may already have been purged,
2503 during purge or semi-consistent read. */
2504 return false;
2505 }
2506 }
2507
2508 type_cmpl_t type_cmpl;
2509 ptr = trx_undo_rec_get_pars(undo_rec, &type, &cmpl_info, &dummy_extern,
2510 &undo_no, &table_id, type_cmpl);
2511
2512 if (table_id != index->table->id) {
2513 /* The table should have been rebuilt, but purge has
2514 not yet removed the undo log records for the
2515 now-dropped old table (table_id). */
2516 return true;
2517 }
2518
2519 ptr = trx_undo_update_rec_get_sys_cols(ptr, &trx_id, &roll_ptr, &info_bits);
2520
2521 /* (a) If a clustered index record version is such that the
2522 trx id stamp in it is bigger than purge_sys->view, then the
2523 BLOBs in that version are known to exist (the purge has not
2524 progressed that far);
2525
2526 (b) if the version is the first version such that trx id in it
2527 is less than purge_sys->view, and it is not delete-marked,
2528 then the BLOBs in that version are known to exist (the purge
2529 cannot have purged the BLOBs referenced by that version
2530 yet).
2531
2532 This function does not fetch any BLOBs. The callers might, by
2533 possibly invoking row_ext_create() via row_build(). However,
2534 they should have all needed information in the *old_vers
2535 returned by this function. This is because *old_vers is based
2536 on the transaction undo log records. The function
2537 trx_undo_page_fetch_ext() will write BLOB prefixes to the
2538 transaction undo log that are at least as long as the longest
2539 possible column prefix in a secondary index. Thus, secondary
2540 index entries for *old_vers can be constructed without
2541 dereferencing any BLOB pointers. */
2542
2543 ptr = trx_undo_rec_skip_row_ref(ptr, index);
2544
2545 ptr = trx_undo_update_rec_get_update(ptr, index, type, trx_id, roll_ptr,
2546 info_bits, heap, &update, lob_undo,
2547 type_cmpl);
2548 ut_a(ptr);
2549
2550 if (row_upd_changes_field_size_or_external(index, offsets, update)) {
2551 /* We should confirm the existence of disowned external data,
2552 if the previous version record is delete marked. If the trx_id
2553 of the previous record is seen by purge view, we should treat
2554 it as missing history, because the disowned external data
2555 might be purged already.
2556
2557 The inherited external data (BLOBs) can be freed (purged)
2558 after trx_id was committed, provided that no view was started
2559 before trx_id. If the purge view can see the committed
2560 delete-marked record by trx_id, no transactions need to access
2561 the BLOB. */
2562
2563 /* the row_upd_changes_disowned_external(update) call could be
2564 omitted, but the synchronization on purge_sys->latch is likely
2565 more expensive. */
2566
2567 if ((update->info_bits & REC_INFO_DELETED_FLAG) &&
2568 row_upd_changes_disowned_external(update)) {
2569 bool missing_extern;
2570
2571 rw_lock_s_lock(&purge_sys->latch, UT_LOCATION_HERE);
2572
2573 missing_extern =
2574 purge_sys->view.changes_visible(trx_id, index->table->name);
2575
2576 rw_lock_s_unlock(&purge_sys->latch);
2577
2578 if (missing_extern) {
2579 /* treat as a fresh insert, not to
2580 cause assertion error at the caller. */
2581 if (update != nullptr) {
2582 update->reset();
2583 }
2584 return true;
2585 }
2586 }
2587
2588 /* We have to set the appropriate extern storage bits in the
2589 old version of the record: the extern bits in rec for those
2590 fields that update does NOT update, as well as the bits for
2591 those fields that update updates to become externally stored
2592 fields. Store the info: */
2593
2594 entry = row_rec_to_index_entry(rec, index, offsets, heap);
2595 /* The page containing the clustered index record
2596 corresponding to entry is latched in mtr. Thus the
2597 following call is safe. */
2598 row_upd_index_replace_new_col_vals(entry, index, update, heap);
2599
2600 buf = static_cast<byte *>(
2601 mem_heap_alloc(heap, rec_get_converted_size(index, entry)));
2602
2603 *old_vers = rec_convert_dtuple_to_rec(buf, index, entry);
2604 } else {
2605 buf = static_cast<byte *>(mem_heap_alloc(heap, rec_offs_size(offsets)));
2606
2607 *old_vers = rec_copy(buf, rec, offsets);
2608 rec_offs_make_valid(*old_vers, index, offsets);
2609 row_upd_rec_in_place(*old_vers, index, offsets, update, nullptr);
2610 }
2611
2612 /* Set the old value (which is the after image of an update) in the
2613 update vector to dtuple vrow */
2614 if (v_status & TRX_UNDO_GET_OLD_V_VALUE) {
2615 row_upd_replace_vcol((dtuple_t *)*vrow, index->table, update, false,
2616 nullptr, nullptr);
2617 }
2618
2619 #if defined UNIV_DEBUG || defined UNIV_BLOB_LIGHT_DEBUG
2620 ut_a(!rec_offs_any_null_extern(
2621 index, *old_vers,
2622 rec_get_offsets(*old_vers, index, nullptr, ULINT_UNDEFINED,
2623 UT_LOCATION_HERE, &heap)));
2624 #endif // defined UNIV_DEBUG || defined UNIV_BLOB_LIGHT_DEBUG
2625
2626 /* If vrow is not NULL it means that the caller is interested in the values of
2627 the virtual columns for this version.
2628 If the UPD_NODE_NO_ORD_CHANGE flag is set on cmpl_info, it means that the
2629 change which created this entry in undo log did not affect any column of any
2630 secondary index (in particular: virtual), and thus the values of virtual
2631 columns were not recorded in undo. In such case the caller may assume that the
2632 values of (virtual) columns present in secondary index are exactly the same as
2633 they are in the next (more recent) version.
2634 If on the other hand the UPD_NODE_NO_ORD_CHANGE flag is not set, then we will
2635 make sure that *vrow points to a properly allocated memory and contains the
2636 values of virtual columns for this version recovered from undo log.
2637 This implies that if the caller has provided a non-NULL vrow, and the *vrow is
2638 still NULL after the call, (and old_vers is not NULL) it must be because the
2639 UPD_NODE_NO_ORD_CHANGE flag was set for this version.
2640 This last statement is an important assumption made by the
2641 row_vers_impl_x_locked_low() function. */
2642 if (vrow && !(cmpl_info & UPD_NODE_NO_ORD_CHANGE)) {
2643 if (!(*vrow)) {
2644 *vrow = dtuple_create_with_vcol(v_heap ? v_heap : heap,
2645 index->table->get_n_cols(),
2646 dict_table_get_n_v_cols(index->table));
2647 dtuple_init_v_fld(*vrow);
2648 }
2649
2650 ut_ad(index->table->n_v_cols);
2651 trx_undo_read_v_cols(index->table, ptr, *vrow,
2652 v_status & TRX_UNDO_PREV_IN_PURGE, false, nullptr,
2653 (v_heap != nullptr ? v_heap : heap));
2654 }
2655
2656 if (update != nullptr) {
2657 update->reset();
2658 }
2659
2660 return true;
2661 }
2662
2663 /** Read virtual column value from undo log
2664 @param[in] table the table
2665 @param[in] ptr undo log pointer
2666 @param[in,out] row the dtuple to fill
2667 @param[in] in_purge called by purge thread
2668 @param[in] online true if this is from online DDL log
2669 @param[in] col_map online rebuild column map
2670 @param[in,out] heap memory heap to keep value when necessary */
2671 void trx_undo_read_v_cols(const dict_table_t *table, const byte *ptr,
2672 const dtuple_t *row, bool in_purge, bool online,
2673 const ulint *col_map, mem_heap_t *heap) {
2674 const byte *end_ptr;
2675 bool first_v_col = true;
2676 bool is_undo_log = true;
2677
2678 end_ptr = ptr + mach_read_from_2(ptr);
2679 ptr += 2;
2680 while (ptr < end_ptr) {
2681 dfield_t *dfield;
2682 dfield_t multi_value_field;
2683 const byte *field = nullptr;
2684 ulint field_no;
2685 ulint len = 0;
2686 ulint orig_len = 0;
2687 bool is_virtual;
2688 dict_v_col_t *vcol = nullptr;
2689 ulint col_no;
2690
2691 field_no = mach_read_next_compressed(const_cast<const byte **>(&ptr));
2692
2693 is_virtual = (field_no >= REC_MAX_N_FIELDS);
2694
2695 if (is_virtual) {
2696 ptr =
2697 trx_undo_read_v_idx(table, ptr, first_v_col, &is_undo_log, &field_no);
2698 first_v_col = false;
2699 }
2700
2701 if (!is_virtual || field_no == ULINT_UNDEFINED) {
2702 /* The virtual column is no longer indexed or does not exist.
2703 "continue" needs to put after ptr gets advanced */
2704 if (trx_undo_rec_is_multi_value(ptr)) {
2705 ptr = trx_undo_rec_get_multi_value(ptr, nullptr, heap);
2706 } else {
2707 ptr = trx_undo_rec_get_col_val(ptr, &field, &len, &orig_len);
2708 }
2709 continue;
2710 }
2711
2712 vcol = dict_table_get_nth_v_col(table, field_no);
2713
2714 if (!col_map) {
2715 col_no = vcol->v_pos;
2716 } else {
2717 col_no = col_map[vcol->v_pos];
2718 }
2719
2720 if (col_no == ULINT_UNDEFINED) {
2721 if (trx_undo_rec_is_multi_value(ptr)) {
2722 ptr = trx_undo_rec_get_multi_value(ptr, nullptr, heap);
2723 } else {
2724 ptr = trx_undo_rec_get_col_val(ptr, &field, &len, &orig_len);
2725 }
2726 continue;
2727 }
2728
2729 dfield = dtuple_get_nth_v_field(row, col_no);
2730
2731 if (trx_undo_rec_is_multi_value(ptr)) {
2732 ut_ad(vcol->m_col.is_multi_value());
2733 ptr = trx_undo_rec_get_multi_value(ptr, &multi_value_field, heap);
2734 } else {
2735 ut_ad(!vcol->m_col.is_multi_value());
2736 ptr = trx_undo_rec_get_col_val(ptr, &field, &len, &orig_len);
2737 }
2738
2739 if (!in_purge || dfield_get_type(dfield)->mtype == DATA_MISSING) {
2740 vcol->m_col.copy_type(dfield_get_type(dfield));
2741 if (online && !vcol->m_col.is_multi_value()) {
2742 dfield->adjust_v_data_mysql(vcol, dict_table_is_comp(table), field, len,
2743 heap);
2744 } else if (!vcol->m_col.is_multi_value()) {
2745 dfield_set_data(dfield, field, len);
2746 } else {
2747 dfield_copy_data(dfield, &multi_value_field);
2748 }
2749 }
2750 }
2751
2752 ut_ad(ptr == end_ptr);
2753 }
2754 #endif /* !UNIV_HOTBACKUP */