Skip to content

Commit

Permalink
execution: fix the incorrect use of cached plan for point get (#24749)
Browse files Browse the repository at this point in the history
  • Loading branch information
cfzjywxk authored May 19, 2021
1 parent 8fb29eb commit f71fbdb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1820,7 +1820,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 @@ -4485,3 +4485,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")
}

0 comments on commit f71fbdb

Please sign in to comment.