@@ -2088,8 +2088,8 @@ func TestExplainAnalyzeDMLWithFKInfo(t *testing.T) {
2088
2088
{
2089
2089
sql : "explain analyze insert ignore into t6 values (1,1,10)" ,
2090
2090
plan : "Insert_.* root time:.* loops:.* prepare:.* check_insert.* fk_check:.*" +
2091
- "├─Foreign_Key_Check.* 0 root table:t5 total:0s , foreign_keys:1 foreign_key:fk_1, check_exist N/A N/A.*" +
2092
- "├─Foreign_Key_Check.* 0 root table:t5, index:idx2 total:0s , foreign_keys:1 foreign_key:fk_2, check_exist N/A N/A.*" +
2091
+ "├─Foreign_Key_Check.* 0 root table:t5 total:.*, lock:.* , foreign_keys:1 foreign_key:fk_1, check_exist N/A N/A.*" +
2092
+ "├─Foreign_Key_Check.* 0 root table:t5, index:idx2 total:.*, lock:.* , foreign_keys:1 foreign_key:fk_2, check_exist N/A N/A.*" +
2093
2093
"└─Foreign_Key_Check.* 0 root table:t5, index:idx3 total:0s, foreign_keys:1 foreign_key:fk_3, check_exist N/A N/A" ,
2094
2094
},
2095
2095
{
@@ -2494,3 +2494,42 @@ func TestFKBuild(t *testing.T) {
2494
2494
tk .MustExec ("delete from test.t3" )
2495
2495
tk .MustQuery ("select * from test.t2" ).Check (testkit .Rows ())
2496
2496
}
2497
+
2498
+ func TestLockKeysInInsertIgnore (t * testing.T ) {
2499
+ store := realtikvtest .CreateMockStoreAndSetup (t )
2500
+ tk := testkit .NewTestKit (t , store )
2501
+ tk .MustExec ("use test" )
2502
+ tk .MustExec ("create table t1 (id int primary key);" )
2503
+ tk .MustExec ("create table t2 (id int primary key, foreign key fk (id) references t1(id));" )
2504
+ tk .MustExec ("insert into t1 values (1)" )
2505
+
2506
+ tk .MustExec ("BEGIN" )
2507
+ tk .MustExec ("INSERT IGNORE INTO t2 VALUES (1)" )
2508
+
2509
+ var wg sync.WaitGroup
2510
+ var tk2CommitTime time.Time
2511
+ tk2StartTime := time .Now ()
2512
+ wg .Add (1 )
2513
+ go func () {
2514
+ defer wg .Done ()
2515
+
2516
+ tk2 := testkit .NewTestKit (t , store )
2517
+ // unistore has a bug to handle the fair locking mechanism. We need to disable it to pass this test.
2518
+ // Ref: https://github.com/pingcap/tidb/issues/56663
2519
+ tk2 .MustExec ("set tidb_pessimistic_txn_fair_locking = 'OFF'" )
2520
+ tk2 .MustExec ("use test" )
2521
+ tk2 .MustExec ("BEGIN" )
2522
+ require .NotNil (t , tk2 .ExecToErr ("UPDATE t1 SET id = 2 WHERE id = 1" ))
2523
+ tk2 .MustExec ("COMMIT" )
2524
+ tk2CommitTime = time .Now ()
2525
+ }()
2526
+
2527
+ sleepDuration := 500 * time .Millisecond
2528
+ time .Sleep (sleepDuration )
2529
+ tk .MustExec ("COMMIT" )
2530
+ wg .Wait ()
2531
+
2532
+ require .Greater (t , tk2CommitTime .Sub (tk2StartTime ), sleepDuration )
2533
+ tk .MustQuery ("SELECT * FROM t1" ).Check (testkit .Rows ("1" ))
2534
+ tk .MustQuery ("SELECT * FROM t2" ).Check (testkit .Rows ("1" ))
2535
+ }
0 commit comments