Skip to content

Commit 6aeebd0

Browse files
committed
Revert "executor: lock duplicated keys on insert-ignore & replace-nothing (pingcap#42210) (pingcap#42285)"
This reverts commit f01eaf1.
1 parent 29116c0 commit 6aeebd0

File tree

5 files changed

+17
-103
lines changed

5 files changed

+17
-103
lines changed

executor/insert_common.go

-12
Original file line numberDiff line numberDiff line change
@@ -1166,10 +1166,6 @@ func (e *InsertValues) batchCheckAndInsert(ctx context.Context, rows [][]types.D
11661166
}
11671167
} else {
11681168
e.ctx.GetSessionVars().StmtCtx.AppendWarning(r.handleKey.dupErr)
1169-
if txnCtx := e.ctx.GetSessionVars().TxnCtx; txnCtx.IsPessimistic {
1170-
// lock duplicated row key on insert-ignore
1171-
txnCtx.AddUnchangedRowKey(r.handleKey.newKey)
1172-
}
11731169
continue
11741170
}
11751171
} else if !kv.IsErrNotFound(err) {
@@ -1181,10 +1177,6 @@ func (e *InsertValues) batchCheckAndInsert(ctx context.Context, rows [][]types.D
11811177
if err == nil {
11821178
// If duplicate keys were found in BatchGet, mark row = nil.
11831179
e.ctx.GetSessionVars().StmtCtx.AppendWarning(uk.dupErr)
1184-
if txnCtx := e.ctx.GetSessionVars().TxnCtx; txnCtx.IsPessimistic {
1185-
// lock duplicated unique key on insert-ignore
1186-
txnCtx.AddUnchangedRowKey(uk.newKey)
1187-
}
11881180
skip = true
11891181
break
11901182
}
@@ -1233,10 +1225,6 @@ func (e *InsertValues) removeRow(ctx context.Context, txn kv.Transaction, r toBe
12331225
return err
12341226
}
12351227
if identical {
1236-
_, err := appendUnchangedRowForLock(e.ctx, r.t, handle, oldRow)
1237-
if err != nil {
1238-
return err
1239-
}
12401228
return nil
12411229
}
12421230

executor/insert_test.go

-66
Original file line numberDiff line numberDiff line change
@@ -1481,69 +1481,3 @@ func TestIssue32213(t *testing.T) {
14811481
tk.MustQuery("select cast(test.t1.c1 as decimal(5, 3)) from test.t1").Check(testkit.Rows("99.999"))
14821482
tk.MustQuery("select cast(test.t1.c1 as decimal(6, 3)) from test.t1").Check(testkit.Rows("100.000"))
14831483
}
1484-
1485-
func TestInsertLock(t *testing.T) {
1486-
store := testkit.CreateMockStore(t)
1487-
tk1 := testkit.NewTestKit(t, store)
1488-
tk2 := testkit.NewTestKit(t, store)
1489-
tk1.MustExec("use test")
1490-
tk2.MustExec("use test")
1491-
1492-
for _, tt := range []struct {
1493-
name string
1494-
ddl string
1495-
dml string
1496-
}{
1497-
{
1498-
"replace-pk",
1499-
"create table t (c int primary key clustered)",
1500-
"replace into t values (1)",
1501-
},
1502-
{
1503-
"replace-uk",
1504-
"create table t (c int unique key)",
1505-
"replace into t values (1)",
1506-
},
1507-
{
1508-
"insert-ingore-pk",
1509-
"create table t (c int primary key clustered)",
1510-
"insert ignore into t values (1)",
1511-
},
1512-
{
1513-
"insert-ingore-uk",
1514-
"create table t (c int unique key)",
1515-
"insert ignore into t values (1)",
1516-
},
1517-
{
1518-
"insert-update-pk",
1519-
"create table t (c int primary key clustered)",
1520-
"insert into t values (1) on duplicate key update c = values(c)",
1521-
},
1522-
{
1523-
"insert-update-uk",
1524-
"create table t (c int unique key)",
1525-
"insert into t values (1) on duplicate key update c = values(c)",
1526-
},
1527-
} {
1528-
t.Run(tt.name, func(t *testing.T) {
1529-
tk1.MustExec("drop table if exists t")
1530-
tk1.MustExec(tt.ddl)
1531-
tk1.MustExec("insert into t values (1)")
1532-
tk1.MustExec("begin")
1533-
tk1.MustExec(tt.dml)
1534-
done := make(chan struct{})
1535-
go func() {
1536-
tk2.MustExec("delete from t")
1537-
done <- struct{}{}
1538-
}()
1539-
select {
1540-
case <-done:
1541-
require.Failf(t, "txn2 is not blocked by %q", tt.dml)
1542-
case <-time.After(100 * time.Millisecond):
1543-
}
1544-
tk1.MustExec("commit")
1545-
<-done
1546-
tk1.MustQuery("select * from t").Check([][]interface{}{})
1547-
})
1548-
}
1549-
}

executor/replace.go

-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ func (e *ReplaceExec) removeRow(ctx context.Context, txn kv.Transaction, handle
8686
}
8787
if rowUnchanged {
8888
e.ctx.GetSessionVars().StmtCtx.AddAffectedRows(1)
89-
_, err := appendUnchangedRowForLock(e.ctx, r.t, handle, oldRow)
90-
if err != nil {
91-
return false, err
92-
}
9389
return true, nil
9490
}
9591

executor/write.go

+16-20
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,22 @@ func updateRecord(ctx context.Context, sctx sessionctx.Context, h kv.Handle, old
139139
if sctx.GetSessionVars().ClientCapability&mysql.ClientFoundRows > 0 {
140140
sc.AddAffectedRows(1)
141141
}
142-
_, err := appendUnchangedRowForLock(sctx, t, h, oldData)
143-
return false, err
142+
143+
physicalID := t.Meta().ID
144+
if pt, ok := t.(table.PartitionedTable); ok {
145+
p, err := pt.GetPartitionByRow(sctx, oldData)
146+
if err != nil {
147+
return false, err
148+
}
149+
physicalID = p.GetPhysicalID()
150+
}
151+
152+
unchangedRowKey := tablecodec.EncodeRowKeyWithHandle(physicalID, h)
153+
txnCtx := sctx.GetSessionVars().TxnCtx
154+
if txnCtx.IsPessimistic {
155+
txnCtx.AddUnchangedRowKey(unchangedRowKey)
156+
}
157+
return false, nil
144158
}
145159

146160
// Fill values into on-update-now fields, only if they are really changed.
@@ -217,24 +231,6 @@ func updateRecord(ctx context.Context, sctx sessionctx.Context, h kv.Handle, old
217231
return true, nil
218232
}
219233

220-
func appendUnchangedRowForLock(sctx sessionctx.Context, t table.Table, h kv.Handle, row []types.Datum) (bool, error) {
221-
txnCtx := sctx.GetSessionVars().TxnCtx
222-
if !txnCtx.IsPessimistic {
223-
return false, nil
224-
}
225-
physicalID := t.Meta().ID
226-
if pt, ok := t.(table.PartitionedTable); ok {
227-
p, err := pt.GetPartitionByRow(sctx, row)
228-
if err != nil {
229-
return false, err
230-
}
231-
physicalID = p.GetPhysicalID()
232-
}
233-
unchangedRowKey := tablecodec.EncodeRowKeyWithHandle(physicalID, h)
234-
txnCtx.AddUnchangedRowKey(unchangedRowKey)
235-
return true, nil
236-
}
237-
238234
func rebaseAutoRandomValue(ctx context.Context, sctx sessionctx.Context, t table.Table, newData *types.Datum, col *table.Column) error {
239235
tableInfo := t.Meta()
240236
if !tableInfo.ContainsAutoRandomBits() {

tests/realtikvtest/pessimistictest/pessimistic_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ func TestOptimisticConflicts(t *testing.T) {
534534
tk.MustExec("begin pessimistic")
535535
// This SQL use BatchGet and cache data in the txn snapshot.
536536
// It can be changed to other SQLs that use BatchGet.
537-
tk.MustExec("select * from conflict where id in (1, 2, 3)")
537+
tk.MustExec("insert ignore into conflict values (1, 2)")
538538

539539
tk2.MustExec("update conflict set c = c - 1")
540540

0 commit comments

Comments
 (0)