Skip to content

Add time-series codecs to auto-created TimeSeries samples columns - #112110

Merged
nikitamikhaylov merged 4 commits into
masterfrom
timeseries-samples-default-codecs
Jul 28, 2026
Merged

nikitamikhaylov merged 4 commits into
masterfrom
timeseries-samples-default-codecs

Conversation

@nikitamikhaylov

@nikitamikhaylov nikitamikhaylov commented Jul 27, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

Auto-created timestamp and value columns of the TimeSeries samples inner table now default to CODEC(DoubleDelta, ZSTD(1)) and CODEC(Gorilla, ZSTD(1)) respectively.

Documentation entry for user-facing changes

Extracted from #111697 (rebased onto master so it can be reviewed independently). Companion PR: #112108 (parallelize the Prometheus query API).

Under generic LZ4, the near-monotonic millisecond timestamps of a scrape-like workload barely compress (ratio ~0.65) and dominate the samples inner table. On a ~15.7B-sample kube-mixin corpus (tsbench, scale 16) the timestamp column was 76.3 of 83.1 GiB — 92% of the table — and decompression+deserialization was ~45% of PromQL query CPU. DoubleDelta for timestamps and Gorilla (XOR) for float values are the standard encodings for this kind of data (the same family Prometheus uses natively).

Scope and compatibility:

  • Only columns the engine creates itself are affected; explicitly declared samples columns keep whatever codec the user wrote (or none).
  • Existing tables are untouched: on ATTACH the columns already exist in the stored DDL, so normalization keeps them as-is (round-trip covered by the new 04648_timeseries_samples_codecs test).
  • All types accepted by the samples-column validation are codec-compatible (timestamp: DateTime64/DateTime/UInt32; value: Float64/Float32).

Measured on the corpus above (debug build): samples storage 82.8 GiB → 30.6 GiB (5.68 → 2.09 bytes/sample); ingest of 15.7B samples unchanged within noise (1095 s → 1037 s); identical results on all 24 benchmark queries.

🤖 Generated with Claude Code

Version info

  • Merged into: 26.8.1.315 (included in 26.8 and later)

Under generic LZ4 the near-monotonic millisecond timestamps of a
scrape-like corpus barely compress (ratio ~0.65) and dominate the
samples table: 76.3 of 83.1 GiB (92%) on a 15.7B-sample benchmark
corpus, where decompression+deserialization was ~45% of query CPU.
Auto-created `timestamp` and `value` columns now default to
CODEC(DoubleDelta, ZSTD(1)) and CODEC(Gorilla, ZSTD(1)) respectively.
All types accepted by the type validation are compatible with these
codecs; explicitly declared columns keep the user's choice.

Co-Authored-By: Claude Fable 5 <[email protected]>
(cherry picked from commit ec97042)
@clickhouse-gh

clickhouse-gh Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f9eb85a]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_tsan, 1/6) FAIL
test_postgresql_replica_database_engine/test_2.py::test_remove_table_from_replication FAIL cidb IGNORED
test_postgresql_replica_database_engine/test_2.py::test_predefined_connection_configuration FAIL cidb IGNORED
test_postgresql_replica_database_engine/test_2.py::test_database_with_single_non_default_schema FAIL cidb IGNORED
test_postgresql_replica_database_engine/test_2.py::test_database_with_multiple_non_default_schemas_1 FAIL cidb IGNORED
test_postgresql_replica_database_engine/test_2.py::test_database_with_multiple_non_default_schemas_2 FAIL cidb IGNORED

AI Review

Summary

This PR makes auto-created TimeSeries samples columns use CODEC(DoubleDelta, ZSTD(1)) / CODEC(Gorilla, ZSTD(1)) and adds focused stateless coverage for the default schema, explicit inner columns, and DETACH/ATTACH. The implementation path and CI evidence look sound, but the user-facing docs are still inconsistent with the shipped behavior, so I would not approve it until the documentation contract is corrected.

Findings

⚠️ Majors

  • [src/Storages/StorageTimeSeries.cpp:950, docs/en/engines/table-engines/integrations/time-series.md:290] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/112110#discussion_r3658214718] The updated Adjusting types of columns section now states that leaving codecs out of explicit SAMPLES INNER COLUMNS uses the time-series defaults, but the new 04648_timeseries_samples_codecs regression proves the opposite: explicitly declared timestamp / value columns keep no codec at all. That means both the website page and system.table_engines will tell users to expect DoubleDelta / Gorilla compression that they will not actually get. Suggested fix: document that only engine-created columns receive those defaults, and that explicit inner columns must spell out CODEC(...) if users want them.
  • [src/Storages/StorageTimeSeries.cpp:797, docs/en/engines/table-engines/integrations/time-series.md:137] The samples-table type matrix still documents timestamp as only DateTime64(X), but normalizeTimeSeriesDefinition and 04600_timeseries_column_validation accept DateTime and UInt32 too. After this PR, that stale matrix directly contradicts the new codec note and the Array(Tuple(UInt32, Float32)) example above. Suggested fix: expand the documented timestamp type set in both mirrored doc surfaces to match the actual validation rules.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: fix the explicit-no-codec docs contract and update the documented supported timestamp types in the TimeSeries source/docs mirror.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jul 27, 2026
