Skip to content

allow non-transactional removal of a part with unknown creation_csn - #109537

Closed
Diskein wants to merge 2 commits into
masterfrom
dk-smt-fix-non-transactional-removal-validation
Closed

Diskein wants to merge 2 commits into
masterfrom
dk-smt-fix-non-transactional-removal-validation

Conversation

@Diskein

@Diskein Diskein commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Fix a logical error thrown when a non-transactional TRUNCATE covers a part whose creating transaction is not committed yet.

Logical error: 'Object test_2.t_truncate_optimize_race (...)|all_12_25_7, creation_csn is not set while removal_csn is set to 1'.

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 LOGICAL_ERROR when TRUNCATE TABLE removes a MergeTree part whose creating transaction has not committed yet.

@clickhouse-gh

clickhouse-gh Bot commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [aa88f8e]

Summary: ✅


AI Review

Summary

This PR relaxes VersionMetadata::validateInfo so a non-transactional remover can cover a part whose creation_csn is still unknown, and it adds a regression test for TRUNCATE. The narrow TRUNCATE case makes sense, but the validator change is global and currently also enables other non-transactional DDL paths to externalize uncommitted parts, so I do not think this is safe to merge as-is.

Findings

❌ Blockers

  • [src/Interpreters/MergeTreeTransaction/VersionMetadata.cpp:516] This now makes creation_csn == UnknownCSN + non-transactional removal valid for every non-transactional remover, not only TRUNCATE. DETACH PARTITION clones the selected active parts into detached/ before covering them ([src/Storages/StorageMergeTree.cpp:2802-2823]), and MOVE PARTITION TO TABLE clones them into the destination table before covering the source ([src/Storages/StorageMergeTree.cpp:3239-3283]). Both paths still enumerate active parts without MVCC filtering when there is no current transaction ([src/Storages/MergeTree/MergeTreeData.cpp:6512-6528] and [src/Storages/MergeTree/MergeTreeData.cpp:7946-7958]), so an uncommitted insert part is eligible there. Before this patch those cases stopped on the logical error; after it they can complete and leak uncommitted rows outside the creating transaction.
    Suggested fix: either narrow the new allowance to the TRUNCATE/empty-coverage paths that never externalize the covered part, or make the externalizing DDL entrypoints skip parts whose creation_csn is still unknown.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: prevent the new allowance from letting DETACH / move-partition paths externalize uncommitted parts, either by narrowing the validator exemption or by filtering those DDL paths.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 6, 2026
@Diskein
Diskein requested a review from tuanpach July 6, 2026 16:22
@alexey-milovidov

Copy link
Copy Markdown
Member

Updating the branch to fix the 04001_join_reorder_through_expression test, which was broken today in master.

/// check below.
bool non_transactional_removal = info.removal_tid.isNonTransactional();

if (info.removal_csn && !(non_transactional_removal && info.removal_csn == Tx::NonTransactionalCSN))

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.

This makes creation_csn == UnknownCSN + non-transactional removal valid for every non-transactional remover, not just TRUNCATE. That changes the behavior of callers that externalize the source part before covering it. For example, DETACH PARTITION clones each selected active part into detached/ at src/Storages/StorageMergeTree.cpp:2802-2823, and MOVE PARTITION TO TABLE clones it into the destination table at src/Storages/StorageMergeTree.cpp:3239-3283. Their part selection still goes through getVisibleDataPartsVector* without MVCC filtering when there is no current transaction (src/Storages/MergeTree/MergeTreeData.cpp:6512-6528 and src/Storages/MergeTree/MergeTreeData.cpp:7946-7958), so an uncommitted insert part is eligible there. Before this patch those cases stopped on the logical error; after it they can complete and leak uncommitted rows outside the creating transaction. I think the exemption needs to be narrowed to the TRUNCATE/empty-coverage paths, or those externalizing DDLs need to stop selecting parts whose creation_csn is still unknown.

@clickhouse-gh

clickhouse-gh Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.60% 85.60% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 77.80% 77.80% +0.00%

Changed lines: Changed C/C++ lines covered: 4/9 (44.44%) · Uncovered code

Full report · Diff report

@tuanpach tuanpach self-assigned this Jul 6, 2026
@tuanpach

tuanpach commented Jul 7, 2026

Copy link
Copy Markdown
Member

I think we should only remove committed parts when truncating with non-tx query.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants