Repository navigation
Fix UNKNOWN_IDENTIFIER in a multi-command ALTER that changes a column and then mutates it - #130322
Conversation
`ALTER TABLE t ADD COLUMN c UInt8, UPDATE c = 1 WHERE c = 0` failed with UNKNOWN_IDENTIFIER `c` when `enable_shared_storage_snapshot_in_query` is enabled (the default). `runCommandSegments` runs the segments of one ALTER one after another, but it cleared only the query-scoped metadata cache, and only before the ALTER segment's own reads. Those reads re-cache the pre-ALTER metadata, and a mutation segment that ran earlier leaves its storage snapshot cached. The following segments then analyze their expressions against the old columns: `TableNode(storage, context)` in `MutationsInterpreter::validate`, `findNonDeterministicVirtualColumn` for Replicated tables, and the structure check of `ATTACH PARTITION ... FROM`. Drop both caches before the ALTER segment's reads and after it commits. The clear is full, not per table: an `Alias` table forwards to its target, so the cache entries are keyed by the target storage. Between segments nothing reads data through the query's snapshots (mutations execute in background tasks with their own context, and validation never executes), so the clear does not change any part set the query reads. Closes ClickHouse#130110 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the doc comment of dropQueryMetadataCache that restates its body; state the post-ALTER drop as an invariant. 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; 2 findings over 2 runs), plus my own review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261109-122300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261109-113100 |
|
Workflow [PR], commit [33e5c53] Summary: ✅
AI ReviewSummaryThe PR clears both query-scoped caches before and after metadata-changing Final VerdictStatus: ✅ Approve |
|
cc @Michicosun @evillique, could you review this and add the |
…n one ALTER Each of these also reads the table through the query-scoped caches after the preceding ALTER segment committed, and fails on master with enable_shared_storage_snapshot_in_query = 1: - RENAME COLUMN v TO w, UPDATE w = ... WHERE w = ...: UNKNOWN_IDENTIFIER w - ADD COLUMN c, DELETE WHERE c = 0: UNKNOWN_IDENTIFIER c - ADD COLUMN e, MOVE PARTITION ... TO TABLE: INCOMPATIBLE_COLUMNS Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units8 translation units recompiled, 32 s compile time in total, 8 of them have a recent master baseline. |
…et's lock runCommandSegments clears the query-scoped caches after taking the locks of the table it ALTERs, but for an Alias those are the alias's own locks: the reads that follow cache the target's metadata, and StorageAlias::alter takes the target's DDLGuard and alter lock only later. StorageMergeTree::alter then builds the new metadata from that cached copy, so a concurrent ALTER of the target that committed in between is overwritten. Measured: 150 ALTER ... ADD COLUMN through the alias concurrently with 150 directly on the target leave 151 columns instead of 301 with enable_shared_storage_snapshot_in_query = 1, and 301 with it set to 0. Clear the caches again once the target's alter lock is held, as StorageMaterializedView::alter does for its inner table. The clear of both caches moves into QueryMetadataCache::clear, also used by runCommandSegments. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵️ @groeneai Reviewed — the fix is correct and well-targeted for its scope, and CI is green. One sibling gap worth folding into this PR:
This PR introduces if (auto metadata_cache = local_context->getQueryMetadataCache())
{
auto [cache, cache_lock] = metadata_cache->getStorageMetadataCache();
cache->clear();
}Its own comment ("the alter below reads the inner table's metadata through that cache") is the same premise as the Suggested fix (same one-liner as the other two sites): if (auto metadata_cache = local_context->getQueryMetadataCache())
metadata_cache->clear();If you believe the inner comment-only alter can't be affected by the snapshot cache (e.g. it never re-resolves through |
The column-comment ALTER that a materialized view forwards to its inner table cleared only the metadata half of the query caches after taking the inner table's alter lock. Use the same full clear as runCommandSegments and StorageAlias::alter, so the three sites do not diverge. No behaviour change: nothing on that path reads through the snapshot cache (checkAlterIsPossible and the MergeTree alter read only metadata). Measured: 20 COMMENT COLUMN through the view racing 20 ADD COLUMN on the inner table keep all 21 columns with this change and on master, and keep 1 to 7 when the clear is removed entirely. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Done in 24738af: It changes no behaviour there, because that path reads no snapshot: |
…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]>
|
Yes. #130528 is in the merge queue now; once it is in master I will update this PR on top of it and drop the duplicated |
…stale-query-metadata-cache # Conflicts: # src/Storages/StorageAlias.cpp
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]>
|
Updated: merged master with #130528 in 0cfdb5e, taking its side of the I also removed 05617 in 33e5c53: without the Every case of 05616 still fails on master with #130528 merged, and passes 50 of 50 randomized runs with this PR. The description is updated. |
…napshot The merged master has ClickHouse#130322, which refreshes the shared snapshot of the query between the segments of an `ALTER`, so 05483 no longer needs `enable_shared_storage_snapshot_in_query = 0`. Related: ClickHouse#123959 Related: ClickHouse#130110 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #130110
Related: #92118
Related: #127982
Related: #130528
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed an
ALTER TABLEthat combines a column change with a mutation or a partition command, such asALTER TABLE t ADD COLUMN c UInt8, UPDATE c = 1 WHERE c = 0, failing withUNKNOWN_IDENTIFIER(orINCOMPATIBLE_COLUMNSforATTACH PARTITION ... FROMandMOVE PARTITION ... TO TABLE) whenenable_shared_storage_snapshot_in_queryis enabled, which is the default. Closes #130110.Description
On 26.10.2.103 (also 26.6.8.7 and 26.8.18.2, not 26.3.60.2), the repro from #130110 fails with
UNKNOWN_IDENTIFIER; it works withenable_shared_storage_snapshot_in_query = 0.Root cause:
runCommandSegmentscleared only the metadata cache, and only before the ALTER segment's own reads, which then cache the pre-ALTER metadata again. The later segments analyze against the old columns:MutationsInterpreter::validate,findNonDeterministicVirtualColumnand the partition commands' structure checks.Change:
runCommandSegmentscallsQueryMetadataCache::clear(added by #130528), which drops both the metadata and the snapshot cache, before the ALTER segment's reads and after it commits.StorageMaterializedView::alternow uses it too instead of its metadata-only clear, with no behaviour change: that path reads no snapshot. The clear is full, since anAliascaches under its target's storage.Covered: MergeTree, ReplicatedMergeTree,
Alias;UPDATE/DELETEafterADD COLUMNorRENAME COLUMN,UPDATEbefore and after, both partition commands. AReplicateddatabase rejects mixedALTERs.Not changed (the same with the setting off): the
session_timezonerewrite ofDateTimeliterals uses the columns from before the statement; withalter_sync = 0ReplicatedMergeTree does not wait before the next command.Validation: each case of 05616 fails on master (including #130528); with the fix it passes 50 of 50 randomized runs.
Workflow [PR]
Sync PR [sync-upstream/pr/130322]
Version info
26.11.1.2209-master(included in26.11and later)