Skip to content

perf(promql): Prewarm the TimeSeries tags deduplication cache when the table starts - #126132

Draft
JTCunning wants to merge 9 commits into
ClickHouse:masterfrom
JTCunning:jtcunning/timeseries-tags-cache-prewarm
Draft

JTCunning wants to merge 9 commits into
ClickHouse:masterfrom
JTCunning:jtcunning/timeseries-tags-cache-prewarm

Conversation

@JTCunning

@JTCunning JTCunning commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

A restarted server begins with an empty tags deduplication cache, so the server reinserted every series again, while also drastically reducing insert performance while it hydrated its cache.

StorageTimeSeries now reads the tags table into the cache during startup, up to the cache capacity.

The client time is one call that detaches, attaches, and inserts 10,000 series, so the prewarm offsets the faster insert. The insert CPU time and tag misses cover only the re-insert, with an empty cache before the prewarm and a filled cache after it.

Client time Insert CPU time Tag cache misses
Before 86.4 ms 29.3 ms 10,000
After 85.7 ms 20.7 ms 0

Changelog category (leave one):

  • Experimental Feature

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

The TimeSeries tags deduplication cache is filled from the tags table when the table starts, so a restarted server does not degrade on write performance when initially filling its cache.


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

A restarted server began with an empty cache, so the first inserts wrote every time series again. The table now reads the tags table into the cache during startup, up to the cache capacity.
@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [911d38a]

Summary: ❌

job_name test_name status info comment
Performance Comparison (arm_release, master_head, iceberg_local, 1/3) FAIL Performance dashboard
Insert historical data FAIL
Check Results FAIL
Performance Comparison (arm_release, master_head, iceberg_local, 2/3) FAIL Performance dashboard
Insert historical data FAIL
Check Results FAIL
Performance Comparison (arm_release, master_head, iceberg_local, 3/3) FAIL Performance dashboard
Insert historical data FAIL
Check Results FAIL
Performance Comparison (arm_release, master_head, iceberg_s3, 1/3) FAIL Performance dashboard
Insert historical data FAIL
Check Results FAIL
Performance Comparison (arm_release, master_head, iceberg_s3, 2/3) FAIL Performance dashboard
Insert historical data FAIL
Check Results FAIL
Performance Comparison (arm_release, master_head, iceberg_s3, 3/3) FAIL Performance dashboard
Insert historical data FAIL
Check Results FAIL

AI Review

Summary

The PR fills the TimeSeries tags deduplication cache during startup, with bounded reads and spread expiration times. ❌ Block: a truncated prewarm can cache an older value for a custom id and silently suppress a subsequent tag update.

Findings

❌ Blockers

  • StorageTimeSeries.cpp:572–573: DISTINCT ... LIMIT does not preserve the cache's latest-row-per-key invariant. With a custom id shared by changing tag rows, it can preload historical A while ReplacingMergeTree currently returns B. Reinserting A is then skipped, leaving FINAL at B. Reproduced on the PR-head fast-test binary with a one-entry cache, id UInt64 DEFAULT sipHash64(metric_name), index_granularity = 1, and two rows inserted together with optimize_on_insert = 0. The update remains suppressed after a merge removes historical A; clearing the cache restores the update. Seed only entries known to match the current row for their key, or exclude uncertain keys within the read budget.
Tests
  • ⚠️ Add a focused regression with multiple stored values for one cache key and a prewarm budget that stops before the current value. Verify that reinserting the older value changes the FINAL result, including after a merge.
Final Verdict

❌ Block. Preserve the latest-row invariant during bounded prewarming and add the regression above.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 15, 2026
@JTCunning
JTCunning marked this pull request as draft October 15, 2026 21:25
@JTCunning

Copy link
Copy Markdown
Contributor Author

A robot was over-eager. Dropped into draft.

@JTCunning JTCunning changed the title Prewarm the TimeSeries tags deduplication cache when the table starts perf(promql): Prewarm the TimeSeries tags deduplication cache when the table starts Oct 15, 2026
@JTCunning JTCunning added 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-experimental Experimental Feature labels Oct 15, 2026
@clickhouse-gh clickhouse-gh Bot removed the pr-improvement Pull request with some product improvements label Oct 15, 2026
Comment thread src/Storages/StorageTimeSeries.cpp Resolved
Comment thread src/Storages/StorageTimeSeries.cpp Resolved
The prewarm query keeps distinct series, and the remembered age stays inside the range steady_clock can subtract. Startup waits for the tags table by name.
Each timed repetition detaches the table, attaches it, and inserts the stored series, so the insert starts from a fresh cache.
@clickhouse-gh

clickhouse-gh Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 911d38a85 with master 22a323d93 (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.

Object file sizes

4 object files changed (+73.69 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/StorageTimeSeries.cpp.o 597.32 KiB 668.72 KiB +71.40 KiB (+11.95%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

Comment thread src/Storages/StorageTimeSeries.cpp Resolved
@JTCunning JTCunning added the pr-performance Pull request with some performance improvements label Oct 17, 2026
The startup read stops after one row per cache entry, one byte per cache byte, or ten seconds. Overflow mode is `break`. An internal process list entry applies the row and byte limits.
Comment thread src/Storages/StorageTimeSeries.cpp Resolved
JTCunning and others added 3 commits October 17, 2026 15:04
A loader thread has no thread group. `ProcessList::insert` requires one. The prewarm attaches a group with `ThreadGroupSwitcher` and restores the caller group.
…s-tags-cache-prewarm

# Conflicts:
#	src/Storages/StorageTimeSeries.cpp
Comment on lines +571 to +573
/// `LIMIT` then applies to distinct rows, so repeated rows of one series do not fill the cache.
select_query->distinct = true;
select_query->setExpression(ASTSelectQuery::Expression::LIMIT_LENGTH, make_intrusive<ASTLiteral>(static_cast<UInt64>(max_entries)));

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.

❌ Blocker: Preserve the latest row for each key when prewarming

DISTINCT removes identical rows, but does not choose the latest row for an id. With a custom id expression, different tag rows can share a key; this LIMIT can retain an older row and omit the current one. addWrittenRows then treats that older value as current, so a subsequent insert changing the tags back to it is silently skipped. This violates the cache's existing latest-row-per-key contract and changes query results.

Reproduced with the PR-head fast-test binary: use tags_deduplication_cache_size_bytes = 144, TAGS INNER COLUMNS (id UInt64 DEFAULT sipHash64(metric_name)), and ReplacingMergeTree ORDER BY (metric_name, id) SETTINGS index_granularity = 1. With optimize_on_insert = 0, insert ('m', {'job':'A'}) and ('m', {'job':'B'}) together, then detach/attach and insert A again. SELECT ... FINAL still returns B, and the physical tags count stays at two. After OPTIMIZE ... FINAL removes the old A row, another insert of A is still skipped and only B remains. Clearing the cache makes the same insert correctly produce A.

Please seed only rows known to represent the current value for their key, or conservatively exclude keys for which that cannot be established within the read budget. Add regression coverage for a bounded prewarm over multiple stored values of one key.

@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

PromQL Compliance Report

Baseline: S3 master 22a323d93

Suite Baseline Current Delta
Prometheus compliance 100.00% 100.00% +0.00%
Extended support (upstream engine tests) 93.17% 93.17% +0.00%
Suite Passed Failed Unsupported
Prometheus compliance 539 -> 539 (+0) 0 -> 0 (+0) 0 -> 0 (+0)
Extended support (upstream engine tests) 682 -> 682 (+0) 16 -> 16 (+0) 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

eoger-ant pushed a commit to eoger-ant/ClickHouse that referenced this pull request Nov 16, 2026
Since ClickHouse#128261 a PR that changes only performance tests runs the Iceberg
jobs on the changed tests only, so often no query is measured on both
servers (the Iceberg rewrite refuses every changed test, only partial
queries, or a skipped test). Then analyze_queries crashed:
query-run-metric-names.tsv is empty, so its scalar subquery throws
INCORRECT_RESULT_OF_SCALAR_SUBQUERY, and the per-query loop never
creates analyze/commands.txt and analyze/query-metric-stats.tsv. The
denorm file was never written, report() stopped at its first read,
before it wrote all-query-metrics.tsv and the other comparison files,
the check said "Errors while building the report." instead of the
per-test errors, and "Insert historical data" failed with
FileNotFoundError.

Create the two files before the loop, the same way confirm_changes
already does for its own files, and take any(n) from the names file:
an aggregate without GROUP BY returns one row, [] for an empty file and
the only row otherwise. The Total ratio of a report with no compared
query is the neutral 1x instead of nan, as for a test whose changes are
all demoted. A run with compared queries writes the same files as
before.

Validated by replaying compare.sh (stage=analyze_queries) in the
performance-comparison image on the CI artifacts of ClickHouse#101273, ClickHouse#113575
and ClickHouse#126132 (before: the CI errors; after: an empty errors.log, and
"20 errors" with the Iceberg rewrite hint for ClickHouse#101273), and on a
MergeTree job of ClickHouse#101273 (same analysis and report files).

Related: ClickHouse#101273

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

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-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... pr-experimental Experimental Feature pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants