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

execution: fix the incorrect use of cached plan for point get #24749

Merged
merged 3 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,7 @@ func (s *session) IsCachedExecOk(ctx context.Context, preparedStmt *plannercore.
return false, nil
}
// check auto commit
if !s.GetSessionVars().IsAutocommit() {
if !plannercore.IsAutoCommitTxn(s) {
return false, nil
}
// check schema version
Expand Down
58 changes: 58 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4486,3 +4486,61 @@ func (s *testSessionSuite) TestReadDMLBatchSize(c *C) {
_, _ = se.Execute(context.TODO(), "select 1")
c.Assert(se.GetSessionVars().DMLBatchSize, Equals, 1000)
}

func (s *testSessionSuite) TestInTxnPSProtoPointGet(c *C) {
ctx := context.Background()
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t1(c1 int primary key, c2 int, c3 int)")
tk.MustExec("insert into t1 values(1, 10, 100)")

// Generate the ps statement and make the prepared plan cached for point get.
id, _, _, err := tk.Se.PrepareStmt("select c1, c2 from t1 where c1 = ?")
c.Assert(err, IsNil)
idForUpdate, _, _, err := tk.Se.PrepareStmt("select c1, c2 from t1 where c1 = ? for update")
c.Assert(err, IsNil)
params := []types.Datum{types.NewDatum(1)}
rs, err := tk.Se.ExecutePreparedStmt(ctx, id, params)
c.Assert(err, IsNil)
tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows("1 10"))
rs, err = tk.Se.ExecutePreparedStmt(ctx, idForUpdate, params)
c.Assert(err, IsNil)
tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows("1 10"))

// Query again the cached plan will be used.
rs, err = tk.Se.ExecutePreparedStmt(ctx, id, params)
c.Assert(err, IsNil)
tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows("1 10"))
rs, err = tk.Se.ExecutePreparedStmt(ctx, idForUpdate, params)
c.Assert(err, IsNil)
tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows("1 10"))

// Start a transaction, now the in txn flag will be added to the session vars.
_, err = tk.Se.Execute(ctx, "start transaction")
c.Assert(err, IsNil)
rs, err = tk.Se.ExecutePreparedStmt(ctx, id, params)
c.Assert(err, IsNil)
tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows("1 10"))
txn, err := tk.Se.Txn(false)
c.Assert(err, IsNil)
c.Assert(txn.Valid(), IsTrue)
rs, err = tk.Se.ExecutePreparedStmt(ctx, idForUpdate, params)
c.Assert(err, IsNil)
tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows("1 10"))
txn, err = tk.Se.Txn(false)
c.Assert(err, IsNil)
c.Assert(txn.Valid(), IsTrue)
_, err = tk.Se.Execute(ctx, "update t1 set c2 = c2 + 1")
c.Assert(err, IsNil)
// Check the read result after in-transaction update.
rs, err = tk.Se.ExecutePreparedStmt(ctx, id, params)
c.Assert(err, IsNil)
tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows("1 11"))
rs, err = tk.Se.ExecutePreparedStmt(ctx, idForUpdate, params)
c.Assert(err, IsNil)
tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Rows("1 11"))
txn, err = tk.Se.Txn(false)
c.Assert(err, IsNil)
c.Assert(txn.Valid(), IsTrue)
tk.MustExec("commit")
}