Skip to content

fix(ingester): do not retain unfiltered postings in lazy-matcher cache entries - #7992

Merged
alanprot merged 2 commits into
cortexproject:masterfrom
alanprot:fix-lazy-matcher-postings-retention
Dec 4, 2026
Merged

alanprot merged 2 commits into
cortexproject:masterfrom
alanprot:fix-lazy-matcher-postings-retention

Conversation

@alanprot

@alanprot alanprot commented Dec 4, 2026

Copy link
Copy Markdown
Member

What this PR does:

Fixes the expanded postings cache keeping the whole unfiltered postings list in memory for entries resolved with lazy matchers.

fetchWithLazyMatchers filters in place with filtered := ids[:0]. The returned slice shares the backing array of every series the selective matchers returned, but the cache charges the entry only len(filtered)*8 bytes. A query whose lazy matchers keep a few series out of many therefore holds far more memory than max_bytes accounts for.

For example, with 50k series selected by __name__="cpu" and a deferred pod=~"keep-.*" that keeps 10, each cached entry kept the full array reachable: 17.2 MB retained against 6.6 KB charged across 40 entries.

The fix copies the filtered refs into an exact-size slice when filtering removed anything. Filtering still happens in place, and no copy is made when every series matched. make + append is used instead of slices.Clone so the capacity equals the length exactly.

Which issue(s) this PR fixes:
None filed. The in-place filter came in with #7553.

Checklist

  • Tests updated (TestFetchWithLazyMatchers_ResultDoesNotRetainUnfilteredRefs: fails before the fix with cap 1280 for 2 kept refs, passes after)
  • Documentation added (not needed)
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
  • docs/configuration/v1-guarantees.md updated if this PR introduces experimental flags (no new flags)
  • I did not look at code from AGPL licensed projects to make this PR

…e entries

fetchWithLazyMatchers filtered in place with filtered := ids[:0], so the
cached slice kept the whole expanded postings array reachable while the
cache charged only len(filtered)*8 bytes. A query whose lazy matchers
kept a few series out of many held far more memory than max_bytes
accounted for.

Copy the filtered refs into an exact-size slice when filtering removed
anything.

Signed-off-by: Alan Protasio <alanprot@gmail.com>
@alanprot
alanprot requested a review from a team as a code owner December 4, 2026 17:35
@alanprot
alanprot requested a review from yeya24 December 4, 2026 17:35
Signed-off-by: Alan Protasio <alanprot@gmail.com>
@alanprot
alanprot enabled auto-merge (squash) December 4, 2026 17:38
@alanprot
alanprot merged commit 32756c7 into cortexproject:master Dec 4, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants