Skip to content

Bypass index uncompressed cache when its size is zero - #104063

Merged
al13n321 merged 4 commits into
masterfrom
fix-empty-index-uncompressed-cache
May 5, 2026
Merged

al13n321 merged 4 commits into
masterfrom
fix-empty-index-uncompressed-cache

Conversation

@al13n321

@al13n321 al13n321 commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Performance Improvement

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

Avoid index uncompressed cache overhead when the cache is disabled (server setting index_uncompressed_cache_size = 0, which is the default).


Simple change.

(Artisanally written changelog entry and C++ code. Vibecoded unreviewed test, but I did check that it fails the expected way without this PR. The rest of this PR summary is from claude.)
(See the reverse-linked support escalation for customer report.)


With index_uncompressed_cache_size = 0 (the server default, advertised as "disabled"), Context::getIndexUncompressedCache still returned a non-null UncompressedCache of capacity zero. MergeTreeIndexReader then took the CachedCompressedReadBuffer path in MergeTreeReaderStream::init, so every skip-index block read acquired the global CacheBase mutex twice, evicted itself via removeOverflow, and unconditionally incremented UncompressedCacheMisses and UncompressedCacheWeightLost. With small-GRANULARITY skip indexes (ngrambf_v1, bloom_filter, etc.) this can produce hundreds of thousands of contended mutex acquisitions per query and tens of GB of phantom UncompressedCacheWeightLost, with zero UncompressedCacheHits.

The original 2021 code only constructed the cache when the configured size was non-zero. The if (size) guard at the call site in Server.cpp was later removed so the cache could be resized at runtime via SYSTEM RELOAD CONFIG; the data uncompressed cache survived this because MergeTreeReadPoolBase already gates it on pool_settings.use_uncompressed_cache, but the index-read path had no such guard.

This change tracks whether the cache should actually be used in a separate flag, set in setIndexUncompressedCache and refreshed in updateIndexUncompressedCacheConfiguration. getIndexUncompressedCache returns nullptr when the flag is false; MergeTreeReaderStream::init already falls through to CompressedReadBufferFromFile on a null pointer. Runtime resize from 0 -> N still works.

Version info

  • Merged into: 26.5.1.314
  • Backported to: 26.4.2.4, 26.3.10.62, 26.2.18.10, 25.8.24.4

`index_uncompressed_cache_size = 0` (the server default) is documented
as disabling the cache, but `Context::getIndexUncompressedCache`
returned a non-null `UncompressedCache` of capacity zero.
`MergeTreeIndexReader` -> `MergeTreeReaderStream::init` then took the
`CachedCompressedReadBuffer` path, where every skip-index block read
acquired the global `CacheBase` mutex twice (lookup + insert), evicted
itself immediately via `removeOverflow`, and unconditionally bumped
`UncompressedCacheMisses` and `UncompressedCacheWeightLost`. On a query
evaluating skip indexes with small `GRANULARITY` this materialized as
hundreds of thousands of contended mutex acquisitions per query and
tens of GB of phantom `UncompressedCacheWeightLost`, with zero
`UncompressedCacheHits`.

The original code created the cache object only when the size was
non-zero. The `if (size)` guard at the call site was dropped to allow
runtime resizing via `SYSTEM RELOAD CONFIG`. Restore the bypass by
tracking whether the cache should be used in a separate flag, set when
the cache is constructed and re-evaluated by
`updateIndexUncompressedCacheConfiguration`. `getIndexUncompressedCache`
returns nullptr when the flag is false; `MergeTreeReaderStream::init`
already falls through to `CompressedReadBufferFromFile` on a null
pointer. Runtime resize 0 -> N still works because the flag is
refreshed after `setMaxSizeInBytes`.

The data uncompressed cache is unaffected because
`MergeTreeReadPoolBase` already gates `getUncompressedCache` on
`pool_settings.use_uncompressed_cache`; the index path had no such
guard.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f78db8e]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_tsan, s3 storage, parallel, 2/2) FAIL
02270_errors_in_files_s3 FAIL cidb
Docker server image FAIL
clickhouse/clickhouse-server:head-amd64 FAIL cidb
docker library image test (clickhouse/clickhouse-server:head-amd64) FAIL cidb

AI Review

Summary

