Repository navigation
Description
Caused by: #107924
QueryPlan::clone() did not copy max_threads, so the speculative IN-set build runs on the cloned plan without the thread cap. #109722 changes clone() to preserve it on master; it is not in 26.8. Not yet tested against this repro.
Description
On 26.8, an IN (subquery) whose subquery reads a ReplacingMergeTree with FINAL across many parts or partitions runs with about one thread per FINAL input stream when the outer query reads a MergeTree table (about 1600 threads vs 18 on 26.6). With a filesystem-cache disk, peak memory grows about 6x (0.78 GB -> 4.98 GB), so the query fails a 2 GB max_memory_usage limit that 26.6 completes within.
Regression between 26.6.1.2301 (OLD) and 26.8.1.390 (NEW).
How to reproduce
Run the setup once, then measure only the SELECT on each version.
CREATE DATABASE d1 ENGINE = Atomic;
CREATE TABLE d1.t1 (id String, c0 String, c1 Nullable(String), c2 LowCardinality(String), c3 String, ts DateTime64(3)) ENGINE = ReplacingMergeTree(ts) PARTITION BY toYYYYMMDD(ts) ORDER BY (c0, id) SETTINGS storage_policy = 'local_cache';
SYSTEM STOP MERGES d1.t1;
INSERT INTO d1.t1 SELECT toString(number), toString(number % 97), toString(number % 50), ['A','B','C','D'][number % 4 + 1], randomPrintableASCII(120), toDateTime64('2025-01-01 00:00:00', 3) + toIntervalDay(number % 400) + toIntervalSecond(number % 86000) FROM numbers(4000000) SETTINGS max_partitions_per_insert_block = 1000;
CREATE TABLE d1.t2 (k1 UUID, c4 String, ts DateTime64(3), c5 String) ENGINE = ReplacingMergeTree PARTITION BY toYYYYMMDD(ts) ORDER BY (c4, k1, ts);
INSERT INTO d1.t2 SELECT generateUUIDv4(), if(number % 3 = 0, 'M', 'N'), toDateTime64('2025-01-01 00:00:00', 3) + toIntervalDay(number % 10), concat('{"a":{"b":{"c":{"S":"', toString(number % 600000), '"}}},"p":"', repeat('x', 100), if(number % 100 = 0, ' needle text', ''), '"}') FROM numbers(1000000);
SELECT count(), sum(length(id)), sum(length(c0)), countIf(c1 = '0'), countIf(c2 = 'A'), sum(length(c3)), max(ts) FROM d1.t1 FORMAT Null;
SELECT countDistinct(k1), countDistinct(JSONExtractString(c5, 'a', 'b', 'c', 'S')) FROM d1.t2 WHERE c4 = 'M' AND c5 LIKE '%needle text%' AND JSONExtractString(c5, 'a', 'b', 'c', 'S') IN (SELECT id FROM d1.t1 FINAL WHERE c1 = '0' AND c2 IN ('A', 'B', 'C') AND c3 != '')
SETTINGS compatibility = '24.12', max_threads = '8', max_memory_usage = '2000000000';Performance
Original query (milliseconds; SQL omitted):
| OLD 26.6.1.2301 | NEW 26.8.1.390 | NEW/OLD |
|---|---|---|
| 3635 | 7318 | 2.01× |
Synthetic repro above:
| OLD 26.6.1.2301 | NEW 26.8.1.390 |
|---|---|
| Succeeded | Failed with Code 241 |
NEW did not complete successfully; no timing ratio is available.
Expected behavior
The IN-set subquery should respect max_threads (8) as on 26.6.1, use under 1 GB and complete under max_memory_usage=2000000000.
Version info
- Resolved by: Preserve execution limits and resources in derived QueryPlans #109722
- Backported to:
26.8.16.40
Activity
fm4v commentedon Oct 3, 2026
@groeneai Please investigate the root cause of this regression and create a fix.
groeneai commentedon Oct 3, 2026
Acknowledged. I'm investigating why the IN-set build on the cloned plan ignores
max_threads. #109722 was backported to 26.8 in #123665 and is included inv26.8.16.41-lts, so I will first measure this repro on master and on that release; I'll follow up here with a PR (or my findings) shortly.groeneai commentedon Oct 3, 2026
I measured the repro from the issue body with official binaries in
clickhouse local(local_cacheis a cache disk over a local object-storage disk withcache_on_write_operations = 1;t1ends up with 1600 active parts).max_threads = 8, 2-3 runs per arm,peak_threads_usageandmemory_usagefromsystem.query_log:Without the
compatibility = '24.12'pin the picture is the same (26.8.15.10: 1602 threads; 26.8.16.41 and master: 10 to 19).Root cause: the PREWHERE optimization moves the
INinto PREWHERE, andReadFromMergeTree::updatePrewhereInfothen builds the set right away throughFutureSetFromSubquery::buildOrderedSetInplace. Since #107924 that build runs onsource->clone(), andQueryPlan::clone()did not copymax_threads, so the speculative pipeline had no thread limit and used about one thread per part of theFINALread (1600 parts, 1602 threads). On 26.8.15.10, eitheruse_index_for_in_with_subqueries = 0(unordered build on the original plan) oroptimize_move_to_prewhere = 0, query_plan_optimize_prewhere = 0(set built byCreatingSetsStep) brings the query back to 10 threads and 2.8-3.1 s.#109722 makes
clone()keepmax_threads. Its 26.8 backport #123665 is the only change toQueryPlan.cppandPreparedSets.cppbetween v26.8.15.10-lts and v26.8.16.41-lts, so 26.8.16.41 and later are fixed; 26.8.1.390 from the report predates it.On this host 26.8.15.10 peaked at 1.0 GiB, so the 2 GB
max_memory_usageerror itself did not reproduce here; the thread fan-out behind it did, and it is gone on the fixed versions.26.7 is still affected: it contains #107924 but not #109722.
v26.7-must-backporton #109722 would cover it, if 26.7 should get the fix.No PR from me for this issue: master, 26.9 and 26.8.16.41 already contain the fix.