| 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 include/ibuf0ibuf.h |
| 29 |
Insert buffer |
| 30 |
|
| 31 |
Created 7/19/1997 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef ibuf0ibuf_h |
| 35 |
#define ibuf0ibuf_h |
| 36 |
|
| 37 |
#include "univ.i" |
| 38 |
|
| 39 |
#include "dict0mem.h" |
| 40 |
#include "fsp0fsp.h" |
| 41 |
#include "mtr0mtr.h" |
| 42 |
|
| 43 |
#include "ibuf0types.h" |
| 44 |
|
| 45 |
/** Default value for maximum on-disk size of change buffer in terms |
| 46 |
of percentage of the buffer pool. */ |
| 47 |
constexpr uint32_t CHANGE_BUFFER_DEFAULT_SIZE = 25; |
| 48 |
|
| 49 |
#ifndef UNIV_HOTBACKUP |
| 50 |
/* Possible operations buffered in the insert/whatever buffer. See |
| 51 |
ibuf_insert(). DO NOT CHANGE THE VALUES OF THESE, THEY ARE STORED ON DISK. */ |
| 52 |
typedef enum { |
| 53 |
IBUF_OP_INSERT = 0, |
| 54 |
IBUF_OP_DELETE_MARK = 1, |
| 55 |
IBUF_OP_DELETE = 2, |
| 56 |
|
| 57 |
/* Number of different operation types. */ |
| 58 |
IBUF_OP_COUNT = 3 |
| 59 |
} ibuf_op_t; |
| 60 |
|
| 61 |
/** Combinations of operations that can be buffered. |
| 62 |
@see innodb_change_buffering_names */ |
| 63 |
enum ibuf_use_t { |
| 64 |
IBUF_USE_NONE = 0, |
| 65 |
IBUF_USE_INSERT, /* insert */ |
| 66 |
IBUF_USE_DELETE_MARK, /* delete */ |
| 67 |
IBUF_USE_INSERT_DELETE_MARK, /* insert+delete */ |
| 68 |
IBUF_USE_DELETE, /* delete+purge */ |
| 69 |
IBUF_USE_ALL /* insert+delete+purge */ |
| 70 |
}; |
| 71 |
|
| 72 |
/** Operations that can currently be buffered. */ |
| 73 |
extern ulong innodb_change_buffering; |
| 74 |
|
| 75 |
/** The insert buffer control structure */ |
| 76 |
extern ibuf_t *ibuf; |
| 77 |
|
| 78 |
/* The purpose of the insert buffer is to reduce random disk access. |
| 79 |
When we wish to insert a record into a non-unique secondary index and |
| 80 |
the B-tree leaf page where the record belongs to is not in the buffer |
| 81 |
pool, we insert the record into the insert buffer B-tree, indexed by |
| 82 |
(space_id, page_no). When the page is eventually read into the buffer |
| 83 |
pool, we look up the insert buffer B-tree for any modifications to the |
| 84 |
page, and apply these upon the completion of the read operation. This |
| 85 |
is called the insert buffer merge. */ |
| 86 |
|
| 87 |
/* The insert buffer merge must always succeed. To guarantee this, |
| 88 |
the insert buffer subsystem keeps track of the free space in pages for |
| 89 |
which it can buffer operations. Two bits per page in the insert |
| 90 |
buffer bitmap indicate the available space in coarse increments. The |
| 91 |
free bits in the insert buffer bitmap must never exceed the free space |
| 92 |
on a page. It is safe to decrement or reset the bits in the bitmap in |
| 93 |
a mini-transaction that is committed before the mini-transaction that |
| 94 |
affects the free space. It is unsafe to increment the bits in a |
| 95 |
separately committed mini-transaction, because in crash recovery, the |
| 96 |
free bits could momentarily be set too high. */ |
| 97 |
|
| 98 |
/** Creates the insert buffer data structure at a database startup. */ |
| 99 |
void ibuf_init_at_db_start(void); |
| 100 |
/** Updates the max_size value for ibuf. */ |
| 101 |
void ibuf_max_size_update(ulint new_val); /*!< in: new value in terms of |
| 102 |
percentage of the buffer pool size */ |
| 103 |
/** Reads the biggest tablespace id from the high end of the insert buffer |
| 104 |
tree and updates the counter in fil_system. */ |
| 105 |
void ibuf_update_max_tablespace_id(void); |
| 106 |
/** Starts an insert buffer mini-transaction. */ |
| 107 |
static inline void ibuf_mtr_start(mtr_t *mtr); /*!< out: mini-transaction */ |
| 108 |
/** Commits an insert buffer mini-transaction. */ |
| 109 |
static inline void ibuf_mtr_commit(mtr_t *mtr); /*!< in/out: mini-transaction */ |
| 110 |
|
| 111 |
/** Initializes an ibuf bitmap page. */ |
| 112 |
void ibuf_bitmap_page_init(buf_block_t *block, /*!< in: bitmap page */ |
| 113 |
mtr_t *mtr /*!< in: mtr */); |
| 114 |
|
| 115 |
/** Resets the free bits of the page in the ibuf bitmap. This is done in a |
| 116 |
separate mini-transaction, hence this operation does not restrict |
| 117 |
further work to only ibuf bitmap operations, which would result if the |
| 118 |
latch to the bitmap page were kept. NOTE: The free bits in the insert |
| 119 |
buffer bitmap must never exceed the free space on a page. It is safe |
| 120 |
to decrement or reset the bits in the bitmap in a mini-transaction |
| 121 |
that is committed before the mini-transaction that affects the free |
| 122 |
space. */ |
| 123 |
void ibuf_reset_free_bits( |
| 124 |
buf_block_t *block); /*!< in: index page; free bits are set to 0 |
| 125 |
if the index is a non-clustered |
| 126 |
non-unique, and page level is 0 */ |
| 127 |
|
| 128 |
/** Updates the free bits of an uncompressed page in the ibuf bitmap if there |
| 129 |
is not enough free on the page any more. This is done in a separate |
| 130 |
mini-transaction, hence this operation does not restrict further work to only |
| 131 |
ibuf bitmap operations, which would result if the latch to the bitmap page were |
| 132 |
kept. NOTE: The free bits in the insert buffer bitmap must never exceed the |
| 133 |
free space on a page. It is unsafe to increment the bits in a separately |
| 134 |
committed mini-transaction, because in crash recovery, the free bits could |
| 135 |
momentarily be set too high. It is only safe to use this function for |
| 136 |
decrementing the free bits. Should more free space become available, we must |
| 137 |
not update the free bits here, because that would break crash recovery. |
| 138 |
@param[in] block index page to which we have added new records; |
| 139 |
the free bits are updated if the index is |
| 140 |
non-clustered and non-unique and the page level |
| 141 |
is 0, and the page becomes fuller |
| 142 |
@param[in] max_ins_size value of maximum insert size with reorganize |
| 143 |
before the latest operation performed to the |
| 144 |
page |
| 145 |
@param[in] increase upper limit for the additional space used in |
| 146 |
the latest operation, if known, or |
| 147 |
ULINT_UNDEFINED */ |
| 148 |
static inline void ibuf_update_free_bits_if_full(buf_block_t *block, |
| 149 |
ulint max_ins_size, |
| 150 |
ulint increase); |
| 151 |
|
| 152 |
/** Updates the free bits for an uncompressed page to reflect the present |
| 153 |
state. Does this in the mtr given, which means that the latching |
| 154 |
order rules virtually prevent any further operations for this OS |
| 155 |
thread until mtr is committed. NOTE: The free bits in the insert |
| 156 |
buffer bitmap must never exceed the free space on a page. It is safe |
| 157 |
to set the free bits in the same mini-transaction that updated the |
| 158 |
page. */ |
| 159 |
void ibuf_update_free_bits_low(const buf_block_t *block, /*!< in: index page */ |
| 160 |
ulint max_ins_size, /*!< in: value of |
| 161 |
maximum insert size |
| 162 |
with reorganize before |
| 163 |
the latest operation |
| 164 |
performed to the page */ |
| 165 |
mtr_t *mtr); /*!< in/out: mtr */ |
| 166 |
/** Updates the free bits for a compressed page to reflect the present |
| 167 |
state. Does this in the mtr given, which means that the latching |
| 168 |
order rules virtually prevent any further operations for this OS |
| 169 |
thread until mtr is committed. NOTE: The free bits in the insert |
| 170 |
buffer bitmap must never exceed the free space on a page. It is safe |
| 171 |
to set the free bits in the same mini-transaction that updated the |
| 172 |
page. */ |
| 173 |
void ibuf_update_free_bits_zip(buf_block_t *block, /*!< in/out: index page */ |
| 174 |
mtr_t *mtr); /*!< in/out: mtr */ |
| 175 |
/** Updates the free bits for the two pages to reflect the present state. |
| 176 |
Does this in the mtr given, which means that the latching order rules |
| 177 |
virtually prevent any further operations until mtr is committed. |
| 178 |
NOTE: The free bits in the insert buffer bitmap must never exceed the |
| 179 |
free space on a page. It is safe to set the free bits in the same |
| 180 |
mini-transaction that updated the pages. */ |
| 181 |
void ibuf_update_free_bits_for_two_pages_low( |
| 182 |
buf_block_t *block1, /*!< in: index page */ |
| 183 |
buf_block_t *block2, /*!< in: index page */ |
| 184 |
mtr_t *mtr); /*!< in: mtr */ |
| 185 |
|
| 186 |
/** A basic partial test if an insert to the insert buffer could be possible |
| 187 |
and recommended. |
| 188 |
@param[in] index index where to insert |
| 189 |
@param[in] ignore_sec_unique if != 0, we should ignore UNIQUE |
| 190 |
constraint on a secondary index when |
| 191 |
we decide*/ |
| 192 |
static inline bool ibuf_should_try(dict_index_t *index, |
| 193 |
ulint ignore_sec_unique); |
| 194 |
|
| 195 |
/** Returns true if the current OS thread is performing an insert buffer |
| 196 |
routine. |
| 197 |
|
| 198 |
For instance, a read-ahead of non-ibuf pages is forbidden by threads |
| 199 |
that are executing an insert buffer routine. |
| 200 |
@return true if inside an insert buffer routine */ |
| 201 |
[[nodiscard]] static inline bool ibuf_inside( |
| 202 |
const mtr_t *mtr); /*!< in: mini-transaction */ |
| 203 |
|
| 204 |
/** Checks if a page address is an ibuf bitmap page (level 3 page) address. |
| 205 |
@param[in] page_id page id |
| 206 |
@param[in] page_size page size |
| 207 |
@return true if a bitmap page */ |
| 208 |
static inline bool ibuf_bitmap_page(const page_id_t &page_id, |
| 209 |
const page_size_t &page_size); |
| 210 |
|
| 211 |
/** Checks if a page is a level 2 or 3 page in the ibuf hierarchy of pages. |
| 212 |
Must not be called when recv_no_ibuf_operations==true. |
| 213 |
@param[in] page_id page id |
| 214 |
@param[in] page_size page size |
| 215 |
@param[in] x_latch false if relaxed check (avoid latching the |
| 216 |
bitmap page) |
| 217 |
@param[in] location Location where called |
| 218 |
@param[in,out] mtr mtr which will contain an x-latch to the |
| 219 |
bitmap page if the page is not one of the fixed address ibuf pages, or NULL, |
| 220 |
in which case a new transaction is created. |
| 221 |
@return true if level 2 or level 3 page */ |
| 222 |
[[nodiscard]] bool ibuf_page_low(const page_id_t &page_id, |
| 223 |
const page_size_t &page_size, |
| 224 |
IF_DEBUG(bool x_latch, ) ut::Location location, |
| 225 |
mtr_t *mtr) MY_ATTRIBUTE((warn_unused_result)); |
| 226 |
|
| 227 |
/** Checks if a page is a level 2 or 3 page in the ibuf hierarchy of pages. |
| 228 |
Must not be called when recv_no_ibuf_operations==true. |
| 229 |
@param[in] page_id Tablespace/page identifier |
| 230 |
@param[in] page_size Page size |
| 231 |
@param[in] location Location where requested |
| 232 |
@param[in,out] mtr Mini-transaction or NULL |
| 233 |
@return true if level 2 or level 3 page */ |
| 234 |
inline bool ibuf_page(const page_id_t &page_id, const page_size_t &page_size, |
| 235 |
ut::Location location, mtr_t *mtr) { |
| 236 |
return ibuf_page_low(page_id, page_size, IF_DEBUG(true, ) location, mtr); |
| 237 |
} |
| 238 |
|
| 239 |
/** Frees excess pages from the ibuf free list. This function is called when an |
| 240 |
OS thread calls fsp services to allocate a new file segment, or a new page to a |
| 241 |
file segment, and the thread did not own the fsp latch before this call. */ |
| 242 |
void ibuf_free_excess_pages(void); |
| 243 |
|
| 244 |
/** Buffer an operation in the insert/delete buffer, instead of doing it |
| 245 |
directly to the disk page, if this is possible. Does not do it if the index |
| 246 |
is clustered or unique. |
| 247 |
@param[in] op operation type |
| 248 |
@param[in] entry index entry to insert |
| 249 |
@param[in,out] index index where to insert |
| 250 |
@param[in] page_id page id where to insert |
| 251 |
@param[in] page_size page size |
| 252 |
@param[in,out] thr query thread |
| 253 |
@return true if success */ |
| 254 |
bool ibuf_insert(ibuf_op_t op, const dtuple_t *entry, dict_index_t *index, |
| 255 |
const page_id_t &page_id, const page_size_t &page_size, |
| 256 |
que_thr_t *thr); |
| 257 |
|
| 258 |
/** When an index page is read from a disk to the buffer pool, this function |
| 259 |
applies any buffered operations to the page and deletes the entries from the |
| 260 |
insert buffer. If the page is not read, but created in the buffer pool, this |
| 261 |
function deletes its buffered entries from the insert buffer; there can |
| 262 |
exist entries for such a page if the page belonged to an index which |
| 263 |
subsequently was dropped. |
| 264 |
@param[in,out] block if page has been read from disk, |
| 265 |
pointer to the page x-latched, else NULL |
| 266 |
@param[in] page_id page id of the index page |
| 267 |
@param[in] update_ibuf_bitmap normally this is set to true, but |
| 268 |
if we have deleted or are deleting the tablespace, then we naturally do not |
| 269 |
want to update a non-existent bitmap page |
| 270 |
@param[in] page_size page size */ |
| 271 |
void ibuf_merge_or_delete_for_page(buf_block_t *block, const page_id_t &page_id, |
| 272 |
const page_size_t *page_size, |
| 273 |
bool update_ibuf_bitmap); |
| 274 |
|
| 275 |
/** Deletes all entries in the insert buffer for a given space id. This is used |
| 276 |
in DISCARD TABLESPACE and IMPORT TABLESPACE. |
| 277 |
NOTE: this does not update the page free bitmaps in the space. The space will |
| 278 |
become CORRUPT when you call this function! */ |
| 279 |
void ibuf_delete_for_discarded_space(space_id_t space); /*!< in: space id */ |
| 280 |
/** Contract the change buffer by reading pages to the buffer pool. |
| 281 |
@param[in] full If true, do a full contraction based on |
| 282 |
PCT_IO(100). If false, the size of contract batch is determined based on the |
| 283 |
current size of the change buffer. |
| 284 |
@return a lower limit for the combined size in bytes of entries which will be |
| 285 |
merged from ibuf trees to the pages read, 0 if ibuf is empty */ |
| 286 |
ulint ibuf_merge_in_background(bool full); |
| 287 |
|
| 288 |
/** Contracts insert buffer trees by reading pages referring to space_id |
| 289 |
to the buffer pool. |
| 290 |
@returns number of pages merged.*/ |
| 291 |
ulint ibuf_merge_space(space_id_t space); /*!< in: space id */ |
| 292 |
|
| 293 |
#endif /* !UNIV_HOTBACKUP */ |
| 294 |
/** Parses a redo log record of an ibuf bitmap page init. |
| 295 |
@param[in] ptr Buffer. |
| 296 |
@param[in] end_ptr Buffer end. |
| 297 |
@param[in] block Block or nullptr. |
| 298 |
@param[in] mtr MTR or nullptr. |
| 299 |
@return end of log record or NULL */ |
| 300 |
byte *ibuf_parse_bitmap_init(byte *ptr, byte *end_ptr, buf_block_t *block, |
| 301 |
mtr_t *mtr); |
| 302 |
#ifndef UNIV_HOTBACKUP |
| 303 |
#ifdef UNIV_IBUF_COUNT_DEBUG |
| 304 |
|
| 305 |
/** Gets the ibuf count for a given page. |
| 306 |
@param[in] page_id page id |
| 307 |
@return number of entries in the insert buffer currently buffered for |
| 308 |
this page */ |
| 309 |
ulint ibuf_count_get(const page_id_t &page_id); |
| 310 |
|
| 311 |
#endif /* UNIV_IBUF_COUNT_DEBUG */ |
| 312 |
|
| 313 |
/** Looks if the insert buffer is empty. |
| 314 |
@return true if empty */ |
| 315 |
bool ibuf_is_empty(void); |
| 316 |
/** Prints info of ibuf. */ |
| 317 |
void ibuf_print(FILE *file); /*!< in: file where to print */ |
| 318 |
/******************************************************************** |
| 319 |
Read the first two bytes from a record's fourth field (counter field in new |
| 320 |
records; something else in older records). |
| 321 |
@return "counter" field, or ULINT_UNDEFINED if for some reason it can't be read |
| 322 |
*/ |
| 323 |
ulint ibuf_rec_get_counter(const rec_t *rec); /*!< in: ibuf record */ |
| 324 |
|
| 325 |
/** Determine if there is any multi-value field data on the change buffer |
| 326 |
record |
| 327 |
@param[in] rec ibuf record |
| 328 |
@return true if there is any multi-value field in the record */ |
| 329 |
bool ibuf_rec_has_multi_value(const rec_t *rec); |
| 330 |
|
| 331 |
/** Closes insert buffer and frees the data structures. */ |
| 332 |
void ibuf_close(void); |
| 333 |
|
| 334 |
/** Checks the insert buffer bitmaps on IMPORT TABLESPACE. |
| 335 |
@return DB_SUCCESS or error code */ |
| 336 |
[[nodiscard]] dberr_t ibuf_check_bitmap_on_import( |
| 337 |
const trx_t *trx, /*!< in: transaction */ |
| 338 |
space_id_t space_id); /*!< in: tablespace identifier */ |
| 339 |
|
| 340 |
/** Updates free bits and buffered bits for bulk loaded page. |
| 341 |
@param[in] block index page |
| 342 |
@param[in] reset flag if reset free val */ |
| 343 |
void ibuf_set_bitmap_for_bulk_load(buf_block_t *block, bool reset); |
| 344 |
|
| 345 |
constexpr uint32_t IBUF_HEADER_PAGE_NO = FSP_IBUF_HEADER_PAGE_NO; |
| 346 |
constexpr uint32_t IBUF_TREE_ROOT_PAGE_NO = FSP_IBUF_TREE_ROOT_PAGE_NO; |
| 347 |
|
| 348 |
#endif /* !UNIV_HOTBACKUP */ |
| 349 |
|
| 350 |
/* The ibuf header page currently contains only the file segment header |
| 351 |
for the file segment from which the pages for the ibuf tree are allocated */ |
| 352 |
constexpr uint32_t IBUF_HEADER = PAGE_DATA; |
| 353 |
/** fseg header for ibuf tree */ |
| 354 |
constexpr uint32_t IBUF_TREE_SEG_HEADER = 0; |
| 355 |
|
| 356 |
#include "ibuf0ibuf.ic" |
| 357 |
|
| 358 |
#endif |