MySQL 8.3.0
Source Code Documentation
btr0sea.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1996, 2023, 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 also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25*****************************************************************************/
26
27/** @file include/btr0sea.h
28 The index tree adaptive search
29
30 Created 2/17/1996 Heikki Tuuri
31 *************************************************************************/
32
33#ifndef btr0sea_h
34#define btr0sea_h
35
36#include "univ.i"
37
38#include "btr0types.h"
39#include "dict0dict.h"
40#include "ha0ha.h"
41#include "mtr0mtr.h"
42#include "rem0rec.h"
43
44/** The search info struct in an index */
46 /** Number of blocks in this index tree that have search index built i.e.
47 block->ahi.index points to this index. */
48 std::atomic<size_t> ref_count;
49
50 /** @{ The following fields are not protected by any latch.
51 Unfortunately, this means that they must be aligned to the machine word, i.e.,
52 they cannot be turned into bit-fields. */
53
54 /** the root page frame when it was last time fetched, or NULL. */
56 /** when this exceeds BTR_SEARCH_HASH_ANALYSIS, the hash analysis starts; this
57 is reset if no success noticed. */
58 std::atomic<uint64_t> hash_analysis;
59 /** true if the last search would have succeeded, or did succeed, using the
60 hash index; NOTE that the value here is not exact: it is not calculated for
61 every search, and the calculation itself is not always accurate! */
63 /** number of consecutive searches which would have succeeded, or did succeed,
64 using the hash index; the range is 0 .. BTR_SEARCH_BUILD_LIMIT + 5. */
65 std::atomic<uint64_t> n_hash_potential;
66 /** @} */
67
68 std::atomic<btr_search_prefix_info_t> prefix_info;
69 static_assert(decltype(prefix_info)::is_always_lock_free);
70#ifdef UNIV_SEARCH_PERF_STAT
71 /** number of successful hash searches so far. */
72 std::atomic<ulint> n_hash_succ;
73 /** number of failed hash searches */
74 std::atomic<ulint> n_hash_fail;
75 /** number of successful pattern searches thus far */
76 std::atomic<ulint> n_patt_succ;
77 /** number of searches */
78 std::atomic<ulint> n_searches;
79#endif /* UNIV_SEARCH_PERF_STAT */
80#ifdef UNIV_DEBUG
81 /** magic number @see BTR_SEARCH_MAGIC_N */
83#endif /* UNIV_DEBUG */
84};
85
86#ifdef UNIV_DEBUG
87/** value of btr_search_t::magic_n, used in assertions */
88constexpr uint32_t BTR_SEARCH_MAGIC_N = 1112765;
89#endif /* UNIV_DEBUG */
90
91/** The hash index system */
93 public:
94 btr_search_sys_t(size_t hash_size);
95
97 public:
98 void initialize(size_t hash_size);
99 /** The latch protecting the adaptive search part: this latch protects the
100 (1) positions of records on those pages where a hash index has been built.
101 NOTE: It does not protect values of non-ordering fields within a record from
102 being updated in-place! We can use fact (1) to perform unique searches to
103 indexes. */
105 /** The adaptive hash table, mapping dtuple_hash values to rec_t pointers
106 on index pages. For any hash value at most one pointer is hold. Is protected
107 by the part's latch. It is in a separate cache line to not collide with the
108 possible multiple readers that are registering for the latching. */
110 /** A pointer to a free block that the heap in the hash table may use for
111 adding new hash nodes. Changes to nullptr are done under appropriate
112 X-latched rwlock. Changes from nullptr to non-nullptr are done without any
113 protection. Changes from non-null to a different non-null are prohibited. */
114 std::atomic<buf_block_t *> free_block_for_heap;
115 };
116
117 /** Partitions of the AHI system. */
119};
120
121/** The adaptive hash index */
123
124/** Creates and initializes the adaptive search system at a database start.
125@param[in] hash_size hash table size. */
126void btr_search_sys_create(ulint hash_size);
127
128/** Resize hash index hash table.
129@param[in] hash_size hash index hash table size */
130void btr_search_sys_resize(ulint hash_size);
131
132/** Frees the adaptive search system at a database shutdown. */
134
135/** Disable the adaptive hash search system and empty the index.
136@returns true if the AHI system was enabled and became disabled. */
137bool btr_search_disable();
138/** Enable the adaptive hash search system. */
139void btr_search_enable();
140
141/** Creates and initializes a search info struct.
142@param[in] heap heap where created.
143@return own: search info struct */
145
146/** Wait for the specified index to have all references from AHI dropped. We
147assume the caller prevents the new references from AHI from being created. This
148means the reference count will monotonically drop to zero.
149@param[in,out] table table handler
150@param[in,out] index index data dictionary structure
151@param[in] force should the wait be forced even if SRV_SHUTDOWN_CLEANUP
152 state was reached? */
154 bool force);
155
156/** Updates the search info statistics following a search in B-tree that was
157performed not using or not finding row with the AHI index. It may do nothing or
158decide to try to update the searched record on which the supplied cursor in
159positioned at, or add the whole page to AHI.
160@param[in] cursor cursor which was just positioned */
161static inline void btr_search_info_update(btr_cur_t *cursor);
162
163/** Tries to guess the right search position based on the hash search info
164of the index. Note that if mode is PAGE_CUR_LE, which is used in inserts,
165and the function returns true, then cursor->up_match and cursor->low_match
166both have sensible values.
167@param[in] tuple Logical record
168@param[in] mode PAGE_CUR_L, ....
169@param[in] latch_mode BTR_SEARCH_LEAF, ...;
170 NOTE that only if has_search_latch is 0, we will
171 have a latch set on the cursor page, otherwise
172 we assume the caller uses his search latch
173 to protect the record!
174@param[out] cursor Tree cursor
175@param[in] has_search_latch
176 Latch mode the caller currently has on
177 search system: RW_S/X_LATCH or 0
178@param[in] mtr Mini-transaction
179@return true if succeeded */
181 ulint latch_mode, btr_cur_t *cursor,
182 ulint has_search_latch, mtr_t *mtr);
183
184/** Moves or deletes hash entries for moved records. If new_page is already
185hashed in compatible way or not cached at all, then the hash index for the new
186page is (re-)built. Otherwise the old page hash records are dropped.
187@param[in,out] new_block records are copied to this page.
188@param[in,out] block index page from which record are copied, and the
189 copied records will be deleted from this page.
190@param[in,out] index record descriptor */
192 dict_index_t *index);
193
194/** Drop any adaptive hash index entries that point to an index page.
195@param[in,out] block block containing index page, s- or x-latched, or an
196 index page for which we know that
197 block->buf_fix_count == 0 or it is an index page which
198 has already been removed from the buf_pool->page_hash
199 i.e.: it is in state BUF_BLOCK_REMOVE_HASH
200@param[in] force Should the block's index be reset even if AHI is
201 disabled? */
202void btr_search_drop_page_hash_index(buf_block_t *block, bool force = false);
203
204/** Resets block's AHI index field to nullptr, removes the reference from
205index's reference counter. This is done after all blocks' records were removed
206from AHI hash tables and caller assures the block still has reference to index.
207@param[in,out] block index page from which records were removed from
208 AHI hash tables. */
210
211/** Drop any adaptive hash index entries that may point to an index
212page that may be in the buffer pool, when a page is evicted from the
213buffer pool or freed in a file segment.
214@param[in] page_id page id
215@param[in] page_size page size */
217 const page_size_t &page_size);
218
219/** Drop any adaptive hash index entries for a table.
220@param[in,out] table to drop indexes of this table */
222
223/** Drop any adaptive hash index entries for a index.
224@param[in,out] index to drop hash indexes for this index */
225void btr_drop_ahi_for_index(const dict_index_t *index);
226
227/** Updates the page hash index when a single record is inserted on a page.
228@param[in] cursor cursor which was positioned to the place to insert using
229 btr_cur_search_, and the new record has been inserted
230 next to the cursor. */
232
233/** Updates the page hash index when a single record is inserted on a page.
234@param[in,out] cursor cursor which was positioned to the place to insert using
235 btr_cur_search_..., and the new record has been inserted
236 next to the cursor. */
238
239/** Updates the page hash index when a single record is deleted from a page.
240@param[in] cursor cursor which was positioned on the record to delete
241 using btr_cur_search_, the record is not yet deleted. */
243
244/** Validates the search system.
245@return true if ok */
247
248/** X-Lock the search latch (corresponding to given index)
249@param[in] index index handler
250@param[in] location source location */
251static inline void btr_search_x_lock(const dict_index_t *index,
252 ut::Location location);
253
254/** X-Lock the search latch (corresponding to given index), does not block.
255@param[in] index index handler
256@param[in] location source location
257@return true if the latch could was acquired.*/
258[[nodiscard]] static inline bool btr_search_x_lock_nowait(
259 const dict_index_t *index, ut::Location location);
260
261/** X-Unlock the search latch (corresponding to given index)
262@param[in] index index handler */
263static inline void btr_search_x_unlock(const dict_index_t *index);
264
265/** Lock all search latches in exclusive mode.
266@param[in] location source location */
267static inline void btr_search_x_lock_all(ut::Location location);
268
269/** Unlock all search latches from exclusive mode. */
270static inline void btr_search_x_unlock_all();
271
272/** S-Lock the search latch (corresponding to given index)
273@param[in] index index handler
274@param[in] location source location */
275static inline void btr_search_s_lock(const dict_index_t *index,
276 ut::Location location);
277
278/** S-Lock the search latch (corresponding to given index), does not block.
279@param[in] index index handler
280@param[in] location source location
281@return true if the latch could was acquired.*/
282[[nodiscard]] static inline bool btr_search_s_lock_nowait(
283 const dict_index_t *index, ut::Location location);
284
285/** S-Unlock the search latch (corresponding to given index)
286@param[in] index index handler */
287static inline void btr_search_s_unlock(const dict_index_t *index);
288
289/** Lock all search latches in shared mode.
290@param[in] location source location */
291static inline void btr_search_s_lock_all(ut::Location location);
292
293#ifdef UNIV_DEBUG
294/** Check if thread owns all the search latches.
295@param[in] mode lock mode check
296@retval true if owns all of them
297@retval false if does not own some of them */
298[[nodiscard]] static inline bool btr_search_own_all(ulint mode);
299
300/** Check if thread owns any of the search latches.
301@param[in] mode lock mode check
302@retval true if owns any of them
303@retval false if owns no search latch */
304[[nodiscard]] static inline bool btr_search_own_any(ulint mode);
305#endif /* UNIV_DEBUG */
306
307/** Unlock all search latches from shared mode. */
308static inline void btr_search_s_unlock_all();
309
310/** Compute a hash value for a specified index.
311@param[in] index Index structure
312@return hash value of the index */
313static inline size_t btr_search_hash_index_id(const dict_index_t *index);
314
315/** Gets a pointer to a adaptive search part structure for a specified index.
316@param[in] index Index structure
317@return hash value of the index */
319 const dict_index_t *index);
320
321/** Get the latch based on index attributes.
322A latch is selected from an array of latches using pair of index-id, space-id.
323@param[in] index index handler
324@return latch */
325static inline rw_lock_t *btr_get_search_latch(const dict_index_t *index);
326
327#ifdef UNIV_SEARCH_PERF_STAT
328/** Number of successful adaptive hash index lookups */
329extern ulint btr_search_n_succ;
330/** Number of failed adaptive hash index lookups */
331extern ulint btr_search_n_hash_fail;
332#endif /* UNIV_SEARCH_PERF_STAT */
333
334/** After change in n_fields or n_bytes in info, this many rounds are waited
335before starting the hash analysis again: this is to save CPU time when there
336is no hope in building a hash index. */
337constexpr uint32_t BTR_SEARCH_HASH_ANALYSIS = 17;
338
339/** Limit of consecutive searches for trying a search shortcut on the search
340pattern */
341constexpr uint32_t BTR_SEARCH_ON_PATTERN_LIMIT = 3;
342
343/** Limit of consecutive searches for trying a search shortcut using
344the hash index */
345constexpr uint32_t BTR_SEARCH_ON_HASH_LIMIT = 3;
346
347#include "btr0sea.ic"
348
349#endif
void btr_search_drop_page_hash_index(buf_block_t *block, bool force=false)
Drop any adaptive hash index entries that point to an index page.
Definition: btr0sea.cc:1005
constexpr uint32_t BTR_SEARCH_ON_HASH_LIMIT
Limit of consecutive searches for trying a search shortcut using the hash index.
Definition: btr0sea.h:345
bool btr_search_disable()
Disable the adaptive hash search system and empty the index.
Definition: btr0sea.cc:313
static rw_lock_t * btr_get_search_latch(const dict_index_t *index)
Get the latch based on index attributes.
void btr_search_update_hash_on_move(buf_block_t *new_block, buf_block_t *block, dict_index_t *index)
Moves or deletes hash entries for moved records.
Definition: btr0sea.cc:1580
static void btr_search_s_unlock(const dict_index_t *index)
S-Unlock the search latch (corresponding to given index)
bool btr_search_validate()
Validates the search system.
Definition: btr0sea.cc:2047
static bool btr_search_s_lock_nowait(const dict_index_t *index, ut::Location location)
S-Lock the search latch (corresponding to given index), does not block.
void btr_search_update_hash_on_insert(btr_cur_t *cursor)
Updates the page hash index when a single record is inserted on a page.
Definition: btr0sea.cc:1736
static size_t btr_search_hash_index_id(const dict_index_t *index)
Compute a hash value for a specified index.
constexpr uint32_t BTR_SEARCH_MAGIC_N
value of btr_search_t::magic_n, used in assertions
Definition: btr0sea.h:88
static void btr_search_x_unlock_all()
Unlock all search latches from exclusive mode.
void btr_search_sys_free()
Frees the adaptive search system at a database shutdown.
Definition: btr0sea.cc:255
void btr_search_update_hash_on_delete(btr_cur_t *cursor)
Updates the page hash index when a single record is deleted from a page.
Definition: btr0sea.cc:1631
static void btr_search_x_lock(const dict_index_t *index, ut::Location location)
X-Lock the search latch (corresponding to given index)
static class btr_search_sys_t::search_part_t & btr_get_search_part(const dict_index_t *index)
Gets a pointer to a adaptive search part structure for a specified index.
static void btr_search_s_unlock_all()
Unlock all search latches from shared mode.
btr_search_t * btr_search_info_create(mem_heap_t *heap)
Creates and initializes a search info struct.
Definition: btr0sea.cc:373
static bool btr_search_x_lock_nowait(const dict_index_t *index, ut::Location location)
X-Lock the search latch (corresponding to given index), does not block.
void btr_search_await_no_reference(dict_table_t *table, dict_index_t *index, bool force)
Wait for the specified index to have all references from AHI dropped.
Definition: btr0sea.cc:272
void btr_search_set_block_not_cached(buf_block_t *block)
Resets block's AHI index field to nullptr, removes the reference from index's reference counter.
Definition: btr0sea.cc:1225
static bool btr_search_own_any(ulint mode)
Check if thread owns any of the search latches.
static void btr_search_x_lock_all(ut::Location location)
Lock all search latches in exclusive mode.
constexpr uint32_t BTR_SEARCH_ON_PATTERN_LIMIT
Limit of consecutive searches for trying a search shortcut on the search pattern.
Definition: btr0sea.h:341
void btr_search_sys_resize(ulint hash_size)
Resize hash index hash table.
Definition: btr0sea.cc:222
void btr_search_sys_create(ulint hash_size)
Creates and initializes the adaptive search system at a database start.
Definition: btr0sea.cc:185
static bool btr_search_own_all(ulint mode)
Check if thread owns all the search latches.
void btr_drop_ahi_for_index(const dict_index_t *index)
Drop any adaptive hash index entries for a index.
Definition: btr0sea.cc:1383
void btr_drop_ahi_for_table(dict_table_t *table)
Drop any adaptive hash index entries for a table.
Definition: btr0sea.cc:1346
btr_search_sys_t * btr_search_sys
The adaptive hash index.
Definition: btr0sea.cc:84
bool btr_search_guess_on_hash(const dtuple_t *tuple, ulint mode, ulint latch_mode, btr_cur_t *cursor, ulint has_search_latch, mtr_t *mtr)
Tries to guess the right search position based on the hash search info of the index.
Definition: btr0sea.cc:804
constexpr uint32_t BTR_SEARCH_HASH_ANALYSIS
After change in n_fields or n_bytes in info, this many rounds are waited before starting the hash ana...
Definition: btr0sea.h:337
void btr_search_drop_page_hash_when_freed(const page_id_t &page_id, const page_size_t &page_size)
Drop any adaptive hash index entries that may point to an index page that may be in the buffer pool,...
Definition: btr0sea.cc:1258
static void btr_search_info_update(btr_cur_t *cursor)
Updates the search info statistics following a search in B-tree that was performed not using or not f...
static void btr_search_s_lock_all(ut::Location location)
Lock all search latches in shared mode.
void btr_search_enable()
Enable the adaptive hash search system.
Definition: btr0sea.cc:358
void btr_search_update_hash_node_on_insert(btr_cur_t *cursor)
Updates the page hash index when a single record is inserted on a page.
Definition: btr0sea.cc:1686
static void btr_search_s_lock(const dict_index_t *index, ut::Location location)
S-Lock the search latch (corresponding to given index)
static void btr_search_x_unlock(const dict_index_t *index)
X-Unlock the search latch (corresponding to given index)
The index tree adaptive search.
The index tree general types.
Definition: btr0sea.h:96
std::atomic< buf_block_t * > free_block_for_heap
A pointer to a free block that the heap in the hash table may use for adding new hash nodes.
Definition: btr0sea.h:114
hash_table_t * hash_table
The adaptive hash table, mapping dtuple_hash values to rec_t pointers on index pages.
Definition: btr0sea.h:109
rw_lock_t latch
The latch protecting the adaptive search part: this latch protects the (1) positions of records on th...
Definition: btr0sea.h:104
void initialize(size_t hash_size)
Definition: btr0sea.cc:208
The hash index system.
Definition: btr0sea.h:92
btr_search_sys_t(size_t hash_size)
Definition: btr0sea.cc:194
ut::unique_ptr_aligned< search_part_t[]> parts
Partitions of the AHI system.
Definition: btr0sea.h:118
Definition: hash0hash.h:373
Page identifier.
Definition: buf0types.h:206
Page size descriptor.
Definition: page0size.h:49
Data dictionary system.
The hash table with external chains.
Mini-transaction buffer.
static PFS_engine_table_share_proxy table
Definition: pfs.cc:60
mode
Definition: file_handle.h:59
constexpr size_t INNODB_CACHE_LINE_SIZE
CPU cache line size.
Definition: ut0cpu_cache.h:40
std::conditional_t< !std::is_array< T >::value, std::unique_ptr< T, detail::Aligned_deleter< T > >, std::conditional_t< detail::is_unbounded_array_v< T >, std::unique_ptr< T, detail::Aligned_array_deleter< std::remove_extent_t< T > > >, void > > unique_ptr_aligned
The following is a common type that is returned by all the ut::make_unique_aligned (non-aligned) spec...
Definition: ut0new.h:2571
Record manager.
The tree cursor: the definition appears here only for the compiler to know struct size!
Definition: btr0cur.h:667
The search info struct in an index.
Definition: btr0sea.h:45
std::atomic< btr_search_prefix_info_t > prefix_info
Definition: btr0sea.h:68
bool last_hash_succ
true if the last search would have succeeded, or did succeed, using the hash index; NOTE that the val...
Definition: btr0sea.h:62
ulint magic_n
magic number
Definition: btr0sea.h:69
std::atomic< uint64_t > hash_analysis
when this exceeds BTR_SEARCH_HASH_ANALYSIS, the hash analysis starts; this is reset if no success not...
Definition: btr0sea.h:58
buf_block_t * root_guess
the root page frame when it was last time fetched, or NULL.
Definition: btr0sea.h:55
std::atomic< uint64_t > n_hash_potential
number of consecutive searches which would have succeeded, or did succeed, using the hash index; the ...
Definition: btr0sea.h:65
std::atomic< size_t > ref_count
Number of blocks in this index tree that have search index built i.e.
Definition: btr0sea.h:48
The buffer control block structure.
Definition: buf0buf.h:1750
Data structure for an index.
Definition: dict0mem.h:1045
Data structure for a database table.
Definition: dict0mem.h:1908
Structure for an SQL data tuple of fields (logical record)
Definition: data0data.h:681
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:301
Mini-transaction handle and buffer.
Definition: mtr0mtr.h:176
The structure used in the spin lock implementation of a read-write lock.
Definition: sync0rw.h:362
Definition: ut0core.h:35
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:405