Skip to content

Slow down use_skip_indexes_on_data_read = 1 - #106918

Closed
shankar-iyer wants to merge 3 commits into
ClickHouse:masterfrom
shankar-iyer:skip_index_on_data_read_scheduling
Closed

shankar-iyer wants to merge 3 commits into
ClickHouse:masterfrom
shankar-iyer:skip_index_on_data_read_scheduling

Conversation

@shankar-iyer

Copy link
Copy Markdown
Member

Change : With use_skip_indexes_on_data_read = 1, evaluate skip indexes before instantiating MergeTree readers so that fully filtered parts do not load column marks (or prefetch column data), while keeping skip index checks parallel across read threads.

Note that the essential identity of use_skip_indexes_on_data_read = 1 is still maintained. Data read of a selected part will begin immediately when it's skip index filtering is complete.

Currently, take a case where PK filtering has resulted in 1000 parts and skip index filtering is deferred to read time with use_skip_indexes_on_data_read = 1. The column readers of the 1000 parts will immediately start reading their marks etc and cause significant load on the thread pool / storage. Skip Index filtering may finally find only 20 parts to be read.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

… read.

Evaluate skip indexes before creating MergeTree readers so fully filtered parts do not load column marks, while keeping checks parallel across read threads.

Co-authored-by: Cursor <[email protected]>
@shankar-iyer
shankar-iyer marked this pull request as draft June 10, 2026 04:31
@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [35bc325]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug, targeted) FAIL
Logical error: Column identifier A is already registered (STID: 4697-4326) FAIL cidb
Stateless tests (arm_asan_ubsan, azure, parallel) FAIL
Server died FAIL cidb
04202_filelog_attach_path_outside_user_files UNKNOWN cidb
04033_tpc_ds_q04 UNKNOWN cidb
03155_datasketches_ubsan UNKNOWN cidb
02125_lz4_compression_bug_TSV UNKNOWN cidb
00956_sensitive_data_masking UNKNOWN cidb
OOM in dmesg FAIL cidb

AI Review

Summary

This PR moves use_skip_indexes_on_data_read filtering before MergeTree reader creation so fully pruned parts avoid loading marks and prefetching column data. The core direction is sound, but the current implementation still breaks important scheduling guarantees in the prefetched and parallel-replica read pools, so I would not merge it as-is.

Missing context / blind spots
  • ⚠️ Full CI was still in progress when reviewed (Fast test and Build (arm_tidy) were not complete). The completed Praktika report showed no failed tests.
Findings

⚠️ Majors

  • [src/Storages/MergeTree/MergeTreePrefetchedReadPool.cpp:228] MergeTreePrefetchedReadPool no longer schedules look-ahead prefetches. preparePrefetchedReadersIfNeeded only starts a prefetch for the task currently being returned, and createTask immediately waits on that same future via readers_future->get. After removing startPrefetches / prefetch_queue, task N+1 no longer prefetches while task N is being read, so remote reads can degrade to synchronous per-task prefetching. Restore ahead-of-consumption scheduling for tasks that survive skip-index pruning and budget acquisition.
  • [src/Storages/MergeTree/MergeTreeReadPoolParallelReplicas.cpp:139] Passing index_build_context into MergeTreeReadPoolParallelReplicas makes createTask run shouldSkipPartRead, but getTask still holds mutex through return createTask(...). partHasSelectedGranules can run skip/projection index reads via getOrBuildIndexReadResult, serializing that work across all parallel-replica reader threads. The same pattern exists in MergeTreeReadPoolParallelReplicasInOrder. Split range selection from task construction and call createTask after releasing the mutex in both pools.
Tests
  • ⚠️ There is no focused benchmark or regression test proving that MergeTreePrefetchedReadPool still overlaps prefetches after skip-index pruning. A remote-disk workload with many fully pruned early tasks and surviving later tasks would close this gap.
  • ⚠️ There is no focused parallel-replica coverage showing that use_skip_indexes_on_data_read index checks remain parallel after being moved into createTask. A small parallel-replica benchmark or instrumentation test around concurrent getTask calls would close this gap.
Final Verdict

Status: ⚠️ Request changes

