diff --git a/session/bootstrap.go b/session/bootstrap.go index 415a9850b5b94..e736731a4bf8e 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -857,11 +857,14 @@ const ( // version 141 set the value of `tidb_session_plan_cache_size` to "tidb_prepared_plan_cache_size" if there is no `tidb_session_plan_cache_size`. // This will only happens when we upgrade a cluster before 7.1. version141 = 141 + // version 142 insert "tidb_enable_non_prepared_plan_cache|0" to mysql.GLOBAL_VARIABLES if there is no tidb_enable_non_prepared_plan_cache. + // This will only happens when we upgrade a cluster before 6.5. + version142 = 142 ) // 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 = version140 +var currentBootstrapVersion int64 = version142 // 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 @@ -991,6 +994,7 @@ var ( upgradeToVer139, upgradeToVer140, upgradeToVer141, + upgradeToVer142, } ) @@ -2467,6 +2471,25 @@ func upgradeToVer141(s Session, ver int64) { mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBSessionPlanCacheSize, val) } +func upgradeToVer142(s Session, ver int64) { + if ver >= version142 { + return + } + ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap) + rs, err := s.ExecuteInternal(ctx, "SELECT VARIABLE_VALUE FROM %n.%n WHERE VARIABLE_NAME=%?;", + mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableNonPreparedPlanCache) + terror.MustNil(err) + req := rs.NewChunk(nil) + err = rs.Next(ctx, req) + terror.MustNil(err) + if req.NumRows() != 0 { + return + } + + mustExecute(s, "INSERT HIGH_PRIORITY IGNORE INTO %n.%n VALUES (%?, %?);", + mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableNonPreparedPlanCache, variable.Off) +} + func writeOOMAction(s Session) { comment := "oom-action is `log` by default in v3.0.x, `cancel` by default in v4.0.11+" mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, %?) ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`, diff --git a/session/bootstrap_test.go b/session/bootstrap_test.go index 470bcc8c57235..06e86e67fa6a4 100644 --- a/session/bootstrap_test.go +++ b/session/bootstrap_test.go @@ -1903,12 +1903,20 @@ func TestTiDBNonPrepPlanCacheUpgradeFrom540To700(t *testing.T) { err = txn.Commit(context.Background()) require.NoError(t, err) mustExec(t, seV54, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver54)) + mustExec(t, seV54, fmt.Sprintf("delete from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBEnableNonPreparedPlanCache)) mustExec(t, seV54, "commit") unsetStoreBootstrapped(store.UUID()) ver, err := getBootstrapVersion(seV54) require.NoError(t, err) require.Equal(t, int64(ver54), ver) + // We are now in 5.4, check TiDBCostModelVersion should not exist. + res := mustExecToRecodeSet(t, seV54, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBEnableNonPreparedPlanCache)) + chk := res.NewChunk(nil) + err = res.Next(ctx, chk) + require.NoError(t, err) + require.Equal(t, 0, chk.NumRows()) + // Upgrade to 7.0 domCurVer, err := BootstrapSession(store) require.NoError(t, err) @@ -1919,8 +1927,8 @@ func TestTiDBNonPrepPlanCacheUpgradeFrom540To700(t *testing.T) { require.Equal(t, currentBootstrapVersion, ver) // We are now in 7.0 - res := mustExecToRecodeSet(t, seCurVer, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBEnableNonPreparedPlanCache)) - chk := res.NewChunk(nil) + res = mustExecToRecodeSet(t, seCurVer, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBEnableNonPreparedPlanCache)) + chk = res.NewChunk(nil) err = res.Next(ctx, chk) require.NoError(t, err) require.Equal(t, 1, chk.NumRows()) diff --git a/sessionctx/variable/tidb_vars.go b/sessionctx/variable/tidb_vars.go index 49bc83900d6bf..84b6b24fb6b66 100644 --- a/sessionctx/variable/tidb_vars.go +++ b/sessionctx/variable/tidb_vars.go @@ -1200,7 +1200,7 @@ const ( DefTiDBEnableFastReorg = true DefTiDBDDLDiskQuota = 100 * 1024 * 1024 * 1024 // 100GB DefExecutorConcurrency = 5 - DefTiDBEnableNonPreparedPlanCache = false + DefTiDBEnableNonPreparedPlanCache = true DefTiDBNonPreparedPlanCacheSize = 100 DefTiDBPlanCacheMaxPlanSize = 2 * size.MB DefTiDBEnableTiFlashReadForWriteStmt = true