1 /*****************************************************************************
2 
3 Copyright (c) 2009, 2018, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2017, 2020, 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/dict0stats.h
22 Code used for calculating and manipulating table statistics.
23 
24 Created Jan 06, 2010 Vasil Dimov
25 *******************************************************/
26 
27 #ifndef dict0stats_h
28 #define dict0stats_h
29 
30 #include "dict0types.h"
31 #include "trx0types.h"
32 
33 #define TABLE_STATS_NAME        "mysql/innodb_table_stats"
34 #define INDEX_STATS_NAME        "mysql/innodb_index_stats"
35 
36 enum dict_stats_upd_option_t {
37 	DICT_STATS_RECALC_PERSISTENT,/* (re) calculate the
38 				statistics using a precise and slow
39 				algo and save them to the persistent
40 				storage, if the persistent storage is
41 				not present then emit a warning and
42 				fall back to transient stats */
43 	DICT_STATS_RECALC_TRANSIENT,/* (re) calculate the statistics
44 				using an imprecise quick algo
45 				without saving the results
46 				persistently */
47 	DICT_STATS_EMPTY_TABLE,	/* Write all zeros (or 1 where it makes sense)
48 				into a table and its indexes' statistics
49 				members. The resulting stats correspond to an
50 				empty table. If the table is using persistent
51 				statistics, then they are saved on disk. */
52 	DICT_STATS_FETCH_ONLY_IF_NOT_IN_MEMORY /* fetch the stats
53 				from the persistent storage if the in-memory
54 				structures have not been initialized yet,
55 				otherwise do nothing */
56 };
57 
58 /*********************************************************************//**
59 Set the persistent statistics flag for a given table. This is set only
60 in the in-memory table object and is not saved on disk. It will be read
61 from the .frm file upon first open from MySQL after a server restart. */
62 UNIV_INLINE
63 void
64 dict_stats_set_persistent(
65 /*======================*/
66 	dict_table_t*	table,	/*!< in/out: table */
67 	ibool		ps_on,	/*!< in: persistent stats explicitly enabled */
68 	ibool		ps_off)	/*!< in: persistent stats explicitly disabled */
69 	MY_ATTRIBUTE((nonnull));
70 
71 /** @return whether persistent statistics is enabled for a given table */
72 UNIV_INLINE
73 bool
74 dict_stats_is_persistent_enabled(const dict_table_t* table)
75 	MY_ATTRIBUTE((nonnull, warn_unused_result));
76 
77 /*********************************************************************//**
78 Set the auto recalc flag for a given table (only honored for a persistent
79 stats enabled table). The flag is set only in the in-memory table object
80 and is not saved in InnoDB files. It will be read from the .frm file upon
81 first open from MySQL after a server restart. */
82 UNIV_INLINE
83 void
84 dict_stats_auto_recalc_set(
85 /*=======================*/
86 	dict_table_t*	table,			/*!< in/out: table */
87 	ibool		auto_recalc_on,		/*!< in: explicitly enabled */
88 	ibool		auto_recalc_off);	/*!< in: explicitly disabled */
89 
90 /** @return whether auto recalc is enabled for a given table*/
91 UNIV_INLINE
92 bool
93 dict_stats_auto_recalc_is_enabled(const dict_table_t* table)
94 	MY_ATTRIBUTE((nonnull, warn_unused_result));
95 
96 /*********************************************************************//**
97 Initialize table's stats for the first time when opening a table. */
98 UNIV_INLINE
99 void
100 dict_stats_init(
101 /*============*/
102 	dict_table_t*	table);	/*!< in/out: table */
103 
104 /*********************************************************************//**
105 Deinitialize table's stats after the last close of the table. This is
106 used to detect "FLUSH TABLE" and refresh the stats upon next open. */
107 UNIV_INLINE
108 void
109 dict_stats_deinit(
110 /*==============*/
111 	dict_table_t*	table)	/*!< in/out: table */
112 	MY_ATTRIBUTE((nonnull));
113 
114 #ifdef WITH_WSREP
115 /** Update the table modification counter and if necessary,
116 schedule new estimates for table and index statistics to be calculated.
117 @param[in,out]	table	persistent or temporary table
118 @param[in]	trx	transaction */
119 void dict_stats_update_if_needed(dict_table_t *table, const trx_t &trx)
120 	MY_ATTRIBUTE((nonnull));
121 #else
122 /** Update the table modification counter and if necessary,
123 schedule new estimates for table and index statistics to be calculated.
124 @param[in,out]	table	persistent or temporary table */
125 void dict_stats_update_if_needed_func(dict_table_t *table)
126 	MY_ATTRIBUTE((nonnull));
127 # define dict_stats_update_if_needed(t,trx) dict_stats_update_if_needed_func(t)
128 #endif
129 
130 /*********************************************************************//**
131 Calculates new estimates for table and index statistics. The statistics
132 are used in query optimization.
133 @return DB_* error code or DB_SUCCESS */
134 dberr_t
135 dict_stats_update(
136 /*==============*/
137 	dict_table_t*		table,	/*!< in/out: table */
138 	dict_stats_upd_option_t	stats_upd_option);
139 					/*!< in: whether to (re) calc
140 					the stats or to fetch them from
141 					the persistent storage */
142 
143 /*********************************************************************//**
144 Removes the information for a particular index's stats from the persistent
145 storage if it exists and if there is data stored for this index.
146 This function creates its own trx and commits it.
147 @return DB_SUCCESS or error code */
148 dberr_t
149 dict_stats_drop_index(
150 /*==================*/
151 	const char*	tname,	/*!< in: table name */
152 	const char*	iname,	/*!< in: index name */
153 	char*		errstr, /*!< out: error message if != DB_SUCCESS
154 				is returned */
155 	ulint		errstr_sz);/*!< in: size of the errstr buffer */
156 
157 /*********************************************************************//**
158 Removes the statistics for a table and all of its indexes from the
159 persistent storage if it exists and if there is data stored for the table.
160 This function creates its own transaction and commits it.
161 @return DB_SUCCESS or error code */
162 dberr_t
163 dict_stats_drop_table(
164 /*==================*/
165 	const char*	table_name,	/*!< in: table name */
166 	char*		errstr,		/*!< out: error message
167 					if != DB_SUCCESS is returned */
168 	ulint		errstr_sz);	/*!< in: size of errstr buffer */
169 
170 /*********************************************************************//**
171 Fetches or calculates new estimates for index statistics. */
172 void
173 dict_stats_update_for_index(
174 /*========================*/
175 	dict_index_t*	index)	/*!< in/out: index */
176 	MY_ATTRIBUTE((nonnull));
177 
178 /*********************************************************************//**
179 Renames a table in InnoDB persistent stats storage.
180 This function creates its own transaction and commits it.
181 @return DB_SUCCESS or error code */
182 dberr_t
183 dict_stats_rename_table(
184 /*====================*/
185 	const char*	old_name,	/*!< in: old table name */
186 	const char*	new_name,	/*!< in: new table name */
187 	char*		errstr,		/*!< out: error string if != DB_SUCCESS
188 					is returned */
189 	size_t		errstr_sz);	/*!< in: errstr size */
190 
191 /** Save an individual index's statistic into the persistent statistics
192 storage.
193 @param[in]	index			index to be updated
194 @param[in]	last_update		timestamp of the stat
195 @param[in]	stat_name		name of the stat
196 @param[in]	stat_value		value of the stat
197 @param[in]	sample_size		n pages sampled or NULL
198 @param[in]	stat_description	description of the stat
199 @param[in,out]	trx			in case of NULL the function will
200 allocate and free the trx object. If it is not NULL then it will be
201 rolled back only in the case of error, but not freed.
202 @return DB_SUCCESS or error code */
203 dberr_t
204 dict_stats_save_index_stat(
205 	dict_index_t*	index,
206 	time_t		last_update,
207 	const char*	stat_name,
208 	ib_uint64_t	stat_value,
209 	ib_uint64_t*	sample_size,
210 	const char*	stat_description,
211 	trx_t*		trx);
212 
213 /** Report an error if updating table statistics failed because
214 .ibd file is missing, table decryption failed or table is corrupted.
215 @param[in,out]	table	Table
216 @param[in]	defragment	true if statistics is for defragment
217 @retval DB_DECRYPTION_FAILED if decryption of the table failed
218 @retval DB_TABLESPACE_DELETED if .ibd file is missing
219 @retval DB_CORRUPTION if table is marked as corrupted */
220 dberr_t
221 dict_stats_report_error(dict_table_t* table, bool defragment = false)
222 	MY_ATTRIBUTE((nonnull, warn_unused_result));
223 
224 #include "dict0stats.inl"
225 
226 #ifdef UNIV_ENABLE_UNIT_TEST_DICT_STATS
227 void test_dict_stats_all();
228 #endif /* UNIV_ENABLE_UNIT_TEST_DICT_STATS */
229 
230 #endif /* dict0stats_h */
231