MySQL 9.1.0
Source Code Documentation
table_stats.h
Go to the documentation of this file.
1/* Copyright (c) 2016, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef DD_INFO_SCHEMA_TABLE_STATS_INCLUDED
25#define DD_INFO_SCHEMA_TABLE_STATS_INCLUDED
26
27#include <sys/types.h>
28#include <string>
29
30#include "sql/dd/object_id.h" // Object_id
31#include "sql/dd/string_type.h" // dd::String_type
32#include "sql/handler.h" // ha_statistics
33#include "sql_string.h" // String
34
35class THD;
36class Table_ref;
37
38namespace dd {
39namespace info_schema {
40
41/**
42 Get dynamic table statistics of a table and store them into
43 mysql.table_stats.
44
45 @param thd Thread.
46 @param table Table_ref pointing to table info.
47
48 @returns false on success.
49 true on failure.
50*/
52
53/**
54 Get dynamic index statistics of a table and store them into
55 mysql.index_stats.
56
57 @param thd Thread.
58 @param table Table_ref pointing to table info.
59
60 @returns false on success.
61 true on failure.
62*/
64
65/**
66 If the db is 'information_schema' then convert 'db' to
67 lowercase and 'table_name' to upper case. Mainly because all
68 information schema tables are stored in upper case in server.
69
70 @param db Database name
71 @param table_name Table name.
72
73 @returns true if the conversion was done.
74 false if not.
75*/
76bool convert_table_name_case(char *db, char *table_name);
77
78// Statistics that are cached.
91};
92
93/**
94 The class hold dynamic table statistics for a table.
95 This cache is used by internal UDF's defined for the purpose
96 of INFORMATION_SCHEMA queries which retrieve dynamic table
97 statistics. The class caches statistics for just one table.
98
99 Overall aim of introducing this cache is to avoid making
100 multiple calls to same SE API to retrieve the statistics.
101*/
102
104 public:
106
107 /**
108 Check if the stats are cached for given db.table_name.
109
110 @param db_name - Schema name.
111 @param table_name - Table name.
112 @param partition_name - Partition name.
113
114 @return true if stats are cached, else false.
115 */
117 const char *partition_name) {
118 return (m_key == form_key(db_name, table_name, partition_name));
119 }
120
122 return is_stat_cached_in_mem(db_name, table_name, nullptr);
123 }
124
125 /**
126 Store the statistics form the given handler
127
128 @param db_name - Schema name.
129 @param table_name - Table name.
130 @param partition_name - Partition name.
131 @param file - Handler object for the table.
132 */
134 const char *partition_name, handler *file) {
135 m_stats = file->stats;
136 m_checksum = file->checksum();
137 m_error.clear();
138 set_stat_cached(db_name, table_name, partition_name);
139 }
140
142 handler *file) {
144 }
145
146 /**
147 Store the statistics
148
149 @param db_name - Schema name.
150 @param table_name - Table name.
151 @param partition_name - Partition name.
152 @param stats - ha_statistics of the table.
153 */
155 const char *partition_name, ha_statistics &stats) {
156 m_stats = stats;
157 m_checksum = 0;
158 m_error.clear();
159 set_stat_cached(db_name, table_name, partition_name);
160 }
161
165 }
166
167 /**
168 @brief
169 Read dynamic table/index statistics from SE by opening the user table
170 provided OR by reading cached statistics from Query_block.
171
172 @param thd - Current thread.
173 @param schema_name_ptr - Schema name of table.
174 @param table_name_ptr - Table name of which we need stats.
175 @param index_name_ptr - Index name of which we need stats.
176 @param partition_name - Partition name.
177 @param column_name_ptr - Column name for index.
178 @param index_ordinal_position - Ordinal position of index in table.
179 @param column_ordinal_position - Ordinal position of column in table.
180 @param engine_name_ptr - Engine of the table.
181 @param se_private_id - se_private_id of the table.
182 @param ts_se_private_data - Tablespace SE private data.
183 @param tbl_se_private_data - Table SE private data.
184 @param table_stat_data - Cached data from mysql.table_stats /
185 mysql.index_stats table
186 @param cached_time - Timestamp value when data was cached.
187 @param stype - Enum specifying the stat we are
188 interested to read.
189
190 @return ulonglong representing value for the status being read.
191 */
193 THD *thd, const String &schema_name_ptr, const String &table_name_ptr,
194 const String &index_name_ptr, const char *partition_name,
195 const String &column_name_ptr, uint index_ordinal_position,
196 uint column_ordinal_position, const String &engine_name_ptr,
197 dd::Object_id se_private_id, const char *ts_se_private_data,
198 const char *tbl_se_private_data, const ulonglong &table_stat_data,
199 const ulonglong &cached_time, enum_table_stats_type stype);
200
201 // Fetch table stats. Invokes the above method.
203 THD *thd, const String &schema_name_ptr, const String &table_name_ptr,
204 const String &engine_name_ptr, const char *partition_name,
205 dd::Object_id se_private_id, const char *ts_se_private_data,
206 const char *tbl_se_private_data, const ulonglong &table_stat_data,
207 const ulonglong &cached_time, enum_table_stats_type stype) {
208 const String tmp;
209 return read_stat(thd, schema_name_ptr, table_name_ptr, tmp, partition_name,
210 tmp, 0, 0, engine_name_ptr, se_private_id,
211 ts_se_private_data, tbl_se_private_data, table_stat_data,
212 cached_time, stype);
213 }
214
215 // Invalidate the cache.
216 void invalidate_cache(void) {
217 m_key.clear();
218 m_error.clear();
219 }
220
221 // Get error string. Its empty if a error is not reported.
222 inline String_type error() { return m_error; }
223
224 /**
225 Set error string for the given key. The combination of (db, table and
226 partition name) forms the key.
227
228 @param db_name - Schema name.
229 @param table_name - Table name.
230 @param partition_name - Partition name.
231 @param error_msg - Error message.
232
233 @note We store the error message so that the error message is shown in
234 I_S.TABLES.COMMENT field. Apart from storing the error message, the
235 below function resets the statistics, this will make sure,
236
237 1. We do not invoke open_tables_for_query() again for other
238 dynamic columns that are fetch from the current row being
239 processed.
240
241 2. We will not see junk values for statistics in results.
242 */
244 const char *partition_name,
245 const String_type &error_msg) {
246 m_stats = {};
247 m_checksum = 0;
248 m_error = error_msg;
249 m_key = form_key(db_name, table_name, partition_name);
250 }
251
252 /**
253 Check if we have seen a error.
254
255 @param db_name Database name.
256 @param table_name Table name.
257
258 @returns true if there is error reported.
259 false if not.
260 */
262 const String &table_name) {
264 return true;
265
266 return false;
267 }
268
269 /// Check if open table in progress.
271
272 private:
273 /**
274 Read dynamic table/index statistics from SE API's OR by reading
275 cached statistics from Query_block.
276
277 @param thd - Current thread.
278 @param schema_name_ptr - Schema name of table.
279 @param table_name_ptr - Table name of which we need stats.
280 @param index_name_ptr - Index name of which we need stats.
281 @param column_name_ptr - Column name for index.
282 @param index_ordinal_position - Ordinal position of index in table.
283 @param column_ordinal_position - Ordinal position of column in table.
284 @param se_private_id - se_private_id of the table.
285 @param ts_se_private_data - Tablespace SE private data.
286 @param tbl_se_private_data - Table SE private data.
287 @param stype - Enum specifying the stat we are
288 interested to read.
289 @param hton - Handle to SE for the given table.
290
291 @return ulonglong representing value for the status being read.
292 */
294 THD *thd, const String &schema_name_ptr, const String &table_name_ptr,
295 const String &index_name_ptr, const String &column_name_ptr,
296 uint index_ordinal_position, uint column_ordinal_position,
297 dd::Object_id se_private_id, const char *ts_se_private_data,
298 const char *tbl_se_private_data, enum_table_stats_type stype,
299 handlerton *hton);
300
301 /**
302 Read dynamic table/index statistics by opening the table OR by reading
303 cached statistics from Query_block.
304
305 @param thd - Current thread.
306 @param schema_name_ptr - Schema name of table.
307 @param table_name_ptr - Table name of which we need stats.
308 @param index_name_ptr - Index name of which we need stats.
309 @param column_name_ptr - Column name for index.
310 @param column_ordinal_position - Ordinal position of column in table.
311 @param partition_name - Partition name.
312 @param stype - Enum specifying the stat we are
313 interested to read.
314
315 @return ulonglong representing value for the status being read.
316 */
317 ulonglong read_stat_by_open_table(THD *thd, const String &schema_name_ptr,
318 const String &table_name_ptr,
319 const String &index_name_ptr,
320 const char *partition_name,
321 const String &column_name_ptr,
322 uint column_ordinal_position,
324
325 /**
326 Mark the cache as valid for a given table. This creates a key for the
327 cache element. We store just a single table statistics in this cache.
328
329 @param db_name - Database name.
330 @param table_name - Table name.
331 @param partition_name - Partition name.
332 */
334 const char *partition_name) {
335 m_key = form_key(db_name, table_name, partition_name);
336 }
337
340 }
341
342 /**
343 Build a key representing the table for which stats are cached.
344
345 @param db_name - Database name.
346 @param table_name - Table name.
347 @param partition_name - Partition name.
348
349 @returns String_type representing the key.
350 */
352 const char *partition_name) {
353 return String_type(db_name.ptr()) + "." + String_type(table_name.ptr()) +
354 (partition_name ? ("." + String_type(partition_name)) : "");
355 }
356
357 /**
358 Return statistics of the a given type.
359
360 @param stat ha_statistics for the current cached table.
361 @param stype Type of statistics requested.
362
363 @returns ulonglong statistics value.
364 */
367 return get_stat(m_stats, stype);
368 }
369
370 /// Set checksum
371 void set_checksum(ulonglong &&checksum) { m_checksum = checksum; }
372
373 /// Get checksum
375
376 /// Set open table in progress.
378
379 public:
380 /// Predicate for determinig if cache is valid
381 bool is_valid() const { return !m_key.empty(); }
382
383 private:
384 // The cache key
385 String_type m_key; // Format '<db_name>.<table_name>'
386
387 // Error found when reading statistics.
389
390 // Table checksum value retrieved from SE.
392
393 /*
394 Status if opening a table is in progress to read statistics.
395
396 This is used by heap table, to avoid write a command "DELETE FROM
397 TABLE" to binlog just after server restart. See open_table_entry_fini()
398 for more info.
399 */
401
402 public:
403 // Cached statistics.
405};
406
407} // namespace info_schema
408} // namespace dd
409
410#endif // DD_INFO_SCHEMA_TABLE_STATS_INCLUDED
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: table.h:2900
The class hold dynamic table statistics for a table.
Definition: table_stats.h:103
String_type m_error
Definition: table_stats.h:388
String_type m_key
Definition: table_stats.h:385
void cache_stats_in_mem(const String &db_name, const String &table_name, handler *file)
Definition: table_stats.h:141
void store_error_message(const String &db_name, const String &table_name, const char *partition_name, const String_type &error_msg)
Set error string for the given key.
Definition: table_stats.h:243
bool is_valid() const
Predicate for determinig if cache is valid.
Definition: table_stats.h:381
bool m_read_stats_by_open
Definition: table_stats.h:400
ha_statistics m_stats
Definition: table_stats.h:404
void set_stat_cached(const String &db_name, const String &table_name, const char *partition_name)
Mark the cache as valid for a given table.
Definition: table_stats.h:333
bool is_stat_cached_in_mem(const String &db_name, const String &table_name)
Definition: table_stats.h:121
void cache_stats_in_mem(const String &db_name, const String &table_name, ha_statistics &stats)
Definition: table_stats.h:162
ulonglong read_stat_by_open_table(THD *thd, const String &schema_name_ptr, const String &table_name_ptr, const String &index_name_ptr, const char *partition_name, const String &column_name_ptr, uint column_ordinal_position, enum_table_stats_type stype)
Read dynamic table/index statistics by opening the table OR by reading cached statistics from Query_b...
Definition: table_stats.cc:666
ulonglong read_stat(THD *thd, const String &schema_name_ptr, const String &table_name_ptr, const String &engine_name_ptr, const char *partition_name, dd::Object_id se_private_id, const char *ts_se_private_data, const char *tbl_se_private_data, const ulonglong &table_stat_data, const ulonglong &cached_time, enum_table_stats_type stype)
Definition: table_stats.h:202
void set_checksum(ulonglong &&checksum)
Set checksum.
Definition: table_stats.h:371
ulonglong get_stat(enum_table_stats_type stype)
Definition: table_stats.h:366
void cache_stats_in_mem(const String &db_name, const String &table_name, const char *partition_name, handler *file)
Store the statistics form the given handler.
Definition: table_stats.h:133
void invalidate_cache(void)
Definition: table_stats.h:216
String_type error()
Definition: table_stats.h:222
ulonglong read_stat(THD *thd, const String &schema_name_ptr, const String &table_name_ptr, const String &index_name_ptr, const char *partition_name, const String &column_name_ptr, uint index_ordinal_position, uint column_ordinal_position, const String &engine_name_ptr, dd::Object_id se_private_id, const char *ts_se_private_data, const char *tbl_se_private_data, const ulonglong &table_stat_data, const ulonglong &cached_time, enum_table_stats_type stype)
Read dynamic table/index statistics from SE by opening the user table provided OR by reading cached s...
Definition: table_stats.cc:444
bool is_stat_cached_in_mem(const String &db_name, const String &table_name, const char *partition_name)
Check if the stats are cached for given db.table_name.
Definition: table_stats.h:116
ulonglong get_stat(ha_statistics &stat, enum_table_stats_type stype)
Return statistics of the a given type.
Definition: table_stats.cc:402
Table_statistics()
Definition: table_stats.h:105
ulonglong m_checksum
Definition: table_stats.h:391
void set_read_stats_by_open(bool status)
Set open table in progress.
Definition: table_stats.h:377
ulonglong read_stat_from_SE(THD *thd, const String &schema_name_ptr, const String &table_name_ptr, const String &index_name_ptr, const String &column_name_ptr, uint index_ordinal_position, uint column_ordinal_position, dd::Object_id se_private_id, const char *ts_se_private_data, const char *tbl_se_private_data, enum_table_stats_type stype, handlerton *hton)
Read dynamic table/index statistics from SE API's OR by reading cached statistics from Query_block.
Definition: table_stats.cc:531
bool check_error_for_key(const String &db_name, const String &table_name)
Check if we have seen a error.
Definition: table_stats.h:261
ulonglong get_checksum() const
Get checksum.
Definition: table_stats.h:374
String_type form_key(const String &db_name, const String &table_name, const char *partition_name)
Build a key representing the table for which stats are cached.
Definition: table_stats.h:351
void set_stat_cached(const String &db_name, const String &table_name)
Definition: table_stats.h:338
bool is_reading_stats_by_open() const
Check if open table in progress.
Definition: table_stats.h:270
void cache_stats_in_mem(const String &db_name, const String &table_name, const char *partition_name, ha_statistics &stats)
Store the statistics.
Definition: table_stats.h:154
Definition: handler.h:4048
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4583
unsigned long long int ulonglong
Definition: my_inttypes.h:56
struct stats stats
Definition: mysqlslap.cc:238
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
enum_table_stats_type
Definition: table_stats.h:79
bool update_table_stats(THD *thd, Table_ref *table)
Get dynamic table statistics of a table and store them into mysql.table_stats.
Definition: table_stats.cc:318
bool convert_table_name_case(char *db, char *table_name)
If the db is 'information_schema' then convert 'db' to lowercase and 'table_name' to upper case.
Definition: table_stats.cc:389
bool update_index_stats(THD *thd, Table_ref *table)
Get dynamic index statistics of a table and store them into mysql.index_stats.
Definition: table_stats.cc:344
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
unsigned long long Object_id
Definition: object_id.h:31
Char_string_template< String_type_allocator > String_type
Definition: string_type.h:51
Definition: os0file.h:89
const char * table_name
Definition: rules_table_service.cc:56
const char * db_name
Definition: rules_table_service.cc:55
required uint32 status
Definition: replication_asynchronous_connection_failover.proto:61
Our own string classes, used pervasively throughout the executor.
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2740
Definition: mysqlslap.cc:240