The auto-created `timestamp` and `value` samples columns now carry
`CODEC(DoubleDelta, ZSTD(1))` and `CODEC(Gorilla, ZSTD(1))`, so the
`extract` pattern `\(([^)]*)\)` truncated the `SAMPLES INNER COLUMNS`
block at the first closing parenthesis inside the codec. Anchor the
extraction at `) SAMPLES INNER ENGINE` instead and update the reference.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111697&sha=ec9704295fe958b3049ff9e841cf248d8a907345&name_0=PR&name_1=Fast%20test
PR: #111697

Co-Authored-By: Claude Fable 5 <[email protected]>
@nikitamikhaylov nikitamikhaylov added the comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... label Jul 27, 2026
@mintlify

mintlify Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
ClickHouse-docs 🟢 Ready View Preview Jul 27, 2026, 2:37 PM

@clickhouse-gh

clickhouse-gh Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This PR only changes normalization of the experimental TimeSeries table engine (auto-adding DoubleDelta/Gorilla codecs to engine-created columns), plus its docs and tests. None of that touches the query-execution path ClickBench Q4 runs, so it cannot plausibly cause a real speedup. The flagged -19.9% on Q4 shows essentially unchanged CPU (2700.9 vs 2703.9 ms), confirming it is run-to-run variance within master's current band; the deterministic no_change verdict is correct and consistent with the diff.

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 4cfd8871-6113-431b-88f7-8c2dce5efe06
  • MIRAI run: 948918d7-d014-4e54-8937-60c6f3af65e5
  • PR check IDs:
    • clickbench_701812_1785261710
    • clickbench_701819_1785261709
    • clickbench_701830_1785261710
    • tpch_adapted_1_official_701842_1785261709
    • tpch_adapted_1_official_701867_1785261711
    • tpch_adapted_1_official_701884_1785261709

/// (>90% of on-disk bytes on a scrape-like corpus). All types accepted by the validation
/// above are compatible: DoubleDelta takes DateTime64/DateTime/UInt32, Gorilla takes
/// Float64/Float32. Explicitly declared columns keep whatever the user wrote.
auto set_codec_for_new_column = [&](const char * codec_name)

@vitlibar vitlibar Jul 28, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This helper function doesn't really look good. Probably better to make add_column_if_missing return ASTColumnDeclaration * instead of bool and use it then to add codec to declaration.

Address review comment: instead of a bool and a helper reaching into
new_list->children.back(), return the newly created declaration (nullptr
if the column already existed) and set the codec on it directly. Also
simplifies the ephemeral all_tags column setup the same way.

#112110 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
| `timestamp` | [x] | `DateTime64(3)` | `DateTime64(X)` | A time point |
| `value` | [x] | `Float64` | `Float32` or `Float64` | A value associated with the `timestamp` |

Columns the engine creates itself get time-series compression codecs:

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.

The Possible types cell above is still incomplete for the new codec-default contract. normalizeTimeSeriesDefinition accepts DateTime and UInt32 for timestamp too, and 04600_timeseries_column_validation exercises the UInt32 case, so this table now contradicts both the implementation and the time_series Array(Tuple(UInt32, Float32)) example earlier on the page. Please expand the documented timestamp type set here and in the mirrored docs/en page to match the actual validation rules.

@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.30% 86.30% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 78.40% 78.40% +0.00%

Changed lines: Changed C/C++ lines covered: 70/70 (100.00%) · Uncovered code

Full report · Diff report

@nikitamikhaylov

Copy link
Copy Markdown
Member Author

@groeneai help investigate test failures. As usual, link existing PRs or fix yourself.

@nikitamikhaylov
nikitamikhaylov added this pull request to the merge queue Jul 28, 2026
Merged via the queue into master with commit 2734eda Jul 28, 2026
177 of 179 checks passed
@nikitamikhaylov
nikitamikhaylov deleted the timeseries-samples-default-codecs branch July 28, 2026 21:54
@groeneai

Copy link
Copy Markdown
Collaborator

Not caused by this PR. The one red check is Integration tests (amd_tsan, 1/6) with Failures: 5/948, and those 5 rows are a single server death, not 5 test failures.

Decomposition. All 5 failures are in test_postgresql_replica_database_engine/test_2.py, and all 5 carry Code: 210. DB::NetException: Connection refused (172.16.2.4:9000) raised inside the autouse setup_teardown fixture (test_2.py:65-68), whose pg_manager.restart() runs DROP DATABASE IF EXISTS ... against the ClickHouse instance. That file has exactly 6 tests; test_add_new_table_to_replication passed and the other 5 are simply every test that ran after the server was gone. So the count 5 is "the server died after the first test", not a measure of five distinct problems.

Root cause, from the TSan report in the job artifacts. The context of test_database_with_multiple_non_default_schemas_2 contains one SUMMARY: ThreadSanitizer: data race (0 Logical error, 0 Received signal, 0 std::bad_alloc), and the racing pair is the materialized_tables map of DatabaseMaterializedPostgreSQL:

  • Read, async-metrics thread T167 (mutexes: M0): DatabaseMaterializedPostgreSQL::getTablesIterator DatabaseMaterializedPostgreSQL.cpp:537 <- ServerAsynchronousMetrics::updateImpl ServerAsynchronousMetrics.cpp:439 <- AsynchronousMetrics::update <- AsynchronousMetrics::run.
  • Previous write, DROP thread T8 (mutexes: M1, M2): DatabaseMaterializedPostgreSQL::detachTablePermanently DatabaseMaterializedPostgreSQL.cpp:489 <- InterpreterDropQuery::executeToTableImpl InterpreterDropQuery.cpp:310 <- TCPHandler::runImpl.

Line 537 is the materialized_tables.empty() test in the early-return of getTablesIterator, which is read outside tables_mutex; the lock is taken only two lines later at :546 for the copy loop. Line 489 is materialized_tables.erase(table_name), which mutates the same map while holding handler_mutex (:466) and never tables_mutex. The mutex sets TSan prints are disjoint for exactly this reason. On origin/master tables_mutex is taken at only 3 places (:266, :546, :576). Classifying every access to materialized_tables by whether it falls inside one of those three lock scopes gives 5 guarded and 10 unguarded, so :537 is not the only one: :149, :155, :259, :286, :417, :446, :483, :489, :510 are in the same condition, including the other .erase at :483 and the .clear() at :510.

This is worse than a sanitizer-only report. The container is std::map<String, StoragePtr> and the erase destroys a map node while the metrics thread is reading the tree, so in a build without TSan this is a genuine use-after-free rather than only a diagnostic.

Why it is not yours. The PR changes 7 files (StorageTimeSeries.cpp, normalizeTimeSeriesDefinition.cpp, the time-series doc page, and 2 stateless test pairs); nothing under src/Databases/ or src/Interpreters/ServerAsynchronousMetrics.cpp. DatabaseMaterializedPostgreSQL.cpp is byte-identical at the PR head and at its merge base. Breadth over 30 days for this suite: 3243 failing rows across 27 distinct PRs, including 92 rows on 4 distinct master commits. Keyed on the exact frame pair above, today alone gives 6 unrelated PRs plus 3 rows on 2 master commits (edf159f617d5, 5854921c25b0). Note the frame pair is only visible in 0.59% of the rows, because the full sanitizer text survives ingestion for one row per run, so frame-keyed counts understate the real spread by roughly two orders of magnitude.

Owner. No fixing PR exists yet. #110886 is the only open PR reworking this area, and it does not fix this: it moves materialized_tables.clear() into a nested handler_mutex scope in stopReplication but adds no tables_mutex acquisition anywhere, so both sides of this race are untouched. A fix task is investigating at full effort, and I will post the fixing-PR link here when it opens. Nothing is needed on this PR.

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112110&sha=f9eb85aa73767a4395f350ced48717ec7fbfd121&name_0=PR&name_1=Integration%20tests%20%28amd_tsan%2C%201%2F6%29
Master hit with the same frame pair: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=edf159f617d53feb776fd2701e21183b690cd38c&name_0=MasterCI&name_1=Integration%20tests%20%28amd_tsan%2C%201%2F6%29

@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 28, 2026
nikitamikhaylov added a commit that referenced this pull request Jul 29, 2026
The samples inner table of a newly created TimeSeries table now also has
the `locality_hash` column, so the test added on master in
#112110 lists one more column.

Co-Authored-By: Claude Fable 5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator

Fixing PR for the test_postgresql_replica_database_engine Connection-refused cascade dispositioned to a fix task on this PR's CI finish ledger: #112381 (merged), with the re-land #112382.

Root cause: #109339 added a materialized_tables traversal to DatabaseMaterializedPostgreSQL::getTablesIterator. The asynchronous-metrics thread reaches that iterator (ServerAsynchronousMetrics.cpp:447, and this database reports isExternal() == false so it is not skipped) while stopReplication clears the same map holding only handler_mutex, never tables_mutex. That is a heap-use-after-free on the stored wrappers, it killed the server, and every remaining test in the module then failed inside the autouse setup_teardown fixture with Code: 210 Connection refused. One server death per job, not N independent flaky tests.

Timeline: #109339 merged 2026-07-28 13:16 UTC, the master cascade began the same evening, #112381 reverted it 2026-07-29 01:39 UTC. Every failing build contained the introducing commit and lacked the revert. True-master runs since the revert are clean: 0 failures against 6804 passing rows across 23 distinct commits. #112382 re-lands the feature with tables_mutex taken on all materialized_tables touch points, including the swap-and-destroy-outside-the-lock form in stopReplication.

No action is needed on this PR: its diff never touched PostgreSQL replication and did not cause this line.

This branch was successfully deployed

1 active deployment
staging - docs — f9eb85aa Deployed Jul 28, 2026 by mintlify[bot]
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-performance Pull request with some performance improvements 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