Skip to content

Commit 2714f6c

Browse files
release-25.2: sql/stats: make table statistics cache size configurable (#169264)
release-25.2: sql/stats: make table statistics cache size configurable
2 parents 8ba05e7 + b4cc0ba commit 2714f6c

12 files changed

Lines changed: 618 additions & 29 deletions

File tree

docs/generated/settings/settings-for-tenants.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ sql.stats.response.max integer 20000 the maximum number of statements and transa
378378
sql.stats.response.show_internal.enabled boolean false controls if statistics for internal executions should be returned by the CombinedStatements and if internal sessions should be returned by the ListSessions endpoints. These endpoints are used to display statistics on the SQL Activity pages application
379379
sql.stats.system_tables.enabled boolean true when true, enables use of statistics on system tables by the query optimizer application
380380
sql.stats.system_tables_autostats.enabled boolean true when true, enables automatic collection of statistics on system tables application
381+
sql.stats.table_statistics_cache.capacity integer 256 the maximum number of table statistics entries stored in the LRU cache. Each cache entry corresponds to a single table. application
381382
sql.stats.virtual_computed_columns.enabled boolean true set to true to collect table statistics on virtual computed columns application
382383
sql.telemetry.query_sampling.enabled boolean false when set to true, executed queries will emit an event on the telemetry logging channel application
383384
sql.telemetry.query_sampling.internal.enabled boolean false when set to true, internal queries will be sampled in telemetry logging application

docs/generated/settings/settings.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@
333333
<tr><td><div id="setting-sql-stats-response-show-internal-enabled" class="anchored"><code>sql.stats.response.show_internal.enabled</code></div></td><td>boolean</td><td><code>false</code></td><td>controls if statistics for internal executions should be returned by the CombinedStatements and if internal sessions should be returned by the ListSessions endpoints. These endpoints are used to display statistics on the SQL Activity pages</td><td>Serverless/Dedicated/Self-Hosted</td></tr>
334334
<tr><td><div id="setting-sql-stats-system-tables-enabled" class="anchored"><code>sql.stats.system_tables.enabled</code></div></td><td>boolean</td><td><code>true</code></td><td>when true, enables use of statistics on system tables by the query optimizer</td><td>Serverless/Dedicated/Self-Hosted</td></tr>
335335
<tr><td><div id="setting-sql-stats-system-tables-autostats-enabled" class="anchored"><code>sql.stats.system_tables_autostats.enabled</code></div></td><td>boolean</td><td><code>true</code></td><td>when true, enables automatic collection of statistics on system tables</td><td>Serverless/Dedicated/Self-Hosted</td></tr>
336+
<tr><td><div id="setting-sql-stats-table-statistics-cache-capacity" class="anchored"><code>sql.stats.table_statistics_cache.capacity</code></div></td><td>integer</td><td><code>256</code></td><td>the maximum number of table statistics entries stored in the LRU cache. Each cache entry corresponds to a single table.</td><td>Serverless/Dedicated/Self-Hosted</td></tr>
336337
<tr><td><div id="setting-sql-stats-virtual-computed-columns-enabled" class="anchored"><code>sql.stats.virtual_computed_columns.enabled</code></div></td><td>boolean</td><td><code>true</code></td><td>set to true to collect table statistics on virtual computed columns</td><td>Serverless/Dedicated/Self-Hosted</td></tr>
337338
<tr><td><div id="setting-sql-telemetry-query-sampling-enabled" class="anchored"><code>sql.telemetry.query_sampling.enabled</code></div></td><td>boolean</td><td><code>false</code></td><td>when set to true, executed queries will emit an event on the telemetry logging channel</td><td>Serverless/Dedicated/Self-Hosted</td></tr>
338339
<tr><td><div id="setting-sql-telemetry-query-sampling-internal-enabled" class="anchored"><code>sql.telemetry.query_sampling.internal.enabled</code></div></td><td>boolean</td><td><code>false</code></td><td>when set to true, internal queries will be sampled in telemetry logging</td><td>Serverless/Dedicated/Self-Hosted</td></tr>

pkg/server/config.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ const (
8787
minimumNetworkFileDescriptors = 256
8888
recommendedNetworkFileDescriptors = 5000
8989

90-
defaultSQLTableStatCacheSize = 256
91-
9290
// This comes out to 1024 cache entries.
9391
defaultSQLQueryCacheSize = 8 * 1024 * 1024
9492
)
@@ -500,10 +498,6 @@ type SQLConfig struct {
500498
// used by SQL clients to store row data in server RAM.
501499
MemoryPoolSize int64
502500

503-
// TableStatCacheSize is the size (number of tables) of the table
504-
// statistics cache.
505-
TableStatCacheSize int
506-
507501
// QueryCacheSize is the memory size (in bytes) of the query plan cache.
508502
QueryCacheSize int64
509503

@@ -570,7 +564,6 @@ func (sqlCfg *SQLConfig) SetDefaults(tempStorageCfg base.TempStorageConfig) {
570564
tenName := sqlCfg.TenantName
571565
*sqlCfg = SQLConfig{TenantID: tenID, TenantName: tenName}
572566
sqlCfg.MemoryPoolSize = defaultSQLMemoryPoolSize
573-
sqlCfg.TableStatCacheSize = defaultSQLTableStatCacheSize
574567
sqlCfg.QueryCacheSize = defaultSQLQueryCacheSize
575568
sqlCfg.TempStorageConfig = tempStorageCfg
576569
sqlCfg.LicenseEnforcer = license.NewEnforcer(nil)

pkg/server/server_controller_new_server.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ func makeSharedProcessTenantServerConfig(
354354
// of them will be mostly idle.
355355
// We might want to reconsider this if we use more than 1 in-memory tenant at a time.
356356
sqlCfg.MemoryPoolSize = kvServerCfg.SQLConfig.MemoryPoolSize
357-
sqlCfg.TableStatCacheSize = kvServerCfg.SQLConfig.TableStatCacheSize
358357
sqlCfg.QueryCacheSize = kvServerCfg.SQLConfig.QueryCacheSize
359358

360359
// LocalKVServerInfo tells the rpc.Context of the tenant's server

pkg/server/server_sql.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,10 +1039,11 @@ func newSQLServer(ctx context.Context, cfg sqlServerArgs) (*SQLServer, error) {
10391039
),
10401040

10411041
TableStatsCache: stats.NewTableStatisticsCache(
1042-
cfg.TableStatCacheSize,
1042+
ctx,
10431043
cfg.Settings,
10441044
cfg.internalDB,
10451045
cfg.stopper,
1046+
serverCacheMemoryMonitor,
10461047
),
10471048

10481049
QueryCache: querycache.New(cfg.QueryCacheSize),

pkg/sql/logictest/testdata/logic_test/stats

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,32 @@ query II
122122
SELECT row_count, distinct_count FROM [SHOW STATISTICS FOR TABLE t141448] WHERE column_names = ARRAY['b'];
123123
----
124124
3 1
125+
126+
subtest table_stats_cache_settings
127+
128+
# Verify the table statistics cache capacity setting.
129+
query I
130+
SHOW CLUSTER SETTING sql.stats.table_statistics_cache.capacity
131+
----
132+
256
133+
134+
statement ok
135+
SET CLUSTER SETTING sql.stats.table_statistics_cache.capacity = 128
136+
137+
query I
138+
SHOW CLUSTER SETTING sql.stats.table_statistics_cache.capacity
139+
----
140+
128
141+
142+
statement ok
143+
RESET CLUSTER SETTING sql.stats.table_statistics_cache.capacity
144+
145+
query I
146+
SHOW CLUSTER SETTING sql.stats.table_statistics_cache.capacity
147+
----
148+
256
149+
150+
statement error cannot be set to a negative value
151+
SET CLUSTER SETTING sql.stats.table_statistics_cache.capacity = -1
152+
153+
subtest end

pkg/sql/stats/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ go_test(
138138
"//pkg/util/protoutil",
139139
"//pkg/util/randutil",
140140
"//pkg/util/retry",
141+
"//pkg/util/stop",
141142
"//pkg/util/timeutil",
142143
"//pkg/util/timeutil/pgdate",
143144
"@com_github_cockroachdb_errors//:errors",

pkg/sql/stats/automatic_stats_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ func TestMaybeRefreshStats(t *testing.T) {
6767
internalDB := s.InternalDB().(descs.DB)
6868
descA := desctestutils.TestingGetPublicTableDescriptor(s.DB(), codec, "t", "a")
6969
cache := NewTableStatisticsCache(
70-
10, /* cacheSize */
70+
ctx,
7171
s.ClusterSettings(),
7272
s.InternalDB().(descs.DB),
7373
s.AppStopper(),
74+
nil, /* parentMon */
7475
)
7576
require.NoError(t, cache.Start(ctx, codec, s.RangeFeedFactory().(*rangefeed.Factory)))
7677
refresher := MakeRefresher(s.AmbientCtx(), st, internalDB, cache, time.Microsecond /* asOfTime */, nil /* knobs */, false /* readOnlyTenant */)
@@ -229,10 +230,11 @@ func TestEnsureAllTablesQueries(t *testing.T) {
229230

230231
internalDB := s.InternalDB().(descs.DB)
231232
cache := NewTableStatisticsCache(
232-
10, /* cacheSize */
233+
ctx,
233234
s.ClusterSettings(),
234235
s.InternalDB().(descs.DB),
235236
s.AppStopper(),
237+
nil, /* parentMon */
236238
)
237239
require.NoError(t, cache.Start(ctx, codec, s.RangeFeedFactory().(*rangefeed.Factory)))
238240
r := MakeRefresher(s.AmbientCtx(), st, internalDB, cache, time.Microsecond /* asOfTime */, nil /* knobs */, false /* readOnlyTenant */)
@@ -332,10 +334,11 @@ func BenchmarkEnsureAllTables(b *testing.B) {
332334

333335
internalDB := s.InternalDB().(descs.DB)
334336
cache := NewTableStatisticsCache(
335-
10, /* cacheSize */
337+
ctx,
336338
s.ClusterSettings(),
337339
s.InternalDB().(descs.DB),
338340
s.AppStopper(),
341+
nil, /* parentMon */
339342
)
340343
require.NoError(b, cache.Start(ctx, codec, s.RangeFeedFactory().(*rangefeed.Factory)))
341344
r := MakeRefresher(s.AmbientCtx(), st, internalDB, cache, time.Microsecond /* asOfTime */, nil /* knobs */, false /* readOnlyTenant */)
@@ -406,10 +409,11 @@ func TestAverageRefreshTime(t *testing.T) {
406409
internalDB := s.InternalDB().(descs.DB)
407410
table := desctestutils.TestingGetPublicTableDescriptor(s.DB(), codec, "t", "a")
408411
cache := NewTableStatisticsCache(
409-
10, /* cacheSize */
412+
ctx,
410413
s.ClusterSettings(),
411414
s.InternalDB().(descs.DB),
412415
s.AppStopper(),
416+
nil, /* parentMon */
413417
)
414418
require.NoError(t, cache.Start(ctx, codec, s.RangeFeedFactory().(*rangefeed.Factory)))
415419
refresher := MakeRefresher(s.AmbientCtx(), st, internalDB, cache, time.Microsecond /* asOfTime */, nil /* knobs */, false /* readOnlyTenant */)
@@ -656,10 +660,11 @@ func TestAutoStatsReadOnlyTables(t *testing.T) {
656660

657661
internalDB := s.InternalDB().(descs.DB)
658662
cache := NewTableStatisticsCache(
659-
10, /* cacheSize */
663+
ctx,
660664
s.ClusterSettings(),
661665
s.InternalDB().(descs.DB),
662666
s.AppStopper(),
667+
nil, /* parentMon */
663668
)
664669
require.NoError(t, cache.Start(ctx, codec, s.RangeFeedFactory().(*rangefeed.Factory)))
665670
refresher := MakeRefresher(s.AmbientCtx(), st, internalDB, cache, time.Microsecond /* asOfTime */, nil /* knobs */, false /* readOnlyTenant */)
@@ -712,10 +717,11 @@ func TestAutoStatsOnStartupClusterSettingOff(t *testing.T) {
712717

713718
internalDB := s.InternalDB().(descs.DB)
714719
cache := NewTableStatisticsCache(
715-
10, /* cacheSize */
720+
ctx,
716721
s.ClusterSettings(),
717722
s.InternalDB().(descs.DB),
718723
s.AppStopper(),
724+
nil, /* parentMon */
719725
)
720726
require.NoError(t, cache.Start(ctx, codec, s.RangeFeedFactory().(*rangefeed.Factory)))
721727
refresher := MakeRefresher(s.AmbientCtx(), st, internalDB, cache, time.Microsecond /* asOfTime */, nil /* knobs */, false /* readOnlyTenant */)
@@ -760,10 +766,11 @@ func TestNoRetryOnFailure(t *testing.T) {
760766

761767
internalDB := s.InternalDB().(descs.DB)
762768
cache := NewTableStatisticsCache(
763-
10, /* cacheSize */
769+
ctx,
764770
s.ClusterSettings(),
765771
s.InternalDB().(descs.DB),
766772
s.AppStopper(),
773+
nil, /* parentMon */
767774
)
768775
require.NoError(t, cache.Start(ctx, codec, s.RangeFeedFactory().(*rangefeed.Factory)))
769776
r := MakeRefresher(s.AmbientCtx(), st, internalDB, cache, time.Microsecond /* asOfTime */, nil /* knobs */, false /* readOnlyTenant */)
@@ -878,10 +885,11 @@ func TestAnalyzeSystemTables(t *testing.T) {
878885
defer evalCtx.Stop(ctx)
879886
executor := s.InternalExecutor().(isql.Executor)
880887
cache := NewTableStatisticsCache(
881-
10, /* cacheSize */
888+
ctx,
882889
s.ClusterSettings(),
883890
s.InternalDB().(descs.DB),
884891
s.AppStopper(),
892+
nil, /* parentMon */
885893
)
886894
require.NoError(t, cache.Start(ctx, codec, s.RangeFeedFactory().(*rangefeed.Factory)))
887895

@@ -999,10 +1007,11 @@ func TestAutoStatsDisabledReadOnlyTenant(t *testing.T) {
9991007
internalDB := s.InternalDB().(descs.DB)
10001008
descA := desctestutils.TestingGetPublicTableDescriptor(s.DB(), codec, "t", "a")
10011009
cache := NewTableStatisticsCache(
1002-
10, /* cacheSize */
1010+
ctx,
10031011
s.ClusterSettings(),
10041012
s.InternalDB().(descs.DB),
10051013
s.AppStopper(),
1014+
nil, /* parentMon */
10061015
)
10071016
require.NoError(t, cache.Start(ctx, codec, s.RangeFeedFactory().(*rangefeed.Factory)))
10081017
refresher := MakeRefresher(s.AmbientCtx(), st, internalDB, cache,

pkg/sql/stats/delete_stats_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ func TestDeleteOldStatsForColumns(t *testing.T) {
4040
s := srv.ApplicationLayer()
4141
db := s.InternalDB().(descs.DB)
4242
cache := NewTableStatisticsCache(
43-
10, /* cacheSize */
43+
ctx,
4444
s.ClusterSettings(),
4545
db,
4646
s.AppStopper(),
47+
nil, /* parentMon */
4748
)
4849
require.NoError(t, cache.Start(ctx, s.Codec(), s.RangeFeedFactory().(*rangefeed.Factory)))
4950

@@ -341,10 +342,11 @@ func TestDeleteOldStatsForOtherColumns(t *testing.T) {
341342
s := srv.ApplicationLayer()
342343
db := s.InternalDB().(isql.DB)
343344
cache := NewTableStatisticsCache(
344-
10, /* cacheSize */
345+
ctx,
345346
s.ClusterSettings(),
346347
s.InternalDB().(descs.DB),
347348
s.AppStopper(),
349+
nil, /* parentMon */
348350
)
349351
require.NoError(t, cache.Start(ctx, s.Codec(), s.RangeFeedFactory().(*rangefeed.Factory)))
350352
testData := []TableStatisticProto{

0 commit comments

Comments
 (0)