Bug #117416 Rename will leave table status information, resulting in incorrect table status
Submitted: 8 Feb 11:29 Modified: 10 Feb 8:17
Reporter: Huaxiong Song (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Data Dictionary Severity:S3 (Non-critical)
Version:8.0.41, 8.4.4 OS:Any
Assigned to: CPU Architecture:Any
Tags: Contribution

[8 Feb 11:29] Huaxiong Song
Description:
After performing a rename operation (t1 rename to t1_tmp), the table status information of the old table t1 before the rename will not be cleared. When another table is renamed to t1 (t2 rename to t1), the new t1 table status will be incorrect.

How to repeat:
In debug mysql-server, my the MTR:

```

source include/have_debug.inc;

--echo #
--echo # 1) Prepare
--echo #
SET DEBUG='+d,skip_dd_table_access_check';
CREATE DATABASE my_test;
USE my_test;

# table status of t1 and t2 will be significantly different
CREATE TABLE t1 (id INT);
CREATE TABLE t2 LIKE t1;

INSERT INTO t1 VALUES (1);
DELIMITER $$;

CREATE PROCEDURE insert_n_times(IN n INT)
BEGIN
    DECLARE i INT DEFAULT 1;

    WHILE i <= n DO
        INSERT INTO t1 SELECT * FROM t1;
        SET i = i + 1;
    END WHILE;
END$$

DELIMITER ;$$

CALL insert_n_times(16);

--echo #
--echo # 2) SHOW TABLE STATUS
--echo #
SHOW TABLE STATUS;

--echo #
--echo # 3) RENAME and SHOW TABLE STATUS
--echo #
RENAME TABLE t1 TO t1_tmp;
SHOW TABLE STATUS;

# We can find residual information of t1 in DD(mysql.table_stats), it is wrong.
--replace_column 11 update_time 12 check_time 13 cached_time
--sorted_result
SELECT * FROM mysql.table_stats WHERE schema_name = 'my_test';

# Now we rename t2 to t1, we can see the new t1 has old table status, which is from residual information.
RENAME TABLE t2 TO t1;
SHOW TABLE STATUS;

--replace_column 11 update_time 12 check_time 13 cached_time
--sorted_result
SELECT * FROM mysql.table_stats WHERE schema_name = 'my_test';

--echo #
--echo # 4) ANALYZE TABLE and SHOW TABLE STATUS
--echo #
ANALYZE TABLE t1, t1_tmp;
SHOW TABLE STATUS;

--replace_column 11 update_time 12 check_time 13 cached_time
--sorted_result
SELECT * FROM mysql.table_stats WHERE schema_name = 'my_test';

--echo #
--echo # 5) Cleanup
--echo #
DROP DATABASE my_test;

```

We can see:
1. The table status information of old t1 is residual
2. The new t1 mistakenly uses the table status of old t1

```result

```

Suggested fix:
After rename table, remove the dynamic statistics too.
[10 Feb 8:17] MySQL Verification Team
Hello Huaxiong Song,

Thank you for the report and test case.

regards,
Umesh
[10 Feb 11:52] Huaxiong Song
Bugfix of bug117416

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: bug117416.patch (application/octet-stream, text), 9.65 KiB.