Skip to content

Improve query performance for compressed posting lists - #95176

Closed
fastio wants to merge 0 commit into
ClickHouse:masterfrom
fastio:improve-compressed-posting-list-query-perf
Closed

fastio wants to merge 0 commit into
ClickHouse:masterfrom
fastio:improve-compressed-posting-list-query-perf

Conversation

@fastio

@fastio fastio commented Jan 26, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Improvement

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

  • Improved query performance for compressed posting lists in text indexes by introducing a new posting_list_apply_mode parameter. The new lazy mode uses cursor-based iteration with skip-list algorithms for efficient intersection/union operations, avoiding full decompression. This significantly reduces memory usage and latency for selective queries involving many posting lists.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Motivation:

In the current implementation, compressed posting lists must first be decompressed and then materialized into Roaring Bitmaps before AND/OR operations can be applied. Building Roaring Bitmaps is expensive, and the cost scales roughly proportionally to the number of posting lists involved. Therefore, queries involving a large number of posting lists suffer from noticeable latency due to this bitmap construction overhead.

This PR optimizes the execution path by bypassing the Roaring Bitmap construction phase. Instead of materializing each decompressed posting list into a Roaring Bitmap, we directly build the final AND/OR result during decompression, eliminating the per-posting-list bitmap construction cost.

Parameters

  • New parameter posting_list_apply_mode: Allows users to choose between lazy and materialize modes
  • New system settings: text_index_brute_force_apply and text_index_brute_force_density_threshold for fine-tuning query behavior.

The details of posting_list_apply_mode:

  • lazy:

Uses cursor-based iteration for on-demand decoding
Employs skip-list algorithms for efficient intersection/union operations
Avoids full decompression, resulting in lower memory usage
Best for selective queries (e.g., hasAllTokens with rare tokens)
Note: When using lazy mode, codec cannot be none

  • materialize:

Fully decompresses posting lists into Roaring Bitmaps before set operations
May be faster for queries accessing most posting list entries (e.g., dense token unions)

Example use

CREATE TABLE tab
(
    ...
    INDEX idx col TYPE text([, tokenizer = ...],...,[, posting_list_apply_mode = 'lazy' | 'materialize'])
) ENGINE = MergeTree
ORDER BY ...

Note

Low Risk
Low risk because the provided diff contains no modifications to code or configuration.

Overview
The provided diff contains no file or code changes (it only shows +++ /dev/null with no associated hunks), so there is nothing to review for functional impact.

Written by Cursor Bugbot for commit b4ac574. This will update automatically on new commits. Configure here.

@fastio
fastio marked this pull request as draft January 26, 2026 14:35
@fastio
fastio marked this pull request as ready for review January 27, 2026 11:34
@fastio
fastio marked this pull request as draft January 27, 2026 11:34
@fastio
fastio marked this pull request as ready for review January 27, 2026 11:40
@EmeraldShift

Copy link
Copy Markdown
Contributor

cc @rschu1ze -- I think this one still needs approval to run tests

@rschu1ze rschu1ze added the can be tested Allows running workflows for external contributors label Feb 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Feb 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [768ffc7]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_ubsan, parallel) failure
03411_dynamic_resize_filesystem_cache_3 FAIL cidb

@rschu1ze

rschu1ze commented Feb 2, 2026

Copy link
Copy Markdown
Member

I added the can be tested label - CI now runs automatically for new commits.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Feb 2, 2026
@Ergus

Ergus commented Feb 2, 2026

Copy link
Copy Markdown
Member

Hi @fastio

You are back on board :), thanks!!

I see that there are a few small issues in the CI. Once solved I will review all the changes all together in a first iteration as usual.

@fastio

fastio commented Feb 3, 2026

Copy link
Copy Markdown
Contributor Author

Hi @fastio

You are back on board :), thanks!!

I see that there are a few small issues in the CI. Once solved I will review all the changes all together in a first iteration as usual.

Hi @Ergus , thanks!! Happy to be back 😄
Yep, I noticed the CI hiccups — I’ll get them sorted and will ping you once CI is green for the first pass review.

- `none` - the posting lists are stored without additional compression.
- `bitpacking` - apply [differential (delta) coding](https://en.wikipedia.org/wiki/Delta_encoding), followed by [bit-packing](https://dev.to/madhav_baby_giraffe/bit-packing-the-secret-to-optimizing-data-storage-and-transmission-m70) (each within blocks of fixed-size). Slows down SELECT queries and is not recommended at the moment.

Optional parameter `posting_list_apply_mode` (default: `lazy`) specifies how compressed posting lists are decoded and applied during query execution. This parameter is only effective when `posting_list_codec` is set to a compressed codec (e.g., `bitpacking`):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does posting_list_apply_mode need to be an index parameter? Can it not be a normal session setting?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can indeed be made into a session-level setting.
If apply_mode='lazy' is enabled, then during posting list compression we need to write block-level metadata such as the offset and the max row id.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts:

  • we should avoid having two different persistence formats in the first place. This would cause too much complexity for too little benefit.
  • if lazy reading needs extra metadata, then let's just add it to the persistence format - I didn't read the code yet but I assume it is negligibly small in comparison to the rest of the posting lists
  • note that we'll need to increment the index version (see enum class SparseIndexVersion) whenever we change the persistence format after 26.2 (this is when the text index is planned to be made GA)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rschu1ze , Thanks for the feedback — I’ll think it through and reorganize the code accordingly.

@Ergus

Ergus commented Feb 13, 2026

Copy link
Copy Markdown
Member

Hi @fastio

I have been checking this performance improvement but I don't get any clear performance improvement. Do you have some specific cases where I can notice some performance improvement?

At least some select queries where time improves end-to-end? If the performance improvement is not significant maybe we can/should consider some other optimizations instead of this.

@fastio fastio closed this Mar 13, 2026
@fastio
fastio force-pushed the improve-compressed-posting-list-query-perf branch from 768ffc7 to b4ac574 Compare March 13, 2026 06:18
@Ergus

Ergus commented Mar 13, 2026

Copy link
Copy Markdown
Member

Hi @fastio

Any reason why you closed this PR? Do you need any help?

@fastio

fastio commented Mar 13, 2026

Copy link
Copy Markdown
Contributor Author

Hi @fastio

Any reason why you closed this PR? Do you need any help?

Hi @Ergus, It looks like I accidentally deleted the branch associated with this PR while cleaning up my repository. I'm converting it to draft for now. I've validated the optimization on a separate branch and it's working
well. I'll reopen this PR once it's ready. Thanks!

@fastio

fastio commented Mar 13, 2026

Copy link
Copy Markdown
Contributor Author

I'm converting this to draft for now and will reopen it once it's ready.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants