Skip to content

Find bad usage of storage in-memory metadata - #106200

Merged
Michicosun merged 27 commits into
masterfrom
find_bad_usage_of_in_memory_metadata
Jun 20, 2026
Merged

Michicosun merged 27 commits into
masterfrom
find_bad_usage_of_in_memory_metadata

Conversation

@Michicosun

@Michicosun Michicosun commented May 31, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

There is no guarantee that metadata will outlive the query if the pointer is dropped early. Let's find all the places here.

Version info

  • Merged into: 26.6.1.1067

@clickhouse-gh

clickhouse-gh Bot commented May 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4a4f69a]

Summary: ❌

job_name test_name status info comment
Stress test (amd_tsan) FAIL
Hung check failed, possible deadlock found FAIL cidb, issue

AI Review

Summary

This PR replaces direct StorageMetadataPtr returns from getInMemoryMetadataPtr with a StorageMetadataHandle that prevents borrowing metadata subobjects from temporaries, then updates the affected storage/analyzer/planner call sites to keep the metadata handle or snapshot alive explicitly. I rechecked the prior bot-authored threads against the current code and did not find any remaining unresolved issue that needs a new inline comment.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 31, 2026
@Michicosun

Copy link
Copy Markdown
Member Author

The text index drops metadata when a reference to it is used.

@kssenii kssenii self-assigned this Jun 1, 2026
Comment thread src/Storages/StorageInMemoryMetadata.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

🟢 AI verdict: improvement — 2 query(s) improved out of 38 analysed

This is a broad refactor that swaps heavyweight metadata pointers for lighter handles and caches metadata within a query, touching the setup path that every query runs through. Three ClickBench queries (Q15, Q18, Q28) show consistent small improvements (-5% to -11.5%), which matches what cheaper metadata handling in query analysis/setup would produce; the execution hot paths are untouched. Q15's larger -11.5% delta is partly inflated by a recent slowdown trend on the base branch, so treat its magnitude with some caution. No regressions were flagged.

clickbench

🟢 2 improved · ⚠️ 1 inconclusive

Flagged queries (3 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
🟢 15 improvement 252 223 -11.5% <0.0001 cpu: Lighter metadata handles speed query setup; -11.5% partly reflects a recent master slowdown on this query.
🟢 28 improvement 7046 6612 -6.2% <0.0001 cpu: Tight, reproducible -6%; consistent with fewer metadata-object copies during query analysis/setup.
⚠️ 18 not_sure 1370 1300 -5.1% 0.0002 cpu: Borderline with mixed signals from the two tests; small gain fits cheaper metadata handling in setup.

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: 3b40ab02-9176-4934-aded-6050fbbc975f
  • MIRAI run: 2e59cf61-401f-4229-a60b-e68aa099c80b
  • PR check IDs:
    • clickbench_160358_1781913719
    • clickbench_160368_1781913719
    • clickbench_160376_1781913719
    • tpch_adapted_1_official_160383_1781913720
    • tpch_adapted_1_official_160395_1781913720
    • tpch_adapted_1_official_160402_1781913720

@Michicosun
Michicosun requested a review from kssenii June 16, 2026 10:01
Comment thread src/Storages/StorageInMemoryMetadata.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.20% +0.00%
Functions 92.60% 92.50% -0.10%
Branches 77.40% 77.40% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 857/1034 (82.88%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 70 line(s) · Uncovered code

Full report · Diff report

@Michicosun
Michicosun added this pull request to the merge queue Jun 20, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Jun 20, 2026
@Michicosun
Michicosun added this pull request to the merge queue Jun 20, 2026
Merged via the queue into master with commit ccd31d5 Jun 20, 2026
164 of 166 checks passed
@Michicosun
Michicosun deleted the find_bad_usage_of_in_memory_metadata branch June 20, 2026 10:28
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 20, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 14, 2026
`MergeTreeData::getInMemoryMetadataPtr` deep-copied the whole
`StorageInMemoryMetadata` on every call in debug and sanitizer builds, so
that a reference kept after its metadata pointer was dropped became a
use-after-free under ASan. With the bucketed `system.metric_log` schema
every copy clones thousands of `ALIAS` default ASTs, a large part of why
`03540_system_dashboards` is slow on the sanitizer lanes.

The copy was added together with `StorageMetadataHandle` in ClickHouse#106200, and
the handle already rejects the common misuse, a reference taken through a
temporary handle, at compile time. Removing the copy is simpler than
making it cheaper, so this drops it and reverts the AST-sharing copies of
the previous two commits, as asked in review.

Dashboards probe (debug, 166 dashboards x 3 variants, sum of query
durations, min of 2, same base):
- bucketed: base 24.57 s, AST sharing 21.50 s, copy removed 20.06 s
- wide:     base 13.36 s, AST sharing 13.23 s, copy removed 11.99 s

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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.

3 participants