summaryrefslogtreecommitdiff
path: root/mysql-test/main/mdl_sync.test
blob: fbecd6bf547316f174d5f530572e79b023655027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
#
# We need the Debug Sync Facility.
#
--source include/have_debug_sync.inc

# We need InnoDB tables for some of the tests.
--source include/have_innodb.inc

# Save the initial number of concurrent sessions.
--source include/count_sessions.inc


# Clean up resources used in this test case.
--disable_warnings
SET DEBUG_SYNC= 'RESET';
--enable_warnings

#
# Test the case of when a exclusive lock request waits for a
# shared lock being upgraded to a exclusive lock.
#

connect (con1,localhost,root,,test,,);
connect (con2,localhost,root,,test,,);
connect (con3,localhost,root,,test,,);

connection default;

--disable_warnings
drop table if exists t1,t2,t3;
--enable_warnings

create table t1 (i int);
create table t2 (i int);

--echo connection: default
lock tables t2 read;

connection con1;
--echo connection: con1
set debug_sync='mdl_upgrade_lock SIGNAL parked WAIT_FOR go';
--send alter table t1 rename t3

connection default;
--echo connection: default
set debug_sync= 'now WAIT_FOR parked';

connection con2;
--echo connection: con2
set debug_sync='mdl_acquire_lock_wait SIGNAL go';
--send drop table t1,t2

connection con1;
--echo connection: con1
--reap

connection default;
--echo connection: default
unlock tables;

connection con2;
--echo connection: con2
--error ER_BAD_TABLE_ERROR
--reap

connection default;
drop table t3;

disconnect con1;
disconnect con2;
disconnect con3;

# Clean up resources used in this test case.
--disable_warnings
SET DEBUG_SYNC= 'RESET';
--enable_warnings


--echo #
--echo # Basic test coverage for type-of-operation aware metadata locks.
--echo #
--disable_warnings
drop table if exists t1, t2, t3;
--enable_warnings
connect(mdl_con1,localhost,root,,);
connect(mdl_con2,localhost,root,,);
connect(mdl_con3,localhost,root,,);
connection default;
set debug_sync= 'RESET';
create table t1 (c1 int);

--echo # 
--echo # A) First let us check compatibility rules between differend kinds of
--echo #    type-of-operation aware metadata locks.
--echo #    Of course, these rules are already covered by the tests scattered
--echo #    across the test suite. But it still makes sense to have one place
--echo #    which covers all of them.
--echo #

--echo # 1) Acquire S (simple shared) lock on the table (by using HANDLER):
--echo #
handler t1 open;
--echo # 
connection mdl_con1;
--echo # Check that S, SH, SR and SW locks are compatible with it.
handler t1 open t;
handler t close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
select count(*) from t1;
insert into t1 values (1), (1);
--echo # Check that SU lock is compatible with it. To do this use ALTER TABLE
--echo # which will fail when constructing .frm and thus obtaining SU metadata
--echo # lock.
--error ER_KEY_COLUMN_DOES_NOT_EXITS
alter table t1 add index (not_exist);
--echo # Check that SNW lock is compatible with it. To do this use ALTER TABLE
--echo # which will fail during copying the table and thus obtaining SNW metadata
--echo # lock.
--error ER_DUP_ENTRY
alter table t1 add primary key (c1);
--echo # Check that X lock is incompatible with S lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con2;
--echo # Check that the above RENAME is blocked because of S lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # 
connection default;
--echo # Unblock RENAME TABLE.
handler t1 close;
--echo # 
connection mdl_con1;
--echo # Reaping RENAME TABLE.
--reap
--echo # Restore the original state of the things.
rename table t2 to t1;
--echo # 
connection default;
handler t1 open;
--echo # 
connection mdl_con1;
--echo # Check that upgrade from SNW to X is blocked by presence of S lock.
--echo # Sending:
--send alter table t1 add column c2 int;
--echo # 
connection mdl_con2;
--echo # Check that the above ALTER TABLE is blocked because of S lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "alter table t1 add column c2 int";
--source include/wait_condition.inc
--echo # 
connection default;
--echo # Unblock ALTER TABLE.
handler t1 close;
--echo # 
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--reap
--echo # Restore the original state of the things.
alter table t1 drop column c2;
--echo # 
connection default;
--echo #
--echo # 2) Acquire SH (shared high-priority) lock on the table.
--echo #    We have to involve DEBUG_SYNC facility for this as usually
--echo #    such kind of locks are short-lived.
--echo #
set debug_sync= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send select table_name, table_type, auto_increment, table_comment from information_schema.tables where table_schema='test' and table_name='t1';
--echo # 
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that S, SH, SR and SW locks are compatible with it.
handler t1 open;
handler t1 close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
select count(*) from t1;
insert into t1 values (1);
--echo # Check that SU lock is compatible with it. To do this use ALTER TABLE
--echo # which will fail when constructing .frm and thus obtaining SU metadata
--echo # lock.
--error ER_KEY_COLUMN_DOES_NOT_EXITS
alter table t1 add index (not_exist);
--echo # Check that SNW lock is compatible with it. To do this use ALTER TABLE
--echo # which will fail during copying the table and thus obtaining SNW metadata
--echo # lock.
--error ER_DUP_ENTRY
alter table t1 add primary key (c1);
--echo # Check that SNRW lock is compatible with SH lock.
lock table t1 write;
delete from t1 limit 1;
unlock tables;
--echo # Check that X lock is incompatible with SH lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con2;
--echo # Check that the above RENAME is blocked because of SH lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
set debug_sync= 'now SIGNAL finish';
--echo # 
connection default;
--echo # Reaping SELECT ... FROM I_S.
--reap
--echo # 
connection mdl_con1;
--echo # Reaping RENAME TABLE.
--reap
--echo # Restore the original state of the things.
rename table t2 to t1;
--echo # 
connection default;
set debug_sync= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send select table_name, table_type, auto_increment, table_comment from information_schema.tables where table_schema='test' and table_name='t1';
--echo # 
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that upgrade from SNW to X is blocked by presence of SH lock.
--echo # Sending:
--send alter table t1 add column c2 int;
--echo # 
connection mdl_con2;
--echo # Check that the above ALTER TABLE is blocked because of SH lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "alter table t1 add column c2 int";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
set debug_sync= 'now SIGNAL finish';
--echo # 
connection default;
--echo # Reaping SELECT ... FROM I_S.
--reap
--echo # 
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--reap
--echo # Restore the original state of the things.
alter table t1 drop column c2;
--echo # 
connection default;
set debug_sync= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--send select table_name, table_type, auto_increment, table_comment from information_schema.tables where table_schema='test' and table_name='t1';
--echo # 
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that upgrade from SNRW to X is blocked by presence of S lock.
lock table t1 write;
--echo # Sending:
--send alter table t1 add column c2 int;
--echo # 
connection mdl_con2;
--echo # Check that the above upgrade of SNRW to X in ALTER TABLE is blocked
--echo # because of S lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "alter table t1 add column c2 int";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
set debug_sync= 'now SIGNAL finish';
--echo # 
connection default;
--echo # Reaping SELECT ... FROM I_S.
--reap
--echo # 
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--reap
--echo # Restore the original state of the things.
alter table t1 drop column c2;
unlock tables;
--echo # 
connection default;
--echo #
--echo #
--echo # 3) Acquire SR lock on the table.
--echo #
--echo #
begin;
select count(*) from t1;
--echo # 
connection mdl_con1;
--echo # Check that S, SH, SR and SW locks are compatible with it.
handler t1 open;
handler t1 close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
select count(*) from t1;
insert into t1 values (1);
--echo # Check that SU lock is compatible with it. To do this use ALTER TABLE
--echo # which will fail when constructing .frm and thus obtaining SU metadata
--echo # lock.
--error ER_KEY_COLUMN_DOES_NOT_EXITS
alter table t1 add index (not_exist);
--echo # Check that SNW lock is compatible with it. To do this use ALTER TABLE
--echo # which will fail during copying the table and thus obtaining SNW metadata
--echo # lock.
--error ER_DUP_ENTRY
alter table t1 add primary key (c1);
--echo # Check that SNRW lock is not compatible with SR lock.
--echo # Sending:
--send lock table t1 write;
--echo # 
connection default;
--echo # Check that the above LOCK TABLES is blocked because of SR lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and 
        info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Unblock LOCK TABLES.
