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