Skip to content

Sync Introduce Invalidated System Columns - #111801

Merged
Michicosun merged 3 commits into
masterfrom
sync_invalidated_virtual_columns
Jul 27, 2026
Merged

Michicosun merged 3 commits into
masterfrom
sync_invalidated_virtual_columns

Conversation

@Michicosun

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

During attach/detach/move/clone part manipulations, materialized into part block_number and block_offset become stale. This patch changes the behavior to regenerate these columns.

Fix is implemented as a materialization of an additional file, invalidated_system_columns.txt, which will not be included in packed/compact archives and will be written during attach/clone partition operations.

@clickhouse-gh

clickhouse-gh Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b6c3333]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_tsan, s3 storage, parallel, 1/3) FAIL
04625_hash_table_sizes_stats_table_expression_modifiers FAIL cidb

AI Review

Summary

This PR introduces invalidated_system_columns.txt so parts cloned through ATTACH / MOVE / REPLACE can stop trusting stale persisted _block_number / _block_offset files, and it teaches readers, part min-max loading, skip-index loading, and projection loading to synthesize or skip those stale values. That direction is correct, but the current head still has two correctness gaps: some clone paths still clear the invalidation marker by default, and mixed-version replication silently drops the marker for old peers.

PR Metadata
  • Changelog category should be Bug Fix, not Improvement: this PR fixes user-visible stale _block_number / _block_offset results after existing MergeTree part operations.
  • The current Changelog entry is too implementation-heavy for CHANGELOG.md. A better replacement is: Fixed stale _block_number and _block_offset values after ATTACH, MOVE, REPLACE, and related part-clone operations in MergeTree tables.
Findings

❌ Blockers

  • [src/Storages/MergeTree/IDataPartStorage.h:278, src/Storages/MergeTree/IMergeTreeDataPart.cpp:1746-1750, src/Storages/StorageReplicatedMergeTree.cpp:5598-5602, src/Storages/MergeTree/MutateTask.cpp:3420-3426, src/Storages/MergeTree/MergeTreeData.cpp:10797-10808] ClonePartParams still defaults invalidated_columns_to_write to an empty set, and writeInvalidatedSystemColumnsFile interprets that as “delete the marker.” The current tree still relies on that default in the local-replica clone fetch path, the untouched-mutation fast path, and FREEZE, so cloning an already-invalidated part through any of those flows strips the marker and makes the stale persisted _block_number / _block_offset files authoritative again. Suggested fix: preserve the source marker by default, or explicitly pass the source invalidation set / {BlockNumberColumn::name, BlockOffsetColumn::name} in every clone and freeze caller that is meant to keep invalidated semantics.
  • [src/Storages/MergeTree/DataPartsExchange.cpp:272-274] The new replication protocol version gate silently omits invalidated_system_columns.txt when the peer only speaks the pre-v10 protocol. During a rolling upgrade, an old replica can therefore fetch an invalidated part from a new replica and accept the stale _block_number / _block_offset files as if they were valid, so replicas can diverge on query results and on any projections or skip indices derived from those columns. Suggested fix: fail closed when an invalidated part is about to be sent to an old peer, or materialize fresh system-column files before transfer.
Tests
  • ⚠️ Add a focused regression where an invalidated part goes through the untouched-mutation clone path (UPDATE ... WHERE 0, or any mutation that leaves the part unchanged). The new stateless test only covers mutations that rewrite rows, so it does not exercise the still-broken fast path.
  • ⚠️ Add a compatibility test for an old replication-protocol peer fetching an invalidated part, or a unit test around sendPart that proves the code rejects or materializes instead of silently dropping the marker.
Final Verdict
  • Status: ⚠️ Request changes

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 24, 2026
@Michicosun

Copy link
Copy Markdown
Member Author

No need to review. Changes were reviewed in the private repo.

bool make_source_readonly = false;
DiskTransactionPtr external_transaction = nullptr;
std::optional<int32_t> metadata_version_to_write = std::nullopt;
NameSet invalidated_columns_to_write = {};

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 new field defaults to an empty set, but freeze / freezeRemote now always rewrite invalidated_system_columns.txt. That means any clone caller that still uses the default does not preserve the source part's invalidation marker; it actively clears it in the clone.

There are already such callers in the current tree (MutateTask's !any_rows_affected fast path, StorageReplicatedMergeTree's local-clone fetch shortcut, FREEZE). If one of those paths clones a part that was invalidated by ATTACH / MOVE / REPLACE, the new part falls back to the stale persisted _block_number / _block_offset files again.

Can this default be changed to "preserve the source marker unless the caller explicitly overrides it", or can those clone callers be updated in the same patch?

&& name == IMergeTreeDataPart::COLUMNS_SUBSTREAMS_FILE_NAME)
continue;

if (client_protocol_version < REPLICATION_PROTOCOL_VERSION_WITH_INVALIDATED_SYSTEM_COLUMNS

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.

Skipping invalidated_system_columns.txt for older replication-protocol peers is not just feature loss: it drops the only metadata that tells the receiver the stored _block_number / _block_offset files are stale.

During a rolling upgrade, a new replica can create an invalidated part (for example via ATTACH / MOVE / REPLACE) and an older replica will accept the transferred part without that marker, so the two replicas can return different _block_number / _block_offset values and may also reuse stale projections / skip indices built on them.

This needs a fail-closed behavior for old peers: either reject transferring such parts to them, or materialize fresh system-column files when talking to an older protocol version.

@clickhouse-gh

clickhouse-gh Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 78.40% 78.40% +0.00%

Changed lines: Changed C/C++ lines covered: 260/283 (91.87%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This sync PR reworks MergeTree part-storage plumbing for invalidated system columns (block number/offset persistence, projection rebuilds on merge, and the index getDeserializedFormat signature) — none of which the read-only ClickBench SELECT hot path executes. Both flagged items are improvements, not regressions, and neither aligns with the diff: Q4's -25.5% falls inside master's own variance band [201-293.5 ms] and was correctly suppressed, while Q15's -14.8% is downgraded to not_sure as run-to-run noise. No credible performance change is attributable to this PR.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 15 not_sure 223 190 -14.8% <0.0001 The changed code is MergeTree part-storage, projection-rebuild, and index deserialization plumbing — none of which a read-only ClickBench Q15 exercises, so this -14.8% is run-to-run variance, not a PR effect.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 5cbd3169-9649-4d60-8018-d13ca5409b6d
  • MIRAI run: 5ea7ddea-254f-4a5f-96b1-1e1730e1c8b4
  • PR check IDs:
    • clickbench_417989_1785083557
    • clickbench_418007_1785083557
    • tpch_adapted_1_official_418025_1785083557
    • tpch_adapted_1_official_418057_1785083557

@Michicosun
Michicosun added this pull request to the merge queue Jul 27, 2026
Merged via the queue into master with commit 57e33be Jul 27, 2026
177 of 179 checks passed
@Michicosun
Michicosun deleted the sync_invalidated_virtual_columns branch July 27, 2026 09:13
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 27, 2026
arsenmuk added a commit that referenced this pull request Jul 27, 2026
Update to latest master (9125947, 86 commits since the previous merge). Conflicts, all from #111801 (Invalidated System Columns): fold writeInvalidatedSystemColumnsFile into the redesigned unified freeze in DataPartStorageOnDiskBase and DataPartStorageOnDiskPacked, and add the block-number/block-offset invalidation skip to the redesigned loadProjections in IMergeTreeDataPart.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
wickdChromosome pushed a commit to wickdChromosome/ClickHouse that referenced this pull request Sep 26, 2026
A clone of a part that passes no invalidated_columns_to_write (the
untouched-part clone in a mutation, clone-instead-of-fetch of a
replicated mutation, moves between disks) used to delete the
invalidated_system_columns.txt it had just hardlinked or copied from
the source part: writeInvalidatedSystemColumnsFileImpl unconditionally
removed the existing file and wrote nothing back for an empty set.
Losing the file resurrects the stale physically stored _block_number
and _block_offset values that were disclaimed when the part was adopted
from another table (ClickHouse#111801), so several co-active clones of one source
part expose duplicate row identities.

A lightweight UPDATE then captures the duplicates into a single patch
part with a constant _part_data_version, and applying that patch in
Join mode fails the 'current_version != existing_version' assertion in
PatchJoinCache::Entry::addBlock, which aborts debug and sanitizer
builds; release builds silently drop one of the duplicate rows.

Fix in two places:
- writeInvalidatedSystemColumnsFileImpl keeps an existing file when the
  caller has nothing new to invalidate; this covers the clones that
  copy or hardlink the whole part directory.
- cloneAndLoadDataPart merges the source part's invalidated set into
  the clone parameters; this also covers the Packed storage, whose
  freeze/freezeRemote copy only the data archive, so the destination
  never inherits the file.

Failure history:
https://play.clickhouse.com/play?user=play&tab=Query%20V#U0VMRUNUIGNoZWNrX3N0YXJ0X3RpbWUsIHB1bGxfcmVxdWVzdF9udW1iZXIsIGNoZWNrX25hbWUsIHRlc3RfbmFtZSBGUk9NIGNoZWNrcwpXSEVSRSB0ZXN0X25hbWUgTElLRSAnJWN1cnJlbnRfdmVyc2lvbiAhPSBleGlzdGluZ192ZXJzaW9uJScgQU5EIGNoZWNrX3N0YXJ0X3RpbWUgPj0gbm93KCkgLSBJTlRFUlZBTCAzIE1PTlRICk9SREVSIEJZIGNoZWNrX3N0YXJ0X3RpbWU=

The reproduced CI failure (the only one on a branch that already had
the invalidation mechanism from ClickHouse#111801):
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113029&sha=cebdc42b01538df822d2da154bb5b51f1d4538d6&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29
ClickHouse#113029

Co-Authored-By: Claude Fable 5 <[email protected]>
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.

2 participants