revision-id: 4e4ce8ed6906806216e402d80ceff8f61f41964e (mariadb-10.1.38-151-g4e4ce8ed690) parent(s): 021c7216c05f3143c5113fc9a4aea3d11b6bac48 author: Varun Gupta committer: Varun Gupta timestamp: 2019-04-30 03:48:01 +0530 message: MDEV-19352: Server crash in alloc_histograms_for_table_share upon query from information schema To read histograms for a table, we should check if the allocation of statistics was done or not, if not done we should not try to read histograms for such a table. --- mysql-test/r/stat_tables.result | 16 ++++++++++++++++ mysql-test/r/stat_tables_innodb.result | 16 ++++++++++++++++ mysql-test/t/stat_tables.test | 17 +++++++++++++++++ sql/sql_show.cc | 2 ++ sql/sql_statistics.cc | 31 +++++++++++++++++-------------- 5 files changed, 68 insertions(+), 14 deletions(-) diff --git a/mysql-test/r/stat_tables.result b/mysql-test/r/stat_tables.result index bd3e9ed7a40..ce1afcf5d57 100644 --- a/mysql-test/r/stat_tables.result +++ b/mysql-test/r/stat_tables.result @@ -692,5 +692,21 @@ DROP DATABASE dbt3_s001; delete from mysql.table_stats; delete from mysql.column_stats; delete from mysql.index_stats; +# +# MDEV-19352: Server crash in alloc_histograms_for_table_share upon query from information schema +# +use test; +set @save_optimizer_use_condition_selectivity= @@optimizer_use_condition_selectivity; +set @@optimizer_use_condition_selectivity= 4; +CREATE TABLE t1 (a INT); +CREATE TABLE t2 (b INT); +CREATE VIEW v1 AS SELECT * FROM t1 JOIN t2; +INSERT INTO t1 SELECT * FROM x; +ERROR 42S02: Table 'test.x' doesn't exist +SELECT * FROM information_schema.tables where TABLE_NAME in ('v'); +TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE ENGINE VERSION ROW_FORMAT TABLE_ROWS AVG_ROW_LENGTH DATA_LENGTH MAX_DATA_LENGTH INDEX_LENGTH DATA_FREE AUTO_INCREMENT CREATE_TIME UPDATE_TIME CHECK_TIME TABLE_COLLATION CHECKSUM CREATE_OPTIONS TABLE_COMMENT +DROP VIEW v1; +DROP TABLE t1, t2; +set @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity; set @save_optimizer_switch=@@optimizer_switch; set use_stat_tables=@save_use_stat_tables; diff --git a/mysql-test/r/stat_tables_innodb.result b/mysql-test/r/stat_tables_innodb.result index 579d19462e7..246baf823bf 100644 --- a/mysql-test/r/stat_tables_innodb.result +++ b/mysql-test/r/stat_tables_innodb.result @@ -719,6 +719,22 @@ DROP DATABASE dbt3_s001; delete from mysql.table_stats; delete from mysql.column_stats; delete from mysql.index_stats; +# +# MDEV-19352: Server crash in alloc_histograms_for_table_share upon query from information schema +# +use test; +set @save_optimizer_use_condition_selectivity= @@optimizer_use_condition_selectivity; +set @@optimizer_use_condition_selectivity= 4; +CREATE TABLE t1 (a INT); +CREATE TABLE t2 (b INT); +CREATE VIEW v1 AS SELECT * FROM t1 JOIN t2; +INSERT INTO t1 SELECT * FROM x; +ERROR 42S02: Table 'test.x' doesn't exist +SELECT * FROM information_schema.tables where TABLE_NAME in ('v'); +TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE ENGINE VERSION ROW_FORMAT TABLE_ROWS AVG_ROW_LENGTH DATA_LENGTH MAX_DATA_LENGTH INDEX_LENGTH DATA_FREE AUTO_INCREMENT CREATE_TIME UPDATE_TIME CHECK_TIME TABLE_COLLATION CHECKSUM CREATE_OPTIONS TABLE_COMMENT +DROP VIEW v1; +DROP TABLE t1, t2; +set @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity; set @save_optimizer_switch=@@optimizer_switch; set use_stat_tables=@save_use_stat_tables; set optimizer_switch=@save_optimizer_switch_for_stat_tables_test; diff --git a/mysql-test/t/stat_tables.test b/mysql-test/t/stat_tables.test index 97f9f08569f..b406276f6c3 100644 --- a/mysql-test/t/stat_tables.test +++ b/mysql-test/t/stat_tables.test @@ -453,6 +453,23 @@ delete from mysql.table_stats; delete from mysql.column_stats; delete from mysql.index_stats; +--echo # +--echo # MDEV-19352: Server crash in alloc_histograms_for_table_share upon query from information schema +--echo # + +use test; +set @save_optimizer_use_condition_selectivity= @@optimizer_use_condition_selectivity; +set @@optimizer_use_condition_selectivity= 4; +CREATE TABLE t1 (a INT); +CREATE TABLE t2 (b INT); +CREATE VIEW v1 AS SELECT * FROM t1 JOIN t2; +--error ER_NO_SUCH_TABLE +INSERT INTO t1 SELECT * FROM x; +SELECT * FROM information_schema.tables where TABLE_NAME in ('v'); +DROP VIEW v1; +DROP TABLE t1, t2; +set @@optimizer_use_condition_selectivity= @save_optimizer_use_condition_selectivity; + set @save_optimizer_switch=@@optimizer_switch; set use_stat_tables=@save_use_stat_tables; diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 7f0c50a2e55..3d9d3514902 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -4213,6 +4213,7 @@ fill_schema_table_by_open(THD *thd, bool is_show_fields_or_keys, /* Prepare temporary LEX. */ thd->lex= lex= &temp_lex; lex_start(thd); + lex->sql_command= old_lex->sql_command; /* Disable constant subquery evaluation as we won't be locking tables. */ lex->context_analysis_only= CONTEXT_ANALYSIS_ONLY_VIEW; @@ -4282,6 +4283,7 @@ fill_schema_table_by_open(THD *thd, bool is_show_fields_or_keys, MYSQL_OPEN_FAIL_ON_MDL_CONFLICT : 0)), DT_PREPARE | DT_CREATE)); + lex->sql_command= old_lex->sql_command; (void) read_statistics_for_tables_if_needed(thd, table_list); thd->force_read_stats= false; diff --git a/sql/sql_statistics.cc b/sql/sql_statistics.cc index 061622a2cd6..cd8d3f58fbe 100644 --- a/sql/sql_statistics.cc +++ b/sql/sql_statistics.cc @@ -3276,22 +3276,25 @@ int read_statistics_for_tables_if_needed(THD *thd, TABLE_LIST *tables) continue; if (table_share && - table_share->stats_cb.stats_can_be_read && - !table_share->stats_cb.stats_is_read) - { - (void) read_statistics_for_table(thd, tl->table, stat_tables); - table_share->stats_cb.stats_is_read= TRUE; - } - if (table_share->stats_cb.stats_is_read) - tl->table->stats_is_read= TRUE; - if (thd->variables.optimizer_use_condition_selectivity > 3 && - table_share && !table_share->stats_cb.histograms_are_read) + table_share->stats_cb.stats_can_be_read) { - (void) read_histograms_for_table(thd, tl->table, stat_tables); - table_share->stats_cb.histograms_are_read= TRUE; + if (!table_share->stats_cb.stats_is_read) + { + (void) read_statistics_for_table(thd, tl->table, stat_tables); + table_share->stats_cb.stats_is_read= TRUE; + } + if (table_share->stats_cb.stats_is_read) + tl->table->stats_is_read= TRUE; + + if (thd->variables.optimizer_use_condition_selectivity > 3 && + !table_share->stats_cb.histograms_are_read) + { + (void) read_histograms_for_table(thd, tl->table, stat_tables); + table_share->stats_cb.histograms_are_read= TRUE; + } + if (table_share->stats_cb.histograms_are_read) + tl->table->histograms_are_read= TRUE; } - if (table_share->stats_cb.stats_is_read) - tl->table->histograms_are_read= TRUE; } }