Minimum required actions: restore look-ahead prefetch scheduling for surviving prefetched-pool tasks, move skip-index task construction outside the parallel-replica pool mutexes, and add focused evidence for those scheduling paths.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jun 10, 2026
Prepare prefetched readers only after task selection so skip-index checks stay parallel across read threads.

Co-authored-by: Cursor <[email protected]>
…runing.

Reserve prefetch memory and reader slots only for tasks that survive skip-index filtering, so fully pruned parts do not block later remote prefetches.

Co-authored-by: Cursor <[email protected]>
if (!thread_task)
return nullptr;

preparePrefetchedReadersIfNeeded(*thread_task);

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.

preparePrefetchedReadersIfNeeded now runs only for the task that getTask is about to return, and createTask immediately consumes that same future through readers_future->get. After removing startPrefetches and prefetch_queue, later tasks no longer have their readers and prefetchBeginOfRange queued while the current task is being read; each task starts its prefetch only when selected and then waits for it before returning. That breaks the prefetched read pool contract for remote reads and can turn the target workload into synchronous per-task prefetching.

Please keep the skip-index check outside the mutex, but still schedule surviving tasks ahead of consumption, for example with a queue of pending tasks after skip pruning and budget acquisition, or with per-thread look-ahead so task N+1 can prefetch while task N is read.

params_,
context_)
context_,
std::move(index_build_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.

Passing index_build_context into MergeTreeReadPoolParallelReplicas makes createTask call shouldSkipPartRead, but getTask still holds mutex from the lock_guard through return createTask(...). partHasSelectedGranules can run skip/projection index reads via getOrBuildIndexReadResult, so one reader thread can serialize that storage/index work while every other parallel-replica reader waits on the pool mutex. This violates the PR contract that skip-index checks stay parallel across read threads; the same pattern exists in MergeTreeReadPoolParallelReplicasInOrder, where getTask also holds its mutex through createTask.

Please split range selection from task construction and call createTask after releasing the mutex in both parallel-replica pools.

@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.70% 84.70% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.30% 77.30% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 233/250 (93.20%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 13 line(s) · Uncovered code

Full report · Diff report

@EmeraldShift

Copy link
Copy Markdown
Contributor

Nice! I think this is very similar to what I'm trying to do in #103117.

My PR is specific to projection indexes: once the projection bitmap exists, it converts that bitmap to MarkRanges and intersects in getTask() before creating the read task. That helps reduce the reader's work when the projection index matches very few (or zero!) marks in a part.

This PR looks more general, evaluating all the skip indexes per part and then skipping instantiating readers for the parts that are empty. For the parts that survive this check, the full range of marks is still loaded, though.

I wonder if there's a way to combine the PRs neatly, so the shared abstraction returns std::optional<MarkRanges>:

  • nullopt means no narrowing is available; read the entire part normally
  • empty MarkRanges: no granules match for this part, skip creating the reader
  • (new) non-empty MarkRanges: intersect the PK-filtered MarkRanges with the MarkRanges from skip index analysis and projection index analysis, then only read those ranges.

Then projection bitmaps, text-index filters, and ordinary deferred skip indexes with granules_selected can all work the same way. Rather than partHasSelectedGranules() returning a bool, we can get full information about which marks need to be loaded.

What do you think? I don't think this necessarily needs to happen all in this PR, but maybe it's worth implementing in follow-up PRs? I'd be happy to help contribute these changes (like my above PR) with some help in code review.

@shankar-iyer

Copy link
Copy Markdown
Member Author

Hey @EmeraldShift I will take a look at the PR.

For the parts that survive this check, the full range of marks is still loaded, though.

Is the point that if only 1 or 2 granules are needed from a part that has 10000s of granules, there are excessive reads? That could just be : #106662

I will be closing this PR.

@shankar-iyer

Copy link
Copy Markdown
Member Author

Closing this PR - I just needed gating of createReaders.

@EmeraldShift

Copy link
Copy Markdown
Contributor

Is the point that if only 1 or 2 granules are needed from a part that has 10000s of granules, there are excessive reads? That could just be : #106662

I'm not sure if #106662 covers this: it sounds like that PR is about necesasry block-based compression fetching a few more MiB than necessary from object storage. In my case I have a projection index like SELECT _part_offset WHERE trace_id = ..., and a filter matching (say) one row from this projection index still causes all marks of all columns of all parts to be loaded.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants