Skip to content

TimeSeries: Add deduplication caches for TimeSeries metric families and tags - #121915

Merged
vitlibar merged 1 commit into
ClickHouse:masterfrom
vitlibar:timeseries-metric-families-deduplication-cache
Sep 28, 2026
Merged

vitlibar merged 1 commit into
ClickHouse:masterfrom
vitlibar:timeseries-metric-families-deduplication-cache

Conversation

@vitlibar

@vitlibar vitlibar commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Experimental Feature

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

Add deduplication caches for TimeSeries metric families and tags

The caches store hashes of the rows recently written to the metric families and tags tables, so the same rows aren't written with every insert. The tags table is deduplicated only if store_min_time_and_max_time is disabled.

An entry expires a fixed time after the row was written, so any difference between a cache and its table disappears within the expiration period.

The caches exist for tables of version 7 and later. They are configured by the *_deduplication_cache_max_entries and *_deduplication_cache_expiration_seconds settings, and cleared by TRUNCATE TABLE and by the new SYSTEM CLEAR TIME SERIES CACHES statement.


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

Version info

  • Merged into: 26.10.1.900-master (included in 26.10 and later)

@vitlibar vitlibar changed the title Add deduplication caches for TimeSeries metric families and tags TimeSeries: Add deduplication caches for TimeSeries metric families and tags Sep 24, 2026
@vitlibar vitlibar added the comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... label Sep 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fe7d43d]

Summary: ✅


AI Review

Summary

Adds deduplication caches for TimeSeries metric families and tags, plus SYSTEM CLEAR TIME SERIES CACHES and new cache settings. I cannot approve it yet because the new cache-reset paths still do not fence all insert-visible dedup state, so the recovery operations this PR introduces can still leave rows missing after a clear or a cache-setting change.

PR Metadata
  • Changelog category is correct: Experimental Feature.
  • Changelog entry is required and mostly specific, but it refers to nonexistent *_deduplication_cache_max_entries settings. Replace that sentence with: The caches exist for tables of version 7 and later. They are configured by the *_deduplication_cache_size_bytes and *_deduplication_cache_expiration_seconds settings, and cleared by TRUNCATE TABLE and by the new SYSTEM CLEAR TIME SERIES CACHES statement.
Findings

