diff --git a/session/bootstrap.go b/session/bootstrap.go index 6f16db51a0ad6..e44a90e43b0e4 100644 --- a/session/bootstrap.go +++ b/session/bootstrap.go @@ -528,11 +528,23 @@ const ( version77 = 77 // version78 updates mysql.stats_buckets.lower_bound, mysql.stats_buckets.upper_bound and mysql.stats_histograms.last_analyze_pos from BLOB to LONGBLOB. version78 = 78 +<<<<<<< HEAD +======= + // version79 adds the mysql.table_cache_meta table + version79 = 79 + // version80 fixes the issue https://github.com/pingcap/tidb/issues/25422. + // If the TiDB upgrading from the 4.x to a newer version, we keep the tidb_analyze_version to 1. + version80 = 80 +>>>>>>> d660e483c... sessionctx: fix the value of analyze_version when upgrading 4.x to 5.… (#30743) ) // currentBootstrapVersion is defined as a variable, so we can modify its value for testing. // please make sure this is the largest version +<<<<<<< HEAD var currentBootstrapVersion int64 = version78 +======= +var currentBootstrapVersion int64 = version80 +>>>>>>> d660e483c... sessionctx: fix the value of analyze_version when upgrading 4.x to 5.… (#30743) var ( bootstrapVersion = []func(Session, int64){ @@ -614,6 +626,11 @@ var ( upgradeToVer76, upgradeToVer77, upgradeToVer78, +<<<<<<< HEAD +======= + upgradeToVer79, + upgradeToVer80, +>>>>>>> d660e483c... sessionctx: fix the value of analyze_version when upgrading 4.x to 5.… (#30743) } ) @@ -1612,6 +1629,37 @@ func upgradeToVer78(s Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.stats_histograms MODIFY last_analyze_pos LONGBLOB DEFAULT NULL") } +<<<<<<< HEAD +======= +func upgradeToVer79(s Session, ver int64) { + if ver >= version79 { + return + } + doReentrantDDL(s, CreateTableCacheMetaTable) +} + +func upgradeToVer80(s Session, ver int64) { + if ver >= version80 { + return + } + // Check if tidb_analyze_version exists in mysql.GLOBAL_VARIABLES. + // If not, insert "tidb_analyze_version | 1" since this is the old behavior before we introduce this variable. + ctx := context.Background() + rs, err := s.ExecuteInternal(ctx, "SELECT VARIABLE_VALUE FROM %n.%n WHERE VARIABLE_NAME=%?;", + mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBAnalyzeVersion) + 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.TiDBAnalyzeVersion, 1) +} + +>>>>>>> d660e483c... sessionctx: fix the value of analyze_version when upgrading 4.x to 5.… (#30743) 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_serial_test.go b/session/bootstrap_serial_test.go index d0e5094e6d4ee..f4cb820e48d90 100644 --- a/session/bootstrap_serial_test.go +++ b/session/bootstrap_serial_test.go @@ -879,3 +879,52 @@ func TestReferencesPrivilegeOnColumn(t *testing.T) { mustExec(t, se, "create table t1 (a int)") mustExec(t, se, "GRANT select (a), update (a),insert(a), references(a) on t1 to issue28531") } + +func TestAnalyzeVersionUpgradeFrom300To500(t *testing.T) { + ctx := context.Background() + store, _ := createStoreAndBootstrap(t) + defer func() { require.NoError(t, store.Close()) }() + + // Upgrade from 3.0.0 to 5.1+ or above. + ver300 := 33 + seV3 := createSessionAndSetID(t, store) + txn, err := store.Begin() + require.NoError(t, err) + m := meta.NewMeta(txn) + err = m.FinishBootstrap(int64(ver300)) + require.NoError(t, err) + err = txn.Commit(context.Background()) + require.NoError(t, err) + mustExec(t, seV3, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver300)) + mustExec(t, seV3, fmt.Sprintf("delete from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBAnalyzeVersion)) + mustExec(t, seV3, "commit") + unsetStoreBootstrapped(store.UUID()) + ver, err := getBootstrapVersion(seV3) + require.NoError(t, err) + require.Equal(t, int64(ver300), ver) + + // We are now in 3.0.0, check tidb_analyze_version should not exist. + res := mustExec(t, seV3, fmt.Sprintf("select * from mysql.GLOBAL_VARIABLES where variable_name='%s'", variable.TiDBAnalyzeVersion)) + chk := res.NewChunk(nil) + err = res.Next(ctx, chk) + require.NoError(t, err) + require.Equal(t, 0, chk.NumRows()) + + domCurVer, err := BootstrapSession(store) + require.NoError(t, err) + defer domCurVer.Close() + seCurVer := createSessionAndSetID(t, store) + ver, err = getBootstrapVersion(seCurVer) + require.NoError(t, err) + require.Equal(t, currentBootstrapVersion, ver) + + // We are now in version no lower than 5.x, tidb_enable_index_merge should be 1. + res = mustExec(t, seCurVer, "select @@tidb_analyze_version") + chk = res.NewChunk(nil) + err = res.Next(ctx, chk) + require.NoError(t, err) + require.Equal(t, 1, chk.NumRows()) + row := chk.GetRow(0) + require.Equal(t, 1, row.Len()) + require.Equal(t, "1", row.GetString(0)) +}