Debian Codegraph @

storage/innobase/include/ibuf0ibuf.ic

raw
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.ic
29 Insert buffer
30
31 Created 7/19/1997 Heikki Tuuri
32 *******************************************************/
33
34 #include "fsp0types.h"
35 #include "page0page.h"
36 #include "page0zip.h"
37 #ifndef UNIV_HOTBACKUP
38 #include "buf0lru.h"
39
40 /** An index page must contain at least UNIV_PAGE_SIZE /
41 IBUF_PAGE_SIZE_PER_FREE_SPACE bytes of free space for ibuf to try to
42 buffer inserts to this page. If there is this much of free space, the
43 corresponding bits are set in the ibuf bitmap. */
44 constexpr uint32_t IBUF_PAGE_SIZE_PER_FREE_SPACE = 32;
45
46 /** Starts an insert buffer mini-transaction. */
47 static inline void ibuf_mtr_start(mtr_t *mtr) /*!< out: mini-transaction */
48 {
49 mtr_start(mtr);
50 mtr->enter_ibuf();
51 }
52 /** Commits an insert buffer mini-transaction. */
53 static inline void ibuf_mtr_commit(mtr_t *mtr) /*!< in/out: mini-transaction */
54 {
55 ut_ad(mtr->is_inside_ibuf());
56 ut_d(mtr->exit_ibuf());
57
58 mtr_commit(mtr);
59 }
60
61 /** Insert buffer struct */
62 struct ibuf_t {
63 ulint size; /*!< current size of the ibuf index
64 tree, in pages */
65 ulint max_size; /*!< recommended maximum size of the
66 ibuf index tree, in pages */
67 ulint seg_size; /*!< allocated pages of the file
68 segment containing ibuf header and
69 tree */
70 bool empty; /*!< Protected by the page
71 latch of the root page of the
72 insert buffer tree
73 (FSP_IBUF_TREE_ROOT_PAGE_NO). true
74 if and only if the insert
75 buffer tree is empty. */
76 ulint free_list_len; /*!< length of the free list */
77 ulint height; /*!< tree height */
78 dict_index_t *index; /*!< insert buffer index */
79
80 std::atomic<ulint> n_merges; /*!< number of pages merged */
81 std::atomic<ulint> n_merged_ops[IBUF_OP_COUNT];
82 /*!< number of operations of each type
83 merged to index pages */
84 std::atomic<ulint> n_discarded_ops[IBUF_OP_COUNT];
85 /*!< number of operations of each type
86 discarded without merging due to the
87 tablespace being deleted or the
88 index being dropped */
89 };
90
91 /** Sets the free bit of the page in the ibuf bitmap. This is done in a separate
92 mini-transaction, hence this operation does not restrict further work to only
93 ibuf bitmap operations, which would result if the latch to the bitmap page
94 were kept. */
95 void ibuf_set_free_bits_func(
96 buf_block_t *block, /*!< in: index page of a non-clustered index;
97 free bit is reset if page level is 0 */
98 #ifdef UNIV_IBUF_DEBUG
99 ulint max_val, /*!< in: ULINT_UNDEFINED or a maximum
100 value which the bits must have before
101 setting; this is for debugging */
102 #endif /* UNIV_IBUF_DEBUG */
103 ulint val); /*!< in: value to set: < 4 */
104 #ifdef UNIV_IBUF_DEBUG
105 static inline void ibuf_set_free_bits(buf_block_t *b, ulint v, ulint max) {
106 ibuf_set_free_bits_func(b, max, v);
107 }
108 #else /* UNIV_IBUF_DEBUG */
109 static inline void ibuf_set_free_bits(buf_block_t *b, ulint v, ulint) {
110 ibuf_set_free_bits_func(b, v);
111 }
112 #endif /* UNIV_IBUF_DEBUG */
113
114 /** A basic partial test if an insert to the insert buffer could be possible and
115 recommended. */
116 static inline bool ibuf_should_try(
117 dict_index_t *index, /*!< in: index where to insert */
118 ulint ignore_sec_unique) /*!< in: if != 0, we should
119 ignore UNIQUE constraint on
120 a secondary index when we
121 decide */
122 {
123 return (innodb_change_buffering != IBUF_USE_NONE && ibuf->max_size != 0 &&
124 index->space != dict_sys_t::s_dict_space_id &&
125 !index->is_clustered() && !dict_index_is_spatial(index) &&
126 !dict_index_has_desc(index) &&
127 index->table->quiesce == QUIESCE_NONE &&
128 (ignore_sec_unique || !dict_index_is_unique(index)) &&
129 srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE);
130 }
131
132 /** Returns true if the current OS thread is performing an insert buffer
133 routine.
134
135 For instance, a read-ahead of non-ibuf pages is forbidden by threads
136 that are executing an insert buffer routine.
137 @return true if inside an insert buffer routine */
138 static inline bool ibuf_inside(const mtr_t *mtr) /*!< in: mini-transaction */
139 {
140 return (mtr->is_inside_ibuf());
141 }
142
143 /** Checks if a page address is an ibuf bitmap page (level 3 page) address.
144 @param[in] page_id page id
145 @param[in] page_size page size
146 @return true if a bitmap page */
147 static inline bool ibuf_bitmap_page(const page_id_t &page_id,
148 const page_size_t &page_size) {
149 return ((page_id.page_no() & (page_size.physical() - 1)) ==
150 FSP_IBUF_BITMAP_OFFSET);
151 }
152
153 /** Translates the free space on a page to a value in the ibuf bitmap.
154 @param[in] page_size page size in bytes
155 @param[in] max_ins_size maximum insert size after reorganize for
156 the page
157 @return value for ibuf bitmap bits */
158 static inline ulint ibuf_index_page_calc_free_bits(ulint page_size,
159 ulint max_ins_size) {
160 ulint n;
161 ut_ad(ut_is_2pow(page_size));
162 ut_ad(page_size > IBUF_PAGE_SIZE_PER_FREE_SPACE);
163
164 n = max_ins_size / (page_size / IBUF_PAGE_SIZE_PER_FREE_SPACE);
165
166 if (n == 3) {
167 n = 2;
168 }
169
170 if (n > 3) {
171 n = 3;
172 }
173
174 return (n);
175 }
176
177 /** Translates the ibuf free bits to the free space on a page in bytes.
178 @param[in] page_size page_size
179 @param[in] bits value for ibuf bitmap bits
180 @return maximum insert size after reorganize for the page */
181 static inline ulint ibuf_index_page_calc_free_from_bits(
182 const page_size_t &page_size, ulint bits) {
183 ut_ad(bits < 4);
184 ut_ad(!page_size.is_compressed() ||
185 page_size.physical() > IBUF_PAGE_SIZE_PER_FREE_SPACE);
186
187 if (bits == 3) {
188 return (4 * page_size.physical() / IBUF_PAGE_SIZE_PER_FREE_SPACE);
189 }
190
191 return (bits * (page_size.physical() / IBUF_PAGE_SIZE_PER_FREE_SPACE));
192 }
193
194 /** Translates the free space on a compressed page to a value in the ibuf
195 bitmap.
196 @return value for ibuf bitmap bits */
197 static inline ulint ibuf_index_page_calc_free_zip(
198 const buf_block_t *block) /*!< in: buffer block */
199 {
200 ulint max_ins_size;
201 lint zip_max_ins;
202
203 ut_ad(block->page.size.is_compressed());
204
205 /* Consider the maximum insert size on the uncompressed page
206 without reorganizing the page. We must not assume anything
207 about the compression ratio. If zip_max_ins > max_ins_size and
208 there is 1/4 garbage on the page, recompression after the
209 reorganize could fail, in theory. So, let us guarantee that
210 merging a buffered insert to a compressed page will always
211 succeed without reorganizing or recompressing the page, just
212 by using the page modification log. */
213 max_ins_size = page_get_max_insert_size(buf_block_get_frame(block), 1);
214
215 const auto page_zip = block->get_page_zip();
216 zip_max_ins = page_zip_max_ins_size(page_zip, false /* not clustered */);
217
218 if (zip_max_ins < 0) {
219 return (0);
220 } else if (max_ins_size > (ulint)zip_max_ins) {
221 max_ins_size = (ulint)zip_max_ins;
222 }
223
224 return (ibuf_index_page_calc_free_bits(block->page.size.physical(),
225 max_ins_size));
226 }
227
228 /** Translates the free space on a page to a value in the ibuf bitmap.
229 @return value for ibuf bitmap bits */
230 static inline ulint ibuf_index_page_calc_free(
231 const buf_block_t *block) /*!< in: buffer block */
232 {
233 if (!block->page.size.is_compressed()) {
234 ulint max_ins_size;
235
236 max_ins_size = page_get_max_insert_size_after_reorganize(
237 buf_block_get_frame(block), 1);
238
239 return (ibuf_index_page_calc_free_bits(block->page.size.physical(),
240 max_ins_size));
241 } else {
242 return (ibuf_index_page_calc_free_zip(block));
243 }
244 }
245
246 /** Updates the free bits of an uncompressed page in the ibuf bitmap if
247 there is not enough free on the page any more. This is done in a
248 separate mini-transaction, hence this operation does not restrict
249 further work to only ibuf bitmap operations, which would result if the
250 latch to the bitmap page were kept. NOTE: The free bits in the insert
251 buffer bitmap must never exceed the free space on a page. It is
252 unsafe to increment the bits in a separately committed
253 mini-transaction, because in crash recovery, the free bits could
254 momentarily be set too high. It is only safe to use this function for
255 decrementing the free bits. Should more free space become available,
256 we must not update the free bits here, because that would break crash
257 recovery. */
258 static inline void ibuf_update_free_bits_if_full(
259 buf_block_t *block, /*!< in: index page to which we have added new
260 records; the free bits are updated if the
261 index is non-clustered and non-unique and
262 the page level is 0, and the page becomes
263 fuller */
264 ulint max_ins_size, /*!< in: value of maximum insert size with
265 reorganize before the latest operation
266 performed to the page */
267 ulint increase) /*!< in: upper limit for the additional space
268 used in the latest operation, if known, or
269 ULINT_UNDEFINED */
270 {
271 ulint before;
272 ulint after;
273
274 ut_ad(block->get_page_zip() == nullptr);
275
276 before =
277 ibuf_index_page_calc_free_bits(block->page.size.physical(), max_ins_size);
278
279 if (max_ins_size >= increase) {
280 static_assert(UINT32_UNDEFINED > UNIV_PAGE_SIZE_MAX,
281 "UINT32_UNDEFINED <= UNIV_PAGE_SIZE_MAX");
282
283 after = ibuf_index_page_calc_free_bits(block->page.size.physical(),
284 max_ins_size - increase);
285 #ifdef UNIV_IBUF_DEBUG
286 ut_a(after <= ibuf_index_page_calc_free(block));
287 #endif /* UNIV_IBUF_DEBUG */
288
289 } else {
290 after = ibuf_index_page_calc_free(block);
291 }
292
293 if (after == 0) {
294 /* We move the page to the front of the buffer pool LRU list:
295 the purpose of this is to prevent those pages to which we
296 cannot make inserts using the insert buffer from slipping
297 out of the buffer pool */
298
299 buf_page_make_young(&block->page);
300 }
301
302 if (before > after) {
303 ibuf_set_free_bits(block, after, before);
304 }
305 }
306 #endif /* !UNIV_HOTBACKUP */