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