commit;
--echo # 
connection mdl_con1;
--echo # Reaping LOCK TABLES.
--reap
delete from t1 limit 1;
unlock tables;
--echo # 
connection default;
begin;
select count(*) from t1;
--echo # 
connection mdl_con1;
--echo # Check that X lock is incompatible with SR lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con2;
--echo # Check that the above RENAME is blocked because of SR lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # 
connection default;
--echo # Unblock RENAME TABLE.
commit;
--echo # 
connection mdl_con1;
--echo # Reaping RENAME TABLE.
--reap
--echo # Restore the original state of the things.
rename table t2 to t1;
--echo # 
connection default;
begin;
select count(*) from t1;
--echo # 
connection mdl_con1;
--echo # Check that upgrade from SNW to X is blocked by presence of SR lock.
--echo # Sending:
--send alter table t1 add column c2 int;
--echo # 
connection mdl_con2;
--echo # Check that the above ALTER TABLE is blocked because of SR lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "alter table t1 add column c2 int";
--source include/wait_condition.inc
--echo # 
connection default;
--echo # Unblock ALTER TABLE.
commit;
--echo # 
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--reap
--echo # Restore the original state of the things.
alter table t1 drop column c2;
--echo #
--echo # There is no need to check that upgrade from SNRW to X is blocked
--echo # by presence of SR lock because SNRW is incompatible with SR anyway.
--echo # 
--echo # 
connection default;
--echo #
--echo #
--echo # 4) Acquire SW lock on the table.
--echo #
--echo #
begin;
insert into t1 values (1);
--echo # 
connection mdl_con1;
--echo # Check that S, SH, SR and SW locks are compatible with it.
handler t1 open;
handler t1 close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
--echo # Disable result log to make test robust against
--echo # effects of concurrent insert.
--disable_result_log
select * from t1;
--enable_result_log
insert into t1 values (1);
--echo # Check that SU lock is compatible with it. To do this use ALTER TABLE
--echo # which will fail when constructing .frm and thus obtaining SU metadata
--echo # lock.
--error ER_KEY_COLUMN_DOES_NOT_EXITS
alter table t1 add index (not_exist);
--echo # Check that SNW lock is not compatible with SW lock.
--echo # Again we use ALTER TABLE which fails during copying
--echo # the table to avoid upgrade of SNW -> X.
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
connection default;
--echo # Check that the above ALTER TABLE is blocked because of SW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE.
commit;
--echo # 
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
connection default;
begin;
insert into t1 values (1);
--echo # 
connection mdl_con1;
--echo # Check that SNRW lock is not compatible with SW lock.
--echo # Sending:
--send lock table t1 write;
--echo # 
connection default;
--echo # Check that the above LOCK TABLES is blocked because of SW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Unblock LOCK TABLES.
commit;
--echo # 
connection mdl_con1;
--echo # Reaping LOCK TABLES.
--reap
delete from t1 limit 2;
unlock tables;
--echo # 
connection default;
begin;
insert into t1 values (1);
--echo # 
connection mdl_con1;
--echo # Check that X lock is incompatible with SW lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con2;
--echo # Check that the above RENAME is blocked because of SW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # 
connection default;
--echo # Unblock RENAME TABLE.
commit;
--echo # 
connection mdl_con1;
--echo # Reaping RENAME TABLE.
--reap
--echo # Restore the original state of the things.
rename table t2 to t1;
--echo #
--echo # There is no need to check that upgrade from SNW/SNRW to X is
--echo # blocked by presence of SW lock because SNW/SNRW is incompatible
--echo # with SW anyway.
--echo # 
--echo # 
connection default;
--echo #
--echo #
--echo # 5) Acquire SU lock on the table. We have to use DEBUG_SYNC for
--echo #    this, to prevent SU from being immediately upgraded to X.
--echo #
set debug_sync= 'alter_opened_table SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that S, SH, SR and SW locks are compatible with it.
handler t1 open;
handler t1 close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
select count(*) from t1;
delete from t1 limit 1;
--echo # Check that SU lock is incompatible with SU lock.
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
connection mdl_con2;
--echo # Check that the above ALTER is blocked because of SU lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # Unblock ALTERs.
set debug_sync= 'now SIGNAL finish';
--echo # 
connection default;
--echo # Reaping first ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
connection mdl_con1;
--echo # Reaping another ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
connection default;
set debug_sync= 'alter_opened_table SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that SNRW lock is incompatible with SU lock.
--echo # Sending:
--send lock table t1 write;
--echo # 
connection mdl_con2;
--echo # Check that the above LOCK TABLES is blocked because of SU lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Unblock ALTER and thus LOCK TABLES.
set debug_sync= 'now SIGNAL finish';
--echo # 
connection default;
--echo # Reaping ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
connection mdl_con1;
--echo # Reaping LOCK TABLES
--reap
insert into t1 values (1);
unlock tables;
--echo # 
connection default;
set debug_sync= 'alter_opened_table SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that X lock is incompatible with SU lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con2;
--echo # Check that the above RENAME is blocked because of SU lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Unblock ALTER and thus RENAME TABLE.
set debug_sync= 'now SIGNAL finish';
--echo # 
connection default;
--echo # Now we have ALTER TABLE with SU->SNW and RENAME TABLE with pending
--echo # X-lock. In this case ALTER TABLE should be chosen as victim.
--echo # Reaping ALTER TABLE.
--error ER_LOCK_DEADLOCK
--reap
--echo # 
connection mdl_con1;
--echo # Reaping RENAME TABLE
--reap
--echo # Revert back to original state of things.
rename table t2 to t1;
--echo #
--echo # There is no need to check that upgrade from SNW/SNRW to X is
--echo # blocked by presence of another SU lock because SNW/SNRW is
--echo # incompatible with SU anyway.
--echo # 
connection default;
--echo #
--echo #
--echo # 6) Acquire SNW lock on the table. We have to use DEBUG_SYNC for
--echo #    this, to prevent SNW from being immediately upgraded to X.
--echo #
set debug_sync= 'alter_table_copy_after_lock_upgrade SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t1 add primary key (c1), lock=shared, algorithm=copy;
--echo # 
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that S, SH and SR locks are compatible with it.
handler t1 open;
handler t1 close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
select count(*) from t1;
--echo # Check that SW lock is incompatible with SNW lock.
--echo # Sending:
--send delete from t1 limit 2;
--echo # 
connection mdl_con2;
--echo # Check that the above DELETE is blocked because of SNW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "delete from t1 limit 2";
--source include/wait_condition.inc
--echo # Unblock ALTER and thus DELETE.
set debug_sync= 'now SIGNAL finish';
--echo # 
connection default;
--echo # Reaping ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
connection mdl_con1;
--echo # Reaping DELETE.
--reap
--echo # 
connection default;
set debug_sync= 'alter_table_copy_after_lock_upgrade SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t1 add primary key (c1), lock=shared, algorithm=copy;
--echo # 
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that SU lock is incompatible with SNW lock.
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
connection mdl_con2;
--echo # Check that the above ALTER is blocked because of SNW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # Unblock ALTERs.
set debug_sync= 'now SIGNAL finish';
--echo # 
connection default;
--echo # Reaping first ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
connection mdl_con1;
--echo # Reaping another ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Note that we can't easily check SNW vs SNW locks since
--echo # SNW is only used by ALTER TABLE after upgrading from SU
--echo # and SU is also incompatible with SNW.
--echo #
connection default;
set debug_sync= 'alter_table_copy_after_lock_upgrade SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t1 add primary key (c1), lock=shared, algorithm=copy;
--echo # 
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that SNRW lock is incompatible with SNW lock.
--echo # Sending:
--send lock table t1 write;
--echo # 
connection mdl_con2;
--echo # Check that the above LOCK TABLES is blocked because of SNW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Unblock ALTER and thus LOCK TABLES.
set debug_sync= 'now SIGNAL finish';
--echo # 
connection default;
--echo # Reaping ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
connection mdl_con1;
--echo # Reaping LOCK TABLES
--reap
insert into t1 values (1);
unlock tables;
--echo # 
connection default;
set debug_sync= 'alter_table_copy_after_lock_upgrade SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t1 add primary key (c1), algorithm=copy, lock=shared;
--echo # 
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that X lock is incompatible with SNW lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con2;
--echo # Check that the above RENAME is blocked because of SNW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Unblock ALTER and thus RENAME TABLE.
set debug_sync= 'now SIGNAL finish';
--echo # 
connection default;
--echo # Reaping ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
connection mdl_con1;
--echo # Reaping RENAME TABLE
--reap
--echo # Revert back to original state of things.
rename table t2 to t1;
--echo #
--echo # There is no need to check that upgrade from SNW/SNRW to X is
--echo # blocked by presence of another SNW lock because SNW/SNRW is
--echo # incompatible with SNW anyway.
--echo # 
connection default;
--echo #
--echo #
--echo # 7) Acquire SNRW lock on the table. 
--echo #
--echo #
lock table t1 write;
--echo # 
connection mdl_con1;
--echo # Check that S and SH locks are compatible with it.
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
--echo # Check that SR lock is incompatible with SNRW lock.
--echo # Sending:
--send select count(*) from t1;
--echo # 
connection default;
--echo # Check that the above SELECT is blocked because of SNRW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "select count(*) from t1";
--source include/wait_condition.inc
--echo # Unblock SELECT.
unlock tables;
--echo # 
connection mdl_con1;
--echo # Reaping SELECT.
--reap
--echo # 
connection default;
lock table t1 write;
--echo # 
connection mdl_con1;
--echo # Check that SW lock is incompatible with SNRW lock.
--echo # Sending:
--send delete from t1 limit 1;
--echo # 
connection default;
--echo # Check that the above DELETE is blocked because of SNRW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "delete from t1 limit 1";
--source include/wait_condition.inc
--echo # Unblock DELETE.
unlock tables;
--echo # 
connection mdl_con1;
--echo # Reaping DELETE.
--reap
--echo # 
connection default;
lock table t1 write;
--echo # 
connection mdl_con1;
--echo # Check that SU lock is incompatible with SNRW lock.
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
connection default;
--echo # Check that the above ALTER is blocked because of SNRW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # Unblock ALTER.
unlock tables;
--echo # 
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Note that we can't easily check SNW vs SNRW locks since
--echo # SNW is only used by ALTER TABLE after upgrading from SU
--echo # and SU is also incompatible with SNRW.
--echo #
connection default;
lock table t1 write;
--echo # 
connection mdl_con1;
--echo # Check that SNRW lock is incompatible with SNRW lock.
--echo # Sending:
--send lock table t1 write;
--echo # 
connection default;
--echo # Check that the above LOCK TABLES is blocked because of SNRW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Unblock waiting LOCK TABLES.
unlock tables;
--echo # 
connection mdl_con1;
--echo # Reaping LOCK TABLES
--reap
insert into t1 values (1);
unlock tables;
--echo # 
connection default;
lock table t1 write;
--echo # 
connection mdl_con1;
--echo # Check that X lock is incompatible with SNRW lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection default;
--echo # Check that the above RENAME is blocked because of SNRW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
connection mdl_con1;
--echo # Reaping RENAME TABLE
--reap
--echo # Revert back to original state of things.
rename table t2 to t1;
--echo #
--echo # There is no need to check that upgrade from SNW/SNRW to X is
--echo # blocked by presence of another SNRW lock because SNW/SNRW is
--echo # incompatible with SNRW anyway.
--echo # 
connection default;
--echo #
--echo #
--echo # 8) Now do the same round of tests for X lock. We use additional
--echo #    table to get long-lived lock of this type.
--echo #
create table t2 (c1 int);
--echo # 
connection mdl_con2;
--echo # Take a lock on t2, so RENAME TABLE t1 TO t2 will get blocked
--echo # after acquiring X lock on t1.
lock tables t2 read;
--echo # 
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that S lock in incompatible with X lock.
--echo # Sending:
--send handler t1 open;
--echo # 
connection mdl_con2;
--echo # Check that the above HANDLER statement is blocked because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "handler t1 open";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection mdl_con1;
--echo # Reaping HANDLER.
--reap
handler t1 close;
--echo # 
connection mdl_con2;
--echo # Prepare for blocking RENAME TABLE.
lock tables t2 read;
--echo # 
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SH lock in incompatible with X lock.
--echo # Sending:
--send select column_name from information_schema.columns where table_schema='test' and table_name='t1';
--echo # 
connection mdl_con2;
--echo # Check that the above SELECT ... FROM I_S ... statement is blocked
--echo # because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info like "select column_name from information_schema.columns%";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection mdl_con1;
--echo # Reaping SELECT ... FROM I_S.
--reap
--echo # 
connection mdl_con2;
--echo # Prepare for blocking RENAME TABLE.
lock tables t2 read;
--echo # 
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SR lock in incompatible with X lock.
--echo # Sending:
--send select count(*) from t1;
--echo # 
connection mdl_con2;
--echo # Check that the above SELECT statement is blocked
--echo # because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and 
      info = "select count(*) from t1";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection mdl_con1;
--echo # Reaping SELECT.
--reap
--echo # 
connection mdl_con2;
--echo # Prepare for blocking RENAME TABLE.
lock tables t2 read;
--echo # 
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SW lock in incompatible with X lock.
--echo # Sending:
--send delete from t1 limit 1;
--echo # 
connection mdl_con2;
--echo # Check that the above DELETE statement is blocked
--echo # because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "delete from t1 limit 1";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection mdl_con1;
--echo # Reaping DELETE.
--reap
--echo # 
connection mdl_con2;
--echo # Prepare for blocking RENAME TABLE.
lock tables t2 read;
--echo # 
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SU lock is incompatible with X lock.
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
connection mdl_con2;
--echo # Check that the above ALTER statement is blocked
--echo # because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
connection default;
--echo # Reaping RENAME TABLE
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection mdl_con1;
--echo # Reaping ALTER.
--error ER_DUP_ENTRY
--reap
--echo #
--echo # Note that we can't easily check SNW vs X locks since
--echo # SNW is only used by ALTER TABLE after upgrading from SU
--echo # and SU is also incompatible with X.
--echo #
connection mdl_con2;
--echo # Prepare for blocking RENAME TABLE.
lock tables t2 read;
--echo # 
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SNRW lock is incompatible with X lock.
--echo # Sending:
--send lock table t1 write;
--echo # 
connection mdl_con2;
--echo # Check that the above LOCK TABLE statement is blocked
--echo # because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
connection default;
--echo # Reaping RENAME TABLE
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection mdl_con1;
--echo # Reaping LOCK TABLE.
--reap
unlock tables;
--echo # 
connection mdl_con2;
--echo # Prepare for blocking RENAME TABLE.
lock tables t2 read;
--echo # 
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that X lock is incompatible with X lock.
--echo # Sending:
--send rename table t1 to t3;
--echo # 
connection mdl_con2;
--echo # Check that the above RENAME statement is blocked
--echo # because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t3";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
connection default;
--echo # Reaping RENAME TABLE
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection mdl_con1;
--echo # Reaping RENAME.
--reap
rename table t3 to t1;

--echo #
--echo # B) Now let us test compatibility in cases when both locks
--echo #    are pending. I.e. let us test rules for priorities between
--echo #    different types of metadata locks.
--echo #
--echo #    Note: No tests for pending SU lock as this lock requires
--echo #          even stronger active or pending lock.
--echo #

