-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tidb_enable_async_merge_global_stats
cannot work correctly
#53972
Labels
affects-7.1
affects-7.5
affects-8.1
severity/major
sig/planner
SIG: Planner
type/bug
The issue is confirmed as a bug.
Comments
ti-chi-bot
bot
added
may-affects-5.4
This bug maybe affects 5.4.x versions.
may-affects-6.1
may-affects-6.5
may-affects-7.1
may-affects-7.5
may-affects-8.1
labels
Jun 12, 2024
Rustin170506
added
affects-7.5
affects-8.1
and removed
may-affects-5.4
This bug maybe affects 5.4.x versions.
may-affects-6.1
may-affects-6.5
may-affects-7.1
may-affects-7.5
may-affects-8.1
labels
Jun 12, 2024
|
A quick fix: diff --git a/pkg/executor/analyze_global_stats.go b/pkg/executor/analyze_global_stats.go
index 59d0163852..5e5e65efbc 100644
--- a/pkg/executor/analyze_global_stats.go
+++ b/pkg/executor/analyze_global_stats.go
@@ -76,8 +76,8 @@ func (e *AnalyzeExec) handleGlobalStats(ctx context.Context, globalStatsMap glob
}
}
globalStatsI, err := statsHandle.MergePartitionStats2GlobalStatsByTableID(
- e.Ctx(),
- globalOpts, e.Ctx().GetInfoSchema().(infoschema.InfoSchema),
+ globalOpts,
+ e.Ctx().GetInfoSchema().(infoschema.InfoSchema),
globalStatsID.tableID,
info.isIndex == 1,
info.histIDs,
diff --git a/pkg/statistics/handle/globalstats/global_stats.go b/pkg/statistics/handle/globalstats/global_stats.go
index 78ee277659..2a180d8add 100644
--- a/pkg/statistics/handle/globalstats/global_stats.go
+++ b/pkg/statistics/handle/globalstats/global_stats.go
@@ -47,13 +47,25 @@ func NewStatsGlobal(statsHandler util.StatsHandle) util.StatsGlobal {
}
// MergePartitionStats2GlobalStatsByTableID merge the partition-level stats to global-level stats based on the tableID.
-func (sg *statsGlobalImpl) MergePartitionStats2GlobalStatsByTableID(sc sessionctx.Context,
- opts map[ast.AnalyzeOptionType]uint64, is infoschema.InfoSchema,
+func (sg *statsGlobalImpl) MergePartitionStats2GlobalStatsByTableID(
+ opts map[ast.AnalyzeOptionType]uint64,
+ is infoschema.InfoSchema,
physicalID int64,
isIndex bool,
histIDs []int64,
) (globalStats interface{}, err error) {
- return MergePartitionStats2GlobalStatsByTableID(sc, sg.statsHandler, opts, is, physicalID, isIndex, histIDs)
+ err = util.CallWithSCtx(sg.statsHandler.SPool(), func(sctx sessionctx.Context) error {
+ g, err := MergePartitionStats2GlobalStatsByTableID(sctx, sg.statsHandler, opts, is, physicalID, isIndex, histIDs)
+ if err != nil {
+ return err
+ }
+ globalStats = g
+ return nil
+ })
+ if err != nil {
+ return nil, err
+ }
+ return
}
// UpdateGlobalStats will trigger the merge of global-stats when we drop table partition
@@ -103,6 +115,7 @@ func MergePartitionStats2GlobalStats(
histIDs []int64,
) (globalStats *GlobalStats, err error) {
if sc.GetSessionVars().EnableAsyncMergeGlobalStats {
+ logutil.BgLogger().Info("use async merge global stats", zap.String("table", globalTableInfo.Name.L))
worker, err := NewAsyncMergePartitionStats2GlobalStats(statsHandle, globalTableInfo, histIDs, is)
if err != nil {
return nil, errors.Trace(err)
@@ -113,6 +126,7 @@ func MergePartitionStats2GlobalStats(
}
return worker.Result(), nil
}
+ logutil.BgLogger().Info("use blocking merge global stats", zap.String("table", globalTableInfo.Name.L))
return blockingMergePartitionStats2GlobalStats(sc, statsHandle.GPool(), opts, is, globalTableInfo, isIndex, histIDs, nil, statsHandle)
}
diff --git a/pkg/statistics/handle/util/interfaces.go b/pkg/statistics/handle/util/interfaces.go
index 6086a330a2..a062b973f8 100644
--- a/pkg/statistics/handle/util/interfaces.go
+++ b/pkg/statistics/handle/util/interfaces.go
@@ -300,8 +300,9 @@ type StatsReadWriter interface {
// StatsGlobal is used to manage partition table global stats.
type StatsGlobal interface {
// MergePartitionStats2GlobalStatsByTableID merges partition stats to global stats by table ID.
- MergePartitionStats2GlobalStatsByTableID(sctx sessionctx.Context,
- opts map[ast.AnalyzeOptionType]uint64, is infoschema.InfoSchema,
+ MergePartitionStats2GlobalStatsByTableID(
+ opts map[ast.AnalyzeOptionType]uint64,
+ is infoschema.InfoSchema,
physicalID int64,
isIndex bool,
histIDs []int64,
diff --git a/pkg/statistics/handle/util/util.go b/pkg/statistics/handle/util/util.go
index 61adcf8e21..f77c5fb197 100644
--- a/pkg/statistics/handle/util/util.go
+++ b/pkg/statistics/handle/util/util.go
@@ -114,6 +114,12 @@ func CallWithSCtx(pool SessionPool, f func(sctx sessionctx.Context) error, flags
// UpdateSCtxVarsForStats updates all necessary variables that may affect the behavior of statistics.
func UpdateSCtxVarsForStats(sctx sessionctx.Context) error {
+ // async
+ enableAsyncMergeGlobalStats, err := sctx.GetSessionVars().GlobalVarsAccessor.GetGlobalSysVar(variable.TiDBEnableAsyncMergeGlobalStats)
+ if err != nil {
+ return err
+ }
+ sctx.GetSessionVars().EnableAsyncMergeGlobalStats = variable.TiDBOptOn(enableAsyncMergeGlobalStats)
// analyzer version
verInString, err := sctx.GetSessionVars().GlobalVarsAccessor.GetGlobalSysVar(variable.TiDBAnalyzeVersion)
if err != nil { But I don't know why it cannot work with the old approach. |
#45202 (comment) This is why. // HandleAutoAnalyze analyzes the newly created table or index.
func (sa *statsAnalyze) HandleAutoAnalyze(is infoschema.InfoSchema) (analyzed bool) {
_ = statsutil.CallWithSCtx(sa.statsHandle.SPool(), func(sctx sessionctx.Context) error {
enabled := sctx.GetSessionVars().EnableAsyncMergeGlobalStats
logutil.BgLogger().Info("EnableAsyncMergeGlobalStats", zap.Bool("EnableAsyncMergeGlobalStats", enabled))
analyzed = HandleAutoAnalyze(sctx, sa.statsHandle, is)
return nil
})
return
} We used this ctx to execute the auto-analyze, so we missed this variable. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
affects-7.1
affects-7.5
affects-8.1
severity/major
sig/planner
SIG: Planner
type/bug
The issue is confirmed as a bug.
Bug Report
Please answer these questions before submitting your issue. Thanks!
1. Minimal reproduce step (Required)
2. What did you expect to see? (Required)
3. What did you see instead (Required)
4. What is your TiDB version? (Required)
v7.5.1 with patch
The text was updated successfully, but these errors were encountered: