Repository navigation
Fix logical error when fetched part has newer metadata version than table - #96008
Merged
alexey-milovidov merged 4 commits intoFeb 7, 2026
Merged
Conversation
…able When a part is fetched from another replica that has already processed an `ALTER ADD COLUMN`, the part may have a higher metadata version than the local table schema. Previously this would throw a `LOGICAL_ERROR` exception because the code only handled the case where `part_metadata_version < table_metadata_version` (assuming the column was dropped). This fix handles the case where `part_metadata_version > table_metadata_version` by logging a warning and keeping the column in the output part. The column was legitimately added via ALTER on another replica and will be added to this replica's table schema once the ALTER is processed locally. Co-Authored-By: Claude Opus 4.5 <[email protected]>
The case where part_metadata_version > table_metadata_version can only happen with replicated tables when a part is fetched from another replica. Co-Authored-By: Claude Opus 4.5 <[email protected]>
Contributor
|
Workflow [PR], commit [6853a66] Summary: ❌
|
alexey-milovidov
deleted the
fix-mutation-part-metadata-version-mismatch
branch
February 7, 2026 06:50
alesapin
reviewed
Feb 17, 2026
| /// When part has higher metadata version than the table, it means the part was fetched from another replica | ||
| /// that already processed an ALTER ADD COLUMN. The column will be added to this replica's table schema | ||
| /// once the ALTER is processed locally. We should keep the column in the output part. | ||
| if (part_metadata_version > table_metadata_version) |
Member
There was a problem hiding this comment.
@alexey-milovidov Mutation shouldn't be executed ahead of corresponding metadata change. This situation shouldn't be possible and must throw LOGICAL ERROR. The code you added will lead data loss of freshly inserted column.
1 task
1 task done
pull Bot
pushed a commit
to Haofei/ClickHouse
that referenced
this pull request
Jul 31, 2026
A part stores its metadata version on disk in metadata_version.txt and keeps it verbatim when it is fetched from another replica (DataPartsExchange.cpp:264-266). A replica's table metadata version, in contrast, only advances through setTableStructure (StorageReplicatedMergeTree.cpp:1822), whose only caller is executeMetadataAlter (:6731), so it advances only when that replica executes its own ALTER_METADATA queue entry. A replica can therefore hold a fetched part at metadata version 1, containing a column the replica does not know about yet, while its own snapshot is still at version 0. A plain data mutation such as ALTER DELETE carries alter_version = -1, so isAlterMutation() is false and the alter_sequence gating in shouldExecuteLogEntry does not apply to it. splitAndModifyMutationCommands then iterates the part's columns, finds one that is absent from the table snapshot and throws LOGICAL_ERROR at MutateTask.cpp:510, which aborts the server in debug and sanitizer builds. The soft path just above it only covers the opposite ordering, where the part is older than the table and the column was genuinely dropped. This is a gap between the two subclasses of ReplicatedMergeMutateTaskBase. MergeFromLogEntryTask::prepare already refuses to run in exactly this state (MergeFromLogEntryTask.cpp:186-196, "ALTER_METADATA is still in progress"); the mutation path had no equivalent. Add the same rule for MUTATE_PART entries in ReplicatedMergeTreeQueue::shouldExecuteLogEntry, so such an entry is postponed until the pending ALTER_METADATA is applied, after which the mutation runs normally. The queue predicate is used rather than MutateFromLogEntryTask::prepare so that nothing is reserved or locked before bailing out: prepare would already have built a FutureMergedMutatedPart, reserved disk, taken a table lockForShare, inserted a MergeListEntry and, under zero-copy replication, possibly taken a zero-copy lock. Writing the reason through LogToStr also surfaces it in system.replication_queue.postpone_reason. MutateTask.cpp is deliberately unchanged. The assertion keeps its full meaning and the equal-versions case still throws, so this is not the soft-keep direction of ClickHouse#96008, which was reverted in ClickHouse#97301 as a data-loss risk. ClickHouse#102349 reached the same placement and was closed by its own author. The guard is unconditional, matching the merge sibling, so it inherits that guard's liveness assumption that some ALTER_METADATA eventually applies. Every producer of the mismatch keeps or plants one, including the dummy_alter entry that the recovery path synthesizes (StorageReplicatedMergeTree.cpp:3966-3976). Closes: ClickHouse#91015 Co-Authored-By: Claude Fable 5 <[email protected]>
groeneai
added a commit
to groeneai/ClickHouse
that referenced
this pull request
Sep 15, 2026
`splitAndModifyMutationCommands` tolerated a part column that the table no longer has only when the part was behind the table's metadata version, or when it carried no metadata version on disk at all. `ATTACH`/`REPLACE PARTITION FROM` and `MOVE PARTITION TO TABLE` stamp the destination's metadata version on a part that keeps the source's `columns.txt` (`metadata_version_to_write` in `executeReplaceRange`, `replacePartitionFromImpl` and `movePartitionToTable`) and require matching structures, so a partition cloned out of a table that had dropped the column arrives with `part_metadata_version == table_metadata_version` and a column that is in no schema at all. That reached the logical error, which is an abort on a debug or sanitizer build, and the `MUTATE_PART` entry replays from Keeper after the restart. The throw is kept for `part_metadata_version > table_metadata_version`, where the replica is behind an `ALTER ADD COLUMN` and the column is real data: that is the case ClickHouse#96008 turned into a logical error and ClickHouse#97301 reverted as "replace LOGICAL_ERROR with potential dataloss", and `ReplicatedMergeTreeQueue` now postpones the mutation before it can get here. The test gains a `ReplicatedMergeTree` arm and a cloned-partition arm, and drops `no-shared-merge-tree`: with the change every arm takes the version comparison, which does not consult `supportsReplication()`. Measured on debug builds, per arm (`MergeTree` / `ReplicatedMergeTree` / cloned by `ATTACH PARTITION FROM`): master @ 45efaa4 FAIL / FAIL / FAIL Code: 47, retried forever this branch, no fix OK / OK / ABORT signal 6, split... -> prepare this branch OK / OK / OK part-vs-table mv 0/0, 1/2, 0/0 Reported on the PR, with the repro and the one-line diff, by Avogar.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a part is fetched from another replica that has already processed an
ALTER ADD COLUMN, the part may have a higher metadata version than the local table schema. Previously this would throw aLOGICAL_ERRORexception because the code only handled the case wherepart_metadata_version < table_metadata_version(assuming the column was dropped).This fix handles the case where
part_metadata_version > table_metadata_versionby logging a warning and keeping the column in the output part. The column was legitimately added via ALTER on another replica and will be added to this replica's table schema once the ALTER is processed locally.Changelog category (leave one):
Closes #67799
Version info
26.2.1.379