Debian Codegraph @

storage/innobase/include/dict0stats.ic

raw
1 /*****************************************************************************
2
3 Copyright (c) 2012, 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/dict0stats.ic
29 Code used for calculating and manipulating table statistics.
30
31 Created Jan 23, 2012 Vasil Dimov
32 *******************************************************/
33
34 #include "dict0dict.h"
35 #include "dict0types.h"
36 #include "srv0srv.h"
37
38 /** Set the persistent statistics flag for a given table. This is set only
39 in the in-memory table object and is not saved on disk. It will be read
40 from the .frm file upon first open from MySQL after a server restart. */
41 static inline void dict_stats_set_persistent(
42 dict_table_t *table, /*!< in/out: table */
43 bool ps_on, /*!< in: persistent stats explicitly enabled */
44 bool ps_off) /*!< in: persistent stats explicitly disabled */
45 {
46 /* Not allowed to have both flags set, but a CREATE or ALTER
47 statement that contains "STATS_PERSISTENT=0 STATS_PERSISTENT=1" would
48 end up having both set. In this case we clear the OFF flag. */
49 if (ps_on && ps_off) {
50 ps_off = false;
51 }
52
53 uint32_t stat_persistent = 0;
54
55 if (ps_on) {
56 stat_persistent |= DICT_STATS_PERSISTENT_ON;
57 }
58
59 if (ps_off) {
60 stat_persistent |= DICT_STATS_PERSISTENT_OFF;
61 }
62
63 /* we rely on this assignment to be atomic */
64 table->stat_persistent = stat_persistent;
65 }
66
67 /** Check whether persistent statistics is enabled for a given table.
68 @return true if enabled, false otherwise */
69 static inline bool dict_stats_is_persistent_enabled(
70 const dict_table_t *table) /*!< in: table */
71 {
72 /* Because of the nature of this check (non-locking) it is possible
73 that a table becomes:
74 * PS-disabled immediately after this function has returned true or
75 * PS-enabled immediately after this function has returned false.
76 This means that it is possible that we do:
77 + dict_stats_update(DICT_STATS_RECALC_PERSISTENT) on a table that has
78 just been PS-disabled or
79 + dict_stats_update(DICT_STATS_RECALC_TRANSIENT) on a table that has
80 just been PS-enabled.
81 This is acceptable. Avoiding this would mean that we would have to
82 protect the ::stat_persistent with dict_table_stats_lock() like the
83 other ::stat_ members which would be too big performance penalty,
84 especially when this function is called from
85 row_update_statistics_if_needed(). */
86
87 /* we rely on this read to be atomic */
88 uint32_t stat_persistent = table->stat_persistent;
89
90 if (stat_persistent & DICT_STATS_PERSISTENT_ON) {
91 ut_ad(!(stat_persistent & DICT_STATS_PERSISTENT_OFF));
92 return true;
93 } else if (stat_persistent & DICT_STATS_PERSISTENT_OFF) {
94 return false;
95 } else {
96 return (srv_stats_persistent);
97 }
98 }
99
100 /** Set the auto recalc flag for a given table (only honored for a persistent
101 stats enabled table). The flag is set only in the in-memory table object
102 and is not saved in InnoDB files. It will be read from the .frm file upon
103 first open from MySQL after a server restart. */
104 static inline void dict_stats_auto_recalc_set(
105 dict_table_t *table, /*!< in/out: table */
106 bool auto_recalc_on, /*!< in: explicitly enabled */
107 bool auto_recalc_off) /*!< in: explicitly disabled */
108 {
109 ut_ad(!auto_recalc_on || !auto_recalc_off);
110
111 uint32_t stats_auto_recalc = 0;
112
113 if (auto_recalc_on) {
114 stats_auto_recalc |= DICT_STATS_AUTO_RECALC_ON;
115 }
116
117 if (auto_recalc_off) {
118 stats_auto_recalc |= DICT_STATS_AUTO_RECALC_OFF;
119 }
120
121 /* we rely on this assignment to be atomic */
122 table->stats_auto_recalc = stats_auto_recalc;
123 }
124
125 /** Check whether auto recalc is enabled for a given table.
126 @return true if enabled, false otherwise */
127 static inline bool dict_stats_auto_recalc_is_enabled(
128 const dict_table_t *table) /*!< in: table */
129 {
130 /* we rely on this read to be atomic */
131 uint32_t stats_auto_recalc = table->stats_auto_recalc;
132
133 if (stats_auto_recalc & DICT_STATS_AUTO_RECALC_ON) {
134 ut_ad(!(stats_auto_recalc & DICT_STATS_AUTO_RECALC_OFF));
135 return true;
136 } else if (stats_auto_recalc & DICT_STATS_AUTO_RECALC_OFF) {
137 return false;
138 } else {
139 return (srv_stats_auto_recalc);
140 }
141 }
142
143 /** Initialize table's stats for the first time when opening a table. */
144 static inline void dict_stats_init(dict_table_t *table) /*!< in/out: table */
145 {
146 ut_ad(!dict_sys_mutex_own());
147
148 if (table->stat_initialized) {
149 return;
150 }
151
152 dict_stats_upd_option_t opt;
153
154 if (dict_stats_is_persistent_enabled(table)) {
155 opt = DICT_STATS_FETCH_ONLY_IF_NOT_IN_MEMORY;
156 } else {
157 opt = DICT_STATS_RECALC_TRANSIENT;
158 }
159
160 dict_stats_update(table, opt);
161 }
162
163 /** Deinitialize table's stats after the last close of the table. This is
164 used to detect "FLUSH TABLE" and refresh the stats upon next open. */
165 static inline void dict_stats_deinit(dict_table_t *table) /*!< in/out: table */
166 {
167 /* Don't assert the table->n_ref_count is 0 here, since there could
168 be some background threads opening the table concurrently. This is
169 not prevented. The cases prevented are in opening table code paths,
170 to acquire an existing table, table->mutex is required */
171
172 dict_table_stats_lock(table, RW_X_LATCH);
173
174 if (!table->stat_initialized) {
175 dict_table_stats_unlock(table, RW_X_LATCH);
176 return;
177 }
178
179 table->stat_initialized = false;
180
181 #ifdef UNIV_DEBUG_VALGRIND
182 UNIV_MEM_INVALID(&table->stat_n_rows, sizeof(table->stat_n_rows));
183 UNIV_MEM_INVALID(&table->stat_clustered_index_size,
184 sizeof(table->stat_clustered_index_size));
185 UNIV_MEM_INVALID(&table->stat_sum_of_other_index_sizes,
186 sizeof(table->stat_sum_of_other_index_sizes));
187 UNIV_MEM_INVALID(&table->stat_modified_counter,
188 sizeof(table->stat_modified_counter));
189
190 dict_index_t *index;
191
192 for (index = table->first_index(); index != NULL; index = index->next()) {
193 ulint n_uniq = dict_index_get_n_unique(index);
194
195 UNIV_MEM_INVALID(index->stat_n_diff_key_vals,
196 n_uniq * sizeof(index->stat_n_diff_key_vals[0]));
197 UNIV_MEM_INVALID(index->stat_n_sample_sizes,
198 n_uniq * sizeof(index->stat_n_sample_sizes[0]));
199 UNIV_MEM_INVALID(index->stat_n_non_null_key_vals,
200 n_uniq * sizeof(index->stat_n_non_null_key_vals[0]));
201 UNIV_MEM_INVALID(&index->stat_index_size, sizeof(index->stat_index_size));
202 UNIV_MEM_INVALID(&index->stat_n_leaf_pages,
203 sizeof(index->stat_n_leaf_pages));
204 }
205 #endif /* UNIV_DEBUG_VALGRIND */
206
207 dict_table_stats_unlock(table, RW_X_LATCH);
208 }