Skip to content

release-26.2: sql/stats: make table statistics cache size configurable#169247

Merged
trunk-io[bot] merged 2 commits into
cockroachdb:release-26.2from
DrewKimball:backport26.2-168711
Apr 28, 2026
Merged

release-26.2: sql/stats: make table statistics cache size configurable#169247
trunk-io[bot] merged 2 commits into
cockroachdb:release-26.2from
DrewKimball:backport26.2-168711

Conversation

@DrewKimball
Copy link
Copy Markdown
Collaborator

@DrewKimball DrewKimball commented Apr 28, 2026

Backport 2/3 commits from #168711.

/cc @cockroachdb/release


stats: make table statistics cache size configurable

Replace the hardcoded table statistics cache size (previously passed
through server config plumbing as defaultSQLTableStatCacheSize = 256)
with a cluster setting sql.stats.table_statistics_cache.capacity. The
ShouldEvict callback now reads the setting dynamically, so the cache
resizes on the next insertion after the setting is changed.

This removes the cacheSize int parameter from NewTableStatisticsCache
along with the SQLConfig.TableStatCacheSize field and associated
plumbing through server_controller_new_server.go.

Informs: #54030

Release note (sql change): The new cluster setting
sql.stats.table_statistics_cache.capacity controls the maximum number
of tables whose statistics are retained in the in-memory LRU cache
(default: 256).

Co-Authored-By: roachdev-claude roachdev-claude-bot@cockroachlabs.com

stats: add memory monitoring to table statistics cache

Add a mon.BytesMonitor to the table statistics cache, tracking memory
usage under the server-cache-monroot SQL memory pool hierarchy.
Each cache entry's memory footprint is estimated when populated
(accounting for histogram buckets, datum sizes, column IDs, and string
fields) and tracked via a mon.BoundAccount.

When a Grow or Resize call fails (e.g. because the SQL memory pool
is exhausted), LRU entries are evicted until the cumulative freed memory
exceeds the new entry's size, ensuring a net decrease even under
concurrent memory pressure. If eviction is insufficient, the new entry
is removed — callers who were already waiting still receive the stats,
but the entry won't be retained for future lookups. On refresh the size
delta between the old and new stats is reserved or released.

A new LRUEntry method is added to cache.UnorderedCache to support
the eviction logic.

Fixes: #54030

Release note: None

Co-Authored-By: roachdev-claude roachdev-claude-bot@cockroachlabs.com


Fixes ENGREQ-437

Release justification: scalability improvement requested via ENGREQ

DrewKimball and others added 2 commits April 28, 2026 11:14
Replace the hardcoded table statistics cache size (previously passed
through server config plumbing as `defaultSQLTableStatCacheSize = 256`)
with a cluster setting `sql.stats.table_statistics_cache.capacity`. The
`ShouldEvict` callback now reads the setting dynamically, so the cache
resizes on the next insertion after the setting is changed.

This removes the `cacheSize int` parameter from `NewTableStatisticsCache`
along with the `SQLConfig.TableStatCacheSize` field and associated
plumbing through `server_controller_new_server.go`.

Informs: cockroachdb#54030

Release note (sql change): The new cluster setting
`sql.stats.table_statistics_cache.capacity` controls the maximum number
of tables whose statistics are retained in the in-memory LRU cache
(default: 256).

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
Add a `mon.BytesMonitor` to the table statistics cache, tracking memory
usage under the `server-cache-mon` → `root` SQL memory pool hierarchy.
Each cache entry's memory footprint is estimated when populated
(accounting for histogram buckets, datum sizes, column IDs, and string
fields) and tracked via a `mon.BoundAccount`.

When a `Grow` or `Resize` call fails (e.g. because the SQL memory pool
is exhausted), LRU entries are evicted until the cumulative freed memory
exceeds the new entry's size, ensuring a net decrease even under
concurrent memory pressure. If eviction is insufficient, the new entry
is removed — callers who were already waiting still receive the stats,
but the entry won't be retained for future lookups. On refresh the size
delta between the old and new stats is reserved or released.

A new `LRUEntry` method is added to `cache.UnorderedCache` to support
the eviction logic.

Fixes: cockroachdb#54030

Release note: None

Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
@DrewKimball DrewKimball requested a review from mw5h April 28, 2026 16:17
@DrewKimball DrewKimball requested review from a team as code owners April 28, 2026 16:17
@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io Bot commented Apr 28, 2026

😎 Merged successfully - details.

@blathers-crl
Copy link
Copy Markdown

blathers-crl Bot commented Apr 28, 2026

Thanks for opening a backport.

Before merging, please confirm that the change does not break backwards compatibility and otherwise complies with the backport policy. Include a brief release justification in the PR description explaining why the backport is appropriate. All backports must be reviewed by the TL for the owning area. While the stricter LTS policy does not yet apply, please exercise judgment and consider gating non-critical changes behind a disabled-by-default feature flag when appropriate.

@blathers-crl blathers-crl Bot added backport Label PR's that are backports to older release branches T-sql-queries SQL Queries Team labels Apr 28, 2026
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

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

LGTM

@DrewKimball
Copy link
Copy Markdown
Collaborator Author

TFTR!

/trunk merge

@trunk-io trunk-io Bot merged commit aa3ad39 into cockroachdb:release-26.2 Apr 28, 2026
22 checks passed
@DrewKimball DrewKimball deleted the backport26.2-168711 branch April 29, 2026 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport Label PR's that are backports to older release branches T-sql-queries SQL Queries Team target-release-26.2.1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants