statistics: skip non-exicted table when to init stats (#58381)

close pingcap/tidb#58378
This commit is contained in:
Weizhen Wang
2024-12-18 20:43:22 +08:00
committed by GitHub
parent 80b34784bf
commit f05cbddfd3
3 changed files with 50 additions and 1 deletions

View File

@ -164,6 +164,12 @@ func (*Handle) initStatsHistograms4ChunkLite(cache statstypes.StatsCache, iter *
}
func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache statstypes.StatsCache, iter *chunk.Iterator4Chunk, isCacheFull bool) {
defer func() {
if r := recover(); r != nil {
logutil.BgLogger().Error("panic when initStatsHistograms4Chunk", zap.Any("r", r),
zap.Stack("stack"))
}
}()
var table *statistics.Table
for row := iter.Begin(); row != iter.End(); row = iter.Next() {
tblID, statsVer := row.GetInt64(0), row.GetInt64(8)
@ -185,7 +191,12 @@ func (h *Handle) initStatsHistograms4Chunk(is infoschema.InfoSchema, cache stats
}
id, ndv, nullCount, version, totColSize := row.GetInt64(2), row.GetInt64(3), row.GetInt64(5), row.GetUint64(4), row.GetInt64(7)
lastAnalyzePos := row.GetDatum(11, types.NewFieldType(mysql.TypeBlob))
tbl, _ := h.TableInfoByID(is, table.PhysicalID)
tbl, ok := h.TableInfoByID(is, table.PhysicalID)
if !ok {
// this table has been dropped. but stats meta still exists and wait for being deleted.
logutil.BgLogger().Warn("cannot find this table when to init stats", zap.Int64("tableID", table.PhysicalID))
continue
}
if row.GetInt64(1) > 0 {
var idxInfo *model.IndexInfo
for _, idx := range tbl.Meta().Indices {

View File

@ -8,6 +8,7 @@ go_test(
"main_test.go",
],
flaky = True,
shard_count = 4,
deps = [
"//pkg/config",
"//pkg/parser/model",

View File

@ -102,3 +102,40 @@ func testConcurrentlyInitStats(t *testing.T) {
}
require.Equal(t, int64(126), handle.GetMaxTidRecordForTest())
}
func TestDropTableBeforeConcurrentlyInitStats(t *testing.T) {
restore := config.RestoreFunc()
defer restore()
config.UpdateGlobal(func(conf *config.Config) {
conf.Performance.LiteInitStats = false
conf.Performance.ConcurrentlyInitStats = true
})
testDropTableBeforeInitStats(t)
}
func TestDropTableBeforeNonLiteInitStats(t *testing.T) {
restore := config.RestoreFunc()
defer restore()
config.UpdateGlobal(func(conf *config.Config) {
conf.Performance.LiteInitStats = false
conf.Performance.ConcurrentlyInitStats = false
})
testDropTableBeforeInitStats(t)
}
func testDropTableBeforeInitStats(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test;")
tk.MustExec("create table t( id int, a int, b int, index idx(id, a));")
tk.MustExec("insert into t values (1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5);")
tk.MustExec("insert into t select * from t where id<>2;")
tk.MustExec("insert into t select * from t where id<>2;")
tk.MustExec("insert into t select * from t where id<>2;")
tk.MustExec("insert into t select * from t where id<>2;")
tk.MustExec("analyze table t all columns;")
tk.MustExec("drop table t")
h := dom.StatsHandle()
is := dom.InfoSchema()
require.NoError(t, h.InitStats(context.Background(), is))
}