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
ti-srebot authored May 31, 2021
1 parent 8132c81 commit d34be8a
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 @@ -1719,7 +1719,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 @@ -4238,3 +4238,61 @@ func (s *testSessionSerialSuite) TestParseWithParams(c *C) {
c.Assert(err, IsNil)
c.Assert(sb.String(), Equals, "SELECT 3")
}

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 d34be8a

Please sign in to comment.