--echo # 
connection mdl_con2;
--echo #
--echo # 1) Check compatibility for pending SNW lock.
--echo #
--echo # Acquire SW lock in order to create pending SNW lock later.
begin;
insert into t1 values (1);
--echo # 
connection default;
--echo # Add pending SNW lock.
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
connection mdl_con1;
--echo # Check that ALTER TABLE is waiting with pending SNW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # Check that S, SH and SR locks are compatible with pending SNW
handler t1 open t;
handler t close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
select count(*) from t1;
--echo # Check that SW is incompatible with pending SNW
--echo # Sending:
--send delete from t1 limit 1;
--echo # 
connection mdl_con2;
--echo # Check that the above DELETE is blocked because of pending SNW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "delete from t1 limit 1";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE.
commit;
--echo # 
connection default;
--echo # Reaping ALTER.
--error ER_DUP_ENTRY
--reap
--echo # 
connection mdl_con1;
--echo # Reaping DELETE.
--reap
--echo #
--echo # We can't do similar check for SNW, SNRW and X locks because
--echo # they will also be blocked by active SW lock.
--echo #
--echo # 
connection mdl_con2;
--echo #
--echo # 2) Check compatibility for pending SNRW lock.
--echo #
--echo # Acquire SR lock in order to create pending SNRW lock.
begin;
select count(*) from t1;
--echo # 
connection default;
--echo # Add pending SNRW lock.
--echo # Sending:
--send lock table t1 write;
--echo # 
connection mdl_con1;
--echo # Check that LOCK TABLE is waiting with pending SNRW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Check that S and SH locks are compatible with pending SNRW
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
--echo # Check that SR is incompatible with pending SNRW
--echo # Sending:
--send select count(*) from t1;
--echo # 
connection mdl_con2;
--echo # Check that the above SELECT is blocked because of pending SNRW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "select count(*) from t1";
--source include/wait_condition.inc
--echo # Unblock LOCK TABLE.
commit;
--echo # 
connection default;
--echo # Reaping LOCK TABLE.
--reap
unlock tables;
--echo # 
connection mdl_con1;
--echo # Reaping SELECT.
--reap
--echo # Restore pending SNRW lock.
--echo # 
connection mdl_con2;
begin;
select count(*) from t1;
--echo # 
connection default;
--echo # Sending:
--send lock table t1 write;
--echo # 
connection mdl_con1;
--echo # Check that LOCK TABLE is waiting with pending SNRW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Check that SW is incompatible with pending SNRW
--echo # Sending:
--send insert into t1 values (1);
--echo # 
connection mdl_con2;
--echo # Check that the above INSERT is blocked because of pending SNRW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "insert into t1 values (1)";
--source include/wait_condition.inc
--echo # Unblock LOCK TABLE.
commit;
--echo # 
connection default;
--echo # Reaping LOCK TABLE.
--reap
unlock tables;
--echo # 
connection mdl_con1;
--echo # Reaping INSERT.
--reap
--echo # Restore pending SNRW lock.
--echo # 
connection mdl_con2;
begin;
select count(*) from t1;
--echo # 
connection default;
--echo # Sending:
--send lock table t1 write;
--echo # 
connection mdl_con1;
--echo # Check that LOCK TABLE is waiting with pending SNRW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Check that SNW is compatible with pending SNRW
--echo # So ALTER TABLE statements are not starved by LOCK TABLEs.
--error ER_DUP_ENTRY
alter table t1 add primary key (c1);
--echo # 
connection mdl_con2;
--echo # Unblock LOCK TABLE.
commit;
--echo # 
connection default;
--echo # Reaping LOCK TABLE.
--reap
unlock tables;
--echo #
--echo # We can't do similar check for SNRW and X locks because
--echo # they will also be blocked by active SR lock.
--echo #
--echo # 
connection mdl_con2;
--echo #
--echo # 3) Check compatibility for pending X lock.
--echo #
--echo # Acquire SR lock in order to create pending X lock.
begin;
select count(*) from t1;
--echo # 
connection default;
--echo # Add pending X lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con1;
--echo # Check that RENAME TABLE is waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SH locks are compatible with pending X
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
--echo # Check that S is incompatible with pending X
--echo # Sending:
--send handler t1 open;
--echo # 
connection mdl_con2;
--echo # Check that the above HANDLER OPEN is blocked because of pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "handler t1 open";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
commit;
--echo # 
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection mdl_con1;
--echo # Reaping HANDLER t1 OPEN.
--reap
handler t1 close;
--echo # Restore pending X lock.
--echo # 
connection mdl_con2;
begin;
select count(*) from t1;
--echo # 
connection default;
--echo # Add pending X lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con1;
--echo # Check that RENAME TABLE is waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SR is incompatible with pending X
--echo # Sending:
--send select count(*) from t1;
--echo # 
connection mdl_con2;
--echo # Check that the above SELECT is blocked because of pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "select count(*) from t1";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
commit;
--echo # 
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection mdl_con1;
--echo # Reaping SELECT.
--reap
--echo # Restore pending X lock.
--echo # 
connection mdl_con2;
begin;
select count(*) from t1;
--echo # 
connection default;
--echo # Add pending X lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con1;
--echo # Check that RENAME TABLE is waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SW is incompatible with pending X
--echo # Sending:
--send delete from t1 limit 1;
--echo # 
connection mdl_con2;
--echo # Check that the above DELETE is blocked because of pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "delete from t1 limit 1";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
commit;
--echo # 
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection mdl_con1;
--echo # Reaping DELETE.
--reap
--echo # Restore pending X lock.
--echo # 
connection mdl_con2;
begin;
select count(*) from t1;
--echo # 
connection default;
--echo # Add pending X lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con1;
--echo # Check that RENAME TABLE is waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SNW is incompatible with pending X
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
connection mdl_con2;
--echo # Check that the above ALTER TABLE is blocked because of pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
commit;
--echo # 
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # Restore pending X lock.
--echo # 
connection mdl_con2;
handler t1 open;
--echo # 
connection default;
--echo # Add pending X lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection mdl_con1;
--echo # Check that RENAME TABLE is waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SNRW is incompatible with pending X
--echo # Sending:
--send lock table t1 write;
--echo # 
connection mdl_con3;
--echo # Check that the above LOCK TABLES is blocked because of pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "lock table t1 write";
--source include/wait_condition.inc
--echo # 
connection mdl_con2;
--echo # Unblock RENAME TABLE.
handler t1 close;
--echo # 
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection mdl_con1;
--echo # Reaping LOCK TABLES.
--reap
unlock tables;
--echo # 
connection default;

--echo #
--echo #
--echo # C) Now let us test how type-of-operation locks are handled in
--echo #    transactional context. Obviously we are mostly interested
--echo #    in conflicting types of locks.
--echo #
--echo #    Note: No tests for active/pending SU lock since
--echo #          ALTER TABLE is in its own transaction.
--echo #

--echo #
--echo # 1) Let us check how various locks used within transactional
--echo #    context interact with active/pending SNW lock.
--echo #
--echo #    We start with case when we are acquiring lock on the table
--echo #    which was not used in the transaction before.
begin;
select count(*) from t1;
--echo # 
connection mdl_con1;
--echo # Create an active SNW lock on t2.
--echo # We have to use DEBUG_SYNC facility as otherwise SNW lock
--echo # will be immediately released (or upgraded to X lock).
insert into t2 values (1), (1);
set debug_sync= 'alter_table_copy_after_lock_upgrade SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t2 add primary key (c1), algorithm=copy, lock=shared;
--echo # 
connection default;
set debug_sync= 'now WAIT_FOR locked';
--echo # SR lock should be acquired without any waiting.
select count(*) from t2;
commit;
--echo # Now let us check that we will wait in case of SW lock.
begin;
select count(*) from t1;
--echo # Sending:
--send insert into t2 values (1);
--echo # 
connection mdl_con2;
--echo # Check that the above INSERT is blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "insert into t2 values (1)";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE and thus INSERT.
set debug_sync= 'now SIGNAL finish';
--echo # 
connection mdl_con1;
--echo # Reap ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
connection default;
--echo # Reap INSERT.
--reap
commit;
--echo #
--echo # Now let us see what happens when we are acquiring lock on the table
--echo # which is already used in transaction.
--echo #
--echo # *) First, case when transaction which has SR lock on the table also
--echo #    locked in SNW mode acquires yet another SR lock and then tries
--echo #    to acquire SW lock.
begin;
select count(*) from t1;
--echo # 
connection mdl_con1;
--echo # Create an active SNW lock on t1.
set debug_sync= 'alter_table_copy_after_lock_upgrade SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t1 add primary key (c1), algorithm=copy, lock=shared;
--echo # 
connection default;
set debug_sync= 'now WAIT_FOR locked';
--echo # We should still be able to get SR lock without waiting.
select count(*) from t1;
--echo # Since the above ALTER TABLE is not upgrading SNW lock to X by waiting
--echo # for SW lock we won't create deadlock.
--echo # So the below INSERT should not end-up with ER_LOCK_DEADLOCK error.
--echo # Sending:
--send insert into t1 values (1);
--echo # 
connection mdl_con2;
--echo # Check that the above INSERT is blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "insert into t1 values (1)";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE and thus INSERT.
set debug_sync= 'now SIGNAL finish';
--echo # 
connection mdl_con1;
--echo # Reap ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
connection default;
--echo # Reap INSERT.
--reap
commit;
--echo #
--echo # **) Now test in which transaction that has SW lock on the table
--echo #     against which there is pending SNW lock acquires SR and SW
--echo #     locks on this table.
--echo #
begin;
insert into t1 values (1);
--echo # 
connection mdl_con1;
--echo # Create pending SNW lock on t1.
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
connection default;
--echo # Wait until ALTER TABLE starts waiting for SNW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # We should still be able to get both SW and SR locks without waiting.
select count(*) from t1;
delete from t1 limit 1;
--echo # Unblock ALTER TABLE.
commit;
--echo # 
connection mdl_con1;
--echo # Reap ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
connection default;
--echo #
--echo # 2) Now similar tests for active SNW lock which is being upgraded
--echo #    to X lock.
--echo #
--echo #    Again we start with case when we are acquiring lock on the
--echo #    table which was not used in the transaction before.
begin;
select count(*) from t1;
--echo # 
connection mdl_con2;
--echo # Start transaction which will prevent SNW -> X upgrade from
--echo # completing immediately.
begin;
select count(*) from t2;
--echo # 
connection mdl_con1;
--echo # Create SNW lock pending upgrade to X on t2.
--echo # Sending:
--send alter table t2 add column c2 int;
--echo # 
connection default;
--echo # Wait until ALTER TABLE starts waiting X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "alter table t2 add column c2 int";
--source include/wait_condition.inc
--echo # Check that attempt to acquire SR lock on t2 causes waiting.
--echo # Sending:
--send select count(*) from t2;
--echo # 
connection mdl_con2;
--echo # Check that the above SELECT is blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "select count(*) from t2";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE.
commit;
--echo # 
connection mdl_con1;
--echo # Reap ALTER TABLE.
--reap
--echo # 
connection default;
--echo # Reap SELECT.
--reap
commit;
--echo # Do similar check for SW lock.
begin;
select count(*) from t1;
--echo # 
connection mdl_con2;
--echo # Start transaction which will prevent SNW -> X upgrade from
--echo # completing immediately.
begin;
select count(*) from t2;
--echo # 
connection mdl_con1;
--echo # Create SNW lock pending upgrade to X on t2.
--echo # Sending:
--send alter table t2 drop column c2;
--echo # 
connection default;
--echo # Wait until ALTER TABLE starts waiting X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "alter table t2 drop column c2";
--source include/wait_condition.inc
--echo # Check that attempt to acquire SW lock on t2 causes waiting.
--echo # Sending:
--send insert into t2 values (1);
--echo # 
connection mdl_con2;
--echo # Check that the above INSERT is blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "insert into t2 values (1)";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE.
commit;
--echo # 
connection mdl_con1;
--echo # Reap ALTER TABLE.
--reap
--echo # 
connection default;
--echo # Reap INSERT.
--reap
commit;
--echo #
--echo # Test for the case in which we are acquiring lock on the table
--echo # which is already used in transaction.
--echo #
begin;
select count(*) from t1;
--echo # 
connection mdl_con1;
--echo # Create SNW lock pending upgrade to X.
--echo # Sending:
--send alter table t1 add column c2 int;
--echo # 
connection default;
--echo # Wait until ALTER TABLE starts waiting X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "alter table t1 add column c2 int";
--source include/wait_condition.inc
--echo # Check that transaction is still able to acquire SR lock.
select count(*) from t1;
--echo # Waiting trying to acquire SW lock will cause deadlock and
--echo # therefore should cause an error.
--error ER_LOCK_DEADLOCK
delete from t1 limit 1;
--echo # Unblock ALTER TABLE.
commit;
--echo # 
connection mdl_con1;
--echo # Reap ALTER TABLE.
--reap
--echo # 
connection default;
--echo #
--echo # 3) Check how various locks used within transactional context
--echo #    interact with active/pending SNRW lock.
--echo # 
--echo #    Once again we start with case when we are acquiring lock on
--echo #    the table which was not used in the transaction before.
begin;
select count(*) from t1;
--echo # 
connection mdl_con1;
lock table t2 write;
--echo # 
connection default;
--echo # Attempt to acquire SR should be blocked. It should
--echo # not cause errors as it does not creates deadlock.
--echo # Sending:
--send select count(*) from t2;
--echo # 
connection mdl_con1;
--echo # Check that the above SELECT is blocked 
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "select count(*) from t2";
--source include/wait_condition.inc
--echo # Unblock SELECT.
unlock tables;
--echo # 
connection default;
--echo # Reap SELECT.
--reap
commit;
--echo # Repeat the same test for SW lock.
begin;
select count(*) from t1;
--echo # 
connection mdl_con1;
lock table t2 write;
--echo # 
connection default;
--echo # Again attempt to acquire SW should be blocked and should
--echo # not cause any errors.
--echo # Sending:
--send delete from t2 limit 1;
--echo # 
connection mdl_con1;
--echo # Check that the above DELETE is blocked 
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "delete from t2 limit 1";
--source include/wait_condition.inc
--echo # Unblock DELETE.
unlock tables;
--echo # 
connection default;
--echo # Reap DELETE.
--reap
commit;
--echo #
--echo # Now coverage for the case in which we are acquiring lock on
--echo # the table which is already used in transaction and against
--echo # which there is a pending SNRW lock request.
--echo #
--echo # *) Let us start with case when transaction has only a SR lock.
--echo #
begin;
select count(*) from t1;
--echo # 
connection mdl_con1;
--echo # Sending:
--send lock table t1 write;
--echo # 
connection default;
--echo # Wait until LOCK TABLE is blocked creating pending request for X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Check that another instance of SR lock is granted without waiting.
select count(*) from t1;
--echo # Attempt to wait for SW lock will lead to deadlock, thus
--echo # the below statement should end with ER_LOCK_DEADLOCK error.
--error ER_LOCK_DEADLOCK
delete from t1 limit 1;
--echo # Unblock LOCK TABLES.
commit;
--echo # 
connection mdl_con1;
--echo # Reap LOCK TABLES.
--reap
unlock tables;
--echo # 
connection default;
--echo #
--echo # **) Now case when transaction has a SW lock.
--echo #
begin;
delete from t1 limit 1;
--echo # 
connection mdl_con1;
--echo # Sending:
--send lock table t1 write;
--echo # 
connection default;
--echo # Wait until LOCK TABLE is blocked creating pending request for X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Check that both SR and SW locks are granted without waiting
--echo # and errors.
select count(*) from t1;
insert into t1 values (1, 1);
--echo # Unblock LOCK TABLES.
commit;
--echo # 
connection mdl_con1;
--echo # Reap LOCK TABLES.
--reap
unlock tables;
--echo # 
connection default;
--echo #
--echo # 4) Check how various locks used within transactional context
--echo #    interact with active/pending X lock.
--echo # 
--echo #    As usual we start with case when we are acquiring lock on
--echo #    the table which was not used in the transaction before.
begin;
select count(*) from t1;
--echo # 
connection mdl_con2;
--echo # Start transaction which will prevent X lock from going away
--echo # immediately.
begin;
select count(*) from t2;
--echo # 
connection mdl_con1;
--echo # Create pending X lock on t2.
--echo # Sending:
--send rename table t2 to t3;
--echo # 
connection default;
--echo # Wait until RENAME TABLE starts waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t2 to t3";
--source include/wait_condition.inc
--echo # Check that attempt to acquire SR lock on t2 causes waiting.
--echo # Sending:
--send select count(*) from t2;
--echo # 
connection mdl_con2;
--echo # Check that the above SELECT is blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "select count(*) from t2";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
commit;
--echo # 
connection mdl_con1;
--echo # Reap RENAME TABLE.
--reap
--echo # 
connection default;
--echo # Reap SELECT.
--error ER_NO_SUCH_TABLE
--reap
commit;
rename table t3 to t2;
--echo # The same test for SW lock.
begin;
select count(*) from t1;
--echo # 
connection mdl_con2;
--echo # Start transaction which will prevent X lock from going away
--echo # immediately.
begin;
select count(*) from t2;
--echo # 
connection mdl_con1;
--echo # Create pending X lock on t2.
--echo # Sending:
--send rename table t2 to t3;
--echo # 
connection default;
--echo # Wait until RENAME TABLE starts waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t2 to t3";
--source include/wait_condition.inc
--echo # Check that attempt to acquire SW lock on t2 causes waiting.
--echo # Sending:
--send delete from t2 limit 1;
--echo # 
connection mdl_con2;
--echo # Check that the above DELETE is blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "delete from t2 limit 1";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
commit;
--echo # 
connection mdl_con1;
--echo # Reap RENAME TABLE.
--reap
--echo # 
connection default;
--echo # Reap DELETE.
--error ER_NO_SUCH_TABLE
--reap
commit;
rename table t3 to t2;
--echo #
--echo # Coverage for the case in which we are acquiring lock on
--echo # the table which is already used in transaction and against
--echo # which there is a pending X lock request.
--echo #
--echo # *) The first case is when transaction has only a SR lock.
--echo #
begin;
select count(*) from t1;
--echo # 
connection mdl_con1;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection default;
--echo # Wait until RENAME TABLE is blocked creating pending request for X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that another instance of SR lock is granted without waiting.
select count(*) from t1;
--echo # Attempt to wait for SW lock will lead to deadlock, thus
--echo # the below statement should end with ER_LOCK_DEADLOCK error.
--error ER_LOCK_DEADLOCK
delete from t1 limit 1;
--echo # Unblock RENAME TABLE.
commit;
--echo # 
connection mdl_con1;
--echo # Reap RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection default;
--echo #
--echo # **) The second case is when transaction has a SW lock.
--echo #
begin;
delete from t1 limit 1;
--echo # 
connection mdl_con1;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
connection default;
--echo # Wait until RENAME TABLE is blocked creating pending request for X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table metadata lock" and
      info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that both SR and SW locks are granted without waiting
--echo # and errors.
select count(*) from t1;
insert into t1 values (1, 1);
--echo # Unblock RENAME TABLE.
commit;
--echo # 
connection mdl_con1;
--echo # Reap RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
connection default;

--echo # Clean-up.
disconnect mdl_con1;
disconnect mdl_con2;
disconnect mdl_con3;
set debug_sync= 'RESET';
drop table t1, t2;

--echo #
--echo # Test coverage for basic deadlock detection in metadata
--echo # locking subsystem.
--echo #
--disable_warnings
drop tables if exists t0, t1, t2, t3, t4, t5;
--enable_warnings
set debug_sync= 'RESET';

connect(deadlock_con1,localhost,root,,);
connect(deadlock_con2,localhost,root,,);
connect(deadlock_con3,localhost,root,,);
connection default;
create table t1 (i int);
create table t2 (j int);
create table t3 (k int);
create table t4 (k int);

--echo #
--echo # Test for the case in which no deadlock occurs.
--echo #

--echo #
connection deadlock_con1;
begin;
insert into t1 values (1);

--echo #
connection deadlock_con2;
begin;
insert into t2 values (1);

--echo #
connection default;
--echo # Send:
--send rename table t2 to t0, t3 to t2, t0 to t3;

--echo #
connection deadlock_con1;
--echo # Wait until the above RENAME TABLE is blocked because it has to wait
--echo # for 'deadlock_con2' which holds shared metadata lock on 't2'.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "rename table t2 to t0, t3 to t2, t0 to t3";
--source include/wait_condition.inc
--echo # The below statement should wait for exclusive metadata lock
--echo # on 't2' to go away and should not produce ER_LOCK_DEADLOCK
--echo # as no deadlock is possible in this situation.
--echo # Send:
--send select * from t2;

--echo #
connection deadlock_con2;
--echo # Wait until the above SELECT * FROM t2 is starts waiting
--echo # for an exclusive metadata lock to go away.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "select * from t2";
--source include/wait_condition.inc
--echo #
--echo # Unblock RENAME TABLE by releasing shared metadata lock on t2.
commit;

--echo #
connection default;
--echo # Reap RENAME TABLE.
--reap

--echo #
connection deadlock_con1;
--echo # Reap SELECT.
--reap

--echo #
connection default;
--echo #
--echo # Let us check that in the process of waiting for conflicting lock
--echo # on table 't2' to go away transaction in connection 'deadlock_con1'
--echo # has not released metadata lock on table 't1'.
--echo # Send:
--send rename table t1 to t0, t3 to t1, t0 to t3;

--echo #
connection deadlock_con1;
--echo # Wait until the above RENAME TABLE is blocked because it has to wait
--echo # for 'deadlock_con1' which should still hold shared metadata lock on
--echo # table 't1'.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "rename table t1 to t0, t3 to t1, t0 to t3";
--source include/wait_condition.inc
--echo # Commit transaction to unblock RENAME TABLE.
commit;

--echo #
connection default;
--echo # Reap RENAME TABLE.
--reap

--echo #
--echo # Test for case when deadlock occurs and should be detected immediately.
--echo #

--echo #
connection deadlock_con1;
begin;
insert into t2 values (2);

--echo #
connection default;
--echo # Send:
--send rename table t2 to t0, t1 to t2, t0 to t1;

--echo #
connection deadlock_con1;
--echo # Wait until the above RENAME TABLE is blocked because it has to wait
--echo # for 'deadlock_con1' which holds shared metadata lock on 't2'.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "rename table t2 to t0, t1 to t2, t0 to t1";
--source include/wait_condition.inc
--echo # 
--echo # The below statement should not wait as doing so will cause deadlock.
--echo # Instead it should fail and emit ER_LOCK_DEADLOCK statement and 
--echo # transaction should be rolled back.
--error ER_LOCK_DEADLOCK
select * from t1;

--echo #
connection default;
--echo # Reap RENAME TABLE.
--reap

--echo #
--echo # Test for the case in which deadlock also occurs but not immediately.
--echo #

--echo #
connection deadlock_con1;
begin;
insert into t2 values (1);

--echo #
connection default;
lock table t1 write;

--echo #
connection deadlock_con1;
--echo # The below SELECT statement should wait for metadata lock
--echo # on table 't1' and should not produce ER_LOCK_DEADLOCK
--echo # immediately as no deadlock is possible at the moment.
--send select * from t1;

--echo #
connection deadlock_con2;
--echo # Wait until the above SELECT * FROM t1 is starts waiting
--echo # for an UNRW metadata lock to go away.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and info = "select * from t1";
--source include/wait_condition.inc

--echo # Send RENAME TABLE statement that will deadlock with the
--echo # SELECT statement and thus should abort the latter.
--send rename table t1 to t0, t2 to t1, t0 to t2;

--echo #
connection default;
--echo # Wait till above RENAME TABLE is blocked while holding
--echo # pending X lock on t1.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "rename table t1 to t0, t2 to t1, t0 to t2";
--source include/wait_condition.inc
--echo # Allow the above RENAME TABLE to acquire lock on t1 and
--echo # create pending lock on t2 thus creating deadlock.
unlock tables;

--echo #
connection deadlock_con1;
--echo # Since the latest RENAME TABLE entered in deadlock with SELECT
--echo # statement the latter should be aborted and emit ER_LOCK_DEADLOCK
--echo # error and transaction should be rolled back.
--echo # Reap SELECT * FROM t1.
--error ER_LOCK_DEADLOCK
--reap

--echo #
connection deadlock_con2;
--echo # Reap RENAME TABLE ... .
--reap;

--echo #
connection default;

drop tables t1, t2, t3, t4;

--echo #
--echo # Now, test case which shows that deadlock detection empiric
--echo # also takes into account requests for metadata lock upgrade.
--echo #
create table t1 (i int);
insert into t1 values (1);
--echo # Avoid race which occurs when SELECT in 'deadlock_con1' connection
--echo # accesses table before the above INSERT unlocks the table and thus
--echo # its result becomes visible to other connections.
select * from t1;

--echo #
connection deadlock_con1;
begin;
select * from t1;

--echo #
connection default;
--echo # Send:
--send alter table t1 add column j int, rename to t2;

--echo #
connection deadlock_con1;
--echo # Wait until the above ALTER TABLE ... RENAME acquires exclusive
--echo # metadata lock on 't2' and starts waiting for connection
--echo # 'deadlock_con1' which holds shared lock on 't1'.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "alter table t1 add column j int, rename to t2";
--source include/wait_condition.inc

--echo # The below statement should not wait as it will cause deadlock.
--echo # An appropriate error should be reported instead and transaction
--echo # should be rolled back.
--error ER_LOCK_DEADLOCK
select * from t2;

--echo #
connection default;
--echo # Reap ALTER TABLE ... RENAME.
--reap

drop table t2;

--echo #
--echo # Test that in situation when MDL subsystem detects a deadlock
--echo # but it turns out that it can be resolved by backing-off locks
--echo # acquired by one of participating transactions (which is
--echo # possible when one of transactions consists only of currently
--echo # executed statement, e.g. in autocommit mode) no error is
--echo # reported.
--echo #
create table t1 (i int);
create table t2 (j int);
--echo # Ensure that the below SELECT stops once it has acquired metadata
--echo # lock on table 't2'.
set debug_sync= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send select * from t2, t1

--echo #
connection deadlock_con1;
--echo # Wait till SELECT acquires MDL on 't2' and starts waiting for signal.
set debug_sync= 'now WAIT_FOR locked';
--echo # Sending:
--send lock tables t1 write, t2 write

--echo #
connection deadlock_con2;
--echo # Wait until LOCK TABLES acquires SNRW lock on 't1' and is blocked
--echo # while trying to acquire SNRW lock on 't1'.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "lock tables t1 write, t2 write";
--source include/wait_condition.inc
--echo # Resume SELECT execution, this should eventually unblock LOCK TABLES.
set debug_sync= 'now SIGNAL finish';

--echo #
connection deadlock_con1;
--echo # Reaping LOCK TABLES.
--reap
unlock tables;

--echo #
connection default;
--echo # Reaping SELECT. It succeed and not report ER_LOCK_DEADLOCK error.
--reap

drop tables t1, t2;

--echo #
--echo # Test coverage for situation in which a race has happened
--echo # during deadlock detection process which led to unwarranted
--echo # ER_LOCK_DEADLOCK error.
--echo #
create table t1 (i int);

--echo # Ensure that ALTER waits once it has acquired SNW lock.
set debug_sync='alter_table_copy_after_lock_upgrade SIGNAL parked1 WAIT_FOR go1';
--echo # Sending:
--send alter table t1 add column j int

--echo #
connection deadlock_con1;
--echo # Wait till ALTER acquires SNW lock and stops.
set debug_sync='now WAIT_FOR parked1';
--echo # Ensure that INSERT is paused once it detects that there is
--echo # a conflicting metadata lock so it has to wait, but before
--echo # deadlock detection is run.
set debug_sync='mdl_acquire_lock_wait SIGNAL parked2 WAIT_FOR go2';
--echo # Sending:
--send insert into t1 values ()

--echo #
connection deadlock_con2;
--echo # Wait till INSERT is paused.
set debug_sync='now WAIT_FOR parked2';
--echo # Resume ALTER execution. Eventually it will release its
--echo # metadata lock and INSERT's request for SW lock will be
--echo # satisified.
set debug_sync='now SIGNAL go1';

--echo #
connection default;
--echo # Reaping ALTER TABLE.
--reap
--echo # Add a new request for SNW lock to waiting graph.
--echo # Sending:
--send alter table t1 drop column j

--echo #
connection deadlock_con2;
--echo # Wait until ALTER is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "alter table t1 drop column j";
--source include/wait_condition.inc
--echo # Resume INSERT so it can start deadlock detection.
--echo #
--echo # At this point there is a discrepancy between the fact that INSERT's
--echo # SW lock is already satisfied, but INSERT's connection is still
--echo # marked as waiting for it. Looking for a loop in waiters graph
--echo # without additional checks has detected a deadlock (INSERT waits
--echo # for SW lock; which is not granted because of pending SNW lock from
--echo # ALTER; which waits for active SW lock from INSERT). Since requests
--echo # for SW and SNW locks have same weight ALTER was selected as a victim
--echo # and ended with ER_LOCK_DEADLOCK error.
set debug_sync='now SIGNAL go2';

--echo #
connection deadlock_con1;
--echo # Reaping INSERT.
--reap

--echo #
connection default;
--echo # Reaping ALTER. It should succeed and not produce ER_LOCK_DEADLOCK.
--reap

drop table t1;

--echo #
--echo # Now, test for a situation in which deadlock involves waiting not
--echo # only in MDL subsystem but also for TDC. Such deadlocks should be
--echo # successfully detected. If possible, they should be resolved without
--echo # resorting to ER_LOCK_DEADLOCK error.
--echo #
create table t1(i int);
create table t2(j int);

--echo #
--echo # First, let us check how we handle a simple scenario involving
--echo # waits in MDL and TDC.
--echo #
set debug_sync= 'RESET';

connection deadlock_con1;
--echo # Start a statement, which will acquire SR metadata lock on t1, open it
--echo # and then stop, before trying to acquire SW lock on t2 and opening it.
set debug_sync='open_tables_after_open_and_process_table SIGNAL parked WAIT_FOR go';
--echo # Sending:
--send select * from t1 where i in (select j from t2 for update)

connection deadlock_con2;
--echo # Wait till the above SELECT stops.
set debug_sync='now WAIT_FOR parked';
--echo # The below FLUSH TABLES WITH READ LOCK should acquire
--echo # SNW locks on t1 and t2 and wait till SELECT closes t1.
--echo # Sending:
send flush tables t1, t2 with read lock;

connection deadlock_con3;
--echo # Wait until FLUSH TABLES WITH t1, t2 READ LOCK starts waiting
--echo # for SELECT to close t1.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table flush" and
        info = "flush tables t1, t2 with read lock";
--source include/wait_condition.inc

--echo # Resume SELECT, so it tries to acquire SW lock on t1 and blocks,
--echo # creating a deadlock. This deadlock should be detected and resolved
--echo # by backing-off SELECT. As a result FTWRL should be able to finish.
set debug_sync='now SIGNAL go';

connection deadlock_con2;
--echo # Reap FLUSH TABLES WITH READ LOCK.
reap;
unlock tables;

connection deadlock_con1;
--echo # Reap SELECT.
reap;

--echo #
--echo # The same scenario with a slightly different order of events
--echo # which emphasizes that setting correct deadlock detector weights
--echo # for flush waits is important.
--echo #
set debug_sync= 'RESET';

connection deadlock_con2;
set debug_sync='flush_tables_with_read_lock_after_acquire_locks SIGNAL parked WAIT_FOR go';

--echo # The below FLUSH TABLES WITH READ LOCK should acquire
--echo # SNW locks on t1 and t2 and wait on debug sync point.
--echo # Sending:
send flush tables t1, t2 with read lock;

connection deadlock_con1;
--echo # Wait till FLUSH TABLE WITH READ LOCK stops.
set debug_sync='now WAIT_FOR parked';

--echo # Start statement which will acquire SR metadata lock on t1, open
--echo # it and then will block while trying to acquire SW lock on t2.
--echo # Sending:
send select * from t1 where i in (select j from t2 for update);

connection deadlock_con3;
--echo # Wait till the above SELECT blocks.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "select * from t1 where i in (select j from t2 for update)";
--source include/wait_condition.inc

--echo # Resume FLUSH TABLES, so it tries to flush t1, thus creating
--echo # a deadlock. This deadlock should be detected and resolved by
--echo # backing-off SELECT. As a result FTWRL should be able to finish.
set debug_sync='now SIGNAL go';

connection deadlock_con2;
--echo # Reap FLUSH TABLES WITH READ LOCK.
reap;
unlock tables;

connection deadlock_con1;
--echo # Reap SELECT.
reap;

--echo #
--echo # Now a more complex scenario involving two connections
--echo # waiting for MDL and one for TDC.
--echo #
set debug_sync= 'RESET';

connection deadlock_con1;
--echo # Start a statement which will acquire SR metadata lock on t2, open it
--echo # and then stop, before trying to acquire SR on t1 and opening it.
set debug_sync='open_tables_after_open_and_process_table SIGNAL parked WAIT_FOR go';
--echo # Sending:
send select * from t2, t1;

connection deadlock_con2;
--echo # Wait till the above SELECT stops.
set debug_sync='now WAIT_FOR parked';
--echo # The below FLUSH TABLES WITH READ LOCK should acquire
--echo # SNW locks on t2 and wait till SELECT closes t2.
--echo # Sending:
send flush tables t2 with read lock;

connection deadlock_con3;
--echo # Wait until FLUSH TABLES WITH READ LOCK starts waiting
--echo # for SELECT to close t2.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table flush" and
        info = "flush tables t2 with read lock";
--source include/wait_condition.inc

--echo # The below DROP TABLES should acquire X lock on t1 and start
--echo # waiting for X lock on t2.
--echo # Sending:
send drop tables t1, t2;

connection default;
--echo # Wait until DROP TABLES starts waiting for X lock on t2.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "drop tables t1, t2";
--source include/wait_condition.inc

--echo # Resume SELECT, so it tries to acquire SR lock on t1 and blocks,
--echo # creating a deadlock. This deadlock should be detected and resolved
--echo # by backing-off SELECT. As a result, FTWRL should be able to finish.
set debug_sync='now SIGNAL go';

connection deadlock_con2;
--echo # Reap FLUSH TABLES WITH READ LOCK.
reap;
--echo # Unblock DROP TABLES.
unlock tables;

connection deadlock_con3;
--echo # Reap DROP TABLES.
reap;

connection deadlock_con1;
--echo # Reap SELECT. It should emit error about missing table.
--error ER_NO_SUCH_TABLE
reap;

connection default;

set debug_sync= 'RESET';

disconnect deadlock_con1;
disconnect deadlock_con2;
disconnect deadlock_con3;


--echo #
--echo # Test for a scenario in which FLUSH TABLES <list> WITH READ LOCK
--echo # used to erroneously release metadata locks.
--echo # 
connect(con1,localhost,root,,);
connect(con2,localhost,root,,);
connection default;
--disable_warnings
drop tables if exists t1, t2;
--enable_warnings
set debug_sync= 'RESET';
create table t1(i int);
create table t2(j int);

connection con2;
set debug_sync='open_tables_after_open_and_process_table SIGNAL parked WAIT_FOR go';

--echo # The below FLUSH TABLES <list> WITH READ LOCK should acquire
--echo # SNW locks on t1 and t2, open table t1 and block on the debug
--echo # sync point.
--echo # Sending:
send flush tables t1, t2 with read lock;

connection con1;
--echo # Wait till FLUSH TABLES <list> WITH READ LOCK stops.
set debug_sync='now WAIT_FOR parked';

--echo # Start a statement which will flush all tables and thus
--echo # invalidate table t1 open by FLUSH TABLES <list> WITH READ LOCK.
--echo # Sending:
send flush tables;

connection default;
--echo # Wait till the above FLUSH TABLES blocks.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table flush" and
        info = "flush tables";
--source include/wait_condition.inc

--echo # Resume FLUSH TABLES <list> WITH READ LOCK, so it tries to open t2
--echo # discovers that its t1 is obsolete and tries to reopen all tables.
--echo # Such reopen should not cause releasing of SNW metadata locks
--echo # which would result in assertion failures.
set debug_sync='now SIGNAL go';

connection con2;
--echo # Reap FLUSH TABLES <list> WITH READ LOCK.
reap;
unlock tables;

connection con1;
--echo # Reap FLUSH TABLES.
reap;

--echo # Clean-up.
connection default;
drop tables t1, t2;
set debug_sync= 'RESET';
disconnect con1;
disconnect con2;


--echo #
--echo # Test for bug #46748 "Assertion in MDL_context::wait_for_locks()
--echo # on INSERT + CREATE TRIGGER".
--echo #
--disable_warnings
drop tables if exists t1, t2, t3, t4, t5;
--enable_warnings
--echo # Let us simulate scenario in which we open some tables from extended
--echo # part of prelocking set but then encounter conflicting metadata lock,
--echo # so have to back-off and wait for it to go away.
connect (con1root,localhost,root,,test,,);
connect (con2root,localhost,root,,test,,);
connection default;
create table t1 (i int);
create table t2 (j int);
create table t3 (k int);
create table t4 (l int);
create trigger t1_bi before insert on t1 for each row
  insert into t2 values (new.i);
create trigger t2_bi before insert on t2 for each row
  insert into t3 values (new.j);
--echo #
connection con1root;
lock tables t4 read;
--echo #
connection con2root;
--echo # Send :
--send rename table t3 to t5, t4 to t3;
--echo #
connection default;
--echo # Wait until the above RENAME TABLE adds pending requests for exclusive
--echo # metadata lock on its tables and blocks due to 't4' being used by LOCK
--echo # TABLES.
let $wait_condition= select count(*)= 1 from information_schema.processlist
                       where state= 'Waiting for table metadata lock' and
                             info='rename table t3 to t5, t4 to t3';
