Repository navigation
Cherry pick #109356 to 25.8: Fix data loss when a merge races ALTER RENAME COLUMN on a column without a default expression - #112622
Merged
robot-ch-test-poll merged 25 commits intoJul 31, 2026
Conversation
…NAME COLUMN A merge applies a pending RENAME COLUMN to its source parts on-fly at read time: the metadata (and storage_columns) already carries the new name, while the source parts still physically store the old name. MergeTask's expired-columns optimization compared storage_columns against the parts' raw physical names and, finding the new name absent, treated it as an expired column. A column with no default expression (e.g. Dynamic) was therefore dropped from the merged part, so every value read back as NULL. See #80648. Fix: when scanning for expired columns, treat a rename target as present whenever a source part physically holds the matching old name (via the same alter_conversions rename map the merge already uses to apply the rename on-fly). A genuinely dropped column, whose old name is absent, still expires. The pending-mutations snapshot is computed once and reused by both the expired-columns check and the later alter_conversions/patch handling so both observe the same mutation set. Residual facet of #80648 that the primary fix (#104822, atomic metadata + pending-mutation publication) does not close: that fix handles columns with a default (the String case), which are refilled rather than dropped. Adds the Dynamic-phase regression test to 04238_alter_rename_column_merge_race. Related: #104822 Co-Authored-By: Claude Opus 4.8 <[email protected]>
The probabilistic race (25 iterations at ~0.25% per iteration) caught the residual no-default facet of #80648 only about 6% of the time, so a green CI run did not meaningfully guard against a regression. Force the exact window deterministically instead, using the same idiom as 03830_vertical_merge_inject_column_after_drop: the mt_select_parts_to_mutate_no_free_threads failpoint keeps the RENAME mutation from being selected, so the source parts stay physically at the old column name while the metadata already carries the new one; alter_sync=0 returns right after the metadata commit; OPTIMIZE FINAL then merges the still-old parts under the new metadata and applies the rename on-fly. That is exactly the path that dropped a no-default column. Disable the failpoint afterwards and wait for the pending mutation to materialize before the final read. Both phases are now deterministic (no iteration loop, no long tag): the Dynamic no-default phase fails every run without the fix and passes every run with it; the String DEFAULT '' phase continues to pass. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The final SELECT is only meaningful after the pending RENAME COLUMN mutation has materialized: reading the renamed column while its mutation is still pending returns NULL regardless of the fix. Give wait_mutation a larger budget and, on timeout, print the still-pending mutations and return non-zero so a slow-worker case surfaces as a timeout instead of falling through to the pre-materialization NULL, which would look like a false data-loss regression. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The rewritten String DEFAULT '' and Dynamic no-default count checks only printed diagnostics and fell through to echo OK, so a wrong count still exited 0 on the exact data-loss path the test guards. Add exit 1 after dumping the offending rows in both mismatch branches, matching the old loop, so a regression is a hard failure. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The test arms the server-global failpoint mt_select_parts_to_mutate_no_free_threads and relied on an explicit SYSTEM DISABLE FAILPOINT later in the flow. Under set -e, a failure in ALTER, OPTIMIZE or wait_mutation before that disable would exit the script with the failpoint still armed, so every later stateless test on the same server inherits postponed mutations (a poisoned-server leak, in both phases). Move the disable into a disable_failpoint helper wired to a trap ... EXIT so the failpoint is cleared on every exit path, including the set -e abort and the wait_mutation timeout return 1. The in-flow disables stay (the pending rename mutation only materializes once the failpoint is off, which wait_mutation needs); they now call the same helper. The EXIT trap does not mask the failure: bash keeps the pending non-zero status through the trap, so a real regression still surfaces as a test failure. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…residual-dynamic-expired-columns
…residual-dynamic-expired-columns
…residual-dynamic-expired-columns
Follow-up on the #80648 residual fix. The merge expired-columns check treats a pending RENAME COLUMN old -> new target as present when a source part still physically holds old. RENAME COLUMN is a barrier, but a later non-barrier ADD COLUMN old does not wait for the pending rename, so metadata can carry both old and new while the parts store only the pre-rename old. In that state the check kept new alive off the physical old bytes, so the merged part carried both old and new from one physical column and the pending rename mutation, finding new already present, aborted with a LOGICAL_ERROR in getColumnsForNewDataPart. Skip the keep-alive when the rename source is itself a live storage column (the re-added case). The merge then expires new as before the #80648 fix; the rename mutation re-derives it and the re-added old is defaulted, which is the correct pre-fix behavior for this case. The Dynamic no-default preservation (#80648) is unchanged for the pure-rename case. Adds a third deterministic phase to 04238_alter_rename_column_merge_race.sh that fails (server abort) without this guard and passes with it. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The AI review raised a scenario where a merge sees a mix of pre-rename
parts (physically holding `a` under a pending `RENAME COLUMN a TO b`)
and fresh post-`ADD COLUMN a` parts (physically holding `b` and the
re-added `a`), claiming the old `a` bytes would be double-bound to both
logical names and the pending rename mutation would then fail with a
logical error on the merged part.
Such a merge cannot be assigned: all source parts of one merge must
have the same current mutation version, which is enforced by
`canMergeParts` in every merge predicate (plain, replicated, and
distributed), so a single merge never mixes parts from before and after
a pending `RENAME COLUMN`. Verified empirically: `OPTIMIZE FINAL` over
such parts is refused with `CANNOT_ASSIGN_OPTIMIZE` ("Parts ... have
different mutation version"), and once the rename materializes the
generations merge cleanly with correct data.
Document this invariant next to the expired-columns guard in
`MergeTask.cpp` and add a regression phase to
`04238_alter_rename_column_merge_race.sh` that asserts both the refusal
while the rename is pending (on plain `MergeTree`, where the failpoint
pins the mutation) and the correct post-materialization merge result.
…ed-columns decision The AI review flagged a lightweight `UPDATE` on the rename target (after `RENAME COLUMN a TO b` and a re-`ADD COLUMN a`) as a potential data-loss path through the expired-columns decision. It is not: patch selection for a merge is version-gated in `getPatchesToApplyOnMerge`, so a post-rename patch is never consumed by a merge over pre-rename source parts; it stays alive, is applied on read, and materializes only after the rename does. Document that invariant next to the guard in `MergeTask.cpp` and codify the scenario as phase 5 of `04238_alter_rename_column_merge_race` (`RENAME` + re-`ADD` + lightweight `UPDATE` + `OPTIMIZE FINAL` under the failpoint; the patched value must survive). Co-Authored-By: Claude Fable 5 <[email protected]>
…residual-dynamic-expired-columns
Trim the bloated comment blocks in `MergeTask.cpp` and in `04238_alter_rename_column_merge_race.sh` to their essentials, as requested by the review. Co-Authored-By: Claude Fable 5 <[email protected]>
…residual-dynamic-expired-columns
Addresses the review finding on the expired-columns scan in `MergeTask::ExecuteAndFinalizeHorizontalPart::prepare`: the scan only looked at the physical columns of the source data parts, so a column with no default expression that was added by `ADD COLUMN` and then filled by a lightweight `UPDATE` was expired even though its values live in a patch part selected for the merge. Expiring it removes the column from the merge read set, so `AlterConversions::getPatchesForColumns` never requests the patch and the updated value is silently lost. Reproduced on this branch's binary with `clickhouse local`: after `ADD COLUMN a String` + `UPDATE ... SET a = 'patched'` + `OPTIMIZE FINAL`, the patched value read back empty; with a `DEFAULT` on the same column it survived, confirming the expired-columns scan as the cause. Now the selected patch parts' physical columns are taken into account both when deciding whether a storage column is expired and when keeping a pending `RENAME COLUMN` target alive - a patch written before the rename stores the old name, so only the rename mapping can protect the target. `04238_alter_rename_column_merge_race` gains phase 6, covering the patch-only column both under its own name and as a pending rename target.
…residual-dynamic-expired-columns
…-parallel 04238_alter_rename_column_merge_race.sh is restored to its master content, and the coverage this PR added for the no-default facet of #80648 moves into the new 04648_alter_rename_column_no_default_merge_race.sh. The no-parallel tag was introduced by this PR, not by the existing test. Its stated reason was that the assertions had to wait for the pending RENAME mutation to materialize, so a concurrent test clearing the server-global mt_select_parts_to_mutate_no_free_threads failpoint could postpone that mutation and make the final read flaky. Removing the wait removes the reason. The wait was needed because a merge that runs while the rename is still pending writes the new column name into the merged part but leaves the part's data version below the mutation, so a reader applies the rename a second time and sees an empty column even on a correct server. Instead of waiting, the test now asserts on system.parts_columns: whether the merge kept a physical column for the rename target is decided by the merge itself and is stable from the moment OPTIMIZE returns. For a column with no default, dropping it is exactly the data loss, so this is the same property with no timing dependency. The patch-only own-name phase needs no failpoint at all and keeps asserting values. The failpoint is still required to open the window: SYSTEM STOP MERGES aborts the explicit OPTIMIZE as well ("Cancelled merging parts"), and the pool-size thresholds are bypassed on an idle server by the occupied <= 1 short circuit in CompactionStatistics::getMaxSourcePartBytesForMutation. It stays server-global, but no assertion depends on it staying armed: if a concurrent test clears it, the rename materializes early and the merge no longer sees the race, which costs coverage for that run but cannot turn a correct server red. That is the same trade-off as 03830_vertical_merge_inject_column_after_drop, which uses the same failpoint and carries no tags. Verified on a debug build: the new test passes 3/3 sequential runs and 6/6 concurrent copies against the fix, and fails on a pre-fix master binary in all 9 of those runs. Each of the Dynamic, patch-only own-name and patch-only rename-target phases also fails on its own against pre-fix master, so no phase is masked by an earlier one. The rename-reuse phase asserts the opposite direction (the keep-alive must not claim the rename target as present) and passes on both binaries by design.
Two gaps in the previous commit, both found by re-running the test with the failpoint never armed: - The rename-reuse phase asserted that the merged part does NOT physically hold the rename target. That is only true while the rename is still pending; if the failpoint is cleared the rename materializes, a physical target is correct, and the assertion failed on a correct server. It is now guarded on the mutation still being pending, so clearing the failpoint costs coverage instead of failing the run - which is what the comment at the top already promised. - The positive assertions only checked that the merged part still lists a column for the rename target, so a part that kept the column but rewrote it as empty defaults would have passed. They now also require a minimum number of uncompressed bytes for that column, and the phase-1 payload is sized so an all-defaults rewrite cannot reach the floor. Dropping the floor back to zero makes phase 1 pass on a pre-fix binary, so the floor is load-bearing. Re-verified on a debug build: 3/3 sequential and 6/6 concurrent runs pass against the fix and all 9 fail against a pre-fix master binary, and with the failpoint removed entirely the test still passes against the fix.
getAlterConversionsForPart takes an extra EnabledMaskingPoliciesPtr under CLICKHOUSE_CLOUD and the parameter has no default, so the call added by this PR would not compile in a cloud build. Pass the guarded nullptr, matching the existing call a few hundred lines below.
The rename-reuse phase issues RENAME COLUMN a TO b and ADD COLUMN a as two separate statements. A merge that runs between them legitimately sees no live a, so it materializes a physical b that the later re-add cannot remove, and the negative assertion then reads ['a','b'] on a correct server. On an idle machine the window is almost never hit, but under the flaky check's ~50 concurrent copies it accounted for 20 of the 21 observed failures across four sanitizers. Wrapping the two ALTERs in SYSTEM STOP MERGES / START MERGES closes it, which is what phases 4 and 5 of the same file already do. Every assertion in the file is a statement about what a merge decided, so all five OPTIMIZE calls now run with optimize_throw_if_noop = 1. A merge that is silently skipped otherwise reads as a column the merge dropped, which is how the single remaining failure reported lost Dynamic values for a table whose parts had simply never been merged. Verified on the fix build by injecting a merge between the two ALTERs: without the STOP MERGES pair the assertion reproduces the exact CI signature (pending=1, cols=['a','b']), with it the phase passes. The phase still yields cols=['a'] on a pre-fix binary, so it keeps its role as the guard against the keep-alive over-applying. Co-Authored-By: Claude Fable 5 <[email protected]>
The flaky check runs ~50 copies of the test at once. The failpoint that holds
the rename pending is server-global, so one copy's EXIT trap releases another
copy's rename mid-window. The test claimed to tolerate that ("costs coverage
instead of turning a correct server red") but did not, and reddened on four
sanitizers. Two independent causes, both of them the test's, not the server's.
First, phase 3 gated its assertion on min(is_done) = 0 over system.mutations.
That aggregates over every mutation on the table, so an unrelated pending one
makes it claim this rename is unfinished after it has materialized, and the
then-correct physical b reads as a failure. It also asserted the physical
column set, which is not stable: while the rename is pending only a exists,
and once it materializes a part legitimately holds both b and the re-added a,
so no predicate over parts_columns separates the correct shapes from the
defect. Assert the value instead - what over-applying the keep-alive costs is
b's data, and that holds in both states.
Second, an early release lets the rename materialize on only some source
parts, and OPTIMIZE then legitimately refuses to merge parts with different
mutation versions (Code 388). optimize_throw_if_noop = 1, added so a silently
skipped merge could not read as a lost column, turned that into a hard
failure. optimize_or_skip skips that one reason and keeps failing on every
other refusal, so both properties hold.
Verified on a debug build: with a concurrent clear injected deterministically
the old test fails with the exact CI message and the new one passes; with the
aliasing state injected the new assertion still fires; the skip triggers only
on a mixed mutation version and a genuine refusal still fails loudly;
50 runs x 10 jobs, 50 OK.
The previous commit moved phase 3 from asserting the physical column set to asserting the values, which falsified the header's blanket claim that every assertion reads system.parts_columns. State the per-assertion reason instead.
Four empty Strings occupy 9 bytes, so the previous >= 1 threshold passed even when the merge preserved the rename target but dropped the patch value. The value itself cannot be asserted here: logical b reads back empty until the rename materializes, so an exact-value check would fail on a correct server in the state this phase covers.
…dynamic-expired-columns Fix data loss when a merge races ALTER RENAME COLUMN on a column without a default expression
…ypick/25.8/109356 # Conflicts: # src/Storages/MergeTree/MergeTask.cpp
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.
Original pull-request #109356
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job