Skip to content

Wait unfinished rename column alter mutation - #112783

Merged
Michicosun merged 4 commits into
masterfrom
wait_unfinished_rename_column_alter_mutation
Aug 3, 2026
Merged

Michicosun merged 4 commits into
masterfrom
wait_unfinished_rename_column_alter_mutation

Conversation

@Michicosun

@Michicosun Michicosun commented Jul 31, 2026 •

Copy link
Copy Markdown
Member

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):

This pr fixes an alter application for regular merge tree storage. Now, alter will not be applied while there is an incomplete rename-column alter-mutation, because it may update storage metadata in an incompatible way, which will block mutations completely.

The solution is similar to RMT but less strict, because RMT does not execute alters unless there is another non-complete alter-mutation with a smaller metadata version.

Fixes: #109957

@clickhouse-gh

clickhouse-gh Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [531388a]

Summary: ✅


AI Review

Summary

This PR serializes later ALTERs behind unfinished RENAME COLUMN mutations for regular MergeTree tables and adds a regression test for the two-statement reproducer from #109957. High-level verdict: the fix is incomplete, because the same rename/drop/re-add sequence is still legal inside a single multi-command ALTER, so the stale-column / type-mismatch path remains reachable.

Findings

❌ Blockers

  • [src/Storages/StorageMergeTree.cpp:562] The new wait only covers a later ALTER statement. A single ALTER TABLE ... RENAME COLUMN a TO b, DROP COLUMN b, ADD COLUMN b UInt64 SETTINGS alter_sync = 0 still builds the same RENAME_COLUMN a -> b + DROP_COLUMN b mutation sequence while publishing metadata where b is the re-added UInt64. AlterCommand::tryConvertToMutationCommand applies each subcommand to the working metadata as it goes, and IMergeTreeReader rewrites logical b back to physical a before isColumnDropped runs, so the old a bytes are still treated as live for the new b. That keeps the original #109957 contract violation reachable even after this wait.
    Suggested fix: either reject/rewrite same-statement reuse of the rename target as well, or make AlterConversions propagate DROP COLUMN b back through rename_map so the source a is considered dropped when b is re-added.
Tests
  • ⚠️ Add a focused regression for ALTER TABLE ... RENAME COLUMN a TO b, DROP COLUMN b, ADD COLUMN b UInt64 SETTINGS alter_sync = 0 with merges stopped or with a follow-up mutation. The two-statement reproducer is now covered, but the single-statement form still exercises the same invariant through unchanged code.
Final Verdict

Changes requested.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.80% +0.10%

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

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 31, 2026
disable_failpoint
${CLICKHOUSE_CLIENT} --query="DROP TABLE t_rename_no_default_dynamic"

# Phase 3: the opposite direction - the keep-alive must not over-apply. The old name is re-added

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.

this will not work after this patch, because the second alter

ALTER TABLE t_rename_no_default_reuse ADD COLUMN a String DEFAULT 'reused_default' SETTINGS alter_sync = 0;

will hang since for regular MT we do not have an alter sequence materialized on disk.

int64_t mutation_to_wait = 0;
{
std::lock_guard lock(currently_processing_in_background_mutex);
for (const auto & [version, mutation_entry] : current_mutations_by_version | std::views::reverse)

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 only serializes separate ALTERs. The original invariant is still violated inside one multi-command ALTER, so the stale-column path from #109957 stays reachable.

AlterCommand::tryConvertToMutationCommand applies each subcommand to the working metadata as it goes, so ALTER TABLE ... RENAME COLUMN a TO b, DROP COLUMN b, ADD COLUMN b UInt64 SETTINGS alter_sync = 0 is accepted and produces the same RENAME_COLUMN a -> b + DROP_COLUMN b sequence that used to come from multiple statements (src/Storages/AlterCommands.cpp:1357). AlterConversions then keeps rename_map = { b <- a } and dropped_columns = { b } (src/Storages/MergeTree/AlterConversions.cpp:167), but the reader rewrites logical b back to physical a before checking isColumnDropped, so the drop of b no longer hides the old a bytes (src/Storages/MergeTree/IMergeTreeReader.cpp:355, src/Storages/MergeTree/MergeTreeBlockReadUtils.cpp:105).

So this wait closes the two-statement reproducer, but the same rename/drop/re-add sequence is still legal in one statement. We need either to reject/rewrite same-statement reuse of the rename target as well, or to propagate DROP COLUMN b backwards through the rename map so the source a is treated as dropped when b is re-added.

@Michicosun
Michicosun added this pull request to the merge queue Aug 3, 2026
@alesapin alesapin self-assigned this Aug 3, 2026
Merged via the queue into master with commit c66a839 Aug 3, 2026
179 checks passed
@Michicosun
Michicosun deleted the wait_unfinished_rename_column_alter_mutation branch August 3, 2026 14:51
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 3, 2026
@RinChanNOWWW

Copy link
Copy Markdown
Contributor

Hi @Michicosun, does this PR also fix this #109860?

alexey-milovidov added a commit to niyue/ClickHouse that referenced this pull request Aug 4, 2026
…me-mutation barrier

Since ClickHouse#112783, any `ALTER` on `StorageMergeTree` waits for a pending
`RENAME COLUMN` barrier mutation regardless of `alter_sync`, so keeping the
rename mutation pending with `SYSTEM STOP MERGES` and then issuing
`MODIFY COLUMN` deadlocks until `max_execution_time` (the `Fast test`
timeout). Wait for the rename synchronously instead, and keep only the
`Nullable` to non-`Nullable` type-conversion mutation pending, which is the
part of the scenario that exercises the matcher-expanded `DEFAULT`
conversion on reads and mutations.
@Michicosun

Copy link
Copy Markdown
Member Author

Hi @Michicosun, does this PR also fix this #109860?

Hi! @RinChanNOWWW, no, it should not; we need both fixes.

alexey-milovidov added a commit to amosbird/ClickHouse that referenced this pull request Aug 8, 2026
… as a single ALTER

Since ClickHouse#112783, a metadata ALTER
on plain MergeTree waits for a pending barrier (`RENAME COLUMN`) mutation to
finish, so the sequential `RENAME`/`DROP`/`ADD` statements under
`SYSTEM STOP MERGES` blocked forever and the pending on-the-fly state could
never be observed. Issue the three commands as a single ALTER instead: it
creates the same pending rename map and drop set without a preceding barrier
mutation to wait for.

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

Endless error in squashed mutation.

4 participants