Skip to content

IN (subquery) set built from a cloned plan ignores max_threads: about 1600 threads and MEMORY_LIMIT_EXCEEDED (26.6 -> 26.8) #123701

Closed

Description

@fm4v

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

Activity

  1. added
    bug
    replay-finding
    on Oct 3, 2026
  2. added
    comp-query-optimizer
    on Oct 3, 2026
  3. removed
    bug
    on Oct 3, 2026
  4. fm4v commented on Oct 3, 2026

    @fm4v
    MemberAuthor

    @groeneai Please investigate the root cause of this regression and create a fix.

  5. groeneai commented on Oct 3, 2026

    @groeneai
    Collaborator

    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 in v26.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.

  6. groeneai commented on Oct 3, 2026

    @groeneai
    Collaborator

    I measured the repro from the issue body with official binaries in clickhouse local (local_cache is a cache disk over a local object-storage disk with cache_on_write_operations = 1; t1 ends up with 1600 active parts). max_threads = 8, 2-3 runs per arm, peak_threads_usage and memory_usage from system.query_log:

    • 26.6.8.7: 10 threads, 2.7-3.0 s, 0.72 GiB
    • 26.7.20.28: 1602 threads, 20-28 s, 1.1-1.3 GiB
    • 26.8.15.10: 1602 threads, 5-28 s, 0.4-1.0 GiB
    • 26.8.16.41: 10 threads, 2.6-2.9 s, 0.72 GiB
    • 26.9.9.28: 10-16 threads, 3.0-3.1 s, 0.72 GiB
    • master (26.10.1.1442 at efb38ea): 10 threads, 2.7-3.1 s, 0.72 GiB

    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 IN into PREWHERE, and ReadFromMergeTree::updatePrewhereInfo then builds the set right away through FutureSetFromSubquery::buildOrderedSetInplace. Since #107924 that build runs on source->clone(), and QueryPlan::clone() did not copy max_threads, so the speculative pipeline had no thread limit and used about one thread per part of the FINAL read (1600 parts, 1602 threads). On 26.8.15.10, either use_index_for_in_with_subqueries = 0 (unordered build on the original plan) or optimize_move_to_prewhere = 0, query_plan_optimize_prewhere = 0 (set built by CreatingSetsStep) brings the query back to 10 threads and 2.8-3.1 s.

    #109722 makes clone() keep max_threads. Its 26.8 backport #123665 is the only change to QueryPlan.cpp and PreparedSets.cpp between 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_usage error 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-backport on #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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

comp-query-optimizerperformancereplay-finding

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions