Debian Codegraph @

storage/innobase/row/row0vers.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1997, 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/row0vers.cc
29 Row versions
30
31 Created 2/6/1997 Heikki Tuuri
32 *******************************************************/
33
34 #include <stddef.h>
35
36 #include "btr0btr.h"
37 #include "current_thd.h"
38 #include "dict0boot.h"
39 #include "dict0dict.h"
40 #include "ha_prototypes.h"
41 #include "lock0lock.h"
42 #include "mach0data.h"
43 #include "que0que.h"
44 #include "read0read.h"
45 #include "rem0cmp.h"
46 #include "row0ext.h"
47 #include "row0mysql.h"
48 #include "row0row.h"
49 #include "row0upd.h"
50 #include "row0vers.h"
51 #include "trx0purge.h"
52 #include "trx0rec.h"
53 #include "trx0roll.h"
54 #include "trx0rseg.h"
55 #include "trx0trx.h"
56 #include "trx0undo.h"
57
58 #include "my_dbug.h"
59
60 /** Check whether all non-virtual columns in a index entries match
61 @param[in] index the secondary index
62 @param[in] ientry1 first index entry to compare
63 @param[in] ientry2 second index entry to compare
64 @param[in,out] n_non_v_col number of non-virtual columns
65 in the index
66 @return true if all matches, false otherwise */
67 static bool row_vers_non_vc_index_entry_match(dict_index_t *index,
68 const dtuple_t *ientry1,
69 const dtuple_t *ientry2,
70 ulint *n_non_v_col);
71
72 /** Checks if a particular version of a record from clustered index matches
73 the secondary index record. The match occurs if and only if two condition hold:
74 1) the clust_rec exists and is not delete marked
75 2) the values in columns in clust_rec match those in sec_rec
76 Please note that the delete marker on sec_rec does not play any role in this
77 definition!
78 @param[in] clust_index the clustered index
79 @param[in] clust_rec the clustered index record, can be null or
80 delete marked
81 @param[in] clust_vrow the values of virtual columns, can be NULL if
82 the clust_rec was stored in undo log by
83 operation that did not change any secondary
84 index column (and was not a DELETE operation)
85 @param[in] clust_offsets the offsets for clust_rec,
86 rec_get_offsets(clust_rec, clust_index)
87 @param[in] sec_index the secondary index
88 @param[in] sec_rec the secondary index record
89 @param[in] sec_offsets the offsets for secondary index record,
90 rec_get_offsets(sec_rec, sec_index)
91 @param[in] comp the compression flag for both the clustered and
92 the secondary index, as both are assumed equal
93 @param[in] looking_for_match are we looking for match?
94 false means that we are looking for non-match
95 @param[in] heap the heap to be used for all allocations
96 @return true iff the clust_rec matches sec_rec
97 */
98 static bool row_clust_vers_matches_sec(
99 const dict_index_t *const clust_index, const rec_t *const clust_rec,
100 const dtuple_t *const clust_vrow, const ulint *const clust_offsets,
101 const dict_index_t *const sec_index, const rec_t *const sec_rec,
102 const ulint *const sec_offsets, const bool comp,
103 const bool looking_for_match, mem_heap_t *const heap) {
104 /** If we could not find a clust_rec version, it means it either never existed
105 or was garbage collected, in either case we can interpret it as the row not
106 being present at that point in time. Similarly, if it is delete marked.
107 In all this cases, we report that there is no match. */
108 if (!clust_rec || rec_get_deleted_flag(clust_rec, comp)) {
109 return false;
110 }
111
112 /** If the index involves virtual columns, then we can relay on the assumption
113 that `trx_undo_prev_version_build` will try to retrieve clust_vrow, and the
114 only reason it can not retrieve it is because there was no change to any of
115 the indexed columns. In particular this should mean, that the answer to the
116 question "does this clust_rec match sec_row?" did not change, and we can
117 return the same value as before, which was !looking_for_match. We know it was
118 not looking_for_match because in such case the loop would stop.
119
120 There are some difficulties we should take into consideration here:
121
122 1. It could be the case that there was no "previous iteration".
123 Indeed, it can happen, that this is the first call.
124 If we got to this line, it means that there are at least two versions of the
125 clustered index row: the most recent, which we don't see, and the one passed
126 here as `clust_rec`, which we know has all the important columns equal to the
127 most recent one.
128 Moreover, we know that `clust_rec` is not delete marked.
129 We also know, that the most recent version is also not delete marked,
130 because, if it was delete marked, then the most recent change would be a
131 DELETE operation, and in such cases we always undo log the values of columns,
132 yet `clust_vrow` is null.
133 So, the most recent version, and the version just before it, not only have
134 the same values of indexed columns, but also the same delete mark.
135 If so, then it is impossible, that this particular change created, or
136 removed a secondary index entry.
137 Therefore we need to continue the loop, and to do so we have to return the
138 opposite of what the loop is searching for, thus !looking_for_match.
139
140 Here's a bit different argument, perhaps more persuasive in case we want to
141 prove that the returned value correctly answers the question "does clust_rec
142 match the sec_rec?".
143 Consider two cases, depending on sec_rec delete mark:
144
145 A) sec_rec is delete marked
146 In this case, looking_for_match is set to true, thus we are about to return
147 false. So, our claim is that clust_rec does not match sec_rec.
148 For consider for a moment the opposite, that clust_rec does match sec_rec -
149 it would follow, that also the most recent version matches sec_rec, as it has
150 the same values of columns, and delete mark. But then, we have that two most
151 recent versions of the clustered index record are not delete marked and match
152 the secondary index record, yet for some reason the change was not
153 synchronized to the secondary index, which is still delete marked!
154 This contradicts the assumption that at most one most recent change is not
155 synchronized to the secondary index.
156
157 B) sec_rec is not delete marked
158 In this case, looking_for_match is set to false, thus we are about to return
159 true. So, our claim is that clust_rec does match sec_rec.
160 For consider for a moment the opposite, that clust_rec doesn't match sec_rec -
161 it would follow, that also the most recent version doesn't match sec_rec, as
162 it has the same values of columns, and delete mark. But then, we have that two
163 most recent versions of the clustered index record do not match the secondary
164 index record, yet for some reason the change was not synchronized to the
165 secondary index, which is still not delete marked!
166 This contradicts the assumption that at most one most recent change is not
167 synchronized to the secondary index.
168
169 2. It could (hypothetically) be the case that in previous iteration the answer
170 was different, because the two versions differ in delete mark
171
172 Again, before getting here we've already established that `clust_rec` is not
173 delete marked, and if `clust_vrow` is missing, then it must mean that the
174 later version is also not delete marked, as otherwise we would have to log all
175 columns to the undo log */
176 if (dict_index_has_virtual(sec_index) && !clust_vrow) {
177 return !looking_for_match;
178 }
179
180 /** Reconstruct all the columns */
181 row_ext_t *ext;
182 dtuple_t *row =
183 row_build(ROW_COPY_POINTERS, clust_index, clust_rec, clust_offsets,
184 nullptr, nullptr, nullptr, &ext, heap);
185 if (dict_index_has_virtual(sec_index)) {
186 ut_ad(row->n_v_fields == clust_vrow->n_v_fields);
187 dtuple_copy_v_fields(row, clust_vrow);
188 }
189 dtuple_t *entry = row_build_index_entry(row, ext, sec_index, heap);
190
191 /** If the reconstructed values do not match the secondary index then we know
192 we should report no match. We compare the strings in binary mode to make it
193 more robust, because a thread which has changed "a" to "A" should prevent
194 concurrent transactions from peeking into the new binary representation,
195 say via CONVERT(column_name, binary). */
196 dtuple_set_types_binary(entry, dtuple_get_n_fields(entry));
197 return (0 == cmp_dtuple_rec(entry, sec_rec, sec_index, sec_offsets));
198 }
199
200 /**
201 Loops through the history of clustered index record in the undo log, stopping
202 after the first version which was not created by the given active transaction,
203 and reports if it found a version which satisfies criterion specified by
204 looking_for_match. If looking_for_match is true, it searches for a version which
205 matches the secondary index record. Otherwise it searches for a version which
206 does not match.
207 @param[in] looking_for_match are we looking for match?
208 false means that we are looking for non-match
209 @param[in] clust_index the clustered index
210 @param[in] clust_rec the clustered index record, can be null or
211 delete marked
212 @param[in] clust_offsets the offsets for clust_rec,
213 rec_get_offsets(clust_rec, clust_index)
214 @param[in] sec_index the secondary index
215 @param[in] sec_rec the secondary index record
216 @param[in] sec_offsets the offsets for secondary index record,
217 rec_get_offsets(sec_rec, sec_index)
218 @param[in] comp the compression flag for both the clustered
219 and the secondary index, as both are assumed
220 equal
221 @param[in] trx_id the active transaction which created the most
222 recent version of clustered index record
223 @param[in] mtr the mtr inside which we are operating
224 @param[in,out] heap the heap to be used for all allocations. This
225 heap might get deallocated, and a newly
226 allocated one will be returned, along with its
227 ownership
228 @return true iff a version of the clust_rec which is in relation specified by
229 looking_for_match to the given sec_rec is found among versions created by trx_id
230 or the one version before them
231 */
232 static bool row_vers_find_matching(
233 bool looking_for_match, const dict_index_t *const clust_index,
234 const rec_t *const clust_rec, ulint *&clust_offsets,
235 const dict_index_t *const sec_index, const rec_t *const sec_rec,
236 const ulint *const sec_offsets, const bool comp, const trx_id_t trx_id,
237 mtr_t *const mtr, mem_heap_t *&heap) {
238 const rec_t *version = clust_rec;
239 trx_id_t version_trx_id = trx_id;
240
241 while (version_trx_id == trx_id) {
242 mem_heap_t *old_heap = heap;
243 const dtuple_t *clust_vrow = nullptr;
244 rec_t *prev_version = nullptr;
245
246 /* We keep the semaphore in mtr on the clust_rec page, so
247 that no other transaction can update it and get an
248 implicit x-lock on rec until mtr_commit(mtr). */
249
250 heap = mem_heap_create(1024, UT_LOCATION_HERE);
251
252 trx_undo_prev_version_build(
253 clust_rec, mtr, version, clust_index, clust_offsets, heap,
254 &prev_version, nullptr,
255 dict_index_has_virtual(sec_index) ? &clust_vrow : nullptr, 0, nullptr);
256
257 /* The oldest visible clustered index version must not be
258 delete-marked, because we never start a transaction by
259 inserting a delete-marked record. */
260 ut_ad(prev_version || !rec_get_deleted_flag(version, comp) ||
261 !trx_rw_is_active(trx_id, false));
262
263 /* Free version and clust_offsets. */
264 mem_heap_free(old_heap);
265
266 version = prev_version;
267
268 if (version == nullptr) {
269 version_trx_id = 0;
270 } else {
271 clust_offsets = rec_get_offsets(version, clust_index, nullptr,
272 ULINT_UNDEFINED, UT_LOCATION_HERE, &heap);
273 version_trx_id = row_get_rec_trx_id(version, clust_index, clust_offsets);
274 }
275
276 if (row_clust_vers_matches_sec(
277 clust_index, version, clust_vrow, clust_offsets, sec_index, sec_rec,
278 sec_offsets, comp, looking_for_match, heap) == looking_for_match) {
279 return true;
280 }
281 }
282
283 return false;
284 }
285
286 /** Finds out if an active transaction has inserted or modified a secondary
287 index record.
288 @param[in] clust_rec Clustered index record
289 @param[in] clust_index The clustered index
290 @param[in] sec_rec Secondary index record
291 @param[in] sec_index The secondary index
292 @param[in] sec_offsets Rec_get_offsets(sec_rec, sec_index)
293 @param[in,out] mtr Mini-transaction
294 @return 0 if committed, else the active transaction id;
295 NOTE that this function can return false positives but never false
296 negatives. The caller must confirm all positive results by calling checking if
297 the trx is still active.*/
298 static inline trx_t *row_vers_impl_x_locked_low(
299 const rec_t *const clust_rec, const dict_index_t *const clust_index,
300 const rec_t *const sec_rec, const dict_index_t *const sec_index,
301 const ulint *const sec_offsets, mtr_t *const mtr) {
302 trx_id_t trx_id;
303
304 ulint *clust_offsets;
305 mem_heap_t *heap;
306
307 /** Here's my best understanding of what this code is doing.
308
309 When we call this function we already have `sec_rec` - a row from secondary
310 index `sec_index`, which includes:
311 - obviously the values of columns mentioned in secondary index definition,
312 in particular materialized values of virtual columns
313 - primary key columns not mentioned explicitly in secondary index definition,
314 - information about row format (`comp`)
315 - information if the row is delete marked or not (`rec_del`: 32 or 0)
316
317 We assume that this `sec_rec` really is a record in the secondary index, as
318 opposed to some artificially "made up" sequence of bytes. Moreover we
319 assume that this secondary index row is currently latched (not to be confused
320 with "locked"), so that `sec_rec` is the most current state of this row.
321
322 Also, we assume, that rows in secondary index are either added, or removed,
323 (or delete marked, or delete un-marked) but never modified.
324 Moreover, we assume, that each of these secondary index operations is done
325 after the primary (clustered) index was modified, to reflect the new state of
326 affairs.
327
328 We assume that `clust_rec` is the current version of the clustered index
329 record to which the secondary record `sec_rec` points to.
330
331 To be more precise:
332
333 Let S[f] mean value of field f in the secondary index record S.
334 Let C[t][f] mean value of field f in version t of clustered record C, where we
335 use consecutive natural numbers to denote versions: t=0,1,...,current_version.
336
337 Note: secondary index is not versioned
338
339 Let S.deleted and C[t].deleted be delete markers of these records.
340
341 Definition 1.
342 We say that secondary index row S `points-to` a clustered index row C if and
343 only if:
344 S[pkey] = C[t][pkey] for each primary key column pkey (for any version t)
345
346 Note: it does not matter which version t we pick, as for our purposes primary
347 key fields may be thought as immutable (say, we emulate their modification by
348 combination of delete + insert).
349
350 Definition 2.
351 We say that secondary index row S `matches` a clustered index row C in
352 version t if and only if:
353 (S[f] = C[t][f] for each column f) and not (C[t].deleted)
354
355 Note: In the above definition f might be a virtual column.
356 Note: There might be multiple versions which a single S `matches`, for
357 example when a transaction modifies a row back and forth, or changes columns
358 which are not indexed by secondary index.
359 Note: The definition of `matches` does not depend on S.deleted
360
361 Definition 3.
362 We say that secondary index row S `corresponds-to` a clustered index row C in
363 version t if and only if:
364 (not(S.deleted) and (S `matches` C[t]))
365 or
366 (S.deleted and not (S `matches` C[t]))
367
368 In other words, S `corresponds-to` C[t] means that the state of secondary
369 index row S is synchronized with the state of the row in clustered index in
370 version t.
371
372 Assumption 1.
373 (S `corresponds-to` C[current_version]) or
374 (S `corresponds-to` C[current_version-1])
375 In other words, `sec_rec` `corresponds-to` either the most current_version of
376 the primary record it `points-to` (i.e. the changes in the clustered index
377 were synchronized to the secondary index), or the current_version-1 - (i.e.
378 the changes in the clustered index was not synchronized to the secondary index
379 yet). This belief is supported by reading the source code and observation that
380 to modify secondary index, one has to modify clustered index first, and
381 modifying clustered index and later secondary index requires holding (implicit
382 or explicit) lock on the clustered index record, so there is at most one
383 transaction operating on any given clustered index row, and thus at most one
384 change "unsynchronized" to secondary index yet.
385
386 An equivalent formulation of Assumption 1 in terms of `matches` is:
387 (not(S.deleted) =>
388 ((S `matches` C[current_version]) or (S `matches` C[current_version-1]))
389 ) and (
390 S.deleted =>
391 not((S `matches` C[current_version]) and (S `matches` C[current_version-1]))
392 )
393 So, a non-deleted S implies that one of the two most recent versions
394 `matches` it, and a deleted S, means that at least one of the two most recent
395 versions does not `match` it.
396
397 Definition 4.
398 We say that S `could-be-authored-by` a clustered index row C in
399 version t if and only if:
400 (S `corresponds-to` C[t]) and !(S `corresponds-to` C[t-1])
401
402 This can be equivalently expressed using `matches` relation as:
403 (not(S.deleted) and (S `matches` C[t]) and not(S `matches` C[t-1]))
404 or
405 (S.deleted and not(S `matches` C[t]) and (S `matches` C[t-1]))
406
407
408 Definition 5.
409 We say that secondary index row S `was-authored-by` a clustered index row C in
410 version t if and only if:
411 (S `could-be-authored-by` C[t]) and
412 (for each v > t. not(S `could-be-authored-by` C[v]))
413 So, t is the latest version in which S `could-be-authored-by` C[t].
414
415 Equivalently, one can define `was-authored-by` in terms of `matches`, by
416 identifying the most recent version t for which `matches` relation between
417 S and C[t] has changed in the right direction, that is,
418 in case S.deleted we search for the first change from (S `matches` C[t-1]) to
419 not(S `matches` C[t]), while in case of not(S.deleted) we search for the first
420 change from not(S `matches` C[t-1]) to (S `matches` C[t]).
421
422 We are now ready to explain precisely what the call to
423 row_vers_impl_x_locked_low(C=`clust_rec`,...,S=`sec_rec`,...)
424 tries to achieve.
425
426 Post-condition of row_vers_impl_x_locked_low:
427 =============================================
428
429 If there is t, such that S `was-authored-by` C[t], and C[t].trx_id is active
430 then the return value is C[t].trx_id.
431 Otherwise the return value is 0.
432
433 Explanation of the algorithm in row_vers_impl_x_locked_low:
434 ===========================================================
435
436 The implementation is tricky, as it tries hard to avoid ever looking at the
437 C[current_version], instead looking only at older versions.
438 (One reason for this effort, IMHO, is that virtual columns might be expensive
439 to materialize, and are not stored in clustered index at all. Another reason,
440 I guess, might be to have only one way of reading data - from undo log).
441 Take a moment to realize that this is wonderful that it's even possible, as
442 this is not apparent from the Def 5! After all it might well be the case that
443 the `t` we are looking for is equal to `current_version` in which case the
444 definition of `was-authored-by` used naively would require us to check if S
445 `matches` C[current_version], which in turn done naively forces us to
446 look at fields of C[current_version]!
447
448 So, how can we do that without ever looking at C[current_version] fields?
449
450 We start by reading C[current_version].trx_id, and this is the only piece of
451 information we read from current_version.
452 We store that in `trx_id` variable.
453
454 We check if `trx_id` is active.
455
456 If `trx_id` is not active, then we know that we can return 0.
457 Why? Because it is impossible for any other C[t].trx_id to be still active, if
458 the most recent trx to modify the record is already inactive.
459
460 From now on we assume that `trx_id` is active.
461
462 We observe that the definition of S `was-authored-by` C[t] requires
463 not(S `corresponds-to` C[t-1]).
464 So, one thing we can use to filter interesting versions, is to proceed through
465 most recent versions t=current_version, current_version-1, ...
466 until we find the first t, such that not(S `corresponds-to` C[t-1]).
467 Surprisingly this is the only condition we have to check! Why?
468 Observe, that it must also be the case that (S `corresponds-to` C[t]),
469 because we either have tested that explicitly in the previous step of the loop
470 or in case of first iteration, it follows from Assumption 1.
471 This means, that (S `could-be-authored-by` C[t]), and since the t is maximal,
472 we have (S `was-authored-by` C[t]).
473
474 Therefore our algorithm is to simply loop over versions t, as long as
475 C[t].trx_id = trx_id, and stop as soon as not(S `corresponds-to` C[t-1]) in
476 which case the answer is yes, or if we can't find such a version, the answer
477 is no.
478
479 The reality is however much more complicated, as it needs to deal with:
480 A) incomplete history of versions (we remove old undo log entries from tail)
481 B) missing information about virtual columns (we don't log values of virtual
482 columns to undo log if they had not changed)
483
484 I'll explain our approach to these two problems in comments at the place they
485 are handled.*/
486
487 DBUG_TRACE;
488
489 ut_ad(rec_offs_validate(sec_rec, sec_index, sec_offsets));
490
491 heap = mem_heap_create(1024, UT_LOCATION_HERE);
492
493 clust_offsets = rec_get_offsets(clust_rec, clust_index, nullptr,
494 ULINT_UNDEFINED, UT_LOCATION_HERE, &heap);
495
496 trx_id = row_get_rec_trx_id(clust_rec, clust_index, clust_offsets);
497
498 trx_t *trx = trx_rw_is_active(trx_id, true);
499
500 if (trx == nullptr) {
501 /* The transaction that modified or inserted clust_rec is no
502 longer active, or it is corrupt: no implicit lock on rec */
503 lock_check_trx_id_sanity(trx_id, clust_rec, clust_index, clust_offsets);
504 mem_heap_free(heap);
505 return nullptr;
506 }
507
508 auto comp = page_rec_is_comp(sec_rec);
509 ut_ad(sec_index->table == clust_index->table);
510 ut_ad(comp == dict_table_is_comp(sec_index->table));
511 ut_ad(!comp == !page_rec_is_comp(clust_rec));
512
513 bool looking_for_match = rec_get_deleted_flag(sec_rec, comp);
514
515 if (!row_vers_find_matching(looking_for_match, clust_index, clust_rec,
516 clust_offsets, sec_index, sec_rec, sec_offsets,
517 comp, trx_id, mtr, heap)) {
518 trx_release_reference(trx);
519 trx = nullptr;
520 }
521
522 DBUG_PRINT("info", ("Implicit lock is held by trx:" TRX_ID_FMT, trx_id));
523
524 mem_heap_free(heap);
525 return trx;
526 }
527
528 trx_t *row_vers_impl_x_locked(const rec_t *rec, const dict_index_t *index,
529 const ulint *offsets) {
530 mtr_t mtr;
531 trx_t *trx;
532 const rec_t *clust_rec;
533 dict_index_t *clust_index;
534
535 ut_ad(!locksys::owns_exclusive_global_latch());
536 ut_ad(!trx_sys_mutex_own());
537
538 mtr_start(&mtr);
539
540 /* Search for the clustered index record. The latch on the
541 page of clust_rec locks the top of the stack of versions. The
542 bottom of the version stack is not locked; oldest versions may
543 disappear by the fact that transactions may be committed and
544 collected by the purge. This is not a problem, because we are
545 only interested in active transactions. */
546
547 clust_rec =
548 row_get_clust_rec(BTR_SEARCH_LEAF, rec, index, &clust_index, &mtr);
549
550 if (!clust_rec) {
551 /* In a rare case it is possible that no clust rec is found
552 for a secondary index record: if in row0umod.cc
553 row_undo_mod_remove_clust_low() we have already removed the
554 clust rec, while purge is still cleaning and removing
555 secondary index records associated with earlier versions of
556 the clustered index record. In that case there cannot be
557 any implicit lock on the secondary index record, because
558 an active transaction which has modified the secondary index
559 record has also modified the clustered index record. And in
560 a rollback we always undo the modifications to secondary index
561 records before the clustered index record. */
562
563 trx = nullptr;
564 } else {
565 trx = row_vers_impl_x_locked_low(clust_rec, clust_index, rec, index,
566 offsets, &mtr);
567
568 ut_ad(trx == nullptr || trx_is_referenced(trx));
569 }
570
571 mtr_commit(&mtr);
572
573 return (trx);
574 }
575
576 /** Finds out if we must preserve a delete marked earlier version of a clustered
577 index record, because it is >= the purge view.
578 @param[in] trx_id Transaction id in the version
579 @param[in] name Table name
580 @param[in,out] mtr Mini-transaction holding the latch on the
581 clustered index record; it will also hold
582 the latch on purge_view
583 @return true if earlier version should be preserved */
584 bool row_vers_must_preserve_del_marked(trx_id_t trx_id,
585 const table_name_t &name, mtr_t *mtr) {
586 ut_ad(!rw_lock_own(&(purge_sys->latch), RW_LOCK_S));
587
588 mtr_s_lock(&purge_sys->latch, mtr, UT_LOCATION_HERE);
589
590 return (!purge_sys->view.changes_visible(trx_id, name));
591 }
592
593 /** Check whether all non-virtual columns in a index entries match
594 @param[in] index the secondary index
595 @param[in] ientry1 first index entry to compare
596 @param[in] ientry2 second index entry to compare
597 @param[in,out] n_non_v_col number of non-virtual columns
598 in the index
599 @return true if all matches, false otherwise */
600 static bool row_vers_non_vc_index_entry_match(dict_index_t *index,
601 const dtuple_t *ientry1,
602 const dtuple_t *ientry2,
603 ulint *n_non_v_col) {
604 ulint n_fields = dtuple_get_n_fields(ientry1);
605 ulint ret = true;
606
607 *n_non_v_col = 0;
608
609 ut_ad(n_fields == dtuple_get_n_fields(ientry2));
610
611 for (ulint i = 0; i < n_fields; i++) {
612 const dict_field_t *ind_field = index->get_field(i);
613
614 const dict_col_t *col = ind_field->col;
615
616 /* Only check non-virtual columns */
617 if (col->is_virtual()) {
618 continue;
619 }
620
621 if (ret) {
622 const dfield_t *field1 = dtuple_get_nth_field(ientry1, i);
623 const dfield_t *field2 = dtuple_get_nth_field(ientry2, i);
624
625 if (cmp_dfield_dfield(field1, field2, ind_field->is_ascending) != 0) {
626 ret = false;
627 }
628 }
629
630 (*n_non_v_col)++;
631 }
632
633 return (ret);
634 }
635
636 /** build virtual column value from current cluster index record data
637 @param[in,out] row the cluster index row in dtuple form
638 @param[in] clust_index clustered index
639 @param[in] index the secondary index
640 @param[in] heap heap used to build virtual dtuple */
641 static void row_vers_build_clust_v_col(dtuple_t *row, dict_index_t *clust_index,
642 dict_index_t *index, mem_heap_t *heap) {
643 mem_heap_t *local_heap = nullptr;
644 for (ulint i = 0; i < dict_index_get_n_fields(index); i++) {
645 const dict_field_t *ind_field = index->get_field(i);
646
647 if (ind_field->col->is_virtual()) {
648 const dict_v_col_t *col;
649
650 col = reinterpret_cast<const dict_v_col_t *>(ind_field->col);
651
652 innobase_get_computed_value(row, col, clust_index->table, &local_heap,
653 heap, current_thd, nullptr);
654 }
655 }
656
657 if (local_heap) {
658 mem_heap_free(local_heap);
659 }
660 }
661
662 /** Build latest virtual column data from undo log
663 @param[in] in_purge whether this is the purge thread
664 @param[in] rec clustered index record
665 @param[in] clust_index clustered index
666 @param[in,out] clust_offsets offsets on the clustered index record
667 @param[in] index the secondary index
668 @param[in] roll_ptr the rollback pointer for the purging record
669 @param[in] trx_id trx id for the purging record
670 @param[in,out] v_heap heap used to build vrow
671 @param[out] vrow dtuple holding the virtual rows
672 @param[in,out] mtr mtr holding the latch on rec */
673 static void row_vers_build_cur_vrow_low(
674 bool in_purge, const rec_t *rec, dict_index_t *clust_index,
675 ulint *clust_offsets, dict_index_t *index, roll_ptr_t roll_ptr,
676 trx_id_t trx_id, mem_heap_t *v_heap, const dtuple_t **vrow, mtr_t *mtr) {
677 const rec_t *version;
678 rec_t *prev_version;
679 mem_heap_t *heap = nullptr;
680 ulint num_v = dict_table_get_n_v_cols(index->table);
681 const dfield_t *field;
682 ulint i;
683 bool all_filled = false;
684
685 *vrow = dtuple_create_with_vcol(v_heap, 0, num_v);
686 dtuple_init_v_fld(*vrow);
687
688 for (i = 0; i < num_v; i++) {
689 dfield_get_type(dtuple_get_nth_v_field(*vrow, i))->mtype = DATA_MISSING;
690 }
691
692 version = rec;
693
694 /* If this is called by purge thread, set TRX_UNDO_PREV_IN_PURGE
695 bit to search the undo log until we hit the current undo log with
696 roll_ptr */
697 const ulint status = in_purge
698 ? TRX_UNDO_PREV_IN_PURGE | TRX_UNDO_GET_OLD_V_VALUE
699 : TRX_UNDO_GET_OLD_V_VALUE;
700
701 while (!all_filled) {
702 mem_heap_t *heap2 = heap;
703 heap = mem_heap_create(1024, UT_LOCATION_HERE);
704 roll_ptr_t cur_roll_ptr =
705 row_get_rec_roll_ptr(version, clust_index, clust_offsets);
706
707 trx_undo_prev_version_build(rec, mtr, version, clust_index, clust_offsets,
708 heap, &prev_version, nullptr, vrow, status,
709 nullptr);
710
711 if (heap2) {
712 mem_heap_free(heap2);
713 }
714
715 if (!prev_version) {
716 /* Versions end here */
717 break;
718 }
719
720 clust_offsets = rec_get_offsets(prev_version, clust_index, nullptr,
721 ULINT_UNDEFINED, UT_LOCATION_HERE, &heap);
722
723 ulint entry_len = dict_index_get_n_fields(index);
724
725 all_filled = true;
726
727 for (i = 0; i < entry_len; i++) {
728 const dict_field_t *ind_field = index->get_field(i);
729 const dict_col_t *col = ind_field->col;
730
731 if (!col->is_virtual()) {
732 continue;
733 }
734
735 const dict_v_col_t *v_col = reinterpret_cast<const dict_v_col_t *>(col);
736 field = dtuple_get_nth_v_field(*vrow, v_col->v_pos);
737
738 if (dfield_get_type(field)->mtype == DATA_MISSING) {
739 all_filled = false;
740 break;
741 }
742 }
743
744 trx_id_t rec_trx_id =
745 row_get_rec_trx_id(prev_version, clust_index, clust_offsets);
746
747 if (rec_trx_id < trx_id || roll_ptr == cur_roll_ptr) {
748 break;
749 }
750
751 version = prev_version;
752 }
753
754 mem_heap_free(heap);
755 }
756
757 /** Check a virtual column value index secondary virtual index matches
758 that of current cluster index record, which is recreated from information
759 stored in undo log
760 @param[in] in_purge called by purge thread
761 @param[in] rec record in the clustered index
762 @param[in] icentry the index entry built from a cluster row
763 @param[in] clust_index cluster index
764 @param[in] clust_offsets offsets on the cluster record
765 @param[in] index the secondary index
766 @param[in] ientry the secondary index entry
767 @param[in] roll_ptr the rollback pointer for the purging record
768 @param[in] trx_id trx id for the purging record
769 @param[in,out] v_heap heap used to build virtual dtuple
770 @param[in,out] vrow dtuple holding the virtual rows (if needed)
771 @param[in] mtr mtr holding the latch on rec
772 @return true if matches, false otherwise */
773 static bool row_vers_vc_matches_cluster(
774 bool in_purge, const rec_t *rec, const dtuple_t *icentry,
775 dict_index_t *clust_index, ulint *clust_offsets, dict_index_t *index,
776 const dtuple_t *ientry, roll_ptr_t roll_ptr, trx_id_t trx_id,
777 mem_heap_t *v_heap, const dtuple_t **vrow, mtr_t *mtr) {
778 const rec_t *version;
779 rec_t *prev_version;
780 mem_heap_t *heap2;
781 mem_heap_t *heap = nullptr;
782 mem_heap_t *tuple_heap;
783 ulint num_v = dict_table_get_n_v_cols(index->table);
784 bool compare[REC_MAX_N_FIELDS];
785 ulint n_fields = dtuple_get_n_fields(ientry);
786 ulint n_non_v_col = 0;
787 ulint n_cmp_v_col = 0;
788 const dfield_t *field1;
789 dfield_t *field2;
790 ulint i;
791
792 /* First compare non-virtual columns (primary keys) */
793 if (!row_vers_non_vc_index_entry_match(index, ientry, icentry,
794 &n_non_v_col)) {
795 return (false);
796 }
797
798 tuple_heap = mem_heap_create(1024, UT_LOCATION_HERE);
799
800 ut_ad(n_fields > n_non_v_col);
801
802 *vrow = dtuple_create_with_vcol(v_heap ? v_heap : tuple_heap, 0, num_v);
803 dtuple_init_v_fld(*vrow);
804
805 for (i = 0; i < num_v; i++) {
806 dfield_get_type(dtuple_get_nth_v_field(*vrow, i))->mtype = DATA_MISSING;
807 compare[i] = false;
808 }
809
810 version = rec;
811
812 /* If this is called by purge thread, set TRX_UNDO_PREV_IN_PURGE
813 bit to search the undo log until we hit the current undo log with
814 roll_ptr */
815 ulint status =
816 (in_purge ? TRX_UNDO_PREV_IN_PURGE : 0) | TRX_UNDO_GET_OLD_V_VALUE;
817
818 while (n_cmp_v_col < n_fields - n_non_v_col) {
819 heap2 = heap;
820 heap = mem_heap_create(1024, UT_LOCATION_HERE);
821 roll_ptr_t cur_roll_ptr =
822 row_get_rec_roll_ptr(version, clust_index, clust_offsets);
823
824 ut_ad(cur_roll_ptr != 0);
825 ut_ad(in_purge == (roll_ptr != 0));
826
827 trx_undo_prev_version_build(rec, mtr, version, clust_index, clust_offsets,
828 heap, &prev_version, nullptr, vrow, status,
829 nullptr);
830
831 if (heap2) {
832 mem_heap_free(heap2);
833 }
834
835 if (!prev_version) {
836 /* Versions end here */
837 goto func_exit;
838 }
839
840 clust_offsets = rec_get_offsets(prev_version, clust_index, nullptr,
841 ULINT_UNDEFINED, UT_LOCATION_HERE, &heap);
842
843 ulint entry_len = dict_index_get_n_fields(index);
844
845 for (i = 0; i < entry_len; i++) {
846 const dict_field_t *ind_field = index->get_field(i);
847 const dict_col_t *col = ind_field->col;
848 field1 = dtuple_get_nth_field(ientry, i);
849
850 if (!col->is_virtual()) {
851 continue;
852 }
853
854 const dict_v_col_t *v_col = reinterpret_cast<const dict_v_col_t *>(col);
855 field2 = dtuple_get_nth_v_field(*vrow, v_col->v_pos);
856
857 if ((dfield_get_type(field2)->mtype != DATA_MISSING) &&
858 (!compare[v_col->v_pos])) {
859 if (ind_field->prefix_len != 0 && !dfield_is_null(field2) &&
860 field2->len > ind_field->prefix_len) {
861 field2->len = ind_field->prefix_len;
862 }
863
864 /* For multi-byte character sets (like utf8mb4)
865 and index on prefix of varchar vcol, we log
866 prefix_len * mbmaxlen bytes but the actual
867 secondary index record size can be less than
868 that. For comparison, use actual length of
869 secondary index record */
870 ulint mbmax_len = DATA_MBMAXLEN(field2->type.mbminmaxlen);
871 if (ind_field->prefix_len != 0 && !dfield_is_null(field2) &&
872 mbmax_len > 1) {
873 field2->len = field1->len;
874 }
875
876 /* The index field mismatch */
877 if (v_heap ||
878 (dfield_is_multi_value(field2) &&
879 cmp_multi_value_dfield_dfield(field2, field1) != 0) ||
880 (!dfield_is_multi_value(field2) &&
881 cmp_dfield_dfield(field2, field1, ind_field->is_ascending) != 0)) {
882 if (v_heap) {
883 dtuple_dup_v_fld(*vrow, v_heap);
884 }
885
886 mem_heap_free(tuple_heap);
887 mem_heap_free(heap);
888 return (false);
889 }
890
891 compare[v_col->v_pos] = true;
892 n_cmp_v_col++;
893 }
894 }
895
896 trx_id_t rec_trx_id =
897 row_get_rec_trx_id(prev_version, clust_index, clust_offsets);
898
899 if (rec_trx_id < trx_id || roll_ptr == cur_roll_ptr) {
900 break;
901 }
902
903 version = prev_version;
904 }
905
906 func_exit:
907 if (n_cmp_v_col == 0) {
908 *vrow = nullptr;
909 }
910
911 mem_heap_free(tuple_heap);
912 mem_heap_free(heap);
913
914 /* FIXME: In the case of n_cmp_v_col is not the same as
915 n_fields - n_non_v_col, callback is needed to compare the rest
916 columns. At the timebeing, we will need to return true */
917 return (true);
918 }
919
920 /** Build a dtuple contains virtual column data for current cluster index
921 @param[in] in_purge called by purge thread
922 @param[in] rec cluster index rec
923 @param[in] clust_index cluster index
924 @param[in] clust_offsets cluster rec offset
925 @param[in] index secondary index
926 @param[in] roll_ptr roll_ptr for the purge record
927 @param[in] trx_id transaction ID on the purging record
928 @param[in,out] heap heap memory
929 @param[in,out] v_heap heap memory to keep virtual column dtuple
930 @param[in] mtr mtr holding the latch on rec
931 @return dtuple contains virtual column data */
932 static const dtuple_t *row_vers_build_cur_vrow(
933 bool in_purge, const rec_t *rec, dict_index_t *clust_index,
934 ulint **clust_offsets, dict_index_t *index, roll_ptr_t roll_ptr,
935 trx_id_t trx_id, mem_heap_t *heap, mem_heap_t *v_heap, mtr_t *mtr) {
936 const dtuple_t *cur_vrow = nullptr;
937
938 roll_ptr_t t_roll_ptr =
939 row_get_rec_roll_ptr(rec, clust_index, *clust_offsets);
940
941 /* if the row is newly inserted, then the virtual
942 columns need to be computed */
943 if (trx_undo_roll_ptr_is_insert(t_roll_ptr)) {
944 ut_ad(!rec_get_deleted_flag(rec, page_rec_is_comp(rec)));
945
946 /* This is a newly inserted record and cannot
947 be deleted, So the externally stored field
948 cannot be freed yet. */
949 dtuple_t *row =
950 row_build(ROW_COPY_POINTERS, clust_index, rec, *clust_offsets, nullptr,
951 nullptr, nullptr, nullptr, heap);
952
953 row_vers_build_clust_v_col(row, clust_index, index, heap);
954 cur_vrow = dtuple_copy(row, v_heap);
955 dtuple_dup_v_fld(cur_vrow, v_heap);
956 } else {
957 /* Try to fetch virtual column data from undo log */
958 row_vers_build_cur_vrow_low(in_purge, rec, clust_index, *clust_offsets,
959 index, roll_ptr, trx_id, v_heap, &cur_vrow,
960 mtr);
961 }
962
963 *clust_offsets = rec_get_offsets(rec, clust_index, nullptr, ULINT_UNDEFINED,
964 UT_LOCATION_HERE, &heap);
965 return (cur_vrow);
966 }
967
968 /** Finds out if a version of the record, where the version >= the current
969 purge view, should have ientry as its secondary index entry. We check
970 if there is any not delete marked version of the record where the trx
971 id >= purge view, and the secondary index entry and ientry are identified in
972 the alphabetical ordering; exactly in this case we return true.
973 @return true if earlier version should have */
974 bool row_vers_old_has_index_entry(
975 bool also_curr, /*!< in: true if also rec is included in the
976 versions to search; otherwise only versions
977 prior to it are searched */
978 const rec_t *rec, /*!< in: record in the clustered index; the
979 caller must have a latch on the page */
980 mtr_t *mtr, /*!< in: mtr holding the latch on rec; it will
981 also hold the latch on purge_view */
982 dict_index_t *index, /*!< in: the secondary index */
983 const dtuple_t *ientry, /*!< in: the secondary index entry */
984 roll_ptr_t roll_ptr, /*!< in: roll_ptr for the purge record */
985 trx_id_t trx_id) /*!< in: transaction ID on the purging record */
986 {
987 const rec_t *version;
988 rec_t *prev_version;
989 dict_index_t *clust_index;
990 ulint *clust_offsets;
991 mem_heap_t *heap;
992 mem_heap_t *heap2;
993 dtuple_t *row;
994 const dtuple_t *entry;
995 ulint comp;
996 const dtuple_t *vrow = nullptr;
997 mem_heap_t *v_heap = nullptr;
998 const dtuple_t *cur_vrow = nullptr;
999
1000 ut_ad(mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_X_FIX) ||
1001 mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_S_FIX));
1002 ut_ad(!rw_lock_own(&(purge_sys->latch), RW_LOCK_S));
1003
1004 clust_index = index->table->first_index();
1005
1006 comp = page_rec_is_comp(rec);
1007 ut_ad(!dict_table_is_comp(index->table) == !comp);
1008 heap = mem_heap_create(1024, UT_LOCATION_HERE);
1009 clust_offsets = rec_get_offsets(rec, clust_index, nullptr, ULINT_UNDEFINED,
1010 UT_LOCATION_HERE, &heap);
1011
1012 DBUG_EXECUTE_IF("ib_purge_virtual_index_crash", DBUG_SUICIDE(););
1013
1014 DBUG_EXECUTE_IF("ib_purge_virtual_index_crash", DBUG_SUICIDE(););
1015
1016 DBUG_EXECUTE_IF("ib_purge_virtual_index_crash", DBUG_SUICIDE(););
1017
1018 DBUG_EXECUTE_IF("ib_purge_virtual_index_crash", DBUG_SUICIDE(););
1019
1020 if (dict_index_has_virtual(index)) {
1021 v_heap = mem_heap_create(100, UT_LOCATION_HERE);
1022 }
1023
1024 if (also_curr && !rec_get_deleted_flag(rec, comp)) {
1025 row_ext_t *ext;
1026
1027 /* The top of the stack of versions is locked by the
1028 mtr holding a latch on the page containing the
1029 clustered index record. The bottom of the stack is
1030 locked by the fact that the purge_sys->view must
1031 'overtake' any read view of an active transaction.
1032 Thus, it is safe to fetch the prefixes for
1033 externally stored columns. */
1034 row = row_build(ROW_COPY_POINTERS, clust_index, rec, clust_offsets, nullptr,
1035 nullptr, nullptr, &ext, heap);
1036
1037 if (dict_index_has_virtual(index)) {
1038 #ifndef UNIV_DEBUG
1039 #define dbug_v_purge false
1040 #else /* UNIV_DEBUG */
1041 bool dbug_v_purge = false;
1042 #endif /* UNIV_DEBUG */
1043
1044 DBUG_EXECUTE_IF("ib_purge_virtual_index_callback", dbug_v_purge = true;);
1045
1046 roll_ptr_t t_roll_ptr =
1047 row_get_rec_roll_ptr(rec, clust_index, clust_offsets);
1048
1049 /* if the row is newly inserted, then the virtual
1050 columns need to be computed */
1051 if (trx_undo_roll_ptr_is_insert(t_roll_ptr) || dbug_v_purge) {
1052 #ifdef INNODB_DD_VC_SUPPORT
1053 row_vers_build_clust_v_col(row, clust_index, index, heap);
1054
1055 entry = row_build_index_entry(row, ext, index, heap);
1056 if (entry && dtuple_coll_eq(entry, ientry)) {
1057 mem_heap_free(heap);
1058
1059 if (v_heap) {
1060 mem_heap_free(v_heap);
1061 }
1062
1063 return true;
1064 }
1065 #else
1066 mem_heap_free(heap);
1067
1068 if (v_heap) {
1069 mem_heap_free(v_heap);
1070 }
1071
1072 return true;
1073 #endif /* INNODB_DD_VC_SUPPORT */
1074 } else {
1075 /* Build index entry out of row */
1076 entry = row_build_index_entry(row, ext, index, heap);
1077
1078 /* If entry == NULL, the record contains unset
1079 BLOB pointers. The record may be safely removed,
1080 see below for full explanation */
1081
1082 if (entry &&
1083 row_vers_vc_matches_cluster(also_curr, rec, entry, clust_index,
1084 clust_offsets, index, ientry, roll_ptr,
1085 trx_id, nullptr, &vrow, mtr)) {
1086 mem_heap_free(heap);
1087
1088 if (v_heap) {
1089 mem_heap_free(v_heap);
1090 }
1091
1092 return true;
1093 }
1094 }
1095 clust_offsets = rec_get_offsets(rec, clust_index, nullptr,
1096 ULINT_UNDEFINED, UT_LOCATION_HERE, &heap);
1097 } else {
1098 entry = row_build_index_entry(row, ext, index, heap);
1099
1100 /* If entry == NULL, the record contains unset BLOB
1101 pointers. This must be a freshly inserted record. If
1102 this is called from
1103 row_purge_remove_sec_if_poss_low(), the thread will
1104 hold latches on the clustered index and the secondary
1105 index. Because the insert works in three steps:
1106
1107 (1) insert the record to clustered index
1108 (2) store the BLOBs and update BLOB pointers
1109 (3) insert records to secondary indexes
1110
1111 the purge thread can safely ignore freshly inserted
1112 records and delete the secondary index record. The
1113 thread that inserted the new record will be inserting
1114 the secondary index records. */
1115
1116 /* NOTE that we cannot do the comparison as binary
1117 fields because the row is maybe being modified so that
1118 the clustered index record has already been updated to
1119 a different binary value in a char field, but the
1120 collation identifies the old and new value anyway! */
1121 if (entry && dtuple_coll_eq(entry, ientry)) {
1122 mem_heap_free(heap);
1123
1124 if (v_heap) {
1125 mem_heap_free(v_heap);
1126 }
1127 return true;
1128 }
1129 }
1130 } else if (dict_index_has_virtual(index)) {
1131 /* The current cluster index record could be
1132 deleted, but the previous version of it might not. We will
1133 need to get the virtual column data from undo record
1134 associated with current cluster index */
1135 cur_vrow =
1136 row_vers_build_cur_vrow(also_curr, rec, clust_index, &clust_offsets,
1137 index, roll_ptr, trx_id, heap, v_heap, mtr);
1138 }
1139
1140 version = rec;
1141
1142 for (;;) {
1143 heap2 = heap;
1144 heap = mem_heap_create(1024, UT_LOCATION_HERE);
1145 vrow = nullptr;
1146
1147 trx_undo_prev_version_build(
1148 rec, mtr, version, clust_index, clust_offsets, heap, &prev_version,
1149 nullptr, dict_index_has_virtual(index) ? &vrow : nullptr, 0, nullptr);
1150 mem_heap_free(heap2); /* free version and clust_offsets */
1151
1152 if (!prev_version) {
1153 /* Versions end here */
1154
1155 mem_heap_free(heap);
1156
1157 if (v_heap) {
1158 mem_heap_free(v_heap);
1159 }
1160
1161 return false;
1162 }
1163
1164 clust_offsets = rec_get_offsets(prev_version, clust_index, nullptr,
1165 ULINT_UNDEFINED, UT_LOCATION_HERE, &heap);
1166
1167 if (dict_index_has_virtual(index)) {
1168 if (vrow) {
1169 /* Keep the virtual row info for the next
1170 version, unless it is changed */
1171 mem_heap_empty(v_heap);
1172 cur_vrow = dtuple_copy(vrow, v_heap);
1173 dtuple_dup_v_fld(cur_vrow, v_heap);
1174 }
1175
1176 if (!cur_vrow) {
1177 /* Nothing for this index has changed,
1178 continue */
1179 version = prev_version;
1180 continue;
1181 }
1182 }
1183
1184 if (!rec_get_deleted_flag(prev_version, comp)) {
1185 row_ext_t *ext;
1186
1187 /* The stack of versions is locked by mtr.
1188 Thus, it is safe to fetch the prefixes for
1189 externally stored columns. */
1190 row = row_build(ROW_COPY_POINTERS, clust_index, prev_version,
1191 clust_offsets, nullptr, nullptr, nullptr, &ext, heap);
1192
1193 if (dict_index_has_virtual(index)) {
1194 ut_ad(cur_vrow);
1195 ut_ad(row->n_v_fields == cur_vrow->n_v_fields);
1196 dtuple_copy_v_fields(row, cur_vrow);
1197 }
1198
1199 entry = row_build_index_entry(row, ext, index, heap);
1200
1201 /* If entry == NULL, the record contains unset
1202 BLOB pointers. This must be a freshly
1203 inserted record that we can safely ignore.
1204 For the justification, see the comments after
1205 the previous row_build_index_entry() call. */
1206
1207 /* NOTE that we cannot do the comparison as binary
1208 fields because maybe the secondary index record has
1209 already been updated to a different binary value in
1210 a char field, but the collation identifies the old
1211 and new value anyway! */
1212
1213 if (entry && dtuple_coll_eq(entry, ientry)) {
1214 mem_heap_free(heap);
1215 if (v_heap) {
1216 mem_heap_free(v_heap);
1217 }
1218
1219 return true;
1220 }
1221 }
1222
1223 version = prev_version;
1224 }
1225 }
1226
1227 /** Constructs the version of a clustered index record which a consistent
1228 read should see. We assume that the trx id stored in rec is such that
1229 the consistent read should not see rec in its present version.
1230 @param[in] rec record in a clustered index; the caller must have a latch
1231 on the page; this latch locks the top of the stack of
1232 versions of this records
1233 @param[in] mtr mtr holding the latch on rec; it will also hold the latch
1234 on purge_view
1235 @param[in] index the clustered index
1236 @param[in] offsets offsets returned by rec_get_offsets(rec, index)
1237 @param[in] view the consistent read view
1238 @param[in,out] offset_heap memory heap from which the offsets are
1239 allocated
1240 @param[in] in_heap memory heap from which the memory for *old_vers is
1241 allocated; memory for possible intermediate versions
1242 is allocated and freed locally within the function
1243 @param[out] old_vers old version, or NULL if the history is missing or
1244 the record does not exist in the view, that is, it
1245 was freshly inserted afterwards.
1246 @param[out] vrow reports virtual column info if any
1247 @param[in] lob_undo undo log to be applied to blobs.
1248 @return DB_SUCCESS or DB_MISSING_HISTORY */
1249 dberr_t row_vers_build_for_consistent_read(
1250 const rec_t *rec, mtr_t *mtr, dict_index_t *index, ulint **offsets,
1251 ReadView *view, mem_heap_t **offset_heap, mem_heap_t *in_heap,
1252 rec_t **old_vers, const dtuple_t **vrow, lob::undo_vers_t *lob_undo) {
1253 DBUG_TRACE;
1254 const rec_t *version;
1255 rec_t *prev_version;
1256 trx_id_t trx_id;
1257 mem_heap_t *heap = nullptr;
1258 byte *buf;
1259 dberr_t err;
1260
1261 ut_ad(index->is_clustered());
1262 ut_ad(mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_X_FIX) ||
1263 mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_S_FIX));
1264 ut_ad(!rw_lock_own(&(purge_sys->latch), RW_LOCK_S));
1265
1266 ut_ad(rec_offs_validate(rec, index, *offsets));
1267
1268 trx_id = row_get_rec_trx_id(rec, index, *offsets);
1269
1270 /* Reset the collected LOB undo information. */
1271 if (lob_undo != nullptr) {
1272 lob_undo->reset();
1273 }
1274
1275 ut_ad(!view->changes_visible(trx_id, index->table->name));
1276
1277 ut_ad(!vrow || !(*vrow));
1278
1279 version = rec;
1280
1281 for (;;) {
1282 mem_heap_t *prev_heap = heap;
1283
1284 heap = mem_heap_create(1024, UT_LOCATION_HERE);
1285
1286 if (vrow) {
1287 *vrow = nullptr;
1288 }
1289
1290 /* If purge can't see the record then we can't rely on
1291 the UNDO log record. */
1292
1293 bool purge_sees =
1294 trx_undo_prev_version_build(rec, mtr, version, index, *offsets, heap,
1295 &prev_version, nullptr, vrow, 0, lob_undo);
1296
1297 err = (purge_sees) ? DB_SUCCESS : DB_MISSING_HISTORY;
1298
1299 if (prev_heap != nullptr) {
1300 mem_heap_free(prev_heap);
1301 }
1302
1303 if (prev_version == nullptr) {
1304 /* It was a freshly inserted version */
1305 *old_vers = nullptr;
1306 ut_ad(!vrow || !(*vrow));
1307 break;
1308 }
1309
1310 *offsets = rec_get_offsets(prev_version, index, *offsets, ULINT_UNDEFINED,
1311 UT_LOCATION_HERE, offset_heap);
1312
1313 #if defined UNIV_DEBUG || defined UNIV_BLOB_LIGHT_DEBUG
1314 ut_a(!rec_offs_any_null_extern(index, prev_version, *offsets));
1315 #endif /* UNIV_DEBUG || UNIV_BLOB_LIGHT_DEBUG */
1316
1317 trx_id = row_get_rec_trx_id(prev_version, index, *offsets);
1318
1319 if (view->changes_visible(trx_id, index->table->name)) {
1320 /* The view already sees this version: we can copy
1321 it to in_heap and return */
1322
1323 buf =
1324 static_cast<byte *>(mem_heap_alloc(in_heap, rec_offs_size(*offsets)));
1325
1326 *old_vers = rec_copy(buf, prev_version, *offsets);
1327 rec_offs_make_valid(*old_vers, index, *offsets);
1328
1329 if (vrow && *vrow) {
1330 *vrow = dtuple_copy(*vrow, in_heap);
1331 dtuple_dup_v_fld(*vrow, in_heap);
1332 }
1333 break;
1334 }
1335
1336 version = prev_version;
1337 }
1338
1339 mem_heap_free(heap);
1340
1341 return err;
1342 }
1343
1344 /** Constructs the last committed version of a clustered index record,
1345 which should be seen by a semi-consistent read.
1346 @param[in] rec Record in a clustered index; the caller must have a latch on the
1347 page; this latch locks the top of the stack of versions of this records
1348 @param[in] mtr Mini-transaction holding the latch on rec
1349 @param[in] index The clustered index
1350 @param[in,out] offsets Offsets returned by rec_get_offsets(rec, index)
1351 @param[in,out] offset_heap Memory heap from which the offsets are allocated
1352 @param[in] in_heap Memory heap from which the memory for *old_vers is allocated;
1353 memory for possible intermediate versions is allocated and freed locally within
1354 the function
1355 @param[out] old_vers Rec, old version, or null if the record does not exist in
1356 the view, that is, it was freshly inserted afterwards
1357 @param[out] vrow Virtual row, old version, or null if it is not updated in the
1358 view */
1359 void row_vers_build_for_semi_consistent_read(
1360 const rec_t *rec, mtr_t *mtr, dict_index_t *index, ulint **offsets,
1361 mem_heap_t **offset_heap, mem_heap_t *in_heap, const rec_t **old_vers,
1362 const dtuple_t **vrow) {
1363 const rec_t *version;
1364 mem_heap_t *heap = nullptr;
1365 byte *buf;
1366 trx_id_t rec_trx_id = 0;
1367
1368 ut_ad(index->is_clustered());
1369 ut_ad(mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_X_FIX) ||
1370 mtr_memo_contains_page(mtr, rec, MTR_MEMO_PAGE_S_FIX));
1371 ut_ad(!rw_lock_own(&(purge_sys->latch), RW_LOCK_S));
1372
1373 ut_ad(rec_offs_validate(rec, index, *offsets));
1374
1375 version = rec;
1376 ut_ad(!vrow || !(*vrow));
1377
1378 for (;;) {
1379 mem_heap_t *heap2;
1380 rec_t *prev_version;
1381 trx_id_t version_trx_id;
1382
1383 version_trx_id = row_get_rec_trx_id(version, index, *offsets);
1384 if (rec == version) {
1385 rec_trx_id = version_trx_id;
1386 }
1387 if (!trx_rw_is_active(version_trx_id, false)) {
1388 committed_version_trx:
1389 /* We found a version that belongs to a
1390 committed transaction: return it. */
1391
1392 #if defined UNIV_DEBUG || defined UNIV_BLOB_LIGHT_DEBUG
1393 ut_a(!rec_offs_any_null_extern(index, version, *offsets));
1394 #endif /* UNIV_DEBUG || UNIV_BLOB_LIGHT_DEBUG */
1395
1396 if (rec == version) {
1397 *old_vers = rec;
1398 if (vrow) {
1399 *vrow = nullptr;
1400 }
1401 break;
1402 }
1403
1404 /* We assume that a rolled-back transaction stays in
1405 TRX_STATE_ACTIVE state until all the changes have been
1406 rolled back and the transaction is removed from
1407 the global list of transactions. */
1408
1409 if (rec_trx_id == version_trx_id) {
1410 /* The transaction was committed while
1411 we searched for earlier versions.
1412 Return the current version as a
1413 semi-consistent read. */
1414
1415 version = rec;
1416 *offsets = rec_get_offsets(version, index, *offsets, ULINT_UNDEFINED,
1417 UT_LOCATION_HERE, offset_heap);
1418 }
1419
1420 buf =
1421 static_cast<byte *>(mem_heap_alloc(in_heap, rec_offs_size(*offsets)));
1422
1423 *old_vers = rec_copy(buf, version, *offsets);
1424 rec_offs_make_valid(*old_vers, index, *offsets);
1425 if (vrow && *vrow) {
1426 *vrow = dtuple_copy(*vrow, in_heap);
1427 dtuple_dup_v_fld(*vrow, in_heap);
1428 }
1429 break;
1430 }
1431
1432 DEBUG_SYNC_C("after_row_vers_check_trx_active");
1433
1434 heap2 = heap;
1435 heap = mem_heap_create(1024, UT_LOCATION_HERE);
1436
1437 if (!trx_undo_prev_version_build(rec, mtr, version, index, *offsets, heap,
1438 &prev_version, in_heap, vrow, 0,
1439 nullptr)) {
1440 mem_heap_free(heap);
1441 heap = heap2;
1442 heap2 = nullptr;
1443 goto committed_version_trx;
1444 }
1445
1446 if (heap2) {
1447 mem_heap_free(heap2); /* free version */
1448 }
1449
1450 if (prev_version == nullptr) {
1451 /* It was a freshly inserted version */
1452 *old_vers = nullptr;
1453 ut_ad(!vrow || !(*vrow));
1454 break;
1455 }
1456
1457 version = prev_version;
1458 *offsets = rec_get_offsets(version, index, *offsets, ULINT_UNDEFINED,
1459 UT_LOCATION_HERE, offset_heap);
1460 #if defined UNIV_DEBUG || defined UNIV_BLOB_LIGHT_DEBUG
1461 ut_a(!rec_offs_any_null_extern(index, version, *offsets));
1462 #endif /* UNIV_DEBUG || UNIV_BLOB_LIGHT_DEBUG */
1463 } /* for (;;) */
1464
1465 if (heap) {
1466 mem_heap_free(heap);
1467 }
1468 }