Skip to content

Fix logical error when fetched part has newer metadata version than table - #96008

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-mutation-part-metadata-version-mismatch
Feb 7, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-mutation-part-metadata-version-mismatch

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 5, 2026 •

Copy link
Copy Markdown
Member

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.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Closes #67799

Version info

  • Merged into: 26.2.1.379

alexey-milovidov and others added 2 commits February 5, 2026 03:13
…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]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6853a66]

Summary: ❌

job_name test_name status info comment
Stress test (amd_debug) failure
Logical error: Block structure mismatch in A stream: different number of columns: (STID: 0993-38e6) FAIL cidb, issue

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Feb 5, 2026
@alexey-milovidov alexey-milovidov self-assigned this Feb 7, 2026
@alexey-milovidov
alexey-milovidov merged commit 4306efb into master Feb 7, 2026
255 of 258 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-mutation-part-metadata-version-mismatch branch February 7, 2026 06:50
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 7, 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)

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.

@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.

@CheSema CheSema added the post-rejected Should've been rejected, but got merged. label Mar 20, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

post-rejected Should've been rejected, but got merged. pr-ci 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.

Logical error: 'Part -2_0_26_2_47 with metadata version 13 contains column h that is absent in table test with metadata version 12'.

4 participants