mysql-test index_merge1 can hit a deadlock on ANALYZE TABLE t3 after preceding DDLs
- 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)
The flaky failure was reported from `PingCAP-QE/tidb-test/mysql_test/t/index_merge1.test`.
The failing statement is `analyze table t3;` at line 324, immediately after several `ALTER TABLE ... ADD COLUMN/INDEX` statements:
```sql
create table t3 like t0;
insert into t3 select * from t0;
alter table t3 add key9 int not null;
alter table t3 add index i9(key9);
alter table t3 add keyA int not null;
alter table t3 add index iA(keyA);
alter table t3 add keyB int not null;
alter table t3 add index iB(keyB);
alter table t3 add keyC int not null;
alter table t3 add index iC(keyC);
update t3 set key9=key1,keyA=key1,keyB=key1,keyC=key1;
analyze table t3;
```
The CI log reported:
```text
run test [index_merge1] err: sql:analyze table t3;: run "analyze table t3;" at line 324 err Error 1105 (HY000): [executor:1213]Deadlock found when trying to get lock; try restarting transaction
```
I have not reproduced it locally yet, but I did try to stress the same case:
- `mysql_test/index_merge1` passed 21/21 times locally
- one full local `mysql_test` part-2 shard run also passed `index_merge1`
- an additional 20 isolated `index_merge1` runs with `GOMAXPROCS=1` also passed
So this still looks like a timing-sensitive CI-only or low-probability race at the moment.
### 2. What did you expect to see? (Required)
`analyze table t3;` should finish successfully and the mysql-test shard should not fail with a client-visible deadlock.
### 3. What did you see instead (Required)
CI reported a non-retryable deadlock from `ANALYZE TABLE` in `index_merge1`:
```text
Error 1105 (HY000): [executor:1213]Deadlock found when trying to get lock; try restarting transaction
```
Additional context:
- `pkg/statistics/handle/storage/save.go` already has a workaround for a previous analyze-vs-stats deadlock (`#62069`, fixed by `#62327`)
- this failure happens after multiple DDLs and before any concurrent user SQL in the test case
- because of that ordering, my current guess is that there may still be another lock-ordering path between `ANALYZE` and background statistics/schema-change work
The last point is only an inference from the test shape and existing code comments, not a confirmed root cause.
### 4. What is your TiDB version? (Required)
Observed in CI on master around 2026-03-20.
Local attempts were done with a local master build at commit:
```text
c35c7a2615e088f73b4d707a62355e3bd187678b
```
Contributor guide
Assessment
This issue has not been assessed yet.