Skip to content

Commit

Permalink
*: add the tid to the schema-change related tables only when keys nee…
Browse files Browse the repository at this point in the history
…d to be locked (pingcap#15698) (pingcap#15708)
  • Loading branch information
jackysp authored Mar 26, 2020
1 parent 79e6622 commit 046d35d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
14 changes: 8 additions & 6 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,12 +776,6 @@ func (e *SelectLockExec) Open(ctx context.Context) error {
return err
}

txnCtx := e.ctx.GetSessionVars().TxnCtx
for id := range e.Schema().TblID2Handle {
// This operation is only for schema validator check.
txnCtx.UpdateDeltaForTable(id, 0, 0, map[int64]int64{})
}

if len(e.Schema().TblID2Handle) > 0 && len(e.partitionedTable) > 0 {
e.tblID2Table = make(map[int64]table.PartitionedTable, len(e.partitionedTable))
for id := range e.Schema().TblID2Handle {
Expand Down Expand Up @@ -835,6 +829,14 @@ func (e *SelectLockExec) Next(ctx context.Context, req *chunk.Chunk) error {
return nil
}
lockWaitTime := e.ctx.GetSessionVars().LockWaitTimeout

if len(e.keys) > 0 {
// This operation is only for schema validator check.
for id := range e.Schema().TblID2Handle {
e.ctx.GetSessionVars().TxnCtx.UpdateDeltaForTable(id, 0, 0, map[int64]int64{})
}
}

return doLockKeys(ctx, e.ctx, newLockCtx(e.ctx.GetSessionVars(), lockWaitTime), e.keys...)
}

Expand Down
22 changes: 22 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1935,6 +1935,28 @@ func (s *testSchemaSuite) TestCommitWhenSchemaChanged(c *C) {
c.Assert(terror.ErrorEqual(err, plannercore.ErrWrongValueCountOnRow), IsTrue, Commentf("err %v", err))
}

func (s *testSchemaSuite) TestRetrySchemaChangeForEmptyChange(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk1 := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create table t (i int)")
tk.MustExec("create table t1 (i int)")
tk.MustExec("begin")
tk1.MustExec("alter table t add j int")
tk.MustExec("select * from t for update")
tk.MustExec("update t set i = -i")
tk.MustExec("delete from t")
tk.MustExec("insert into t1 values (1)")
tk.MustExec("commit")

tk.MustExec("begin pessimistic")
tk1.MustExec("alter table t add k int")
tk.MustExec("select * from t for update")
tk.MustExec("update t set i = -i")
tk.MustExec("delete from t")
tk.MustExec("insert into t1 values (1)")
tk.MustExec("commit")
}

func (s *testSchemaSuite) TestRetrySchemaChange(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk1 := testkit.NewTestKitWithInit(c, s.store)
Expand Down

0 comments on commit 046d35d

Please sign in to comment.