File: sys_vars.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (7729 lines) | stat: -rw-r--r-- 326,504 bytes parent folder | download
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
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
/* Copyright (c) 2009, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

/**
  @file
  Definitions of all server's session or global variables.

  How to add new variables:

  1. copy one of the existing variables, and edit the declaration.
  2. if you need special behavior on assignment or additional checks
     use ON_CHECK and ON_UPDATE callbacks.
  3. *Don't* add new Sys_var classes or uncle Occam will come
     with his razor to haunt you at nights

  Note - all storage engine variables (for example myisam_whatever)
  should go into the corresponding storage engine sources
  (for example in storage/myisam/ha_myisam.cc) !
*/

#include "sql/sys_vars.h"

#include "my_config.h"

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <zlib.h>
#include <atomic>
#include <limits>

#include "include/compression.h"

#include "my_loglevel.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/log_shared.h"
#include "mysql_com.h"
#include "sql/protocol.h"
#include "sql/rpl_trx_tracking.h"
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <algorithm>
#include <map>
#include <utility>

#include "ft_global.h"
#include "libbinlogevents/include/binlog_event.h"
#include "libbinlogevents/include/binlog_event.h"  // binary_log::max_log_event_size
#include "libbinlogevents/include/compression/zstd_comp.h"  // DEFAULT_COMPRESSION_LEVEL
#include "m_string.h"
#include "my_aes.h"  // my_aes_opmode_names
#include "my_command.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_dir.h"
#include "my_double2ulonglong.h"
#include "my_io.h"
#include "my_macros.h"
#include "my_sqlcommand.h"
#include "my_thread.h"
#include "my_thread_local.h"
#include "my_time.h"
#include "myisam.h"  // myisam_flush
#include "mysql/plugin_group_replication.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql_version.h"
#include "sql/auth/auth_acls.h"
#include "sql/auth/auth_common.h"  // validate_user_plugins
#include "sql/binlog.h"            // mysql_bin_log
#include "sql/changestreams/apply/replication_thread_status.h"
#include "sql/clone_handler.h"
#include "sql/conn_handler/connection_handler_impl.h"  // Per_thread_connection_handler
#include "sql/conn_handler/connection_handler_manager.h"  // Connection_handler_manager
#include "sql/conn_handler/socket_connection.h"  // MY_BIND_ALL_ADDRESSES
#include "sql/derror.h"                          // read_texts
#include "sql/discrete_interval.h"
#include "sql/events.h"          // Events
#include "sql/hostname_cache.h"  // host_cache_resize
#include "sql/log.h"
#include "sql/mdl.h"
#include "sql/my_decimal.h"
#include "sql/opt_trace_context.h"
#include "sql/options_mysqld.h"
#include "sql/protocol_classic.h"
#include "sql/psi_memory_key.h"
#include "sql/query_options.h"
#include "sql/rpl_group_replication.h"  // is_group_replication_running
#include "sql/rpl_handler.h"            // delegates_update_lock_type
#include "sql/rpl_info_factory.h"       // Rpl_info_factory
#include "sql/rpl_info_handler.h"       // INFO_REPOSITORY_TABLE
#include "sql/rpl_log_encryption.h"
#include "sql/rpl_mi.h"                 // Master_info
#include "sql/rpl_msr.h"                // channel_map
#include "sql/rpl_mta_submode.h"        // MTS_PARALLEL_TYPE_DB_NAME
#include "sql/rpl_replica.h"            // SLAVE_THD_TYPE
#include "sql/rpl_rli.h"                // Relay_log_info
#include "sql/rpl_write_set_handler.h"  // transaction_write_set_hashing_algorithms
#include "sql/server_component/log_builtins_filter_imp.h"  // until we have pluggable variables
#include "sql/server_component/log_builtins_imp.h"
#include "sql/session_tracker.h"
#include "sql/sp_head.h"  // SP_PSI_STATEMENT_INFO_COUNT
#include "sql/sql_lex.h"
#include "sql/sql_locale.h"            // my_locale_by_number
#include "sql/sql_parse.h"             // killall_non_super_threads
#include "sql/sql_show_processlist.h"  // pfs_processlist_enabled
#include "sql/sql_tmp_table.h"         // internal_tmp_mem_storage_engine_names
#include "sql/ssl_acceptor_context_operator.h"
#include "sql/system_variables.h"
#include "sql/table_cache.h"  // Table_cache_manager
#include "sql/transaction.h"  // trans_commit_stmt
#include "sql/transaction_info.h"
#include "sql/xa.h"
#include "template_utils.h"  // pointer_cast
#include "thr_lock.h"
#ifdef _WIN32
#include "sql/named_pipe.h"
#endif
#include "my_openssl_fips.h"

#ifdef WITH_LOCK_ORDER
#include "sql/debug_lock_order.h"
#endif /* WITH_LOCK_ORDER */

#ifdef WITH_PERFSCHEMA_STORAGE_ENGINE
#include "storage/perfschema/pfs_server.h"
#include "storage/perfschema/terminology_use_previous.h"
#endif /* WITH_PERFSCHEMA_STORAGE_ENGINE */

static constexpr const unsigned long DEFAULT_ERROR_COUNT{1024};
static constexpr const unsigned long DEFAULT_SORT_MEMORY{256UL * 1024UL};
static constexpr const unsigned HOST_CACHE_SIZE{128};
static constexpr const unsigned long SCHEMA_DEF_CACHE_DEFAULT{256};
static constexpr const unsigned long STORED_PROGRAM_DEF_CACHE_DEFAULT{256};
static constexpr const unsigned long TABLESPACE_DEF_CACHE_DEFAULT{256};

/**
  We must have room for at least 400 table definitions in the table
  cache, since otherwise there is no chance prepared
  statements that use these many tables can work.
  Prepared statements use table definition cache ids (table_map_id)
  as table version identifiers. If the table definition
  cache size is less than the number of tables used in a statement,
  the contents of the table definition cache is guaranteed to rotate
  between a prepare and execute. This leads to stable validation
  errors. In future we shall use more stable version identifiers,
  for now the only solution is to ensure that the table definition
  cache can contain at least all tables of a given statement.
*/
static constexpr const unsigned long TABLE_DEF_CACHE_MIN{400};
static constexpr const unsigned long SCHEMA_DEF_CACHE_MIN{256};
static constexpr const unsigned long STORED_PROGRAM_DEF_CACHE_MIN{256};
static constexpr const unsigned long TABLESPACE_DEF_CACHE_MIN{256};
/*
  Default time to wait before aborting a new client connection
  that does not respond to "initial server greeting" timely
*/
static constexpr const unsigned long CONNECT_TIMEOUT{10};

/* Defaults for deprecated "insert delayed" */
static constexpr const unsigned long DELAYED_LIMIT{100};
static constexpr const unsigned long DELAYED_QUEUE_SIZE{1000};
static constexpr const unsigned long DELAYED_WAIT_TIMEOUT{5 * 60};

static constexpr const unsigned long QUERY_ALLOC_BLOCK_SIZE{8192};
static constexpr const unsigned long QUERY_ALLOC_PREALLOC_SIZE{8192};
static constexpr const unsigned long TRANS_ALLOC_PREALLOC_SIZE{4096};
static constexpr const unsigned long RANGE_ALLOC_BLOCK_SIZE{4096};

// Including the switch in this set, makes its default 'on'
static constexpr const unsigned long long OPTIMIZER_SWITCH_DEFAULT{
    OPTIMIZER_SWITCH_INDEX_MERGE | OPTIMIZER_SWITCH_INDEX_MERGE_UNION |
    OPTIMIZER_SWITCH_INDEX_MERGE_SORT_UNION |
    OPTIMIZER_SWITCH_INDEX_MERGE_INTERSECT |
    OPTIMIZER_SWITCH_ENGINE_CONDITION_PUSHDOWN |
    OPTIMIZER_SWITCH_INDEX_CONDITION_PUSHDOWN | OPTIMIZER_SWITCH_MRR |
    OPTIMIZER_SWITCH_MRR_COST_BASED | OPTIMIZER_SWITCH_BNL |
    OPTIMIZER_SWITCH_MATERIALIZATION | OPTIMIZER_SWITCH_SEMIJOIN |
    OPTIMIZER_SWITCH_LOOSE_SCAN | OPTIMIZER_SWITCH_FIRSTMATCH |
    OPTIMIZER_SWITCH_DUPSWEEDOUT | OPTIMIZER_SWITCH_SUBQ_MAT_COST_BASED |
    OPTIMIZER_SWITCH_USE_INDEX_EXTENSIONS |
    OPTIMIZER_SWITCH_COND_FANOUT_FILTER | OPTIMIZER_SWITCH_DERIVED_MERGE |
    OPTIMIZER_SKIP_SCAN | OPTIMIZER_SWITCH_HASH_JOIN |
    OPTIMIZER_SWITCH_PREFER_ORDERING_INDEX |
    OPTIMIZER_SWITCH_DERIVED_CONDITION_PUSHDOWN};

static constexpr const unsigned long MYSQLD_NET_RETRY_COUNT{10};

TYPELIB bool_typelib = {array_elements(bool_values) - 1, "", bool_values,
                        nullptr};

static bool update_buffer_size(THD *, KEY_CACHE *key_cache,
                               ptrdiff_t offset [[maybe_unused]],
                               ulonglong new_value) {
  bool error = false;
  assert(offset == offsetof(KEY_CACHE, param_buff_size));

  if (new_value == 0) {
    if (key_cache == dflt_key_cache) {
      my_error(ER_WARN_CANT_DROP_DEFAULT_KEYCACHE, MYF(0));
      return true;
    }

    if (key_cache->key_cache_inited)  // If initied
    {
      /*
        Move tables using this key cache to the default key cache
        and clear the old key cache.
      */
      key_cache->in_init = true;
      mysql_mutex_unlock(&LOCK_global_system_variables);
      key_cache->param_buff_size = 0;
      ha_resize_key_cache(key_cache);
      ha_change_key_cache(key_cache, dflt_key_cache);
      /*
        We don't delete the key cache as some running threads my still be in
        the key cache code with a pointer to the deleted (empty) key cache
      */
      mysql_mutex_lock(&LOCK_global_system_variables);
      key_cache->in_init = false;
    }
    return error;
  }

  key_cache->param_buff_size = new_value;

  /* If key cache didn't exist initialize it, else resize it */
  key_cache->in_init = true;
  mysql_mutex_unlock(&LOCK_global_system_variables);

  if (!key_cache->key_cache_inited)
    error = ha_init_key_cache({}, key_cache);
  else
    error = ha_resize_key_cache(key_cache);

  mysql_mutex_lock(&LOCK_global_system_variables);
  key_cache->in_init = false;

  return error;
}

static bool update_keycache_param(THD *, KEY_CACHE *key_cache, ptrdiff_t offset,
                                  ulonglong new_value) {
  bool error = false;
  assert(offset != offsetof(KEY_CACHE, param_buff_size));

  keycache_var(key_cache, offset) = new_value;

  key_cache->in_init = true;
  mysql_mutex_unlock(&LOCK_global_system_variables);
  error = ha_resize_key_cache(key_cache);

  mysql_mutex_lock(&LOCK_global_system_variables);
  key_cache->in_init = false;

  return error;
}

/**
  Check if REPLICATION_APPLIER granted. Throw SQL error if not.

  Use this when setting session variables that are to be protected within
  replication applier context.

  @note For compatibility we also accept SUPER.

  @retval true failure
  @retval false success

  @param self the system variable to set value for
  @param thd the session context
  @param setv the SET operations metadata
 */
static bool check_session_admin_or_replication_applier(sys_var *self
                                                       [[maybe_unused]],
                                                       THD *thd,
                                                       set_var *setv) {
  assert(self->scope() != sys_var::GLOBAL);
  Security_context *sctx = thd->security_context();
  if ((setv->type == OPT_SESSION || setv->type == OPT_DEFAULT) &&
      !sctx->has_global_grant(STRING_WITH_LEN("REPLICATION_APPLIER")).first &&
      !sctx->has_global_grant(STRING_WITH_LEN("SESSION_VARIABLES_ADMIN"))
           .first &&
      !sctx->has_global_grant(STRING_WITH_LEN("SYSTEM_VARIABLES_ADMIN"))
           .first &&
      !sctx->check_access(SUPER_ACL)) {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0),
             "SUPER, SYSTEM_VARIABLES_ADMIN, SESSION_VARIABLES_ADMIN or "
             "REPLICATION_APPLIER");
    return true;
  }
  return false;
}

/**
  Utility method that checks if user has correct session administrative
  dynamic privileges.
  @return 0 on success, 1 on failure.
*/
static bool check_session_admin_privileges_only(sys_var *self [[maybe_unused]],
                                                THD *thd, set_var *setv) {
  // Privilege check for global variable must have already done before.
  assert(self->scope() != sys_var::GLOBAL);
  Security_context *sctx = thd->security_context();
  if ((setv->type == OPT_SESSION || setv->type == OPT_DEFAULT) &&
      !sctx->has_global_grant(STRING_WITH_LEN("SESSION_VARIABLES_ADMIN"))
           .first &&
      !sctx->has_global_grant(STRING_WITH_LEN("SYSTEM_VARIABLES_ADMIN"))
           .first) {
    return true;
  }
  return false;
}

/**
  Check if SESSION_VARIABLES_ADMIN granted. Throw SQL error if not.

  Use this when setting session variables that are sensitive and should
  be protected.

  We also accept SYSTEM_VARIABLES_ADMIN since it doesn't make a lot of
  sense to be allowed to set the global variable and not the session ones.

  @retval true failure
  @retval false success

  @param self the system variable to set value for
  @param thd the session context
  @param setv the SET operations metadata
 */
static bool check_session_admin_no_super(sys_var *self, THD *thd,
                                         set_var *setv) {
  if (check_session_admin_privileges_only(self, thd, setv)) {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0),
             "SYSTEM_VARIABLES_ADMIN or SESSION_VARIABLES_ADMIN");
    return true;
  }
  return false;
}

/**
  Check if SESSION_VARIABLES_ADMIN granted. Throw SQL error if not.

  Use this when setting session variables that are sensitive and should
  be protected.

  We also accept SYSTEM_VARIABLES_ADMIN since it doesn't make a lot of
  sense to be allowed to set the global variable and not the session ones.

  @note For compatibility we also accept SUPER.

  @retval true failure
  @retval false success

  @param self the system variable to set value for
  @param thd the session context
  @param setv the SET operations metadata
 */
static bool check_session_admin(sys_var *self, THD *thd, set_var *setv) {
  Security_context *sctx = thd->security_context();
  if (check_session_admin_privileges_only(self, thd, setv) &&
      !sctx->check_access(SUPER_ACL)) {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0),
             "SUPER, SYSTEM_VARIABLES_ADMIN or SESSION_VARIABLES_ADMIN");
    return true;
  }
  return false;
}

/*
  The rule for this file: everything should be 'static'. When a sys_var
  variable or a function from this file is - in very rare cases - needed
  elsewhere it should be explicitly declared 'export' here to show that it's
  not a mistakenly forgotten 'static' keyword.
*/
#define export /* not static */

#ifdef WITH_LOCK_ORDER

#define LO_TRAILING_PROPERTIES                                          \
  NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(NULL), ON_UPDATE(NULL), NULL, \
      sys_var::PARSE_EARLY

static Sys_var_bool Sys_lo_enabled("lock_order", "Enable the lock order.",
                                   READ_ONLY GLOBAL_VAR(lo_param.m_enabled),
                                   CMD_LINE(OPT_ARG), DEFAULT(false),
                                   LO_TRAILING_PROPERTIES);

static Sys_var_charptr Sys_lo_out_dir("lock_order_output_directory",
                                      "Lock order output directory.",
                                      READ_ONLY GLOBAL_VAR(lo_param.m_out_dir),
                                      CMD_LINE(OPT_ARG), IN_FS_CHARSET,
                                      DEFAULT(nullptr), LO_TRAILING_PROPERTIES);

static Sys_var_charptr Sys_lo_dep_1(
    "lock_order_dependencies", "Lock order dependencies file.",
    READ_ONLY GLOBAL_VAR(lo_param.m_dependencies_1), CMD_LINE(OPT_ARG),
    IN_FS_CHARSET, DEFAULT(nullptr), LO_TRAILING_PROPERTIES);

static Sys_var_charptr Sys_lo_dep_2(
    "lock_order_extra_dependencies", "Lock order extra dependencies file.",
    READ_ONLY GLOBAL_VAR(lo_param.m_dependencies_2), CMD_LINE(OPT_ARG),
    IN_FS_CHARSET, DEFAULT(nullptr), LO_TRAILING_PROPERTIES);

static Sys_var_bool Sys_lo_print_txt("lock_order_print_txt",
                                     "Print the lock_order.txt file.",
                                     READ_ONLY GLOBAL_VAR(lo_param.m_print_txt),
                                     CMD_LINE(OPT_ARG), DEFAULT(false),
                                     LO_TRAILING_PROPERTIES);

static Sys_var_bool Sys_lo_trace_loop(
    "lock_order_trace_loop", "Enable tracing for all loops.",
    READ_ONLY GLOBAL_VAR(lo_param.m_trace_loop), CMD_LINE(OPT_ARG),
    DEFAULT(false), LO_TRAILING_PROPERTIES);

static Sys_var_bool Sys_lo_debug_loop(
    "lock_order_debug_loop", "Enable debugging for all loops.",
    READ_ONLY GLOBAL_VAR(lo_param.m_debug_loop), CMD_LINE(OPT_ARG),
    DEFAULT(false), LO_TRAILING_PROPERTIES);

static Sys_var_bool Sys_lo_trace_missing_arc(
    "lock_order_trace_missing_arc", "Enable tracing for all missing arcs.",
    READ_ONLY GLOBAL_VAR(lo_param.m_trace_missing_arc), CMD_LINE(OPT_ARG),
    DEFAULT(true), LO_TRAILING_PROPERTIES);

static Sys_var_bool Sys_lo_debug_missing_arc(
    "lock_order_debug_missing_arc", "Enable debugging for all missing arcs.",
    READ_ONLY GLOBAL_VAR(lo_param.m_debug_missing_arc), CMD_LINE(OPT_ARG),
    DEFAULT(false), LO_TRAILING_PROPERTIES);

static Sys_var_bool Sys_lo_trace_missing_unlock(
    "lock_order_trace_missing_unlock", "Enable tracing for all missing unlocks",
    READ_ONLY GLOBAL_VAR(lo_param.m_trace_missing_unlock), CMD_LINE(OPT_ARG),
    DEFAULT(true), LO_TRAILING_PROPERTIES);

static Sys_var_bool Sys_lo_debug_missing_unlock(
    "lock_order_debug_missing_unlock",
    "Enable debugging for all missing unlocks",
    READ_ONLY GLOBAL_VAR(lo_param.m_debug_missing_unlock), CMD_LINE(OPT_ARG),
    DEFAULT(false), LO_TRAILING_PROPERTIES);

static Sys_var_bool Sys_lo_trace_missing_key(
    "lock_order_trace_missing_key",
    "Enable trace for missing performance schema keys",
    READ_ONLY GLOBAL_VAR(lo_param.m_trace_missing_key), CMD_LINE(OPT_ARG),
    DEFAULT(false), LO_TRAILING_PROPERTIES);

static Sys_var_bool Sys_lo_debug_missing_key(
    "lock_order_debug_missing_key",
    "Enable debugging for missing performance schema keys",
    READ_ONLY GLOBAL_VAR(lo_param.m_debug_missing_key), CMD_LINE(OPT_ARG),
    DEFAULT(false), LO_TRAILING_PROPERTIES);

#endif /* WITH_LOCK_ORDER */

#ifdef WITH_PERFSCHEMA_STORAGE_ENGINE

#define PFS_TRAILING_PROPERTIES                                         \
  NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(NULL), ON_UPDATE(NULL), NULL, \
      sys_var::PARSE_EARLY

static Sys_var_bool Sys_pfs_enabled("performance_schema",
                                    "Enable the performance schema.",
                                    READ_ONLY GLOBAL_VAR(pfs_param.m_enabled),
                                    CMD_LINE(OPT_ARG), DEFAULT(true),
                                    PFS_TRAILING_PROPERTIES);

static Sys_var_charptr Sys_pfs_instrument(
    "performance_schema_instrument",
    "Default startup value for a performance schema instrument.",
    READ_ONLY NOT_VISIBLE GLOBAL_VAR(pfs_param.m_pfs_instrument),
    CMD_LINE(OPT_ARG, OPT_PFS_INSTRUMENT), IN_FS_CHARSET, DEFAULT(""),
    PFS_TRAILING_PROPERTIES);

/**
  Update the performance_schema_show_processlist.
  Warn that the use of information_schema processlist is deprecated.
*/
static bool performance_schema_show_processlist_update(sys_var *, THD *thd,
                                                       enum_var_type) {
  push_warning_printf(thd, Sql_condition::SL_WARNING,
                      ER_WARN_DEPRECATED_WITH_NOTE,
                      ER_THD(thd, ER_WARN_DEPRECATED_WITH_NOTE),
                      "@@performance_schema_show_processlist",
                      "When it is removed, SHOW PROCESSLIST will always use the"
                      " performance schema implementation.");

  return false;
}

static Sys_var_bool Sys_pfs_processlist(
    "performance_schema_show_processlist",
    "Default startup value to enable SHOW PROCESSLIST "
    "in the performance schema.",
    GLOBAL_VAR(pfs_processlist_enabled), CMD_LINE(OPT_ARG), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(performance_schema_show_processlist_update), nullptr,
    sys_var::PARSE_NORMAL);

static Sys_var_bool Sys_pfs_consumer_events_stages_current(
    "performance_schema_consumer_events_stages_current",
    "Default startup value for the events_stages_current consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_events_stages_current_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(false), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_events_stages_history(
    "performance_schema_consumer_events_stages_history",
    "Default startup value for the events_stages_history consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_events_stages_history_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(false), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_events_stages_history_long(
    "performance_schema_consumer_events_stages_history_long",
    "Default startup value for the events_stages_history_long consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_events_stages_history_long_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(false), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_events_statements_cpu(
    "performance_schema_consumer_events_statements_cpu",
    "Default startup value for the events_statements_cpu consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_events_statements_cpu_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(false), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_events_statements_current(
    "performance_schema_consumer_events_statements_current",
    "Default startup value for the events_statements_current consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_events_statements_current_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(true), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_events_statements_history(
    "performance_schema_consumer_events_statements_history",
    "Default startup value for the events_statements_history consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_events_statements_history_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(true), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_events_statements_history_long(
    "performance_schema_consumer_events_statements_history_long",
    "Default startup value for the events_statements_history_long consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_events_statements_history_long_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(false), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_events_transactions_current(
    "performance_schema_consumer_events_transactions_current",
    "Default startup value for the events_transactions_current consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_events_transactions_current_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(true), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_events_transactions_history(
    "performance_schema_consumer_events_transactions_history",
    "Default startup value for the events_transactions_history consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_events_transactions_history_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(true), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_events_transactions_history_long(
    "performance_schema_consumer_events_transactions_history_long",
    "Default startup value for the events_transactions_history_long consumer.",
    READ_ONLY NOT_VISIBLE GLOBAL_VAR(
        pfs_param.m_consumer_events_transactions_history_long_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(false), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_events_waits_current(
    "performance_schema_consumer_events_waits_current",
    "Default startup value for the events_waits_current consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_events_waits_current_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(false), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_events_waits_history(
    "performance_schema_consumer_events_waits_history",
    "Default startup value for the events_waits_history consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_events_waits_history_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(false), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_events_waits_history_long(
    "performance_schema_consumer_events_waits_history_long",
    "Default startup value for the events_waits_history_long consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_events_waits_history_long_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(false), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_global_instrumentation(
    "performance_schema_consumer_global_instrumentation",
    "Default startup value for the global_instrumentation consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_global_instrumentation_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(true), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_thread_instrumentation(
    "performance_schema_consumer_thread_instrumentation",
    "Default startup value for the thread_instrumentation consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_thread_instrumentation_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(true), PFS_TRAILING_PROPERTIES);

static Sys_var_bool Sys_pfs_consumer_statement_digest(
    "performance_schema_consumer_statements_digest",
    "Default startup value for the statements_digest consumer.",
    READ_ONLY NOT_VISIBLE
        GLOBAL_VAR(pfs_param.m_consumer_statement_digest_enabled),
    CMD_LINE(OPT_ARG), DEFAULT(true), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_events_waits_history_long_size(
    "performance_schema_events_waits_history_long_size",
    "Number of rows in EVENTS_WAITS_HISTORY_LONG."
    " Use 0 to disable, -1 for automated sizing.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_events_waits_history_long_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 1024 * 1024),
    DEFAULT(PFS_AUTOSIZE_VALUE), BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_events_waits_history_size(
    "performance_schema_events_waits_history_size",
    "Number of rows per thread in EVENTS_WAITS_HISTORY."
    " Use 0 to disable, -1 for automated sizing.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_events_waits_history_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 1024), DEFAULT(PFS_AUTOSIZE_VALUE),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_ulong Sys_pfs_max_cond_classes(
    "performance_schema_max_cond_classes",
    "Maximum number of condition instruments.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_cond_class_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 1024), DEFAULT(PFS_MAX_COND_CLASS), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_cond_instances(
    "performance_schema_max_cond_instances",
    "Maximum number of instrumented condition objects."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_cond_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(-1, 1024 * 1024), DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_program_instances(
    "performance_schema_max_program_instances",
    "Maximum number of instrumented programs."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_program_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(-1, 1024 * 1024), DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static constexpr int num_prepared_stmt_limit = 4 * 1024 * 1024;

static Sys_var_long Sys_pfs_max_prepared_stmt_instances(
    "performance_schema_max_prepared_statements_instances",
    "Maximum number of instrumented prepared statements."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_prepared_stmt_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, num_prepared_stmt_limit),
    DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_ulong Sys_pfs_max_file_classes(
    "performance_schema_max_file_classes",
    "Maximum number of file instruments.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_file_class_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 1024), DEFAULT(PFS_MAX_FILE_CLASS), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_ulong Sys_pfs_max_file_handles(
    "performance_schema_max_file_handles",
    "Maximum number of opened instrumented files.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_file_handle_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 1024 * 1024),
    DEFAULT(PFS_MAX_FILE_HANDLE), BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_file_instances(
    "performance_schema_max_file_instances",
    "Maximum number of instrumented files."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_file_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(-1, 1024 * 1024), DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_sockets(
    "performance_schema_max_socket_instances",
    "Maximum number of opened instrumented sockets."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_socket_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(-1, 1024 * 1024), DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_ulong Sys_pfs_max_socket_classes(
    "performance_schema_max_socket_classes",
    "Maximum number of socket instruments.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_socket_class_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 1024), DEFAULT(PFS_MAX_SOCKET_CLASS),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_ulong Sys_pfs_max_mutex_classes(
    "performance_schema_max_mutex_classes",
    "Maximum number of mutex instruments.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_mutex_class_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 1024), DEFAULT(PFS_MAX_MUTEX_CLASS),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_mutex_instances(
    "performance_schema_max_mutex_instances",
    "Maximum number of instrumented MUTEX objects."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_mutex_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(-1, 100 * 1024 * 1024), DEFAULT(PFS_AUTOSCALE_VALUE),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_ulong Sys_pfs_max_rwlock_classes(
    "performance_schema_max_rwlock_classes",
    "Maximum number of rwlock instruments.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_rwlock_class_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 1024), DEFAULT(PFS_MAX_RWLOCK_CLASS),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_rwlock_instances(
    "performance_schema_max_rwlock_instances",
    "Maximum number of instrumented RWLOCK objects."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_rwlock_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(-1, 100 * 1024 * 1024), DEFAULT(PFS_AUTOSCALE_VALUE),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_table_handles(
    "performance_schema_max_table_handles",
    "Maximum number of opened instrumented tables."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_table_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(-1, 1024 * 1024), DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_table_instances(
    "performance_schema_max_table_instances",
    "Maximum number of instrumented tables."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_table_share_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 1024 * 1024),
    DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_table_lock_stat(
    "performance_schema_max_table_lock_stat",
    "Maximum number of lock statistics for instrumented tables."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_table_lock_stat_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 1024 * 1024),
    DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_index_stat(
    "performance_schema_max_index_stat",
    "Maximum number of index statistics for instrumented tables."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_index_stat_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(-1, 1024 * 1024), DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_ulong Sys_pfs_max_thread_classes(
    "performance_schema_max_thread_classes",
    "Maximum number of thread instruments.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_thread_class_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 1024), DEFAULT(PFS_MAX_THREAD_CLASS),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_thread_instances(
    "performance_schema_max_thread_instances",
    "Maximum number of instrumented threads."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_thread_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(-1, 1024 * 1024), DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_setup_actors_size(
    "performance_schema_setup_actors_size",
    "Maximum number of rows in SETUP_ACTORS."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_setup_actor_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 1024 * 1024),
    DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_setup_objects_size(
    "performance_schema_setup_objects_size",
    "Maximum number of rows in SETUP_OBJECTS."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_setup_object_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 1024 * 1024),
    DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_accounts_size(
    "performance_schema_accounts_size",
    "Maximum number of instrumented user@host accounts."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_account_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(-1, 1024 * 1024), DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_hosts_size(
    "performance_schema_hosts_size",
    "Maximum number of instrumented hosts."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_host_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(-1, 1024 * 1024), DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_users_size(
    "performance_schema_users_size",
    "Maximum number of instrumented users."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_user_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(-1, 1024 * 1024), DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_ulong Sys_pfs_max_stage_classes(
    "performance_schema_max_stage_classes",
    "Maximum number of stage instruments.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_stage_class_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 1024), DEFAULT(PFS_MAX_STAGE_CLASS),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_events_stages_history_long_size(
    "performance_schema_events_stages_history_long_size",
    "Number of rows in EVENTS_STAGES_HISTORY_LONG."
    " Use 0 to disable, -1 for automated sizing.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_events_stages_history_long_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 1024 * 1024),
    DEFAULT(PFS_AUTOSIZE_VALUE), BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_events_stages_history_size(
    "performance_schema_events_stages_history_size",
    "Number of rows per thread in EVENTS_STAGES_HISTORY."
    " Use 0 to disable, -1 for automated sizing.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_events_stages_history_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 1024), DEFAULT(PFS_AUTOSIZE_VALUE),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

/**
  Variable performance_schema_max_statement_classes.
  The default number of statement classes is the sum of:
  - COM_END for all regular "statement/com/...",
  - 1 for "statement/com/new_packet", for unknown enum_server_command
  - 1 for "statement/com/Error", for invalid enum_server_command
  - SQLCOM_END for all regular "statement/sql/...",
  - 1 for "statement/sql/error", for invalid enum_sql_command.
  - SP_PSI_STATEMENT_INFO_COUNT for "statement/sp/...".
  - CLONE_PSI_STATEMENT_COUNT for "statement/clone/...".
  - 1 for "statement/rpl/relay_log", for replicated statements.
  - 1 for "statement/scheduler/event", for scheduled events.
*/
static Sys_var_ulong Sys_pfs_max_statement_classes(
    "performance_schema_max_statement_classes",
    "Maximum number of statement instruments.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_statement_class_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 256),
    DEFAULT((ulong)SQLCOM_END + (ulong)COM_END + 5 +
            SP_PSI_STATEMENT_INFO_COUNT + CLONE_PSI_STATEMENT_COUNT),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_events_statements_history_long_size(
    "performance_schema_events_statements_history_long_size",
    "Number of rows in EVENTS_STATEMENTS_HISTORY_LONG."
    " Use 0 to disable, -1 for automated sizing.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_events_statements_history_long_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 1024 * 1024),
    DEFAULT(PFS_AUTOSIZE_VALUE), BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_events_statements_history_size(
    "performance_schema_events_statements_history_size",
    "Number of rows per thread in EVENTS_STATEMENTS_HISTORY."
    " Use 0 to disable, -1 for automated sizing.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_events_statements_history_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 1024), DEFAULT(PFS_AUTOSIZE_VALUE),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_ulong Sys_pfs_statement_stack_size(
    "performance_schema_max_statement_stack",
    "Number of rows per thread in EVENTS_STATEMENTS_CURRENT.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_statement_stack_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(1, 256),
    DEFAULT(PFS_STATEMENTS_STACK_SIZE), BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_ulong Sys_pfs_max_memory_classes(
    "performance_schema_max_memory_classes",
    "Maximum number of memory pool instruments.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_memory_class_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 1024), DEFAULT(PFS_MAX_MEMORY_CLASS),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_digest_size(
    "performance_schema_digests_size",
    "Size of the statement digest."
    " Use 0 to disable, -1 for automated sizing.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_digest_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(-1, 1024 * 1024), DEFAULT(PFS_AUTOSIZE_VALUE), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_events_transactions_history_long_size(
    "performance_schema_events_transactions_history_long_size",
    "Number of rows in EVENTS_TRANSACTIONS_HISTORY_LONG."
    " Use 0 to disable, -1 for automated sizing.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_events_transactions_history_long_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 1024 * 1024),
    DEFAULT(PFS_AUTOSIZE_VALUE), BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_events_transactions_history_size(
    "performance_schema_events_transactions_history_size",
    "Number of rows per thread in EVENTS_TRANSACTIONS_HISTORY."
    " Use 0 to disable, -1 for automated sizing.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_events_transactions_history_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 1024), DEFAULT(PFS_AUTOSIZE_VALUE),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_digest_length(
    "performance_schema_max_digest_length",
    "Maximum length considered for digest text, when stored in "
    "performance_schema tables.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_max_digest_length), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 1024 * 1024), DEFAULT(1024), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_ulong Sys_pfs_max_digest_sample_age(
    "performance_schema_max_digest_sample_age",
    "The time in seconds after which a previous query sample is considered old."
    " When the value is 0, queries are sampled once."
    " When the value is greater than zero, queries are re sampled if the"
    " last sample is more than performance_schema_max_digest_sample_age "
    "seconds old.",
    GLOBAL_VAR(pfs_param.m_max_digest_sample_age), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 1024 * 1024), DEFAULT(60), BLOCK_SIZE(1),
    PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_connect_attrs_size(
    "performance_schema_session_connect_attrs_size",
    "Size of session attribute string buffer per thread."
    " Use 0 to disable, -1 for automated sizing.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_session_connect_attrs_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 1024 * 1024),
    DEFAULT(PFS_AUTOSIZE_VALUE), BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_metadata_locks(
    "performance_schema_max_metadata_locks",
    "Maximum number of metadata locks."
    " Use 0 to disable, -1 for automated scaling.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_metadata_lock_sizing),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(-1, 100 * 1024 * 1024),
    DEFAULT(PFS_AUTOSCALE_VALUE), BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_max_sql_text_length(
    "performance_schema_max_sql_text_length",
    "Maximum length of displayed sql text.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_max_sql_text_length),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 1024 * 1024), DEFAULT(1024),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

static Sys_var_long Sys_pfs_error_size(
    "performance_schema_error_size", "Number of server errors instrumented.",
    READ_ONLY GLOBAL_VAR(pfs_param.m_error_sizing), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 1024 * 1024), DEFAULT(PFS_MAX_GLOBAL_SERVER_ERRORS),
    BLOCK_SIZE(1), PFS_TRAILING_PROPERTIES);

#endif /* WITH_PERFSCHEMA_STORAGE_ENGINE */

static Sys_var_ulong Sys_auto_increment_increment(
    "auto_increment_increment",
    "Auto-increment columns are incremented by this",
    HINT_UPDATEABLE SESSION_VAR(auto_increment_increment), CMD_LINE(OPT_ARG),
    VALID_RANGE(1, 65535), DEFAULT(1), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    IN_BINLOG);

static Sys_var_ulong Sys_auto_increment_offset(
    "auto_increment_offset",
    "Offset added to Auto-increment columns. Used when "
    "auto-increment-increment != 1",
    HINT_UPDATEABLE SESSION_VAR(auto_increment_offset), CMD_LINE(OPT_ARG),
    VALID_RANGE(1, 65535), DEFAULT(1), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    IN_BINLOG);

static Sys_var_bool Sys_windowing_use_high_precision(
    "windowing_use_high_precision",
    "For SQL window functions, determines whether to enable inversion "
    "optimization for moving window frames also for floating values.",
    HINT_UPDATEABLE SESSION_VAR(windowing_use_high_precision),
    CMD_LINE(OPT_ARG), DEFAULT(true));

static Sys_var_uint Sys_cte_max_recursion_depth(
    "cte_max_recursion_depth",
    "Abort a recursive common table expression "
    "if it does more than this number of iterations.",
    HINT_UPDATEABLE SESSION_VAR(cte_max_recursion_depth),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, UINT_MAX32), DEFAULT(1000),
    BLOCK_SIZE(1));

static Sys_var_bool Sys_automatic_sp_privileges(
    "automatic_sp_privileges",
    "Creating and dropping stored procedures alters ACLs",
    GLOBAL_VAR(sp_automatic_privileges), CMD_LINE(OPT_ARG), DEFAULT(true));

static Sys_var_ulong Sys_back_log(
    "back_log",
    "The number of outstanding connection requests "
    "MySQL can have. This comes into play when the main MySQL thread "
    "gets very many connection requests in a very short time",
    READ_ONLY GLOBAL_VAR(back_log), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 65535), DEFAULT(0), BLOCK_SIZE(1));

static Sys_var_charptr Sys_basedir(
    "basedir",
    "Path to installation directory. All paths are "
    "usually resolved relative to this",
    READ_ONLY NON_PERSIST GLOBAL_VAR(mysql_home_ptr),
    CMD_LINE(REQUIRED_ARG, 'b'), IN_FS_CHARSET, DEFAULT(nullptr));

/*
  --authentication_policy will take precedence over this variable
  except in case where plugin name for first factor is not a concrete
  value. Please refer authentication_policy variable.
*/
static Sys_var_charptr Sys_default_authentication_plugin(
    "default_authentication_plugin",
    "The default authentication plugin "
    "used by the server to hash the password.",
    READ_ONLY NON_PERSIST GLOBAL_VAR(default_auth_plugin),
    CMD_LINE(REQUIRED_ARG), IN_FS_CHARSET, DEFAULT("caching_sha2_password"),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr),
    DEPRECATED_VAR("authentication_policy"));

static PolyLock_mutex Plock_default_password_lifetime(
    &LOCK_default_password_lifetime);
static Sys_var_uint Sys_default_password_lifetime(
    "default_password_lifetime",
    "The number of days after which the "
    "password will expire.",
    GLOBAL_VAR(default_password_lifetime), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, UINT_MAX16), DEFAULT(0), BLOCK_SIZE(1),
    &Plock_default_password_lifetime);

static Sys_var_charptr Sys_my_bind_addr(
    "bind_address",
    "IP address(es) to bind to. Syntax: address[,address]...,"
    " where address can be an IPv4 address, IPv6 address,"
    " host name or one of the wildcard values *, ::, 0.0.0.0."
    " In case more than one address is specified in a"
    " comma-separated list, wildcard values are not allowed."
    " Every address can have optional network namespace separated"
    " by the delimiter / from the address value. E.g., the following value"
    " 192.168.1.1/red,172.16.1.1/green,193.168.1.1 specifies three IP"
    " addresses to listen for incoming TCP connections two of that have"
    " to be placed in corresponding namespaces: the address 192.168.1.1"
    " must be placed into the namespace red and the address 172.16.1.1"
    " must be placed into the namespace green. Using of network namespace"
    " requires its support from underlying Operating System. Attempt to specify"
    " a network namespace for a platform that doesn't support it results in"
    " error during socket creation.",
    READ_ONLY NON_PERSIST GLOBAL_VAR(my_bind_addr_str), CMD_LINE(REQUIRED_ARG),
    IN_FS_CHARSET, DEFAULT(MY_BIND_ALL_ADDRESSES));

static Sys_var_charptr Sys_admin_addr(
    "admin_address",
    "IP address to bind to for service connection. Address can be an IPv4"
    " address, IPv6 address, or host name. Wildcard values *, ::, 0.0.0.0"
    " are not allowed. Address value can have following optional network"
    " namespace separated by the delimiter / from the address value."
    " E.g., the following value 192.168.1.1/red specifies IP addresses to"
    " listen for incoming TCP connections that have to be placed into"
    " the namespace 'red'. Using of network namespace requires its support"
    " from underlying Operating System. Attempt to specify a network namespace"
    " for a platform that doesn't support it results in error during socket"
    " creation.",
    READ_ONLY NON_PERSIST GLOBAL_VAR(my_admin_bind_addr_str),
    CMD_LINE(REQUIRED_ARG), IN_FS_CHARSET, DEFAULT(nullptr));

static Sys_var_uint Sys_admin_port(
    "admin_port",
    "Port number to use for service connection,"
    " built-in default (" STRINGIFY_ARG(MYSQL_ADMIN_PORT) ")",
    READ_ONLY NON_PERSIST GLOBAL_VAR(mysqld_admin_port), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 65535), DEFAULT(MYSQL_ADMIN_PORT), BLOCK_SIZE(1));

static Sys_var_bool Sys_use_separate_thread_for_admin(
    "create_admin_listener_thread",
    "Use a dedicated thread for listening incoming connections on admin"
    " interface",
    READ_ONLY NON_PERSIST GLOBAL_VAR(listen_admin_interface_in_separate_thread),
    CMD_LINE(OPT_ARG), DEFAULT(false));

static Sys_var_bool Sys_password_require_current(
    "password_require_current",
    "Current password is needed to be specified in order to change it",
    GLOBAL_VAR(password_require_current), CMD_LINE(OPT_ARG), DEFAULT(false));

/**
  Checks,
  if there exists at least a partial revoke on a database at the time
  of turning OFF the system variable "@@partial_revokes". If it does then
  throw error.
  if there exists at least a DB grant with wildcard entry at the time of
  turning ON the system variable "@@partial_revokes". If it does then
  throw error.

  @retval true failure
  @retval false success

  @param self the system variable to set value for
  @param thd the session context
  @param setv the SET operations metadata
*/
static bool check_partial_revokes(sys_var *self, THD *thd, set_var *setv) {
  if (is_partial_revoke_exists(thd) && setv->save_result.ulonglong_value == 0) {
    my_error(ER_PARTIAL_REVOKES_EXIST, MYF(0), self->name.str);
    return true;
  }
  return false;
}

/**
  Set the updated global variable to the corresponding atomic system variable.
*/
static bool partial_revokes_update(sys_var *, THD *, enum_var_type) {
  set_mysqld_partial_revokes(opt_partial_revokes);
  return false;
}

/**
  We also modify the global variable outside of sys_var structure.
  Protect the global variable updates through this lock.
*/
static PolyLock_mutex Plock_partial_revokes(&LOCK_partial_revokes);

static Sys_var_bool Sys_partial_revokes(
    "partial_revokes",
    "Access of database objects can be restricted, "
    "even if user has global privileges granted.",
    GLOBAL_VAR(opt_partial_revokes), CMD_LINE(OPT_ARG),
    DEFAULT(DEFAULT_PARTIAL_REVOKES), &Plock_partial_revokes, IN_BINLOG,
    ON_CHECK(check_partial_revokes), ON_UPDATE(partial_revokes_update), nullptr,
    sys_var::PARSE_EARLY);

static bool fix_binlog_cache_size(sys_var *, THD *thd, enum_var_type) {
  check_binlog_cache_size(thd);
  return false;
}

static bool fix_binlog_stmt_cache_size(sys_var *, THD *thd, enum_var_type) {
  check_binlog_stmt_cache_size(thd);
  return false;
}

static Sys_var_ulong Sys_binlog_cache_size(
    "binlog_cache_size",
    "The size of the transactional cache for "
    "updates to transactional engines for the binary log. "
    "If you often use transactions containing many statements, "
    "you can increase this to get more performance",
    GLOBAL_VAR(binlog_cache_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(IO_SIZE, ULONG_MAX), DEFAULT(32768), BLOCK_SIZE(IO_SIZE),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_binlog_cache_size));

static Sys_var_ulong Sys_binlog_stmt_cache_size(
    "binlog_stmt_cache_size",
    "The size of the statement cache for "
    "updates to non-transactional engines for the binary log. "
    "If you often use statements updating a great number of rows, "
    "you can increase this to get more performance",
    GLOBAL_VAR(binlog_stmt_cache_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(IO_SIZE, ULONG_MAX), DEFAULT(32768), BLOCK_SIZE(IO_SIZE),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_binlog_stmt_cache_size));

static Sys_var_int32 Sys_binlog_max_flush_queue_time(
    "binlog_max_flush_queue_time",
    "The maximum time that the binary log group commit will keep reading"
    " transactions before it flush the transactions to the binary log (and"
    " optionally sync, depending on the value of sync_binlog).",
    GLOBAL_VAR(opt_binlog_max_flush_queue_time),
    CMD_LINE(REQUIRED_ARG, OPT_BINLOG_MAX_FLUSH_QUEUE_TIME),
    VALID_RANGE(0, 100000), DEFAULT(0), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr), DEPRECATED_VAR(""));

static Sys_var_long Sys_binlog_group_commit_sync_delay(
    "binlog_group_commit_sync_delay",
    "The number of microseconds the server waits for the "
    "binary log group commit sync queue to fill before "
    "continuing. Default: 0. Min: 0. Max: 1000000.",
    GLOBAL_VAR(opt_binlog_group_commit_sync_delay), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 1000000 /* max 1 sec */), DEFAULT(0), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG);

static Sys_var_ulong Sys_binlog_group_commit_sync_no_delay_count(
    "binlog_group_commit_sync_no_delay_count",
    "If there are this many transactions in the commit sync "
    "queue and the server is waiting for more transactions "
    "to be enqueued (as set using --binlog-group-commit-sync-delay), "
    "the commit procedure resumes.",
    GLOBAL_VAR(opt_binlog_group_commit_sync_no_delay_count),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 100000 /* max connections */),
    DEFAULT(0), BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG);

static bool check_outside_trx(sys_var *var, THD *thd, set_var *) {
  if (thd->in_active_multi_stmt_transaction()) {
    my_error(ER_VARIABLE_NOT_SETTABLE_IN_TRANSACTION, MYF(0), var->name.str);
    return true;
  }
  if (!thd->owned_gtid_is_empty()) {
    char buf[Gtid::MAX_TEXT_LENGTH + 1];
    if (thd->owned_gtid.sidno > 0)
      thd->owned_gtid.to_string(thd->owned_sid, buf);
    else
      strcpy(buf, "ANONYMOUS");
    my_error(ER_CANT_SET_VARIABLE_WHEN_OWNING_GTID, MYF(0), var->name.str, buf);
    return true;
  }
  return false;
}

static bool check_session_admin_outside_trx_outside_sf(sys_var *self, THD *thd,
                                                       set_var *var) {
  if (thd->in_sub_stmt) {
    my_error(ER_VARIABLE_NOT_SETTABLE_IN_SF_OR_TRIGGER, MYF(0), self->name.str);
    return true;
  }
  if (check_outside_trx(self, thd, var)) return true;
  if (self->scope() != sys_var::GLOBAL)
    return check_session_admin(self, thd, var);
  return false;
}

static bool check_explicit_defaults_for_timestamp(sys_var *self, THD *thd,
                                                  set_var *var) {
  // Deprecation warning if switching OFF explicit_defaults_for_timestamp
  if (thd->variables.explicit_defaults_for_timestamp) {
    if (!var->save_result.ulonglong_value)
      push_warning_printf(thd, Sql_condition::SL_WARNING,
                          ER_WARN_DEPRECATED_SYNTAX,
                          ER_THD(thd, ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT),
                          self->name.str);
  }
  if (thd->in_sub_stmt) {
    my_error(ER_VARIABLE_NOT_SETTABLE_IN_SF_OR_TRIGGER, MYF(0), self->name.str);
    return true;
  }
  if (thd->in_active_multi_stmt_transaction()) {
    my_error(ER_VARIABLE_NOT_SETTABLE_IN_TRANSACTION, MYF(0), self->name.str);
    return true;
  }
  return false;
}

/**
  Check-function to @@GTID_NEXT system variable.

  @param self   a pointer to the sys_var, i.e. gtid_next
  @param thd    a reference to THD object
  @param var    a pointer to the set_var created by the parser.

  @return @c false if the change is allowed, otherwise @c true.
*/

static bool check_gtid_next(sys_var *self, THD *thd, set_var *var) {
  bool is_prepared_trx =
      thd->get_transaction()->xid_state()->has_state(XID_STATE::XA_PREPARED);

  if (thd->in_sub_stmt) {
    my_error(ER_VARIABLE_NOT_SETTABLE_IN_SF_OR_TRIGGER, MYF(0), self->name.str);
    return true;
  }
  if (!is_prepared_trx && thd->in_active_multi_stmt_transaction()) {
    my_error(ER_VARIABLE_NOT_SETTABLE_IN_TRANSACTION, MYF(0), self->name.str);
    return true;
  }
  return check_session_admin_or_replication_applier(self, thd, var);
}

static bool check_session_admin_outside_trx_outside_sf_outside_sp(
    sys_var *self, THD *thd, set_var *var) {
  if (check_session_admin_outside_trx_outside_sf(self, thd, var)) return true;
  if (thd->lex->sphead) {
    my_error(ER_VARIABLE_NOT_SETTABLE_IN_SP, MYF(0), self->name.str);
    return true;
  }
  return false;
}

static bool binlog_format_check(sys_var *self, THD *thd, set_var *var) {
  if (check_session_admin(self, thd, var)) return true;

  if (var->type == OPT_GLOBAL || var->type == OPT_PERSIST) {
    /*
      SET @@global.binlog_format and SET @@persist.binlog_format must be
      disallowed if any replication channel has open temporary table(s).
      Otherwise DROP TEMPORARY TABLE is written into binary log on slave
      (which disobeys the simple rule: When @@session.binlog_format=
       ROW/MIXED, the server must not write CREATE/DROP TEMPORARY TABLE
      to the binary log) in the following case:
        slave> SET @@global.binlog_format=STATEMENT;
        slave> START SLAVE;
        master> CREATE TEMPORARY TABLE t1(a INT);
        slave> [wait for t1 to replicate]
        slave> STOP SLAVE;
        slave> SET @@global.binlog_format=ROW / SET @@persist.binlog_format=ROW
        master> DROP TEMPORARY TABLE t1;
        slave> START SLAVE;
      Note: SET @@persist_only.binlog_format is not disallowed if any
      replication channel has temporary table(s), since unlike PERSIST,
      PERSIST_ONLY does not modify the runtime global system variable value.

      SET @@global.binlog_format and SET @@persist.binlog_format must be
      disallowed if any replication channel applier is running, because
      SET @@global.binlog_format does not take effect when any replication
      channel applier is running. SET @@global.binlog_format takes effect
      on the channel until its applier is (re)starting.
      Note: SET @@persist_only.binlog_format is not disallowed if any
      replication channel applier is running, since unlike PERSIST,
      PERSIST_ONLY does not modify the runtime global system variable value.
    */
    enum_slave_channel_status slave_channel_status =
        has_any_slave_channel_open_temp_table_or_is_its_applier_running();
    if (slave_channel_status == SLAVE_CHANNEL_APPLIER_IS_RUNNING) {
      my_error(ER_RUNNING_APPLIER_PREVENTS_SWITCH_GLOBAL_BINLOG_FORMAT, MYF(0));
      return true;
    } else if (slave_channel_status == SLAVE_CHANNEL_HAS_OPEN_TEMPORARY_TABLE) {
      my_error(ER_TEMP_TABLE_PREVENTS_SWITCH_GLOBAL_BINLOG_FORMAT, MYF(0));
      return true;
    }
  }

  if (!var->is_global_persist()) {
    /*
      SET @@session.binlog_format must be disallowed if the session has open
      temporary table(s). Otherwise DROP TEMPORARY TABLE is written into
      binary log (which disobeys the simple rule: When
      @@session.binlog_format=ROW/MIXED, the server must not write
      CREATE/DROP TEMPORARY TABLE to the binary log) in the following case:
        SET @@session.binlog_format=STATEMENT;
        CREATE TEMPORARY TABLE t1 (a INT);
        SET @@session.binlog_format=ROW;
        DROP TEMPORARY TABLE t1;
      And more, if binlog_format=ROW/MIXED and the session has open temporary
      table(s), these CREATE TEMPORARY TABLE are not written into the binlog,
      so we can not switch to STATEMENT.
    */
    if (thd->temporary_tables) {
      my_error(ER_TEMP_TABLE_PREVENTS_SWITCH_SESSION_BINLOG_FORMAT, MYF(0));
      return true;
    }

    /*
      if in a stored function/trigger, it's too late to change mode
    */
    if (thd->in_sub_stmt) {
      my_error(ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_FORMAT, MYF(0));
      return true;
    }
    /*
      Make the session variable 'binlog_format' read-only inside a transaction.
    */
    if (thd->in_active_multi_stmt_transaction()) {
      my_error(ER_INSIDE_TRANSACTION_PREVENTS_SWITCH_BINLOG_FORMAT, MYF(0));
      return true;
    }
  }

  /*
    If moving to statement format, and binlog_row_value_options is set,
    generate a warning.
  */
  if (var->save_result.ulonglong_value == BINLOG_FORMAT_STMT) {
    if ((var->is_global_persist() &&
         global_system_variables.binlog_row_value_options != 0) ||
        (!var->is_global_persist() &&
         thd->variables.binlog_row_value_options != 0)) {
      push_warning_printf(thd, Sql_condition::SL_WARNING,
                          ER_WARN_BINLOG_PARTIAL_UPDATES_DISABLED,
                          ER_THD(thd, ER_WARN_BINLOG_PARTIAL_UPDATES_DISABLED),
                          "binlog_format=STATEMENT", "PARTIAL_JSON");
    }
  }

  return false;
}

static bool fix_binlog_format_after_update(sys_var *, THD *thd,
                                           enum_var_type type) {
  if (type == OPT_SESSION) thd->reset_current_stmt_binlog_format_row();
  return false;
}

static bool prevent_global_rbr_exec_mode_idempotent(sys_var *self, THD *,
                                                    set_var *var) {
  if (var->is_global_persist()) {
    my_error(ER_LOCAL_VARIABLE, MYF(0), self->name.str);
    return true;
  }
  return false;
}

static Sys_var_test_flag Sys_core_file("core_file",
                                       "write a core-file on crashes",
                                       TEST_CORE_ON_SIGNAL);

static Sys_var_enum Sys_binlog_format(
    "binlog_format",
    "The format used when writing the binary log. ROW writes each changed "
    "row in a binary format. STATEMENT writes SQL statements. MIXED writes "
    "SQL statements for most statements, and row format for statements that "
    "cannot be replayed in a deterministic manner using SQL. If NDBCLUSTER "
    "is enabled and binlog-format is MIXED, the format switches to row-based "
    "and back implicitly for each query accessing an NDBCLUSTER table."
    " This option is deprecated and will be removed in a future version.",
    SESSION_VAR(binlog_format), CMD_LINE(REQUIRED_ARG, OPT_BINLOG_FORMAT),
    binlog_format_names, DEFAULT(BINLOG_FORMAT_ROW), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(binlog_format_check),
    ON_UPDATE(fix_binlog_format_after_update), DEPRECATED_VAR(""));

static const char *rbr_exec_mode_names[] = {"STRICT", "IDEMPOTENT", nullptr};
static Sys_var_enum rbr_exec_mode(
    "rbr_exec_mode",
    "Modes for how row events should be executed. Legal values "
    "are STRICT (default) and IDEMPOTENT. In IDEMPOTENT mode, "
    "the server will not throw errors for operations that are idempotent. "
    "In STRICT mode, server will throw errors for the operations that "
    "cause a conflict.",
    SESSION_VAR(rbr_exec_mode_options), NO_CMD_LINE, rbr_exec_mode_names,
    DEFAULT(RBR_EXEC_MODE_STRICT), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(prevent_global_rbr_exec_mode_idempotent), ON_UPDATE(nullptr));

static bool check_binlog_row_image(sys_var *self [[maybe_unused]], THD *thd,
                                   set_var *var) {
  DBUG_TRACE;
  if (check_session_admin(self, thd, var)) return true;
  if (var->save_result.ulonglong_value == BINLOG_ROW_IMAGE_FULL) {
    if ((var->is_global_persist() &&
         global_system_variables.binlog_row_value_options != 0) ||
        (!var->is_global_persist() &&
         thd->variables.binlog_row_value_options != 0)) {
      push_warning_printf(
          thd, Sql_condition::SL_WARNING,
          ER_WARN_BINLOG_PARTIAL_UPDATES_SUGGESTS_PARTIAL_IMAGES,
          ER_THD(thd, ER_WARN_BINLOG_PARTIAL_UPDATES_SUGGESTS_PARTIAL_IMAGES),
          "binlog_row_image=FULL", "PARTIAL_JSON");
    }
  }
  return false;
}

static const char *binlog_row_image_names[] = {"MINIMAL", "NOBLOB", "FULL",
                                               NullS};
static Sys_var_enum Sys_binlog_row_image(
    "binlog_row_image",
    "Controls whether rows should be logged in 'FULL', 'NOBLOB' or "
    "'MINIMAL' formats. 'FULL', means that all columns in the before "
    "and after image are logged. 'NOBLOB', means that mysqld avoids logging "
    "blob columns whenever possible (e.g. blob column was not changed or "
    "is not part of primary key). 'MINIMAL', means that a PK equivalent (PK "
    "columns or full row if there is no PK in the table) is logged in the "
    "before image, and only changed columns are logged in the after image. "
    "(Default: FULL).",
    SESSION_VAR(binlog_row_image), CMD_LINE(REQUIRED_ARG),
    binlog_row_image_names, DEFAULT(BINLOG_ROW_IMAGE_FULL), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_binlog_row_image), ON_UPDATE(nullptr));

static const char *binlog_row_metadata_names[] = {"MINIMAL", "FULL", NullS};
static Sys_var_enum Sys_binlog_row_metadata(
    "binlog_row_metadata",
    "Controls how much type information is written to the binary log when "
    "using ROW format. FULL causes all metadata to be logged. MINIMAL means "
    "that only metadata actually needed by replicas is logged.",
    GLOBAL_VAR(binlog_row_metadata), CMD_LINE(REQUIRED_ARG),
    binlog_row_metadata_names, DEFAULT(BINLOG_ROW_METADATA_MINIMAL),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr));

static bool check_binlog_trx_compression(sys_var *self [[maybe_unused]],
                                         THD *thd, set_var *var) {
  DBUG_TRACE;
  if (check_session_admin(self, thd, var)) return true;

  if (!var->is_global_persist() && thd->in_active_multi_stmt_transaction()) {
    my_error(ER_VARIABLE_NOT_SETTABLE_IN_TRANSACTION, MYF(0), self->name.str);
    return true;
  }
  return false;
}

static Sys_var_bool Sys_binlog_trx_compression(
    "binlog_transaction_compression",
    "Whether to compress transactions or not. Transactions are compressed "
    "using the ZSTD compression algorythm.",
    SESSION_VAR(binlog_trx_compression), CMD_LINE(OPT_ARG), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_binlog_trx_compression));

static Sys_var_uint Sys_binlog_transaction_compression_level_zstd(
    "binlog_transaction_compression_level_zstd",
    "Specifies the transaction compression level for ZSTD "
    "transaction compression in the binary log.",
    SESSION_VAR(binlog_trx_compression_level_zstd), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, 22),
    DEFAULT(binary_log::transaction::compression::Zstd_comp::
                default_compression_level),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_binlog_trx_compression), ON_UPDATE(nullptr));

static bool on_session_track_gtids_update(sys_var *, THD *thd, enum_var_type) {
  thd->session_tracker.get_tracker(SESSION_GTIDS_TRACKER)->update(thd);
  return false;
}

static const char *session_track_gtids_names[] = {"OFF", "OWN_GTID",
                                                  "ALL_GTIDS", NullS};
static Sys_var_enum Sys_session_track_gtids(
    "session_track_gtids",
    "Controls the amount of global transaction ids to be "
    "included in the response packet sent by the server."
    "(Default: OFF).",
    SESSION_VAR(session_track_gtids), CMD_LINE(REQUIRED_ARG),
    session_track_gtids_names, DEFAULT(SESSION_TRACK_GTIDS_OFF), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_outside_trx),
    ON_UPDATE(on_session_track_gtids_update));

static bool binlog_direct_check(sys_var *self, THD *thd, set_var *var) {
  if (check_session_admin(self, thd, var)) return true;

  if (var->is_global_persist()) return false;

  /*
    Makes the session variable 'binlog_direct_non_transactional_updates'
    read-only if within a procedure, trigger or function.
  */
  if (thd->in_sub_stmt) {
    my_error(ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_DIRECT, MYF(0));
    return true;
  }
  /*
    Makes the session variable 'binlog_direct_non_transactional_updates'
    read-only inside a transaction.
  */
  if (thd->in_active_multi_stmt_transaction()) {
    my_error(ER_INSIDE_TRANSACTION_PREVENTS_SWITCH_BINLOG_DIRECT, MYF(0));
    return true;
  }

  return false;
}

static Sys_var_bool Sys_binlog_direct(
    "binlog_direct_non_transactional_updates",
    "Causes updates to non-transactional engines using statement format to "
    "be written directly to binary log, after executing them and before "
    "committing the transaction. Before using this option make sure "
    "that there are no dependencies between transactional and "
    "non-transactional tables such as in the statement INSERT INTO t_myisam "
    "SELECT * FROM t_innodb; otherwise, replicas may diverge.",
    SESSION_VAR(binlog_direct_non_trans_update), CMD_LINE(OPT_ARG),
    DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(binlog_direct_check));

/**
  This variable is read only to users. It can be enabled or disabled
  only at mysqld startup. This variable is used by User thread and
  as well as by replication slave applier thread to apply relay_log.
  Slave applier thread enables/disables this option based on
  relay_log's from replication master versions. There is possibility of
  slave applier thread and User thread to have different setting for
  explicit_defaults_for_timestamp, hence this options is defined as
  SESSION_VAR rather than GLOBAL_VAR.
*/
static Sys_var_bool Sys_explicit_defaults_for_timestamp(
    "explicit_defaults_for_timestamp",
    "This option causes CREATE TABLE to create all TIMESTAMP columns "
    "as NULL with DEFAULT NULL attribute, Without this option, "
    "TIMESTAMP columns are NOT NULL and have implicit DEFAULT clauses. "
    "The old behavior is deprecated. "
    "The variable can only be set by users having the SUPER privilege.",
    SESSION_VAR(explicit_defaults_for_timestamp), CMD_LINE(OPT_ARG),
    DEFAULT(true), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_explicit_defaults_for_timestamp));

static bool repository_check(sys_var *self, THD *thd, set_var *var,
                             SLAVE_THD_TYPE thread_mask) {
  bool ret = false;
  if (check_session_admin_outside_trx_outside_sf(self, thd, var)) return true;
  Master_info *mi;
  int running = 0;
  const char *msg = nullptr;
  bool rpl_info_option = static_cast<uint>(var->save_result.ulonglong_value);

  /* don't convert if the repositories are same */
  if (rpl_info_option ==
      (0 != (thread_mask == SLAVE_THD_IO ? opt_mi_repository_id
                                         : opt_rli_repository_id)))
    return false;

  channel_map.wrlock();

  /* Repository conversion not possible, when multiple channels exist */
  if (channel_map.get_num_instances(true) > 1) {
    msg = "Repository conversion is possible when only default channel exists";
    my_error(ER_CHANGE_RPL_INFO_REPOSITORY_FAILURE, MYF(0), msg);
    channel_map.unlock();
    return true;
  }

  mi = channel_map.get_default_channel_mi();

  if (mi != nullptr) {
    mi->channel_wrlock();
    lock_slave_threads(mi);
    init_thread_mask(&running, mi, false);
    if (!running) {
      bool is_pos_info_invalid{false};
      switch (thread_mask) {
        case SLAVE_THD_IO:
          is_pos_info_invalid = mi->is_receiver_position_info_invalid();
          mysql_mutex_lock(&mi->data_lock);
          mi->flush_info(true);
          mysql_mutex_unlock(&mi->data_lock);
          if (Rpl_info_factory::change_mi_repository(
                  mi, static_cast<uint>(var->save_result.ulonglong_value),
                  &msg)) {
            ret = true;
            my_error(ER_CHANGE_RPL_INFO_REPOSITORY_FAILURE, MYF(0), msg);
          }
          mi->set_receiver_position_info_invalid(is_pos_info_invalid);
          break;
        case SLAVE_THD_SQL:
          mts_recovery_groups(mi->rli);
          if (!mi->rli->is_mts_recovery()) {
            is_pos_info_invalid =
                mi->rli->is_applier_source_position_info_invalid();
            if (Rpl_info_factory::reset_workers(mi->rli) ||
                mi->rli->flush_info(
                    Relay_log_info::RLI_FLUSH_IGNORE_SYNC_OPT |
                    Relay_log_info::RLI_FLUSH_IGNORE_GTID_ONLY) ||
                Rpl_info_factory::change_rli_repository(
                    mi->rli,
                    static_cast<uint>(var->save_result.ulonglong_value),
                    &msg)) {
              ret = true;
              my_error(ER_CHANGE_RPL_INFO_REPOSITORY_FAILURE, MYF(0), msg);
            }
            mi->rli->set_applier_source_position_info_invalid(
                is_pos_info_invalid);
          } else
            LogErr(WARNING_LEVEL, ER_RPL_REPO_HAS_GAPS);
          break;
        default:
          assert(0);
          break;
      }
    } else {
      ret = true;
      my_error(ER_REPLICA_CHANNEL_MUST_STOP, MYF(0), mi->get_channel());
    }
    unlock_slave_threads(mi);
    mi->channel_unlock();
  }
  channel_map.unlock();
  return ret;
}

static bool relay_log_info_repository_check(sys_var *self, THD *thd,
                                            set_var *var) {
  return repository_check(self, thd, var, SLAVE_THD_SQL);
}

static bool master_info_repository_check(sys_var *self, THD *thd,
                                         set_var *var) {
  return repository_check(self, thd, var, SLAVE_THD_IO);
}

static bool replica_parallel_workers_update(sys_var *, THD *thd,
                                            enum_var_type) {
  if (opt_mts_replica_parallel_workers == 0) {
    push_warning_printf(thd, Sql_condition::SL_WARNING,
                        ER_WARN_DEPRECATED_SYNTAX,
                        ER_THD(thd, ER_WARN_DEPRECATED_SYNTAX), "0", "1");
  }
  return false;
}

static const char *repository_names[] = {"FILE", "TABLE",
#ifndef NDEBUG
                                         "DUMMY",
#endif
                                         nullptr};

ulong opt_mi_repository_id = INFO_REPOSITORY_TABLE;
static Sys_var_enum Sys_mi_repository(
    "master_info_repository",
    "The repository format for the replication connection configuration.",
    GLOBAL_VAR(opt_mi_repository_id),
    CMD_LINE(REQUIRED_ARG, OPT_MASTER_INFO_REPOSITORY), repository_names,
    DEFAULT(INFO_REPOSITORY_TABLE), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(master_info_repository_check), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

ulong opt_rli_repository_id = INFO_REPOSITORY_TABLE;
static Sys_var_enum Sys_rli_repository(
    "relay_log_info_repository",
    "Defines the type of the repository for the relay log information "
    "and associated workers.",
    GLOBAL_VAR(opt_rli_repository_id),
    CMD_LINE(REQUIRED_ARG, OPT_RELAY_LOG_INFO_REPOSITORY), repository_names,
    DEFAULT(INFO_REPOSITORY_TABLE), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(relay_log_info_repository_check), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static Sys_var_bool Sys_binlog_rows_query(
    "binlog_rows_query_log_events",
    "Allow writing of Rows_query_log events into binary log.",
    SESSION_VAR(binlog_rows_query_log_events), CMD_LINE(OPT_ARG),
    DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_session_admin));

static Sys_var_bool Sys_binlog_order_commits(
    "binlog_order_commits",
    "Issue internal commit calls in the same order as transactions are"
    " written to the binary log. Default is to order commits.",
    GLOBAL_VAR(opt_binlog_order_commits), CMD_LINE(OPT_ARG), DEFAULT(true));

static Sys_var_ulong Sys_bulk_insert_buff_size(
    "bulk_insert_buffer_size",
    "Size of tree cache used in bulk "
    "insert optimisation. Note that this is a limit per thread!",
    HINT_UPDATEABLE SESSION_VAR(bulk_insert_buff_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, ULONG_MAX), DEFAULT(8192 * 1024), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_session_admin));

static Sys_var_charptr Sys_character_sets_dir(
    "character_sets_dir", "Directory where character sets are",
    READ_ONLY NON_PERSIST GLOBAL_VAR(charsets_dir), CMD_LINE(REQUIRED_ARG),
    IN_FS_CHARSET, DEFAULT(nullptr));

static Sys_var_ulong Sys_select_into_buffer_size(
    "select_into_buffer_size", "Buffer size for SELECT INTO OUTFILE/DUMPFILE.",
    HINT_UPDATEABLE SESSION_VAR(select_into_buffer_size), CMD_LINE(OPT_ARG),
    VALID_RANGE(IO_SIZE * 2, INT_MAX32), DEFAULT(128 * 1024),
    BLOCK_SIZE(IO_SIZE), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_session_admin_no_super));

static Sys_var_bool Sys_select_into_disk_sync(
    "select_into_disk_sync",
    "Synchronize flushed buffer with disk for SELECT INTO OUTFILE/DUMPFILE.",
    HINT_UPDATEABLE SESSION_VAR(select_into_disk_sync), CMD_LINE(OPT_ARG),
    DEFAULT(false));

static Sys_var_uint Sys_select_into_disk_sync_delay(
    "select_into_disk_sync_delay",
    "The delay in milliseconds after each buffer sync "
    "for SELECT INTO OUTFILE/DUMPFILE. Requires select_into_sync_disk = ON.",
    HINT_UPDATEABLE SESSION_VAR(select_into_disk_sync_delay), CMD_LINE(OPT_ARG),
    VALID_RANGE(0, LONG_TIMEOUT), DEFAULT(0), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_session_admin_no_super));

static bool check_not_null(sys_var *, THD *, set_var *var) {
  return var->value && var->value->is_null();
}

/**
  Check storage engine is not empty and log warning.

  Checks if default_storage_engine or default_tmp_storage_engine is set
  empty and return true. This method also logs warning if the
  storage engine set is a disabled storage engine specified in
  disabled_storage_engines.

  @param self    pointer to system variable object.
  @param thd     Connection handle.
  @param var     pointer to set variable object.

  @return  true if the set variable is empty.
           false if the set variable is not empty.
*/
static bool check_storage_engine(sys_var *self, THD *thd, set_var *var) {
  if (check_not_null(self, thd, var)) return true;

  if (!opt_initialize && !opt_noacl) {
    char buff[STRING_BUFFER_USUAL_SIZE];
    String str(buff, sizeof(buff), system_charset_info), *res;
    LEX_CSTRING se_name;

    if (var->value) {
      res = var->value->val_str(&str);
      lex_cstring_set(&se_name, res->ptr());
    } else {
      // Use the default value defined by sys_var.
      lex_cstring_set(
          &se_name,
          pointer_cast<const char *>(
              down_cast<Sys_var_plugin *>(self)->global_value_ptr(thd, {})));
    }

    plugin_ref plugin;
    if ((plugin = ha_resolve_by_name(nullptr, &se_name, false))) {
      handlerton *hton = plugin_data<handlerton *>(plugin);
      if (ha_is_storage_engine_disabled(hton))
        LogErr(WARNING_LEVEL, ER_DISABLED_STORAGE_ENGINE_AS_DEFAULT,
               self->name.str, se_name.str);
      plugin_unlock(nullptr, plugin);
    }
  }
  return false;
}

static bool check_charset(sys_var *, THD *thd, set_var *var) {
  if (!var->value) return false;

  char buff[STRING_BUFFER_USUAL_SIZE];
  if (var->value->result_type() == STRING_RESULT) {
    String str(buff, sizeof(buff), system_charset_info), *res;
    if (!(res = var->value->val_str(&str)))
      var->save_result.ptr = nullptr;
    else {
      ErrConvString err(res); /* Get utf8 '\0' terminated string */
      if (!(var->save_result.ptr =
                get_charset_by_csname(err.ptr(), MY_CS_PRIMARY, MYF(0))) &&
          !(var->save_result.ptr = get_old_charset_by_name(err.ptr()))) {
        my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), err.ptr());
        return true;
      }
      warn_on_deprecated_charset(
          thd, static_cast<const CHARSET_INFO *>(var->save_result.ptr),
          err.ptr());
    }
  } else  // INT_RESULT
  {
    int csno = (int)var->value->val_int();
    if (!(var->save_result.ptr = get_charset(csno, MYF(0)))) {
      my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), llstr(csno, buff));
      return true;
    }
    warn_on_deprecated_charset(
        thd, static_cast<const CHARSET_INFO *>(var->save_result.ptr),
        static_cast<const CHARSET_INFO *>(var->save_result.ptr)->m_coll_name);
  }
  return false;
}
static bool check_charset_not_null(sys_var *self, THD *thd, set_var *var) {
  return check_charset(self, thd, var) || check_not_null(self, thd, var);
}

namespace {
struct Get_name {
  explicit Get_name(const CHARSET_INFO *ci) : m_ci(ci) {}
  const uchar *get_name() const {
    return pointer_cast<const uchar *>(m_ci->m_coll_name);
  }
  const CHARSET_INFO *m_ci;
};

struct Get_csname {
  explicit Get_csname(const CHARSET_INFO *ci) : m_ci(ci) {}
  const uchar *get_name() const {
    return pointer_cast<const uchar *>(m_ci->csname);
  }
  const CHARSET_INFO *m_ci;
};

}  // namespace

static CHARSET_INFO *charset_system_default = &my_charset_utf8mb3_general_ci;

static Sys_var_struct<CHARSET_INFO, Get_csname> Sys_character_set_system(
    "character_set_system",
    "The character set used by the server "
    "for storing identifiers",
    READ_ONLY NON_PERSIST GLOBAL_VAR(system_charset_info), NO_CMD_LINE,
    DEFAULT(&charset_system_default));

static Sys_var_struct<CHARSET_INFO, Get_csname> Sys_character_set_server(
    "character_set_server", "The default character set",
    PERSIST_AS_READONLY SESSION_VAR(collation_server), NO_CMD_LINE,
    DEFAULT(&default_charset_info), NO_MUTEX_GUARD, IN_BINLOG,
    ON_CHECK(check_charset_not_null));

static bool check_charset_db(sys_var *self, THD *thd, set_var *var) {
  if (check_session_admin(self, thd, var)) return true;
  if (check_charset_not_null(self, thd, var)) return true;
  if (!var->value)  // = DEFAULT
    var->save_result.ptr = thd->db_charset;
  return false;
}

static bool update_deprecated_with_removal_message(sys_var *self, THD *thd,
                                                   enum_var_type) {
  push_warning_printf(thd, Sql_condition::SL_WARNING, ER_WARN_DEPRECATED_SYNTAX,
                      ER_THD(thd, ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT),
                      self->name.str);
  return false;
}
static bool update_deprecated(sys_var *self, THD *thd, enum_var_type) {
  push_warning_printf(
      thd, Sql_condition::SL_WARNING, ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT,
      ER_THD(thd, ER_WARN_DEPRECATED_SYSVAR_UPDATE), self->name.str);
  return false;
}
static Sys_var_struct<CHARSET_INFO, Get_csname> Sys_character_set_database(
    "character_set_database", " The character set used by the default database",
    SESSION_VAR(collation_database), NO_CMD_LINE,
    DEFAULT(&default_charset_info), NO_MUTEX_GUARD, IN_BINLOG,
    ON_CHECK(check_charset_db), ON_UPDATE(update_deprecated));

static bool check_cs_client(sys_var *self, THD *thd, set_var *var) {
  if (check_charset_not_null(self, thd, var)) return true;

  // We don't currently support any variable-width character set with a minimum
  // length greater than 1. If we ever do, we have to revisit
  // is_supported_parser_charset(). See Item_func_statement_digest::val_str()
  // and Item_func_statement_digest_text::val_str().
  return (static_cast<const CHARSET_INFO *>(var->save_result.ptr))->mbminlen >
         1;
}
static bool fix_thd_charset(sys_var *, THD *thd, enum_var_type type) {
  if (type == OPT_SESSION) thd->update_charset();
  return false;
}
static Sys_var_struct<CHARSET_INFO, Get_csname> Sys_character_set_client(
    "character_set_client",
    "The character set for statements "
    "that arrive from the client",
    SESSION_VAR(character_set_client), NO_CMD_LINE,
    DEFAULT(&default_charset_info), NO_MUTEX_GUARD, IN_BINLOG,
    ON_CHECK(check_cs_client), ON_UPDATE(fix_thd_charset));

static Sys_var_struct<CHARSET_INFO, Get_csname> Sys_character_set_connection(
    "character_set_connection",
    "The character set used for "
    "literals that do not have a character set introducer and for "
    "number-to-string conversion",
    SESSION_VAR(collation_connection), NO_CMD_LINE,
    DEFAULT(&default_charset_info), NO_MUTEX_GUARD, IN_BINLOG,
    ON_CHECK(check_charset_not_null), ON_UPDATE(fix_thd_charset));

static Sys_var_struct<CHARSET_INFO, Get_csname> Sys_character_set_results(
    "character_set_results",
    "The character set used for returning "
    "query results to the client",
    SESSION_VAR(character_set_results), NO_CMD_LINE,
    DEFAULT(&default_charset_info), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_charset));

static bool check_cs_filesystem(sys_var *self, THD *thd, set_var *var) {
  if (check_session_admin(self, thd, var)) return true;
  if (check_charset_not_null(self, thd, var)) return true;

  return false;
}

static Sys_var_struct<CHARSET_INFO, Get_csname> Sys_character_set_filesystem(
    "character_set_filesystem", "The filesystem character set",
    SESSION_VAR(character_set_filesystem), NO_CMD_LINE,
    DEFAULT(&character_set_filesystem), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_cs_filesystem), ON_UPDATE(fix_thd_charset));

static const char *completion_type_names[] = {"NO_CHAIN", "CHAIN", "RELEASE",
                                              nullptr};
static Sys_var_enum Sys_completion_type(
    "completion_type",
    "The transaction completion type, one of "
    "NO_CHAIN, CHAIN, RELEASE",
    SESSION_VAR(completion_type), CMD_LINE(REQUIRED_ARG), completion_type_names,
    DEFAULT(0));

static bool check_collation_not_null(sys_var *self, THD *thd, set_var *var) {
  if (!var->value) return false;

  char buff[STRING_BUFFER_USUAL_SIZE];
  if (var->value->result_type() == STRING_RESULT) {
    String str(buff, sizeof(buff), system_charset_info), *res;
    if (!(res = var->value->val_str(&str)))
      var->save_result.ptr = nullptr;
    else {
      ErrConvString err(res); /* Get utf8 '\0'-terminated string */
      if (!(var->save_result.ptr = get_charset_by_name(err.ptr(), MYF(0)))) {
        my_error(ER_UNKNOWN_COLLATION, MYF(0), err.ptr());
        return true;
      }
    }
  } else  // INT_RESULT
  {
    int csno = (int)var->value->val_int();
    if (!(var->save_result.ptr = get_charset(csno, MYF(0)))) {
      my_error(ER_UNKNOWN_COLLATION, MYF(0), llstr(csno, buff));
      return true;
    }
  }
  if (var->save_result.ptr) {
    warn_on_deprecated_collation(
        thd, static_cast<const CHARSET_INFO *>(var->save_result.ptr));
  }

  return check_not_null(self, thd, var);
}
static Sys_var_struct<CHARSET_INFO, Get_name> Sys_collation_connection(
    "collation_connection",
    "The collation of the connection "
    "character set",
    SESSION_VAR(collation_connection), NO_CMD_LINE,
    DEFAULT(&default_charset_info), NO_MUTEX_GUARD, IN_BINLOG,
    ON_CHECK(check_collation_not_null), ON_UPDATE(fix_thd_charset));

static bool check_collation_db(sys_var *self, THD *thd, set_var *var) {
  if (check_collation_not_null(self, thd, var)) return true;
  if (!var->value)  // = DEFAULT
    var->save_result.ptr = thd->db_charset;
  return false;
}
static Sys_var_struct<CHARSET_INFO, Get_name> Sys_collation_database(
    "collation_database",
    "The collation of the database "
    "character set",
    SESSION_VAR(collation_database), NO_CMD_LINE,
    DEFAULT(&default_charset_info), NO_MUTEX_GUARD, IN_BINLOG,
    ON_CHECK(check_collation_db), ON_UPDATE(update_deprecated));

static Sys_var_struct<CHARSET_INFO, Get_name> Sys_collation_server(
    "collation_server", "The server default collation",
    SESSION_VAR(collation_server), NO_CMD_LINE, DEFAULT(&default_charset_info),
    NO_MUTEX_GUARD, IN_BINLOG, ON_CHECK(check_collation_not_null));

static const char *concurrent_insert_names[] = {"NEVER", "AUTO", "ALWAYS",
                                                nullptr};
static Sys_var_enum Sys_concurrent_insert(
    "concurrent_insert",
    "Use concurrent insert with MyISAM. Possible "
    "values are NEVER, AUTO, ALWAYS",
    GLOBAL_VAR(myisam_concurrent_insert), CMD_LINE(OPT_ARG),
    concurrent_insert_names, DEFAULT(1));

static Sys_var_ulong Sys_connect_timeout(
    "connect_timeout",
    "The number of seconds the mysqld server is waiting for a connect "
    "packet before responding with 'Bad handshake'",
    GLOBAL_VAR(connect_timeout), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(2, LONG_TIMEOUT), DEFAULT(CONNECT_TIMEOUT), BLOCK_SIZE(1));

static Sys_var_ulong Sys_information_schema_stats_expiry(
    "information_schema_stats_expiry",
    "The number of seconds after which mysqld server will fetch "
    "data from storage engine and replace the data in cache.",
    SESSION_VAR(information_schema_stats_expiry), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, LONG_TIMEOUT), DEFAULT(24 * 60 * 60), BLOCK_SIZE(1));

static Sys_var_charptr Sys_datadir(
    "datadir", "Path to the database root directory",
    READ_ONLY NON_PERSIST GLOBAL_VAR(mysql_real_data_home_ptr),
    CMD_LINE(REQUIRED_ARG, 'h'), IN_FS_CHARSET, DEFAULT(mysql_real_data_home));

#ifndef NDEBUG
static Sys_var_dbug Sys_dbug("debug", "Debug log", sys_var::SESSION,
                             CMD_LINE(OPT_ARG, '#'), DEFAULT(""),
                             NO_MUTEX_GUARD, NOT_IN_BINLOG,
                             ON_CHECK(check_session_admin));
#endif

/**
  @todo
    When updating myisam_delay_key_write, we should do a 'flush tables'
    of all MyISAM tables to ensure that they are reopen with the
    new attribute.
*/
export bool fix_delay_key_write(sys_var *, THD *, enum_var_type) {
  switch (delay_key_write_options) {
    case DELAY_KEY_WRITE_NONE:
      myisam_delay_key_write = false;
      break;
    case DELAY_KEY_WRITE_ON:
      myisam_delay_key_write = true;
      break;
    case DELAY_KEY_WRITE_ALL:
      myisam_delay_key_write = true;
      ha_open_options |= HA_OPEN_DELAY_KEY_WRITE;
      break;
  }
  return false;
}
static const char *delay_key_write_names[] = {"OFF", "ON", "ALL", NullS};
static Sys_var_enum Sys_delay_key_write(
    "delay_key_write", "Type of DELAY_KEY_WRITE",
    GLOBAL_VAR(delay_key_write_options), CMD_LINE(OPT_ARG),
    delay_key_write_names, DEFAULT(DELAY_KEY_WRITE_ON), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(fix_delay_key_write));

static Sys_var_ulong Sys_delayed_insert_limit(
    "delayed_insert_limit",
    "After inserting delayed_insert_limit rows, the INSERT DELAYED "
    "handler will check if there are any SELECT statements pending. "
    "If so, it allows these to execute before continuing. "
    "This variable is deprecated along with INSERT DELAYED.",
    GLOBAL_VAR(delayed_insert_limit), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, ULONG_MAX), DEFAULT(DELAYED_LIMIT), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static Sys_var_ulong Sys_delayed_insert_timeout(
    "delayed_insert_timeout",
    "How long a INSERT DELAYED thread should wait for INSERT statements "
    "before terminating. "
    "This variable is deprecated along with INSERT DELAYED.",
    GLOBAL_VAR(delayed_insert_timeout), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, LONG_TIMEOUT), DEFAULT(DELAYED_WAIT_TIMEOUT), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static Sys_var_ulong Sys_delayed_queue_size(
    "delayed_queue_size",
    "What size queue (in rows) should be allocated for handling INSERT "
    "DELAYED. If the queue becomes full, any client that does INSERT "
    "DELAYED will wait until there is room in the queue again. "
    "This variable is deprecated along with INSERT DELAYED.",
    GLOBAL_VAR(delayed_queue_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, ULONG_MAX), DEFAULT(DELAYED_QUEUE_SIZE), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static const char *event_scheduler_names[] = {"OFF", "ON", "DISABLED", NullS};
static bool event_scheduler_check(sys_var *, THD *, set_var *var) {
  /* DISABLED is only accepted on the command line */
  if (var->save_result.ulonglong_value == Events::EVENTS_DISABLED) return true;
  if (Events::opt_event_scheduler == Events::EVENTS_DISABLED) {
    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0),
             "--event-scheduler=DISABLED or --skip-grant-tables");
    return true;
  }
  return false;
}
static bool event_scheduler_update(sys_var *, THD *, enum_var_type) {
  int err_no = 0;
  ulong opt_event_scheduler_value = Events::opt_event_scheduler;
  mysql_mutex_unlock(&LOCK_global_system_variables);
  /*
    Events::start() is heavyweight. In particular it creates a new THD,
    which takes LOCK_global_system_variables internally.
    Thus we have to release it here.
    We need to re-take it before returning, though.

    Note that since we release LOCK_global_system_variables before calling
    start/stop, there is a possibility that the server variable
    can become out of sync with the real event scheduler state.

    This can happen with two concurrent statements if the first gets
    interrupted after start/stop but before retaking
    LOCK_global_system_variables. However, this problem should be quite
    rare and it's difficult to avoid it without opening up possibilities
    for deadlocks. See bug#51160.
  */
  bool ret = opt_event_scheduler_value == Events::EVENTS_ON
                 ? Events::start(&err_no)
                 : Events::stop();
  mysql_mutex_lock(&LOCK_global_system_variables);
  if (ret) {
    Events::opt_event_scheduler = Events::EVENTS_OFF;
    my_error(ER_EVENT_SET_VAR_ERROR, MYF(0), err_no);
  }
  return ret;
}

static Sys_var_enum Sys_event_scheduler(
    "event_scheduler",
    "Enable the event scheduler. Possible values are "
    "ON, OFF, and DISABLED (keep the event scheduler completely "
    "deactivated, it cannot be activated run-time)",
    GLOBAL_VAR(Events::opt_event_scheduler), CMD_LINE(OPT_ARG),
    event_scheduler_names, DEFAULT(Events::EVENTS_ON), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(event_scheduler_check),
    ON_UPDATE(event_scheduler_update));

static bool check_expire_logs_days(sys_var *, THD *, set_var *var) {
  ulonglong expire_logs_days_value = var->save_result.ulonglong_value;

  if (expire_logs_days_value && binlog_expire_logs_seconds) {
    my_error(ER_BINLOG_EXPIRE_LOG_DAYS_AND_SECS_USED_TOGETHER, MYF(0));
    return true;
  }
  return false;
}

static bool check_expire_logs_seconds(sys_var *, THD *, set_var *var) {
  ulonglong expire_logs_seconds_value = var->save_result.ulonglong_value;

  if (expire_logs_days && expire_logs_seconds_value) {
    my_error(ER_DA_EXPIRE_LOGS_DAYS_IGNORED, MYF(0));
    return true;
  }
  return false;
}

static Sys_var_ulong Sys_expire_logs_days(
    "expire_logs_days",
    "If non-zero, binary logs will be purged after expire_logs_days "
    "days; If this option alone is set on the command line or in a "
    "configuration file, it overrides the default value for "
    "binlog-expire-logs-seconds. If both options are set to nonzero values, "
    "binlog-expire-logs-seconds takes priority. Possible purges happen at "
    "startup and at binary log rotation.",
    GLOBAL_VAR(expire_logs_days), CMD_LINE(REQUIRED_ARG, OPT_EXPIRE_LOGS_DAYS),
    VALID_RANGE(0, 99), DEFAULT(0), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_expire_logs_days), ON_UPDATE(nullptr),
    DEPRECATED_VAR("binlog_expire_logs_seconds"));

static Sys_var_ulong Sys_binlog_expire_logs_seconds(
    "binlog_expire_logs_seconds",
    "If non-zero, binary logs will be purged after binlog_expire_logs_seconds"
    " seconds; If both this option and expire_logs_days are set to non-zero"
    "  values, this option takes priority. Purges happen at"
    " startup and at binary log rotation.",
    GLOBAL_VAR(binlog_expire_logs_seconds),
    CMD_LINE(REQUIRED_ARG, OPT_BINLOG_EXPIRE_LOGS_SECONDS),
    VALID_RANGE(0, 0xFFFFFFFF), DEFAULT(2592000), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_expire_logs_seconds), ON_UPDATE(nullptr));

static Sys_var_bool Sys_binlog_expire_logs_auto_purge(
    "binlog_expire_logs_auto_purge",
    "Controls whether the server shall automatically purge binary log "
    "files or not. If this variable is set to FALSE then the server will "
    "not purge binary log files automatically.",
    GLOBAL_VAR(opt_binlog_expire_logs_auto_purge), CMD_LINE(OPT_ARG),
    DEFAULT(true));

static Sys_var_bool Sys_flush(
    "flush", "Flush MyISAM tables to disk between SQL commands",
    GLOBAL_VAR(myisam_flush), CMD_LINE(OPT_ARG), DEFAULT(false));

static Sys_var_ulong Sys_flush_time(
    "flush_time",
    "A dedicated thread is created to flush all tables at the "
    "given interval",
    GLOBAL_VAR(flush_time), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, LONG_TIMEOUT), DEFAULT(0), BLOCK_SIZE(1));

static bool check_ftb_syntax(sys_var *, THD *, set_var *var) {
  return ft_boolean_check_syntax_string(
      (uchar *)(var->save_result.string_value.str));
}
/// @todo make SESSION_VAR (usability enhancement and a fix for a race
/// condition)
static Sys_var_charptr Sys_ft_boolean_syntax(
    "ft_boolean_syntax",
    "List of operators for "
    "MATCH ... AGAINST ( ... IN BOOLEAN MODE)",
    GLOBAL_VAR(ft_boolean_syntax), CMD_LINE(REQUIRED_ARG), IN_SYSTEM_CHARSET,
    DEFAULT(DEFAULT_FTB_SYNTAX), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_ftb_syntax));

static Sys_var_ulong Sys_ft_max_word_len(
    "ft_max_word_len",
    "The maximum length of the word to be included in a FULLTEXT index. "
    "Note: FULLTEXT indexes must be rebuilt after changing this variable",
    READ_ONLY GLOBAL_VAR(ft_max_word_len), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(10, HA_FT_MAXCHARLEN), DEFAULT(HA_FT_MAXCHARLEN),
    BLOCK_SIZE(1));

static Sys_var_ulong Sys_ft_min_word_len(
    "ft_min_word_len",
    "The minimum length of the word to be included in a FULLTEXT index. "
    "Note: FULLTEXT indexes must be rebuilt after changing this variable",
    READ_ONLY GLOBAL_VAR(ft_min_word_len), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, HA_FT_MAXCHARLEN), DEFAULT(4), BLOCK_SIZE(1));

/// @todo make it an updatable SESSION_VAR
static Sys_var_ulong Sys_ft_query_expansion_limit(
    "ft_query_expansion_limit",
    "Number of best matches to use for query expansion",
    READ_ONLY GLOBAL_VAR(ft_query_expansion_limit), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 1000), DEFAULT(20), BLOCK_SIZE(1));

static Sys_var_charptr Sys_ft_stopword_file(
    "ft_stopword_file", "Use stopwords from this file instead of built-in list",
    READ_ONLY NON_PERSIST GLOBAL_VAR(ft_stopword_file), CMD_LINE(REQUIRED_ARG),
    IN_FS_CHARSET, DEFAULT(nullptr));

static bool check_init_string(sys_var *, THD *, set_var *var) {
  if (var->save_result.string_value.str == nullptr) {
    var->save_result.string_value.str = const_cast<char *>("");
    var->save_result.string_value.length = 0;
  }
  return false;
}
static PolyLock_rwlock PLock_sys_init_connect(&LOCK_sys_init_connect);
static Sys_var_lexstring Sys_init_connect(
    "init_connect",
    "Command(s) that are executed for each "
    "new connection",
    GLOBAL_VAR(opt_init_connect), CMD_LINE(REQUIRED_ARG), IN_SYSTEM_CHARSET,
    DEFAULT(""), &PLock_sys_init_connect, NOT_IN_BINLOG,
    ON_CHECK(check_init_string));

static Sys_var_charptr Sys_init_file(
    "init_file", "Read SQL commands from this file at startup",
    READ_ONLY NON_PERSIST GLOBAL_VAR(opt_init_file), CMD_LINE(REQUIRED_ARG),
    IN_FS_CHARSET, DEFAULT(nullptr));

static PolyLock_rwlock PLock_sys_init_replica(&LOCK_sys_init_replica);
static Sys_var_lexstring Sys_init_replica(
    "init_replica",
    "Command(s) that are executed by the replication applier thread "
    "each time the applier threads start.",
    GLOBAL_VAR(opt_init_replica), CMD_LINE(REQUIRED_ARG), IN_SYSTEM_CHARSET,
    DEFAULT(""), &PLock_sys_init_replica, NOT_IN_BINLOG,
    ON_CHECK(check_init_string));

static Sys_var_deprecated_alias Sys_init_slave("init_slave", Sys_init_replica);

static Sys_var_ulong Sys_interactive_timeout(
    "interactive_timeout",
    "The number of seconds the server waits for activity on an interactive "
    "connection before closing it",
    SESSION_VAR(net_interactive_timeout), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, LONG_TIMEOUT), DEFAULT(NET_WAIT_TIMEOUT), BLOCK_SIZE(1));

static Sys_var_ulong Sys_join_buffer_size(
    "join_buffer_size", "The size of the buffer that is used for full joins",
    HINT_UPDATEABLE SESSION_VAR(join_buff_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(128, ULONG_MAX), DEFAULT(256 * 1024), BLOCK_SIZE(128));

static Sys_var_keycache Sys_key_buffer_size(
    "key_buffer_size",
    "The size of the buffer used for "
    "index blocks for MyISAM tables. Increase this to get better index "
    "handling (for all reads and multiple writes) to as much as you can "
    "afford",
    KEYCACHE_VAR(param_buff_size), CMD_LINE(REQUIRED_ARG, OPT_KEY_BUFFER_SIZE),
    VALID_RANGE(0, SIZE_T_MAX), DEFAULT(KEY_CACHE_SIZE), BLOCK_SIZE(IO_SIZE),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(update_buffer_size));

static Sys_var_keycache Sys_key_cache_block_size(
    "key_cache_block_size", "The default size of key cache blocks",
    KEYCACHE_VAR(param_block_size),
    CMD_LINE(REQUIRED_ARG, OPT_KEY_CACHE_BLOCK_SIZE),
    VALID_RANGE(512, 1024 * 16), DEFAULT(KEY_CACHE_BLOCK_SIZE), BLOCK_SIZE(512),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(update_keycache_param));

static Sys_var_keycache Sys_key_cache_division_limit(
    "key_cache_division_limit",
    "The minimum percentage of warm blocks in key cache",
    KEYCACHE_VAR(param_division_limit),
    CMD_LINE(REQUIRED_ARG, OPT_KEY_CACHE_DIVISION_LIMIT), VALID_RANGE(1, 100),
    DEFAULT(100), BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(nullptr), ON_UPDATE(update_keycache_param));

static Sys_var_keycache Sys_key_cache_age_threshold(
    "key_cache_age_threshold",
    "This characterizes the number of "
    "hits a hot block has to be untouched until it is considered aged "
    "enough to be downgraded to a warm block. This specifies the "
    "percentage ratio of that number of hits to the total number of "
    "blocks in key cache",
    KEYCACHE_VAR(param_age_threshold),
    CMD_LINE(REQUIRED_ARG, OPT_KEY_CACHE_AGE_THRESHOLD),
    VALID_RANGE(100, ULONG_MAX), DEFAULT(300), BLOCK_SIZE(100), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(update_keycache_param));

static Sys_var_bool Sys_large_files_support(
    "large_files_support",
    "Whether mysqld was compiled with options for large file support",
    READ_ONLY NON_PERSIST GLOBAL_VAR(opt_large_files), NO_CMD_LINE,
    DEFAULT(sizeof(my_off_t) > 4));

static Sys_var_uint Sys_large_page_size(
    "large_page_size",
    "If large page support is enabled, this shows the size of memory pages",
    READ_ONLY NON_PERSIST GLOBAL_VAR(opt_large_page_size), NO_CMD_LINE,
    VALID_RANGE(0, UINT_MAX), DEFAULT(0), BLOCK_SIZE(1));

static Sys_var_bool Sys_large_pages("large_pages",
                                    "Enable support for large pages",
                                    READ_ONLY GLOBAL_VAR(opt_large_pages),
                                    IF_WIN(NO_CMD_LINE, CMD_LINE(OPT_ARG)),
                                    DEFAULT(false));

static Sys_var_charptr Sys_language(
    "lc_messages_dir", "Directory where error messages are",
    READ_ONLY NON_PERSIST GLOBAL_VAR(lc_messages_dir_ptr),
    CMD_LINE(REQUIRED_ARG, OPT_LC_MESSAGES_DIRECTORY), IN_FS_CHARSET,
    DEFAULT(nullptr));

static Sys_var_bool Sys_local_infile("local_infile",
                                     "Enable LOAD DATA LOCAL INFILE",
                                     GLOBAL_VAR(opt_local_infile),
                                     CMD_LINE(OPT_ARG), DEFAULT(false));

static Sys_var_ulong Sys_lock_wait_timeout(
    "lock_wait_timeout",
    "Timeout in seconds to wait for a lock before returning an error.",
    HINT_UPDATEABLE SESSION_VAR(lock_wait_timeout), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, LONG_TIMEOUT), DEFAULT(LONG_TIMEOUT), BLOCK_SIZE(1));

#ifdef HAVE_MLOCKALL
static Sys_var_bool Sys_locked_in_memory(
    "locked_in_memory", "Whether mysqld was locked in memory with --memlock",
    READ_ONLY NON_PERSIST GLOBAL_VAR(locked_in_memory), NO_CMD_LINE,
    DEFAULT(false));
#endif

/* this says NO_CMD_LINE, as command-line option takes a string, not a bool */
static Sys_var_bool Sys_log_bin("log_bin", "Whether the binary log is enabled",
                                READ_ONLY NON_PERSIST GLOBAL_VAR(opt_bin_log),
                                NO_CMD_LINE, DEFAULT(true));

static bool transaction_write_set_check(sys_var *self, THD *thd, set_var *var) {
  if (check_session_admin(self, thd, var)) return true;
  // Can't change the algorithm when group replication is enabled.
  if (is_group_replication_running()) {
    my_message(
        ER_GROUP_REPLICATION_RUNNING,
        "The write set algorithm cannot be changed when Group replication"
        " is running.",
        MYF(0));
    return true;
  }

  if ((var->is_global_persist()) &&
      global_system_variables.binlog_format != BINLOG_FORMAT_ROW) {
    my_error(ER_PREVENTS_VARIABLE_WITHOUT_RBR, MYF(0), self->name.str);
    return true;
  }

  if (var->type == OPT_SESSION &&
      thd->variables.binlog_format != BINLOG_FORMAT_ROW) {
    my_error(ER_PREVENTS_VARIABLE_WITHOUT_RBR, MYF(0), self->name.str);
    return true;
  }
  /*
    if in a stored function/trigger, it's too late to change
  */
  if (thd->in_sub_stmt) {
    my_error(ER_VARIABLE_NOT_SETTABLE_IN_TRANSACTION, MYF(0), self->name.str);
    return true;
  }
  /*
    Make the session variable 'transaction_write_set_extraction' read-only
    inside a transaction.
  */
  if (thd->in_active_multi_stmt_transaction()) {
    my_error(ER_VARIABLE_NOT_SETTABLE_IN_TRANSACTION, MYF(0), self->name.str);
    return true;
  }
  /*
    Disallow changing variable 'transaction_write_set_extraction' while
    binlog_transaction_dependency_tracking is different from COMMIT_ORDER.
  */
  if (mysql_bin_log.m_dependency_tracker.m_opt_tracking_mode !=
      DEPENDENCY_TRACKING_COMMIT_ORDER) {
    my_error(ER_WRONG_USAGE, MYF(0),
             "transaction_write_set_extraction (changed)",
             "binlog_transaction_dependency_tracking (!= COMMIT_ORDER)");
    return true;
  }

  return false;
}

static Sys_var_enum Sys_extract_write_set(
    "transaction_write_set_extraction",
    "This option is used to let the server know when to "
    "extract the write set which will be used for various purposes. ",
    SESSION_VAR(transaction_write_set_extraction),
    CMD_LINE(OPT_ARG, OPT_TRANSACTION_WRITE_SET_EXTRACTION),
    transaction_write_set_hashing_algorithms, DEFAULT(HASH_ALGORITHM_XXHASH64),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(transaction_write_set_check),
    ON_UPDATE(nullptr), DEPRECATED_VAR(""));

static Sys_var_ulong Sys_rpl_stop_replica_timeout(
    "rpl_stop_replica_timeout",
    "Timeout in seconds to wait for replication threads to stop, before "
    "STOP REPLICA returns a warning.",
    GLOBAL_VAR(rpl_stop_replica_timeout), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(2, LONG_TIMEOUT), DEFAULT(LONG_TIMEOUT), BLOCK_SIZE(1));

static Sys_var_deprecated_alias Sys_rpl_stop_slave_timeout(
    "rpl_stop_slave_timeout", Sys_rpl_stop_replica_timeout);

static Sys_var_enum Sys_binlog_error_action(
    "binlog_error_action",
    "When statements cannot be written to the binary log due to a fatal "
    "error, this option determines whether the server ignores the error and "
    "closes the binary log, or aborts.",
    GLOBAL_VAR(binlog_error_action), CMD_LINE(REQUIRED_ARG),
    binlog_error_action_list, DEFAULT(ABORT_SERVER));

static Sys_var_bool Sys_trust_function_creators(
    "log_bin_trust_function_creators",
    "If set to FALSE (the default), then when --log-bin is used, creation "
    "of a stored function (or trigger) is allowed only to users having the "
    "SUPER privilege and only if this stored function (trigger) may not "
    "break binary logging. Note that if ALL connections to this server "
    "ALWAYS use row-based binary logging, the security issues do not "
    "exist and the binary logging cannot break, so you can safely set "
    "this to TRUE. This variable is deprecated and will be removed in a "
    "future version.",
    GLOBAL_VAR(trust_function_creators), CMD_LINE(OPT_ARG), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static Sys_var_bool Sys_check_proxy_users(
    "check_proxy_users",
    "If set to FALSE (the default), then proxy user identity will not be "
    "mapped for authentication plugins which support mapping from grant "
    "tables.  When set to TRUE, users associated with authentication "
    "plugins which signal proxy user mapping should be done according to "
    "GRANT PROXY privilege definition.",
    GLOBAL_VAR(check_proxy_users), CMD_LINE(OPT_ARG), DEFAULT(false));

static Sys_var_bool Sys_mysql_native_password_proxy_users(
    "mysql_native_password_proxy_users",
    "If set to FALSE (the default), then the mysql_native_password "
    "plugin will not signal for authenticated users to be checked for "
    "mapping "
    "to proxy users.  When set to TRUE, the plugin will flag associated "
    "authenticated accounts to be mapped to proxy users when the server "
    "option "
    "check_proxy_users is enabled.",
    GLOBAL_VAR(mysql_native_password_proxy_users), CMD_LINE(OPT_ARG),
    DEFAULT(false));

static Sys_var_bool Sys_sha256_password_proxy_users(
    "sha256_password_proxy_users",
    "If set to FALSE (the default), then the sha256_password authentication "
    "plugin will not signal for authenticated users to be checked for "
    "mapping "
    "to proxy users.  When set to TRUE, the plugin will flag associated "
    "authenticated accounts to be mapped to proxy users when the server "
    "option "
    "check_proxy_users is enabled.",
    GLOBAL_VAR(sha256_password_proxy_users), CMD_LINE(OPT_ARG), DEFAULT(false));

static bool check_log_bin_use_v1_row_events(sys_var *, THD *thd, set_var *var) {
  if (var->save_result.ulonglong_value == 1 &&
      global_system_variables.binlog_row_value_options != 0)
    push_warning_printf(thd, Sql_condition::SL_WARNING,
                        ER_WARN_BINLOG_V1_ROW_EVENTS_DISABLED,
                        ER_THD(thd, ER_WARN_BINLOG_V1_ROW_EVENTS_DISABLED),
                        "binlog_row_value_options=PARTIAL_JSON");
  return false;
}

static Sys_var_bool Sys_log_bin_use_v1_row_events(
    "log_bin_use_v1_row_events",
    "If equal to 1 then version 1 row events are written to a row based "
    "binary log.  If equal to 0, then the latest version of events are "
    "written.  "
    "This option is useful during some upgrades.",
    NON_PERSIST GLOBAL_VAR(log_bin_use_v1_row_events),
    CMD_LINE(OPT_ARG, OPT_LOG_BIN_USE_V1_ROW_EVENTS), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_log_bin_use_v1_row_events),
    ON_UPDATE(nullptr), DEPRECATED_VAR(""));

static Sys_var_charptr Sys_log_error(
    "log_error", "Error log file",
    READ_ONLY NON_PERSIST GLOBAL_VAR(log_error_dest),
    CMD_LINE(OPT_ARG, OPT_LOG_ERROR), IN_FS_CHARSET,
    DEFAULT(disabled_my_option), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(nullptr), ON_UPDATE(nullptr), nullptr, sys_var::PARSE_EARLY);

static bool check_log_error_services(sys_var *self, THD *thd, set_var *var) {
  // test whether syntax is OK and services exist
  size_t pos;
  log_error_stack_error ret;

  if (var->save_result.string_value.str == nullptr) return true;

  ret = log_builtins_error_stack(var->save_result.string_value.str, true, &pos);

  if (strlen(var->save_result.string_value.str) < 1) {
    push_warning_printf(
        thd, Sql_condition::SL_WARNING, ER_EMPTY_PIPELINE_FOR_ERROR_LOG_SERVICE,
        ER_THD(thd, ER_EMPTY_PIPELINE_FOR_ERROR_LOG_SERVICE), self->name.str);
  } else if (ret != LOG_ERROR_STACK_SUCCESS) {
    int err_code = 0;
    switch (ret) {
      case LOG_ERROR_STACK_NO_PFS_SUPPORT:
        err_code = ER_DA_ERROR_LOG_TABLE_DISABLED;
        break;
      case LOG_ERROR_STACK_NO_LOG_PARSER:
        err_code = ER_DA_NO_ERROR_LOG_PARSER_CONFIGURED;
        break;
      case LOG_ERROR_MULTIPLE_FILTERS:
        err_code = ER_DA_ERROR_LOG_MULTIPLE_FILTERS;
        break;
      default:
        push_warning_printf(
            thd, Sql_condition::SL_WARNING, ER_CANT_SET_ERROR_LOG_SERVICE,
            ER_THD(thd, ER_CANT_SET_ERROR_LOG_SERVICE), self->name.str,
            &((char *)var->save_result.string_value.str)[pos]);
        return true;
    }

    push_warning(thd, Sql_condition::SL_NOTE, err_code,
                 ER_THD_NONCONST(thd, err_code));
    return false;
  }

  return false;
}

static bool fix_log_error_services(sys_var *self [[maybe_unused]], THD *thd,
                                   enum_var_type type [[maybe_unused]]) {
  bool ret = false;
  // syntax is OK and services exist; try to initialize them!
  size_t pos;

  char *pipeline_config =
      my_strdup(PSI_NOT_INSTRUMENTED, opt_log_error_services, MYF(0));

  if (pipeline_config == nullptr) return true;

  /*
    Temporarily release mutex.
    This solves two issues:

    a) Setting up the error-logger may implicitly load external
       logging components. The init-function of such a component
       may try to install a system-variable and then ask the system
       for a (persisted / passed on the command-line / ...) initial
       value for said variable. The function in the component framework
       that tries to obtain this value tries to obtain the mutex
       LOCK_global_system_variables.

       Note that implicit loading is attempted during the pre-check
       phase and thus should already have happened at this stage
       and no longer be a concern.

    b) This function is called with the mutex held.
       log_builtins_error_stack() will obtain an exclusive lock on
       THR_LOCK_log_stack while it re-configures the error-logger.
       A different session might run FLUSH ERROR LOGS at the same time.
       This obtains THR_LOCK_log_stack first; an individual component's
       flush function might then try to re-install its system-variables
       on flush, which would try to obtain LOCK_global_system_variables
       as per above. I.e. both functions would try to obtain the two
       locks in a different order.

       Note that components should not behave that way; they should
       install/uninstall their variables on init/exit, not on open/close.

     Both issues are admittedly unlikely, but guarding against them is cheap.
  */
  mysql_mutex_unlock(&LOCK_global_system_variables);

  if (log_builtins_error_stack(pipeline_config, false, &pos) < 0) {
    if (pos < strlen(pipeline_config)) /* purecov: begin inspected */
      push_warning_printf(thd, Sql_condition::SL_WARNING,
                          ER_CANT_START_ERROR_LOG_SERVICE,
                          ER_THD(thd, ER_CANT_START_ERROR_LOG_SERVICE),
                          self->name.str, &((char *)pipeline_config)[pos]);
    ret = true; /* purecov: end */
  }

  my_free(pipeline_config);

  mysql_mutex_lock(&LOCK_global_system_variables);

  return ret;
}

static Sys_var_charptr Sys_log_error_services(
    "log_error_services",
    "Services that should be called when an error event is received",
    PERSIST_AS_READONLY GLOBAL_VAR(opt_log_error_services),
    CMD_LINE(REQUIRED_ARG), IN_SYSTEM_CHARSET,
    DEFAULT(LOG_ERROR_SERVICES_DEFAULT), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_log_error_services), ON_UPDATE(fix_log_error_services),
    /*
      We parse it early so it goes into one logical chunk with log_error
      and log_timestamps, but we don't activate it immediately. We need
      to wait until component_infrastructure_init() has run, but want to
      set up logging services before get_options() is run. That way, any
      loadable components are ready in case component system variables
      are set from get_options().
    */
    nullptr, sys_var::PARSE_EARLY);

static bool check_log_error_suppression_list(sys_var *self, THD *thd,
                                             set_var *var) {
  int i;

  if (var->save_result.string_value.str == nullptr) return true;

  if ((i = log_builtins_filter_parse_suppression_list(
           var->save_result.string_value.str, false)) < 0) {
    push_warning_printf(
        thd, Sql_condition::SL_WARNING, ER_CANT_SET_ERROR_SUPPRESSION_LIST,
        ER_THD(thd, ER_CANT_SET_ERROR_SUPPRESSION_LIST), self->name.str,
        &((char *)var->save_result.string_value.str)[-(i + 1)]);
    return true;
  }

  return false;
}

static bool fix_log_error_suppression_list(sys_var *self [[maybe_unused]],
                                           THD *thd [[maybe_unused]],
                                           enum_var_type type
                                           [[maybe_unused]]) {
  // syntax is OK and errcodes have messages; try to make filter rules for
  // them!
  int rr = log_builtins_filter_parse_suppression_list(
      opt_log_error_suppression_list, true);
  return (rr < 0) ? true : false;
}

static Sys_var_charptr Sys_log_error_suppression_list(
    "log_error_suppression_list",
    "Comma-separated list of error-codes. Error messages corresponding to "
    "these codes will not be included in the error log. Only events with a "
    "severity of Warning or Information can be suppressed; events with "
    "System "
    "or Error severity will always be included. Requires the filter "
    "\'log_filter_internal\' to be set in @@global.log_error_services, which "
    "is the default.",
    PERSIST_AS_READONLY GLOBAL_VAR(opt_log_error_suppression_list),
    CMD_LINE(REQUIRED_ARG), IN_SYSTEM_CHARSET, DEFAULT(""), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_log_error_suppression_list),
    ON_UPDATE(fix_log_error_suppression_list));

static Sys_var_bool Sys_log_queries_not_using_indexes(
    "log_queries_not_using_indexes",
    "Log queries that are executed without benefit of any index to the "
    "slow log if it is open",
    GLOBAL_VAR(opt_log_queries_not_using_indexes), CMD_LINE(OPT_ARG),
    DEFAULT(false));

static Sys_var_bool Sys_log_slow_admin_statements(
    "log_slow_admin_statements",
    "Log slow OPTIMIZE, ANALYZE, ALTER and other administrative statements "
    "to "
    "the slow log if it is open.",
    GLOBAL_VAR(opt_log_slow_admin_statements), CMD_LINE(OPT_ARG),
    DEFAULT(false));

static Sys_var_bool Sys_log_slow_replica_statements(
    "log_slow_replica_statements",
    "Log slow statements executed by the replication applier threads to the "
    "slow log if it is open.",
    GLOBAL_VAR(opt_log_slow_replica_statements), CMD_LINE(OPT_ARG),
    DEFAULT(false));

static Sys_var_deprecated_alias Sys_log_slow_slave_statements(
    "log_slow_slave_statements", Sys_log_slow_replica_statements);

static bool update_log_throttle_queries_not_using_indexes(sys_var *, THD *thd,
                                                          enum_var_type) {
  // Check if we should print a summary of any suppressed lines to the slow
  // log now since opt_log_throttle_queries_not_using_indexes was changed.
  log_throttle_qni.flush(thd);
  return false;
}

static Sys_var_ulong Sys_log_throttle_queries_not_using_indexes(
    "log_throttle_queries_not_using_indexes",
    "Log at most this many 'not using index' warnings per minute to the "
    "slow log. Any further warnings will be condensed into a single "
    "summary line. A value of 0 disables throttling. "
    "Option has no effect unless --log_queries_not_using_indexes is set.",
    GLOBAL_VAR(opt_log_throttle_queries_not_using_indexes),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, ULONG_MAX), DEFAULT(0),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(update_log_throttle_queries_not_using_indexes));

static bool update_log_error_verbosity(sys_var *, THD *, enum_var_type) {
  return (log_builtins_filter_update_verbosity(log_error_verbosity) < 0);
}

static Sys_var_ulong Sys_log_error_verbosity(
    "log_error_verbosity",
    "How detailed the error log should be. "
    "1, log errors only. "
    "2, log errors and warnings. "
    "3, log errors, warnings, and notes. "
    "Messages sent to the client are unaffected by this setting.",
    PERSIST_AS_READONLY GLOBAL_VAR(log_error_verbosity), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, 3), DEFAULT(2), BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(nullptr), ON_UPDATE(update_log_error_verbosity));

static Sys_var_enum Sys_log_timestamps(
    "log_timestamps",
    "UTC to timestamp log files in zulu time, for more concise timestamps "
    "and easier correlation of logs from servers from multiple time zones, "
    "or SYSTEM to use the system's local time. "
    "This affects only log files, not log tables, as the timestamp columns "
    "of the latter can be converted at will.",
    GLOBAL_VAR(opt_log_timestamps), CMD_LINE(REQUIRED_ARG),
    timestamp_type_names, DEFAULT(0), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(nullptr), ON_UPDATE(nullptr), nullptr,
    /* log_error is an early option, so its timestamp format should be, too. */
    sys_var::PARSE_EARLY);

static Sys_var_bool Sys_log_statements_unsafe_for_binlog(
    "log_statements_unsafe_for_binlog",
    "Log statements considered unsafe when using statement based binary "
    "logging. This variable is deprecated and will be removed in a "
    "future version.",
    GLOBAL_VAR(opt_log_unsafe_statements), CMD_LINE(OPT_ARG), DEFAULT(true),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static bool update_cached_long_query_time(sys_var *, THD *thd,
                                          enum_var_type type) {
  if (type == OPT_SESSION)
    thd->variables.long_query_time =
        double2ulonglong(thd->variables.long_query_time_double * 1e6);
  else
    global_system_variables.long_query_time =
        double2ulonglong(global_system_variables.long_query_time_double * 1e6);
  return false;
}

static Sys_var_double Sys_long_query_time(
    "long_query_time",
    "Log all queries that have taken more than long_query_time seconds "
    "to execute to file. The argument will be treated as a decimal value "
    "with microsecond precision",
    SESSION_VAR(long_query_time_double), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, LONG_TIMEOUT), DEFAULT(10), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(nullptr), ON_UPDATE(update_cached_long_query_time));

static bool fix_low_prio_updates(sys_var *, THD *thd, enum_var_type type) {
  if (type == OPT_SESSION) {
    thd->update_lock_default =
        (thd->variables.low_priority_updates ? TL_WRITE_LOW_PRIORITY
                                             : TL_WRITE);
    thd->insert_lock_default =
        (thd->variables.low_priority_updates ? TL_WRITE_LOW_PRIORITY
                                             : TL_WRITE_CONCURRENT_INSERT);
  } else
    thr_upgraded_concurrent_insert_lock =
        (global_system_variables.low_priority_updates ? TL_WRITE_LOW_PRIORITY
                                                      : TL_WRITE);
  return false;
}
static Sys_var_bool Sys_low_priority_updates(
    "low_priority_updates",
    "INSERT/DELETE/UPDATE has lower priority than selects",
    SESSION_VAR(low_priority_updates), CMD_LINE(OPT_ARG), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_session_admin_no_super),
    ON_UPDATE(fix_low_prio_updates));

static Sys_var_bool Sys_lower_case_file_system(
    "lower_case_file_system",
    "Case sensitivity of file names on the file system where the "
    "data directory is located",
    READ_ONLY NON_PERSIST GLOBAL_VAR(lower_case_file_system), NO_CMD_LINE,
    DEFAULT(false));

static Sys_var_uint Sys_lower_case_table_names(
    "lower_case_table_names",
    "If set to 1 table names are stored in lowercase on disk and table "
    "names will be case-insensitive.  Should be set to 2 if you are using "
    "a case insensitive file system",
    READ_ONLY GLOBAL_VAR(lower_case_table_names),
    CMD_LINE(OPT_ARG, OPT_LOWER_CASE_TABLE_NAMES), VALID_RANGE(0, 2),
#ifdef FN_NO_CASE_SENSE
    DEFAULT(1),
#else
    DEFAULT(0),
#endif
    BLOCK_SIZE(1));

static bool session_readonly(sys_var *self, THD *, set_var *var) {
  if (var->is_global_persist()) return false;
  my_error(ER_VARIABLE_IS_READONLY, MYF(0), "SESSION", self->name.str,
           "GLOBAL");
  return true;
}

static bool check_max_allowed_packet(sys_var *self, THD *thd, set_var *var) {
  longlong val;
  if (session_readonly(self, thd, var)) return true;

  val = var->save_result.ulonglong_value;
  if (val < (longlong)global_system_variables.net_buffer_length) {
    push_warning_printf(thd, Sql_condition::SL_WARNING, WARN_OPTION_BELOW_LIMIT,
                        ER_THD(thd, WARN_OPTION_BELOW_LIMIT),
                        "max_allowed_packet", "net_buffer_length");
  }
  return false;
}

static Sys_var_ulong Sys_max_allowed_packet(
    "max_allowed_packet",
    "Max packet length to send to or receive from the server",
    SESSION_VAR(max_allowed_packet), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1024, 1024 * 1024 * 1024), DEFAULT(64 * 1024 * 1024),
    BLOCK_SIZE(1024), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_max_allowed_packet));

static Sys_var_ulong Sys_replica_max_allowed_packet(
    "replica_max_allowed_packet",
    "The maximum size of packets sent from an upstream source server to this "
    "server.",
    GLOBAL_VAR(replica_max_allowed_packet), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1024, binary_log::max_log_event_size),
    DEFAULT(binary_log::max_log_event_size), BLOCK_SIZE(1024));

static Sys_var_deprecated_alias Sys_slave_max_allowed_packet(
    "slave_max_allowed_packet", Sys_replica_max_allowed_packet);

static Sys_var_ulonglong Sys_max_binlog_cache_size(
    "max_binlog_cache_size", "Sets the total size of the transactional cache",
    GLOBAL_VAR(max_binlog_cache_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(IO_SIZE, ULLONG_MAX), DEFAULT((ULLONG_MAX / IO_SIZE) * IO_SIZE),
    BLOCK_SIZE(IO_SIZE), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_binlog_cache_size));

static Sys_var_ulonglong Sys_max_binlog_stmt_cache_size(
    "max_binlog_stmt_cache_size", "Sets the total size of the statement cache",
    GLOBAL_VAR(max_binlog_stmt_cache_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(IO_SIZE, ULLONG_MAX), DEFAULT((ULLONG_MAX / IO_SIZE) * IO_SIZE),
    BLOCK_SIZE(IO_SIZE), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_binlog_stmt_cache_size));

static bool fix_max_binlog_size(sys_var *, THD *, enum_var_type) {
  mysql_bin_log.set_max_size(max_binlog_size);
  /*
    For multisource replication, this max size is set to all relay logs
    per channel. So, run through them
  */
  if (!max_relay_log_size) {
    Master_info *mi = nullptr;

    channel_map.wrlock();
    for (mi_map::iterator it = channel_map.begin(); it != channel_map.end();
         it++) {
      mi = it->second;
      if (mi != nullptr) mi->rli->relay_log.set_max_size(max_binlog_size);
    }
    channel_map.unlock();
  }
  return false;
}
static Sys_var_ulong Sys_max_binlog_size(
    "max_binlog_size",
    "Binary log will be rotated automatically when the size exceeds this "
    "value. Will also apply to relay logs if max_relay_log_size is 0",
    GLOBAL_VAR(max_binlog_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(IO_SIZE, 1024 * 1024L * 1024L), DEFAULT(1024 * 1024L * 1024L),
    BLOCK_SIZE(IO_SIZE), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_max_binlog_size));

static Sys_var_ulong Sys_max_connections(
    "max_connections", "The number of simultaneous clients allowed",
    GLOBAL_VAR(max_connections), CMD_LINE(REQUIRED_ARG), VALID_RANGE(1, 100000),
    DEFAULT(MAX_CONNECTIONS_DEFAULT), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr), nullptr,
    /* max_connections is used as a sizing hint by the performance schema. */
    sys_var::PARSE_EARLY);

static Sys_var_ulong Sys_max_connect_errors(
    "max_connect_errors",
    "If there is more than this number of interrupted connections from "
    "a host this host will be blocked from further connections",
    GLOBAL_VAR(max_connect_errors), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, ULONG_MAX), DEFAULT(100), BLOCK_SIZE(1));

static Sys_var_long Sys_max_digest_length(
    "max_digest_length", "Maximum length considered for digest text.",
    READ_ONLY GLOBAL_VAR(max_digest_length), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 1024 * 1024), DEFAULT(1024), BLOCK_SIZE(1));

static bool check_max_delayed_threads(sys_var *self, THD *thd, set_var *var) {
  if (check_session_admin_no_super(self, thd, var)) return true;
  return (!var->is_global_persist()) && var->save_result.ulonglong_value != 0 &&
         var->save_result.ulonglong_value !=
             global_system_variables.max_insert_delayed_threads;
}

// Alias for max_delayed_threads
static Sys_var_ulong Sys_max_insert_delayed_threads(
    "max_insert_delayed_threads",
    "Don't start more than this number of threads to handle INSERT "
    "DELAYED statements. If set to zero INSERT DELAYED will be not used. "
    "This variable is deprecated along with INSERT DELAYED.",
    SESSION_VAR(max_insert_delayed_threads), NO_CMD_LINE, VALID_RANGE(0, 16384),
    DEFAULT(20), BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_max_delayed_threads), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static Sys_var_ulong Sys_max_delayed_threads(
    "max_delayed_threads",
    "Don't start more than this number of threads to handle INSERT "
    "DELAYED statements. If set to zero INSERT DELAYED will be not used. "
    "This variable is deprecated along with INSERT DELAYED.",
    SESSION_VAR(max_insert_delayed_threads), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 16384), DEFAULT(20), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_max_delayed_threads), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static Sys_var_ulong Sys_max_error_count(
    "max_error_count", "Max number of errors/warnings to store for a statement",
    HINT_UPDATEABLE SESSION_VAR(max_error_count), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 65535), DEFAULT(DEFAULT_ERROR_COUNT), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_session_admin_no_super));

static Sys_var_ulonglong Sys_max_heap_table_size(
    "max_heap_table_size",
    "Don't allow creation of heap tables bigger than this",
    HINT_UPDATEABLE SESSION_VAR(max_heap_table_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(16384, (ulonglong) ~(intptr)0), DEFAULT(16 * 1024 * 1024),
    BLOCK_SIZE(1024));

// relies on assert(sizeof(my_thread_id) == 4);
static Sys_var_uint Sys_pseudo_thread_id(
    "pseudo_thread_id", "This variable is for internal server use",
    SESSION_ONLY(pseudo_thread_id), NO_CMD_LINE, VALID_RANGE(0, UINT_MAX32),
    DEFAULT(0), BLOCK_SIZE(1), NO_MUTEX_GUARD, IN_BINLOG,
    ON_CHECK(check_session_admin));

static bool fix_max_join_size(sys_var *self, THD *thd, enum_var_type type) {
  System_variables *sv = (self->is_global_persist(type))
                             ? &global_system_variables
                             : &thd->variables;
  if (sv->max_join_size == HA_POS_ERROR)
    sv->option_bits |= OPTION_BIG_SELECTS;
  else
    sv->option_bits &= ~OPTION_BIG_SELECTS;
  return false;
}
static Sys_var_harows Sys_max_join_size(
    "max_join_size",
    "Joins that are probably going to read more than max_join_size "
    "records return an error",
    HINT_UPDATEABLE SESSION_VAR(max_join_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, HA_POS_ERROR), DEFAULT(HA_POS_ERROR), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_max_join_size));

static Sys_var_ulong Sys_max_seeks_for_key(
    "max_seeks_for_key",
    "Limit assumed max number of seeks when looking up rows based on a key",
    HINT_UPDATEABLE SESSION_VAR(max_seeks_for_key), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, ULONG_MAX), DEFAULT(ULONG_MAX), BLOCK_SIZE(1));

static Sys_var_ulong Sys_max_length_for_sort_data(
    "max_length_for_sort_data",
    "This variable is deprecated and will be removed in a future release.",
    HINT_UPDATEABLE SESSION_VAR(max_length_for_sort_data),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(4, 8192 * 1024L), DEFAULT(4096),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(nullptr), DEPRECATED_VAR(""));

static Sys_var_ulong Sys_max_points_in_geometry(
    "max_points_in_geometry", "Maximum number of points in a geometry",
    HINT_UPDATEABLE SESSION_VAR(max_points_in_geometry), CMD_LINE(OPT_ARG),
    VALID_RANGE(3, 1024 * 1024L), DEFAULT(64 * 1024), BLOCK_SIZE(1));

static PolyLock_mutex PLock_prepared_stmt_count(&LOCK_prepared_stmt_count);

static Sys_var_ulong Sys_max_prepared_stmt_count(
    "max_prepared_stmt_count",
    "Maximum number of prepared statements in the server",
    GLOBAL_VAR(max_prepared_stmt_count), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, num_prepared_stmt_limit), DEFAULT(16382), BLOCK_SIZE(1),
    &PLock_prepared_stmt_count, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(nullptr), nullptr,
    /* max_prepared_stmt_count is used as a sizing hint by the performance
       schema. */
    sys_var::PARSE_EARLY);

static bool fix_max_relay_log_size(sys_var *, THD *, enum_var_type) {
  Master_info *mi = nullptr;

  channel_map.wrlock();
  for (mi_map::iterator it = channel_map.begin(); it != channel_map.end();
       it++) {
    mi = it->second;

    if (mi != nullptr)
      mi->rli->relay_log.set_max_size(max_relay_log_size ? max_relay_log_size
                                                         : max_binlog_size);
  }
  channel_map.unlock();
  return false;
}
static Sys_var_ulong Sys_max_relay_log_size(
    "max_relay_log_size",
    "If non-zero: relay log will be rotated automatically when the "
    "size exceeds this value; if zero: when the size "
    "exceeds max_binlog_size",
    GLOBAL_VAR(max_relay_log_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 1024L * 1024 * 1024), DEFAULT(0), BLOCK_SIZE(IO_SIZE),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_max_relay_log_size));

static Sys_var_ulong Sys_max_sort_length(
    "max_sort_length",
    "The number of bytes to use when sorting long values with PAD SPACE "
    "collations (only the first max_sort_length bytes of each value are "
    "used; the rest are ignored)",
    HINT_UPDATEABLE SESSION_VAR(max_sort_length), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(4, 8192 * 1024L), DEFAULT(1024), BLOCK_SIZE(1));

static Sys_var_ulong Sys_max_sp_recursion_depth(
    "max_sp_recursion_depth", "Maximum stored procedure recursion depth",
    SESSION_VAR(max_sp_recursion_depth), CMD_LINE(OPT_ARG), VALID_RANGE(0, 255),
    DEFAULT(0), BLOCK_SIZE(1));

// non-standard session_value_ptr() here
static Sys_var_max_user_conn Sys_max_user_connections(
    "max_user_connections",
    "The maximum number of active connections for a single user "
    "(0 = no limit)",
    SESSION_VAR(max_user_connections), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, UINT_MAX), DEFAULT(0), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(session_readonly));

static Sys_var_ulong Sys_max_write_lock_count(
    "max_write_lock_count",
    "After this many write locks, allow some read locks to run in between",
    GLOBAL_VAR(max_write_lock_count), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, ULONG_MAX), DEFAULT(ULONG_MAX), BLOCK_SIZE(1));

static Sys_var_ulong Sys_min_examined_row_limit(
    "min_examined_row_limit",
    "Don't write queries to slow log that examine fewer rows "
    "than that",
    SESSION_VAR(min_examined_row_limit), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, ULONG_MAX), DEFAULT(0), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_session_admin_no_super));

#ifdef _WIN32
static Sys_var_bool Sys_named_pipe("named_pipe", "Enable the named pipe (NT)",
                                   READ_ONLY NON_PERSIST
                                       GLOBAL_VAR(opt_enable_named_pipe),
                                   CMD_LINE(OPT_ARG), DEFAULT(false));

static PolyLock_rwlock PLock_named_pipe_full_access_group(
    &LOCK_named_pipe_full_access_group);
static bool check_named_pipe_full_access_group(sys_var *self, THD *,
                                               set_var *var) {
  if (!var->value) return false;  // DEFAULT is ok

  if (!is_valid_named_pipe_full_access_group(
          var->save_result.string_value.str)) {
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), self->name.str,
             var->save_result.string_value.str);
    return true;
  }
  return false;
}
static bool fix_named_pipe_full_access_group(sys_var *, THD *, enum_var_type) {
  return update_named_pipe_full_access_group(named_pipe_full_access_group);
}
static Sys_var_charptr Sys_named_pipe_full_access_group(
    "named_pipe_full_access_group",
    "Name of Windows group granted full access to the named pipe",
    GLOBAL_VAR(named_pipe_full_access_group),
    CMD_LINE(REQUIRED_ARG, OPT_NAMED_PIPE_FULL_ACCESS_GROUP), IN_FS_CHARSET,
    DEFAULT(DEFAULT_NAMED_PIPE_FULL_ACCESS_GROUP),
    &PLock_named_pipe_full_access_group, NOT_IN_BINLOG,
    ON_CHECK(check_named_pipe_full_access_group),
    ON_UPDATE(fix_named_pipe_full_access_group));
#endif

static bool check_net_buffer_length(sys_var *self, THD *thd, set_var *var) {
  longlong val;
  if (session_readonly(self, thd, var)) return true;

  val = var->save_result.ulonglong_value;
  if (val > (longlong)global_system_variables.max_allowed_packet) {
    push_warning_printf(thd, Sql_condition::SL_WARNING, WARN_OPTION_BELOW_LIMIT,
                        ER_THD(thd, WARN_OPTION_BELOW_LIMIT),
                        "max_allowed_packet", "net_buffer_length");
  }
  return false;
}
static Sys_var_ulong Sys_net_buffer_length(
    "net_buffer_length", "Buffer length for TCP/IP and socket communication",
    SESSION_VAR(net_buffer_length), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1024, 1024 * 1024), DEFAULT(16384), BLOCK_SIZE(1024),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_net_buffer_length));

static bool fix_net_read_timeout(sys_var *self, THD *thd, enum_var_type type) {
  if (!self->is_global_persist(type)) {
    // net_buffer_length is a specific property for the classic protocols
    if (!thd->is_classic_protocol()) {
      my_error(ER_PLUGGABLE_PROTOCOL_COMMAND_NOT_SUPPORTED, MYF(0));
      return true;
    }
    my_net_set_read_timeout(thd->get_protocol_classic()->get_net(),
                            thd->variables.net_read_timeout);
  }
  return false;
}
static Sys_var_ulong Sys_net_read_timeout(
    "net_read_timeout",
    "Number of seconds to wait for more data from a connection before "
    "aborting the read",
    SESSION_VAR(net_read_timeout), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, LONG_TIMEOUT), DEFAULT(NET_READ_TIMEOUT), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_net_read_timeout));

static bool fix_net_write_timeout(sys_var *self, THD *thd, enum_var_type type) {
  if (!self->is_global_persist(type)) {
    // net_read_timeout is a specific property for the classic protocols
    if (!thd->is_classic_protocol()) {
      my_error(ER_PLUGGABLE_PROTOCOL_COMMAND_NOT_SUPPORTED, MYF(0));
      return true;
    }
    my_net_set_write_timeout(thd->get_protocol_classic()->get_net(),
                             thd->variables.net_write_timeout);
  }
  return false;
}
static Sys_var_ulong Sys_net_write_timeout(
    "net_write_timeout",
    "Number of seconds to wait for a block to be written to a connection "
    "before aborting the write",
    SESSION_VAR(net_write_timeout), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, LONG_TIMEOUT), DEFAULT(NET_WRITE_TIMEOUT), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_net_write_timeout));

static bool fix_net_retry_count(sys_var *self, THD *thd, enum_var_type type) {
  if (!self->is_global_persist(type)) {
    // net_write_timeout is a specific property for the classic protocols
    if (!thd->is_classic_protocol()) {
      my_error(ER_PLUGGABLE_PROTOCOL_COMMAND_NOT_SUPPORTED, MYF(0));
      return true;
    }
    thd->get_protocol_classic()->get_net()->retry_count =
        thd->variables.net_retry_count;
  }
  return false;
}
static Sys_var_ulong Sys_net_retry_count(
    "net_retry_count",
    "If a read on a communication port is interrupted, retry this "
    "many times before giving up",
    SESSION_VAR(net_retry_count), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, ULONG_MAX), DEFAULT(MYSQLD_NET_RETRY_COUNT), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_net_retry_count));

static Sys_var_bool Sys_new_mode("new",
                                 "Use very new possible \"unsafe\" functions",
                                 SESSION_VAR(new_mode), CMD_LINE(OPT_ARG, 'n'),
                                 DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG,
                                 ON_CHECK(nullptr), ON_UPDATE(nullptr),
                                 DEPRECATED_VAR(""));

static Sys_var_bool Sys_old_mode("old", "Use compatible behavior",
                                 READ_ONLY GLOBAL_VAR(old_mode),
                                 CMD_LINE(OPT_ARG, OPT_OLD_OPTION),
                                 DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG,
                                 ON_CHECK(nullptr), ON_UPDATE(nullptr),
                                 DEPRECATED_VAR(""));

static Sys_var_bool Sys_old_alter_table("old_alter_table",
                                        "Use old, non-optimized alter table",
                                        SESSION_VAR(old_alter_table),
                                        CMD_LINE(OPT_ARG), DEFAULT(false));

static Sys_var_ulong Sys_open_files_limit(
    "open_files_limit",
    "If this is not 0, then mysqld will use this value to reserve file "
    "descriptors to use with setrlimit(). If this value is 0 then mysqld "
    "will reserve max_connections*5 or max_connections + table_open_cache*2 "
    "(whichever is larger) number of file descriptors",
    READ_ONLY GLOBAL_VAR(open_files_limit), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, OS_FILE_LIMIT), DEFAULT(0), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr), nullptr,
    /* open_files_limit is used as a sizing hint by the performance schema. */
    sys_var::PARSE_EARLY);

/// @todo change to enum
static Sys_var_ulong Sys_optimizer_prune_level(
    "optimizer_prune_level",
    "Controls the heuristic(s) applied during query optimization to prune "
    "less-promising partial plans from the optimizer search space. "
    "Meaning: 0 - do not apply any heuristic, thus perform exhaustive "
    "search; 1 - prune plans based on number of retrieved rows",
    HINT_UPDATEABLE SESSION_VAR(optimizer_prune_level), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 1), DEFAULT(1), BLOCK_SIZE(1));

static Sys_var_ulong Sys_optimizer_search_depth(
    "optimizer_search_depth",
    "Maximum depth of search performed by the query optimizer. Values "
    "larger than the number of relations in a query result in better "
    "query plans, but take longer to compile a query. Values smaller "
    "than the number of tables in a relation result in faster "
    "optimization, but may produce very bad query plans. If set to 0, "
    "the system will automatically pick a reasonable value",
    HINT_UPDATEABLE SESSION_VAR(optimizer_search_depth), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, MAX_TABLES + 1), DEFAULT(MAX_TABLES + 1), BLOCK_SIZE(1));

static Sys_var_ulong Sys_optimizer_max_subgraph_pairs(
    "optimizer_max_subgraph_pairs",
    "Maximum depth of subgraph pairs a query can have before the "
    "hypergraph join optimizer starts reducing the search space "
    "heuristically. Larger values may result in better query plans "
    "for large queries, but also more time and memory spent during planning. "
    "Increasing this larger than the actual number of subgraph pairs "
    "in the query will have no further effect. "
    "Ignored by the old (non-hypergraph) join optimizer",
    HINT_UPDATEABLE SESSION_VAR(optimizer_max_subgraph_pairs),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(1, INT_MAX), DEFAULT(100000),
    BLOCK_SIZE(1));

static Sys_var_ulong Sys_range_optimizer_max_mem_size(
    "range_optimizer_max_mem_size",
    "Maximum amount of memory used by the range optimizer "
    "to allocate predicates during range analysis. "
    "The larger the number, more memory may be consumed during "
    "range analysis. If the value is too low to completed range "
    "optimization of a query, index range scan will not be "
    "considered for this query. A value of 0 means range optimizer "
    "does not have any cap on memory. ",
    HINT_UPDATEABLE SESSION_VAR(range_optimizer_max_mem_size),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, ULONG_MAX), DEFAULT(8388608),
    BLOCK_SIZE(1));

static bool limit_parser_max_mem_size(sys_var *, THD *thd, set_var *var) {
  if (var->is_global_persist()) return false;
  ulonglong val = var->save_result.ulonglong_value;
  if (val > global_system_variables.parser_max_mem_size) {
    if (thd->security_context()->check_access(SUPER_ACL)) return false;
    var->save_result.ulonglong_value =
        global_system_variables.parser_max_mem_size;
    return throw_bounds_warning(thd, "parser_max_mem_size",
                                true,  // fixed
                                true,  // is_unsigned
                                val);
  }
  return false;
}

constexpr size_t max_mem_sz = std::numeric_limits<size_t>::max();

static Sys_var_ulonglong Sys_histogram_generation_max_mem_size(
    "histogram_generation_max_mem_size",
    "Maximum amount of memory available for generating histograms",
    SESSION_VAR(histogram_generation_max_mem_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1000000, max_mem_sz), DEFAULT(20000000), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_session_admin),
    ON_UPDATE(nullptr));

/*
  Need at least 400Kb to get through bootstrap.
  Need at least 8Mb to get through mtr check testcase, which does
    SELECT * FROM INFORMATION_SCHEMA.VIEWS
*/
static Sys_var_ulonglong Sys_parser_max_mem_size(
    "parser_max_mem_size", "Maximum amount of memory available to the parser",
    SESSION_VAR(parser_max_mem_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(10 * 1000 * 1000, max_mem_sz), DEFAULT(max_mem_sz),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(limit_parser_max_mem_size), ON_UPDATE(nullptr));

/*
  There is no call on Sys_var_integer::do_check() for 'set xxx=default';
  The predefined default for parser_max_mem_size is "infinite".
  Update it in case we have seen option maximum-parser-max-mem-size
  Also update global_system_variables, so 'SELECT parser_max_mem_size'
  reports correct data.
*/
void update_parser_max_mem_size() {
  const ulonglong max_max = max_system_variables.parser_max_mem_size;
  if (max_max == max_mem_sz) return;
  // In case parser-max-mem-size is also set:
  const ulonglong new_val =
      std::min(max_max, global_system_variables.parser_max_mem_size);
  Sys_parser_max_mem_size.update_default(new_val);
  global_system_variables.parser_max_mem_size = new_val;
}

void update_optimizer_switch() {
#ifndef WITH_HYPERGRAPH_OPTIMIZER
  global_system_variables.optimizer_switch &=
      ~OPTIMIZER_SWITCH_HYPERGRAPH_OPTIMIZER;
#endif
}

static bool check_optimizer_switch(sys_var *, THD *thd [[maybe_unused]],
                                   set_var *var) {
  const bool current_hypergraph_optimizer =
      thd->optimizer_switch_flag(OPTIMIZER_SWITCH_HYPERGRAPH_OPTIMIZER);
  const bool want_hypergraph_optimizer =
      var->save_result.ulonglong_value & OPTIMIZER_SWITCH_HYPERGRAPH_OPTIMIZER;

  if (current_hypergraph_optimizer && !want_hypergraph_optimizer) {
    // Don't turn off the hypergraph optimizer on set optimizer_switch=DEFAULT.
    // This is so that mtr --hypergraph should not be easily cancelled in the
    // middle of a test, unless the test explicitly meant it.
    if (var->value == nullptr) {
      var->save_result.ulonglong_value |= OPTIMIZER_SWITCH_HYPERGRAPH_OPTIMIZER;
    }
  } else if (!current_hypergraph_optimizer && want_hypergraph_optimizer) {
#ifdef WITH_HYPERGRAPH_OPTIMIZER
    // Allow, with a warning.
    push_warning(thd, Sql_condition::SL_WARNING, ER_WARN_DEPRECATED_SYNTAX,
                 ER_THD(thd, ER_WARN_HYPERGRAPH_EXPERIMENTAL));
    return false;
#else
    // Disallow; the hypergraph optimizer is not ready for production yet.
    my_error(ER_HYPERGRAPH_NOT_SUPPORTED_YET, MYF(0),
             "use in non-debug builds");
    return true;
#endif
  }
  return false;
}

/**
  @note
  @b BEWARE! These must have the same order as the \#defines in sql_const.h!
*/
static const char *optimizer_switch_names[] = {
    "index_merge",
    "index_merge_union",
    "index_merge_sort_union",
    "index_merge_intersection",
    "engine_condition_pushdown",
    "index_condition_pushdown",
    "mrr",
    "mrr_cost_based",
    "block_nested_loop",
    "batched_key_access",
    "materialization",
    "semijoin",
    "loosescan",
    "firstmatch",
    "duplicateweedout",
    "subquery_materialization_cost_based",
    "use_index_extensions",
    "condition_fanout_filter",
    "derived_merge",
    "use_invisible_indexes",
    "skip_scan",
    "hash_join",
    "subquery_to_derived",
    "prefer_ordering_index",
    "hypergraph_optimizer",  // Deliberately not documented below.
    "derived_condition_pushdown",
    "default",
    NullS};
static Sys_var_flagset Sys_optimizer_switch(
    "optimizer_switch",
    "optimizer_switch=option=val[,option=val...], where option is one of "
    "{index_merge, index_merge_union, index_merge_sort_union, "
    "index_merge_intersection, engine_condition_pushdown, "
    "index_condition_pushdown, mrr, mrr_cost_based"
    ", materialization, semijoin, loosescan, firstmatch, duplicateweedout,"
    " subquery_materialization_cost_based, skip_scan,"
    " block_nested_loop, batched_key_access, use_index_extensions,"
    " condition_fanout_filter, derived_merge, hash_join,"
    " subquery_to_derived, prefer_ordering_index,"
    " derived_condition_pushdown} and val is one of "
    "{on, off, default}",
    HINT_UPDATEABLE SESSION_VAR(optimizer_switch), CMD_LINE(REQUIRED_ARG),
    optimizer_switch_names, DEFAULT(OPTIMIZER_SWITCH_DEFAULT), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_optimizer_switch), ON_UPDATE(nullptr));

static PolyLock_mutex PLock_global_conn_mem_limit(&LOCK_global_conn_mem_limit);

static Sys_var_ulonglong Sys_global_connection_memory_limit(
    "global_connection_memory_limit",
    "Maximum amount of memory all connections can consume",
    GLOBAL_VAR(global_conn_mem_limit), CMD_LINE(REQUIRED_ARG),
#ifndef NDEBUG
    VALID_RANGE(1, max_mem_sz), DEFAULT(max_mem_sz),
#else
    VALID_RANGE(1024 * 1024 * 16, max_mem_sz), DEFAULT(max_mem_sz),
#endif
    BLOCK_SIZE(1), &PLock_global_conn_mem_limit, NOT_IN_BINLOG,
    ON_CHECK(nullptr), ON_UPDATE(nullptr));

static Sys_var_ulonglong Sys_connection_memory_limit(
    "connection_memory_limit",
    "Maximum amount of memory connection can consume",
    SESSION_VAR(conn_mem_limit), CMD_LINE(REQUIRED_ARG),
#ifndef NDEBUG
    VALID_RANGE(1, max_mem_sz), DEFAULT(max_mem_sz),
#else
    VALID_RANGE(1024 * 1024 * 2, max_mem_sz), DEFAULT(max_mem_sz),
#endif
    BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_session_admin),
    ON_UPDATE(nullptr));

static Sys_var_ulong Sys_connection_memory_chunk_size(
    "connection_memory_chunk_size",
    "Chunk size regulating frequency of updating the global memory counter",
    SESSION_VAR(conn_mem_chunk_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, 1024 * 1024 * 512), DEFAULT(8192), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_session_admin),
    ON_UPDATE(nullptr));

static Sys_var_bool Sys_connection_global_memory_tracking(
    "global_connection_memory_tracking",
    "Enable updating the global memory counter and checking "
    "the global connection memory limit exceeding",
    SESSION_VAR(conn_global_mem_tracking), CMD_LINE(OPT_ARG), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_session_admin),
    ON_UPDATE(nullptr));

static Sys_var_bool Sys_var_end_markers_in_json(
    "end_markers_in_json",
    "In JSON output (\"EXPLAIN FORMAT=JSON\" and optimizer trace), "
    "if variable is set to 1, repeats the structure's key (if it has one) "
    "near the closing bracket",
    HINT_UPDATEABLE SESSION_VAR(end_markers_in_json), CMD_LINE(OPT_ARG),
    DEFAULT(false));

static Sys_var_flagset Sys_optimizer_trace(
    "optimizer_trace",
    "Controls tracing of the Optimizer:"
    " optimizer_trace=option=val[,option=val...], where option is one of"
    " {enabled, one_line}"
    " and val is one of {on, default}",
    SESSION_VAR(optimizer_trace), CMD_LINE(REQUIRED_ARG),
    Opt_trace_context::flag_names, DEFAULT(Opt_trace_context::FLAG_DEFAULT));
// @see set_var::is_var_optimizer_trace()
export sys_var *Sys_optimizer_trace_ptr = &Sys_optimizer_trace;

/**
  Note how "misc" is not here: it is not accessible to the user; disabling
  "misc" would disable the top object, which would make an empty trace.
*/
static Sys_var_flagset Sys_optimizer_trace_features(
    "optimizer_trace_features",
    "Enables/disables tracing of selected features of the Optimizer:"
    " optimizer_trace_features=option=val[,option=val...], where option is "
    "one "
    "of"
    " {greedy_search, range_optimizer, dynamic_range, repeated_subselect}"
    " and val is one of {on, off, default}",
    SESSION_VAR(optimizer_trace_features), CMD_LINE(REQUIRED_ARG),
    Opt_trace_context::feature_names,
    DEFAULT(Opt_trace_context::default_features));

/** Delete all old optimizer traces */
static bool optimizer_trace_update(sys_var *, THD *thd, enum_var_type) {
  thd->opt_trace.reset();
  return false;
}

static Sys_var_long Sys_optimizer_trace_offset(
    "optimizer_trace_offset",
    "Offset of first optimizer trace to show; see manual",
    SESSION_VAR(optimizer_trace_offset), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(LONG_MIN, LONG_MAX), DEFAULT(-1), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(optimizer_trace_update));

static Sys_var_long Sys_optimizer_trace_limit(
    "optimizer_trace_limit", "Maximum number of shown optimizer traces",
    SESSION_VAR(optimizer_trace_limit), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, LONG_MAX), DEFAULT(1), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(optimizer_trace_update));

static Sys_var_ulong Sys_optimizer_trace_max_mem_size(
    "optimizer_trace_max_mem_size",
    "Maximum allowed cumulated size of stored optimizer traces",
    SESSION_VAR(optimizer_trace_max_mem_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, ULONG_MAX), DEFAULT(1024 * 1024), BLOCK_SIZE(1));

static Sys_var_charptr Sys_pid_file(
    "pid_file", "Pid file used by safe_mysqld",
    READ_ONLY NON_PERSIST GLOBAL_VAR(pidfile_name_ptr), CMD_LINE(REQUIRED_ARG),
    IN_FS_CHARSET, DEFAULT(pidfile_name));

static Sys_var_charptr Sys_plugin_dir(
    "plugin_dir", "Directory for plugins",
    READ_ONLY NON_PERSIST GLOBAL_VAR(opt_plugin_dir_ptr),
    CMD_LINE(REQUIRED_ARG), IN_FS_CHARSET, DEFAULT(nullptr));

static Sys_var_uint Sys_port(
    "port",
    "Port number to use for connection or 0 to default to, "
    "my.cnf, $MYSQL_TCP_PORT, "
#if MYSQL_PORT_DEFAULT == 0
    "/etc/services, "
#endif
    "built-in default (" STRINGIFY_ARG(MYSQL_PORT) "), whatever comes first",
    READ_ONLY NON_PERSIST GLOBAL_VAR(mysqld_port), CMD_LINE(REQUIRED_ARG, 'P'),
    VALID_RANGE(0, 65535), DEFAULT(0), BLOCK_SIZE(1));

static Sys_var_ulong Sys_preload_buff_size(
    "preload_buffer_size",
    "The size of the buffer that is allocated when preloading indexes",
    SESSION_VAR(preload_buff_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1024, 1024 * 1024 * 1024), DEFAULT(32768), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_session_admin_no_super));

static Sys_var_uint Sys_protocol_version(
    "protocol_version",
    "The version of the client/server protocol used by the MySQL server",
    READ_ONLY NON_PERSIST GLOBAL_VAR(protocol_version), NO_CMD_LINE,
    VALID_RANGE(0, ~0), DEFAULT(PROTOCOL_VERSION), BLOCK_SIZE(1));

static Sys_var_proxy_user Sys_proxy_user(
    "proxy_user", "The proxy user account name used when logging in",
    IN_SYSTEM_CHARSET);

static Sys_var_external_user Sys_external_user(
    "external_user", "The external user account used when logging in",
    IN_SYSTEM_CHARSET);

static Sys_var_ulong Sys_read_buff_size(
    "read_buffer_size",
    "Each thread that does a sequential scan allocates a buffer of "
    "this size for each table it scans. If you do many sequential scans, "
    "you may want to increase this value",
    HINT_UPDATEABLE SESSION_VAR(read_buff_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(IO_SIZE * 2, INT_MAX32), DEFAULT(128 * 1024),
    BLOCK_SIZE(IO_SIZE));

static bool check_read_only(sys_var *, THD *thd, set_var *) {
  /* Prevent self dead-lock */
  if (thd->locked_tables_mode || thd->in_active_multi_stmt_transaction()) {
    my_error(ER_LOCK_OR_ACTIVE_TRANSACTION, MYF(0));
    return true;
  }
  return false;
}

static bool check_require_secure_transport(sys_var *, THD *,
                                           set_var *var [[maybe_unused]]) {
#if !defined(_WIN32)
  /*
    always allow require_secure_transport to be enabled on
    Linux, as socket is secure.
  */
  return false;
#else
  /*
    check whether SSL or shared memory transports are enabled before
    turning require_secure_transport ON, otherwise no connections will
    be allowed on Windows.
  */

  if (!var->save_result.ulonglong_value) return false;
  if (have_ssl() || opt_enable_shared_memory) return false;
  /* reject if SSL and shared memory are both disabled: */
  my_error(ER_NO_SECURE_TRANSPORTS_CONFIGURED, MYF(0));
  return true;

#endif
}

static void event_scheduler_restart(THD *thd) {
  /*
    Restart event scheduler if needed.

    At present, turning on SUPER_READ_ONLY means that we
    can no longer acquire an MDL to update mysql.*.
    As a result of this, updating the "last run at ..."
    timestamp of events fails, and the event scheduler
    shuts down when trying to do so.

    As a convenience, we restart the event scheduler when
    [SUPER_]READ_ONLY is turned off while the scheduler is
    enabled (in the settings), but not actually running.
  */
  if (Events::opt_event_scheduler == Events::EVENTS_ON) {
    bool evsched_error;       // Did we fail to start the event scheduler?
    int evsched_errcode = 0;  // If we failed, what was the actual error code?

    /*
      We must not hold the lock while starting the event scheduler,
      as that will internally try to take the lock while creating a THD.
    */
    mysql_mutex_unlock(&LOCK_global_system_variables);
    evsched_error = Events::start(&evsched_errcode);
    mysql_mutex_lock(&LOCK_global_system_variables);

    if (evsched_error) {
      /*
        The user requested a change of super_read_only.
        That change succeeded, so we do not signal a failure here,
        since it is only the side-effect/convenience of restarting
        the event scheduler that failed.
        We do however notify them of that failure, since we're
        just that nice.
        We also do not modify opt_event_scheduler, since user
        intent has not changed. If this policy ever changes,
        opt_event_scheduler should probably be unset when the
        event scheduler shuts down.
      */
      push_warning_printf(thd, Sql_condition::SL_WARNING,
                          ER_EVENT_SET_VAR_ERROR,
                          ER_THD(thd, ER_EVENT_SET_VAR_ERROR), evsched_errcode);
    }
  }
}

static bool fix_read_only(sys_var *self, THD *thd, enum_var_type) {
  bool result = true;
  bool new_read_only = read_only;  // make a copy before releasing a mutex
  DBUG_TRACE;

  /*
    If we're not newly turning on READ_ONLY, we don't have to worry
    about locks.
  */
  if (read_only == false || read_only == opt_readonly) {
    opt_readonly = read_only;

    /*
      If we're turning off READ_ONLY here, turn off
      SUPER_READ_ONLY as well (if on).
    */
    if (opt_super_readonly && !read_only) {
      opt_super_readonly = false;
      super_read_only = false;

      // Do this last as it temporarily releases the global sys-var lock.
      event_scheduler_restart(thd);
    }
    return false;
  }

  /*
    Check whether we can change read_only state without causing a deadlock.

    Not to be confused with check_readonly(), which checks in a
    standardized way whether the current settings of opt_readonly
    and opt_super_readonly prohibit certain operations.
  */
  if (check_read_only(self, thd, nullptr))  // just in case
    goto end;

  if (thd->global_read_lock.is_acquired()) {
    /*
      This connection already holds the global read lock.
      This can be the case with:
      - FLUSH TABLES WITH READ LOCK
      - SET GLOBAL READ_ONLY = 1
    */
    opt_readonly = read_only;

    if (opt_super_readonly && !read_only) {
      opt_super_readonly = false;
      super_read_only = false;

      // Do this last as it temporarily releases the global sys-var lock.
      event_scheduler_restart(thd);
    }
    return false;
  }

  /*
    READ_ONLY=1 prevents write locks from being taken on tables and
    blocks transactions from committing. We therefore should make sure
    that no such events occur while setting the read_only variable.
    This is a 2 step process:
    [1] lock_global_read_lock()
      Prevents connections from obtaining new write locks on
      tables. Note that we can still have active rw transactions.
    [2] make_global_read_lock_block_commit()
      Prevents transactions from committing.
  */

  read_only = opt_readonly;
  mysql_mutex_unlock(&LOCK_global_system_variables);

  if (thd->global_read_lock.lock_global_read_lock(thd))
    goto end_with_mutex_unlock;

  if ((result = thd->global_read_lock.make_global_read_lock_block_commit(thd)))
    goto end_with_read_lock;

  /* Change the opt_readonly system variable, safe because the lock is held */
  opt_readonly = new_read_only;

  result = false;

end_with_read_lock:
  /* Release the lock */
  thd->global_read_lock.unlock_global_read_lock(thd);
end_with_mutex_unlock:
  mysql_mutex_lock(&LOCK_global_system_variables);
end:
  read_only = opt_readonly;
  return result;
}

static bool fix_super_read_only(sys_var *, THD *thd, enum_var_type type) {
  DBUG_TRACE;

  /* return if no changes: */
  if (super_read_only == opt_super_readonly) return false;

  /* return immediately if turning super_read_only OFF: */
  if (super_read_only == false) {
    opt_super_readonly = false;

    // Do this last as it temporarily releases the global sys-var lock.
    event_scheduler_restart(thd);

    return false;
  }
  bool result = true;
  bool new_super_read_only =
      super_read_only; /* make a copy before releasing a mutex */

  /* set read_only to ON if it is OFF, letting fix_read_only()
     handle its own locking needs
  */
  if (!opt_readonly) {
    read_only = true;
    if ((result = fix_read_only(nullptr, thd, type))) goto end;
  }

  /* if we already have global read lock, set super_read_only
     and return immediately:
  */
  if (thd->global_read_lock.is_acquired()) {
    opt_super_readonly = super_read_only;
    return false;
  }

  /* now we're turning ON super_read_only: */
  super_read_only = opt_super_readonly;
  mysql_mutex_unlock(&LOCK_global_system_variables);

  if (thd->global_read_lock.lock_global_read_lock(thd))
    goto end_with_mutex_unlock;

  if ((result = thd->global_read_lock.make_global_read_lock_block_commit(thd)))
    goto end_with_read_lock;
  opt_super_readonly = new_super_read_only;
  result = false;

end_with_read_lock:
  /* Release the lock */
  thd->global_read_lock.unlock_global_read_lock(thd);
end_with_mutex_unlock:
  mysql_mutex_lock(&LOCK_global_system_variables);
end:
  super_read_only = opt_super_readonly;
  return result;
}

static Sys_var_bool Sys_require_secure_transport(
    "require_secure_transport",
    "When this option is enabled, connections attempted using insecure "
    "transport will be rejected.  Secure transports are SSL/TLS, "
    "Unix socket or Shared Memory (on Windows).",
    GLOBAL_VAR(opt_require_secure_transport), CMD_LINE(OPT_ARG), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_require_secure_transport),
    ON_UPDATE(nullptr));

/**
  The read_only boolean is always equal to the opt_readonly boolean except
  during fix_read_only(); when that function is entered, opt_readonly is
  the pre-update value and read_only is the post-update value.
  fix_read_only() compares them and runs needed operations for the
  transition (especially when transitioning from false to true) and
  synchronizes both booleans in the end.
*/
static Sys_var_bool Sys_readonly(
    "read_only",
    "Make all non-temporary tables read-only, with the exception for "
    "replication applier threads and users with the SUPER privilege.",
    GLOBAL_VAR(read_only), CMD_LINE(OPT_ARG), DEFAULT(false), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_read_only), ON_UPDATE(fix_read_only));

/**
Setting super_read_only to ON triggers read_only to also be set to ON.
*/
static Sys_var_bool Sys_super_readonly(
    "super_read_only",
    "Make all non-temporary tables read-only, with the exception for "
    "replication applier threads.  Users with the SUPER privilege are "
    "affected, unlike read_only.  Setting super_read_only to ON "
    "also sets read_only to ON.",
    GLOBAL_VAR(super_read_only), CMD_LINE(OPT_ARG), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_super_read_only));

// Small lower limit to be able to test MRR
static Sys_var_ulong Sys_read_rnd_buff_size(
    "read_rnd_buffer_size",
    "When reading rows in sorted order after a sort, the rows are read "
    "through this buffer to avoid a disk seeks",
    HINT_UPDATEABLE SESSION_VAR(read_rnd_buff_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, INT_MAX32), DEFAULT(256 * 1024), BLOCK_SIZE(1));

static Sys_var_ulong Sys_div_precincrement(
    "div_precision_increment",
    "Precision of the result of '/' "
    "operator will be increased on that value",
    HINT_UPDATEABLE SESSION_VAR(div_precincrement), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, DECIMAL_MAX_SCALE), DEFAULT(4), BLOCK_SIZE(1));

static Sys_var_uint Sys_eq_range_index_dive_limit(
    "eq_range_index_dive_limit",
    "The optimizer will use existing index statistics instead of "
    "doing index dives for equality ranges if the number of equality "
    "ranges for the index is larger than or equal to this number. "
    "If set to 0, index dives are always used.",
    HINT_UPDATEABLE SESSION_VAR(eq_range_index_dive_limit),
    CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, UINT_MAX32), DEFAULT(200),
    BLOCK_SIZE(1));

static Sys_var_ulong Sys_range_alloc_block_size(
    "range_alloc_block_size",
    "Allocation block size for storing ranges during optimization",
    HINT_UPDATEABLE SESSION_VAR(range_alloc_block_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(RANGE_ALLOC_BLOCK_SIZE, UINT32_MAX),
    DEFAULT(RANGE_ALLOC_BLOCK_SIZE), BLOCK_SIZE(1024));

static bool fix_thd_mem_root(sys_var *self, THD *thd, enum_var_type type) {
  if (!self->is_global_persist(type))
    thd->mem_root->set_block_size(thd->variables.query_alloc_block_size);
  return false;
}
static Sys_var_ulong Sys_query_alloc_block_size(
    "query_alloc_block_size",
    "Allocation block size for query parsing and execution",
    SESSION_VAR(query_alloc_block_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1024, UINT_MAX32), DEFAULT(QUERY_ALLOC_BLOCK_SIZE),
    BLOCK_SIZE(1024), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_thd_mem_root));

static Sys_var_ulong Sys_query_prealloc_size(
    "query_prealloc_size", "Persistent buffer for query parsing and execution",
    SESSION_VAR(query_prealloc_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(QUERY_ALLOC_PREALLOC_SIZE, ULONG_MAX),
    DEFAULT(QUERY_ALLOC_PREALLOC_SIZE), BLOCK_SIZE(1024), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr), DEPRECATED_VAR(""));

#if defined(_WIN32)
static Sys_var_bool Sys_shared_memory(
    "shared_memory", "Enable the shared memory",
    READ_ONLY NON_PERSIST GLOBAL_VAR(opt_enable_shared_memory),
    CMD_LINE(OPT_ARG), DEFAULT(false));

static Sys_var_charptr Sys_shared_memory_base_name(
    "shared_memory_base_name", "Base name of shared memory",
    READ_ONLY NON_PERSIST GLOBAL_VAR(shared_memory_base_name),
    CMD_LINE(REQUIRED_ARG), IN_FS_CHARSET, DEFAULT(0));
#endif

// this has to be NO_CMD_LINE as the command-line option has a different name
static Sys_var_bool Sys_skip_external_locking(
    "skip_external_locking", "Don't use system (external) locking",
    READ_ONLY NON_PERSIST GLOBAL_VAR(my_disable_locking), NO_CMD_LINE,
    DEFAULT(true));

static Sys_var_bool Sys_skip_networking(
    "skip_networking", "Don't allow connection with TCP/IP",
    READ_ONLY NON_PERSIST GLOBAL_VAR(opt_disable_networking), CMD_LINE(OPT_ARG),
    DEFAULT(false));

static Sys_var_bool Sys_skip_name_resolve(
    "skip_name_resolve",
    "Don't resolve hostnames. All hostnames are IP's or 'localhost'.",
    READ_ONLY GLOBAL_VAR(opt_skip_name_resolve),
    CMD_LINE(OPT_ARG, OPT_SKIP_RESOLVE), DEFAULT(false));

static Sys_var_bool Sys_skip_show_database(
    "skip_show_database", "Don't allow 'SHOW DATABASE' commands",
    READ_ONLY GLOBAL_VAR(opt_skip_show_db), CMD_LINE(OPT_ARG), DEFAULT(false));

static Sys_var_charptr Sys_socket(
    "socket", "Socket file to use for connection",
    READ_ONLY NON_PERSIST GLOBAL_VAR(mysqld_unix_port), CMD_LINE(REQUIRED_ARG),
    IN_FS_CHARSET, DEFAULT(nullptr));

static Sys_var_ulong Sys_thread_stack(
    "thread_stack", "The stack size for each thread",
    READ_ONLY GLOBAL_VAR(my_thread_stack_size), CMD_LINE(REQUIRED_ARG),
#if defined(__clang__) && defined(HAVE_UBSAN)
    // Clang with DEBUG needs more stack, esp. with UBSAN.
    VALID_RANGE(DEFAULT_THREAD_STACK, ULONG_MAX),
#else
    VALID_RANGE(128 * 1024, ULONG_MAX),
#endif
    DEFAULT(DEFAULT_THREAD_STACK), BLOCK_SIZE(1024));

static Sys_var_charptr Sys_tmpdir(
    "tmpdir",
    "Path for temporary files. Several paths may "
    "be specified, separated by a "
#if defined(_WIN32)
    "semicolon (;)"
#else
    "colon (:)"
#endif
    ", in this case they are used in a round-robin fashion",
    READ_ONLY NON_PERSIST GLOBAL_VAR(opt_mysql_tmpdir),
    CMD_LINE(REQUIRED_ARG, 't'), IN_FS_CHARSET, DEFAULT(nullptr));

static bool fix_trans_mem_root(sys_var *self, THD *thd, enum_var_type type) {
  if (!self->is_global_persist(type))
    thd->get_transaction()->init_mem_root_defaults(
        thd->variables.trans_alloc_block_size,
        thd->variables.trans_prealloc_size);
  return false;
}
static Sys_var_ulong Sys_trans_alloc_block_size(
    "transaction_alloc_block_size",
    "Allocation block size for transactions to be stored in binary log",
    SESSION_VAR(trans_alloc_block_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1024, 128 * 1024), DEFAULT(QUERY_ALLOC_BLOCK_SIZE),
    BLOCK_SIZE(1024), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_trans_mem_root));

static Sys_var_ulong Sys_trans_prealloc_size(
    "transaction_prealloc_size",
    "Persistent buffer for transactions to be stored in binary log",
    SESSION_VAR(trans_prealloc_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1024, 128 * 1024), DEFAULT(TRANS_ALLOC_PREALLOC_SIZE),
    BLOCK_SIZE(1024), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(nullptr), DEPRECATED_VAR(""));

static const char *thread_handling_names[] = {
    "one-thread-per-connection", "no-threads", "loaded-dynamically", nullptr};
static Sys_var_enum Sys_thread_handling(
    "thread_handling",
    "Define threads usage for handling queries, one of "
    "one-thread-per-connection, no-threads, loaded-dynamically",
    READ_ONLY GLOBAL_VAR(Connection_handler_manager::thread_handling),
    CMD_LINE(REQUIRED_ARG), thread_handling_names, DEFAULT(0));

static Sys_var_charptr Sys_secure_file_priv(
    "secure_file_priv",
    "Limit LOAD DATA, SELECT ... OUTFILE, and LOAD_FILE() to files "
    "within specified directory",
    READ_ONLY NON_PERSIST GLOBAL_VAR(opt_secure_file_priv),
    CMD_LINE(REQUIRED_ARG), IN_FS_CHARSET,
    DEFAULT(DEFAULT_SECURE_FILE_PRIV_DIR));

static bool fix_server_id(sys_var *, THD *thd, enum_var_type) {
  // server_id is 'MYSQL_PLUGIN_IMPORT ulong'
  // So we cast here, rather than change its type.
  server_id_supplied = true;
  thd->server_id = static_cast<uint32>(server_id);
  return false;
}
static Sys_var_ulong Sys_server_id(
    "server_id",
    "Uniquely identifies the server instance in the community of "
    "replication partners",
    PERSIST_AS_READONLY GLOBAL_VAR(server_id),
    CMD_LINE(REQUIRED_ARG, OPT_SERVER_ID), VALID_RANGE(0, UINT_MAX32),
    DEFAULT(1), BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_server_id));

static Sys_var_charptr Sys_server_uuid(
    "server_uuid", "Uniquely identifies the server instance in the universe",
    READ_ONLY NON_PERSIST GLOBAL_VAR(server_uuid_ptr), NO_CMD_LINE,
    IN_FS_CHARSET, DEFAULT(server_uuid));

#if defined(HAVE_BUILD_ID_SUPPORT)
static Sys_var_charptr Sys_server_build_id(
    "build_id",
    "A unique Build ID generated by the linker, a 160 bit sha1 signature.",
    READ_ONLY NON_PERSIST GLOBAL_VAR(server_build_id_ptr), NO_CMD_LINE,
    IN_FS_CHARSET, DEFAULT(server_build_id));
#endif

static Sys_var_uint Sys_server_id_bits(
    "server_id_bits", "Set number of significant bits in server-id",
    GLOBAL_VAR(opt_server_id_bits), CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 32),
    DEFAULT(32), BLOCK_SIZE(1));

static Sys_var_int32 Sys_regexp_time_limit(
    "regexp_time_limit",
    "Timeout for regular expressions matches, in steps of the match "
    "engine, typically on the order of milliseconds.",
    GLOBAL_VAR(opt_regexp_time_limit), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, INT32_MAX), DEFAULT(32), BLOCK_SIZE(1));

static Sys_var_int32 Sys_regexp_stack_limit(
    "regexp_stack_limit", "Stack size limit for regular expressions matches",
    GLOBAL_VAR(opt_regexp_stack_limit), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, INT32_MAX), DEFAULT(8000000), BLOCK_SIZE(1));

static Sys_var_bool Sys_replica_compressed_protocol(
    "replica_compressed_protocol",
    "Use compression in the source/replica protocol.",
    GLOBAL_VAR(opt_replica_compressed_protocol), CMD_LINE(OPT_ARG),
    DEFAULT(false));

static Sys_var_deprecated_alias Sys_slave_compressed_protocol(
    "slave_compressed_protocol", Sys_replica_compressed_protocol);

static const char *replica_exec_mode_names[] = {"STRICT", "IDEMPOTENT",
                                                nullptr};
static Sys_var_enum Sys_replica_exec_mode(
    "replica_exec_mode",
    "Modes for how replication events should be executed. Legal values "
    "are STRICT (default) and IDEMPOTENT. In IDEMPOTENT mode, "
    "replication will ignore duplicate key errors and key not found errors. "
    "In STRICT mode, replication will stop at those errors.",
    GLOBAL_VAR(replica_exec_mode_options), CMD_LINE(REQUIRED_ARG),
    replica_exec_mode_names, DEFAULT(RBR_EXEC_MODE_STRICT));

static Sys_var_deprecated_alias Sys_slave_exec_mode("slave_exec_mode",
                                                    Sys_replica_exec_mode);

const char *replica_type_conversions_name[] = {
    "ALL_LOSSY", "ALL_NON_LOSSY", "ALL_UNSIGNED", "ALL_SIGNED", nullptr};
static Sys_var_set Sys_replica_type_conversions(
    "replica_type_conversions",
    "Set of type conversions that may be used by the replication applier "
    "thread for row events. Allowed values are:"
    " ALL_LOSSY to enable lossy conversions,"
    " ALL_NON_LOSSY to enable non-lossy conversions,"
    " ALL_UNSIGNED to treat all integer column type data to be unsigned "
    "values, and"
    " ALL_SIGNED to treat all integer column type data to be signed values."
    " Default treatment is ALL_SIGNED. If ALL_SIGNED and ALL_UNSIGNED both "
    "are"
    " specified, ALL_SIGNED will take higher priority than ALL_UNSIGNED."
    " If the variable is assigned the empty set, no conversions are"
    " allowed and it is expected that the types match exactly.",
    GLOBAL_VAR(replica_type_conversions_options), CMD_LINE(REQUIRED_ARG),
    replica_type_conversions_name, DEFAULT(0));

static Sys_var_deprecated_alias Sys_slave_type_conversions(
    "slave_type_conversions", Sys_replica_type_conversions);

static Sys_var_bool Sys_replica_sql_verify_checksum(
    "replica_sql_verify_checksum",
    "Force checksum verification of replication events after reading them "
    "from relay log. Note: The replica always verifies checksums for events "
    "received from the network, if the event has a checksum at all, before "
    "it writes the event to the relay log. Enabled by default.",
    GLOBAL_VAR(opt_replica_sql_verify_checksum), CMD_LINE(OPT_ARG),
    DEFAULT(true));

static Sys_var_deprecated_alias Sys_slave_sql_verify_checksum(
    "slave_sql_verify_checksum", Sys_replica_sql_verify_checksum);

static bool check_not_null_not_empty(sys_var *self, THD *thd, set_var *var) {
  String str, *res;
  /* null value is not allowed */
  if (check_not_null(self, thd, var)) return true;

  /** empty value ('') is not allowed */
  res = var->value ? var->value->val_str(&str) : nullptr;
  if (res && res->is_empty()) return true;

  return false;
}

static bool check_slave_stopped(sys_var *self, THD *thd, set_var *var) {
  bool result = false;
  Master_info *mi = nullptr;

  if (check_not_null_not_empty(self, thd, var)) return true;

  channel_map.wrlock();

  for (mi_map::iterator it = channel_map.begin(); it != channel_map.end();
       it++) {
    mi = it->second;
    if (mi) {
      mysql_mutex_lock(&mi->rli->run_lock);
      if (mi->rli->slave_running) {
        my_error(ER_REPLICA_SQL_THREAD_MUST_STOP, MYF(0));
        result = true;
      }
      mysql_mutex_unlock(&mi->rli->run_lock);
    }
  }
  channel_map.unlock();
  return result;
}

static const char *slave_rows_search_algorithms_names[] = {
    "TABLE_SCAN", "INDEX_SCAN", "HASH_SCAN", nullptr};
static Sys_var_set Slave_rows_search_algorithms(
    "slave_rows_search_algorithms",
    "The set of algorithms used by the replication applier while searching the "
    "table for rows to update or delete. Possible values are: INDEX_SCAN, "
    "TABLE_SCAN and HASH_SCAN. Any combination is allowed, and the applier "
    "picks the most efficient among them for any given scenario. "
    "(Default: INDEX_SCAN, HASH_SCAN).",
    GLOBAL_VAR(slave_rows_search_algorithms_options),
    CMD_LINE(REQUIRED_ARG, OPT_SLAVE_ROWS_SEARCH_ALGORITHMS),
    slave_rows_search_algorithms_names,
    DEFAULT(SLAVE_ROWS_INDEX_SCAN | SLAVE_ROWS_HASH_SCAN), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_not_null_not_empty), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static const char *mts_parallel_type_names[] = {"DATABASE", "LOGICAL_CLOCK",
                                                nullptr};
static Sys_var_enum Sys_replica_parallel_type(
    "replica_parallel_type",
    "The method used by the replication applier to parallelize "
    "transactions. DATABASE, indicates that it "
    "may apply transactions in parallel in case they update different "
    "databases. LOGICAL_CLOCK, which is the default, indicates that it decides "
    "whether two "
    "transactions can be applied in parallel using the logical timestamps "
    "computed by the source, according to "
    "binlog_transaction_dependency_tracking.",
    PERSIST_AS_READONLY GLOBAL_VAR(mts_parallel_option),
    CMD_LINE(REQUIRED_ARG, OPT_REPLICA_PARALLEL_TYPE), mts_parallel_type_names,
    DEFAULT(MTS_PARALLEL_TYPE_LOGICAL_CLOCK), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_slave_stopped), ON_UPDATE(nullptr), DEPRECATED_VAR(""));

static Sys_var_deprecated_alias Sys_slave_parallel_type(
    "slave_parallel_type", Sys_replica_parallel_type);

static bool check_binlog_transaction_dependency_tracking(sys_var *, THD *,
                                                         set_var *var) {
  if (global_system_variables.transaction_write_set_extraction ==
          HASH_ALGORITHM_OFF &&
      var->save_result.ulonglong_value != DEPENDENCY_TRACKING_COMMIT_ORDER) {
    my_error(ER_WRONG_USAGE, MYF(0),
             "binlog_transaction_dependency_tracking (!= COMMIT_ORDER)",
             "transaction_write_set_extraction (= OFF)");

    return true;
  }
  return false;
}

static bool update_binlog_transaction_dependency_tracking(sys_var *, THD *,
                                                          enum_var_type) {
  /*
    the writeset_history_start needs to be set to 0 whenever there is a
    change in the transaction dependency source so that WS and COMMIT
    transition smoothly.
  */
  mysql_bin_log.m_dependency_tracker.tracking_mode_changed();
  return false;
}

static PolyLock_mutex PLock_slave_trans_dep_tracker(
    &LOCK_replica_trans_dep_tracker);
static const char *opt_binlog_transaction_dependency_tracking_names[] = {
    "COMMIT_ORDER", "WRITESET", "WRITESET_SESSION", NullS};
static Sys_var_enum Binlog_transaction_dependency_tracking(
    "binlog_transaction_dependency_tracking",
    "Selects the source of dependency information from which to "
    "compute logical timestamps, which replicas can use to decide which "
    "transactions can be executed in parallel when using "
    "replica_parallel_type=LOGICAL_CLOCK. "
    "Possible values are COMMIT_ORDER, WRITESET and WRITESET_SESSION.",
    GLOBAL_VAR(mysql_bin_log.m_dependency_tracker.m_opt_tracking_mode),
    CMD_LINE(REQUIRED_ARG, OPT_BINLOG_TRANSACTION_DEPENDENCY_TRACKING),
    opt_binlog_transaction_dependency_tracking_names,
    DEFAULT(DEPENDENCY_TRACKING_COMMIT_ORDER), &PLock_slave_trans_dep_tracker,
    NOT_IN_BINLOG, ON_CHECK(check_binlog_transaction_dependency_tracking),
    ON_UPDATE(update_binlog_transaction_dependency_tracking),
    DEPRECATED_VAR(""));
static Sys_var_ulong Binlog_transaction_dependency_history_size(
    "binlog_transaction_dependency_history_size",
    "Maximum number of rows to keep in the writeset history.",
    GLOBAL_VAR(mysql_bin_log.m_dependency_tracker.get_writeset()
                   ->m_opt_max_history_size),
    CMD_LINE(REQUIRED_ARG, 0), VALID_RANGE(1, 1000000), DEFAULT(25000),
    BLOCK_SIZE(1), &PLock_slave_trans_dep_tracker, NOT_IN_BINLOG,
    ON_CHECK(nullptr), ON_UPDATE(nullptr));

static Sys_var_bool Sys_replica_preserve_commit_order(
    "replica_preserve_commit_order",
    "Force replication worker threads to commit in the same order as on the "
    "source. Enabled by default",
    PERSIST_AS_READONLY GLOBAL_VAR(opt_replica_preserve_commit_order),
    CMD_LINE(OPT_ARG, OPT_REPLICA_PRESERVE_COMMIT_ORDER), DEFAULT(true),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_slave_stopped),
    ON_UPDATE(nullptr));

static Sys_var_deprecated_alias Sys_slave_preserve_commit_order(
    "slave_preserve_commit_order", Sys_replica_preserve_commit_order);

bool Sys_var_charptr::global_update(THD *, set_var *var) {
  char *new_val, *ptr = var->save_result.string_value.str;
  size_t len = var->save_result.string_value.length;
  if (ptr) {
    new_val = (char *)my_memdup(key_memory_Sys_var_charptr_value, ptr, len + 1,
                                MYF(MY_WME));
    if (!new_val) return true;
    new_val[len] = 0;
  } else
    new_val = nullptr;
  if (flags & ALLOCATED) my_free(global_var(char *));
  flags |= ALLOCATED;
  global_var(char *) = new_val;
  return false;
}

bool Sys_var_enum_binlog_checksum::global_update(THD *thd, set_var *var) {
  bool check_purge = false;

  /*
    SET binlog_checksome command should ignore 'read-only' and
    'super_read_only' options so that it can update 'mysql.gtid_executed'
    replication repository table.
  */
  thd->set_skip_readonly_check();
  mysql_mutex_lock(mysql_bin_log.get_log_lock());
  if (mysql_bin_log.is_open()) {
    bool alg_changed =
        (binlog_checksum_options != (uint)var->save_result.ulonglong_value);
    if (alg_changed)
      mysql_bin_log.checksum_alg_reset =
          (uint8)var->save_result.ulonglong_value;
    mysql_bin_log.rotate(true, &check_purge);
    if (alg_changed)
      mysql_bin_log.checksum_alg_reset =
          binary_log::BINLOG_CHECKSUM_ALG_UNDEF;  // done
  } else {
    binlog_checksum_options =
        static_cast<ulong>(var->save_result.ulonglong_value);
  }
  assert(binlog_checksum_options == var->save_result.ulonglong_value);
  assert(mysql_bin_log.checksum_alg_reset ==
         binary_log::BINLOG_CHECKSUM_ALG_UNDEF);
  mysql_mutex_unlock(mysql_bin_log.get_log_lock());

  if (check_purge) mysql_bin_log.auto_purge();

  return false;
}

bool Sys_var_gtid_next::session_update(THD *thd, set_var *var) {
  DBUG_TRACE;
  char buf[Gtid::MAX_TEXT_LENGTH + 1];
  // Get the value
  String str(buf, sizeof(buf), &my_charset_latin1);
  char *res = nullptr;
  if (!var->value) {
    // set session gtid_next= default
    assert(var->save_result.string_value.str);
    assert(var->save_result.string_value.length);
    res = var->save_result.string_value.str;
  } else if (var->value->val_str(&str))
    res = var->value->val_str(&str)->c_ptr_safe();
  if (!res) {
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name.str, "NULL");
    return true;
  }
  global_sid_lock->rdlock();
  Gtid_specification spec;
  if (spec.parse(global_sid_map, res) != RETURN_STATUS_OK) {
    global_sid_lock->unlock();
    return true;
  }

  bool ret = set_gtid_next(thd, spec);
  // set_gtid_next releases global_sid_lock
  return ret;
}

#ifdef HAVE_GTID_NEXT_LIST
bool Sys_var_gtid_set::session_update(THD *thd, set_var *var) {
  DBUG_TRACE;
  Gtid_set_or_null *gsn = (Gtid_set_or_null *)session_var_ptr(thd);
  char *value = var->save_result.string_value.str;
  if (value == NULL)
    gsn->set_null();
  else {
    Gtid_set *gs = gsn->set_non_null(global_sid_map);
    if (gs == NULL) {
      my_error(ER_OUT_OF_RESOURCES, MYF(0));  // allocation failed
      return true;
    }
    /*
      If string begins with '+', add to the existing set, otherwise
      replace existing set.
    */
    while (isspace(*value)) value++;
    if (*value == '+')
      value++;
    else
      gs->clear();
    // Add specified set of groups to Gtid_set.
    global_sid_lock->rdlock();
    enum_return_status ret = gs->add_gtid_text(value);
    global_sid_lock->unlock();
    if (ret != RETURN_STATUS_OK) {
      gsn->set_null();
      return true;
    }
  }
  return false;
}
#endif  // HAVE_GTID_NEXT_LIST

/**
  This function shall issue a deprecation warning
  if the new gtid mode is set to GTID_MODE_ON and
  there is at least one replication channel with
  IGNORE_SERVER_IDS configured (i.e., not empty).

  The caller must have acquired a lock on the
  channel_map object before calling this function.

  The warning emitted is: ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT .

  @param thd The current session thread context.
  @param oldmode The old value of @@global.gtid_mode.
  @param newmode The new value for @@global.gtid_mode.

*/
static void issue_deprecation_warnings_gtid_mode(
    THD *thd, Gtid_mode::value_type oldmode [[maybe_unused]],
    Gtid_mode::value_type newmode) {
  channel_map.assert_some_lock();

  /*
    Check that if changing to gtid_mode=on no channel is configured
    to ignore server ids. If it is, issue a deprecation warning.
  */
  if (newmode == Gtid_mode::ON) {
    for (mi_map::iterator it = channel_map.begin(); it != channel_map.end();
         it++) {
      Master_info *mi = it->second;
      if (mi != nullptr && mi->is_ignore_server_ids_configured()) {
        push_warning_printf(
            thd, Sql_condition::SL_WARNING, ER_WARN_DEPRECATED_SYNTAX,
            ER_THD(thd, ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT),
            "CHANGE MASTER TO ... IGNORE_SERVER_IDS='...' "
            "(when @@GLOBAL.GTID_MODE = ON)");

        break;  // Only push one warning
      }
    }
  }
}

/**
  This function shall be called whenever the global scope
  of gtid_mode var is updated.

  It checks some preconditions and also emits deprecation
  warnings conditionally when changing the value.

  Deprecation warnings are emitted after error conditions
  have been checked and only if there is no error raised.
*/
bool Sys_var_gtid_mode::global_update(THD *thd, set_var *var) {
  DBUG_TRACE;
  bool ret = true;

  /*
    SET binlog_checksome command should ignore 'read-only' and
    'super_read_only' options so that it can update 'mysql.gtid_executed'
    replication repository table.
  */
  thd->set_skip_readonly_check();
  /*
    Hold lock_log so that:
    - other transactions are not flushed while gtid_mode is changed;
    - gtid_mode is not changed while some other thread is rotating
    the binlog.

    Hold channel_map lock so that:
    - gtid_mode is not changed during the execution of some
    replication command; particularly CHANGE MASTER. CHANGE MASTER
    checks if GTID_MODE is compatible with AUTO_POSITION, and
    later it actually updates the in-memory structure for
    AUTO_POSITION.  If gtid_mode was changed between these calls,
    auto_position could be set incompatible with gtid_mode.

    Hold global_sid_lock.wrlock so that:
    - other transactions cannot acquire ownership of any gtid.

    Hold Gtid_mode::lock so that all places that don't want to hold
    any of the other locks, but want to read gtid_mode, don't need
    to take the other locks.
  */

  auto new_gtid_mode =
      static_cast<Gtid_mode::value_type>(var->save_result.ulonglong_value);

  if (Gtid_mode::lock.trywrlock()) {
    my_error(ER_CANT_SET_GTID_MODE, MYF(0), Gtid_mode::to_string(new_gtid_mode),
             "there is a concurrent operation that disallows changes to "
             "@@GLOBAL.GTID_MODE");
    return ret;
  }

  channel_map.wrlock();
  mysql_mutex_lock(mysql_bin_log.get_log_lock());
  global_sid_lock->wrlock();
  int lock_count = 4;

  auto old_gtid_mode = global_gtid_mode.get();
  assert(new_gtid_mode <= Gtid_mode::ON);

  DBUG_PRINT("info", ("old_gtid_mode=%d new_gtid_mode=%d", old_gtid_mode,
                      new_gtid_mode));

  if (new_gtid_mode == old_gtid_mode) goto end;

  // Can only change one step at a time.
  /*
   Change gtid_mode value without checking for one step change during
   server startup.
  */
  if (mysqld_server_started &&
      abs((int)new_gtid_mode - (int)old_gtid_mode) > 1) {
    my_error(ER_GTID_MODE_CAN_ONLY_CHANGE_ONE_STEP_AT_A_TIME, MYF(0));
    goto err;
  }

  DBUG_PRINT("info", ("sql_replica_skip_counter=%d", sql_replica_skip_counter));
  if (new_gtid_mode == Gtid_mode::ON && sql_replica_skip_counter > 0) {
    push_warning(
        thd, Sql_condition::SL_WARNING,
        ER_SQL_REPLICA_SKIP_COUNTER_USED_WITH_GTID_MODE_ON,
        ER_THD(thd, ER_SQL_REPLICA_SKIP_COUNTER_USED_WITH_GTID_MODE_ON));
  }

  if (new_gtid_mode != Gtid_mode::ON && replicate_same_server_id &&
      opt_log_replica_updates && opt_bin_log) {
    std::stringstream ss;

    ss << "replicate_same_server_id is set together with log_replica_updates"
       << " and log_bin. Thus, any anonymous transactions"
       << " would circulate infinitely in case this server is part of a"
       << " circular replication topology";

    my_error(ER_CANT_SET_GTID_MODE, MYF(0), Gtid_mode::to_string(new_gtid_mode),
             ss.str().c_str());
    goto err;
  }

  // Cannot set OFF when some channel uses AUTO_POSITION.
  if (new_gtid_mode == Gtid_mode::OFF) {
    for (mi_map::iterator it = channel_map.begin(); it != channel_map.end();
         it++) {
      Master_info *mi = it->second;
      if (mi != nullptr && mi->is_auto_position()) {
        DBUG_PRINT("info", ("auto_position for channel '%s' is %d",
                            mi->get_channel(), mi->is_auto_position()));
        char buf[1024];
        snprintf(buf, sizeof(buf),
                 "replication channel '%.192s' is configured "
                 "in AUTO_POSITION mode. Execute "
                 "CHANGE REPLICATION SOURCE TO SOURCE_AUTO_POSITION = 0 "
                 "FOR CHANNEL '%.192s' before you set "
                 "@@GLOBAL.GTID_MODE = OFF.",
                 mi->get_channel(), mi->get_channel());
        my_error(ER_CANT_SET_GTID_MODE, MYF(0), "OFF", buf);
        goto err;
      }
    }
  }

  // Cannot set to GTID_MODE <> ON when some channel uses
  // ASSIGN_GTIDS_TO_ANONYMOUS_TRANSACTIONS = LOCAL|UUID.
  if (old_gtid_mode == Gtid_mode::ON && new_gtid_mode != Gtid_mode::ON) {
    for (auto it : channel_map) {
      Master_info *mi = it.second;
      if (mi != nullptr &&
          mi->rli->m_assign_gtids_to_anonymous_transactions_info.get_type() >
              Assign_gtids_to_anonymous_transactions_info::enum_type::
                  AGAT_OFF) {
        DBUG_PRINT(
            "info",
            ("assign_gtids_to_anonymous_transactions for channel '%s' is %d",
             mi->get_channel(),
             static_cast<int>(
                 mi->rli->m_assign_gtids_to_anonymous_transactions_info
                     .get_type())));
        char buf[1024];
        snprintf(buf, sizeof(buf),
                 "replication channel '%.192s' is configured "
                 "with ASSIGN_GTIDS_TO_ANONYMOUS_TRANSACTIONS set to LOCAL or "
                 "to a UUID. "
                 "Execute CHANGE REPLICATION SOURCE TO "
                 "ASSIGN_GTIDS_TO_ANONYMOUS_TRANSACTIONS = OFF "
                 "FOR CHANNEL '%.192s' before you set "
                 "@@GLOBAL.GTID_MODE = '%s'",
                 mi->get_channel(), mi->get_channel(),
                 Gtid_mode::to_string(new_gtid_mode));
        my_error(ER_CANT_SET_GTID_MODE, MYF(0),
                 Gtid_mode::to_string(new_gtid_mode), buf);
        goto err;
      }
    }
  }
  /*
    Cannot set OFF when source_connection_auto_failover is enabled for any
    channel.
  */
  if (new_gtid_mode != Gtid_mode::ON) {
    for (mi_map::iterator it = channel_map.begin(); it != channel_map.end();
         it++) {
      Master_info *mi = it->second;
      if (mi != nullptr && mi->is_source_connection_auto_failover()) {
        my_error(ER_DISABLE_GTID_MODE_REQUIRES_ASYNC_RECONNECT_OFF, MYF(0),
                 Gtid_mode::to_string(new_gtid_mode));
        goto err;
      }
    }
  }
  /*
    Cannot set to <> ON when gtid_only is enabled for any channel.
  */
  if (old_gtid_mode == Gtid_mode::ON && new_gtid_mode != Gtid_mode::ON) {
    for (auto it : channel_map) {
      Master_info *mi = it.second;
      if (mi != nullptr && mi->is_gtid_only_mode()) {
        char buf[1024];
        snprintf(buf, sizeof(buf),
                 "replication channel '%.192s' is configured "
                 "with GTID_ONLY = 1. "
                 "Execute CHANGE REPLICATION SOURCE TO "
                 "GTID_ONLY = 0 "
                 "FOR CHANNEL '%.192s' before you set "
                 "@@GLOBAL.GTID_MODE = '%s'",
                 mi->get_channel(), mi->get_channel(),
                 Gtid_mode::to_string(new_gtid_mode));
        my_error(ER_CANT_SET_GTID_MODE, MYF(0),
                 Gtid_mode::to_string(new_gtid_mode), buf);
        goto err;
      }
    }
  }

  // Can't set GTID_MODE != ON when group replication is enabled.
  if (is_group_replication_running()) {
    assert(old_gtid_mode == Gtid_mode::ON);
    assert(new_gtid_mode == Gtid_mode::ON_PERMISSIVE);
    my_error(ER_CANT_SET_GTID_MODE, MYF(0), Gtid_mode::to_string(new_gtid_mode),
             "group replication requires @@GLOBAL.GTID_MODE=ON");
    goto err;
  }

  // Compatible with ongoing transactions.
  DBUG_PRINT("info", ("anonymous_ownership_count=%d owned_gtids->is_empty=%d",
                      gtid_state->get_anonymous_ownership_count(),
                      gtid_state->get_owned_gtids()->is_empty()));
  gtid_state->get_owned_gtids()->dbug_print("global owned_gtids");
  if (new_gtid_mode == Gtid_mode::ON &&
      gtid_state->get_anonymous_ownership_count() > 0) {
    my_error(ER_CANT_SET_GTID_MODE, MYF(0), "ON",
             "there are ongoing, anonymous transactions. Before "
             "setting @@GLOBAL.GTID_MODE = ON, wait until "
             "SHOW STATUS LIKE 'ANONYMOUS_TRANSACTION_COUNT' "
             "shows zero on all servers. Then wait for all "
             "existing, anonymous transactions to replicate to "
             "all replicas, and then execute "
             "SET @@GLOBAL.GTID_MODE = ON on all servers. "
             "See the Manual for details");
    goto err;
  }

  if (new_gtid_mode == Gtid_mode::OFF &&
      !gtid_state->get_owned_gtids()->is_empty()) {
    my_error(ER_CANT_SET_GTID_MODE, MYF(0), "OFF",
             "there are ongoing transactions that have a GTID. "
             "Before you set @@GLOBAL.GTID_MODE = OFF, wait "
             "until SELECT @@GLOBAL.GTID_OWNED is empty on all "
             "servers. Then wait for all GTID-transactions to "
             "replicate to all servers, and then execute "
             "SET @@GLOBAL.GTID_MODE = OFF on all servers. "
             "See the Manual for details");
    goto err;
  }

  // Compatible with ongoing GTID-violating transactions
  DBUG_PRINT("info",
             ("automatic_gtid_violating_transaction_count=%d",
              gtid_state->get_automatic_gtid_violating_transaction_count()));
  if (new_gtid_mode >= Gtid_mode::ON_PERMISSIVE &&
      gtid_state->get_automatic_gtid_violating_transaction_count() > 0) {
    my_error(ER_CANT_SET_GTID_MODE, MYF(0), "ON_PERMISSIVE",
             "there are ongoing transactions that use "
             "GTID_NEXT = 'AUTOMATIC', which violate GTID "
             "consistency. Adjust your workload to be "
             "GTID-consistent before setting "
             "@@GLOBAL.GTID_MODE = ON_PERMISSIVE. "
             "See the Manual for "
             "@@GLOBAL.ENFORCE_GTID_CONSISTENCY for details");
    goto err;
  }

  // Compatible with ENFORCE_GTID_CONSISTENCY.
  if (new_gtid_mode == Gtid_mode::ON &&
      get_gtid_consistency_mode() != GTID_CONSISTENCY_MODE_ON) {
    my_error(ER_CANT_SET_GTID_MODE, MYF(0), "ON",
             "ENFORCE_GTID_CONSISTENCY is not ON");
    goto err;
  }

  // Can't set GTID_MODE=OFF with ongoing calls to
  // WAIT_FOR_EXECUTED_GTID_SET or
  // WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS.
  DBUG_PRINT("info",
             ("gtid_wait_count=%d", gtid_state->get_gtid_wait_count() > 0));
  if (new_gtid_mode == Gtid_mode::OFF &&
      gtid_state->get_gtid_wait_count() > 0) {
    my_error(ER_CANT_SET_GTID_MODE, MYF(0), "OFF",
             "there are ongoing calls to "
             "WAIT_FOR_EXECUTED_GTID_SET or "
             "WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS. Before you set "
             "@@GLOBAL.GTID_MODE = OFF, ensure that no other "
             "client is waiting for GTID-transactions to be "
             "committed");
    goto err;
  }

  // Update the mode
  global_var(ulong) = new_gtid_mode;
  global_gtid_mode.set(new_gtid_mode);
  global_sid_lock->unlock();
  lock_count = 3;

  // Generate note in log
  LogErr(SYSTEM_LEVEL, ER_CHANGED_GTID_MODE,
         Gtid_mode::to_string(old_gtid_mode),
         Gtid_mode::to_string(new_gtid_mode));

  // Rotate
  {
    bool dont_care = false;
    if (mysql_bin_log.rotate(true, &dont_care)) goto err;
  }

end:
  /* handle deprecations warning */
  issue_deprecation_warnings_gtid_mode(thd, old_gtid_mode, new_gtid_mode);

  ret = false;
err:
  assert(lock_count >= 0);
  assert(lock_count <= 4);
  if (lock_count == 4) global_sid_lock->unlock();
  mysql_mutex_unlock(mysql_bin_log.get_log_lock());
  channel_map.unlock();
  Gtid_mode::lock.unlock();
  return ret;
}

bool Sys_var_enforce_gtid_consistency::global_update(THD *thd, set_var *var) {
  DBUG_TRACE;
  bool ret = true;

  /*
    Hold global_sid_lock.wrlock so that other transactions cannot
    acquire ownership of any gtid.
  */
  global_sid_lock->wrlock();

  DBUG_PRINT("info", ("var->save_result.ulonglong_value=%llu",
                      var->save_result.ulonglong_value));
  enum_gtid_consistency_mode new_mode =
      (enum_gtid_consistency_mode)var->save_result.ulonglong_value;
  enum_gtid_consistency_mode old_mode = get_gtid_consistency_mode();
  auto gtid_mode = global_gtid_mode.get();

  assert(new_mode <= GTID_CONSISTENCY_MODE_WARN);

  DBUG_PRINT("info", ("old enforce_gtid_consistency=%d "
                      "new enforce_gtid_consistency=%d "
                      "gtid_mode=%d ",
                      old_mode, new_mode, gtid_mode));

  if (new_mode == old_mode) goto end;

  // Can't turn off GTID-consistency when GTID_MODE=ON.
  if (new_mode != GTID_CONSISTENCY_MODE_ON && gtid_mode == Gtid_mode::ON) {
    my_error(ER_GTID_MODE_ON_REQUIRES_ENFORCE_GTID_CONSISTENCY_ON, MYF(0));
    goto err;
  }
  // If there are ongoing GTID-violating transactions, and we are
  // moving from OFF->ON, WARN->ON, or OFF->WARN, generate warning
  // or error accordingly.
  if (new_mode == GTID_CONSISTENCY_MODE_ON ||
      (old_mode == GTID_CONSISTENCY_MODE_OFF &&
       new_mode == GTID_CONSISTENCY_MODE_WARN)) {
    DBUG_PRINT("info",
               ("automatic_gtid_violating_transaction_count=%d "
                "anonymous_gtid_violating_transaction_count=%d",
                gtid_state->get_automatic_gtid_violating_transaction_count(),
                gtid_state->get_anonymous_gtid_violating_transaction_count()));
    if (gtid_state->get_automatic_gtid_violating_transaction_count() > 0 ||
        gtid_state->get_anonymous_gtid_violating_transaction_count() > 0) {
      if (new_mode == GTID_CONSISTENCY_MODE_ON) {
        my_error(
            ER_CANT_ENFORCE_GTID_CONSISTENCY_WITH_ONGOING_GTID_VIOLATING_TX,
            MYF(0));
        goto err;
      } else {
        push_warning(
            thd, Sql_condition::SL_WARNING,
            ER_ENFORCE_GTID_CONSISTENCY_WARN_WITH_ONGOING_GTID_VIOLATING_TX,
            ER_THD(
                thd,
                ER_ENFORCE_GTID_CONSISTENCY_WARN_WITH_ONGOING_GTID_VIOLATING_TX));
      }
    }
  }

  // Update the mode
  global_var(ulong) = new_mode;

  // Generate note in log
  LogErr(INFORMATION_LEVEL, ER_CHANGED_ENFORCE_GTID_CONSISTENCY,
         get_gtid_consistency_mode_string(old_mode),
         get_gtid_consistency_mode_string(new_mode));

end:
  ret = false;
err:
  global_sid_lock->unlock();
  return ret;
}

static Sys_var_enum_binlog_checksum Binlog_checksum_enum(
    "binlog_checksum",
    "Type of BINLOG_CHECKSUM_ALG. Include checksum for "
    "log events in the binary log. Possible values are NONE and CRC32; "
    "default is CRC32.",
    GLOBAL_VAR(binlog_checksum_options), CMD_LINE(REQUIRED_ARG),
    binlog_checksum_type_names, DEFAULT(binary_log::BINLOG_CHECKSUM_ALG_CRC32),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_outside_trx));

static Sys_var_bool Sys_source_verify_checksum(
    "source_verify_checksum",
    "Force checksum verification of events in binary log before "
    "sending them to replicas or printing them in output of SHOW BINLOG "
    "EVENTS. "
    "Disabled by default.",
    GLOBAL_VAR(opt_source_verify_checksum), CMD_LINE(OPT_ARG), DEFAULT(false));

static Sys_var_deprecated_alias Sys_master_verify_checksum(
    "master_verify_checksum", Sys_source_verify_checksum);

static Sys_var_ulong Sys_slow_launch_time(
    "slow_launch_time",
    "If creating the thread takes longer than this value (in seconds), "
    "the Slow_launch_threads counter will be incremented",
    GLOBAL_VAR(slow_launch_time), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, LONG_TIMEOUT), DEFAULT(2), BLOCK_SIZE(1));

static Sys_var_ulong Sys_sort_buffer(
    "sort_buffer_size",
    "Each thread that needs to do a sort allocates a buffer of this size",
    HINT_UPDATEABLE SESSION_VAR(sortbuff_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(MIN_SORT_MEMORY, ULONG_MAX), DEFAULT(DEFAULT_SORT_MEMORY),
    BLOCK_SIZE(1));

/**
  Check sql modes strict_mode, 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and
  'ERROR_FOR_DIVISION_BY_ZERO' are used together. If only subset of it
  is set then warning is reported.

  @param sql_mode sql mode.
  @param thd      Current thread
*/
static void check_sub_modes_of_strict_mode(sql_mode_t &sql_mode, THD *thd) {
  const sql_mode_t strict_modes =
      (MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES);

  const sql_mode_t new_strict_submodes =
      (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
       MODE_ERROR_FOR_DIVISION_BY_ZERO);

  const sql_mode_t strict_modes_set = (sql_mode & strict_modes);
  const sql_mode_t new_strict_submodes_set = (sql_mode & new_strict_submodes);

  if (((strict_modes_set | new_strict_submodes_set) != 0) &&
      ((new_strict_submodes_set != new_strict_submodes) ||
       (strict_modes_set == 0))) {
    if (thd)
      push_warning(thd, Sql_condition::SL_WARNING, ER_SQL_MODE_MERGED,
                   ER_THD(thd, ER_SQL_MODE_MERGED));
    else
      LogErr(WARNING_LEVEL, ER_SQL_MODE_MERGED_WITH_STRICT_MODE);
  }
}

export sql_mode_t expand_sql_mode(sql_mode_t sql_mode, THD *thd) {
  if (sql_mode & MODE_ANSI) {
    /*
      Note that we dont set
      MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS | MODE_NO_FIELD_OPTIONS
      to allow one to get full use of MySQL in this mode.
    */
    sql_mode |= (MODE_REAL_AS_FLOAT | MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
                 MODE_IGNORE_SPACE | MODE_ONLY_FULL_GROUP_BY);
  }
  if (sql_mode & MODE_TRADITIONAL)
    sql_mode |= (MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES |
                 MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
                 MODE_ERROR_FOR_DIVISION_BY_ZERO | MODE_NO_ENGINE_SUBSTITUTION);

  check_sub_modes_of_strict_mode(sql_mode, thd);
  return sql_mode;
}
static bool check_sql_mode(sys_var *, THD *thd, set_var *var) {
  sql_mode_t candidate_mode =
      expand_sql_mode(var->save_result.ulonglong_value, thd);

  if (candidate_mode & ~(MODE_ALLOWED_MASK | MODE_IGNORED_MASK)) {
    my_error(ER_UNSUPPORTED_SQL_MODE, MYF(0),
             candidate_mode & ~(MODE_ALLOWED_MASK | MODE_IGNORED_MASK));
    return true;  // mode seems never supported before
  }

  if (candidate_mode & ~MODE_ALLOWED_MASK) {
    if (thd->variables.pseudo_replica_mode &&  // (1)
        thd->lex->sphead == nullptr) {         // (2)
      /*
        (1): catch the auto-generated SET SQL_MODE calls in the output of
             mysqlbinlog,
        (2): but ignore the other ones (e.g. nested SET SQL_MODE calls in
             SBR-invoked trigger calls).
      */
      push_warning_printf(
          thd, Sql_condition::SL_WARNING, ER_WARN_REMOVED_SQL_MODE,
          ER_THD(thd, ER_WARN_REMOVED_SQL_MODE),
          static_cast<uint>(candidate_mode & ~MODE_ALLOWED_MASK));
      // ignore obsolete mode flags in case this is an old mysqlbinlog:
      candidate_mode &= MODE_ALLOWED_MASK;
    } else {
      my_error(ER_UNSUPPORTED_SQL_MODE, MYF(0),
               candidate_mode & ~MODE_ALLOWED_MASK);
      return true;  // error on obsolete mode flags
    }
  }

  if (candidate_mode & MODE_PAD_CHAR_TO_FULL_LENGTH) {
    push_warning_printf(
        thd, Sql_condition::SL_WARNING, ER_WARN_DEPRECATED_SQLMODE,
        ER_THD(thd, ER_WARN_DEPRECATED_SQLMODE), "PAD_CHAR_TO_FULL_LENGTH");
  }

  var->save_result.ulonglong_value = candidate_mode;
  return false;
}
static bool fix_sql_mode(sys_var *self, THD *thd, enum_var_type type) {
  if (!self->is_global_persist(type)) {
    /* Update thd->server_status */
    if (thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES)
      thd->server_status |= SERVER_STATUS_NO_BACKSLASH_ESCAPES;
    else
      thd->server_status &= ~SERVER_STATUS_NO_BACKSLASH_ESCAPES;
  }
  return false;
}
/*
  WARNING: When adding new SQL modes don't forget to update the
  tables definitions that stores it's value (ie: mysql.event, mysql.routines,
  mysql.triggers)
*/
static const char *sql_mode_names[] = {"REAL_AS_FLOAT",
                                       "PIPES_AS_CONCAT",
                                       "ANSI_QUOTES",
                                       "IGNORE_SPACE",
                                       "NOT_USED",
                                       "ONLY_FULL_GROUP_BY",
                                       "NO_UNSIGNED_SUBTRACTION",
                                       "NO_DIR_IN_CREATE",
                                       "NOT_USED_9",
                                       "NOT_USED_10",
                                       "NOT_USED_11",
                                       "NOT_USED_12",
                                       "NOT_USED_13",
                                       "NOT_USED_14",
                                       "NOT_USED_15",
                                       "NOT_USED_16",
                                       "NOT_USED_17",
                                       "NOT_USED_18",
                                       "ANSI",
                                       "NO_AUTO_VALUE_ON_ZERO",
                                       "NO_BACKSLASH_ESCAPES",
                                       "STRICT_TRANS_TABLES",
                                       "STRICT_ALL_TABLES",
                                       "NO_ZERO_IN_DATE",
                                       "NO_ZERO_DATE",
                                       "ALLOW_INVALID_DATES",
                                       "ERROR_FOR_DIVISION_BY_ZERO",
                                       "TRADITIONAL",
                                       "NOT_USED_29",
                                       "HIGH_NOT_PRECEDENCE",
                                       "NO_ENGINE_SUBSTITUTION",
                                       "PAD_CHAR_TO_FULL_LENGTH",
                                       "TIME_TRUNCATE_FRACTIONAL",
                                       nullptr};
export bool sql_mode_string_representation(THD *thd, sql_mode_t sql_mode,
                                           LEX_STRING *ls) {
  set_to_string(thd, ls, sql_mode, sql_mode_names);
  return ls->str == nullptr;
}
export bool sql_mode_quoted_string_representation(THD *thd, sql_mode_t sql_mode,
                                                  LEX_STRING *ls) {
  set_to_string(thd, ls, sql_mode, sql_mode_names, true);
  return ls->str == nullptr;
}
/*
  sql_mode should *not* be IN_BINLOG: even though it is written to the binlog,
  the slave ignores the MODE_NO_DIR_IN_CREATE variable, so slave's value
  differs from master's (see log_event.cc: Query_log_event::do_apply_event()).
*/
static Sys_var_set Sys_sql_mode(
    "sql_mode",
    "Syntax: sql-mode=mode[,mode[,mode...]]. See the manual for the "
    "complete list of valid sql modes",
    HINT_UPDATEABLE SESSION_VAR(sql_mode), CMD_LINE(REQUIRED_ARG),
    sql_mode_names,
    DEFAULT(MODE_NO_ENGINE_SUBSTITUTION | MODE_ONLY_FULL_GROUP_BY |
            MODE_STRICT_TRANS_TABLES | MODE_NO_ZERO_IN_DATE |
            MODE_NO_ZERO_DATE | MODE_ERROR_FOR_DIVISION_BY_ZERO),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_sql_mode),
    ON_UPDATE(fix_sql_mode));

static Sys_var_ulong Sys_max_execution_time(
    "max_execution_time",
    "Kill SELECT statement that takes over the specified number of "
    "milliseconds",
    HINT_UPDATEABLE SESSION_VAR(max_execution_time), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, ULONG_MAX), DEFAULT(0), BLOCK_SIZE(1));

static const char *ssl_fips_mode_names[] = {"OFF", "ON", "STRICT", nullptr};
static Sys_var_enum Sys_ssl_fips_mode(
    "ssl_fips_mode",
    "SSL FIPS mode (applies only for OpenSSL); "
    "permitted values are: OFF, ON, STRICT",
    READ_ONLY GLOBAL_VAR(opt_ssl_fips_mode),
    CMD_LINE(REQUIRED_ARG, OPT_SSL_FIPS_MODE), ssl_fips_mode_names, DEFAULT(0),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""), sys_var::PARSE_EARLY);

static Sys_var_bool Sys_auto_generate_certs(
    "auto_generate_certs",
    "Auto generate SSL certificates at server startup if --ssl is set to "
    "ON and none of the other SSL system variables are specified and "
    "certificate/key files are not present in data directory.",
    READ_ONLY NON_PERSIST GLOBAL_VAR(opt_auto_generate_certs),
    CMD_LINE(OPT_ARG), DEFAULT(true), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(nullptr), ON_UPDATE(nullptr), nullptr);

// why ENUM and not BOOL ?
static const char *updatable_views_with_limit_names[] = {"NO", "YES", nullptr};
static Sys_var_enum Sys_updatable_views_with_limit(
    "updatable_views_with_limit",
    "YES = Don't issue an error message (warning only) if a VIEW without "
    "presence of a key of the underlying table is used in queries with a "
    "LIMIT clause for updating. NO = Prohibit update of a VIEW, which "
    "does not contain a key of the underlying table and the query uses "
    "a LIMIT clause (usually get from GUI tools)",
    HINT_UPDATEABLE SESSION_VAR(updatable_views_with_limit),
    CMD_LINE(REQUIRED_ARG), updatable_views_with_limit_names, DEFAULT(true));

static Sys_var_system_time_zone Sys_system_time_zone(
    "system_time_zone", "The server system time zone");

static Sys_var_ulong Sys_table_def_size(
    "table_definition_cache", "The number of cached table definitions",
    GLOBAL_VAR(table_def_size),
    CMD_LINE(REQUIRED_ARG, OPT_TABLE_DEFINITION_CACHE),
    VALID_RANGE(TABLE_DEF_CACHE_MIN, 512 * 1024),
    DEFAULT(TABLE_DEF_CACHE_DEFAULT), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr), nullptr,
    /* table_definition_cache is used as a sizing hint by the performance
       schema. */
    sys_var::PARSE_EARLY);

static Sys_var_ulong Sys_schema_def_size(
    "schema_definition_cache", "The number of cached schema definitions",
    GLOBAL_VAR(schema_def_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(SCHEMA_DEF_CACHE_MIN, 512 * 1024),
    DEFAULT(SCHEMA_DEF_CACHE_DEFAULT), BLOCK_SIZE(1));

static Sys_var_ulong Sys_tablespace_def_size(
    "tablespace_definition_cache",
    "The number of cached tablespace definitions",
    GLOBAL_VAR(tablespace_def_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(TABLESPACE_DEF_CACHE_MIN, 512 * 1024),
    DEFAULT(TABLESPACE_DEF_CACHE_DEFAULT), BLOCK_SIZE(1));

static Sys_var_ulong Sys_stored_program_def_size(
    "stored_program_definition_cache",
    "The number of cached stored program definitions",
    GLOBAL_VAR(stored_program_def_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(STORED_PROGRAM_DEF_CACHE_MIN, 512 * 1024),
    DEFAULT(STORED_PROGRAM_DEF_CACHE_DEFAULT), BLOCK_SIZE(1));

static bool fix_table_cache_size(sys_var *, THD *, enum_var_type) {
  /*
    table_open_cache parameter is a soft limit for total number of objects
    in all table cache instances. Once this value is updated we need to
    update value of a per-instance soft limit on table cache size.
  */
  table_cache_size_per_instance = table_cache_size / table_cache_instances;
  return false;
}

static Sys_var_ulong Sys_table_cache_size(
    "table_open_cache",
    "The number of cached open tables "
    "(total for all table cache instances)",
    GLOBAL_VAR(table_cache_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, 512 * 1024), DEFAULT(TABLE_OPEN_CACHE_DEFAULT),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_table_cache_size), nullptr,
    /* table_open_cache is used as a sizing hint by the performance schema. */
    sys_var::PARSE_EARLY);

static Sys_var_ulong Sys_table_cache_instances(
    "table_open_cache_instances", "The number of table cache instances",
    READ_ONLY GLOBAL_VAR(table_cache_instances), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, Table_cache_manager::MAX_TABLE_CACHES),
    DEFAULT(Table_cache_manager::DEFAULT_MAX_TABLE_CACHES), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr),
    nullptr,
    /*
      table_open_cache is used as a sizing hint by the performance schema,
      and 'table_open_cache' is a prefix of 'table_open_cache_instances'.
      Is is better to keep these options together, to avoid confusing
      handle_options() with partial name matches.
    */
    sys_var::PARSE_EARLY);

/**
  Modify the thread size cache size.
*/

static inline bool modify_thread_cache_size(sys_var *, THD *, enum_var_type) {
  if (Connection_handler_manager::thread_handling ==
      Connection_handler_manager::SCHEDULER_ONE_THREAD_PER_CONNECTION) {
    Per_thread_connection_handler::modify_thread_cache_size(
        Per_thread_connection_handler::max_blocked_pthreads);
  }
  return false;
}

static Sys_var_ulong Sys_thread_cache_size(
    "thread_cache_size", "How many threads we should keep in a cache for reuse",
    GLOBAL_VAR(Per_thread_connection_handler::max_blocked_pthreads),
    CMD_LINE(REQUIRED_ARG, OPT_THREAD_CACHE_SIZE), VALID_RANGE(0, 16384),
    DEFAULT(0), BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG, nullptr,
    ON_UPDATE(modify_thread_cache_size));

/**
  Function to check if the 'next' transaction isolation level
  can be changed.

  @param[in] thd    Thread handler.
  @param[in] var    A pointer to set_var holding the specified list of
                    system variable names.

  @retval   false   Success.
  @retval   true    Error.
*/
static bool check_transaction_isolation(sys_var *, THD *thd, set_var *var) {
  if (var->type == OPT_DEFAULT &&
      (thd->in_active_multi_stmt_transaction() || thd->in_sub_stmt)) {
    assert(thd->in_multi_stmt_transaction_mode() || thd->in_sub_stmt);
    my_error(ER_CANT_CHANGE_TX_CHARACTERISTICS, MYF(0));
    return true;
  }
  return false;
}

/**
  This function sets the session variable thd->variables.transaction_isolation
  to reflect changes to @@session.transaction_isolation.

  @param[in] thd    Thread handler.
  @param[in] var    A pointer to the set_var.

  @retval   false   Success.
  @retval   true    Error.
*/
bool Sys_var_transaction_isolation::session_update(THD *thd, set_var *var) {
  if (var->type == OPT_SESSION && Sys_var_enum::session_update(thd, var))
    return true;
  if (var->type == OPT_DEFAULT ||
      !(thd->in_active_multi_stmt_transaction() || thd->in_sub_stmt)) {
    /*
      Update the isolation level of the next transaction.
      I.e. if one did:
      COMMIT;
      SET SESSION ISOLATION LEVEL ...
      BEGIN; <-- this transaction has the new isolation
      Note, that in case of:
      COMMIT;
      SET TRANSACTION ISOLATION LEVEL ...
      SET SESSION ISOLATION LEVEL ...
      BEGIN; <-- the session isolation level is used, not the
      result of SET TRANSACTION statement.

      When we are in a trigger/function the transaction is already
      started. Adhering to above behavior, the SET TRANSACTION would
      fail when run from within trigger/function. And SET SESSION
      TRANSACTION would always succeed making the characteristics
      effective for the next transaction that starts.
     */
    enum_tx_isolation tx_isol;
    tx_isol = (enum_tx_isolation)var->save_result.ulonglong_value;
    bool one_shot = (var->type == OPT_DEFAULT);
    return set_tx_isolation(thd, tx_isol, one_shot);
  }
  return false;
}

// NO_CMD_LINE
static Sys_var_transaction_isolation Sys_transaction_isolation(
    "transaction_isolation", "Default transaction isolation level",
    UNTRACKED_DEFAULT SESSION_VAR(transaction_isolation), NO_CMD_LINE,
    tx_isolation_names, DEFAULT(ISO_REPEATABLE_READ), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_transaction_isolation));

/**
  Function to check if the state of 'transaction_read_only' can be changed.
  The state cannot be changed if there is already a transaction in progress.

  @param[in] thd    Thread handler
  @param[in] var    A pointer to set_var holding the specified list of
                    system variable names.

  @retval   false   Success.
  @retval   true    Error.
*/
static bool check_transaction_read_only(sys_var *, THD *thd, set_var *var) {
  if (var->type == OPT_DEFAULT &&
      (thd->in_active_multi_stmt_transaction() || thd->in_sub_stmt)) {
    assert(thd->in_multi_stmt_transaction_mode() || thd->in_sub_stmt);
    my_error(ER_CANT_CHANGE_TX_CHARACTERISTICS, MYF(0));
    return true;
  }
  return false;
}

/**
  This function sets the session variable thd->variables.transaction_read_only
  to reflect changes to @@session.transaction_read_only.

  @param[in] thd    Thread handler.
  @param[in] var    A pointer to the set_var.

  @retval   false   Success.
*/
bool Sys_var_transaction_read_only::session_update(THD *thd, set_var *var) {
  if (var->type == OPT_SESSION && Sys_var_bool::session_update(thd, var))
    return true;
  if (var->type == OPT_DEFAULT ||
      !(thd->in_active_multi_stmt_transaction() || thd->in_sub_stmt)) {
    // @see Sys_var_transaction_isolation::session_update() above for the
    // rules.
    thd->tx_read_only = var->save_result.ulonglong_value;

    if (thd->variables.session_track_transaction_info > TX_TRACK_NONE) {
      TX_TRACKER_GET(tst);

      if (var->type == OPT_DEFAULT)
        tst->set_read_flags(thd,
                            thd->tx_read_only ? TX_READ_ONLY : TX_READ_WRITE);
      else
        tst->set_read_flags(thd, TX_READ_INHERIT);
    }
  }
  return false;
}

static Sys_var_transaction_read_only Sys_transaction_read_only(
    "transaction_read_only",
    "Set default transaction access mode to read only.",
    UNTRACKED_DEFAULT SESSION_VAR(transaction_read_only), NO_CMD_LINE,
    DEFAULT(0), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_transaction_read_only));

static Sys_var_ulonglong Sys_tmp_table_size(
    "tmp_table_size",
    "If an internal in-memory temporary table in the MEMORY or TempTable "
    "storage engine exceeds this size, MySQL will automatically convert it "
    "to an on-disk table ",
    HINT_UPDATEABLE SESSION_VAR(tmp_table_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1024, std::numeric_limits<ulonglong>::max()),
    DEFAULT(16 * 1024 * 1024), BLOCK_SIZE(1));

static char *server_version_ptr;
static Sys_var_version Sys_version(
    "version", "Server version",
    READ_ONLY NON_PERSIST GLOBAL_VAR(server_version_ptr), NO_CMD_LINE,
    IN_SYSTEM_CHARSET, DEFAULT(server_version));

static char *server_version_comment_ptr;
static Sys_var_charptr Sys_version_comment(
    "version_comment", "version_comment",
    READ_ONLY NON_PERSIST GLOBAL_VAR(server_version_comment_ptr), NO_CMD_LINE,
    IN_SYSTEM_CHARSET, DEFAULT(MYSQL_COMPILATION_COMMENT_SERVER));

static char *server_version_compile_machine_ptr;
static Sys_var_charptr Sys_version_compile_machine(
    "version_compile_machine", "version_compile_machine",
    READ_ONLY NON_PERSIST GLOBAL_VAR(server_version_compile_machine_ptr),
    NO_CMD_LINE, IN_SYSTEM_CHARSET, DEFAULT(MACHINE_TYPE));

static char *server_version_compile_os_ptr;
static Sys_var_charptr Sys_version_compile_os(
    "version_compile_os", "version_compile_os",
    READ_ONLY NON_PERSIST GLOBAL_VAR(server_version_compile_os_ptr),
    NO_CMD_LINE, IN_SYSTEM_CHARSET, DEFAULT(SYSTEM_TYPE));

static const char *server_version_compile_zlib_ptr = ZLIB_VERSION;
static Sys_var_charptr Sys_version_compile_zlib(
    "version_compile_zlib", "version_compile_zlib",
    READ_ONLY NON_PERSIST GLOBAL_VAR(server_version_compile_zlib_ptr),
    NO_CMD_LINE, IN_SYSTEM_CHARSET, DEFAULT(ZLIB_VERSION));

static Sys_var_ulong Sys_net_wait_timeout(
    "wait_timeout",
    "The number of seconds the server waits for activity on a "
    "connection before closing it",
    SESSION_VAR(net_wait_timeout), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, IF_WIN(INT_MAX32 / 1000, LONG_TIMEOUT)),
    DEFAULT(NET_WAIT_TIMEOUT), BLOCK_SIZE(1));

static Sys_var_plugin Sys_default_storage_engine(
    "default_storage_engine", "The default storage engine for new tables",
    SESSION_VAR(table_plugin), NO_CMD_LINE, MYSQL_STORAGE_ENGINE_PLUGIN,
    DEFAULT(&default_storage_engine), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_storage_engine));

const char *internal_tmp_mem_storage_engine_names[] = {"MEMORY", "TempTable",
                                                       nullptr};
static Sys_var_enum Sys_internal_tmp_mem_storage_engine(
    "internal_tmp_mem_storage_engine",
    "The default storage engine for in-memory internal temporary tables.",
    HINT_UPDATEABLE SESSION_VAR(internal_tmp_mem_storage_engine),
    CMD_LINE(REQUIRED_ARG), internal_tmp_mem_storage_engine_names,
    DEFAULT(TMP_TABLE_TEMPTABLE), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_session_admin_no_super));

static Sys_var_ulonglong Sys_temptable_max_ram(
    "temptable_max_ram",
    "Maximum amount of memory (in bytes) the TempTable storage engine is "
    "allowed to allocate from the main memory (RAM) before starting to "
    "store data on disk.",
    GLOBAL_VAR(temptable_max_ram), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(2 << 20 /* 2 MiB */, ULLONG_MAX), DEFAULT(1 << 30 /* 1 GiB */),
    BLOCK_SIZE(1));

static Sys_var_ulonglong Sys_temptable_max_mmap(
    "temptable_max_mmap",
    "Maximum amount of memory (in bytes) the TempTable storage engine is "
    "allowed to allocate from MMAP-backed files before starting to "
    "store data on disk.",
    GLOBAL_VAR(temptable_max_mmap), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, ULLONG_MAX), DEFAULT(1 << 30 /* 1 GiB */), BLOCK_SIZE(1));

static Sys_var_bool Sys_temptable_use_mmap(
    "temptable_use_mmap",
    "Use mmap files for temptables. "
    "This variable is deprecated and will be removed in a future release.",
    GLOBAL_VAR(temptable_use_mmap), CMD_LINE(OPT_ARG), DEFAULT(true),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(update_deprecated_with_removal_message), nullptr,
    sys_var::PARSE_NORMAL);

static Sys_var_plugin Sys_default_tmp_storage_engine(
    "default_tmp_storage_engine",
    "The default storage engine for new explicit temporary tables",
    HINT_UPDATEABLE SESSION_VAR(temp_table_plugin), NO_CMD_LINE,
    MYSQL_STORAGE_ENGINE_PLUGIN, DEFAULT(&default_tmp_storage_engine),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_storage_engine));

#if defined(ENABLED_DEBUG_SYNC)
/*
  Variable can be set for the session only.

  This could be changed later. Then we need to have a global array of
  actions in addition to the thread local ones. SET GLOBAL would
  manage the global array, SET [SESSION] the local array. A sync point
  would need to look for a local and a global action. Setting and
  executing of global actions need to be protected by a mutex.

  The purpose of global actions could be to allow synchronizing with
  connectionless threads that cannot execute SET statements.
*/
static Sys_var_debug_sync Sys_debug_sync("debug_sync", "Debug Sync Facility",
                                         sys_var::ONLY_SESSION, NO_CMD_LINE,
                                         DEFAULT(nullptr), NO_MUTEX_GUARD,
                                         NOT_IN_BINLOG,
                                         ON_CHECK(check_session_admin));
#endif /* defined(ENABLED_DEBUG_SYNC) */

/**
  Pre-update function to commit connection's active transactions when autocommit
  is enabled.

  @note This hook relies on the fact that it is called while not holding any
        locks. Breaking this assumption might result in deadlocks as commit
        acquires many different locks in its process (e.g. to open GTID-related
        tables).

  @param[in] self   A pointer to the sys_var, i.e. Sys_autocommit.
  @param[in] thd    A reference to THD object.
  @param[in] var    A pointer to the set_var created by the parser.

  @retval true   Error during commit
  @retval false  Otherwise
*/
static bool pre_autocommit(sys_var *self, THD *thd, set_var *var) {
  if (!(self->is_global_persist(var->type)) &&
      (thd->variables.option_bits & OPTION_NOT_AUTOCOMMIT) &&
      var->save_result.ulonglong_value) {
    // Autocommit mode is about to be activated.
    if (trans_commit_stmt(thd) || trans_commit(thd)) return true;
  }
  return false;
}

static bool fix_autocommit(sys_var *self, THD *thd, enum_var_type type) {
  if (self->is_global_persist(type)) {
    if (global_system_variables.option_bits & OPTION_AUTOCOMMIT)
      global_system_variables.option_bits &= ~OPTION_NOT_AUTOCOMMIT;
    else
      global_system_variables.option_bits |= OPTION_NOT_AUTOCOMMIT;
    return false;
  }

  if (thd->variables.option_bits & OPTION_AUTOCOMMIT &&
      thd->variables.option_bits &
          OPTION_NOT_AUTOCOMMIT) {  // activating autocommit
    /*
      Don't close thread tables or release metadata locks: if we do so, we
      risk releasing locks/closing tables of expressions used to assign
      other variables, as in:
      set @var=my_stored_function1(), @@autocommit=1, @var2=(select max(a)
      from my_table), ...
      The locks will be released at statement end anyway, as SET
      statement that assigns autocommit is marked to commit
      transaction implicitly at the end (@sa stmt_causes_implicitcommit()).
    */
    thd->variables.option_bits &= ~(OPTION_BEGIN | OPTION_NOT_AUTOCOMMIT);
    thd->get_transaction()->reset_unsafe_rollback_flags(
        Transaction_ctx::SESSION);
    thd->server_status |= SERVER_STATUS_AUTOCOMMIT;
    return false;
  }

  if (!(thd->variables.option_bits & OPTION_AUTOCOMMIT) &&
      !(thd->variables.option_bits &
        OPTION_NOT_AUTOCOMMIT)) {  // disabling autocommit

    thd->get_transaction()->reset_unsafe_rollback_flags(
        Transaction_ctx::SESSION);
    thd->server_status &= ~SERVER_STATUS_AUTOCOMMIT;
    thd->variables.option_bits |= OPTION_NOT_AUTOCOMMIT;
    return false;
  }

  return false;  // autocommit value wasn't changed
}
static Sys_var_bit Sys_autocommit("autocommit", "autocommit",
                                  SESSION_VAR(option_bits), NO_CMD_LINE,
                                  OPTION_AUTOCOMMIT, DEFAULT(true),
                                  NO_MUTEX_GUARD, NOT_IN_BINLOG,
                                  ON_CHECK(nullptr), PRE_UPDATE(pre_autocommit),
                                  ON_UPDATE(fix_autocommit));
export sys_var *Sys_autocommit_ptr = &Sys_autocommit;  // for sql_yacc.yy

static Sys_var_bool Sys_big_tables(
    "big_tables",
    "Allow big result sets by saving all "
    "temporary sets on file (Solves most 'table full' errors)",
    HINT_UPDATEABLE SESSION_VAR(big_tables), CMD_LINE(OPT_ARG), DEFAULT(false));

static Sys_var_bit Sys_big_selects("sql_big_selects", "sql_big_selects",
                                   HINT_UPDATEABLE SESSION_VAR(option_bits),
                                   NO_CMD_LINE, OPTION_BIG_SELECTS,
                                   DEFAULT(false));

static Sys_var_bit Sys_log_off("sql_log_off", "sql_log_off",
                               SESSION_VAR(option_bits), NO_CMD_LINE,
                               OPTION_LOG_OFF, DEFAULT(false), NO_MUTEX_GUARD,
                               NOT_IN_BINLOG, ON_CHECK(check_session_admin));

/**
  This function sets the session variable thd->variables.sql_log_bin
  to reflect changes to @@session.sql_log_bin.

  @param     thd    Current thread
  @param[in] type   The type either session or global.

  @return @c false.
*/
static bool fix_sql_log_bin_after_update(sys_var *, THD *thd,
                                         enum_var_type type [[maybe_unused]]) {
  assert(type == OPT_SESSION);

  if (thd->variables.sql_log_bin)
    thd->variables.option_bits |= OPTION_BIN_LOG;
  else
    thd->variables.option_bits &= ~OPTION_BIN_LOG;

  return false;
}

/**
  This function checks if the sql_log_bin can be changed,
  what is possible if:
    - the user is a super user;
    - the set is not called from within a function/trigger;
    - there is no on-going transaction.

  @param     thd    Current thread
  @param[in] self   A pointer to the sys_var, i.e. Sys_log_binlog.
  @param[in] var    A pointer to the set_var created by the parser.

  @return @c false if the change is allowed, otherwise @c true.
*/
static bool check_sql_log_bin(sys_var *self, THD *thd, set_var *var) {
  if (check_session_admin(self, thd, var)) return true;

  if (var->is_global_persist()) return true;

  /* If in a stored function/trigger, it's too late to change sql_log_bin. */
  if (thd->in_sub_stmt) {
    my_error(ER_STORED_FUNCTION_PREVENTS_SWITCH_SQL_LOG_BIN, MYF(0));
    return true;
  }
  /* Make the session variable 'sql_log_bin' read-only inside a transaction.
   */
  if (thd->in_active_multi_stmt_transaction()) {
    my_error(ER_INSIDE_TRANSACTION_PREVENTS_SWITCH_SQL_LOG_BIN, MYF(0));
    return true;
  }

  return false;
}

static Sys_var_bool Sys_log_binlog(
    "sql_log_bin", "Controls whether logging to the binary log is done",
    SESSION_ONLY(sql_log_bin), NO_CMD_LINE, DEFAULT(true), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_sql_log_bin),
    ON_UPDATE(fix_sql_log_bin_after_update));

static Sys_var_bit Sys_transaction_allow_batching(
    "transaction_allow_batching", "transaction_allow_batching",
    SESSION_ONLY(option_bits), NO_CMD_LINE, OPTION_ALLOW_BATCH, DEFAULT(false));

static Sys_var_bit Sys_sql_warnings("sql_warnings", "sql_warnings",
                                    SESSION_VAR(option_bits), NO_CMD_LINE,
                                    OPTION_WARNINGS, DEFAULT(false));

static Sys_var_bit Sys_sql_notes("sql_notes", "sql_notes",
                                 SESSION_VAR(option_bits), NO_CMD_LINE,
                                 OPTION_SQL_NOTES, DEFAULT(true));

static Sys_var_bit Sys_auto_is_null("sql_auto_is_null", "sql_auto_is_null",
                                    HINT_UPDATEABLE SESSION_VAR(option_bits),
                                    NO_CMD_LINE, OPTION_AUTO_IS_NULL,
                                    DEFAULT(false), NO_MUTEX_GUARD, IN_BINLOG);

static Sys_var_bit Sys_safe_updates("sql_safe_updates", "sql_safe_updates",
                                    HINT_UPDATEABLE SESSION_VAR(option_bits),
                                    NO_CMD_LINE, OPTION_SAFE_UPDATES,
                                    DEFAULT(false));

static Sys_var_bit Sys_buffer_results("sql_buffer_result", "sql_buffer_result",
                                      HINT_UPDATEABLE SESSION_VAR(option_bits),
                                      NO_CMD_LINE, OPTION_BUFFER_RESULT,
                                      DEFAULT(false));

static Sys_var_bit Sys_quote_show_create("sql_quote_show_create",
                                         "sql_quote_show_create",
                                         SESSION_VAR(option_bits), NO_CMD_LINE,
                                         OPTION_QUOTE_SHOW_CREATE,
                                         DEFAULT(true));

static Sys_var_bit Sys_foreign_key_checks(
    "foreign_key_checks", "foreign_key_checks",
    HINT_UPDATEABLE SESSION_VAR(option_bits), NO_CMD_LINE,
    REVERSE(OPTION_NO_FOREIGN_KEY_CHECKS), DEFAULT(true), NO_MUTEX_GUARD,
    IN_BINLOG);

static Sys_var_bit Sys_unique_checks("unique_checks", "unique_checks",
                                     HINT_UPDATEABLE SESSION_VAR(option_bits),
                                     NO_CMD_LINE,
                                     REVERSE(OPTION_RELAXED_UNIQUE_CHECKS),
                                     DEFAULT(true), NO_MUTEX_GUARD, IN_BINLOG);

#ifdef ENABLED_PROFILING
static Sys_var_bit Sys_profiling("profiling", "profiling",
                                 SESSION_VAR(option_bits), NO_CMD_LINE,
                                 OPTION_PROFILING, DEFAULT(false),
                                 NO_MUTEX_GUARD, NOT_IN_BINLOG,
                                 ON_CHECK(nullptr), PRE_UPDATE(nullptr),
                                 ON_UPDATE(nullptr), DEPRECATED_VAR(""));

static Sys_var_ulong Sys_profiling_history_size(
    "profiling_history_size", "Limit of query profiling memory",
    SESSION_VAR(profiling_history_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 100), DEFAULT(15), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr), DEPRECATED_VAR(""));
#endif

static Sys_var_harows Sys_select_limit(
    "sql_select_limit",
    "The maximum number of rows to return from SELECT statements",
    HINT_UPDATEABLE SESSION_VAR(select_limit), NO_CMD_LINE,
    VALID_RANGE(0, HA_POS_ERROR), DEFAULT(HA_POS_ERROR), BLOCK_SIZE(1));

static bool update_timestamp(THD *thd, set_var *var) {
  if (var->value) {
    double intpart;
    double fractpart = modf(var->save_result.double_value, &intpart);
    double micros = fractpart * 1000000.0;
    // Double multiplication, and conversion to integral may yield
    // 1000000 rather than 999999.
    struct timeval tmp;
    tmp.tv_sec = llrint(intpart);
    tmp.tv_usec = std::min(llrint(micros), 999999LL);
    thd->set_time(&tmp);
  } else  // SET timestamp=DEFAULT
  {
    thd->user_time.tv_sec = 0;
    thd->user_time.tv_usec = 0;
  }
  return false;
}
static double read_timestamp(THD *thd) {
  return (double)thd->start_time.tv_sec +
         (double)thd->start_time.tv_usec / 1000000;
}

static bool check_timestamp(sys_var *, THD *, set_var *var) {
  double val;

  if (!var->value) return false;

  val = var->save_result.double_value;
  if (val != 0 &&  // this is how you set the default value
      (val < TYPE_TIMESTAMP_MIN_VALUE || val > TYPE_TIMESTAMP_MAX_VALUE)) {
    ErrConvString prm(val);
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), "timestamp", prm.ptr());
    return true;
  }
  return false;
}

static Sys_var_session_special_double Sys_timestamp(
    "timestamp", "Set the time for this client",
    HINT_UPDATEABLE sys_var::ONLY_SESSION, NO_CMD_LINE, VALID_RANGE(0, 0),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, IN_BINLOG, ON_CHECK(check_timestamp),
    ON_UPDATE(update_timestamp), ON_READ(read_timestamp));

static bool update_last_insert_id(THD *thd, set_var *var) {
  if (!var->value) {
    my_error(ER_NO_DEFAULT, MYF(0), var->m_var_tracker.get_var_name());
    return true;
  }
  thd->first_successful_insert_id_in_prev_stmt =
      var->save_result.ulonglong_value;
  return false;
}
static ulonglong read_last_insert_id(THD *thd) {
  return thd->read_first_successful_insert_id_in_prev_stmt();
}
static Sys_var_session_special Sys_last_insert_id(
    "last_insert_id", "The value to be returned from LAST_INSERT_ID()",
    sys_var::ONLY_SESSION, NO_CMD_LINE, VALID_RANGE(0, ULLONG_MAX),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(update_last_insert_id), ON_READ(read_last_insert_id));

// alias for last_insert_id(), Sybase-style
static Sys_var_session_special Sys_identity(
    "identity", "Synonym for the last_insert_id variable",
    sys_var::ONLY_SESSION, NO_CMD_LINE, VALID_RANGE(0, ULLONG_MAX),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(update_last_insert_id), ON_READ(read_last_insert_id));

/*
  insert_id should *not* be marked as written to the binlog (i.e., it
  should *not* be IN_BINLOG), because we want any statement that
  refers to insert_id explicitly to be unsafe.  (By "explicitly", we
  mean using @@session.insert_id, whereas insert_id is used
  "implicitly" when NULL value is inserted into an auto_increment
  column).

  We want statements referring explicitly to @@session.insert_id to be
  unsafe, because insert_id is modified internally by the slave sql
  thread when NULL values are inserted in an AUTO_INCREMENT column.
  This modification interfers with the value of the
  @@session.insert_id variable if @@session.insert_id is referred
  explicitly by an insert statement (as is seen by executing "SET
  @@session.insert_id=0; CREATE TABLE t (a INT, b INT KEY
  AUTO_INCREMENT); INSERT INTO t(a) VALUES (@@session.insert_id);" in
  statement-based logging mode: t will be different on master and
  slave).
*/
static bool update_insert_id(THD *thd, set_var *var) {
  if (!var->value) {
    my_error(ER_NO_DEFAULT, MYF(0), var->m_var_tracker.get_var_name());
    return true;
  }
  thd->force_one_auto_inc_interval(var->save_result.ulonglong_value);
  return false;
}

static ulonglong read_insert_id(THD *thd) {
  return thd->auto_inc_intervals_forced.minimum();
}
static Sys_var_session_special Sys_insert_id(
    "insert_id",
    "The value to be used by the following INSERT "
    "or ALTER TABLE statement when inserting an AUTO_INCREMENT value",
    HINT_UPDATEABLE sys_var::ONLY_SESSION, NO_CMD_LINE,
    VALID_RANGE(0, ULLONG_MAX), BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(nullptr), ON_UPDATE(update_insert_id), ON_READ(read_insert_id));

static bool update_rand_seed1(THD *thd, set_var *var) {
  if (!var->value) {
    my_error(ER_NO_DEFAULT, MYF(0), var->m_var_tracker.get_var_name());
    return true;
  }
  thd->rand.seed1 = (ulong)var->save_result.ulonglong_value;
  return false;
}
static ulonglong read_rand_seed(THD *) { return 0; }
static Sys_var_session_special Sys_rand_seed1(
    "rand_seed1",
    "Sets the internal state of the RAND() "
    "generator for replication purposes",
    sys_var::ONLY_SESSION, NO_CMD_LINE, VALID_RANGE(0, ULONG_MAX),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(update_rand_seed1), ON_READ(read_rand_seed));

static bool update_rand_seed2(THD *thd, set_var *var) {
  if (!var->value) {
    my_error(ER_NO_DEFAULT, MYF(0), var->m_var_tracker.get_var_name());
    return true;
  }
  thd->rand.seed2 = (ulong)var->save_result.ulonglong_value;
  return false;
}
static Sys_var_session_special Sys_rand_seed2(
    "rand_seed2",
    "Sets the internal state of the RAND() "
    "generator for replication purposes",
    sys_var::ONLY_SESSION, NO_CMD_LINE, VALID_RANGE(0, ULONG_MAX),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(update_rand_seed2), ON_READ(read_rand_seed));

static ulonglong read_error_count(THD *thd) {
  return thd->get_stmt_da()->error_count(thd);
}
// this really belongs to the SHOW STATUS
static Sys_var_session_special Sys_error_count(
    "error_count",
    "The number of errors that resulted from the "
    "last statement that generated messages",
    READ_ONLY sys_var::ONLY_SESSION, NO_CMD_LINE, VALID_RANGE(0, ULLONG_MAX),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(nullptr), ON_READ(read_error_count));

static ulonglong read_warning_count(THD *thd) {
  return thd->get_stmt_da()->warn_count(thd);
}

static ulonglong read_statement_id(THD *thd) {
  return (ulonglong)thd->query_id;
}

// this really belongs to the SHOW STATUS
static Sys_var_session_special Sys_warning_count(
    "warning_count",
    "The number of errors, warnings, and notes "
    "that resulted from the last statement that generated messages",
    READ_ONLY sys_var::ONLY_SESSION, NO_CMD_LINE, VALID_RANGE(0, ULLONG_MAX),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(nullptr), ON_READ(read_warning_count));

static Sys_var_ulong Sys_default_week_format(
    "default_week_format", "The default week format used by WEEK() functions",
    SESSION_VAR(default_week_format), CMD_LINE(REQUIRED_ARG), VALID_RANGE(0, 7),
    DEFAULT(0), BLOCK_SIZE(1));

static Sys_var_ulong Sys_group_concat_max_len(
    "group_concat_max_len",
    "The maximum length of the result of function  GROUP_CONCAT()",
    HINT_UPDATEABLE SESSION_VAR(group_concat_max_len), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(4, ULONG_MAX), DEFAULT(1024), BLOCK_SIZE(1));

static char *glob_hostname_ptr;
static Sys_var_charptr Sys_hostname(
    "hostname", "Server host name",
    READ_ONLY NON_PERSIST GLOBAL_VAR(glob_hostname_ptr), NO_CMD_LINE,
    IN_FS_CHARSET, DEFAULT(glob_hostname));

static Sys_var_charptr Sys_repl_report_host(
    "report_host",
    "Hostname or IP that this replica will report to the source while "
    "initiating the replication connection. Will appear in the output of "
    "SHOW REPLICAS. Leave this unset if you do not want the replica to "
    "register itself with the source. Note that it is not sufficient for "
    "the source to simply read the IP of the replica off the socket once the "
    "replica connects: in the presence of NAT other routing features, that IP "
    "may not be valid for connecting to the replica from the source or other "
    "hosts.",
    READ_ONLY GLOBAL_VAR(report_host), CMD_LINE(REQUIRED_ARG), IN_FS_CHARSET,
    DEFAULT(nullptr));

static Sys_var_charptr Sys_repl_report_user(
    "report_user",
    "The account user name that this replica will report to the source "
    "while initiating the replication connection.",
    READ_ONLY GLOBAL_VAR(report_user), CMD_LINE(REQUIRED_ARG), IN_FS_CHARSET,
    DEFAULT(nullptr));

static Sys_var_charptr Sys_repl_report_password(
    "report_password",
    "The account password that this replica will report to the source "
    "while initiating the replication connection.",
    READ_ONLY GLOBAL_VAR(report_password), CMD_LINE(REQUIRED_ARG),
    IN_FS_CHARSET, DEFAULT(nullptr));

static Sys_var_uint Sys_repl_report_port(
    "report_port",
    "The port for connecting to the replica, which this replica will report "
    "to the source while initiating the replication connection. "
    "Set it only if the replica is listening on a non-default "
    "port or if you have a special tunnel from the source or other clients "
    "to this replica. If not sure, leave this option unset.",
    READ_ONLY GLOBAL_VAR(report_port), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, 65535), DEFAULT(0), BLOCK_SIZE(1));

static Sys_var_bool Sys_keep_files_on_create(
    "keep_files_on_create",
    "Don't overwrite stale .MYD and .MYI even if no directory is specified",
    SESSION_VAR(keep_files_on_create), CMD_LINE(OPT_ARG), DEFAULT(false));

static char *license;
static Sys_var_charptr Sys_license("license",
                                   "The type of license the server has",
                                   READ_ONLY NON_PERSIST GLOBAL_VAR(license),
                                   NO_CMD_LINE, IN_SYSTEM_CHARSET,
                                   DEFAULT(STRINGIFY_ARG(LICENSE)));

static bool check_log_path(sys_var *self, THD *, set_var *var) {
  if (!var->value) return false;  // DEFAULT is ok

  if (!var->save_result.string_value.str) return true;

  if (!is_valid_log_name(var->save_result.string_value.str,
                         var->save_result.string_value.length)) {
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), self->name.str,
             var->save_result.string_value.str);
    return true;
  }

  if (var->save_result.string_value.length > FN_REFLEN) {  // path is too long
    my_error(ER_PATH_LENGTH, MYF(0), self->name.str);
    return true;
  }

  char path[FN_REFLEN];
  size_t path_length = unpack_filename(path, var->save_result.string_value.str);

  if (!path_length) return true;

  if (!is_filename_allowed(var->save_result.string_value.str,
                           var->save_result.string_value.length, true)) {
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), self->name.str,
             var->save_result.string_value.str);
    return true;
  }

  MY_STAT f_stat;

  if (my_stat(path, &f_stat, MYF(0))) {
    if (!MY_S_ISREG(f_stat.st_mode) || !(f_stat.st_mode & MY_S_IWRITE))
      return true;  // not a regular writable file
    return false;
  }

  (void)dirname_part(path, var->save_result.string_value.str, &path_length);

  if (var->save_result.string_value.length - path_length >=
      FN_LEN) {  // filename is too long
    my_error(ER_PATH_LENGTH, MYF(0), self->name.str);
    return true;
  }

  if (!path_length)  // no path is good path (remember, relative to datadir)
    return false;

  if (my_access(path, (F_OK | W_OK))) return true;  // directory is not writable

  return false;
}

static bool fix_general_log_file(sys_var *, THD *, enum_var_type) {
  bool res;

  if (!opt_general_logname)  // SET ... = DEFAULT
  {
    char buff[FN_REFLEN];
    opt_general_logname = my_strdup(
        key_memory_LOG_name, make_query_log_name(buff, QUERY_LOG_GENERAL),
        MYF(MY_FAE + MY_WME));
    if (!opt_general_logname) return true;
  }

  res = query_logger.set_log_file(QUERY_LOG_GENERAL);

  if (opt_general_log) {
    mysql_mutex_unlock(&LOCK_global_system_variables);

    if (!res)
      res = query_logger.reopen_log_file(QUERY_LOG_GENERAL);
    else
      query_logger.deactivate_log_handler(QUERY_LOG_GENERAL);

    mysql_mutex_lock(&LOCK_global_system_variables);
  }

  if (res) opt_general_log = false;

  return res;
}

static Sys_var_charptr Sys_general_log_path(
    "general_log_file", "Log connections and queries to given file",
    GLOBAL_VAR(opt_general_logname), CMD_LINE(REQUIRED_ARG), IN_FS_CHARSET,
    DEFAULT(nullptr), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_log_path),
    ON_UPDATE(fix_general_log_file));

static bool fix_slow_log_file(sys_var *, THD *thd [[maybe_unused]],
                              enum_var_type) {
  bool res;

  DEBUG_SYNC(thd, "log_fix_slow_log_holds_sysvar_lock");

  if (!opt_slow_logname)  // SET ... = DEFAULT
  {
    char buff[FN_REFLEN];
    opt_slow_logname = my_strdup(key_memory_LOG_name,
                                 make_query_log_name(buff, QUERY_LOG_SLOW),
                                 MYF(MY_FAE + MY_WME));
    if (!opt_slow_logname) return true;
  }

  res = query_logger.set_log_file(QUERY_LOG_SLOW);

  DEBUG_SYNC(thd, "log_fix_slow_log_released_logger_lock");

  if (opt_slow_log) {
    mysql_mutex_unlock(&LOCK_global_system_variables);

    DEBUG_SYNC(thd, "log_fix_slow_log_released_sysvar_lock");

    if (!res)
      res = query_logger.reopen_log_file(QUERY_LOG_SLOW);
    else
      query_logger.deactivate_log_handler(QUERY_LOG_SLOW);

    mysql_mutex_lock(&LOCK_global_system_variables);
  }

  if (res) opt_slow_log = false;

  return res;
}
static Sys_var_charptr Sys_slow_log_path(
    "slow_query_log_file",
    "Log slow queries to given log file. "
    "Defaults logging to hostname-slow.log. Must be enabled to activate "
    "other slow log options",
    GLOBAL_VAR(opt_slow_logname), CMD_LINE(REQUIRED_ARG), IN_FS_CHARSET,
    DEFAULT(nullptr), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_log_path),
    ON_UPDATE(fix_slow_log_file));

static Sys_var_have Sys_have_compress(
    "have_compress", "have_compress",
    READ_ONLY NON_PERSIST GLOBAL_VAR(have_compress), NO_CMD_LINE);

static Sys_var_have Sys_have_dlopen(
    "have_dynamic_loading", "have_dynamic_loading",
    READ_ONLY NON_PERSIST GLOBAL_VAR(have_dlopen), NO_CMD_LINE);

static Sys_var_have Sys_have_geometry(
    "have_geometry", "have_geometry",
    READ_ONLY NON_PERSIST GLOBAL_VAR(have_geometry), NO_CMD_LINE);

static SHOW_COMP_OPTION have_ssl_func(THD *thd [[maybe_unused]]) {
  return have_ssl() ? SHOW_OPTION_YES : SHOW_OPTION_DISABLED;
}

enum SHOW_COMP_OPTION Sys_var_have_func::dummy_;

static Sys_var_have_func Sys_have_openssl("have_openssl", "have_openssl",
                                          have_ssl_func, DEPRECATED_VAR(""));

static Sys_var_have Sys_have_profiling(
    "have_profiling", "have_profiling",
    READ_ONLY NON_PERSIST GLOBAL_VAR(have_profiling), NO_CMD_LINE,
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static Sys_var_have Sys_have_query_cache(
    "have_query_cache",
    "have_query_cache. "
    "This variable is deprecated and will be removed in a future release.",
    READ_ONLY NON_PERSIST GLOBAL_VAR(have_query_cache), NO_CMD_LINE,
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static Sys_var_have Sys_have_rtree_keys(
    "have_rtree_keys", "have_rtree_keys",
    READ_ONLY NON_PERSIST GLOBAL_VAR(have_rtree_keys), NO_CMD_LINE);

static Sys_var_have_func Sys_have_ssl(
    "have_ssl", "have_ssl", have_ssl_func,
    DEPRECATED_VAR("performance_schema.tls_channel_status table"));

static Sys_var_have Sys_have_symlink(
    "have_symlink", "have_symlink",
    READ_ONLY NON_PERSIST GLOBAL_VAR(have_symlink), NO_CMD_LINE);

static Sys_var_have Sys_have_statement_timeout(
    "have_statement_timeout", "have_statement_timeout",
    READ_ONLY NON_PERSIST GLOBAL_VAR(have_statement_timeout), NO_CMD_LINE);

static bool fix_general_log_state(sys_var *, THD *thd, enum_var_type) {
  bool new_state = opt_general_log, res = false;

  if (query_logger.is_log_file_enabled(QUERY_LOG_GENERAL) == new_state)
    return false;

  mysql_mutex_unlock(&LOCK_global_system_variables);

  if (!new_state) {
    query_logger.deactivate_log_handler(QUERY_LOG_GENERAL);
  } else {
    res = query_logger.activate_log_handler(thd, QUERY_LOG_GENERAL);
  }

  mysql_mutex_lock(&LOCK_global_system_variables);

  if (res) opt_general_log = false;

  return res;
}
static Sys_var_bool Sys_general_log(
    "general_log",
    "Log connections and queries to a table or log file. "
    "Defaults to logging to a file hostname.log, "
    "or if --log-output=TABLE is used, to a table mysql.general_log.",
    GLOBAL_VAR(opt_general_log), CMD_LINE(OPT_ARG), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_general_log_state));

static Sys_var_bool Sys_log_raw(
    "log_raw",
    "Log to general log before any rewriting of the query. For use in "
    "debugging, not production as sensitive information may be logged.",
    GLOBAL_VAR(opt_general_log_raw), CMD_LINE(OPT_ARG), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG);

static bool fix_slow_log_state(sys_var *, THD *thd, enum_var_type) {
  bool new_state = opt_slow_log, res = false;

  if (query_logger.is_log_file_enabled(QUERY_LOG_SLOW) == new_state)
    return false;

  mysql_mutex_unlock(&LOCK_global_system_variables);

  if (!new_state) {
    query_logger.deactivate_log_handler(QUERY_LOG_SLOW);
  } else {
    res = query_logger.activate_log_handler(thd, QUERY_LOG_SLOW);
  }

  mysql_mutex_lock(&LOCK_global_system_variables);

  if (res) opt_slow_log = false;

  return res;
}
static Sys_var_bool Sys_slow_query_log(
    "slow_query_log",
    "Log slow queries to a table or log file. Defaults logging to a file "
    "hostname-slow.log or a table mysql.slow_log if --log-output=TABLE is "
    "used. Must be enabled to activate other slow log options",
    GLOBAL_VAR(opt_slow_log), CMD_LINE(OPT_ARG), DEFAULT(false), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(fix_slow_log_state));

static bool check_slow_log_extra(sys_var *, THD *thd, set_var *) {
  // If FILE is not one of the log-targets, succeed but warn!
  if (!(log_output_options & LOG_FILE))
    push_warning(
        thd, Sql_condition::SL_WARNING,
        ER_SLOW_LOG_MODE_IGNORED_WHEN_NOT_LOGGING_TO_FILE,
        ER_THD(thd, ER_SLOW_LOG_MODE_IGNORED_WHEN_NOT_LOGGING_TO_FILE));

  return false;
}

static Sys_var_bool Sys_slow_log_extra(
    "log_slow_extra",
    "Print more attributes to the slow query log file. Has no effect on "
    "logging to table.",
    GLOBAL_VAR(opt_log_slow_extra), CMD_LINE(OPT_ARG), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_slow_log_extra),
    ON_UPDATE(nullptr));

static bool check_not_empty_set(sys_var *, THD *, set_var *var) {
  return var->save_result.ulonglong_value == 0;
}
static bool fix_log_output(sys_var *, THD *, enum_var_type) {
  query_logger.set_handlers(static_cast<uint>(log_output_options));
  return false;
}

static const char *log_output_names[] = {"NONE", "FILE", "TABLE", nullptr};

static Sys_var_set Sys_log_output(
    "log_output",
    "Syntax: log-output=value[,value...], "
    "where \"value\" could be TABLE, FILE or NONE",
    GLOBAL_VAR(log_output_options), CMD_LINE(REQUIRED_ARG), log_output_names,
    DEFAULT(LOG_FILE), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_not_empty_set), ON_UPDATE(fix_log_output));

static Sys_var_bool Sys_log_replica_updates(
    "log_replica_updates",
    "If enabled, the replication applier threads will write to this server's "
    "binary log.",
    READ_ONLY GLOBAL_VAR(opt_log_replica_updates),
    CMD_LINE(OPT_ARG, OPT_LOG_REPLICA_UPDATES), DEFAULT(1));

static Sys_var_deprecated_alias Sys_log_slave_updates("log_slave_updates",
                                                      Sys_log_replica_updates);

static Sys_var_charptr Sys_relay_log(
    "relay_log", "The location and name to use for relay logs",
    READ_ONLY NON_PERSIST GLOBAL_VAR(opt_relay_logname), CMD_LINE(REQUIRED_ARG),
    IN_FS_CHARSET, DEFAULT(nullptr));

/*
  Uses NO_CMD_LINE since the --relay-log-index option set
  opt_relaylog_index_name variable and computes a value for the
  relay_log_index variable.
*/
static Sys_var_charptr Sys_relay_log_index(
    "relay_log_index",
    "The location and name to use for the file "
    "that keeps a list of the last relay logs",
    READ_ONLY NON_PERSIST GLOBAL_VAR(relay_log_index), NO_CMD_LINE,
    IN_FS_CHARSET, DEFAULT(nullptr));

/*
  Uses NO_CMD_LINE since the --log-bin-index option set
  opt_binlog_index_name variable and computes a value for the
  log_bin_index variable.
*/
static Sys_var_charptr Sys_binlog_index(
    "log_bin_index", "File that holds the names for last binary log files.",
    READ_ONLY NON_PERSIST GLOBAL_VAR(log_bin_index), NO_CMD_LINE, IN_FS_CHARSET,
    DEFAULT(nullptr));

static Sys_var_charptr Sys_relay_log_basename(
    "relay_log_basename",
    "The full path of the relay log file names, excluding the extension.",
    READ_ONLY NON_PERSIST GLOBAL_VAR(relay_log_basename), NO_CMD_LINE,
    IN_FS_CHARSET, DEFAULT(nullptr));

static Sys_var_charptr Sys_log_bin_basename(
    "log_bin_basename",
    "The full path of the binary log file names, excluding the extension.",
    READ_ONLY NON_PERSIST GLOBAL_VAR(log_bin_basename), NO_CMD_LINE,
    IN_FS_CHARSET, DEFAULT(nullptr));

static Sys_var_charptr Sys_relay_log_info_file(
    "relay_log_info_file",
    "The location and name of the file that "
    "remembers where the SQL replication thread is in the relay logs",
    READ_ONLY NON_PERSIST GLOBAL_VAR(relay_log_info_file),
    CMD_LINE(REQUIRED_ARG, OPT_RELAY_LOG_INFO_FILE), IN_FS_CHARSET,
    DEFAULT(nullptr), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(nullptr), DEPRECATED_VAR(""));

static Sys_var_bool Sys_relay_log_purge(
    "relay_log_purge",
    "if disabled - do not purge relay logs. "
    "if enabled - purge them as soon as they are no more needed",
    GLOBAL_VAR(relay_log_purge), CMD_LINE(OPT_ARG), DEFAULT(true));

static Sys_var_bool Sys_relay_log_recovery(
    "relay_log_recovery",
    "If enabled, existing relay logs will be skipped by the "
    "replication threads. The receiver will start a new relay "
    "log and the applier will start reading from the beginning of that file. "
    "The receiver's position relative to the source will be reset to the "
    "applier's "
    "position relative to the source; the receiver uses this in case "
    "SOURCE_AUTO_POSITION=0.",
    READ_ONLY GLOBAL_VAR(relay_log_recovery), CMD_LINE(OPT_ARG),
    DEFAULT(false));

static Sys_var_ulong Sys_rpl_read_size(
    "rpl_read_size",
    "The size for reads done from the binlog and relay log. "
    "It must be a multiple of 4kb. Making it larger might help with IO "
    "stalls while reading these files when they are not in the OS buffer "
    "cache",
    GLOBAL_VAR(rpl_read_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(IO_SIZE * 2, ULONG_MAX), DEFAULT(IO_SIZE * 2),
    BLOCK_SIZE(IO_SIZE));

static Sys_var_bool Sys_replica_allow_batching(
    "replica_allow_batching",
    "Allow this replica to batch requests when "
    "using the NDB storage engine.",
    GLOBAL_VAR(opt_replica_allow_batching), CMD_LINE(OPT_ARG), DEFAULT(true));

static Sys_var_deprecated_alias Sys_slave_allow_batching(
    "slave_allow_batching", Sys_replica_allow_batching);

static Sys_var_charptr Sys_replica_load_tmpdir(
    "replica_load_tmpdir",
    "The location where this replica will store temporary files when "
    "replicating a LOAD DATA INFILE command from a source having "
    "binlog_format=STATEMENT.",
    READ_ONLY NON_PERSIST GLOBAL_VAR(replica_load_tmpdir),
    CMD_LINE(REQUIRED_ARG), IN_FS_CHARSET, DEFAULT(nullptr));

static Sys_var_deprecated_alias Sys_slave_load_tmpdir("slave_load_tmpdir",
                                                      Sys_replica_load_tmpdir);

static bool fix_replica_net_timeout(sys_var *, THD *thd, enum_var_type) {
  DEBUG_SYNC(thd, "fix_replica_net_timeout");
  Master_info *mi;

  /* @TODO: slave net timeout is for all channels, but does this make
           sense?
   */

  /*
   Here we have lock on LOCK_global_system_variables and we need
    lock on channel_map lock. In START_SLAVE handler, we take these
    two locks in different order. This can lead to DEADLOCKs. See
    BUG#14236151 for more details.
   So we release lock on LOCK_global_system_variables before acquiring
    lock on channel_map lock. But this could lead to isolation issues
    between multiple setters. Hence introducing secondary guard
    for this global variable and releasing the lock here and acquiring
    locks back again at the end of this function.
   */
  mysql_mutex_unlock(&LOCK_replica_net_timeout);
  mysql_mutex_unlock(&LOCK_global_system_variables);
  channel_map.wrlock();

  for (mi_map::iterator it = channel_map.begin(); it != channel_map.end();
       it++) {
    mi = it->second;

    DBUG_PRINT("info",
               ("replica_net_timeout=%u mi->heartbeat_period=%.3f",
                replica_net_timeout, (mi ? mi->heartbeat_period : 0.0)));
    if (mi != nullptr && replica_net_timeout < mi->heartbeat_period)
      push_warning(thd, Sql_condition::SL_WARNING,
                   ER_REPLICA_HEARTBEAT_VALUE_OUT_OF_RANGE_MAX,
                   ER_THD(thd, ER_REPLICA_HEARTBEAT_VALUE_OUT_OF_RANGE_MAX));
  }

  channel_map.unlock();
  mysql_mutex_lock(&LOCK_global_system_variables);
  mysql_mutex_lock(&LOCK_replica_net_timeout);
  return false;
}
static PolyLock_mutex PLock_replica_net_timeout(&LOCK_replica_net_timeout);
static Sys_var_uint Sys_replica_net_timeout(
    "replica_net_timeout",
    "Number of seconds to wait for more data "
    "from a replication connection before aborting the read.",
    GLOBAL_VAR(replica_net_timeout), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1, LONG_TIMEOUT), DEFAULT(REPLICA_NET_TIMEOUT), BLOCK_SIZE(1),
    &PLock_replica_net_timeout, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_replica_net_timeout));

static Sys_var_deprecated_alias Sys_slave_net_timeout("slave_net_timeout",
                                                      Sys_replica_net_timeout);

static bool check_slave_skip_counter(sys_var *, THD *thd, set_var *var) {
  /*
    @todo: move this check into the set function and hold the lock on
    Gtid_mode::lock until the operation has completed, so that we are
    sure a concurrent connection does not change gtid_mode between
    check and fix.
  */
  if (global_gtid_mode.get() == Gtid_mode::ON &&
      var->save_result.ulonglong_value > 0)
    push_warning(
        thd, Sql_condition::SL_WARNING,
        ER_SQL_REPLICA_SKIP_COUNTER_USED_WITH_GTID_MODE_ON,
        ER_THD(thd, ER_SQL_REPLICA_SKIP_COUNTER_USED_WITH_GTID_MODE_ON));
  return false;
}

static PolyLock_mutex PLock_sql_replica_skip_counter(
    &LOCK_sql_replica_skip_counter);
static Sys_var_uint Sys_sql_replica_skip_counter(
    "sql_replica_skip_counter", "sql_replica_skip_counter",
    GLOBAL_VAR(sql_replica_skip_counter), NO_CMD_LINE, VALID_RANGE(0, UINT_MAX),
    DEFAULT(0), BLOCK_SIZE(1), &PLock_sql_replica_skip_counter, NOT_IN_BINLOG,
    ON_CHECK(check_slave_skip_counter));

static Sys_var_deprecated_alias Sys_sql_slave_skip_counter(
    "sql_slave_skip_counter", Sys_sql_replica_skip_counter);

static Sys_var_charptr Sys_replica_skip_errors(
    "replica_skip_errors",
    "Comma-separated list of error numbers. If an applier thread on this "
    "replica encounters one of these errors while applying a Query_log_event, "
    "it will ignore the error, rather than stop.",
    READ_ONLY GLOBAL_VAR(opt_replica_skip_errors), CMD_LINE(REQUIRED_ARG),
    IN_SYSTEM_CHARSET, DEFAULT(nullptr));

static Sys_var_deprecated_alias Sys_slave_skip_errors("slave_skip_errors",
                                                      Sys_replica_skip_errors);

static Sys_var_ulonglong Sys_relay_log_space_limit(
    "relay_log_space_limit", "Maximum space to use for all relay logs",
    READ_ONLY GLOBAL_VAR(relay_log_space_limit), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, ULLONG_MAX), DEFAULT(0), BLOCK_SIZE(1));

static Sys_var_uint Sys_sync_relaylog_period(
    "sync_relay_log",
    "Synchronously flush relay log to disk after "
    "every #th event. Use 0 to disable synchronous flushing",
    GLOBAL_VAR(sync_relaylog_period), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, UINT_MAX), DEFAULT(10000), BLOCK_SIZE(1));

static Sys_var_uint Sys_sync_relayloginfo_period(
    "sync_relay_log_info",
    "Synchronously flush relay log info "
    "to disk after every #th transaction. Use 0 to disable "
    "synchronous flushing. This variable is deprecated and will be removed in "
    "a future version.",
    GLOBAL_VAR(sync_relayloginfo_period),
    CMD_LINE(REQUIRED_ARG, OPT_SYNC_RELAY_LOG_INFO), VALID_RANGE(0, UINT_MAX),
    DEFAULT(10000), BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(nullptr), ON_UPDATE(nullptr), DEPRECATED_VAR(""));

static Sys_var_uint Sys_replica_checkpoint_period(
    "replica_checkpoint_period",
    "When using a multi-threaded applier (replica_parallel_workers>0), it "
    "will update the worker progress status periodically. This option "
    "specifies the maximum number of milliseconds between updates.",
    GLOBAL_VAR(opt_mta_checkpoint_period), CMD_LINE(REQUIRED_ARG),
#ifndef NDEBUG
    VALID_RANGE(0, UINT_MAX), DEFAULT(300), BLOCK_SIZE(1));
#else
    VALID_RANGE(1, UINT_MAX), DEFAULT(300), BLOCK_SIZE(1));
#endif /* NDEBUG */

static Sys_var_deprecated_alias Sys_slave_checkpoint_period(
    "slave_checkpoint_period", Sys_replica_checkpoint_period);

static Sys_var_uint Sys_replica_checkpoint_group(
    "replica_checkpoint_group",
    "When using multi-threaded applier (replica_parallel_workers>0), it will "
    "update the worker progress status periodically. This option specifies "
    "the maximum number of committed transactions between updates.",
    GLOBAL_VAR(opt_mta_checkpoint_group), CMD_LINE(REQUIRED_ARG),
#ifndef NDEBUG
    VALID_RANGE(1, MTS_MAX_BITS_IN_GROUP), DEFAULT(512), BLOCK_SIZE(1));
#else
    VALID_RANGE(32, MTS_MAX_BITS_IN_GROUP), DEFAULT(512), BLOCK_SIZE(8));
#endif /* NDEBUG */

static Sys_var_deprecated_alias Sys_slave_checkpoint_group(
    "slave_checkpoint_group", Sys_replica_checkpoint_group);

static Sys_var_uint Sys_sync_binlog_period(
    "sync_binlog",
    "Synchronously flush binary log to disk after"
    " every #th write to the file. Use 0 to disable synchronous"
    " flushing",
    GLOBAL_VAR(sync_binlog_period), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, UINT_MAX), DEFAULT(1), BLOCK_SIZE(1));

static Sys_var_uint Sys_sync_source_info(
    "sync_source_info",
    "Synchronize replication receiver positions to disk periodically, after "
    "the specified number of events. Use 0 to disable periodic "
    "synchronization.",
    GLOBAL_VAR(sync_masterinfo_period), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, UINT_MAX), DEFAULT(10000), BLOCK_SIZE(1));

static Sys_var_deprecated_alias Sys_sync_master_info("sync_master_info",
                                                     Sys_sync_source_info);

static Sys_var_ulonglong Sys_var_original_commit_timestamp(
    "original_commit_timestamp",
    "The time when the current transaction was committed on the originating "
    "source, measured in microseconds since 1970 (the \"epoch\").",
    SESSION_ONLY(original_commit_timestamp), NO_CMD_LINE,
    VALID_RANGE(0, MAX_COMMIT_TIMESTAMP_VALUE),
    DEFAULT(MAX_COMMIT_TIMESTAMP_VALUE), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    IN_BINLOG, ON_CHECK(check_session_admin_or_replication_applier));

static Sys_var_ulong Sys_replica_transaction_retries(
    "replica_transaction_retries",
    "Number of times the replication applier will retry a transaction in "
    "case it failed with a deadlock or other transient error, before it gives "
    "up and stops.",
    GLOBAL_VAR(slave_trans_retries), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, ULONG_MAX), DEFAULT(10), BLOCK_SIZE(1));

static Sys_var_deprecated_alias Sys_slave_transaction_retries(
    "slave_transaction_retries", Sys_replica_transaction_retries);

static Sys_var_ulong Sys_replica_parallel_workers(
    "replica_parallel_workers",
    "Number of worker threads for executing events in parallel ",
    PERSIST_AS_READONLY GLOBAL_VAR(opt_mts_replica_parallel_workers),
    CMD_LINE(REQUIRED_ARG, OPT_REPLICA_PARALLEL_WORKERS),
    VALID_RANGE(0, MTS_MAX_WORKERS), DEFAULT(4), BLOCK_SIZE(1), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(replica_parallel_workers_update));

static Sys_var_deprecated_alias Sys_slave_parallel_workers(
    "slave_parallel_workers", Sys_replica_parallel_workers);

static Sys_var_ulonglong Sys_replica_pending_jobs_size_max(
    "replica_pending_jobs_size_max",
    "Soft limit on the size, in bytes, of per-worker queues of events that "
    "have not yet been applied. The queue size may exceed this limit in case "
    "a single event is bigger than the limit.",
    GLOBAL_VAR(opt_mts_pending_jobs_size_max), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(1024, (ulonglong) ~(intptr)0), DEFAULT(128 * 1024 * 1024),
    BLOCK_SIZE(1024), ON_CHECK(nullptr));

static Sys_var_deprecated_alias Sys_slave_pending_jobs_size_max(
    "slave_pending_jobs_size_max", Sys_replica_pending_jobs_size_max);

static bool check_locale(sys_var *self, THD *thd, set_var *var) {
  if (!var->value) return false;

  MY_LOCALE *locale;
  char buff[STRING_BUFFER_USUAL_SIZE];
  if (var->value->result_type() == INT_RESULT) {
    int lcno = (int)var->value->val_int();
    if (!(locale = my_locale_by_number(lcno))) {
      my_error(ER_UNKNOWN_LOCALE, MYF(0), llstr(lcno, buff));
      return true;
    }
    if (check_not_null(self, thd, var)) return true;
  } else  // STRING_RESULT
  {
    String str(buff, sizeof(buff), system_charset_info), *res;
    if (!(res = var->value->val_str(&str)))
      return true;
    else if (!(locale = my_locale_by_name(thd, res->ptr(), res->length()))) {
      ErrConvString err(res);
      my_error(ER_UNKNOWN_LOCALE, MYF(0), err.ptr());
      return true;
    }
  }

  var->save_result.ptr = locale;

  if (!locale->errmsgs->is_loaded()) {
    mysql_mutex_lock(&LOCK_error_messages);
    if (!locale->errmsgs->is_loaded() && locale->errmsgs->read_texts()) {
      push_warning_printf(thd, Sql_condition::SL_WARNING, ER_UNKNOWN_ERROR,
                          "Can't process error message file for locale '%s'",
                          locale->name);
      mysql_mutex_unlock(&LOCK_error_messages);
      return true;
    }
    mysql_mutex_unlock(&LOCK_error_messages);
  }
  return false;
}

namespace {
struct Get_locale_name {
  explicit Get_locale_name(const MY_LOCALE *ml) : m_ml(ml) {}
  const uchar *get_name() const {
    return pointer_cast<const uchar *>(m_ml->name);
  }
  const MY_LOCALE *m_ml;
};
}  // namespace

static Sys_var_struct<MY_LOCALE, Get_locale_name> Sys_lc_messages(
    "lc_messages", "Set the language used for the error messages",
    SESSION_VAR(lc_messages), NO_CMD_LINE, DEFAULT(&my_default_lc_messages),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_locale));

static Sys_var_struct<MY_LOCALE, Get_locale_name> Sys_lc_time_names(
    "lc_time_names",
    "Set the language used for the month "
    "names and the days of the week",
    SESSION_VAR(lc_time_names), NO_CMD_LINE, DEFAULT(&my_default_lc_time_names),
    NO_MUTEX_GUARD, IN_BINLOG, ON_CHECK(check_locale));

static Sys_var_tz Sys_time_zone("time_zone", "time_zone",
                                HINT_UPDATEABLE SESSION_VAR(time_zone),
                                NO_CMD_LINE, DEFAULT(&default_tz),
                                NO_MUTEX_GUARD, IN_BINLOG);

static bool fix_host_cache_size(sys_var *, THD *, enum_var_type) {
  hostname_cache_resize(host_cache_size);
  return false;
}

static Sys_var_uint Sys_host_cache_size(
    "host_cache_size",
    "How many host names should be cached to avoid resolving.",
    GLOBAL_VAR(host_cache_size), CMD_LINE(REQUIRED_ARG, OPT_HOST_CACHE_SIZE),
    VALID_RANGE(0, 65536), DEFAULT(HOST_CACHE_SIZE), BLOCK_SIZE(1),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(fix_host_cache_size));

const Sys_var_multi_enum::ALIAS enforce_gtid_consistency_aliases[] = {
    {"OFF", 0},   {"ON", 1},   {"WARN", 2},
    {"FALSE", 0}, {"TRUE", 1}, {nullptr, 0}};
static Sys_var_enforce_gtid_consistency Sys_enforce_gtid_consistency(
    "enforce_gtid_consistency",
    "Prevents execution of statements that would be impossible to log "
    "in a transactionally safe manner. Currently, the disallowed "
    "statements include CREATE TEMPORARY TABLE inside transactions, "
    "all updates to non-transactional tables, and CREATE TABLE ... SELECT.",
    PERSIST_AS_READONLY GLOBAL_VAR(_gtid_consistency_mode),
    CMD_LINE(OPT_ARG, OPT_ENFORCE_GTID_CONSISTENCY),
    enforce_gtid_consistency_aliases, 3,
    DEFAULT(3 /*position of "FALSE" in enforce_gtid_consistency_aliases*/),
    DEFAULT(GTID_CONSISTENCY_MODE_ON), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_session_admin_outside_trx_outside_sf_outside_sp));
const char *fixup_enforce_gtid_consistency_command_line(char *value_arg) {
  return Sys_enforce_gtid_consistency.fixup_command_line(value_arg);
}

static Sys_var_bool Sys_binlog_gtid_simple_recovery(
    "binlog_gtid_simple_recovery",
    "If this option is enabled, the server does not open more than "
    "two binary logs when initializing GTID_PURGED and "
    "GTID_EXECUTED, either during server restart or when binary "
    "logs are being purged. Enabling this option is useful when "
    "the server has already generated many binary logs without "
    "GTID events (e.g., having GTID_MODE = OFF). Note: If this "
    "option is enabled, GLOBAL.GTID_EXECUTED and "
    "GLOBAL.GTID_PURGED may be initialized wrongly in two cases: "
    "(1) All binary logs were generated by MySQL 5.7.5 or older, "
    "and GTID_MODE was ON for some binary logs but OFF for the "
    "newest binary log. (2) The oldest existing binary log was "
    "generated by MySQL 5.7.5 or older, and SET GTID_PURGED was "
    "issued after the oldest binary log was generated. If a wrong "
    "set is computed in one of case (1) or case (2), it will "
    "remain wrong even if the server is later restarted with this "
    "option disabled.",
    READ_ONLY GLOBAL_VAR(binlog_gtid_simple_recovery), CMD_LINE(OPT_ARG),
    DEFAULT(true));

static Sys_var_ulong Sys_sp_cache_size(
    "stored_program_cache",
    "The soft upper limit for number of cached stored routines for "
    "one connection.",
    GLOBAL_VAR(stored_program_cache_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(16, 512 * 1024), DEFAULT(256), BLOCK_SIZE(1));

static bool check_pseudo_replica_mode(sys_var *self, THD *thd, set_var *var) {
  if (check_session_admin_or_replication_applier(self, thd, var)) return true;
  if (check_outside_trx(self, thd, var)) return true;
  longlong previous_val = thd->variables.pseudo_replica_mode;
  longlong val = (longlong)var->save_result.ulonglong_value;
  bool rli_fake = false;

  rli_fake = thd->rli_fake ? true : false;

  if (rli_fake) {
    if (!val) {
      thd->rli_fake->end_info();
      delete thd->rli_fake;
      thd->rli_fake = nullptr;
    } else if (previous_val && val)
      goto ineffective;
    else if (!previous_val && val)
      push_warning(thd, Sql_condition::SL_WARNING, ER_WRONG_VALUE_FOR_VAR,
                   "'pseudo_replica_mode' is already ON.");
  } else {
    if (!previous_val && !val)
      goto ineffective;
    else if (previous_val && !val)
      push_warning(thd, Sql_condition::SL_WARNING, ER_WRONG_VALUE_FOR_VAR,
                   "Replica applier execution mode not active, "
                   "statement ineffective.");
  }
  goto end;

ineffective:
  push_warning(thd, Sql_condition::SL_WARNING, ER_WRONG_VALUE_FOR_VAR,
               "'pseudo_replica_mode' change was ineffective.");

end:
  return false;
}
static Sys_var_bool Sys_pseudo_replica_mode(
    "pseudo_replica_mode",
    "Internal variable that will be enabled while applying a "
    "Format_description_log_event encoded in a BINLOG statement printed "
    "by mysqlbinlog.",
    SESSION_ONLY(pseudo_replica_mode), NO_CMD_LINE, DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_pseudo_replica_mode));

static Sys_var_deprecated_alias Sys_pseudo_slave_mode("pseudo_slave_mode",
                                                      Sys_pseudo_replica_mode);

#ifdef HAVE_GTID_NEXT_LIST
static bool check_gtid_next_list(sys_var *self, THD *thd, set_var *var) {
  DBUG_TRACE;
  my_error(ER_NOT_SUPPORTED_YET, MYF(0), "GTID_NEXT_LIST");
  if (check_session_admin_outside_trx_outside_sf_outside_sp(self, thd, var))
    return true;
  /*
    @todo: move this check into the set function and hold the lock on
    Gtid_mode::lock until the operation has completed, so that we are
    sure a concurrent connection does not change gtid_mode between
    check and fix - if we ever implement this variable.
  */
  if (global_gtid_mode.get() == Gtid_mode::OFF &&
      var->save_result.string_value.str != NULL)
    my_error(ER_CANT_SET_GTID_NEXT_LIST_TO_NON_NULL_WHEN_GTID_MODE_IS_OFF,
             MYF(0));
  return false;
}

static bool update_gtid_next_list(sys_var *self, THD *thd, enum_var_type type) {
  assert(type == OPT_SESSION);
  if (thd->get_gtid_next_list() != NULL)
    return gtid_acquire_ownership_multiple(thd) != 0 ? true : false;
  return false;
}

static Sys_var_gtid_set Sys_gtid_next_list(
    "gtid_next_list",
    "Before re-executing a transaction that contains multiple "
    "Global Transaction Identifiers, this variable must be set "
    "to the set of all re-executed transactions.",
    SESSION_ONLY(gtid_next_list), NO_CMD_LINE, DEFAULT(NULL), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_gtid_next_list),
    ON_UPDATE(update_gtid_next_list));
export sys_var *Sys_gtid_next_list_ptr = &Sys_gtid_next_list;
#endif  // HAVE_GTID_NEXT_LIST

static Sys_var_gtid_next Sys_gtid_next(
    "gtid_next",
    "Specifies the Global Transaction Identifier for the following "
    "transaction.",
    SESSION_ONLY(gtid_next), NO_CMD_LINE, DEFAULT("AUTOMATIC"), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_gtid_next));
export sys_var *Sys_gtid_next_ptr = &Sys_gtid_next;

static Sys_var_gtid_executed Sys_gtid_executed(
    "gtid_executed",
    "The global variable contains the set of GTIDs in the "
    "binary log. The session variable contains the set of GTIDs "
    "in the current, ongoing transaction.");

static bool check_gtid_purged(sys_var *self, THD *thd, set_var *var) {
  DBUG_TRACE;

  /*
    GTID_PURGED must not be set / updated when GR is running (it goes against
    the whole purpose of update everywhere replication).
  */
  if (is_group_replication_running()) {
    my_error(ER_UPDATE_GTID_PURGED_WITH_GR, MYF(0));
    return true;
  }

  if (!var->value || check_session_admin_outside_trx_outside_sf(self, thd, var))
    return true;

  if (var->value->result_type() != STRING_RESULT ||
      !var->save_result.string_value.str)
    return true;

  return false;
}

bool Sys_var_gtid_purged::global_update(THD *thd, set_var *var) {
  DBUG_TRACE;
  bool error = false;
  bool gtid_threshold_breach = false;

  global_sid_lock->wrlock();

  /*
    ensures the commit of the transaction started when saving the
    purged gtid set in the table
  */
  thd->lex->autocommit = true;

  /*
    SET GITD_PURGED command should ignore 'read-only' and 'super_read_only'
    options so that it can update 'mysql.gtid_executed' replication repository
    table.
  */
  thd->set_skip_readonly_check();
  char *previous_gtid_executed = nullptr, *previous_gtid_purged = nullptr,
       *current_gtid_executed = nullptr, *current_gtid_purged = nullptr;
  gtid_state->get_executed_gtids()->to_string(&previous_gtid_executed);
  gtid_state->get_lost_gtids()->to_string(&previous_gtid_purged);
  Gtid_set gtid_set(global_sid_map, global_sid_lock);
  bool starts_with_plus = false;
  enum_return_status ret = gtid_set.add_gtid_text(
      var->save_result.string_value.str, nullptr, &starts_with_plus);

  if (ret != RETURN_STATUS_OK) {
    error = true;
    goto end;
  }
  ret = gtid_state->add_lost_gtids(&gtid_set, starts_with_plus);
  if (ret != RETURN_STATUS_OK) {
    error = true;
    goto end;
  }
  gtid_state->get_executed_gtids()->to_string(&current_gtid_executed);
  gtid_state->get_lost_gtids()->to_string(&current_gtid_purged);

  gtid_threshold_breach =
      (gtid_state->get_executed_gtids()->get_gtid_count(
           gtid_state->get_server_sidno()) > GNO_WARNING_THRESHOLD);

  // Log messages saying that GTID_PURGED and GTID_EXECUTED were changed.
  LogErr(SYSTEM_LEVEL, ER_GTID_PURGED_WAS_UPDATED, previous_gtid_purged,
         current_gtid_purged);
  LogErr(SYSTEM_LEVEL, ER_GTID_EXECUTED_WAS_UPDATED, previous_gtid_executed,
         current_gtid_executed);

end:
  global_sid_lock->unlock();
  my_free(previous_gtid_executed);
  my_free(previous_gtid_purged);
  my_free(current_gtid_executed);
  my_free(current_gtid_purged);

  if (gtid_threshold_breach)
    LogErr(WARNING_LEVEL, ER_WARN_GTID_THRESHOLD_BREACH);

  return error;
}

Gtid_set *gtid_purged;
static Sys_var_gtid_purged Sys_gtid_purged(
    "gtid_purged",
    "The set of GTIDs that existed in previous, purged binary logs.",
    NON_PERSIST GLOBAL_VAR(gtid_purged), NO_CMD_LINE, DEFAULT(nullptr),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_gtid_purged));
export sys_var *Sys_gtid_purged_ptr = &Sys_gtid_purged;

static Sys_var_gtid_owned Sys_gtid_owned(
    "gtid_owned",
    "The global variable lists all GTIDs owned by all threads. "
    "The session variable lists all GTIDs owned by the current thread.");

static Sys_var_gtid_mode Sys_gtid_mode(
    "gtid_mode",
    "Controls whether Global Transaction Identifiers (GTIDs) are "
    "enabled. Can be OFF, OFF_PERMISSIVE, ON_PERMISSIVE, or ON. OFF "
    "means that no transaction has a GTID. OFF_PERMISSIVE means that "
    "new transactions (committed in a client session using "
    "GTID_NEXT='AUTOMATIC') are not assigned any GTID, and "
    "replicated transactions are allowed to have or not have a "
    "GTID. ON_PERMISSIVE means that new transactions are assigned a "
    "GTID, and replicated transactions are allowed to have or not "
    "have a GTID. ON means that all transactions have a GTID. "
    "ON is required on a source before any replica can use "
    "SOURCE_AUTO_POSITION=1. To safely switch from OFF to ON, first "
    "set all servers to OFF_PERMISSIVE, then set all servers to "
    "ON_PERMISSIVE, then wait for all transactions without a GTID to "
    "be replicated and executed on all servers, and finally set all "
    "servers to GTID_MODE = ON.",
    PERSIST_AS_READONLY GLOBAL_VAR(Gtid_mode::sysvar_mode),
    CMD_LINE(REQUIRED_ARG), Gtid_mode::names, DEFAULT(Gtid_mode::DEFAULT),
    NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_session_admin_outside_trx_outside_sf_outside_sp));

static Sys_var_uint Sys_gtid_executed_compression_period(
    "gtid_executed_compression_period",
    "Compress the mysql.gtid_executed table whenever this number of "
    "transactions have been added, by waking up a foreground thread "
    "(compress_gtid_table). This compression method only operates when "
    "binary logging is disabled on the replica; if binary logging is "
    "enabled, the table is compressed every time the binary log is "
    "rotated, and this value is ignored. Before MySQL 8.0.23, the "
    "default is 1000, and from MySQL 8.0.23, the default is zero, which "
    "disables this compression method. This is because in releases from "
    "MySQL 8.0.17, InnoDB transactions are written to the "
    "mysql.gtid_executed table by a separate process to non-InnoDB "
    "transactions. If the server has a mix of InnoDB and non-InnoDB "
    "transactions, attempting to compress the table with the "
    "compress_gtid_table thread can slow this process, so from "
    "MySQL 8.0.17 it is recommended that you set "
    "gtid_executed_compression_period to 0.",
    GLOBAL_VAR(gtid_executed_compression_period), CMD_LINE(OPT_ARG),
    VALID_RANGE(0, UINT_MAX32), DEFAULT(0), BLOCK_SIZE(1));

static Sys_var_bool Sys_disconnect_on_expired_password(
    "disconnect_on_expired_password",
    "Give clients that don't signal password expiration support execution "
    "time "
    "error(s) instead of connection error",
    READ_ONLY GLOBAL_VAR(disconnect_on_expired_password), CMD_LINE(OPT_ARG),
    DEFAULT(true));

static Sys_var_bool Sys_validate_user_plugins(
    "validate_user_plugins",
    "Turns on additional validation of authentication plugins assigned "
    "to user accounts. ",
    READ_ONLY NOT_VISIBLE GLOBAL_VAR(validate_user_plugins), CMD_LINE(OPT_ARG),
    DEFAULT(true), NO_MUTEX_GUARD, NOT_IN_BINLOG);

static Sys_var_enum Sys_block_encryption_mode(
    "block_encryption_mode", "mode for AES_ENCRYPT/AES_DECRYPT",
    SESSION_VAR(my_aes_mode), CMD_LINE(REQUIRED_ARG), my_aes_opmode_names,
    DEFAULT(my_aes_128_ecb));

static bool check_track_session_sys_vars(sys_var *, THD *thd, set_var *var) {
  DBUG_TRACE;
  return thd->session_tracker.get_tracker(SESSION_SYSVARS_TRACKER)
      ->check(thd, var);
  return false;
}

static bool update_track_session_sys_vars(sys_var *, THD *thd,
                                          enum_var_type type) {
  DBUG_TRACE;
  /* Populate map only for session variable. */
  if (type == OPT_SESSION)
    return thd->session_tracker.get_tracker(SESSION_SYSVARS_TRACKER)
        ->update(thd);
  return false;
}

static Sys_var_charptr Sys_track_session_sys_vars(
    "session_track_system_variables",
    "Track changes in registered system variables.",
    SESSION_VAR(track_sysvars_ptr), CMD_LINE(REQUIRED_ARG), IN_FS_CHARSET,
    DEFAULT("time_zone,autocommit,character_set_client,character_set_results,"
            "character_set_connection"),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_track_session_sys_vars),
    ON_UPDATE(update_track_session_sys_vars));
export sys_var *Sys_track_session_sys_vars_ptr = &Sys_track_session_sys_vars;

static bool update_session_track_schema(sys_var *, THD *thd, enum_var_type) {
  DBUG_TRACE;
  return thd->session_tracker.get_tracker(CURRENT_SCHEMA_TRACKER)->update(thd);
}

static Sys_var_bool Sys_session_track_schema(
    "session_track_schema", "Track changes to the 'default schema'.",
    SESSION_VAR(session_track_schema), CMD_LINE(OPT_ARG), DEFAULT(true),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(update_session_track_schema));

static bool update_session_track_tx_info(sys_var *, THD *thd, enum_var_type) {
  DBUG_TRACE;
  TX_TRACKER_GET(tst);
  return tst->update(thd);
}

static const char *session_track_transaction_info_names[] = {
    "OFF", "STATE", "CHARACTERISTICS", NullS};

static Sys_var_enum Sys_session_track_transaction_info(
    "session_track_transaction_info",
    "Track changes to the transaction attributes. OFF to disable; "
    "STATE to track just transaction state (Is there an active transaction? "
    "Does it have any data? etc.); CHARACTERISTICS to track transaction "
    "state "
    "and report all statements needed to start a transaction with the same "
    "characteristics (isolation level, read only/read write, snapshot - "
    "but not any work done / data modified within the transaction).",
    SESSION_VAR(session_track_transaction_info), CMD_LINE(REQUIRED_ARG),
    session_track_transaction_info_names, DEFAULT(TX_TRACK_NONE),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(update_session_track_tx_info));

static bool update_session_track_state_change(sys_var *, THD *thd,
                                              enum_var_type) {
  DBUG_TRACE;
  return thd->session_tracker.get_tracker(SESSION_STATE_CHANGE_TRACKER)
      ->update(thd);
}

static Sys_var_bool Sys_session_track_state_change(
    "session_track_state_change", "Track changes to the 'session state'.",
    SESSION_VAR(session_track_state_change), CMD_LINE(OPT_ARG), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(update_session_track_state_change));

static bool handle_offline_mode(sys_var *, THD *thd, enum_var_type) {
  DBUG_TRACE;
  DEBUG_SYNC(thd, "after_lock_offline_mode_acquire");

  if (mysqld_offline_mode()) {
    // Unlock the global system variable lock as kill holds LOCK_thd_data.
    mysql_mutex_unlock(&LOCK_global_system_variables);
    killall_non_super_threads(thd);
    mysql_mutex_lock(&LOCK_global_system_variables);
  }

  return false;
}

/**
  Checks if user has an additional CONNECTION_ADMIN privilege, needed
  to modify OFFLINE_MODE system variable (unless having SUPER).

  @retval true failure
  @retval false success

  @param thd the session context
*/
static bool check_offline_mode(sys_var * /*self*/, THD *thd,
                               set_var * /*setv*/) {
  Security_context *sctx = thd->security_context();
  if (!sctx->has_global_grant(STRING_WITH_LEN("CONNECTION_ADMIN")).first &&
      !sctx->check_access(SUPER_ACL)) {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0),
             "SYSTEM_VARIABLES_ADMIN plus CONNECTION_ADMIN or SUPER");
    return true;
  }
  return false;
}

static Sys_var_bool Sys_offline_mode("offline_mode",
                                     "Make the server into offline mode",
                                     GLOBAL_VAR(offline_mode),
                                     CMD_LINE(OPT_ARG), DEFAULT(false),
                                     NO_MUTEX_GUARD, NOT_IN_BINLOG,
                                     ON_CHECK(check_offline_mode),
                                     ON_UPDATE(handle_offline_mode));

static Sys_var_bool Sys_avoid_temporal_upgrade(
    "avoid_temporal_upgrade",
    "When this option is enabled, the pre-5.6.4 temporal types are "
    "not upgraded to the new format for ALTER TABLE requests "
    "ADD/CHANGE/MODIFY"
    " COLUMN, ADD INDEX or FORCE operation. "
    "This variable is deprecated and will be removed in a future release.",
    GLOBAL_VAR(avoid_temporal_upgrade),
    CMD_LINE(OPT_ARG, OPT_AVOID_TEMPORAL_UPGRADE), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static Sys_var_bool Sys_show_old_temporals(
    "show_old_temporals",
    "When this option is enabled, the pre-5.6.4 temporal types will "
    "be marked in the 'SHOW CREATE TABLE' and 'INFORMATION_SCHEMA.COLUMNS' "
    "table as a comment in COLUMN_TYPE field. "
    "This variable is deprecated and will be removed in a future release.",
    SESSION_VAR(show_old_temporals), CMD_LINE(OPT_ARG, OPT_SHOW_OLD_TEMPORALS),
    DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_session_admin_no_super), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static Sys_var_charptr Sys_disabled_storage_engines(
    "disabled_storage_engines",
    "Limit CREATE TABLE for the storage engines listed",
    READ_ONLY GLOBAL_VAR(opt_disabled_storage_engines), CMD_LINE(REQUIRED_ARG),
    IN_SYSTEM_CHARSET, DEFAULT(""));

static Sys_var_bool Sys_persisted_globals_load(
    PERSISTED_GLOBALS_LOAD,
    "When this option is enabled, config file mysqld-auto.cnf is read "
    "and applied to server, else this file is ignored even if present.",
    READ_ONLY NON_PERSIST GLOBAL_VAR(persisted_globals_load), CMD_LINE(OPT_ARG),
    DEFAULT(true), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(nullptr));

static bool sysvar_check_authid_string(sys_var *, THD *thd, set_var *var) {
  /*
    Since mandatory_roles is similar to a GRANT role statement without a
    GRANT ADMIN privilege, setting this variable requires both the
    ROLE_ADMIN and the SYSTEM_VARIABLES_ADMIN.
  */
  Security_context *sctx = thd->security_context();
  assert(sctx != nullptr);
  if (sctx && !sctx->has_global_grant(STRING_WITH_LEN("ROLE_ADMIN")).first) {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0),
             "SYSTEM_VARIABLES_ADMIN or SUPER privileges, as well as the "
             "ROLE_ADMIN");
    /* No privilege access error */
    return true;
  }
  if (var->save_result.string_value.str == nullptr) {
    var->save_result.string_value.str = const_cast<char *>("");
    var->save_result.string_value.length = 0;
  }
  return check_authorization_id_string(thd, var->save_result.string_value);
}

static bool sysvar_update_mandatory_roles(sys_var *, THD *, enum_var_type) {
  update_mandatory_roles();
  return false;
}

static PolyLock_mutex PLock_sys_mandatory_roles(&LOCK_mandatory_roles);
static Sys_var_lexstring Sys_mandatory_roles(
    "mandatory_roles",
    "All the specified roles are always considered granted to every user and "
    "they"
    " can't be revoked. Mandatory roles still require activation unless they "
    "are made into "
    "default roles. The granted roles will not be visible in the "
    "mysql.role_edges"
    " table.",
    GLOBAL_VAR(opt_mandatory_roles), CMD_LINE(REQUIRED_ARG), IN_SYSTEM_CHARSET,
    DEFAULT(""), &PLock_sys_mandatory_roles, NOT_IN_BINLOG,
    ON_CHECK(sysvar_check_authid_string),
    ON_UPDATE(sysvar_update_mandatory_roles));

static Sys_var_bool Sys_always_activate_granted_roles(
    "activate_all_roles_on_login",
    "Automatically set all granted roles as active after the user has "
    "authenticated successfully.",
    GLOBAL_VAR(opt_always_activate_granted_roles), CMD_LINE(OPT_ARG),
    DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(nullptr));

static PolyLock_mutex plock_sys_password_history(&LOCK_password_history);
static Sys_var_uint Sys_password_history(
    "password_history",
    "The number of old passwords to check in the history."
    " Set to 0 (the default) to turn the checks off",
    GLOBAL_VAR(global_password_history), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, UINT_MAX32), DEFAULT(0), BLOCK_SIZE(1),
    &plock_sys_password_history);

static PolyLock_mutex plock_sys_password_reuse_interval(
    &LOCK_password_reuse_interval);
static Sys_var_uint Sys_password_reuse_interval(
    "password_reuse_interval",
    "The minimum number of days that need to pass before a password can "
    "be reused. Set to 0 (the default) to turn the checks off",
    GLOBAL_VAR(global_password_reuse_interval), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(0, UINT_MAX32), DEFAULT(0), BLOCK_SIZE(1),
    &plock_sys_password_reuse_interval);

static bool check_resultset_metadata(sys_var *, THD *thd, set_var *var) {
  /*
    Set @@resultset_metadata to the value other than FULL only if
    the client supports it.
  */
  if (var->save_result.ulonglong_value != RESULTSET_METADATA_FULL &&
      !thd->get_protocol()->has_client_capability(
          CLIENT_OPTIONAL_RESULTSET_METADATA)) {
    my_error(ER_CLIENT_DOES_NOT_SUPPORT, MYF(0), "optional metadata transfer");
    return true;
  }
  return false;
}

static const char *resultset_metadata_names[] = {"NONE", "FULL", NullS};

static Sys_var_enum Sys_resultset_metadata(
    "resultset_metadata",
    "Controls what meatadata the server will send to the client: "
    "either FULL (default) for all metadata, NONE for no metadata.",
    SESSION_ONLY(resultset_metadata), NO_CMD_LINE, resultset_metadata_names,
    DEFAULT(static_cast<ulong>(RESULTSET_METADATA_FULL)), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_resultset_metadata), ON_UPDATE(nullptr));

static bool check_binlog_row_value_options(sys_var *self, THD *thd,
                                           set_var *var) {
  DBUG_TRACE;
  if (check_session_admin_outside_trx_outside_sf_outside_sp(self, thd, var))
    return true;
  if (var->save_result.ulonglong_value != 0) {
    const char *msg = nullptr;
    int code = ER_WARN_BINLOG_PARTIAL_UPDATES_DISABLED;
    if (!mysql_bin_log.is_open())
      msg = "the binary log is closed";
    else if (!var->is_global_persist()) {
      if (!thd->variables.sql_log_bin)
        msg = "the binary log is disabled";
      else if (thd->variables.binlog_format == BINLOG_FORMAT_STMT)
        msg = "binlog_format=STATEMENT";
      else if (log_bin_use_v1_row_events) {
        msg = "binlog_row_value_options=PARTIAL_JSON";
        code = ER_WARN_BINLOG_V1_ROW_EVENTS_DISABLED;
      } else if (thd->variables.binlog_row_image == BINLOG_ROW_IMAGE_FULL) {
        msg = "binlog_row_image=FULL";
        code = ER_WARN_BINLOG_PARTIAL_UPDATES_SUGGESTS_PARTIAL_IMAGES;
      }
    } else {
      if (global_system_variables.binlog_format == BINLOG_FORMAT_STMT)
        msg = "binlog_format=STATEMENT";
      else if (log_bin_use_v1_row_events) {
        msg = "binlog_row_value_options=PARTIAL_JSON";
        code = ER_WARN_BINLOG_V1_ROW_EVENTS_DISABLED;
      } else if (global_system_variables.binlog_row_image ==
                 BINLOG_ROW_IMAGE_FULL) {
        msg = "binlog_row_image=FULL";
        code = ER_WARN_BINLOG_PARTIAL_UPDATES_SUGGESTS_PARTIAL_IMAGES;
      }
    }
    if (msg) {
      switch (code) {
        case ER_WARN_BINLOG_PARTIAL_UPDATES_DISABLED:
          push_warning_printf(
              thd, Sql_condition::SL_WARNING, code,
              ER_THD(thd, ER_WARN_BINLOG_PARTIAL_UPDATES_DISABLED), msg,
              "PARTIAL_JSON");
          break;
        case ER_WARN_BINLOG_PARTIAL_UPDATES_SUGGESTS_PARTIAL_IMAGES:
          push_warning_printf(
              thd, Sql_condition::SL_WARNING, code,
              ER_THD(thd,
                     ER_WARN_BINLOG_PARTIAL_UPDATES_SUGGESTS_PARTIAL_IMAGES),
              msg, "PARTIAL_JSON");
          break;
        case ER_WARN_BINLOG_V1_ROW_EVENTS_DISABLED:
          push_warning_printf(
              thd, Sql_condition::SL_WARNING, code,
              ER_THD(thd, ER_WARN_BINLOG_V1_ROW_EVENTS_DISABLED), msg);
          break;
        default:
          assert(0); /* purecov: deadcode */
      }
    }
  }

  return false;
}

const char *binlog_row_value_options_names[] = {"PARTIAL_JSON", nullptr};
static Sys_var_set Sys_binlog_row_value_options(
    "binlog_row_value_options",
    "When set to PARTIAL_JSON, this option enables a space-efficient "
    "row-based binary log format for UPDATE statements that modify a "
    "JSON value using only the functions JSON_SET, JSON_REPLACE, and "
    "JSON_REMOVE. For such updates, only the modified parts of the "
    "JSON document are included in the binary log, so small changes of "
    "big documents may need significantly less space.",
    SESSION_VAR(binlog_row_value_options), CMD_LINE(REQUIRED_ARG),
    binlog_row_value_options_names, DEFAULT(0), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_binlog_row_value_options));

static bool check_keyring_access(sys_var *, THD *thd, set_var *) {
  if (!thd->security_context()->check_access(SUPER_ACL) &&
      !(thd->security_context()
            ->has_global_grant(STRING_WITH_LEN("ENCRYPTION_KEY_ADMIN"))
            .first)) {
    my_error(ER_KEYRING_ACCESS_DENIED_ERROR, MYF(0),
             "SUPER or ENCRYPTION_KEY_ADMIN");
    return true;
  }
  return false;
}

/**
  This is a mutex used to protect global variable @@keyring_operations.
*/
static PolyLock_mutex PLock_keyring_operations(&LOCK_keyring_operations);
/**
  This variable provides access to keyring service APIs. When this variable
  is disabled calls to keyring_key_generate(), keyring_key_store() and
  keyring_key_remove() will report error until this variable is enabled.
  This variable is protected under a mutex named PLock_keyring_operations.
  To access this variable you must first set this mutex.

  @sa PLock_keyring_operations
*/
static Sys_var_bool Sys_keyring_operations(
    "keyring_operations",
    "This variable provides access to keyring service APIs. When this "
    "option is disabled calls to keyring_key_generate(), keyring_key_store() "
    "and keyring_key_remove() will report error until this variable is "
    "enabled.",
    NON_PERSIST GLOBAL_VAR(opt_keyring_operations), NO_CMD_LINE, DEFAULT(true),
    &PLock_keyring_operations, NOT_IN_BINLOG, ON_CHECK(check_keyring_access),
    ON_UPDATE(nullptr));

static bool check_default_collation_for_utf8mb4(sys_var *self, THD *thd,
                                                set_var *var) {
  if (check_collation_not_null(self, thd, var)) {
    return true;
  }

  if (!var->value)
    var->save_result.ptr = reinterpret_cast<void *>(self->get_default());

  auto cs = static_cast<const CHARSET_INFO *>(var->save_result.ptr);
  if (cs == &my_charset_utf8mb4_0900_ai_ci ||
      cs == &my_charset_utf8mb4_general_ci)
    return false;

  my_error(ER_INVALID_DEFAULT_UTF8MB4_COLLATION, MYF(0), cs->m_coll_name);
  return true;
}

static Sys_var_struct<CHARSET_INFO, Get_name> Sys_default_collation_for_utf8mb4(
    "default_collation_for_utf8mb4",
    "Controls default collation for utf8mb4 while replicating implicit "
    "utf8mb4 collations.",
    SESSION_VAR(default_collation_for_utf8mb4), NO_CMD_LINE,
    DEFAULT(&my_charset_utf8mb4_0900_ai_ci), NO_MUTEX_GUARD, IN_BINLOG,
    ON_CHECK(check_default_collation_for_utf8mb4),
    ON_UPDATE(update_deprecated));

static Sys_var_bool Sys_show_create_table_verbosity(
    "show_create_table_verbosity",
    "When this option is enabled, it increases the verbosity of "
    "'SHOW CREATE TABLE'.",
    SESSION_VAR(show_create_table_verbosity), CMD_LINE(OPT_ARG), DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr));

static const char *use_secondary_engine_values[] = {"OFF", "ON", "FORCED",
                                                    nullptr};
static Sys_var_enum Sys_use_secondary_engine(
    "use_secondary_engine",
    "Controls preparation of SELECT statements against secondary storage "
    "engine. Valid values: OFF/ON/FORCED. OFF = Prepare only against primary "
    "storage engine. ON = First prepare against secondary storage engine, "
    "reprepare against primary storage engine if error. FORCED = Prepare all "
    "SELECT statements referencing one or more base tables only against "
    "secondary storage engine.",
    HINT_UPDATEABLE SESSION_ONLY(use_secondary_engine), NO_CMD_LINE,
    use_secondary_engine_values, DEFAULT(SECONDARY_ENGINE_ON), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr));

static Sys_var_session_special Sys_statement_id(
    "statement_id",
    "statement_id: represents the id of the query "
    "When this option is enabled it returns the statement id to the client, "
    "the client can find more data about this query from the performance schema"
    "(such as: events_statements_history table, rpd_query_stats table etc)  by "
    "searching for a specific statement_id value.",
    READ_ONLY sys_var::ONLY_SESSION, NO_CMD_LINE, VALID_RANGE(0, INT_MAX64),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(nullptr), ON_READ(read_statement_id));

/**
  Cost threshold for executing queries in a secondary storage engine. Only
  queries that have an estimated cost above this value will be attempted
  executed in a secondary storage engine.

  Secondary storage engines are meant to accelerate queries that would otherwise
  take a relatively long time to execute. If a secondary storage engine accepts
  a query, it is assumed that it will be able to accelerate it. However, if the
  estimated cost of the query is low, the query will execute fast in the primary
  engine too, so there is little to gain by offloading the query to the
  secondary engine.

  The default value aims to avoid use of secondary storage engines for queries
  that could be executed by the primary engine in a few tenths of seconds or
  less, and attempt to use secondary storage engines for queries would take
  seconds or more.
*/
static Sys_var_double Sys_secondary_engine_cost_threshold(
    "secondary_engine_cost_threshold",
    "Controls which statements to consider for execution in a secondary "
    "storage engine. Only statements that have a cost estimate higher than "
    "this value will be attempted executed in a secondary storage engine.",
    HINT_UPDATEABLE SESSION_VAR(secondary_engine_cost_threshold),
    CMD_LINE(OPT_ARG), VALID_RANGE(0, DBL_MAX), DEFAULT(100000), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr));

static Sys_var_bool Sys_sql_require_primary_key{
    "sql_require_primary_key",
    "When set, tables must be created with a primary key, and an existing "
    "primary key cannot be removed with 'ALTER TABLE'. Attempts to do so "
    "will result in an error.",
    HINT_UPDATEABLE SESSION_VAR(sql_require_primary_key),
    CMD_LINE(OPT_ARG),
    DEFAULT(false),
    NO_MUTEX_GUARD,
    IN_BINLOG,
    ON_CHECK(check_session_admin)};

static Sys_var_bool Sys_sql_generate_invisible_primary_key(
    "sql_generate_invisible_primary_key",
    "When set, if a table is created without a primary key then server "
    "generates invisible auto-increment column as a primary key for the table.",
    SESSION_VAR(sql_generate_invisible_primary_key), CMD_LINE(OPT_ARG),
    DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_session_admin), ON_UPDATE(nullptr));

static Sys_var_bool Sys_show_gipk_in_create_table_and_information_schema(
    "show_gipk_in_create_table_and_information_schema",
    "When set, if a primary key is generated for a table then SHOW commands "
    "and INFORMATION_SCHEMA tables shows generated invisible primary key "
    "definition.",
    SESSION_VAR(show_gipk_in_create_table_and_information_schema),
    CMD_LINE(OPT_ARG), DEFAULT(true), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(nullptr), ON_UPDATE(nullptr));

static Sys_var_charptr Sys_sys_variables_admin_subject(
    PERSIST_ONLY_ADMIN_X509_SUBJECT,
    "The client peer certificate name required to enable setting all "
    "system variables via SET PERSIST[_ONLY]",
    READ_ONLY NON_PERSIST GLOBAL_VAR(sys_var_persist_only_admin_x509_subject),
    CMD_LINE(OPT_ARG), IN_SYSTEM_CHARSET, DEFAULT(""));

static Sys_var_ulong Sys_binlog_row_event_max_size(
    "binlog_row_event_max_size",
    "The maximum size of a row-based binary log event in bytes. Rows will be "
    "grouped into events smaller than this size if possible. "
    "The value has to be a multiple of 256.",
    READ_ONLY GLOBAL_VAR(binlog_row_event_max_size), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(256, ULONG_MAX), DEFAULT(8192), BLOCK_SIZE(256));

static bool check_group_replication_consistency(sys_var *self, THD *thd,
                                                set_var *var) {
  if (var->type == OPT_GLOBAL || var->type == OPT_PERSIST) {
    Security_context *sctx = thd->security_context();
    if (!sctx->check_access(SUPER_ACL) &&
        !sctx->has_global_grant(STRING_WITH_LEN("GROUP_REPLICATION_ADMIN"))
             .first) {
      my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0),
               "SUPER or GROUP_REPLICATION_ADMIN");
      return true;
    }
  }

  return check_outside_trx(self, thd, var);
}

static const char *group_replication_consistency_names[] = {
    "EVENTUAL", "BEFORE_ON_PRIMARY_FAILOVER", "BEFORE",
    "AFTER",    "BEFORE_AND_AFTER",           NullS};

static Sys_var_enum Sys_group_replication_consistency(
    "group_replication_consistency",
    "Transaction consistency guarantee, possible values: EVENTUAL, "
    "BEFORE_ON_PRIMARY_FAILOVER, BEFORE, AFTER, BEFORE_AND_AFTER",
    SESSION_VAR(group_replication_consistency), CMD_LINE(OPT_ARG),
    group_replication_consistency_names,
    DEFAULT(GROUP_REPLICATION_CONSISTENCY_EVENTUAL), NO_MUTEX_GUARD,
    NOT_IN_BINLOG, ON_CHECK(check_group_replication_consistency),
    ON_UPDATE(nullptr));

static bool check_binlog_encryption_admin(sys_var *, THD *thd, set_var *) {
  DBUG_TRACE;
  if (!thd->security_context()->check_access(SUPER_ACL) &&
      !(thd->security_context()
            ->has_global_grant(STRING_WITH_LEN("BINLOG_ENCRYPTION_ADMIN"))
            .first)) {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0),
             "SUPER or BINLOG_ENCRYPTION_ADMIN");
    return true;
  }
  return false;
}

bool Sys_var_binlog_encryption::global_update(THD *thd, set_var *var) {
  DBUG_TRACE;

  /* No-op if trying to set to current value */
  bool new_value = var->save_result.ulonglong_value;
  if (new_value == rpl_encryption.is_enabled()) return false;

  DEBUG_SYNC(thd, "after_locking_global_sys_var_set_binlog_enc");
  /* We unlock in following statement to avoid deadlock involving following
   * conditions.
   * ------------------------------------------------------------------------
   * Thread 1 (START SLAVE)  has locked channel_map and waiting for cond_wait
   * that is supposed to be done by Thread 2.
   *
   * Thread 2 (handle_slave_io) is supposed to signal Thread 1 but waiting to
   * lock LOCK_global_system_variables.
   *
   * Thread 3 (SET GLOBAL binlog_encryption=ON|OFF) has locked
   * LOCK_global_system_variables and waiting for channel_map.
   */
  mysql_mutex_unlock(&LOCK_global_system_variables);
  /* Set the option new value */
  bool res = false;
  if (new_value)
    res = rpl_encryption.enable(thd);
  else
    rpl_encryption.disable(thd);
  mysql_mutex_lock(&LOCK_global_system_variables);
  return res;
}

static Sys_var_binlog_encryption Sys_binlog_encryption(
    "binlog_encryption", "Enable/disable binary and relay logs encryption.",
    GLOBAL_VAR(rpl_encryption.get_enabled_var()), CMD_LINE(OPT_ARG),
    DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_binlog_encryption_admin));

static Sys_var_bool Sys_binlog_rotate_encryption_master_key_at_startup(
    "binlog_rotate_encryption_master_key_at_startup",
    "Force binlog encryption master key rotation at startup",
    READ_ONLY GLOBAL_VAR(
        rpl_encryption.get_master_key_rotation_at_startup_var()),
    CMD_LINE(OPT_ARG), DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG);

static Sys_var_uint Sys_original_server_version(
    "original_server_version",
    "The version of the server where the transaction was originally executed",
    SESSION_ONLY(original_server_version), NO_CMD_LINE,
    VALID_RANGE(0, UNDEFINED_SERVER_VERSION), DEFAULT(UNDEFINED_SERVER_VERSION),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, IN_BINLOG,
    ON_CHECK(check_session_admin_or_replication_applier));

static Sys_var_uint Sys_immediate_server_version(
    "immediate_server_version",
    "The server version of the immediate server in the replication topology",
    SESSION_ONLY(immediate_server_version), NO_CMD_LINE,
    VALID_RANGE(0, UNDEFINED_SERVER_VERSION), DEFAULT(UNDEFINED_SERVER_VERSION),
    BLOCK_SIZE(1), NO_MUTEX_GUARD, IN_BINLOG,
    ON_CHECK(check_session_admin_or_replication_applier));

static bool check_set_default_table_encryption_access(sys_var *self
                                                      [[maybe_unused]],
                                                      THD *thd, set_var *var) {
  DBUG_EXECUTE_IF("skip_table_encryption_admin_check_for_set",
                  { return false; });
  if ((var->type == OPT_GLOBAL || var->type == OPT_PERSIST) &&
      is_group_replication_running()) {
    my_message(ER_GROUP_REPLICATION_RUNNING,
               "The default_table_encryption option cannot be changed when "
               "Group replication is running.",
               MYF(0));
    return true;
  }

  // Should own one of SUPER or both (SYSTEM_VARIABLES_ADMIN and
  // TABLE_ENCRYPTION_ADMIN), unless this is the session option and
  // the value is unchanged.
  longlong previous_val = thd->variables.default_table_encryption;
  longlong val = (longlong)var->save_result.ulonglong_value;
  if ((!var->is_global_persist() && val == previous_val) ||
      thd->security_context()->check_access(SUPER_ACL) ||
      (thd->security_context()
           ->has_global_grant(STRING_WITH_LEN("SYSTEM_VARIABLES_ADMIN"))
           .first &&
       thd->security_context()
           ->has_global_grant(STRING_WITH_LEN("TABLE_ENCRYPTION_ADMIN"))
           .first)) {
    return false;
  }

  my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0),
           "SUPER or SYSTEM_VARIABLES_ADMIN and TABLE_ENCRYPTION_ADMIN");
  return true;
}

static Sys_var_bool Sys_default_table_encryption(
    "default_table_encryption",
    "Database and tablespace are created with this default encryption property "
    "unless the user specifies an explicit encryption property.",
    HINT_UPDATEABLE SESSION_VAR(default_table_encryption), CMD_LINE(OPT_ARG),
    DEFAULT(false), NO_MUTEX_GUARD, IN_BINLOG,
    ON_CHECK(check_set_default_table_encryption_access), ON_UPDATE(nullptr));

static bool check_set_table_encryption_privilege_access(sys_var *, THD *thd,
                                                        set_var *) {
  DBUG_EXECUTE_IF("skip_table_encryption_admin_check_for_set",
                  { return false; });
  if (!thd->security_context()->check_access(SUPER_ACL)) {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return true;
  }
  return false;
}

static Sys_var_bool Sys_table_encryption_privilege_check(
    "table_encryption_privilege_check",
    "Indicates if server enables privilege check when user tries to use "
    "non-default value for CREATE DATABASE or CREATE TABLESPACE or when "
    "user tries to do CREATE TABLE with ENCRYPTION option which deviates "
    "from per-database default.",
    GLOBAL_VAR(opt_table_encryption_privilege_check), CMD_LINE(OPT_ARG),
    DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_set_table_encryption_privilege_access), ON_UPDATE(nullptr));

static Sys_var_bool Sys_var_print_identified_with_as_hex(
    "print_identified_with_as_hex",
    "SHOW CREATE USER will print the AS clause as HEX if it contains "
    "non-prinable characters",
    SESSION_VAR(print_identified_with_as_hex), CMD_LINE(OPT_ARG),
    DEFAULT(false));

/**
   Session only flag to skip printing secondary engine in SHOW CREATE
   TABLE.

   @sa store_create_info
*/
static Sys_var_bool Sys_var_show_create_table_skip_secondary_engine(
    "show_create_table_skip_secondary_engine",
    "SHOW CREATE TABLE will skip SECONDARY_ENGINE when printing the table "
    "definition",
    SESSION_ONLY(show_create_table_skip_secondary_engine), NO_CMD_LINE,
    DEFAULT(false));

static Sys_var_uint Sys_generated_random_password_length(
    "generated_random_password_length",
    "Determines the length randomly generated passwords in CREATE USER-,"
    "SET PASSWORD- or ALTER USER statements",
    SESSION_VAR(generated_random_password_length), CMD_LINE(REQUIRED_ARG),
    VALID_RANGE(5, 255), DEFAULT(20), BLOCK_SIZE(1), NO_MUTEX_GUARD, IN_BINLOG,
    ON_CHECK(nullptr));

static bool check_set_protocol_compression_algorithms(sys_var *, THD *,
                                                      set_var *var) {
  if (!(var->save_result.string_value.str)) return true;
  return validate_compression_attributes(var->save_result.string_value.str,
                                         std::string(), true);
}

static Sys_var_charptr Sys_protocol_compression_algorithms(
    "protocol_compression_algorithms",
    "List of compression algorithms supported by server. Supported values "
    "are any combination of zlib, zstd, uncompressed. Command line clients "
    "may use the --compression-algorithms flag to specify a set of algorithms, "
    "and the connection will use an algorithm supported by both client and "
    "server. It picks zlib if both client and server support it; otherwise it "
    "picks zstd if both support it; otherwise it picks uncompressed if both "
    "support it; otherwise it fails.",
    GLOBAL_VAR(opt_protocol_compression_algorithms), CMD_LINE(REQUIRED_ARG),
    IN_FS_CHARSET,
    DEFAULT(const_cast<char *>(PROTOCOL_COMPRESSION_DEFAULT_VALUE)),
    NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(check_set_protocol_compression_algorithms), ON_UPDATE(nullptr));

static bool check_set_require_row_format(sys_var *, THD *thd, set_var *var) {
  /*
   Should own SUPER or SYSTEM_VARIABLES_ADMIN or SESSION_VARIABLES_ADMIN
   when the value is changing to NO, no privileges are needed to set to YES
  */
  longlong previous_val = thd->variables.require_row_format;
  longlong val = (longlong)var->save_result.ulonglong_value;
  assert(!var->is_global_persist());

  // if it was true and we are changing it
  if (previous_val && val != previous_val) {
    if (thd->security_context()->check_access(SUPER_ACL) ||
        thd->security_context()
            ->has_global_grant(STRING_WITH_LEN("SYSTEM_VARIABLES_ADMIN"))
            .first ||
        thd->security_context()
            ->has_global_grant(STRING_WITH_LEN("SESSION_VARIABLES_ADMIN"))
            .first)
      return false;

    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0),
             "SUPER or SYSTEM_VARIABLES_ADMIN or SESSION_VARIABLES_ADMIN");
    return true;
  }
  return false;
}

/**
   Session only flag to limit the application of queries to row based events
   and DDLs with the exception of temporary table creation/deletion
*/
static Sys_var_bool Sys_var_require_row_format(
    "require_row_format",
    "Limit the application of queries to row based events "
    "and DDLs with the exception of temporary table creation/deletion.",
    SESSION_ONLY(require_row_format), NO_CMD_LINE, DEFAULT(false),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(check_set_require_row_format));

/**
  Changes the `Delegate` internal state in regards to which type of lock to
  use and in regards to whether or not to take plugin locks in each hook
  invocation.
*/
static bool handle_plugin_lock_type_change(sys_var *, THD *, enum_var_type) {
  DBUG_TRACE;
  delegates_acquire_locks();
  delegates_update_lock_type();
  delegates_release_locks();
  return false;
}

static Sys_var_bool Sys_replication_optimize_for_static_plugin_config(
    "replication_optimize_for_static_plugin_config",
    "Optional flag that blocks plugin install/uninstall and allows skipping "
    "the acquisition of the lock to read from the plugin list and the usage "
    "of read-optimized spin-locks. Use only when plugin hook callback needs "
    "optimization (a lot of semi-sync replicas, for instance).",
    GLOBAL_VAR(opt_replication_optimize_for_static_plugin_config),
    CMD_LINE(OPT_ARG), DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(nullptr), ON_UPDATE(handle_plugin_lock_type_change));

static Sys_var_bool Sys_replication_sender_observe_commit_only(
    "replication_sender_observe_commit_only",
    "Optional flag that allows for only calling back observer hooks at "
    "commit.",
    GLOBAL_VAR(opt_replication_sender_observe_commit_only), CMD_LINE(OPT_ARG),
    DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(nullptr));

static Sys_var_bool Sys_skip_replica_start(
    "skip_replica_start",
    "Do not start replication threads automatically "
    "when the server starts.",
    READ_ONLY GLOBAL_VAR(opt_skip_replica_start), CMD_LINE(OPT_ARG),
    DEFAULT(false), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr),
    ON_UPDATE(nullptr));

static bool check_authentication_policy(sys_var *, THD *, set_var *var) {
  if (!(var->save_result.string_value.str)) return true;
  return validate_authentication_policy(var->save_result.string_value.str);
}

static bool fix_authentication_policy(sys_var *, THD *, enum_var_type) {
  DBUG_TRACE;
  update_authentication_policy();
  return false;
}
/**
  This is a mutex used to protect @@global.authentication_policy variable.
*/
static PolyLock_mutex PLock_authentication_policy(&LOCK_authentication_policy);
/*
  when authentication_policy = 'mysql_native_password,,' and
  --default-authentication-plugin = 'caching_sha2_password'
  set default as mysql_native_password.
  --authentication_policy has precedence over --default-authentication-plugin
  with 1 exception as below: when authentication_policy = '*,,' and
  --default-authentication-plugin = 'mysql_native_password'
  set default as mysql_native_password
  in case no concrete plugin can be extracted from --authentication_policy
  for first factor, server picks plugin name from
  --default-authentication-plugin
*/
static Sys_var_charptr Sys_authentication_policy(
    "authentication_policy",
    "Defines policies around how user account can be configured with Multi "
    "Factor authentication methods during CREATE/ALTER USER statement. "
    "This variable accepts at-most 3 comma separated list of authentication "
    "plugin names where each value refers to what authentication plugin should "
    "be used in place of 1st Factor Authentication (FA), 2FA and 3FA method. "
    "Value * indicates any plugin is allowed for 1FA, 2FA and 3FA method. "
    "An empty value means nth FA method is optional.",
    GLOBAL_VAR(opt_authentication_policy), CMD_LINE(REQUIRED_ARG),
    IN_FS_CHARSET, DEFAULT("*,,"), &PLock_authentication_policy, NOT_IN_BINLOG,
    ON_CHECK(check_authentication_policy),
    ON_UPDATE(fix_authentication_policy));

static Sys_var_deprecated_alias Sys_skip_slave_start("skip_slave_start",
                                                     Sys_skip_replica_start);

static const char *terminology_use_previous_names[] = {"NONE", "BEFORE_8_0_26",
                                                       nullptr};

static Sys_var_enum Sys_terminology_use_previous(
    "terminology_use_previous",
    "Make monitoring tables and statements use the identifiers that were "
    "in use before they were changed in a given release. That includes names "
    "for mutexes, read/write locks, condition variables, memory allocations, "
    "thread names, thread stages, and thread commands. When the session "
    "option is set to BEFORE_8_0_26, the session uses the names that were in "
    "use until 8.0.25, when it selects from performance_schema tables, or "
    "selects from INFORMATION_SCHEMA.PROCESSLIST, or issues SHOW PROCESSLIST "
    "or SHOW REPLICA STATUS. When the global option is set to BEFORE_8_0_26, "
    "new sessions use BEFORE_8_0_26 as default for the session option, and in "
    "addition the thread commands that were in use until 8.0.25 are written "
    "to the slow query log.",
    SESSION_VAR(terminology_use_previous), CMD_LINE(REQUIRED_ARG),
    terminology_use_previous_names, DEFAULT(terminology_use_previous::NONE),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr),
    DEPRECATED_VAR(""));

static Sys_var_bool Sys_xa_detatch_on_prepare(
    "xa_detach_on_prepare",
    "When set, XA transactions will be detached (AKA dissociated or "
    "disconnected) from connection as part of XA PREPARE. This means that "
    "the XA transaction can be committed/rolled back by any connection, "
    "even if the starting connection has not terminated, and the starting "
    "connection can start new transactions. As a side effect, temporary "
    "tables cannot be used inside XA transactions. "
    "When disabled, XA transactions are associated with the same connection "
    "until the session disconnects. ON is the only safe choice for "
    "replication.",
    HINT_UPDATEABLE SESSION_VAR(xa_detach_on_prepare), CMD_LINE(OPT_ARG),
    DEFAULT(true), NO_MUTEX_GUARD, IN_BINLOG,
    ON_CHECK(check_session_admin_outside_trx_outside_sf));

#ifndef NDEBUG
static Sys_var_charptr Sys_debug_sensitive_session_string(
    "debug_sensitive_session_string",
    "Debug variable to test sensitive session string variable.",
    SENSITIVE SESSION_VAR(debug_sensitive_session_str), CMD_LINE(REQUIRED_ARG),
    IN_FS_CHARSET, DEFAULT(""));
#endif /* NDEBUG */

static Sys_var_bool Sys_persist_sensitive_variables_in_plaintext(
    "persist_sensitive_variables_in_plaintext",
    "If set to FALSE, server will refuse to persist SENSITIVE variables in "
    "plaintext and refuse to start if encrypted part of persited file cannot "
    "be processed.",
    READ_ONLY NON_PERSIST
        GLOBAL_VAR(opt_persist_sensitive_variables_in_plaintext),
    CMD_LINE(OPT_ARG), DEFAULT(true), NO_MUTEX_GUARD, NOT_IN_BINLOG,
    ON_CHECK(nullptr), ON_UPDATE(nullptr), nullptr, sys_var::PARSE_EARLY);

static const char *explain_format_names[] = {
    "TRADITIONAL", "TRADITIONAL_STRICT", "TREE", "JSON", NullS};
static Sys_var_enum Sys_explain_format(
    "explain_format",
    "The default format in which the EXPLAIN statement displays information. "
    "Valid values are TRADITIONAL (default), TREE, JSON and TRADITIONAL_STRICT."
    " TRADITIONAL_STRICT is only used internally by the mtr test suite, and is "
    "not meant to be used anywhere else.",
    SESSION_VAR(explain_format), CMD_LINE(OPT_ARG), explain_format_names,
    DEFAULT(static_cast<ulong>(Explain_format_type::TRADITIONAL)),
    NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr));