Skip to content

Fix loss of a lightweight UPDATE made before RENAME COLUMN when a merge runs before the rename's mutation - #124653

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:lwu-before-rename-merge
Oct 10, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:lwu-before-rename-merge

Conversation

@groeneai

@groeneai groeneai commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #119144
Related: #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 into CHANGELOG.md):

Fixed a lightweight UPDATE made before ALTER TABLE ... RENAME COLUMN on a ReplicatedMergeTree table being lost when a merge ran before the rename's mutation (for example with apply_patches_on_merge = 0): queries returned the old value, and the mutation then dropped the update from disk.

Description

On 26.3.42.3, with apply_patches_on_merge = 0, INSERT (1, 1), UPDATE t SET v = 5, RENAME COLUMN v TO w, then OPTIMIZE TABLE t FINAL before the rename's MUTATE_PART runs, SELECT w returns 1 instead of 5, and after the mutation SELECT w ... SETTINGS apply_patch_parts = 0 returns 1. Follow-up requested in #119144 (comment).

A merge writes the part at the current metadata version, and ReplicatedMergeTreeQueue::getMutationsSnapshot kept only ALTERs above the base parts' lowest metadata version. The patch still stores v, but the snapshot no longer had the rename, so reads looked w up in the patch and the rename's MUTATE_PART skipped the patch column, after which the patch was dropped as unused. The same happens in a merge with apply_patches_on_merge = 1 over parts already at the new version.

The snapshot now lowers its ALTER bound by the metadata versions of the patch parts it is applied with (MergeTask passes its patches through the new Params::min_patch_metadata_version, and also takes rename targets from them, so a column whose values are only in a patch is not dropped as expired). ALTERs that every base part has passed keep only their RENAME/DROP COLUMN commands and go to a patch that has not passed them. Patches written before #119144 are all at metadata version 0, also those written after a rename, so for a version-0 patch the rename applies only if the patch's data predates the rename's mutation version. checkAlterIsPossible (dropped_columns) reads no patches, so it is unchanged.

The new test fails on master and passes with the fix (50 randomized runs); it also covers an UPDATE by the old name on a replica that has not applied the rename yet. Data written by 26.3.42.3 reads correctly after the upgrade; without the version-0 rule its UPDATE made after the rename becomes invisible.


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

Version info

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

…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]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 8, 2026
@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)

Cold review of the diff plus an independent model review (Gate B, 2 rounds, 0 findings).

Finding Verdict
⚠️ With #119144 on the base, no line of the test exercised the part_metadata_version > 0 arm of the patch rule AGREE, fixed: the test covers an UPDATE by the old name on a replica that had not applied the rename yet; without that arm those lines read 1
💡 While a patch older than a retained RENAME/DROP COLUMN exists, the snapshot reports metadata mutations, so PREWHERE read-ahead is skipped noted, not blocking: conservative; after #119144 only patches that predate the ALTER or were written by older versions trigger it
💡 A version-0 patch is handled as possibly written by an older version, also when it comes from a stale replica of a table that was never altered (an UPDATE by the old name at version 0) noted, not blocking: that case stays as on master; nothing on disk tells it from a legacy patch

