Skip to content

Simplify and optimize building of text indexes - #105850

Closed
CurtizJ wants to merge 7 commits into
ClickHouse:masterfrom
CurtizJ:optimize-text-index-insert-perf
Closed

CurtizJ wants to merge 7 commits into
ClickHouse:masterfrom
CurtizJ:optimize-text-index-insert-perf

Conversation

@CurtizJ

@CurtizJ CurtizJ commented May 26, 2026

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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

Improved performance of building text indexes during inserts (especially for indexes with bitpacking codec).

CurtizJ added 6 commits April 15, 2026 23:31
Move the Roaring-bitmap-based serialization of posting lists into
`PostingListCodecNone::encode` / `decode` so that all non-raw cases go
through `IPostingListCodec::encode`. `PostingsSerialization::serialize`
now has only one special case for `RawPostings` (small posting lists
written as VarUInts); everything else delegates to the configured codec,
which serializes the data and fills `TokenPostingsInfo` with per-segment
offsets and ranges.

Removed the `serialize(const roaring_bitmap_t &, ...)` overload and the
ad-hoc multi-block path in `serializePostings` — segment splitting is
now the codec's responsibility, matching how `PostingListCodecBitpacking`
already worked. `deserialize` mirrors the change: `RawPostings` uses
the inline VarUInt buffer, otherwise the configured codec decodes.

`MergeTextIndexesTask::flushDictionaryBlock` was updated to use the
unified builder-based `serialize` for embedded postings instead of the
removed bitmap overload.
@CurtizJ CurtizJ changed the title Simply and optimize building of text indexes Simplify and optimize building of text indexes May 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [07c1124]

Summary: ⏳

job_name test_name status info comment
Stress test (amd_tsan) FAIL
Assertion 'px != 0' failed (STID: 4062-4c5b) FAIL cidb

AI Review

Summary

This PR refactors text-index posting list construction/serialization to operate on row-id arrays and routes none codec through the unified codec interface, with additional merge-path changes. The direction is reasonable, but there are unresolved performance/safety regressions around allocation behavior and peak memory that should be fixed before merge.

Findings

❌ Blockers

  • [src/Storages/MergeTree/TextIndexUtils.h:135] output_postings changed from compressed roaring::Roaring to std::vector<UInt32>, and flushPostingList now sorts/uniques after full accumulation. For dense tokens during large merges this can require O(cardinality) flat memory (hundreds of MB/GB), creating a realistic OOM/peak-memory regression versus the previous compressed accumulator.
    Suggested fix: keep a compressed/incremental accumulator (or incremental merge/dedup) so merge does not require full flat materialization of all row ids per token.

⚠️ Majors

  • [src/Storages/MergeTree/MergeTreeIndexTextPostingListCodec.cpp:57] PostingListCodecNone::decode allocates std::vector<char> on every non-contiguous read path. The prior implementation reused a scratch buffer; this change can add allocator churn in fragmented/postings-heavy reads and merges.
    Suggested fix: restore a reusable decode scratch buffer for none codec decode path.

  • [src/Storages/MergeTree/MergeTreeIndexText.cpp:1282] memoryUsageBytes still underreports per-token memory after switching tokens_map value to PostingListBuilder *: base posting_list_builders object/list-node storage is omitted, while only spill-heap bytes are counted.
    Suggested fix: include base storage for posting_list_builders entries (object + container node overhead), not only getSizeInBytes spill memory.

Final Verdict

Status: ⚠️ Request changes
Minimum required actions:

  1. Remove or mitigate the new peak-memory blow-up in merge accumulation (output_postings flat vector path).
  2. Restore reusable scratch buffering for none decode fallback path.
  3. Fix memoryUsageBytes accounting for PostingListBuilder ownership/storage.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label May 26, 2026
@antaljanosbenjamin antaljanosbenjamin self-assigned this May 26, 2026
@CurtizJ
CurtizJ marked this pull request as draft May 26, 2026 13:19
return;
}

std::vector<char> buffer(num_bytes);

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.

This adds a fresh heap allocation for every fallback decode (std::vector<char> buffer(num_bytes)).

Before this refactor, the same path reused PostingsSerialization::deserialization_buffer; now merges/reads that decode many posting blocks can hit allocator churn on every block when the payload is not fully contiguous in the current ReadBuffer window.

Can we keep a reusable scratch buffer for none decoding (for example by restoring a reusable buffer in PostingsSerialization and using it here) so we don't regress decode throughput/latency under fragmented reads?

for (const auto & plist : posting_lists)
posting_lists_size += plist.getSizeInBytes();
/// Heap-allocated posting list data (beyond the inline buffer in each PostingListBuilder).
/// The inline portion is already included in tokens_map.getBufferSizeInBytes().

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.

tokens_map now stores PostingListBuilder *, so tokens_map.getBufferSizeInBytes() no longer includes inline PostingListBuilder storage.

With this change, memoryUsageBytes underreports per-token memory by omitting the posting_list_builders objects (and list-node overhead), while only counting heap spill bytes via getSizeInBytes.

Please include base posting_list_builders object storage in this estimate as well (for example, iterate posting_list_builders instead of only tokens_and_postings pointers, and add per-builder object cost).

PostingList output_postings;
/// Row_ids accumulated for the current token across all source parts.
/// Sorted and deduplicated before serialization.
std::vector<UInt32> output_postings;

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.

Switching output_postings from roaring::Roaring to std::vector<UInt32> means we now materialize all row ids for a token before dedup (sort + unique in flushPostingList).

For dense tokens in large merges, this can grow to hundreds of MB/GB (4 * cardinality bytes) and is a real peak-memory regression compared to the previous compressed accumulator.

Can we avoid full materialization here (for example by keeping a compressed accumulator, or by incremental merge/dedup that does not require holding the full token posting list as a flat vector)?

@clickhouse-gh

clickhouse-gh Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.20% 84.10% -0.10%
Functions 91.40% 91.40% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 90.80% (158/174) | lost baseline coverage: 3 line(s) · Uncovered code

Full report · Diff report

@antaljanosbenjamin antaljanosbenjamin removed their assignment May 28, 2026
@CurtizJ

CurtizJ commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

Continued in #106735.

@CurtizJ CurtizJ closed this Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants