@@ -997,6 +997,86 @@ func TestMDLPreparePlanCacheExecute2(t *testing.T) {
997
997
tk .MustExec ("admin check table t" )
998
998
}
999
999
1000
+ << << << < HEAD
1001
+ == == == =
1002
+ // TestMDLPreparePlanCacheExecuteInsert makes sure the insert statement handle the schema correctly in plan cache.
1003
+ func TestMDLPreparePlanCacheExecuteInsert (t * testing.T ) {
1004
+ store , dom := testkit .CreateMockStoreAndDomain (t )
1005
+ defer ingesttestutil .InjectMockBackendMgr (t , store )()
1006
+
1007
+ sv := server .CreateMockServer (t , store )
1008
+
1009
+ sv .SetDomain (dom )
1010
+ dom .InfoSyncer ().SetSessionManager (sv )
1011
+ defer sv .Close ()
1012
+
1013
+ conn1 := server .CreateMockConn (t , sv )
1014
+ tk := testkit .NewTestKitWithSession (t , store , conn1 .Context ().Session )
1015
+ conn2 := server .CreateMockConn (t , sv )
1016
+ tkDDL := testkit .NewTestKitWithSession (t , store , conn2 .Context ().Session )
1017
+ tk .MustExec ("use test" )
1018
+ tk .MustExec ("set global tidb_enable_metadata_lock=1" )
1019
+ tk .MustExec ("create table t(a int primary key, b int);" )
1020
+ tk .MustExec ("create table t2(a int);" )
1021
+ tk .MustExec ("insert into t values(1, 1), (2, 2), (3, 3), (4, 4);" )
1022
+
1023
+ tk .MustExec (`prepare insert_stmt from 'insert into t values (?, ?)'` )
1024
+ tk .MustExec (`set @a=4, @b=4;` )
1025
+
1026
+ ch := make (chan struct {})
1027
+
1028
+ first := true
1029
+ testfailpoint .EnableCall (t , "github.com/pingcap/tidb/pkg/ddl/onJobUpdated" , func (job * model.Job ) {
1030
+ switch job .SchemaState {
1031
+ case model .StateWriteReorganization :
1032
+ tbl , _ := dom .InfoSchema ().TableByID (context .Background (), job .TableID )
1033
+ idx := tbl .Meta ().FindIndexByName ("idx" )
1034
+ switch idx .BackfillState {
1035
+ case model .BackfillStateRunning :
1036
+ if first {
1037
+ // generate plan, cache it, and make some row change to make
1038
+ // sure backfill state 'merging' is not skipped.
1039
+ tk .MustExec (`begin` )
1040
+ tk .MustExec (`delete from t where a = 4;` )
1041
+ tk .MustExec (`execute insert_stmt using @a, @b;` )
1042
+ tk .MustQuery ("select @@last_plan_from_cache;" ).Check (testkit .Rows ("0" ))
1043
+ tk .MustExec (`commit` )
1044
+
1045
+ tk .MustExec ("begin" )
1046
+ // Activate txn.
1047
+ tk .MustExec ("select * from t2" )
1048
+ first = false
1049
+ return
1050
+ }
1051
+ }
1052
+ }
1053
+ })
1054
+
1055
+ ddl .MockDMLExecutionMerging = func () {
1056
+ tk .MustExec (`delete from t where a = 4;` )
1057
+ // we must generate a new plan here, because the schema has changed since
1058
+ // the last plan was generated.
1059
+ tk .MustExec (`execute insert_stmt using @a, @b;` )
1060
+ tk .MustQuery ("select @@last_plan_from_cache;" ).Check (testkit .Rows ("0" ))
1061
+ tk .MustExec ("commit" )
1062
+ }
1063
+ require .NoError (t , failpoint .Enable ("github.com/pingcap/tidb/pkg/ddl/mockDMLExecutionMerging" , "1*return(true)->return(false)" ))
1064
+
1065
+ var wg sync.WaitGroup
1066
+ wg .Add (1 )
1067
+ go func () {
1068
+ <- ch
1069
+ tkDDL .MustExec ("alter table test.t add index idx(a);" )
1070
+ wg .Done ()
1071
+ }()
1072
+
1073
+ ch <- struct {}{}
1074
+ wg .Wait ()
1075
+
1076
+ tk .MustExec ("admin check table t" )
1077
+ }
1078
+
1079
+ >> >> >> > 93295 c233a3 (ddl : simplify TestMDLPreparePlanCacheExecuteInsert (#58113 ))
1000
1080
func TestMDLDisable2Enable (t * testing.T ) {
1001
1081
store , dom := testkit .CreateMockStoreAndDomain (t )
1002
1082
sv := server .CreateMockServer (t , store )
0 commit comments