Debian Codegraph @

storage/innobase/lob/lob0lob.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 2015, 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 #include <sys/types.h>
29
30 #include "btr0pcur.h"
31 #include "fil0fil.h"
32 #include "lob0first.h"
33 #include "lob0inf.h"
34 #include "lob0lob.h"
35 #include "lob0zip.h"
36 #include "log0chkp.h"
37 #include "row0upd.h"
38 #include "zlob0first.h"
39
40 #include "my_dbug.h"
41
42 namespace lob {
43
44 /** A BLOB field reference has all the bits set to zero, except the "being
45 modified" bit. */
46 const byte field_ref_almost_zero[FIELD_REF_SIZE] = {
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x20, 0, 0, 0, 0, 0, 0, 0,
48 };
49
50 #ifdef UNIV_DEBUG
51 bool ReadContext::assert_read_uncommitted() const {
52 ut_ad(m_trx == nullptr || m_trx->is_read_uncommitted());
53 return (true);
54 }
55 #endif /* UNIV_DEBUG */
56
57 ulint btr_rec_get_field_ref_offs(const dict_index_t *index,
58 const ulint *offsets, ulint n) {
59 ulint field_ref_offs;
60 ulint local_len;
61
62 ut_a(rec_offs_nth_extern(index, offsets, n));
63 field_ref_offs = rec_get_nth_field_offs(index, offsets, n, &local_len);
64 ut_a(rec_field_not_null_not_add_col_def(local_len));
65 ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
66
67 return (field_ref_offs + local_len - BTR_EXTERN_FIELD_REF_SIZE);
68 }
69
70 /** Marks non-updated off-page fields as disowned by this record.
71 The ownership must be transferred to the updated record which is
72 inserted elsewhere in the index tree. In purge only the owner of
73 externally stored field is allowed to free the field.
74 @param[in] update update vector. */
75 void BtrContext::disown_inherited_fields(const upd_t *update) {
76 ut_ad(rec_offs_validate());
77 ut_ad(!rec_offs_comp(m_offsets) || !rec_get_node_ptr_flag(m_rec));
78 ut_ad(rec_offs_any_extern(m_offsets));
79 ut_ad(m_mtr);
80
81 for (ulint i = 0; i < rec_offs_n_fields(m_offsets); i++) {
82 if (rec_offs_nth_extern(m_index, m_offsets, i) &&
83 !upd_get_field_by_field_no(update, i, false)) {
84 set_ownership_of_extern_field(i, false);
85 }
86 }
87 }
88
89 /** When bulk load is being done, check if there is enough space in redo
90 log file. */
91 void BtrContext::check_redolog_bulk() {
92 ut_ad(is_bulk());
93
94 Flush_observer *observer = m_mtr->get_flush_observer();
95
96 rec_block_fix();
97
98 commit_btr_mtr();
99
100 DEBUG_SYNC_C("blob_write_middle");
101
102 log_free_check();
103
104 start_btr_mtr();
105 m_mtr->set_flush_observer(observer);
106
107 rec_block_unfix();
108 ut_ad(validate());
109 }
110
111 /** Check if there is enough space in log file. Commit and re-start the
112 mini-transaction. */
113 void BtrContext::check_redolog_normal() {
114 ut_ad(!is_bulk());
115
116 Flush_observer *observer = m_mtr->get_flush_observer();
117 store_position();
118
119 commit_btr_mtr();
120
121 DEBUG_SYNC_C("blob_write_middle");
122
123 log_free_check();
124
125 DEBUG_SYNC_C("blob_write_middle_after_check");
126
127 start_btr_mtr();
128
129 m_mtr->set_flush_observer(observer);
130
131 restore_position();
132
133 ut_ad(validate());
134 }
135
136 /** Print this blob directory into the given output stream.
137 @param[in] out the output stream.
138 @return the output stream. */
139 std::ostream &blob_dir_t::print(std::ostream &out) const {
140 out << "[blob_dir_t: ";
141 for (const blob_page_info_t &info : m_pages) {
142 out << info;
143 }
144 out << "]";
145 return (out);
146 }
147
148 /** Print this blob_page_into_t object into the given output stream.
149 @param[in] out the output stream.
150 @return the output stream. */
151 std::ostream &blob_page_info_t::print(std::ostream &out) const {
152 out << "[blob_page_info_t: m_page_no=" << m_page_no << ", m_bytes=" << m_bytes
153 << ", m_zbytes=" << m_zbytes << "]";
154 return (out);
155 }
156
157 /** Do setup of the zlib stream.
158 @return code returned by zlib. */
159 int zReader::setup_zstream() {
160 const ulint local_prefix = m_rctx.m_local_len - BTR_EXTERN_FIELD_REF_SIZE;
161
162 m_stream.next_out = m_rctx.m_buf + local_prefix;
163 m_stream.avail_out = static_cast<uInt>(m_rctx.m_len - local_prefix);
164 m_stream.next_in = Z_NULL;
165 m_stream.avail_in = 0;
166
167 /* Zlib inflate needs 32 kilobytes for the default
168 window size, plus a few kilobytes for small objects. */
169 m_heap = mem_heap_create(40000, UT_LOCATION_HERE);
170 page_zip_set_alloc(&m_stream, m_heap);
171
172 int err = inflateInit(&m_stream);
173 return (err);
174 }
175
176 /** Fetch the BLOB.
177 @return DB_SUCCESS on success, DB_FAIL on error. */
178 dberr_t zReader::fetch() {
179 DBUG_TRACE;
180
181 dberr_t err = DB_SUCCESS;
182
183 ut_ad(m_rctx.is_valid_blob());
184 ut_ad(assert_empty_local_prefix());
185
186 ut_d(m_page_type_ex =
187 m_rctx.is_sdi() ? FIL_PAGE_SDI_ZBLOB : FIL_PAGE_TYPE_ZBLOB);
188
189 setup_zstream();
190
191 m_remaining = m_rctx.m_blobref.length();
192
193 while (m_rctx.m_page_no != FIL_NULL) {
194 page_no_t curr_page_no = m_rctx.m_page_no;
195
196 err = fetch_page();
197 if (err != DB_SUCCESS) {
198 break;
199 }
200
201 m_stream.next_in = m_bpage->zip.data + m_rctx.m_offset;
202 m_stream.avail_in =
203 static_cast<uInt>(m_rctx.m_page_size.physical() - m_rctx.m_offset);
204
205 int zlib_err = inflate(&m_stream, Z_NO_FLUSH);
206 switch (zlib_err) {
207 case Z_OK:
208 if (m_stream.avail_out == 0) {
209 goto end_of_blob;
210 }
211 break;
212 case Z_STREAM_END:
213 if (m_rctx.m_page_no == FIL_NULL) {
214 goto end_of_blob;
215 }
216 [[fallthrough]];
217 default:
218 err = DB_FAIL;
219 ib::error(ER_IB_MSG_630)
220 << "inflate() of compressed BLOB page "
221 << page_id_t(m_rctx.m_space_id, curr_page_no) << " returned "
222 << zlib_err << " (" << m_stream.msg << ")";
223 ut_error;
224 [[fallthrough]];
225 case Z_BUF_ERROR:
226 goto end_of_blob;
227 }
228
229 buf_page_release_zip(m_bpage);
230
231 m_rctx.m_offset = FIL_PAGE_NEXT;
232
233 ut_d(if (!m_rctx.m_is_sdi) m_page_type_ex = FIL_PAGE_TYPE_ZBLOB2);
234 }
235
236 end_of_blob:
237 buf_page_release_zip(m_bpage);
238 inflateEnd(&m_stream);
239 mem_heap_free(m_heap);
240 UNIV_MEM_ASSERT_RW(m_rctx.m_buf, m_stream.total_out);
241 return err;
242 }
243
244 #ifdef UNIV_DEBUG
245 /** Assert that the local prefix is empty. For compressed row format,
246 there is no local prefix stored. This function doesn't return if the
247 local prefix is non-empty.
248 @return true if local prefix is empty*/
249 bool zReader::assert_empty_local_prefix() {
250 ut_ad(m_rctx.m_local_len == BTR_EXTERN_FIELD_REF_SIZE);
251 return (true);
252 }
253 #endif /* UNIV_DEBUG */
254
255 dberr_t zReader::fetch_page() {
256 dberr_t err(DB_SUCCESS);
257
258 m_bpage = buf_page_get_zip(page_id_t(m_rctx.m_space_id, m_rctx.m_page_no),
259 m_rctx.m_page_size);
260
261 ut_a(m_bpage != nullptr);
262 ut_ad(fil_page_get_type(m_bpage->zip.data) == m_page_type_ex);
263 m_rctx.m_page_no = mach_read_from_4(m_bpage->zip.data + FIL_PAGE_NEXT);
264
265 if (m_rctx.m_offset == FIL_PAGE_NEXT) {
266 /* When the BLOB begins at page header,
267 the compressed data payload does not
268 immediately follow the next page pointer. */
269 m_rctx.m_offset = FIL_PAGE_DATA;
270 } else {
271 m_rctx.m_offset += 4;
272 }
273
274 return (err);
275 }
276
277 /** This is used to take action when we enter and exit a scope. When we enter
278 the scope the constructor will set the "being modified" bit in the lob reference
279 objects that are either being inserted or updated. When we exit the scope the
280 destructor will clear the "being modified" bit in the lob reference objects. */
281 struct Being_modified {
282 /** Constructor. Set the "being modified" bit in LOB references.
283 @param[in] ctx the B-tree context for LOB operation.
284 @param[in] big_rec_vec the LOB vector
285 @param[in] pcur persistent cursor
286 @param[in] offsets the record offsets
287 @param[in] op the operation code
288 @param[in] mtr the mini-transaction context. */
289 Being_modified(BtrContext &ctx, const big_rec_t *big_rec_vec,
290 btr_pcur_t *pcur, ulint *offsets, opcode op, mtr_t *mtr)
291 : m_btr_ctx(ctx),
292 m_big_rec_vec(big_rec_vec),
293 m_pcur(pcur),
294 m_offsets(offsets),
295 m_op(op),
296 m_mtr(mtr) {
297 /* All pointers to externally stored columns in the record
298 must either be zero or they must be pointers to inherited
299 columns, owned by this record or an earlier record version. */
300 rec_t *rec = m_pcur->get_rec();
301 dict_index_t *index = m_pcur->index();
302 #ifdef UNIV_DEBUG
303 rec_offs_make_valid(rec, index, m_offsets);
304 #endif /* UNIV_DEBUG */
305 for (uint i = 0; i < m_big_rec_vec->n_fields; i++) {
306 ulint field_no = m_big_rec_vec->fields[i].field_no;
307 byte *field_ref = btr_rec_get_field_ref(index, rec, m_offsets, field_no);
308 ref_t blobref(field_ref);
309
310 ut_ad(!blobref.is_being_modified());
311
312 /* Before we release latches in a subsequent ctx.check_redolog() call,
313 mark the blobs as being modified. This is needed to ensure that READ
314 UNCOMMITTED transactions don't read an inconsistent BLOB. */
315 if (index->is_compressed()) {
316 blobref.set_being_modified(true, nullptr);
317
318 if (m_op == OPCODE_INSERT_UPDATE) {
319 /* Inserting by updating a del-marked record. */
320 blobref.set_page_no(FIL_NULL, nullptr);
321 }
322
323 if (!m_btr_ctx.is_bulk()) {
324 buf_block_t *rec_block = m_pcur->get_block();
325 page_zip_des_t *page_zip = buf_block_get_page_zip(rec_block);
326 page_zip_write_blob_ptr(page_zip, rec, index, m_offsets,
327 index->get_field_off_pos(field_no), m_mtr);
328 }
329 } else {
330 blobref.set_being_modified(true, m_mtr);
331 }
332
333 #if defined UNIV_DEBUG || defined UNIV_BLOB_LIGHT_DEBUG
334
335 /* Make a in-memory copy of the LOB ref. */
336 ref_mem_t ref_mem;
337 blobref.parse(ref_mem);
338
339 ut_a(blobref.is_owner());
340 /* Either this must be an update in place,
341 or the BLOB must be inherited, or the BLOB pointer
342 must be zero (will be written in this function). */
343 ut_a(m_op == OPCODE_UPDATE || m_op == OPCODE_INSERT_UPDATE ||
344 blobref.is_inherited() || blobref.is_null_relaxed());
345 ut_ad(blobref.is_being_modified());
346
347 #endif /* UNIV_DEBUG || UNIV_BLOB_LIGHT_DEBUG */
348 }
349 }
350
351 /** Destructor. Clear the "being modified" bit in LOB references. */
352 ~Being_modified() {
353 rec_t *rec = m_pcur->get_rec();
354 dict_index_t *index = m_pcur->index();
355 #ifdef UNIV_DEBUG
356 rec_offs_make_valid(rec, index, m_offsets);
357 #endif /* UNIV_DEBUG */
358 for (uint i = 0; i < m_big_rec_vec->n_fields; i++) {
359 ulint field_no = m_big_rec_vec->fields[i].field_no;
360 byte *field_ref =
361 btr_rec_get_field_ref(m_btr_ctx.index(), rec, m_offsets, field_no);
362 ref_t blobref(field_ref);
363
364 if (index->is_compressed()) {
365 blobref.set_being_modified(false, nullptr);
366 if (!m_btr_ctx.is_bulk()) {
367 buf_block_t *rec_block = m_pcur->get_block();
368 page_zip_des_t *page_zip = buf_block_get_page_zip(rec_block);
369 page_zip_write_blob_ptr(page_zip, rec, index, m_offsets,
370 index->get_field_off_pos(field_no), m_mtr);
371 }
372 } else {
373 blobref.set_being_modified(false, m_mtr);
374 }
375 }
376 }
377
378 BtrContext &m_btr_ctx;
379 const big_rec_t *m_big_rec_vec;
380 btr_pcur_t *m_pcur;
381 ulint *m_offsets;
382 opcode m_op;
383 mtr_t *m_mtr;
384 };
385
386 /** Stores the fields in big_rec_vec to the tablespace and puts pointers to
387 them in rec. The extern flags in rec will have to be set beforehand. The
388 fields are stored on pages allocated from leaf node file segment of the index
389 tree.
390
391 TODO: If the allocation extends the tablespace, it will not be redo logged, in
392 any mini-transaction. Tablespace extension should be redo-logged, so that
393 recovery will not fail when the big_rec was written to the extended portion of
394 the file, in case the file was somehow truncated in the crash.
395 @param[in] trx the trx doing LOB store. If unavailable it
396 could be nullptr.
397 @param[in,out] pcur a persistent cursor. if btr_mtr is restarted,
398 then this can be repositioned.
399 @param[in] upd update vector
400 @param[in,out] offsets rec_get_offsets() on pcur. the "external in
401 offsets will correctly correspond storage"
402 flagsin offsets will correctly correspond to
403 rec when this function returns
404 @param[in] big_rec_vec vector containing fields to be stored
405 externally
406 @param[in,out] btr_mtr mtr containing the latches to the clustered
407 index. can be committed and restarted.
408 @param[in] op operation code
409 @return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
410 dberr_t btr_store_big_rec_extern_fields(trx_t *trx, btr_pcur_t *pcur,
411 const upd_t *upd, ulint *offsets,
412 const big_rec_t *big_rec_vec,
413 mtr_t *btr_mtr, opcode op) {
414 mtr_t mtr;
415 mtr_t mtr_bulk;
416 page_zip_des_t *page_zip;
417 dberr_t error = DB_SUCCESS;
418 dict_index_t *index = pcur->index();
419 dict_table_t *table = index->table;
420 buf_block_t *rec_block = pcur->get_block();
421 rec_t *rec = pcur->get_rec();
422
423 ut_ad(rec_offs_validate(rec, index, offsets));
424 ut_ad(rec_offs_any_extern(offsets));
425 ut_ad(btr_mtr);
426 ut_ad(mtr_memo_contains_flagged(btr_mtr, dict_index_get_lock(index),
427 MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) ||
428 index->table->is_intrinsic() || !index->is_committed());
429 ut_ad(
430 mtr_is_block_fix(btr_mtr, rec_block, MTR_MEMO_PAGE_X_FIX, index->table));
431 ut_ad(buf_block_get_frame(rec_block) == page_align(rec));
432 ut_a(index->is_clustered());
433
434 ut_a(dict_table_page_size(table).equals_to(rec_block->page.size));
435
436 /* Create a blob operation context. */
437 BtrContext btr_ctx(btr_mtr, pcur, index, rec, offsets, rec_block, op);
438 InsertContext ctx(btr_ctx, big_rec_vec);
439
440 Being_modified bm(btr_ctx, big_rec_vec, pcur, offsets, op, btr_mtr);
441
442 /* The pcur could be re-positioned. Commit and restart btr_mtr. */
443 ctx.check_redolog();
444 rec_block = pcur->get_block();
445 rec = pcur->get_rec();
446
447 page_zip = buf_block_get_page_zip(rec_block);
448 ut_a(fil_page_index_page_check(page_align(rec)) || op == OPCODE_INSERT_BULK);
449
450 if (page_zip != nullptr) {
451 DBUG_EXECUTE_IF("lob_insert_single_zstream",
452 { goto insert_single_zstream; });
453
454 if (dict_index_is_sdi(index)) {
455 goto insert_single_zstream;
456 }
457
458 } else {
459 /* Uncompressed LOB */
460
461 DBUG_EXECUTE_IF("lob_insert_noindex", { goto insert_noindex; });
462
463 if (dict_index_is_sdi(index)) {
464 goto insert_noindex;
465 }
466 }
467
468 for (uint i = 0; i < big_rec_vec->n_fields; i++) {
469 ulint field_no = big_rec_vec->fields[i].field_no;
470
471 /* Cursor could have changed position. */
472 rec = pcur->get_rec();
473 rec_offs_make_valid(rec, index, offsets);
474 ut_ad(rec_offs_validate(rec, index, offsets));
475
476 byte *field_ref = btr_rec_get_field_ref(index, rec, offsets, field_no);
477
478 ref_t blobref(field_ref);
479 ut_ad(blobref.validate(btr_mtr));
480
481 bool can_do_partial_update = false;
482
483 if (op == lob::OPCODE_UPDATE && upd != nullptr &&
484 big_rec_vec->fields[i].ext_in_old) {
485 can_do_partial_update = blobref.is_lob_partially_updatable(index);
486 }
487
488 if (page_zip != nullptr) {
489 bool do_insert = true;
490
491 if (op == lob::OPCODE_UPDATE && upd != nullptr &&
492 blobref.is_big(rec_block->page.size) && can_do_partial_update) {
493 if (upd->is_partially_updated(field_no)) {
494 /* Do partial update. */
495 error = lob::z_update(ctx, trx, index, upd, field_no, blobref);
496 switch (error) {
497 case DB_SUCCESS:
498 do_insert = false;
499 break;
500 case DB_FAIL:
501 break;
502 default:
503 ut_error;
504 }
505 } else {
506 /* This is to inform the purge thread that
507 the older version LOB in this update operation
508 can be freed. */
509 blobref.mark_not_partially_updatable(trx, btr_mtr, index,
510 dict_table_page_size(table));
511 }
512 }
513
514 if (do_insert) {
515 const ulint lob_len = big_rec_vec->fields[i].len;
516 if (ref_t::use_single_z_stream(lob_len)) {
517 zInserter zblob_writer(&ctx);
518 error = zblob_writer.prepare();
519 if (error == DB_SUCCESS) {
520 zblob_writer.write_one_blob(i);
521 error = zblob_writer.finish();
522 }
523 } else {
524 error = lob::z_insert(&ctx, trx, blobref, &big_rec_vec->fields[i], i);
525 }
526
527 if (op == lob::OPCODE_UPDATE && upd != nullptr) {
528 /* Get the corresponding upd_field_t
529 object.*/
530 const upd_field_t *const uf =
531 upd->get_field_by_field_no(field_no, index);
532
533 if (uf != nullptr) {
534 /* Update the LOB reference
535 stored in upd_field_t */
536 const dfield_t *const new_val = &uf->new_val;
537
538 if (dfield_is_ext(new_val)) {
539 byte *field_ref = new_val->blobref();
540 blobref.copy(field_ref);
541 ref_t::set_being_modified(field_ref, false, nullptr);
542 }
543 }
544 }
545 }
546
547 } else {
548 /* Uncompressed LOB */
549 bool do_insert = true;
550
551 if (op == lob::OPCODE_UPDATE && upd != nullptr &&
552 blobref.is_big(rec_block->page.size) && can_do_partial_update) {
553 if (upd->is_partially_updated(field_no)) {
554 /* Do partial update. */
555 error = lob::update(ctx, trx, index, upd, field_no, blobref);
556 switch (error) {
557 case DB_SUCCESS:
558 do_insert = false;
559 break;
560 case DB_FAIL:
561 break;
562 case DB_OUT_OF_FILE_SPACE:
563 break;
564 default:
565 ut_error;
566 }
567
568 } else {
569 /* This is to inform the purge thread that
570 the older version LOB in this update operation
571 can be freed. */
572 blobref.mark_not_partially_updatable(trx, btr_mtr, index,
573 dict_table_page_size(table));
574 }
575 }
576
577 if (do_insert) {
578 error = lob::insert(&ctx, trx, blobref, &big_rec_vec->fields[i], i);
579
580 if (op == lob::OPCODE_UPDATE && upd != nullptr) {
581 /* Get the corresponding upd_field_t
582 object.*/
583 const upd_field_t *const uf =
584 upd->get_field_by_field_no(field_no, index);
585
586 if (uf != nullptr) {
587 /* Update the LOB reference
588 stored in upd_field_t */
589 const dfield_t *const new_val = &uf->new_val;
590 if (dfield_is_ext(new_val)) {
591 byte *field_ref = new_val->blobref();
592 blobref.copy(field_ref);
593 ref_t::set_being_modified(field_ref, false, nullptr);
594 }
595 }
596 }
597 }
598 }
599
600 if (error != DB_SUCCESS) {
601 break;
602 }
603
604 #ifdef UNIV_DEBUG
605 /* Ensure that the LOB references are valid now. */
606 rec = pcur->get_rec();
607 rec_offs_make_valid(rec, index, offsets);
608 field_ref = btr_rec_get_field_ref(index, rec, offsets, field_no);
609 ref_t lobref(field_ref);
610
611 ut_ad(!lobref.is_null());
612 #endif /* UNIV_DEBUG */
613 }
614 return (error);
615
616 {
617 insert_single_zstream:
618 /* Insert the LOB as a single zlib stream spanning multiple
619 LOB pages. This is the old way of storing LOBs. */
620 zInserter zblob_writer(&ctx);
621 error = zblob_writer.prepare();
622 if (error == DB_SUCCESS) {
623 zblob_writer.write();
624 error = zblob_writer.finish();
625 }
626 return (error);
627 }
628 {
629 insert_noindex:
630 /* Insert the uncompressed LOB without LOB index. */
631 Inserter blob_writer(&ctx);
632 error = blob_writer.write();
633 return (error);
634 }
635 }
636
637 byte *btr_rec_copy_externally_stored_field_func(
638 trx_t *trx, const dict_index_t *index, const rec_t *rec,
639 const ulint *offsets, const page_size_t &page_size, ulint no, ulint *len,
640 size_t *lob_version, IF_DEBUG(bool is_sdi, ) mem_heap_t *heap,
641 bool is_rebuilt) {
642 ulint local_len;
643 const byte *data;
644
645 const dict_index_t *check_instant_index = index;
646 if (is_rebuilt) {
647 /* nullptr if it is being called when table is being rebuilt because then
648 offsets will be for new records which won't have instant columns. */
649 check_instant_index = nullptr;
650 }
651
652 ut_a(rec_offs_nth_extern(check_instant_index, offsets, no));
653
654 /* An externally stored field can contain some initial
655 data from the field, and in the last 20 bytes it has the
656 space id, page number, and offset where the rest of the
657 field data is stored, and the data length in addition to
658 the data stored locally. We may need to store some data
659 locally to get the local record length above the 128 byte
660 limit so that field offsets are stored in two bytes, and
661 the extern bit is available in those two bytes. */
662
663 data = rec_get_nth_field(check_instant_index, rec, offsets, no, &local_len);
664 const byte *field_ref = data + local_len - BTR_EXTERN_FIELD_REF_SIZE;
665
666 lob::ref_t ref(const_cast<byte *>(field_ref));
667
668 ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
669
670 /* Verify if the LOB reference is sane. */
671 ut_d(space_id_t space_id = ref.space_id());
672 ut_ad(space_id == 0 || space_id == index->space);
673
674 if (ref.is_null()) {
675 /* The externally stored field was not written yet.
676 This record should only be seen by
677 trx_rollback_or_clean_all_recovered() or any
678 TRX_ISO_READ_UNCOMMITTED transactions. */
679
680 return (nullptr);
681 }
682
683 return (btr_copy_externally_stored_field_func(trx, index, len, lob_version,
684 data, page_size, local_len,
685 IF_DEBUG(is_sdi, ) heap));
686 }
687
688 /** Returns the page number where the next BLOB part is stored.
689 @param[in] blob_header the BLOB header.
690 @return page number or FIL_NULL if no more pages */
691 static inline page_no_t btr_blob_get_next_page_no(const byte *blob_header) {
692 return (mach_read_from_4(blob_header + LOB_HDR_NEXT_PAGE_NO));
693 }
694
695 /** Check the FIL_PAGE_TYPE on an uncompressed BLOB page.
696 @param[in] space_id space identifier.
697 @param[in] page_no page number.
698 @param[in] page the page
699 @param[in] read true=read, false=purge */
700 static void btr_check_blob_fil_page_type(space_id_t space_id, page_no_t page_no,
701 const page_t *page, bool read) {
702 ulint type = fil_page_get_type(page);
703
704 ut_a(space_id == page_get_space_id(page));
705 ut_a(page_no == page_get_page_no(page));
706
707 switch (type) {
708 uint32_t flags;
709 case FIL_PAGE_TYPE_BLOB:
710 case FIL_PAGE_SDI_BLOB:
711 break;
712
713 default:
714 flags = fil_space_get_flags(space_id);
715 #ifndef UNIV_DEBUG /* Improve debug test coverage */
716 if (!DICT_TF_HAS_ATOMIC_BLOBS(flags)) {
717 /* Old versions of InnoDB did not initialize
718 FIL_PAGE_TYPE on BLOB pages. Do not print
719 anything about the type mismatch when reading
720 a BLOB page that may be from old versions. */
721 return;
722 }
723 #endif /* !UNIV_DEBUG */
724
725 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_631)
726 << "FIL_PAGE_TYPE=" << type << " on BLOB "
727 << (read ? "read" : "purge") << " space " << space_id << " page "
728 << page_no << " flags " << flags;
729 }
730 }
731
732 /** Returns the length of a BLOB part stored on the header page.
733 @param[in] blob_header the BLOB header.
734 @return part length */
735 static inline ulint btr_blob_get_part_len(const byte *blob_header) {
736 return (mach_read_from_4(blob_header + LOB_HDR_PART_LEN));
737 }
738
739 /** Fetch one BLOB page. */
740 void Reader::fetch_page() {
741 mtr_t mtr;
742
743 /* Bytes of LOB data available in the current LOB page. */
744 ulint part_len;
745
746 /* Bytes of LOB data obtained from the current LOB page. */
747 ulint copy_len;
748
749 ut_ad(m_rctx.m_page_no != FIL_NULL);
750 ut_ad(m_rctx.m_page_no > 0);
751
752 mtr_start(&mtr);
753
754 m_cur_block =
755 buf_page_get(page_id_t(m_rctx.m_space_id, m_rctx.m_page_no),
756 m_rctx.m_page_size, RW_S_LATCH, UT_LOCATION_HERE, &mtr);
757 buf_block_dbg_add_level(m_cur_block, SYNC_EXTERN_STORAGE);
758 page_t *page = buf_block_get_frame(m_cur_block);
759
760 btr_check_blob_fil_page_type(m_rctx.m_space_id, m_rctx.m_page_no, page, true);
761
762 byte *blob_header = page + m_rctx.m_offset;
763 part_len = btr_blob_get_part_len(blob_header);
764 copy_len = std::min(part_len, m_rctx.m_len - m_copied_len);
765
766 memcpy(m_rctx.m_buf + m_copied_len, blob_header + LOB_HDR_SIZE, copy_len);
767
768 m_copied_len += copy_len;
769 m_rctx.m_page_no = btr_blob_get_next_page_no(blob_header);
770 mtr_commit(&mtr);
771 m_rctx.m_offset = FIL_PAGE_DATA;
772 }
773
774 /** Fetch the complete or prefix of the uncompressed LOB data.
775 @return bytes of LOB data fetched. */
776 ulint Reader::fetch() {
777 if (m_rctx.m_blobref.is_null()) {
778 ut_ad(m_copied_len == 0);
779 return (m_copied_len);
780 }
781
782 while (m_copied_len < m_rctx.m_len) {
783 if (m_rctx.m_page_no == FIL_NULL) {
784 /* End of LOB has been reached. */
785 break;
786 }
787
788 fetch_page();
789 }
790
791 /* Assure that we have fetched the requested amount or the LOB
792 has ended. */
793 ut_ad(m_copied_len == m_rctx.m_len || m_rctx.m_page_no == FIL_NULL);
794
795 return (m_copied_len);
796 }
797
798 /** Copies the prefix of an externally stored field of a record.
799 The clustered index record must be protected by a lock or a page latch.
800 @param[in] trx the current transaction object if available
801 or nullptr.
802 @param[in] index the clust index in which lob is read.
803 @param[out] buf the field, or a prefix of it
804 @param[in] len length of buf, in bytes
805 @param[in] page_size BLOB page size
806 @param[in] data 'internally' stored part of the field
807 containing also the reference to the external
808 part; must be protected by a lock or a page
809 latch.
810 @param[in] is_sdi true for SDI indexes
811 @param[in] local_len length of data, in bytes
812 @return the length of the copied field, or 0 if the column was being
813 or has been deleted */
814 ulint btr_copy_externally_stored_field_prefix_func(
815 trx_t *trx, const dict_index_t *index, byte *buf, ulint len,
816 const page_size_t &page_size, const byte *data,
817 IF_DEBUG(bool is_sdi, ) ulint local_len) {
818 ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
819
820 if (page_size.is_compressed()) {
821 ut_a(local_len == BTR_EXTERN_FIELD_REF_SIZE);
822
823 ReadContext rctx(page_size, data, local_len, buf, len IF_DEBUG(, is_sdi));
824
825 rctx.m_index = const_cast<dict_index_t *>(index);
826 rctx.m_trx = trx;
827
828 /* Obtain length of LOB available in clustered index.*/
829 ulint avail_lob = rctx.m_blobref.length();
830
831 if (avail_lob == 0) {
832 /* No LOB data available. */
833 return (0);
834 }
835
836 /* Read the LOB data. */
837 ulint fetch_len = lob::z_read(&rctx, rctx.m_blobref, 0, len, buf);
838
839 /* Either fetch the requested length or fetch the complete
840 LOB. If complete LOB is fetched, then it means that requested
841 length is bigger than the available length. */
842 ut_a(fetch_len == 0 || fetch_len == len ||
843 (fetch_len == avail_lob && avail_lob < len));
844
845 return (fetch_len);
846 }
847
848 local_len -= BTR_EXTERN_FIELD_REF_SIZE;
849
850 if (UNIV_UNLIKELY(local_len >= len)) {
851 memcpy(buf, data, len);
852 return (len);
853 }
854
855 memcpy(buf, data, local_len);
856 data += local_len;
857
858 ut_a(memcmp(data, field_ref_zero, BTR_EXTERN_FIELD_REF_SIZE));
859
860 if (!mach_read_from_4(data + BTR_EXTERN_LEN + 4)) {
861 /* The externally stored part of the column has been
862 (partially) deleted. Signal the half-deleted BLOB
863 to the caller. */
864
865 return (0);
866 }
867
868 ReadContext rctx(page_size, data, local_len + BTR_EXTERN_FIELD_REF_SIZE,
869 buf + local_len, len IF_DEBUG(, false));
870
871 rctx.m_index = (dict_index_t *)index;
872 rctx.m_trx = trx;
873
874 ulint fetch_len = lob::read(&rctx, rctx.m_blobref, 0, len, buf + local_len);
875 return (local_len + fetch_len);
876 }
877
878 byte *btr_copy_externally_stored_field_func(
879 trx_t *trx, const dict_index_t *index, ulint *len, size_t *lob_version,
880 const byte *data, const page_size_t &page_size, ulint local_len,
881 IF_DEBUG(bool is_sdi, ) mem_heap_t *heap) {
882 uint32_t extern_len;
883 byte *buf;
884
885 ut_a(index->is_clustered());
886
887 ut_a(local_len >= BTR_EXTERN_FIELD_REF_SIZE);
888
889 local_len -= BTR_EXTERN_FIELD_REF_SIZE;
890
891 /* Currently a BLOB cannot be bigger than 4 GB; we
892 leave the 4 upper bytes in the length field unused */
893
894 const byte *field_ref = data + local_len;
895
896 extern_len = mach_read_from_4(data + local_len + BTR_EXTERN_LEN + 4);
897
898 buf = (byte *)mem_heap_alloc(heap, local_len + extern_len);
899
900 ReadContext rctx(page_size, data, local_len + BTR_EXTERN_FIELD_REF_SIZE,
901 buf + local_len, extern_len IF_DEBUG(, is_sdi));
902
903 rctx.m_index = (dict_index_t *)index;
904
905 if (ref_t::is_being_modified(field_ref)) {
906 #ifdef UNIV_DEBUG
907 /* Check the sanity of the LOB reference. */
908 if (ref_t::is_null_relaxed(field_ref) ||
909 ref_t::space_id(field_ref) == index->space) {
910 /* Valid scenario. Do nothing. */
911 } else {
912 bool lob_ref_is_corrupt = false;
913 ut_ad(lob_ref_is_corrupt);
914 }
915 #endif /* UNIV_DEBUG */
916
917 /* This is applicable only for READ UNCOMMITTED transactions because they
918 don't take transaction locks. */
919 ut_ad(trx == nullptr || trx->is_read_uncommitted());
920
921 *len = 0;
922 return (buf);
923 }
924
925 if (extern_len == 0) {
926 /* The lob has already been purged. */
927 ut_ad(ref_t::page_no(field_ref) == FIL_NULL);
928 *len = 0;
929 return (buf);
930 }
931
932 if (page_size.is_compressed()) {
933 ut_ad(local_len == 0);
934 *len = 0;
935
936 if (extern_len > 0) {
937 *len = lob::z_read(&rctx, rctx.m_blobref, 0, extern_len, buf + local_len);
938 }
939
940 return (buf);
941 } else {
942 if (local_len > 0) {
943 memcpy(buf, data, local_len);
944 }
945
946 ulint fetch_len =
947 lob::read(&rctx, rctx.m_blobref, 0, extern_len, buf + local_len);
948
949 *len = local_len + fetch_len;
950
951 if (lob_version != nullptr) {
952 *lob_version = rctx.m_lob_version;
953 }
954
955 return (buf);
956 }
957 }
958
959 void BtrContext::free_updated_extern_fields(trx_id_t trx_id, undo_no_t undo_no,
960 const upd_t *update, bool rollback,
961 big_rec_t *big_rec_vec) {
962 ulint n_fields;
963 ulint i;
964 ut_ad(rollback);
965
966 #ifdef UNIV_DEBUG
967 ut_ad(big_rec_vec == nullptr || materialize_instant_default(m_index, m_rec));
968 #endif
969
970 ut_ad(rec_offs_validate());
971 ut_ad(mtr_is_page_fix(m_mtr, m_rec, MTR_MEMO_PAGE_X_FIX, m_index->table));
972 /* Assert that the cursor position and the record are matching. */
973 ut_ad(!need_recalc());
974
975 /* Free possible externally stored fields in the record */
976
977 n_fields = upd_get_n_fields(update);
978
979 for (i = 0; i < n_fields; i++) {
980 const upd_field_t *ufield = upd_get_nth_field(update, i);
981
982 /* No need to free the column if it is a virtual column as it does not
983 consume any storage. */
984 if (!ufield->is_virtual() &&
985 rec_offs_nth_extern(m_index, m_offsets, ufield->field_no)) {
986 /* Skip freeing fields which are added as part of updating a record in
987 table having instant index */
988 if (big_rec_vec != nullptr) {
989 bool found = false;
990 for (size_t i = 0; i < big_rec_vec->n_fields; i++) {
991 if (ufield->field_no == big_rec_vec->fields[i].field_no) {
992 found = true;
993 break;
994 }
995 }
996 if (found) {
997 continue;
998 }
999 }
1000
1001 ulint len;
1002 byte *data =
1003 rec_get_nth_field(m_index, m_rec, m_offsets, ufield->field_no, &len);
1004 ut_a(len >= BTR_EXTERN_FIELD_REF_SIZE);
1005
1006 byte *field_ref = data + len - BTR_EXTERN_FIELD_REF_SIZE;
1007
1008 DeleteContext ctx(*this, field_ref, ufield->field_no, rollback);
1009
1010 /* Last argument is nullptr because this is rollback. */
1011 lob::purge(&ctx, m_index, trx_id, undo_no, 0, ufield, nullptr);
1012 if (need_recalc()) {
1013 recalc();
1014 }
1015 }
1016 }
1017 }
1018
1019 /** Deallocate a buffer block that was reserved for a BLOB part.
1020 @param[in] index Index
1021 @param[in] block Buffer block
1022 @param[in] all true=remove also the compressed page
1023 if there is one
1024 @param[in] mtr Mini-transaction to commit */
1025 void blob_free(dict_index_t *index, buf_block_t *block, bool all, mtr_t *mtr) {
1026 buf_pool_t *buf_pool = buf_pool_from_block(block);
1027 page_id_t page_id(block->page.id.space(), block->page.id.page_no());
1028 bool freed = false;
1029
1030 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
1031
1032 mtr_commit(mtr);
1033
1034 mutex_enter(&buf_pool->LRU_list_mutex);
1035 buf_page_mutex_enter(block);
1036
1037 /* Only free the block if it is still allocated to
1038 the same file page. */
1039
1040 if (buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE &&
1041 page_id == block->page.id) {
1042 freed = buf_LRU_free_page(&block->page, all);
1043
1044 if (!freed && all && block->page.zip.data &&
1045 buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE &&
1046 page_id == block->page.id) {
1047 /* Attempt to deallocate the uncompressed page
1048 if the whole block cannot be deallocted. */
1049
1050 freed = buf_LRU_free_page(&block->page, false);
1051 }
1052 }
1053
1054 if (!freed) {
1055 mutex_exit(&buf_pool->LRU_list_mutex);
1056 buf_page_mutex_exit(block);
1057 }
1058 }
1059
1060 /** Gets the externally stored size of a record, in units of a database page.
1061 @param[in] index index
1062 @param[in] rec record
1063 @param[in] offsets array returned by rec_get_offsets()
1064 @return externally stored part, in units of a database page */
1065 ulint btr_rec_get_externally_stored_len(const dict_index_t *index,
1066 const rec_t *rec,
1067 const ulint *offsets) {
1068 ulint n_fields;
1069 ulint total_extern_len = 0;
1070 ulint i;
1071
1072 ut_ad(!rec_offs_comp(offsets) || !rec_get_node_ptr_flag(rec));
1073
1074 if (!rec_offs_any_extern(offsets)) {
1075 return (0);
1076 }
1077
1078 n_fields = rec_offs_n_fields(offsets);
1079
1080 for (i = 0; i < n_fields; i++) {
1081 if (rec_offs_nth_extern(index, offsets, i)) {
1082 ulint extern_len = mach_read_from_4(
1083 btr_rec_get_field_ref(index, rec, offsets, i) + BTR_EXTERN_LEN + 4);
1084
1085 total_extern_len += ut_calc_align(extern_len, UNIV_PAGE_SIZE);
1086 }
1087 }
1088
1089 return (total_extern_len / UNIV_PAGE_SIZE);
1090 }
1091
1092 /** Frees the externally stored fields for a record.
1093 @param[in] trx_id transaction identifier whose LOB is
1094 being freed.
1095 @param[in] undo_no undo number within a transaction whose
1096 LOB is being freed.
1097 @param[in] rollback performing rollback?
1098 @param[in] rec_type undo record type.
1099 @param[in] node purge node or nullptr */
1100 void BtrContext::free_externally_stored_fields(trx_id_t trx_id,
1101 undo_no_t undo_no, bool rollback,
1102 ulint rec_type,
1103 purge_node_t *node) {
1104 ut_ad(rec_offs_validate());
1105 ut_ad(mtr_is_page_fix(m_mtr, m_rec, MTR_MEMO_PAGE_X_FIX, m_index->table));
1106 /* Assert that the cursor position and the record are matching. */
1107 ut_ad(!need_recalc());
1108
1109 /* Free possible externally stored fields in the record */
1110 ut_ad(dict_table_is_comp(m_index->table) == rec_offs_comp(m_offsets));
1111 ulint n_fields = rec_offs_n_fields(m_offsets);
1112 DeleteContext ctx(*this, rollback);
1113 for (ulint i = 0; i < n_fields; i++) {
1114 if (rec_offs_nth_extern(m_index, m_offsets, i)) {
1115 byte *field_ref = btr_rec_get_field_ref(m_index, m_rec, m_offsets, i);
1116 ctx.set_blob(field_ref, m_index->get_field_off_pos(i));
1117 upd_field_t *uf = nullptr;
1118 lob::purge(&ctx, m_index, trx_id, undo_no, rec_type, uf, node);
1119 if (need_recalc()) {
1120 recalc();
1121 }
1122 }
1123 }
1124 ctx.free_lob_blocks();
1125 }
1126
1127 /** Load the first page of LOB and read its page type.
1128 @param[in] index the index object.
1129 @param[in] page_size the page size of LOB.
1130 @param[out] is_partially_updatable is the LOB partially updatable.
1131 @return the page type of first page of LOB.*/
1132 ulint ref_t::get_lob_page_info(const dict_index_t *index,
1133 const page_size_t &page_size,
1134 bool &is_partially_updatable) const {
1135 mtr_t mtr;
1136 buf_block_t *block;
1137 ref_mem_t ref_mem;
1138
1139 parse(ref_mem);
1140
1141 mtr_start(&mtr);
1142
1143 block = buf_page_get(page_id_t(ref_mem.m_space_id, ref_mem.m_page_no),
1144 page_size, RW_S_LATCH, UT_LOCATION_HERE, &mtr);
1145
1146 page_type_t page_type = block->get_page_type();
1147
1148 switch (page_type) {
1149 case FIL_PAGE_TYPE_LOB_FIRST: {
1150 first_page_t first_page(block, &mtr, (dict_index_t *)index);
1151 is_partially_updatable = first_page.can_be_partially_updated();
1152 break;
1153 }
1154 case FIL_PAGE_TYPE_ZLOB_FIRST: {
1155 z_first_page_t z_first_page(block, &mtr, (dict_index_t *)index);
1156 is_partially_updatable = z_first_page.can_be_partially_updated();
1157 break;
1158 }
1159 default:
1160 is_partially_updatable = false;
1161 }
1162
1163 mtr_commit(&mtr);
1164
1165 return (page_type);
1166 }
1167
1168 /** Load the first page of the LOB and mark it as not partially
1169 updatable anymore.
1170 @param[in] trx Current transaction
1171 @param[in] mtr Mini-transaction context.
1172 @param[in] index Index dictionary object.
1173 @param[in] page_size Page size information. */
1174 void ref_t::mark_not_partially_updatable(trx_t *trx, mtr_t *mtr,
1175 dict_index_t *index,
1176 const page_size_t &page_size) {
1177 buf_block_t *block;
1178 ref_mem_t ref_mem;
1179
1180 parse(ref_mem);
1181
1182 /* If LOB has already been purged, ignore it. */
1183 if (ref_mem.is_purged()) {
1184 return;
1185 }
1186
1187 block = buf_page_get(page_id_t(ref_mem.m_space_id, ref_mem.m_page_no),
1188 page_size, RW_X_LATCH, UT_LOCATION_HERE, mtr);
1189
1190 page_type_t page_type = block->get_page_type();
1191
1192 switch (page_type) {
1193 case FIL_PAGE_TYPE_LOB_FIRST: {
1194 first_page_t first_page(block, mtr, (dict_index_t *)index);
1195 first_page.mark_cannot_be_partially_updated(trx);
1196 break;
1197 }
1198 case FIL_PAGE_TYPE_ZLOB_FIRST: {
1199 z_first_page_t z_first_page(block, mtr, (dict_index_t *)index);
1200 z_first_page.mark_cannot_be_partially_updated(trx);
1201 break;
1202 }
1203 default:
1204 /* do nothing */
1205 break;
1206 }
1207 }
1208
1209 /** Check if the LOB can be partially updated. This is done by loading
1210 the first page of LOB and looking at the flags.
1211 @param[in] index the index to which LOB belongs.
1212 @return true if LOB is partially updatable, false otherwise.*/
1213 bool ref_t::is_lob_partially_updatable(const dict_index_t *index) const {
1214 if (is_null_relaxed()) {
1215 return (false);
1216 }
1217
1218 const page_size_t page_size = dict_table_page_size(index->table);
1219
1220 if (page_size.is_compressed() && use_single_z_stream()) {
1221 return (false);
1222 }
1223
1224 bool can_do_partial_update = false;
1225 ulint page_type = get_lob_page_info(index, page_size, can_do_partial_update);
1226
1227 bool page_type_ok = (page_type == FIL_PAGE_TYPE_LOB_FIRST ||
1228 page_type == FIL_PAGE_TYPE_ZLOB_FIRST);
1229
1230 return (page_type_ok && can_do_partial_update);
1231 }
1232
1233 std::ostream &ref_t::print(std::ostream &out) const {
1234 out << "[ref_t: m_ref=" << (void *)m_ref << ", space_id=" << space_id()
1235 << ", page_no=" << page_no() << ", offset=" << offset()
1236 << ", length=" << length()
1237 << ", is_being_modified=" << is_being_modified() << "]";
1238 return (out);
1239 }
1240
1241 #ifdef UNIV_DEBUG
1242 bool ref_t::check_space_id(dict_index_t *index) const {
1243 space_id_t idx_space_id = index->space;
1244 space_id_t ref_space_id = space_id();
1245
1246 bool lob_ref_valid = (ref_space_id == 0 || idx_space_id == ref_space_id);
1247 return (lob_ref_valid);
1248 }
1249 #endif /* UNIV_DEBUG */
1250
1251 /** Acquire an x-latch on the index page containing the clustered
1252 index record, in the given mini-transaction context.
1253 @param[in] mtr Mini-transaction context. */
1254 void DeleteContext::x_latch_rec_page(mtr_t *mtr) {
1255 bool found;
1256 page_t *rec_page = m_blobref.page_align();
1257 page_no_t rec_page_no = page_get_page_no(rec_page);
1258 space_id_t rec_space_id = page_get_space_id(rec_page);
1259
1260 const page_size_t &rec_page_size =
1261 fil_space_get_page_size(rec_space_id, &found);
1262 ut_ad(found);
1263
1264 #ifdef UNIV_DEBUG
1265 buf_block_t *block =
1266 #endif /* UNIV_DEBUG */
1267 buf_page_get(page_id_t(rec_space_id, rec_page_no), rec_page_size,
1268 RW_X_LATCH, UT_LOCATION_HERE, mtr);
1269
1270 ut_ad(block != nullptr);
1271 }
1272
1273 #ifdef UNIV_DEBUG
1274 bool rec_check_lobref_space_id(dict_index_t *index, const rec_t *rec,
1275 const ulint *offsets) {
1276 /* Make it more robust. If rec pointer is null, don't do anything. */
1277 if (rec == nullptr) {
1278 return (true);
1279 }
1280
1281 ut_ad(index->is_clustered());
1282 ut_ad(rec_offs_validate(rec, nullptr, offsets));
1283
1284 const ulint n = rec_offs_n_fields(offsets);
1285
1286 for (ulint i = 0; i < n; i++) {
1287 ulint len;
1288
1289 if (rec_offs_nth_default(index, offsets, i)) {
1290 continue;
1291 }
1292
1293 const byte *data = rec_get_nth_field(index, rec, offsets, i, &len);
1294
1295 if (len == UNIV_SQL_NULL) {
1296 continue;
1297 }
1298
1299 if (rec_offs_nth_extern(index, offsets, i)) {
1300 ulint local_len = len - BTR_EXTERN_FIELD_REF_SIZE;
1301 ut_ad(len >= BTR_EXTERN_FIELD_REF_SIZE);
1302
1303 const byte *field_ref = data + local_len;
1304 if (!ref_t{const_cast<byte *>(field_ref)}.check_space_id(index)) {
1305 return (false);
1306 }
1307 }
1308 }
1309 return (true);
1310 }
1311 #endif /* UNIV_DEBUG */
1312
1313 dberr_t mark_not_partially_updatable(trx_t *trx, dict_index_t *index,
1314 const upd_t *update,
1315 const mtr_t *btr_mtr) {
1316 if (!index->is_clustered()) {
1317 /* Only clustered index can have LOBs. */
1318 return (DB_SUCCESS);
1319 }
1320
1321 const ulint n_fields = upd_get_n_fields(update);
1322
1323 for (ulint i = 0; i < n_fields; i++) {
1324 const upd_field_t *ufield = upd_get_nth_field(update, i);
1325
1326 if (ufield->is_virtual()) {
1327 continue;
1328 }
1329
1330 if (update->is_partially_updated(ufield->field_no)) {
1331 continue;
1332 }
1333
1334 const dfield_t *new_field = &ufield->new_val;
1335
1336 if (ufield->ext_in_old && !dfield_is_ext(new_field)) {
1337 const dfield_t *old_field = &ufield->old_val;
1338 byte *field_ref = old_field->blobref();
1339 ref_t ref(field_ref);
1340
1341 if (!ref.is_null_relaxed()) {
1342 mtr_t local_mtr;
1343 ut_ad(ref.space_id() == index->space_id());
1344
1345 mtr_start(&local_mtr);
1346 local_mtr.set_log_mode(btr_mtr->get_log_mode());
1347 ref.mark_not_partially_updatable(trx, &local_mtr, index,
1348 index->get_page_size());
1349 mtr_commit(&local_mtr);
1350 }
1351 }
1352 }
1353
1354 return (DB_SUCCESS);
1355 }
1356
1357 } // namespace lob