This PR makes index_uncompressed_cache_size = 0 truly disable index uncompressed cache usage while preserving runtime re-enable via SYSTEM RELOAD CONFIG, and adds an integration test that covers disable → enable → disable transitions. I did not find correctness, safety, or performance-regression issues in the final diff.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label May 5, 2026
Comment thread src/Interpreters/Context.cpp Resolved
al13n321 and others added 3 commits May 4, 2026 19:21
Spins up a server with `index_uncompressed_cache_size = 0`, runs a
query that exercises a `bloom_filter` skip index, and asserts that
`UncompressedCacheHits`, `UncompressedCacheMisses`, and
`UncompressedCacheWeightLost` in `system.query_log` are all zero - the
bypass path verified.

Then enables the cache via `SYSTEM RELOAD CONFIG`, asserts misses
appear on the first query and hits on a repeated one, and finally
restores the original config (also via `SYSTEM RELOAD CONFIG`) and
re-asserts that the profile events are zero again. This covers the
runtime resize path in `Context::updateIndexUncompressedCacheConfiguration`
in both directions.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 5, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 93.75% (15/16) | lost baseline coverage: 1 line(s) · Uncovered code

Full report · Diff report

@Algunenano Algunenano self-assigned this May 5, 2026

@Algunenano Algunenano left a comment

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.

Looks good for an artisanal PR 😄 . I left a small comment

