Skip to content

Size the first block read from a large compact part by preferred_block_size_bytes - #132445

Open
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:compact-part-first-block-size
Open

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:compact-part-first-block-size

Conversation

@groeneai

@groeneai groeneai commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #124553

Changelog category (leave one):

  • Performance Improvement

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

A read without PREWHERE from a compact part with more rows than max_block_size now sizes its first block by preferred_block_size_bytes when it reads a column of variable size such as String, Array or Map, instead of always reading max_block_size rows. This lowers the peak memory and the time of such reads.

Description

Since #124553, queries 1 and 3 of the map_subcolumns_prewhere performance test (compact part) are about 30% slower on master (CI history: 0.34 -> 0.46 s and 0.35 -> 0.44 s, peak memory 264 -> 456 MB), as measured by @ snelheidai on #124553.

Root cause: a compact part has no sizes of individual columns, and the block size predictor is built from empty sample columns, so it estimates every column of variable size at 0 bytes per row until the first block is read. The first block of every read task therefore has max_block_size rows. Since #124553 the whole Map is read there instead of 16 key subcolumns.

Change: for a compact part with more rows than max_block_size, read without PREWHERE, row policy or lightweight-delete steps, the pool passes the uncompressed size of the part per row, and the predictor uses it as the minimum estimate of the first block only. Wide parts, smaller parts and subcolumn-only reads are unchanged. So are PREWHERE reads, query 5 included: there the Map is read only by PREWHERE, and the predictor never measures such columns (a different mechanism).

Validation: the new test fails on master (first block of 8192 rows) and passes with the fix, 50/50 with randomized settings. Release build, ratio = PR / master, 60 runs per side: query 1 0.72 and query 3 0.71 (peak memory 1350 -> 206 MB and 1247 -> 140 MB), wide and PREWHERE queries within noise, and a narrow read of one String out of 30 over 100 compact parts of 100000 rows 0.98 (1 thread) and 0.96 (32 threads). SELECT * of a 10M-row compact part: 0.57-0.81 over four 15-run comparisons.

Whether the part-size estimate is acceptable for all such reads is the MergeTree owner's call.


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

…k_size_bytes

A compact part has no sizes of individual columns, and the block size
predictor is built from empty sample columns, so a column of variable size
was estimated at 0 bytes per row until the first block was read. The first
block of every read task then had max_block_size rows regardless of
preferred_block_size_bytes. Since ClickHouse#124553 a basic Map is read whole there
instead of as key subcolumns, which made map_subcolumns_prewhere ClickHouse#1 and ClickHouse#3
about 30% slower on compact parts and raised their peak memory.

For a compact part with more rows than max_block_size and a task without
pre-read steps (PREWHERE, row policy, lightweight delete, on-the-fly
mutation, index read), the pool now passes the uncompressed size of the
part per row (getBytesUncompressedOnDisk, the same whole-part charge the
columns cache estimate uses). The predictor raises bytes_per_row_current
to it once, when a whole column of variable size is read. The first update
re-initializes the estimate from the measured block as before; if the first
read is filtered out entirely and never reaches update, the next startBlock
restores the estimate without the floor, so filtering readers that are not
pre-read steps (MergeTreeReaderIndex) keep the old behaviour from the second
read on. The size is computed in the pool from the concrete part because
the predictor must stay on the reader abstraction.

Parts with at most max_block_size rows are read in one block per task
either way and are left alone, as are wide parts, PREWHERE reads and reads
of subcolumns only.

Reported and first measured by snelheidai on ClickHouse#124553.

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

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding), plus my own review of the code and the description.

# Sev Finding Verdict Evidence / action
1 ⚠️ The test does not discriminate: query 3 (n) passes on master, query 2 allows blocks up to 2047 rows, and the reset after a fully filtered first read is not exercised DISAGREE Query 2 is the check that fails on master (first block of 8192 rows there, 256 with the fix). Query 3 is the inverse check: it fails if the estimate outlives the first block (every block would be one 256-row granule); the observed sizes {256, 3360, 8192} show it does not. The < 2048 / > 2048 margins keep task splits and parallel-replica segments from failing it. The reset only changes block sizes after a fully filtered first read, never results; a SQL check of it would have to expect the old max_block_size read after the filtered one, so I did not add one.
2 💡 Description: #1/#3 autolinked to unrelated issues, the changelog entry did not say it applies to reads without PREWHERE, and no reason was given for leaving query 5 alone AGREE, fixed Description and changelog entry reworded.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261117-180700

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @CheSema, could you review this? A compact part has no per-column sizes, so the first block of every read task had max_block_size rows; for a large compact part read without PREWHERE, the pool now passes the part's uncompressed size per row as the predictor's first-block estimate.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes: query 2 of 05821_block_size_predictor_compact_part prints 0 on master (first block of 8192 rows), every run.
b Root cause explained? A compact part has no column sizes and the predictor's sample columns are empty, so a column of variable size is estimated at 0 bytes per row and the first block of each task gets max_block_size rows.
c Fix matches root cause? Yes: the pool passes the part's uncompressed size per row and the predictor uses it as the minimum estimate of the first block only.
d Test intent preserved / new tests added? New stateless test; its third query checks that later blocks still use the measured size.
e Both directions demonstrated? Fails on master with the inverse diff, passes with the fix; 50/50 randomized, 20 runs with the columns cache, 20 with parallel replicas.
f Fix is general across code paths? One predictor construction site for every read pool (buildReadTaskInfo); merges and mutations use no predictor.
g Fix generalizes across inputs (params/datatypes/wrappers)? Any whole column of variable size (String, Array, Map measured); subcolumn-only and fixed-size-only reads, smaller parts and PREWHERE reads are unchanged.
h Backward compatible? (maintainer-approved exception only) Yes: no setting, format or protocol change; only the size of the first block changes.
i Invariants and contracts preserved? update re-initializes from the measured block as before; each task's copy has its own one-shot state, dropped on the next startBlock if the first read was filtered out entirely.

Session id: cron:clickhouse-impl-slot-4:20261117-145300

@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d666316]

Summary: ✅


AI Review

Summary

Reviewed commit d6663167eee4ff5ddec5f5c4e4fc2f5ca4ba58c7. The PR uses a compact part's uncompressed bytes per row to size the first block of eligible reads, then returns to measured estimates. No actionable findings. The focused stateless regression passes in CI, and the supplied release-build comparisons demonstrate reduced query time and peak memory, including checks for narrow-column regressions.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements can be tested Allows running workflows for external contributors comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing d6663167e with master 95bfedb4d (stripped binary size, per-symbol sizes and ThinLTO time; 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.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.64 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 6.4 s 15.1 s +11.0 s (+266%)
Compile time of recompiled translation units

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

Job report

This branch has not been deployed

No deployments
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 comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant