From 8ae430c633cdfa9ca39af6e4b3e2e2a7bb5dd338 Mon Sep 17 00:00:00 2001 From: Jack Yu Date: Thu, 26 Mar 2020 11:17:38 +0800 Subject: [PATCH] *: add the tid to the schema-change related tables only when keys need to be locked (#15698) --- executor/executor.go | 14 ++++++++------ session/session_test.go | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/executor/executor.go b/executor/executor.go index e4958039e719c..9e32321591408 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -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 { @@ -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...) } diff --git a/session/session_test.go b/session/session_test.go index 0ac2c99a84e45..7379922d26a50 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -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)