Skip to content

Fix outdated data parts resurrection caused by incorrectly cleaning up empty covering parts - #98698

Merged
alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
tiandiwonder:fix_empty_parts_cleanup
Mar 15, 2026
Merged

alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
tiandiwonder:fix_empty_parts_cleanup

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Mar 4, 2026 •

Copy link
Copy Markdown
Contributor
`TRUNCATE` without a transaction creates an empty covering part (e.g.
`all_1_2_2`) to protect outdated data parts from resurrection. When the
table is later `DETACH`ed and `ATTACH`ed, `clearEmptyParts` is called
from `startup` before outdated data parts finish loading asynchronously.
At that point `getCoveredOutdatedParts(all_1_2_2)` returns an empty list
(the outdated parts are not yet in `data_parts_indexes`), so `all_1_2_2`
is wrongly dropped. Background cleanup then deletes it from disk. On the
next `ATTACH`, the previously outdated parts load as Active — zombie data.

Fix: guard `clearEmptyParts` with `outdated_data_parts_loading_finished`.
If outdated parts are still loading asynchronously, return early and let
`clearEmptyParts` be called again once loading completes. After loading
finishes all outdated parts are in memory, so `grabOldParts` can enforce
the correct physical deletion order (covered parts before the empty
covering part) via its own `getCoveredOutdatedParts` check.

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):

Fix outdated data parts resurrection caused by incorrectly cleaning up empty covering parts.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Note

Medium Risk
Touches MergeTree part cleanup during startup/attach; while the change is small, it affects data-part lifecycle ordering and could impact correctness if the loading state is mis-signaled.

Overview
Prevents MergeTreeData::clearEmptyParts() from removing empty covering parts until asynchronous outdated-part loading has finished, avoiding cases where covered outdated parts can later resurrect as active after DETACH/ATTACH or restart.

Updates existing stateless tests to ignore timing-dependent empty parts and adds a regression test (04007_truncate_empty_parts_detach_attach.sh) that reproduces the TRUNCATE→DETACH/ATTACH “zombie parts” scenario.

Written by Cursor Bugbot for commit b43ca3c. This will update automatically on new commits. Configure here.

Version info

  • Merged into: 26.3.1.675

@tiandiwonder tiandiwonder changed the title Fix outdated data parts resurrection after TRUNCATE + DETACH + ATTACH Fix outdated data parts resurrection due to empty covering data part has been cleaned up wrongly Mar 4, 2026
@tiandiwonder tiandiwonder changed the title Fix outdated data parts resurrection due to empty covering data part has been cleaned up wrongly Fix resurrection of outdated data parts caused by incorrectly cleaning up empty covering parts Mar 4, 2026
@tiandiwonder tiandiwonder changed the title Fix resurrection of outdated data parts caused by incorrectly cleaning up empty covering parts Fix outdated data parts resurrection caused by incorrectly cleaning up empty covering parts Mar 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b43ca3c]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.20% 83.80% +0.60%
Functions 23.60% 23.80% +0.20%
Branches 75.30% 76.30% +1.00%

PR changed lines: PR changed-lines coverage: 80.00% (8/10)
Diff coverage report
Uncovered code

@tiandiwonder
tiandiwonder enabled auto-merge March 11, 2026 23:43
@alexey-milovidov
alexey-milovidov merged commit 345c68d into ClickHouse:master Mar 15, 2026
159 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 15, 2026
alexey-milovidov added a commit that referenced this pull request Mar 27, 2026
The test relies on precise empty parts cleanup lifecycle. The random
DETACH/ATTACH from PR #96130 interferes because after reattachment,
`outdated_data_parts_loading_finished` is temporarily false, which
blocks `clearEmptyParts` (guard from PR #98698), preventing timely
cleanup and potentially resurrecting outdated parts.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=96130&sha=b69a3125f0167db133946240e9b08d368c9c842a&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
alexey-milovidov added a commit that referenced this pull request Mar 27, 2026
The test relies on precise empty parts cleanup lifecycle. The random
DETACH/ATTACH interferes because after reattachment,
`outdated_data_parts_loading_finished` is temporarily false, which
blocks `clearEmptyParts` (guard from PR #98698), preventing timely
cleanup and potentially resurrecting outdated parts.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=96130&sha=b69a3125f0167db133946240e9b08d368c9c842a&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
The test relies on precise empty parts cleanup lifecycle. The random
DETACH/ATTACH from PR ClickHouse#96130 interferes because after reattachment,
`outdated_data_parts_loading_finished` is temporarily false, which
blocks `clearEmptyParts` (guard from PR ClickHouse#98698), preventing timely
cleanup and potentially resurrecting outdated parts.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=96130&sha=b69a3125f0167db133946240e9b08d368c9c842a&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
The test relies on precise empty parts cleanup lifecycle. The random
DETACH/ATTACH interferes because after reattachment,
`outdated_data_parts_loading_finished` is temporarily false, which
blocks `clearEmptyParts` (guard from PR ClickHouse#98698), preventing timely
cleanup and potentially resurrecting outdated parts.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=96130&sha=b69a3125f0167db133946240e9b08d368c9c842a&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
GSokol pushed a commit to GSokol/ClickHouse that referenced this pull request Oct 28, 2026
…etree

`ALTER TABLE ... DETACH PARTITION 1` covers the inserted part with a new empty
part `1_1_1_1`. It is built from the covered part's columns, so it still has
column `c` (the test drops `c` only afterwards). `clearEmptyParts` outdates it
in memory only, and it stays on disk for `old_parts_lifetime` (480 s).

Every `local_query` is a separate `clickhouse-local` process over the same
path, and each one loads `1_1_1_1` as an active part again. At startup
`clearEmptyParts` hides it, but not while outdated parts are still loading
asynchronously (the guard from ClickHouse#98698). The `DELETE` leaves the outdated
`1_3_3_0` on disk, so from then on every process keeps `1_1_1_1` active until
the pending mutations rewrite it in the background. When that rewrite has not
committed before the `system.parts_columns` query, the query sees `c` and the
test prints `the rewrite dropped the column: 1` (seen in `Stateless tests
(arm_binary, parallel)` and `Fast test`).

Set `old_parts_lifetime = 0` for the test table, so the empty part is removed
from disk inside the `DETACH PARTITION` statement and no later process can
load it.

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

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

Development

Successfully merging this pull request may close these issues.

4 participants