diff --git a/executor/write.go b/executor/write.go index c0d8dbd7cdbb2..cfec063c83d76 100644 --- a/executor/write.go +++ b/executor/write.go @@ -162,7 +162,11 @@ func updateRecord(ctx sessionctx.Context, h int64, oldData, newData []types.Datu tid := t.Meta().ID ctx.StmtAddDirtyTableOP(DirtyTableDeleteRow, tid, h, nil) - ctx.StmtAddDirtyTableOP(DirtyTableAddRow, tid, h, newData) + if handleChanged { + ctx.StmtAddDirtyTableOP(DirtyTableAddRow, tid, newHandle, newData) + } else { + ctx.StmtAddDirtyTableOP(DirtyTableAddRow, tid, h, newData) + } if onDup { sc.AddAffectedRows(2) diff --git a/executor/write_test.go b/executor/write_test.go index a385de7906432..a8f878a641b6c 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -1572,3 +1572,21 @@ func (s *testSuite) TestUpdateSelect(c *C) { tk.MustExec("UPDATE msg SET msg.status = (SELECT detail.status FROM detail WHERE msg.id = detail.id)") tk.MustExec("admin check table msg") } + +func (s *testSuite) TestUpdateDelete(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("CREATE TABLE ttt (id bigint(20) NOT NULL, host varchar(30) NOT NULL, PRIMARY KEY (id), UNIQUE KEY i_host (host));") + tk.MustExec("insert into ttt values (8,8),(9,9);") + + tk.MustExec("begin") + tk.MustExec("update ttt set id = 0, host='9' where id = 9 limit 1;") + tk.MustExec("delete from ttt where id = 0 limit 1;") + tk.MustQuery("select * from ttt use index (i_host) order by host;").Check(testkit.Rows("8 8")) + tk.MustExec("update ttt set id = 0, host='8' where id = 8 limit 1;") + tk.MustExec("delete from ttt where id = 0 limit 1;") + tk.MustQuery("select * from ttt use index (i_host) order by host;").Check(testkit.Rows()) + tk.MustExec("commit") + tk.MustExec("admin check table ttt;") + tk.MustExec("drop table ttt") +}