From b317ede07adb3d9906b2e73873d7948557581b7b Mon Sep 17 00:00:00 2001 From: ARaveala Date: Thu, 21 May 2026 11:49:47 +0300 Subject: [PATCH] MDEV-39597: Remove save/restore of proc_info in free_tmp_table SHOW PROFILE reports duplicate stage entries for INSERT...SELECT caused by a save/restore pattern in free_tmp_table that blindly restores the previous stage name after tmp table removal. The fix removes the save/restore. The 'Removing tmp table' stage now remains active until the caller sets the next stage. Test uses --disable_ps2_protocol following the pattern in show_profile.test to ensure consistent QUERY_ID in PS protocol mode. fixup: add trailing newline to test file --- mysql-test/main/mdev-39597.result | 31 ++++++++++++++++++++++++ mysql-test/main/mdev-39597.test | 39 +++++++++++++++++++++++++++++++ sql/sql_select.cc | 3 --- 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 mysql-test/main/mdev-39597.result create mode 100644 mysql-test/main/mdev-39597.test diff --git a/mysql-test/main/mdev-39597.result b/mysql-test/main/mdev-39597.result new file mode 100644 index 0000000000000..92ba163050642 --- /dev/null +++ b/mysql-test/main/mdev-39597.result @@ -0,0 +1,31 @@ +# +# MDEV-39597: SHOW PROFILE reports duplicate sending data +# stage for INSERT...SELECT due to save/restore in free_tmp_table +# +CREATE TABLE profile_test ( +id INT PRIMARY KEY AUTO_INCREMENT, +name VARCHAR(100), +value INT +); +INSERT INTO profile_test (name, value) VALUES +('alpha',1),('beta',2),('gamma',3),('delta',4),('epsilon',5), +('zeta',6),('eta',7),('theta',8),('iota',9),('kappa',10); +SET profiling = 1; +INSERT INTO profile_test (name, value) +SELECT name, value FROM profile_test LIMIT 5; +# Check that no stage appears twice consecutively around removing tmp table +SELECT COUNT(*) AS duplicate_stage_found +FROM ( +SELECT +STATE, +LAG(STATE) OVER (ORDER BY SEQ) AS prev_status, +LEAD(STATE) OVER (ORDER BY SEQ) AS next_status +FROM information_schema.profiling +WHERE QUERY_ID = 1 +) t +WHERE STATE = 'Removing tmp table' +AND prev_status = next_status; +duplicate_stage_found +0 +SET profiling = 0; +DROP TABLE profile_test; diff --git a/mysql-test/main/mdev-39597.test b/mysql-test/main/mdev-39597.test new file mode 100644 index 0000000000000..62b8c5b38f2b4 --- /dev/null +++ b/mysql-test/main/mdev-39597.test @@ -0,0 +1,39 @@ +--echo # +--echo # MDEV-39597: SHOW PROFILE reports duplicate sending data +--echo # stage for INSERT...SELECT due to save/restore in free_tmp_table +--echo # + +--source include/have_profiling.inc +--disable_ps2_protocol + +CREATE TABLE profile_test ( + id INT PRIMARY KEY AUTO_INCREMENT, + name VARCHAR(100), + value INT +); +INSERT INTO profile_test (name, value) VALUES + ('alpha',1),('beta',2),('gamma',3),('delta',4),('epsilon',5), + ('zeta',6),('eta',7),('theta',8),('iota',9),('kappa',10); +SET profiling = 1; +INSERT INTO profile_test (name, value) + SELECT name, value FROM profile_test LIMIT 5; + +--echo # Check that no stage appears twice consecutively around removing tmp table + +SELECT COUNT(*) AS duplicate_stage_found +FROM ( + SELECT + STATE, + LAG(STATE) OVER (ORDER BY SEQ) AS prev_status, + LEAD(STATE) OVER (ORDER BY SEQ) AS next_status + FROM information_schema.profiling + WHERE QUERY_ID = 1 +) t +WHERE STATE = 'Removing tmp table' +AND prev_status = next_status; + +--enable_ps2_protocol + +# clean up +SET profiling = 0; +DROP TABLE profile_test; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 70785f531bb00..90763725f8b8e 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -22321,12 +22321,10 @@ void free_tmp_table(THD *thd, TABLE *entry) { MEM_ROOT own_root= entry->mem_root; - const char *save_proc_info; DBUG_ENTER("free_tmp_table"); DBUG_PRINT("enter",("table: %s alias: %s",entry->s->table_name.str, entry->alias.c_ptr())); - save_proc_info=thd->proc_info; THD_STAGE_INFO(thd, stage_removing_tmp_table); if (entry->file && entry->is_created()) @@ -22362,7 +22360,6 @@ free_tmp_table(THD *thd, TABLE *entry) } free_root(&own_root, MYF(0)); /* the table is allocated in its own root */ - thd_proc_info(thd, save_proc_info); DBUG_VOID_RETURN; }