Repository navigation
Fix CREATE OR REPLACE MATERIALIZED VIEW ... POPULATE losing the source subscription - #108728
Conversation
…e subscription `CREATE OR REPLACE MATERIALIZED VIEW ... POPULATE` left the new view unsubscribed from its source table, so every row inserted after the replace was silently dropped, including rows inserted concurrently with the replace. The replace creates a temporary view, runs the `POPULATE` `INSERT` into it, then atomically swaps it with the target via `EXCHANGE`. The `POPULATE` `INSERT` resolves the temporary table by name and caches its name -> UUID mapping in the per-query storage cache (`Context::getOrCacheStorage`). After the `EXCHANGE` that UUID is the new, live view, but the temporary name now refers to the old table that is about to be dropped. The internal `DROP` resolved the temporary name through the stale cache and shut down the live view instead, detaching it from its source. Fix `getOrCacheStorage` to treat a cached entry as stale when the resolved storage no longer has the requested name (it was renamed or reassigned within the same query) and re-resolve it by name. Closes: #108726 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [a5dd2d3] Summary: ✅
AI ReviewSummaryThis PR fixes the deterministic Final Verdict
|
Addresses review feedback on #108728: the existing test only inserted into the source after `CREATE OR REPLACE MATERIALIZED VIEW ... POPULATE` had completed, so it covered the deterministic detached-view symptom but not the concurrent scenario the issue describes (rows inserted while the replace is in progress). This adds `04490_..._concurrent.sh`, which inserts into the source concurrently with the replace (the snapshot window is widened via `merge_tree_storage_snapshot_sleep_ms` so the inserts reliably overlap it) and asserts that no source row is lost - the set of distinct ids in the view equals the set in the source. Verified non-flaky across many local runs against a build that carries the fix. The test asserts only that no row is lost (the contract this change restores). It deliberately does not assert exactly-once delivery: plain `POPULATE` is not atomic, so a concurrently inserted row can be both captured by the snapshot and delivered through the subscription, i.e. duplicated. Making `POPULATE` exactly-once is a separate change. The existing deterministic test is renumbered from `04415` to `04489` because the merge with master introduced colliding `04415_*` tests.
|
Pushed
On the review point about the dependency-graph window during the rename/ CI: the only red is |
The per-query storage cache (`Context::getOrCacheStorage`) is keyed by qualified name only (`StorageCache::Shard::set` uses `StorageID::DatabaseAndTableNameEqual`), so a cache hit can carry a UUID that differs from the `StorageID` being resolved. After a same-name replacement the name can map to UUID B while a caller still asks for UUID A; the freshness check only compared names, so it would hand out UUID B instead of letting the fresh lookup report `UNKNOWN_TABLE`/`TABLE_UUID_MISMATCH`. Require UUID equality on the cache-hit path when the requested `StorageID` has a UUID. This only makes the cache more conservative (more re-resolutions through the authoritative lookup); it never returns a different table than requested. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ent no-loss The concurrent regression test asserted that no source row is lost when inserts race the replace (`uniqExact(mv) = uniqExact(src)`). That contract is not what this PR provides: the source-to-view dependency transfer inside the internal `EXCHANGE` (`InterpreterRenameQuery`) is not yet atomic, so a row inserted in the brief instant the replace swaps the tables can still be missed. Under the sanitizer and loaded CI runs that window opened reliably, so the test failed across Fast test and every flaky check. Rewrite it to prove only the deterministic fix this PR makes: once the (concurrent) replace has completed, the new view is still subscribed to its source, so a sentinel row inserted after everything settles must reach the view. This is deterministic and independent of the residual race. Also enable `set -e`, keep the background-insert PID and `wait` on it explicitly, so a failed insert fails the test (previously the bare `wait` could not). Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108728&sha=c86d0bc3a50d1575af7560811b4db5532cbca979&name_0=PR&name_1=Fast%20test%20%28arm_darwin%29 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the AI review's "Needs changes" verdict and the CI failure.
Merged |
The flaky check failed `04490_..._concurrent` because the last revision enabled
`set -e` and `wait`-ed on the background inserts "so a failed insert fails the
test". But a concurrent `INSERT INTO src` that lands in the brief instant the
replace swaps the tables can legitimately fail with `UNKNOWN_TABLE` ("Target
table ... of view ... doesn't exist"): the source-to-view dependency transfer
inside the internal `EXCHANGE` is not yet atomic, so the new view's target table
is momentarily unreachable. That is precisely the residual window this PR
documents as out of scope, so failing the test on it contradicts the test's own
stated contract (assert subscription liveness, not concurrent delivery).
Make the concurrent inserts tolerate that specific `UNKNOWN_TABLE` error while
still failing the test on any other insert error. The assertion is unchanged: a
sentinel row inserted after everything settles must reach the view.
Verified against a local build carrying the fix: the rewritten test passes 50/50
runs, and it still fails (sentinel count 0) against a build without the fix, so
it continues to catch the regression. The error-handling itself was unit-checked
to tolerate `UNKNOWN_TABLE` and re-raise any other error.
Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108728&sha=84561e343cc5b95655a0d81ef488845931b9d82b&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29
PR: #108728
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Pushed Fixed the flaky check on this PR's own test (
|
`master` now carries `04489_max_threads_auto_parsing_compat` and two `04490_*` tests, which collided with the deterministic and concurrent regression tests added here. Renumber them to the next free prefixes `04492`/`04493` and update the cross-reference comment in the concurrent test. Test-only change; the test bodies are unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Pushed Cleared the AI-Review "Request changes" verdictIts sole minimum-required action was the issue relationship. I changed This is the conservative direction: under-claiming what the PR fixes is safer than Renumbered the regression tests off a fresh
|
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 7/7 (100.00%) · Uncovered code |
The per-query storage cache `Context::getOrCacheStorage` (added in ClickHouse#97411) pins a table name to the storage first resolved for it within a query, so a running query keeps reading the table version it was planned against even when a concurrent query renames or exchanges that table. ClickHouse#108728 added two freshness checks to the cache-hit path - reject the hit if the cached storage's current qualified name no longer matches the requested name, and reject it if the requested `StorageID` carries a UUID that differs from the cached one. Both silently defeat the pinning: after a concurrent `EXCHANGE TABLES` renames the pinned storage (and reassigns the name to a different UUID), resolving the original name re-resolves to the swapped table, so the query is analyzed for one column type and executed against another - raising a spurious `LOGICAL_ERROR` (and aborting the server in debug/coverage builds). This regressed `03915_exchange_tables_race` on `master`: the check `Stateless tests (amd_llvm_coverage, old analyzer, s3 storage, DBReplicated, WasmEdge, parallel, 2/3)` started dying with Logical error: 'Arguments of 'multiply' have incorrect data types: 'n' of type 'Int256', '0.123' of type 'Float64'' on 2026-07-07 - the day ClickHouse#108728 merged - after 4.5 months with zero occurrences. Only the old analyzer is affected (the new analyzer resolves storages once). Restore the pinning: a cache hit is reused whenever its cached UUID still resolves to a live table, regardless of the storage's current name or the requested UUID (this is the pre-ClickHouse#108728 behavior). To keep ClickHouse#108728's fix - `CREATE OR REPLACE ... POPULATE` must drop the old table by the temporary name after its internal `EXCHANGE`, not the live one - a query that renames or exchanges a table now drops the affected names from its own per-query storage cache (`dropStorageCacheEntry`, called from `InterpreterRenameQuery`). So the query's own later lookups re-resolve to the current tables, while concurrent queries keep their own per-query caches and stay isolated. Verified on a local build: 3000 `SELECT n * 0.123 FROM (SELECT * FROM t)` racing a continuous `EXCHANGE TABLES` under the old analyzer produce zero `LOGICAL_ERROR` with the fix (vs. reproducing it with the ClickHouse#108728 checks in place), while `04492`/`04493` keep passing. Covered by existing tests: `03915_exchange_tables_race` (external exchange, must not raise `LOGICAL_ERROR`) and `04492`/`04493_create_or_replace_materialized_view_populate_keeps_subscription` (internal exchange, subscription must survive). CI report (Server died on master): https://play.clickhouse.com/play?user=play&run=1&tab=Query%20F#U0VMRUNUIGNoZWNrX3N0YXJ0X3RpbWUsIGNoZWNrX25hbWUsIHRlc3RfbmFtZSwgcmVwb3J0X3VybApGUk9NIGNoZWNrcwpXSEVSRSBjaGVja19zdGFydF90aW1lID49IG5vdygpIC0gSU5URVJWQUwgMjQgSE9VUgogICAgQU5EIChoZWFkX3JlZiA9ICdtYXN0ZXInIEFORCBzdGFydHNXaXRoKGhlYWRfcmVwbywgJ0NsaWNrSG91c2UvJykpCiAgICBBTkQgdGVzdF9zdGF0dXMgIT0gJ1NLSVBQRUQnCiAgICBBTkQgKHRlc3Rfc3RhdHVzIExJS0UgJ0YlJyBPUiB0ZXN0X3N0YXR1cyBMSUtFICdFJScpCiAgICBBTkQgY2hlY2tfc3RhdHVzICE9ICdzdWNjZXNzJwogICAgQU5EIGNoZWNrX25hbWUgTk9UIExJS0UgJ2xpYkZ1enplciUnCiAgICBBTkQgY2hlY2tfbmFtZSAhPSAnQ2xpY2tIb3VzZSBLZWVwZXIgSmVwc2VuJwogICAgQU5EIGNoZWNrX25hbWUgTk9UIElMSUtFICclZXhwZXJpbWVudGFsJScKICAgIEFORCBwb3NpdGlvbih0ZXN0X25hbWUsICdTZXJ2ZXIgZGllZCcpID4gMApPUkRFUiBCWSBjaGVja19zdGFydF90aW1l Related: ClickHouse#97411 Related: ClickHouse#108728 Related: ClickHouse#108726 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`CREATE OR REPLACE MATERIALIZED VIEW` does not exist on 26.3: the parser guards the `MATERIALIZED` keyword with `!replace_view` and only master dropped that guard, in `ed062fd96e1` (first released in 26.5). Both backported tests therefore failed with `Syntax error ... at position 19 (MATERIALIZED)` rather than exercising anything. `04493` also collided with the existing `04493_async_insert_dedup_optimize_on_insert_replicated_merge_tree`. The backported fix is still wanted here, because the same stale storage-cache entry breaks `CREATE OR REPLACE TABLE ... AS SELECT`, which 26.3 does support: the internal drop of the replaced table resolves the temporary name to the new, live table and cancels its merges forever. So both tests are replaced by one that covers that path. Verified in both directions with release binaries: on 26.3.17.110, which lacks the fix, `OPTIMIZE` fails with `Code: 236 ... Cancelled merging parts (ABORTED)`; on 26.7.4.58, which has it, the test passes. Related: #115288 Related: #108728 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… POPULATE losing the source subscription
Backport #108728 to 26.6: Fix CREATE OR REPLACE MATERIALIZED VIEW ... POPULATE losing the source subscription
Backport #108728 to 26.5: Fix CREATE OR REPLACE MATERIALIZED VIEW ... POPULATE losing the source subscription
Backport #108728 to 26.3: Fix CREATE OR REPLACE MATERIALIZED VIEW ... POPULATE losing the source subscription
…D VIEW ... POPULATE losing the source subscription
…D VIEW ... POPULATE losing the source subscription
The per-query storage cache `Context::getOrCacheStorage` (added in ClickHouse#97411) pins a table name to the storage first resolved for it within a query, so a running query keeps reading the table version it was planned against even when a concurrent query renames or exchanges that table. ClickHouse#108728 added two freshness checks to the cache-hit path - reject the hit if the cached storage's current qualified name no longer matches the requested name, and reject it if the requested `StorageID` carries a UUID that differs from the cached one. Both silently defeat the pinning: after a concurrent `EXCHANGE TABLES` renames the pinned storage (and reassigns the name to a different UUID), resolving the original name re-resolves to the swapped table, so the query is analyzed for one column type and executed against another - raising a spurious `LOGICAL_ERROR` (and aborting the server in debug/coverage builds). This regressed `03915_exchange_tables_race` on `master`: the check `Stateless tests (amd_llvm_coverage, old analyzer, s3 storage, DBReplicated, WasmEdge, parallel, 2/3)` started dying with Logical error: 'Arguments of 'multiply' have incorrect data types: 'n' of type 'Int256', '0.123' of type 'Float64'' on 2026-07-07 - the day ClickHouse#108728 merged - after 4.5 months with zero occurrences. Only the old analyzer is affected (the new analyzer resolves storages once). Restore the pinning: a cache hit is reused whenever its cached UUID still resolves to a live table, regardless of the storage's current name or the requested UUID (this is the pre-ClickHouse#108728 behavior). To keep ClickHouse#108728's fix - `CREATE OR REPLACE ... POPULATE` must drop the old table by the temporary name after its internal `EXCHANGE`, not the live one - a query that renames or exchanges a table now drops the affected names from its own per-query storage cache (`dropStorageCacheEntry`, called from `InterpreterRenameQuery`). So the query's own later lookups re-resolve to the current tables, while concurrent queries keep their own per-query caches and stay isolated. Verified on a local build: 3000 `SELECT n * 0.123 FROM (SELECT * FROM t)` racing a continuous `EXCHANGE TABLES` under the old analyzer produce zero `LOGICAL_ERROR` with the fix (vs. reproducing it with the ClickHouse#108728 checks in place), while `04492`/`04493` keep passing. Covered by existing tests: `03915_exchange_tables_race` (external exchange, must not raise `LOGICAL_ERROR`) and `04492`/`04493_create_or_replace_materialized_view_populate_keeps_subscription` (internal exchange, subscription must survive). CI report (Server died on master): https://play.clickhouse.com/play?user=play&run=1&tab=Query%20F#U0VMRUNUIGNoZWNrX3N0YXJ0X3RpbWUsIGNoZWNrX25hbWUsIHRlc3RfbmFtZSwgcmVwb3J0X3VybApGUk9NIGNoZWNrcwpXSEVSRSBjaGVja19zdGFydF90aW1lID49IG5vdygpIC0gSU5URVJWQUwgMjQgSE9VUgogICAgQU5EIChoZWFkX3JlZiA9ICdtYXN0ZXInIEFORCBzdGFydHNXaXRoKGhlYWRfcmVwbywgJ0NsaWNrSG91c2UvJykpCiAgICBBTkQgdGVzdF9zdGF0dXMgIT0gJ1NLSVBQRUQnCiAgICBBTkQgKHRlc3Rfc3RhdHVzIExJS0UgJ0YlJyBPUiB0ZXN0X3N0YXR1cyBMSUtFICdFJScpCiAgICBBTkQgY2hlY2tfc3RhdHVzICE9ICdzdWNjZXNzJwogICAgQU5EIGNoZWNrX25hbWUgTk9UIExJS0UgJ2xpYkZ1enplciUnCiAgICBBTkQgY2hlY2tfbmFtZSAhPSAnQ2xpY2tIb3VzZSBLZWVwZXIgSmVwc2VuJwogICAgQU5EIGNoZWNrX25hbWUgTk9UIElMSUtFICclZXhwZXJpbWVudGFsJScKICAgIEFORCBwb3NpdGlvbih0ZXN0X25hbWUsICdTZXJ2ZXIgZGllZCcpID4gMApPUkRFUiBCWSBjaGVja19zdGFydF90aW1l Related: ClickHouse#97411 Related: ClickHouse#108728 Related: ClickHouse#108726 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The read-consistency direction was only covered by `03915_exchange_tables_race`, which did not pin `enable_analyzer`. The spurious `LOGICAL_ERROR` from a table exchanged mid-query only reproduces on the old analyzer, so the test previously caught the regression from ClickHouse#108728 only because a CI shard happened to run with the old analyzer. Loop the race over both analyzers and pin `enable_analyzer` per run so the fixed path is exercised deterministically on any configuration, while keeping new-analyzer coverage of the exchange race. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Addresses review feedback on ClickHouse#108728: the existing test only inserted into the source after `CREATE OR REPLACE MATERIALIZED VIEW ... POPULATE` had completed, so it covered the deterministic detached-view symptom but not the concurrent scenario the issue describes (rows inserted while the replace is in progress). This adds `04490_..._concurrent.sh`, which inserts into the source concurrently with the replace (the snapshot window is widened via `merge_tree_storage_snapshot_sleep_ms` so the inserts reliably overlap it) and asserts that no source row is lost - the set of distinct ids in the view equals the set in the source. Verified non-flaky across many local runs against a build that carries the fix. The test asserts only that no row is lost (the contract this change restores). It deliberately does not assert exactly-once delivery: plain `POPULATE` is not atomic, so a concurrently inserted row can be both captured by the snapshot and delivered through the subscription, i.e. duplicated. Making `POPULATE` exactly-once is a separate change. The existing deterministic test is renumbered from `04415` to `04489` because the merge with master introduced colliding `04415_*` tests.
The flaky check failed `04490_..._concurrent` because the last revision enabled
`set -e` and `wait`-ed on the background inserts "so a failed insert fails the
test". But a concurrent `INSERT INTO src` that lands in the brief instant the
replace swaps the tables can legitimately fail with `UNKNOWN_TABLE` ("Target
table ... of view ... doesn't exist"): the source-to-view dependency transfer
inside the internal `EXCHANGE` is not yet atomic, so the new view's target table
is momentarily unreachable. That is precisely the residual window this PR
documents as out of scope, so failing the test on it contradicts the test's own
stated contract (assert subscription liveness, not concurrent delivery).
Make the concurrent inserts tolerate that specific `UNKNOWN_TABLE` error while
still failing the test on any other insert error. The assertion is unchanged: a
sentinel row inserted after everything settles must reach the view.
Verified against a local build carrying the fix: the rewritten test passes 50/50
runs, and it still fails (sentinel count 0) against a build without the fix, so
it continues to catch the regression. The error-handling itself was unit-checked
to tolerate `UNKNOWN_TABLE` and re-raise any other error.
Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108728&sha=84561e343cc5b95655a0d81ef488845931b9d82b&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29
PR: ClickHouse#108728
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…place-mv-race Fix CREATE OR REPLACE MATERIALIZED VIEW ... POPULATE losing the source subscription
OTHER: CREATE OR REPLACE TABLE ... AS SELECT over an existing table -> the internal DROP of the replaced table resolves the temporary name through a stale storage-cache entry and shuts down the new, live table: its merges are cancelled for good (every OPTIMIZE fails with ABORTED, background merges never run again until a restart). Upstream reaches the same cache also through CREATE OR REPLACE MATERIALIZED VIEW ... POPULATE (the source subscription is lost). Effect in the production build a394e92: the backport's test 04492_create_or_replace_table_as_select_keeps_merges fails on it and passes on official 26.8.8.8, run on isolated servers (evidence/fix_ci_20260925/upstream_repro/corrected3/). The upstream tests 04492/04493 (MATERIALIZED VIEW ... POPULATE) are no evidence here: 26.3 has no CREATE OR REPLACE MATERIALIZED VIEW, so they fail on it with a syntax error. Upstream patch unchanged, with the backport's test: 04492_create_or_replace_table_as_select_keeps_merges. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… topology check for the open GROUP BY defect - stateless_tests.txt: +77 tests of the third batch and 10319 (13 pyarrow tests run outside the gate). - known_defects.json: +64 KD-UP entries (fixed, each naming its commit), now 145 entries, 1 open. Three entries use the tests their 26.3 backports carry instead of the upstream tests the triage had used: ClickHouse#108728 (26.3 has no CREATE OR REPLACE MATERIALIZED VIEW; 04492_create_or_replace_table_as_select_keeps_merges), ClickHouse#110590 (05059 aborts the production server; 04307/04350 with the separately backported guards; 03057 is external), ClickHouse#117536 (05055 and the fork test 10319; upstream 04401 also needs ClickHouse#108465). - KD-D512-MIXED-GROUPBY-NULLABLE stays open; its decision records the isolated validation of the replica-group upgrade topology and the rules it needs. It is not a rollout approval. - tools/replication/upgrade_topology_check.sh: that validation, vendored from the skill draft. - tools/upstream/triage_server_tests.py: a row whose test came from the upstream PR (the backport does not carry it) is classed `...-upstream-test` (a lead to review, not evidence); https URLs and web disks count as external. - VENDORED.sha256 and README (selection count, the mandatory mixed-version GROUP BY release check - the text still said it was not part of the gate - and the two tools). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #108726
Related: #108715
CREATE OR REPLACE MATERIALIZED VIEW ... POPULATEleft the new view unsubscribed from its source table, so every row inserted after the replace was silently dropped.The replace creates a temporary view, runs the
POPULATEINSERTinto it, then atomically swaps it with the target viaEXCHANGE. ThePOPULATEINSERTresolves the temporary table by name and caches its name → UUID mapping in the per-query storage cache (Context::getOrCacheStorage). After theEXCHANGEthat UUID is the new, live view, but the temporary name now refers to the old table that is about to be dropped. The internalDROPthen resolved the temporary name through the stale cache and shut down the live view instead, detaching it from its source.The fix makes
getOrCacheStoragetreat a cached entry as stale when the resolved storage no longer has the requested name (it was renamed or reassigned within the same query, e.g. by the internalEXCHANGEofCREATE OR REPLACE), or when the caller asked for a specific UUID that the name-keyed cache no longer matches. In those cases the entry is dropped and the table is re-resolved by name. This is a natural extension of the existing staleness check that already re-resolves when the cached UUID no longer exists.With the fix the new view stays subscribed to its source after the replace, so inserts that follow the replace reach the view again.
Note on scope: this fixes the deterministic detach. A separate, much narrower window remains — the source-to-view dependency transfer inside the
EXCHANGE(InterpreterRenameQuery) is not yet atomic, so a row inserted concurrently, in the brief instant the replace is swapping the tables, can still be missed (and an insert that lands exactly mid-swap can even fail withUNKNOWN_TABLE). Making that transfer atomic (andPOPULATEexactly-once) is left for a follow-up. Because #108726 asks for full concurrent exactly-once delivery, which is only partially addressed here, this PR is markedRelated:rather thanCloses:and #108726 stays open to track that residual concurrent contract (see also #108715).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
CREATE OR REPLACE MATERIALIZED VIEW ... POPULATEleaving the new view unsubscribed from its source table, which silently dropped every row inserted after the replace.Documentation entry for user-facing changes
Version info
26.6.4.3,26.5.8.5,26.3.20.9