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
//! The `Table` storage object: row insert/update/delete, index
//! construction + rebuild (BTree / BRIN / GIN / GIN-trgm /
//! GIN-fulltext / NSW), cold-locator registration, and schema
//! mutation (add/drop/rename column). Split out of lib.rs (monster
//! tier-3 cut 4). The `Table` struct itself stays in lib.rs as
//! storage vocabulary; this module is the inherent `impl` over it.
//! `Table`'s private fields are reachable here because `table` is a
//! descendant module of the crate root where the struct is declared.
use super::*;
impl Table {
pub fn new(schema: TableSchema) -> Self {
Self {
schema,
rel_id: crate::row_header::RelId::UNASSIGNED,
rows: PersistentVec::new(),
headers: PersistentVec::new(),
rowids: PersistentVec::new(),
next_rowid: 1,
dead_rows: 0,
stat_tup_ins: 0,
stat_tup_upd: 0,
stat_tup_del: 0,
scan_stats: crate::ScanStats::default(),
last_autovacuum_us: None,
last_analyze_us: None,
indices: Vec::new(),
hot_bytes: 0,
cold_row_count: 0,
cold_row_count_stale: false,
redo_log: None,
excl_indexes: Vec::new(),
prune_horizon: 0,
}
}
/// v7.37.15 (Phase C.1) — allocate the next stable [`RowId`] for
/// this relation. Monotonic, never reused. Callers push the
/// returned id onto `rowids` in lock-step with the `rows` /
/// `headers` append so `rowids[i]` names the row at slot `i`.
fn alloc_rowid(&mut self) -> crate::row_header::RowId {
let id = crate::row_header::RowId(self.next_rowid);
self.next_rowid += 1;
id
}
/// v7.37.15 (Phase C.1) — read-only access to the stable row ids
/// parallel to `rows()`. `rowids().len() == rows().len()` is the
/// load-bearing lock-step invariant (asserted in debug builds at
/// every mutation boundary alongside `headers`).
#[must_use]
pub fn rowids(&self) -> &PersistentVec<crate::row_header::RowId> {
&self.rowids
}
/// v7.37.15 (Phase C.1) — this relation's stable identity.
/// [`RelId::UNASSIGNED`](crate::row_header::RelId::UNASSIGNED) for
/// a bare `Table::new`; a real id once the catalog stamps it.
#[must_use]
pub fn rel_id(&self) -> crate::row_header::RelId {
self.rel_id
}
/// v7.37.15 (Phase C.1) — stamp this relation's stable identity.
/// Called by `Catalog::create_table` and the deserialize
/// dense-assign pass; idempotent overwrite.
pub(crate) fn set_rel_id(&mut self, id: crate::row_header::RelId) {
self.rel_id = id;
}
/// v7.37.15 (Phase C.1) — rebuild the `rowids` vec so it is dense
/// `1..=rows.len()` and reset the allocator above it. Used on the
/// load / snapshot-restore path where rows arrive without ids
/// (pre-V6 envelope): every row gets a fresh id, sufficient while
/// ids are process-local bookkeeping. Keeps the lock-step
/// invariant against the freshly-loaded `rows`.
pub fn assign_dense_rowids(&mut self) {
let n = self.rows.len();
let mut fresh: PersistentVec<crate::row_header::RowId> = PersistentVec::new();
for i in 0..n {
fresh.push_mut(crate::row_header::RowId((i + 1) as u64));
}
self.rowids = fresh;
self.next_rowid = (n as u64) + 1;
debug_assert_eq!(
self.rows.len(),
self.rowids.len(),
"rowids must stay in lock-step with rows after assign_dense_rowids"
);
}
/// v7.37.16 (autovacuum) — number of tombstoned-but-present hot rows.
/// Incrementally maintained; drives the engine's autovacuum threshold.
#[must_use]
pub fn dead_rows(&self) -> u64 {
self.dead_rows
}
/// v7.37.16 (autovacuum) — loader-side rebase of the dead-row
/// counter (the v53 MVCC appendix restores headers verbatim).
pub(crate) fn set_dead_rows_on_load(&mut self, dead: u64) {
self.dead_rows = dead;
}
/// v7.39 (pg_stat knife A) — bump the volatile write counters the
/// engine's DML dispatcher reports per statement.
pub fn bump_write_stats(&mut self, ins: u64, upd: u64, del: u64) {
self.stat_tup_ins = self.stat_tup_ins.saturating_add(ins);
self.stat_tup_upd = self.stat_tup_upd.saturating_add(upd);
self.stat_tup_del = self.stat_tup_del.saturating_add(del);
}
/// `(n_tup_ins, n_tup_upd, n_tup_del)` for pg_stat_user_tables.
#[must_use]
pub fn write_stats(&self) -> (u64, u64, u64) {
(self.stat_tup_ins, self.stat_tup_upd, self.stat_tup_del)
}
/// v7.39 (pg_stat knife C) — maintenance stamps for
/// pg_stat_user_tables (`(last_autovacuum_us, last_analyze_us)`).
#[must_use]
pub fn maintenance_stamps(&self) -> (Option<i64>, Option<i64>) {
(self.last_autovacuum_us, self.last_analyze_us)
}
pub fn stamp_autovacuum(&mut self, unix_us: i64) {
self.last_autovacuum_us = Some(unix_us);
}
pub fn stamp_analyze(&mut self, unix_us: i64) {
self.last_analyze_us = Some(unix_us);
}
/// v7.39 (pg_stat knife B) — the scan counters (read side of
/// pg_stat_user_tables).
#[must_use]
pub fn scan_stats(&self) -> &crate::ScanStats {
&self.scan_stats
}
/// v7.39 (pg_stat knife B) — one sequential scan over the visible
/// rows, reported by engine scan loops that walk headers directly
/// (parallel shards, the aggregate full scan) instead of
/// `scan_visible`.
pub fn note_seq_scan(&self) {
use core::sync::atomic::Ordering;
self.scan_stats.seq_scan.fetch_add(1, Ordering::Relaxed);
let visible = (self.rows.len() as u64).saturating_sub(self.dead_rows);
self.scan_stats
.seq_tup_read
.fetch_add(visible, Ordering::Relaxed);
}
/// v7.39 (pg_stat knife B) — one index scan returning `fetched`
/// rows (the engine's index-seek paths report here).
pub fn note_index_scan(&self, fetched: u64) {
use core::sync::atomic::Ordering;
self.scan_stats.idx_scan.fetch_add(1, Ordering::Relaxed);
self.scan_stats
.idx_tup_fetch
.fetch_add(fetched, Ordering::Relaxed);
}
/// v7.37.15 (Phase A.2) — read-only access to the per-row
/// MVCC visibility headers. `headers().len() == rows().len()`
/// is the load-bearing invariant; Phase B scan paths consult
/// `headers()[idx]` to decide visibility.
#[must_use]
pub fn headers(&self) -> &PersistentVec<crate::row_header::RowHeader> {
&self.headers
}
/// v7.37.15 (Phase B TDD) — `#[cfg(test)]`-only mutable header
/// access for tests that need to simulate Phase C semantics
/// (writer-side xmin/xmax stamping) before the real stamping
/// API lands. Phase C will provide a writer-aware setter that
/// keeps headers + xact bookkeeping consistent.
#[cfg(test)]
pub(crate) fn headers_mut_for_test(
&mut self,
) -> &mut PersistentVec<crate::row_header::RowHeader> {
&mut self.headers
}
/// v7.37.16 (Epic W) — `#[cfg(test)]`-only read of the relation's
/// next-RowId allocator cursor, so the snapshot round-trip tests can
/// assert it is restored correctly (strictly above every persisted
/// id) without a public accessor on the hot path.
#[cfg(test)]
pub(crate) fn next_rowid_for_test(&self) -> u64 {
self.next_rowid
}
/// v7.37.15 (Phase C) — engine writer path. Same as [`insert`]
/// but stamps `xmin` on the new row's header with the writing
/// transaction's id (caller-supplied; obtained from the engine's
/// monotonic version counter). The fresh insert is alive
/// (`xmax = XMAX_ALIVE`); a later UPDATE / DELETE will set
/// `xmax` to a later version, leaving the row physically
/// present until vacuum reclaims it (Phase D).
///
/// Callers in [`crate::row_header::next_version`] order:
/// 1. allocate version V via `next_version()`
/// 2. call `insert_with_xmin(row, V)`
/// 3. update any indexes (as `insert` does)
///
/// `xmin = XMIN_FROZEN` short-circuits to plain `insert`
/// behaviour so the legacy in-memory / WAL-replay paths keep
/// returning identical results when they end up here.
pub fn insert_with_xmin(&mut self, row: Row<'static>, xmin: u64) -> Result<(), StorageError> {
if xmin == crate::row_header::XMIN_FROZEN {
return self.insert(row);
}
self.insert(row)?;
// Insert appended `RowHeader::frozen()`; overwrite with the
// alive-xmin header so visibility scans against snapshots
// taken before the writer's commit hide this row. Subsequent
// commit is recorded by the WAL; replay re-applies via the
// plain `insert_no_index` path and stamps frozen — but a
// snapshot taken AFTER commit sees `xmin = V <= snapshot.version`
// and the in_progress bitset no longer contains V, so the
// row passes the visibility predicate identically.
let last = self
.headers
.len()
.checked_sub(1)
.expect("insert appended a header");
if let Some(new_headers) = self
.headers
.set(last, crate::row_header::RowHeader::alive(xmin))
{
self.headers = new_headers;
}
debug_assert_eq!(
self.rows.len(),
self.headers.len(),
"headers must stay in lock-step with rows after insert_with_xmin"
);
Ok(())
}
/// v7.39 (round 493) — publish the snapshot floor the insert path may
/// prune dead index entries under. See `prune_horizon`.
///
/// The engine sets this from `vacuum_oldest_active()` before a
/// statement's inserts. `0` disables pruning, which is the default and
/// is always safe.
pub fn set_prune_horizon(&mut self, horizon: u64) {
self.prune_horizon = horizon;
}
/// v7.37.15 (Phase D) — single-table vacuum pass. Walks the
/// header vec and physically removes any row whose delete
/// commit is older than `oldest_active_snapshot`. Returns the
/// number of reclaimable rows (with `dry_run == true`) or the
/// number actually reclaimed.
///
/// `oldest_active_snapshot` is the floor of every live
/// snapshot's `version` — the engine maintains this; hosts
/// pass it through.
///
/// Phase D ships the storage primitive. Hosts (spg-embedded /
/// spg-server) schedule the pass on their own thread.
pub fn vacuum(
&mut self,
oldest_active_snapshot: u64,
dry_run: bool,
) -> crate::vacuum::VacuumReport {
let examined = self.headers.len() as u64;
// Collect the reclaimable positions in a first pass so the
// mutation can rebuild both the rows and the headers vec
// together (their lock-step invariant survives).
let to_reclaim: alloc::vec::Vec<usize> = (0..self.headers.len())
.filter(|&i| {
self.headers
.get(i)
.map(|h| crate::vacuum::is_reclaimable(h.xmax, oldest_active_snapshot))
.unwrap_or(false)
})
.collect();
if to_reclaim.is_empty() || dry_run {
return crate::vacuum::VacuumReport {
rows_reclaimed: to_reclaim.len() as u64,
rows_examined: examined,
per_table: alloc::vec::Vec::new(),
};
}
// Drive the existing per-position delete path so both rows
// and headers shrink together (it's the only mutator that
// already maintains the lock-step invariant).
let removed = self.delete_rows_no_index(&to_reclaim);
self.rebuild_indices();
crate::vacuum::VacuumReport {
rows_reclaimed: removed as u64,
rows_examined: examined,
per_table: alloc::vec::Vec::new(),
}
}
/// v7.37.15 (Phase C) — mark the row at `position` as deleted
/// by version `xmax`. The row stays physically present; later
/// vacuum (Phase D) reclaims it once no live snapshot can
/// still see it.
///
/// Returns `Err(Corrupt)` on out-of-bounds and silently no-ops
/// when the row is already tombstoned (a later DELETE on an
/// already-deleted row should not change xmax — the original
/// deletion wins).
pub fn mark_row_deleted(&mut self, position: usize, xmax: u64) -> Result<(), StorageError> {
if position >= self.headers.len() {
return Err(StorageError::Corrupt(alloc::format!(
"mark_row_deleted: position {position} out of bounds (headers={})",
self.headers.len()
)));
}
let mut h = *self.headers.get(position).expect("position bounds-checked");
if h.xmax != crate::row_header::XMAX_ALIVE {
// Already tombstoned by an earlier delete. Keep the
// original xmax — first-deleter-wins.
return Ok(());
}
h.xmax = xmax;
if let Some(new_headers) = self.headers.set(position, h) {
self.headers = new_headers;
}
self.dead_rows += 1;
// v7.37.15 (Epic W durable-tombstone slice) — capture the
// in-place tombstone as row-level redo so a gate-on
// (`SPG_MVCC_INPLACE`) DELETE / UPDATE-old-version /
// ON-CONFLICT survives crash recovery. Unlike `delete_rows`
// (which records `RowChange::Delete` with physical positions),
// the tombstone keeps the slot, so it is named by the row's
// stable `RowId` — read from `self.rowids()[position]` here,
// before any later compaction shifts the slot. `xmax` is the
// deleting statement's writer version (the engine passes
// `writer_version_for_current_stmt`), so no post-drain stamp is
// needed. Only paid for when redo capture is on; a no-op
// (already-tombstoned / out-of-bounds) returned above and
// records nothing.
if self.redo_log.is_some() {
let rowid = self
.rowids()
.get(position)
.copied()
.unwrap_or(crate::row_header::RowId::UNASSIGNED);
self.record_redo(move |table| RowChange::Tombstone {
table,
rowids: alloc::vec![rowid],
xmax,
});
}
Ok(())
}
/// v7.37.16 — batch form of [`Table::mark_row_deleted`]: stamp `xmax`
/// on every alive, in-bounds position and record ONE
/// `RowChange::Tombstone` carrying all affected `RowId`s (the codec
/// and replay already handle multi-rowid records). The per-row form
/// paid one redo record — a Vec alloc plus a log push — PER ROW,
/// ~800 ns/row on a 10k-row gate-on DELETE (heavy_write del_10k).
/// Semantics match the single-row form: already-tombstoned keeps its
/// original xmax (first-deleter-wins), out-of-bounds is skipped.
/// Returns the number of rows NEWLY tombstoned.
pub fn mark_rows_deleted(&mut self, positions: &[usize], xmax: u64) -> usize {
let mut rowids: alloc::vec::Vec<crate::row_header::RowId> = alloc::vec::Vec::new();
let capture = self.redo_log.is_some();
let mut newly = 0usize;
for &position in positions {
// v7.37.16 — `get_mut` (transient in-place edit when the
// headers trie is uniquely owned) instead of the `set`
// path-copy: a 10k-row tombstone pass was spending ~3 ms in
// per-row spine copies.
match self.headers.get_mut(position) {
Some(h) if h.xmax == crate::row_header::XMAX_ALIVE => {
h.xmax = xmax;
}
_ => continue, // out-of-bounds or already tombstoned
}
self.dead_rows += 1;
newly += 1;
if capture {
rowids.push(
self.rowids()
.get(position)
.copied()
.unwrap_or(crate::row_header::RowId::UNASSIGNED),
);
}
}
if capture && !rowids.is_empty() {
self.record_redo(move |table| RowChange::Tombstone {
table,
rowids,
xmax,
});
}
newly
}
/// v7.37.17 (Phase E RC rebase) — extract the write-set one writer
/// version left on this table, expressed against stable [`RowId`]s
/// so it can be replayed onto a FRESHER catalog clone whose
/// physical slots differ. `inserted` carries INSERT rows and the
/// new versions of UPDATEs (`xmin == v`); `tombstoned` carries the
/// ids DELETE / UPDATE-old-version stamped (`xmax == v`). A row
/// both inserted and tombstoned by the same version appears in
/// both lists; replay applies inserts first, tombstones second —
/// net effect identical.
#[must_use]
pub fn extract_tx_writeset(&self, v: u64) -> crate::TxWriteSet {
let mut inserted: alloc::vec::Vec<(crate::row_header::RowId, Row<'static>)> =
alloc::vec::Vec::new();
let mut tombstoned: alloc::vec::Vec<crate::row_header::RowId> = alloc::vec::Vec::new();
for (i, h) in self.headers.iter().enumerate() {
let rid = self
.rowids
.get(i)
.copied()
.unwrap_or(crate::row_header::RowId::UNASSIGNED);
if h.xmin == v
&& let Some(row) = self.rows.get(i)
{
inserted.push((rid, row.clone()));
}
if h.xmax == v {
tombstoned.push(rid);
}
}
crate::TxWriteSet {
inserted,
tombstoned,
}
}
/// v7.37.17 (Phase E4 fix) — read-only conflict probe for a
/// write-set's tombstones against THIS (fresher) relation: a target
/// RowId that is gone, or already tombstoned by a DIFFERENT
/// version, is a write-write conflict. Callers use this BEFORE
/// `replay_tx_writeset` so a conflicting UPDATE can drop its
/// paired insert too (atomicity of tombstone+insert pairs).
#[must_use]
pub fn tombstone_conflicts(
&self,
rids: &[crate::row_header::RowId],
v: u64,
) -> alloc::vec::Vec<crate::row_header::RowId> {
rids.iter()
.filter(
|rid| match (0..self.rowids.len()).find(|&i| self.rowids.get(i) == Some(rid)) {
Some(i) => self
.headers
.get(i)
.is_some_and(|h| h.xmax != crate::row_header::XMAX_ALIVE && h.xmax != v),
None => true,
},
)
.copied()
.collect()
}
/// v7.37.17 (Phase E RC rebase) — replay a write-set extracted from
/// an OLDER clone of this relation onto this (fresher) one, keeping
/// the original RowIds. Deliberately does NOT capture redo: a
/// replay re-expresses writes the transaction already made, it is
/// not a new mutation (the redo story rides the eventual COMMIT).
/// Returns the ids whose tombstone could not be applied because the
/// row is gone or already tombstoned by a DIFFERENT version — the
/// write-write conflict surface (RC skips them per PG semantics;
/// RR/SER turn them into serialization_failure — Phase E3).
pub fn replay_tx_writeset(
&mut self,
ws: &crate::TxWriteSet,
v: u64,
) -> alloc::vec::Vec<crate::row_header::RowId> {
for (rid, row) in &ws.inserted {
// Full insert (validation + index maintenance + fresh
// header/rowid), then re-stamp the header's xmin and put
// the ORIGINAL RowId back. The allocator id the insert
// burned is simply never used — ids are never recycled, so
// a gap is harmless. Insert can only fail on schema
// mismatch, impossible for a row this same relation
// already accepted; a debug_assert documents that.
let res = self.insert(row.clone());
debug_assert!(res.is_ok(), "writeset replay re-inserts a validated row");
if res.is_err() {
continue;
}
let last = self.rows.len() - 1;
if let Some(h) = self.headers.get_mut(last) {
h.xmin = v;
}
if let Some(slot) = self.rowids.get_mut(last) {
*slot = *rid;
}
}
let mut conflicts: alloc::vec::Vec<crate::row_header::RowId> = alloc::vec::Vec::new();
for rid in &ws.tombstoned {
let pos = (0..self.rowids.len()).find(|&i| self.rowids.get(i) == Some(rid));
match pos {
Some(i) => match self.headers.get_mut(i) {
Some(h) if h.xmax == crate::row_header::XMAX_ALIVE => {
h.xmax = v;
self.dead_rows += 1;
}
Some(h) if h.xmax == v => {} // already ours (idempotent)
_ => conflicts.push(*rid),
},
None => conflicts.push(*rid),
}
}
conflicts
}
/// v7.34 (crash-recovery P0 #2) — start capturing row-level redo into
/// this table (engine call before a mutating statement when
/// persistence is on). Idempotent; existing captured changes are kept.
pub fn enable_redo(&mut self) {
if self.redo_log.is_none() {
self.redo_log = Some(Vec::new());
}
}
/// v7.34 — drain the captured redo changes and stop capturing.
/// Returns the physical [`RowChange`]s applied since `enable_redo`,
/// in apply order (empty when capture was off or nothing changed).
pub fn take_redo(&mut self) -> Vec<RowChange> {
self.redo_log.take().unwrap_or_default()
}
/// Record one captured change when redo capture is on. The table name
/// rides on the change (taken from the schema) so a drained log is
/// self-describing against the whole catalog.
fn record_redo(&mut self, make: impl FnOnce(String) -> RowChange) {
if self.redo_log.is_some() {
let change = make(self.schema.name.clone());
if let Some(log) = self.redo_log.as_mut() {
log.push(change);
}
}
}
/// Total encoded byte size of every row currently in the hot tier
/// (`self.rows`). See struct docs for the maintenance contract.
/// Returns 0 for an empty table.
#[must_use]
pub const fn hot_bytes(&self) -> u64 {
self.hot_bytes
}
/// v6.7.0 — cached count of cold-tier rows. See struct field
/// docs for the staleness contract.
#[must_use]
pub const fn cold_row_count(&self) -> u64 {
self.cold_row_count
}
/// v6.7.0 — overwrite the cached count. Called by the engine's
/// `analyze_one_table` after walking the indices.
pub fn set_cold_row_count(&mut self, n: u64) {
self.cold_row_count = n;
self.cold_row_count_stale = false;
}
/// v6.7.0 — mark the cached count as potentially out of date.
/// Called by freezer / promote / DELETE paths so a subsequent
/// `spg_statistic` read knows the number may not reflect the
/// current state.
pub fn mark_cold_row_count_stale(&mut self) {
self.cold_row_count_stale = true;
}
/// v6.7.0 — report whether the cached count is known to be out
/// of date. Exposed for completeness; the virtual table surface
/// returns the cached value regardless.
#[must_use]
pub const fn cold_row_count_stale(&self) -> bool {
self.cold_row_count_stale
}
/// v7.36 — O(1) "could this table possibly have cold rows?"
/// predicate, intended for perf-critical executor hot paths
/// that just need to skip the cold-tier branch when there's
/// definitely nothing there. Reads the cached `cold_row_count`:
/// - cache fresh + cache == 0 → return false (fast path)
/// - cache stale → return true (conservative; the executor
/// pays the cold-aware path's `iter_cold_rows_*` cost but
/// stays correct)
/// - cache fresh + cache > 0 → return true
/// `count_cold_locators` remains the right call for the EXACT
/// count (ANALYZE etc.) — its O(N) walk is unsuitable per join
/// stage.
#[must_use]
pub const fn has_cold_rows_fast(&self) -> bool {
self.cold_row_count_stale || self.cold_row_count > 0
}
/// r944 — every BTree index a cold row could have been filed under.
///
/// The freeze writes a row's locator into exactly ONE index
/// (`register_cold_locators` takes a single index name) and the
/// freezer picks that index by its own rule, so a reader that guesses
/// a different one finds nothing. Round 943 is that bug: the freezer
/// chose the first BTree index over any integer column, the scan
/// looked at the first index on the primary key's column, and 15
/// frozen rows of 40 vanished from a plain `SELECT`.
///
/// Union over all of them rather than guessing one. Because each
/// row's locator exists in exactly one index, the union yields every
/// row once and needs no visited-set.
///
/// Deliberately NOT filtered to declared-unique indices. Freezing
/// through an index whose keys repeat is a real limitation —
/// `resolve_cold_locator` resolves BY KEY and cannot say which of two
/// rows sharing one was meant — but that limit belongs to the freeze,
/// which builds the segment keyed that way. Filtering it here only
/// hides rows that were frozen anyway, which is the bug rather than a
/// guard against it; the freezer's own tests freeze tables whose
/// integer index carries no uniqueness constraint.
pub fn cold_capable_indices(&self) -> impl Iterator<Item = &Index> {
self.indices
.iter()
.filter(|i| matches!(i.kind, IndexKind::BTree(_)))
}
/// v6.7.0 — walk every BTree index and count `RowLocator::Cold`
/// entries; return the MAX across indices. The freeze path
/// (`freeze_oldest_to_cold`) writes cold locators to ONE
/// designated index — that index ends up with the full per-row
/// count. MAX-across-indices yields the precise count when a
/// PK-style index exists; for multi-index tables without a
/// covering index it's a lower bound (rare in practice).
/// Caller responsibility: only invoke under `engine.write()`
/// or after taking ownership; the walk is O(N) over every
/// (key, locator) pair.
#[must_use]
pub fn count_cold_locators(&self) -> u64 {
let mut best: u64 = 0;
for idx in &self.indices {
if let IndexKind::BTree(map) = &idx.kind {
let n: u64 = map
.iter()
.map(|(_, locs)| locs.iter().filter(|l| l.is_cold()).count() as u64)
.sum();
if n > best {
best = n;
}
}
}
best
}
pub const fn schema(&self) -> &TableSchema {
&self.schema
}
/// v6.7.2 — mutable schema accessor for ALTER TABLE paths.
/// Used by `Engine::exec_alter_table` to flip per-table
/// settings like `hot_tier_bytes`.
pub const fn schema_mut(&mut self) -> &mut TableSchema {
&mut self.schema
}
/// v4.39: returns the persistent row vector by reference. Callers that
/// used to take `&[Row]` should switch to `.iter()` (via
/// `IntoIterator for &PersistentVec`) or `.get(i)` for indexing.
pub const fn rows(&self) -> &PersistentVec<Row<'static>> {
&self.rows
}
pub const fn row_count(&self) -> usize {
self.rows.len()
}
/// v7.37.15 (Phase B) — answer "is row at `idx` visible under
/// `snapshot`?" without exposing the header internals to the
/// engine. Callers in scan paths consult this BEFORE yielding
/// the row.
///
/// Defensive: out-of-bounds `idx` and the (impossible, asserted)
/// length mismatch return `false`, mirroring "row is not there
/// so it's not visible." Production scans never see either.
///
/// Phase A always returns `true` because every header is
/// `RowHeader::frozen()` and `Snapshot::unbounded()` accepts
/// every header. The full visibility behaviour engages once
/// Phase C writers start stamping real `xmin`/`xmax`.
#[must_use]
pub fn is_row_visible(&self, idx: usize, snapshot: &crate::snapshot::Snapshot) -> bool {
match self.headers.get(idx) {
Some(h) => self.header_visible(idx, h, snapshot),
None => false,
}
}
/// v7.39 (round 486) — the visibility decision once the header is
/// already in hand. `scan_visible` walks rows and headers in
/// lockstep, so it has the header without paying a second trie
/// descent to look it up by index.
fn header_visible(
&self,
idx: usize,
h: &crate::row_header::RowHeader,
snapshot: &crate::snapshot::Snapshot,
) -> bool {
// v7.39 (round 297, E3 Phase 1b) — `SKIP LOCKED` rides here so
// that every row source honours it; see `Snapshot::locked_out`.
if let Some((rel, set)) = &snapshot.locked_out
&& *rel == self.rel_id
&& set.contains(&idx)
{
return false;
}
snapshot.visible(h)
}
/// v7.37.15 (Phase D) — true iff every row in this table is
/// known-all-visible to every snapshot (frozen xmin + alive
/// xmax). When true, `scan_visible` skips the per-row check
/// entirely — the scan degenerates to a plain `rows().iter()`.
///
/// Maintained lazily: any insert/update that stamps a non-
/// frozen xmin / xmax clears the cached flag; the next call to
/// this method recomputes by walking the header vec. The walk
/// is O(n) in the rare case (only when an MVCC writer ran on
/// this table); steady-state legacy workloads hit the cached
/// `true` and scan at pre-v7.37.15 speed.
///
/// Phase D wires this into the engine's hot-tier scan
/// optimisation; the bit also serves the per-segment all-
/// visible bitmap (each cold segment is a separately tracked
/// `all_visible` bit, but cold segments are frozen wholesale
/// so they're trivially `true`).
#[must_use]
pub fn is_all_visible(&self) -> bool {
// Compute on the fly. Caching is a follow-up optimisation
// (would require &mut self or a Cell); the v7.37.15
// initial ship favours correctness + simplicity over the
// amortised constant.
self.headers
.iter()
.all(crate::row_header::RowHeader::is_all_visible_fast)
}
/// v7.37.15 (Phase B / D) — iterate over `(idx, row)` pairs whose
/// header is visible under `snapshot`. This is the engine-side
/// drop-in replacement for `for (i, r) in t.rows().iter().enumerate()`
/// at scan sites. The check is a single branch + atomic
/// register read inside the snapshot path; with `Snapshot::unbounded`
/// the optimiser folds the gate away.
///
/// `'a` lifetime on `snapshot` keeps the helper zero-cost in
/// the hot loop — no Arc bump, no allocation.
/// v7.39 (round 560) — is the row at this position visible to the
/// snapshot? Exposed so an index-only walk can decide without
/// fetching the row it is deciding about.
#[must_use]
pub fn position_visible(&self, idx: usize, snapshot: &crate::snapshot::Snapshot) -> bool {
self.headers
.get(idx)
.is_some_and(|h| self.header_visible(idx, h, snapshot))
}
/// v7.39 (round 562) — the same question asked many times over
/// ascending positions, without descending the header trie for each
/// one.
///
/// A profile of the server serving a 100k-row index-only range put
/// 27% of the connection thread's CPU on the per-row visibility test.
/// The headers are a `PersistentVec` — a 32-way trie — so
/// `position_visible` is four dependent pointer loads per row. A
/// sequential scan never pays that: it walks rows and headers in
/// lockstep. An index walk cannot, but its positions arrive in
/// ascending order and a leaf holds 32 of them, so keeping the run
/// between calls turns 32 descents into one.
///
/// A position outside the held run just descends, so an index whose
/// order is uncorrelated with position costs what it costs today.
#[must_use]
pub fn header_runs(&self) -> HeaderRuns<'_> {
HeaderRuns {
table: self,
run: None,
}
}
/// v7.39 (round 559) — how many rows a snapshot sees, without
/// touching a single one of them.
///
/// `count(*)` already short-circuits to `rows.len()` in the
/// aggregate layer, so the O(1) part was never the problem: the cost
/// is UPSTREAM, materialising every visible row so that layer can
/// take its length. `scan_visible` zips the row trie with the
/// headers, and a count needs only the headers.
///
/// Measured over pgwire on 500k rows, `SELECT count(*)`:
///
/// ```text
/// PG18 (2 parallel workers) 8.2 ms
/// PG18 (parallelism off) 10.3 ms
/// SPG 16.5 ms = 33 ns/row
/// ```
///
/// — 1.6x slower than a single-threaded PG on the commonest
/// aggregate there is, which no ledger entry recorded.
pub fn count_visible(&self, snapshot: &crate::snapshot::Snapshot) -> usize {
self.note_seq_scan();
self.headers
.iter()
.enumerate()
.filter(|(i, h)| self.header_visible(*i, h, snapshot))
.count()
}
pub fn scan_visible<'a, 'b>(
&'a self,
snapshot: &'b crate::snapshot::Snapshot,
) -> impl Iterator<Item = (usize, &'a Row<'static>)> + 'b
where
'a: 'b,
{
// v7.39 (pg_stat knife B) — one sequential scan; tup_read is
// the visible-row estimate (an early-terminating consumer —
// LIMIT — reads fewer; the lazy iterator can't report back).
// Two relaxed atomic adds per SCAN (not per row).
self.note_seq_scan();
// v7.39 (round 486) — headers ride alongside the rows instead of
// being looked up by index. `headers.len() == rows.len()` is an
// asserted invariant, so the zip drops nothing; an index lookup
// costs a trie descent per row, and the walk costs one per leaf.
self.rows
.iter()
.zip(self.headers.iter())
.enumerate()
.filter(move |(i, (_, h))| self.header_visible(*i, h, snapshot))
.map(|(i, (r, _))| (i, r))
}
/// The hot-tier slot a scan should resume at, given the last
/// [`RowId`](crate::row_header::RowId) it consumed and where that row
/// used to sit.
///
/// Slots move. `vacuum` reclaims tombstones by rebuilding the row
/// vector, so every position after the first reclaimed one shifts
/// down — a reader that remembered a bare index would silently skip
/// or repeat rows. Row ids do not move: they are allocated
/// monotonically and never reused, which makes them the only stable
/// way to say "carry on after this row".
///
/// `hint` is the position that row occupied when it was read. It is
/// still right whenever nothing was reclaimed under the reader, so
/// the check costs one lookup; the binary search is the fallback for
/// when it is not, and it works because appends only ever push
/// larger ids and reclaiming preserves their order.
pub fn resume_slot_after(&self, last: crate::row_header::RowId, hint: usize) -> usize {
if hint > 0 && self.rowids.get(hint - 1).is_some_and(|&r| r == last) {
return hint;
}
let (mut lo, mut hi) = (0usize, self.rowids.len());
while lo < hi {
let mid = lo + (hi - lo) / 2;
match self.rowids.get(mid) {
Some(&r) if r <= last => lo = mid + 1,
_ => hi = mid,
}
}
lo
}
/// The same visibility-gated walk as [`Table::scan_visible`], resuming
/// at hot-tier index `start`.
///
/// A server-side cursor hands out its result in batches and has to
/// continue where the previous batch stopped. Restarting the walk per
/// batch and discarding a growing prefix would make an N-batch drain
/// quadratic in the row count, so the resume point is a parameter
/// rather than something the caller skips over.
///
/// `start` is a hot-tier position, not a [`RowId`](crate::row_header::RowId):
/// callers that resume across a compaction must re-derive it, which is
/// why the cursor path only resumes tables with no cold segments.
///
/// `note_seq_scan` fires only for `start == 0`. One cursor drained in
/// 300 batches is one sequential scan of the table, and counting it
/// 300 times would misreport `pg_stat_user_tables.seq_scan`.
pub fn scan_visible_from<'a, 'b>(
&'a self,
start: usize,
snapshot: &'b crate::snapshot::Snapshot,
) -> impl Iterator<Item = (usize, &'a Row<'static>)> + 'b
where
'a: 'b,
{
if start == 0 {
self.note_seq_scan();
}
self.rows
.iter()
.zip(self.headers.iter())
.enumerate()
.skip(start)
.filter(move |(i, (_, h))| self.header_visible(*i, h, snapshot))
.map(|(i, (r, _))| (i, r))
}
/// v6.8.0 — exposed for the engine layer to patch
/// `Index::included_columns` post-creation. Could fold into
/// `add_index` once the engine's IF-NOT-EXISTS guard moves up,
/// but the patch shape is the minimal change for v6.8.0.
pub fn indices_mut(&mut self) -> &mut [Index] {
&mut self.indices
}
pub fn indices(&self) -> &[Index] {
&self.indices
}
/// Compute the next `AUTO_INCREMENT` value for the column at
/// `col_pos`. Defined as `max(existing) + 1`, falling back to `1`
/// when the column currently holds no integer values. NULL / non-
/// integer cells are skipped. Returns `None` when the column isn't
/// an integer type.
pub fn next_auto_value(&self, col_pos: usize) -> Option<i64> {
let ty = self.schema.columns.get(col_pos)?.ty;
if !matches!(ty, DataType::SmallInt | DataType::Int | DataType::BigInt) {
return None;
}
let mut max: Option<i64> = None;
for row in &self.rows {
match row.values.get(col_pos) {
Some(Value::SmallInt(n)) => {
let v = i64::from(*n);
max = Some(max.map_or(v, |m| m.max(v)));
}
Some(Value::Int(n)) => {
let v = i64::from(*n);
max = Some(max.map_or(v, |m| m.max(v)));
}
Some(Value::BigInt(n)) => {
max = Some(max.map_or(*n, |m| m.max(*n)));
}
_ => {}
}
}
// v7.39 (round 220) — `ALTER … ALTER COLUMN … RESTART [WITH n]`
// lifts the next allocated value to at least n (a floor over the
// max+1 scan). A dump-restore RESTART lands exactly on n; a
// backward RESTART is safely ignored (no duplicate-key landmine,
// unlike PG).
let base = max.map_or(1, |m| m + 1);
let floor = self
.schema
.columns
.get(col_pos)
.and_then(|c| c.auto_restart)
.unwrap_or(i64::MIN);
Some(base.max(floor))
}
/// Return the first index defined over `column_position`, if any.
/// (`v0.8` supports at most one index per column logically; the search
/// just picks the first match.)
pub fn index_on(&self, column_position: usize) -> Option<&Index> {
// v6.7.1 — prefer BTree (has the key→locator map needed
// for `lookup_eq`) over BRIN (metadata-only). When only a
// BRIN exists on the column, return None so the executor
// falls back to the hot-tier row scan instead of trying
// to use BRIN for an equality lookup (which would always
// return an empty slice and look like "no rows matched").
self.indices
.iter()
.find(|i| i.column_position == column_position && matches!(i.kind, IndexKind::BTree(_)))
.or_else(|| {
self.indices.iter().find(|i| {
i.column_position == column_position && matches!(i.kind, IndexKind::Nsw(_))
})
})
}
/// Insert one row after validating it matches the schema (length + type).
/// Returns `StorageError` on mismatch — the table is left unchanged.
/// Updates every defined index with the new row's key.
pub fn insert(&mut self, row: Row<'static>) -> Result<(), StorageError> {
if row.len() != self.schema.columns.len() {
return Err(StorageError::ArityMismatch {
expected: self.schema.columns.len(),
actual: row.len(),
});
}
for (i, (val, col)) in row.values.iter().zip(&self.schema.columns).enumerate() {
if val.is_null() {
if !col.nullable {
return Err(StorageError::NullInNotNull {
column: col.name.clone(),
});
}
continue;
}
// v7.39 (read01 round 54) — `data_type()` is None for the
// eval-only variants that carry no DataType (RegClass, Composite).
// They are NOT NULL, so `.expect("non-null")` PANICKED on them —
// materialising a CTE like `WITH w AS (SELECT 't'::regclass)` blew
// up the query with an "internal error". Report a clean type
// mismatch instead; the engine coerces these before they get here
// on every path that knows how.
let Some(actual) = val.data_type() else {
// An eval-only value (RegClass carries oid + name, Composite a
// field tuple) has no DataType in the storage lattice. It is
// NOT NULL, so the old `.expect("non-null")` PANICKED — which
// is how `WITH w AS (SELECT 't'::regclass)` blew up with an
// "internal error". Accept it: the value keeps its dual shape
// and downstream comparisons (RegClass vs BigInt oid) handle it.
continue;
};
// A Vector column needs the variant AND the dimension to
// agree, which the equality inside `column_accepts` already
// encodes because DataType::Vector carries the dim.
let compatible = column_accepts(actual, col.ty);
if !compatible {
return Err(StorageError::TypeMismatch {
column: col.name.clone(),
expected: col.ty,
actual,
position: i,
});
}
}
let new_row_idx = self.rows.len();
// v7.39 (round 493) — disjoint borrows: the BTree arm below reads
// headers to decide which of this key's locators are dead while
// holding `indices` mutably.
let horizon = self.prune_horizon;
let headers = &self.headers;
// Pre-validate before mutating: ensure indices receive an IndexKey.
// For NSW we defer the graph update to *after* the row is pushed
// so the kNN search can see it in `self.rows`.
for idx in &mut self.indices {
match &mut idx.kind {
IndexKind::BTree(map) => {
if let Some(key) = IndexKey::from_value(&row.values[idx.column_position]) {
// v4.40: PersistentBTreeMap has no in-place entry-or-default.
// Clone-then-insert keeps the same semantics — for typical
// unique-key schemas the Vec is 1-element so the clone is
// O(1). For dup-heavy columns it's O(M) per insert, traded
// for the structural-sharing win at clone time.
//
// v7.39 (round 558) — TAKE the list instead of cloning it.
// `insert_mut` returns the previous value by MOVE, so the
// O(M) copy the note above accepted is avoidable, and the
// retain below still gets the list in hand. What that
// trade cost, measured on a 50k table:
//
// UPDATE h SET v = 1 WHERE v <= 10000 (10k -> ONE key)
// v indexed 150.6 ms v unindexed 31.2 ms
// UPDATE h SET v = v + 1 WHERE v <= 10000 (distinct keys)
// v indexed 34.7 ms v unindexed 32.0 ms
//
// 11.9 µs/row when the new keys collide against 0.27 when
// they do not — 44x for the same row count, because the
// k-th insert under one key copied a k-element list. Under
// in-place MVCC an UPDATE appends a new row VERSION, so an
// ordinary `SET flag = 'done'` over a batch lands every
// one of them on the same key.
let mut entries =
map.insert_mut(key.clone(), Vec::new()).unwrap_or_default();
// v7.39 (round 493) — drop this key's dead versions while
// the list is already in hand.
//
// "The Vec is 1-element for unique-key schemas" is what
// churn breaks: a posting list carries one locator per row
// VERSION, so deleting and re-inserting the same id grows
// it without bound between vacuums. Round 492 counted 61
// locators under one PK by cycle 60, each costing the
// uniqueness probe a header lookup, and round 490 found the
// range seek walking the same versions.
//
// Vacuum already prunes them — by rebuilding every index,
// which is why it runs rarely enough for this to matter.
// Here the work is free: the list is cloned on this path
// anyway and is about to be written back.
//
// Safety is vacuum's own argument: `prune_horizon` is the
// floor of every live snapshot, so a version reclaimable
// under it is invisible to every reader that exists or can
// yet begin (a later snapshot's version is >= the floor).
// A horizon of 0 keeps everything.
// v7.39 (round 558) — AMORTISE it.
//
// The retain walks the whole list, so running it on
// every insert is O(M) per insert and O(n²) over a
// statement that puts n row versions under one key.
// Measured on a 50k table, 10k rows updated:
//
// retain every insert off
// SET v = 1 (dupes) 135.9 ms 11.7
// SET v = v+1 (distinct) 31.4 ms 13.4
//
// and the second line has no colliding key at all —
// the OTHER index (g, 100 distinct values over 50k
// rows) supplies lists long enough on its own. Every
// insert on every index was paying it.
//
// Pruning only when the list has DOUBLED keeps round
// 493's bound — the list stays within 2x its pruned
// size, so the seek still never walks an unbounded
// version chain — while the total work over n inserts
// becomes n + n/2 + n/4 + … = O(n). Skipping a prune
// can only delay reclamation; it never drops a live
// locator, so the safety argument in the note above is
// untouched.
if horizon > 0 && entries.len() > 1 && entries.len().is_power_of_two() {
entries.retain(|loc| match loc {
RowLocator::Hot(i) => headers.get(*i).is_none_or(|h| {
!crate::vacuum::is_reclaimable(h.xmax, horizon)
}),
RowLocator::Cold { .. } => true,
});
}
entries.push(RowLocator::Hot(new_row_idx));
map.insert_mut(key, entries);
}
}
IndexKind::Gin(map) => {
// v7.12.3 — extend posting list per lexeme word.
// NULL or non-TsVector cell → no-op (cell carries
// no lexemes to index).
if let Value::TsVector(lexemes) = &row.values[idx.column_position] {
for lex in lexemes {
if let Some(entries) = map.get_mut(&lex.word) {
entries.push(RowLocator::Hot(new_row_idx));
} else {
map.insert_mut(
lex.word.clone(),
alloc::vec![RowLocator::Hot(new_row_idx)],
);
}
}
}
}
IndexKind::GinTrgm(map) => {
// v7.15.0 — trigram GIN. Shingle the TEXT cell
// into PG-compatible 3-byte trigrams and extend
// each trigram's posting list.
if let Value::Text(s) = &row.values[idx.column_position] {
for tri in trgm::extract_trigrams(s) {
// r1019 — address the String-keyed map with the borrowed
// trigram; allocate one only for a key the map has never
// seen, which after the first rows is rare.
let key = trgm::trigram_str(&tri);
if let Some(entries) = map.get_mut_by(key) {
entries.push(RowLocator::Hot(new_row_idx));
} else {
map.insert_mut(
alloc::string::ToString::to_string(key),
alloc::vec![RowLocator::Hot(new_row_idx)],
);
}
}
}
}
IndexKind::GinFulltext(map) => {
// v7.17.0 Phase 2.2 — MySQL FULLTEXT-shape
// GIN over a TEXT / VARCHAR cell. Tokenise
// via the storage-local `simple_lex` (same
// rule as `to_tsvector('simple', text)`) and
// extend each lexeme's posting list.
let text_cell = match &row.values[idx.column_position] {
Value::Text(s) => Some(s.as_ref()),
// mysqldump-style mediumtext / longtext
// land as Value::Text on insert; varchar
// cells likewise. Anything else (NULL,
// integer, …) contributes no lexemes.
_ => None,
};
if let Some(s) = text_cell {
for lex in fts_simple::simple_lex(s) {
if let Some(entries) = map.get_mut(&lex) {
entries.push(RowLocator::Hot(new_row_idx));
} else {
map.insert_mut(lex, alloc::vec![RowLocator::Hot(new_row_idx)]);
}
}
}
}
IndexKind::GinJsonb(map) => {
// v7.37.8(sentori Epic 5 P2)— real JSONB-GIN.
// Extract canonical `(path, leaf)` tokens from
// the cell text and extend each token's posting
// list. NULL or non-Json cell contributes no
// tokens(`labels @> '...'` against a NULL row
// is always false so absence here is correct).
let json_cell = match &row.values[idx.column_position] {
Value::Json(s) => Some(s.as_ref()),
_ => None,
};
if let Some(s) = json_cell {
for tok in jsonb_gin::extract_tokens(s) {
if let Some(entries) = map.get_mut(&tok) {
entries.push(RowLocator::Hot(new_row_idx));
} else {
map.insert_mut(tok, alloc::vec![RowLocator::Hot(new_row_idx)]);
}
}
}
}
// NSW handled below after the row push (so the new row
// is visible to the kNN-graph connect step). BRIN
// carries no per-row state.
IndexKind::Nsw(_) | IndexKind::Brin { .. } => {}
}
}
// v7.39 (round 215) — maintain the range-exclusion indexes for the
// freshly-inserted row (before the move; `new_row_idx` is the slot it
// will occupy). Mirrors the BTree maintenance above.
if !self.excl_indexes.is_empty() {
self.excl_indexes_on_insert(&row, new_row_idx);
}
// v5.2.1: maintain incremental hot-tier byte counter. Computed
// before the move so we don't need to borrow `row` after push.
self.hot_bytes = self
.hot_bytes
.saturating_add(row_body_encoded_len(&row, &self.schema) as u64);
// v7.34 — capture the row-level redo before the row is moved in.
// v7.37.15 (Epic W slice 1) — carry the stable RowId this insert
// will receive. `alloc_rowid` below hands out `RowId(next_rowid)`
// and bumps the counter unconditionally, so the id read here is
// exactly the one the row ends up with. `writer_version` (xmin)
// is 0: the writing TxId is not threaded to this layer yet (the
// header pushed below is `RowHeader::frozen()`).
let redo_rowid = crate::row_header::RowId(self.next_rowid);
self.record_redo(|table| RowChange::Insert {
table,
row: row.clone(),
rowid: redo_rowid,
writer_version: 0,
});
// v4.39.1: push_mut keeps streaming inserts at Vec::push speed when
// the table is uniquely owned (the spg-embedded path); inside a TX
// wrap where a Catalog snapshot exists, push_mut path-copies the
// tail just like push() and the snapshot stays valid.
self.rows.push_mut(row);
// v7.37.15 (Phase A.2) — keep `headers` lock-step with `rows`.
// Phase A defaults every new insert to RowHeader::frozen() so
// visibility checks against any snapshot return true; Phase C
// upgrades the inserter to stamp the writing tx's xmin.
self.headers
.push_mut(crate::row_header::RowHeader::frozen());
// v7.37.15 (Phase C.1) — allocate + push the stable RowId in
// lock-step with rows/headers. Index locators still address
// by physical slot at this commit; the id is additive
// bookkeeping the lock table / HOT chains / WAL migrate to.
let rid = self.alloc_rowid();
self.rowids.push_mut(rid);
// v7.37.15 (Epic W slice 1) — the id captured for the redo log
// above must be the one actually assigned to the row.
debug_assert_eq!(
rid, redo_rowid,
"redo-captured RowId must match the allocated RowId"
);
debug_assert_eq!(
self.rows.len(),
self.headers.len(),
"headers must stay in lock-step with rows after insert"
);
debug_assert_eq!(
self.rows.len(),
self.rowids.len(),
"rowids must stay in lock-step with rows after insert"
);
// NSW updates after the push so the new row is visible to the
// greedy search used during connect.
let new_row_idx = self.rows.len() - 1;
let nsw_targets: Vec<usize> = self
.indices
.iter()
.enumerate()
.filter_map(|(i, idx)| {
if matches!(idx.kind, IndexKind::Nsw(_)) {
Some(i)
} else {
None
}
})
.collect();
for idx_pos in nsw_targets {
nsw_insert_at(self, idx_pos, new_row_idx);
}
Ok(())
}
/// Build a new B-tree index over the named column. Rebuilds from
/// existing rows. Errors if `column_name` doesn't exist or the index
/// name is taken.
pub fn add_index(&mut self, name: String, column_name: &str) -> Result<(), StorageError> {
if self.indices.iter().any(|i| i.name == name) {
return Err(StorageError::DuplicateIndex { name });
}
let column_position = self.schema.column_position(column_name).ok_or_else(|| {
StorageError::ColumnNotFound {
column: column_name.into(),
}
})?;
let mut idx = Index::new_btree(name, column_position);
if let IndexKind::BTree(map) = &mut idx.kind {
for (i, row) in self.rows.iter().enumerate() {
if let Some(key) = IndexKey::from_value(&row.values[column_position]) {
if let Some(entries) = map.get_mut(&key) {
entries.push(RowLocator::Hot(i));
} else {
map.insert_mut(key, alloc::vec![RowLocator::Hot(i)]);
}
}
}
}
self.indices.push(idx);
Ok(())
}
/// v7.39 (round 215) — ensure a range-exclusion index exists on
/// `column_position`, building it from the current rows. Idempotent: a
/// second call for the same column is a no-op. Called at CREATE TABLE /
/// ALTER ADD EXCLUDE and on catalog load (rebuild-from-constraints).
/// Tombstoned rows are indexed too (they are filtered by the consumer via
/// `is_deleted()` at query time — the established index pattern).
pub fn ensure_excl_range_index(&mut self, column_position: usize) {
if self
.excl_indexes
.iter()
.any(|e| e.column_position == column_position)
{
return;
}
let mut map: crate::PersistentBTreeMap<(i128, u8), Vec<RowLocator>> =
crate::PersistentBTreeMap::new();
for (i, row) in self.rows.iter().enumerate() {
if let Some(v) = row.values.get(column_position)
&& let Some(key) = crate::range_excl_index_key(v)
{
if let Some(entries) = map.get_mut(&key) {
entries.push(RowLocator::Hot(i));
} else {
map.insert_mut(key, alloc::vec![RowLocator::Hot(i)]);
}
}
}
self.excl_indexes.push(crate::ExclRangeIndex {
column_position,
map,
});
}
/// v7.39 (round 215) — the range-exclusion index on `column_position`, if
/// one was built. The EXCLUDE enforcement path probes its
/// [`predecessor`](crate::PersistentBTreeMap::predecessor) + successors to
/// find candidate overlaps in O(log n).
#[must_use]
pub fn excl_range_index(
&self,
column_position: usize,
) -> Option<&crate::PersistentBTreeMap<(i128, u8), Vec<RowLocator>>> {
self.excl_indexes
.iter()
.find(|e| e.column_position == column_position)
.map(|e| &e.map)
}
/// v7.39 (round 215) — add a freshly-appended row at `row_idx` to every
/// range-exclusion index. Called from `insert` after the row is pushed,
/// mirroring the BTree secondary-index maintenance.
fn excl_indexes_on_insert(&mut self, row: &Row<'static>, row_idx: usize) {
for ex in &mut self.excl_indexes {
if let Some(v) = row.values.get(ex.column_position)
&& let Some(key) = crate::range_excl_index_key(v)
{
if let Some(entries) = ex.map.get_mut(&key) {
entries.push(RowLocator::Hot(row_idx));
} else {
ex.map
.insert_mut(key, alloc::vec![RowLocator::Hot(row_idx)]);
}
}
}
}
/// v7.39 (round 215) — rebuild every range-exclusion index from the
/// current rows (called from `rebuild_indices`, i.e. after a physical
/// compaction/delete that shifted slots). Preserves which columns are
/// indexed; re-emits all `Hot` locators.
fn rebuild_excl_indexes(&mut self) {
let cols: Vec<usize> = self
.excl_indexes
.iter()
.map(|e| e.column_position)
.collect();
self.excl_indexes.clear();
for c in cols {
self.ensure_excl_range_index(c);
}
}
/// Build a new NSW (HNSW-flavoured) index over the named column.
/// Required for `ORDER BY col <-> literal LIMIT k` to plan as a
/// graph traversal instead of a full scan. Column must be a Vector
/// type. `m` is the maximum number of neighbours per node.
pub fn add_nsw_index(
&mut self,
name: String,
column_name: &str,
m: usize,
) -> Result<(), StorageError> {
self.add_nsw_index_inner(name, column_name, m, None)
}
/// v6.0.4 — synchronous rebuild of the named NSW index. If
/// `new_encoding` is `Some(target)` and differs from the column's
/// current encoding, every stored cell at the indexed column is
/// re-coded into the target encoding before the new graph
/// builds. Returns `IndexNotFound` if no index by that name exists
/// and `Unsupported` for non-NSW indexes (`BTree` REBUILD is a no-op
/// the engine layer rejects, not a storage-level concept).
///
/// Holds the caller's `&mut self` for the duration — no
/// concurrency / staging / WAL-replay machinery in v6.0.4. The
/// "live" optimisation lands as v6.0.4.1.
pub fn rebuild_nsw_index(
&mut self,
name: &str,
new_encoding: Option<VecEncoding>,
) -> Result<(), StorageError> {
let idx_pos = self
.indices
.iter()
.position(|i| i.name == name)
.ok_or_else(|| StorageError::IndexNotFound {
name: String::from(name),
})?;
let col_pos = self.indices[idx_pos].column_position;
let m = match &self.indices[idx_pos].kind {
IndexKind::Nsw(g) => g.m,
IndexKind::BTree(_)
| IndexKind::Brin { .. }
| IndexKind::Gin(_)
| IndexKind::GinTrgm(_)
| IndexKind::GinFulltext(_)
| IndexKind::GinJsonb(_) => {
return Err(StorageError::Unsupported(format!(
"ALTER INDEX REBUILD on non-NSW index {name:?} — only NSW indexes can rebuild"
)));
}
};
let col_name = self.schema.columns[col_pos].name.clone();
// 1. Optional re-encoding pass. Done first so the cells
// match the schema before the graph rebuild walks them.
if let Some(target) = new_encoding {
let current = match self.schema.columns[col_pos].ty {
DataType::Vector { encoding, .. } => encoding,
ref other => {
return Err(StorageError::Unsupported(format!(
"ALTER INDEX REBUILD WITH (encoding=…) on non-vector column type {other:?}"
)));
}
};
if target != current {
let DataType::Vector { dim, .. } = self.schema.columns[col_pos].ty else {
unreachable!("checked above")
};
let n = self.rows.len();
for i in 0..n {
let row = self
.rows
.get_mut(i)
.expect("row index in bounds (we iterated up to len())");
let cell = core::mem::replace(&mut row.values[col_pos], Value::Null);
let recoded = recode_vector_cell(cell, target)?;
row.values[col_pos] = recoded;
}
self.schema.columns[col_pos].ty = DataType::Vector {
dim,
encoding: target,
};
}
}
// 2. Drop the existing index slot + rebuild from row payload.
self.indices.remove(idx_pos);
self.add_nsw_index_inner(String::from(name), &col_name, m, None)?;
Ok(())
}
/// Restore an NSW index from a pre-built graph (used on
/// deserialize). Skips the bulk-build pass since the topology is
/// already known. Returns `DuplicateIndex` or `ColumnNotFound` on
/// schema mismatch as usual.
pub fn restore_nsw_index(
&mut self,
name: String,
column_name: &str,
graph: NswGraph,
) -> Result<(), StorageError> {
self.add_nsw_index_inner(name, column_name, graph.m, Some(graph))
}
/// Restore a `BTree` index from a pre-built `(IndexKey, Vec<RowLocator>)`
/// map. Used by [`Catalog::deserialize`] when reading a v9 (or later)
/// catalog snapshot — the map travels on disk so cold-tier locators
/// survive a round-trip, instead of being rebuilt from `self.rows`
/// (which would lose every Cold entry). Same error contract as
/// [`Table::add_index`].
pub fn restore_btree_index(
&mut self,
name: String,
column_name: &str,
map: PersistentBTreeMap<IndexKey, Vec<RowLocator>>,
) -> Result<(), StorageError> {
if self.indices.iter().any(|i| i.name == name) {
return Err(StorageError::DuplicateIndex { name });
}
let column_position = self.schema.column_position(column_name).ok_or_else(|| {
StorageError::ColumnNotFound {
column: column_name.into(),
}
})?;
self.indices.push(Index {
name,
column_position,
kind: IndexKind::BTree(map),
included_columns: Vec::new(),
partial_predicate: None,
expression: None,
is_unique: false,
nulls_not_distinct: false,
descending: false,
nulls_first: None,
collation: None,
extra_column_positions: Vec::new(),
});
Ok(())
}
/// v6.7.1 — public restore counterpart for BRIN indices. Used
/// by `Catalog::deserialize` when a v10 snapshot carries a
/// BRIN index entry. BRIN carries no in-memory data — only the
/// `column_type` snapshot is restored.
pub fn restore_brin_index(
&mut self,
name: String,
column_name: &str,
column_type: DataType,
) -> Result<(), StorageError> {
if self.indices.iter().any(|i| i.name == name) {
return Err(StorageError::DuplicateIndex { name });
}
let column_position = self.schema.column_position(column_name).ok_or_else(|| {
StorageError::ColumnNotFound {
column: column_name.into(),
}
})?;
self.indices
.push(Index::new_brin(name, column_position, column_type));
Ok(())
}
/// v6.7.1 — public CREATE INDEX counterpart for BRIN. Creates
/// the index entry with a snapshot of the indexed column's
/// current `DataType`.
pub fn add_brin_index(&mut self, name: String, column_name: &str) -> Result<(), StorageError> {
if self.indices.iter().any(|i| i.name == name) {
return Err(StorageError::DuplicateIndex { name });
}
let column_position = self.schema.column_position(column_name).ok_or_else(|| {
StorageError::ColumnNotFound {
column: column_name.into(),
}
})?;
let column_type = self.schema.columns[column_position].ty;
self.indices
.push(Index::new_brin(name, column_position, column_type));
Ok(())
}
/// v7.12.3 — Build a new GIN inverted index over a `tsvector`
/// column. Populates posting lists from existing rows. Errors
/// if the column doesn't exist, isn't `TsVector`, or the index
/// name is taken.
pub fn add_gin_index(&mut self, name: String, column_name: &str) -> Result<(), StorageError> {
if self.indices.iter().any(|i| i.name == name) {
return Err(StorageError::DuplicateIndex { name });
}
let column_position = self.schema.column_position(column_name).ok_or_else(|| {
StorageError::ColumnNotFound {
column: column_name.into(),
}
})?;
if self.schema.columns[column_position].ty != DataType::TsVector {
return Err(StorageError::Corrupt(format!(
"GIN index {name:?} requires a tsvector column; \
{column_name:?} is {:?}",
self.schema.columns[column_position].ty
)));
}
let mut idx = Index::new_gin(name, column_position);
if let IndexKind::Gin(map) = &mut idx.kind {
for (i, row) in self.rows.iter().enumerate() {
if let Value::TsVector(lexemes) = &row.values[column_position] {
for lex in lexemes {
if let Some(entries) = map.get_mut(&lex.word) {
entries.push(RowLocator::Hot(i));
} else {
map.insert_mut(lex.word.clone(), alloc::vec![RowLocator::Hot(i)]);
}
}
}
}
}
self.indices.push(idx);
Ok(())
}
/// v7.12.3 — Restore a GIN index from a deserialised snapshot.
/// Mirrors [`Self::restore_btree_index`] but takes the GIN's
/// `word → Vec<RowLocator>` posting-list map (already populated
/// from the catalog stream) instead of an `IndexKey` map.
pub fn restore_gin_index(
&mut self,
name: String,
column_name: &str,
map: PersistentBTreeMap<String, Vec<RowLocator>>,
) -> Result<(), StorageError> {
if self.indices.iter().any(|i| i.name == name) {
return Err(StorageError::DuplicateIndex { name });
}
let column_position = self.schema.column_position(column_name).ok_or_else(|| {
StorageError::ColumnNotFound {
column: column_name.into(),
}
})?;
let mut idx = Index::new_gin(name, column_position);
idx.kind = IndexKind::Gin(map);
self.indices.push(idx);
Ok(())
}
/// v7.15.0 — `gin_trgm_ops` GIN over a TEXT column. Walks
/// every row, shingles the cell into PG-compatible trigrams,
/// and builds the posting-list map. NULL / non-TEXT cells
/// contribute nothing (no trigrams).
pub fn add_gin_trgm_index(
&mut self,
name: String,
column_name: &str,
) -> Result<(), StorageError> {
if self.indices.iter().any(|i| i.name == name) {
return Err(StorageError::DuplicateIndex { name });
}
let column_position = self.schema.column_position(column_name).ok_or_else(|| {
StorageError::ColumnNotFound {
column: column_name.into(),
}
})?;
if !matches!(
self.schema.columns[column_position].ty,
DataType::Text | DataType::Varchar(_)
) {
return Err(StorageError::Corrupt(format!(
"trigram-GIN index {name:?} requires a TEXT/VARCHAR column; \
{column_name:?} is {:?}",
self.schema.columns[column_position].ty
)));
}
let mut idx = Index::new_gin_trgm(name, column_position);
if let IndexKind::GinTrgm(map) = &mut idx.kind {
for (i, row) in self.rows.iter().enumerate() {
if let Value::Text(s) = &row.values[column_position] {
for tri in trgm::extract_trigrams(s) {
// r1019 — address the String-keyed map with the borrowed
// trigram; allocate one only for a key the map has never
// seen, which after the first rows is rare.
let key = trgm::trigram_str(&tri);
if let Some(entries) = map.get_mut_by(key) {
entries.push(RowLocator::Hot(i));
} else {
map.insert_mut(
alloc::string::ToString::to_string(key),
alloc::vec![RowLocator::Hot(i)],
);
}
}
}
}
}
self.indices.push(idx);
Ok(())
}
/// v7.15.0 — restore a trigram-GIN from its catalog snapshot
/// payload. Mirrors [`Self::restore_gin_index`].
pub fn restore_gin_trgm_index(
&mut self,
name: String,
column_name: &str,
map: PersistentBTreeMap<String, Vec<RowLocator>>,
) -> Result<(), StorageError> {
if self.indices.iter().any(|i| i.name == name) {
return Err(StorageError::DuplicateIndex { name });
}
let column_position = self.schema.column_position(column_name).ok_or_else(|| {
StorageError::ColumnNotFound {
column: column_name.into(),
}
})?;
let mut idx = Index::new_gin_trgm(name, column_position);
idx.kind = IndexKind::GinTrgm(map);
self.indices.push(idx);
Ok(())
}
/// v7.17.0 Phase 2.2 — MySQL `FULLTEXT KEY` GIN over a TEXT
/// column. Walks every row, tokenises the cell into lower-
/// cased word lexemes (`fts_simple::simple_lex` — same rule
/// as `to_tsvector('simple', text)`), and builds the
/// posting-list map. NULL / non-TEXT cells contribute
/// nothing (no lexemes).
pub fn add_gin_fulltext_index(
&mut self,
name: String,
column_name: &str,
) -> Result<(), StorageError> {
if self.indices.iter().any(|i| i.name == name) {
return Err(StorageError::DuplicateIndex { name });
}
let column_position = self.schema.column_position(column_name).ok_or_else(|| {
StorageError::ColumnNotFound {
column: column_name.into(),
}
})?;
if !matches!(
self.schema.columns[column_position].ty,
DataType::Text | DataType::Varchar(_)
) {
return Err(StorageError::Corrupt(format!(
"fulltext-GIN index {name:?} requires a TEXT/VARCHAR column; \
{column_name:?} is {:?}",
self.schema.columns[column_position].ty
)));
}
let mut idx = Index::new_gin_fulltext(name, column_position);
if let IndexKind::GinFulltext(map) = &mut idx.kind {
for (i, row) in self.rows.iter().enumerate() {
if let Value::Text(s) = &row.values[column_position] {
for lex in fts_simple::simple_lex(s) {
if let Some(entries) = map.get_mut(&lex) {
entries.push(RowLocator::Hot(i));
} else {
map.insert_mut(lex, alloc::vec![RowLocator::Hot(i)]);
}
}
}
}
}
self.indices.push(idx);
Ok(())
}
/// v7.17.0 Phase 2.2 — restore a fulltext-GIN from its
/// catalog snapshot payload. Mirrors
/// [`Self::restore_gin_trgm_index`].
pub fn restore_gin_fulltext_index(
&mut self,
name: String,
column_name: &str,
map: PersistentBTreeMap<String, Vec<RowLocator>>,
) -> Result<(), StorageError> {
if self.indices.iter().any(|i| i.name == name) {
return Err(StorageError::DuplicateIndex { name });
}
let column_position = self.schema.column_position(column_name).ok_or_else(|| {
StorageError::ColumnNotFound {
column: column_name.into(),
}
})?;
let mut idx = Index::new_gin_fulltext(name, column_position);
idx.kind = IndexKind::GinFulltext(map);
self.indices.push(idx);
Ok(())
}
/// v7.37.8(sentori Epic 5 P2)— JSONB-GIN over a `Json` /
/// `Jsonb` column. Walks every row, extracts canonical
/// `(path, leaf)` tokens via
/// [`crate::jsonb_gin::extract_tokens`], and builds the
/// posting-list map. NULL or non-Json cells contribute no
/// tokens(`<col> @> <jsonb>` against a NULL row is always
/// false so absence here is correct).
pub fn add_gin_jsonb_index(
&mut self,
name: String,
column_name: &str,
) -> Result<(), StorageError> {
if self.indices.iter().any(|i| i.name == name) {
return Err(StorageError::DuplicateIndex { name });
}
let column_position = self.schema.column_position(column_name).ok_or_else(|| {
StorageError::ColumnNotFound {
column: column_name.into(),
}
})?;
if !matches!(
self.schema.columns[column_position].ty,
DataType::Json | DataType::Jsonb
) {
return Err(StorageError::Corrupt(format!(
"JSONB-GIN index {name:?} requires a JSON/JSONB column; \
{column_name:?} is {:?}",
self.schema.columns[column_position].ty
)));
}
let mut idx = Index::new_gin_jsonb(name, column_position);
if let IndexKind::GinJsonb(map) = &mut idx.kind {
for (i, row) in self.rows.iter().enumerate() {
if let Value::Json(s) = &row.values[column_position] {
for tok in jsonb_gin::extract_tokens(s) {
if let Some(entries) = map.get_mut(&tok) {
entries.push(RowLocator::Hot(i));
} else {
map.insert_mut(tok, alloc::vec![RowLocator::Hot(i)]);
}
}
}
}
}
self.indices.push(idx);
Ok(())
}
/// v7.37.8 — restore a JSONB-GIN from its catalog snapshot
/// payload. Mirrors [`Self::restore_gin_fulltext_index`].
pub fn restore_gin_jsonb_index(
&mut self,
name: String,
column_name: &str,
map: PersistentBTreeMap<String, Vec<RowLocator>>,
) -> Result<(), StorageError> {
if self.indices.iter().any(|i| i.name == name) {
return Err(StorageError::DuplicateIndex { name });
}
let column_position = self.schema.column_position(column_name).ok_or_else(|| {
StorageError::ColumnNotFound {
column: column_name.into(),
}
})?;
let mut idx = Index::new_gin_jsonb(name, column_position);
idx.kind = IndexKind::GinJsonb(map);
self.indices.push(idx);
Ok(())
}
/// v5.1: register cold-tier locators on a `BTree` index. Used
/// after [`Catalog::load_segment_bytes`] to wire every cold-
/// tier row's PK back to its segment so
/// [`Catalog::lookup_by_pk`] can resolve it. Each call
/// appends to the index — keys that already have hot or cold
/// locators keep them. Returns the number of locators
/// registered.
///
/// Pre-v5.2 (freezer) this is the only path that adds Cold
/// variants to a PB; post-freezer the background freezer
/// thread produces these as a batch under the engine write
/// lock and this API becomes its in-memory primitive.
///
/// Errors if `index_name` doesn't exist or names an NSW graph
/// (NSW indices don't carry per-key row locators — they're
/// vector-search structures).
pub fn register_cold_locators<I>(
&mut self,
index_name: &str,
locators: I,
) -> Result<usize, StorageError>
where
I: IntoIterator<Item = (IndexKey, RowLocator)>,
{
let idx = self
.indices
.iter_mut()
.find(|i| i.name == index_name)
.ok_or_else(|| StorageError::Corrupt(format!("index {index_name:?} not found")))?;
let map = match &mut idx.kind {
IndexKind::BTree(map) => map,
IndexKind::Nsw(_)
| IndexKind::Brin { .. }
| IndexKind::Gin(_)
| IndexKind::GinTrgm(_)
| IndexKind::GinFulltext(_)
| IndexKind::GinJsonb(_) => {
return Err(StorageError::Corrupt(format!(
"index {index_name:?} is not BTree; cold locators apply only to BTree indices"
)));
}
};
let mut count = 0usize;
for (key, locator) in locators {
if let Some(entries) = map.get_mut(&key) {
entries.push(locator);
} else {
map.insert_mut(key, alloc::vec![locator]);
}
count += 1;
}
Ok(count)
}
/// v7.12.3 — GIN-side parallel to [`Self::register_cold_locators`].
/// Re-attaches `word → cold RowLocator` posting-list entries after
/// the from-rows rebuild loop. Errors when the index doesn't
/// exist or isn't a GIN. Both tsvector-GIN and trigram-GIN
/// variants share posting-list shape (`String → Vec<RowLocator>`),
/// so this helper accepts either.
pub fn register_gin_cold_locators<I>(
&mut self,
index_name: &str,
locators: I,
) -> Result<usize, StorageError>
where
I: IntoIterator<Item = (String, RowLocator)>,
{
let idx = self
.indices
.iter_mut()
.find(|i| i.name == index_name)
.ok_or_else(|| StorageError::Corrupt(format!("index {index_name:?} not found")))?;
let map = match &mut idx.kind {
// v7.17.0 Phase 2.2 — fulltext-GIN posting lists are
// shape-compatible with tsvector / trigram GINs, so
// cold-locator re-attach handles all three.
// v7.37.8 — JSONB-GIN shares the same posting-list shape,
// so it joins the same re-attach path.
IndexKind::Gin(map)
| IndexKind::GinTrgm(map)
| IndexKind::GinFulltext(map)
| IndexKind::GinJsonb(map) => map,
IndexKind::BTree(_) | IndexKind::Nsw(_) | IndexKind::Brin { .. } => {
return Err(StorageError::Corrupt(format!(
"register_gin_cold_locators: index {index_name:?} is not GIN"
)));
}
};
let mut count = 0usize;
for (word, locator) in locators {
if let Some(entries) = map.get_mut(&word) {
entries.push(locator);
} else {
map.insert_mut(word, alloc::vec![locator]);
}
count += 1;
}
Ok(count)
}
/// v5.2.3: remove every `Cold` locator currently registered on
/// `index_name` under the given `key`. `Hot` locators for the
/// same key are left in place — useful when a row has just been
/// promoted hot-side and the caller wants the old Cold pointer
/// retired without losing the new hot entry.
///
/// Returns the number of cold locators removed (0 when the key
/// has only hot entries or the key isn't present at all).
/// Errors when the index doesn't exist or isn't a `BTree`.
pub fn remove_cold_locators_for_key(
&mut self,
index_name: &str,
key: &IndexKey,
) -> Result<usize, StorageError> {
let idx = self
.indices
.iter_mut()
.find(|i| i.name == index_name)
.ok_or_else(|| {
StorageError::Corrupt(format!(
"remove_cold_locators_for_key: index {index_name:?} not found"
))
})?;
let map = match &mut idx.kind {
IndexKind::BTree(map) => map,
IndexKind::Nsw(_)
| IndexKind::Brin { .. }
| IndexKind::Gin(_)
| IndexKind::GinTrgm(_)
| IndexKind::GinFulltext(_)
| IndexKind::GinJsonb(_) => {
return Err(StorageError::Corrupt(format!(
"remove_cold_locators_for_key: index {index_name:?} is not BTree; \
cold locators apply only to BTree indices"
)));
}
};
let Some(entries) = map.get(key) else {
return Ok(0);
};
let mut kept: Vec<RowLocator> =
entries.iter().copied().filter(RowLocator::is_hot).collect();
let removed = entries.len() - kept.len();
if removed == 0 {
return Ok(0);
}
kept.shrink_to_fit();
// PersistentBTreeMap has no remove API in v5.2; when every
// locator for `key` was Cold, the key keeps an empty Vec
// entry. `Index::lookup_eq` already treats `Some(&[])` and
// `None` as the same empty slice (via `Vec::as_slice`), so
// callers can't distinguish the two. The space cost is one
// empty Vec per shadowed-then-promoted key — bounded and
// recoverable when the future compaction job lands.
map.insert_mut(key.clone(), kept);
Ok(removed)
}
/// v7.13.0 — append a new column to the schema and back-fill
/// every existing row with `fill_value`. Used by the engine's
/// `ALTER TABLE t ADD COLUMN …` handler (mailrs round-5 G1).
/// Indices on existing columns keep working — column positions
/// don't shift since the new column lands at the end — so no
/// index rebuild is needed.
pub fn add_column(&mut self, col: ColumnSchema, fill_value: Value<'static>) {
self.schema.columns.push(col);
let mut new_rows: PersistentVec<Row<'static>> = PersistentVec::new();
for row in self.rows.iter() {
let mut values = row.values.clone();
values.push(fill_value.clone());
new_rows.push_mut(Row::new(values));
}
self.rows = new_rows;
}
/// v7.15.0 — replace the partial-index predicate source on
/// the index at slot `idx`. Used by `ALTER TABLE … RENAME
/// COLUMN` after the engine rewrites column-identifier
/// references in the predicate source text. Pure metadata
/// edit; index rows are unaffected (they're keyed by
/// column position, not predicate text).
pub fn set_partial_predicate(&mut self, idx: usize, pred: Option<String>) {
debug_assert!(idx < self.indices.len());
self.indices[idx].partial_predicate = pred;
}
/// v7.15.0 — rename the column at `col_pos` to `new_name`.
/// The on-disk row encoding is positional, so no row rewrite
/// is needed; only the schema's column name changes. Indices,
/// UCs, FKs all key off column positions and are unaffected.
/// Source-text references that hold the column name (CHECK
/// predicates, partial-index predicates, runtime DEFAULT
/// expressions, trigger `UPDATE OF` lists) are rewritten by
/// the engine before this helper is called — the storage
/// layer doesn't depend on `spg-sql` and so can't re-parse the
/// predicate sources itself.
pub fn rename_column(&mut self, col_pos: usize, new_name: &str) {
debug_assert!(col_pos < self.schema.columns.len());
self.schema.columns[col_pos].name = new_name.to_string();
}
/// v7.13.3 — drop the column at `col_pos`. Removes the entry
/// from the schema, the value from every row, any index that
/// references the column (pure drop, not shift), and shifts
/// every remaining index/UC/FK column position that pointed
/// past `col_pos` down by one. Used by `ALTER TABLE t DROP
/// COLUMN <c>` (mailrs round-7 S8). FK dependents on this
/// column must already have been removed by the caller (CASCADE
/// path); the helper assumes only same-column index removal is
/// needed.
pub fn drop_column(&mut self, col_pos: usize) {
debug_assert!(col_pos < self.schema.columns.len());
// v7.39 (round 215) — dropping a column shifts every later column's
// position, which would leave a range-exclusion index pointing at the
// wrong column. Drop the indexes rather than risk a silent-wrong
// probe; enforce falls back to the correct O(n) scan until they are
// rebuilt (`ensure_excl_range_index` from the constraint's updated
// column position).
self.excl_indexes.clear();
// Strip the column from the schema.
self.schema.columns.remove(col_pos);
// Rewrite every row to omit the cell at col_pos.
let mut new_rows: PersistentVec<Row> = PersistentVec::new();
for row in self.rows.iter() {
let mut values = row.values.clone();
if col_pos < values.len() {
values.remove(col_pos);
}
new_rows.push_mut(Row::new(values));
}
self.rows = new_rows;
// Drop indices on the column outright; shift the rest.
self.indices.retain(|idx| idx.column_position != col_pos);
for idx in &mut self.indices {
if idx.column_position > col_pos {
idx.column_position -= 1;
}
// Same shift for any included-columns reference.
for inc in &mut idx.included_columns {
if *inc > col_pos {
*inc -= 1;
}
}
}
// Shift uniqueness-constraint column positions (and drop
// entries that lose all columns, though that shouldn't
// happen in practice — caller has already CASCADE-removed
// FKs and there's no general CASCADE for UCs).
let mut surviving_ucs: Vec<UniquenessConstraint> = Vec::new();
for mut uc in core::mem::take(&mut self.schema.uniqueness_constraints) {
uc.columns.retain(|&c| c != col_pos);
if uc.columns.is_empty() {
continue;
}
for c in &mut uc.columns {
if *c > col_pos {
*c -= 1;
}
}
surviving_ucs.push(uc);
}
self.schema.uniqueness_constraints = surviving_ucs;
// Shift FK local_columns (parent-pointing column positions
// are off-table and untouched).
for fk in &mut self.schema.foreign_keys {
for c in &mut fk.local_columns {
if *c > col_pos {
*c -= 1;
}
}
}
// Rebuild remaining indices' payload — the column-position
// shift means existing IndexKey entries are still keyed by
// the same column data but the position numbers changed;
// existing key→locator maps stay valid because they're
// keyed by Value not position. The rebuild is conservative
// — same pattern delete_rows uses post-mutation.
self.rebuild_indices();
}
/// v4.4: delete the rows at the given positions in one pass.
/// `positions` must be unique; ordering doesn't matter. Indices
/// are rebuilt from scratch (cheaper than tracking incremental
/// shifts across both B-tree and NSW). Returns the number of
/// rows removed.
/// v7.17.0 Phase 1.3 — wipe every row. Used by REFRESH
/// MATERIALIZED VIEW; same effect as `delete_rows((0..N).into())`
/// but skips the per-position bookkeeping for the all-removed
/// fast path. Indices are rebuilt (empty).
pub fn truncate(&mut self) {
self.rows = PersistentVec::new();
// v7.37.15 (Phase A.2) — keep headers lock-step.
self.headers = PersistentVec::new();
// v7.37.15 (Phase C.1) — clear rowids lock-step. `next_rowid`
// is NOT reset: ids stay globally monotonic within the
// relation so a post-truncate insert never reuses a pre-
// truncate id that a stale reference might still name.
self.rowids = PersistentVec::new();
self.hot_bytes = 0;
self.rebuild_indices();
}
pub fn delete_rows(&mut self, positions: &[usize]) -> usize {
// v7.37.15 (Epic W slice 1) — capture the RowIds of the targeted
// rows BEFORE the deletion shifts them out. One id per input
// position (parallel to `positions`), `RowId::UNASSIGNED` for an
// out-of-bounds position. Only pay for it when redo capture is
// on. `writer_version` (xmax) is 0: the deleting TxId is not
// threaded to this layer yet.
let redo_rowids: Vec<crate::row_header::RowId> = if self.redo_log.is_some() {
positions
.iter()
.map(|&p| {
self.rowids()
.get(p)
.copied()
.unwrap_or(crate::row_header::RowId::UNASSIGNED)
})
.collect()
} else {
Vec::new()
};
let removed = self.delete_rows_no_index(positions);
if removed > 0 {
self.rebuild_indices();
// v7.34 — capture row-level redo. Record the input positions
// (replay's `delete_rows` dedups + bounds-filters identically);
// skip a no-op delete so the log stays minimal.
self.record_redo(move |table| RowChange::Delete {
table,
positions: positions.to_vec(),
rowids: redo_rowids,
writer_version: 0,
});
}
removed
}
/// v7.37.5 (mailrs crash-recovery Ask 3) — row-only delete for the
/// WAL-replay batch path: removes the rows + decrements `hot_bytes`,
/// **does NOT** call `rebuild_indices()` and does **NOT** capture
/// redo. The caller is responsible for invoking `rebuild_indices_pub`
/// once after a sequence of `*_no_index` mutations on this table.
/// Skipping the per-call rebuild closes the
/// O(records × rows × indices × log rows) replay blow-up
/// (5000 DELETEs × 100k × 13 × ln 100k ≈ minutes → seconds).
/// Returns the number of rows actually removed (dedup + bounds-
/// filtered identically to `delete_rows`).
pub fn delete_rows_no_index(&mut self, positions: &[usize]) -> usize {
if positions.is_empty() {
return 0;
}
// Mark positions; v4.39: PV has no in-place retain, so we rebuild
// a fresh PV by pushing the survivors. Still O(n log₃₂ n); the
// structural-sharing win shows up at `Catalog::clone()`, not here.
let mut to_remove = alloc::vec![false; self.rows.len()];
let mut removed = 0;
for &p in positions {
if p < to_remove.len() && !to_remove[p] {
to_remove[p] = true;
removed += 1;
}
}
if removed == 0 {
return 0;
}
let mut new_rows: PersistentVec<Row> = PersistentVec::new();
let mut new_headers: PersistentVec<crate::row_header::RowHeader> = PersistentVec::new();
// v7.37.15 (Phase C.1) — survivors carry their stable RowId
// across the compaction so a held lock / redo reference keeps
// naming the same row while its physical slot shifts down.
let mut new_rowids: PersistentVec<crate::row_header::RowId> = PersistentVec::new();
let mut removed_bytes: u64 = 0;
// v7.37.16 (autovacuum) — recount dead survivors: this rebuild
// is the compaction hub (vacuum and physical delete both land
// here), so the incremental counter re-bases exactly.
let mut surviving_dead: u64 = 0;
for (i, row) in self.rows.iter().enumerate() {
if to_remove[i] {
removed_bytes =
removed_bytes.saturating_add(row_body_encoded_len(row, &self.schema) as u64);
} else {
new_rows.push_mut(row.clone());
// v7.37.15 (Phase A.2) — keep headers lock-step.
// Phase C will stamp xmax with the deleting tx's
// id INSTEAD of physically dropping the row; Phase
// A.2 keeps physical-delete semantics so
// serialisation + WAL paths stay identical.
if let Some(h) = self.headers.get(i) {
if h.xmax != crate::row_header::XMAX_ALIVE {
surviving_dead += 1;
}
new_headers.push_mut(*h);
} else {
new_headers.push_mut(crate::row_header::RowHeader::frozen());
}
if let Some(rid) = self.rowids.get(i) {
new_rowids.push_mut(*rid);
} else {
// Should not happen once C.1 is wired everywhere;
// allocate a fresh id as a defensive fallback so
// the lock-step invariant survives a legacy path.
let rid = crate::row_header::RowId(self.next_rowid);
self.next_rowid += 1;
new_rowids.push_mut(rid);
}
}
}
self.rows = new_rows;
self.headers = new_headers;
self.rowids = new_rowids;
self.hot_bytes = self.hot_bytes.saturating_sub(removed_bytes);
self.dead_rows = surviving_dead;
debug_assert_eq!(
self.rows.len(),
self.headers.len(),
"headers must stay in lock-step with rows after delete_rows_no_index"
);
removed
}
/// v7.37.5 — public alias for the private `rebuild_indices` helper.
/// Used by `Catalog::apply_redo` to coalesce per-record rebuilds
/// across a batch of `RowChange`s into one rebuild per touched table.
pub fn rebuild_indices_pub(&mut self) {
self.rebuild_indices();
}
/// v7.37.5 (mailrs crash-recovery Ask 3) — replace the table's
/// row vector + `hot_bytes` in one shot, then rebuild every
/// index from the new rows. Used by `Catalog::apply_redo`'s
/// batched run: a contiguous slice of `RowChange`s targeting
/// this table is composed into a final `(PersistentVec<Row>,
/// hot_bytes)` pair via in-memory bookkeeping, then handed to
/// this method ONCE for index regeneration. Replaces N per-
/// record `rebuild_indices` calls with 1 per run.
/// v7.39 (flip crash-replay P0) — like
/// [`Self::set_rows_and_rebuild_indices`] but KEEPS the caller's
/// per-slot RowIds. Redo replay applies one WAL record per
/// statement; reassigning ids between records broke every later
/// record's tombstone targets (they name the ids the crashed
/// process allocated), resurrecting deleted rows. The id
/// allocator advances past every preserved id so post-replay
/// inserts never collide.
pub fn set_rows_and_rebuild_indices_with_rowids(
&mut self,
new_rows: PersistentVec<Row<'static>>,
new_hot_bytes: u64,
rowids: &[crate::row_header::RowId],
headers: &[crate::row_header::RowHeader],
) {
debug_assert_eq!(new_rows.len(), rowids.len());
debug_assert_eq!(new_rows.len(), headers.len());
let mut new_headers: PersistentVec<crate::row_header::RowHeader> = PersistentVec::new();
let mut new_rowids: PersistentVec<crate::row_header::RowId> = PersistentVec::new();
let mut dead: u64 = 0;
for (rid, h) in rowids.iter().zip(headers) {
// Preserve the caller's header — an earlier replayed WAL
// record's tombstone stamp must survive this record's
// rebuild (per-statement replay re-freezing every header
// resurrected every previously-deleted row).
if h.xmax != crate::row_header::XMAX_ALIVE {
dead += 1;
}
new_headers.push_mut(*h);
let rid = if *rid == crate::row_header::RowId::UNASSIGNED {
let fresh = crate::row_header::RowId(self.next_rowid);
self.next_rowid += 1;
fresh
} else {
if rid.0 >= self.next_rowid {
self.next_rowid = rid.0 + 1;
}
*rid
};
new_rowids.push_mut(rid);
}
self.rows = new_rows;
self.headers = new_headers;
self.rowids = new_rowids;
self.hot_bytes = new_hot_bytes;
self.dead_rows = dead;
debug_assert_eq!(self.rows.len(), self.headers.len());
debug_assert_eq!(self.rows.len(), self.rowids.len());
self.rebuild_indices();
}
pub fn set_rows_and_rebuild_indices(
&mut self,
new_rows: PersistentVec<Row<'static>>,
new_hot_bytes: u64,
) {
// v7.37.15 (Phase A.2) — synthesise frozen headers for
// the replacement rows. Phase D's catalog snapshot format
// (bumped to V6) will start carrying headers verbatim,
// letting recovery preserve real xmin/xmax instead of
// freezing everything; until then frozen is the safe
// default for replay (all visible to every snapshot).
let mut new_headers: PersistentVec<crate::row_header::RowHeader> = PersistentVec::new();
// v7.37.15 (Phase C.1) — fresh monotonic ids for the
// replacement rows drawn from the relation allocator, so a
// post-replay id never collides with a pre-replay one.
let mut new_rowids: PersistentVec<crate::row_header::RowId> = PersistentVec::new();
for _ in 0..new_rows.len() {
new_headers.push_mut(crate::row_header::RowHeader::frozen());
let rid = crate::row_header::RowId(self.next_rowid);
self.next_rowid += 1;
new_rowids.push_mut(rid);
}
self.rows = new_rows;
self.headers = new_headers;
self.rowids = new_rowids;
self.hot_bytes = new_hot_bytes;
// All-frozen replacement headers → no dead rows by construction.
self.dead_rows = 0;
debug_assert_eq!(
self.rows.len(),
self.headers.len(),
"headers must stay in lock-step with rows after set_rows_and_rebuild_indices"
);
debug_assert_eq!(
self.rows.len(),
self.rowids.len(),
"rowids must stay in lock-step with rows after set_rows_and_rebuild_indices"
);
self.rebuild_indices();
}
/// v7.37.5 (mailrs crash-recovery Ask 3) — row-only insert for the
/// WAL-replay batch path: pushes the row + bumps `hot_bytes`, and
/// **does NOT** update any index (B-tree, GIN, NSW). The caller is
/// responsible for invoking `rebuild_indices_pub` once after a
/// sequence of `*_no_index` mutations on this table.
/// Schema validation (arity + per-column type compatibility) is
/// applied so a malformed redo log surfaces honestly.
pub fn insert_no_index(&mut self, row: Row<'static>) -> Result<(), StorageError> {
if row.len() != self.schema.columns.len() {
return Err(StorageError::ArityMismatch {
expected: self.schema.columns.len(),
actual: row.len(),
});
}
validate_row_against_schema(&row.values, &self.schema)?;
self.hot_bytes = self
.hot_bytes
.saturating_add(row_body_encoded_len(&row, &self.schema) as u64);
self.rows.push_mut(row);
// v7.37.15 (Phase A.2) — keep headers lock-step for the
// WAL replay path. Replay-time headers are frozen because
// pre-V6 envelopes carry no header info; Phase D will
// restore the original xmin/xmax once the V6 catalog
// format ships.
self.headers
.push_mut(crate::row_header::RowHeader::frozen());
// v7.37.15 (Phase C.1) — RowId lock-step for the WAL-replay
// append path.
let rid = self.alloc_rowid();
self.rowids.push_mut(rid);
debug_assert_eq!(
self.rows.len(),
self.headers.len(),
"headers must stay in lock-step with rows after insert_no_index"
);
debug_assert_eq!(
self.rows.len(),
self.rowids.len(),
"rowids must stay in lock-step with rows after insert_no_index"
);
Ok(())
}
/// v7.37.5 (mailrs crash-recovery Ask 3) — row-only update for the
/// WAL-replay batch path: replaces the row at `position` + adjusts
/// `hot_bytes`, and **does NOT** touch any index. Skipping the
/// per-update incremental index work is safe because the trailing
/// `rebuild_indices_pub` regenerates indices from `self.rows` in
/// their final state.
pub fn update_row_no_index(
&mut self,
position: usize,
new_values: Vec<Value<'static>>,
) -> Result<(), StorageError> {
if position >= self.rows.len() {
return Err(StorageError::Corrupt(alloc::format!(
"update_row_no_index: position {position} out of bounds (rows={})",
self.rows.len()
)));
}
if new_values.len() != self.schema.columns.len() {
return Err(StorageError::ArityMismatch {
expected: self.schema.columns.len(),
actual: new_values.len(),
});
}
validate_row_against_schema(&new_values, &self.schema)?;
let old_row = self
.rows
.get(position)
.expect("position bounds-checked above");
let old_bytes = row_body_encoded_len(old_row, &self.schema) as u64;
let new_row = Row::new(new_values);
let new_bytes = row_body_encoded_len(&new_row, &self.schema) as u64;
self.rows = self
.rows
.set(position, new_row)
.expect("position bounds-checked above");
self.hot_bytes = self
.hot_bytes
.saturating_sub(old_bytes)
.saturating_add(new_bytes);
Ok(())
}
/// v4.4: replace the row at `position` with `new_values` (must
/// match the schema arity + types). v7.20: index maintenance is
/// incremental — only indices whose key value changed are
/// touched (B-tree entry move in place; NSW / BRIN / GIN fall
/// back to a full rebuild when their column changed).
pub fn update_row(
&mut self,
position: usize,
new_values: Vec<Value<'static>>,
) -> Result<(), StorageError> {
if position >= self.rows.len() {
return Err(StorageError::Corrupt(alloc::format!(
"update_row: position {position} out of bounds (rows={})",
self.rows.len()
)));
}
if new_values.len() != self.schema.columns.len() {
return Err(StorageError::ArityMismatch {
expected: self.schema.columns.len(),
actual: new_values.len(),
});
}
// Reuse the per-cell type-compat validation that `insert`
// applies. The body below mirrors that check intentionally —
// factoring it would be more code than the duplication.
for (i, (val, col)) in new_values.iter().zip(&self.schema.columns).enumerate() {
if val.is_null() {
if !col.nullable {
return Err(StorageError::NullInNotNull {
column: col.name.clone(),
});
}
continue;
}
// v7.39 (read01 round 54) — `data_type()` is None for the
// eval-only variants that carry no DataType (RegClass, Composite).
// They are NOT NULL, so `.expect("non-null")` PANICKED on them —
// materialising a CTE like `WITH w AS (SELECT 't'::regclass)` blew
// up the query with an "internal error". Report a clean type
// mismatch instead; the engine coerces these before they get here
// on every path that knows how.
let Some(actual) = val.data_type() else {
// An eval-only value (RegClass carries oid + name, Composite a
// field tuple) has no DataType in the storage lattice. It is
// NOT NULL, so the old `.expect("non-null")` PANICKED — which
// is how `WITH w AS (SELECT 't'::regclass)` blew up with an
// "internal error". Accept it: the value keeps its dual shape
// and downstream comparisons (RegClass vs BigInt oid) handle it.
continue;
};
let compatible = column_accepts(actual, col.ty);
if !compatible {
return Err(StorageError::TypeMismatch {
column: col.name.clone(),
expected: col.ty,
actual,
position: i,
});
}
}
let old_row = self
.rows
.get(position)
.expect("position bounds-checked above");
let old_bytes = row_body_encoded_len(old_row, &self.schema) as u64;
let new_row = Row::new(new_values);
let new_bytes = row_body_encoded_len(&new_row, &self.schema) as u64;
// v7.20 P4 — incremental index maintenance. `rows.set`
// replaces the row in place, so every OTHER row's Hot
// locator stays valid; only indices whose key value
// actually changed at `position` need touching. The
// common OLTP shape (`UPDATE … SET non_indexed_col = …
// WHERE pk = $1`) touches no index at all — pre-v7.20
// this path paid a full rebuild_indices() (O(rows ×
// indices)) per UPDATE, which dominated the profiled
// write cost on a 5k-row table (~1 ms/stmt).
//
// BTree gets an in-place entry move (drop Hot(position)
// from the old key's locator list, append to the new
// key's). NSW graphs / BRIN summaries / GIN posting
// lists have no cheap single-key move — a changed column
// under one of those falls back to the full rebuild.
enum IdxFix {
BTreeMove {
idx_pos: usize,
old_key: Option<IndexKey>,
new_key: Option<IndexKey>,
},
FullRebuild,
}
let mut fixes: Vec<IdxFix> = Vec::new();
for (idx_pos, idx) in self.indices.iter().enumerate() {
let col = idx.column_position;
let old_v = &old_row.values[col];
let new_v = &new_row.values[col];
if old_v == new_v {
continue;
}
match &idx.kind {
IndexKind::BTree(_) => fixes.push(IdxFix::BTreeMove {
idx_pos,
old_key: IndexKey::from_value(old_v),
new_key: IndexKey::from_value(new_v),
}),
IndexKind::Nsw(_)
| IndexKind::Brin { .. }
| IndexKind::Gin(_)
| IndexKind::GinTrgm(_)
| IndexKind::GinFulltext(_)
| IndexKind::GinJsonb(_) => {
fixes.clear();
fixes.push(IdxFix::FullRebuild);
break;
}
}
}
// v7.39 (round 215) — capture the range-exclusion key move BEFORE the
// in-place `set` consumes `new_row`. A `FullRebuild` (a GIN/NSW/BRIN
// column changed) rebuilds the excl indexes too via `rebuild_indices`,
// so only apply the incremental move on the pure-BTreeMove path.
let excl_has_full = fixes.iter().any(|f| matches!(f, IdxFix::FullRebuild));
let excl_moves: Vec<(usize, Option<(i128, u8)>, Option<(i128, u8)>)> =
if self.excl_indexes.is_empty() || excl_has_full {
Vec::new()
} else {
self.excl_indexes
.iter()
.filter_map(|e| {
let c = e.column_position;
let old_k = old_row.values.get(c).and_then(crate::range_excl_index_key);
let new_k = new_row.values.get(c).and_then(crate::range_excl_index_key);
if old_k == new_k {
None // range bound unchanged — no index touch
} else {
Some((c, old_k, new_k))
}
})
.collect()
};
self.rows = self
.rows
.set(position, new_row)
.expect("position bounds-checked above");
self.hot_bytes = self
.hot_bytes
.saturating_sub(old_bytes)
.saturating_add(new_bytes);
// v7.34 — capture row-level redo (after the row is in place; the
// immutable read of the new values is dropped before record_redo's
// mutable borrow, and gated so capture-off pays nothing).
if self.redo_log.is_some() {
let new_row = self
.rows
.get(position)
.map(|r| r.values.clone())
.unwrap_or_default();
// v7.37.15 (Epic W slice 1) — carry the stable RowId of the
// updated row (`position` is bounds-checked above, so the id
// is present). `writer_version` (xmax of the superseded
// tuple) is 0: the writing TxId is not threaded here yet.
let redo_rowid = self
.rowids()
.get(position)
.copied()
.unwrap_or(crate::row_header::RowId::UNASSIGNED);
self.record_redo(|table| RowChange::Update {
table,
pos: position,
new_row,
rowid: redo_rowid,
writer_version: 0,
});
}
for fix in fixes {
match fix {
IdxFix::FullRebuild => {
self.rebuild_indices();
break;
}
IdxFix::BTreeMove {
idx_pos,
old_key,
new_key,
} => {
let IndexKind::BTree(map) = &mut self.indices[idx_pos].kind else {
unreachable!("IdxFix::BTreeMove built from a BTree index");
};
// NULL keys never enter the B-tree (from_value
// returns None), so a None on either side means
// "no entry on that side".
if let Some(k) = old_key
&& let Some(locs) = map.get(&k)
{
let mut locs = locs.clone();
locs.retain(|l| *l != RowLocator::Hot(position));
// No remove_mut on the persistent map: an
// empty locator list is the tombstone —
// lookup_eq returns an empty slice, and the
// next rebuild_indices() drops the key.
map.insert_mut(k, locs);
}
if let Some(k) = new_key {
if let Some(entries) = map.get_mut(&k) {
entries.push(RowLocator::Hot(position));
} else {
map.insert_mut(k, alloc::vec![RowLocator::Hot(position)]);
}
}
}
}
}
// v7.39 (round 215) — apply the range-exclusion key moves captured
// above (skipped when a FullRebuild already re-emitted every excl
// index). Same shape as the BTreeMove: drop Hot(position) from the
// old key, append it to the new key.
for (col, old_k, new_k) in excl_moves {
let Some(ex) = self
.excl_indexes
.iter_mut()
.find(|e| e.column_position == col)
else {
continue;
};
if let Some(k) = old_k
&& let Some(locs) = ex.map.get(&k)
{
let mut locs = locs.clone();
locs.retain(|l| *l != RowLocator::Hot(position));
ex.map.insert_mut(k, locs);
}
if let Some(k) = new_k {
if let Some(entries) = ex.map.get_mut(&k) {
entries.push(RowLocator::Hot(position));
} else {
ex.map.insert_mut(k, alloc::vec![RowLocator::Hot(position)]);
}
}
}
Ok(())
}
/// v4.4 helper used by `delete_rows` / `update_row`: discard all
/// index payloads and rebuild from `self.rows`. Cheap enough
/// for typical SPG scale (catalogs in the docker-compose
/// deployment shape are small); the alternative — incremental
/// shift bookkeeping across B-tree + NSW — would be far more
/// invasive than the savings justify.
fn rebuild_indices(&mut self) {
// v5.2.3: capture every `Cold` locator on every BTree index
// before the rebuild, so the from-rows re-emission below
// (which only produces `Hot` locators) doesn't drop cold-
// tier entries on keys unrelated to the row that changed.
// Pre-v5.2.3 this was a `freeze_oldest_to_cold` worry only
// and the freezer did its own capture-then-reregister; v5.2.3
// promotes that pattern into the base helper because UPDATE
// / DELETE now run rebuild_indices on tables with cold rows.
let preserved_cold: Vec<(String, Vec<(IndexKey, RowLocator)>)> = self
.indices
.iter()
.filter_map(|idx| match &idx.kind {
IndexKind::BTree(map) => {
let cold: Vec<(IndexKey, RowLocator)> = map
.iter()
.flat_map(|(k, locs)| {
locs.iter()
.filter(|l| l.is_cold())
.copied()
.map(move |l| (k.clone(), l))
})
.collect();
if cold.is_empty() {
None
} else {
Some((idx.name.clone(), cold))
}
}
// BRIN / NSW carry no key→locator map. GIN handles
// its own cold preservation below in `preserved_gin_cold`.
IndexKind::Nsw(_)
| IndexKind::Brin { .. }
| IndexKind::Gin(_)
| IndexKind::GinTrgm(_)
| IndexKind::GinFulltext(_)
| IndexKind::GinJsonb(_) => None,
})
.collect();
// v7.12.3 — same cold-preservation pattern for GIN's
// `word → Vec<RowLocator>` posting lists. Parallel to the
// BTree pass above (different key type so a separate vec is
// cleaner than a generic merge). v7.15.0: trigram-GIN
// (`gin_trgm_ops`) shares the same posting-list shape, so
// one pass handles both — the `RebuildKind` carries the
// kind tag to drive resurrection.
let preserved_gin_cold: Vec<(String, Vec<(String, RowLocator)>)> = self
.indices
.iter()
.filter_map(|idx| match &idx.kind {
// v7.17.0 Phase 2.2 — fulltext-GIN posting lists
// share the `String → Vec<RowLocator>` shape, so
// cold preservation handles all three GIN flavours
// in one pass.
IndexKind::Gin(map)
| IndexKind::GinTrgm(map)
| IndexKind::GinFulltext(map)
| IndexKind::GinJsonb(map) => {
let cold: Vec<(String, RowLocator)> = map
.iter()
.flat_map(|(w, locs)| {
locs.iter()
.filter(|l| l.is_cold())
.copied()
.map(move |l| (w.clone(), l))
})
.collect();
if cold.is_empty() {
None
} else {
Some((idx.name.clone(), cold))
}
}
IndexKind::BTree(_) | IndexKind::Nsw(_) | IndexKind::Brin { .. } => None,
})
.collect();
// v6.7.1 — descriptor needs to capture index kind so the
// rebuild loop can resurrect BTree / NSW / BRIN / GIN exactly
// as they were. (NSW carries m; BRIN carries the column type
// snapshot; BTree / GIN need no extra payload.)
#[derive(Clone)]
enum RebuildKind {
BTree,
Nsw(usize),
Brin(DataType),
Gin,
GinTrgm,
GinFulltext,
GinJsonb,
}
// v7.39 (round 170) — the descriptor must carry the FULL index
// metadata: the rebuild used to reconstruct via bare
// `Index::new_btree(name, pos)`, silently DROPPING is_unique /
// extra_column_positions / partial_predicate / expression /
// included_columns / nulls_not_distinct — so the first VACUUM
// (or any delete-path rebuild) turned every UNIQUE INDEX into a
// plain one and stopped enforcing it (probe-reproduced:
// duplicate keys inserted silently after VACUUM).
struct RebuildDesc {
name: String,
column_position: usize,
kind: RebuildKind,
is_unique: bool,
extra_column_positions: Vec<usize>,
partial_predicate: Option<String>,
expression: Option<String>,
included_columns: Vec<usize>,
nulls_not_distinct: bool,
// v7.39 (round 537) — carried through a rebuild like the rest.
descending: bool,
nulls_first: Option<bool>,
collation: Option<String>,
}
let descriptors: Vec<RebuildDesc> = self
.indices
.iter()
.map(|idx| {
let kind = match &idx.kind {
IndexKind::Nsw(g) => RebuildKind::Nsw(g.m),
IndexKind::Brin { column_type } => RebuildKind::Brin(*column_type),
IndexKind::BTree(_) => RebuildKind::BTree,
IndexKind::Gin(_) => RebuildKind::Gin,
IndexKind::GinTrgm(_) => RebuildKind::GinTrgm,
IndexKind::GinFulltext(_) => RebuildKind::GinFulltext,
IndexKind::GinJsonb(_) => RebuildKind::GinJsonb,
};
RebuildDesc {
name: idx.name.clone(),
column_position: idx.column_position,
kind,
is_unique: idx.is_unique,
extra_column_positions: idx.extra_column_positions.clone(),
partial_predicate: idx.partial_predicate.clone(),
expression: idx.expression.clone(),
included_columns: idx.included_columns.clone(),
nulls_not_distinct: idx.nulls_not_distinct,
descending: idx.descending,
nulls_first: idx.nulls_first,
collation: idx.collation.clone(),
}
})
.collect();
self.indices.clear();
for desc in descriptors {
let RebuildDesc {
name,
column_position,
kind: rebuild_kind,
is_unique,
extra_column_positions,
partial_predicate,
expression,
included_columns,
nulls_not_distinct,
descending,
nulls_first,
collation,
} = desc;
let pre_len = self.indices.len();
match rebuild_kind {
RebuildKind::Nsw(m) => {
let idx = Index::new_nsw(name, column_position, m);
self.indices.push(idx);
let idx_pos = self.indices.len() - 1;
let row_indices: Vec<usize> = (0..self.rows.len()).collect();
for row_idx in row_indices {
nsw_insert_at(self, idx_pos, row_idx);
}
}
RebuildKind::Brin(column_type) => {
// BRIN has no in-memory rebuild — the summaries
// live in cold segments which freeze emits.
self.indices
.push(Index::new_brin(name, column_position, column_type));
}
RebuildKind::BTree => {
// v7.39 (round 170) — bulk build: collect + sort +
// group + from_sorted. The per-row insert_mut paid a
// path-copy allocation per row per index (~15ms per
// index on a 50k-row VACUUM, the dominant cost).
let mut idx = Index::new_btree(name, column_position);
let mut pairs: Vec<(IndexKey, usize)> = Vec::with_capacity(self.rows.len());
for (i, row) in self.rows.iter().enumerate() {
if let Some(key) = IndexKey::from_value(&row.values[column_position]) {
pairs.push((key, i));
}
}
pairs.sort_by(|a, b| a.0.cmp(&b.0));
let mut grouped: Vec<(IndexKey, Vec<RowLocator>)> = Vec::new();
for (key, i) in pairs {
match grouped.last_mut() {
Some((k, locs)) if *k == key => locs.push(RowLocator::Hot(i)),
_ => grouped.push((key, alloc::vec![RowLocator::Hot(i)])),
}
}
idx.kind = IndexKind::BTree(
crate::persistent_btree::PersistentBTreeMap::from_sorted(grouped),
);
self.indices.push(idx);
}
RebuildKind::Gin => {
let mut idx = Index::new_gin(name, column_position);
if let IndexKind::Gin(map) = &mut idx.kind {
for (i, row) in self.rows.iter().enumerate() {
if let Value::TsVector(lexemes) = &row.values[column_position] {
for lex in lexemes {
if let Some(entries) = map.get_mut(&lex.word) {
entries.push(RowLocator::Hot(i));
} else {
map.insert_mut(
lex.word.clone(),
alloc::vec![RowLocator::Hot(i)],
);
}
}
}
}
}
self.indices.push(idx);
}
RebuildKind::GinTrgm => {
let mut idx = Index::new_gin_trgm(name, column_position);
if let IndexKind::GinTrgm(map) = &mut idx.kind {
for (i, row) in self.rows.iter().enumerate() {
if let Value::Text(s) = &row.values[column_position] {
for tri in trgm::extract_trigrams(s) {
// r1019 — address the String-keyed map with the borrowed
// trigram; allocate one only for a key the map has never
// seen, which after the first rows is rare.
let key = trgm::trigram_str(&tri);
if let Some(entries) = map.get_mut_by(key) {
entries.push(RowLocator::Hot(i));
} else {
map.insert_mut(
alloc::string::ToString::to_string(key),
alloc::vec![RowLocator::Hot(i)],
);
}
}
}
}
}
self.indices.push(idx);
}
RebuildKind::GinFulltext => {
// v7.17.0 Phase 2.2 — re-derive the lexeme
// posting list from each TEXT/VARCHAR cell.
// Mirrors the GinTrgm rebuild shape but
// tokenises via `fts_simple::simple_lex`
// (same rule as `to_tsvector('simple')`).
let mut idx = Index::new_gin_fulltext(name, column_position);
if let IndexKind::GinFulltext(map) = &mut idx.kind {
for (i, row) in self.rows.iter().enumerate() {
if let Value::Text(s) = &row.values[column_position] {
for lex in fts_simple::simple_lex(s) {
if let Some(entries) = map.get_mut(&lex) {
entries.push(RowLocator::Hot(i));
} else {
map.insert_mut(lex, alloc::vec![RowLocator::Hot(i)]);
}
}
}
}
}
self.indices.push(idx);
}
RebuildKind::GinJsonb => {
// v7.37.8 — re-derive the JSONB posting list
// from each `Value::Json` cell.
let mut idx = Index::new_gin_jsonb(name, column_position);
if let IndexKind::GinJsonb(map) = &mut idx.kind {
for (i, row) in self.rows.iter().enumerate() {
if let Value::Json(s) = &row.values[column_position] {
for tok in jsonb_gin::extract_tokens(s) {
if let Some(entries) = map.get_mut(&tok) {
entries.push(RowLocator::Hot(i));
} else {
map.insert_mut(tok, alloc::vec![RowLocator::Hot(i)]);
}
}
}
}
}
self.indices.push(idx);
}
}
// v7.39 (round 170) — restore the captured metadata onto
// whatever this arm pushed (see RebuildDesc above).
if let Some(idx) = self.indices.get_mut(pre_len) {
idx.is_unique = is_unique;
idx.extra_column_positions = extra_column_positions;
idx.partial_predicate = partial_predicate;
idx.expression = expression;
idx.included_columns = included_columns;
idx.nulls_not_distinct = nulls_not_distinct;
idx.descending = descending;
idx.nulls_first = nulls_first;
idx.collation = collation;
}
}
// Re-attach preserved cold locators after the from-rows
// rebuild. `register_cold_locators` handles the per-key
// entries-vec append; no key collisions arise because the
// rebuild loop above produced only Hot locators.
for (idx_name, locators) in preserved_cold {
// Errors here would only fire if the index disappeared
// between snapshot and rebuild, which can't happen
// because the rebuild restores the same descriptor set.
let _ = self.register_cold_locators(&idx_name, locators);
}
// v7.12.3 — same for GIN posting-list cold locators.
for (idx_name, locators) in preserved_gin_cold {
let _ = self.register_gin_cold_locators(&idx_name, locators);
}
// v7.39 (round 215) — the range-exclusion indexes address rows by the
// same physical slot, so a compaction that shifted slots invalidates
// their Hot locators too. Re-emit them from the (post-compaction) rows.
if !self.excl_indexes.is_empty() {
self.rebuild_excl_indexes();
}
}
fn add_nsw_index_inner(
&mut self,
name: String,
column_name: &str,
m: usize,
restore: Option<NswGraph>,
) -> Result<(), StorageError> {
if self.indices.iter().any(|i| i.name == name) {
return Err(StorageError::DuplicateIndex { name });
}
let column_position = self.schema.column_position(column_name).ok_or_else(|| {
StorageError::ColumnNotFound {
column: column_name.into(),
}
})?;
if !matches!(
self.schema.columns[column_position].ty,
DataType::Vector { .. }
) {
return Err(StorageError::TypeMismatch {
column: column_name.into(),
expected: DataType::Vector {
dim: 0,
encoding: VecEncoding::F32,
},
actual: self.schema.columns[column_position].ty,
position: column_position,
});
}
if let Some(graph) = restore {
self.indices.push(Index {
name,
column_position,
kind: IndexKind::Nsw(graph),
included_columns: Vec::new(),
partial_predicate: None,
expression: None,
is_unique: false,
nulls_not_distinct: false,
descending: false,
nulls_first: None,
collation: None,
extra_column_positions: Vec::new(),
});
return Ok(());
}
let idx = Index::new_nsw(name, column_position, m);
self.indices.push(idx);
let idx_pos = self.indices.len() - 1;
// Bulk-build by walking the existing rows in order — each insert
// sees the partial graph and links into it.
let row_indices: Vec<usize> = (0..self.rows.len()).collect();
for row_idx in row_indices {
nsw_insert_at(self, idx_pos, row_idx);
}
Ok(())
}
}
/// v7.37.5 (mailrs crash-recovery Ask 3) — per-cell schema-compat
/// check shared by `insert_no_index` and `update_row_no_index`. The
/// logic mirrors the inline body in `insert` / `update_row` (NULL
/// handling, the cross-type compatibility map: TEXT ↔ VARCHAR/CHAR/
/// JSON/JSONB, TIMESTAMP ↔ TIMESTAMPTZ, BIT ↔ VARBIT, INET ↔ CIDR,
/// NUMERIC scale match).
/// v7.39 (round 642/643) — does a value of type `actual` belong in a
/// column declared `declared`?
///
/// This existed in THREE copies — insert, update and the standalone
/// row validator — and they had drifted apart in three independent
/// places: only insert accepted the `name` pairs, only insert and
/// update accepted a bit-to-bit pair with differing typmods, and only
/// update accepted a NEGATIVE declared numeric scale. Each omission was
/// a hole waiting for a value to reach that path; none was a deliberate
/// tightening, so the union below is the rule and all three now ask it.
///
/// Measured before converging: every shape the three disagreed about
/// answers identically to PG18 today, so this fixes nothing observable.
/// What it fixes is the next type — adding `xid` in round 640 meant
/// remembering to patch three places, and forgetting one would have
/// half-wired it.
///
/// The rule itself: a pair is compatible when the value's storage shape
/// is what the column stores. Length and precision contracts are NOT
/// checked here — they belong to coercion, which runs first.
/// `#[inline]` is not decoration. Extracting this matrix out of its
/// three call sites — a change with no semantic content at all — cost
/// `SELECT count(*) FROM d WHERE g BETWEEN 10 AND 20` **23x**, 5.8 ms
/// to 133 ms over 500 000 rows, reproducibly and outside the panel.
/// None of the three callers is on a scan path; taking the matrix out
/// of them was enough to move whatever else in this module the row loop
/// depends on being inlined. Round 641 learned the same thing about
/// `eval::binop::compare`. A refactor that reads as pure structure is
/// still a codegen change.
#[inline]
fn column_accepts(actual: DataType, declared: DataType) -> bool {
if actual == declared {
return true;
}
if matches!(
(actual, declared),
// A NAME column stores a Value::Text: the type identity is the
// schema's and a value can never be one, so both directions.
(
DataType::Text,
DataType::Varchar(_)
| DataType::Char(_)
| DataType::Name
| DataType::Json
| DataType::Jsonb
) | (DataType::Name, DataType::Text)
// An XID column stores the Value::BigInt a transaction id
// has always been; xid8 has no value of its own at all.
| (DataType::BigInt, DataType::Xid | DataType::Xid8)
| (DataType::Xid | DataType::Xid8, DataType::BigInt)
// v7.39 (round 667) — an OID column likewise stores a plain
// integer. INT is listed as well as BIGINT because a bare
// literal arrives as one: PG takes `INSERT INTO t(o) VALUES
// (42)` into an oid column, and measured, it does NOT take the
// same integer into an xid column ("column is of type xid but
// expression is of type integer"). SPG has been laxer than PG
// on that xid direction since before this round — that is the
// limitation `DataType::Xid8` documents, not something added
// here.
| (
DataType::BigInt | DataType::Int | DataType::SmallInt,
DataType::Oid,
)
| (DataType::Oid, DataType::BigInt | DataType::Int)
// v7.39 (round 694) — `oid[]` rides in a BigIntArray cell, so
// it accepts one either way, exactly as the scalar above does.
| (DataType::BigIntArray | DataType::IntArray, DataType::OidArray)
| (DataType::OidArray, DataType::BigIntArray)
| (DataType::Json | DataType::Jsonb, DataType::Text)
| (DataType::Json, DataType::Jsonb)
| (DataType::Jsonb, DataType::Json)
| (DataType::Timestamp, DataType::Timestamptz)
| (DataType::Timestamptz, DataType::Timestamp)
// BIT / VARBIT share the BitString storage shape; INET /
// CIDR likewise. Same-family pairs with different typmods
// are compatible HERE — the length contract is coercion's.
| (DataType::Bit(_), DataType::BitVarying(_))
| (DataType::BitVarying(_), DataType::Bit(_))
| (DataType::Bit(_), DataType::Bit(_))
| (DataType::BitVarying(_), DataType::BitVarying(_))
| (DataType::Inet, DataType::Cidr)
| (DataType::Cidr, DataType::Inet)
) {
return true;
}
// NUMERIC carries its own scale in the value while the column
// declares the expected one. An unconstrained `numeric` (the
// precision-0/scale-0 sentinel) takes any scale; a declared
// `numeric(p,s)` needs the rescaled value; and a NEGATIVE declared
// scale stores at display scale 0, having been rounded to a
// multiple of 10^|s|.
matches!(
(actual, declared),
(
DataType::Numeric { scale: a, .. },
DataType::Numeric {
precision: bp,
scale: b,
},
) if a == b || (bp == 0 && b == 0) || (b < 0 && a == 0)
)
}
fn validate_row_against_schema(
values: &[Value<'static>],
schema: &TableSchema,
) -> Result<(), StorageError> {
for (i, (val, col)) in values.iter().zip(&schema.columns).enumerate() {
if val.is_null() {
if !col.nullable {
return Err(StorageError::NullInNotNull {
column: col.name.clone(),
});
}
continue;
}
// v7.39 (read01 round 54) — see above: no panic on an untyped value.
let Some(actual) = val.data_type() else {
// See above: an eval-only untyped value is accepted, not a panic.
continue;
};
let compatible = column_accepts(actual, col.ty);
if !compatible {
return Err(StorageError::TypeMismatch {
column: col.name.clone(),
expected: col.ty,
actual,
position: i,
});
}
}
Ok(())
}
/// v6.0.4 — re-encode a single cell to the target `VecEncoding`.
/// Used by `Table::rebuild_nsw_index` when ALTER INDEX REBUILD
/// includes the optional `WITH (encoding = …)` clause. Round-trip
/// goes through f32: `current → Vec<f32> → target`, leaving NULL
/// cells untouched. Returns `Unsupported` on a non-vector cell —
/// the caller should have rejected the schema before reaching this.
fn recode_vector_cell(
cell: Value<'static>,
target: VecEncoding,
) -> Result<Value<'static>, StorageError> {
if matches!(cell, Value::Null) {
return Ok(cell);
}
// Step 1 — extract the f32 representation of the source cell.
let as_f32: Vec<f32> = match &cell {
Value::Vector(v) => v.to_vec(),
Value::Sq8Vector(q) => quantize::dequantize(q),
Value::HalfVector(h) => h.to_f32_vec(),
other => {
return Err(StorageError::Unsupported(format!(
"ALTER INDEX REBUILD: cannot recode non-vector cell {:?}",
other.data_type()
)));
}
};
// Step 2 — encode into the target shape. `F32` is the identity
// path (saves one alloc round-trip when the source is already
// F32 — but `Value::Vector(as_f32)` is the right answer
// regardless).
Ok(match target {
VecEncoding::F32 => Value::Vector(Cow::Owned(as_f32)),
VecEncoding::Sq8 => Value::Sq8Vector(quantize::quantize(&as_f32)),
VecEncoding::F16 => Value::HalfVector(halfvec::HalfVector::from_f32_slice(&as_f32)),
})
}
/// v7.39 (round 562) — a cursor over `Table`'s row headers that holds
/// the trie leaf it last descended to.
///
/// See `Table::header_runs` for why. Ask about ascending positions and
/// the descent happens once per 32; ask about scattered ones and it
/// happens as often as `position_visible` would have done it.
#[derive(Debug)]
pub struct HeaderRuns<'a> {
table: &'a Table,
/// `(start, run)` — `run[i - start]` is the header for position `i`.
run: Option<(usize, &'a [crate::row_header::RowHeader])>,
}
impl HeaderRuns<'_> {
/// Is the row at this position visible to the snapshot?
///
/// Answers exactly as `Table::position_visible` does — same
/// `SKIP LOCKED` handling, same snapshot rules — and the pins in
/// `e2e_index_only_scan_round560` hold both to it.
pub fn visible(&mut self, idx: usize, snapshot: &crate::snapshot::Snapshot) -> bool {
if let Some((start, run)) = self.run
&& idx >= start
&& idx - start < run.len()
{
return self.table.header_visible(idx, &run[idx - start], snapshot);
}
let Some((start, run)) = self.table.headers.run_containing(idx) else {
return false;
};
self.run = Some((start, run));
self.table.header_visible(idx, &run[idx - start], snapshot)
}
}