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
use crate::alloc::TryClone;
use crate::error::io_error;
#[cfg(any(test, injected_yields))]
use crate::mvcc::yield_points::{FailureInjector, YieldInjector};
use crate::statement::StatementOrigin;
use crate::storage::{journal_mode, pager::SavepointResult};
use crate::sync::{
atomic::{
AtomicBool, AtomicI32, AtomicI64, AtomicIsize, AtomicU16, AtomicU64, AtomicU8, Ordering,
},
Arc, RwLock,
};
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
use crate::types::{WalFrameInfo, WalState};
#[cfg(feature = "fs")]
use crate::util::{OpenMode, OpenOptions};
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
use crate::Page;
use crate::{
ast, function,
io::{MemoryIO, IO},
progress::{ProgressHandler, ProgressHandlerCallback},
translate,
translate::collate::CollationSeq,
util::IOExt,
vdbe, AllViewsTxState, AtomicCipherMode, AtomicSyncMode, AtomicTempStore, BusyHandler,
BusyHandlerCallback, CaptureDataChangesInfo, CheckpointMode, CheckpointResult, CipherMode, Cmd,
Completion, ConnectionMetrics, Database, DatabaseCatalog, DatabaseOpts, Duration,
EncryptionKey, EncryptionOpts, IOResult, IndexMethod, LimboError, MvStore, OpenFlags, PageSize,
Pager, Parser, Program, QueryMode, QueryRunner, Result, Schema, Statement, SyncMode,
TransactionMode, Trigger, Value, VirtualTable, WalAutoActions,
};
use crate::{is_memory_like, turso_assert};
use crate::{MAIN_DB_ID, TEMP_DB_ID};
use arc_swap::ArcSwap;
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
use smallvec::SmallVec;
use std::cmp::Ordering as CmpOrdering;
use std::fmt::Display;
use std::ops::Deref;
#[cfg(feature = "simulator")]
use std::path::Path;
#[cfg(not(target_family = "wasm"))]
use tempfile::TempDir;
use tracing::{instrument, Level};
use turso_macros::{turso_assert_ne, AtomicEnum};
#[cfg(feature = "simulator")]
fn db_identity_for_testing(db_path: &Path) -> Result<(u32, u32)> {
let bytes =
std::fs::read(db_path).map_err(|e| io_error(e, "read db header for simulator testing"))?;
let db_header_size = crate::storage::sqlite3_ondisk::DatabaseHeader::SIZE;
if bytes.len() < db_header_size {
return Err(LimboError::InternalError(format!(
"database file is smaller than the header: got {}, need at least {}",
bytes.len(),
db_header_size
)));
}
let db_size_pages = u32::from_be_bytes(bytes[28..32].try_into().unwrap());
let crc = crc32c::crc32c(&bytes[..db_header_size]);
Ok((db_size_pages, crc))
}
#[derive(Clone, AtomicEnum, Copy, PartialEq, Eq, Debug)]
pub(crate) enum TransactionState {
Write {
schema_did_change: bool,
},
Read,
/// PendingUpgrade remembers what transaction state was before upgrade to write (has_read_txn is true if before transaction were in Read state)
/// This is important, because if we failed to initialize write transaction immediatley - we need to end implicitly started read txn (e.g. for simiple INSERT INTO operation)
/// But for late upgrade of transaction we should keep read transaction active (e.g. BEGIN; SELECT ...; INSERT INTO ...)
PendingUpgrade {
has_read_txn: bool,
},
None,
}
pub(crate) struct TempDatabase {
pub(crate) db: Arc<Database>,
pub(crate) pager: Arc<Pager>,
#[cfg(not(target_family = "wasm"))]
_temp_dir: Option<TempDir>,
}
/// All of the connection-local state needed to manage the `TEMP` schema.
///
/// Grouped so that anything that touches the temp database (the pager,
/// the last committed schema snapshot, the dirty-schema flag) lives in
/// one place. The individual fields keep their own locks because their
/// access patterns differ: `database` is read on every temp-qualified
/// lookup while `committed_schema` is only touched at commit/rollback
/// boundaries, and `schema_did_change` is flipped from inside `SetCookie`.
pub(crate) struct TempDbContext {
/// Per-connection temp database (`TEMP_DB_ID`/schema `temp`).
/// Lazily initialized on first temp DDL.
pub(crate) database: RwLock<Option<TempDatabase>>,
/// Last committed snapshot of `database.read().as_ref().unwrap().db.schema`.
/// Updated on successful commit and consulted on full-txn rollback
/// to restore the in-memory temp schema (there is no shared
/// `Database::schema` for temp the way main has). `None` until the
/// first temp DDL is committed; a rollback with `None` resets the
/// temp schema to empty.
pub(crate) committed_schema: RwLock<Option<Arc<Schema>>>,
/// Set by `SetCookie` when a temp DDL runs; read by commit/rollback
/// to decide whether to snapshot/restore. Cleared on transaction
/// end. Mirrors the `schema_did_change` field inside
/// `TransactionState::Write` for the main DB.
pub(crate) schema_did_change: AtomicBool,
}
impl TempDbContext {
pub(crate) fn new() -> Self {
Self {
database: RwLock::new(None),
committed_schema: RwLock::new(None),
schema_did_change: AtomicBool::new(false),
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct NamedSavepointFrame {
pub(crate) name: String,
pub(crate) starts_transaction: bool,
pub(crate) deferred_fk_violations: isize,
/// Snapshot of `conn.schema` taken at SAVEPOINT begin. Used by
/// ROLLBACK TO to restore the in-memory main schema without re-
/// reading sqlite_schema from disk — disk reparse from inside a
/// vdbe opcode would block on cursor I/O (and additionally, for
/// sequences, on `prepare_internal + run_with_row_callback`),
/// violating the vdbe async contract. Cheap to capture: bumps
/// the `Arc<Schema>` refcount. DDL after SAVEPOINT goes through
/// `Connection::with_schema_mut` which uses `Arc::make_mut`, so
/// the snapshot keeps pointing to the pre-DDL schema even when
/// the current schema diverges.
pub(crate) main_schema_snapshot: Arc<Schema>,
/// Snapshot of `temp_db.db.schema` taken at SAVEPOINT begin. `None`
/// when the temp database had not been initialized yet. Used by
/// ROLLBACK TO to restore the in-memory temp schema after the
/// on-disk pages have been rolled back via the mirror call.
pub(crate) temp_schema_snapshot: Option<Arc<Schema>>,
/// Snapshot of the connection-local `database_schemas` map at
/// SAVEPOINT begin. Cheap — values are `Arc`. Used by ROLLBACK TO
/// to restore staged DDL on attached databases.
pub(crate) staged_schema_snapshot: HashMap<usize, Arc<Schema>>,
}
/// Info returned by `rollback_named_savepoint_frame` so callers can
/// restore in-memory schema state after the pager has rolled back.
pub(crate) struct RollbackFrameInfo {
pub(crate) main_schema_snapshot: Arc<Schema>,
pub(crate) temp_schema_snapshot: Option<Arc<Schema>>,
pub(crate) staged_schema_snapshot: HashMap<usize, Arc<Schema>>,
}
struct SchemaReparseGuard {
connection: Arc<Connection>,
}
impl Drop for SchemaReparseGuard {
fn drop(&mut self) {
self.connection
.schema_reparse_in_progress
.store(false, Ordering::SeqCst);
}
}
/// Re-entrant state for [`Connection::reparse_schema_nonblock`] and the
/// VACUUM-only [`Connection::reparse_schema_with_cookie_keeping_sequences`].
/// `Start` is the fresh state (cookie not yet read / schema build not yet
/// begun); `Building` carries the half-built schema, captured table-valued
/// functions, the held reparse guard, and the current sub-phase across IO
/// yields.
#[derive(Default)]
pub enum ReparseSchemaState {
#[default]
Start,
Building(Box<ReparseSchemaInner>),
}
pub struct ReparseSchemaInner {
/// Held for the whole reparse so a concurrent reparse on this connection
/// trips the recursion assert. Dropped when the schema is finalized.
_guard: SchemaReparseGuard,
fresh: Schema,
/// Built-in table-valued functions captured from the old schema; rehydrated
/// after the sqlite_schema scan since they don't survive re-parsing.
tvfs: Vec<Arc<crate::vtab::VirtualTable>>,
/// VACUUM-supplied sequence descriptors to graft onto the rebuilt schema
/// instead of re-reading each backing table. `None` for a normal reparse,
/// which recovers descriptors from disk in the `PopulateSequences` phase.
preserved_sequences: Option<rustc_hash::FxHashMap<String, Arc<crate::schema::Sequence>>>,
phase: ReparsePhase,
}
enum ReparsePhase {
/// Scanning `SELECT * FROM sqlite_schema` into `fresh`.
ParseSchema {
parse: Box<crate::util::ParseSchemaRowsState>,
},
/// Recovering sequence descriptors from each `__turso_internal_seq_*`
/// backing table via SQL (or grafting the VACUUM-preserved map). The
/// per-backing-table descriptor read yields IO, so the worklist and the
/// in-flight statement are carried across re-entry here.
PopulateSequences {
/// `(backing_table_name, seq_name)` worklist; `None` until lazily
/// computed. Left empty when preserved sequences are grafted.
pending: Option<crate::alloc::Vec<(String, String)>>,
/// Index of the backing table currently being read.
idx: usize,
/// In-flight descriptor `SELECT`, created lazily per backing table.
stmt: Option<Box<Statement>>,
/// Descriptor row `(start, inc, min, max, cycle)` captured from `stmt`.
meta: Option<(i64, i64, i64, i64, bool)>,
/// Sequence reconstructed from `meta`, retained while the watermark
/// query yields IO.
seq: Option<crate::schema::Sequence>,
/// In-flight watermark `SELECT`, created after `seq` is known.
watermark_stmt: Option<Box<Statement>>,
/// Watermark row `(value, is_called)` captured from `watermark_stmt`.
watermark_row: Option<(i64, bool)>,
},
/// Loading custom type definitions from the internal types table.
LoadTypes {
stmt: Box<Statement>,
type_rows: Vec<String>,
},
/// Best-effort ANALYZE-stats refresh before finalizing.
RefreshStats {
stats: crate::stats::RefreshAnalyzeStatsState,
},
}
#[cfg(not(feature = "fs"))]
#[derive(Default)]
pub(crate) enum AttachDatabaseState {
#[default]
Start,
}
#[cfg(feature = "fs")]
#[derive(Default)]
pub(crate) enum AttachDatabaseState {
#[default]
Start,
Init(Box<AttachDatabaseInitState>),
Bootstrap(Box<AttachDatabaseBootstrapState>),
Publish {
alias: String,
db: Arc<Database>,
pager: Arc<Pager>,
},
Done,
}
#[cfg(feature = "fs")]
pub(crate) struct AttachDatabaseInitState {
alias: String,
reserved_space: Option<u8>,
db: Arc<Database>,
attached_is_fresh: bool,
encryption_key: Option<EncryptionKey>,
init_st: crate::InitState,
}
#[cfg(feature = "fs")]
pub(crate) struct AttachDatabaseBootstrapState {
alias: String,
db: Arc<Database>,
pager: Arc<Pager>,
encryption_key: Option<EncryptionKey>,
bootstrap_conn: Option<Arc<Connection>>,
bootstrap_st: crate::mvcc::database::BootstrapState,
}
/// Re-entrant driver state for
/// [`Connection::load_sequence_descriptors_via_sql_nonblock`]. Walks every
/// `__turso_internal_seq_*` backing table and registers its descriptor,
/// carrying the worklist and the in-flight descriptor read across IO yields.
#[derive(Default)]
pub enum LoadSequenceDescriptorsState {
#[default]
Start,
Reading {
/// `(backing_table_name, seq_name)` worklist captured from the schema.
pending: crate::alloc::Vec<(String, String)>,
/// Index of the backing table currently being read.
idx: usize,
/// In-flight descriptor `SELECT`, created lazily per backing table.
stmt: Option<Box<Statement>>,
/// Descriptor row `(start, inc, min, max, cycle)` captured from `stmt`.
meta: Option<(i64, i64, i64, i64, bool)>,
/// Sequence reconstructed from `meta`, retained while the watermark
/// query yields IO.
seq: Option<crate::schema::Sequence>,
/// In-flight watermark `SELECT`, created after `seq` is known.
watermark_stmt: Option<Box<Statement>>,
/// Watermark row `(value, is_called)` captured from `watermark_stmt`.
watermark_row: Option<(i64, bool)>,
},
}
/// Re-entrant driver state for
/// [`Connection::sync_autoincrement_backing_tables_from_sqlite_sequence_nonblock`].
#[derive(Default)]
pub enum SyncAutoincrementState {
#[default]
Start,
/// Reading all `(name, seq)` rows from `sqlite_sequence`.
ReadSeqRows {
stmt: Box<Statement>,
rows: Vec<(String, i64)>,
},
/// Per-table: read the backing `MAX(value)` then maybe upsert a watermark.
Process {
rows: Vec<(String, i64)>,
idx: usize,
sub: SyncRowStep,
},
}
/// Per-row sub-state of [`SyncAutoincrementState::Process`].
#[derive(Default)]
pub enum SyncRowStep {
/// Resolve `rows[idx]`'s backing table and start reading `MAX(value)`.
#[default]
Start,
/// Reading `MAX(value)` from the backing table.
ReadMax {
backing_table_name: String,
stmt: Box<Statement>,
current_max: Option<i64>,
},
/// Running the `INSERT OR REPLACE` watermark upsert.
Upsert { stmt: Box<Statement> },
}
/// Database connection handle.
///
/// If you add a setting that affects SQL compilation or execution, call
/// `bump_prepare_context_generation()` in its setter so cached prepared
/// statements know they need to be reprepared.
pub struct Connection {
pub(crate) db: Arc<Database>,
pub(crate) pager: ArcSwap<Pager>,
pub(crate) schema: RwLock<Arc<Schema>>,
/// Per-database schema cache (database_index -> schema)
/// Loaded lazily to avoid copying all schemas on connection open
pub(super) database_schemas: RwLock<HashMap<usize, Arc<Schema>>>,
/// Whether to automatically commit transaction
pub(crate) auto_commit: AtomicBool,
pub(super) transaction_state: AtomicTransactionState,
/// True when an unfinished write statement inside an explicit transaction
/// was reset or dropped and there was no statement savepoint to undo only
/// that statement. COMMIT must roll back the whole transaction.
pub(crate) poisoned_tx: AtomicBool,
pub(super) last_insert_rowid: AtomicI64,
pub(crate) changes: AtomicI64,
pub(crate) total_changes: AtomicI64,
pub(crate) syms: parking_lot::RwLock<SymbolTable>,
pub(super) _shared_cache: bool,
pub(super) cache_size: AtomicI32,
/// page size used for an uninitialized database or the next vacuum command.
/// it's not always equal to the current page size of the database
pub(super) page_size: AtomicU16,
/// Allowed automatic WAL maintenance actions for this connection.
/// Stored as the `bits()` of a `WalAutoActions`. Default is
/// `WalAutoActions::all_enabled()`. `wal_auto_actions_disable` clears
/// every bit, opting out of both auto-checkpoint and WAL header
/// restart — sync-engine consumers rely on the latter staying disabled
/// because rotating the WAL header invalidates their published
/// watermarks.
pub(super) wal_auto_actions: AtomicU8,
/// Whether MVCC commits should include portable logical-change metadata in
/// the logical log.
///
/// This is off by default because the metadata is only useful for raw-log
/// consumers such as sync clients.
#[cfg(feature = "conn_raw_api")]
pub(super) portable_logical_changes_enabled: AtomicBool,
#[cfg(feature = "conn_raw_api")]
pub(super) mvcc_log_metadata: RwLock<HashMap<String, String>>,
pub(super) capture_data_changes: RwLock<Option<CaptureDataChangesInfo>>,
/// CDC v2: transaction ID for grouping CDC records by transaction.
/// -1 means unset (will be assigned on first CDC write in the transaction).
pub(crate) cdc_transaction_id: AtomicI64,
pub(super) closed: AtomicBool,
/// Per-connection state for the `TEMP` schema (pager, last-committed
/// snapshot, dirty-schema flag). See `TempDbContext`.
pub(crate) temp: TempDbContext,
/// Attached databases
pub(super) attached_databases: RwLock<DatabaseCatalog>,
pub(super) query_only: AtomicBool,
pub(super) vdbe_trace: AtomicBool,
/// If enabled, the UPDATE/DELETE statements must have a WHERE clause
pub(super) dml_require_where: AtomicBool,
/// SQLite DQS misfeature: when ON (default), unresolved double-quoted identifiers
/// in DML statements fall back to string literals instead of raising an error.
pub(super) dqs_dml: AtomicBool,
/// Deprecated pragma: when ON, column names include table prefix (TABLE.COLUMN)
pub(super) full_column_names: AtomicBool,
/// Deprecated pragma: when ON (default), column refs use just the column name
pub(super) short_column_names: AtomicBool,
/// Per-connection runtime extension loading flag.
pub(super) enable_load_extension: AtomicBool,
/// Cumulative count of autonomous sequence inner-tx retries (each
/// `WriteWriteConflict` / `BusySnapshot` / `Conflict` that
/// `op_sequence_commit_inner_tx` absorbs via its retry budget bumps
/// this). Lives on the connection rather than `ProgramState` because
/// autocommit nextval/setval allocates a fresh `ProgramState` per
/// statement — the per-state counter resets on every Step and can't
/// witness across-statement contention. Tests use this counter to
/// assert the hot path is conflict-free: any non-zero increment on
/// a non-CYCLE nextval means inline backing-table compaction
/// (or another contended write) was reintroduced.
pub(crate) sequence_inner_retries: AtomicU64,
pub(crate) mv_tx: RwLock<Option<(crate::mvcc::database::TxID, TransactionMode)>>,
/// Per-attached-database MVCC transactions.
/// Main DB uses `mv_tx` above for zero-cost hot path access.
pub(crate) attached_mv_txs:
RwLock<HashMap<usize, (crate::mvcc::database::TxID, TransactionMode)>>,
#[cfg(any(test, injected_yields))]
pub(super) yield_injector: RwLock<Option<Arc<dyn YieldInjector>>>,
#[cfg(any(test, injected_yields))]
pub(super) failure_injector: RwLock<Option<Arc<dyn FailureInjector>>>,
#[cfg(any(test, injected_yields))]
pub(super) yield_instance_id_counter: AtomicU64,
/// Per-connection view transaction states for uncommitted changes. This represents
/// one entry per view that was touched in the transaction.
pub(crate) view_transaction_states: AllViewsTxState,
/// Connection-level metrics aggregation
pub metrics: RwLock<ConnectionMetrics>,
/// Greater than zero if connection executes a program within a program
/// This is necessary in order for connection to not "finalize" transaction (commit/abort) when program ends
/// (because parent program is still pending and it will handle "finalization" instead)
///
/// The state is integer as we may want to spawn deep nested programs (e.g. Root -[run]-> S1 -[run]-> S2 -[run]-> ...)
/// and we need to track current nestedness depth in order to properly understand when we will reach the root back again
pub(super) nestedness: AtomicI32,
/// Stack of currently compiling triggers to prevent recursive trigger subprogram compilation
pub(super) compiling_triggers: RwLock<Vec<Arc<Trigger>>>,
/// Stack of currently executing triggers to prevent recursive trigger execution
/// Only prevents the same trigger from firing again, allowing different triggers on the same table to fire
pub(super) executing_triggers: RwLock<Vec<Arc<Trigger>>>,
pub(crate) encryption_key: RwLock<Option<EncryptionKey>>,
pub(super) encryption_cipher_mode: AtomicCipherMode,
pub(super) sync_mode: AtomicSyncMode,
pub(super) temp_store: AtomicTempStore,
pub(super) data_sync_retry: AtomicBool,
/// Busy handler for lock contention
/// Default is BusyHandler::None (return SQLITE_BUSY immediately)
pub(super) busy_handler: RwLock<BusyHandler>,
/// Step-based progress callback for SQLite-compatible cancellation hooks.
pub(super) progress_handler: ProgressHandler,
/// Maximum execution time for a single statement on this connection.
/// `Duration::ZERO` means disabled.
pub(super) query_timeout_ms: AtomicU64,
/// True when sqlite3_interrupt()-style cancellation is pending for active root statements.
pub(super) interrupt_requested: AtomicBool,
/// Whether this is an internal connection used for MVCC bootstrap
pub(super) is_mvcc_bootstrap_connection: AtomicBool,
/// Whether pragma foreign_keys=ON for this connection
pub(super) fk_pragma: AtomicBool,
pub(crate) fk_deferred_violations: AtomicIsize,
/// Number of active top-level write statements on this connection.
///
/// This is currently only 0 or 1. We return Busy instead of allowing a
/// second same-connection writer to start.
pub(crate) n_active_writes: AtomicI32,
/// Number of active root statements currently executing on this connection.
/// This is Turso's equivalent of SQLite's top-level active-VDBE count
/// (`db->nVdbeActive`) for user statements, excluding internal helpers and
/// subprogram execution.
pub(crate) n_active_root_statements: AtomicI32,
/// Whether pragma ignore_check_constraints=ON for this connection
pub(super) check_constraints_pragma: AtomicBool,
/// Track when each virtual table instance is currently in transaction.
pub(crate) vtab_txn_states: RwLock<HashSet<u64>>,
/// Connection-level named savepoint stack used to mirror savepoint state
/// onto temp/attached databases that start participating after SAVEPOINT.
pub(crate) named_savepoints: RwLock<Vec<NamedSavepointFrame>>,
/// True while this connection is rebuilding its schema from sqlite_schema.
/// Internal helper statements used during reload must not recursively
/// trigger another schema reparse on the same connection.
pub(crate) schema_reparse_in_progress: AtomicBool,
/// Generation counter bumped whenever any setting that affects PrepareContext
/// changes. Allows prepared statements to cheaply detect when they need to be
/// reprepared (single u64 comparison instead of rebuilding the full context).
/// IMPORTANT: this is a bit of a regression landmine because the generation
/// MUST be incremented whenever any setting that affects PrepareContext changes,
/// and this is not currently centralized; each setter bumps the generation individually.
pub(crate) prepare_context_generation: AtomicU64,
/// Per-connection last-returned value for each sequence (for currval()).
pub(crate) sequence_currvals: RwLock<HashMap<String, i64>>,
}
// SAFETY: This needs to be audited for thread safety.
// See: https://github.com/tursodatabase/turso/issues/1552
crate::assert::assert_send_sync!(Connection);
impl Drop for Connection {
fn drop(&mut self) {
if !self.is_closed() {
// Roll back any active MVCC transactions so that MvStore entries
// don't leak and block future checkpoints. The tx may have
// already been committed/aborted externally (e.g. by tests that
// manipulate MvStore directly), so only rollback if still active.
if let Some(mv_store) = self.db.get_mv_store().as_ref() {
if let Some(tx_id) = self.get_mv_tx_id() {
let pager = self.pager.load();
if mv_store.is_tx_rollbackable(tx_id) {
mv_store.rollback_tx(tx_id, pager.clone(), self, MAIN_DB_ID);
} else {
self.set_mv_tx(None);
}
pager.end_read_tx();
}
}
self.rollback_attached_mvcc_txs(false);
// Release any WAL locks the connection might be holding.
// This prevents deadlocks if a connection is dropped (e.g., due to a panic)
// while holding a read or write lock.
let pager = self.pager.load();
if let Some(wal) = &pager.wal {
if wal.holds_write_lock() {
wal.end_write_tx();
}
if wal.holds_read_lock() {
wal.end_read_tx();
}
}
// Also release WAL locks on all attached database pagers
self.with_all_attached_pagers_with_index(|attached_pagers| {
for (_, attached_pager) in attached_pagers {
if let Some(wal) = &attached_pager.wal {
if wal.holds_write_lock() {
wal.end_write_tx();
}
if wal.holds_read_lock() {
wal.end_read_tx();
}
}
}
});
// if connection wasn't properly closed, decrement the connection counter
self.db
.n_connections
.fetch_sub(1, crate::sync::atomic::Ordering::SeqCst);
}
}
}
impl Connection {
fn schema_reparse_guard(self: &Arc<Connection>) -> SchemaReparseGuard {
let was_reparsing = self.schema_reparse_in_progress.swap(true, Ordering::SeqCst);
turso_assert!(
!was_reparsing,
"schema reparse must not recurse on the same connection"
);
SchemaReparseGuard {
connection: self.clone(),
}
}
pub(crate) fn schema_reparse_in_progress(&self) -> bool {
self.schema_reparse_in_progress.load(Ordering::Acquire)
}
pub(crate) fn empty_temp_schema(&self) -> Arc<Schema> {
// with_options only fails if built-in type SQL is malformed (programmer bug).
let mut schema = Schema::with_options(self.db.experimental_custom_types_enabled())
.expect("built-in type definitions are malformed");
schema.generated_columns_enabled = self.db.experimental_generated_columns_enabled();
Arc::new(schema)
}
fn make_temp_database_opts(&self) -> DatabaseOpts {
DatabaseOpts::new()
.with_views(self.db.experimental_views_enabled())
.with_custom_types(self.db.experimental_custom_types_enabled())
.with_index_method(self.db.experimental_index_method_enabled())
.with_vacuum(self.db.experimental_vacuum_enabled())
.with_generated_columns(self.db.experimental_generated_columns_enabled())
.with_without_rowid(self.db.experimental_without_rowid_enabled())
}
fn effective_temp_store(&self) -> crate::TempStore {
let temp_store = self.get_temp_store();
#[cfg(feature = "fs")]
{
temp_store
}
#[cfg(not(feature = "fs"))]
{
let _ = temp_store;
crate::TempStore::Memory
}
}
#[cfg(feature = "fs")]
fn create_temp_database(&self) -> Result<TempDatabase> {
let temp_store = self.effective_temp_store();
let db_opts = self.make_temp_database_opts();
let page_size = self.get_page_size();
if matches!(temp_store, crate::TempStore::Memory) {
let io: Arc<dyn IO> = Arc::new(MemoryIO::new());
let db = Database::open_file_with_flags(
io,
crate::util::MEMORY_PATH,
OpenFlags::Create,
db_opts,
None,
)?;
let pager = Arc::new(db._init(None)?);
pager.set_initial_page_size(page_size)?;
return Ok(TempDatabase {
db,
pager,
#[cfg(not(target_family = "wasm"))]
_temp_dir: None,
});
}
#[cfg(not(target_family = "wasm"))]
{
let temp_dir = self.create_tempdir()?;
let temp_path = temp_dir.path().join("tursodb-temp.db");
let temp_path_str = temp_path.to_str().ok_or_else(|| {
LimboError::InternalError("temp db path is not valid UTF-8".into())
})?;
// Always create a fresh IO for the temp file. Cloning the
// main db's IO is wrong when the main db uses a mock /
// simulated backend (e.g. the deterministic simulator
// with `--io-backend=memory`) that can't access real
// filesystem paths produced by `tempfile::tempdir()`.
let io = Database::io_for_path(temp_path_str)?;
let db = Database::open_file_with_flags(
io,
temp_path_str,
OpenFlags::Create,
db_opts,
None,
)?;
let pager = Arc::new(db._init(None)?);
pager.set_initial_page_size(page_size)?;
Ok(TempDatabase {
db,
pager,
_temp_dir: Some(temp_dir),
})
}
#[cfg(target_family = "wasm")]
{
let io: Arc<dyn IO> = Arc::new(MemoryIO::new());
let db = Database::open_file_with_flags(
io,
crate::util::MEMORY_PATH,
OpenFlags::Create,
db_opts,
None,
)?;
let pager = Arc::new(db._init(None)?);
pager.set_initial_page_size(page_size)?;
Ok(TempDatabase { db, pager })
}
}
#[cfg(not(feature = "fs"))]
fn create_temp_database(&self) -> Result<TempDatabase> {
let io: Arc<dyn IO> = Arc::new(MemoryIO::new());
let db = Database::open_file_with_flags(
io,
crate::util::MEMORY_PATH,
OpenFlags::Create,
self.make_temp_database_opts(),
None,
)?;
let pager = Arc::new(db._init(None)?);
pager.set_initial_page_size(self.get_page_size())?;
Ok(TempDatabase {
db,
pager,
#[cfg(not(target_family = "wasm"))]
_temp_dir: None,
})
}
pub(crate) fn ensure_temp_database(&self) -> Result<()> {
if self.temp.database.read().is_some() {
return Ok(());
}
let temp_db = self.create_temp_database()?;
let mut guard = self.temp.database.write();
if guard.is_none() {
*guard = Some(temp_db);
}
Ok(())
}
/// Tear down the per-connection temp database.
///
/// Drops the temp pager, clears the committed schema snapshot and
/// the dirty-schema flag. Called by `set_temp_store` when the user
/// changes `PRAGMA temp_store` outside of an explicit transaction.
fn reset_temp_database(&self) {
if let Some(temp_db) = self.temp.database.write().take() {
temp_db.pager.rollback_attached();
}
*self.temp.committed_schema.write() = None;
self.temp.schema_did_change.store(false, Ordering::Release);
}
/// Flag a temp-schema mutation within the current transaction so the
/// commit/rollback path knows to snapshot or restore the in-memory
/// temp schema. Called from `SetCookie` for `TEMP_DB_ID`.
pub(crate) fn mark_temp_schema_did_change(&self) {
// If we're marking the temp schema dirty, temp DDL must have
// just run against the temp pager — which means the temp
// database was initialized. The opposite state is unreachable.
turso_assert!(
self.temp.database.read().is_some(),
"mark_temp_schema_did_change called without an initialized temp database"
);
self.temp.schema_did_change.store(true, Ordering::Release);
}
/// On successful commit, snapshot the current `temp_db.db.schema`
/// into `committed_temp_schema` so a future full-txn rollback can
/// restore it. No-op if no temp DDL ran in this transaction.
pub(crate) fn commit_temp_schema(&self) {
if !self.temp.schema_did_change.load(Ordering::Acquire) {
return;
}
// `schema_did_change` is only ever set by
// `mark_temp_schema_did_change`, which asserts temp is
// initialized. If it's somehow clear here we have a logic
// bug — no safe recovery, so fail loud.
let guard = self.temp.database.read();
turso_assert!(
guard.is_some(),
"commit_temp_schema: schema_did_change set but temp is uninitialized"
);
let snap = guard
.as_ref()
.expect("asserted above")
.db
.schema
.lock()
.clone();
drop(guard);
// save snapshot for potential future rollback.
*self.temp.committed_schema.write() = Some(snap);
self.temp.schema_did_change.store(false, Ordering::Release);
}
/// On full-txn rollback, restore `temp_db.db.schema` from the last
/// committed snapshot. If nothing was ever committed, reset to an
/// empty schema (matches the disk state the pager rolled back to).
pub(crate) fn rollback_temp_schema(&self) {
if !self.temp.schema_did_change.load(Ordering::Acquire) {
return;
}
// Same invariant as `commit_temp_schema` — the flag can only
// be set while temp is initialized.
let committed = self.temp.committed_schema.read().clone();
{
let guard = self.temp.database.read();
turso_assert!(
guard.is_some(),
"rollback_temp_schema: schema_did_change set but temp is uninitialized"
);
let temp_db = guard.as_ref().expect("asserted above");
match committed {
Some(snap) => *temp_db.db.schema.lock() = snap,
None => *temp_db.db.schema.lock() = self.empty_temp_schema(),
}
}
self.temp.schema_did_change.store(false, Ordering::Release);
self.bump_prepare_context_generation();
}
/// Bump the prepare context generation counter. Must be called whenever any
/// connection setting that is tracked in `PrepareContext` changes, so that
/// prepared statements know they need to be reprepared.
#[inline]
pub(crate) fn bump_prepare_context_generation(&self) {
self.prepare_context_generation
.fetch_add(1, Ordering::Release);
}
#[inline]
pub(crate) fn prepare_context_generation(&self) -> u64 {
self.prepare_context_generation.load(Ordering::Acquire)
}
/// check if connection executes nested program (so it must not do any "finalization" work as parent program will handle it)
pub fn is_nested_stmt(&self) -> bool {
self.nestedness.load(Ordering::SeqCst) > 0
}
/// starts nested program execution
pub fn start_nested(&self) {
self.nestedness.fetch_add(1, Ordering::SeqCst);
}
/// ends nested program execution
pub fn end_nested(&self) {
self.nestedness.fetch_add(-1, Ordering::SeqCst);
}
/// Check if a specific trigger is currently compiling (for recursive trigger prevention)
pub fn trigger_is_compiling(&self, trigger: &Arc<Trigger>) -> bool {
let compiling = self.compiling_triggers.read();
if let Some(trigger) = compiling.iter().find(|t| Arc::ptr_eq(t, trigger)) {
tracing::debug!("Trigger is already compiling: {}", trigger.name);
return true;
}
false
}
pub fn start_trigger_compilation(&self, trigger: Arc<Trigger>) {
tracing::debug!("Starting trigger compilation: {}", trigger.name);
self.compiling_triggers.write().push(trigger);
}
pub fn end_trigger_compilation(&self) {
tracing::debug!(
"Ending trigger compilation: {:?}",
self.compiling_triggers.read().last().map(|t| &t.name)
);
self.compiling_triggers.write().pop();
}
/// Check if a specific trigger is currently executing (for recursive trigger prevention)
pub fn is_trigger_executing(&self, trigger: &Arc<Trigger>) -> bool {
let executing = self.executing_triggers.read();
if let Some(active_trigger) = executing.iter().find(|t| Arc::ptr_eq(t, trigger)) {
tracing::debug!("Trigger is already executing: {}", trigger.name);
debug_assert!(Arc::ptr_eq(active_trigger, trigger));
return true;
}
false
}
pub fn start_trigger_execution(&self, trigger: Arc<Trigger>) {
tracing::debug!("Starting trigger execution: {}", trigger.name);
self.executing_triggers.write().push(trigger);
}
pub fn end_trigger_execution(&self) {
tracing::debug!(
"Ending trigger execution: {:?}",
self.executing_triggers.read().last().map(|t| &t.name)
);
self.executing_triggers.write().pop();
}
fn should_retry_cross_process_schema_lookup(
self: &Arc<Connection>,
err: &LimboError,
) -> Result<bool> {
let LimboError::ParseError(msg) = err else {
return Ok(false);
};
if !msg.contains("no such table") && !msg.contains("table not found") {
return Ok(false);
}
if self.get_tx_state() != TransactionState::None {
return Ok(false);
}
if self.db.shared_wal_coordination()?.is_none() {
return Ok(false);
}
self.maybe_reparse_schema()?;
Ok(true)
}
#[turso_macros::trace_stack]
fn compile_cmd(
self: &Arc<Connection>,
cmd: Cmd,
input: &str,
) -> Result<(Program, Arc<Pager>, QueryMode)> {
self.maybe_update_schema();
let syms = self.syms.read();
let pager = self.pager.load().clone();
let mode = QueryMode::new(&cmd);
let (Cmd::Stmt(stmt) | Cmd::Explain(stmt) | Cmd::ExplainQueryPlan(stmt)) = cmd;
let schema = self.schema.read().clone();
match translate::translate(
&schema,
stmt,
pager.clone(),
self.clone(),
&syms,
mode,
input,
) {
Ok(program) => Ok((program, pager, mode)),
Err(err) if self.should_retry_cross_process_schema_lookup(&err)? => {
// Cold path: re-parse the SQL from scratch after schema refresh rather
// than cloning the original AST, which can overflow the stack
// on deeply nested expression trees.
drop(syms);
let cmd = {
crate::stack::trace_stack!("schema_retry_parse");
let mut parser = Parser::new(input.as_bytes());
let Some(cmd) = parser.next_cmd()? else {
return Err(err);
};
cmd
};
self.maybe_update_schema();
let syms = self.syms.read();
let pager = self.pager.load().clone();
let mode = QueryMode::new(&cmd);
let (Cmd::Stmt(stmt) | Cmd::Explain(stmt) | Cmd::ExplainQueryPlan(stmt)) = cmd;
let schema = self.schema.read().clone();
translate::translate(
&schema,
stmt,
pager.clone(),
self.clone(),
&syms,
mode,
input,
)
.map(|program| (program, pager, mode))
}
Err(err) => Err(err),
}
}
pub fn prepare(self: &Arc<Connection>, sql: impl AsRef<str>) -> Result<Statement> {
self._prepare(sql)
}
pub(crate) fn prepare_internal(
self: &Arc<Connection>,
sql: impl AsRef<str>,
) -> Result<Statement> {
self.prepare_with_origin(sql, StatementOrigin::InternalHelper)
}
#[instrument(skip_all, level = Level::DEBUG)]
pub fn _prepare(self: &Arc<Connection>, sql: impl AsRef<str>) -> Result<Statement> {
self.prepare_with_origin(sql, StatementOrigin::Root)
}
#[turso_macros::trace_stack]
fn prepare_with_origin(
self: &Arc<Connection>,
sql: impl AsRef<str>,
origin: StatementOrigin,
) -> Result<Statement> {
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
if sql.as_ref().is_empty() {
return Err(LimboError::InvalidArgument(
"The supplied SQL string contains no statements".to_string(),
));
}
let needs_nested_guard = origin.needs_nested_guard();
if needs_nested_guard {
self.start_nested();
}
let result = (|| {
let sql = sql.as_ref();
tracing::debug!("Preparing: {}", sql);
let (cmd, byte_offset_end) = {
crate::stack::trace_stack!("parse");
let mut parser = Parser::new(sql.as_bytes());
let cmd = match parser.next_cmd()? {
Some(cmd) => cmd,
None => {
return Err(LimboError::InvalidArgument(
"The supplied SQL string contains no statements".to_string(),
));
}
};
(cmd, parser.offset())
};
let input = str::from_utf8(&sql.as_bytes()[..byte_offset_end])
.unwrap()
.trim();
let (program, pager, mode) = self.compile_cmd(cmd, input)?;
Ok(Statement::new_with_origin(
program,
pager,
mode,
byte_offset_end,
origin,
needs_nested_guard,
))
})();
if result.is_err() && needs_nested_guard {
self.end_nested();
}
result
}
/// Prepare a statement from an AST node directly, skipping SQL parsing.
/// This is more efficient when AST is already available or constructed programmatically.
pub fn prepare_stmt(self: &Arc<Connection>, stmt: ast::Stmt) -> Result<Statement> {
self.prepare_stmt_with_origin(stmt, StatementOrigin::Root)
}
#[turso_macros::trace_stack]
fn prepare_stmt_with_origin(
self: &Arc<Connection>,
stmt: ast::Stmt,
origin: StatementOrigin,
) -> Result<Statement> {
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
let needs_nested_guard = origin.needs_nested_guard();
if needs_nested_guard {
self.start_nested();
}
let result = (|| {
self.maybe_update_schema();
let syms = self.syms.read();
let pager = self.pager.load().clone();
let mode = QueryMode::Normal;
let schema = self.schema.read().clone();
let program = translate::translate(
&schema,
stmt,
pager.clone(),
self.clone(),
&syms,
mode,
"<ast>", // No SQL input string available
)?;
Ok(Statement::new_with_origin(
program,
pager,
mode,
0,
origin,
needs_nested_guard,
))
})();
if result.is_err() && needs_nested_guard {
self.end_nested();
}
result
}
/// Whether this is an internal connection used for MVCC bootstrap
pub fn is_mvcc_bootstrap_connection(&self) -> bool {
self.is_mvcc_bootstrap_connection.load(Ordering::SeqCst)
}
/// Promote MVCC bootstrap connection to a regular connection so it reads from the MV store again.
pub fn promote_to_regular_connection(&self) {
assert!(self.is_mvcc_bootstrap_connection.load(Ordering::SeqCst));
self.is_mvcc_bootstrap_connection
.store(false, Ordering::SeqCst);
}
/// Demote regular connection to MVCC bootstrap connection so it does not read from the MV store.
pub fn demote_to_mvcc_connection(&self) {
assert!(!self.is_mvcc_bootstrap_connection.load(Ordering::SeqCst));
self.is_mvcc_bootstrap_connection
.store(true, Ordering::SeqCst);
}
/// Parse schema from scratch if version of schema for the connection differs from the schema cookie in the root page.
/// This function must be called outside of any transaction because internally it will start transaction session by itself.
/// In multi-process mode, this is the only way to discover schema changes made by other processes.
pub fn maybe_reparse_schema(self: &Arc<Connection>) -> Result<()> {
let pager = self.pager.load().clone();
let mv_store = self.mv_store();
// maybe_reparse_schema must be called outside any explicit transaction
// because it starts its own read transaction to load a fresh view of
// sqlite_schema from disk.
if self.get_tx_state() != TransactionState::None {
return Ok(());
}
let had_main_mv_tx = self.get_mv_tx().is_some();
if self.db.shared_wal_coordination()?.is_some() {
// Cross-process schema changes can leave page 1 and sqlite_schema
// pages cached from an earlier WAL snapshot. Drop the cache before
// probing the cookie so reparsing observes the current committed view.
pager.clear_page_cache(false);
pager.set_schema_cookie(None);
}
let on_disk_schema_version = if mv_store.as_ref().is_some() {
self.read_current_schema_cookie().or_else(|err| match err {
LimboError::Page1NotAlloc => Ok(0),
other => Err(other),
})?
} else {
// first, quickly read schema_version from the root page in order to check if schema changed
pager.begin_read_tx()?;
let on_disk_schema_version = pager
.io
.block(|| pager.with_header(|header| header.schema_cookie));
let on_disk_schema_version = match on_disk_schema_version {
Ok(db_schema_version) => db_schema_version.get(),
Err(LimboError::Page1NotAlloc) => {
// this means this is a fresh db, so return a schema version of 0
0
}
Err(err) => {
pager.end_read_tx();
return Err(err);
}
};
pager.end_read_tx();
on_disk_schema_version
};
let db_schema_version = self.db.schema.lock().schema_version;
tracing::debug!(
"path: {}, db_schema_version={} vs on_disk_schema_version={}",
self.db.path,
db_schema_version,
on_disk_schema_version
);
// if schema_versions matches - exit early
if db_schema_version == on_disk_schema_version {
return Ok(());
}
// start read transaction manually, because we will read schema cookie once again and
// we must be sure that it will consistent with schema content
//
// from now on we must be very careful with errors propagation
// in order to not accidentally keep read transaction opened
pager.begin_read_tx()?;
self.set_tx_state(TransactionState::Read);
let reparse_result = self.reparse_schema();
let previous = self.transaction_state.swap(TransactionState::None);
turso_assert!(
matches!(previous, TransactionState::None | TransactionState::Read),
"unexpected end transaction state"
);
// close opened transaction if it was kept open
// (in most cases, it will be automatically closed if stmt was executed properly)
if previous == TransactionState::Read {
pager.end_read_tx();
}
if !had_main_mv_tx {
self.clear_internal_main_mvcc_tx(&pager);
}
reparse_result?;
let schema = self.schema.read().clone();
self.db.update_schema_if_newer(schema);
Ok(())
}
/// Parse schema from scratch even if the schema cookie did not change.
///
/// Sync replace-base can install a page snapshot outside ordinary SQL DDL.
/// The replacement may reuse the same schema cookie while changing root
/// pages, so cookie-based refresh would keep stale btree metadata.
#[cfg(feature = "conn_raw_api")]
pub fn force_reparse_schema(self: &Arc<Connection>) -> Result<()> {
self.force_reparse_schema_inner(true)
}
/// Like [`Self::force_reparse_schema`], but refreshes only this connection's
/// own schema snapshot without publishing it to the shared database cache.
///
/// Use this when the caller must further mutate the schema before it becomes
/// visible to other connections.
pub fn force_reparse_schema_without_publish(self: &Arc<Connection>) -> Result<()> {
self.force_reparse_schema_inner(false)
}
fn force_reparse_schema_inner(self: &Arc<Connection>, publish: bool) -> Result<()> {
if self.get_tx_state() != TransactionState::None {
return Err(LimboError::Busy);
}
if self.get_mv_tx().is_some() || self.next_attached_mv_tx().is_some() {
return Err(LimboError::Busy);
}
let pager = self.pager.load().clone();
pager.clear_page_cache(false);
pager.set_schema_cookie(None);
pager.begin_read_tx()?;
self.set_tx_state(TransactionState::Read);
let reparse_result = self.reparse_schema();
let previous = self.transaction_state.swap(TransactionState::None);
turso_assert!(
matches!(previous, TransactionState::None | TransactionState::Read),
"unexpected end transaction state"
);
if previous == TransactionState::Read {
pager.end_read_tx();
}
self.clear_internal_main_mvcc_tx(&pager);
reparse_result?;
if publish {
let schema = self.schema.read().clone();
self.db.update_schema_if_newer(schema);
}
Ok(())
}
fn clear_internal_main_mvcc_tx(&self, pager: &Arc<Pager>) {
let Some(tx_id) = self.get_mv_tx_id() else {
return;
};
if let Some(mv_store) = self.mv_store().as_ref() {
if mv_store.is_tx_rollbackable(tx_id) {
mv_store.rollback_tx(tx_id, pager.clone(), self, MAIN_DB_ID);
} else {
self.set_mv_tx(None);
}
} else {
self.set_mv_tx(None);
}
pager.cleanup_read_tx();
}
/// Blocking shim: drives [`Self::reparse_schema_nonblock`] to completion.
/// Retained for the many synchronous callers (statement reprepare, attach,
/// extension load, vacuum). The genuinely non-blocking callers (MVCC
/// bootstrap, the open-db state machine) drive `*_nonblock` directly.
pub(crate) fn reparse_schema(self: &Arc<Connection>) -> Result<()> {
let io = self.pager.load().io.clone();
let mut state = ReparseSchemaState::default();
io.block(|| self.reparse_schema_nonblock(&mut state))
}
/// VACUUM-only reparse that grafts a caller-supplied sequence-
/// descriptor map onto the freshly parsed schema rather than re-
/// reading the backing tables from disk. VACUUM preserves every
/// sequence's definition (start/inc/min/max/cycle) — only physical
/// page locations change — so the source connection's pre-VACUUM
/// sequences map is still valid for the post-VACUUM image.
///
/// Blocking shim: VACUUM runs through the VDBE stepping layer (which
/// does not thread IO out), so it drives the non-blocking reparse to
/// completion here, seeding the `PopulateSequences` phase with the
/// preserved map so it grafts rather than re-reading each backing table.
pub(crate) fn reparse_schema_with_cookie_keeping_sequences(
self: &Arc<Connection>,
cookie: u32,
sequences: rustc_hash::FxHashMap<String, Arc<crate::schema::Sequence>>,
) -> Result<()> {
let io = self.pager.load().io.clone();
let mut state = ReparseSchemaState::default();
// `init_reparse_building` consumes the preserved map exactly once, on
// the first (Start) invocation; `io.block` re-invokes the closure on
// every IO completion, so `take()` yields `Some` only that first time.
let mut preserved = Some(sequences);
io.block(|| {
if matches!(state, ReparseSchemaState::Start) {
state = ReparseSchemaState::Building(Box::new(
self.init_reparse_building(cookie, preserved.take())?,
));
}
self.drive_reparse_building(&mut state)
})
}
/// Non-blocking schema reparse. Reads the current schema cookie, then drives
/// the schema rebuild via the shared [`ReparseSchemaState`].
pub(crate) fn reparse_schema_nonblock(
self: &Arc<Connection>,
state: &mut ReparseSchemaState,
) -> Result<crate::types::IOResult<()>> {
use crate::types::IOResult;
if matches!(state, ReparseSchemaState::Start) {
// read cookie before consuming statement program - otherwise we can
// end up reading cookie with closed transaction state
let cookie = crate::return_if_io!(self.read_current_schema_cookie_nonblock());
*state =
ReparseSchemaState::Building(Box::new(self.init_reparse_building(cookie, None)?));
}
self.drive_reparse_building(state)
}
/// Synchronous setup shared by the reparse entry points: install the cookie,
/// build a fresh schema, capture table-valued functions, install the empty
/// schema (the reprepare hack), and prepare the sqlite_schema scan.
/// `preserved_sequences` is `Some` only for VACUUM, which grafts the map in
/// the `PopulateSequences` phase instead of re-reading the backing tables.
fn init_reparse_building(
self: &Arc<Connection>,
cookie: u32,
preserved_sequences: Option<rustc_hash::FxHashMap<String, Arc<crate::schema::Sequence>>>,
) -> Result<ReparseSchemaInner> {
let guard = self.schema_reparse_guard();
self.pager.load().set_schema_cookie(Some(cookie));
// create fresh schema as some objects can be deleted
let mut fresh = Schema::with_options(self.experimental_custom_types_enabled())?;
fresh.generated_columns_enabled = self.db.experimental_generated_columns_enabled();
fresh.schema_version = cookie;
// Capture built-in table-valued functions (e.g. generate_series, json_each)
// before dropping the old schema. These are registered programmatically and
// don't survive re-parsing from sqlite_schema alone.
let tvfs: Vec<Arc<crate::vtab::VirtualTable>> = self
.schema
.read()
.tables
.values()
.filter_map(|table| match table.as_ref() {
crate::schema::Table::Virtual(vtab)
if matches!(vtab.kind, turso_ext::VTabKind::TableValuedFunction) =>
{
Some(vtab.clone())
}
_ => None,
})
.collect();
// TODO: this is hack to avoid a cyclical problem with schema reprepare
// The problem here is that we prepare a statement here, but when the statement tries
// to execute it, it first checks the schema cookie to see if it needs to reprepare the statement.
// But in this occasion it will always reprepare, and we get an error. So we trick the statement by swapping our schema
// with a new clean schema that has the same header cookie.
self.with_schema_mut(|schema| {
*schema = fresh.try_clone()?;
Ok::<_, crate::alloc::TryReserveError>(())
})??;
let stmt = self.prepare("SELECT * FROM sqlite_schema")?;
// MVCC bootstrap connection gets the "baseline" from the DB file and ignores anything in MV store
let mv_tx = if self.is_mvcc_bootstrap_connection() {
None
} else {
self.get_mv_tx()
};
Ok(ReparseSchemaInner {
_guard: guard,
fresh,
tvfs,
preserved_sequences,
phase: ReparsePhase::ParseSchema {
parse: Box::new(crate::util::ParseSchemaRowsState::new(stmt, mv_tx)),
},
})
}
/// Drive the schema-rebuild phases held in `state` (must be `Building`).
/// Yields IO; on completion installs the finished schema and resets `state`.
fn drive_reparse_building(
self: &Arc<Connection>,
state: &mut ReparseSchemaState,
) -> Result<crate::types::IOResult<()>> {
use crate::types::IOResult;
loop {
let ReparseSchemaState::Building(inner) = state else {
unreachable!("drive_reparse_building requires Building state");
};
match &mut inner.phase {
ReparsePhase::ParseSchema { parse } => {
// Resolver so attached-db qualifiers in temp triggers can be
// mapped to their actual index on this connection.
let attached_resolver = |name: &str| -> Option<usize> {
self.attached_databases
.read()
.get_database_by_name(&crate::util::normalize_ident(name))
.map(|(idx, _)| idx)
};
crate::return_if_io!(crate::util::parse_schema_rows(
parse,
&mut inner.fresh,
&self.syms.read(),
&attached_resolver,
));
// Rehydrate built-in table-valued functions captured at init.
for vtab in &inner.tvfs {
let normalized = crate::util::normalize_ident(&vtab.name);
inner.fresh.tables.entry(normalized).or_insert_with(|| {
Arc::new(crate::schema::Table::Virtual(vtab.clone()))
});
}
// Next: recover sequence descriptors (or graft the VACUUM map).
inner.phase = ReparsePhase::PopulateSequences {
pending: None,
idx: 0,
stmt: None,
meta: None,
seq: None,
watermark_stmt: None,
watermark_row: None,
};
}
ReparsePhase::PopulateSequences {
pending,
idx,
stmt,
meta,
seq,
watermark_stmt,
watermark_row,
} => {
// Lazy init: graft the VACUUM-preserved descriptor map, or
// compute the worklist of backing tables to read from disk.
// When there is real work, install `fresh` first so the
// descriptor SELECTs can resolve the backing tables.
if pending.is_none() {
if let Some(sequences) = inner.preserved_sequences.take() {
inner.fresh.sequences = sequences;
*pending = Some(crate::alloc::vec![]);
} else {
let work = inner.fresh.sequence_backing_table_names();
if !work.is_empty() {
self.with_schema_mut(|schema| {
*schema = inner.fresh.try_clone()?;
Ok::<_, crate::alloc::TryReserveError>(())
})??;
}
*pending = Some(work);
}
}
// Read each remaining backing table's descriptor row. The
// backing table is internal; a missing/unreadable descriptor
// row is on-disk corruption (not "sequence missing"), so any
// failure surfaces as `Corrupt` and fails the open rather than
// silently dropping the sequence.
loop {
let entry = {
let work = pending.as_ref().expect("pending initialized above");
if *idx >= work.len() {
break;
}
work[*idx].clone()
};
let (backing_table_name, seq_name) = entry;
let normalized = crate::util::normalize_ident(&seq_name);
if inner.fresh.sequences.contains_key(&normalized) {
*idx += 1;
*stmt = None;
*meta = None;
*seq = None;
*watermark_stmt = None;
*watermark_row = None;
continue;
}
if seq.is_none() {
crate::return_if_io!(self.read_seq_descriptor_row_nonblock(
&backing_table_name,
&seq_name,
stmt,
meta,
));
*seq = Some(Self::sequence_from_descriptor_meta(
&seq_name,
&backing_table_name,
*meta,
)?);
*stmt = None;
*meta = None;
}
let sequence = seq.as_ref().expect("sequence set above");
crate::return_if_io!(self.read_sequence_watermark_row_nonblock(
&backing_table_name,
sequence,
watermark_stmt,
watermark_row,
));
let watermark = Self::sequence_watermark_from_row(
&backing_table_name,
sequence,
*watermark_row,
)?;
if let Some(mv_store) = self.db.get_mv_store().as_ref() {
mv_store.set_sequence_watermark(&normalized, watermark);
}
let sequence = seq.take().expect("sequence set above");
inner.fresh.sequences.insert(normalized, Arc::new(sequence));
*idx += 1;
*stmt = None;
*meta = None;
*watermark_stmt = None;
*watermark_row = None;
}
// Decide whether to load custom types next.
if self.experimental_custom_types_enabled()
&& inner
.fresh
.tables
.contains_key(crate::schema::TURSO_TYPES_TABLE_NAME)
{
// Temporarily install the schema so we can query against it.
self.with_schema_mut(|schema| {
*schema = inner.fresh.try_clone()?;
Ok::<_, crate::alloc::TryReserveError>(())
})??;
let stmt = self.prepare_internal(format!(
"SELECT name, sql FROM {}",
crate::schema::TURSO_TYPES_TABLE_NAME
))?;
inner.phase = ReparsePhase::LoadTypes {
stmt: Box::new(stmt),
type_rows: Vec::new(),
};
} else {
inner.phase = ReparsePhase::RefreshStats {
stats: Default::default(),
};
}
}
ReparsePhase::LoadTypes { stmt, type_rows } => {
// Type loading is best-effort: log and continue on error.
let scan = (|| -> Result<IOResult<()>> {
crate::return_if_io!(stmt.run_with_row_callback_nonblock(|row| {
type_rows.push(row.get::<&str>(1)?.to_string());
Ok(())
}));
Ok(IOResult::Done(()))
})();
match scan {
Ok(IOResult::IO(io)) => return Ok(IOResult::IO(io)),
Ok(IOResult::Done(())) => {
let type_rows = std::mem::take(type_rows);
if let Err(e) = inner.fresh.load_type_definitions(&type_rows) {
tracing::warn!("Failed to load custom types: {}", e);
}
inner.phase = ReparsePhase::RefreshStats {
stats: Default::default(),
};
}
Err(e) => {
tracing::warn!("Failed to load custom types: {}", e);
inner.phase = ReparsePhase::RefreshStats {
stats: Default::default(),
};
}
}
}
ReparsePhase::RefreshStats { stats } => {
// Best-effort load stats if sqlite_stat1 is present.
crate::return_if_io!(crate::stats::refresh_analyze_stats_nonblock(self, stats));
// Finalize: install the rebuilt schema. Take ownership so the
// guard drops and `state` is reusable.
let ReparseSchemaState::Building(inner) = std::mem::take(state) else {
unreachable!("state is Building");
};
let fresh = inner.fresh;
tracing::debug!(
"reparse_schema: schema_version={}, tables={:?}",
fresh.schema_version,
fresh.tables.keys()
);
self.with_schema_mut(|schema| {
*schema = fresh;
})?;
return Ok(IOResult::Done(()));
}
}
}
}
pub(crate) fn read_current_schema_cookie(&self) -> Result<u32> {
if let Some(mv_store) = self.mv_store().as_ref() {
let tx_id = self.get_mv_tx_id();
mv_store.with_header(|header| header.schema_cookie.get(), tx_id.as_ref())
} else {
let pager = self.pager.load();
pager
.io
.block(|| pager.with_header(|header| header.schema_cookie))
.map(|cookie| cookie.get())
}
}
/// Non-blocking variant of [`Self::read_current_schema_cookie`]. The MVCC
/// path reads an in-memory header (never yields); the pager path may yield
/// while reading page 1. Idempotent across re-entry.
pub(crate) fn read_current_schema_cookie_nonblock(
&self,
) -> Result<crate::types::IOResult<u32>> {
use crate::types::IOResult;
if let Some(mv_store) = self.mv_store().as_ref() {
let tx_id = self.get_mv_tx_id();
let cookie =
mv_store.with_header(|header| header.schema_cookie.get(), tx_id.as_ref())?;
Ok(crate::types::IOResult::Done(cookie))
} else {
let pager = self.pager.load();
let cookie = crate::return_if_io!(pager.with_header(|header| header.schema_cookie));
Ok(crate::types::IOResult::Done(cookie.get()))
}
}
#[instrument(skip_all, level = Level::DEBUG)]
pub fn prepare_execute_batch(self: &Arc<Connection>, sql: impl AsRef<str>) -> Result<()> {
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
if sql.as_ref().is_empty() {
return Err(LimboError::InvalidArgument(
"The supplied SQL string contains no statements".to_string(),
));
}
let sql = sql.as_ref();
tracing::trace!("Preparing and executing batch: {}", sql);
let mut parser = Parser::new(sql.as_bytes());
while let Some(cmd) = parser.next_cmd()? {
let byte_offset_end = parser.offset();
let input = str::from_utf8(&sql.as_bytes()[..byte_offset_end])
.unwrap()
.trim();
let (program, pager, mode) = self.compile_cmd(cmd, input)?;
Statement::new(program, pager.clone(), mode, 0).run_ignore_rows()?;
}
Ok(())
}
#[instrument(skip_all, level = Level::DEBUG)]
pub fn query(self: &Arc<Connection>, sql: impl AsRef<str>) -> Result<Option<Statement>> {
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
let sql = sql.as_ref();
tracing::trace!("Querying: {}", sql);
let mut parser = Parser::new(sql.as_bytes());
let cmd = parser.next_cmd()?;
let byte_offset_end = parser.offset();
let input = str::from_utf8(&sql.as_bytes()[..byte_offset_end])
.unwrap()
.trim();
match cmd {
Some(cmd) => self.run_cmd(cmd, input),
None => Ok(None),
}
}
#[instrument(skip_all, level = Level::DEBUG)]
pub(crate) fn run_cmd(
self: &Arc<Connection>,
cmd: Cmd,
input: &str,
) -> Result<Option<Statement>> {
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
let (program, pager, mode) = self.compile_cmd(cmd, input)?;
let stmt = Statement::new(program, pager, mode, 0);
Ok(Some(stmt))
}
pub fn query_runner<'a>(self: &'a Arc<Connection>, sql: &'a [u8]) -> QueryRunner<'a> {
QueryRunner::new(self, sql)
}
/// Execute will run a query from start to finish taking ownership of I/O because it will run pending I/Os if it didn't finish.
/// TODO: make this api async
#[instrument(skip_all, level = Level::DEBUG)]
#[turso_macros::trace_stack]
pub fn execute(self: &Arc<Connection>, sql: impl AsRef<str>) -> Result<()> {
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
let sql = sql.as_ref();
let mut parser = Parser::new(sql.as_bytes());
while let Some(cmd) = parser.next_cmd()? {
let byte_offset_end = parser.offset();
let input = str::from_utf8(&sql.as_bytes()[..byte_offset_end])
.unwrap()
.trim();
let (program, pager, mode) = self.compile_cmd(cmd, input)?;
{
crate::stack::trace_stack!("run");
Statement::new(program, pager.clone(), mode, 0).run_ignore_rows()?;
}
}
Ok(())
}
#[instrument(skip_all, level = Level::DEBUG)]
pub fn consume_stmt(
self: &Arc<Connection>,
sql: impl AsRef<str>,
) -> Result<Option<(Statement, usize)>> {
let mut parser = Parser::new(sql.as_ref().as_bytes());
let Some(cmd) = parser.next_cmd()? else {
return Ok(None);
};
let byte_offset_end = parser.offset();
let input = str::from_utf8(&sql.as_ref().as_bytes()[..byte_offset_end])
.unwrap()
.trim();
let (program, pager, mode) = self.compile_cmd(cmd, input)?;
let stmt = Statement::new(program, pager, mode, 0);
Ok(Some((stmt, parser.offset())))
}
#[cfg(feature = "fs")]
pub fn from_uri(uri: &str, db_opts: DatabaseOpts) -> Result<(Arc<dyn IO>, Arc<Connection>)> {
use crate::util::MEMORY_PATH;
let opts = OpenOptions::parse(uri)?;
let flags = opts.get_flags()?;
if opts.path == MEMORY_PATH || matches!(opts.mode, OpenMode::Memory) {
let io = Arc::new(MemoryIO::new());
let db = Database::open_file_with_flags(io.clone(), MEMORY_PATH, flags, db_opts, None)?;
let conn = db.connect()?;
return Ok((io, conn));
}
let encryption_opts = match (opts.cipher.clone(), opts.hexkey.clone()) {
(Some(cipher), Some(hexkey)) => Some(EncryptionOpts { cipher, hexkey }),
(Some(_), None) => {
return Err(LimboError::InvalidArgument(
"hexkey is required when cipher is provided".to_string(),
));
}
(None, Some(_)) => {
return Err(LimboError::InvalidArgument(
"cipher is required when hexkey is provided".to_string(),
));
}
(None, None) => None,
};
let (io, db) = Database::open_new(
&opts.path,
opts.vfs.as_ref(),
flags,
db_opts,
encryption_opts,
)?;
if let Some(modeof) = opts.modeof {
let perms = std::fs::metadata(modeof).map_err(|e| io_error(e, "metadata"))?;
std::fs::set_permissions(&opts.path, perms.permissions())
.map_err(|e| io_error(e, "set_permissions"))?;
}
let conn = db.connect()?;
if let Some(cipher) = opts.cipher {
let _ = conn.pragma_update("cipher", format!("'{cipher}'"));
}
if let Some(hexkey) = opts.hexkey {
let _ = conn.pragma_update("hexkey", format!("'{hexkey}'"));
}
Ok((io, conn))
}
#[cfg(feature = "fs")]
fn from_uri_attached(
uri: &str,
mut db_opts: DatabaseOpts,
main_db_flags: OpenFlags,
io: Arc<dyn IO>,
) -> Result<(Arc<Database>, Option<EncryptionOpts>)> {
let opts = OpenOptions::parse(uri)?;
let mut flags = opts.get_flags()?;
if main_db_flags.contains(OpenFlags::ReadOnly) {
flags |= OpenFlags::ReadOnly;
}
let encryption_opts = match (opts.cipher.clone(), opts.hexkey.clone()) {
(Some(cipher), Some(hexkey)) => Some(EncryptionOpts { cipher, hexkey }),
(Some(_), None) => {
return Err(LimboError::InvalidArgument(
"hexkey is required when cipher is provided".to_string(),
));
}
(None, Some(_)) => {
return Err(LimboError::InvalidArgument(
"cipher is required when hexkey is provided".to_string(),
));
}
(None, None) => None,
};
if encryption_opts.is_some() {
db_opts = db_opts.with_encryption(true);
}
let io = opts.vfs.map(Database::io_for_vfs).unwrap_or(Ok(io))?;
let db = Database::open_file_with_flags(
io.clone(),
&opts.path,
flags,
db_opts,
encryption_opts.clone(),
)?;
if let Some(modeof) = opts.modeof {
let perms = std::fs::metadata(modeof).map_err(|e| io_error(e, "metadata"))?;
std::fs::set_permissions(&opts.path, perms.permissions())
.map_err(|e| io_error(e, "set_permissions"))?;
}
Ok((db, encryption_opts))
}
pub fn set_foreign_keys_enabled(&self, enable: bool) {
self.fk_pragma.store(enable, Ordering::Release);
self.bump_prepare_context_generation();
}
pub fn foreign_keys_enabled(&self) -> bool {
self.fk_pragma.load(Ordering::Acquire)
}
pub fn set_check_constraints_ignored(&self, ignore: bool) {
self.check_constraints_pragma
.store(ignore, Ordering::Release);
}
pub fn check_constraints_ignored(&self) -> bool {
self.check_constraints_pragma.load(Ordering::Acquire)
}
pub(crate) fn clear_deferred_foreign_key_violations(&self) -> isize {
self.fk_deferred_violations.swap(0, Ordering::Release)
}
pub(crate) fn get_deferred_foreign_key_violations(&self) -> isize {
self.fk_deferred_violations.load(Ordering::Acquire)
}
pub(crate) fn increment_deferred_foreign_key_violations(&self, v: isize) {
self.fk_deferred_violations.fetch_add(v, Ordering::AcqRel);
}
/// Query the CREATE TYPE SQL definitions stored in __turso_internal_types.
/// The connection's schema must already contain the table definitions so
/// that `prepare` can resolve the table name. Returns an empty Vec if the
/// types table does not exist.
pub(crate) fn query_stored_type_definitions(self: &Arc<Connection>) -> Result<Vec<String>> {
let has_types_table = {
let s = self.schema.read();
s.tables.contains_key(crate::schema::TURSO_TYPES_TABLE_NAME)
};
if !has_types_table {
return Ok(Vec::new());
}
let mut type_stmt = self.prepare_internal(format!(
"SELECT name, sql FROM {}",
crate::schema::TURSO_TYPES_TABLE_NAME
))?;
let mut type_rows = Vec::new();
type_stmt.run_with_row_callback(|row| {
type_rows.push(row.get::<&str>(1)?.to_string());
Ok(())
})?;
Ok(type_rows)
}
pub fn maybe_update_schema(&self) {
if self.schema_reparse_in_progress() {
return;
}
let current_schema = self.schema.read().clone();
let schema = self.db.schema.lock();
// MVCC checkpoint can publish physical btree roots into the shared
// schema without changing SQLite's schema cookie. If this connection
// still has the older schema snapshot, prepared statements must be
// invalidated and recompiled with the published roots.
if self.has_no_open_transaction_state()
&& (current_schema.schema_version != schema.schema_version
|| self
.has_mvcc_schema_snapshot_changed_with_same_version(¤t_schema, &schema))
{
let mut adopted = schema.clone();
// Resolve placeholder (negative) roots to the real pages a checkpoint has
// materialized, so consumers that skip negative roots (integrity_check) see them.
let mv_store_guard = self.db.get_mv_store();
if let Some(mv_store) = mv_store_guard.as_ref() {
if let Ok(schema) = Schema::try_make_mut(&mut adopted) {
mv_store.resolve_schema_negative_roots(schema);
}
}
*self.schema.write() = adopted;
self.bump_prepare_context_generation();
}
}
fn has_no_open_transaction_state(&self) -> bool {
matches!(self.get_tx_state(), TransactionState::None)
&& self.get_mv_tx().is_none()
&& self.next_attached_mv_tx().is_none()
}
fn has_mvcc_schema_snapshot_changed_with_same_version(
&self,
current_schema: &Arc<Schema>,
schema: &Arc<Schema>,
) -> bool {
self.mvcc_enabled()
&& current_schema.schema_version == schema.schema_version
&& !Arc::ptr_eq(current_schema, schema)
}
pub(crate) fn mvcc_schema_requires_reprepare_before_tx(&self) -> bool {
if !self.has_no_open_transaction_state() {
return false;
}
let current_schema = self.schema.read().clone();
let schema = self.db.schema.lock();
self.has_mvcc_schema_snapshot_changed_with_same_version(¤t_schema, &schema)
}
/// Begin-tx schema gate for MVCC. Returns the `MvStore::schema_generation` this connection's
/// prepared schema is valid as of, or `SchemaUpdated` if it is already stale (a passive
/// checkpoint republished physical roots without a cookie change). The returned generation is
/// re-checked inside `begin_tx`'s clock callback: a publish bumps `schema_generation` under the
/// same clock, so if one lands between here and the begin clock the generations differ and the
/// statement is forced to reprepare against the published roots.
pub(crate) fn mvcc_begin_schema_generation(&self) -> Result<Option<u64>> {
let mv_guard = self.db.get_mv_store();
let Some(mv) = mv_guard.as_ref() else {
return Ok(None);
};
// Mid-transaction (e.g. a multi-statement BEGIN): the snapshot and schema are fixed at the
// first begin, so a later checkpoint republication must not gate or reprepare here. Mirror
// the guard of `mvcc_schema_requires_reprepare_before_tx`.
if !self.has_no_open_transaction_state() {
return Ok(None);
}
// Read the generation before the snapshot comparison: any publish that mutates the shared
// schema after this read is caught by the comparison below (it changes the Arc), and any
// publish that lands during begin is caught by the clock re-check (it bumps the generation).
let generation = mv.schema_generation();
let current_schema = self.schema.read().clone();
let schema = self.db.schema.lock();
if self.has_mvcc_schema_snapshot_changed_with_same_version(¤t_schema, &schema) {
return Err(LimboError::SchemaUpdated);
}
Ok(Some(generation))
}
pub(crate) fn refresh_schema_from_shared_for_reprepare(&self) {
let current_schema = self.schema.read().clone();
let schema = self.db.schema.lock().clone();
if current_schema.schema_version < schema.schema_version
|| (self.has_no_open_transaction_state()
&& self
.has_mvcc_schema_snapshot_changed_with_same_version(¤t_schema, &schema))
{
*self.schema.write() = schema;
self.bump_prepare_context_generation();
}
}
/// Read schema version at current transaction
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn read_schema_version(&self) -> Result<u32> {
let pager = self.pager.load();
pager
.io
.block(|| pager.with_header(|header| header.schema_cookie))
.map(|version| version.get())
}
/// Update schema version to the new value within opened write transaction
///
/// New version of the schema must be strictly greater than previous one - otherwise method will panic
/// Write transaction must be opened in advance - otherwise method will panic
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn write_schema_version(self: &Arc<Connection>, version: u32) -> Result<()> {
let TransactionState::Write { .. } = self.get_tx_state() else {
return Err(LimboError::InternalError(
"write_schema_version must be called from within Write transaction".to_string(),
));
};
let pager = self.pager.load();
pager.io.block(|| {
pager.with_header_mut(|header| {
turso_assert!(
header.schema_cookie.get() < version,
"cookie can't go back in time"
);
self.with_schema_mut(|schema| schema.schema_version = version)
.map(|()| {
self.set_tx_state(TransactionState::Write {
schema_did_change: true,
});
header.schema_cookie = version.into();
})
})
})??;
self.reparse_schema()?;
Ok(())
}
/// Try to read page with given ID with fixed WAL watermark position
/// This method return false if page is not found (so, this is probably new page created after watermark position which wasn't checkpointed to the DB file yet)
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn try_wal_watermark_read_page(
&self,
page_idx: u32,
page: &mut [u8],
frame_watermark: Option<u64>,
) -> Result<bool> {
let Some((page_ref, c)) =
self.try_wal_watermark_read_page_begin(page_idx, frame_watermark)?
else {
return Ok(false);
};
match self.get_pager().io.wait_for_completion(c) {
Err(LimboError::CompletionError(err))
if Self::wal_watermark_read_error_is_absent_page(&err) =>
{
return Ok(false);
}
Err(e) => return Err(e),
_ => {}
}
self.try_wal_watermark_read_page_end(page, page_ref)
}
/// Classify a completion error raised while reading a page at a fixed WAL
/// watermark. On Windows under `experimental_win_iocp`, an absent /
/// zero-length page read surfaces as `UnexpectedEof` (see
/// `core/io/win_iocp.rs`); every watermark-read site must treat that as
/// "page absent" (size 0) rather than a hard error. Centralized here so the
/// platform handling cannot drift across the (now four) call sites.
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn wal_watermark_read_error_is_absent_page(err: &crate::error::CompletionError) -> bool {
#[cfg(all(target_os = "windows", feature = "experimental_win_iocp"))]
{
matches!(
err,
crate::error::CompletionError::IOError(std::io::ErrorKind::UnexpectedEof, _)
)
}
#[cfg(not(all(target_os = "windows", feature = "experimental_win_iocp")))]
{
let _ = err;
false
}
}
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn try_wal_watermark_read_page_begin(
&self,
page_idx: u32,
frame_watermark: Option<u64>,
) -> Result<Option<(Arc<Page>, Completion)>> {
let pager = self.pager.load();
let (page_ref, c) = match pager.read_page_no_cache(page_idx as i64, frame_watermark, true) {
Ok(result) => result,
// on windows, zero read will trigger UnexpectedEof
#[cfg(target_os = "windows")]
Err(LimboError::CompletionError(crate::error::CompletionError::IOError(
std::io::ErrorKind::UnexpectedEof,
_,
))) => return Ok(None),
Err(err) => return Err(err),
};
Ok(Some((page_ref, c)))
}
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn try_wal_watermark_read_page_end(
&self,
page: &mut [u8],
page_ref: Arc<Page>,
) -> Result<bool> {
let content = page_ref.get_contents();
// empty read - attempt to read absent page
if content.buffer.as_ref().is_none_or(|b| b.is_empty()) {
return Ok(false);
}
page.copy_from_slice(content.as_ptr());
Ok(true)
}
/// Return unique set of page numbers changes after WAL watermark position in the current WAL session
/// (so, if concurrent connection wrote something to the WAL - this method will not see this change)
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn wal_changed_pages_after(&self, frame_watermark: u64) -> Result<Vec<u32>> {
self.pager.load().wal_changed_pages_after(frame_watermark)
}
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn wal_state(&self) -> Result<WalState> {
self.pager.load().wal_state()
}
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn wal_get_frame(&self, frame_no: u64, frame: &mut [u8]) -> Result<WalFrameInfo> {
use crate::storage::sqlite3_ondisk::parse_wal_frame_header;
let c = self.pager.load().wal_get_frame(frame_no, frame)?;
self.db.io.wait_for_completion(c)?;
let (header, _) = parse_wal_frame_header(frame);
Ok(WalFrameInfo {
page_no: header.page_number,
db_size: header.db_size,
})
}
/// Insert `frame` (header included) at the position `frame_no` in the WAL
/// If WAL already has frame at that position - turso-db will compare content of the page and either report conflict or return OK
/// If attempt to write frame at the position `frame_no` will create gap in the WAL - method will return error
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn wal_insert_frame(&self, frame_no: u64, frame: &[u8]) -> Result<WalFrameInfo> {
self.pager.load().wal_insert_frame(frame_no, frame)
}
/// Start WAL session by initiating read+write transaction for this connection
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn wal_insert_begin(&self) -> Result<()> {
let pager = self.pager.load();
pager.begin_read_tx()?;
// Sync-engine drives WAL maintenance explicitly: any auto-restart of
// the WAL header here would invalidate the watermarks the caller has
// already published (see `wal_changed_pages_after`), so opt out of
// every auto action for this write transaction.
pager
.io
.block(|| pager.begin_write_tx(WalAutoActions::empty()))
.inspect_err(|_| {
pager.end_read_tx();
})?;
// start write transaction and disable auto-commit mode as SQL can be executed within WAL session (at caller own risk)
self.set_tx_state(TransactionState::Write {
schema_did_change: false,
});
self.auto_commit.store(false, Ordering::SeqCst);
Ok(())
}
/// Finish WAL session by ending read+write transaction taken in the [Self::wal_insert_begin] method
/// All frames written after last commit frame (db_size > 0) within the session will be rolled back
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn wal_insert_end(self: &Arc<Connection>, force_commit: bool) -> Result<()> {
use crate::{return_if_io, types::IOResult};
{
let pager = self.pager.load();
let Some(wal) = pager.wal.as_ref() else {
return Err(LimboError::InternalError(
"wal_insert_end called without a wal".to_string(),
));
};
let commit_err = if force_commit {
pager
.io
.block(|| {
return_if_io!(pager.commit_wal(
WalAutoActions::empty(),
self.get_sync_mode(),
self.get_data_sync_retry(),
));
pager.commit_wal_end();
Ok(IOResult::Done(()))
})
.err()
} else {
None
};
self.auto_commit.store(true, Ordering::SeqCst);
self.set_tx_state(TransactionState::None);
wal.end_write_tx();
wal.end_read_tx();
if !force_commit {
// remove all non-commited changes in case if WAL session left some suffix without commit frame
if let Some(mv_store) = self.mv_store().as_ref() {
if let Some(tx_id) = self.get_mv_tx_id() {
mv_store.rollback_tx(tx_id, pager.clone(), self, MAIN_DB_ID);
}
}
pager.rollback(false, self, true);
}
if let Some(err) = commit_err {
return Err(err);
}
}
// let's re-parse schema from scratch if schema cookie changed compared to the our in-memory view of schema
self.maybe_reparse_schema()?;
Ok(())
}
/// Flush dirty pages to disk.
pub fn cacheflush(&self) -> Result<Vec<Completion>> {
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
let pager = self.pager.load();
pager.io.block(|| pager.cacheflush())
}
pub fn checkpoint(self: &Arc<Self>, mode: CheckpointMode) -> Result<CheckpointResult> {
use crate::mvcc::database::CheckpointStateMachine;
use crate::state_machine::{StateTransition, TransitionResult};
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
if let Some(mv_store) = self.mv_store().as_ref() {
let pager = self.pager.load().clone();
let io = pager.io.clone();
let mut ckpt_sm = CheckpointStateMachine::new(
pager,
mv_store.clone(),
self.clone(),
true,
self.get_sync_mode(),
MAIN_DB_ID,
// Explicit Connection::checkpoint fully resets the WAL.
crate::storage::wal::CheckpointMode::Truncate {
upper_bound_inclusive: None,
},
);
loop {
match ckpt_sm.step(&()) {
Ok(TransitionResult::Continue) => {}
Ok(TransitionResult::Done(result)) => return Ok(result),
Ok(TransitionResult::Io(iocompletions)) => {
if let Err(err) = iocompletions.wait(io.as_ref()) {
ckpt_sm.cleanup_after_external_io_error(err.clone())?;
return Err(err);
}
}
Err(err) => return Err(err),
}
}
} else {
self.pager
.load()
.blocking_checkpoint(mode, self.get_sync_mode())
}
}
/// Close a connection and checkpoint.
pub fn close(&self) -> Result<()> {
if self.is_closed() {
return Ok(());
}
self.closed.store(true, Ordering::SeqCst);
let pager = self.pager.load();
match self.get_tx_state() {
TransactionState::None => {
// No active transaction
}
_ => {
if self.mvcc_enabled() {
if let Some(mv_store) = self.mv_store().as_ref() {
if let Some(tx_id) = self.get_mv_tx_id() {
mv_store.rollback_tx(tx_id, pager.clone(), self, MAIN_DB_ID);
}
}
pager.end_read_tx();
} else {
pager.rollback_tx(self);
}
// Roll back all attached DB transactions regardless of main
// DB mode — a :memory: attached DB may use WAL even when the
// main DB uses MVCC.
self.rollback_attached_mvcc_txs(false);
self.rollback_attached_wal_txns();
self.set_tx_state(TransactionState::None);
}
}
self.clear_mvcc_log_meta();
let is_memory_db = is_memory_like(&self.db.path);
let should_checkpoint_on_close = pager
.wal
.as_ref()
.is_none_or(|wal| wal.should_checkpoint_on_close());
if self.db.n_connections.fetch_sub(1, Ordering::SeqCst).eq(&1)
&& !self.db.is_readonly()
&& !is_memory_db
&& should_checkpoint_on_close
{
self.pager
.load()
.checkpoint_shutdown(self.wal_auto_actions(), self.get_sync_mode())?;
};
Ok(())
}
/// Disable every automatic WAL maintenance action for this connection
/// (auto-checkpoint AND WAL header restart). Sync-engine consumers call
/// this so they own all WAL bookkeeping themselves.
pub fn wal_auto_actions_disable(&self) {
self.wal_auto_actions
.store(WalAutoActions::empty().bits(), Ordering::SeqCst);
}
/// Returns the set of automatic WAL maintenance actions this connection
/// permits. MVCC connections always return an empty set because the
/// MVCC checkpoint state machine drives WAL maintenance explicitly.
pub fn wal_auto_actions(&self) -> WalAutoActions {
if self.db.get_mv_store().is_some() {
return WalAutoActions::empty();
}
WalAutoActions::from_bits_truncate(self.wal_auto_actions.load(Ordering::SeqCst))
}
/// Publish the connection's current schema snapshot to the shared database
/// cache after a successful commit so other live connections can refresh.
pub fn publish_schema_if_newer(&self) {
let schema = self.schema.read().clone();
self.db.update_schema_if_newer(schema);
}
/// Publish the connection's current schema snapshot after pages were
/// replaced outside normal SQL commit ordering.
///
/// External restore paths can move the schema cookie backwards. In that
/// case the shared schema cache must be replaced rather than updated
/// monotonically, otherwise new connections can re-adopt stale metadata.
#[cfg(feature = "conn_raw_api")]
pub fn publish_schema_after_external_restore(&self) -> Result<()> {
if self.get_tx_state() != TransactionState::None {
return Err(LimboError::Busy);
}
if self.get_mv_tx().is_some() || self.next_attached_mv_tx().is_some() {
return Err(LimboError::Busy);
}
let schema = self.schema.read().clone();
self.db.with_schema_mut(|current| {
*current = schema.as_ref().try_clone()?;
Ok(())
})?;
Ok(())
}
/// Roll back the main-database MVCC transaction while keeping the
/// surrounding raw WAL-insert session open.
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn reset_main_mvcc_tx_for_wal_session(&self) {
let mv_store = self.mv_store();
let Some(mv_store) = mv_store.as_ref() else {
return;
};
let Some(tx_id) = self.get_mv_tx_id() else {
return;
};
let pager = self.pager.load();
mv_store.rollback_tx(tx_id, pager.clone(), self, MAIN_DB_ID);
}
/// Discard the main-db MVCC transaction left by a sync raw-WAL session
/// before reparsing state after external file replacement.
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn discard_main_mvcc_tx_after_external_restore(&self) {
let pager = self.pager.load();
self.clear_internal_main_mvcc_tx(&pager);
}
/// Returns whether the main database currently has a live MVCC transaction.
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn has_main_mvcc_tx_for_wal_session(&self) -> bool {
self.get_mv_tx_id().is_some()
}
/// Commit the main-database MVCC transaction while keeping the surrounding
/// raw WAL-insert session open.
#[cfg(all(feature = "fs", feature = "conn_raw_api"))]
pub fn commit_main_mvcc_tx_for_wal_session(self: &Arc<Self>) -> Result<()> {
let mv_store_handle = self.mv_store();
let Some(mv_store) = mv_store_handle.as_ref() else {
return Ok(());
};
let Some(tx_id) = self.get_mv_tx_id() else {
return Ok(());
};
let mut state_machine = mv_store.commit_tx(tx_id, self, MAIN_DB_ID)?;
while let IOResult::IO(io) = state_machine.step(mv_store)? {
io.wait(self.db.io.as_ref())?;
}
assert!(state_machine.is_finalized());
self.set_mv_tx(None);
self.publish_schema_if_newer();
Ok(())
}
#[cfg(feature = "conn_raw_api")]
pub fn reload_wal_after_external_restore(&self) -> Result<()> {
self.db.reload_wal_after_external_restore()
}
/// Enable or disable writing portable logical-change metadata into MVCC
/// logical-log frames.
pub fn set_portable_logical_changes_enabled(&self, enabled: bool) {
#[cfg(feature = "conn_raw_api")]
{
self.portable_logical_changes_enabled
.store(enabled, Ordering::Release);
}
let _ = enabled;
}
pub fn portable_logical_changes_enabled(&self) -> bool {
#[cfg(feature = "conn_raw_api")]
{
self.portable_logical_changes_enabled
.load(Ordering::Acquire)
}
#[cfg(not(feature = "conn_raw_api"))]
{
false
}
}
pub fn set_mvcc_log_meta(&self, key: String, value: Option<String>) {
#[cfg(feature = "conn_raw_api")]
{
let mut metadata = self.mvcc_log_metadata.write();
match value {
Some(value) => {
metadata.insert(key, value);
}
None => {
metadata.remove(&key);
}
}
}
#[cfg(not(feature = "conn_raw_api"))]
{
let _ = (key, value);
}
}
#[cfg(feature = "conn_raw_api")]
pub(crate) fn mvcc_log_meta_snapshot(&self) -> HashMap<String, String> {
self.mvcc_log_metadata.read().clone()
}
pub(crate) fn clear_mvcc_log_meta(&self) {
#[cfg(feature = "conn_raw_api")]
{
self.mvcc_log_metadata.write().clear();
}
}
pub fn mvcc_log_meta(&self, key: &str) -> Option<String> {
#[cfg(feature = "conn_raw_api")]
{
return self.mvcc_log_metadata.read().get(key).cloned();
}
#[cfg(not(feature = "conn_raw_api"))]
{
let _ = key;
None
}
}
#[cfg(feature = "simulator")]
pub fn checkpoint_for_testing(&self, mode: CheckpointMode) -> Result<CheckpointResult> {
let pager = self.pager.load();
pager
.io
.block(|| pager.checkpoint(mode, SyncMode::Full, true))
}
#[cfg(all(feature = "simulator", target_pointer_width = "64", host_shared_wal))]
pub fn install_unpublished_backfill_proof_for_testing(
&self,
upper_bound_inclusive: u64,
) -> Result<()> {
let pager = self.pager.load();
let proof_nbackfills =
pager.run_checkpoint_until_post_sync_gap_for_testing(CheckpointMode::Passive {
upper_bound_inclusive: Some(upper_bound_inclusive),
})?;
let authority = self.db.shared_wal_coordination()?.ok_or_else(|| {
LimboError::InternalError("shared WAL authority is unavailable".into())
})?;
let snapshot_before_publish = authority.snapshot();
if snapshot_before_publish.nbackfills != 0 {
return Err(LimboError::InternalError(
"unpublished-proof setup requires nbackfills to remain unpublished".into(),
));
}
let (db_size_pages, db_header_crc32c) = db_identity_for_testing(Path::new(&self.db.path))?;
authority.install_backfill_proof(
crate::storage::shared_wal_coordination::SharedWalCoordinationHeader {
nbackfills: proof_nbackfills,
..snapshot_before_publish
},
db_size_pages,
db_header_crc32c,
);
Ok(())
}
pub fn last_insert_rowid(&self) -> i64 {
self.last_insert_rowid.load(Ordering::SeqCst)
}
pub(crate) fn update_last_rowid(&self, rowid: i64) {
self.last_insert_rowid.store(rowid, Ordering::SeqCst);
}
pub(crate) fn add_total_changes(&self, num_changes: i64) {
self.total_changes.fetch_add(num_changes, Ordering::SeqCst);
}
pub fn set_changes(&self, num_changes: i64) {
self.changes.store(num_changes, Ordering::SeqCst);
}
pub fn changes(&self) -> i64 {
self.changes.load(Ordering::SeqCst)
}
pub fn total_changes(&self) -> i64 {
self.total_changes.load(Ordering::SeqCst)
}
pub fn get_cache_size(&self) -> i32 {
self.cache_size.load(Ordering::SeqCst)
}
pub fn set_cache_size(&self, size: i32) {
self.cache_size.store(size, Ordering::SeqCst);
self.bump_prepare_context_generation();
}
pub fn get_capture_data_changes_info(
&self,
) -> crate::sync::RwLockReadGuard<'_, Option<CaptureDataChangesInfo>> {
self.capture_data_changes.read()
}
pub fn set_capture_data_changes_info(&self, opts: Option<CaptureDataChangesInfo>) {
*self.capture_data_changes.write() = opts;
self.bump_prepare_context_generation();
}
pub fn get_cdc_transaction_id(&self) -> i64 {
self.cdc_transaction_id.load(Ordering::SeqCst)
}
pub fn set_cdc_transaction_id(&self, id: i64) {
self.cdc_transaction_id.store(id, Ordering::SeqCst);
}
pub fn get_page_size(&self) -> PageSize {
let value = self.page_size.load(Ordering::SeqCst);
PageSize::new_from_header_u16(value).unwrap_or_default()
}
pub fn is_closed(&self) -> bool {
self.closed.load(Ordering::SeqCst)
}
pub fn is_query_only(&self) -> bool {
self.query_only.load(Ordering::SeqCst)
}
pub fn get_database_canonical_path(&self) -> String {
self.db.get_database_canonical_path()
}
/// Check if a specific attached database is read only or not, by its index
pub fn is_readonly(&self, index: usize) -> bool {
match index {
crate::MAIN_DB_ID => self.db.is_readonly(),
crate::TEMP_DB_ID => self
.temp
.database
.read()
.as_ref()
.is_some_and(|temp_db| temp_db.db.is_readonly()),
_ => {
let db = self.attached_databases.read().get_database_by_index(index);
db.expect("Should never have called this without being sure the database exists")
.is_readonly()
}
}
}
/// Reset the page size for the current connection.
///
/// Specifying a new page size does not change the page size immediately.
/// Instead, the new page size is remembered and is used to set the page size when the database
/// is first created, if it does not already exist when the page_size pragma is issued,
/// or at the next VACUUM command that is run on the same database connection while not in WAL mode.
pub fn reset_page_size(&self, size: u32) -> Result<()> {
if self.db.initialized() {
return Ok(());
}
let Some(size) = PageSize::new(size) else {
return Ok(());
};
self.page_size.store(size.get_raw(), Ordering::SeqCst);
self.pager.load().set_initial_page_size(size)?;
// MvStore caches a copy of the database header in `global_header`, captured from the
// pager during bootstrap (before any PRAGMA page_size can run). Propagate the new
// page size so subsequent transactions and any header lookups see the same value the
// pager will write to disk; otherwise paths like op_open_ephemeral allocate buffers
// sized to the connection's page_size but compute usable_space from the stale 4 KiB
// global header, tripping the btree_init_page assertion.
if let Some(mv_store) = self.db.get_mv_store().as_ref() {
mv_store.set_global_page_size(size);
}
self.bump_prepare_context_generation();
Ok(())
}
#[cfg(feature = "fs")]
pub fn open_new(&self, path: &str, vfs: &str) -> Result<(Arc<dyn IO>, Arc<Database>)> {
Database::open_with_vfs(&self.db, path, vfs)
}
pub fn list_vfs(&self) -> Vec<String> {
#[allow(unused_mut)]
let mut all_vfs = vec![String::from("memory")];
#[cfg(feature = "fs")]
{
#[cfg(target_family = "unix")]
{
all_vfs.push("syscall".to_string());
}
#[cfg(all(target_os = "linux", feature = "io_uring"))]
{
all_vfs.push("io_uring".to_string());
}
#[cfg(all(target_os = "windows", feature = "experimental_win_iocp"))]
{
all_vfs.push("experimental_win_iocp".to_string());
}
all_vfs.extend(crate::ext::list_vfs_modules());
}
all_vfs
}
pub fn get_auto_commit(&self) -> bool {
self.auto_commit.load(Ordering::SeqCst)
}
/// Mark the active explicit transaction poisoned so COMMIT rolls it back.
///
/// This is used when a write statement under BEGIN is abandoned before it
/// reaches Halt/Done and that statement did not open a statement savepoint.
pub(crate) fn mark_tx_poisoned(&self) {
self.poisoned_tx.store(true, Ordering::SeqCst);
}
/// Return whether the active explicit transaction must roll back at COMMIT.
pub(crate) fn tx_is_poisoned(&self) -> bool {
self.poisoned_tx.load(Ordering::SeqCst)
}
/// Clear the poison marker after BEGIN, COMMIT, or ROLLBACK.
pub(crate) fn clear_tx_poison(&self) {
self.poisoned_tx.store(false, Ordering::SeqCst);
}
pub fn set_load_extension_enabled(&self, enabled: bool) {
self.enable_load_extension.store(enabled, Ordering::Release);
}
pub(crate) fn can_load_extensions(&self) -> bool {
self.enable_load_extension.load(Ordering::Acquire)
}
pub fn reparse_schema_after_extension_load(self: &Arc<Connection>) -> Result<()> {
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
// Collect row data from the Statement first, then drop the Statement
// before taking the schema write lock. This prevents a deadlock in MVCC
// mode where Statement::drop -> abort -> rollback_tx -> schema.read()
// would deadlock against the schema write lock.
let mut rows_data: Vec<(String, String, String, i64, Option<String>)> = Vec::new();
{
let mut rows = self
.query("SELECT * FROM sqlite_schema")?
.expect("query must be parsed to statement");
rows.run_with_row_callback(|row| {
let ty = row.get::<&str>(0)?.to_string();
let name = row.get::<&str>(1)?.to_string();
let table_name = row.get::<&str>(2)?.to_string();
let root_page = row.get::<i64>(3)?;
let sql = row.get::<&str>(4).ok().map(|s| s.to_string());
rows_data.push((ty, name, table_name, root_page, sql));
Ok(())
})?;
} // Statement dropped here, before schema write lock
let syms = self.syms.read();
self.with_schema_mut(|schema| -> Result<()> {
// Incremental re-parse after extension loading. The schema already has
// tables/indices/views from initial parse. We only need to pick up
// entries that previously failed (e.g. virtual tables whose module
// wasn't loaded yet). "Already exists" errors are expected and skipped.
let mut from_sql_indexes = crate::alloc::vec![];
let mut automatic_indices = HashMap::default();
let mut dbsp_state_roots = HashMap::default();
let mut dbsp_state_index_roots = HashMap::default();
let mut materialized_view_info = HashMap::default();
let attached_resolver = |name: &str| -> Option<usize> {
self.attached_databases
.read()
.get_database_by_name(&crate::util::normalize_ident(name))
.map(|(idx, _)| idx)
};
for (ty, name, table_name, root_page, sql) in &rows_data {
match schema.handle_schema_row(
ty,
name,
table_name,
*root_page,
sql.as_deref(),
&syms,
&mut from_sql_indexes,
&mut automatic_indices,
&mut dbsp_state_roots,
&mut dbsp_state_index_roots,
&mut materialized_view_info,
&attached_resolver,
) {
Ok(()) => {}
Err(LimboError::ParseError(msg)) if msg.contains("already exists") => {}
Err(LimboError::ExtensionError(msg)) => {
eprintln!("Warning: {msg}");
}
Err(e) => return Err(e),
}
}
match schema.populate_indices(&syms, from_sql_indexes, automatic_indices, false) {
Ok(()) => {}
Err(LimboError::ParseError(msg)) if msg.contains("already exists") => {}
Err(LimboError::ExtensionError(msg)) => eprintln!("Warning: {msg}"),
Err(e) => return Err(e),
}
match schema.populate_materialized_views(
materialized_view_info,
dbsp_state_roots,
dbsp_state_index_roots,
) {
Ok(()) => {}
Err(LimboError::ExtensionError(msg)) => eprintln!("Warning: {msg}"),
Err(e) => return Err(e),
}
Ok(())
})?
}
// Clearly there is something to improve here, Vec<Vec<Value>> isn't a couple of tea
/// Query the current rows/values of `pragma_name`.
pub fn pragma_query(self: &Arc<Connection>, pragma_name: &str) -> Result<Vec<Vec<Value>>> {
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
let pragma = format!("PRAGMA {pragma_name}");
let mut stmt = self.prepare(pragma)?;
stmt.run_collect_rows()
}
/// Set a new value to `pragma_name`.
///
/// Some pragmas will return the updated value which cannot be retrieved
/// with this method.
pub fn pragma_update<V: Display>(
self: &Arc<Connection>,
pragma_name: &str,
pragma_value: V,
) -> Result<Vec<Vec<Value>>> {
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
let pragma = format!("PRAGMA {pragma_name} = {pragma_value}");
let mut stmt = self.prepare(pragma)?;
stmt.run_collect_rows()
}
pub fn experimental_views_enabled(&self) -> bool {
self.db.experimental_views_enabled()
}
pub fn experimental_index_method_enabled(&self) -> bool {
self.db.experimental_index_method_enabled()
}
pub fn experimental_custom_types_enabled(&self) -> bool {
self.db.experimental_custom_types_enabled()
}
pub fn experimental_attach_enabled(&self) -> bool {
self.db.experimental_attach_enabled()
}
pub fn experimental_vacuum_enabled(&self) -> bool {
self.db.experimental_vacuum_enabled()
}
pub fn experimental_mvcc_passive_checkpoint_enabled(&self) -> bool {
self.db.experimental_mvcc_passive_checkpoint_enabled()
}
pub fn experimental_multiprocess_wal_enabled(&self) -> bool {
self.db.experimental_multiprocess_wal_enabled()
}
pub fn experimental_generated_columns_enabled(&self) -> bool {
self.db.experimental_generated_columns_enabled()
}
pub fn experimental_without_rowid_enabled(&self) -> bool {
self.db.experimental_without_rowid_enabled()
}
pub fn mvcc_enabled(&self) -> bool {
self.db.mvcc_enabled()
}
pub fn mv_store(&self) -> impl Deref<Target = Option<Arc<MvStore>>> {
struct TransparentWrapper<T>(T);
impl<T> Deref for TransparentWrapper<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
// Never use MV store for bootstrapping - we read state directly from sqlite_schema in the DB file.
if !self.is_mvcc_bootstrap_connection() {
either::Left(self.db.get_mv_store())
} else {
either::Right(TransparentWrapper(None))
}
}
#[cfg(any(test, injected_yields))]
pub fn set_yield_injector(&self, injector: Option<Arc<dyn YieldInjector>>) {
let mut slot = self.yield_injector.write();
match injector {
Some(injector) => {
turso_assert!(
slot.is_none(),
"yield injector should be empty before installing a new one"
);
*slot = Some(injector);
}
None => {
turso_assert!(
slot.is_some(),
"yield injector should be installed before it is cleared"
);
*slot = None;
}
}
}
#[cfg(any(test, injected_yields))]
pub(crate) fn yield_injector(&self) -> Option<Arc<dyn YieldInjector>> {
self.yield_injector.read().clone()
}
#[cfg(any(test, injected_yields))]
pub fn set_failure_injector(&self, injector: Option<Arc<dyn FailureInjector>>) {
let mut slot = self.failure_injector.write();
match injector {
Some(injector) => {
turso_assert!(
slot.is_none(),
"failure injector should be empty before installing a new one"
);
*slot = Some(injector);
}
None => {
turso_assert!(
slot.is_some(),
"failure injector should be installed before it is cleared"
);
*slot = None;
}
}
}
#[cfg(any(test, injected_yields))]
pub(crate) fn failure_injector(&self) -> Option<Arc<dyn FailureInjector>> {
self.failure_injector.read().clone()
}
#[cfg(any(test, injected_yields))]
#[inline(always)]
pub(crate) fn next_yield_instance_id(&self) -> u64 {
self.yield_instance_id_counter
.fetch_add(1, Ordering::Relaxed)
}
/// Query the current value(s) of `pragma_name` associated to
/// `pragma_value`.
///
/// This method can be used with query-only pragmas which need an argument
/// (e.g. `table_info('one_tbl')`) or pragmas which returns value(s)
/// (e.g. `integrity_check`).
pub fn pragma<V: Display>(
self: &Arc<Connection>,
pragma_name: &str,
pragma_value: V,
) -> Result<Vec<Vec<Value>>> {
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
let pragma = format!("PRAGMA {pragma_name}({pragma_value})");
let mut stmt = self.prepare(pragma)?;
let mut results = Vec::new();
loop {
match stmt.step()? {
vdbe::StepResult::Row => {
let row: Vec<Value> = stmt.row().unwrap().get_values().cloned().collect();
results.push(row);
}
vdbe::StepResult::Interrupt | vdbe::StepResult::Busy => {
return Err(LimboError::Busy);
}
_ => break,
}
}
Ok(results)
}
#[inline]
pub fn with_schema_mut<T>(&self, f: impl FnOnce(&mut Schema) -> T) -> Result<T> {
let mut schema_ref = self.schema.write();
let schema = Schema::try_make_mut(&mut schema_ref)?;
Ok(f(schema))
}
/// Mutate the schema for a specific database (main or attached).
pub(crate) fn with_database_schema_mut<T>(
&self,
database_id: usize,
f: impl FnOnce(&mut Schema) -> T,
) -> Result<T> {
match database_id {
crate::MAIN_DB_ID => self.with_schema_mut(f),
crate::TEMP_DB_ID => {
// The temp database is connection-local, no other connection can
// reference its schema, so we can mutate it directly without cloning
// into `database_schemas`.
let temp_db_guard = self.temp.database.read();
let temp_db = temp_db_guard
.as_ref()
.expect("temp database should be initialized before schema mutation");
let mut schema_guard = temp_db.db.schema.lock();
let schema = Schema::try_make_mut(&mut schema_guard)?;
let result = f(schema);
self.bump_prepare_context_generation();
Ok(result)
}
_ => {
// For attached databases, update a connection-local copy of the schema.
// We don't update the shared db.schema until after the WAL commit, so
// other connections won't see uncommitted schema changes (which would
// cause SchemaUpdated mismatches).
let mut schemas = self.database_schemas.write();
let schema_arc = schemas.entry(database_id).or_insert_with(|| {
let attached_dbs = self.attached_databases.read();
let (db, _pager) = attached_dbs
.index_to_data
.get(&database_id)
.expect("Database ID should be valid");
let schema = db.schema.lock().clone();
schema
});
let schema = Schema::try_make_mut(schema_arc)?;
let result = f(schema);
self.bump_prepare_context_generation();
Ok(result)
}
}
}
pub fn is_db_initialized(&self) -> bool {
self.db.initialized()
}
pub(crate) fn get_pager_from_database_index(&self, index: &usize) -> Result<Arc<Pager>> {
match *index {
crate::MAIN_DB_ID => Ok(self.pager.load().clone()),
crate::TEMP_DB_ID => {
// Lazily initialize the temp database if it hasn't been created yet.
if self.temp.database.read().is_none() {
self.ensure_temp_database()?;
}
Ok(self
.temp
.database
.read()
.as_ref()
.map(|temp_db| temp_db.pager.clone())
.expect("temp database should be initialized after ensure_temp_database"))
}
_ => Ok(self.attached_databases.read().get_pager_by_index(index)),
}
}
/// Get the database name for a given database index.
/// Returns "main" for index 0, "temp" for index 1, and the alias for attached databases.
pub(crate) fn get_database_name_by_index(&self, index: usize) -> Option<String> {
match index {
MAIN_DB_ID => Some("main".to_string()),
TEMP_DB_ID => Some("temp".to_string()),
_ => self.attached_databases.read().get_name_by_index(index),
}
}
/// Get the database id for a schema name ("main", "temp", or an attached db alias).
pub(crate) fn get_database_id_by_name(&self, name: &str) -> Result<usize> {
let normalized: String = crate::util::normalize_ident(name);
match normalized.as_str() {
"main" => Ok(MAIN_DB_ID),
"temp" => Ok(TEMP_DB_ID),
_ => self
.attached_databases
.read()
.get_database_by_name(&normalized)
.map(|(idx, _)| idx)
.ok_or_else(|| LimboError::InvalidArgument(format!("no such database: {name}"))),
}
}
/// Get the Database object for a given database id.
pub(crate) fn get_source_database(&self, database_id: usize) -> Arc<Database> {
match database_id {
MAIN_DB_ID => self.db.clone(),
TEMP_DB_ID => self
.temp
.database
.read()
.as_ref()
.map(|temp_db| temp_db.db.clone())
.unwrap_or_else(|| self.db.clone()),
_ => self
.attached_databases
.read()
.get_database_by_index(database_id)
.expect("database index should be valid"),
}
}
fn is_attached(&self, alias: &str) -> bool {
self.attached_databases
.read()
.name_to_index
.contains_key(alias)
}
/// Returns the reserved-space value inherited from the main connection's pager.
/// (This reads the main database pager, not the pager of db to be attached)
fn inherited_reserved_space_for_fresh_attach(&self) -> u8 {
let pager = self.pager.load();
pager
.get_reserved_space()
.unwrap_or_else(|| pager.io_ctx.read().get_reserved_space_bytes())
}
/// Returns the minimum reserved space required by the attached pager's own IO context.
/// This is used as a floor so inherited or explicit values cannot undercut the attached DB.
fn minimum_reserved_space_for_fresh_attach(pager: &Pager) -> u8 {
pager
.get_reserved_space()
.unwrap_or(0)
.max(pager.io_ctx.read().get_reserved_space_bytes())
}
fn database_has_existing_wal_state(db: &Database) -> bool {
let shared_wal = db.shared_wal.read();
shared_wal.page_size() != 0 || shared_wal.last_checksum_and_max_frame().1 != 0
}
fn install_database_wal_on_pager(db: &Arc<Database>, pager: &mut Arc<Pager>) {
let shared_wal = db.shared_wal.clone();
let last_checksum_and_max_frame = shared_wal.read().last_checksum_and_max_frame();
let wal = Arc::new(crate::storage::wal::WalFile::new(
db.io.clone(),
shared_wal,
last_checksum_and_max_frame,
db.buffer_pool.clone(),
));
let pager = Arc::get_mut(pager)
.expect("fresh attached pager must not be shared before bootstrap or publication");
pager.set_wal(wal);
}
fn set_mvcc_journal_mode_fresh_db(pager: &Pager) -> Result<()> {
turso_assert!(!pager.db_initialized());
pager.set_initial_journal_version(crate::storage::sqlite3_ondisk::Version::Mvcc)
}
fn validate_attach_target(db: &Database, is_fresh: bool, alias: &str) -> Result<()> {
if is_fresh && Self::database_has_existing_wal_state(db) {
return Err(LimboError::InvalidArgument(format!(
"cannot attach database '{alias}': main database file is uninitialized but WAL state exists"
)));
}
if is_fresh && db.is_readonly() {
return Err(LimboError::InvalidArgument(format!(
"cannot attach database '{alias}': fresh read-only databases cannot be initialized during attach"
)));
}
Ok(())
}
fn apply_page_layout_to_fresh_attach_db(
&self,
alias: &str,
attached_db_pager: &Pager,
reserved_space: Option<u8>,
) -> Result<()> {
let target_page_size = self.get_page_size();
let attached_min_reserved_space =
Self::minimum_reserved_space_for_fresh_attach(attached_db_pager);
let target_reserved_space = match reserved_space {
Some(space) => {
// this happens reserved_space is explicitly passed along with encryption or checksum
if space < attached_min_reserved_space {
return Err(LimboError::InvalidArgument(format!(
"cannot attach database '{alias}': reserved space {space} is smaller than attached database minimum {attached_min_reserved_space}"
)));
}
Some(space)
}
None => Some(
self.inherited_reserved_space_for_fresh_attach()
.max(attached_min_reserved_space),
),
};
attached_db_pager.set_initial_page_size(target_page_size)?;
if let Some(reserved_space) = target_reserved_space {
attached_db_pager.set_reserved_space_bytes(reserved_space);
}
Ok(())
}
fn reject_initialized_attach_mismatches(
&self,
alias: &str,
db: &Database,
pager: &Pager,
) -> Result<()> {
// Reject incompatible journal modes for initialized attached databases:
// we cannot silently convert the header (the user may have attached read-only).
if self.mvcc_enabled() != db.mvcc_enabled() {
let main_mode = if self.mvcc_enabled() { "MVCC" } else { "WAL" };
let attached_mode = if db.mvcc_enabled() { "MVCC" } else { "WAL" };
return Err(LimboError::InvalidArgument(format!(
"cannot attach database '{alias}': main database uses {main_mode} journal mode \
but attached database uses {attached_mode}. Both must use the same journal mode."
)));
}
// Reject mismatched page sizes: ephemeral tables and cross-database
// operations assume a uniform page size across all attached databases.
let main_pager = self.pager.load();
if let (Some(main_ps), Some(attached_ps)) =
(main_pager.get_page_size(), pager.get_page_size())
{
if main_ps != attached_ps {
return Err(LimboError::InvalidArgument(format!(
"cannot attach database '{alias}': page size mismatch \
(main={main_ps:?}, attached={attached_ps:?})"
)));
}
}
Ok(())
}
fn reject_unsupported_fresh_mvcc_attach_durable_storage(
&self,
alias: &str,
db: &Database,
attached_is_fresh: bool,
) -> Result<()> {
if attached_is_fresh
&& self.mvcc_enabled()
&& self.db.durable_storage.is_some()
&& db.durable_storage.is_none()
{
return Err(LimboError::InvalidArgument(format!(
"cannot attach database '{alias}': fresh MVCC attach does not support inheriting custom durable storage"
)));
}
Ok(())
}
/// Attach a database file with the given alias name
#[cfg(not(feature = "fs"))]
pub(crate) fn attach_database(
&self,
_path: &str,
_alias: &str,
_state: &mut AttachDatabaseState,
) -> Result<IOResult<()>> {
Err(LimboError::InvalidArgument(
"attach not available in this build (no-fs)".to_string(),
))
}
#[cfg(not(feature = "fs"))]
pub(crate) fn attach_database_with_config(
&self,
_path: &str,
_alias: &str,
_reserved_space: Option<u8>,
_state: &mut AttachDatabaseState,
) -> Result<IOResult<()>> {
// File-backed ATTACH is unavailable without `fs`, so pre-initialization
// page-layout overrides are also unsupported in this build.
self.attach_database(_path, _alias, _state)
}
/// Attach a database file with the given alias name
#[cfg(feature = "fs")]
pub(crate) fn attach_database(
&self,
path: &str,
alias: &str,
state: &mut AttachDatabaseState,
) -> Result<IOResult<()>> {
self.attach_database_with_config(path, alias, None, state)
}
/// Attach a database file with an optional pre-initialization reserved-space override.
#[cfg(feature = "fs")]
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn attach_database_with_config(
&self,
path: &str,
alias: &str,
reserved_space: Option<u8>,
state: &mut AttachDatabaseState,
) -> Result<IOResult<()>> {
loop {
match state {
AttachDatabaseState::Start => {
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
if self.is_attached(alias) {
return Err(LimboError::InvalidArgument(format!(
"database {alias} is already in use"
)));
}
if alias.eq_ignore_ascii_case("main") || alias.eq_ignore_ascii_case("temp") {
return Err(LimboError::InvalidArgument(format!(
"reserved name {alias} is already in use"
)));
}
let db_opts = DatabaseOpts::new()
.with_views(self.db.experimental_views_enabled())
.with_custom_types(self.db.experimental_custom_types_enabled())
.with_index_method(self.db.experimental_index_method_enabled())
.with_vacuum(self.db.experimental_vacuum_enabled())
.with_generated_columns(self.db.experimental_generated_columns_enabled())
.with_without_rowid(self.db.experimental_without_rowid_enabled());
let is_memory_db = is_memory_like(path);
let io: Arc<dyn IO> = if is_memory_db {
Arc::new(MemoryIO::new())
} else if self.db.is_in_memory_db() {
Database::io_for_path(path)?
} else {
self.db.io.clone()
};
let main_db_flags = self.db.open_flags;
let (db, encryption_opts) =
Self::from_uri_attached(path, db_opts, main_db_flags, io)?;
let attached_is_fresh = !db.initialized();
if !is_memory_db {
Self::validate_attach_target(&db, attached_is_fresh, alias)?;
}
self.reject_unsupported_fresh_mvcc_attach_durable_storage(
alias,
&db,
attached_is_fresh,
)?;
let encryption_key = if let Some(ref enc) = encryption_opts {
Some(EncryptionKey::from_hex_string(&enc.hexkey)?)
} else {
None
};
*state = AttachDatabaseState::Init(Box::new(AttachDatabaseInitState {
alias: alias.to_string(),
reserved_space,
db,
attached_is_fresh,
encryption_key,
init_st: crate::InitState::default(),
}));
}
AttachDatabaseState::Init(init) => {
let mut pager = Arc::new(crate::return_if_io!(init
.db
._init_nonblock(&mut init.init_st, init.encryption_key.as_ref(),)));
if !init.attached_is_fresh {
self.reject_initialized_attach_mismatches(&init.alias, &init.db, &pager)?;
*state = AttachDatabaseState::Publish {
alias: init.alias.clone(),
db: init.db.clone(),
pager,
};
continue;
}
self.apply_page_layout_to_fresh_attach_db(
&init.alias,
&pager,
init.reserved_space,
)?;
if self.mvcc_enabled() && !init.db.mvcc_enabled() {
Self::set_mvcc_journal_mode_fresh_db(&pager)?;
Self::install_database_wal_on_pager(&init.db, &mut pager);
let enc_ctx = pager.io_ctx.read().encryption_context().cloned();
let mv_store = journal_mode::open_mv_store(
init.db.io.clone(),
&init.db.path,
init.db.open_flags,
init.db.durable_storage.clone(),
enc_ctx,
init.db.mv_store_allocator.clone(),
init.db.experimental_mvcc_passive_checkpoint_enabled(),
)?;
init.db.mv_store.store(Some(mv_store));
*state = AttachDatabaseState::Bootstrap(Box::new(
AttachDatabaseBootstrapState {
alias: init.alias.clone(),
db: init.db.clone(),
pager,
encryption_key: init.encryption_key.take(),
bootstrap_conn: None,
bootstrap_st: crate::mvcc::database::BootstrapState::default(),
},
));
} else {
*state = AttachDatabaseState::Publish {
alias: init.alias.clone(),
db: init.db.clone(),
pager,
};
}
}
AttachDatabaseState::Bootstrap(bootstrap) => {
if bootstrap.bootstrap_conn.is_none() {
let default_cache_size = match bootstrap
.pager
.with_header(|header| header.default_page_cache_size)
{
Ok(IOResult::Done(default_cache_size)) => default_cache_size.get(),
Ok(IOResult::IO(io)) => return Ok(IOResult::IO(io)),
Err(_) => 0,
};
bootstrap.bootstrap_conn =
Some(bootstrap.db._connect_with_pager_and_default_cache_size(
true,
bootstrap.pager.clone(),
bootstrap.encryption_key.take(),
default_cache_size,
)?);
}
let mv_store_guard = bootstrap.db.get_mv_store();
let Some(mv_store) = mv_store_guard.as_ref() else {
return Err(LimboError::InternalError(
"fresh MVCC attach missing MV store".to_string(),
));
};
crate::return_if_io!(mv_store.bootstrap_nonblock(
bootstrap
.bootstrap_conn
.as_ref()
.expect("bootstrap connection initialized above"),
&mut bootstrap.bootstrap_st,
));
*state = AttachDatabaseState::Publish {
alias: bootstrap.alias.clone(),
db: bootstrap.db.clone(),
pager: bootstrap.pager.clone(),
};
}
AttachDatabaseState::Publish { alias, db, pager } => {
self.attached_databases
.write()
.insert(alias.as_str(), (db.clone(), pager.clone()));
self.bump_prepare_context_generation();
*state = AttachDatabaseState::Done;
return Ok(IOResult::Done(()));
}
AttachDatabaseState::Done => {
return Err(LimboError::InternalError(
"attach_database called after completion".to_string(),
));
}
}
}
}
// Detach a database by alias name
pub(crate) fn detach_database(&self, alias: &str) -> Result<()> {
if self.is_closed() {
return Err(LimboError::InternalError("Connection closed".to_string()));
}
if alias == "main" || alias == "temp" {
return Err(LimboError::InvalidArgument(format!(
"cannot detach database: {alias}"
)));
}
// Look up the database index first, then rollback any MVCC transaction
// *before* removing the database from the catalog. mv_store_for_db
// and get_pager_from_database_index read `attached_databases`, so we
// must not hold the write lock during the rollback.
let database_id = {
let attached_dbs = self.attached_databases.read();
match attached_dbs.name_to_index.get(alias).copied() {
Some(id) => id,
None => {
return Err(LimboError::InvalidArgument(format!(
"no such database: {alias}"
)));
}
}
};
// Rollback any active transaction on this database before detaching.
// After the Database is removed from the catalog, the MvStore / Pager
// become unreachable and the transaction would leak forever.
let pager = self
.get_pager_from_database_index(&database_id)
.expect("attached database should always have a pager");
if pager.holds_read_lock() || pager.holds_write_lock() {
return Err(LimboError::InvalidArgument(format!(
"database {alias} is locked"
)));
}
if let Some((tx_id, _mode)) = self.get_mv_tx_for_db(database_id) {
if let Some(mv_store) = self.mv_store_for_db(database_id) {
mv_store.rollback_tx(tx_id, pager.clone(), self, database_id);
pager.end_read_tx();
}
self.set_mv_tx_for_db(database_id, None);
} else {
// Non-MVCC attached DB (e.g. :memory:) — rollback WAL state.
pager.rollback_attached();
}
// Remove from catalog. The write lock must be released before
// acquiring database_schemas.write() to maintain consistent lock
// ordering (attached_databases before database_schemas).
{
let mut attached_dbs = self.attached_databases.write();
attached_dbs.remove(alias);
}
// Invalidate the cached schema for this database index so that a future
// ATTACH reusing the same index won't see stale schema entries.
self.database_schemas.write().remove(&database_id);
self.bump_prepare_context_generation();
Ok(())
}
/// List all attached database aliases
pub fn list_attached_databases(&self) -> Vec<String> {
self.attached_databases
.read()
.name_to_index
.keys()
.cloned()
.collect()
}
/// Invoke `f` with a slice of all non-main database (index, pager) pairs
/// (temp + attached).The internal locks are released before `f` runs, which also
/// makes it safe for `f` to call back into the connection (e.g. `mv_store_for_db`,
/// which re-reads the attached-database catalog).
pub(crate) fn with_all_attached_pagers_with_index<F, R>(&self, f: F) -> R
where
F: FnOnce(&[(usize, Arc<Pager>)]) -> R,
{
let mut pagers: SmallVec<[(usize, Arc<Pager>); 8]> = SmallVec::new();
if let Some(temp_db) = self.temp.database.read().as_ref() {
pagers.push((crate::TEMP_DB_ID, temp_db.pager.clone()));
}
{
let catalog = self.attached_databases.read();
for (&idx, (_db, pager)) in catalog.index_to_data.iter() {
pagers.push((idx, pager.clone()));
}
}
f(&pagers)
}
pub(crate) fn database_schemas(&self) -> &RwLock<HashMap<usize, Arc<Schema>>> {
&self.database_schemas
}
fn cached_non_main_schema(&self, database_id: usize) -> Arc<Schema> {
turso_assert_ne!(database_id, crate::MAIN_DB_ID);
// TEMP is the sole source-of-truth path: writes go directly to
// `temp_db.db.schema` (see `with_database_schema_mut`), so skip
// `database_schemas` entirely to avoid stale reads.
if database_id == crate::TEMP_DB_ID {
return self
.temp
.database
.read()
.as_ref()
.map(|temp_db| temp_db.db.schema.lock().clone())
.unwrap_or_else(|| self.empty_temp_schema());
}
if let Some(schema) = self.database_schemas.read().get(&database_id).cloned() {
return schema;
}
let attached_dbs = self.attached_databases.read();
let (db, _pager) = attached_dbs
.index_to_data
.get(&database_id)
.expect("Database ID should be valid after resolve_database_id");
let schema = db.schema.lock().clone();
schema
}
/// Publish a connection-local non-main schema after commit.
///
/// TEMP is not staged in `database_schemas` — writes go directly to
/// `temp_db.db.schema` via `with_database_schema_mut`, so there is
/// nothing to publish here. Attached databases still stage mutations
/// in `database_schemas` so other connections don't see uncommitted
/// DDL; those get published to the shared `db.schema` on commit.
pub(crate) fn publish_database_schema(&self, database_id: usize) {
if database_id == crate::TEMP_DB_ID {
return;
}
let mut schemas = self.database_schemas.write();
if let Some(local_schema) = schemas.remove(&database_id) {
let attached_dbs = self.attached_databases.read();
if let Some((db, _pager)) = attached_dbs.index_to_data.get(&database_id) {
*db.schema.lock() = local_schema;
}
self.bump_prepare_context_generation();
}
}
pub(crate) fn attached_databases(&self) -> &RwLock<DatabaseCatalog> {
&self.attached_databases
}
/// Access schema for a database using a closure pattern to avoid cloning
pub(crate) fn with_schema<T>(&self, database_id: usize, f: impl FnOnce(&Schema) -> T) -> T {
match database_id {
crate::MAIN_DB_ID => {
let schema = self.schema.read();
f(&schema)
}
_ => {
let schema = self.cached_non_main_schema(database_id);
f(&schema)
}
}
}
/// Clone the *shared* schema of `database_id` (main or attached), bypassing
/// the per-connection schema cache. Falls back to the main DB's shared
/// schema when `database_id` does not name an attached database — callers
/// in error paths get something usable instead of a panic.
///
/// MVCC checkpoint specifically must call this rather than [`Self::with_schema`]:
/// it writes from the mv store to the pager, so the schema it uses must
/// match the pager being checkpointed and cannot be a stale per-connection
/// copy.
pub(crate) fn clone_shared_schema(&self, database_id: usize) -> Arc<Schema> {
if database_id == crate::MAIN_DB_ID {
self.db.clone_schema()
} else {
self.attached_databases
.read()
.index_to_data
.get(&database_id)
.map(|(db, _)| db.schema.lock().clone())
.unwrap_or_else(|| self.db.clone_schema())
}
}
// Get the canonical path for a database given its Database object
fn get_canonical_path_for_database(db: &Database) -> String {
if db.is_in_memory_db() {
// For in-memory databases, SQLite shows empty string
String::new()
} else {
// For file databases, try to show the full absolute path if that doesn't fail
match std::fs::canonicalize(&db.path) {
Ok(abs_path) => abs_path.to_string_lossy().to_string(),
Err(_) => db.path.to_string(),
}
}
}
/// List all databases (main + attached) with their sequence numbers, names, and file paths
/// Returns a vector of tuples: (seq_number, name, file_path)
pub fn list_all_databases(&self) -> Vec<(usize, String, String)> {
let mut databases = Vec::new();
// Add main database (always seq=0, name="main")
let main_path = Self::get_canonical_path_for_database(&self.db);
databases.push((MAIN_DB_ID, "main".to_string(), main_path));
// SQLite only exposes the temp schema in database_list after it has
// been initialized, and reports an empty path rather than the backing
// temp filename.
if self.temp.database.read().is_some() {
databases.push((crate::TEMP_DB_ID, "temp".to_string(), String::new()));
}
// Add attached databases
let attached_dbs = self.attached_databases.read();
for (alias, &seq_number) in attached_dbs.name_to_index.iter() {
let file_path = if let Some((db, _pager)) = attached_dbs.index_to_data.get(&seq_number)
{
Self::get_canonical_path_for_database(db)
} else {
String::new()
};
databases.push((seq_number, alias.clone(), file_path));
}
// Sort by sequence number to ensure consistent ordering
databases.sort_by_key(|&(seq, _, _)| seq);
databases
}
pub fn get_pager(&self) -> Arc<Pager> {
self.pager.load().clone()
}
pub fn get_query_only(&self) -> bool {
self.is_query_only()
}
pub fn set_query_only(&self, value: bool) {
self.query_only.store(value, Ordering::SeqCst);
self.bump_prepare_context_generation();
}
pub fn set_vdbe_trace(&self, value: bool) {
self.vdbe_trace.store(value, Ordering::SeqCst);
}
pub fn get_vdbe_trace(&self) -> bool {
self.vdbe_trace.load(Ordering::SeqCst)
}
pub fn get_dml_require_where(&self) -> bool {
self.dml_require_where.load(Ordering::SeqCst)
}
pub fn set_dml_require_where(&self, value: bool) {
self.dml_require_where.store(value, Ordering::SeqCst);
}
pub fn get_dqs_dml(&self) -> bool {
self.dqs_dml.load(Ordering::SeqCst)
}
pub fn set_dqs_dml(&self, value: bool) {
self.dqs_dml.store(value, Ordering::SeqCst);
self.bump_prepare_context_generation();
}
pub fn get_full_column_names(&self) -> bool {
self.full_column_names.load(Ordering::SeqCst)
}
pub fn set_full_column_names(&self, value: bool) {
self.full_column_names.store(value, Ordering::SeqCst);
self.bump_prepare_context_generation();
}
pub fn get_short_column_names(&self) -> bool {
self.short_column_names.load(Ordering::SeqCst)
}
pub fn set_short_column_names(&self, value: bool) {
self.short_column_names.store(value, Ordering::SeqCst);
self.bump_prepare_context_generation();
}
pub fn get_sync_mode(&self) -> SyncMode {
self.sync_mode.get()
}
pub fn set_sync_mode(&self, mode: SyncMode) {
self.sync_mode.set(mode);
self.bump_prepare_context_generation();
}
pub fn get_temp_store(&self) -> crate::TempStore {
self.temp_store.get()
}
pub fn set_temp_store(&self, value: crate::TempStore) {
if self.temp_store.get() == value {
return;
}
self.reset_temp_database();
self.temp_store.set(value);
self.bump_prepare_context_generation();
}
/// Find a sequence by name, supporting optional schema qualification.
///
/// - `"my_seq"` → searches main database only
/// - `"aux.my_seq"` → searches the attached database named `aux`
pub fn find_sequence(&self, name: &str) -> Result<Arc<crate::schema::Sequence>> {
let (db_id, seq_name) = if let Some((schema, seq)) = name.split_once('.') {
let db_id = self.get_database_id_by_name(schema)?;
(db_id, crate::util::normalize_ident(seq))
} else {
(MAIN_DB_ID, crate::util::normalize_ident(name))
};
self.with_schema(db_id, |schema| {
schema.get_sequence(&seq_name).map(Arc::clone)
})
.ok_or_else(|| LimboError::ParseError(format!("sequence \"{name}\" does not exist")))
}
/// Record that this connection has seen a value from the named sequence (for currval).
pub fn set_sequence_currval(&self, name: &str, value: i64) {
let normalized = crate::util::normalize_ident(name);
self.sequence_currvals.write().insert(normalized, value);
}
/// Get the last value returned by nextval/setval for the named sequence on this connection.
pub fn get_sequence_currval(&self, name: &str) -> Option<i64> {
let normalized = crate::util::normalize_ident(name);
self.sequence_currvals.read().get(&normalized).copied()
}
/// Drop this connection's currval entry for a sequence. Called on DROP
/// SEQUENCE (and implicit drops via DROP TABLE on AUTOINCREMENT) so that
/// a subsequent `CREATE SEQUENCE <same-name>` does not silently inherit
/// the stale per-session currval from the prior sequence — `currval()`
/// on the fresh sequence must error with "not yet defined in this
/// session" until a nextval/setval establishes it.
pub fn clear_sequence_currval(&self, name: &str) {
let normalized = crate::util::normalize_ident(name);
self.sequence_currvals.write().remove(&normalized);
}
/// Total times this connection's autonomous sequence inner-tx ran into
/// a transient conflict (`WriteWriteConflict` / `BusySnapshot` /
/// `Conflict(_)`) and was retried by `op_sequence_commit_inner_tx`.
/// A non-CYCLE nextval on a non-contended seq must keep this at zero —
/// the regression test for "no inline backing-table compaction"
/// asserts the delta is 0 across the concurrent-nextval scenario.
pub fn sequence_inner_retries(&self) -> u64 {
self.sequence_inner_retries
.load(std::sync::atomic::Ordering::Relaxed)
}
/// Reset the inner-tx retry counter. Test-only helper so a setup
/// phase (priming the backing table, etc.) doesn't pollute the
/// counter the assertion phase observes.
#[doc(hidden)]
pub fn reset_sequence_inner_retries(&self) {
self.sequence_inner_retries
.store(0, std::sync::atomic::Ordering::Relaxed);
}
/// Bootstrap-time sequence descriptor loader. Used by MVCC bootstrap
/// after log recovery: walks `__turso_internal_seq_*` tables and registers
/// a pure descriptor for each into the active schema. No atomic state is
/// seeded — the runtime watermark is always read from disk by
/// nextval/setval.
///
/// Non-blocking: driven by the bootstrap state machine via `return_if_io!`,
/// so the per-backing-table descriptor read yields IO rather than pumping
/// `io.step()`. Re-entrant — the worklist and in-flight read live in
/// `state`.
pub(crate) fn load_sequence_descriptors_via_sql_nonblock(
self: &Arc<Connection>,
state: &mut LoadSequenceDescriptorsState,
) -> Result<crate::types::IOResult<()>> {
use crate::types::IOResult;
loop {
match state {
LoadSequenceDescriptorsState::Start => {
// Walk schema.tables in-memory rather than issuing a SELECT
// against sqlite_master — avoids the side-effects of running
// a fresh statement here, which can leave the connection's
// mv_tx in a non-exclusive state and cause the next DDL to
// trip the exclusive-tx guard in op_open_write.
let pending =
self.with_schema(MAIN_DB_ID, |s| s.sequence_backing_table_names());
*state = LoadSequenceDescriptorsState::Reading {
pending,
idx: 0,
stmt: None,
meta: None,
seq: None,
watermark_stmt: None,
watermark_row: None,
};
}
LoadSequenceDescriptorsState::Reading {
pending,
idx,
stmt,
meta,
seq,
watermark_stmt,
watermark_row,
} => loop {
let entry = {
if *idx >= pending.len() {
return Ok(IOResult::Done(()));
}
pending[*idx].clone()
};
let (backing_table_name, seq_name) = entry;
let normalized = crate::util::normalize_ident(&seq_name);
let already_present =
self.with_schema(MAIN_DB_ID, |s| s.get_sequence(&normalized).is_some());
if already_present {
*idx += 1;
*stmt = None;
*meta = None;
*seq = None;
*watermark_stmt = None;
*watermark_row = None;
continue;
}
if seq.is_none() {
crate::return_if_io!(self.read_seq_descriptor_row_nonblock(
&backing_table_name,
&seq_name,
stmt,
meta,
));
*seq = Some(Self::sequence_from_descriptor_meta(
&seq_name,
&backing_table_name,
*meta,
)?);
*stmt = None;
*meta = None;
}
let sequence = seq.as_ref().expect("sequence set above");
crate::return_if_io!(self.read_sequence_watermark_row_nonblock(
&backing_table_name,
sequence,
watermark_stmt,
watermark_row,
));
let watermark = Self::sequence_watermark_from_row(
&backing_table_name,
sequence,
*watermark_row,
)?;
if let Some(mv_store) = self.db.get_mv_store().as_ref() {
mv_store.set_sequence_watermark(&normalized, watermark);
}
let sequence = seq.take().expect("sequence set above");
self.with_database_schema_mut(MAIN_DB_ID, |schema| {
schema
.sequences
.insert(normalized.clone(), Arc::new(sequence));
})?;
*idx += 1;
*stmt = None;
*meta = None;
*watermark_stmt = None;
*watermark_row = None;
},
}
}
}
/// Drive one backing-table descriptor read to completion (re-entrant).
/// Lazily prepares the `SELECT` into `*stmt`, then runs it non-blocking,
/// stashing the captured row in `*meta`. The backing table is internal
/// (`__turso_internal_seq_*`); a prepare/read failure is on-disk
/// corruption, not "the sequence doesn't exist", so it surfaces
/// `LimboError::Corrupt` — silently dropping the sequence would manifest
/// as a misleading "sequence does not exist" error on the next nextval
/// that masks the real problem.
fn read_seq_descriptor_row_nonblock(
self: &Arc<Connection>,
backing_table_name: &str,
seq_name: &str,
stmt: &mut Option<Box<Statement>>,
meta: &mut Option<(i64, i64, i64, i64, bool)>,
) -> Result<crate::types::IOResult<()>> {
use crate::types::IOResult;
if stmt.is_none() {
let escaped = backing_table_name.replace('"', "\"\"");
let sql = format!("SELECT start, inc, min, max, cycle FROM \"{escaped}\" LIMIT 1");
let prepared = self.prepare_internal(sql).map_err(|err| {
LimboError::Corrupt(format!(
"internal sequence backing table \"{backing_table_name}\" for sequence \
\"{seq_name}\": cannot prepare descriptor SELECT: {err}"
))
})?;
*stmt = Some(Box::new(prepared));
// Fresh statement → clear any descriptor captured for a prior backing
// table, so an empty backing table is detected as missing-row
// corruption rather than silently reusing the previous descriptor.
*meta = None;
}
let s = stmt.as_mut().expect("stmt set above");
match s.run_with_row_callback_nonblock(|row| {
*meta = Some((
row.get::<i64>(0)?,
row.get::<i64>(1)?,
row.get::<i64>(2)?,
row.get::<i64>(3)?,
row.get::<i64>(4)? != 0,
));
Ok(())
}) {
Ok(IOResult::IO(io)) => Ok(IOResult::IO(io)),
Ok(IOResult::Done(())) => Ok(IOResult::Done(())),
Err(err) => Err(LimboError::Corrupt(format!(
"internal sequence backing table \"{backing_table_name}\" for sequence \
\"{seq_name}\": descriptor row read failed: {err}"
))),
}
}
/// Build a `Sequence` from a descriptor row captured by
/// [`Self::read_seq_descriptor_row_nonblock`]. An absent/invalid descriptor
/// is on-disk corruption (see that method's doc).
fn sequence_from_descriptor_meta(
seq_name: &str,
backing_table_name: &str,
meta: Option<(i64, i64, i64, i64, bool)>,
) -> Result<crate::schema::Sequence> {
let (start, inc, min, max, cycle) = meta.ok_or_else(|| {
LimboError::Corrupt(format!(
"internal sequence backing table \"{backing_table_name}\" for sequence \
\"{seq_name}\" is empty; the descriptor metadata row must always be present"
))
})?;
crate::schema::Sequence::new(
seq_name.to_string(),
Some(start),
Some(inc),
Some(min),
Some(max),
cycle,
)
.map_err(|err| {
LimboError::Corrupt(format!(
"internal sequence backing table \"{backing_table_name}\" for sequence \
\"{seq_name}\" descriptor is invalid: {err}"
))
})
}
/// Drive one backing-table watermark read to completion (re-entrant).
///
/// The returned row is converted by [`Self::sequence_watermark_from_row`]
/// into the exclusive upper bound used by `sequence_watermark_experimental()`.
fn read_sequence_watermark_row_nonblock(
self: &Arc<Connection>,
backing_table_name: &str,
seq: &crate::schema::Sequence,
stmt: &mut Option<Box<Statement>>,
row: &mut Option<(i64, bool)>,
) -> Result<crate::types::IOResult<()>> {
use crate::types::IOResult;
if stmt.is_none() {
let escaped = backing_table_name.replace('"', "\"\"");
let direction = if seq.increment_by >= 0 { "DESC" } else { "ASC" };
let sql = format!(
"SELECT value, is_called FROM \"{escaped}\" ORDER BY value {direction} LIMIT 1"
);
let prepared = self.prepare_internal(sql).map_err(|err| {
LimboError::Corrupt(format!(
"internal sequence backing table \"{backing_table_name}\" for sequence \
\"{}\": cannot prepare watermark SELECT: {err}",
seq.name
))
})?;
*stmt = Some(Box::new(prepared));
*row = None;
}
let s = stmt.as_mut().expect("stmt set above");
match s.run_with_row_callback_nonblock(|r| {
let value = r.get::<i64>(0)?;
let is_called = r.get::<i64>(1)? != 0;
*row = Some((value, is_called));
Ok(())
}) {
Ok(IOResult::IO(io)) => Ok(IOResult::IO(io)),
Ok(IOResult::Done(())) => Ok(IOResult::Done(())),
Err(err) => Err(LimboError::Corrupt(format!(
"internal sequence backing table \"{backing_table_name}\" for sequence \
\"{}\": watermark row read failed: {err}",
seq.name
))),
}
}
fn sequence_watermark_from_row(
backing_table_name: &str,
seq: &crate::schema::Sequence,
row: Option<(i64, bool)>,
) -> Result<i64> {
let (value, is_called) = row.ok_or_else(|| {
LimboError::Corrupt(format!(
"internal sequence backing table \"{backing_table_name}\" for sequence \
\"{}\" is empty; cannot derive sequence watermark",
seq.name
))
})?;
Ok(crate::mvcc::database::first_unsafe_sequence_watermark(
seq, value, is_called,
))
}
/// Sync AUTOINCREMENT backing-table watermarks from `sqlite_sequence`.
///
/// Covers the WAL→MVCC mode-switch compatibility path: a WAL-mode
/// database with AUTOINCREMENT tables tracks the high-water mark in
/// `sqlite_sequence` (legacy SQLite contract) and never writes to
/// the backing table created by CREATE TABLE bytecode. The MVCC
/// AUTOINCREMENT path reads the backing table to compute the next
/// rowid, so without a sync step the next INSERT would regress to
/// start_value and collide with the existing rowid.
///
/// For each `name` in `sqlite_sequence`, locate the backing table
/// `__turso_internal_seq___turso_internal_autoincrement_<name>` and,
/// if its current MAX(value) is below the sqlite_sequence value,
/// INSERT a new watermark row to advance it. This is the same
/// pattern the translator emits for `emit_disk_advance_past`,
/// expressed as statement-level SQL so it can run at bootstrap.
///
/// Tables whose backing table is missing are skipped — that
/// indicates the table was never an AUTOINCREMENT under Turso's
/// CREATE TABLE bytecode (i.e. it predates this engine touching
/// the DB), and synthesising a backing table here would forge data
/// the user did not author. Importing a foreign SQLite database is
/// out of scope for this helper.
///
/// Non-blocking: driven by the bootstrap state machine via `return_if_io!`.
/// Each step is on the correctness path documented above — a silent failure
/// leaves MVCC AUTOINCREMENT able to re-emit a rowid already in use after a
/// WAL→MVCC mode switch, so errors propagate to fail the open rather than
/// continue into a state where the next INSERT NULL collides on disk.
pub(crate) fn sync_autoincrement_backing_tables_from_sqlite_sequence_nonblock(
self: &Arc<Connection>,
state: &mut SyncAutoincrementState,
) -> Result<crate::types::IOResult<()>> {
use crate::schema::{autoincrement_sequence_name, SQLITE_SEQUENCE_TABLE_NAME};
use crate::translate::sequence::sequence_backing_table_name;
use crate::types::IOResult;
loop {
match state {
SyncAutoincrementState::Start => {
let has_seq_table = self.with_schema(MAIN_DB_ID, |s| {
s.get_btree_table(SQLITE_SEQUENCE_TABLE_NAME).is_some()
});
if !has_seq_table {
return Ok(IOResult::Done(()));
}
let stmt = self.prepare_internal(format!(
"SELECT name, seq FROM {SQLITE_SEQUENCE_TABLE_NAME}"
))?;
*state = SyncAutoincrementState::ReadSeqRows {
stmt: Box::new(stmt),
rows: Vec::new(),
};
}
SyncAutoincrementState::ReadSeqRows { stmt, rows } => {
crate::return_if_io!(stmt.run_with_row_callback_nonblock(|row| {
let name = row.get::<&str>(0)?.to_string();
let seq = row.get::<i64>(1)?;
rows.push((name, seq));
Ok(())
}));
let rows = std::mem::take(rows);
*state = SyncAutoincrementState::Process {
rows,
idx: 0,
sub: SyncRowStep::Start,
};
}
SyncAutoincrementState::Process { rows, idx, sub } => {
if *idx >= rows.len() {
return Ok(IOResult::Done(()));
}
match sub {
SyncRowStep::Start => {
let backing_table_name = sequence_backing_table_name(
&autoincrement_sequence_name(&rows[*idx].0),
);
let has_backing = self.with_schema(MAIN_DB_ID, |s| {
s.get_btree_table(&backing_table_name).is_some()
});
if !has_backing {
*idx += 1;
continue;
}
// Read current backing watermark; only upsert if we'd
// actually advance it (avoids needless writes on boot).
let escaped = backing_table_name.replace('"', "\"\"");
let stmt = self.prepare_internal(format!(
"SELECT MAX(value) FROM \"{escaped}\""
))?;
*sub = SyncRowStep::ReadMax {
backing_table_name,
stmt: Box::new(stmt),
current_max: None,
};
}
SyncRowStep::ReadMax {
backing_table_name,
stmt,
current_max,
} => {
crate::return_if_io!(stmt.run_with_row_callback_nonblock(|row| {
if let crate::Value::Numeric(crate::Numeric::Integer(v)) =
row.get_value(0)
{
*current_max = Some(*v);
}
Ok(())
}));
let watermark = rows[*idx].1;
// Skip only when the backing table is already strictly
// ahead; an equal value is NOT enough because the
// initial row written by CREATE TABLE bytecode is
// (value=1, is_called=false), which would cause the
// next nextval to re-emit value=1 and collide with the
// rowid already inserted in WAL mode. We always upsert
// with is_called=1 so the next nextval computes
// watermark+1 like sqlite_sequence semantics demand.
if matches!(*current_max, Some(c) if c > watermark) {
*idx += 1;
*sub = SyncRowStep::Start;
continue;
}
// Standard AUTOINCREMENT descriptor columns (start=1,
// inc=1, min=1, max=i64::MAX, cycle=0) — mirror what
// the translator emits when CREATE TABLE bytecode
// creates the backing table for an AUTOINCREMENT column.
let escaped = backing_table_name.replace('"', "\"\"");
let insert_sql = format!(
"INSERT OR REPLACE INTO \"{escaped}\"\
(value, is_called, start, inc, min, max, cycle) \
VALUES ({watermark}, 1, 1, 1, 1, {}, 0)",
i64::MAX
);
let stmt = self.prepare_internal(insert_sql)?;
*sub = SyncRowStep::Upsert {
stmt: Box::new(stmt),
};
}
SyncRowStep::Upsert { stmt } => {
crate::return_if_io!(stmt.run_with_row_callback_nonblock(|_| Ok(())));
if let Some(mv_store) = self.db.get_mv_store().as_ref() {
let watermark = rows[*idx].1;
let first_unsafe = watermark.checked_add(1).unwrap_or(watermark);
mv_store.set_sequence_watermark(
&autoincrement_sequence_name(&rows[*idx].0),
first_unsafe,
);
}
*idx += 1;
*sub = SyncRowStep::Start;
}
}
}
}
}
}
/// Create a `TempDir` honoring `TURSO_TMPDIR` and `SQLITE_TMPDIR`,
/// falling back to the OS default (`env::temp_dir()`).
///
/// `&self` is reserved for a future per-connection
/// `temp_store_directory` setting (e.g. `PRAGMA temp_store_directory`)
/// so call sites don't need to change when that lands.
#[cfg(not(target_family = "wasm"))]
pub(crate) fn create_tempdir(&self) -> Result<TempDir> {
let res = if let Some(d) = std::env::var_os("TURSO_TMPDIR") {
tempfile::tempdir_in(d)
} else if let Some(d) = std::env::var_os("SQLITE_TMPDIR") {
tempfile::tempdir_in(d)
} else {
tempfile::tempdir()
};
res.map_err(|e| io_error(e, "tempdir"))
}
pub fn get_data_sync_retry(&self) -> bool {
self.data_sync_retry
.load(crate::sync::atomic::Ordering::SeqCst)
}
pub fn set_data_sync_retry(&self, value: bool) {
self.data_sync_retry
.store(value, crate::sync::atomic::Ordering::SeqCst);
self.bump_prepare_context_generation();
}
/// Get the sync type setting.
pub fn get_sync_type(&self) -> crate::io::FileSyncType {
self.pager.load().get_sync_type()
}
/// Set the sync type (for PRAGMA fullfsync).
pub fn set_sync_type(&self, value: crate::io::FileSyncType) {
self.pager.load().set_sync_type(value);
}
/// Creates a HashSet of modules that have been loaded
pub fn get_syms_vtab_mods(&self) -> HashSet<String> {
self.syms.read().vtab_modules.keys().cloned().collect()
}
/// Returns external (extension) functions: (name, is_aggregate, argc, deterministic)
pub fn get_syms_functions(&self) -> Vec<(String, bool, i32, bool)> {
self.syms
.read()
.functions
.values()
.map(|f| {
let is_agg = f.func.is_aggregate();
let argc = match &f.func {
function::ExtFunc::Aggregate { argc, .. } => *argc,
function::ExtFunc::Scalar { argc, .. } => *argc,
};
(
f.name.clone(),
is_agg,
argc,
function::Deterministic::is_deterministic(f.as_ref()),
)
})
.collect()
}
pub fn register_external_collation(
&self,
name: String,
context: usize,
callback: crate::ContextCollationFunction,
context_destructor: Option<crate::ContextDestructor>,
) {
let collation = CollationSeq::custom(&name);
let normalized_name = crate::util::normalize_ident(&name);
self.syms.write().collations.insert(
collation.id(),
Arc::new(function::ExternalCollation::new(
normalized_name,
context,
callback,
context_destructor,
)),
);
self.bump_prepare_context_generation();
}
pub fn unregister_external_collation(&self, name: &str) {
if let Some(collation) = CollationSeq::known_custom(name) {
if self
.syms
.write()
.collations
.remove(&collation.id())
.is_some()
{
self.bump_prepare_context_generation();
}
}
}
pub(crate) fn get_external_collation(
&self,
collation: CollationSeq,
) -> Result<Arc<function::ExternalCollation>> {
self.syms
.read()
.collations
.get(&collation.id())
.cloned()
.ok_or_else(|| {
LimboError::ParseError(format!("no such collation sequence: {}", collation.name()))
})
}
pub(crate) fn custom_collation_compare(
external: &function::ExternalCollation,
left: &str,
right: &str,
) -> CmpOrdering {
let result = unsafe {
(external.callback)(
external.context,
left.as_ptr(),
left.len(),
right.as_ptr(),
right.len(),
)
};
result.cmp(&0)
}
pub(crate) fn external_collation_comparator(
external: Arc<function::ExternalCollation>,
) -> crate::vdbe::sorter::SortComparator {
Arc::new(move |left, right| {
Ok(match (left, right) {
(crate::ValueRef::Text(left), crate::ValueRef::Text(right)) => {
Self::custom_collation_compare(&external, left.as_str(), right.as_str())
}
_ => left.partial_cmp(right).unwrap_or(CmpOrdering::Equal),
})
})
}
pub(crate) fn make_collation_comparator(
&self,
collation: CollationSeq,
) -> Result<crate::vdbe::sorter::SortComparator> {
let external = self.get_external_collation(collation)?;
Ok(Self::external_collation_comparator(external))
}
pub(crate) fn compare_external_collation(
&self,
collation: CollationSeq,
left: &str,
right: &str,
) -> Result<CmpOrdering> {
let external = self.get_external_collation(collation)?;
Ok(Self::custom_collation_compare(&external, left, right))
}
pub(crate) fn database_ptr(&self) -> usize {
Arc::as_ptr(&self.db) as usize
}
pub fn set_encryption_key(&self, key: EncryptionKey) -> Result<()> {
tracing::trace!("setting encryption key for connection");
self.ensure_can_change_encryption_settings()?;
*self.encryption_key.write() = Some(key);
self.bump_prepare_context_generation();
self.set_encryption_context()
}
pub fn set_encryption_cipher(&self, cipher_mode: CipherMode) -> Result<()> {
tracing::trace!("setting encryption cipher for connection");
self.ensure_can_change_encryption_settings()?;
self.encryption_cipher_mode.set(cipher_mode);
self.bump_prepare_context_generation();
self.set_encryption_context()
}
pub fn set_reserved_bytes(&self, reserved_bytes: u8) -> Result<()> {
let pager = self.pager.load();
pager.set_reserved_space_bytes(reserved_bytes);
Ok(())
}
/// Get the reserved bytes value from the pager cache.
/// Returns None if not yet set (database not initialized).
pub fn get_reserved_bytes(&self) -> Option<u8> {
let pager = self.pager.load();
pager.get_reserved_space()
}
pub fn get_encryption_cipher_mode(&self) -> Option<CipherMode> {
match self.encryption_cipher_mode.get() {
CipherMode::None => None,
mode => Some(mode),
}
}
fn ensure_can_change_encryption_settings(&self) -> Result<()> {
let pager = self.pager.load();
if pager.is_encryption_ctx_set() {
return Err(LimboError::InvalidArgument(
"cannot reset encryption attributes if already set in the session".to_string(),
));
}
if self.db.get_mv_store().is_some() {
return Err(LimboError::InvalidArgument(
"cannot enable encryption after MVCC is active; configure encryption before PRAGMA journal_mode='mvcc'"
.to_string(),
));
}
Ok(())
}
// if both key and cipher are set, set encryption context on pager
fn set_encryption_context(&self) -> Result<()> {
let key_guard = self.encryption_key.read();
let Some(key) = key_guard.as_ref() else {
return Ok(());
};
let cipher_mode = self.get_encryption_cipher_mode();
let Some(cipher_mode) = cipher_mode else {
return Ok(());
};
tracing::trace!("setting encryption ctx for connection");
let pager = self.pager.load();
pager.set_encryption_context(cipher_mode, key)
}
/// Sets a custom busy handler callback.
pub fn set_busy_handler(&self, handler: Option<BusyHandlerCallback>) {
*self.busy_handler.write() = match handler {
Some(callback) => BusyHandler::Custom { callback },
None => BusyHandler::None,
};
self.bump_prepare_context_generation();
}
/// Sets maximum total accumulated timeout. If the duration is Zero, we unset the busy handler.
pub fn set_busy_timeout(&self, duration: Duration) {
*self.busy_handler.write() = if duration.is_zero() {
BusyHandler::None
} else {
BusyHandler::Timeout(duration)
};
self.bump_prepare_context_generation();
}
/// Get the busy timeout duration.
pub fn get_busy_timeout(&self) -> Duration {
match &*self.busy_handler.read() {
BusyHandler::Timeout(d) => *d,
_ => Duration::ZERO,
}
}
/// Sets the maximum duration a statement is allowed to run.
/// `Duration::ZERO` disables query timeout.
pub fn set_query_timeout(&self, duration: Duration) {
let millis = duration.as_millis().min(u128::from(u64::MAX)) as u64;
self.query_timeout_ms.store(millis, Ordering::SeqCst);
}
/// Get the query timeout duration.
pub fn get_query_timeout(&self) -> Duration {
Duration::from_millis(self.query_timeout_ms.load(Ordering::SeqCst))
}
/// Get a reference to the busy handler.
pub fn get_busy_handler(&self) -> crate::sync::RwLockReadGuard<'_, BusyHandler> {
self.busy_handler.read()
}
/// Sets a progress handler invoked approximately every `ops` VM steps.
/// Passing `ops == 0` or `None` disables the progress handler.
pub fn set_progress_handler(&self, ops: u64, handler: Option<ProgressHandlerCallback>) {
self.progress_handler.set(ops, handler);
}
/// Returns true when the step-based progress handler requests interruption.
pub fn should_interrupt_for_progress(&self, vm_steps: u64) -> bool {
self.progress_handler.should_interrupt(vm_steps)
}
/// Request interruption of currently running root statements on this connection.
/// If no root statement is active, the request is ignored to match SQLite semantics.
pub fn interrupt(&self) {
if self.n_active_root_statements.load(Ordering::SeqCst) > 0 {
self.interrupt_requested.store(true, Ordering::SeqCst);
}
}
/// Returns true if an interrupt is currently pending for this connection.
pub fn is_interrupted(&self) -> bool {
self.interrupt_requested.load(Ordering::SeqCst)
}
/// Clear the connection interrupt once no root statements remain active.
pub(crate) fn clear_interrupt_if_idle(&self) {
if self.n_active_root_statements.load(Ordering::SeqCst) == 0 {
self.interrupt_requested.store(false, Ordering::SeqCst);
}
}
pub(crate) fn set_tx_state(&self, state: TransactionState) {
self.transaction_state.set(state);
}
pub(crate) fn get_tx_state(&self) -> TransactionState {
self.transaction_state.get()
}
/// Returns true if the connection is currently in a write transaction.
/// Used by index methods to determine if it's safe to flush writes.
pub fn is_in_write_tx(&self) -> bool {
matches!(self.get_tx_state(), TransactionState::Write { .. })
}
pub(crate) fn get_mv_tx_id(&self) -> Option<u64> {
self.mv_tx.read().map(|(tx_id, _)| tx_id)
}
pub(crate) fn get_mv_tx(&self) -> Option<(u64, TransactionMode)> {
*self.mv_tx.read()
}
#[inline(always)]
pub(crate) fn set_mv_tx(&self, tx_id_and_mode: Option<(u64, TransactionMode)>) {
tracing::debug!("set_mv_tx: {:?}", tx_id_and_mode);
*self.mv_tx.write() = tx_id_and_mode;
}
/// Get MVCC transaction ID for a specific database.
/// Uses fast path for main DB, O(1) HashMap lookup for attached DBs.
pub(crate) fn get_mv_tx_id_for_db(&self, db: usize) -> Option<u64> {
if db == crate::MAIN_DB_ID {
self.get_mv_tx_id()
} else {
self.attached_mv_txs
.read()
.get(&db)
.map(|(tx_id, _)| *tx_id)
}
}
/// Get MVCC transaction ID and mode for a specific database.
pub(crate) fn get_mv_tx_for_db(&self, db: usize) -> Option<(u64, TransactionMode)> {
if db == crate::MAIN_DB_ID {
self.get_mv_tx()
} else {
self.attached_mv_txs.read().get(&db).copied()
}
}
/// Set MVCC transaction for a specific database.
pub(crate) fn set_mv_tx_for_db(&self, db: usize, val: Option<(u64, TransactionMode)>) {
if db == crate::MAIN_DB_ID {
self.set_mv_tx(val);
} else {
let mut txs = self.attached_mv_txs.write();
match val {
Some(v) => {
txs.insert(db, v);
}
None => {
txs.remove(&db);
}
}
}
}
/// Rollback MVCC transactions on all attached databases and clear the
/// attached transaction list. When `clear_schemas` is true the
/// connection-local schema cache for each attached DB is also removed so
/// that post-rollback queries see the committed schema.
///
/// This is the single source of truth for attached-MVCC rollback logic —
/// callers in `close()`, `rollback_current_txn()`, and `op_auto_commit`
/// should all delegate here.
pub(crate) fn rollback_attached_mvcc_txs(&self, clear_schemas: bool) {
let txs: HashMap<usize, _> = self.attached_mv_txs.read().clone();
let mut cleared_any_schema = false;
for (&db_id, &(tx_id, _mode)) in &txs {
if let Some(attached_mv_store) = self.mv_store_for_db(db_id) {
let attached_pager = self
.get_pager_from_database_index(&db_id)
.expect("attached MVCC transaction should always have a pager");
if attached_mv_store.is_tx_rollbackable(tx_id) {
attached_mv_store.rollback_tx(tx_id, attached_pager.clone(), self, db_id);
} else {
self.set_mv_tx_for_db(db_id, None);
}
if clear_schemas {
self.database_schemas().write().remove(&db_id);
cleared_any_schema = true;
}
attached_pager.end_read_tx();
}
}
self.attached_mv_txs.write().clear();
if cleared_any_schema {
self.bump_prepare_context_generation();
}
}
/// Rollback WAL-mode transactions on all attached databases and discard
/// their connection-local schema caches. MVCC-enabled attached databases
/// are skipped — those are handled by `rollback_attached_mvcc_txs`.
pub(crate) fn rollback_attached_wal_txns(&self) {
self.with_all_attached_pagers_with_index(|pagers| {
// Record indices of WAL-mode entries so we can batch the schema
// removal under a single write lock and avoid calling
// `mv_store_for_db` more than once per entry.
let mut wal_indices: SmallVec<[usize; 4]> = SmallVec::new();
for (i, (db_id, _)) in pagers.iter().enumerate() {
if self.mv_store_for_db(*db_id).is_none() {
wal_indices.push(i);
}
}
if wal_indices.is_empty() {
return;
}
{
let mut schemas = self.database_schemas().write();
for &i in &wal_indices {
schemas.remove(&pagers[i].0);
}
}
self.bump_prepare_context_generation();
for &i in &wal_indices {
pagers[i].1.rollback_attached();
}
});
}
pub(crate) fn with_named_savepoints<F, T>(&self, f: F) -> T
where
F: FnOnce(&[NamedSavepointFrame]) -> T,
{
let savepoints = self.named_savepoints.read();
f(&savepoints)
}
pub(crate) fn push_named_savepoint(&self, frame: NamedSavepointFrame) {
self.named_savepoints.write().push(frame);
}
/// Snapshot the in-memory schemas (main, temp, attached) for a
/// savepoint frame so ROLLBACK TO can restore them without re-
/// reading sqlite_schema from disk. Disk reparse from inside the
/// vdbe ROLLBACK TO opcode would block on cursor I/O and violate
/// the vdbe async contract.
pub(crate) fn with_savepoint_schema_snapshot<F, T>(&self, f: F) -> T
where
F: FnOnce(Arc<Schema>, Option<Arc<Schema>>, HashMap<usize, Arc<Schema>>) -> T,
{
let main_schema_snapshot = self.schema.read().clone();
let temp_schema_snapshot = self
.temp
.database
.read()
.as_ref()
.map(|temp_db| temp_db.db.schema.lock().clone());
let staged_schema_snapshot = self.database_schemas.read().clone();
f(
main_schema_snapshot,
temp_schema_snapshot,
staged_schema_snapshot,
)
}
pub(crate) fn release_named_savepoint_frame(&self, name: &str) -> SavepointResult {
let mut savepoints = self.named_savepoints.write();
let Some(target_idx) = savepoints
.iter()
.rposition(|savepoint| savepoint.name == name)
else {
return SavepointResult::NotFound;
};
if savepoints[target_idx].starts_transaction && target_idx == 0 {
return SavepointResult::Commit;
}
savepoints.truncate(target_idx);
SavepointResult::Release
}
pub(crate) fn rollback_named_savepoint_frame(&self, name: &str) -> Option<RollbackFrameInfo> {
let mut savepoints = self.named_savepoints.write();
let target_idx = savepoints
.iter()
.rposition(|savepoint| savepoint.name == name)?;
let frame = &savepoints[target_idx];
let info = RollbackFrameInfo {
main_schema_snapshot: frame.main_schema_snapshot.clone(),
temp_schema_snapshot: frame.temp_schema_snapshot.clone(),
staged_schema_snapshot: frame.staged_schema_snapshot.clone(),
};
// ROLLBACK TO keeps the target savepoint itself on the stack;
// only nested savepoints above it are discarded.
savepoints.truncate(target_idx + 1);
Some(info)
}
pub(crate) fn clear_named_savepoints(&self) {
self.named_savepoints.write().clear();
}
/// Roll back the current main-db transaction state and any attached-db
/// transaction state on this connection.
pub(crate) fn rollback_current_txn_state(
&self,
pager: &Arc<Pager>,
clear_attached_schemas: bool,
) {
if let Some(mv_store) = self.mv_store().as_ref() {
if let Some(tx_id) = self.get_mv_tx_id() {
self.auto_commit.store(true, Ordering::SeqCst);
if mv_store.is_tx_rollbackable(tx_id) {
mv_store.rollback_tx(tx_id, pager.clone(), self, crate::MAIN_DB_ID);
} else {
self.set_mv_tx(None);
}
}
pager.end_read_tx();
self.rollback_attached_mvcc_txs(clear_attached_schemas);
} else {
pager.rollback_tx(self);
self.auto_commit.store(true, Ordering::SeqCst);
}
self.rollback_attached_wal_txns();
self.set_tx_state(TransactionState::None);
self.clear_tx_poison();
}
/// Roll back transaction state for helpers that start a manual `BEGIN`
/// outside the normal Transaction opcode path.
///
/// Unlike `rollback_current_txn_state`, this tolerates the attached-only
/// case where the connection flipped `auto_commit` off but never opened a
/// main-db read transaction.
pub(crate) fn rollback_manual_txn_cleanup(
&self,
pager: &Arc<Pager>,
clear_attached_schemas: bool,
) {
let main_has_implicit_state = self.get_tx_state() != TransactionState::None
|| self.get_mv_tx().is_some()
|| pager.holds_read_lock()
|| pager.holds_write_lock();
if main_has_implicit_state {
self.rollback_current_txn_state(pager, clear_attached_schemas);
} else {
if self.next_attached_mv_tx().is_some() {
self.rollback_attached_mvcc_txs(clear_attached_schemas);
}
self.rollback_attached_wal_txns();
self.set_tx_state(TransactionState::None);
self.auto_commit.store(true, Ordering::SeqCst);
}
self.rollback_temp_schema();
self.clear_tx_poison();
self.set_cdc_transaction_id(-1);
self.clear_named_savepoints();
self.clear_deferred_foreign_key_violations();
}
/// Iterate over all attached MVCC transactions, calling `f(db_id, tx_id)` for each.
pub(crate) fn for_each_attached_mv_tx(&self, mut f: impl FnMut(usize, u64)) {
let txs = self.attached_mv_txs.read();
for (&db_id, &(tx_id, _)) in txs.iter() {
f(db_id, tx_id);
}
}
/// Get the next attached MVCC transaction.
/// Returns an arbitrary entry from `attached_mv_txs`, or `None` if empty.
pub(crate) fn next_attached_mv_tx(&self) -> Option<(usize, u64, TransactionMode)> {
self.attached_mv_txs
.read()
.iter()
.next()
.map(|(&db_id, &(tx_id, mode))| (db_id, tx_id, mode))
}
/// Get the MvStore for a specific database.
/// Returns None for databases without MVCC or for bootstrap connections.
pub(crate) fn mv_store_for_db(&self, db: usize) -> Option<Arc<MvStore>> {
if self.is_mvcc_bootstrap_connection() {
return None;
}
match db {
crate::MAIN_DB_ID => self.db.get_mv_store().as_ref().cloned(),
crate::TEMP_DB_ID => None,
_ => {
let catalog = self.attached_databases.read();
catalog
.index_to_data
.get(&db)
.and_then(|(db, _)| db.get_mv_store().as_ref().cloned())
}
}
}
pub(crate) fn set_mvcc_checkpoint_threshold(&self, threshold: i64) -> Result<()> {
match self.db.get_mv_store().as_ref() {
Some(mv_store) => {
mv_store.set_checkpoint_threshold(threshold);
self.bump_prepare_context_generation();
Ok(())
}
None => Err(LimboError::InternalError("MVCC not enabled".into())),
}
}
pub(crate) fn mvcc_checkpoint_threshold(&self) -> Result<i64> {
match self.db.get_mv_store().as_ref() {
Some(mv_store) => Ok(mv_store.checkpoint_threshold()),
None => Err(LimboError::InternalError("MVCC not enabled".into())),
}
}
pub(crate) fn set_mvcc_gc_threshold(&self, threshold: i64) -> Result<()> {
match self.db.get_mv_store().as_ref() {
Some(mv_store) => {
mv_store.set_gc_threshold(threshold);
self.bump_prepare_context_generation();
Ok(())
}
None => Err(LimboError::InternalError("MVCC not enabled".into())),
}
}
pub(crate) fn mvcc_gc_threshold(&self) -> Result<i64> {
match self.db.get_mv_store().as_ref() {
Some(mv_store) => Ok(mv_store.gc_threshold()),
None => Err(LimboError::InternalError("MVCC not enabled".into())),
}
}
pub(crate) fn mvcc_tx_should_abort(&self) -> bool {
match (self.db.get_mv_store().clone(), self.get_mv_tx_id()) {
(Some(mv_store), Some(tx_id)) => mv_store.tx_should_abort(tx_id),
_ => false,
}
}
}
pub type Row = vdbe::Row;
pub type StepResult = vdbe::StepResult;
#[derive(Default)]
pub struct SymbolTable {
pub functions: HashMap<String, Arc<function::ExternalFunc>>,
pub collations: HashMap<u32, Arc<function::ExternalCollation>>,
pub vtabs: HashMap<String, Arc<VirtualTable>>,
pub vtab_modules: HashMap<String, Arc<crate::ext::VTabImpl>>,
pub index_methods: HashMap<String, Arc<dyn IndexMethod>>,
}
impl std::fmt::Debug for SymbolTable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SymbolTable")
.field("functions", &self.functions)
.field("collations", &self.collations)
.finish()
}
}
fn is_shared_library(path: &std::path::Path) -> bool {
path.extension()
.is_some_and(|ext| ext == "so" || ext == "dylib" || ext == "dll")
}
pub fn resolve_ext_path(extpath: &str) -> Result<std::path::PathBuf> {
let path = std::path::Path::new(extpath);
if !path.exists() {
if is_shared_library(path) {
return Err(LimboError::ExtensionError(format!(
"Extension file not found: {extpath}"
)));
};
let maybe = path.with_extension(std::env::consts::DLL_EXTENSION);
maybe.exists().then_some(maybe).ok_or_else(|| {
LimboError::ExtensionError(format!("Extension file not found: {extpath}"))
})
} else {
Ok(path.to_path_buf())
}
}
impl SymbolTable {
pub fn new() -> Self {
Self {
functions: HashMap::default(),
collations: HashMap::default(),
vtabs: HashMap::default(),
vtab_modules: HashMap::default(),
index_methods: HashMap::default(),
}
}
pub fn resolve_function(
&self,
name: &str,
arg_count: usize,
) -> Option<Arc<function::ExternalFunc>> {
self.functions
.get(name)
.cloned()
.or_else(|| {
self.functions
.get(&crate::util::normalize_ident(name))
.cloned()
})
.filter(|func| func.func.matches_arg_count(arg_count))
}
pub fn resolve_collation(&self, name: &str) -> Option<CollationSeq> {
let collation = CollationSeq::known_custom(name)?;
self.collations
.contains_key(&collation.id())
.then_some(collation)
}
pub fn extend(&mut self, other: &SymbolTable) {
for (name, func) in &other.functions {
self.functions.insert(name.clone(), func.clone());
}
for (id, collation) in &other.collations {
self.collations.insert(*id, collation.clone());
}
for (name, vtab) in &other.vtabs {
self.vtabs.insert(name.clone(), vtab.clone());
}
for (name, module) in &other.vtab_modules {
self.vtab_modules.insert(name.clone(), module.clone());
}
for (name, module) in &other.index_methods {
self.index_methods.insert(name.clone(), module.clone());
}
}
}
#[cfg(all(test, feature = "fs"))]
mod tests {
use super::*;
use tempfile::TempDir;
fn open_connection_with_opts(path: &std::path::Path, opts: DatabaseOpts) -> Arc<Connection> {
let io: Arc<dyn IO> = Arc::new(crate::PlatformIO::new().unwrap());
let db = Database::open_file_with_flags(
io,
path.to_str().unwrap(),
OpenFlags::default(),
opts,
None,
)
.unwrap();
db.connect().unwrap()
}
fn open_connection(path: &std::path::Path) -> Arc<Connection> {
open_connection_with_opts(path, DatabaseOpts::new())
}
fn drive_attach(conn: &Arc<Connection>, path: &str, alias: &str) -> Result<()> {
let mut state = AttachDatabaseState::default();
loop {
match conn.attach_database(path, alias, &mut state)? {
IOResult::Done(()) => return Ok(()),
IOResult::IO(io) => io.wait(conn.db.io.as_ref())?,
}
}
}
fn drive_attach_with_config(
conn: &Arc<Connection>,
path: &str,
alias: &str,
reserved_space: Option<u8>,
) -> Result<()> {
let mut state = AttachDatabaseState::default();
loop {
match conn.attach_database_with_config(path, alias, reserved_space, &mut state)? {
IOResult::Done(()) => return Ok(()),
IOResult::IO(io) => io.wait(conn.db.io.as_ref())?,
}
}
}
fn query_single_i64(conn: &Arc<Connection>, sql: &str) -> i64 {
let mut stmt = conn.prepare(sql).unwrap();
match stmt.step().unwrap() {
StepResult::Row => stmt.row().unwrap().get::<i64>(0).unwrap(),
other => panic!("expected a row, got {other:?}"),
}
}
fn text_value(value: &Value) -> &str {
match value {
Value::Text(text) => text.as_str(),
other => panic!("expected text value, got {other:?}"),
}
}
// given a attached 'alias', return the Database and Pager for that attached database
fn attached_entry(conn: &Connection, alias: &str) -> (Arc<Database>, Arc<Pager>) {
let catalog = conn.attached_databases.read();
let index = *catalog.name_to_index.get(alias).unwrap();
catalog.index_to_data.get(&index).unwrap().clone()
}
#[test]
fn test_named_memory_databases_on_same_io_are_distinct() {
let io: Arc<dyn IO> = Arc::new(MemoryIO::new());
let draft_db = Database::open_file(io.clone(), ":memory:sync-draft").unwrap();
let synced_db = Database::open_file(io, ":memory:sync-synced").unwrap();
assert!(!Arc::ptr_eq(&draft_db, &synced_db));
let draft = draft_db.connect().unwrap();
let synced = synced_db.connect().unwrap();
for conn in [&draft, &synced] {
assert_eq!(conn.get_database_canonical_path(), "");
assert_eq!(
conn.list_all_databases(),
vec![(MAIN_DB_ID, "main".to_string(), String::new())]
);
}
draft
.execute("CREATE TABLE t(x INTEGER); INSERT INTO t VALUES(11)")
.unwrap();
synced
.execute("CREATE TABLE t(x INTEGER); INSERT INTO t VALUES(22)")
.unwrap();
assert_eq!(query_single_i64(&draft, "SELECT x FROM t"), 11);
assert_eq!(query_single_i64(&synced, "SELECT x FROM t"), 22);
}
#[test]
fn test_named_memory_database_reopened_on_same_io_sees_same_rows() {
let io: Arc<dyn IO> = Arc::new(MemoryIO::new());
let first_db = Database::open_file(io.clone(), ":memory:reopen").unwrap();
let first = first_db.connect().unwrap();
first
.execute("CREATE TABLE t(x INTEGER); INSERT INTO t VALUES(99)")
.unwrap();
let second_db = Database::open_file(io, ":memory:reopen").unwrap();
let second = second_db.connect().unwrap();
assert_eq!(query_single_i64(&second, "SELECT x FROM t"), 99);
}
#[test]
fn test_attach_named_memory_database_reports_empty_path() {
let temp_dir = TempDir::new().unwrap();
let main_path = temp_dir.path().join("main.db");
let conn = open_connection_with_opts(&main_path, DatabaseOpts::new().with_attach(true));
conn.execute("ATTACH ':memory:aux' AS aux").unwrap();
conn.execute("CREATE TABLE aux.t(x INTEGER); INSERT INTO aux.t VALUES(5)")
.unwrap();
assert_eq!(query_single_i64(&conn, "SELECT x FROM aux.t"), 5);
let database_list = conn.pragma_query("database_list").unwrap();
let aux = database_list
.iter()
.find(|row| text_value(&row[1]) == "aux")
.expect("attached aux database must be listed");
assert_eq!(text_value(&aux[2]), "");
}
#[test]
fn test_named_memory_parent_can_attach_real_file_database() {
let temp_dir = TempDir::new().unwrap();
let aux_path = temp_dir.path().join("aux.db");
let io: Arc<dyn IO> = Arc::new(MemoryIO::new());
let db = Database::open_file_with_flags(
io,
":memory:named-main",
OpenFlags::default(),
DatabaseOpts::new().with_attach(true),
None,
)
.unwrap();
let conn = db.connect().unwrap();
conn.execute(format!("ATTACH '{}' AS aux", aux_path.to_str().unwrap()))
.unwrap();
conn.execute("CREATE TABLE aux.t(x INTEGER); INSERT INTO aux.t VALUES(7)")
.unwrap();
conn.execute("DETACH aux").unwrap();
let reopened = open_connection(&aux_path);
assert_eq!(query_single_i64(&reopened, "SELECT x FROM t"), 7);
}
#[test]
fn test_attach_database_with_config_overrides_reserved_space_before_initialization() {
let temp_dir = TempDir::new().unwrap();
let main_path = temp_dir.path().join("main.db");
let aux_path = temp_dir.path().join("aux.db");
let conn = open_connection(&main_path);
drive_attach_with_config(&conn, aux_path.to_str().unwrap(), "aux", Some(48)).unwrap();
let (attached_db, pager) = attached_entry(&conn, "aux");
assert!(!attached_db.initialized());
assert!(!pager.db_initialized());
assert_eq!(pager.get_reserved_space(), Some(48));
}
#[cfg(feature = "checksum")]
#[test]
fn test_attach_database_with_config_rejects_reserved_space_below_minimum() {
let temp_dir = TempDir::new().unwrap();
let main_path = temp_dir.path().join("main.db");
let aux_path = temp_dir.path().join("aux.db");
let conn = open_connection(&main_path);
let err = drive_attach_with_config(&conn, aux_path.to_str().unwrap(), "aux", Some(0))
.unwrap_err()
.to_string();
assert_eq!(
err,
"Invalid argument supplied: cannot attach database 'aux': reserved space 0 is smaller than attached database minimum 8"
);
}
#[test]
fn test_fresh_mvcc_attach_installs_wal_before_bootstrap() {
// this is a test to check that mvcc db on attach with a fresh db, makes the
// attached db also mvcc
let temp_dir = TempDir::new().unwrap();
let main_path = temp_dir.path().join("main.db");
let aux_path = temp_dir.path().join("aux.db");
let conn = open_connection(&main_path);
conn.execute("PRAGMA journal_mode = 'mvcc'").unwrap();
drive_attach(&conn, aux_path.to_str().unwrap(), "aux").unwrap();
let (attached_db, pager) = attached_entry(&conn, "aux");
assert!(attached_db.get_mv_store().as_ref().is_some());
assert!(pager.has_wal());
conn.execute("CREATE TABLE aux.t(x INTEGER)").unwrap();
conn.execute("INSERT INTO aux.t VALUES(1)").unwrap();
conn.execute("PRAGMA aux.wal_checkpoint(TRUNCATE)").unwrap();
}
#[test]
fn test_fresh_mvcc_attach_reuses_database_shared_wal() {
let temp_dir = TempDir::new().unwrap();
let main_path = temp_dir.path().join("main.db");
let aux_path = temp_dir.path().join("aux.db");
let conn = open_connection(&main_path);
conn.execute("PRAGMA journal_mode = 'mvcc'").unwrap();
drive_attach(&conn, aux_path.to_str().unwrap(), "aux").unwrap();
conn.execute("CREATE TABLE aux.t(x INTEGER)").unwrap();
conn.execute("INSERT INTO aux.t VALUES(1)").unwrap();
let (attached_db, pager) = attached_entry(&conn, "aux");
let pager_shared_ptr = pager
.wal_shared_ptr()
.expect("fresh MVCC attach must expose WAL shared state in tests");
let db_shared_ptr = Arc::as_ptr(&attached_db.shared_wal) as usize;
assert_eq!(pager_shared_ptr, db_shared_ptr);
}
#[test]
fn test_temp_tables_are_connection_local_and_shadow_main() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("main.db");
let conn1 = open_connection(&db_path);
conn1.execute("CREATE TABLE t(x INTEGER)").unwrap();
conn1.execute("INSERT INTO main.t VALUES(1)").unwrap();
let conn2 = open_connection(&db_path);
conn1.execute("CREATE TEMP TABLE t(x INTEGER)").unwrap();
conn1.execute("INSERT INTO temp.t VALUES(2)").unwrap();
assert_eq!(query_single_i64(&conn1, "SELECT x FROM t"), 2);
assert_eq!(query_single_i64(&conn1, "SELECT x FROM main.t"), 1);
assert_eq!(query_single_i64(&conn2, "SELECT x FROM t"), 1);
let err = conn2
.prepare("SELECT x FROM temp.t")
.unwrap_err()
.to_string();
assert!(
err.contains("no such table"),
"expected no such table error, got: {err}"
);
}
#[test]
fn test_reprepare_after_temp_store_reset_does_not_panic() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("main.db");
let conn = open_connection(&db_path);
conn.execute("CREATE TEMP TABLE t(x INTEGER)").unwrap();
let mut stmt = conn.prepare("SELECT x FROM t").unwrap();
conn.execute("PRAGMA temp_store = MEMORY").unwrap();
let err = stmt.step().unwrap_err().to_string();
assert!(
err.contains("no such table"),
"expected no such table after temp reset, got: {err}"
);
}
#[test]
fn test_temp_trigger_abort_rolls_back_temp_writes_without_panicking() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("main.db");
let conn = open_connection(&db_path);
conn.execute("CREATE TEMP TABLE t(x INTEGER)").unwrap();
conn.execute("CREATE TEMP TABLE u(y INTEGER)").unwrap();
conn.execute(
"CREATE TRIGGER tr BEFORE INSERT ON temp.t BEGIN \
INSERT INTO u VALUES (NEW.x); \
SELECT RAISE(ABORT, 'boom'); \
END;",
)
.unwrap();
let err = conn.execute("INSERT INTO temp.t VALUES(1)").unwrap_err();
assert!(
err.to_string().contains("boom"),
"expected trigger abort error, got: {err}"
);
assert_eq!(query_single_i64(&conn, "SELECT COUNT(*) FROM temp.u"), 0);
assert_eq!(query_single_i64(&conn, "SELECT COUNT(*) FROM temp.t"), 0);
}
#[test]
fn test_temp_trigger_abort_rolls_back_main_and_temp_writes() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("main.db");
let conn = open_connection(&db_path);
conn.execute("CREATE TABLE m(x INTEGER)").unwrap();
conn.execute("CREATE TEMP TABLE t(x INTEGER)").unwrap();
conn.execute("CREATE TEMP TABLE u(y INTEGER)").unwrap();
conn.execute(
"CREATE TRIGGER tr BEFORE INSERT ON temp.t BEGIN \
INSERT INTO m VALUES (NEW.x); \
INSERT INTO u VALUES (NEW.x); \
SELECT RAISE(ABORT, 'boom'); \
END;",
)
.unwrap();
let err = conn.execute("INSERT INTO temp.t VALUES(1)").unwrap_err();
assert!(
err.to_string().contains("boom"),
"expected trigger abort error, got: {err}"
);
assert_eq!(query_single_i64(&conn, "SELECT COUNT(*) FROM main.m"), 0);
assert_eq!(query_single_i64(&conn, "SELECT COUNT(*) FROM temp.u"), 0);
assert_eq!(query_single_i64(&conn, "SELECT COUNT(*) FROM temp.t"), 0);
}
#[test]
fn test_distinct_triggers_with_same_name_in_different_schemas_can_fire_nested() {
let temp_dir = TempDir::new().unwrap();
let db_path = temp_dir.path().join("main.db");
let conn = open_connection(&db_path);
conn.execute("CREATE TABLE src(x INTEGER)").unwrap();
conn.execute("CREATE TABLE dst(y INTEGER)").unwrap();
conn.execute("CREATE TABLE audit(z INTEGER)").unwrap();
conn.execute(
"CREATE TRIGGER shared_name AFTER INSERT ON dst BEGIN \
INSERT INTO audit VALUES (NEW.y); \
END;",
)
.unwrap();
conn.execute(
"CREATE TEMP TRIGGER shared_name AFTER INSERT ON main.src BEGIN \
INSERT INTO dst VALUES (NEW.x); \
END;",
)
.unwrap();
conn.execute("INSERT INTO src VALUES(7)").unwrap();
assert_eq!(query_single_i64(&conn, "SELECT COUNT(*) FROM main.dst"), 1);
assert_eq!(query_single_i64(&conn, "SELECT SUM(z) FROM main.audit"), 7);
}
/// A committed `setval(X, false)` stores an unconsumed sequence value.
/// After sequence initialization reloads persisted state, the in-memory
/// sequence must still represent that value as unconsumed, so the next
/// `nextval()` returns `X` rather than advancing past it.
/// Disk-only sequence design: setval(value, is_called=false) must be
/// observable as the next nextval() result. Previously this exercised
/// the in-memory-atomic reseeding path; that path no longer exists,
/// but the user-visible contract still holds because every nextval
/// reads the backing-table watermark and applies is_called semantics
/// in op_sequence_compute_next.
#[test]
fn test_setval_uncalled_emits_stored_value_as_next() -> Result<()> {
let temp_dir = TempDir::new().unwrap();
let path = temp_dir.path().join("seq_init.db");
let conn = open_connection_with_opts(&path, DatabaseOpts::new());
conn.execute("PRAGMA journal_mode = 'mvcc'").unwrap();
conn.execute("CREATE SEQUENCE s START 1 INCREMENT 3")?;
conn.execute("SELECT setval('s', 13, 0)")?;
let next_val = query_single_i64(&conn, "SELECT nextval('s')");
assert_eq!(
next_val, 13,
"setval(13, false) committed: next nextval must return 13"
);
Ok(())
}
}