summaryrefslogtreecommitdiff
path: root/sql/sql_statistics.cc
diff options
context:
space:
mode:
Diffstat (limited to 'sql/sql_statistics.cc')
-rw-r--r--sql/sql_statistics.cc141
1 files changed, 70 insertions, 71 deletions
diff --git a/sql/sql_statistics.cc b/sql/sql_statistics.cc
index f27da375562..fb9364e0002 100644
--- a/sql/sql_statistics.cc
+++ b/sql/sql_statistics.cc
@@ -230,17 +230,17 @@ index_stat_def= {INDEX_STAT_N_FIELDS, index_stat_fields, 4, index_stat_pk_col};
Open all statistical tables and lock them
*/
-static int open_stat_tables(THD *thd, TABLE_LIST *tables,
- Open_tables_backup *backup, bool for_write)
+static int open_stat_tables(THD *thd, TABLE_LIST *tables, bool for_write)
{
int rc;
-
Dummy_error_handler deh; // suppress errors
+ DBUG_ASSERT(thd->internal_transaction());
+
thd->push_internal_handler(&deh);
init_table_list_for_stat_tables(tables, for_write);
init_mdl_requests(tables);
thd->in_sub_stmt|= SUB_STMT_STAT_TABLES;
- rc= open_system_tables_for_read(thd, tables, backup);
+ rc= open_system_tables_for_read(thd, tables);
thd->in_sub_stmt&= ~SUB_STMT_STAT_TABLES;
thd->pop_internal_handler();
@@ -253,7 +253,7 @@ static int open_stat_tables(THD *thd, TABLE_LIST *tables,
stat_table_intact.check(tables[COLUMN_STAT].table, &column_stat_def) ||
stat_table_intact.check(tables[INDEX_STAT].table, &index_stat_def)))
{
- close_system_tables(thd, backup);
+ close_thread_tables(thd);
rc= 1;
}
@@ -270,13 +270,12 @@ static int open_stat_tables(THD *thd, TABLE_LIST *tables,
stat tables need to be adjusted accordingly.
*/
static inline int open_stat_table_for_ddl(THD *thd, TABLE_LIST *table,
- const LEX_CSTRING *stat_tab_name,
- Open_tables_backup *backup)
+ const LEX_CSTRING *stat_tab_name)
{
table->init_one_table(&MYSQL_SCHEMA_NAME, stat_tab_name, NULL, TL_WRITE);
No_such_table_error_handler nst_handler;
thd->push_internal_handler(&nst_handler);
- int res= open_system_tables_for_read(thd, table, backup);
+ int res= open_system_tables_for_read(thd, table);
thd->pop_internal_handler();
return res;
}
@@ -1036,27 +1035,25 @@ public:
stat_field->set_notnull();
switch (i) {
case COLUMN_STAT_MIN_VALUE:
+ {
+ /*
+ TODO varun: After MDEV-22583 is fixed, add a function in Field_bit
+ and move this implementation there
+ */
if (table_field->type() == MYSQL_TYPE_BIT)
stat_field->store(stats->min_value->val_int(),true);
else
- {
- stats->min_value->val_str(&val);
- size_t length= Well_formed_prefix(val.charset(), val.ptr(),
- MY_MIN(val.length(), stat_field->field_length)).length();
- stat_field->store(val.ptr(), length, &my_charset_bin);
- }
+ stats->min_value->store_to_statistical_minmax_field(stat_field, &val);
break;
+ }
case COLUMN_STAT_MAX_VALUE:
+ {
if (table_field->type() == MYSQL_TYPE_BIT)
stat_field->store(stats->max_value->val_int(),true);
else
- {
- stats->max_value->val_str(&val);
- size_t length= Well_formed_prefix(val.charset(), val.ptr(),
- MY_MIN(val.length(), stat_field->field_length)).length();
- stat_field->store(val.ptr(), length, &my_charset_bin);
- }
+ stats->max_value->store_to_statistical_minmax_field(stat_field, &val);
break;
+ }
case COLUMN_STAT_NULLS_RATIO:
stat_field->store(stats->get_nulls_ratio());
break;
@@ -1129,31 +1126,25 @@ public:
switch (i) {
case COLUMN_STAT_MIN_VALUE:
- table_field->read_stats->min_value->set_notnull();
+ {
+ Field *field= table_field->read_stats->min_value;
+ field->set_notnull();
if (table_field->type() == MYSQL_TYPE_BIT)
- table_field->read_stats->min_value->store(stat_field->val_int(),
- true);
+ field->store(stat_field->val_int(), true);
else
- {
- stat_field->val_str(&val);
- table_field->read_stats->min_value->store(val.ptr(),
- val.length(),
- &my_charset_bin);
- }
+ field->store_from_statistical_minmax_field(stat_field, &val);
break;
+ }
case COLUMN_STAT_MAX_VALUE:
- table_field->read_stats->max_value->set_notnull();
+ {
+ Field *field= table_field->read_stats->max_value;
+ field->set_notnull();
if (table_field->type() == MYSQL_TYPE_BIT)
- table_field->read_stats->max_value->store(stat_field->val_int(),
- true);
+ field->store(stat_field->val_int(), true);
else
- {
- stat_field->val_str(&val);
- table_field->read_stats->max_value->store(val.ptr(),
- val.length(),
- &my_charset_bin);
- }
+ field->store_from_statistical_minmax_field(stat_field, &val);
break;
+ }
case COLUMN_STAT_NULLS_RATIO:
table_field->read_stats->set_nulls_ratio(stat_field->val_real());
break;
@@ -1441,7 +1432,7 @@ public:
*/
bool init(uint n_keyparts)
{
- if (!(rowid_buf= (uchar*)my_malloc(rowid_size, MYF(0))))
+ if (!(rowid_buf= (uchar*)my_malloc(PSI_INSTRUMENT_ME, rowid_size, MYF(0))))
return true;
if (open_cached_file(&io_cache, mysql_tmpdir, TEMP_PREFIX,
@@ -2786,18 +2777,18 @@ int collect_statistics_for_table(THD *thd, TABLE *table)
int update_statistics_for_table(THD *thd, TABLE *table)
{
TABLE_LIST tables[STATISTICS_TABLES];
- Open_tables_backup open_tables_backup;
uint i;
int err;
enum_binlog_format save_binlog_format;
int rc= 0;
TABLE *stat_table;
-
DBUG_ENTER("update_statistics_for_table");
DEBUG_SYNC(thd, "statistics_update_start");
- if (open_stat_tables(thd, tables, &open_tables_backup, TRUE))
+ start_new_trans new_trans(thd);
+
+ if (open_stat_tables(thd, tables, TRUE))
DBUG_RETURN(rc);
save_binlog_format= thd->set_current_stmt_binlog_format_stmt();
@@ -2847,8 +2838,9 @@ int update_statistics_for_table(THD *thd, TABLE *table)
}
thd->restore_stmt_binlog_format(save_binlog_format);
-
- close_system_tables(thd, &open_tables_backup);
+ if (thd->commit_whole_transaction_and_close_tables())
+ rc= 1;
+ new_trans.restore_old_transaction();
DBUG_RETURN(rc);
}
@@ -3162,7 +3154,6 @@ static void dump_stats_from_share_to_table(TABLE *table)
int read_statistics_for_tables(THD *thd, TABLE_LIST *tables)
{
TABLE_LIST stat_tables[STATISTICS_TABLES];
- Open_tables_backup open_tables_backup;
DBUG_ENTER("read_statistics_for_tables");
@@ -3207,7 +3198,9 @@ int read_statistics_for_tables(THD *thd, TABLE_LIST *tables)
if (found_stat_table || !statistics_for_tables_is_needed)
DBUG_RETURN(0);
- if (open_stat_tables(thd, stat_tables, &open_tables_backup, FALSE))
+ start_new_trans new_trans(thd);
+
+ if (open_stat_tables(thd, stat_tables, FALSE))
DBUG_RETURN(1);
for (TABLE_LIST *tl= tables; tl; tl= tl->next_global)
@@ -3229,7 +3222,8 @@ int read_statistics_for_tables(THD *thd, TABLE_LIST *tables)
}
}
- close_system_tables(thd, &open_tables_backup);
+ thd->commit_whole_transaction_and_close_tables();
+ new_trans.restore_old_transaction();
DBUG_RETURN(0);
}
@@ -3269,8 +3263,10 @@ int delete_statistics_for_table(THD *thd, const LEX_CSTRING *db,
Open_tables_backup open_tables_backup;
int rc= 0;
DBUG_ENTER("delete_statistics_for_table");
+
+ start_new_trans new_trans(thd);
- if (open_stat_tables(thd, tables, &open_tables_backup, TRUE))
+ if (open_stat_tables(thd, tables, TRUE))
DBUG_RETURN(0);
save_binlog_format= thd->set_current_stmt_binlog_format_stmt();
@@ -3313,8 +3309,8 @@ int delete_statistics_for_table(THD *thd, const LEX_CSTRING *db,
rc= 1;
thd->restore_stmt_binlog_format(save_binlog_format);
-
- close_system_tables(thd, &open_tables_backup);
+ thd->commit_whole_transaction_and_close_tables();
+ new_trans.restore_old_transaction();
DBUG_RETURN(rc);
}
@@ -3346,12 +3342,12 @@ int delete_statistics_for_column(THD *thd, TABLE *tab, Field *col)
enum_binlog_format save_binlog_format;
TABLE *stat_table;
TABLE_LIST tables;
- Open_tables_backup open_tables_backup;
int rc= 0;
DBUG_ENTER("delete_statistics_for_column");
- if (open_stat_table_for_ddl(thd, &tables, &stat_table_name[1],
- &open_tables_backup))
+ start_new_trans new_trans(thd);
+
+ if (open_stat_table_for_ddl(thd, &tables, &stat_table_name[1]))
DBUG_RETURN(0);
save_binlog_format= thd->set_current_stmt_binlog_format_stmt();
@@ -3367,8 +3363,9 @@ int delete_statistics_for_column(THD *thd, TABLE *tab, Field *col)
}
thd->restore_stmt_binlog_format(save_binlog_format);
-
- close_system_tables(thd, &open_tables_backup);
+ if (thd->commit_whole_transaction_and_close_tables())
+ rc= 1;
+ new_trans.restore_old_transaction();
DBUG_RETURN(rc);
}
@@ -3403,12 +3400,12 @@ int delete_statistics_for_index(THD *thd, TABLE *tab, KEY *key_info,
enum_binlog_format save_binlog_format;
TABLE *stat_table;
TABLE_LIST tables;
- Open_tables_backup open_tables_backup;
int rc= 0;
DBUG_ENTER("delete_statistics_for_index");
- if (open_stat_table_for_ddl(thd, &tables, &stat_table_name[2],
- &open_tables_backup))
+ start_new_trans new_trans(thd);
+
+ if (open_stat_table_for_ddl(thd, &tables, &stat_table_name[2]))
DBUG_RETURN(0);
save_binlog_format= thd->set_current_stmt_binlog_format_stmt();
@@ -3444,8 +3441,9 @@ int delete_statistics_for_index(THD *thd, TABLE *tab, KEY *key_info,
rc= 1;
thd->restore_stmt_binlog_format(save_binlog_format);
-
- close_system_tables(thd, &open_tables_backup);
+ if (thd->commit_whole_transaction_and_close_tables())
+ rc= 1;
+ new_trans.restore_old_transaction();
DBUG_RETURN(rc);
}
@@ -3487,14 +3485,13 @@ int rename_table_in_stat_tables(THD *thd, const LEX_CSTRING *db,
enum_binlog_format save_binlog_format;
TABLE *stat_table;
TABLE_LIST tables[STATISTICS_TABLES];
- Open_tables_backup open_tables_backup;
int rc= 0;
DBUG_ENTER("rename_table_in_stat_tables");
- if (open_stat_tables(thd, tables, &open_tables_backup, TRUE))
- {
+ start_new_trans new_trans(thd);
+
+ if (open_stat_tables(thd, tables, TRUE))
DBUG_RETURN(0); // not an error
- }
save_binlog_format= thd->set_current_stmt_binlog_format_stmt();
@@ -3543,8 +3540,9 @@ int rename_table_in_stat_tables(THD *thd, const LEX_CSTRING *db,
}
thd->restore_stmt_binlog_format(save_binlog_format);
-
- close_system_tables(thd, &open_tables_backup);
+ if (thd->commit_whole_transaction_and_close_tables())
+ rc= 1;
+ new_trans.restore_old_transaction();
DBUG_RETURN(rc);
}
@@ -3577,15 +3575,15 @@ int rename_column_in_stat_tables(THD *thd, TABLE *tab, Field *col,
enum_binlog_format save_binlog_format;
TABLE *stat_table;
TABLE_LIST tables;
- Open_tables_backup open_tables_backup;
int rc= 0;
DBUG_ENTER("rename_column_in_stat_tables");
if (tab->s->tmp_table != NO_TMP_TABLE)
DBUG_RETURN(0);
- if (open_stat_table_for_ddl(thd, &tables, &stat_table_name[1],
- &open_tables_backup))
+ start_new_trans new_trans(thd);
+
+ if (open_stat_table_for_ddl(thd, &tables, &stat_table_name[1]))
DBUG_RETURN(rc);
save_binlog_format= thd->set_current_stmt_binlog_format_stmt();
@@ -3602,8 +3600,9 @@ int rename_column_in_stat_tables(THD *thd, TABLE *tab, Field *col,
}
thd->restore_stmt_binlog_format(save_binlog_format);
-
- close_system_tables(thd, &open_tables_backup);
+ if (thd->commit_whole_transaction_and_close_tables())
+ rc= 1;
+ new_trans.restore_old_transaction();
DBUG_RETURN(rc);
}