diff --git a/planner/core/plan_stats.go b/planner/core/plan_stats.go index eaf3cf3120a0f..d5bb4929c6cd7 100644 --- a/planner/core/plan_stats.go +++ b/planner/core/plan_stats.go @@ -36,7 +36,7 @@ func (collectPredicateColumnsPoint) optimize(_ context.Context, plan LogicalPlan return plan, nil } predicateNeeded := variable.EnableColumnTracking.Load() - syncWait := plan.SCtx().GetSessionVars().StatsLoadSyncWait * time.Millisecond.Nanoseconds() + syncWait := plan.SCtx().GetSessionVars().StatsLoadSyncWait.Load() * time.Millisecond.Nanoseconds() histNeeded := syncWait > 0 predicateColumns, histNeededColumns := CollectColumnStatsUsage(plan, predicateNeeded, histNeeded) if len(predicateColumns) > 0 { diff --git a/sessionctx/variable/session.go b/sessionctx/variable/session.go index cc820f2b34d03..6b35461c83c7e 100644 --- a/sessionctx/variable/session.go +++ b/sessionctx/variable/session.go @@ -1196,7 +1196,7 @@ type SessionVars struct { ReadConsistency ReadConsistencyLevel // StatsLoadSyncWait indicates how long to wait for stats load before timeout. - StatsLoadSyncWait int64 + StatsLoadSyncWait atomic.Int64 // SysdateIsNow indicates whether Sysdate is an alias of Now function SysdateIsNow bool @@ -1710,7 +1710,6 @@ func NewSessionVars(hctx HookContext) *SessionVars { TMPTableSize: DefTiDBTmpTableMaxSize, MPPStoreFailTTL: DefTiDBMPPStoreFailTTL, Rng: mathutil.NewWithTime(), - StatsLoadSyncWait: StatsLoadSyncWait.Load(), EnableLegacyInstanceScope: DefEnableLegacyInstanceScope, RemoveOrderbyInSubquery: DefTiDBRemoveOrderbyInSubquery, EnableSkewDistinctAgg: DefTiDBSkewDistinctAgg, @@ -1762,6 +1761,7 @@ func NewSessionVars(hctx HookContext) *SessionVars { vars.DiskTracker = disk.NewTracker(memory.LabelForSession, -1) vars.MemTracker = memory.NewTracker(memory.LabelForSession, vars.MemQuotaQuery) vars.MemTracker.IsRootTrackerOfSess = true + vars.StatsLoadSyncWait.Store(StatsLoadSyncWait.Load()) for _, engine := range config.GetGlobalConfig().IsolationRead.Engines { switch engine { diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index 500ddfb02b19d..ed91f6deaf1ca 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -1943,7 +1943,7 @@ var defaultSysVars = []*SysVar{ }}, {Scope: ScopeGlobal | ScopeSession, Name: TiDBStatsLoadSyncWait, Value: strconv.Itoa(DefTiDBStatsLoadSyncWait), Type: TypeInt, MinValue: 0, MaxValue: math.MaxInt32, SetSession: func(s *SessionVars, val string) error { - s.StatsLoadSyncWait = TidbOptInt64(val, DefTiDBStatsLoadSyncWait) + s.StatsLoadSyncWait.Store(TidbOptInt64(val, DefTiDBStatsLoadSyncWait)) return nil }, GetGlobal: func(_ context.Context, s *SessionVars) (string, error) { diff --git a/statistics/handle/handle_hist.go b/statistics/handle/handle_hist.go index a7063e0563940..0c56812f0647c 100644 --- a/statistics/handle/handle_hist.go +++ b/statistics/handle/handle_hist.go @@ -252,6 +252,7 @@ func (h *Handle) HandleOneTask(lastTask *NeededItemTask, readerCtx *StatsReaderC } return task, result.Err case <-time.After(timeout): + task.ToTimeout.Add(time.Duration(h.mu.ctx.GetSessionVars().StatsLoadSyncWait.Load()) * time.Microsecond) return task, nil } } @@ -435,6 +436,7 @@ func (h *Handle) drainColTask(exit chan struct{}) (*NeededItemTask, error) { // if the task has already timeout, no sql is sync-waiting for it, // so do not handle it just now, put it to another channel with lower priority if time.Now().After(task.ToTimeout) { + task.ToTimeout.Add(time.Duration(h.mu.ctx.GetSessionVars().StatsLoadSyncWait.Load()) * time.Microsecond) h.writeToTimeoutChan(h.StatsLoad.TimeoutItemsCh, task) continue }