Repository navigation
Fix data loss when a merge races ALTER RENAME COLUMN on a column without a default expression - #109356
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 ClickHouse#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 ClickHouse#80648 that the primary fix (ClickHouse#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: ClickHouse#104822 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-4:20260703-191500 |
|
cc @Avogar @CurtizJ — could you review this? It is the follow-up to #104822 for #80648: a merge that races |
|
Workflow [PR], commit [56319e1] Summary: ❌
AI ReviewSummaryThis PR fixes the single-rename and patch-only expired-columns cases in Findings❌ Blockers
Final VerdictRequest changes. |
The probabilistic race (25 iterations at ~0.25% per iteration) caught the residual no-default facet of ClickHouse#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]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-2:20260703-212800 |
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]>
CI finish ledger — 9198d5bCI fully finished on this head: all non-exempt checks passed, zero failures. The only non-passing check is
(The 04238 Bugfix-validation failures seen earlier were on superseded force-pushed commits Session id: cron:our-pr-ci-monitor:20260704-053000 |
…residual-dynamic-expired-columns
…residual-dynamic-expired-columns
|
This also fixes a second, crashier manifestation of the same expired-columns-vs-RENAME-COLUMN bug: on a single-column Found by BuzzHouse (arm_asan_ubsan), STID 4909-6cd4: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107091&sha=5e31ccb31390e87186ca0a7a5a047aec6968c392&name_0=PR&name_1=BuzzHouse%20%28arm_asan_ubsan%29 (rare pre-existing finding, 0 master hits). Deterministic repro on master, and it passes with this PR's change: CREATE TABLE t (c0 UInt256) ENGINE=MergeTree ORDER BY tuple() PRIMARY KEY tuple();
INSERT INTO t VALUES (1);
INSERT INTO t VALUES (2);
SYSTEM ENABLE FAILPOINT mt_select_parts_to_mutate_no_free_threads;
ALTER TABLE t RENAME COLUMN c0 TO c1 SETTINGS alter_sync=0;
OPTIMIZE TABLE t FINAL; -- aborts on master, ok with this PR |
…residual-dynamic-expired-columns
| auto conversions = MergeTreeData::getAlterConversionsForPart(part, mutations_snapshot, global_ctx->context); | ||
| for (const auto & rename : conversions->getRenameMap()) | ||
| { | ||
| if (columns_present_in_parts.contains(rename.rename_from)) |
There was a problem hiding this comment.
If the old name is reused before the RENAME COLUMN mutation materializes, this still misbinds data. RENAME a -> b is a barrier, but a later non-barrier ADD COLUMN a ... does not wait for that pending rename (StorageMergeTree::alter only waits when the current ALTER contains a barrier), so a merge can see metadata with both a and b while the source parts still physically store only the pre-rename a.
In that state this loop keeps b alive, but the read path then maps logical b back to physical a (MergeTreeBlockReadUtils.cpp, getColumnOldName) while logical a is also treated as the same physical a, because pending renames do not make rename_from look stale the way pending DROP COLUMN does. The merged part will therefore read the old bytes into both columns instead of defaulting the re-added a.
Can we also mark rename_from as unavailable for direct lookup once a pending rename claims it (or otherwise distinguish a re-added old name), so the old on-disk column can back only b and not both names?
There was a problem hiding this comment.
Confirmed and fixed in 1f934b6.
You are right that re-adding the old name before the pending RENAME materializes is a real problem, and I reproduced it deterministically (failpoint mt_select_parts_to_mutate_no_free_threads + alter_sync=0, same idiom as the existing phases). The manifestation is a bit sharper than a silent read: keeping b alive off the physical a while a is also re-added makes the merged part carry both a and b from one physical column, and the pending rename mutation then hits getColumnsForNewDataPart (MutateTask.cpp:912) with b already present and aborts:
LOGICAL_ERROR: trying to rename column a to b, but part all_1_1_1 already has column b
Root cause is exactly the asymmetry you point at: the expired-columns loop only checks columns_present_in_parts.contains(rename_from), and rename_from is not treated as stale when it is re-added (unlike a pending DROP, which AlterConversions::dropped_columns handles).
Fix: skip the keep-alive when rename_from is itself a live storage column (the re-added case). The merge then expires b as it did before the #80648 change; the rename mutation re-derives b from the physical a, and the re-added a is defaulted. That matches master's behavior for this case, and the #80648 Dynamic-no-default preservation is untouched for the pure-rename case.
Both directions, deterministic, Build-ID verified (debug):
- master (no fix,
6DD87B5D...): reuse scenario already correct (a=default,b=old data, no crash). - current PR without this guard (
D4A8582A...): full 04238 FAILS (server abort above). - with this guard (
FEC5BFAE...): 04238 PASS 30/30; Dynamic-no-default phase stillcount=8.
Added Phase 3 to 04238_alter_rename_column_merge_race.sh (RENAME a->b pending, then ADD COLUMN a, then OPTIMIZE FINAL) that fails without the guard and passes with it.
There was a problem hiding this comment.
The current code still does not make the re-added a default while the RENAME COLUMN a TO b mutation is still pending.
MergeTask.cpp:728-744 now expires b in the reuse case, but the read side is unchanged:
MergeTreeBlockReadUtils.cpp:105-123still maps logicalbback to physicalaviagetColumnOldName.IMergeTreeReader.cpp:370-385will also read logicalafrom the same physicalaif that file exists in the part.AlterConversions.cpp:292-308only treats an on-disk column as stale when it is indropped_columns; a pending rename does not markrename_fromstale.
So for the phase-3 merged part shape (system.parts_columns = ['a'] while the rename is still pending), both logical names still resolve to the old a bytes until the rename mutation materializes. This removes the later LOGICAL_ERROR, but it does not actually deliver the “re-added a defaults” behavior claimed above.
I think the original read-side fix is still needed here: once a pending rename claims physical a for logical b, direct lookup of logical a has to treat that on-disk a as stale, the same way DROP COLUMN/re-add does.
Follow-up on the ClickHouse#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 ClickHouse#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 (ClickHouse#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]>
5a45545
Cherry pick #109356 to 25.8: Fix data loss when a merge races ALTER RENAME COLUMN on a column without a default expression
…ME COLUMN on a column without a default expression
…ME COLUMN on a column without a default expression
Backport #109356 to 26.7: Fix data loss when a merge races ALTER RENAME COLUMN on a column without a default expression
Backport #109356 to 26.5: Fix data loss when a merge races ALTER RENAME COLUMN on a column without a default expression
Backport #109356 to 26.3: Fix data loss when a merge races ALTER RENAME COLUMN on a column without a default expression
…ME COLUMN on a column without a default expression
Backport #109356 to 26.6: Fix data loss when a merge races ALTER RENAME COLUMN on a column without a default expression
|
Follow-up to my design question above (issuecomment-5123880170), now that this PR is merged. Merging it answered the fork I asked about: keeping the pending rename's target alive during the merge The remaining defect. A compound single-ALTER rename Recognizing the compound case in the producer is small. The problem is the consumer, which has to know
So the merge has to record what it materialized, and by this repo's own precedent a new per-part The question: is a per-part record plus a protocol bump acceptable for this case, or would you Happy to implement either. I am not opening a PR until the cost question is settled, since the two Repro, if useful (debug build, CREATE TABLE swap (id UInt64, a String, b String)
ENGINE = MergeTree ORDER BY id SETTINGS min_bytes_for_wide_part = 0;
INSERT INTO swap VALUES (1,'A1','B1'),(2,'A2','B2');
INSERT INTO swap VALUES (3,'A3','B3'),(4,'A4','B4');
SYSTEM ENABLE FAILPOINT mt_select_parts_to_mutate_no_free_threads;
ALTER TABLE swap RENAME COLUMN a TO c, RENAME COLUMN b TO a SETTINGS alter_sync = 0;
OPTIMIZE TABLE swap FINAL;
SYSTEM DISABLE FAILPOINT mt_select_parts_to_mutate_no_free_threads;
SELECT id, c, a FROM swap ORDER BY id;Expected |
Backport #109356 to 25.8: Fix data loss when a merge races ALTER RENAME COLUMN on a column without a default expression
|
One more measurement for the question I asked above (issuecomment-5154420674), which I think makes the The same pending-rename window has a second face: a re-added source. The two-ALTER form no longer CREATE TABLE t (id UInt64, a String) ENGINE = MergeTree ORDER BY id
SETTINGS min_bytes_for_wide_part = 0;
INSERT INTO t SELECT number, 'payload_' || toString(number) FROM numbers(500);
ALTER TABLE t RENAME COLUMN a TO b, ADD COLUMN a String SETTINGS alter_sync = 0;
SELECT countIf(b != ''), countIf(a != '') FROM t; -- 500 500; `a` reads b's values
The reason I am reporting it here rather than sending the obvious fix: I implemented the read-side The mechanism is what I think is decision-relevant. The merge does not bake the rename in. It writes The keep-alive here deliberately declines to protect the target when Scoping the guard to query reads only would work on both axes I measured, but it gives one physical Affected on master and on the release builds of 26.7, 26.6, 26.5, 26.3 and 25.8. Default |
…ALTER RENAME COLUMN on a column without a default expression
…ALTER RENAME COLUMN on a column without a default expression
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix data loss of a column with no default expression when a merge runs concurrently with
ALTER TABLE ... RENAME COLUMN, or when the column's only values come from a lightweightUPDATE. The column could be dropped from the merged part, so all of its values read back as NULL.Description
Follow-up to #104822 (primary fix for #80648), which closed the data-loss window for columns with a default expression but left a residual for columns without one.
A merge applies a pending
RENAME COLUMN old -> newon-fly at read time:storage_columnsalready carriesnew, while the source parts still physically storeold.MergeTask's expired-columns optimization comparedstorage_columnsagainst the parts' raw physical names and, findingnewabsent from every part, treated it as an expired column. A column with no default expression is then dropped from the merged part, so every value reads back as NULL. Columns with a default (for exampleString DEFAULT '') are refilled from the default, which is why #104822 handles them but not columns without one.Fix: when scanning for expired columns, treat a rename target as present whenever a source part physically holds the matching old name, using the
alter_conversionsrename map the merge already uses to apply the rename on-fly. A genuinely dropped column, absent from all parts, still expires as before. Merge selection, part naming and mutation-version stamping are untouched.The same scan had a second carrier: it only looked at the base parts, so a column added by
ADD COLUMNand then filled by a lightweightUPDATE- whose only values live in a patch part selected for the merge - was expired too, and the patch never requested (no rename needed to reproduce). The scan now also considers the selected patch parts' physical columns, both for the column's own name and for a pending rename target.A new test
04648_alter_rename_column_no_default_merge_racepins both carriers with a deterministic reproducer (failpoint-pinned pending rename +OPTIMIZE FINAL), covering a plain column, aDynamiccolumn, a rename target whose old name is re-added first, and the patch-part cases. The existing04238_alter_rename_column_merge_raceis left untouched.Closes #111001
Version info
26.7.2.51,26.6.2.138,26.5.6.97,26.3.17.97,25.8.29.50