Skip to content

Make text index flush limits configurable - #111573

Merged
CurtizJ merged 14 commits into
ClickHouse:masterfrom
rorylshanks:text-index-flush-settings
Jul 28, 2026
Merged

CurtizJ merged 14 commits into
ClickHouse:masterfrom
rorylshanks:text-index-flush-settings

Conversation

@rorylshanks

@rorylshanks rorylshanks commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Allow text index builders to flush temporary segments based on configurable token and estimated memory thresholds. The new MergeTree settings text_index_max_processed_tokens_before_flush and text_index_max_memory_usage_before_flush replace the hard-coded token threshold and reduce retained builder memory while materializing text indexes.

Memory is estimated from the current thread's tracked allocation delta around each processed block. This accounts for posting lists and position data without traversing the text-index structures. The memory threshold is a soft flush trigger evaluated after each block; it does not bound total mutation memory. The TextIndexTemporarySegmentsWritten profile event makes threshold-triggered flushing observable.

Negative error below means that the estimate undercounted tracked retained memory:

Workload Actual tracked bytes Tracking error
1M unique tokens 83,927,136 -0.00005%
1M tokens, 100k distinct 56,926,816 -0.00007%
One token across 1M rows 183,072 -0.02185%
300k unique tokens with positions 88,768,736 -0.00070%
One token across 1M rows with positions 12,817,824 -0.00487%
1M positions in one row 496,352 -0.12572%
Worst case -0.126%

The materialization benchmark used five local runs per implementation with 1M position-bearing rows and a 1 MiB flush threshold. CPU covers the complete MATERIALIZE INDEX mutation, including temporary-segment merging:

Median metric Before After Change
CPU time 7.483 s 7.317 s -2.2%
Wall time 7.483 s 7.317 s -2.2%
CPU utilization ~100% ~100% No change
Peak tracked memory 40.63 MiB 35.06 MiB -13.7%
Temporary segments 1 16 Expected

No CPU regression was observed despite correctly producing additional temporary segments.

Changelog category (leave one):

  • Improvement

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

Added the text_index_max_processed_tokens_before_flush and text_index_max_memory_usage_before_flush settings to control when text index builders flush temporary segments.

Version info

  • Merged into: 26.8.1.275 (included in 26.8 and later)

@CurtizJ CurtizJ self-assigned this Jul 23, 2026
@CurtizJ CurtizJ added the can be tested Allows running workflows for external contributors label Jul 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7bf7dc6]

Summary: ✅


AI Review

Summary

This PR makes text-index temporary-segment flushing configurable and adds a memory-based flush estimate plus the TextIndexTemporarySegmentsWritten profile event. The implementation looks consistent after the earlier review fixes, but the new regression tests still read mutation part_log rows through a known racy pattern, so I would request changes until those assertions are made deterministic.

Findings

⚠️ Majors

  • [tests/queries/0_stateless/04626_text_index_flush_settings.sql:39], [tests/queries/0_stateless/04627_text_index_flush_memory_tracking.sql:35] Both tests read ProfileEvents['TextIndexTemporarySegmentsWritten'] from system.part_log immediately after a single SYSTEM FLUSH LOGS part_log. Mutation part_log entries are not guaranteed to be visible after one flush; this repository already uses retry loops for the same race in 03221_mutate_profile_events.sh, 03221_mutation_analyzer_skip_part.sh, and 03442_alter_delete_empty_part.sh. As written, these assertions can intermittently see no row and fail even when the flush-threshold logic is correct.
    Suggested fix: move these checks to .sh tests or add the same retry-until-row-visible loop before selecting from system.part_log.
Final Verdict
  • Status: ⚠️ Request changes

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 23, 2026
Force a Wide part explicitly so the regression needs only 1,024 rows instead of one million.\n\nhttps://github.com/ClickHouse/pull/111573
Isolate token-triggered flushing and give memory-triggered flushing enough allocation margin across sanitizer builds.\n\nhttps://github.com/ClickHouse/pull/111573
Preserve pre-26.7 token-only behavior when `compatibility` selects an older version.
Record the new `MergeTree` settings in 26.8 and use `tupleElement` in the regression test for old-analyzer compatibility.

CI: ClickHouse#111573
Comment thread src/Storages/MergeTree/MutateTask.cpp Resolved
The memory estimate of the text index builder is a thread memory tracker
delta taken around `MergeTreeIndexAggregatorText::update`. Sanitizer builds
link the sanitizer runtime's `operator new`, which replaces the tracking one
from `AllocationInterceptors.cpp`, so `new` allocations are invisible to the
tracker there. The previous workload - two rows holding one token - grew the
builder only through `operator new`: the arena's first chunk and the token
map are allocated in the aggregator constructor, before the measurement
starts. The measured delta stayed zero, the threshold never triggered, and
the mutation wrote one segment instead of several.

Use 20000 distinct tokens so the builder grows through the arena and the
token map. Those allocations go through `Allocator`, which reports to the
memory tracker in every build. `max_block_size` is pinned so the threshold
is evaluated on more than one block, and `index_granularity = 1` is dropped:
that setting, not the row count, is what made the original million-row
version of this test time out.

Verified against the ASan and release builds of a982c5e from CI, 10
runs each, ~0.5s per run under ASan and ~0.2s under release. The 64 KiB
threshold has about 32x of headroom - the test still passes with a 2 MiB
threshold on both builds.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111573&sha=a982c5e591050c2033ee0ced870f9fcaa6f2b41f&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%29
ClickHouse#111573
@CurtizJ
CurtizJ added this pull request to the merge queue Jul 27, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 27, 2026
@CurtizJ
CurtizJ added this pull request to the merge queue Jul 27, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 27, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 78.40% 78.40% +0.00%

Changed lines: Changed C/C++ lines covered: 73/73 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This PR is scoped entirely to text-index construction during background merges and mutations (BuildTextIndexTransform flush thresholds plus two new merge-tree settings); it does not touch the query-execution path that ClickBench SELECTs use. ClickBench Q4 shows a -15.3% shift (243 ms vs 287 ms) but it falls within master's current variance band and was correctly suppressed as no_change — the changed code cannot plausibly speed up that query, so treat it as run-to-run variance. No real regressions or improvements are attributable to this change.

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 0eed1c04-a148-4a25-8641-e9eeeddc5d05
  • MIRAI run: 065f9337-7beb-4bc9-9a0c-60d13055ce59
  • PR check IDs:
    • clickbench_658303_1785240076
    • clickbench_658309_1785240076
    • clickbench_658320_1785240076
    • tpch_adapted_1_official_658332_1785240077
    • tpch_adapted_1_official_658346_1785240077
    • tpch_adapted_1_official_658373_1785240076

@CurtizJ
CurtizJ added this pull request to the merge queue Jul 28, 2026
Merged via the queue into ClickHouse:master with commit f9a6b88 Jul 28, 2026
178 checks passed
@rorylshanks
rorylshanks deleted the text-index-flush-settings branch July 28, 2026 14:21
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-improvement Pull request with some product improvements 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