@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05331_lightweight_update_before_rename_merge fails 10/10 on master (all 5 read as 1); the same SQL on 26.3.42.3 reads 1 after the merge every time. max_replicated_mutations_in_queue = 0 orders the merge before the rename's MUTATE_PART.
b Root cause explained? A merge writes the part at the new metadata version; ReplicatedMergeTreeQueue::getMutationsSnapshot keeps only ALTERs above the base parts' lowest metadata version, so the patch written before the rename (still storing v) gets no rename: reads miss w in it, and the rename's MUTATE_PART skips the patch column and the patch is dropped as unused.
c Fix matches root cause? Yes. The snapshot builder keeps ALTERs every base part has passed for the patches that have not (RENAME/DROP only); no guard at the read or mutate sites.
d Test intent preserved / new tests added? New stateless test: UPDATEs before and after the rename with a merge, the same after an earlier metadata-only ALTER, the merge path with apply_patches_on_merge = 1, and an UPDATE by the old name on a replica that has not applied the rename yet.
e Both directions demonstrated? Master: FAIL 10/10, every line of the UPDATE made before the rename reads 1. Fix: OK 50/50 with random settings and random MergeTree settings. Without the version-0 data rule the 26.3.42.3 upgrade loses the UPDATE made after the rename; without the metadata version > 0 branch the old-name UPDATE lines read 1. Controls (apply_patches_on_merge = 1, no merge before the mutation, plain MergeTree) read 5 on both.
f Fix is general across code paths? Read path and MutateTask::prepare (both fetch patches in getMutationsSnapshot), MergeTask (passes its patches via min_patch_metadata_version). Merges of patch parts, the dropped_columns check and StorageMergeTree (data-version gated, measured unaffected) need no change.
g Fix generalizes across inputs (params/datatypes/wrappers)? apply_patches_on_merge 0 and 1, an earlier metadata-only ALTER, patches written before and after the rename, version-0 patches written by 26.3.42.3, and patches stamped with their metadata version, including an UPDATE by the old name on a replica that had not applied the rename yet. RENAME/DROP act on names, so type wrappers do not apply.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or on-disk change. Patches written before #119144 are all at version 0; for them the rename applies only if the patch's data predates the rename's mutation version, so a finished rename is not re-applied to a patch written after it (without that rule such an UPDATE becomes invisible, measured). Data written by 26.3.42.3 reads correctly after the upgrade.
i Invariants and contracts preserved? A patch part gets the RENAME/DROP ALTERs above its metadata version; among ALTERs every base part has passed, a version-0 patch gets only those whose mutation version is above its data. ALTERs above the base bound are handled as before, base parts never receive the new entries, command order (oldest first) and locking are unchanged.

Session id: cron:clickhouse-impl-slot-5:20261008-133515

@groeneai
groeneai requested review from CurtizJ and Michicosun October 8, 2026 15:02
@clickhouse-gh

clickhouse-gh Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [95698d3]

Summary: ✅


AI Review

Summary

This PR fixes a real ReplicatedMergeTree correctness hole where a lightweight UPDATE patch could lose a pending RENAME COLUMN once a merge advanced the base parts' metadata version ahead of the patch. The current diff propagates patch metadata versions into the replicated mutations snapshot, keeps the needed metadata-only commands for older patches, and extends the merge-side expired-column protection to patch-only renamed columns. I did not find any remaining correctness or safety issues in the current code.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit 95698d3.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 52/52 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-replication ReplicatedMergeTree* + replication log/state transitions, eventual consistency mechanics. labels Oct 8, 2026
Comment thread src/Storages/MergeTree/MergeTask.cpp Resolved
A merge marks a storage column as expired when no source part stores it and it has no default,
and it took the targets of pending renames only from the conversions of the base parts. A column
added by ADD COLUMN and filled only by a lightweight UPDATE exists only in the patch, under its
old name. Once every base part had passed the following RENAME COLUMN, the new name was marked as
expired, so a merge with apply_patches_on_merge = 1 never read the patch and the update was lost.
Rename targets are now also taken from the conversions of the patches.

Reported by the AI review in ClickHouse#124653 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. and removed comp-replication ReplicatedMergeTree* + replication log/state transitions, eventual consistency mechanics. labels Oct 8, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good.

@alexey-milovidov alexey-milovidov self-assigned this Oct 9, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 9, 2026
Merged via the queue into ClickHouse:master with commit ff6af57 Oct 10, 2026
152 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 10, 2026
@clickhouse-gh clickhouse-gh Bot added comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). groeneai-origin-request PR origin: a maintainer pinged or directed groeneai 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.

3 participants