ddl: CREATE TABLE LIKE waits for ADD INDEX but copies outdated schema
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
Please answer these questions before submitting your issue. Thanks!
### 1. Minimal reproduce step (Required)
1. Start a TiDB cluster (master containing PR #54110).
2. Prepare a test table with enough rows to make ADD INDEX take some time:
```sql
CREATE DATABASE IF NOT EXISTS ddl_test;
USE ddl_test;
DROP TABLE IF EXISTS a, b;
CREATE TABLE a (
id INT PRIMARY KEY,
c INT
);
```
3. In session 1, run `ALTER TABLE a ADD INDEX idx_c(c);`
4. When the DDL job is in reorg/running state (you can check via ADMIN SHOW DDL JOBS), open Session 2 and run `CREATE TABLE b LIKE a; SHOW CREATE TABLE b\G`
5. Observe: `CREATE TABLE b LIKE a` is blocked until `ALTER TABLE a ADD INDEX` finishes, but `SHOW CREATE TABLE b` still does not contain `idx_c`,
### 2. What did you expect to see? (Required)
- `CREATE TABLE b LIKE a` should copy the latest schema of table a (after `ADD INDEX` is finished),
- So `SHOW CREATE TABLE b` should contain `KEY idx_c (c)`.
In other words, once TiDB already makes CREATE TABLE LIKE wait for the running DDL job on a, it is natural for users to expect that the “LIKE” operation uses the up-to-date TableInfo of a, not the old snapshot at the time the statement was submitted.
### 3. What did you see instead (Required)
- CREATE TABLE b LIKE a does wait for ALTER TABLE a ADD INDEX idx_c(c) to finish (you can see it being blocked and only returning after the ADD INDEX job is done).
- However, SHOW CREATE TABLE b still shows:
```sql
mysql> SHOW CREATE TABLE b\G
*************************** 1. row ***************************
Table: b
Create Table: CREATE TABLE `b` (
`id` int NOT NULL,
`c` int DEFAULT NULL,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
1 row in set (0.01 sec)
```
### 4. What is your TiDB version? (Required)
```
Release Version: v9.0.0-beta.2.pre-852-g45b1597
Edition: Community
Git Commit Hash: 45b15978606a977f4ade719c9ea8a16d94794f0f
Git Branch: HEAD
```
Contributor guide
Assessment
This issue has not been assessed yet.