Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session: use TxnCtx.InfoSchema, no matter vars.InTxn() or not (#42027) #42279

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3457,7 +3457,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