Repository navigation
Use max_insert_threads for plain INSERTs without materialized views - #102961
Merged
Merged
Conversation
buildInsertPipeline was requesting max_threads ConcurrencyControl slots for all INSERTs regardless of pipeline width. For plain INSERTs without MVs the pipeline is 1-wide, so this wasted CC capacity and spawned unnecessary threads (up to ~10 for max_threads=16). Now we use max_insert_threads when no MVs are attached, and max_threads only when MVs are involved (their inner SELECTs benefit from full parallelism). Also adds a trace log in PipelineExecutor::allocateCPU to make CC slot allocation visible, and a regression test covering both cases. Fixes: #102947
Contributor
|
Workflow [PR], commit [c4f5e7e] Summary: ❌
AI ReviewSummaryThis PR changes Findings
💡 Nits
ClickHouse Rules
Final Verdict
|
ConcurrencyControl has an issue (#102947) where CC slots are allocated eagerly at query start, and spawnThreads has a race where it spawns threads faster than they can register as idle, resulting in many unnecessary threads being created. Until lazy CC slot allocation is implemented, we have no choice other than to set strict limits on the thread count for insert queries. buildInsertPipeline was requesting max_threads CC slots for all INSERTs regardless of pipeline width. For plain INSERTs without MVs the pipeline is 1-wide, so this wasted CC capacity and spawned unnecessary threads. Now we use max_insert_threads when no MVs are attached, and max_threads only when MVs are involved (their inner SELECTs benefit from full parallelism). Fixes: #102947
Style check requires specifying the log table name explicitly.
CheSema
added a commit
that referenced
this pull request
Apr 17, 2026
…ized settings Pin `input_format_parallel_parsing=0` in INSERT commands so that parallel parsing threads do not inflate `peak_threads_usage` beyond the threshold — the test measures INSERT pipeline concurrency, not parser parallelism. Add `SETTINGS optimize_if_transform_strings_to_enum = 0` to diagnostic queries against `system.query_log` to prevent a column name mismatch when randomized settings enable both this optimization and `parallel_replicas_local_plan`. CI report: #102961 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…ized settings Pin `input_format_parallel_parsing=0` in INSERT commands so that parallel parsing threads do not inflate `peak_threads_usage` beyond the threshold — the test measures INSERT pipeline concurrency, not parser parallelism. Add `SETTINGS optimize_if_transform_strings_to_enum = 0` to diagnostic queries against `system.query_log` to prevent a column name mismatch when randomized settings enable both this optimization and `parallel_replicas_local_plan`. CI report: #102961 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
CheSema
force-pushed
the
sema/fix-insert-thread-count
branch
from
April 17, 2026 20:00
a6bb0a1 to
c208281
Compare
groeneai
added a commit
to groeneai/ClickHouse
that referenced
this pull request
Apr 18, 2026
Regression from ClickHouse#92141: server aborts with signal 6 during `loadDataPart` in debug and sanitizer builds when a part directory contains a leftover `txn_version.txt.tmp` file with no corresponding `txn_version.txt`. This state is produced by `VersionMetadataOnDisk::loadMetadata` case 2 (an incomplete write that was not atomically renamed). It returns a `VersionInfo` with `creation_tid = Tx::DummyTID` and `creation_csn = Tx::RolledBackCSN`. `Tx::DummyTID = {NonTransactionalCSN, DummyLocalTID, Nil} = {1, 2, Nil}` has `start_csn == NonTransactionalCSN` but `local_tid == DummyLocalTID` — which fails the invariant asserted in `TransactionID::isNonTransactional`. The new `VersionMetadata::validateInfo` (added by ClickHouse#92141) and `VersionInfo::wasInvolvedInTransaction` both call `isNonTransactional` on the loaded `creation_tid`, so the assertion aborts the server. Two changes: - `TransactionID::isNonTransactional`: widen the assertion to allow the `DummyTID` special case. The function still correctly returns `false` for `DummyTID` because `local_tid == DummyLocalTID != NonTransactionalLocalTID`. - `VersionMetadata::validateInfo`: early-return for rolled-back parts (`creation_csn == RolledBackCSN`). Such parts are marked `Outdated` immediately after loading by `MergeTreeData::loadDataPart` and cleaned up, so the live-part invariants validated here do not apply. CI reports showing the crash: - PR ClickHouse#103047: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=103047&sha=ec0124d77b94c97a4b1b48049b0215b1dddb94a5&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20distributed%20plan%2C%20s3%20storage%2C%20parallel%29 - PR ClickHouse#102961: same `Stateless tests (amd_debug, distributed plan, s3 storage, parallel)` check Regression test `04104_transaction_version_metadata_dummy_tid_load.sh` deterministically creates the scenario by dropping a bogus `txn_version.txt.tmp` into a part directory and running `DETACH` + `ATTACH`. Before the fix the server aborts with signal 6 at `TransactionID.h:90`; after the fix the rolled-back part is marked `Outdated` and invisible. Session: cron:clickhouse-ci-task-worker:20260418-221500
1 task
DatabaseReplicated: `query_log` lookup by `query_id` assumes single-node execution; replicated setup may miss entries or return duplicates from other replicas. AsyncInsert: injected `async_insert=1` queues the INSERT and returns immediately, so `peak_threads_usage` reflects only the queuing step, not the MV processing pipeline the test is designed to verify. CI report: #102961 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
ParallelReplicas settings alter query execution plans and thread allocation, making `peak_threads_usage` checks unreliable. S3 I/O threads (multi-part uploads, cache connections) inflate `peak_threads_usage` beyond the pipeline thread count the test measures. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102961&sha=43b3cf5f2a16a3ddc9683efa36016d69558b0531&name_0=PR PR: #102961 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Contributor
LLVM Coverage Report
Changed lines: 100.00% (4/4) · Uncovered code |
serxa
approved these changes
Apr 20, 2026
clickhouse-gh Bot
added a commit
that referenced
this pull request
Apr 21, 2026
Backport #102961 to 26.1: Use max_insert_threads for plain INSERTs without materialized views
clickhouse-gh Bot
added a commit
that referenced
this pull request
Apr 21, 2026
Backport #102961 to 26.2: Use max_insert_threads for plain INSERTs without materialized views
CheSema
added a commit
that referenced
this pull request
Apr 22, 2026
Backport #102961 to 26.3: Use max_insert_threads for plain INSERTs without materialized views
clickhouse-gh Bot
pushed a commit
that referenced
this pull request
Apr 30, 2026
Use max_insert_threads for plain INSERTs without materialized views
clickhouse-gh Bot
added a commit
that referenced
this pull request
Apr 30, 2026
Backport #102961 to 25.12: Use max_insert_threads for plain INSERTs without materialized views
CheSema
added a commit
that referenced
this pull request
Apr 30, 2026
Backport #102961 to 25.10: Use max_insert_threads for plain INSERTs without materialized views
kewin-robetti
pushed a commit
to viasoftkorp/ClickHouse
that referenced
this pull request
May 1, 2026
…thread-count Use max_insert_threads for plain INSERTs without materialized views
robot-ch-test-poll1
added a commit
that referenced
this pull request
May 6, 2026
Cherry pick #102961 to 25.8: Use max_insert_threads for plain INSERTs without materialized views
robot-clickhouse
added a commit
that referenced
this pull request
May 6, 2026
…thout materialized views
1 task done
antonio2368
added a commit
that referenced
this pull request
May 7, 2026
Backport #102961 to 25.8: Use max_insert_threads for plain INSERTs without materialized views
groeneai
added a commit
to groeneai/ClickHouse
that referenced
this pull request
May 13, 2026
Apply review feedback from @azat on ClickHouse#104697: - Revert the .sh conversion. The previous commit rewrote the test as a shell script in order to set `--receive_timeout=900` on the client; that bumped the client wait window past the 10-minute global per-test cap, so the test would bail at 10 min anyway. Restore the original `.sql` form. The "client `receive_timeout` is not derived from per-query SETTINGS" point from the bot still stands; the right answer here is not to extend the wait at all but to stop running the test where the wait is needed. - Add `no-tsan` to the test tags. The 1200+ column `metric_log` merge on `s3_no_cache` storage takes ~80s on a non-sanitizer local build and grows to several minutes under TSan; the test does not actually depend on TSan coverage, so skipping it on TSan removes the timeout failure mode without weakening any other build. Additionally, fix the MEMORY_LIMIT_EXCEEDED failure @azat flagged on `Stateless tests (arm_binary, sequential)` and several other non-TSan/non-MSan builds (`arm_asan_ubsan, azure`, `amd_asan_ubsan, db disk, distributed plan`, `amd_debug, sequential`, ...). All hit the same signature in PR ClickHouse#76867: Code: 241. DB::Exception: Query memory limit exceeded: would use 4.66 GiB (attempt to allocate chunk of 1.09 MiB), maximum: 4.66 GiB.. (MEMORY_LIMIT_EXCEEDED) (query: insert into metric_log select * from generateRandom() limit 10;) with the failure inside `MergeTreeDataPartWriterWide::addStreams` / `MergeTreeDataPartWriterOnDisk::initStreamsIfNeeded`. The 11/11-reproducible diagnosis run on `arm_binary` rules out sanitizer overhead. Root cause: since ClickHouse#102961 ("Use max_insert_threads for plain INSERTs without materialized views", merged 2026-04-20), the `INSERT INTO metric_log SELECT * FROM generateRandom()` fans out into `max_insert_threads` parallel writer threads. Each thread opens its own copy of all column streams and each stream pre-allocates a compress buffer (~`max_compress_block_size`) plus a default 1 MiB write buffer. With 1721 columns and the randomized `max_insert_threads = 3` the writer footprint is ~3 * 1721 * ~2 MiB ≈ 10 GiB worst case, well above the default 4.66 GiB per-query memory limit. Fix: pin `max_insert_threads = 1` at the top of the test. The test verifies merge behaviour controlled by `max_merge_delayed_streams_for_parallel_write`, not parallel insert performance, so collapsing the writer to a single thread preserves what the test checks while removing the OOM trigger. Verified locally on a debug build + MinIO with `s3_no_cache` storage and the same writer-randomized settings observed in the failing CI run (`--max_insert_threads=3`, `--max_memory_usage=4660000000`, `--min_compress_block_size=1873194`, `--max_compress_block_size=567430`): the `.sql` test passes in ~20s with the pinned setting in place. CI report (the MEMORY_LIMIT_EXCEEDED on `arm_binary` that @azat asked us to debug): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=76867&sha=8175ad6ae6252d416ba993505609e4fb0407c582&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20sequential%29
1 task done
groeneai
added a commit
to groeneai/ClickHouse
that referenced
this pull request
May 13, 2026
@azat asked for one-sentence comments instead of multi-line blocks. - no-tsan tag reason: collapsed 4 lines to 1, matching the style of the existing no-fasttest / no-flaky-check tag explanations - max_insert_threads pinning rationale: collapsed 10 lines to 1 sentence referencing ClickHouse#102961 as the cause of the memory blowup No behaviour change; comment-only edit. Review: ClickHouse#104697 (comment)
serxa
added a commit
to seva-potapov/ClickHouse
that referenced
this pull request
Jun 2, 2026
- Replace four stale "SHOULD_SPAWN handler" comment references in PipelineExecutor with "upscaling block": after the rework, `pushTasks` returns `size_t spawn_count` instead of a `SpawnStatus` enum, so the block that handles it is no longer a "SHOULD_SPAWN handler". `tasks.upscale` in spawnThreads still legitimately uses SpawnStatus and is left untouched. - Add 04229_lazy_concurrency_control_insert_with_mv stateless test covering the remaining slice of issue ClickHouse#102947 that PR ClickHouse#102961 did not reach: an INSERT with a materialized view asks for max_threads because views are involved, but under serial view processing the pipeline only needs a handful of threads. Pre-this-PR the eager ConcurrencyControl allocation grants all max_threads slots up front, inflating peak_threads_usage. Post-this-PR lazy allocation keeps it bounded by actual demand.
1 task done
serxa
added a commit
to seva-potapov/ClickHouse
that referenced
this pull request
Jun 19, 2026
The ternary `isViewsInvolved() ? max_threads : max_insert_threads` introduced as ClickHouse#102961 was a defensive workaround for the eager-slot allocation bug. With lazy `ConcurrencyControl` / `CPULeaseAllocation` in place (this PR), pipeline slot grants are demand-driven; `setNumThreads(max_threads)` on a 1-wide INSERT pipeline no longer translates into reserved-but-unused CPU slots.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ConcurrencyControl has an issue (#102947) where CC slots are allocated eagerly at query start, and
spawnThreadshas a race where it spawns threads faster than they can register as idle, resulting in many unnecessary threads being created.Until lazy CC slot allocation is implemented, we have no choice other than to set strict limits on the thread count for insert queries.
buildInsertPipelinewas requestingmax_threadsCC slots for all INSERTs regardless of pipeline width. For plain INSERTs without MVs the pipeline is 1-wide, so this wasted CC capacity and spawned unnecessary threads.Now we use
max_insert_threadswhen no MVs are attached, andmax_threadsonly when MVs are involved (their inner SELECTs benefit from full parallelism).Mitigates #102947
Changelog category (leave one):
Changelog entry
Plain INSERTs without materialized views no longer request excessive ConcurrencyControl slots and threads (
max_threadsinstead ofmax_insert_threads), preventing CC slot starvation and thread count blowup on clusters with high INSERT throughput.Documentation entry for user-facing changes
Version info
26.4.1.108126.3.10.29,26.2.17.14,26.1.12.10,25.12.11.4,25.10.7.6,25.8.24.5