Skip to content

Do not take the parts snapshot of a materialized view's source table during analysis - #126589

Open
tuanpach wants to merge 4 commits into
ClickHouse:masterfrom
tuanpach:fix-mv-view-source-snapshot
Open

tuanpach wants to merge 4 commits into
ClickHouse:masterfrom
tuanpach:fix-mv-view-source-snapshot

Conversation

@tuanpach

@tuanpach tuanpach commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

When a block is pushed to a materialized view, the analyzer resolves the view's source table from the catalog, so engine checks (FINAL, SAMPLE, _shard_num) see the real table, and takes the column types from the inserted block. It also called getStorageSnapshot on the real table. replaceStorageInQueryTree then replaces that table with the block before anything reads it, so the parts in that snapshot were never used.

Building them is not free. It walks all parts of the table once per view per inserted block. For a MergeTree table used inside a transaction it is worse: it copies all Active and Outdated parts and checks the visibility of each. While a long transaction keeps Outdated parts from being removed, that cost grows with every insert. In a load test with three materialized views and a transaction held open for 30 minutes, it became the largest growing part of INSERT CPU.

This takes getStorageSnapshotWithoutData for the view source instead. The metadata is the same; only the parts list is left out. Analysis reads only the metadata, and every place that reads the table (the view query, scalar subqueries, IN) swaps in the block first. The old analyzer already uses getStorageSnapshotWithoutData when it only analyzes.

The new test checks that materialized views keep reading the inserted block (plain, IN, scalar subquery, CTE, join), and that an ordinary view and the view table function inside a materialized view keep reading the table, with and without implicit_transaction.

Related: #123514

Changelog category (leave one):

  • Performance Improvement

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

Inserting into a table with materialized views no longer builds an unused list of the source table's parts for every view and block. This was expensive for MergeTree tables inside a transaction while old parts are kept.

🤖 Generated with Claude Code


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

…during analysis

When a block is pushed to a materialized view, the analyzer resolves the source table from
the catalog (for engine checks) and takes the types from the block, but called
`getStorageSnapshot` on the real table. `replaceStorageInQueryTree` then replaces the table
with the block before anything reads it, so the snapshot's parts were never used.

Building them is not free: it walks all parts of the table, and for a `MergeTree` table with
a transaction it copies all Active and Outdated parts and checks the visibility of each,
once per view per inserted block.

Take `getStorageSnapshotWithoutData` for the view source instead: analysis reads only the
metadata, and every place that reads the table (the query itself, scalar subqueries, `IN`)
swaps in the block first. The test checks that materialized views keep reading the block
(plain, `IN`, scalar subquery, CTE, join), and ordinary views and the `view` table function
keep reading the table, with and without a transaction.

Related: 97ad587
@clickhouse-gh

clickhouse-gh Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5b2ce37]

Summary: ✅


AI Review

Summary

This PR uses getStorageSnapshotWithoutData when analyzing a materialized view's source table, avoiding collection of parts that execution replaces with the inserted block. ⚠️ Request changes: the existing finding about missing evidence for the claimed performance improvement remains unresolved.

Findings

⚠️ Majors

  • src/Analyzer/Resolve/IdentifierResolver.cpp:382: The performance claim remains unverified. The new stateless test checks results and would also pass with getStorageSnapshot restored; its short implicit transaction does not exercise retained Outdated parts. The PR describes a hotspot but supplies no reproducible workload or before/after measurements. The cloud benchmark comment covers different workloads and explicitly disclaims attribution to this PR. Existing inline finding remains open; no duplicate comment was posted.
Tests
  • ⚠️ Add a focused performance test or publish reproducible before/after insert CPU or latency measurements with multiple materialized views, repeated inserted blocks, and a long-lived transaction retaining parts. Report active/outdated part counts, block counts, view counts, and enable_shared_storage_snapshot_in_query. The current Fast test result confirms 05407_mv_source_table_references passes, but provides no before/after performance comparison.
Final Verdict

Status: ⚠️ Request changes. The minimum remaining action is to substantiate the claimed performance benefit with the focused evidence above.

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). labels Oct 18, 2026
auto storage_snapshot = storage->getStorageSnapshot(metadata_snapshot, context);
/// The view source is read instead of the table, so the table's parts are not needed.
auto storage_snapshot = is_view_source
? storage->getStorageSnapshotWithoutData(metadata_snapshot, context)

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.

⚠️ Major: provide evidence for the claimed insert-performance improvement

The new stateless test verifies result semantics, but it neither measures snapshot cost nor exercises a long-lived transaction retaining Outdated parts. It would also pass with the original getStorageSnapshot call restored. The description explains the suspected hotspot, but provides no reproducible workload or before/after measurements establishing the benefit of this change.

Please add a focused performance test or publish a reproducible before/after comparison of insert CPU or latency with several materialized views and retained parts. Include the part/block counts and enable_shared_storage_snapshot_in_query setting, since snapshot sharing affects how often the old path actually builds a snapshot.

@clickhouse-gh

clickhouse-gh Bot commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

Baseline selected using StressHouse version hint 26.11.1.42530; measured build ancestry could not be verified. Measured differences are not attributed to this PR. ⚙️ Master level shift inside the baseline window (~2026-10-18 06:14:51): baseline for 1 query restricted to the checks matching the selected baseline era.

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

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 2590c9dc-146c-46a7-af33-96f01712b934
  • MIRAI run: 0584bdcc-a779-4950-a98d-8c2862c086da
  • PR check IDs:
    • clickbench_447851_1792336645
    • clickbench_447872_1792336645
    • clickbench_447878_1792336645
    • tpch_adapted_1_official_447892_1792336645
    • tpch_adapted_1_official_447936_1792336645
    • tpch_adapted_1_official_447956_1792336645

@clickhouse-gh

clickhouse-gh Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 5b2ce376f with master 31d10e5ef (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 915c5e7f3; 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.

Compile time of recompiled translation units

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

Job report

@tuanpach

Copy link
Copy Markdown
Member Author

@groeneai Check the CI failure

@groeneai

Copy link
Copy Markdown
Collaborator

The only failing job on 77b78af is Upgrade check (amd_release). The PR did not cause it.

Current runs use v26.9.24.8-stable as the previous release, and STID 5726-4458 has not appeared in any Upgrade check (amd_release) run since 26.9.23.15 was published. A fresh CI run of this PR should pass this job.

@clickhouse-gh clickhouse-gh Bot added comp-materialized-view Materialized views: insert-triggered MV pipeline, insert dependencies, deduplication. and removed comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). labels Nov 19, 2026

This branch has not been deployed

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

Labels

comp-materialized-view Materialized views: insert-triggered MV pipeline, insert dependencies, deduplication. pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants