Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/executor/foreign_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (fkc *FKCheckExec) doCheck(ctx context.Context) error {
fkc.stats = &FKCheckRuntimeStats{}
defer fkc.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl.RegisterStats(fkc.ID(), fkc.stats)
}
if len(fkc.toBeCheckedKeys) == 0 && len(fkc.toBeCheckedPrefixKeys) == 0 {
if len(fkc.toBeCheckedKeys) == 0 && len(fkc.toBeCheckedPrefixKeys) == 0 && len(fkc.toBeLockedKeys) == 0 {
return nil
}
start := time.Now()
Expand All @@ -248,6 +248,7 @@ func (fkc *FKCheckExec) doCheck(ctx context.Context) error {
if fkc.stats != nil {
fkc.stats.Check = time.Since(start)
}

if len(fkc.toBeLockedKeys) == 0 {
return nil
}
Expand Down Expand Up @@ -542,7 +543,7 @@ type fkCheckKey struct {
isPrefix bool
}

func (fkc FKCheckExec) checkRows(ctx context.Context, sc *stmtctx.StatementContext, txn kv.Transaction, rows []toBeCheckedRow) error {
func (fkc *FKCheckExec) checkRows(ctx context.Context, sc *stmtctx.StatementContext, txn kv.Transaction, rows []toBeCheckedRow) error {
if fkc.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl != nil {
fkc.stats = &FKCheckRuntimeStats{}
defer fkc.ctx.GetSessionVars().StmtCtx.RuntimeStatsColl.RegisterStats(fkc.ID(), fkc.stats)
Expand Down
2 changes: 1 addition & 1 deletion pkg/executor/test/fktest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_test(
"main_test.go",
],
flaky = True,
shard_count = 26,
shard_count = 27,
deps = [
"//pkg/config",
"//pkg/executor",
Expand Down
43 changes: 41 additions & 2 deletions pkg/executor/test/fktest/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2088,8 +2088,8 @@ func TestExplainAnalyzeDMLWithFKInfo(t *testing.T) {
{
sql: "explain analyze insert ignore into t6 values (1,1,10)",
plan: "Insert_.* root time:.* loops:.* prepare:.* check_insert.* fk_check:.*" +
"├─Foreign_Key_Check.* 0 root table:t5 total:0s, foreign_keys:1 foreign_key:fk_1, check_exist N/A N/A.*" +
"├─Foreign_Key_Check.* 0 root table:t5, index:idx2 total:0s, foreign_keys:1 foreign_key:fk_2, check_exist N/A N/A.*" +
"├─Foreign_Key_Check.* 0 root table:t5 total:.*, lock:.*, foreign_keys:1 foreign_key:fk_1, check_exist N/A N/A.*" +
"├─Foreign_Key_Check.* 0 root table:t5, index:idx2 total:.*, lock:.*, foreign_keys:1 foreign_key:fk_2, check_exist N/A N/A.*" +
"└─Foreign_Key_Check.* 0 root table:t5, index:idx3 total:0s, foreign_keys:1 foreign_key:fk_3, check_exist N/A N/A",
},
{
Expand Down Expand Up @@ -2527,3 +2527,42 @@ func TestLockKeysInDML(t *testing.T) {
tk.MustQuery("SELECT * FROM t1").Check(testkit.Rows("1"))
tk.MustQuery("SELECT * FROM t2").Check(testkit.Rows("1"))
}

func TestLockKeysInInsertIgnore(t *testing.T) {
store := realtikvtest.CreateMockStoreAndSetup(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1 (id int primary key);")
tk.MustExec("create table t2 (id int primary key, foreign key fk (id) references t1(id));")
tk.MustExec("insert into t1 values (1)")

tk.MustExec("BEGIN")
tk.MustExec("INSERT IGNORE INTO t2 VALUES (1)")

var wg sync.WaitGroup
var tk2CommitTime time.Time
tk2StartTime := time.Now()
wg.Add(1)
go func() {
defer wg.Done()

tk2 := testkit.NewTestKit(t, store)
// unistore has a bug to handle the fair locking mechanism. We need to disable it to pass this test.
// Ref: https://github.com/pingcap/tidb/issues/56663
tk2.MustExec("set tidb_pessimistic_txn_fair_locking = 'OFF'")
tk2.MustExec("use test")
tk2.MustExec("BEGIN")
require.NotNil(t, tk2.ExecToErr("UPDATE t1 SET id = 2 WHERE id = 1"))
tk2.MustExec("COMMIT")
tk2CommitTime = time.Now()
}()

sleepDuration := 500 * time.Millisecond
time.Sleep(sleepDuration)
tk.MustExec("COMMIT")
wg.Wait()

require.Greater(t, tk2CommitTime.Sub(tk2StartTime), sleepDuration)
tk.MustQuery("SELECT * FROM t1").Check(testkit.Rows("1"))
tk.MustQuery("SELECT * FROM t2").Check(testkit.Rows("1"))
}