MySQL 9.1.0
Source Code Documentation
ibuf0ibuf.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1997, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 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
46of percentage of the buffer pool. */
47constexpr uint32_t CHANGE_BUFFER_DEFAULT_SIZE = 25;
48
49#ifndef UNIV_HOTBACKUP
50/* Possible operations buffered in the insert/whatever buffer. See
51ibuf_insert(). DO NOT CHANGE THE VALUES OF THESE, THEY ARE STORED ON DISK. */
52typedef enum {
56
57 /* Number of different operation types. */
60
61/** Combinations of operations that can be buffered.
62@see innodb_change_buffering_names */
65 IBUF_USE_INSERT, /* insert */
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. */
73extern ulong innodb_change_buffering;
74
75/** The insert buffer control structure */
76extern ibuf_t *ibuf;
77
78/* The purpose of the insert buffer is to reduce random disk access.
79When we wish to insert a record into a non-unique secondary index and
80the B-tree leaf page where the record belongs to is not in the buffer
81pool, 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
83pool, we look up the insert buffer B-tree for any modifications to the
84page, and apply these upon the completion of the read operation. This
85is called the insert buffer merge. */
86
87/* The insert buffer merge must always succeed. To guarantee this,
88the insert buffer subsystem keeps track of the free space in pages for
89which it can buffer operations. Two bits per page in the insert
90buffer bitmap indicate the available space in coarse increments. The
91free bits in the insert buffer bitmap must never exceed the free space
92on a page. It is safe to decrement or reset the bits in the bitmap in
93a mini-transaction that is committed before the mini-transaction that
94affects the free space. It is unsafe to increment the bits in a
95separately committed mini-transaction, because in crash recovery, the
96free bits could momentarily be set too high. */
97
98/** Creates the insert buffer data structure at a database startup. */
99void ibuf_init_at_db_start(void);
100/** Updates the max_size value for ibuf. */
101void 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. */
106/** Starts an insert buffer mini-transaction. */
107static inline void ibuf_mtr_start(mtr_t *mtr); /*!< out: mini-transaction */
108/** Commits an insert buffer mini-transaction. */
109static inline void ibuf_mtr_commit(mtr_t *mtr); /*!< in/out: mini-transaction */
110
111/** Initializes an ibuf bitmap page. */
112void 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. */
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
129is not enough free on the page any more. This is done in a separate
130mini-transaction, hence this operation does not restrict further work to only
131ibuf bitmap operations, which would result if the latch to the bitmap page were
132kept. NOTE: The free bits in the insert buffer bitmap must never exceed the
133free space on a page. It is unsafe to increment the bits in a separately
134committed mini-transaction, because in crash recovery, the free bits could
135momentarily be set too high. It is only safe to use this function for
136decrementing the free bits. Should more free space become available, we must
137not 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 */
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. */
159void 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. */
173void 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. */
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
187and 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*/
192static 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 */
208static 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.
212Must 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
216bitmap page)
217@param[in] location Location where called
218@param[in,out] mtr mtr which will contain an x-latch to the
219bitmap page if the page is not one of the fixed address ibuf pages, or NULL,
220in 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.
228Must 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 */
234inline 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
240OS thread calls fsp services to allocate a new file segment, or a new page to a
241file segment, and the thread did not own the fsp latch before this call. */
242void ibuf_free_excess_pages(void);
243
244/** Buffer an operation in the insert/delete buffer, instead of doing it
245directly to the disk page, if this is possible. Does not do it if the index
246is 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 */
254bool 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
259applies any buffered operations to the page and deletes the entries from the
260insert buffer. If the page is not read, but created in the buffer pool, this
261function deletes its buffered entries from the insert buffer; there can
262exist entries for such a page if the page belonged to an index which
263subsequently was dropped.
264@param[in,out] block if page has been read from disk,
265pointer 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
268if we have deleted or are deleting the tablespace, then we naturally do not
269want to update a non-existent bitmap page
270@param[in] page_size page size */
271void 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
276in DISCARD TABLESPACE and IMPORT TABLESPACE.
277NOTE: this does not update the page free bitmaps in the space. The space will
278become CORRUPT when you call this function! */
279void 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
282PCT_IO(100). If false, the size of contract batch is determined based on the
283current size of the change buffer.
284@return a lower limit for the combined size in bytes of entries which will be
285merged from ibuf trees to the pages read, 0 if ibuf is empty */
287
288/** Contracts insert buffer trees by reading pages referring to space_id
289to the buffer pool.
290@returns number of pages merged.*/
291ulint 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 */
300const byte *ibuf_parse_bitmap_init(const byte *ptr, const byte *end_ptr,
301 buf_block_t *block, 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
308this page */
309ulint 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 */
315bool ibuf_is_empty(void);
316/** Prints info of ibuf. */
317void ibuf_print(FILE *file); /*!< in: file where to print */
318/********************************************************************
319Read the first two bytes from a record's fourth field (counter field in new
320records; something else in older records).
321@return "counter" field, or ULINT_UNDEFINED if for some reason it can't be read
322*/
323ulint 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
326record
327@param[in] rec ibuf record
328@return true if there is any multi-value field in the record */
329bool ibuf_rec_has_multi_value(const rec_t *rec);
330
331/** Closes insert buffer and frees the data structures. */
332void ibuf_close(void);
333
334/** Checks the insert buffer bitmaps on IMPORT TABLESPACE.
335 @return DB_SUCCESS or error code */
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 */
344
347
348#endif /* !UNIV_HOTBACKUP */
349
350/* The ibuf header page currently contains only the file segment header
351for the file segment from which the pages for the ibuf tree are allocated */
352constexpr uint32_t IBUF_HEADER = PAGE_DATA;
353/** fseg header for ibuf tree */
354constexpr uint32_t IBUF_TREE_SEG_HEADER = 0;
355
356#include "ibuf0ibuf.ic"
357
358#endif
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:48
Page identifier.
Definition: buf0types.h:207
Page size descriptor.
Definition: page0size.h:50
dberr_t
Definition: db0err.h:39
Data dictionary memory object creation.
File space management.
constexpr uint32_t FSP_IBUF_TREE_ROOT_PAGE_NO
insert buffer B-tree root page in tablespace 0; The ibuf tree root page number in tablespace 0; its f...
Definition: fsp0types.h:168
constexpr uint32_t FSP_IBUF_HEADER_PAGE_NO
The following pages exist in the system tablespace (space 0).
Definition: fsp0types.h:164
static bool ibuf_bitmap_page(const page_id_t &page_id, const page_size_t &page_size)
Checks if a page address is an ibuf bitmap page (level 3 page) address.
bool ibuf_page(const page_id_t &page_id, const page_size_t &page_size, ut::Location location, mtr_t *mtr)
Checks if a page is a level 2 or 3 page in the ibuf hierarchy of pages.
Definition: ibuf0ibuf.h:234
ulint ibuf_merge_in_background(bool full)
Contract the change buffer by reading pages to the buffer pool.
Definition: ibuf0ibuf.cc:2399
constexpr uint32_t IBUF_TREE_SEG_HEADER
fseg header for ibuf tree
Definition: ibuf0ibuf.h:354
void ibuf_print(FILE *file)
Prints info of ibuf.
Definition: ibuf0ibuf.cc:4376
void ibuf_delete_for_discarded_space(space_id_t space)
Deletes all entries in the insert buffer for a given space id.
Definition: ibuf0ibuf.cc:4281
const byte * ibuf_parse_bitmap_init(const byte *ptr, const byte *end_ptr, buf_block_t *block, mtr_t *mtr)
Parses a redo log record of an ibuf bitmap page init.
Definition: ibuf0ibuf.cc:566
bool ibuf_rec_has_multi_value(const rec_t *rec)
Determine if there is any multi-value field data on the change buffer record.
Definition: ibuf0ibuf.cc:1216
bool ibuf_is_empty(void)
Looks if the insert buffer is empty.
Definition: ibuf0ibuf.cc:4357
void ibuf_init_at_db_start(void)
Creates the insert buffer data structure at a database startup.
Definition: ibuf0ibuf.cc:458
void ibuf_merge_or_delete_for_page(buf_block_t *block, const page_id_t &page_id, const page_size_t *page_size, bool update_ibuf_bitmap)
When an index page is read from a disk to the buffer pool, this function applies any buffered operati...
Definition: ibuf0ibuf.cc:3960
static bool ibuf_inside(const mtr_t *mtr)
Returns true if the current OS thread is performing an insert buffer routine.
constexpr uint32_t IBUF_HEADER
Definition: ibuf0ibuf.h:352
void ibuf_update_free_bits_low(const buf_block_t *block, ulint max_ins_size, mtr_t *mtr)
Updates the free bits for an uncompressed page to reflect the present state.
Definition: ibuf0ibuf.cc:841
static void ibuf_mtr_commit(mtr_t *mtr)
Commits an insert buffer mini-transaction.
void ibuf_max_size_update(ulint new_val)
Updates the max_size value for ibuf.
Definition: ibuf0ibuf.cc:531
void ibuf_update_max_tablespace_id(void)
Reads the biggest tablespace id from the high end of the insert buffer tree and updates the counter i...
Definition: ibuf0ibuf.cc:2815
static void ibuf_mtr_start(mtr_t *mtr)
Starts an insert buffer mini-transaction.
dberr_t ibuf_check_bitmap_on_import(const trx_t *trx, space_id_t space_id)
Checks the insert buffer bitmaps on IMPORT TABLESPACE.
Definition: ibuf0ibuf.cc:4417
ulong innodb_change_buffering
Operations that can currently be buffered.
Definition: ibuf0ibuf.cc:204
void ibuf_set_bitmap_for_bulk_load(buf_block_t *block, bool reset)
Updates free bits and buffered bits for bulk loaded page.
Definition: ibuf0ibuf.cc:4535
bool ibuf_page_low(const page_id_t &page_id, const page_size_t &page_size, bool x_latch, ut::Location location, mtr_t *mtr)
Checks if a page is a level 2 or 3 page in the ibuf hierarchy of pages.
Definition: ibuf0ibuf.cc:956
static void ibuf_update_free_bits_if_full(buf_block_t *block, ulint max_ins_size, ulint increase)
Updates the free bits of an uncompressed page in the ibuf bitmap if there is not enough free on the p...
void ibuf_free_excess_pages(void)
Frees excess pages from the ibuf free list.
Definition: ibuf0ibuf.cc:1962
void ibuf_update_free_bits_for_two_pages_low(buf_block_t *block1, buf_block_t *block2, mtr_t *mtr)
Updates the free bits for the two pages to reflect the present state.
Definition: ibuf0ibuf.cc:908
void ibuf_bitmap_page_init(buf_block_t *block, mtr_t *mtr)
Initializes an ibuf bitmap page.
Definition: ibuf0ibuf.cc:543
ibuf_use_t
Combinations of operations that can be buffered.
Definition: ibuf0ibuf.h:63
@ IBUF_USE_ALL
Definition: ibuf0ibuf.h:69
@ IBUF_USE_INSERT_DELETE_MARK
Definition: ibuf0ibuf.h:67
@ IBUF_USE_NONE
Definition: ibuf0ibuf.h:64
@ IBUF_USE_DELETE
Definition: ibuf0ibuf.h:68
@ IBUF_USE_DELETE_MARK
Definition: ibuf0ibuf.h:66
@ IBUF_USE_INSERT
Definition: ibuf0ibuf.h:65
ulint ibuf_merge_space(space_id_t space)
Contracts insert buffer trees by reading pages referring to space_id to the buffer pool.
Definition: ibuf0ibuf.cc:2297
void ibuf_update_free_bits_zip(buf_block_t *block, mtr_t *mtr)
Updates the free bits for a compressed page to reflect the present state.
Definition: ibuf0ibuf.cc:875
constexpr uint32_t CHANGE_BUFFER_DEFAULT_SIZE
Default value for maximum on-disk size of change buffer in terms of percentage of the buffer pool.
Definition: ibuf0ibuf.h:47
ulint ibuf_rec_get_counter(const rec_t *rec)
in: ibuf record
Definition: ibuf0ibuf.cc:1197
bool ibuf_insert(ibuf_op_t op, const dtuple_t *entry, dict_index_t *index, const page_id_t &page_id, const page_size_t &page_size, que_thr_t *thr)
Buffer an operation in the insert/delete buffer, instead of doing it directly to the disk page,...
Definition: ibuf0ibuf.cc:3283
ibuf_t * ibuf
The insert buffer control structure.
Definition: ibuf0ibuf.cc:212
constexpr uint32_t IBUF_TREE_ROOT_PAGE_NO
Definition: ibuf0ibuf.h:346
constexpr uint32_t IBUF_HEADER_PAGE_NO
Definition: ibuf0ibuf.h:345
void ibuf_reset_free_bits(buf_block_t *block)
Resets the free bits of the page in the ibuf bitmap.
Definition: ibuf0ibuf.cc:826
ibuf_op_t
Definition: ibuf0ibuf.h:52
@ IBUF_OP_DELETE_MARK
Definition: ibuf0ibuf.h:54
@ IBUF_OP_COUNT
Definition: ibuf0ibuf.h:58
@ IBUF_OP_DELETE
Definition: ibuf0ibuf.h:55
@ IBUF_OP_INSERT
Definition: ibuf0ibuf.h:53
static bool ibuf_should_try(dict_index_t *index, ulint ignore_sec_unique)
A basic partial test if an insert to the insert buffer could be possible and recommended.
void ibuf_close(void)
Closes insert buffer and frees the data structures.
Definition: ibuf0ibuf.cc:421
Insert buffer.
Insert buffer global types.
Mini-transaction buffer.
std::string HARNESS_EXPORT reset()
get 'reset attributes' ESC sequence.
Definition: vt100.cc:37
const std::string FILE("FILE")
Definition: os0file.h:89
constexpr uint32_t PAGE_DATA
start of data on the page
Definition: page0types.h:105
byte rec_t
Definition: rem0types.h:41
Definition: completion_hash.h:35
The buffer control block structure.
Definition: buf0buf.h:1747
Data structure for an index.
Definition: dict0mem.h:1041
Structure for an SQL data tuple of fields (logical record)
Definition: data0data.h:696
Insert buffer struct.
Definition: ibuf0ibuf.ic:62
Mini-transaction handle and buffer.
Definition: mtr0mtr.h:177
Definition: que0que.h:242
Definition: trx0trx.h:675
Definition: ut0core.h:36
Version control for database, common definitions, and include files.
#define IF_DEBUG(...)
Definition: univ.i:674
unsigned long int ulint
Definition: univ.i:406