Skip to content

TimeSeries: use 256 KiB granules and ALP, ZSTD(4) for the values of the samples table by default - #131678

Merged
vitlibar merged 4 commits into
ClickHouse:masterfrom
groeneai:timeseries-samples-granularity-256k
Nov 16, 2026
Merged

vitlibar merged 4 commits into
ClickHouse:masterfrom
groeneai:timeseries-samples-granularity-256k

Conversation

@groeneai

@groeneai groeneai commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #131459
Related: #117344

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Description

Since #117344 a row of the TimeSeries samples table holds one hour of one series, so the default index_granularity_bytes = 524288 makes a granule about 135 hours of one 15 s series, and a short PromQL window older than the recent samples table reads whole granules. This changes the default of samples_index_granularity_bytes to 262144, and the generated samples.value codec of tables with buckets of samples from ALP, ZSTD(3) to ALP, ZSTD(4): for blocks under 128 KiB, level 3 decodes random decimal digits slower (#131459). Existing tables keep their definitions. Adds the perf test promql_selector_old_short_window.

Data set of #131459, CI amd_release of 9b41283 (before #122919), 15 interleaved repetitions, max_threads = 4; ratio to master, geometric mean, 95% CI:

Query master, ms 262144 only This PR
O19 rate(node_cpu_seconds_total[5m]), 1 h, 5 days back 1044 0.660 0.668 (0.659-0.677)
O23b sum(count_over_time(node_cpu_seconds_total[6h])), 5 days back 1143 0.681 0.684 (0.672-0.697)
Other windows 5 days back 38-1205 0.76-0.88 0.77-0.82
W1 rate(node_disk_written_bytes_total[5m]) by instance, 7 days 200 1.089 0.957 (0.940-0.975)
F6 the same, 14 days 249 1.224 1.043 (1.029-1.058)
Other 7/14-day scans 250-447 1.10-1.15 0.97-1.03

