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