Skip to content

Commit

Permalink
Merge pull request #89706 from msirek/89323
Browse files Browse the repository at this point in the history
release-21.2: stats: fix autostats panic with minStaleRows == 0
  • Loading branch information
Mark Sirek authored Oct 11, 2022
2 parents ed55c13 + 11585ce commit 3e274db
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/sql/stats/automatic_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,15 @@ func (r *Refresher) maybeRefreshStats(
mustRefresh = true
}

targetRows := int64(rowCount*AutomaticStatisticsFractionStaleRows.Get(&r.st.SV)) +
AutomaticStatisticsMinStaleRows.Get(&r.st.SV)
if !mustRefresh && rowsAffected < math.MaxInt32 && r.randGen.randInt(targetRows) >= rowsAffected {
statsFractionStaleRows := AutomaticStatisticsFractionStaleRows.Get(&r.st.SV)
statsMinStaleRows := AutomaticStatisticsMinStaleRows.Get(&r.st.SV)
targetRows := int64(rowCount*statsFractionStaleRows) + statsMinStaleRows
// randInt will panic if we pass it a value of 0.
randomTargetRows := int64(0)
if targetRows > 0 {
randomTargetRows = r.randGen.randInt(targetRows)
}
if !mustRefresh && rowsAffected < math.MaxInt32 && randomTargetRows >= rowsAffected {
// No refresh is happening this time.
return
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/sql/stats/automatic_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/stretchr/testify/require"
)

func TestMaybeRefreshStats(t *testing.T) {
Expand Down Expand Up @@ -121,6 +122,50 @@ func TestMaybeRefreshStats(t *testing.T) {
}
}

func TestMaybeRefreshStatsTargetRowsZeroDoesNotPanic(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
ctx := context.Background()

s, sqlDB, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)

st := cluster.MakeTestingClusterSettings()
evalCtx := tree.NewTestingEvalContext(st)
defer evalCtx.Stop(ctx)

AutomaticStatisticsClusterMode.Override(ctx, &st.SV, false)
AutomaticStatisticsMinStaleRows.Override(ctx, &st.SV, 0)

sqlRun := sqlutils.MakeSQLRunner(sqlDB)
sqlRun.Exec(t,
`CREATE DATABASE t;
CREATE TABLE t.a (k INT PRIMARY KEY);
INSERT INTO t.a VALUES (1);
CREATE VIEW t.vw AS SELECT k, k+1 FROM t.a;`)

executor := s.InternalExecutor().(sqlutil.InternalExecutor)
descA := catalogkv.TestingGetTableDescriptor(s.DB(), keys.SystemSQLCodec, "t", "a")
cache := NewTableStatisticsCache(
ctx,
10, /* cacheSize */
kvDB,
executor,
keys.SystemSQLCodec,
s.ClusterSettings(),
s.RangeFeedFactory().(*rangefeed.Factory),
s.CollectionFactory().(*descs.CollectionFactory),
)
refresher := MakeRefresher(st, executor, cache, time.Microsecond /* asOfTime */)

// This should not panic.
require.NotPanics(t, func() {
refresher.maybeRefreshStats(
ctx, s.Stopper(), descA.GetID(), 0 /* rowsAffected */, time.Microsecond, /* asOf */
)
})
}

func TestAverageRefreshTime(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
Expand Down

0 comments on commit 3e274db

Please sign in to comment.