Skip to content

Commit

Permalink
*: read committed transaction gets a wrong snapshot with subquery (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
jackysp authored Mar 19, 2020
1 parent 86d8e16 commit 7e570b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
9 changes: 4 additions & 5 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,9 @@ func (b *executorBuilder) updateForUpdateTSIfNeeded(selectPlan plannercore.Physi
// refreshForUpdateTSForRC is used to refresh the for-update-ts for reading data at read consistency level in pessimistic transaction.
// It could use the cached tso from the statement future to avoid get tso many times.
func (b *executorBuilder) refreshForUpdateTSForRC() error {
defer func() {
b.snapshotTS = b.ctx.GetSessionVars().TxnCtx.GetForUpdateTS()
}()
future := b.ctx.GetSessionVars().TxnCtx.GetStmtFutureForRC()
if future == nil {
return nil
Expand All @@ -1696,11 +1699,7 @@ func (b *executorBuilder) refreshForUpdateTSForRC() error {
}
b.ctx.GetSessionVars().TxnCtx.SetStmtFutureForRC(nil)
// If newForUpdateTS is 0, it will force to get a new for-update-ts from PD.
if err := UpdateForUpdateTS(b.ctx, newForUpdateTS); err != nil {
return err
}
b.snapshotTS = b.ctx.GetSessionVars().TxnCtx.GetForUpdateTS()
return nil
return UpdateForUpdateTS(b.ctx, newForUpdateTS)
}

func (b *executorBuilder) buildAnalyzeIndexPushdown(task plannercore.AnalyzeIndexTask, opts map[ast.AnalyzeOptionType]uint64, autoAnalyze string) *analyzeTask {
Expand Down
18 changes: 18 additions & 0 deletions session/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1144,3 +1144,21 @@ func (s *testPessimisticSuite) TestRollbackWakeupBlockedTxn(c *C) {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/store/tikv/txnExpireRetTTL"), IsNil)
c.Assert(failpoint.Disable("github.com/pingcap/tidb/store/tikv/getTxnStatusDelay"), IsNil)
}

func (s *testPessimisticSuite) TestRCSubQuery(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t, t1")
tk.MustExec("create table `t` ( `c1` int(11) not null, `c2` int(11) default null, primary key (`c1`) )")
tk.MustExec("insert into t values(1, 3)")
tk.MustExec("create table `t1` ( `c1` int(11) not null, `c2` int(11) default null, primary key (`c1`) )")
tk.MustExec("insert into t1 values(1, 3)")
tk.MustExec("set transaction isolation level read committed")
tk.MustExec("begin pessimistic")

tk2 := testkit.NewTestKitWithInit(c, s.store)
tk2.MustExec("update t1 set c2 = c2 + 1")

tk.MustQuery("select * from t1 where c1 = (select 1) and 1=1;").Check(testkit.Rows("1 4"))
tk.MustQuery("select * from t1 where c1 = (select c1 from t where c1 = 1) and 1=1;").Check(testkit.Rows("1 4"))
tk.MustExec("rollback")
}

0 comments on commit 7e570b1

Please sign in to comment.