Skip to content

Make cascading materialized views see the row being inserted - #105943

Merged
ahmadov merged 8 commits into
masterfrom
ahmadov/cascade-mv-dependent
Jun 8, 2026
Merged

ahmadov merged 8 commits into
masterfrom
ahmadov/cascade-mv-dependent

Conversation

@ahmadov

@ahmadov ahmadov commented May 27, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

A materialized view whose inner SELECT reads the source storage via INNER JOIN back to the source can miss the row currently being inserted: MergeTreeSink::consume and ReplicatedMergeTreeSink::consume defer the part commit via the delayed_chunk pattern, but the MV cascade acquires its StorageSnapshot before that deferred commit runs.

The new setting wait_for_part_commit_in_dependent_materialized_views (default false) makes the sink finish the delayed chunk inside consume whenever the destination has at least one dependent view, committing the part before yielding to the cascade. When disabled, the existing parallel-write overlap is preserved.

Version info

  • Merged into: 26.6.1.518

A materialized view whose inner SELECT reads the source storage via
`INNER JOIN` back to the source can miss the row currently being
inserted: `MergeTreeSink::consume` and `ReplicatedMergeTreeSink::consume`
defer the part commit via the `delayed_chunk` pattern, but the MV
cascade acquires its `StorageSnapshot` before that deferred commit runs.

The new setting `wait_for_part_commit_in_dependent_materialized_views`
(default `false`) makes the sink finish the delayed chunk inside
`consume` whenever the destination has at least one dependent view,
committing the part before yielding to the cascade. When disabled, the
existing parallel-write overlap is preserved.
@ahmadov
ahmadov requested a review from CheSema May 27, 2026 13:19
@clickhouse-gh

clickhouse-gh Bot commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [121b7f9]

Summary: ✅


AI Review

Summary

This PR adds wait_for_part_commit_in_dependent_materialized_views so MergeTreeSink and ReplicatedMergeTreeSink can commit the just-written part before dependent materialized view processing reads back from the source via JOIN. The implementation path is coherent, but the non-replicated MergeTree branch still lacks focused regression evidence.

Missing context / blind spots
  • ⚠️ Full PR CI was still pending during review: Fast test, Build (arm_tidy), and the aggregate PR status had not completed. Passing those checks would close the CI evidence gap.
Tests
  • ⚠️ [src/Storages/MergeTree/MergeTreeSink.cpp:232] The change explicitly modifies both MergeTreeSink and ReplicatedMergeTreeSink, but the added stateless regression covers only ReplicatedMergeTree. Please add the same cascading materialized view pattern for non-replicated MergeTree with wait_for_part_commit_in_dependent_materialized_views = 1, so the MergeTreeSink branch is proven too.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: add focused non-replicated MergeTree coverage for the new setting; then let the pending PR checks complete.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label May 27, 2026
@antaljanosbenjamin antaljanosbenjamin self-assigned this May 27, 2026
finishDelayedChunk();
delayed_chunk = std::make_unique<MergeTreeDelayedChunk>();
delayed_chunk->partitions = std::move(partitions);
if (synchronously_commit_part_for_dependent_views)

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.

The fix updates both MergeTreeSink::consume and ReplicatedMergeTreeSink::consume, but the new regression test covers only ReplicatedMergeTree.

Can you add a focused MergeTree variant of this cascade (INNER JOIN back to the source with wait_for_part_commit_in_dependent_materialized_views = 1) to prove the non-replicated path is protected too? Right now we have no test evidence for this branch.

Comment on lines +220 to +230
{
delayed_chunk = std::make_unique<MergeTreeDelayedChunk>();
delayed_chunk->partitions = std::move(partitions);
finishDelayedChunk();
}
else
{
finishDelayedChunk();
delayed_chunk = std::make_unique<MergeTreeDelayedChunk>();
delayed_chunk->partitions = std::move(partitions);
}

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.

What happens if materialized_views_ignore_errors = 0, wait_for_part_commit_in_dependent_materialized_views = 1. Wouldn't the data be committed to the table in case an exception is thrown in the depedant view? I think we should think about what is the correct way to handle this.

@ahmadov ahmadov May 27, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What about just documenting the behavior properly and let the user to decide to enable the setting since it's disabled by default?

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.

materialized_views_ignore_errors=0 -- ignores the errors in MV's processors (select and sinks)
The data might either be commited in the dest table in that case or not. We do not provide guarantee here. We encourage users do reties, they will commit missed data.

wait_for_part_commit_in_dependent_materialized_views=1 adds prefix guarantees indeed.

I do not see the issue here. Or I do not understand the case.

@CheSema CheSema May 27, 2026 •

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.

No still wait_for_part_commit_in_dependent_materialized_views=1 won't provide prefix commit guarantees.
If your insert several blocks they are handled by different instances of sync processors which work in parallel. Each parallel branch would have its own prefix. All together they would not have it.

prefix commit guarantees is only reachable here when insert works in one thread.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No still wait_for_part_commit_in_dependent_materialized_views=1 won't provide prefix commit guarantees. If you inserts several blocks they are handled by different instances of sync processors in parallel. Each branch would have its own prefix. prefix commit guarantees is only reachable here when insert works in one thread.

