Repository navigation
Add lazy posting list evaluation mode for text index - #100035
Conversation
Implement cursor-based on-demand decoding of posting lists for skip
index text index, avoiding eager materialization into Roaring Bitmaps.
V2 format appends an Index Section after each segment's payload in
the .pst stream, containing per-packed-block `last_row_id` and
`relative_offset` arrays for O(log N) seek within segments.
New `PostingListCursor` class provides lazy seek/next/linearOr/linearAnd
operations, decoding packed blocks on demand via `BitpackingBlockCodec`.
Intersection algorithms:
- Leapfrog (sparse): 2/3/4-way specializations + N-way generics
- Brute-force (dense): bitmap counting for cache-friendly access
- Automatic selection based on `text_index_density_threshold`
New settings:
- `text_index_posting_list_apply_mode` ("materialize"/"lazy")
- `text_index_density_threshold` (default 0.5)
Backward compatible: V1 parts use existing materialize path;
lazy mode requires V2 format and opt-in via setting.
Reference: amosbird#4
|
Workflow [PR], commit [b77fdc1] Summary: ❌
AI ReviewSummaryThis PR introduces an experimental lazy, cursor-based posting-list apply mode for text index queries, adds codec/version metadata in the text index header, and wires adaptive intersection ( Final VerdictStatus: ✅ Approve |
- Fix density overflow: use double for span computation - Validate codec_type in prepareSegment - Add payload_offset bounds check in decodeBlock
…gate - Use `getDeserializedFormat` substreams in read path for correct file extensions - Add `allow_experimental_text_index_lazy_apply` setting to gate lazy mode
|
Hi @Ergus, The current design requires users to explicitly opt in to V2 via version=2 in the text index definition, unlike MinMax which transparently writes the latest version and only maintains backward compatibility on reads — is this explicit version parameter the right approach here? e.g. |
Hi @fastio We are on version 3 by default now. Which is the latest. So any new development is intended to be for v3. The |
Thanks for the feedback — let me take another look at the latest code. |
The block index section is now always written when using compressed posting lists, removing the version-gated conditional. This simplifies the codec interface by eliminating the `version` parameter from `encode` and `serialize` methods throughout the pipeline. Tests updated to remove `version` references and consolidate v1/v2 table variants into a single table.
Add multiple performance optimizations to `linearAnd` and supporting code: - Dense segment/embedded shortcut: skip per-row binary search when every row in the range has a posting, increment the clipped region directly. - All-zeros skip at segment and block level: when the output region is already all-zeros, no increment can contribute to a match, so skip the entire segment or block without decoding. - AVX2-accelerated `hasAllZeros` for fast zero-region detection. - AVX2-accelerated `finalizeCounters` for the brute-force intersection final pass (count == n → 1, else → 0). - Binary search in `seek` across segments instead of linear scan. - Simplify embedded posting list decoding (eliminate intermediate buffer). - Cache lazy cursors across marks within the same part in `MergeTreeReaderTextIndex` to avoid redundant cursor construction. New profile events: `TextIndexLazyAndSegmentsSkippedZero`, `TextIndexLazyAndBlocksSkippedZero`, `TextIndexLazyAndSegmentsSkippedDense`.
|
I've recently made a few minor changes and addressed AI review comments. I've also tested new algorithms on a large dataset in S3, and performance looks great. I'll merge the PR as soon as CI passes. Once again, thank you for your contribution! |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
There are 2 total unresolved issues (including 1 from previous review).
Reviewed by Cursor Bugbot for commit 6109354. Configure here.
@CurtizJ, Awesome, glad the perf looks good on the S3 dataset. Thanks for the thorough review and follow-ups! |
LLVM Coverage Report
Changed lines: 96.72% (2743/2836) | lost baseline coverage: 2 line(s) · Uncovered code |
83c2b12
…osting-list Add lazy posting list evaluation mode for text index
| return true; | ||
|
|
||
| #if USE_MULTITARGET_CODE | ||
| if (isArchSupported(TargetArch::x86_64_v3)) |
There was a problem hiding this comment.
Do we really need AVX2 for this? Also, AVX2 is on by default so we don't need to use multi target code for it, just a __AVX2__ guard
There was a problem hiding this comment.
I'll remove the hand-written AVX2 code here in #123873. The code by complier is faster.

The text index (skip index) currently materializes all posting lists into
Roaring Bitmaps eagerly, which can be wasteful when only a small fraction of
the posting list is needed for intersection. This change introduces a lazy,
cursor-based evaluation path that decodes posting lists on demand at packed-block
granularity, avoiding full materialization.
Block index section in posting list codec. Each segment's payload in the
.pststream now always includes an appended Index Section containing per-packed-block
last_row_idandrelative_offsetarrays, enabling O(log N) seek within segmentswithout decoding entire blocks. This is backward compatible — existing indices
without the index section are handled by reconstructing block metadata via payload
scanning.
PostingListCursor. A new cursor class provides lazyseek/next/linearOr/linearAndoperations, decoding packed blocks on demand using
BitpackingBlockCodec.Embedded (small) postings are handled as a special case. Cursors are cached across
marks within the same part to avoid redundant construction.
Intersection algorithms. Two strategies are selected adaptively based on posting
list density:
generics with min-heap for >8 cursors
AVX2-accelerated
hasAllZeros/hasNoZeros/finalizeCountersfor cache-friendlyaccess and fast zero-region / dense-segment skipping
Observability. 11 new profile events (
TextIndexLazy*) for monitoring blockdecoding, seek counts, algorithm selection, and skip optimizations.
This is a continuation of earlier unfinished work #95176
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added lazy posting list apply mode for the text index. When enabled via
SET allow_experimental_text_index_lazy_apply = 1andSET text_index_posting_list_apply_mode = 'lazy', posting lists are decoded on demandat packed-block granularity using a cursor-based approach instead of being fully
materialized into Roaring Bitmaps, reducing memory usage and CPU time for selective
text index queries.
Documentation entry for user-facing changes
New settings:
allow_experimental_text_index_lazy_apply— experimental gate for lazy posting list mode (defaultfalse)text_index_posting_list_apply_mode— controls whether posting lists are materialized eagerly ('materialize', default) or decoded lazily on demand ('lazy')text_index_density_threshold— density threshold for selecting between leapfrog and brute-force intersection algorithms (default0.5)Note
High Risk
High risk because it changes the on-disk text index header/postings encoding (adds codec metadata + per-segment block index) and introduces a new lazy evaluation path in
MergeTreeReaderTextIndex, which can affect query correctness/performance and compatibility with existing parts.Overview
Adds an experimental lazy, cursor-based posting list evaluation mode for text skip indexes, selectable via new settings
allow_experimental_text_index_lazy_apply,text_index_posting_list_apply_mode(materialize/lazy), andtext_index_density_threshold.Updates text index serialization to write/read a new header (
TextIndexHeader) that includes the index version and posting-list codec type, and extends compressed posting list encoding to append a per-segment packed-block Index Section (HasBlockIndex) used for O(log N) block seeks.Introduces
PostingListCursorplus lazy union/intersection routines (density-based leapfrog vs brute-force), wires them intoMergeTreeReaderTextIndexwith per-(virtual column, token) cursor caching and newTextIndexLazy*profile events, and adjusts codec creation/compat handling and stream initialization accordingly.Reviewed by Cursor Bugbot for commit b77fdc1. Bugbot is set up for automated code reviews on this repo. Configure here.
Version info
26.6.1.118