UncompressedCachePtr Context::getIndexUncompressedCache(bool only_if_enabled) const
{
SharedLockGuard lock(shared->mutex);
if (only_if_enabled && !shared->index_uncompressed_cache_enabled)

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 do we need index_uncompressed_cache_enabled? Could we do if (only_if_enabled && shared->index_uncompressed_cache->maxSizeInBytes() == 0) instead and avoid adding extra members?

@al13n321 al13n321 May 5, 2026 •

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.

maxSizeInBytes() locks an std::mutex, I avoided it just in case, though I don't have a good mental model of what's important or not important for performance here.

(We could go further and make index_uncompressed_cache_enabled atomic so we can check it without locking shared->mutex. But that seemed too weird, and the caller of getIndexUncompressedCache does a few similar calls that lock shared->mutex the same way anyway.)

(EDIT: Or we could make maxSizeInBytes() thread safe by making the field atomic in all 3 ICachePolicy implementations.)

@al13n321
al13n321 enabled auto-merge May 5, 2026 21:48
@al13n321
al13n321 added this pull request to the merge queue May 5, 2026
@al13n321

al13n321 commented May 5, 2026 •

Copy link
Copy Markdown
Contributor Author

Test failures looked unrelated, ran gh api repos/ClickHouse/ClickHouse/statuses/f78db8ebaa816ab6db05027435e9ffe72b29e54b -X POST -F state=success -F description="Manually unset to merge" -F context="Mergeable Check"

EDIT: 02270_errors_in_files_s3 presumably already fixed by #104077 , Docker server image reported in #104152

Merged via the queue into master with commit bc39c42 May 5, 2026
162 of 165 checks passed
@al13n321
al13n321 deleted the fix-empty-index-uncompressed-cache branch May 5, 2026 22:11
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 5, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — could this be a backport candidate for 26.4, 26.3 (LTS), 26.2, 25.8 (LTS)? No backport label was found, and the affected code appears to exist in those branches based on a file/function presence check.

Affected code: Context::getIndexUncompressedCache and skip-index read paths (MergeTreeIndexReader, MergeTreeReaderTextIndex, MergeTreeSkipIndexReader) — core code present in all supported branches.

Why this might apply: Fixes a performance regression affecting all users running the default config (index_uncompressed_cache_size=0). PR description says the regression dates from the 2021 removal of the if (size) guard at the cache construction site in Server.cpp — the affected code (Context::getIndexUncompressedCache + skip-index call sites) predates all currently supported stable branches. Customer escalation is referenced in the PR. No backport label is present.

Caveat: I verified that the modified code exists in those branches, but not whether the bug is actually reachable there — a new caller in master may be what makes this newly observable. If older branches can't reach the affected path, please ignore this; otherwise consider pr-must-backport or a version label like v26.4-must-backport.

@al13n321 al13n321 added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label May 5, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label May 5, 2026
robot-ch-test-poll1 added a commit that referenced this pull request May 5, 2026
Cherry pick #104063 to 26.4: Bypass index uncompressed cache when its size is zero
al13n321 added a commit that referenced this pull request May 5, 2026
… size is zero (#104156)

* Bypass index uncompressed cache when its size is zero

`index_uncompressed_cache_size = 0` (the server default) is documented
as disabling the cache, but `Context::getIndexUncompressedCache`
returned a non-null `UncompressedCache` of capacity zero.
`MergeTreeIndexReader` -> `MergeTreeReaderStream::init` then took the
`CachedCompressedReadBuffer` path, where every skip-index block read
acquired the global `CacheBase` mutex twice (lookup + insert), evicted
itself immediately via `removeOverflow`, and unconditionally bumped
`UncompressedCacheMisses` and `UncompressedCacheWeightLost`. On a query
evaluating skip indexes with small `GRANULARITY` this materialized as
hundreds of thousands of contended mutex acquisitions per query and
tens of GB of phantom `UncompressedCacheWeightLost`, with zero
`UncompressedCacheHits`.

The original code created the cache object only when the size was
non-zero. The `if (size)` guard at the call site was dropped to allow
runtime resizing via `SYSTEM RELOAD CONFIG`. Restore the bypass by
tracking whether the cache should be used in a separate flag, set when
the cache is constructed and re-evaluated by
`updateIndexUncompressedCacheConfiguration`. `getIndexUncompressedCache`
returns nullptr when the flag is false; `MergeTreeReaderStream::init`
already falls through to `CompressedReadBufferFromFile` on a null
pointer. Runtime resize 0 -> N still works because the flag is
refreshed after `setMaxSizeInBytes`.

The data uncompressed cache is unaffected because
`MergeTreeReadPoolBase` already gates `getUncompressedCache` on
`pool_settings.use_uncompressed_cache`; the index path had no such
guard.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* oops

* Add integration test for index_uncompressed_cache_size = 0

Spins up a server with `index_uncompressed_cache_size = 0`, runs a
query that exercises a `bloom_filter` skip index, and asserts that
`UncompressedCacheHits`, `UncompressedCacheMisses`, and
`UncompressedCacheWeightLost` in `system.query_log` are all zero - the
bypass path verified.

Then enables the cache via `SYSTEM RELOAD CONFIG`, asserts misses
appear on the first query and hits on a repeated one, and finally
restores the original config (also via `SYSTEM RELOAD CONFIG`) and
re-asserts that the profile events are zero again. This covers the
runtime resize path in `Context::updateIndexUncompressedCacheConfiguration`
in both directions.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Fix system.server_settings

---------

Co-authored-by: Michael Kolupaev <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
al13n321 added a commit that referenced this pull request May 5, 2026
… size is zero (#104157)

* Bypass index uncompressed cache when its size is zero

`index_uncompressed_cache_size = 0` (the server default) is documented
as disabling the cache, but `Context::getIndexUncompressedCache`
returned a non-null `UncompressedCache` of capacity zero.
`MergeTreeIndexReader` -> `MergeTreeReaderStream::init` then took the
`CachedCompressedReadBuffer` path, where every skip-index block read
acquired the global `CacheBase` mutex twice (lookup + insert), evicted
itself immediately via `removeOverflow`, and unconditionally bumped
`UncompressedCacheMisses` and `UncompressedCacheWeightLost`. On a query
evaluating skip indexes with small `GRANULARITY` this materialized as
hundreds of thousands of contended mutex acquisitions per query and
tens of GB of phantom `UncompressedCacheWeightLost`, with zero
`UncompressedCacheHits`.

The original code created the cache object only when the size was
non-zero. The `if (size)` guard at the call site was dropped to allow
runtime resizing via `SYSTEM RELOAD CONFIG`. Restore the bypass by
tracking whether the cache should be used in a separate flag, set when
the cache is constructed and re-evaluated by
`updateIndexUncompressedCacheConfiguration`. `getIndexUncompressedCache`
returns nullptr when the flag is false; `MergeTreeReaderStream::init`
already falls through to `CompressedReadBufferFromFile` on a null
pointer. Runtime resize 0 -> N still works because the flag is
refreshed after `setMaxSizeInBytes`.

The data uncompressed cache is unaffected because
`MergeTreeReadPoolBase` already gates `getUncompressedCache` on
`pool_settings.use_uncompressed_cache`; the index path had no such
guard.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* oops

* Add integration test for index_uncompressed_cache_size = 0

Spins up a server with `index_uncompressed_cache_size = 0`, runs a
query that exercises a `bloom_filter` skip index, and asserts that
`UncompressedCacheHits`, `UncompressedCacheMisses`, and
`UncompressedCacheWeightLost` in `system.query_log` are all zero - the
bypass path verified.

Then enables the cache via `SYSTEM RELOAD CONFIG`, asserts misses
appear on the first query and hits on a repeated one, and finally
restores the original config (also via `SYSTEM RELOAD CONFIG`) and
re-asserts that the profile events are zero again. This covers the
runtime resize path in `Context::updateIndexUncompressedCacheConfiguration`
in both directions.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Fix system.server_settings

---------

Co-authored-by: Michael Kolupaev <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
al13n321 added a commit that referenced this pull request May 6, 2026
… size is zero (#104158)

* Bypass index uncompressed cache when its size is zero

`index_uncompressed_cache_size = 0` (the server default) is documented
as disabling the cache, but `Context::getIndexUncompressedCache`
returned a non-null `UncompressedCache` of capacity zero.
`MergeTreeIndexReader` -> `MergeTreeReaderStream::init` then took the
`CachedCompressedReadBuffer` path, where every skip-index block read
acquired the global `CacheBase` mutex twice (lookup + insert), evicted
itself immediately via `removeOverflow`, and unconditionally bumped
`UncompressedCacheMisses` and `UncompressedCacheWeightLost`. On a query
evaluating skip indexes with small `GRANULARITY` this materialized as
hundreds of thousands of contended mutex acquisitions per query and
tens of GB of phantom `UncompressedCacheWeightLost`, with zero
`UncompressedCacheHits`.

The original code created the cache object only when the size was
non-zero. The `if (size)` guard at the call site was dropped to allow
runtime resizing via `SYSTEM RELOAD CONFIG`. Restore the bypass by
tracking whether the cache should be used in a separate flag, set when
the cache is constructed and re-evaluated by
`updateIndexUncompressedCacheConfiguration`. `getIndexUncompressedCache`
returns nullptr when the flag is false; `MergeTreeReaderStream::init`
already falls through to `CompressedReadBufferFromFile` on a null
pointer. Runtime resize 0 -> N still works because the flag is
refreshed after `setMaxSizeInBytes`.

The data uncompressed cache is unaffected because
`MergeTreeReadPoolBase` already gates `getUncompressedCache` on
`pool_settings.use_uncompressed_cache`; the index path had no such
guard.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* oops

* Add integration test for index_uncompressed_cache_size = 0

Spins up a server with `index_uncompressed_cache_size = 0`, runs a
query that exercises a `bloom_filter` skip index, and asserts that
`UncompressedCacheHits`, `UncompressedCacheMisses`, and
`UncompressedCacheWeightLost` in `system.query_log` are all zero - the
bypass path verified.

Then enables the cache via `SYSTEM RELOAD CONFIG`, asserts misses
appear on the first query and hits on a repeated one, and finally
restores the original config (also via `SYSTEM RELOAD CONFIG`) and
re-asserts that the profile events are zero again. This covers the
runtime resize path in `Context::updateIndexUncompressedCacheConfiguration`
in both directions.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Fix system.server_settings

---------

Co-authored-by: Michael Kolupaev <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label May 6, 2026
alexey-milovidov added a commit that referenced this pull request May 6, 2026
Backport #104063 to 26.4: Bypass index uncompressed cache when its size is zero
al13n321 added a commit that referenced this pull request May 7, 2026
…ze is zero (#104164)

* Backport #104063 to 25.8: Bypass index uncompressed cache when its size is zero

* Add reload_before/reload_after to with_replace_config

Cherry-picked from b514957 (the cluster.py portion).
The whitespace-only KEEPER_PUBLISH_CLIENT hunk was skipped because that
code does not exist on this branch.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: robot-clickhouse <[email protected]>
Co-authored-by: Michael Kolupaev <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
al13n321 added a commit that referenced this pull request May 7, 2026
…ze is zero (#104166)

* Backport #104063 to 26.3: Bypass index uncompressed cache when its size is zero

* Add reload_before/reload_after to with_replace_config

Cherry-picked from b514957 (the cluster.py portion).
The whitespace-only KEEPER_PUBLISH_CLIENT hunk was skipped because that
code does not exist on this branch.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: robot-clickhouse <[email protected]>
Co-authored-by: Michael Kolupaev <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
al13n321 added a commit that referenced this pull request May 7, 2026
…ze is zero (#104165)

* Backport #104063 to 26.2: Bypass index uncompressed cache when its size is zero

* Add reload_before/reload_after to with_replace_config

Cherry-picked from b514957 (the cluster.py portion).
The whitespace-only KEEPER_PUBLISH_CLIENT hunk was skipped because that
code does not exist on this branch.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: robot-clickhouse <[email protected]>
Co-authored-by: Michael Kolupaev <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
@nikitamikhaylov nikitamikhaylov added v25.12-must-backport and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore labels May 7, 2026
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label May 7, 2026
@nikitamikhaylov nikitamikhaylov added v26.2-must-backport and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore labels May 8, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label May 8, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…4063

Backport ClickHouse#104063 to 26.4: Bypass index uncompressed cache when its size is zero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo v25.12-must-backport v26.2-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants