Skip to content

Apply filesystem_prefetches_limit and filesystem_prefetch_max_memory_usage to merges and mutations - #129042

Merged
CheSema merged 5 commits into
masterfrom
merge-prefetch-limit
Nov 4, 2026
Merged

CheSema merged 5 commits into
masterfrom
merge-prefetch-limit

Conversation

@CheSema

@CheSema CheSema commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Merges and mutations prefetched every column with no limit, so one merge of a wide table could take the whole remote reader pool. Now they use the same limits as queries: a merge or mutation does not prefetch if it needs more prefetches, one per column of each part, than filesystem_prefetches_limit, or if their estimated memory is larger than filesystem_prefetch_max_memory_usage.

Closes: #127403
Related: #129022
Related: #127400

Changelog category (leave one):

  • Improvement

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

Merges and mutations now respect filesystem_prefetches_limit and filesystem_prefetch_max_memory_usage, so a merge of a wide table on remote storage no longer takes the whole remote reader thread pool.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/129042]

Version info

  • Merged into: 26.11.1.1364-master (included in 26.11 and later)
  • Backported to: 26.10.4.207

@CheSema
CheSema requested a review from alesapin November 3, 2026 16:15
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1807eaf]

Summary: ✅


AI Review

Summary

The PR adds prefetch admission checks for merges and mutations using filesystem_prefetches_limit and filesystem_prefetch_max_memory_usage. Reviewed head 1807eaf7f6138729b393f66bca43c4ff91446357. Four previously reported accounting gaps remain in the current code; each author's decision is recorded below. Verdict: request changes.

Findings

⚠️ Majors

  • A vertical merge can spend its prefetch budget twice. MergeTask.cpp:2217: the current and next-column pipelines independently receive the full budget, and both prefetch before the current pipeline is consumed. Two parts with filesystem_prefetches_limit = 2 can therefore submit four outstanding prefetches. Account for both pipelines together. [dismissed by author -- https://github.com/Apply filesystem_prefetches_limit and filesystem_prefetch_max_memory_usage to merges and mutations #129042#discussion_r4467518509] The bounded factor of two still violates a configured limit and can exhaust a smaller remote reader pool.

  • Logical-column accounting misses physical streams. MergeTreeSequentialSource.cpp:627: two parts with one eight-stream Tuple column and 1 MiB buffers are estimated at 4 MiB, although initial prefetch buffers alone can require 16 MiB. An 8 MiB limit admits them. Count distinct physical streams and cap each stream separately. [dismissed by author -- https://github.com/Apply filesystem_prefetches_limit and filesystem_prefetch_max_memory_usage to merges and mutations #129042#discussion_r4457667727] Matching the query pool's approximation still permits large underestimates for multi-stream columns.

  • Cached disks can enlarge buffers after admission. MergeTreeSequentialSource.cpp:619: with force_read_through_cache_for_merges = 1 and filesystem_cache_prefer_bigger_buffer_size = 1, DiskObjectStorage::prepareRead can raise a 1 MiB buffer to a 16 MiB prefetch_buffer_size. Two sufficiently large single-column parts pass an 8 MiB limit with a 4 MiB estimate but allocate 32 MiB for initial prefetches. Estimate the effective buffer size. [dismissed by author -- https://github.com/Apply filesystem_prefetches_limit and filesystem_prefetch_max_memory_usage to merges and mutations #129042#discussion_r4457667736] Cache bypass being the default does not cover this supported configuration.

  • Admission excludes readers added during preparation. MergeTreeSequentialSource.cpp:616: subsequent preparation adds _row_exists, DEFAULT dependencies, and patch readers, all inheriting permission to prefetch. Two parts requesting two columns pass a count limit of four even when delete masks require six column reads. Account for the prepared read sets and patch readers across parts. [dismissed by author -- https://github.com/Apply filesystem_prefetches_limit and filesystem_prefetch_max_memory_usage to merges and mutations #129042#discussion_r4467518505] Dependency expansion and accumulated patch parts can exceed the estimate substantially.

Tests
  • ⚠️ 05493_merge_prefetch_limits covers horizontal merges of scalar Wide parts on s3_no_cache. Add focused cases for overlapping vertical pipelines, multi-stream columns, enlarged cache buffers, expanded read sets, and mutation admission. Assert the relevant count or memory boundary, beyond whether any prefetch occurred.
Missing context / blind spots
  • ⚠️ Runtime reproduction was unavailable because the checkout has no built ClickHouse binary. The findings above follow the current call paths; focused S3 regression runs would provide runtime confirmation.
Final Verdict

⚠️ Request changes. Account for simultaneous pipelines and the actual readers, streams, and buffer sizes before admission, with focused regression coverage. All retained findings already have inline threads; the author's resolutions are preserved.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Nov 3, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Nov 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 1807eaf7f with master c8383042c (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of b306e87de; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

12 translation units recompiled, 96 s compile time in total, 12 of them have a recent master baseline.

Job report

@alesapin alesapin self-assigned this Nov 4, 2026
A merge or mutation does not prefetch if it needs more than `filesystem_prefetches_limit`
prefetches, one per column of each part. `05493` reads one table with 10 columns:
it ran longer than 180 s in the flaky check.
@CheSema
CheSema marked this pull request as ready for review November 4, 2026 12:10
Comment thread src/Storages/MergeTree/MergeTask.cpp Resolved
Comment thread src/Storages/MergeTree/MergeTask.cpp Resolved
@CheSema
CheSema enabled auto-merge November 4, 2026 17:35
@CheSema
CheSema added this pull request to the merge queue Nov 4, 2026
Merged via the queue into master with commit 5ae88f4 Nov 4, 2026
185 checks passed
@CheSema
CheSema deleted the merge-prefetch-limit branch November 4, 2026 18:52
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 4, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 4, 2026
Master renamed the prefetch parameter of the merge sequential source to
prefetch_begin_of_range and added allow_remote_fs_prefetch. This PR no
longer prefetches in the constructor of the source, so the first one is
dropped and the second one is kept.
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 13, 2026
@CheSema CheSema removed pr-synced-to-cloud The PR is synced to the cloud repo pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Nov 13, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 13, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 13, 2026
Backport #129042 to 26.10: Apply filesystem_prefetches_limit and filesystem_prefetch_max_memory_usage to merges and mutations
@CheSema CheSema removed the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 16, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 16, 2026
@clickhouse-gh clickhouse-gh Bot added comp-background-ops Background pools/scheduling for merges, cleanup, maintenance. and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 22, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-background-ops Background pools/scheduling for merges, cleanup, maintenance. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR v26.6-must-backport v26.7-must-backport v26.8-must-backport v26.9-must-backport v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Merges prefetch every column without a limit and can take the whole remote reader pool

5 participants