planner: constant propagation copies a large multi-column IN list into IndexJoin other conditions on release-8.5 (backport #62864 and #63061)
Bug Report
Please answer these questions before submitting your issue. Thanks!
1. Minimal reproduce step (Required)
Schema and data (4 tables + 1 view, 200k rows each):
create table e2 (id bigint not null, org bigint not null, name varchar(240), primary key (id) nonclustered);
create table j (id bigint not null, org bigint not null, item bigint not null, unique key j_u1 (id, org), key j_n1 (item, org));
create table i (item bigint not null, org bigint not null, seg varchar(40), primary key (item, org) nonclustered);
create table it (item bigint not null, org bigint not null, descr varchar(240), lang varchar(4), primary key (item, org, lang) nonclustered);
create view iv as select i.item, i.org, i.seg, it.descr from i join it on i.item = it.item and i.org = it.org and it.lang = 'ZHS';
set session cte_max_recursion_depth = 1000000;
insert into e2 with recursive s(n) as (select 1 union all select n+1 from s where n < 200000) select n, n % 10, concat('WIP-', n) from s;
insert into j select id, org, id % 20000 from e2;
insert into i with recursive s(n) as (select 0 union all select n+1 from s where n < 199999) select n div 10, n % 10, concat('ITEM-', n) from s;
insert into it select item, org, concat('desc ', item), 'ZHS' from i;
analyze table e2, j, i, it;Query generator: a multi-column IN list with N tuples, ~95% of them duplicates (this mirrors a real application query):
import random
def q(n, distinct, seed=1):
rnd = random.Random(seed)
ids = rnd.sample(range(1, 200001), distinct)
base = [(i, i % 10) for i in ids]
tup = [base[rnd.randrange(distinct)] for _ in range(n)]
return ("select e.name, e.id, e.org, iv.seg, iv.descr from e2 e, j, iv "
"where e.id = j.id and e.org = j.org and j.item = iv.item and j.org = iv.org "
"and (e.id, e.org) in (" + ", ".join(f"({a},{b})" for a, b in tup) + ");")
print("explain format='brief' " + q(1000, 50)) # plan
# print(q(20000, 1000)) # timing2. What did you expect to see? (Required)
The tuple-IN predicate should only filter e2, and the cost should grow roughly linearly with N. This is what master does now (nightly 3425d78, 2026-09-29):
Projection
└─IndexJoin equal cond:eq(r.i.item, r.it.item), eq(r.i.org, r.it.org)
├─IndexJoin(Build) equal cond:eq(r.j.item, r.i.item), eq(r.j.org, r.i.org)
│ ├─IndexJoin(Build) equal cond:eq(r.e2.id, r.j.id), eq(r.e2.org, r.j.org)
│ │ ├─Selection(Build) or(or(... and(eq(r.e2.id, 177432), eq(r.e2.org, 2)) ...)) <- the only copy
│ │ │ └─Batch_Point_Get table:e2, index:PRIMARY(id)
│ │ └─IndexLookUp(Probe) (no Selection)
│ └─IndexLookUp(Probe) (no Selection)
└─IndexLookUp(Probe) (no Selection)3. What did you see instead (Required)
On release-8.5 (reproduced on v8.5.2, v8.5.6 and release-8.5 HEAD 3a2e67d7, 2026-09-29), constant propagation substitutes columns through the join equalities. The expanded DNF is then copied into the other cond of both IndexJoins (evaluated once per joined row) and pushed down as extra Selections to TiKV. With N = 1000 there are 6 copies of the DNF:
└─IndexJoin
├─IndexJoin(Build) ... other cond:or(... and(eq(r.e2.id, 177432), eq(r.i.org, 2)) ...) (1000 and-branches)
│ ├─IndexJoin(Build) ... other cond:or(... and(eq(r.e2.id, 177432), eq(r.j.org, 2)) ...) (2000 and-branches)
│ │ ├─Selection(Build) or(... and(eq(r.e2.id, ...), eq(r.e2.org, ...)) ...) (1000 and-branches)
│ │ │ └─Batch_Point_Get table:e2, index:PRIMARY(id)
│ │ └─IndexLookUp(Probe)
│ │ ├─Selection(Build) cop[tikv] or(... and(eq(r.j.id, ...), eq(r.j.org, ...)) ...) (1000 and-branches)
│ └─IndexLookUp(Probe)
│ ├─Selection(Build) cop[tikv] or(eq(r.i.org, 2), ...) (1000 branches)
└─IndexLookUp(Probe)
├─Selection(Build) cop[tikv] or(eq(r.it.org, 2), ...) (1000 branches)Every joined row evaluates the whole DNF, so execution time is quadratic in N, and the cloned expression trees use a lot of memory. That memory is not tracked by the statement memory tracker.
Measurements: TiKV cluster, 200k rows per table, performance.max-procs = 112 to mimic the production host; single query, no concurrency. Total = execution time including compile; heap = peak go_memstats_heap_inuse_bytes; tracked = peak MEM in processlist.
| TiDB | N=10,000 (500 distinct) | N=20,000 (1,000 distinct) | N=180,907 (8,957 distinct) |
|---|---|---|---|
| v8.5.6 | 19.7s, heap 4.5 GiB | 82s, heap 14.2 GiB, tracked 0.7 MiB | not run (quadratic, extrapolated ~2h, heap > 60 GiB) |
v8.5.2 (table with an extra unique(id, org)) |
28.9s, heap 8.0 GiB | 147s, heap 23.5 GiB, tracked 1.0 MiB | - |
master nightly 3425d78 |
3.8s, heap 0.9 GiB | 8.2s, heap 1.8 GiB | 94.5s, heap 13.1 GiB |
| v8.5.6 + minimal port of #62864 + #63061 | 1.0s, heap 0.5 GiB | 2.1s, heap 1.0 GiB | 42.7s, heap 6.2 GiB (with use index(primary), see note) |
Note: on this small test table, without the hint the optimizer picks a TableFullScan on e2 and pushes the 180,907-branch DNF to TiKV; that query fails with TiKV server timeout after 1,558s. I will file this as a separate issue.
Production impact (v8.5.2): a query of this shape with 180,907 tuples (8,957 distinct) ran for more than 3 days (cost_time 273,062s; TiKV cop_time only ~150s). The TiDB heap was about 60 GiB / 300M objects:
sync.(*Pool).pinSlow(per-builtin-function column pools × GOMAXPROCS): 28.6 GiB;ScalarFunction.CloneduringResolveIndices: 11.9 GiB.
The statement memory tracker reported only 589 MB, so tidb_mem_quota_query did not help. The GC pressure, together with the memory-limit tuner resetting GOMEMLIMIT below the live heap, caused multi-second process stalls. The stalls in turn caused gRPC keepalive timeouts to TiKV/PD (keepalive ping failed to receive ACK within timeout, TiKV Keepalive watchdog fired) and client connection drops.
Root cause / fix
This is fixed on master by #62864 (drop constant-propagation results that can only become join other cond) and #63061 (only keep derived conditions whose columns come from the same leaf data source). Neither has been backported to release-8.5.
A/B on master, unistore, N = 10,000:
| master commit | DNF copies left | total |
|---|---|---|
a49b6396 (before #62864) |
both IndexJoin other cond + e2/j Selections |
63.2s |
a6b13998 (#62864) |
top IndexJoin other cond removed |
18.1s |
02d38ee4 (#63061) |
only the e2 Selection |
2.3s |
So both PRs are needed; #63061 is the decisive one.
The two PRs do not cherry-pick cleanly onto release-8.5, because they depend on the predicate-simplification refactors #61316 / #61571 / #61739. A minimal port of the core logic (inner/semi join path only, 2 files) fixes the plan on release-8.5 HEAD 3a2e67d7: unistore N = 10,000 goes from 17.7s to 1.2s, and only the e2 Selection remains. The numbers in the table above are for the same patch on v8.5.6. I can share the patch if helpful. A proper backport should also cover the outer/anti-semi join paths (joinPropConst) from #63061, plus tests.
Request: please backport #62864 and #63061 to release-8.5.
Related:
- #63809 / #63815: per-function
sync.Poolcolumn allocator; #63815 is still open. It is the largest memory consumer in this case, at GOMAXPROCS=112. - #58962 / #70733: removes redundant OR branches, but not duplicated multi-column tuples, so it does not help here.
- #70381: another large-
IN-with-duplicates problem that delays STW.
4. What is your TiDB version? (Required)
- Production: v8.5.2 (
f43a13324440f92209e2a9f04c0bbe9cf763978d). - Reproduced on: v8.5.6 (
ae18096e023780bb56bfce33698abec0d4640d0a) and release-8.5 HEAD3a2e67d7bed5491b2885afd584c17f53862a9580. - Not reproducible on master nightly
3425d7860550d38b43c6faa9f3078647b2796abb(v9.0.0-beta.2.pre, 2026-09-29).
Source: pingcap/tidb