Skip to content

Commit

Permalink
Merge pull request #125473 from cockroachdb/blathers/backport-release…
Browse files Browse the repository at this point in the history
…-23.1.23-rc-124855

release-23.1.23-rc: sql/stats: evict stats cache entry if user-defined types have changed
  • Loading branch information
michae2 authored Jun 11, 2024
2 parents 53f8438 + 1c8eef6 commit 8705c1e
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 28 deletions.
127 changes: 127 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/multi_region_stats
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# LogicTest: multiregion-9node-3region-3azs

query TTTTT colnames,rowsort
SHOW REGIONS
----
region zones database_names primary_region_of secondary_region_of
ap-southeast-2 {ap-az1,ap-az2,ap-az3} {} {} {}
ca-central-1 {ca-az1,ca-az2,ca-az3} {} {} {}
us-east-1 {us-az1,us-az2,us-az3} {} {} {}

query TT colnames,rowsort
SHOW REGIONS FROM CLUSTER
----
region zones
ap-southeast-2 {ap-az1,ap-az2,ap-az3}
ca-central-1 {ca-az1,ca-az2,ca-az3}
us-east-1 {us-az1,us-az2,us-az3}

# Regression test for #124181: check that we re-load table statistics after
# running ALTER DATABASE ADD REGION.

statement ok
CREATE DATABASE db124181 PRIMARY REGION "ap-southeast-2" REGIONS "us-east-1" SURVIVE ZONE FAILURE

statement ok
USE db124181

query TTTT
SHOW ENUMS
----
public crdb_internal_region {ap-southeast-2,us-east-1} root

statement ok
CREATE TABLE t124181 (
region crdb_internal_region NOT NULL,
id UUID NOT NULL DEFAULT gen_random_uuid(),
a INT NOT NULL,
PRIMARY KEY (id),
UNIQUE INDEX (a)
) LOCALITY REGIONAL BY ROW AS region

statement ok
INSERT INTO t124181 (region, a) VALUES ('ap-southeast-2', 0), ('us-east-1', 1)

statement ok
ANALYZE t124181

let $hist_id_1
SELECT histogram_id FROM [SHOW STATISTICS FOR TABLE t124181] WHERE column_names = ARRAY['region']

query TIRI colnames,nosort
SHOW HISTOGRAM $hist_id_1
----
upper_bound range_rows distinct_range_rows equal_rows
'ap-southeast-2' 0 0 1
'us-east-1' 0 0 1

query T
SELECT jsonb_pretty(stat->'histo_buckets')
FROM (
SELECT jsonb_array_elements(statistics) AS stat
FROM [SHOW STATISTICS USING JSON FOR TABLE t124181]
)
WHERE stat->>'columns' = '["region"]'
----
[
{
"distinct_range": 0,
"num_eq": 1,
"num_range": 0,
"upper_bound": "ap-southeast-2"
},
{
"distinct_range": 0,
"num_eq": 1,
"num_range": 0,
"upper_bound": "us-east-1"
}
]

# Implicitly add a value to the crdb_internal_region enum.
statement ok
ALTER DATABASE db124181 ADD REGION "ca-central-1"

query TTTT
SHOW ENUMS
----
public crdb_internal_region {ap-southeast-2,ca-central-1,us-east-1} root

# Make sure we can still SHOW STATISTICS and SHOW HISTOGRAM.
let $hist_id_2
SELECT histogram_id FROM [SHOW STATISTICS FOR TABLE t124181] WHERE column_names = ARRAY['region']

query TIRI colnames,nosort
SHOW HISTOGRAM $hist_id_2
----
upper_bound range_rows distinct_range_rows equal_rows
'ap-southeast-2' 0 0 1
'us-east-1' 0 0 1

# Make sure we can still SHOW STATISTICS USING JSON.
query T
SELECT jsonb_pretty(stat->'histo_buckets')
FROM (
SELECT jsonb_array_elements(statistics) AS stat
FROM [SHOW STATISTICS USING JSON FOR TABLE t124181]
)
WHERE stat->>'columns' = '["region"]'
----
[
{
"distinct_range": 0,
"num_eq": 1,
"num_range": 0,
"upper_bound": "ap-southeast-2"
},
{
"distinct_range": 0,
"num_eq": 1,
"num_range": 0,
"upper_bound": "us-east-1"
}
]

# Make sure we can still use the histogram in statistics_builder.
statement ok
INSERT INTO t124181 (region, a) VALUES ('ca-central-1', 2)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/sql/stats/automatic_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ func (r *Refresher) maybeRefreshStats(
rowsAffected int64,
asOf time.Duration,
) {
tableStats, err := r.cache.getTableStatsFromCache(ctx, tableID, nil /* forecast */)
tableStats, err := r.cache.getTableStatsFromCache(ctx, tableID, nil /* forecast */, nil /* udtCols */)
if err != nil {
log.Errorf(ctx, "failed to get table statistics: %v", err)
return
Expand Down
16 changes: 12 additions & 4 deletions pkg/sql/stats/delete_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,19 @@ func TestDeleteOldStatsForColumns(t *testing.T) {
}

return testutils.SucceedsSoonError(func() error {
tableStats, err := cache.getTableStatsFromCache(ctx, tableID, nil /* forecast */)
tableStats, err := cache.getTableStatsFromCache(
ctx, tableID, nil /* forecast */, nil, /* udtCols */
)
if err != nil {
return err
}

for i := range testData {
stat := &testData[i]
if stat.TableID != tableID {
stats, err := cache.getTableStatsFromCache(ctx, stat.TableID, nil /* forecast */)
stats, err := cache.getTableStatsFromCache(
ctx, stat.TableID, nil /* forecast */, nil, /* udtCols */
)
if err != nil {
return err
}
Expand Down Expand Up @@ -554,15 +558,19 @@ func TestDeleteOldStatsForOtherColumns(t *testing.T) {
}

return testutils.SucceedsSoonError(func() error {
tableStats, err := cache.getTableStatsFromCache(ctx, tableID, nil /* forecast */)
tableStats, err := cache.getTableStatsFromCache(
ctx, tableID, nil /* forecast */, nil, /* udtCols */
)
if err != nil {
return err
}

for i := range testData {
stat := &testData[i]
if stat.TableID != tableID {
stats, err := cache.getTableStatsFromCache(ctx, stat.TableID, nil /* forecast */)
stats, err := cache.getTableStatsFromCache(
ctx, stat.TableID, nil /* forecast */, nil, /* udtCols */
)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 8705c1e

Please sign in to comment.