Skip to content

Add incremental rmv core - #114152

Merged
SmitaRKulkarni merged 29 commits into
masterfrom
Add_incremental_rmv_core
Sep 14, 2026
Merged

SmitaRKulkarni merged 29 commits into
masterfrom
Add_incremental_rmv_core

Conversation

@SmitaRKulkarni

@SmitaRKulkarni SmitaRKulkarni commented Aug 10, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • New Feature

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

Refreshable materialized views can now refresh incrementally with REFRESH ... APPEND INCREMENTAL, appending only the rows committed to the source table since the previous refresh instead of re-reading the whole source.

depends on #111794

cc @alesapin @Michicosun


Workflow [PR]
Sync PR [sync-upstream/pr/114152]

@SmitaRKulkarni
SmitaRKulkarni marked this pull request as draft August 10, 2026 12:46
@clickhouse-gh

clickhouse-gh Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4be8cda]

Summary: ✅


AI Review

Summary

This PR adds REFRESH ... APPEND INCREMENTAL for refreshable materialized views by persisting a per-partition streaming cursor and reusing it on the next refresh. The happy path and the coordinated-restart path are covered, but two correctness holes remain in the current implementation: uncoordinated views still lose their cursor on ATTACH/restart and replay historical data, and the persisted cursor is never invalidated when the source table instance changes under the same name.

Findings

❌ Blockers

  • [src/Storages/MaterializedView/RefreshTask.cpp:1273] znode.cursor = cursor_after_refresh only updates in-memory state for uncoordinated views. The cursor is serialized only through updateCoordinationState when coordination.coordinated is true, while the uncoordinated constructor path rebuilds coordination.root_znode from scratch on ATTACH/restart. An Atomic APPEND INCREMENTAL view therefore replays the whole source after a server restart or manual DETACH/ATTACH, duplicating previously processed rows.
    Suggested fix: either persist the cursor for local views as well, or reject APPEND INCREMENTAL unless the view is coordinated.
  • [src/Storages/MaterializedView/RefreshTask.cpp:1323] The persisted cursor is reused without any stable source identity. If the source table is dropped and recreated under the same name, prepareRefresh resolves the new table by name but execution.znode.cursor still points into the old table's _block_number space. classifyPartitions then treats the recreated table as already consumed and silently skips its early blocks until the new table catches up past the stale cursor.
    Suggested fix: persist the resolved source UUID together with the cursor and clear or reject the cursor whenever that UUID changes.

⚠️ Majors

  • [src/Parsers/ParserCreateQuery.cpp:3284] The user docs still describe APPEND INCREMENTAL as if it always processes "only the rows committed since the previous refresh", but 04812_incremental_refreshable_mv_at_least_once.sql now codifies a different failure contract: if a refresh throws after the append and before the cursor is persisted, the next refresh replays that batch and duplicates rows. That at-least-once behavior is material and should be documented next to the syntax change.
    Suggested fix: add one sentence here that the cursor is advanced only after the append succeeds, so failed refreshes can replay and duplicate the last batch.
Tests
  • ⚠️ The new coverage exercises normal incremental refreshes, validation, at-least-once replay, and coordinated restart, but there is still no regression test for restarting an uncoordinated incremental view or for recreating the source table under the same name.
Final Verdict

Changes requested before merge.

LLVM Coverage Report

Measured on commit 4be8cda.

Metric Baseline Current Δ
Lines 89.00% 89.00% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.50% 81.40% -0.10%

Changed lines: Changed C/C++ lines covered: 299/311 (96.14%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Aug 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4be8cda0f with master 1ada34f52 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 4e8f2df43; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

451 object files changed (+98.54 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/StorageMaterializedView.cpp.o 613.36 KiB 645.50 KiB +32.14 KiB (+5.24%)
src/CMakeFiles/dbms.dir/Analyzer/ValidationUtils.cpp.o 157.19 KiB 184.33 KiB +27.14 KiB (+17.27%)
src/CMakeFiles/dbms.dir/Storages/MaterializedView/RefreshTask.cpp.o 536.00 KiB 554.30 KiB +18.30 KiB (+3.41%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

1742 translation units recompiled, 10036 s compile time in total, 1742 of them have a recent master baseline.

Job report

@SmitaRKulkarni SmitaRKulkarni changed the title [WIP]Add incremental rmv core Add incremental rmv core Aug 13, 2026
@SmitaRKulkarni
SmitaRKulkarni marked this pull request as ready for review August 13, 2026 20:59
@Michicosun Michicosun self-assigned this Aug 21, 2026
Comment thread src/Storages/StorageMaterializedView.h Resolved
refresh_context->setSetting("enable_parallel_replicas", Field(UInt64{0}));
refresh_context->setSetting("parallel_replicas_for_non_replicated_merge_tree", Field(UInt64{0}));

stream_cursor = execution.znode.cursor;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

execution.znode.cursor is reused here without checking whether it still belongs to the same source table instance. Refreshable materialized views intentionally do not keep a source-table dependency, so DROP TABLE src; CREATE TABLE src ... (or any other source replacement that preserves the name) is allowed. The new MergeTree starts its _block_number sequence from scratch, but the old cursor survives in the RMV znode, so the next incremental refresh will treat the recreated source as already consumed and silently skip its rows.

Please persist a stable source identity (for example the resolved source UUID) alongside the cursor and clear or reject the cursor when that identity changes.

Comment thread src/Interpreters/Context.h Resolved
Comment thread src/Storages/StorageMaterializedView.h Resolved
Comment thread src/Interpreters/Context.h Resolved
Comment thread src/Parsers/ParserRefreshStrategy.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-refreshable-materialized-view Refreshable materialized views: scheduled/periodic refresh, refresh state machine, refresh orches... label Sep 4, 2026

@Michicosun Michicosun 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.

LGTM

Comment thread src/Common/QueryFuzzer.cpp Resolved
Comment thread src/Parsers/ParserRefreshStrategy.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 39 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: db9235df-1faf-492d-91e8-e52fc9bde20a
  • MIRAI run: 58a7daef-7a18-405d-a204-1684c208dfc4
  • PR check IDs:
    • clickbench_79275_1789347146
    • clickbench_79390_1789347218
    • clickbench_79503_1789347239
    • tpch_adapted_1_official_79607_1789347290
    • tpch_adapted_1_official_79720_1789347303
    • tpch_adapted_1_official_79865_1789347385

Comment thread src/Parsers/ParserCreateQuery.cpp Resolved
@SmitaRKulkarni
SmitaRKulkarni added this pull request to the merge queue Sep 14, 2026
Merged via the queue into master with commit 807c5f9 Sep 14, 2026
176 checks passed
@SmitaRKulkarni
SmitaRKulkarni deleted the Add_incremental_rmv_core branch September 14, 2026 08:41
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 14, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@SmitaRKulkarni @Michicosun — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-refreshable-materialized-view Refreshable materialized views: scheduled/periodic refresh, refresh state machine, refresh orches... pr-feature Pull request with new product feature 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.

4 participants