Repository navigation
Fix an ALTER through an Alias table overwriting enum values added concurrently to its target - #130528
Conversation
`InterpreterAlterQuery` validates and prepares the commands under the alias's own DDL guard and alter lock, and `StorageAlias::alter` only then locks the target. `AlterCommands::prepare` stores the complete merged type for `MODIFY COLUMN ... ADD ENUM VALUES`, computed from the column type read before the target was locked, so an `ADD ENUM VALUES` executed directly on the target while the alias ALTER waited for its lock was overwritten. Both ALTERs succeeded, the target's values were lost, and with `enable_shared_storage_snapshot_in_query = 0` the overwrite dropped enum values, so the target started a `MODIFY COLUMN` mutation serialized without a type: the table could not be attached after a restart (LOGICAL_ERROR). Copy the commands, prepare them against the target's current metadata (bypassing the query metadata cache) and run the target's `checkAlterIsPossible` under the target's alter lock, as `StorageMaterializedView::alter` does for its inner table. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 4 findings) plus my own cold review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261110-140200 |
|
Workflow [PR], commit [7335498] Summary: ✅
AI ReviewSummaryThis PR prevents concurrent Final VerdictStatus: ✅ Approve |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261109-163500 |
|
cc @evillique @tavplubix, could you review this and add the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes5 object files changed (+35.23 KiB total), 0 added.
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units14 translation units recompiled, 120 s compile time in total, 14 of them have a recent master baseline. |
`createEnumAdd` rewrites the elements written without a number into
`'name' = N` in place, and the relative meaning of `N` survives only in
the temporary enum type. `AlterCommands::prepare` passed it the command's
own AST, so a second `prepare` of the same commands, which
`StorageAlias::alter` now does under the target's lock, read `N` as an
explicit number: `ADD ENUM VALUES ('new')` through an `Alias` of a column
`Enum8('old' = 10)` committed `'new' = 1` instead of `'new' = 11`, and
with `'old' = 1` it was refused with `BAD_ARGUMENTS`. Give `createEnumAdd`
a copy instead.
Found by the automated review of ClickHouse#130528.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
`SharedMergeTree` records even a metadata-only `ALTER` in `system.mutations`, so the test's zero-mutation check cannot hold there (same note as in `04307_add_to_enum`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
…its target The commands are prepared again against the target's current metadata, but `checkAlterIsPossible` and the target's `alter` then read the metadata the query cached before the target's lock (`enable_shared_storage_snapshot_in_query = 1`). When a concurrent ALTER of the target dropped enum values in between, the re-prepared `ADD ENUM VALUES` looked like a type change against that stale copy and was persisted as a mutation without a type, so the table could no longer be attached (`MODIFY COLUMN mutation command doesn't specify type`). `QueryMetadataCache::clear` and the clear in `StorageAlias::alter` are the same as in ClickHouse#130322. 05618 gains a case where the target's enum is replaced concurrently. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The clear in `StorageAlias::alter` that 05617 tested now comes from ClickHouse#130528, and its test 05618 fails without that clear as well (19 mutations instead of 0, 5 of 5 runs; 05617 gives 21 columns instead of 41). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #130322
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
ALTER TABLE ... MODIFY COLUMN ... ADD ENUM VALUESexecuted through anAliastable overwriting the enum values that a concurrentALTERof the target table had added. Both queries succeeded, but the target's values were lost, and withenable_shared_storage_snapshot_in_query = 0the target table could no longer be attached after a restart.Description
On 26.10.2.103, two concurrent
ALTER TABLE ... MODIFY COLUMN e ADD ENUM VALUES (...), one through anAliastable and one on its target, both succeed, but the values added on the target are lost. Withenable_shared_storage_snapshot_in_query = 0the target also gets aMODIFY COLUMNmutation written without a type, and after a restart the table cannot be attached (MODIFY COLUMN mutation command doesn't specify type,LOGICAL_ERROR). Found while answering #130322 (comment).Root cause:
InterpreterAlterQueryvalidates and prepares the commands under the alias's locks, andStorageAlias::alterlocks the target only afterwards. ForADD ENUM VALUES,AlterCommands::preparestores the merged enum type, computed from the column type read before that lock, so it overwrites any value that a directALTERof the target committed in between.Change: after locking the target,
StorageAlias::alterclears the query metadata cache (as #130322 does), prepares a copy of the commands again against the target's current metadata and runs the target'scheckAlterIsPossibleon it. Without it, the target'saltercompares the new type with metadata cached before that lock, and writes an untyped mutation when a concurrentALTERdropped enum values.preparegivescreateEnumAdda copy of theADD ENUM VALUESlist, which it numbers in place, so a secondpreparestill sees them as implicit.validatestill runs before that lock, as it needs unprepared commands.Validation:
05618races 20ADD ENUM VALUESthrough anAliasagainst 20 on the target (master loses the target's values in 10 of 10 runs) and against 20ALTERs replacing the target's enum (19 untyped mutations per run without the clear). With the fix it passes 50 of 50 randomized runs.05619checks that values added without a number through anAliasfollow the target's largest value. Also checked: ReplicatedMergeTree and Memory targets, and a restart on raced data.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/130528]
Version info
26.11.1.2166-master(included in26.11and later)