From 34a309207e9d230a47ea365fe9de0e713897547a Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Sun, 12 Dec 2021 10:27:30 +0800 Subject: [PATCH 1/2] planner: Introduce a new global variable to control the historical statistics feature Use `tidb_enable_historical_stats` to control the behavior above. Default is OFF. --- sessionctx/variable/sysvar.go | 6 +++++- sessionctx/variable/tidb_vars.go | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/sessionctx/variable/sysvar.go b/sessionctx/variable/sysvar.go index cae6fd4c4aa50..8aa5b5cc7628e 100644 --- a/sessionctx/variable/sysvar.go +++ b/sessionctx/variable/sysvar.go @@ -1205,7 +1205,11 @@ var defaultSysVars = []*SysVar{ {Scope: ScopeSession, Name: PluginDir, Value: "/data/deploy/plugin", GetSession: func(s *SessionVars) (string, error) { return config.GetGlobalConfig().Plugin.Dir, nil }}, - + {Scope: ScopeGlobal, Name: TiDBEnableHistoricalStats, Value: Off, Type: TypeBool, GetGlobal: func(s *SessionVars) (string, error) { + return getTiDBTableValue(s, "tidb_enable_historical_stats", Off) + }, SetGlobal: func(s *SessionVars, val string) error { + return setTiDBTableValue(s, "tidb_enable_historical_stats", val, "Current historical statistics enable status") + }}, /* tikv gc metrics */ {Scope: ScopeGlobal, Name: TiDBGCEnable, Value: On, Type: TypeBool, GetGlobal: func(s *SessionVars) (string, error) { return getTiDBTableValue(s, "tikv_gc_enable", On) diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index 2e38db2699969..1fc4a1b35b996 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -620,6 +620,8 @@ const ( TiDBGCScanLockMode = "tidb_gc_scan_lock_mode" // TiDBEnableEnhancedSecurity restricts SUPER users from certain operations. TiDBEnableEnhancedSecurity = "tidb_enable_enhanced_security" + // TiDBEnableHistoricalStats enables the historical statistics feature (default off) + TiDBEnableHistoricalStats = "tidb_enable_historical_stats" ) // TiDB intentional limits From de5601b12e9d4b3158521fc82507fb9d89e5ba9a Mon Sep 17 00:00:00 2001 From: dongjunduo Date: Mon, 13 Dec 2021 12:24:24 +0800 Subject: [PATCH 2/2] planner: add unit test of tidb_enable_historical_stats --- executor/set_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/executor/set_test.go b/executor/set_test.go index 2595b4a3131f5..0f4a4b76a63ad 100644 --- a/executor/set_test.go +++ b/executor/set_test.go @@ -587,6 +587,12 @@ func (s *testSerialSuite1) TestSetVar(c *C) { tk.MustExec("set global tidb_enable_tso_follower_proxy = 0") tk.MustQuery("select @@tidb_enable_tso_follower_proxy").Check(testkit.Rows("0")) c.Assert(tk.ExecToErr("set tidb_enable_tso_follower_proxy = 1"), NotNil) + + tk.MustQuery("select @@tidb_enable_historical_stats").Check(testkit.Rows("0")) + tk.MustExec("set global tidb_enable_historical_stats = 1") + tk.MustQuery("select @@tidb_enable_historical_stats").Check(testkit.Rows("1")) + tk.MustExec("set global tidb_enable_historical_stats = 0") + tk.MustQuery("select @@tidb_enable_historical_stats").Check(testkit.Rows("0")) } func (s *testSuite5) TestTruncateIncorrectIntSessionVar(c *C) {