Skip to content

Commit

Permalink
session: use TxnCtx.InfoSchema, no matter vars.InTxn() or not (#42027) (
Browse files Browse the repository at this point in the history
#42280)

close #41622, close #42003
  • Loading branch information
ti-chi-bot authored Apr 11, 2023
1 parent 5ccbe49 commit e3a3588
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
19 changes: 10 additions & 9 deletions executor/stale_txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl/placement"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/sessiontxn/staleread"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
Expand Down Expand Up @@ -507,15 +508,15 @@ func TestStalenessTransactionSchemaVer(t *testing.T) {

// get the specific old schema
tk.MustExec(fmt.Sprintf(`START TRANSACTION READ ONLY AS OF TIMESTAMP '%s'`, time1.Format("2006-1-2 15:04:05.000")))
require.Equal(t, schemaVer1, tk.Session().GetInfoSchema().SchemaMetaVersion())
require.Equal(t, schemaVer1, sessiontxn.GetTxnManager(tk.Session()).GetTxnInfoSchema().SchemaMetaVersion())

// schema changed back to the newest
tk.MustExec("commit")
require.Equal(t, schemaVer2, tk.Session().GetInfoSchema().SchemaMetaVersion())
require.Equal(t, schemaVer2, sessiontxn.GetTxnManager(tk.Session()).GetTxnInfoSchema().SchemaMetaVersion())

// select does not affect the infoschema
tk.MustExec(fmt.Sprintf(`SELECT * from t AS OF TIMESTAMP '%s'`, time1.Format("2006-1-2 15:04:05.000")))
require.Equal(t, schemaVer2, tk.Session().GetInfoSchema().SchemaMetaVersion())
require.Equal(t, schemaVer2, sessiontxn.GetTxnManager(tk.Session()).GetTxnInfoSchema().SchemaMetaVersion())
}

func TestSetTransactionReadOnlyAsOf(t *testing.T) {
Expand Down Expand Up @@ -888,28 +889,28 @@ func TestSetTransactionInfoSchema(t *testing.T) {
defer tk.MustExec("drop table if exists t")
tk.MustExec("create table t (id int primary key);")

schemaVer1 := tk.Session().GetInfoSchema().SchemaMetaVersion()
schemaVer1 := sessiontxn.GetTxnManager(tk.Session()).GetTxnInfoSchema().SchemaMetaVersion()
time1 := time.Now()
tk.MustExec("alter table t add c int")

// confirm schema changed
schemaVer2 := tk.Session().GetInfoSchema().SchemaMetaVersion()
schemaVer2 := sessiontxn.GetTxnManager(tk.Session()).GetTxnInfoSchema().SchemaMetaVersion()
time2 := time.Now()
require.Less(t, schemaVer1, schemaVer2)
tk.MustExec(fmt.Sprintf(`SET TRANSACTION READ ONLY AS OF TIMESTAMP '%s'`, time1.Format("2006-1-2 15:04:05.000")))
require.Equal(t, schemaVer1, tk.Session().GetInfoSchema().SchemaMetaVersion())
tk.MustExec("select * from t;")
tk.MustExec("alter table t add d int")
schemaVer3 := tk.Session().GetInfoSchema().SchemaMetaVersion()
schemaVer3 := sessiontxn.GetTxnManager(tk.Session()).GetTxnInfoSchema().SchemaMetaVersion()
tk.MustExec(fmt.Sprintf(`SET TRANSACTION READ ONLY AS OF TIMESTAMP '%s'`, time1.Format("2006-1-2 15:04:05.000")))
tk.MustExec("begin;")
require.Equal(t, schemaVer1, tk.Session().GetInfoSchema().SchemaMetaVersion())
require.Equal(t, schemaVer1, sessiontxn.GetTxnManager(tk.Session()).GetTxnInfoSchema().SchemaMetaVersion())
tk.MustExec("commit")
tk.MustExec(fmt.Sprintf(`SET TRANSACTION READ ONLY AS OF TIMESTAMP '%s'`, time2.Format("2006-1-2 15:04:05.000")))
tk.MustExec("begin;")
require.Equal(t, schemaVer2, tk.Session().GetInfoSchema().SchemaMetaVersion())
require.Equal(t, schemaVer2, sessiontxn.GetTxnManager(tk.Session()).GetTxnInfoSchema().SchemaMetaVersion())
tk.MustExec("commit")
require.Equal(t, schemaVer3, tk.Session().GetInfoSchema().SchemaMetaVersion())
require.Equal(t, schemaVer3, sessiontxn.GetTxnManager(tk.Session()).GetTxnInfoSchema().SchemaMetaVersion())
}

func TestStaleSelect(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3882,7 +3882,7 @@ func (s *session) GetInfoSchema() sessionctx.InfoschemaMetaVersion {
if snap, ok := vars.SnapshotInfoschema.(infoschema.InfoSchema); ok {
logutil.BgLogger().Info("use snapshot schema", zap.Uint64("conn", vars.ConnectionID), zap.Int64("schemaVersion", snap.SchemaMetaVersion()))
is = snap
} else if vars.TxnCtx != nil && vars.InTxn() {
} else if vars.TxnCtx != nil {
if tmp, ok := vars.TxnCtx.InfoSchema.(infoschema.InfoSchema); ok {
is = tmp
}
Expand Down

0 comments on commit e3a3588

Please sign in to comment.