Skip to content

Commit 5f1e802

Browse files
sql: fix flaky TestIndexSplitAndScatterWithStats
The test was flaky because the stats cache could contain a stale entry (populated during INSERT planning) when MaybeSplitIndexSpans ran during CREATE INDEX. Despite its name, GetFreshTableStats returns cached data without checking whether stats have been updated. The async cache refresh triggered by CREATE STATISTICS may not complete in time. Fix by invalidating the stats cache on all nodes after CREATE STATISTICS, forcing a fresh read from the database. Fixes: #170609 Release note: None Co-Authored-By: roachdev-claude <roachdev-claude-bot@cockroachlabs.com>
1 parent 4c9163a commit 5f1e802

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

pkg/sql/index_split_scatter_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/cockroachdb/cockroach/pkg/config"
1616
"github.com/cockroachdb/cockroach/pkg/keys"
1717
"github.com/cockroachdb/cockroach/pkg/sql"
18+
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
1819
"github.com/cockroachdb/cockroach/pkg/testutils"
1920
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
2021
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
@@ -165,6 +166,12 @@ func TestIndexSplitAndScatterWithStats(t *testing.T) {
165166
// Generate statistics for these tables.
166167
if statsExist {
167168
runner.Exec(t, "CREATE STATISTICS st FROM multi_column_split")
169+
// Force a stats cache refresh on all nodes so
170+
// MaybeSplitIndexSpans sees the new stats.
171+
tableID := descpb.ID(sqlutils.QueryTableID(t, cluster.ServerConn(0), "defaultdb", "public", "multi_column_split"))
172+
for i := 0; i < numNodes; i++ {
173+
cluster.Server(i).ExecutorConfig().(sql.ExecutorConfig).TableStatsCache.InvalidateTableStats(ctx, tableID)
174+
}
168175
}
169176
// Next create indexes on both tables.
170177
splitHookEnabled.Store(true)

0 commit comments

Comments
 (0)