Skip to content

Fix EXCHANGE TABLES dropping MV source dependencies (#105021) - #105029

Merged
CheSema merged 3 commits into
masterfrom
fix/exchange-tables-mv-source-105021
May 18, 2026
Merged

CheSema merged 3 commits into
masterfrom
fix/exchange-tables-mv-source-105021

Conversation

@CheSema

@CheSema CheSema commented May 15, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix silent data loss after EXCHANGE TABLES or CREATE OR REPLACE TABLE of a materialized view's source table. The MV's source-view dependency edge is now kept on the original name so it continues to fire on inserts. Regression introduced by #98779; restores the pre-regression behavior.

Description

Closes #105021.

EXCHANGE TABLES a, b (and the synthetic exchange used by CREATE OR REPLACE TABLE / REPLACE TABLE) cross-swapped the source-side view_dependencies edges along with the data. The MV's stored select_table_id is not rewritten by the rename, so cross-swapping orphaned the MV:

  • INSERT INTO a after exchange found no dependents — the edge had been moved to b.
  • INSERT INTO b was rejected by InsertDependenciesBuilder::observePath because the MV's recorded source name a no longer matched the parent b.

Both inserts silently dropped rows. Confirmed regression: v26.4.2.10 correct, master broken (since #98779).

The fix keeps source-view edges keyed by the original name on EXCHANGE. Plain RENAME TABLE keeps the post-#98779 re-keying behavior used by 01155_rename_move_materialized_view. This also subsumes the special case from #104907 — the synthetic CREATE OR REPLACE exchange routes through the same exchange_tables=true branch.

Tests

Design note

This restores ClickHouse's pre-#98779 behavior, where the source-view edge follows the name on EXCHANGE ("option C": MV reads from whatever now lives at the name it was created against). This is a regression repair, not the long-term answer. A proper redesign — identity-bound MV with atomic as_select rewrite, like PostgreSQL handles the same problem — would fix this case, the plain-RENAME case (04241), and #76303 in one shot. That redesign belongs in a separate, larger change.

Documentation entry for user-facing changes

  • Documentation is unnecessary.

Version info

  • Merged into: 26.5.1.747

CheSema and others added 2 commits May 15, 2026 14:00
Adds `04240_exchange_tables_drops_mv_source_dependencies`. After
`EXCHANGE TABLES a, b` where a materialized view reads from `a`,
inserts into both `a` and `b` silently stop feeding the `MV` target
table. Regression introduced by #98779 (correct on `v26.4.2.10`,
broken on master).

The reference pins the current buggy behavior: both post-exchange
`SELECT count() FROM dst` assertions stay at 1. When the bug is
fixed, the reference must be updated.

Related: #105021

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
`EXCHANGE TABLES a, b` (and the synthetic exchange used by
`CREATE OR REPLACE TABLE` / `REPLACE TABLE`) cross-swapped the
source-side `view_dependencies` edges along with the data. The
`MV`'s stored `select_table_id` is not rewritten by the rename,
so cross-swapping orphaned the `MV`:

* `INSERT INTO a` after exchange found no dependents — the edge
  had been moved to `b`.
* `INSERT INTO b` was rejected by
  `InsertDependenciesBuilder::observePath` because the `MV`'s
  recorded source name `a` no longer matched the parent `b`.

Both inserts silently dropped rows.

Restore the pre-#98779 semantics for `EXCHANGE`: keep source-view
edges keyed by the original name. The `MV` continues to read from
whatever now lives at the name it was created against. Plain
`RENAME TABLE` keeps the post-#98779 re-keying behavior used by
`01155_rename_move_materialized_view`.

This also subsumes the special case from #104907 — the synthetic
`CREATE OR REPLACE` exchange routes through the same
`exchange_tables=true` branch.

Tests:

* `04240_exchange_tables_drops_mv_source_dependencies` —
  regression test for #105021. Now passes; `MV` continues to feed
  `dst` after `EXCHANGE TABLES`. Reference updated to the fixed
  behavior.
* `04235_create_or_replace_source_table_view_dependencies` —
  cherry from #104907 (originally authored by @serprex). Passes
  with the unified fix.
* `04241_rename_table_mv_source_dependencies` — pins a
  pre-existing latent bug in plain same-database `RENAME TABLE`
  (silent data loss, alive-but-orphaned `MV`). Not a #98779
  regression; not fixed by this PR. Root cause shared with
  #76303; out of scope.

The chosen behavior here ("option C": edges follow the name) is a
regression repair — it matches v26.4. A future redesign to
"option A" (identity-bound, atomic `MV.as_select` rewrite, like
PostgreSQL) is tracked separately.

Closes #105021

Co-Authored-By: Philip Dubé <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e509b53]

Summary: ✅


AI Review

Summary

This PR fixes incorrect handling of source-side view_dependencies during EXCHANGE TABLES (including the synthetic exchange used by CREATE OR REPLACE TABLE and REPLACE TABLE). The change in InterpreterRenameQuery keeps source->dependent-view edges keyed by table name for exchange while still rewiring incoming dependencies for materialized views, which matches how stored select_table_id behaves and addresses the silent data-loss regression from #105021/#98779. The added regression coverage in 04235 and 04240, plus the updated 03290 reference, is consistent with the new invariant. I did not find unresolved correctness issues in the current diff.

Final Verdict
  • Status: ✅ Approve

The cross-swap of source-view dependency edges on `EXCHANGE TABLES`
introduced by #98779 (commit `6133fbc6416`) is undone by the fix
for #105021. The post-`EXCHANGE` sections of this test now produce
the option-C output (edges follow the name) that matches the
reference from before `6133fbc6416`. The `RENAME TABLE` sections
keep #98779's behavior intact.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

@george-larionov george-larionov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me.

@CheSema
CheSema enabled auto-merge May 15, 2026 16:15
@clickhouse-gh

clickhouse-gh Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.60% 76.50% -0.10%

Changed lines: 100.00% (10/10) · Uncovered code

Full report · Diff report

@CheSema
CheSema added this pull request to the merge queue May 18, 2026
Merged via the queue into master with commit 36056c0 May 18, 2026
167 checks passed
@CheSema
CheSema deleted the fix/exchange-tables-mv-source-105021 branch May 18, 2026 12:18
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 18, 2026
pfcoperez added a commit to PeerDB-io/peerdb that referenced this pull request May 25, 2026
Previous PR #4303 reduced the
scope of some e2e tests that were affected by
ClickHouse/ClickHouse#105021 regression in
ClickHouse.

This regression has since then been fixed through
ClickHouse/ClickHouse#105029

Hence, this follow-up PR reverts the workaround in the e2e tests,
restoring their original coverage.
pfcoperez added a commit to PeerDB-io/peerdb that referenced this pull request May 25, 2026
Previous PR #4303 reduced the
scope of some e2e tests that were affected by
ClickHouse/ClickHouse#105021 regression in
ClickHouse.

This regression has since then been fixed through
ClickHouse/ClickHouse#105029

Hence, this follow-up PR reverts the workaround in the e2e tests,
restoring their original coverage.

Closes: https://linear.app/clickhouse/issue/DBI-738
alexey-milovidov pushed a commit to amosbird/ClickHouse that referenced this pull request Oct 8, 2026
When the definition of a system log changes, `SystemLog::prepareTable`
renames the table to `<name>_N` and creates a new one under the old name.
Since ClickHouse#98779 a plain `RENAME TABLE` moves the source-side view-dependency
edges to the new name, but a materialized view's query still reads the log
by name (`select_table_id` is name-only and `InsertDependenciesBuilder`
rejects any other source), so the new table had no dependent views and the
view silently stopped receiving rows until the next restart rebuilt the
graph by name.

Add an internal `InterpreterRenameQuery::setKeepSourceViewDependencies`
that leaves those edges on the old name, as `EXCHANGE TABLES` already does
(ClickHouse#105029), and set it only for the system log rotation. User `RENAME TABLE`
keeps re-keying the edges.

Closes ClickHouse#124177

Co-Authored-By: Claude Opus 5.5 <[email protected]>
pfcoperez added a commit to PeerDB-io/peerdb that referenced this pull request Nov 5, 2026
Previous PR #4303 reduced the
scope of some e2e tests that were affected by
ClickHouse/ClickHouse#105021 regression in
ClickHouse.

This regression has since then been fixed through
ClickHouse/ClickHouse#105029

Hence, this follow-up PR reverts the workaround in the e2e tests,
restoring their original coverage.
pfcoperez added a commit to PeerDB-io/peerdb that referenced this pull request Nov 5, 2026
Previous PR #4303 reduced the
scope of some e2e tests that were affected by
ClickHouse/ClickHouse#105021 regression in
ClickHouse.

This regression has since then been fixed through
ClickHouse/ClickHouse#105029

Hence, this follow-up PR reverts the workaround in the e2e tests,
restoring their original coverage.

Closes: https://linear.app/clickhouse/issue/DBI-738
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EXCHANGE TABLES with a materialized-view -- regression from #98779

4 participants