Costs: marks double, the samples table grows by 0.15%; INSERT takes 13% more CPU and OPTIMIZE FINAL 14% (ZSTD(4) accounts for 6 and 2 points). Whole-metric scans (promql_selector_id_range_row_cost #0, #1) at 262144 vs 524288, on master with #131766: 1.01x in the ARM CI perf comparison, 1.04-1.07x on x86 (1.2-1.3x since #122919, before #131766); the short windows of promql_selector_old_short_window are 0.64-0.69x.

Not for changelog: version 11 and the setting (#117344) are only on master.


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

Version info

  • Merged into: 26.11.1.2419-master (included in 26.11 and later)

A row of the samples table holds one hour of one series (ClickHouse#117344), so with
`index_granularity_bytes = 524288` a granule is about 135 hours of one 15 s
series and a short PromQL window older than the recent samples table reads
whole granules. Change the default of `samples_index_granularity_bytes` to
262144 and update its documentation and the tests that check the default.

Measured on the CI amd_release binary of 9b41283 with the data set of
ClickHouse#131459 (19,800 series, 14 days, byte counters with random decimals),
max_threads = 4, 15 interleaved reps, ratio = 262144 / 524288:
- O19 rate(node_cpu_seconds_total[5m]), 1 h range 5 days back: 0.670
- O23b sum(count_over_time(node_cpu_seconds_total[6h])): 0.696
- W1 / W3 / F4 / F6 long scans: 1.170 / 1.140 / 1.181 / 1.266
- marks 49,282 -> 99,299, samples table on disk +1.0%

Existing tables keep their stored `index_granularity_bytes`. The setting
exists only on master, so no compatibility handling is needed.

Adds the perf test promql_selector_old_short_window.xml.

Closes: ClickHouse#131459

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 (3 nits).

# Sev Finding Verdict Evidence / action
1 ❌ CREATE TABLE copy AS src of a version 11 table created with the old default keeps 524288 (removeGeneratedInnerEngine compares with the current default) DISAGREE Version 11 and the setting are in no release (not in 26.10, no 26.11 branch), so such a source exists only on master builds of the last weeks, and master gives the copy 524288 today too. A 524288 declared in SAMPLES ENGINE ... SETTINGS yields the same stored definition, so it cannot be told apart from the generated one. No change.
2 💡 The description should name the long scans that do not get slower, the 384 KiB option and the perf test's expected direction AGREE Added to the description (W7, 393216, 0.67 / 1.09).
3 💡 ON CLUSTER over hosts on pre- and post-change builds can disagree on index_granularity_bytes of a Replicated inner table DISAGREE Only possible between master builds; a Replicated database replicates the normalized query. No change.

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

Session id: cron:clickhouse-review-slot-8:20261114-195500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @nikitamikhaylov, could you review this? TimeSeries: default samples_index_granularity_bytes 524288 -> 262144 (short PromQL windows older than the recent samples table about 30% faster, long random-decimal counter scans 14-27% slower)

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Same binary (CI amd_release of 9b41283), identical rows in four tables differing only in samples_index_granularity_bytes: O19 reads 0.518x the rows (SelectedRows) at 262144, F6 reads 0.999x. Timings over 15 interleaved repetitions have 95% CIs of 1-2%.
b Root cause explained? A samples row holds one hour of one series, so a 512 KiB granule holds ~135 hours of one series (6,652,800 rows / 49,282 marks). The primary key skips granules, not rows, so a short window reads whole granules; 256 KiB granules hold 67 rows.
c Fix matches root cause? It changes the default of the knob #117344 added for this table and nothing else.
d Test intent preserved / new tests added? The gtest, 05140, 05141 and test_index_granularity assert the new default; new perf test promql_selector_old_short_window covers a one-hour window, a 6-hour window and a 7-day control.
e Both directions demonstrated? 05140 and 05141 fail on unpatched master on exactly the changed line and pass here (100/100 randomized runs). Perf test on one binary: one-hour window 230 -> 150 ms, SelectedRows 0.511; 7-day control SelectedRows 1.000.
f Fix is general across code paths? Only new tables without an explicit setting or a declared engine index_granularity_bytes change. The recent samples table (recent_samples_index_granularity_bytes), tables of version 10 or earlier and non-MergeTree inner engines are unaffected.
g Fix generalizes across inputs (params/datatypes/wrappers)? Measured 128/256/384/512 KiB: 384 KiB keeps the long scans (1.01) but gains less (short windows 0.89); 128 KiB gains no more than 256 KiB (0.78 vs 0.77) and costs more on long scans (1.21 vs 1.19). An explicit setting or a declared engine setting still wins (gtests pass). My A/B covers the random-decimal data set; the issue measured the compressible and integer sets.
h Backward compatible? (maintainer-approved exception only) Yes. The setting is not in any release (#117344 is master-only); the value is pinned in each stored definition, so existing tables keep 524288 after ATTACH (checked with a table created by 9b41283); samples_index_granularity_bytes = 524288 restores the old layout.
i Invariants and contracts preserved? Existing tables keep their stored granularity; query results match up to float summation order (at most 1.9e-15 relative) across all four values and 16 queries.

Session id: cron:clickhouse-impl-slot-6:20261114-185800

@clickhouse-gh

clickhouse-gh Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1d8b351]

Summary: ✅


AI Review

Summary

The PR changes new bucketed TimeSeries tables to use 256 KiB samples granules and ALP, ZSTD(4) for sample values, updates the documentation and expected definitions, and adds a short-window performance test. Reviewed PR head 1d8b3516b4b1108f6d4177857dc31bac50a4d820 in merge checkout e43e26fa4717a507231b382fc42c57784ea48fb6. No new code defects found. The selector fix from PR #131766 is present and removes the identified double-counting trigger for full-range scans, but verification of the previously reported ARM regression remains incomplete.

Tests
  • ⚠️ Complete ARM performance validation before accepting the smaller default at TimeSeriesSettings.cpp:35. The existing review thread documented approximately 29% slower promql_selector_id_range_row_cost queries #0 and Repair subtree2 #1. The author's measurements with the selector fix reduce this to 5–7% on x86, but explicitly exclude ARM. At review completion, the current-head ARM comparisons are pending; AMD comparisons are skipped without the pr-performance label. Publish ARM before/after results for those two queries and promql_selector_old_short_window to establish that the full-scan regression is resolved while the short-window benefit remains. This is the outstanding validation of the existing concern, so no duplicate inline comment was posted.
Final Verdict

Status: ⚠️ Request changes. Provide the ARM measurements after the merged selector fix. If they still show a significant full-scan regression, address it or retain the larger default.

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog can be tested Allows running workflows for external contributors comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... labels Nov 14, 2026
…h ALP, ZSTD(4)

With 262144-byte granules, a granule of the samples table gives ZSTD about
100 KB of ALP output instead of about 200 KB. zstd picks the parameters of a
level by input size: at level 3, inputs up to 128 KiB use a minimum match of
5 bytes (4 bytes between 128 and 256 KiB), so more repeated bytes of values
with random decimal digits stay literals, and zstd Huffman-codes them, which
decodes much slower. Level 4 uses a minimum match of 4 at this size.

The generated `samples.value` codec of tables with buckets of samples (version
11, both the samples and the recent samples table) becomes `ALP, ZSTD(4)`.
Versions 10 and earlier keep `ALP, ZSTD(3)`, and so do existing tables: their
stored definitions declare the codecs.

Measured on the CI amd_release binary of 9b41283 (identical rows,
15 interleaved reps, max_threads = 4, ratio to 524288 + ZSTD(3)):
7/14-day random-decimal counter scans {W1, W3, F4, F6} 1.144 -> 0.993;
O19/O23b 0.671 -> 0.676; samples table on disk +1.0% -> +0.15%;
INSERT +3.8% wall / +5.9% user CPU and OPTIMIZE FINAL +2% vs ZSTD(3) at the
same granularity. Results identical up to float summation order.

Proposed and measured on x86 and Graviton 3 in ClickHouse#131459.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai changed the title TimeSeries: use 256 KiB granules for the samples table by default TimeSeries: use 256 KiB granules and ALP, ZSTD(4) for the values of the samples table by default Nov 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 1d8b3516b with master f3a76d3bc (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.

Compile time of recompiled translation units

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

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - b9603e0

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Performance Comparison (arm_release, master_head, 6/6) / promql_selector_id_range_row_cost #0 regression this PR's smaller default exposes (+29.3%, reproduced after restart; no other PR showed it in 30 days): since #122919 the block size predictor also counts the PREWHERE-only raw samples column of the TimeSeries selector, so the read blocks shrink about 2x at 262144-byte granules, as the PR description says a fix task is created (investigating at full effort, fixing PR link to follow on this PR)
Finish Workflow / Post Hooks review threads gate: one blocking review thread is unresolved PR-caused by design and owned by this PR (#131678): the resolution is addressing the blocking review thread(s)

Session id: cron:our-pr-ci-monitor:20261115-040014

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the promql_selector_id_range_row_cost row of the CI finish ledger above: #131766. The selector now emits its notEmpty(samples) filter only for time ranges shorter than a bucket, so for longer ranges the raw samples column is no longer PREWHERE-only and the block-size predictor counts it once. On x86 (release, master vs #131766 on byte-identical parts), row_cost #0 is 0.81/0.76 of master at 262144-byte granules and 0.86/0.81 at 131072.

@vitlibar vitlibar self-assigned this Nov 15, 2026
@vitlibar

vitlibar commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

@groeneai After merging #131766, check if that regression is gone

… settings

Review feedback: instead of a separate getDefaultBucketedValueCodec(),
getDefaultValueCodec() takes the TimeSeries settings and returns ALP, ZSTD(4) for
tables with buckets of samples and ALP, ZSTD(3) otherwise. No behaviour change.

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

Copy link
Copy Markdown
Collaborator Author

Will do. Once #131766 is merged I'll push here to re-run CI (it builds the merge with master) and post the promql_selector_id_range_row_cost #0/#1 results of both perf comparisons. Locally (x86, release, master + #131766, identical parts) they are 1.05-1.07x at 262144 vs 524288, and 1.18-1.29x without #131766.

CI builds the merge commit with master, which now contains ClickHouse#131766, so the
performance comparisons measure this PR's defaults with the selector fix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
valerypetrov pushed a commit to valerypetrov/ClickHouse that referenced this pull request Nov 16, 2026
…shorter than a bucket

The selector of a table with buckets of samples wrapped every read in
`WHERE notEmpty(samples)` over `timeSeriesSliceSortedArray(samples, a, b)`.
Filter push-down moves that condition into PREWHERE, which reads the raw
`samples`, computes the slice and drops the raw column, so the raw column
is PREWHERE-only. Since ClickHouse#122919 `MergeTreeBlockSizePredictor` counts such a
column at its part estimate on top of the measured slice copy: for a long
window (the slice is the whole array) bytes per row double, and with
granules under 512 KiB a block holds about one granule instead of two or
three. Whole-metric selectors then process about twice as many blocks
(`promql_selector_id_range_row_cost` #0/ClickHouse#1 1.3x slower with 262144-byte
granules, ClickHouse#131678).

The filter is a no-op unless the range is shorter than a bucket: every
sample of a row lies in [bucket, bucket + step), `min_time`/`max_time` are
the smallest/largest of them, and the inner query keeps a row only if
`max_time >= a AND min_time <= b`. If `min_time >= a` the sample at
`min_time` is in the range, if `max_time <= b` the sample at `max_time` is,
so an empty slice needs `min_time < a AND b < max_time`, i.e.
`b - a < max_time - min_time < step`. With a bound absent the same holds for
the other bound. So the filter is now emitted only when both bounds are set
and `b - a < step` in the units of the table being read (the samples table
or the recent samples table, each with its own step). The difference is
computed in Int128: the bounds of a DateTime64 table can be anywhere in
[INT64_MIN, INT64_MAX]. For long windows nothing on `samples` goes to
PREWHERE, the raw column is read by the main step and measured once, and
the slice is computed after it.

The bucket step at the table scale is moved into `getBucketStep`, shared
with `makeBucketedSamplesTimeRangeConditions`.

Measured (release build, master vs this change concurrently on identical
parts, 2 x 25 interleaved runs, max_threads = 4): row_cost #0 0.81/0.76 at
262144, 0.86/0.81 at 131072, 0.93/0.84 at 524288; ClickHouse#1 0.79/0.73,
0.88/0.83, 0.99/0.91; promql_selector_old_short_window 0.94-0.97. Results
identical.

Edge: a row written directly with `min_time`/`max_time` looser than its
samples (against the documented contract) can now be returned as an empty
array by `timeSeriesSelector(..., 'samples')` for a long range. PromQL drops
series whose merged array is empty, as it does for rows emptied by the
stale-marker filter.

Tests: 05770_time_series_selector_empty_slice_filter checks both sides of the
bucket boundary and the plan shape (no-parallel-replicas: without a local
plan the initiator's EXPLAIN does not show the replicas' filters). New perf
test promql_selector_small_granules: the row_cost shape at 131072-byte
granules, the default of the recent samples table.

Related: ClickHouse#131678
Related: ClickHouse#122919

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

Copy link
Copy Markdown
Collaborator Author

The regression is gone. ARM perf comparison of 1d8b351 (merged with master f3a76d3, which contains #131766): promql_selector_id_range_row_cost #0 114 -> 115 ms and #1 126 -> 127 ms (report); before #131766 (b9603e0) they were 119 -> 154 and 130 -> 168 ms. The short windows of promql_selector_old_short_window keep their gain: #0 305 -> 202 ms, #1 314 -> 216 ms (report).

CI ran only the ARM perf comparison, so I also measured x86 locally (master CI amd_release of the #131766 merge, identical parts, 2x25 interleaved runs): row_cost #0/#1 are 1.04-1.07x at 262144 vs 524288, against 1.24-1.30x on the binary before #131766; old_short_window #0/#1 are 0.64-0.68x.

@clickhouse-gh

clickhouse-gh Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

PromQL Compliance Report

Baseline: S3 master f3a76d3bc

Suite Baseline Current Delta
Prometheus compliance 100.00% 100.00% +0.00%
Extended support (upstream engine tests) 93.03% 93.17% +0.14%
Suite Passed Failed Unsupported
Prometheus compliance 539 -> 539 (+0) 0 -> 0 (+0) 0 -> 0 (+0)
Extended support (upstream engine tests) 681 -> 682 (+1) 17 -> 16 (-1) 34 -> 34 (+0)

Extended support (upstream engine tests): native-histogram exclusions: 149; excluded assertions: 248; upstream 3da2514eff5c.

Note: Prometheus compliance score is unchanged vs baseline.

Uploaded compliance JSON for this PR commit

@vitlibar
vitlibar added this pull request to the merge queue Nov 16, 2026
Merged via the queue into ClickHouse:master with commit f6f46e6 Nov 16, 2026
378 of 379 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 16, 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 comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TimeSeries: 256 KiB samples granules make PromQL windows older than the recent table up to 33% faster

3 participants