what solution would you recommend? Should we force to a single thread insert when the setting is enabled or just document the limitation?

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.

The data might either be commited in the dest table in that case or not.

Based on our docs (which might be misunderstood by me) if we have an insert chain insert -> table -> mv -> mv_table and materialized_views_ignore_errors = 0, then if the insertion into mv_table fails, the block won't be inserted into table neither. With this change I think this behavior is changed.

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.

That ineresting that you have such impression from the doc.
Could you please point to the exact part there?

I see there

Allows to ignore errors for MATERIALIZED VIEW, and deliver original block to the table regardless of MVs

And it says nothing about what would be inserted in target table in case of an error in MV part.

@antaljanosbenjamin antaljanosbenjamin May 28, 2026 •

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.

"Allows" suggests that without that it is "prohibited", so that makes the impression for me that if ignore_errors = 0 the original block won't be delivered.

If it would "ensure" that the block is delivered, then I would say it doesn't say anything about what happens if the errors are not ignored.

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.

I see, we better to alter that description then.

@CheSema CheSema May 28, 2026 •

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.

More symboles in the description than it was, but it describes it with complete context.

#106044

, context(context_)
, storage_snapshot(storage.getStorageSnapshotWithoutData(metadata_snapshot, context_))
, deduplicate((*storage.getSettings())[MergeTreeSetting::non_replicated_deduplication_window] > 0 && storage.getDeduplicationLog() != nullptr)
, synchronously_commit_part_for_dependent_views(

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.

The same changes needs to be done in private for SMT, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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 make sense to make private part of that changes in the sync PR. I usually do it in that way.

pull Bot pushed a commit to admariner/ClickHouse that referenced this pull request May 28, 2026
The previous one-line description ("Allows to ignore errors for MATERIALIZED
VIEW, and deliver original block to the table regardless of MVs") was read
as implying that with the setting disabled the original block is not
delivered to the source table when a dependent view throws. That is not
what the setting does: it controls only how MV pipeline errors are reported
and provides no atomicity guarantee against the source table either way.

Discussed in ClickHouse#105943 (comment)

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

OK from me, but I do not see the private part in sync PR

@ahmadov

ahmadov commented May 31, 2026

Copy link
Copy Markdown
Member Author

OK from me, but I do not see the private part in sync PR

it should be done now.

@clickhouse-gh

clickhouse-gh Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This PR adds a new opt-in setting (wait_for_part_commit_in_dependent_materialized_views, default off) that only changes INSERT behavior for tables with dependent materialized views; it touches sink-side part-commit plumbing, not the query-execution path. The two flagged ClickBench improvements (Q4 -12.2%, Q15 -16.7%) are read-only SELECTs that exercise none of this code, and with the setting disabled by default the new branch never runs. Both apparent gains are run-to-run variance rather than a real effect of this change, so both have been downgraded to not-sure. No action needed on these queries.

clickbench

⚠️ 2 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline med (ms) PR med (ms) Change q-value Hint
⚠️ 4 not_sure 267 234 -12.2% <0.0001 PR only adds an INSERT-path MV-commit setting (default off); cannot touch this read-only SELECT
⚠️ 15 not_sure 251 209 -16.7% <0.0001 Insert-side MV cascade plumbing, gated off by default; unrelated to this SELECT's hot path

q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 63a94d45-73bd-4de2-b6dc-5711f78e0bd7
  • MIRAI run: d3db6231-bd51-4b40-a8df-159339248037
  • PR check IDs:
    • clickbench_11892_1780921832
    • clickbench_11905_1780921833
    • clickbench_11911_1780921833
    • tpch_adapted_1_official_11917_1780921833
    • tpch_adapted_1_official_11929_1780921833
    • tpch_adapted_1_official_11993_1780921833

@clickhouse-gh

clickhouse-gh Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.50% 84.50% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.20% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 51/58 (87.93%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 1 line(s) · Uncovered code

Full report · Diff report

@ahmadov
ahmadov added this pull request to the merge queue Jun 8, 2026
Merged via the queue into master with commit 744e32c Jun 8, 2026
166 checks passed
@ahmadov
ahmadov deleted the ahmadov/cascade-mv-dependent branch June 8, 2026 20:15
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 8, 2026
@ahmadov ahmadov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jun 17, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jun 17, 2026
robot-ch-test-poll added a commit that referenced this pull request Jun 17, 2026
Cherry pick #105943 to 26.3: Make cascading materialized views see the row being inserted
robot-clickhouse added a commit that referenced this pull request Jun 17, 2026
robot-ch-test-poll added a commit that referenced this pull request Jun 17, 2026
Cherry pick #105943 to 26.4: Make cascading materialized views see the row being inserted
robot-clickhouse added a commit that referenced this pull request Jun 17, 2026
robot-ch-test-poll added a commit that referenced this pull request Jun 17, 2026
Cherry pick #105943 to 26.5: Make cascading materialized views see the row being inserted
robot-clickhouse added a commit that referenced this pull request Jun 17, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jun 17, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

6 participants