❌ Blockers

  • [src/Storages/TimeSeries/TimeSeriesDeduplicationCache.cpp:117-144, src/Storages/TimeSeries/TimeSeriesSink.h:114-116] SYSTEM CLEAR TIME SERIES CACHES still does not invalidate the per-insert pending_tags / pending_metric_families state. A multi-chunk insert can write key m in chunk 1, then a clear can run, and chunk 2 for the same key is still dropped from pending_rows before the cache is even consulted. That breaks the new recovery contract exactly in the case where the target table was edited or truncated between chunks. Suggested fix: tag pending_rows with a cache generation / clear epoch and discard stale pending state before the duplicate checks, or fence the clear against inserts entirely.
  • [src/Storages/StorageTimeSeries.cpp:470-474, src/Storages/TimeSeries/TimeSeriesSink.cpp:572-573, src/Storages/TimeSeries/TimeSeriesSink.cpp:787-788] [dismissed by author -- https://github.com/TimeSeries: Add deduplication caches for TimeSeries metric families and tags #121915#discussion_r4093565208] SYSTEM CLEAR TIME SERIES CACHES still swaps the tags and metric-families caches independently, and TimeSeriesSink snapshots them independently. An insert that starts while the clear is running can still observe one fresh cache and one stale cache, so one target table may keep suppressing rows after the recovery command. Suggested fix: replace both caches as one generation object or take an exclusive table-level fence around the clear.
  • [src/Storages/StorageTimeSeries.cpp:417-449, src/Storages/StorageTimeSeries.cpp:727-742, src/Storages/TimeSeries/TimeSeriesSink.cpp:572-573, src/Storages/TimeSeries/TimeSeriesSink.cpp:787-788] [dismissed by author -- https://github.com/TimeSeries: Add deduplication caches for TimeSeries metric families and tags #121915#discussion_r4111272336] ALTER TABLE ... MODIFY/RESET SETTING only swaps the storage-held cache pointer; inserts already in flight keep using the old cache object. So ..._deduplication_cache_size_bytes = 0, TTL changes, or size changes do not reliably take effect for those inserts even though the docs and tests in this PR say the cache is replaced with an empty one. Suggested fix: use the same insert-visible generation fence as the clear path, or block concurrent inserts while publishing the new cache generation.
Tests
  • ⚠️ The new coverage clears or alters caches only between whole inserts. It does not prove the advertised recovery behavior when a clear or cache-setting change is interleaved with an insert that already holds cache state. Please add at least one focused regression that splits an insert into multiple chunks, runs SYSTEM CLEAR TIME SERIES CACHES between chunks, and verifies the post-clear chunk is still written; and one companion test for a cache-setting ALTER.
Final Verdict
  • Status: ❌ Block
  • Minimum required actions: fence SYSTEM CLEAR TIME SERIES CACHES and cache-setting changes against both snapped cache pointers and per-insert pending_* state, then add focused regression coverage for the mid-insert CLEAR / ALTER interleavings above.

LLVM Coverage Report

Measured on commit fe7d43d.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.60% 79.50% -0.10%

Changed lines: Changed C/C++ lines covered: 412/421 (97.86%) · Uncovered code

Full report · Diff report

@vitlibar
vitlibar force-pushed the timeseries-metric-families-deduplication-cache branch from 4dda726 to 89195ac Compare September 24, 2026 10:02
Comment thread src/Storages/StorageTimeSeries.cpp Resolved
@vitlibar
vitlibar force-pushed the timeseries-metric-families-deduplication-cache branch from 314216e to 376408c Compare September 24, 2026 11:06
@clickhouse-gh

clickhouse-gh Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing fe7d43d71 with master 8ccea5b3a (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

38 object files changed (+232.92 KiB total), 1 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/TimeSeries/TimeSeriesDeduplicationCache.cpp.o new 176.49 KiB +176.49 KiB
src/CMakeFiles/dbms.dir/Storages/StorageTimeSeries.cpp.o 541.13 KiB 562.12 KiB +20.99 KiB (+3.88%)

716 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

600 translation units recompiled, 3760 s compile time in total, 599 of them have a recent master baseline.

Job report

Comment thread src/Storages/StorageTimeSeries.cpp Resolved
Comment thread src/Storages/StorageTimeSeries.cpp Resolved

@lukaszszafranski lukaszszafranski left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @vitlibar for sharing this change. Here is one finding from a source-only review of the PR head.

Veridical.dev reviewed the change:

  • Merge readiness — ⛔ Address the supported correctness finding before merging
  • Correctness — 🟠 Needs work
  • Security — Not assessed in this source-only pass
  • Tests — 🟡 Add an A → B → A metric-family metadata regression
  • Changed hunks with no finding — Not measured; the whole-review assessment did not complete

1 supported finding · source evidence attached

📝 Walkthrough

This PR adds bounded, expiring deduplication caches for the TimeSeries metric-family and tags targets. The sink filters recently written rows, marks them cached after a successful write, and clears the caches on truncation or through SYSTEM CLEAR TIME SERIES CACHES. It also adds settings and regression coverage for cache behavior.

File Change
src/Storages/TimeSeries/TimeSeriesDeduplicationCache.cpp Hashes full rows, filters cached hashes, and manages capacity and expiration.
src/Storages/TimeSeries/TimeSeriesSink.cpp Applies the caches to metric families and eligible tags, then marks successful writes.
src/Storages/TimeSeries/TimeSeriesSettings.cpp Defines cache controls, including the default 3600-second metric-family expiration.
tests/queries/0_stateless/05243_time_series_deduplication_caches.sql Covers repeated rows, changed descriptions, eviction, expiration, clearing, and disabled caches.
🗺️ How the changes interact
flowchart TD
A[TimeSeries insert] --> B[TimeSeriesSink]
B --> C[Metric-family row]
B --> D[Tags row when eligible]
C --> E[Full-row hash cache]
D --> E
E --> F{Hash already written?}
F -->|Yes| G[Skip target write]
F -->|No| H[Write target row]
H --> I[Mark hash written]
J[Expiration or clear] --> E
Loading

Supported defects

Severity Where Issue
🟡 Medium src/Storages/TimeSeries/TimeSeriesDeduplicationCache.cpp:79 A metric family's metadata changing A → B → A within the cache period suppresses the final A, leaving B as the latest stored description in the default target.

Nonblocking observations

None included in this review.

Findings that were reported by other bots

The live discussion already covers same-insert duplicates at cache capacity, cache clearing during concurrent inserts, an access-control test, and documentation. Those are separate from the metadata reversion issue here and are not reposted.

🔬 How the supported defect was verified

TimeSeriesSink::consumeMetricFamilies gives the cache metric_family, type, unit, and help. The cache hashes all four columns and retains both the A and B hashes; when A returns, written_hashes.contains(hash) filters it out. The default metric-family target uses ReplacingMergeTree ORDER BY metric_family, and the expiration setting defaults to 3600 seconds. The added test covers a changed description but not a change back to an earlier description.


Review scope: source-only, incomplete whole-review assessment

The reviewed head was eca7fdc047075c00b0d3117bb44c1da77000830e (30 changed files / 892 added lines). This finding was checked independently against the saved source and the then-live discussion. The broader review stopped at its selection checkpoint; this is not a claim that every change was fully adjudicated. No target code was built or executed.


Reviewed by Veridical · AI-assisted source review · whole-review assessment incomplete · Veridical.dev · [email protected]

@vitlibar
vitlibar force-pushed the timeseries-metric-families-deduplication-cache branch 3 times, most recently from f56b232 to d88370b Compare September 25, 2026 08:07
@vitlibar
vitlibar force-pushed the timeseries-metric-families-deduplication-cache branch from 167c4de to 74d8ebc Compare September 25, 2026 19:55
@vitlibar
vitlibar force-pushed the timeseries-metric-families-deduplication-cache branch from 74d8ebc to 067ed3e Compare September 25, 2026 20:49
@vitlibar
vitlibar force-pushed the timeseries-metric-families-deduplication-cache branch 2 times, most recently from 1c6351e to e7263d8 Compare September 26, 2026 11:12
Comment thread src/Storages/StorageTimeSeries.cpp Resolved
@vitlibar
vitlibar force-pushed the timeseries-metric-families-deduplication-cache branch from e7263d8 to 51f6d7e Compare September 26, 2026 19:36
@clickhouse-gh

clickhouse-gh Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 37 queries analysed

This PR adds deduplication caches for inserts into TimeSeries tables, plus a SYSTEM DROP TIME SERIES CACHES statement, new settings, metrics and docs. None of this code runs in ClickBench or TPC-H queries. The only flagged query, ClickBench Q33 at -5.2% (1182 ms to 1121 ms), was already classed as no change because it falls inside master's normal variance band, and its CPU time barely moved. That result matches the diff, so this PR shows no performance effect on either benchmark.

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 9cd9e19e-df4c-4614-aa50-2612b96fbe12
  • MIRAI run: 0714af6b-519e-4ea1-aa83-d921b96b2a9e
  • PR check IDs:
    • clickbench_2082016_1790509587
    • clickbench_2082030_1790509587
    • clickbench_2082036_1790509587
    • tpch_adapted_1_official_2082042_1790509587
    • tpch_adapted_1_official_2082049_1790509587
    • tpch_adapted_1_official_2082087_1790509587

The caches store hashes of the rows recently written to the metric families
and tags tables, so the same rows aren't written with every insert. The tags
table is deduplicated only if `store_min_time_and_max_time` is disabled.

An entry expires a fixed time after the row was written, so any difference
between a cache and its table disappears within the expiration period.
@vitlibar
vitlibar force-pushed the timeseries-metric-families-deduplication-cache branch from 7fea5f6 to fe7d43d Compare September 27, 2026 08:51
@clickhouse-gh

clickhouse-gh Bot commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

PromQL Compliance Report

Baseline: S3 master 8ccea5b3a

Suite Baseline Current Delta
Prometheus compliance 98.70% 98.70% +0.00%
Extended support (upstream engine tests) 84.43% 84.43% +0.00%
Suite Passed Failed Unsupported
Prometheus compliance 532 -> 532 (+0) 3 -> 3 (+0) 4 -> 4 (+0)
Extended support (upstream engine tests) 618 -> 618 (+0) 96 -> 96 (+0) 18 -> 18 (+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 Sep 28, 2026
Merged via the queue into ClickHouse:master with commit 92d3e39 Sep 28, 2026
340 of 341 checks passed
@vitlibar
vitlibar deleted the timeseries-metric-families-deduplication-cache branch September 28, 2026 09:54
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 28, 2026
valerypetrov added a commit to valerypetrov/ClickHouse that referenced this pull request Sep 29, 2026
`master` took version 7 of `TimeSeries` for the deduplication caches (ClickHouse#121915),
so the pinned timezone of the default recent samples partition key moves to
version 8: `MIN_WITH_UTC_PARTITION_KEY = 8`, `LATEST = 8`, and the version
history in `TimeSeriesVersion.h` and in the engine documentation gets both rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
valerypetrov added a commit to valerypetrov/ClickHouse that referenced this pull request Oct 1, 2026
Master's ClickHouse#121915 added deduplication caches for the metric families and tags
tables and took TimeSeries version 7. Resolved on master's structure:

- Version 7 stays master's deduplication caches; the separate "tags min max"
  target becomes version 8 (MIN_WITH_SEPARATE_TAGS_MIN_MAX = 8, LATEST = 8).
- The active series cache (TimeSeriesActiveSeriesCache, tags_cache_max_series,
  tags_cache_ttl_seconds) is dropped in favour of master's tags deduplication
  cache, which does the same job.
- The tags deduplication cache is now also used when min_time and max_time are
  kept in the "tags min max" table, which is what the split is for.
  checkTimeSeriesSettings and CREATE AS accept its settings from version 8.
- TimeSeriesSink takes master's version and gets the "tags min max" pipeline
  again; the bounds are written for every block, also when the tags rows were
  skipped by the cache.
- Tests: 05105 and 05239 tested the removed cache and are dropped; 05243 checks
  that a version 8 table deduplicates tags while its bounds still advance;
  04612, 05237, test_external_tags, the gtest and the docs moved to version 8.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

4 participants