From 702c864300489aac9bd300bb506deb9b77c0f1c4 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Thu, 6 Jun 2024 17:20:56 +0800 Subject: [PATCH] *: check delete unique key's handle to handle corner case (#52975) (#53701) close pingcap/tidb#52914 --- table/tables/index.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/table/tables/index.go b/table/tables/index.go index b8a164a9b2ece..ca51c03e2215b 100644 --- a/table/tables/index.go +++ b/table/tables/index.go @@ -340,9 +340,28 @@ func (c *index) Delete(sc *stmtctx.StatementContext, txn kv.Transaction, indexed if distinct { if len(key) > 0 { - err = txn.GetMemBuffer().DeleteWithFlags(key, kv.SetNeedLocked) - if err != nil { - return err + okToDelete := true + if c.idxInfo.BackfillState != model.BackfillStateInapplicable { + // #52914: the delete key is covered by the new ingested key, which shouldn't be deleted. + originVal, err := getKeyInTxn(context.TODO(), txn, key) + if err != nil { + return err + } + if len(originVal) > 0 { + oh, err := tablecodec.DecodeHandleInUniqueIndexValue(originVal, c.tblInfo.IsCommonHandle) + if err != nil { + return err + } + if !h.Equal(oh) { + okToDelete = false + } + } + } + if okToDelete { + err = txn.GetMemBuffer().DeleteWithFlags(key, kv.SetNeedLocked) + if err != nil { + return err + } } } if len(tempKey) > 0 {