Skip to content

Cancel background merges early in DROP DATABASE for ordinary shared merge tree - #98161

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
tiandiwonder:cancel_merge_when_drop
Feb 27, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
tiandiwonder:cancel_merge_when_drop

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Feb 27, 2026 •

Copy link
Copy Markdown
Contributor

In InterpreterDropQuery::executeToDatabaseImpl(), currently flushAndPrepareForShutdown() is not override, so we depends StorageMergeTree::shutdown() to cancel background merges in executeToTableImpl() in sequence

So we override StorageMergeTree::flushAndPrepareForShutdown() to cancel background merges early in prepare stage in parallel.

            /// List the tables, then call flushAndPrepareForShutdown() on them in parallel, then call
            /// executeToTableImpl on them in sequence.

Note: we already override StorageReplicatedMergeTree::flushAndPrepareForShutdown() for replicated merge tree.

Changelog category (leave one):

  • Improvement

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

Cancel background merges early in DROP DATABASE for ordinary shared merge tree.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.3.1.120

@clickhouse-gh

clickhouse-gh Bot commented Feb 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [12d7923]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Feb 27, 2026
@tiandiwonder tiandiwonder changed the title Speed up DROP DATABASE by cancelling background merge for ordinary shared merge tree Cancel background merges early in DROP DATABASE for ordinary shared merge tree Feb 27, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Feb 27, 2026
Merged via the queue into ClickHouse:master with commit 78a7f5f Feb 27, 2026
287 of 288 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 27, 2026
pda added a commit to buildkite-forks/ClickHouse that referenced this pull request Sep 11, 2026
`TRUNCATE ALL TABLES` used the database shutdown-preparation path before
truncating each storage. After PR ClickHouse#98161 moved permanent `MergeTree`
merge cancellation into `flushAndPrepareForShutdown`, this left live
tables unable to merge and caused later `OPTIMIZE FINAL` queries to fail
with `ABORTED`.

Route filtered and unfiltered `TRUNCATE TABLES` through the existing
per-table truncate path. Keep shutdown preparation unchanged for `DROP`,
`DETACH`, and `TRUNCATE DATABASE`.

Add a regression that truncates a `MergeTree`, inserts two new parts,
runs `OPTIMIZE FINAL`, and verifies that they merge into one active part.

Regression associated with:
ClickHouse#98161
pda added a commit to buildkite-forks/ClickHouse that referenced this pull request Sep 11, 2026
The unfiltered `TRUNCATE ALL TABLES FROM db` form used the database
shutdown-preparation path before truncating each storage. After PR ClickHouse#98161
moved permanent `MergeTree` merge cancellation into
`flushAndPrepareForShutdown`, this left live tables unable to merge and
caused later `OPTIMIZE FINAL` queries to fail with `ABORTED`.

Route the unfiltered form through the existing per-table truncate path
already used by filtered `TRUNCATE TABLES ... LIKE` queries. Keep shutdown
preparation unchanged for `DROP`, `DETACH`, and `TRUNCATE DATABASE`.

Add a regression that truncates a `MergeTree`, inserts two new parts,
runs `OPTIMIZE FINAL`, and verifies that they merge into one active part.

Regression associated with:
ClickHouse#98161
pda added a commit to buildkite-forks/ClickHouse that referenced this pull request Sep 11, 2026
PR ClickHouse#98161 moved permanent `MergeTree` merge and move cancellation into
`flushAndPrepareForShutdown`. The unfiltered `TRUNCATE ALL TABLES FROM db`
form used this database shutdown-preparation path before truncating each
storage, leaving retained tables unable to merge and causing later
`OPTIMIZE TABLE ... FINAL` queries to fail with `ABORTED`.

Source-built probes at the exact PR boundary confirm the attribution. Its
first parent passes unfiltered, ordinary, and filtered truncate probes. The
merge commit fails only the unfiltered probes with Code 236, while the
ordinary and filtered controls continue to pass.

Route the unfiltered form through the existing per-table truncate path
already used by filtered `TRUNCATE TABLES ... LIKE` queries. Keep shutdown
preparation unchanged for `DROP`, `DETACH`, and `TRUNCATE DATABASE`.

This also aligns unfiltered truncation with `LIKE '%'` for databases where
`shouldBeEmptyOnDetach` is false and for read-only database metadata disks,
instead of retaining the previous no-op or per-table `DETACH` behavior.

Add a regression that truncates a `MergeTree`, inserts two new parts,
runs `OPTIMIZE FINAL`, and verifies that they merge into one active part.

Regression introduced by:
ClickHouse#98161
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product 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