Skip to content

Fix slow planning of queries with skip indexes on tables with pending patch parts - #124931

Merged
CurtizJ merged 6 commits into
ClickHouse:masterfrom
CurtizJ:text-index-direct-read-per-part-patches
Oct 10, 2026
Merged

CurtizJ merged 6 commits into
ClickHouse:masterfrom
CurtizJ:text-index-direct-read-per-part-patches

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Related: #99543
Related: #118496
Related: #123334
Related: #124085

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):

Fixed slow planning of queries with skip indexes on tables with many parts and pending patch parts of lightweight updates.


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

Version info

  • Merged into: 26.10.1.2081-master (included in 26.10 and later)
  • Backported to: 26.9.16.10, 26.8.24.9, 26.7.27.4

CurtizJ and others added 5 commits October 9, 2026 13:17
… skip index

`collectTextIndexReadInfos` called `getAlterConversionsForPart` for every part of the table to collect
the columns updated on the fly (ClickHouse#99543) and to find whether
any part has a pending patch (ClickHouse#118496). It ran for every
query with any useful skip index, before checking whether one of them is a text index, over all parts of
the table (they are not filtered by the index analysis yet), and the lookup of patches for a part is
linear in the number of patch parts and builds the sorting key of every patch part. On tables with
hundreds of parts and patch parts it added hundreds of milliseconds to the planning of point queries
that use a `bloom_filter` index. Also, a single pending patch anywhere in the table turned the direct
read from a text index off for the whole query.

Now:
- `collectTextIndexReadInfos` returns early if no useful index is a text index.
- The planner rejects a text index whose columns are changed on the fly without building
  `AlterConversions` for every part: the columns updated by patch parts and data mutations are taken
  from the whole mutations snapshot, the columns changed by alter mutations are collected per part only
  when such mutations exist, and masking policies are parsed once. This check must stay in planning:
  the on-fly mutation steps are built from the columns of the query, which do not contain the indexed
  columns replaced by the virtual columns, and whether the preprocessor of the index is applied to the
  search functions must not depend on the part.
- A part with pending patches (of other columns) does not read the virtual columns from the index: like
  a part without the materialized index, it computes them by their default expressions (the original
  predicates), because the patch key columns must be read in the first read step, which the reader of the
  text index cannot do. The other parts read the index.

Related: ClickHouse#123334
Related: ClickHouse#124085
Related: ClickHouse#118496
Related: ClickHouse#99543
Related: ClickHouse/clickhouse-private#75816
Related: https://clickhouse-inc.slack.com/archives/C0B6DBVRPKJ/p1791474075046099

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…n skip index analysis

`filterPartsByPrimaryKeyAndSkipIndexes` called `getAlterConversionsForPart` for every part when the
query has useful skip indexes, also for the parts in which the primary key left no ranges, where skip
indexes cannot filter anything. The lookup of patches for a part is linear in the number of patch
parts, so on tables with many parts and patch parts (and no partition pruning) it added tens of
milliseconds to the planning of point queries (e.g. 2000 parts and 50 patch parts in the performance
test `skip_index_planning_with_patch_parts`).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`refreshStatistics` built the cached `ConditionSelectivityEstimator` from all active parts, including
patch parts, but queries never read patch parts directly, so `isStale` always failed while a table had
pending patch parts, and every query loaded the statistics of all its parts (taking the parts lock for
each part). With 2000 parts and 50 patch parts it took about 80 ms of the planning of a point query.

The performance test `skip_index_planning_with_patch_parts` refreshes the cached statistics every second,
so that they are up to date after the fill.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`IMutationsSnapshot::getAllUpdatedColumns` collected only the columns of `UPDATE` commands, so the planner
collected the columns changed by alter mutations (`MODIFY COLUMN`) for each part by itself. Now the snapshot
collects the columns of all commands applied on the fly with the same rules as `AlterConversions`. The method
is renamed to `getColumnsChangedOnFly`, so that other implementations fail to compile instead of silently
missing alter mutations.

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

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c988f9e]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_asan_ubsan, db disk, distributed plan, targeted) FAIL
03100_lwu_deletes_2 FAIL cidb IGNORED
Stateless tests (arm_binary, ParallelReplicas, s3 storage, parallel) FAIL
04651_nullable_tuple_lowcardinality_subcolumn_shared_cache FAIL cidb IGNORED
02151_hash_table_sizes_stats_joins_reordered NOT_FAILED cidb IGNORED
03273_group_by_in_order_still_used_when_group_by_key_doesnt_match_order_by_key NOT_FAILED cidb IGNORED
02884_create_view_with_sql_security_option NOT_FAILED cidb IGNORED
Stateless tests (arm_asan_ubsan, azure, sequential) ERROR IGNORED

AI Review

Summary

This PR removes the O(parts × patch-parts) planning work from skip-index queries and adds a reader-side fallback so patched parts can still execute text-search predicates without turning the optimization off everywhere. That part looks consistent, but one precision regression is still present: once a ReadFromMergeTree already carries an analysis result, the new whole-snapshot getColumnsChangedOnFly() guard can still disable direct text-index reads for clean parts because of mutations on unrelated partitions.

Findings
  • ⚠️ Majors
    • [src/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp:197] [dismissed by author -- https://github.com/Fix slow planning of queries with skip indexes on tables with pending patch parts #124931#discussion_r4231476231] getColumnsChangedOnFly() now takes changed columns from the whole snapshot, while ReadFromMergeTree::getParts() becomes precise as soon as an earlier optimization has already attached an analysis result. In those plans, a pending patch or mutation on an unrelated partition still lands in updated_columns, canUseIndex() rejects the text index, and direct read is turned off for the clean parts the query actually reads. That regresses the old per-part behavior on already-pruned reads, which is exactly where this PR’s new per-part fallback is supposed to keep the fast path alive.
      Suggested fix: keep the whole-snapshot fast path only while the read has no attached analysis result; once the read is already pruned, derive the changed-column set from getParts() (or at least from those parts’ partitions) so unrelated pending work cannot globally disable direct text-index reads.
Tests
  • ⚠️ The new stateless test covers the unpruned path, but there is still no focused coverage for a read that already has an attached analysis result plus a pending patch or mutation in an unrelated partition. A small regression test for that plan shape would close the gap.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: make the planner-side updated_columns guard respect the already-pruned part set when a ReadFromMergeTree already has an analysis result, or demonstrate why such pruned plans can never reach this pass.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 9, 2026
Comment thread src/Storages/MergeTree/MergeTreeData.h Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 9, 2026
…_patches

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

clickhouse-gh Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

No query shows a performance change that can be traced to this PR. The only flagged row is clickbench Q31 at -12.1% (297 ms to 261 ms), and it was correctly marked no_change: 261 ms is within master's recent range of 241-357 ms, and the baseline for Q30/Q31 had to be re-fit because master itself shifted on 2026-10-09. The PR changes text-index direct reads, how columns changed by mutations and patches are collected, and skip-index filtering for parts already dropped by the primary key. ClickBench's hits table has no skip indexes and no pending patches, so none of that code runs there, and the small CPU drop is not enough to credit the PR. The planning speedup this PR targets should show up in the new skip_index_planning_with_patch_parts performance test, which neither of these benchmarks covers.

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 141bd7d7-5bb6-416c-aa63-58671716e680
  • MIRAI run: b8a45fe3-12fa-4514-ba7c-7a90d17045ec
  • PR check IDs:
    • clickbench_644236_1791596194
    • clickbench_644242_1791596194
    • clickbench_644248_1791596194
    • tpch_adapted_1_official_644258_1791596194
    • tpch_adapted_1_official_644270_1791596194
    • tpch_adapted_1_official_644307_1791596194

@CurtizJ CurtizJ self-assigned this Oct 10, 2026
@CurtizJ

CurtizJ commented Oct 10, 2026 •

Copy link
Copy Markdown
Member Author

Perf tests:
Screenshot 2026-10-10 at 23 37 49

@CurtizJ

CurtizJ commented Oct 10, 2026 •

Copy link
Copy Markdown
Member Author

Failed tests are unrelated.
03100_lwu_deletes_2 is fixed in #116920.
04651_nullable_tuple_lowcardinality_subcolumn_shared_cache is fixed in #124956.

@CurtizJ
CurtizJ added this pull request to the merge queue Oct 10, 2026
@CurtizJ CurtizJ added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 10, 2026
Merged via the queue into ClickHouse:master with commit 3740ea9 Oct 10, 2026
159 of 163 checks passed
@CurtizJ
CurtizJ deleted the text-index-direct-read-per-part-patches branch October 10, 2026 23:06
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 10, 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 Oct 10, 2026
CurtizJ added a commit that referenced this pull request Oct 11, 2026
Resolve conflicts of the cherry-pick of #124931 to 26.7:
- 26.7 has no `has_patched_parts` gate (#118496 is not
  backported), so only the per-part loop over `getAlterConversionsForPart` in `collectTextIndexReadInfos`
  is replaced by `getColumnsUpdatedOnFly`.
- `MergeTreeReadTaskInfo` has no `data_part_info` on 26.7, so `getIndexReadTaskForReadStep` takes the
  part and its `AlterConversions` separately.
- `getMaskingPolicyCommands` is placed before the 26.7 version of `getPatchPartMetadata`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
robot-ch-test-poll added a commit that referenced this pull request Oct 11, 2026
Cherry pick #124931 to 26.7: Fix slow planning of queries with skip indexes on tables with pending patch parts
robot-clickhouse added a commit that referenced this pull request Oct 11, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 11, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 11, 2026
Backport #124931 to 26.9: Fix slow planning of queries with skip indexes on tables with pending patch parts
clickhouse-gh Bot added a commit that referenced this pull request Oct 11, 2026
Backport #124931 to 26.8: Fix slow planning of queries with skip indexes on tables with pending patch parts
CurtizJ added a commit that referenced this pull request Oct 11, 2026
Backport #124931 to 26.7: Fix slow planning of queries with skip indexes on tables with pending patch parts
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 12, 2026
… skip indexes on tables with pending patch parts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default 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.

3 participants