--source include/wait_condition.inc
--echo # Send :
--send insert into t1 values (1);
--echo #
connection con1root;
--echo # Wait until INSERT statement waits due to encountering pending
--echo # exclusive metadata lock on 't3'.
let $wait_condition= select count(*)= 1 from information_schema.processlist
                       where state= 'Waiting for table metadata lock' and
                             info='insert into t1 values (1)';
--source include/wait_condition.inc
unlock tables;
--echo #
connection con2root;
--echo # Reap RENAME TABLE.
--reap
--echo #
connection default;
--echo # Reap INSERT.
--reap
--echo # Clean-up.
disconnect con1root;
disconnect con2root;
drop tables t1, t2, t3, t5;


--echo #
--echo # Bug#42546 - Backup: RESTORE fails, thinking it finds an existing table
--echo #

--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings
set @save_log_output=@@global.log_output;
set global log_output=file;

connect(con2, localhost, root,,);

--echo #
--echo # Test 1: CREATE TABLE
--echo #

connection con2;
--echo # Start insert on the not-yet existing table
--echo # Wait after taking the MDL lock
SET DEBUG_SYNC= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--send INSERT INTO t1 VALUES(1,"def")

connection default;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Now INSERT has a MDL on the non-existent table t1.

--echo #
--echo # Continue the INSERT once CREATE waits for exclusive lock
SET DEBUG_SYNC= 'mdl_acquire_lock_wait SIGNAL finish';
--echo # Try to create that table.
--send CREATE TABLE t1 (c1 INT, c2 VARCHAR(100), KEY(c1))

--echo # Insert fails
connection con2;
--error ER_NO_SUCH_TABLE
--reap

connection default;
--reap;
SET DEBUG_SYNC= 'RESET';
SHOW TABLES;

--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings

--echo #
--echo # Test 2: CREATE TABLE LIKE
--echo #

CREATE TABLE t2 (c1 INT, c2 VARCHAR(100), KEY(c1));

connection con2;
--echo # Start insert on the not-yet existing table
--echo # Wait after taking the MDL
SET DEBUG_SYNC= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--send INSERT INTO t1 VALUES(1,"def")

connection default;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Now INSERT has a MDL on the non-existent table t1.

--echo #
--echo # Continue the INSERT once CREATE waits for exclusive lock
SET DEBUG_SYNC= 'mdl_acquire_lock_wait SIGNAL finish';
--echo # Try to create that table.
--send CREATE TABLE t1 LIKE t2

--echo # Insert fails
connection con2;
--error ER_NO_SUCH_TABLE
--reap

connection default;
--reap
SET DEBUG_SYNC= 'RESET';
SHOW TABLES;

DROP TABLE t2;
disconnect con2;
--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings

set global log_output=@save_log_output;


--echo #
--echo # Bug #46044 "MDL deadlock on LOCK TABLE + CREATE TABLE HIGH_PRIORITY
--echo #             FOR UPDATE"
--echo #
--disable_warnings
drop tables if exists t1, t2;
--enable_warnings
connect (con46044, localhost, root,,);
connect (con46044_2, localhost, root,,);
connect (con46044_3, localhost, root,,);
connection default;
create table t1 (i int);
insert into t1 values(1);

--echo # Let us check that we won't deadlock if during filling
--echo # of I_S table we encounter conflicting metadata lock
--echo # which owner is in its turn waiting for our connection.
lock tables t1 read;

connection con46044_2;
--echo # Sending:
--send update t1 set i = 2

connection con46044;

--echo # Waiting until UPDATE t1 SET ... is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table level lock" and
        info = "update t1 set i = 2";
--source include/wait_condition.inc

--echo # Sending:
--send create table t2 select * from t1;

connection default;
--echo # Waiting until CREATE TABLE ... SELECT ... is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table level lock" and
        info = "create table t2 select * from t1";
--source include/wait_condition.inc

--echo # First let us check that SHOW FIELDS/DESCRIBE doesn't
--echo # gets blocked and emits and error.
--error ER_WARN_I_S_SKIPPED_TABLE
show fields from t2;

--echo # Now test for I_S query which reads only .FRMs.
--echo #
--echo # Query below should only emit a warning.
select column_name from information_schema.columns
  where table_schema='test' and table_name='t2';

--echo # Finally, test for I_S query which does full-blown table open.
--echo #
--echo # Query below should not be blocked. Warning message should be
--echo # stored in the 'table_comment' column.
select table_name, table_type, auto_increment, table_comment
  from information_schema.tables where table_schema='test' and table_name='t2';

connection default;
unlock tables;

connection con46044;
--echo # Reaping CREATE TABLE ... SELECT ... .
--reap
drop table t2;

connection con46044_2;
--echo # Reaping UPDATE t1 statement
--reap

--echo #
--echo # Let us also check that queries to I_S wait for conflicting metadata
--echo # locks to go away instead of skipping table with a warning in cases
--echo # when deadlock is not possible. This is a nice thing from compatibility
--echo # and ease of use points of view.
--echo #
--echo # We check same three queries to I_S in this new situation.

connection con46044_2;
lock tables t1 read;

connection con46044_3;
--echo # Sending:
send update t1 set i = 3;

connection con46044;

--echo # Waiting until UPDATE t1 SET ... is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table level lock" and
        info = "update t1 set i = 3";
--source include/wait_condition.inc

--echo # Sending:
--send create table t2 select * from t1;

connection default;
--echo # Waiting until CREATE TABLE ... SELECT ... is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table level lock" and
        info = "create table t2 select * from t1";
--source include/wait_condition.inc

--echo # Let us check that SHOW FIELDS/DESCRIBE gets blocked.
--echo # Sending:
--send show fields from t2;

connection con46044_2;
--echo # Wait until SHOW FIELDS gets blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "show fields from t2";
--source include/wait_condition.inc

unlock tables;

connection con46044;
--echo # Reaping CREATE TABLE ... SELECT ... .
--reap

connection default;
--echo # Reaping SHOW FIELDS ...
--reap
drop table t2;

connection con46044_3;
--echo # Reaping UPDATE t1 statement
--reap

connection con46044_2;
lock tables t1 read;

connection con46044_3;
--echo # Sending:
--send update t1 set i = 4

connection con46044;

--echo # Waiting until UPDATE t1 SET ... is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table level lock" and
        info = "update t1 set i = 4";
--source include/wait_condition.inc

--echo # Sending:
--send create table t2 select * from t1;

connection default;
--echo # Waiting until CREATE TABLE ... SELECT ... is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table level lock" and
        info = "create table t2 select * from t1";
--source include/wait_condition.inc

--echo # Check that I_S query which reads only .FRMs gets blocked.
--echo # Sending:
--send select column_name from information_schema.columns where table_schema='test' and table_name='t2';

connection con46044_2;
--echo # Wait until SELECT COLUMN_NAME FROM I_S.COLUMNS  gets blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info like "select column_name from information_schema.columns%";
--source include/wait_condition.inc

unlock tables;

connection con46044;
--echo # Reaping CREATE TABLE ... SELECT ... .
--reap

connection default;
--echo # Reaping SELECT COLUMN_NAME FROM I_S.COLUMNS
--reap
drop table t2;

connection con46044_3;
--echo # Reaping UPDATE t1 statement
--reap

connection con46044_2;
lock tables t1 read;

connection con46044_3;
--echo # Sending:
--send update t1 set i = 5

connection con46044;

--echo # Waiting until UPDATE t1 SET ... is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table level lock" and
        info = "update t1 set i = 5";
--source include/wait_condition.inc

--echo # Sending:
--send create table t2 select * from t1;

connection default;
--echo # Waiting until CREATE TABLE ... SELECT ... is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table level lock" and
        info = "create table t2 select * from t1";
--source include/wait_condition.inc

--echo # Finally, check that I_S query which does full-blown table open
--echo # also gets blocked.
--echo # Sending:
--send select table_name, table_type, auto_increment, table_comment from information_schema.tables where table_schema='test' and table_name='t2';

connection con46044_2;
--echo # Wait until SELECT ... FROM I_S.TABLES gets blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info like "select table_name, table_type, auto_increment, table_comment from information_schema.tables%";
--source include/wait_condition.inc

unlock tables;

connection con46044;
--echo # Reaping CREATE TABLE ... SELECT ... .
--reap

connection default;
--echo # Reaping SELECT ... FROM I_S.TABLES
--reap
drop table t2;

connection con46044_3;
--echo # Reaping UPDATE t1 statement
--reap

connection default;
--echo # Clean-up.
disconnect con46044;
disconnect con46044_2;
disconnect con46044_3;
drop table t1;


--echo #
--echo # Test for bug #46273 "MySQL 5.4.4 new MDL: Bug#989 is not fully fixed
--echo #                      in case of ALTER".
--echo #
--disable_warnings
drop table if exists t1;
--enable_warnings
set debug_sync= 'RESET';
connect (con46273,localhost,root,,test,,);
connection default;
create table t1 (c1 int primary key, c2 int, c3 int);
insert into t1 values (1,1,0),(2,2,0),(3,3,0),(4,4,0),(5,5,0);

begin;
select * from t1 where c2 = 3;

--echo #
connection con46273;
set debug_sync='alter_table_copy_after_lock_upgrade SIGNAL alter_table_locked WAIT_FOR alter_go';
--send alter table t1 add column e int, rename to t2;

--echo #
connection default;
set debug_sync='now WAIT_FOR alter_table_locked';
set debug_sync='mdl_acquire_lock_wait SIGNAL alter_go';
--echo # The below statement should get ER_LOCK_DEADLOCK error
--echo # (i.e. it should not allow ALTER to proceed, and then
--echo # fail due to 't1' changing its name to 't2').
--error ER_LOCK_DEADLOCK
update t1 set c3=c3+1 where c2 = 3;

--echo #
connection con46273;
--echo # Reap ALTER TABLE.
--reap

--echo #
connection default;
disconnect con46273;
--echo # Clean-up.
set debug_sync= 'RESET';
drop table t2;


--echo #
--echo # Test for bug #46673 "Deadlock between FLUSH TABLES WITH READ LOCK
--echo #                      and DML".
--echo #
--disable_warnings
drop tables if exists t1;
--enable_warnings
connect (con46673, localhost, root,,);
connection default;
create table t1 (i int);

connection con46673;
begin;
insert into t1 values (1);

connection default;
--echo # Statement below should not get blocked. And if after some
--echo # changes to code it is there should not be a deadlock between
--echo # it and transaction from connection 'con46673'.
flush tables with read lock;
unlock tables;

connection con46673;
delete from t1 where i = 1;
commit;

connection default;
--echo # Clean-up
disconnect con46673;
drop table t1;


--echo #
--echo # Bug#48210 FLUSH TABLES WITH READ LOCK deadlocks 
--echo #           against concurrent CREATE PROCEDURE
--echo #

connect (con2, localhost, root);

--echo # Test 1: CREATE PROCEDURE

connection default;
--echo # Start CREATE PROCEDURE and open mysql.proc
SET DEBUG_SYNC= 'after_open_table_mdl_shared SIGNAL table_opened WAIT_FOR grlwait';
--send CREATE PROCEDURE p1() SELECT 1

connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR table_opened';
--echo # Check that FLUSH must wait to get the GRL
--echo # and let CREATE PROCEDURE continue
SET DEBUG_SYNC= 'mdl_acquire_lock_wait SIGNAL grlwait';
--send FLUSH TABLES WITH READ LOCK

connection default;
--reap

connection con2;
--reap
UNLOCK TABLES;

connection default;
SET DEBUG_SYNC= 'RESET';

--echo # Test 2: DROP PROCEDURE

connection default;
--echo # Start DROP PROCEDURE and open tables
SET DEBUG_SYNC= 'after_open_table_mdl_shared SIGNAL table_opened WAIT_FOR grlwait';
--send DROP PROCEDURE p1

connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR table_opened';
--echo # Check that FLUSH must wait to get the GRL
--echo # and let DROP PROCEDURE continue
SET DEBUG_SYNC= 'mdl_acquire_lock_wait SIGNAL grlwait';
--send FLUSH TABLES WITH READ LOCK

connection default;
--echo # Once FLUSH TABLES WITH READ LOCK starts waiting
--echo # DROP PROCEDURE will be waked up and will drop
--echo # procedure. Global read lock will be granted after
--echo # this statement ends.
--echo #
--echo # Reaping DROP PROCEDURE.
--reap

connection con2;
--echo # Reaping FTWRL.
--reap
UNLOCK TABLES;

connection default;
SET DEBUG_SYNC= 'RESET';

disconnect con2;


--echo #
--echo # Bug#50786 Assertion `thd->mdl_context.trans_sentinel() == __null' 
--echo #           failed in open_ltable()
--echo #

--echo # Supress warnings written to the log file
call mtr.add_suppression("Wait on a lock was aborted due to a pending exclusive lock");
--disable_warnings
DROP TABLE IF EXISTS t1, t2;
--enable_warnings

connect (con1,localhost,root);
connect (con2,localhost,root);
connect (con3,localhost,root);
connection default;

CREATE TABLE t1 (i INT);
CREATE TABLE t2 (i INT);

SET @old_general_log= @@global.general_log;
SET @@global.general_log= 1;

SET @old_log_output= @@global.log_output;
SET @@global.log_output= 'TABLE';

SET @old_sql_log_off= @@session.sql_log_off;
SET @@session.sql_log_off= 1;

--echo # connection: con1
connection con1;
HANDLER t1 OPEN;

--echo # connection: con3
connection con3;
SET @@session.sql_log_off= 1;

--echo # connection: con2
connection con2;
SET DEBUG_SYNC= 'thr_multi_lock_after_thr_lock SIGNAL parked WAIT_FOR go';

# The below statement will block on the debug sync point
# after it gets write lock on mysql.general_log table.
--echo # Sending:
--send SELECT 1

--echo # connection: con3
connection con3;
SET DEBUG_SYNC= 'now WAIT_FOR parked';

--echo # connection: con1
connection con1;
# This statement will block in open_ltable() when
# trying to write into mysql.general_log.
--echo # Sending:
--send SELECT 1

--echo # connection: con3
connection con3;
let $wait_condition=
  SELECT COUNT(*) = 1 FROM information_schema.processlist
  WHERE state = "Waiting for table level lock" and info = "SELECT 1";
--source include/wait_condition.inc
# The ALTER below will try to abort the statement in connection con1,
# since the latter waits on a table-level lock while having a HANDLER
# open. This will cause mysql_lock_tables() in con1 fail which before
# triggered the assert.
ALTER TABLE t1 ADD COLUMN j INT;

--echo # connection: default
connection default;
SET DEBUG_SYNC= 'now SIGNAL go';

--echo # connection: con1
connection con1;
--echo # Reaping SELECT 1
--reap
HANDLER t1 CLOSE;

--echo # connection: con2
connection con2;
--echo # Reaping SELECT 1
--reap

--echo # connection: default
connection default;
DROP TABLE t1, t2;
SET DEBUG_SYNC= 'RESET';
disconnect con1;
disconnect con2;
disconnect con3;
SET @@global.general_log= @old_general_log;
SET @@global.log_output= @old_log_output;
SET @@session.sql_log_off= @old_sql_log_off;


--echo #
--echo # Additional coverage for bug #50913 "Deadlock between
--echo # open_and_lock_tables_derived and MDL". The main test
--echo # case is in lock_multi.test
--echo #
--disable_warnings
drop table if exists t1;
--enable_warnings
set debug_sync= 'RESET';
connect (con50913_1,localhost,root);
connect (con50913_2,localhost,root);
connection default;
create table t1 (i int) engine=InnoDB;

connection con50913_1;
set debug_sync= 'alter_table_copy_after_lock_upgrade SIGNAL parked WAIT_FOR go';
--echo # Sending:
--send alter table t1 add column j int, ALGORITHM=COPY

connection default;
--echo # Wait until ALTER TABLE gets blocked on a sync point after
--echo # acquiring thr_lock.c lock.
set debug_sync= 'now WAIT_FOR parked';
--echo # The below statement should wait on MDL lock and not deadlock on
--echo # thr_lock.c lock.
--echo # Sending:
--send truncate table t1

connection con50913_2;
--echo # Wait until TRUNCATE TABLE is blocked on MDL lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "truncate table t1";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE.
set debug_sync= 'now SIGNAL go';

connection con50913_1;
--echo # Reaping ALTER TABLE.
--reap

connection default;
--echo # Reaping TRUNCATE TABLE.
--reap
disconnect con50913_1;
disconnect con50913_2;
set debug_sync= 'RESET';
drop table t1;


--echo #
--echo # Test for bug #50998 "Deadlock in MDL code during test
--echo #                      rqg_mdl_stability".
--echo # Also provides coverage for the case when addition of
--echo # waiting statement adds several loops in the waiters
--echo # graph and therefore several searches for deadlock
--echo # should be performed.
--disable_warnings
drop table if exists t1;
--enable_warnings
set debug_sync= 'RESET';
connect (con1,localhost,root);
connect (con2,localhost,root);
connect (con3,localhost,root);
connection default;
create table t1 (i int);

connection con1;
begin;
select * from t1;

connection con2;
begin;
select * from t1;

connection default;
--echo # Start ALTER TABLE which will acquire SNW lock and
--echo # table lock and get blocked on sync point.
set debug_sync= 'alter_table_copy_after_lock_upgrade SIGNAL parked WAIT_FOR go';
--echo # Sending:
--send alter table t1 add column j int

connection con1;
--echo # Wait until ALTER TABLE gets blocked on a sync point.
set debug_sync= 'now WAIT_FOR parked';
--echo # Sending:
--send insert into t1 values (1)

connection con2;
--echo # Sending:
--send insert into t1 values (1)

connection con3;
--echo # Wait until both 'con1' and 'con2' are blocked trying to acquire
--echo # SW lock on the table.
let $wait_condition=
  select count(*) = 2 from information_schema.processlist
  where state = "Waiting for table metadata lock" and
        info = "insert into t1 values (1)";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE. Since it will try to upgrade SNW to X lock
--echo # deadlock with two loops in waiting graph will occur. Both loops
--echo # should be found and DML statements in both 'con1' and 'con2'
--echo # should be aborted with ER_LOCK_DEADLOCK errors.
set debug_sync= 'now SIGNAL go';

connection con1;
--echo # Reaping INSERT. It should end with ER_LOCK_DEADLOCK error and
--echo # not wait indefinitely (as it happened before the bugfix).
--error ER_LOCK_DEADLOCK
--reap
commit;

connection con2;
--echo # Reaping INSERT.
--error ER_LOCK_DEADLOCK
--reap
commit;

connection default;
--echo # Reap ALTER TABLE.
--reap

disconnect con1;
disconnect con2;
disconnect con3;
connection default;
set debug_sync= 'RESET';
drop table t1;

--echo #
--echo # Bug#42643: InnoDB does not support replication of TRUNCATE TABLE
--echo #
--echo # Ensure that a acquired lock is not given up due to a conflict.
--echo #

connect (con1,localhost,root,,test,,);
connect (con2,localhost,root,,test,,);
connect (con3,localhost,root,,test,,);

connection default;

--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings

CREATE TABLE t1 (a INT) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1),(2),(3);

connection con1;
LOCK TABLES t1 WRITE;
SET debug_sync='upgrade_lock_for_truncate SIGNAL parked_truncate WAIT_FOR go_truncate';
send TRUNCATE TABLE t1;

connection default;
SET debug_sync='now WAIT_FOR parked_truncate';

connection con2;
SET debug_sync='after_open_table_ignore_flush SIGNAL parked_show WAIT_FOR go_show';
send SHOW FIELDS FROM t1;

connection default;
SET debug_sync='now WAIT_FOR parked_show';

connection con3;
SET debug_sync='after_flush_unlock SIGNAL parked_flush WAIT_FOR go_flush';
send FLUSH TABLES t1;

connection default;
SET debug_sync='now WAIT_FOR parked_flush';
SET debug_sync='now SIGNAL go_truncate';
--echo # Ensure that truncate waits for a exclusive lock
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
  WHERE state='Waiting for table metadata lock' AND info='TRUNCATE TABLE t1';
--source include/wait_condition.inc
SET debug_sync= 'now SIGNAL go_show';

connection con1;
--echo # Reaping...
reap;
UNLOCK TABLES;

connection con2;
--echo # Reaping...
reap;

connection default;
SET debug_sync= 'now SIGNAL go_flush';

connection con3;
--echo # Reaping...
reap;

disconnect con1;
disconnect con2;
disconnect con3;

connection default;
SET debug_sync= 'RESET';
DROP TABLE t1;


--echo #
--echo # Bug#52856 concurrent show columns or show full columns causes a crash!!!
--echo #
CREATE TABLE t1(a CHAR(255));

connect(con1, localhost, root);
SET DEBUG_SYNC= "get_schema_column SIGNAL waiting WAIT_FOR completed";
--send SHOW FULL COLUMNS FROM t1

connection default;
SET DEBUG_SYNC= "now WAIT_FOR waiting";
--replace_column 8 #
SHOW FULL COLUMNS FROM t1;
SET DEBUG_SYNC= "now SIGNAL completed";
--replace_column 8 #
connection con1;
--reap
connection default;
DROP TABLE t1;
disconnect con1;


--echo #
--echo # Tests for schema-scope locks
--echo #

--disable_warnings
DROP DATABASE IF EXISTS db1;
DROP DATABASE IF EXISTS db2;
--enable_warnings

connect (con2, localhost, root);
connect (con3, localhost, root);

--echo # Test 1: 
--echo # CREATE DATABASE blocks database DDL on the same database, but
--echo # not database DDL on different databases. Tests X vs X lock.
--echo #

connection default;
SET DEBUG_SYNC= 'after_wait_locked_schema_name SIGNAL locked WAIT_FOR blocked';
--echo # Sending:
--send CREATE DATABASE db1

connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Sending:
# This should block.
--send CREATE DATABASE db1

connection con3;
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
  WHERE state='Waiting for schema metadata lock' AND info='CREATE DATABASE db1';
--source include/wait_condition.inc
# This should not block.
CREATE DATABASE db2;
ALTER DATABASE db2 DEFAULT CHARACTER SET utf8;
DROP DATABASE db2;
SET DEBUG_SYNC= 'now SIGNAL blocked';

connection default;
--echo # Reaping: CREATE DATABASE db1
--reap

connection con2;
--echo # Reaping: CREATE DATABASE db1
--error ER_DB_CREATE_EXISTS
--reap

--echo # Test 2:
--echo # ALTER DATABASE blocks database DDL on the same database, but
--echo # not database DDL on different databases. Tests X vs X lock.
--echo #

connection default;
SET DEBUG_SYNC= 'after_wait_locked_schema_name SIGNAL locked WAIT_FOR blocked';
--echo # Sending:
--send ALTER DATABASE db1 DEFAULT CHARACTER SET utf8

connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Sending:
# This should block.
--send ALTER DATABASE db1 DEFAULT CHARACTER SET utf8

connection con3;
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
  WHERE state='Waiting for schema metadata lock'
  AND info='ALTER DATABASE db1 DEFAULT CHARACTER SET utf8';
--source include/wait_condition.inc
# This should not block.
CREATE DATABASE db2;
ALTER DATABASE db2 DEFAULT CHARACTER SET utf8;
DROP DATABASE db2;
SET DEBUG_SYNC= 'now SIGNAL blocked';

connection default;
--echo # Reaping: ALTER DATABASE db1 DEFAULT CHARACTER SET utf8
--reap

connection con2;
--echo # Reaping: ALTER DATABASE db1 DEFAULT CHARACTER SET utf8
--reap

connection default;
SET DEBUG_SYNC= 'after_wait_locked_schema_name SIGNAL locked WAIT_FOR blocked';
--echo # Sending:
--send ALTER DATABASE db1 DEFAULT CHARACTER SET utf8

connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Sending:
# This should also block.
--send DROP DATABASE db1

connection con3;
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
  WHERE state='Waiting for schema metadata lock' AND info='DROP DATABASE db1';
--source include/wait_condition.inc
SET DEBUG_SYNC= 'now SIGNAL blocked';

connection default;
--echo # Reaping: ALTER DATABASE db1 DEFAULT CHARACTER SET utf8
--reap

connection con2;
--echo # Reaping: DROP DATABASE db1
--reap
# Recreate the database
CREATE DATABASE db1;

--echo # Test 3:
--echo # Two ALTER..UPGRADE of the same database are mutually exclusive, but
--echo # two ALTER..UPGRADE of different databases are not. Tests X vs X lock.
--echo #

