diff --git a/session/bootstrap.go b/session/bootstrap.go index c3e74ca261889..f34dac109cfda 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -731,7 +731,7 @@ const ( // oom-action when upgrade from v3.0.x to v4.0.11+. tidbDefOOMAction = "default_oom_action" - tiDBStatsGCLastTS = "tidb_stats_gc_last_stats" + tiDBStatsGCLastTS = "tidb_stats_gc_last_ts" tiDBStatsGCLastTSComment = "the previous gc timestamp for statistics" // Const for TiDB server version 2. version2 = 2 @@ -986,7 +986,7 @@ const ( // currentBootstrapVersion is defined as a variable, so we can modify its value for testing. // please make sure this is the largest version -var currentBootstrapVersion int64 = version172 +var currentBootstrapVersion int64 = version173 // DDL owner key's expired time is ManagerSessionTTL seconds, we should wait the time and give more time to have a chance to finish it. var internalSQLTimeout = owner.ManagerSessionTTL + 15 @@ -2801,8 +2801,12 @@ func upgradeToVer173(s Session, ver int64) { if ver >= version173 { return } + writeStatsGCLastPos(s) +} + +func writeStatsGCLastPos(s Session) { mustExecute(s, "INSERT HIGH_PRIORITY INTO %n.%n VALUES(%?, %?, %?) ON DUPLICATE KEY UPDATE VARIABLE_VALUE = %?", - mysql.SystemDB, mysql.TiDBTable, tiDBStatsGCLastTS, 0, tiDBStatsGCLastTSComment) + mysql.SystemDB, mysql.TiDBTable, tiDBStatsGCLastTS, 0, tiDBStatsGCLastTSComment, 0) } func writeOOMAction(s Session) { @@ -3057,6 +3061,8 @@ func doDMLWorks(s Session) { writeStmtSummaryVars(s) + writeStatsGCLastPos(s) + ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap) _, err := s.ExecuteInternal(ctx, "COMMIT") if err != nil { diff --git a/statistics/handle/gc.go b/statistics/handle/gc.go index 406e73f6bc202..72296f66d43ed 100644 --- a/statistics/handle/gc.go +++ b/statistics/handle/gc.go @@ -35,6 +35,8 @@ import ( "go.uber.org/zap" ) +const gcLastTSVarName = "tidb_stats_gc_last_ts" + // GCStats will garbage collect the useless stats info. For dropped tables, we will first update their version so that // other tidb could know that table is deleted. func (h *Handle) GCStats(is infoschema.InfoSchema, ddlLease time.Duration) (err error) { @@ -83,7 +85,7 @@ func (h *Handle) GCStats(is infoschema.InfoSchema, ddlLease time.Duration) (err // GetLastGCTimestamp loads the last gc time from mysql.tidb. func (h *Handle) GetLastGCTimestamp(ctx context.Context) (uint64, error) { - rows, _, err := h.execRestrictedSQL(ctx, "SELECT HIGH_PRIORITY variable_value FROM mysql.tidb WHERE variable_name=%?", "tidb_stats_gc_last_ts") + rows, _, err := h.execRestrictedSQL(ctx, "SELECT HIGH_PRIORITY variable_value FROM mysql.tidb WHERE variable_name=%?", gcLastTSVarName) if err != nil { return 0, errors.Trace(err) } @@ -102,7 +104,7 @@ func (h *Handle) writeGCTimestampToKV(ctx context.Context, newTS uint64) error { _, _, err := h.execRestrictedSQL(ctx, "update mysql.tidb set variable_value = %? where variable_name = %?", newTS, - "tidb_stats_gc_last_ts", + gcLastTSVarName, ) return err }