Skip to content

Fix logical error on TRUNCATE of a TimeSeries table in a Replicated database - #117902

Merged
nikitamikhaylov merged 3 commits into
ClickHouse:masterfrom
Algunenano:fix-timeseries-truncate-replicated-db-txn
Sep 5, 2026
Merged

nikitamikhaylov merged 3 commits into
ClickHouse:masterfrom
Algunenano:fix-timeseries-truncate-replicated-db-txn

Conversation

@Algunenano

@Algunenano Algunenano commented Sep 3, 2026 •

Copy link
Copy Markdown
Member

Closes: #117900
Related: #115965

TRUNCATE TABLE on a TimeSeries table inside a Replicated database failed with LOGICAL_ERROR: Cannot add ZooKeeper operation because query is executed. It's a bug.:

CREATE DATABASE d ENGINE = Replicated('/clickhouse/databases/d', 's1', 'r1');
SET allow_experimental_time_series_table = 1, default_table_engine = 'ReplicatedMergeTree';
CREATE TABLE d.ts ENGINE = TimeSeries;
TRUNCATE TABLE d.ts;  -- Code: 49

A distributed DDL entry carries one ZooKeeperMetadataTransaction, and it can be consumed only once: moveOpsTo drains the ops and marks the transaction executed. StorageTimeSeries::truncate truncates each inner table with a separate nested query and handed the same transaction to every one of them, so the first inner ReplicatedMergeTree consumed it in dropAllPartsInPartitions and the second one threw. It fired even on empty tables, because the ops always carry the alter_partition_version and log set requests.

The nested truncates no longer receive the transaction. DDLWorker::tryExecuteQuery already commits a transaction that the query left unexecuted, so the entry is now marked finished after every inner table is truncated instead of after the first one. If the server dies in between, the entry is executed again, which is safe because TRUNCATE is idempotent. Callers that touch a single owned table, such as StorageMaterializedView::truncate, keep propagating the transaction and are unchanged.

Not addressed here: the inner tables are still truncated in separate ZooKeeper transactions, so a concurrent reader can observe a partially truncated series. That window is not new, and closing it needs a single multi spanning all the inner tables.

In CI this showed up in stress tests, where ignore_drop_queries_probability rewrote the DROP TABLE of 05028_time_series_default_table_engine into a TRUNCATE. It hit six unrelated pull requests within a day.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed the logical error Cannot add ZooKeeper operation because query is executed when running TRUNCATE TABLE on a TimeSeries table in a Replicated database.


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

Version info

  • Merged into: 26.9.1.801 (included in 26.9 and later)

…atabase

`StorageTimeSeries::truncate` truncates every inner table with a separate
nested query, and all of them were given the same
`ZooKeeperMetadataTransaction`. A transaction can be consumed only once, so
the second inner table threw `Cannot add ZooKeeper operation because query is
executed`.

Do not hand the transaction to the nested truncates. `DDLWorker` commits it
after the whole query instead, so the entry is marked finished once every
inner table is truncated. If the server dies in between, the entry is
executed again, which is safe because `TRUNCATE` is idempotent.
@clickhouse-gh

clickhouse-gh Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bf5bfeb]

Summary: ✅


AI Review

Summary

This PR fixes the reused ZooKeeperMetadataTransaction path behind TRUNCATE TABLE on TimeSeries tables in a Replicated database and adds a focused regression test. The C++ change looks sound, but the follow-up tests/clickhouse-test change only rewrites default_table_engine = 'Replicated*MergeTree' when replace_replicated is set, so the SMT rewrite remains incomplete in the cloud path.

Findings

⚠️ Majors

  • [tests/clickhouse-test:542] _replace_default_table_engine is gated behind self.replace_replicated, but --cloud clears replace_replicated_with_shared before switch_to_smt() constructs this replacer. A cloud transform of 01166_truncate_multiple_partitions.sql still leaves set default_table_engine='ReplicatedMergeTree';, so cloud-eligible tests can keep creating ReplicatedMergeTree tables instead of Shared*MergeTree.
    Suggested fix: run _replace_default_table_engine in the cloud path too, or gate it on self.replace_replicated or self.cloud.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Make the default_table_engine rewrite apply in the cloud SMT path as well, not only when replace_replicated is set.

LLVM Coverage Report

Measured on commit bf5bfeb.

Metric Baseline Current Δ
Lines 88.70% 88.70% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 81.10% 81.10% +0.00%

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

Full report · Diff report

A DDL query on a `Replicated` database returns a per-replica status, so the
test printed an extra line under the default `distributed_ddl_output_mode`.
Comment thread src/Storages/StorageTimeSeries.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bf5bfeb7c with master 323d42647 (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

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

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... label Sep 4, 2026
`SharedEngineReplacer` only rewrote engines named in an `ENGINE =` clause, so
inner tables generated from `default_table_engine` stayed replicated and the
test failed on SMT disks with `ReplicatedMergeTree doesn't work with
's3_with_keeper' disk type`. Rewrite the setting value as well and accept both
engine families in the assertion.
Comment thread tests/clickhouse-test
line = line.replace(engine_from, engine_to)
break

line = self._replace_default_table_engine(line)

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.

_replace_default_table_engine only runs inside the self.replace_replicated branch, but the --cloud setup clears args.replace_replicated_with_shared before switch_to_smt() constructs this replacer. That means cloud-mode transforms still leave implicit Replicated*MergeTree selections untouched. For example, running the current logic on 01166_truncate_multiple_partitions.sql still produces set default_table_engine='ReplicatedMergeTree';, so a cloud-eligible test can keep generating ReplicatedMergeTree tables even though the SMT rewrite path is supposed to move those tests onto Shared*MergeTree.

Can we apply this rewrite in the cloud path too (or gate it on self.replace_replicated or self.cloud) so default_table_engine behaves consistently across all SMT rewrites?

@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

PromQL Compliance Report

Baseline: S3 master 323d42647

Metric Baseline Current Δ
Score 78.29% 78.29% +0.00%
Passed 422 422 +0
Failed 3 3 +0
Unsupported 114 114 +0

Note: score is unchanged vs baseline.

Uploaded compliance JSON for this PR commit

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-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

3 participants