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