let $MYSQLD_DATADIR= `select @@datadir`;
# Manually make a 5.0 database from the template
--mkdir $MYSQLD_DATADIR/a-b-c
--copy_file $MYSQLD_DATADIR/db1/db.opt $MYSQLD_DATADIR/a-b-c/db.opt
--mkdir $MYSQLD_DATADIR/a-b-c-d
--copy_file $MYSQLD_DATADIR/db1/db.opt $MYSQLD_DATADIR/a-b-c-d/db.opt

connection default;
SET DEBUG_SYNC= 'after_wait_locked_schema_name SIGNAL locked WAIT_FOR blocked';
--echo # Sending:
--send ALTER DATABASE `#mysql50#a-b-c` UPGRADE DATA DIRECTORY NAME

connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Sending:
# This should block.
--send ALTER DATABASE `#mysql50#a-b-c` UPGRADE DATA DIRECTORY NAME

connection con3;
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
  WHERE state='Waiting for schema metadata lock'
  AND info='ALTER DATABASE `#mysql50#a-b-c` UPGRADE DATA DIRECTORY NAME';
--source include/wait_condition.inc
# This should not block.
ALTER DATABASE `#mysql50#a-b-c-d` UPGRADE DATA DIRECTORY NAME;
SET DEBUG_SYNC= 'now SIGNAL blocked';

connection default;
--echo # Reaping: ALTER DATABASE '#mysql50#a-b-c' UPGRADE DATA DIRECTORY NAME
--reap

connection con2;
--echo # Reaping: ALTER DATABASE '#mysql50#a-b-c' UPGRADE DATA DIRECTORY NAME
--error ER_BAD_DB_ERROR
--reap
DROP DATABASE `a-b-c`;
DROP DATABASE `a-b-c-d`;

--echo # Test 4:
--echo # DROP DATABASE blocks database DDL on the same database, but
--echo # not database DDL on different databases. Tests X vs X lock.
--echo #

connection default;
SET DEBUG_SYNC= 'after_wait_locked_schema_name SIGNAL locked WAIT_FOR blocked';
--echo # Sending:
--send DROP DATABASE db1

connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Sending:
# This should block.
--send DROP DATABASE db1

connection con3;
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
  WHERE state='Waiting for schema metadata lock' AND info='DROP DATABASE db1';
--source include/wait_condition.inc
# This should not block.
CREATE DATABASE db2;
ALTER DATABASE db2 DEFAULT CHARACTER SET utf8;
DROP DATABASE db2;
SET DEBUG_SYNC= 'now SIGNAL blocked';

connection default;
--echo # Reaping: DROP DATABASE db1
--reap

connection con2;
--echo # Reaping: DROP DATABASE db1
--error ER_DB_DROP_EXISTS
--reap

connection default;
CREATE DATABASE db1;
SET DEBUG_SYNC= 'after_wait_locked_schema_name SIGNAL locked WAIT_FOR blocked';
--echo # Sending:
--send DROP DATABASE db1

connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Sending:
# This should also block.
--send ALTER DATABASE db1 DEFAULT CHARACTER SET utf8

connection con3;
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
  WHERE state='Waiting for schema metadata lock'
  AND info='ALTER DATABASE db1 DEFAULT CHARACTER SET utf8';
--source include/wait_condition.inc
SET DEBUG_SYNC= 'now SIGNAL blocked';

connection default;
--echo # Reaping: DROP DATABASE db1
--reap

connection con2;
--echo # Reaping: ALTER DATABASE db1 DEFAULT CHARACTER SET utf8
# Error 1 is from ALTER DATABASE when the database does not exist.
# Listing the error twice to prevent result diffences based on filename.
--error 1,1
--reap


--echo # Test 5:
--echo # Locked database name prevents CREATE of tables in that database.
--echo # Tests X vs IX lock.
--echo #

connection default;
CREATE DATABASE db1;
SET DEBUG_SYNC= 'after_wait_locked_schema_name SIGNAL locked WAIT_FOR blocked';
--echo # Sending:
--send DROP DATABASE db1

connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Sending:
# This should block.
--send CREATE TABLE db1.t1 (a INT)

connection con3;
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
  WHERE state='Waiting for schema metadata lock' AND
        info='CREATE TABLE db1.t1 (a INT)';
--source include/wait_condition.inc
SET DEBUG_SYNC= 'now SIGNAL blocked';

connection default;
--echo # Reaping: DROP DATABASE db1
--reap

connection con2;
--echo # Reaping: CREATE TABLE db1.t1 (a INT)
--error ER_BAD_DB_ERROR
--reap

--echo # Test 6:
--echo # Locked database name prevents RENAME of tables to/from that database.
--echo # Tests X vs IX lock.
--echo #

connection default;
CREATE DATABASE db1;
CREATE TABLE db1.t1 (a INT);
SET DEBUG_SYNC= 'after_wait_locked_schema_name SIGNAL locked WAIT_FOR blocked';
--echo # Sending:
--send DROP DATABASE db1

connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Sending:
# This should block.
--send RENAME TABLE db1.t1 TO test.t1

connection con3;
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
  WHERE state='Waiting for schema metadata lock' AND
        info='RENAME TABLE db1.t1 TO test.t1';
--source include/wait_condition.inc
SET DEBUG_SYNC= 'now SIGNAL blocked';

connection default;
--echo # Reaping: DROP DATABASE db1
--reap

connection con2;
--echo # Reaping: RENAME TABLE db1.t1 TO test.t1
--error ER_NO_SUCH_TABLE
--reap

connection default;
CREATE DATABASE db1;
CREATE TABLE test.t2 (a INT);
SET DEBUG_SYNC= 'after_wait_locked_schema_name SIGNAL locked WAIT_FOR blocked';
--echo # Sending:
--send DROP DATABASE db1

connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Sending:
# This should block.
--send RENAME TABLE test.t2 TO db1.t2

connection con3;
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
  WHERE state='Waiting for schema metadata lock' AND
        info='RENAME TABLE test.t2 TO db1.t2';
--source include/wait_condition.inc
SET DEBUG_SYNC= 'now SIGNAL blocked';

connection default;
--echo # Reaping: DROP DATABASE db1
--reap

connection con2;
--echo # Reaping: RENAME TABLE test.t2 TO db1.t2
# Error 7 is from RENAME TABLE where the target database does not exist.
# Listing the error twice to prevent result diffences based on filename.
--error 7, 7
--reap
DROP TABLE test.t2;


--echo # Test 7:
--echo # Locked database name prevents DROP of tables in that database.
--echo # Tests X vs IX lock.
--echo #

connection default;
CREATE DATABASE db1;
CREATE TABLE db1.t1 (a INT);
SET DEBUG_SYNC= 'after_wait_locked_schema_name SIGNAL locked WAIT_FOR blocked';
--echo # Sending:
--send DROP DATABASE db1

connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Sending:
# This should block.
--send DROP TABLE db1.t1

connection con3;
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
  WHERE state='Waiting for schema metadata lock' AND info='DROP TABLE db1.t1';
--source include/wait_condition.inc
SET DEBUG_SYNC= 'now SIGNAL blocked';

connection default;
--echo # Reaping: DROP DATABASE db1
--reap

connection con2;
--echo # Reaping: DROP TABLE db1.t1
--error ER_BAD_TABLE_ERROR
--reap

connection default;
disconnect con2;
disconnect con3;
SET DEBUG_SYNC= 'RESET';

--echo #
--echo # End of tests for schema-scope locks
--echo #

--echo #
--echo # Tests of granted global S lock (FLUSH TABLE WITH READ LOCK)
--echo #

CREATE DATABASE db1;
CREATE TABLE db1.t1(a INT);
connect(con2, localhost, root);
connect(con3, localhost, root);

connection default;
FLUSH TABLE WITH READ LOCK;

connection con2;
# IX global lock should block
--send CREATE TABLE db1.t2(a INT)

connection default;
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
  WHERE state='Waiting for global read lock'
  AND info='CREATE TABLE db1.t2(a INT)';
--source include/wait_condition.inc
UNLOCK TABLES;

connection con2;
--echo # Reaping CREATE TABLE db1.t2(a INT)
--reap

connection default;
FLUSH TABLE WITH READ LOCK;

connection con2;
# X global lock should block
--send ALTER DATABASE db1 DEFAULT CHARACTER SET utf8

connection default;
let $wait_condition=SELECT COUNT(*)=1 FROM information_schema.processlist
  WHERE state='Waiting for global read lock'
  AND info='ALTER DATABASE db1 DEFAULT CHARACTER SET utf8';
--source include/wait_condition.inc
UNLOCK TABLES;

connection con2;
--echo # Reaping ALTER DATABASE db1 DEFAULT CHARACTER SET utf8
--reap

connection default;
FLUSH TABLE WITH READ LOCK;

connection con2;
# S global lock should not block
FLUSH TABLE WITH READ LOCK;
UNLOCK TABLES;

connection default;
UNLOCK TABLES;
DROP DATABASE db1;
disconnect con2;
disconnect con3;


--echo #
--echo # Bug#56292 Deadlock with ALTER TABLE and MERGE tables
--echo #

--disable_warnings
DROP TABLE IF EXISTS t1, t2, m1;
--enable_warnings

CREATE TABLE t1(a INT) engine=MyISAM;
CREATE TABLE t2(a INT) engine=MyISAM;
CREATE TABLE m1(a INT) engine=MERGE UNION=(t1, t2);

INSERT INTO t1 VALUES (1), (2);
INSERT INTO t2 VALUES (3), (4);

connect(con1, localhost, root);
connect(con2, localhost, root);
connect(con3, localhost, root);

connection con1;
--echo # We need EXECUTE 2 since ALTER TABLE does SU => SNW => X and we want
--echo # to stop at the second upgrade.
SET DEBUG_SYNC= 'mdl_upgrade_lock SIGNAL upgrade WAIT_FOR continue EXECUTE 2';
--echo # Sending:
--send ALTER TABLE m1 engine=MERGE UNION=(t2, t1)

connection con2;
--echo # Waiting for ALTER TABLE to try lock upgrade
SET DEBUG_SYNC= 'now WAIT_FOR upgrade';
SET DEBUG_SYNC= 'now SIGNAL continue';
SET DEBUG_SYNC= 'now WAIT_FOR upgrade';
--echo # Sending:
--send DELETE FROM t2 WHERE a = 3

connection con3;
--echo # Check that DELETE is waiting on a metadata lock and not a table lock.
let $wait_condition=
  SELECT COUNT(*) = 1 FROM information_schema.processlist
  WHERE state = "Waiting for table metadata lock" AND
        info = "DELETE FROM t2 WHERE a = 3";
--source include/wait_condition.inc
--echo # Now that DELETE blocks on a metadata lock, we should be able to do
--echo # SELECT * FROM m1 here. SELECT used to be blocked by a DELETE table
--echo # lock request.
--send SELECT * FROM m1 WHERE a < 3

connection default;
--echo # Resuming ALTER TABLE
SET DEBUG_SYNC= 'now SIGNAL continue';

connection con1;
--echo # Reaping: ALTER TABLE m1 engine=MERGE UNION=(t2, t1)
--reap
connection con2;
--echo # Reaping: DELETE FROM t2 WHERE a = 3
--reap
connection con3;
--echo # Reaping: SELECT * FROM m1 WHERE a < 3
--reap
connection default;
DROP TABLE m1, t1, t2;
SET DEBUG_SYNC= 'RESET';
disconnect con1;
disconnect con2;
disconnect con3;


--echo #
--echo # MDEV-12620 - set lock_wait_timeout = 1;flush tables with read lock;
--echo #              lock not released after timeout
--echo #
CREATE TABLE t1(a INT) ENGINE=InnoDB;
SET debug_sync='open_tables_after_open_and_process_table SIGNAL ready WAIT_FOR go';
send SELECT * FROM t1;

connect (con1,localhost,root,,);
SET debug_sync='now WAIT_FOR ready';
# lock_wait_timeout should be 0 in 10.3, so that we don't have to wait at all
SET lock_wait_timeout=1;
--error ER_LOCK_WAIT_TIMEOUT
FLUSH TABLES WITH READ LOCK;
SET debug_sync='now SIGNAL go';

connection default;
reap;
SET debug_sync='RESET';
DROP TABLE t1;

disconnect con1;


# Check that all connections opened by test cases in this file are really
# gone so execution of other tests won't be affected by their presence.
--source include/wait_until_count_sessions.inc