1
0
mirror of https://github.com/MariaDB/server.git synced 2026-10-23 18:49:29 +03:00

replay infuses statistics into INFORMATION_SCHEMA tables

fill_schema_table_by_open() opens and closes tables in the middle of
the statement. set_statistics_for_table() in get_schema_stat_record()
infused replayed statistics into them, which then went back to the
table cache before the replay restored them, and I_S.STATISTICS
showed the replayed cardinality.

Disable the replay while I_S opens tables.

Assisted-by: Claude:claude-5.5-opus
This commit is contained in:
Sergei Golubchik
2026-10-14 22:57:09 +02:00
parent d466df596b
commit 9c5864f70b
3 changed files with 65 additions and 0 deletions

View File

@@ -1359,4 +1359,41 @@ select json_extract(@opt_context, '$.tables[0].indexes[0].rec_per_key[0]');
json_extract(@opt_context, '$.tables[0].indexes[0].rec_per_key[0]')
"10"
drop table t1;
#
# replay does not apply to tables opened by INFORMATION_SCHEMA
#
create table t1 (a int, b int, key(a)) engine=innodb;
insert into t1 select seq%10, seq from seq_1_to_100;
analyze table t1 persistent for all;
Table Op Msg_type Msg_text
test.t1 analyze status Engine-independent statistics collected
test.t1 analyze status OK
select count(*) from t1 where a=3;
count(*)
10
set @opt_context=
(select REGEXP_SUBSTR(
context,
'(?<=set @opt_context=\')([\n\r].*)*(?=\'\;#opt_context_ends)')
from information_schema.optimizer_context);
set @opt_context_rpk= json_replace(@opt_context,
'$.tables[0].indexes[0].rec_per_key[0]', 50);
set optimizer_replay_context='opt_context_rpk';
select index_name, cardinality from information_schema.statistics
where table_schema='test' and table_name='t1';
index_name cardinality
a 10
set optimizer_replay_context='';
select count(*) from t1 where a=3;
count(*)
10
set @opt_context=
(select REGEXP_SUBSTR(
context,
'(?<=set @opt_context=\')([\n\r].*)*(?=\'\;#opt_context_ends)')
from information_schema.optimizer_context);
select json_extract(@opt_context, '$.tables[0].indexes[0].rec_per_key[0]');
json_extract(@opt_context, '$.tables[0].indexes[0].rec_per_key[0]')
"10"
drop table t1;
# End of 13.1 tests

View File

@@ -401,4 +401,23 @@ select count(*) from t1 where a=3;
select json_extract(@opt_context, '$.tables[0].indexes[0].rec_per_key[0]');
drop table t1;
--echo #
--echo # replay does not apply to tables opened by INFORMATION_SCHEMA
--echo #
create table t1 (a int, b int, key(a)) engine=innodb;
insert into t1 select seq%10, seq from seq_1_to_100;
analyze table t1 persistent for all;
select count(*) from t1 where a=3;
--source include/opt_context_save_in_var.inc
set @opt_context_rpk= json_replace(@opt_context,
'$.tables[0].indexes[0].rec_per_key[0]', 50);
set optimizer_replay_context='opt_context_rpk';
select index_name, cardinality from information_schema.statistics
where table_schema='test' and table_name='t1';
set optimizer_replay_context='';
select count(*) from t1 where a=3;
--source include/opt_context_save_in_var.inc
select json_extract(@opt_context, '$.tables[0].indexes[0].rec_per_key[0]');
drop table t1;
--echo # End of 13.1 tests

View File

@@ -4865,6 +4865,7 @@ fill_schema_table_by_open(THD *thd, MEM_ROOT *mem_root,
LEX *old_lex= thd->lex, temp_lex, *lex;
LEX_CSTRING db_name, table_name;
TABLE_LIST *table_list;
Optimizer_context_replay *old_opt_ctx_replay;
bool result= true, open_result, run, ext_error_handling;
DBUG_ENTER("fill_schema_table_by_open");
@@ -4885,6 +4886,13 @@ fill_schema_table_by_open(THD *thd, MEM_ROOT *mem_root,
thd->stmt_arena= &i_s_arena;
thd->set_n_backup_active_arena(&i_s_arena, &backup_arena);
/*
Tables opened here are closed before the end of the statement, so
the optimizer context replay could not restore their statistics.
*/
old_opt_ctx_replay= thd->opt_ctx_replay;
thd->opt_ctx_replay= NULL;
/* Prepare temporary LEX. */
thd->lex= lex= &temp_lex;
lex_start(thd);
@@ -5017,6 +5025,7 @@ end:
thd->lex= old_lex;
thd->opt_ctx_replay= old_opt_ctx_replay;
thd->stmt_arena= old_arena;
thd->restore_active_arena(&i_s_arena, &backup_arena);