Skip to content

Commit

Permalink
planner: fix begin statement consumed read_ts wrongly (#25492) (#25520)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Jun 18, 2021
1 parent dda34ac commit 38175b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
8 changes: 8 additions & 0 deletions executor/stale_txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,14 @@ func (s *testStaleTxnSerialSuite) TestSetTransactionReadOnlyAsOf(c *C) {
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "start transaction read only as of is forbidden after set transaction read only as of")
tk.MustExec(`SET TRANSACTION READ ONLY as of timestamp '2021-04-21 00:42:12'`)
err = tk.ExecToErr(`START TRANSACTION READ ONLY AS OF TIMESTAMP '2020-09-06 00:00:00'`)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "start transaction read only as of is forbidden after set transaction read only as of")

tk.MustExec("begin")
c.Assert(tk.Se.GetSessionVars().TxnReadTS.PeakTxnReadTS(), Equals, uint64(424394603102208000))
tk.MustExec("commit")
tk.MustExec(`START TRANSACTION READ ONLY AS OF TIMESTAMP '2020-09-06 00:00:00'`)
}

func (s *testStaleTxnSerialSuite) TestValidateReadOnlyInStalenessTransaction(c *C) {
Expand Down
4 changes: 3 additions & 1 deletion planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2447,7 +2447,7 @@ func (b *PlanBuilder) buildSimple(ctx context.Context, node ast.StmtNode) (Plan,
case *ast.ShutdownStmt:
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.ShutdownPriv, "", "", "", nil)
case *ast.BeginStmt:
readTS := b.ctx.GetSessionVars().TxnReadTS.UseTxnReadTS()
readTS := b.ctx.GetSessionVars().TxnReadTS.PeakTxnReadTS()
if raw.AsOf != nil {
startTS, err := calculateTsExpr(b.ctx, raw.AsOf)
if err != nil {
Expand All @@ -2456,6 +2456,8 @@ func (b *PlanBuilder) buildSimple(ctx context.Context, node ast.StmtNode) (Plan,
p.StaleTxnStartTS = startTS
} else if readTS > 0 {
p.StaleTxnStartTS = readTS
// consume read ts here
b.ctx.GetSessionVars().TxnReadTS.UseTxnReadTS()
}
}
return p, nil
Expand Down

0 comments on commit 38175b9

Please sign in to comment.