diff --git a/mysql-test/suite/innodb/r/stat_tables.result b/mysql-test/suite/innodb/r/stat_tables.result index d91b854d347..2a7eac01d73 100644 --- a/mysql-test/suite/innodb/r/stat_tables.result +++ b/mysql-test/suite/innodb/r/stat_tables.result @@ -82,4 +82,22 @@ WHERE database_name='test' AND table_name='t1' AND stat_name='size'; TIMESTAMPDIFF(DAY,last_update,now())<=1 1 DROP TABLE t1; +# +# MDEV-34207: ALTER TABLE...STATS_PERSISTENT=0 fails to drop statistics +# +CREATE TABLE t1 (c1 INT) ENGINE=InnoDB STATS_PERSISTENT 1; +ALTER TABLE t1 STATS_PERSISTENT 0; +DROP TABLE t1; +SET @save_persistent=@@GLOBAL.innodb_stats_persistent; +SET GLOBAL innodb_stats_persistent=1; +CREATE TABLE t2 (c1 INT) ENGINE=InnoDB; +RENAME TABLE t2 TO t1; +DROP TABLE t1; +SET GLOBAL innodb_stats_persistent=0; +CREATE TABLE t2 (c1 INT) ENGINE=InnoDB STATS_PERSISTENT 1; +RENAME TABLE t2 TO t1; +SET GLOBAL innodb_stats_persistent=@save_persistent; +DROP TABLE t1; +CREATE TABLE t1 (c1 INT) ENGINE=InnoDB STATS_PERSISTENT 1; +DROP TABLE t1; # End of 10.6 tests diff --git a/mysql-test/suite/innodb/t/stat_tables.test b/mysql-test/suite/innodb/t/stat_tables.test index 359fb1e00ec..602d05915a0 100644 --- a/mysql-test/suite/innodb/t/stat_tables.test +++ b/mysql-test/suite/innodb/t/stat_tables.test @@ -80,5 +80,29 @@ SELECT TIMESTAMPDIFF(DAY,last_update,now())<=1 FROM mysql.innodb_index_stats WHERE database_name='test' AND table_name='t1' AND stat_name='size'; DROP TABLE t1; +--echo # +--echo # MDEV-34207: ALTER TABLE...STATS_PERSISTENT=0 fails to drop statistics +--echo # +CREATE TABLE t1 (c1 INT) ENGINE=InnoDB STATS_PERSISTENT 1; +ALTER TABLE t1 STATS_PERSISTENT 0; +DROP TABLE t1; + +SET @save_persistent=@@GLOBAL.innodb_stats_persistent; +SET GLOBAL innodb_stats_persistent=1; + +CREATE TABLE t2 (c1 INT) ENGINE=InnoDB; +RENAME TABLE t2 TO t1; + +DROP TABLE t1; +SET GLOBAL innodb_stats_persistent=0; + +CREATE TABLE t2 (c1 INT) ENGINE=InnoDB STATS_PERSISTENT 1; +RENAME TABLE t2 TO t1; + +SET GLOBAL innodb_stats_persistent=@save_persistent; +DROP TABLE t1; + +CREATE TABLE t1 (c1 INT) ENGINE=InnoDB STATS_PERSISTENT 1; +DROP TABLE t1; --echo # End of 10.6 tests diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc index d2604533983..be721a51f3c 100644 --- a/storage/innobase/handler/handler0alter.cc +++ b/storage/innobase/handler/handler0alter.cc @@ -10597,6 +10597,16 @@ commit_try_norebuild( dict_index_t* index; const char *op = "rename index to add"; ulint num_fts_index = 0; + const bool statistics_drop = statistics_exist + && ((HA_OPTION_NO_STATS_PERSISTENT | + HA_OPTION_STATS_PERSISTENT) + & (old_table->s->db_create_options + ^ altered_table->s->db_create_options)) + && ((altered_table->s->db_create_options + & HA_OPTION_NO_STATS_PERSISTENT) + || (!(altered_table->s->db_create_options + & HA_OPTION_STATS_PERSISTENT) + && !srv_stats_persistent)); /* We altered the table in place. Mark the indexes as committed. */ for (ulint i = 0; i < ctx->num_to_add_index; i++) { @@ -10619,7 +10629,8 @@ commit_try_norebuild( } char db[MAX_DB_UTF8_LEN], table[MAX_TABLE_UTF8_LEN]; - if (ctx->num_to_drop_index) { + + if (statistics_exist && (statistics_drop || ctx->num_to_drop_index)) { dict_fs2utf8(ctx->old_table->name.m_name, db, sizeof db, table, sizeof table); } @@ -10654,7 +10665,7 @@ commit_try_norebuild( goto handle_error; } - if (!statistics_exist) { + if (!statistics_exist || statistics_drop) { continue; } @@ -10670,6 +10681,25 @@ commit_try_norebuild( } if (!statistics_exist) { + } else if (statistics_drop) { + error = dict_stats_delete_from_table_stats(db, table, trx); + switch (error) { + case DB_SUCCESS: + case DB_STATS_DO_NOT_EXIST: + break; + default: + goto handle_error; + } + error = dict_stats_delete_from_index_stats(db, table, trx); + switch (error) { + case DB_STATS_DO_NOT_EXIST: + error = DB_SUCCESS; + /* fall through */ + case DB_SUCCESS: + break; + default: + goto handle_error; + } } else if (const size_t size = ha_alter_info->rename_keys.size()) { char tmp_name[5]; char db[MAX_DB_UTF8_LEN], table[MAX_TABLE_UTF8_LEN];