Skip to content

Write a patch part at the table's metadata version - #119144

Merged
alexey-milovidov merged 11 commits into
masterfrom
patch-part-metadata-version-116733
Oct 7, 2026
Merged

alexey-milovidov merged 11 commits into
masterfrom
patch-part-metadata-version-116733

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

On ReplicatedMergeTree, a lightweight UPDATE of a column whose RENAME COLUMN mutation is still unmaterialized was acknowledged and then silently invisible: reads returned the pre-update values until the rename finished materializing on every part of the snapshot. RENAME COLUMN returns before that (default alter_sync = 0), so any update issued in that window lost read-your-writes, with no error and nothing in the log.

A patch part is written with the synthetic metadata that describes the patch's own structure, which carries no metadata version, so the part landed at version 0 - behind every metadata mutation there has ever been. ReplicatedMergeTreeQueue::MutationsSnapshot::getOnFlyMutationCommandsForPart gates a metadata command on alter_version > part_metadata_version, so the still-queued RENAME COLUMN was applied to a patch written after it: AlterConversions::getPatchesForColumns then mapped the new name back to the old one, the patch (which physically stores the new name) had no such column, and the patch was skipped for it. Plain MergeTree gates its on-fly commands by data version, which is why it was unaffected.

The patch part is written against the table as it is at that moment, so it is now stamped with the table's metadata version. A rename that predates the patch is no longer applied to it, and one that follows the patch still is.

The compaction of patch parts rewrote them from the same synthetic metadata and lost the version again, so a background merge of the patches brought the updates' invisibility back. A merged patch is now stamped with the lowest metadata version among its sources: they all share the patch's structure, so they are on the same side of every rename of a column the patch stores, and that is also the version the merge's mutations snapshot already uses.

Closes: #116733

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 to CHANGELOG.md):

Fixed an acknowledged lightweight UPDATE being invisible on ReplicatedMergeTree when it targets a column whose RENAME COLUMN mutation has not materialized yet.


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

Version info

  • Merged into: 26.10.1.1781-master (included in 26.10 and later)

The synthetic metadata that describes a patch part's own structure
carries no metadata version, so the part was written at version 0 -
behind every metadata mutation there has ever been. The replicated
queue hands a part every metadata command with a greater alter version,
so a `RENAME COLUMN` whose materialization was still pending was
applied to a patch written after it: the patch was then looked up under
the column's old name, the new name is what it stores, and the
lightweight update it carries was silently missing from the result
until the rename materialized.

Closes: #116733
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a3549b1]

Summary: ✅


AI Review

Summary

This PR propagates the correct metadata version through foreground patch writes, patch-part compaction, and empty patch-part builders, so pending RENAME COLUMN mutations no longer make acknowledged lightweight updates disappear on ReplicatedMergeTree. I did not find a remaining correctness issue in the current diff.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit a3549b1.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 92.10% 91.80% -0.30%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 37/38 (97.37%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). labels Sep 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a3549b15d with master 7cbb8ad28 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 796d29517; 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.

Object file sizes

6 object files changed (+43.30 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/MergeTree/MergeTreeDataMergerMutator.cpp.o 493.32 KiB 532.04 KiB +38.72 KiB (+7.85%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The Fast test (arm_darwin) failure is unrelated: 05076_error_trace_file_offsets returns 1 0 instead of 1 1 on macOS, and the same failure shows up on other pull requests that touch nothing near it (#119114, #119115).

Related: #117449

alexey-milovidov and others added 3 commits September 10, 2026 18:38
A merge of patch parts took the metadata of the merged patch from the first
source part's `getMetadataSnapshot`, which for a patch part is the synthetic
metadata of the patch's structure, shared by every patch of that structure and
carrying no metadata version. The merged patch was therefore written at
version 0 again, and on `ReplicatedMergeTree` a `RENAME COLUMN` that had not
materialized yet was re-applied to it on read, so the lightweight updates it
carried became invisible until the rename materialized: the same failure this
pull request fixes for a freshly written patch, brought back by the first
background compaction of the patches.

The merged patch is now stamped with the lowest metadata version among its
sources. A rename of a column that a patch stores puts the patches written
before and after it in different partitions, so the sources of one merge are
all on the same side of every such rename, and the lowest of their versions is
also what the mutations snapshot of the merge takes for
`min_part_metadata_version`.

The test gains a third table: a pending rename, two updates, an explicit
`OPTIMIZE` of the patch partition, and the sum checked before and after the
rename materializes. `max_replicated_mutations_in_queue = 0` keeps the merge
selector from scheduling the rename's mutation while the `OPTIMIZE` still
goes through, so no failpoint and no `no-parallel` tag is needed. Without the
fix the case reads 0 instead of 600 while the rename is pending. The test is
renumbered to `05227` after the merge with master.

Addresses the review thread on the compaction path:
#119144 (comment)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
alexey-milovidov and others added 4 commits September 19, 2026 20:50
A patch of the old `a` and of a re-added `a` after `RENAME COLUMN a TO b, ADD COLUMN a` share a patch partition, so the claim that all sources of a patch merge are on the same side of every rename did not hold. That case is already wrong for ordinary parts (#122146) and is not made worse here.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…their source

`createEmptyPartInsteadOfLost`, the empty covering part of `DROP`/`REPLACE PARTITION`,
and the empty parts of `StorageMergeTree` built a patch empty from
`IMergeTreeDataPart::getMetadataSnapshot`, whose synthetic patch-part metadata
carries version 0. Once such an empty patch compacted with real patches, the
`min` in `mergePartsToTemporaryPart` dropped the merged patch back to version 0
and a pending `RENAME COLUMN` was applied to it on read again. Take the version
of the source part instead, through the new `MergeTreeData::getMetadataSnapshotForEmptyPart`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

@alexey-milovidov, the opposite order still loses the update with this PR: an UPDATE made before RENAME COLUMN, then a merge that runs before the rename's mutation. Measured on the CI build of a3549b1 and on master 468fbe0:

CREATE TABLE t (x UInt32, v UInt32) ENGINE = ReplicatedMergeTree('/clickhouse/tables/t', '1') ORDER BY tuple()
SETTINGS enable_block_number_column = 1, enable_block_offset_column = 1, apply_patches_on_merge = 0, max_replicated_mutations_in_queue = 0;
INSERT INTO t VALUES (1, 1);
UPDATE t SET v = 5 WHERE 1;
ALTER TABLE t RENAME COLUMN v TO w SETTINGS alter_sync = 0;
SYSTEM SYNC REPLICA t;
SELECT w FROM t; -- 5
OPTIMIZE TABLE t FINAL;
SELECT w FROM t; -- 1, expected 5
ALTER TABLE t MODIFY SETTING max_replicated_mutations_in_queue = 16;
-- once the RENAME mutation is done:
SELECT w FROM t SETTINGS apply_patch_parts = 0; -- 1, the update is gone from disk

max_replicated_mutations_in_queue = 0 only keeps the rename's MUTATE_PART behind the merge. With apply_patches_on_merge = 1, or without the merge, it returns 5.

The merged part is written at metadata version 1, while the patch stays at 0 and stores v. The mutations snapshot takes min_part_metadata_version from the base parts only (MergeTreeData::getPartsSnapshotInfo, MutateTask::prepare), so ReplicatedMergeTreeQueue::getMutationsSnapshot drops the rename and the patch is no longer mapped to w, both on read and in the rename's own mutation. The bound has to include the metadata versions of the patches the snapshot applies. On master every patch is at 0, so that would apply the rename to patches written after it; it is only sound with this PR.

Should this go into this PR, or should I send it as a follow-up once this PR is merged?

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, thanks for the reproducer. The analysis looks right to me: once the merged base part is at version 1, the snapshot's min_part_metadata_version skips the rename, so neither reads nor the rename's own MUTATE_PART map the patch's v to w, and the update is lost on disk.

Please send it as a follow-up after this PR is merged, and keep this PR scoped to #116733. Your case also fails on master, so it is a separate bug, and the fix has an upgrade problem that deserves its own review:

  • Patches written before this PR are all at metadata version 0, including ones written after a rename. ReplicatedMergeTreeQueue keeps finished mutations (up to finished_mutations_to_keep), so if the bound is lowered to the patches' minimum version, a finished RENAME COLUMN v TO w is applied again to such a legacy patch, which already stores w. The update it carries then goes invisible, which is the bug from ReplicatedMergeTree: acked lightweight UPDATE on a renamed column is silently invisible while the RENAME COLUMN mutation is still pending #116733 but for old patches. That needs to be handled, or at least shown to be acceptable, before the bound includes patch versions.
  • The bound is computed in several places: createStorageSnapshot, MutateTask::prepare, MergeTask (when future_part->patch_parts is not empty, i.e. apply_patches_on_merge = 1) and the dropped_columns check in MergeTreeData::checkAlterIsPossible/alter. Lowering it in one place, e.g. in ReplicatedMergeTreeQueue::getMutationsSnapshot from the patch parts it already collects, would cover the paths that set need_patch_parts. The merge path needs its own handling.

A test along the lines of your reproducer (with max_replicated_mutations_in_queue = 0 holding the rename behind OPTIMIZE, as 05227_lightweight_update_pending_rename already does) would be good to have in the follow-up.

@groeneai

groeneai commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

Acknowledged. I'll send it as a separate follow-up PR after this one is merged, handling the legacy version-0 patches and the merge path, and link it here.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reasonable.

@alexey-milovidov alexey-milovidov self-assigned this Oct 7, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 7, 2026
Merged via the queue into master with commit 4768753 Oct 7, 2026
149 checks passed
@alexey-milovidov
alexey-milovidov deleted the patch-part-metadata-version-116733 branch October 7, 2026 22:35
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 7, 2026
@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

Follow-up as requested in #119144 (comment): #124653. A version-0 patch gets a finished rename only if its data predates the rename mutation, and the merge path passes its patch versions to the snapshot.

kute1654 pushed a commit to kute1654/ClickHouse that referenced this pull request Oct 10, 2026
…ses the rename

On ReplicatedMergeTree, a merge writes the merged part at the current metadata version.
Every mutations snapshot took its ALTER bound from the base parts only, and
ReplicatedMergeTreeQueue::getMutationsSnapshot dropped every ALTER entry at or below that
bound. A patch part written before a RENAME COLUMN still stores the old name and needs the
rename, but once every base part had passed it the snapshot no longer carried it: reads looked
the new name up in the patch and found nothing, and the rename's own MUTATE_PART skipped the
patch column, after which the patch was dropped as unused. With apply_patches_on_merge = 1 a
merge of parts already at the new version consumed the patch the same way.

The snapshot now lowers its ALTER bound by the metadata versions of the patch parts it is
applied with: those fetched in getMutationsSnapshot (read path, MutateTask) and the patches of
a merge (MergeTask, new Params::min_patch_metadata_version). ALTERs that every base part has
passed are kept with their RENAME and DROP COLUMN commands only and are given to a patch only
when the patch has not passed them. Patches written before patches carried a metadata version
are all at version 0, also those written after the rename (they already store the new name),
so for a patch at version 0 the rename applies only if the patch's data predates the rename's
mutation version; block numbers of patches and mutation versions come from the same
per-partition sequence. Entries above the base bound are handled exactly as before.

The test also covers an UPDATE by the old name on a replica that has not applied the rename
yet, whose patch carries the metadata version from before the rename.

Follow-up of ClickHouse#119144 (comment)

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

comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). 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.

ReplicatedMergeTree: acked lightweight UPDATE on a renamed column is silently invisible while the RENAME COLUMN mutation is still pending

3 participants