stoolap 0.4.0

High-performance embedded SQL database with MVCC, time-travel queries, and full ACID compliance
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
// Copyright 2025 Stoolap Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Version store for MVCC row versioning
//!
//! This module provides the core version storage for MVCC, including:
//! - [`RowVersion`] - Represents a specific version of a row
//! - [`VersionStore`] - Tracks latest committed versions for a table
//! - [`TransactionVersionStore`] - Transaction-local changes before commit
//!
//! # Performance
//!
//! The version store uses arena-based storage for zero-copy full table scans.
//! Row data is stored contiguously in memory, enabling 50x+ faster scans
//! compared to traditional per-row cloning.
//!

use std::fmt;
use std::num::{NonZeroU64, NonZeroUsize};
use std::sync::atomic::{AtomicBool, AtomicI64, AtomicUsize, Ordering};
use std::sync::Arc;

use parking_lot::{Mutex, RwLock};

use crate::common::SmartString;

use crate::common::{
    new_cow_btree_map, new_i64_map, new_i64_map_with_capacity, CompactArc, CowBTreeMap, I64Map,
};
use crate::core::types::DataType;
use crate::core::{Error, Row, RowVec, Schema, Value};
use crate::storage::expression::CompiledFilter;
use crate::storage::mvcc::arena::RowArena;
use crate::storage::mvcc::get_fast_timestamp;
#[cfg(not(test))]
use crate::storage::mvcc::registry::TransactionRegistry;
use crate::storage::mvcc::streaming_result::{StreamingResult, VisibleRowInfo};
use crate::storage::Index;
use ahash::AHashMap;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::{smallvec, SmallVec};

/// Type alias for version lists - uses SmallVec to avoid heap allocation
/// for the common case of a single version per row within a transaction.
type VersionList = SmallVec<[RowVersion; 1]>;

/// Group key using CompactArc<Value> to avoid cloning during aggregation.
/// Uses Arc::clone (O(1) atomic increment) instead of Value::clone (deep copy).
#[derive(Clone, Debug)]
pub enum GroupKey {
    Single(CompactArc<Value>),
    Multi(Vec<CompactArc<Value>>),
}

impl PartialEq for GroupKey {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (GroupKey::Single(a), GroupKey::Single(b)) => **a == **b,
            (GroupKey::Multi(a), GroupKey::Multi(b)) => {
                a.len() == b.len() && a.iter().zip(b.iter()).all(|(x, y)| **x == **y)
            }
            _ => false,
        }
    }
}

impl Eq for GroupKey {}

impl std::hash::Hash for GroupKey {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        match self {
            GroupKey::Single(v) => (**v).hash(state),
            GroupKey::Multi(vs) => {
                for v in vs {
                    (**v).hash(state);
                }
            }
        }
    }
}

/// Hash map for GroupKey with randomized hashing (HashDoS resistant)
type GroupKeyMap<V> = AHashMap<GroupKey, V>;

/// Result of storage-level grouped aggregation
#[derive(Debug, Clone)]
pub struct GroupedAggregateResult {
    /// Group key values
    pub group_values: Vec<Value>,
    /// Aggregate results in order of requested aggregates
    pub aggregate_values: Vec<Value>,
}

/// Represents a specific version of a row with complete data
///

#[derive(Clone)]
pub struct RowVersion {
    /// Transaction that created this version
    pub txn_id: i64,
    /// Transaction that deleted this version (0 if not deleted)
    pub deleted_at_txn_id: i64,
    /// Complete row data
    pub data: Row,
    /// Timestamp when this version was created
    pub create_time: i64,
}

impl RowVersion {
    /// Creates a new row version
    pub fn new(txn_id: i64, data: Row) -> Self {
        Self {
            txn_id,
            deleted_at_txn_id: 0,
            data,
            create_time: get_fast_timestamp(),
        }
    }

    /// Creates a new row version with a pre-computed timestamp
    /// This avoids calling SystemTime::now() for each row in bulk operations
    #[inline]
    pub fn new_with_timestamp(txn_id: i64, data: Row, create_time: i64) -> Self {
        Self {
            txn_id,
            deleted_at_txn_id: 0,
            data,
            create_time,
        }
    }

    /// Creates a new deleted version
    pub fn new_deleted(txn_id: i64, data: Row) -> Self {
        Self {
            txn_id,
            deleted_at_txn_id: txn_id,
            data,
            create_time: get_fast_timestamp(),
        }
    }

    /// Creates a new deleted version with a pre-computed timestamp
    #[inline]
    pub fn new_deleted_with_timestamp(txn_id: i64, data: Row, create_time: i64) -> Self {
        Self {
            txn_id,
            deleted_at_txn_id: txn_id,
            data,
            create_time,
        }
    }

    /// Returns true if this version has been marked as deleted
    pub fn is_deleted(&self) -> bool {
        self.deleted_at_txn_id != 0
    }
}

impl fmt::Debug for RowVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RowVersion")
            .field("txn_id", &self.txn_id)
            .field("deleted_at_txn_id", &self.deleted_at_txn_id)
            .field("create_time", &self.create_time)
            .finish()
    }
}

impl fmt::Display for RowVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "RowVersion{{TxnID: {}, DeletedAtTxnID: {}, CreateTime: {}}}",
            self.txn_id, self.deleted_at_txn_id, self.create_time
        )
    }
}

/// Entry in the version chain (linked list of versions)
/// Uses Arc for the prev pointer to enable O(1) cloning of the chain
///
/// # Memory Optimization
/// - `arena_idx`: Uses `Option<NonZeroU64>` (8 bytes) instead of `Option<usize>` (16 bytes)
///   Stored as `idx + 1` to enable niche optimization (0 = None)
struct VersionChainEntry {
    /// Current version
    version: RowVersion,
    /// Previous version in the chain (Arc allows cheap cloning)
    prev: Option<Arc<VersionChainEntry>>,
    /// Index into the arena for zero-copy access (None if data not in arena)
    /// Stored as `idx + 1` to enable niche optimization (NonZeroU64)
    arena_idx: Option<NonZeroU64>,
}

/// Count the depth of a version chain by traversing prev pointers.
/// O(k) where k is the chain length (typically <= max_version_history).
#[inline]
fn count_chain_depth(entry: &VersionChainEntry) -> usize {
    let mut depth = 1;
    let mut current = &entry.prev;
    while let Some(prev) = current {
        depth += 1;
        current = &prev.prev;
    }
    depth
}

/// Convert arena index (usize) to compact representation (Option<NonZeroU64>)
/// Stores `idx + 1` so that 0 can represent None via niche optimization
#[inline(always)]
fn pack_arena_idx(idx: usize) -> Option<NonZeroU64> {
    // idx + 1 is always > 0, supports full usize range on 64-bit systems
    NonZeroU64::new((idx as u64).saturating_add(1))
}

/// Convert compact arena index back to usize
/// Returns None if the stored value was None
#[inline(always)]
fn unpack_arena_idx(packed: Option<NonZeroU64>) -> Option<usize> {
    packed.map(|nz| (nz.get() - 1) as usize)
}

/// Convert Option<usize> to Option<NonZeroUsize> for RowIndex
/// Stores `idx + 1` so that 0 can represent None via niche optimization
#[inline(always)]
fn pack_row_arena_idx(idx: Option<usize>) -> Option<NonZeroUsize> {
    idx.and_then(|i| NonZeroUsize::new(i.wrapping_add(1)))
}

/// Convert Option<NonZeroUsize> back to Option<usize> for RowIndex
#[inline(always)]
fn unpack_row_arena_idx(packed: Option<NonZeroUsize>) -> Option<usize> {
    packed.map(|nz| nz.get().wrapping_sub(1))
}

/// Tracks write operations with the version read for conflict detection
///

#[derive(Clone)]
pub struct WriteSetEntry {
    /// Version when first read (None if row didn't exist)
    pub read_version: Option<RowVersion>,
    /// Sequence number when read
    pub read_version_seq: i64,
}

// ============================================================================
// HashMap Pool for TransactionVersionStore
// ============================================================================
//
// Pools recycled HashMaps to avoid allocation overhead when creating many
// short-lived transactions. Each auto-commit INSERT creates a transaction,
// and without pooling, this causes ~5.5KB allocation per INSERT.
//
// With pooling, maps are returned to the pool on Drop and reused by the next
// transaction, reducing allocation churn by ~99% for bulk insert workloads.

/// Maximum number of maps to keep in each pool.
/// Prevents unbounded memory growth while allowing reasonable reuse.
const MAP_POOL_MAX_SIZE: usize = 64;

/// Global pool for VersionList maps (local_versions in TransactionVersionStore)
static VERSION_LIST_MAP_POOL: Mutex<Vec<I64Map<VersionList>>> = Mutex::new(Vec::new());

/// Global pool for WriteSetEntry maps (write_set in TransactionVersionStore)
static WRITE_SET_MAP_POOL: Mutex<Vec<I64Map<WriteSetEntry>>> = Mutex::new(Vec::new());

/// Get a VersionList map from pool or create a new one
#[inline]
fn get_version_list_map() -> I64Map<VersionList> {
    if let Some(map) = VERSION_LIST_MAP_POOL.lock().pop() {
        map
    } else {
        new_i64_map_with_capacity(TX_VERSION_MAP_INITIAL_CAPACITY)
    }
}

/// Get a WriteSetEntry map from pool or create a new one
#[inline]
fn get_write_set_map() -> I64Map<WriteSetEntry> {
    if let Some(map) = WRITE_SET_MAP_POOL.lock().pop() {
        map
    } else {
        new_i64_map_with_capacity(TX_VERSION_MAP_INITIAL_CAPACITY)
    }
}

/// Return a VersionList map to the pool for reuse
#[inline]
fn return_version_list_map(mut map: I64Map<VersionList>) {
    map.clear();
    let mut pool = VERSION_LIST_MAP_POOL.lock();
    if pool.len() < MAP_POOL_MAX_SIZE {
        pool.push(map);
    }
    // If pool is full, map is dropped (deallocated)
}

/// Return a WriteSetEntry map to the pool for reuse
#[inline]
fn return_write_set_map(mut map: I64Map<WriteSetEntry>) {
    map.clear();
    let mut pool = WRITE_SET_MAP_POOL.lock();
    if pool.len() < MAP_POOL_MAX_SIZE {
        pool.push(map);
    }
    // If pool is full, map is dropped (deallocated)
}

/// Clear the transaction version map pools.
/// Call this when dropping the database to release pooled memory.
pub fn clear_version_map_pools() {
    VERSION_LIST_MAP_POOL.lock().clear();
    WRITE_SET_MAP_POOL.lock().clear();
}

/// Capacity hint for transaction version maps - used by pool functions
const TX_VERSION_MAP_INITIAL_CAPACITY: usize = 16;

/// Lightweight row index for deferred materialization
///
/// Instead of cloning row data during scans, we return indices that can be
/// materialized later. This enables zero-copy filtering and limiting.
///
/// # Performance
/// For `SELECT * FROM t WHERE x > 100 LIMIT 10` on 100K rows:
/// - Old: Clone 100K rows, filter to 50K, limit to 10 (100K allocations)
/// - New: Get 100K indices, filter to 50K, limit to 10, clone 10 (10 allocations)
///
/// # Memory Optimization
/// Uses `Option<NonZeroUsize>` (8 bytes) instead of `Option<usize>` (16 bytes)
/// for arena_idx, reducing struct size from 24 to 16 bytes (33% smaller).
#[derive(Clone, Copy, Debug)]
pub struct RowIndex {
    /// Row ID
    pub row_id: i64,
    /// Arena index (None if row data is not in arena, must clone from version)
    /// Stored as `idx + 1` to enable niche optimization (0 = None)
    arena_idx: Option<NonZeroUsize>,
}

impl RowIndex {
    /// Create a new RowIndex with the given row_id and arena index
    #[inline(always)]
    pub fn new(row_id: i64, arena_idx: Option<usize>) -> Self {
        Self {
            row_id,
            arena_idx: pack_row_arena_idx(arena_idx),
        }
    }

    /// Get the arena index (unpacked to Option<usize>)
    #[inline(always)]
    pub fn arena_idx(&self) -> Option<usize> {
        unpack_row_arena_idx(self.arena_idx)
    }
}

/// Aggregate operation type for deferred aggregation
///
/// Used with `compute_aggregates()` to perform multiple aggregations in a single pass.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AggregateOp {
    Count,
    CountStar,
    Sum,
    Min,
    Max,
    Avg,
}

/// Result of an aggregate operation
#[derive(Clone, Debug)]
pub enum AggregateResult {
    /// Count result
    Count(usize),
    /// Sum result (sum, count of non-null values)
    Sum(f64, usize),
    /// Min result
    Min(Option<Value>),
    /// Max result
    Max(Option<Value>),
    /// Avg result (sum, count of non-null values) - caller computes sum/count
    Avg(f64, usize),
}

/// Internal accumulator for aggregations.
/// Sum/Avg use split i128 + f64 accumulators: i128 for integers (no overflow),
/// f64 for floats. Combined as `int as f64 + float` at finalization.
enum AggregateAccumulator {
    Count(usize),
    /// (int_sum, float_sum, count)
    Sum(i128, f64, usize),
    Min(Option<Value>),
    Max(Option<Value>),
    /// (int_sum, float_sum, count)
    Avg(i128, f64, usize),
}

/// Visibility checker trait - will be implemented by TransactionRegistry
///
/// This allows VersionStore to check visibility without circular dependencies
pub trait VisibilityChecker: Send + Sync {
    /// Check if a version created by `version_txn_id` is visible to `viewing_txn_id`
    fn is_visible(&self, version_txn_id: i64, viewing_txn_id: i64) -> bool;

    /// Get the current global sequence number
    fn get_current_sequence(&self) -> i64;

    /// Get all active transaction IDs (for cleanup operations)
    fn get_active_transaction_ids(&self) -> Vec<i64>;

    /// Check if a transaction was committed before a given commit sequence cutoff.
    ///
    /// Returns true if the transaction is committed AND its commit sequence
    /// is less than the cutoff. Used for consistent snapshot iteration to ensure
    /// only transactions committed before the snapshot point are included.
    ///
    /// Default implementation returns true for all committed transactions.
    fn is_committed_before(&self, _txn_id: i64, _cutoff_commit_seq: i64) -> bool {
        true // Default: no cutoff filtering
    }

    /// Check if a transaction uses snapshot isolation.
    ///
    /// Under snapshot isolation, the arena-only fast path is unsafe because
    /// HEAD versions committed after the viewer's snapshot are not visible,
    /// and the correct behavior requires walking version chains to find older
    /// visible versions.
    fn needs_snapshot_isolation(&self, _txn_id: i64) -> bool {
        false // Default: ReadCommitted (arena fast path is safe)
    }
}

/// Opaque snapshot of the version store at extraction time.
/// Used by `remove_sealed_rows` to detect concurrent commits.
pub struct ExtractionSnapshot {
    inner: crate::common::CowBTree<VersionChainEntry>,
}

/// Token holding pre-removal snapshot data needed for deferred index cleanup.
/// Created by `remove_sealed_rows`, consumed by `remove_sealed_index_entries`.
#[derive(Default)]
pub struct SealedIndexCleanup {
    /// Row IDs that were removed from the version store.
    pub removed_ids: Vec<i64>,
    /// CowBTree snapshot taken BEFORE version removal. Contains the row data
    /// needed to compute index values for removal.
    snapshot: Option<crate::common::CowBTree<VersionChainEntry>>,
}

/// VersionStore tracks the latest committed version of each row for a table
///
/// Uses CowBTreeMap (RwLock<CowBTree>) for the version store because:
/// - O(1) snapshot cloning for lock-free reads (critical for MVCC)
/// - Ordered iteration is free (B+ tree is sorted by key)
/// - MVCC has single-writer semantics per transaction, so concurrent map sharding is overhead
/// - Point lookups are O(log n) which is fast enough for typical row counts
/// - Eliminates the ~350μs sort overhead during full scans
///
/// Arena-based storage provides 50x+ faster full table scans by:
/// - Storing all row data contiguously in memory
/// - Returning slices instead of clones during iteration
/// - Eliminating per-row allocation overhead
pub struct VersionStore {
    /// Row versions indexed by row ID (CowBTree for O(1) snapshot cloning)
    versions: CowBTreeMap<VersionChainEntry>,
    /// The name of the table this store belongs to (SmartString for inline storage ≤24 bytes)
    table_name: SmartString,
    /// Table schema (Arc for zero-cost cloning on read)
    schema: RwLock<CompactArc<Schema>>,
    /// Indexes on this table (FxHashMap for fast string key lookups)
    indexes: RwLock<FxHashMap<String, Arc<dyn Index>>>,
    /// Whether this store has been closed
    closed: AtomicBool,
    /// Auto-increment counter for tables without explicit PK
    auto_increment_counter: AtomicI64,
    /// Track which transaction has uncommitted changes to each row
    uncommitted_writes: RwLock<I64Map<i64>>,
    /// Visibility checker (registry reference)
    /// Production: concrete type for zero-cost inlining in hot paths
    /// Test: dyn trait for TestVisibilityChecker flexibility
    #[cfg(not(test))]
    visibility_checker: Option<Arc<TransactionRegistry>>,
    #[cfg(test)]
    visibility_checker: Option<Arc<dyn VisibilityChecker>>,
    /// Arena-based storage for zero-copy full table scans
    arena: RowArena,
    /// Zone maps for segment pruning (set by ANALYZE)
    /// Uses Arc to avoid cloning on every read - critical for high QPS workloads
    zone_maps: RwLock<Option<Arc<crate::storage::mvcc::zonemap::TableZoneMap>>>,
    /// Maximum number of previous versions to keep per row (0 = unlimited)
    /// This limits memory growth during write-heavy operations.
    /// Default is 10 - enough for most concurrent transaction scenarios.
    max_version_history: usize,
    /// Count of committed non-deleted rows for O(1) COUNT(*) queries.
    /// Updated on commit: +1 for INSERT, -1 for DELETE.
    /// This is an optimization for the common case of autocommit queries.
    committed_row_count: AtomicUsize,
    /// Per-table mutex for ON CONFLICT INSERT serialization.
    /// Only acquired for upsert statements to prevent TOCTOU races.
    /// Plain INSERTs (no ON CONFLICT) proceed lock-free.
    upsert_mutex: Arc<parking_lot::Mutex<()>>,
}

impl VersionStore {
    /// Creates a new version store
    pub fn new(table_name: impl Into<SmartString>, schema: Schema) -> Self {
        Self::with_capacity(table_name, schema, None, 0)
    }

    /// Creates a new version store with pre-allocated capacity
    ///
    /// Pre-allocating capacity avoids hash map resizing during bulk inserts.
    /// Use this when the expected row count is known (e.g., during recovery).
    #[cfg(not(test))]
    pub fn with_capacity(
        table_name: impl Into<SmartString>,
        schema: Schema,
        checker: Option<Arc<TransactionRegistry>>,
        expected_rows: usize,
    ) -> Self {
        let _ = expected_rows;
        let versions = new_cow_btree_map();

        Self {
            versions,
            table_name: table_name.into(),
            schema: RwLock::new(CompactArc::new(schema)),
            indexes: RwLock::new(FxHashMap::default()),
            closed: AtomicBool::new(false),
            auto_increment_counter: AtomicI64::new(0),
            uncommitted_writes: RwLock::new(new_i64_map()),
            visibility_checker: checker,
            arena: RowArena::new(),
            zone_maps: RwLock::new(None),
            max_version_history: 10, // Default: keep up to 10 previous versions
            committed_row_count: AtomicUsize::new(0),
            upsert_mutex: Arc::new(parking_lot::Mutex::new(())),
        }
    }

    /// Creates a new version store with pre-allocated capacity (test version)
    #[cfg(test)]
    pub fn with_capacity(
        table_name: impl Into<SmartString>,
        schema: Schema,
        checker: Option<Arc<dyn VisibilityChecker>>,
        expected_rows: usize,
    ) -> Self {
        let _ = expected_rows;
        let versions = new_cow_btree_map();

        Self {
            versions,
            table_name: table_name.into(),
            schema: RwLock::new(CompactArc::new(schema)),
            indexes: RwLock::new(FxHashMap::default()),
            closed: AtomicBool::new(false),
            auto_increment_counter: AtomicI64::new(0),
            uncommitted_writes: RwLock::new(new_i64_map()),
            visibility_checker: checker,
            arena: RowArena::new(),
            zone_maps: RwLock::new(None),
            max_version_history: 10,
            committed_row_count: AtomicUsize::new(0),
            upsert_mutex: Arc::new(parking_lot::Mutex::new(())),
        }
    }

    /// Sets the maximum version history limit per row
    ///
    /// This controls how many previous versions are kept for each row.
    /// Lower values reduce memory usage but limit time-travel query range.
    /// - 0 = unlimited (not recommended for write-heavy workloads)
    /// - 1 = only keep immediate previous (minimal memory, limited AS OF range)
    /// - 10 = default, good balance for most workloads
    ///
    /// Note: Values above 254 are capped to 254 for practical reasons.
    pub fn set_max_version_history(&mut self, limit: usize) {
        // Cap at 254 for practical memory management
        self.max_version_history = limit.min(254);
    }

    /// Gets the current max version history limit
    pub fn max_version_history(&self) -> usize {
        self.max_version_history
    }

    /// Creates a new version store with a visibility checker (production)
    #[cfg(not(test))]
    pub fn with_visibility_checker(
        table_name: impl Into<SmartString>,
        schema: Schema,
        checker: Arc<TransactionRegistry>,
    ) -> Self {
        Self::with_capacity(table_name, schema, Some(checker), 0)
    }

    /// Creates a new version store with a visibility checker (test)
    #[cfg(test)]
    pub fn with_visibility_checker(
        table_name: impl Into<SmartString>,
        schema: Schema,
        checker: Arc<dyn VisibilityChecker>,
    ) -> Self {
        Self::with_capacity(table_name, schema, Some(checker), 0)
    }

    /// Sets the visibility checker (production)
    #[cfg(not(test))]
    pub fn set_visibility_checker(&mut self, checker: Arc<TransactionRegistry>) {
        self.visibility_checker = Some(checker);
    }

    /// Sets the visibility checker (test)
    #[cfg(test)]
    pub fn set_visibility_checker(&mut self, checker: Arc<dyn VisibilityChecker>) {
        self.visibility_checker = Some(checker);
    }

    /// Returns the table name
    pub fn table_name(&self) -> &str {
        &self.table_name
    }

    /// Returns the schema (cheap CompactArc clone)
    pub fn schema(&self) -> CompactArc<Schema> {
        self.schema.read().clone()
    }

    /// Returns a mutable reference to the schema (for modifications)
    /// Callers must use CompactArc::make_mut() to get &mut Schema
    pub fn schema_mut(&self) -> parking_lot::RwLockWriteGuard<'_, CompactArc<Schema>> {
        self.schema.write()
    }

    /// Returns the current auto-increment counter value
    pub fn get_auto_increment_counter(&self) -> i64 {
        self.auto_increment_counter.load(Ordering::Acquire)
    }

    /// Returns the next available auto-increment ID
    pub fn get_next_auto_increment_id(&self) -> i64 {
        self.auto_increment_counter.fetch_add(1, Ordering::AcqRel) + 1
    }

    /// Sets the auto-increment counter to a specific value (only if current is lower)
    ///
    /// Returns true if the value was updated, false if no update was needed
    pub fn set_auto_increment_counter(&self, value: i64) -> bool {
        loop {
            let current = self.auto_increment_counter.load(Ordering::Acquire);
            if current >= value {
                return false;
            }

            if self
                .auto_increment_counter
                .compare_exchange(current, value, Ordering::AcqRel, Ordering::Acquire)
                .is_ok()
            {
                return true;
            }
        }
    }

    /// Returns the current auto-increment value without incrementing
    pub fn get_current_auto_increment_value(&self) -> i64 {
        self.auto_increment_counter.load(Ordering::Acquire)
    }

    /// Adds a new version for a row
    pub fn add_version(&self, row_id: i64, version: RowVersion) {
        if self.closed.load(Ordering::Acquire) {
            return;
        }

        let is_new_version_deleted = version.deleted_at_txn_id != 0;

        // Use write lock for the entire operation (MVCC single-writer semantics)
        let mut versions = self.versions.write();

        // Use entry API to avoid double traversal
        match versions.entry(row_id) {
            crate::common::cow_btree::Entry::Occupied(mut occupied) => {
                // Extract existing data from the entry
                let existing = occupied.get();
                let existing_arena_idx = existing.arena_idx;
                let was_deleted = existing.version.deleted_at_txn_id != 0;

                // Update committed row count based on delete state transitions
                if was_deleted && !is_new_version_deleted {
                    // Row was deleted, now being re-inserted -> increment
                    self.committed_row_count.fetch_add(1, Ordering::Relaxed);
                } else if !was_deleted && is_new_version_deleted {
                    // Row was visible, now being deleted -> decrement
                    self.committed_row_count.fetch_sub(1, Ordering::Relaxed);
                }

                // O(k) chain management - depth computed by traversal
                // When limit exceeded: drop old chain AND reuse arena slot
                let existing_depth = count_chain_depth(existing);
                let new_depth = existing_depth + 1;
                let can_reuse_arena =
                    self.max_version_history > 0 && new_depth > self.max_version_history;

                // Only clone existing version data when needed:
                // 1. For delete operations that need to preserve data
                // 2. When keeping version history (not pruning)
                let mut new_version = version;
                if new_version.deleted_at_txn_id != 0 && new_version.data.is_empty() {
                    // For deletes, preserve data from current version
                    new_version.data = existing.version.data.clone();
                }

                // Store in arena (only for non-deleted versions)
                // OPTIMIZATION: Always reuse arena slot - historical data is in prev_chain
                let arena_idx = if new_version.deleted_at_txn_id == 0 {
                    // Convert Row to Arc once (takes ownership, no copy if already Arc)
                    let arc_data = std::mem::take(&mut new_version.data).into_arc();

                    // Always reuse existing arena slot if available
                    // Historical versions are stored in prev_chain.version.data
                    let idx = if let Some(old_idx) = unpack_arena_idx(existing_arena_idx) {
                        // Reuse the arena slot - prevents unbounded growth
                        self.arena.update_at(
                            old_idx,
                            row_id,
                            new_version.txn_id,
                            CompactArc::clone(&arc_data),
                        );
                        old_idx
                    } else {
                        // No existing slot (shouldn't happen for updates), append
                        self.arena.insert_arc(
                            row_id,
                            new_version.txn_id,
                            CompactArc::clone(&arc_data),
                        )
                    };

                    // Reuse the Arc for the version's data - enables O(1) clone on read
                    new_version.data = Row::from_arc(arc_data);

                    pack_arena_idx(idx)
                } else {
                    // Deleted version - mark arena as deleted for visibility
                    if let Some(old_arena_idx) = unpack_arena_idx(existing_arena_idx) {
                        self.arena.mark_deleted(old_arena_idx, new_version.txn_id);
                    }
                    existing_arena_idx
                };

                // Build version chain entry
                // When limit exceeded: drop entire history (no prev_chain allocation)
                // When under limit: create prev_chain with existing version
                let final_prev = if can_reuse_arena {
                    // Exceeded limit - drop all history, no allocation
                    None
                } else {
                    // Under limit - clone existing version and create chain
                    let existing_version = existing.version.clone();
                    let existing_prev = existing.prev.clone();
                    Some(Arc::new(VersionChainEntry {
                        version: existing_version,
                        prev: existing_prev,
                        // Historical versions don't use arena (slot reused by new HEAD)
                        arena_idx: None,
                    }))
                };

                let new_entry = VersionChainEntry {
                    version: new_version,
                    prev: final_prev,
                    arena_idx,
                };

                // Replace entry in-place (no additional tree traversal)
                occupied.insert(new_entry);
            }
            crate::common::cow_btree::Entry::Vacant(vacant) => {
                // First version for this row - store in arena
                // OPTIMIZATION: Convert Row to Arc once, then just clone Arc (no data copy)
                let (arena_idx, final_version) = if version.deleted_at_txn_id == 0 {
                    // New non-deleted row -> increment counter
                    self.committed_row_count.fetch_add(1, Ordering::Relaxed);

                    let mut v = version;
                    // Convert Row to Arc once (takes ownership, no copy if already Arc)
                    let arc_data = std::mem::take(&mut v.data).into_arc();
                    // Insert Arc into arena (just Arc::clone, no data copy)
                    let idx = self
                        .arena
                        .insert_arc(row_id, v.txn_id, CompactArc::clone(&arc_data));
                    // Create version with Arc-backed data for O(1) clone
                    v.data = Row::from_arc(arc_data);
                    (pack_arena_idx(idx), v)
                } else {
                    (None, version)
                };

                let new_entry = VersionChainEntry {
                    version: final_version,
                    prev: None,
                    arena_idx,
                };

                // Insert into vacant slot (no additional traversal)
                vacant.insert(new_entry);
            }
        }
    }

    /// Adds multiple versions in batch - used by commit
    ///
    /// Updates both the version store and the arena atomically per row.
    /// Arena updates are O(1) per row (just an insert), so this is efficient.
    /// Row arena index updates are batched under a single lock acquisition.
    /// Version data uses Arc-backed storage for O(1) clones on read.
    #[inline]
    pub fn add_versions_batch(&self, batch: Vec<(i64, RowVersion)>) {
        if self.closed.load(Ordering::Acquire) || batch.is_empty() {
            return;
        }

        // Use write lock for the entire batch operation (MVCC single-writer semantics)
        let mut versions = self.versions.write();

        // Track row count delta: positive for inserts, negative for deletes
        let mut count_delta: isize = 0;

        for (row_id, version) in batch {
            let is_new_version_deleted = version.deleted_at_txn_id != 0;

            // Use entry API to avoid double traversal
            match versions.entry(row_id) {
                crate::common::cow_btree::Entry::Occupied(mut occupied) => {
                    // Extract existing data from the entry
                    let existing = occupied.get();
                    let existing_arena_idx = existing.arena_idx;
                    let was_deleted = existing.version.deleted_at_txn_id != 0;

                    // Track row count changes based on delete state transitions
                    if was_deleted && !is_new_version_deleted {
                        count_delta += 1; // Row re-inserted
                    } else if !was_deleted && is_new_version_deleted {
                        count_delta -= 1; // Row deleted
                    }

                    // O(k) chain management - depth computed by traversal
                    // When limit exceeded: drop old chain AND reuse arena slot
                    let existing_depth = count_chain_depth(existing);
                    let new_depth = existing_depth + 1;
                    let can_reuse_arena =
                        self.max_version_history > 0 && new_depth > self.max_version_history;

                    // Only clone existing version data when needed:
                    // 1. For delete operations that need to preserve data
                    // 2. When keeping version history (not pruning)
                    let mut new_version = version;
                    if new_version.deleted_at_txn_id != 0 && new_version.data.is_empty() {
                        // For deletes, preserve data from current version
                        new_version.data = existing.version.data.clone();
                    }

                    // Update arena with Arc reuse for O(1) clones on read
                    // OPTIMIZATION: Always reuse arena slot - historical data is in prev_chain
                    let arena_idx = if new_version.deleted_at_txn_id == 0 {
                        // Convert Row to Arc once (takes ownership, no copy if already Arc)
                        let arc_data = std::mem::take(&mut new_version.data).into_arc();

                        // Always reuse existing arena slot if available
                        // Historical versions are stored in prev_chain.version.data
                        let idx = if let Some(old_idx) = unpack_arena_idx(existing_arena_idx) {
                            // Reuse the arena slot - prevents unbounded growth
                            self.arena.update_at(
                                old_idx,
                                row_id,
                                new_version.txn_id,
                                CompactArc::clone(&arc_data),
                            );
                            old_idx
                        } else {
                            // No existing slot (shouldn't happen for updates), append
                            self.arena.insert_arc(
                                row_id,
                                new_version.txn_id,
                                CompactArc::clone(&arc_data),
                            )
                        };

                        // Reuse the Arc for the version's data
                        new_version.data = Row::from_arc(arc_data);

                        pack_arena_idx(idx)
                    } else {
                        // Deleted version - mark arena as deleted for visibility
                        if let Some(old_arena_idx) = unpack_arena_idx(existing_arena_idx) {
                            self.arena.mark_deleted(old_arena_idx, new_version.txn_id);
                        }
                        existing_arena_idx
                    };

                    // Build version chain entry
                    // When limit exceeded: drop entire history (no prev_chain allocation)
                    // When under limit: create prev_chain with existing version
                    let final_prev = if can_reuse_arena {
                        // Exceeded limit - drop all history, no allocation
                        None
                    } else {
                        // Under limit - clone existing version and create chain
                        let existing_version = existing.version.clone();
                        let existing_prev = existing.prev.clone();
                        Some(Arc::new(VersionChainEntry {
                            version: existing_version,
                            prev: existing_prev,
                            // Historical versions don't use arena (slot reused by new HEAD)
                            arena_idx: None,
                        }))
                    };

                    let new_entry = VersionChainEntry {
                        version: new_version,
                        prev: final_prev,
                        arena_idx,
                    };

                    // Replace entry in-place (no additional tree traversal)
                    occupied.insert(new_entry);
                }
                crate::common::cow_btree::Entry::Vacant(vacant) => {
                    // First version for this row - store in arena with Arc reuse
                    // OPTIMIZATION: Convert Row to Arc once, then just clone Arc (no data copy)
                    let (arena_idx, final_version) = if version.deleted_at_txn_id == 0 {
                        // New non-deleted row -> will increment counter
                        count_delta += 1;

                        let mut v = version;
                        // Convert Row to Arc once (takes ownership, no copy if already Arc)
                        let arc_data = std::mem::take(&mut v.data).into_arc();
                        // Insert Arc into arena (just Arc::clone, no data copy)
                        let idx =
                            self.arena
                                .insert_arc(row_id, v.txn_id, CompactArc::clone(&arc_data));
                        // Create version with Arc-backed data for O(1) clone
                        v.data = Row::from_arc(arc_data);
                        (pack_arena_idx(idx), v)
                    } else {
                        (None, version)
                    };

                    let new_entry = VersionChainEntry {
                        version: final_version,
                        prev: None,
                        arena_idx,
                    };

                    // Insert into vacant slot (no additional traversal)
                    vacant.insert(new_entry);
                }
            }
        }

        // Apply count delta in a single atomic operation
        if count_delta > 0 {
            self.committed_row_count
                .fetch_add(count_delta as usize, Ordering::Relaxed);
        } else if count_delta < 0 {
            self.committed_row_count
                .fetch_sub((-count_delta) as usize, Ordering::Relaxed);
        }
    }

    /// Add a single version to the store (optimized for auto-commit single-row inserts)
    ///
    /// This avoids Vec allocation for the common single-row commit case.
    #[inline]
    pub fn add_version_single(&self, row_id: i64, version: RowVersion) {
        if self.closed.load(Ordering::Acquire) {
            return;
        }

        let is_new_version_deleted = version.deleted_at_txn_id != 0;
        let mut versions = self.versions.write();

        match versions.entry(row_id) {
            crate::common::cow_btree::Entry::Occupied(mut occupied) => {
                // Extract existing data from the entry
                let existing = occupied.get();
                let existing_arena_idx = existing.arena_idx;
                let was_deleted = existing.version.deleted_at_txn_id != 0;

                // Update committed row count based on delete state transitions
                if was_deleted && !is_new_version_deleted {
                    // Row was deleted, now being re-inserted -> increment
                    self.committed_row_count.fetch_add(1, Ordering::Relaxed);
                } else if !was_deleted && is_new_version_deleted {
                    // Row was visible, now being deleted -> decrement
                    self.committed_row_count.fetch_sub(1, Ordering::Relaxed);
                }

                // O(k) chain management - depth computed by traversal
                // When limit exceeded: drop old chain AND reuse arena slot
                let existing_depth = count_chain_depth(existing);
                let new_depth = existing_depth + 1;
                let can_reuse_arena =
                    self.max_version_history > 0 && new_depth > self.max_version_history;

                // Only clone existing version data when needed:
                // 1. For delete operations that need to preserve data
                // 2. When keeping version history (not pruning)
                let mut new_version = version;
                if new_version.deleted_at_txn_id != 0 && new_version.data.is_empty() {
                    // For deletes, preserve data from current version
                    new_version.data = existing.version.data.clone();
                }

                // OPTIMIZATION: Always reuse arena slot - historical data is in prev_chain
                let arena_idx = if new_version.deleted_at_txn_id == 0 {
                    let arc_data = std::mem::take(&mut new_version.data).into_arc();

                    // Always reuse existing arena slot if available
                    // Historical versions are stored in prev_chain.version.data
                    let idx = if let Some(old_idx) = unpack_arena_idx(existing_arena_idx) {
                        // Reuse the arena slot - prevents unbounded growth
                        self.arena.update_at(
                            old_idx,
                            row_id,
                            new_version.txn_id,
                            CompactArc::clone(&arc_data),
                        );
                        old_idx
                    } else {
                        // No existing slot (shouldn't happen for updates), append
                        self.arena.insert_arc(
                            row_id,
                            new_version.txn_id,
                            CompactArc::clone(&arc_data),
                        )
                    };

                    new_version.data = Row::from_arc(arc_data);

                    pack_arena_idx(idx)
                } else {
                    // Deleted version - mark arena as deleted for visibility
                    if let Some(old_arena_idx) = unpack_arena_idx(existing_arena_idx) {
                        self.arena.mark_deleted(old_arena_idx, new_version.txn_id);
                    }
                    existing_arena_idx
                };

                // Build version chain entry
                // When limit exceeded: drop entire history (no prev_chain allocation)
                // When under limit: create prev_chain with existing version
                let final_prev = if can_reuse_arena {
                    // Exceeded limit - drop all history, no allocation
                    None
                } else {
                    // Under limit - clone existing version and create chain
                    let existing_version = existing.version.clone();
                    let existing_prev = existing.prev.clone();
                    Some(Arc::new(VersionChainEntry {
                        version: existing_version,
                        prev: existing_prev,
                        // Historical versions don't use arena (slot reused by new HEAD)
                        arena_idx: None,
                    }))
                };

                let new_entry = VersionChainEntry {
                    version: new_version,
                    prev: final_prev,
                    arena_idx,
                };

                occupied.insert(new_entry);
            }
            crate::common::cow_btree::Entry::Vacant(vacant) => {
                let (arena_idx, final_version) = if version.deleted_at_txn_id == 0 {
                    // New non-deleted row -> increment counter
                    self.committed_row_count.fetch_add(1, Ordering::Relaxed);

                    let mut v = version;
                    let arc_data = std::mem::take(&mut v.data).into_arc();
                    let idx = self
                        .arena
                        .insert_arc(row_id, v.txn_id, CompactArc::clone(&arc_data));
                    v.data = Row::from_arc(arc_data);
                    (pack_arena_idx(idx), v)
                } else {
                    (None, version)
                };

                let new_entry = VersionChainEntry {
                    version: final_version,
                    prev: None,
                    arena_idx,
                };

                vacant.insert(new_entry);
            }
        }
    }

    /// Quick check if a row might exist
    pub fn quick_check_row_existence(&self, row_id: i64) -> bool {
        if self.closed.load(Ordering::Acquire) {
            return false;
        }

        self.versions.read().contains_key(row_id)
    }

    /// Gets the latest visible version of a row
    pub fn get_visible_version(&self, row_id: i64, txn_id: i64) -> Option<RowVersion> {
        if self.closed.load(Ordering::Acquire) {
            return None;
        }

        let checker = self.visibility_checker.as_ref()?;

        // Phase 1: Speculative arena probe (arena lock only, self-contained).
        // For row_id N, probe arena slot N-1. Verify meta.row_id == row_id.
        // Hit rate ~100% for sequential-insert tables (arena slot reused on UPDATE).
        // Arena guard is acquired and dropped here to avoid lock ordering inversion
        // with the commit path (which holds versions.write → arena.write).
        if row_id > 0 {
            let probe_idx = (row_id - 1) as usize;
            let arena_guard = self.arena.read_guard();
            let arena_meta = arena_guard.meta();
            if probe_idx < arena_meta.len() {
                let meta = arena_meta[probe_idx];
                if meta.row_id == row_id && checker.is_visible(meta.txn_id, txn_id) {
                    if meta.deleted_at_txn_id != 0
                        && checker.is_visible(meta.deleted_at_txn_id, txn_id)
                    {
                        return None;
                    }
                    let data = Row::from_arc(CompactArc::clone(&arena_guard.data()[probe_idx]));
                    return Some(RowVersion {
                        txn_id: meta.txn_id,
                        deleted_at_txn_id: meta.deleted_at_txn_id,
                        data,
                        // Arena metadata doesn't store create_time (saves 8 bytes/row).
                        // All callers of get_visible_version() only use .data, .is_deleted(),
                        // and .txn_id. AS OF queries use get_visible_version_as_of_timestamp()
                        // which goes through CowBTree and has the real create_time.
                        create_time: 0,
                    });
                }
            }
            // arena_guard dropped here before acquiring versions lock
        }

        // Phase 2: CowBTree fallback (correct lock ordering: versions first, then arena)
        let versions = self.versions.read();
        let chain = versions.get(row_id)?;

        // Check HEAD visibility (most common case)
        let head_txn_id = chain.version.txn_id;
        let head_deleted_at = chain.version.deleted_at_txn_id;

        if checker.is_visible(head_txn_id, txn_id) {
            if head_deleted_at != 0 && checker.is_visible(head_deleted_at, txn_id) {
                return None;
            }
            // Use arena data via chain.arena_idx for O(1) Arc clone
            let row = if let Some(idx) = unpack_arena_idx(chain.arena_idx) {
                let arena_guard = self.arena.read_guard();
                if let Some(arc_row) = arena_guard.data().get(idx) {
                    Row::from_arc(CompactArc::clone(arc_row))
                } else {
                    chain.version.data.clone()
                }
            } else {
                chain.version.data.clone()
            };
            return Some(RowVersion {
                txn_id: head_txn_id,
                deleted_at_txn_id: head_deleted_at,
                data: row,
                create_time: chain.version.create_time,
            });
        }

        // Traverse version chain for older visible versions
        let mut current: Option<&VersionChainEntry> = chain.prev.as_ref().map(|b| b.as_ref());

        while let Some(e) = current {
            let version_txn_id = e.version.txn_id;
            let deleted_at_txn_id = e.version.deleted_at_txn_id;

            if checker.is_visible(version_txn_id, txn_id) {
                if deleted_at_txn_id != 0 && checker.is_visible(deleted_at_txn_id, txn_id) {
                    return None;
                }
                return Some(e.version.clone());
            }
            current = e.prev.as_ref().map(|b| b.as_ref());
        }

        None
    }

    /// Check if any of the given row_ids have a visible version
    /// Returns the first row_id that has a visible version, or None if none exist
    /// OPTIMIZATION: Used for conflict detection - stops at first hit, no data fetch
    #[inline]
    pub fn has_any_visible_version(&self, row_ids: &[i64], txn_id: i64) -> Option<i64> {
        if self.closed.load(Ordering::Acquire) || row_ids.is_empty() {
            return None;
        }

        let checker = self.visibility_checker.as_ref()?;

        // Lock ordering: versions first, then arena (matches commit path)
        let versions = self.versions.read();
        let arena_guard = self.arena.read_guard();
        let arena_meta = arena_guard.meta();

        for &row_id in row_ids {
            // Speculative arena probe: O(1) for auto-increment PKs
            if row_id > 0 {
                let probe_idx = (row_id - 1) as usize;
                if probe_idx < arena_meta.len() {
                    let meta = arena_meta[probe_idx];
                    if meta.row_id == row_id && checker.is_visible(meta.txn_id, txn_id) {
                        if meta.deleted_at_txn_id == 0
                            || !checker.is_visible(meta.deleted_at_txn_id, txn_id)
                        {
                            return Some(row_id); // Conflict!
                        }
                        continue; // Deleted, check next
                    }
                }
            }

            // CowBTree lookup: O(log n)
            if let Some(chain) = versions.get(row_id) {
                let head_txn_id = chain.version.txn_id;
                let head_deleted_at = chain.version.deleted_at_txn_id;

                if checker.is_visible(head_txn_id, txn_id) {
                    if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                        return Some(row_id); // Found visible version - conflict!
                    }
                    continue; // Deleted, check next
                }

                // Check chain for older visible versions
                let mut current: Option<&VersionChainEntry> =
                    chain.prev.as_ref().map(|b| b.as_ref());

                while let Some(e) = current {
                    if checker.is_visible(e.version.txn_id, txn_id) {
                        if e.version.deleted_at_txn_id == 0
                            || !checker.is_visible(e.version.deleted_at_txn_id, txn_id)
                        {
                            return Some(row_id); // Found visible version - conflict!
                        }
                        break; // Deleted, move to next row
                    }
                    current = e.prev.as_ref().map(|b| b.as_ref());
                }
            }
        }
        None
    }

    /// Gets multiple visible versions in a single batch operation
    ///
    /// Pre-acquires all locks once, then performs per-key CowBTree lookups
    /// with visibility checking and version chain traversal as needed.
    pub fn get_visible_versions_batch(&self, row_ids: &[i64], txn_id: i64) -> RowVec {
        if self.closed.load(Ordering::Acquire) || row_ids.is_empty() {
            return RowVec::new();
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return RowVec::new(),
        };

        // Lock ordering: versions first, then arena (matches commit path)
        let versions = self.versions.read();
        let arena_guard = self.arena.read_guard();
        let arena_meta = arena_guard.meta();
        let arena_data = arena_guard.data();

        // Fast path: if both arena and version tree are empty, no rows can match.
        // This avoids iterating millions of phantom row_ids from volume-populated indexes.
        if arena_meta.is_empty() && versions.is_empty() {
            return RowVec::new();
        }

        // Don't over-allocate: most row_ids may be phantom (from volume indexes)
        let mut results = RowVec::with_capacity(row_ids.len().min(4096));

        for &row_id in row_ids {
            // Speculative arena probe: O(1) for auto-increment PKs
            if row_id > 0 {
                let probe_idx = (row_id - 1) as usize;
                if probe_idx < arena_meta.len() {
                    let meta = arena_meta[probe_idx];
                    if meta.row_id == row_id && checker.is_visible(meta.txn_id, txn_id) {
                        if meta.deleted_at_txn_id == 0
                            || !checker.is_visible(meta.deleted_at_txn_id, txn_id)
                        {
                            let row = Row::from_arc(CompactArc::clone(&arena_data[probe_idx]));
                            results.push((row_id, row));
                        }
                        continue; // Skip CowBTree lookup
                    }
                }
            }

            // CowBTree lookup: O(log n)
            if let Some(chain) = versions.get(row_id) {
                let head_txn_id = chain.version.txn_id;
                let head_deleted_at = chain.version.deleted_at_txn_id;

                if checker.is_visible(head_txn_id, txn_id) {
                    if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                        let row = if let Some(idx) = unpack_arena_idx(chain.arena_idx) {
                            if let Some(arc_row) = arena_data.get(idx) {
                                Row::from_arc(CompactArc::clone(arc_row))
                            } else {
                                chain.version.data.clone()
                            }
                        } else {
                            chain.version.data.clone()
                        };
                        results.push((row_id, row));
                    }
                    continue;
                }

                // Traverse version chain for older visible versions
                let mut current: Option<&VersionChainEntry> =
                    chain.prev.as_ref().map(|b| b.as_ref());
                while let Some(e) = current {
                    if checker.is_visible(e.version.txn_id, txn_id) {
                        if e.version.deleted_at_txn_id == 0
                            || !checker.is_visible(e.version.deleted_at_txn_id, txn_id)
                        {
                            let row = if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                                if let Some(arc_row) = arena_data.get(idx) {
                                    Row::from_arc(CompactArc::clone(arc_row))
                                } else {
                                    e.version.data.clone()
                                }
                            } else {
                                e.version.data.clone()
                            };
                            results.push((row_id, row));
                        }
                        break;
                    }
                    current = e.prev.as_ref().map(|b| b.as_ref());
                }
            }
        }
        results
    }

    /// Iterates visible versions for given row_ids with early termination support.
    ///
    /// Pre-acquires all locks ONCE, then performs per-key CowBTree lookups.
    /// Calls `callback(row_id, row_data)` for each visible row.
    /// Stops iteration if callback returns `false` (used for LIMIT).
    ///
    /// This is more efficient than calling get_visible_version() per row_id because:
    /// - Single lock acquisition for CowBTree + arena (not per-row)
    /// - Supports early termination (unlike get_visible_versions_batch which collects all)
    pub fn for_each_visible<F>(&self, row_ids: &[i64], txn_id: i64, mut callback: F)
    where
        F: FnMut(i64, Row) -> bool,
    {
        if self.closed.load(Ordering::Acquire) || row_ids.is_empty() {
            return;
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return,
        };

        // Lock ordering: versions first, then arena (matches commit path)
        let versions = self.versions.read();
        let arena_guard = self.arena.read_guard();
        let arena_meta = arena_guard.meta();
        let arena_data = arena_guard.data();

        for &row_id in row_ids {
            // Speculative arena probe: O(1) for auto-increment PKs
            if row_id > 0 {
                let probe_idx = (row_id - 1) as usize;
                if probe_idx < arena_meta.len() {
                    let meta = arena_meta[probe_idx];
                    if meta.row_id == row_id && checker.is_visible(meta.txn_id, txn_id) {
                        if meta.deleted_at_txn_id == 0
                            || !checker.is_visible(meta.deleted_at_txn_id, txn_id)
                        {
                            let row = Row::from_arc(CompactArc::clone(&arena_data[probe_idx]));
                            if !callback(row_id, row) {
                                return;
                            }
                        }
                        continue; // Skip CowBTree lookup
                    }
                }
            }

            // CowBTree lookup: O(log n)
            if let Some(chain) = versions.get(row_id) {
                let head_txn_id = chain.version.txn_id;
                let head_deleted_at = chain.version.deleted_at_txn_id;

                if checker.is_visible(head_txn_id, txn_id) {
                    if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                        let row = if let Some(idx) = unpack_arena_idx(chain.arena_idx) {
                            if let Some(arc_row) = arena_data.get(idx) {
                                Row::from_arc(CompactArc::clone(arc_row))
                            } else {
                                chain.version.data.clone()
                            }
                        } else {
                            chain.version.data.clone()
                        };
                        if !callback(row_id, row) {
                            return;
                        }
                    }
                    continue;
                }

                // Traverse version chain for older visible versions
                let mut current: Option<&VersionChainEntry> =
                    chain.prev.as_ref().map(|b| b.as_ref());
                while let Some(e) = current {
                    if checker.is_visible(e.version.txn_id, txn_id) {
                        if e.version.deleted_at_txn_id == 0
                            || !checker.is_visible(e.version.deleted_at_txn_id, txn_id)
                        {
                            let row = if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                                if let Some(arc_row) = arena_data.get(idx) {
                                    Row::from_arc(CompactArc::clone(arc_row))
                                } else {
                                    e.version.data.clone()
                                }
                            } else {
                                e.version.data.clone()
                            };
                            if !callback(row_id, row) {
                                return;
                            }
                        }
                        break;
                    }
                    current = e.prev.as_ref().map(|b| b.as_ref());
                }
            }
        }
    }

    /// Counts visible versions for batch operations (COUNT optimization)
    ///
    /// This is an optimized version of get_visible_versions_batch that only counts
    /// visible rows without cloning their data. Used for COUNT(*) subqueries.
    ///
    /// Uses parallel processing for large batches (>1000 row_ids) to leverage
    /// multiple CPU cores for visibility checking.
    pub fn count_visible_versions_batch(&self, row_ids: &[i64], txn_id: i64) -> usize {
        if self.closed.load(Ordering::Acquire) || row_ids.is_empty() {
            return 0;
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return 0,
        };

        /// Minimum batch size before enabling parallel processing
        #[cfg(feature = "parallel")]
        const PARALLEL_THRESHOLD: usize = 1000;
        /// Chunk size for parallel processing
        #[cfg(feature = "parallel")]
        const PARALLEL_CHUNK_SIZE: usize = 512;

        // Lock ordering: versions first, then arena (matches commit path)
        // Clone CowBTree for parallel path (O(1) Arc clone of root node)
        let versions = self.versions.read().clone();
        let arena_guard = self.arena.read_guard();
        let arena_meta = arena_guard.meta();

        #[cfg(feature = "parallel")]
        if row_ids.len() >= PARALLEL_THRESHOLD {
            return row_ids
                .par_chunks(PARALLEL_CHUNK_SIZE)
                .map(|chunk| {
                    let mut chunk_count = 0;
                    for &row_id in chunk {
                        // Speculative arena probe: O(1) for auto-increment PKs
                        if row_id > 0 {
                            let probe_idx = (row_id - 1) as usize;
                            if probe_idx < arena_meta.len() {
                                let meta = arena_meta[probe_idx];
                                if meta.row_id == row_id && checker.is_visible(meta.txn_id, txn_id)
                                {
                                    if meta.deleted_at_txn_id == 0
                                        || !checker.is_visible(meta.deleted_at_txn_id, txn_id)
                                    {
                                        chunk_count += 1;
                                    }
                                    continue;
                                }
                            }
                        }

                        // CowBTree fallback: O(log n)
                        if let Some(chain) = versions.get(row_id) {
                            let head_txn_id = chain.version.txn_id;
                            let head_deleted_at = chain.version.deleted_at_txn_id;

                            if checker.is_visible(head_txn_id, txn_id) {
                                if head_deleted_at == 0
                                    || !checker.is_visible(head_deleted_at, txn_id)
                                {
                                    chunk_count += 1;
                                }
                                continue;
                            }

                            let mut current: Option<&VersionChainEntry> =
                                chain.prev.as_ref().map(|b| b.as_ref());
                            while let Some(e) = current {
                                if checker.is_visible(e.version.txn_id, txn_id) {
                                    if e.version.deleted_at_txn_id == 0
                                        || !checker.is_visible(e.version.deleted_at_txn_id, txn_id)
                                    {
                                        chunk_count += 1;
                                    }
                                    break;
                                }
                                current = e.prev.as_ref().map(|b| b.as_ref());
                            }
                        }
                    }
                    chunk_count
                })
                .sum();
        }

        // Sequential path for small batches (or when parallel feature is disabled)
        let mut count = 0;
        for &row_id in row_ids {
            // Speculative arena probe: O(1) for auto-increment PKs
            if row_id > 0 {
                let probe_idx = (row_id - 1) as usize;
                if probe_idx < arena_meta.len() {
                    let meta = arena_meta[probe_idx];
                    if meta.row_id == row_id && checker.is_visible(meta.txn_id, txn_id) {
                        if meta.deleted_at_txn_id == 0
                            || !checker.is_visible(meta.deleted_at_txn_id, txn_id)
                        {
                            count += 1;
                        }
                        continue;
                    }
                }
            }

            // CowBTree fallback: O(log n)
            if let Some(chain) = versions.get(row_id) {
                let head_txn_id = chain.version.txn_id;
                let head_deleted_at = chain.version.deleted_at_txn_id;

                if checker.is_visible(head_txn_id, txn_id) {
                    if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                        count += 1;
                    }
                    continue;
                }

                let mut current: Option<&VersionChainEntry> =
                    chain.prev.as_ref().map(|b| b.as_ref());
                while let Some(e) = current {
                    if checker.is_visible(e.version.txn_id, txn_id) {
                        if e.version.deleted_at_txn_id == 0
                            || !checker.is_visible(e.version.deleted_at_txn_id, txn_id)
                        {
                            count += 1;
                        }
                        break;
                    }
                    current = e.prev.as_ref().map(|b| b.as_ref());
                }
            }
        }
        count
    }

    /// Gets visible versions for batch update operations
    ///
    /// Returns (row_id, row_data, original_version) for each visible row.
    /// The original_version is used for write-set tracking to avoid redundant lookups.
    ///
    /// This is optimized for UPDATE operations where we need to:
    /// 1. Read the current row data
    /// 2. Track the original version for conflict detection
    /// 3. Skip redundant lookups during put
    pub fn get_visible_versions_for_update(
        &self,
        row_ids: &[i64],
        txn_id: i64,
    ) -> Vec<(i64, Row, RowVersion)> {
        if self.closed.load(Ordering::Acquire) {
            return Vec::new();
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return Vec::new(),
        };

        let current_seq = checker.get_current_sequence();
        let mut results = Vec::with_capacity(row_ids.len());

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock for O(1) Arc clones
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Helper: get row from arena (O(1)) or version (O(n) clone)
        let get_row = |entry: &VersionChainEntry| -> Row {
            if let Some(idx) = unpack_arena_idx(entry.arena_idx) {
                if let Some(arc_row) = arena_data.get(idx) {
                    return Row::from_arc(CompactArc::clone(arc_row));
                }
            }
            entry.version.data.clone()
        };
        for &row_id in row_ids {
            if let Some(chain) = versions.get(row_id) {
                // FAST PATH: Check HEAD version first - O(1) for common case
                let head_txn_id = chain.version.txn_id;
                let head_deleted_at = chain.version.deleted_at_txn_id;

                if checker.is_visible(head_txn_id, txn_id) {
                    // HEAD is visible - check if deleted
                    if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                        let mut version_copy = chain.version.clone();
                        version_copy.create_time = current_seq;
                        results.push((row_id, get_row(chain), version_copy));
                    }
                    continue;
                }

                // SLOW PATH: HEAD not visible - traverse chain for older versions
                let mut current: Option<&VersionChainEntry> =
                    chain.prev.as_ref().map(|b| b.as_ref());
                while let Some(e) = current {
                    let version_txn_id = e.version.txn_id;
                    let deleted_at_txn_id = e.version.deleted_at_txn_id;

                    if checker.is_visible(version_txn_id, txn_id) {
                        if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id)
                        {
                            // Create a RowVersion with the correct read_version_seq for tracking
                            let mut version_copy = e.version.clone();
                            // Store the current sequence in create_time for later retrieval
                            // (This is a bit of a hack, but avoids changing the struct)
                            version_copy.create_time = current_seq;
                            results.push((row_id, get_row(e), version_copy));
                        }
                        break;
                    }
                    current = e.prev.as_ref().map(|b| b.as_ref());
                }
            }
        }

        results
    }

    /// Gets the current sequence number for write-set tracking
    pub fn get_current_sequence(&self) -> i64 {
        self.visibility_checker
            .as_ref()
            .map(|c| c.get_current_sequence())
            .unwrap_or(0)
    }

    /// Gets the visible version as of a specific transaction
    pub fn get_visible_version_as_of_transaction(
        &self,
        row_id: i64,
        as_of_txn_id: i64,
    ) -> Option<RowVersion> {
        if self.closed.load(Ordering::Acquire) {
            return None;
        }

        // No need to clone tree for single-row lookup - just hold read guard
        let versions = self.versions.read();
        let chain = versions.get(row_id)?;

        // Traverse version chain from newest to oldest
        let mut current: Option<&VersionChainEntry> = Some(chain);
        while let Some(e) = current {
            // Check if this version was created before or at the asOf transaction
            if e.version.txn_id <= as_of_txn_id {
                // Check if deleted before or at asOfTxnID
                if e.version.deleted_at_txn_id != 0 && e.version.deleted_at_txn_id <= as_of_txn_id {
                    return None;
                }
                return Some(e.version.clone());
            }
            current = e.prev.as_ref().map(|b| b.as_ref());
        }

        None
    }

    /// Gets the visible version as of a specific timestamp
    pub fn get_visible_version_as_of_timestamp(
        &self,
        row_id: i64,
        as_of_timestamp: i64,
    ) -> Option<RowVersion> {
        if self.closed.load(Ordering::Acquire) {
            return None;
        }

        // No need to clone tree for single-row lookup - just hold read guard
        let versions = self.versions.read();
        let chain = versions.get(row_id)?;

        // Traverse version chain from newest to oldest
        let mut current: Option<&VersionChainEntry> = Some(chain);
        while let Some(e) = current {
            // Check if this version was created before or at the asOf timestamp
            if e.version.create_time <= as_of_timestamp {
                // Check if deleted (we can't easily determine timestamp of deletion in this model)
                // For now, check if DeletedAtTxnID is set
                if e.version.deleted_at_txn_id != 0 {
                    return None;
                }
                return Some(e.version.clone());
            }
            current = e.prev.as_ref().map(|b| b.as_ref());
        }

        None
    }

    /// Returns all row IDs in the version store (sorted)
    pub fn get_all_row_ids(&self) -> Vec<i64> {
        if self.closed.load(Ordering::Acquire) {
            return Vec::new();
        }

        // Clone tree to avoid holding lock for the duration of iteration
        let versions = self.versions.read().clone();
        versions.keys().collect()
    }

    /// Populate a FxHashSet with all hot row_ids. Iterates the B-tree under
    /// read lock without cloning — much cheaper than get_all_row_ids() + collect()
    /// for skip-set construction.
    pub fn collect_row_ids_into(&self, dest: &mut rustc_hash::FxHashSet<i64>) {
        if self.closed.load(Ordering::Acquire) {
            return;
        }
        let versions = self.versions.read();
        for key in versions.keys() {
            dest.insert(key);
        }
    }

    /// Returns all row IDs that are visible to the given transaction
    ///
    /// OPTIMIZATION: Single-pass iteration with O(1) lock acquisition instead of O(N).
    /// Uses the same HEAD-first visibility pattern as count_visible_rows.
    pub fn get_all_visible_row_ids(&self, txn_id: i64) -> Vec<i64> {
        if self.closed.load(Ordering::Acquire) {
            return Vec::new();
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return Vec::new(),
        };

        // FAST PATH: Scan arena directly when no uncommitted writes.
        // SAFETY: Only valid under ReadCommitted (see get_visible_row_indices).
        {
            let uncommitted_empty = self.uncommitted_writes.read().is_empty();
            if uncommitted_empty && !checker.needs_snapshot_isolation(txn_id) {
                let arena_guard = self.arena.read_guard();
                let arena_meta = arena_guard.meta();
                let arena_len = arena_guard.len();

                if arena_len > 0 {
                    let mut visible_row_ids = Vec::with_capacity(arena_len);
                    for meta in arena_meta {
                        if meta.txn_id != 0
                            && meta.deleted_at_txn_id == 0
                            && checker.is_visible(meta.txn_id, txn_id)
                        {
                            visible_row_ids.push(meta.row_id);
                        }
                    }
                    return visible_row_ids;
                }
            }
        }

        // SLOW PATH: Full CowBTree iteration
        let versions = self.versions.read().clone();
        let mut visible_row_ids = Vec::with_capacity(versions.len());

        for (&row_id, chain) in versions.iter() {
            // FAST PATH: Check HEAD version first - O(1) for common case
            let head_txn_id = chain.version.txn_id;
            let head_deleted_at = chain.version.deleted_at_txn_id;

            if checker.is_visible(head_txn_id, txn_id) {
                // HEAD is visible - check if deleted
                if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                    visible_row_ids.push(row_id);
                }
                continue;
            }

            // SLOW PATH: HEAD not visible - traverse chain for older versions
            let mut current: Option<&VersionChainEntry> = chain.prev.as_ref().map(|b| b.as_ref());
            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                if checker.is_visible(version_txn_id, txn_id) {
                    if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
                        visible_row_ids.push(row_id);
                    }
                    break;
                }
                current = e.prev.as_ref().map(|b| b.as_ref());
            }
        }

        visible_row_ids
    }

    /// Count visible non-deleted rows in a single pass (optimized for row_count)
    ///
    /// OPTIMIZATION: This method counts rows in O(1) lock acquisition instead of O(N)
    /// by iterating through versions once without cloning any row data.
    pub fn count_visible_rows(&self, txn_id: i64) -> usize {
        if self.closed.load(Ordering::Acquire) {
            return 0;
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return 0,
        };

        // FAST PATH: Scan arena directly when no uncommitted writes.
        // SAFETY: Only valid under ReadCommitted (see get_visible_row_indices).
        {
            let uncommitted_empty = self.uncommitted_writes.read().is_empty();
            if uncommitted_empty && !checker.needs_snapshot_isolation(txn_id) {
                let arena_guard = self.arena.read_guard();
                let arena_meta = arena_guard.meta();
                let arena_len = arena_guard.len();

                if arena_len > 0 {
                    let mut count = 0usize;
                    for meta in arena_meta {
                        if meta.txn_id != 0
                            && meta.deleted_at_txn_id == 0
                            && checker.is_visible(meta.txn_id, txn_id)
                        {
                            count += 1;
                        }
                    }
                    return count;
                }
            }
        }

        // SLOW PATH: Full CowBTree iteration
        let mut count = 0;
        let versions = self.versions.read().clone();
        for chain in versions.values() {
            // FAST PATH: Check HEAD version first - O(1) for common case
            let head_txn_id = chain.version.txn_id;
            let head_deleted_at = chain.version.deleted_at_txn_id;

            if checker.is_visible(head_txn_id, txn_id) {
                // HEAD is visible - check if deleted
                if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                    count += 1;
                }
                continue;
            }

            // SLOW PATH: HEAD not visible - traverse chain for older versions
            let mut current: Option<&VersionChainEntry> = chain.prev.as_ref().map(|b| b.as_ref());
            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                if checker.is_visible(version_txn_id, txn_id) {
                    if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
                        count += 1;
                    }
                    break;
                }
                current = e.prev.as_ref().map(|b| b.as_ref());
            }
        }

        count
    }

    /// Returns the count of committed non-deleted rows (O(1) operation)
    ///
    /// This is the fast path for COUNT(*) queries without WHERE clause.
    /// The counter is updated atomically on INSERT commit (+1) and DELETE commit (-1).
    ///
    /// # Important
    ///
    /// This count does NOT include uncommitted changes from the current transaction.
    /// Use this only for queries that see committed data (e.g., autocommit queries).
    #[inline]
    pub fn committed_row_count(&self) -> usize {
        self.committed_row_count.load(Ordering::Relaxed)
    }

    /// Check if a row_id exists in the committed version store (B-tree).
    /// Used during WAL replay to distinguish sealed INSERTs from post-seal UPDATEs.
    pub fn has_committed_row(&self, row_id: i64) -> bool {
        self.versions.read().contains_key(row_id)
    }

    /// Atomically subtract sealed rows from committed_row_count.
    ///
    /// After seal batch-removes rows from the B-tree, committed_row_count must
    /// decrease by the sealed amount. Using fetch_sub (not store) preserves
    /// concurrent fetch_add/fetch_sub from other threads committing INSERTs or
    /// DELETEs during the seal window. A plain store() would race with those
    /// concurrent updates, causing the counter to drift by ~N (where N is the
    /// number of rows committed during the seal operation).
    pub fn subtract_committed_row_count(&self, sealed: usize) {
        // Use saturating arithmetic to avoid wrapping if concurrent deletes
        // already reduced the counter below the sealed amount.
        loop {
            let current = self.committed_row_count.load(Ordering::Relaxed);
            let new_val = current.saturating_sub(sealed);
            match self.committed_row_count.compare_exchange_weak(
                current,
                new_val,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(_) => continue,
            }
        }
    }

    /// Check if a transaction requires snapshot isolation visibility checks.
    /// When true, the O(1) committed_row_count is inaccurate because it includes
    /// rows committed after this transaction's snapshot point.
    #[inline]
    pub fn needs_snapshot_isolation(&self, txn_id: i64) -> bool {
        self.visibility_checker
            .as_ref()
            .is_some_and(|c| c.needs_snapshot_isolation(txn_id))
    }

    /// Clears all data in O(1) for TRUNCATE.
    /// Drops all versions, arena data, indexes, and resets row count.
    /// Returns the number of rows that were truncated.
    ///
    /// **Non-rollbackable**: Like MySQL/Oracle, data is physically destroyed
    /// immediately with no undo log. This is O(1) vs O(N) for DELETE.
    /// PostgreSQL/SQL Server support rollbackable TRUNCATE by deferring the
    /// physical destruction to commit time, but that adds complexity to every
    /// read path (13+ code locations). Use `DELETE FROM table` for rollback.
    ///
    /// Fails with `TableHasActiveTransactions` if another transaction holds
    /// uncommitted UPDATE/DELETE claims on this table.
    pub fn truncate_all(&self) -> crate::core::Result<i32> {
        // Hold uncommitted_writes(W) for the ENTIRE check-and-clear sequence
        // to prevent TOCTOU race: without this, a concurrent try_claim_row()
        // could add a claim between the check and the clear, and truncate would
        // silently destroy it — causing a ghost row when the UPDATE commits.
        //
        // Lock ordering: uncommitted_writes(W) → versions(W) → arena(W).
        // This is safe because all read methods that use uncommitted_writes
        // check it BEFORE acquiring arena(R), so there is no circular dependency:
        //   - Read paths: uncommitted_writes(R) [brief, dropped] → arena(R)
        //   - Commit path: versions(W) → arena(W) (never uncommitted_writes(W))
        //   - try_claim_row: uncommitted_writes(W) only — blocked by our lock
        let mut uncommitted = self.uncommitted_writes.write();
        if !uncommitted.is_empty() {
            return Err(crate::core::Error::TableHasActiveTransactions);
        }

        // Hold versions(W) while clearing BOTH versions AND arena atomically.
        // INSERT commits acquire versions(W) → arena(W) in the same order,
        // so this blocks them and prevents a race where a commit inserts a
        // version pointing to an arena slot that we then clear.
        // Reset committed_row_count inside the lock so COUNT(*) never sees 0
        // while data still exists.
        let count;
        {
            let mut versions = self.versions.write();
            count = self.committed_row_count.swap(0, Ordering::SeqCst) as i32;
            versions.clear();
            self.arena.clear_all();
        }

        // Clear uncommitted_writes (already held as write lock)
        uncommitted.clear();
        drop(uncommitted);

        // 5. Clear all indexes
        let indexes = self.indexes.read();
        for index in indexes.values() {
            let _ = index.clear();
        }

        // 6. Reset auto-increment counter so new rows start at 1
        //    and the speculative arena probe ((row_id - 1) as usize) stays valid
        self.auto_increment_counter.store(0, Ordering::Release);

        // 7. Invalidate zone maps (stale after truncate)
        *self.zone_maps.write() = None;

        Ok(count)
    }

    /// Returns all visible rows for a transaction (optimized batch operation)
    ///
    /// This is more efficient than calling get_visible_version for each row
    /// because it batches the visibility checks and avoids repeated map lookups.
    /// Results are sorted by row_id.
    #[inline]
    pub fn get_all_visible_rows(&self, txn_id: i64) -> RowVec {
        self.get_all_visible_rows_internal(txn_id)
    }

    /// Extract visible rows AND the CowBTree snapshot at extraction time.
    /// Used by seal: the snapshot records each row's `txn_id` so that
    /// `remove_sealed_rows` can detect concurrent commits and skip them.
    pub fn extract_for_seal(&self, txn_id: i64) -> (RowVec, ExtractionSnapshot) {
        let snapshot = self.versions.read().clone();
        let rows = self.get_all_visible_rows_internal(txn_id);
        (rows, ExtractionSnapshot { inner: snapshot })
    }

    /// Extract committed rows for seal, filtered by commit_seq cutoff.
    /// Only includes rows committed before `commit_seq_cutoff`, ensuring
    /// snapshot isolation transactions that began before the cutoff can still
    /// see those rows after they move to cold storage.
    pub fn extract_for_seal_with_cutoff(
        &self,
        commit_seq_cutoff: i64,
    ) -> (RowVec, ExtractionSnapshot) {
        let snapshot = self.versions.read().clone();
        let mut rows = RowVec::with_capacity(self.committed_row_count());
        self.for_each_committed_version_with_cutoff(
            |row_id, version| {
                rows.push((row_id, version.data.clone()));
                true
            },
            commit_seq_cutoff,
        );
        (rows, ExtractionSnapshot { inner: snapshot })
    }

    /// Internal implementation for getting all visible rows
    #[inline]
    fn get_all_visible_rows_internal(&self, txn_id: i64) -> RowVec {
        if self.closed.load(Ordering::Acquire) {
            return RowVec::new();
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return RowVec::new(),
        };

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock for O(1) Arc clones
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Helper: get row from arena (O(1)) or version (O(n) clone)
        let get_row = |entry: &VersionChainEntry| -> Row {
            if let Some(idx) = unpack_arena_idx(entry.arena_idx) {
                if let Some(arc_row) = arena_data.get(idx) {
                    return Row::from_arc(CompactArc::clone(arc_row));
                }
            }
            entry.version.data.clone()
        };
        let mut results = RowVec::with_capacity(versions.len());

        for (&row_id, chain) in versions.iter() {
            // FAST PATH: Check HEAD version first - O(1) for common case
            let head_txn_id = chain.version.txn_id;
            let head_deleted_at = chain.version.deleted_at_txn_id;

            if checker.is_visible(head_txn_id, txn_id) {
                // HEAD is visible - check if deleted
                if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                    results.push((row_id, get_row(chain)));
                }
                continue;
            }

            // SLOW PATH: HEAD not visible - traverse chain for older versions
            let mut current: Option<&VersionChainEntry> = chain.prev.as_ref().map(|b| b.as_ref());
            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                if checker.is_visible(version_txn_id, txn_id) {
                    if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
                        results.push((row_id, get_row(e)));
                    }
                    break;
                }
                current = e.prev.as_ref().map(|b| b.as_ref());
            }
        }

        results
    }

    /// Returns all visible rows using arena for zero-copy scanning
    ///
    /// This method provides 50x+ faster full table scans by:
    /// 1. Pre-acquiring arena locks once
    /// 2. Reading directly during visibility iteration (single pass)
    /// 3. Using contiguous arena memory for cache locality
    #[inline]
    pub fn get_all_visible_rows_arena(&self, txn_id: i64) -> RowVec {
        if self.closed.load(Ordering::Acquire) {
            return RowVec::new();
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return RowVec::new(),
        };

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock ONCE for the entire operation
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Helper closure to get row data from arena or version
        let get_row_data = |e: &VersionChainEntry| -> Row {
            if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                if let Some(arc_row) = arena_data.get(idx) {
                    return Row::from_arc(CompactArc::clone(arc_row));
                }
            }
            e.version.data.clone()
        };

        // Single-pass: read directly from arena during visibility check
        let mut result = RowVec::with_capacity(versions.len());

        for (&row_id, chain) in versions.iter() {
            // FAST PATH: Check HEAD version first - O(1) for common case
            let head_txn_id = chain.version.txn_id;
            let head_deleted_at = chain.version.deleted_at_txn_id;

            if checker.is_visible(head_txn_id, txn_id) {
                // HEAD is visible - check if deleted
                if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                    result.push((row_id, get_row_data(chain)));
                }
                continue;
            }

            // SLOW PATH: HEAD not visible - traverse chain for older versions
            let mut current: Option<&VersionChainEntry> = chain.prev.as_ref().map(|b| b.as_ref());
            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                if checker.is_visible(version_txn_id, txn_id) {
                    if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
                        result.push((row_id, get_row_data(e)));
                    }
                    break;
                }
                current = e.prev.as_ref().map(|b| b.as_ref());
            }
        }

        result
    }

    /// Returns all visible rows using RowVec for zero-allocation reuse.
    ///
    /// Same as `get_all_visible_rows_arena` but uses cached RowVec.
    /// The returned `RowVec` auto-returns to cache on drop.
    #[inline]
    pub fn get_all_visible_rows_cached(&self, txn_id: i64) -> RowVec {
        let mut result = RowVec::new();

        if self.closed.load(Ordering::Acquire) {
            return result;
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return result,
        };

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock ONCE for the entire operation
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Helper closure to get row data from arena or version
        let get_row_data = |e: &VersionChainEntry| -> Row {
            if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                if let Some(arc_row) = arena_data.get(idx) {
                    return Row::from_arc(CompactArc::clone(arc_row));
                }
            }
            e.version.data.clone()
        };

        // Single-pass: read directly from arena during visibility check

        // Ensure capacity
        let current_capacity = result.capacity();
        let needed = versions.len();
        if current_capacity < needed {
            result.reserve(needed - current_capacity);
        }

        for (&row_id, chain) in versions.iter() {
            // FAST PATH: Check HEAD version first - O(1) for common case
            let head_txn_id = chain.version.txn_id;
            let head_deleted_at = chain.version.deleted_at_txn_id;

            if checker.is_visible(head_txn_id, txn_id) {
                // HEAD is visible - check if deleted
                if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                    result.push((row_id, get_row_data(chain)));
                }
                continue;
            }

            // SLOW PATH: HEAD not visible - traverse chain for older versions
            let mut current: Option<&VersionChainEntry> = chain.prev.as_ref().map(|b| b.as_ref());
            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                if checker.is_visible(version_txn_id, txn_id) {
                    if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
                        result.push((row_id, get_row_data(e)));
                    }
                    break;
                }
                current = e.prev.as_ref().map(|b| b.as_ref());
            }
        }

        result
    }

    /// Returns all visible rows with their original RowVersions for UPDATE operations.
    ///
    /// This is optimized for UPDATE operations that need to track the original version
    /// for conflict detection. By returning the RowVersion along with the row data,
    /// callers can use `put_batch_with_originals()` to avoid redundant `get_visible_version()`
    /// calls during the put phase.
    ///
    /// Returns: Vec of (row_id, row_data, original_version) tuples.
    /// Note: read_version_seq is obtained from visibility_checker, not from create_time.
    #[inline]
    pub fn get_all_visible_rows_for_update(&self, txn_id: i64) -> Vec<(i64, Row, RowVersion)> {
        if self.closed.load(Ordering::Acquire) {
            return Vec::new();
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return Vec::new(),
        };

        let current_seq = checker.get_current_sequence();

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock ONCE for the entire operation
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Helper closure to get row data from arena or version
        let get_row_data = |e: &VersionChainEntry| -> Row {
            if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                if let Some(arc_row) = arena_data.get(idx) {
                    return Row::from_arc(CompactArc::clone(arc_row));
                }
            }
            e.version.data.clone()
        };

        // Single-pass: read directly from arena during visibility check
        let mut result: Vec<(i64, Row, RowVersion)> = Vec::with_capacity(versions.len());

        for (&row_id, chain) in versions.iter() {
            // FAST PATH: Check HEAD version first - O(1) for common case
            let head_txn_id = chain.version.txn_id;
            let head_deleted_at = chain.version.deleted_at_txn_id;

            if checker.is_visible(head_txn_id, txn_id) {
                // HEAD is visible - check if deleted
                if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                    let mut version_copy = chain.version.clone();
                    version_copy.create_time = current_seq;
                    result.push((row_id, get_row_data(chain), version_copy));
                }
                continue;
            }

            // SLOW PATH: HEAD not visible - traverse chain for older versions
            let mut current: Option<&VersionChainEntry> = chain.prev.as_ref().map(|b| b.as_ref());
            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                if checker.is_visible(version_txn_id, txn_id) {
                    if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
                        let mut version_copy = e.version.clone();
                        version_copy.create_time = current_seq;
                        result.push((row_id, get_row_data(e), version_copy));
                    }
                    break;
                }
                current = e.prev.as_ref().map(|b| b.as_ref());
            }
        }

        result
    }

    /// Get all visible rows for UPDATE with filter applied BEFORE cloning.
    ///
    /// This is a performance optimization for UPDATE operations with WHERE clauses.
    /// Instead of fetching all rows and then filtering, we filter during the scan
    /// to avoid allocating Row objects for non-matching rows.
    ///
    /// Returns (row_id, Row, RowVersion) tuples - the RowVersion is needed for
    /// MVCC conflict detection during the update.
    pub fn get_all_visible_rows_for_update_filtered(
        &self,
        txn_id: i64,
        filter: &dyn crate::storage::expression::Expression,
    ) -> Vec<(i64, Row, RowVersion)> {
        if self.closed.load(Ordering::Acquire) {
            return Vec::new();
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return Vec::new(),
        };

        let current_seq = checker.get_current_sequence();

        // Compile the filter once at the start for speedup in hot loop
        let schema = self.schema.read();
        let compiled_filter = CompiledFilter::compile(filter, &schema);
        drop(schema); // Release lock early

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock ONCE for the entire operation
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Single-pass: read, filter, and collect in one loop
        let mut result: Vec<(i64, Row, RowVersion)> = Vec::with_capacity(versions.len() / 4);

        for (&row_id, chain) in versions.iter() {
            // FAST PATH: Check HEAD version first - O(1) for common case
            let head_txn_id = chain.version.txn_id;
            let head_deleted_at = chain.version.deleted_at_txn_id;

            if checker.is_visible(head_txn_id, txn_id) {
                // HEAD is visible - check if deleted
                if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                    // Try arena path first (zero-copy filter check using matches_arc_slice)
                    if let Some(idx) = unpack_arena_idx(chain.arena_idx) {
                        if let Some(arc_row) = arena_data.get(idx) {
                            // Filter directly on Arc slice - no Row allocation for non-matching rows
                            if compiled_filter.matches_arc_slice(arc_row.as_ref()) {
                                let mut version_copy = chain.version.clone();
                                version_copy.create_time = current_seq;
                                result.push((
                                    row_id,
                                    Row::from_arc(CompactArc::clone(arc_row)),
                                    version_copy,
                                ));
                            }
                            continue;
                        }
                    }
                    // Fallback: filter on version data
                    if compiled_filter.matches(&chain.version.data) {
                        let mut version_copy = chain.version.clone();
                        version_copy.create_time = current_seq;
                        result.push((row_id, chain.version.data.clone(), version_copy));
                    }
                }
                continue;
            }

            // SLOW PATH: HEAD not visible - traverse chain for older versions
            let mut current: Option<&VersionChainEntry> = chain.prev.as_ref().map(|b| b.as_ref());
            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                if checker.is_visible(version_txn_id, txn_id) {
                    if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
                        // Try arena path first (zero-copy filter check using matches_arc_slice)
                        if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                            if let Some(arc_row) = arena_data.get(idx) {
                                // Filter directly on Arc slice - no Row allocation for non-matching rows
                                if compiled_filter.matches_arc_slice(arc_row.as_ref()) {
                                    let mut version_copy = e.version.clone();
                                    version_copy.create_time = current_seq;
                                    result.push((
                                        row_id,
                                        Row::from_arc(CompactArc::clone(arc_row)),
                                        version_copy,
                                    ));
                                }
                                break;
                            }
                        }
                        // Fallback: filter on version data
                        if compiled_filter.matches(&e.version.data) {
                            let mut version_copy = e.version.clone();
                            version_copy.create_time = current_seq;
                            result.push((row_id, e.version.data.clone(), version_copy));
                        }
                    }
                    break;
                }
                current = e.prev.as_ref().map(|b| b.as_ref());
            }
        }

        result
    }

    /// Returns all visible rows.
    ///
    /// Note: Functionally identical to get_all_visible_rows_arena (iteration is ordered).
    #[inline]
    pub fn get_all_visible_rows_unsorted(&self, txn_id: i64) -> RowVec {
        if self.closed.load(Ordering::Acquire) {
            return RowVec::new();
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return RowVec::new(),
        };

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock ONCE for the entire operation
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Helper closure to get row data from arena or version
        let get_row_data = |e: &VersionChainEntry| -> Row {
            if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                if let Some(arc_row) = arena_data.get(idx) {
                    return Row::from_arc(CompactArc::clone(arc_row));
                }
            }
            e.version.data.clone()
        };

        // Single-pass: read directly from arena during visibility check
        let mut result = RowVec::with_capacity(versions.len());

        for (&row_id, chain) in versions.iter() {
            // FAST PATH: Check HEAD version first - O(1) for common case
            let head_txn_id = chain.version.txn_id;
            let head_deleted_at = chain.version.deleted_at_txn_id;

            if checker.is_visible(head_txn_id, txn_id) {
                // HEAD is visible - check if deleted
                if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                    result.push((row_id, get_row_data(chain)));
                }
                continue;
            }

            // SLOW PATH: HEAD not visible - traverse chain for older versions
            let mut current: Option<&VersionChainEntry> = chain.prev.as_ref().map(|b| b.as_ref());
            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                if checker.is_visible(version_txn_id, txn_id) {
                    if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
                        result.push((row_id, get_row_data(e)));
                    }
                    break;
                }
                current = e.prev.as_ref().map(|b| b.as_ref());
            }
        }

        result
    }

    /// Get visible rows with limit and offset applied at the storage layer.
    ///
    /// # True Early Termination
    /// Iteration is ordered by row_id, enabling true
    /// early termination: skip `offset` visible rows, then collect `limit` rows
    /// and stop iterating.
    ///
    /// # Arguments
    /// * `txn_id` - Transaction ID for visibility check
    /// * `limit` - Maximum number of rows to return
    /// * `offset` - Number of rows to skip before collecting
    pub fn get_visible_rows_with_limit(&self, txn_id: i64, limit: usize, offset: usize) -> RowVec {
        if self.closed.load(Ordering::Acquire) || limit == 0 {
            return RowVec::new();
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return RowVec::new(),
        };

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock ONCE for the entire operation
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Helper closure to get row data from arena or version
        let get_row_data = |e: &VersionChainEntry| -> Row {
            if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                if let Some(arc_row) = arena_data.get(idx) {
                    return Row::from_arc(CompactArc::clone(arc_row));
                }
            }
            e.version.data.clone()
        };

        // Collect with early termination
        let mut result = RowVec::with_capacity(limit);
        let mut skipped = 0usize;

        for (&row_id, chain) in versions.iter() {
            // FAST PATH: Check HEAD version first - O(1) for common case
            let head_txn_id = chain.version.txn_id;
            let head_deleted_at = chain.version.deleted_at_txn_id;

            // Track the actual visible entry, not just a boolean
            let visible_entry: Option<&VersionChainEntry> = if checker
                .is_visible(head_txn_id, txn_id)
            {
                // HEAD is visible - check if deleted
                if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                    Some(chain)
                } else {
                    None
                }
            } else {
                // SLOW PATH: HEAD not visible - traverse chain for older versions
                let mut found_entry: Option<&VersionChainEntry> = None;
                let mut current: Option<&VersionChainEntry> =
                    chain.prev.as_ref().map(|b| b.as_ref());
                while let Some(e) = current {
                    let version_txn_id = e.version.txn_id;
                    let deleted_at_txn_id = e.version.deleted_at_txn_id;

                    if checker.is_visible(version_txn_id, txn_id) {
                        if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id)
                        {
                            found_entry = Some(e);
                        }
                        break;
                    }
                    current = e.prev.as_ref().map(|b| b.as_ref());
                }
                found_entry
            };

            if let Some(entry) = visible_entry {
                if skipped < offset {
                    skipped += 1;
                } else {
                    result.push((row_id, get_row_data(entry)));
                    if result.len() >= limit {
                        break; // Early termination!
                    }
                }
            }
        }

        result
    }

    /// Get visible rows with LIMIT (with early termination).
    ///
    /// Note: Functionally identical to get_visible_rows_with_limit. Kept for API compatibility.
    #[inline]
    pub fn get_visible_rows_with_limit_unordered(
        &self,
        txn_id: i64,
        limit: usize,
        offset: usize,
    ) -> RowVec {
        // Delegate to the sorted version
        self.get_visible_rows_with_limit(txn_id, limit, offset)
    }

    /// Get a batch of visible rows starting after a given row_id (cursor-based pagination).
    ///
    /// This is designed for lazy/streaming scanners that fetch rows in batches.
    /// Uses range() for efficient cursor-based iteration.
    ///
    /// # Arguments
    /// * `txn_id` - Transaction ID for visibility check
    /// * `after_row_id` - Start fetching rows AFTER this row_id (use i64::MIN to start from beginning)
    /// * `batch_size` - Maximum number of rows to return in this batch
    ///
    /// # Returns
    /// A tuple of (rows, has_more) where has_more indicates if there are more rows to fetch
    pub fn get_visible_rows_batch(
        &self,
        txn_id: i64,
        after_row_id: i64,
        batch_size: usize,
    ) -> (RowVec, bool) {
        if self.closed.load(Ordering::Acquire) || batch_size == 0 {
            return (RowVec::new(), false);
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return (RowVec::new(), false),
        };

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock ONCE for this batch
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Helper closure to get row data from arena or version
        let get_row_data = |e: &VersionChainEntry| -> Row {
            if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                if let Some(arc_row) = arena_data.get(idx) {
                    return Row::from_arc(CompactArc::clone(arc_row));
                }
            }
            e.version.data.clone()
        };

        // Use range for efficient cursor-based iteration
        let mut result = RowVec::with_capacity(batch_size);
        let mut has_more = false;

        // Use range to start after the cursor row_id
        for (&row_id, chain) in versions.range((
            std::ops::Bound::Excluded(after_row_id),
            std::ops::Bound::Unbounded::<i64>,
        )) {
            // FAST PATH: Check HEAD version first - O(1) for common case
            let head_txn_id = chain.version.txn_id;
            let head_deleted_at = chain.version.deleted_at_txn_id;

            // Track the actual visible entry, not just a boolean
            let visible_entry: Option<&VersionChainEntry> = if checker
                .is_visible(head_txn_id, txn_id)
            {
                // HEAD is visible - check if deleted
                if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                    Some(chain)
                } else {
                    None
                }
            } else {
                // SLOW PATH: HEAD not visible - traverse chain for older versions
                let mut found_entry: Option<&VersionChainEntry> = None;
                let mut current: Option<&VersionChainEntry> =
                    chain.prev.as_ref().map(|b| b.as_ref());
                while let Some(e) = current {
                    let version_txn_id = e.version.txn_id;
                    let deleted_at_txn_id = e.version.deleted_at_txn_id;

                    if checker.is_visible(version_txn_id, txn_id) {
                        if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id)
                        {
                            found_entry = Some(e);
                        }
                        break;
                    }
                    current = e.prev.as_ref().map(|b| b.as_ref());
                }
                found_entry
            };

            if let Some(entry) = visible_entry {
                if result.len() >= batch_size {
                    has_more = true;
                    break; // Early termination - found one more than needed
                }
                result.push((row_id, get_row_data(entry)));
            }
        }

        (result, has_more)
    }

    /// Fetch visible rows into an existing buffer (avoids allocation)
    ///
    /// This is the same as `get_visible_rows_batch` but reuses the provided buffer
    /// instead of allocating a new Vec. The buffer is cleared before filling.
    ///
    /// # Arguments
    /// * `txn_id` - Transaction ID for visibility checks
    /// * `after_row_id` - Cursor position (exclusive lower bound)
    /// * `batch_size` - Maximum number of rows to fetch
    /// * `buffer` - Existing buffer to fill (will be cleared first)
    ///
    /// # Returns
    /// `has_more` - true if there are more rows to fetch after this batch
    pub fn get_visible_rows_batch_into(
        &self,
        txn_id: i64,
        after_row_id: i64,
        batch_size: usize,
        buffer: &mut RowVec,
    ) -> bool {
        buffer.clear();

        if self.closed.load(Ordering::Acquire) || batch_size == 0 {
            return false;
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return false,
        };

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock ONCE for this batch
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Helper closure to get row data from arena or version
        let get_row_data = |e: &VersionChainEntry| -> Row {
            if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                if let Some(arc_row) = arena_data.get(idx) {
                    return Row::from_arc(CompactArc::clone(arc_row));
                }
            }
            e.version.data.clone()
        };

        // Use range for efficient cursor-based iteration
        buffer.reserve(batch_size);
        let mut has_more = false;

        // Use range to start after the cursor row_id
        for (&row_id, chain) in versions.range((
            std::ops::Bound::Excluded(after_row_id),
            std::ops::Bound::Unbounded::<i64>,
        )) {
            // FAST PATH: Check HEAD version first - O(1) for common case
            let head_txn_id = chain.version.txn_id;
            let head_deleted_at = chain.version.deleted_at_txn_id;

            // Track the actual visible entry, not just a boolean
            let visible_entry: Option<&VersionChainEntry> = if checker
                .is_visible(head_txn_id, txn_id)
            {
                // HEAD is visible - check if deleted
                if head_deleted_at == 0 || !checker.is_visible(head_deleted_at, txn_id) {
                    Some(chain)
                } else {
                    None
                }
            } else {
                // SLOW PATH: HEAD not visible - traverse chain for older versions
                let mut found_entry: Option<&VersionChainEntry> = None;
                let mut current: Option<&VersionChainEntry> =
                    chain.prev.as_ref().map(|b| b.as_ref());
                while let Some(e) = current {
                    let version_txn_id = e.version.txn_id;
                    let deleted_at_txn_id = e.version.deleted_at_txn_id;

                    if checker.is_visible(version_txn_id, txn_id) {
                        if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id)
                        {
                            found_entry = Some(e);
                        }
                        break;
                    }
                    current = e.prev.as_ref().map(|b| b.as_ref());
                }
                found_entry
            };

            if let Some(entry) = visible_entry {
                if buffer.len() >= batch_size {
                    has_more = true;
                    break; // Early termination - found one more than needed
                }
                buffer.push((row_id, get_row_data(entry)));
            }
        }

        has_more
    }

    /// Collect visible rows ordered by row_id (PRIMARY KEY) with efficient OFFSET/LIMIT
    ///
    /// This method uses range iteration to efficiently skip OFFSET rows
    /// without cloning them, providing O(offset + limit) complexity instead of
    /// O(n) for full materialization.
    ///
    /// # Arguments
    /// * `txn_id` - Transaction ID for visibility checks
    /// * `ascending` - If true, iterate from smallest to largest row_id
    /// * `limit` - Maximum number of rows to return
    /// * `offset` - Number of visible rows to skip before collecting
    ///
    /// # Returns
    /// Vector of rows in row_id order, or None if iteration fails
    pub fn collect_rows_pk_ordered(
        &self,
        txn_id: i64,
        ascending: bool,
        limit: usize,
        offset: usize,
    ) -> Option<RowVec> {
        if self.closed.load(Ordering::Acquire) || limit == 0 {
            return Some(RowVec::new());
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return Some(RowVec::new()),
        };

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock ONCE for this entire operation
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Helper to get row data from an entry
        let get_row_from_entry = |entry: &VersionChainEntry| -> Row {
            if let Some(idx) = unpack_arena_idx(entry.arena_idx) {
                if let Some(arc_row) = arena_data.get(idx) {
                    return Row::from_arc(CompactArc::clone(arc_row));
                }
            }
            entry.version.data.clone()
        };

        // Collect with offset/limit
        // Cap capacity to avoid overflow when limit is usize::MAX
        let capacity = limit.min(versions.len()).min(10_000);
        let mut result = RowVec::with_capacity(capacity);
        let mut skipped = 0usize;

        if ascending {
            // Forward iteration with early termination
            for (row_id, chain) in versions.iter() {
                // Inline visibility check
                let mut current: Option<&VersionChainEntry> = Some(chain);
                while let Some(entry) = current {
                    if checker.is_visible(entry.version.txn_id, txn_id) {
                        if entry.version.deleted_at_txn_id == 0
                            || !checker.is_visible(entry.version.deleted_at_txn_id, txn_id)
                        {
                            // Found visible entry
                            if skipped < offset {
                                skipped += 1;
                            } else {
                                result.push((*row_id, get_row_from_entry(entry)));
                                if result.len() >= limit {
                                    return Some(result);
                                }
                            }
                        }
                        break;
                    }
                    current = entry.prev.as_ref().map(|b| b.as_ref());
                }
            }
        } else {
            // Reverse iteration with early termination: O(limit + offset) instead of O(n)
            for (&row_id, chain) in versions.iter_rev() {
                let mut current: Option<&VersionChainEntry> = Some(chain);
                while let Some(entry) = current {
                    if checker.is_visible(entry.version.txn_id, txn_id) {
                        if entry.version.deleted_at_txn_id == 0
                            || !checker.is_visible(entry.version.deleted_at_txn_id, txn_id)
                        {
                            if skipped < offset {
                                skipped += 1;
                            } else {
                                result.push((row_id, get_row_from_entry(entry)));
                                if result.len() >= limit {
                                    return Some(result);
                                }
                            }
                        }
                        break;
                    }
                    current = entry.prev.as_ref().map(|b| b.as_ref());
                }
            }
        }

        Some(result)
    }

    /// Collect visible rows using keyset pagination (WHERE id > X ORDER BY id LIMIT Y)
    ///
    /// This method uses range iteration starting from a specific row_id,
    /// providing O(limit) complexity instead of O(n) for full table scans.
    ///
    /// # Arguments
    /// * `txn_id` - Transaction ID for visibility checks
    /// * `start_after_row_id` - Start iteration after this row_id (exclusive, for id > X)
    /// * `start_from_row_id` - Start iteration from this row_id (inclusive, for id >= X)
    /// * `ascending` - If true, iterate from smallest to largest row_id
    /// * `limit` - Maximum number of rows to return
    ///
    /// # Returns
    /// RowVec of (row_id, row) pairs in row_id order
    pub fn collect_rows_keyset(
        &self,
        txn_id: i64,
        start_after_row_id: Option<i64>,
        start_from_row_id: Option<i64>,
        ascending: bool,
        limit: usize,
    ) -> RowVec {
        if self.closed.load(Ordering::Acquire) || limit == 0 {
            return RowVec::new();
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return RowVec::new(),
        };

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock ONCE for this entire operation
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Helper to find visible version and get row data
        let find_visible_row = |chain: &VersionChainEntry| -> Option<Row> {
            let mut current: Option<&VersionChainEntry> = Some(chain);
            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                if checker.is_visible(version_txn_id, txn_id) {
                    if deleted_at_txn_id != 0 && checker.is_visible(deleted_at_txn_id, txn_id) {
                        break; // Row is deleted
                    }

                    // Read row data from arena or version
                    let row_data = if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                        if let Some(arc_row) = arena_data.get(idx) {
                            Row::from_arc(CompactArc::clone(arc_row))
                        } else {
                            e.version.data.clone()
                        }
                    } else {
                        e.version.data.clone()
                    };
                    return Some(row_data);
                }
                current = e.prev.as_ref().map(|b| b.as_ref());
            }
            None
        };

        // Determine range bounds
        let start_bound = if let Some(after_id) = start_after_row_id {
            std::ops::Bound::Excluded(after_id)
        } else if let Some(from_id) = start_from_row_id {
            std::ops::Bound::Included(from_id)
        } else {
            std::ops::Bound::Unbounded
        };

        // Collect with early termination
        let mut result = RowVec::with_capacity(limit);

        if ascending {
            for (&row_id, chain) in versions.range((start_bound, std::ops::Bound::Unbounded::<i64>))
            {
                if let Some(row_data) = find_visible_row(chain) {
                    result.push((row_id, row_data));
                    if result.len() >= limit {
                        break;
                    }
                }
            }
        } else {
            // Reverse range iteration with early termination: O(limit) instead of O(n)
            for (&row_id, chain) in
                versions.range_rev((start_bound, std::ops::Bound::Unbounded::<i64>))
            {
                if let Some(row_data) = find_visible_row(chain) {
                    result.push((row_id, row_data));
                    if result.len() >= limit {
                        break;
                    }
                }
            }
        }

        result
    }

    /// Get all visible rows with filter applied during collection
    /// This saves memory by not allocating space for non-matching rows
    ///
    /// # Performance
    ///
    /// The filter expression is compiled into a `CompiledFilter` at the start
    /// to eliminate virtual dispatch overhead in the hot loop. This provides
    /// ~3-5x speedup for filter-heavy queries.
    pub fn get_all_visible_rows_filtered(
        &self,
        txn_id: i64,
        filter: &dyn crate::storage::expression::Expression,
    ) -> RowVec {
        if self.closed.load(Ordering::Acquire) {
            return RowVec::new();
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return RowVec::new(),
        };

        // Compile the filter once at the start for ~3-5x speedup in hot loop
        // CompiledFilter eliminates virtual dispatch via enum-based specialization
        let schema = self.schema.read();
        let compiled_filter = CompiledFilter::compile(filter, &schema);
        drop(schema); // Release lock early

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock ONCE
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Single-pass: read, filter, and collect in one loop
        let mut result = RowVec::with_capacity(versions.len() / 4);

        for (&row_id, chain) in versions.iter() {
            let mut current: Option<&VersionChainEntry> = Some(chain);

            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                if checker.is_visible(version_txn_id, txn_id) {
                    if deleted_at_txn_id != 0 && checker.is_visible(deleted_at_txn_id, txn_id) {
                        break; // Row is deleted
                    }

                    // OPTIMIZATION: Filter BEFORE cloning to avoid allocation for non-matching rows
                    // Try arena path first (zero-copy filter check using matches_arc_slice)
                    if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                        if let Some(arc_row) = arena_data.get(idx) {
                            // Filter directly on Arc slice - no Row allocation for non-matching rows
                            if compiled_filter.matches_arc_slice(arc_row.as_ref()) {
                                result.push((row_id, Row::from_arc(CompactArc::clone(arc_row))));
                            }
                            break;
                        }
                    }
                    // Fallback: filter on version data (already allocated)
                    if compiled_filter.matches(&e.version.data) {
                        result.push((row_id, e.version.data.clone()));
                    }
                    break;
                }
                current = e.prev.as_deref();
            }
        }

        result
    }

    /// Iterate visible rows matching a filter with early termination via callback.
    ///
    /// Calls `callback(row_id, row_data)` for each matching row. The callback
    /// returns `true` to continue or `false` to stop iteration.
    /// This avoids materializing all matching rows into a Vec.
    pub fn for_each_visible_filtered<F>(
        &self,
        txn_id: i64,
        filter: &dyn crate::storage::expression::Expression,
        mut callback: F,
    ) where
        F: FnMut(i64, Row) -> bool,
    {
        if self.closed.load(Ordering::Acquire) {
            return;
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return,
        };

        let schema = self.schema.read();
        let compiled_filter = CompiledFilter::compile(filter, &schema);
        drop(schema);

        let versions = self.versions.read().clone();
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        for (&row_id, chain) in versions.iter() {
            let mut current: Option<&VersionChainEntry> = Some(chain);

            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                if checker.is_visible(version_txn_id, txn_id) {
                    if deleted_at_txn_id != 0 && checker.is_visible(deleted_at_txn_id, txn_id) {
                        break;
                    }

                    if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                        if let Some(arc_row) = arena_data.get(idx) {
                            if compiled_filter.matches_arc_slice(arc_row.as_ref()) {
                                let row = Row::from_arc(CompactArc::clone(arc_row));
                                if !callback(row_id, row) {
                                    return;
                                }
                            }
                            break;
                        }
                    }
                    if compiled_filter.matches(&e.version.data)
                        && !callback(row_id, e.version.data.clone())
                    {
                        return;
                    }
                    break;
                }
                current = e.prev.as_deref();
            }
        }
    }

    /// Get visible rows with filter, limit and offset applied at the storage layer.
    ///
    /// # True Early Termination
    /// Iteration is ordered. After collecting `limit` matching rows
    /// (after skipping `offset`), we can stop iterating.
    ///
    /// # Arguments
    /// * `txn_id` - Transaction ID for visibility check
    /// * `filter` - Expression filter to apply to rows
    /// * `limit` - Maximum number of matching rows to return
    /// * `offset` - Number of matching rows to skip before collecting
    pub fn get_visible_rows_filtered_with_limit(
        &self,
        txn_id: i64,
        filter: &dyn crate::storage::expression::Expression,
        limit: usize,
        offset: usize,
    ) -> RowVec {
        if self.closed.load(Ordering::Acquire) || limit == 0 {
            return RowVec::new();
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return RowVec::new(),
        };

        // Compile the filter once at the start
        let schema = self.schema.read();
        let compiled_filter = CompiledFilter::compile(filter, &schema);
        drop(schema);

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // Pre-acquire arena lock ONCE
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        // Collect with offset/limit and early termination
        let mut result = RowVec::with_capacity(limit);
        let mut skipped = 0usize;

        for (&row_id, chain) in versions.iter() {
            let mut current: Option<&VersionChainEntry> = Some(chain);

            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                if checker.is_visible(version_txn_id, txn_id) {
                    if deleted_at_txn_id != 0 && checker.is_visible(deleted_at_txn_id, txn_id) {
                        break; // Row is deleted
                    }

                    // OPTIMIZATION: Filter BEFORE cloning to avoid allocation for non-matching rows
                    // Try arena path first (zero-copy filter check using matches_arc_slice)
                    if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                        if let Some(arc_row) = arena_data.get(idx) {
                            // Filter directly on Arc slice - no Row allocation for non-matching rows
                            if compiled_filter.matches_arc_slice(arc_row.as_ref()) {
                                if skipped < offset {
                                    skipped += 1;
                                } else {
                                    result
                                        .push((row_id, Row::from_arc(CompactArc::clone(arc_row))));
                                    if result.len() >= limit {
                                        return result; // Early termination!
                                    }
                                }
                            }
                            break;
                        }
                    }
                    // Fallback: filter on version data (already allocated)
                    if compiled_filter.matches(&e.version.data) {
                        if skipped < offset {
                            skipped += 1;
                        } else {
                            result.push((row_id, e.version.data.clone()));
                            if result.len() >= limit {
                                return result; // Early termination!
                            }
                        }
                    }
                    break;
                }
                current = e.prev.as_ref().map(|b| b.as_ref());
            }
        }

        result
    }

    /// Get visible rows with filter and LIMIT (with early termination).
    ///
    /// Note: Functionally identical to get_visible_rows_filtered_with_limit. Kept for API compatibility.
    #[inline]
    pub fn get_visible_rows_filtered_with_limit_unordered(
        &self,
        txn_id: i64,
        filter: &dyn crate::storage::expression::Expression,
        limit: usize,
        offset: usize,
    ) -> RowVec {
        // Delegate to the sorted version
        self.get_visible_rows_filtered_with_limit(txn_id, filter, limit, offset)
    }

    /// Get visible row indices without materializing row data (ZERO ALLOCATION SCAN!)
    ///
    /// This method returns lightweight `RowIndex` structs instead of cloning row data.
    /// Callers can then filter/sort/limit these indices and only materialize the final
    /// set of rows needed using `materialize_rows()`.
    ///
    /// # Performance
    /// For `SELECT * FROM t WHERE x > 100 LIMIT 10` on 100K rows:
    /// - Old approach: Clone 100K rows, filter, limit (100K allocations)
    /// - New approach: Get 100K indices (0 allocations), filter, limit, clone 10 rows
    ///
    /// # Returns
    /// Vector of `RowIndex` structs containing row_id and arena location
    pub fn get_visible_row_indices(&self, txn_id: i64) -> Vec<RowIndex> {
        let checker = self.visibility_checker.as_ref();

        // FAST PATH: If uncommitted_writes is empty, scan arena directly.
        // SAFETY: This fast path is only valid under ReadCommitted isolation.
        // Under SnapshotIsolation, HEAD versions may not be visible and the correct
        // behavior requires walking version chains to find older visible versions.
        //
        // LOCK ORDERING: Check uncommitted_writes BEFORE acquiring arena to maintain
        // consistent ordering with truncate_all (uncommitted_writes → arena).
        if let Some(checker) = checker {
            let uncommitted_empty = self.uncommitted_writes.read().is_empty();

            // Get arena metadata for fast path detection
            let arena_guard = self.arena.read_guard();
            let arena_meta = arena_guard.meta();
            let arena_len = arena_guard.len();

            if uncommitted_empty && arena_len > 0 && !checker.needs_snapshot_isolation(txn_id) {
                let mut indices: Vec<RowIndex> = Vec::with_capacity(arena_len);

                for (idx, meta) in arena_meta.iter().enumerate() {
                    if meta.txn_id != 0
                        && meta.deleted_at_txn_id == 0
                        && checker.is_visible(meta.txn_id, txn_id)
                    {
                        indices.push(RowIndex::new(meta.row_id, Some(idx)));
                    }
                }

                // Sort by row_id for consistent ordering
                indices.sort_unstable_by_key(|idx| idx.row_id);
                return indices;
            }
            // arena_guard dropped here — no need to hold it for slow path
        }

        // SLOW PATH: Full iteration
        let versions = self.versions.read().clone();
        let mut indices: Vec<RowIndex> = Vec::with_capacity(versions.len());

        if let Some(checker) = checker {
            for (&row_id, chain) in versions.iter() {
                let mut current: Option<&VersionChainEntry> = Some(chain);

                while let Some(e) = current {
                    let version_txn_id = e.version.txn_id;
                    let deleted_at_txn_id = e.version.deleted_at_txn_id;

                    if checker.is_visible(version_txn_id, txn_id) {
                        if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id)
                        {
                            // Found visible, non-deleted version
                            indices.push(RowIndex::new(row_id, unpack_arena_idx(e.arena_idx)));
                        }
                        break;
                    }
                    current = e.prev.as_ref().map(|b| b.as_ref());
                }
            }
        }

        indices
    }

    /// Get visible row indices without guaranteed ordering.
    ///
    /// This is optimized for aggregation operations (SUM, MIN, MAX, COUNT) that
    /// don't need row ordering. It skips the expensive sort in the arena fast path.
    #[inline]
    pub fn get_visible_row_indices_unordered(&self, txn_id: i64) -> Vec<RowIndex> {
        let checker = self.visibility_checker.as_ref();

        // FAST PATH: If uncommitted_writes is empty, scan arena directly (NO SORT!)
        // SAFETY: Only valid under ReadCommitted (see get_visible_row_indices).
        //
        // LOCK ORDERING: Check uncommitted_writes BEFORE acquiring arena to maintain
        // consistent ordering with truncate_all (uncommitted_writes → arena).
        if let Some(checker) = checker {
            let uncommitted_empty = self.uncommitted_writes.read().is_empty();

            let arena_guard = self.arena.read_guard();
            let arena_meta = arena_guard.meta();
            let arena_len = arena_guard.len();

            if uncommitted_empty && arena_len > 0 && !checker.needs_snapshot_isolation(txn_id) {
                let mut indices: Vec<RowIndex> = Vec::with_capacity(arena_len);

                for (idx, meta) in arena_meta.iter().enumerate() {
                    if meta.txn_id != 0
                        && meta.deleted_at_txn_id == 0
                        && checker.is_visible(meta.txn_id, txn_id)
                    {
                        indices.push(RowIndex::new(meta.row_id, Some(idx)));
                    }
                }

                // Skip sorting - aggregations don't need ordering
                return indices;
            }
            // arena_guard dropped here — no need to hold it for slow path
        }

        // SLOW PATH: Full iteration
        let versions = self.versions.read().clone();
        let mut indices: Vec<RowIndex> = Vec::with_capacity(versions.len());

        if let Some(checker) = checker {
            for (&row_id, chain) in versions.iter() {
                let mut current: Option<&VersionChainEntry> = Some(chain);

                while let Some(e) = current {
                    let version_txn_id = e.version.txn_id;
                    let deleted_at_txn_id = e.version.deleted_at_txn_id;

                    if checker.is_visible(version_txn_id, txn_id) {
                        if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id)
                        {
                            indices.push(RowIndex::new(row_id, unpack_arena_idx(e.arena_idx)));
                        }
                        break;
                    }
                    current = e.prev.as_ref().map(|b| b.as_ref());
                }
            }
        }

        indices
    }

    /// Materialize selected row indices into actual Row data
    ///
    /// This is the second step of deferred materialization. After filtering/limiting
    /// `RowIndex` values, call this to get the actual row data.
    ///
    /// Same HEAD-fallback caveat as `materialize_row` — see its doc comment.
    ///
    /// # Performance
    /// - Only clones the rows you actually need
    /// - Falls back to version chain for non-arena rows
    pub fn materialize_rows(&self, indices: &[RowIndex]) -> RowVec {
        if indices.is_empty() {
            return RowVec::new();
        }

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();
        let arena_len = arena_guard.len();

        let mut result = RowVec::with_capacity(indices.len());

        for idx in indices {
            if let Some(arena_idx) = idx.arena_idx() {
                // Fast path: get from arena
                if arena_idx < arena_len {
                    if let Some(arc_row) = arena_data.get(arena_idx) {
                        result.push((idx.row_id, Row::from_arc(CompactArc::clone(arc_row))));
                        continue;
                    }
                }
            }

            // Slow path: look up in version chain (CowBTree clone already held)
            if let Some(entry) = versions.get(idx.row_id) {
                result.push((idx.row_id, entry.version.data.clone()));
            }
        }

        result
    }

    /// Materialize a single row by index (for use in iterators)
    ///
    /// NOTE: When `arena_idx` is `None` (chain entries under snapshot isolation),
    /// the fallback reads `chain.version.data` which is HEAD. If a concurrent
    /// transaction updated the row after the RowIndex was collected, this returns
    /// the newer HEAD instead of the older visible version. This is a known
    /// limitation; `get_visible_rows_sorted_limit` avoids it with inline
    /// materialization during collection. Narrow scenario: snapshot isolation
    /// with concurrent updates between collect and materialize.
    #[inline]
    pub fn materialize_row(&self, idx: &RowIndex) -> Option<(i64, Row)> {
        if let Some(arena_idx) = idx.arena_idx() {
            let arena_guard = self.arena.read_guard();
            let arena_data = arena_guard.data();
            if let Some(arc_row) = arena_data.get(arena_idx) {
                return Some((idx.row_id, Row::from_arc(CompactArc::clone(arc_row))));
            }
        }

        // Slow path: look up in version chain (returns HEAD — see doc note above)
        self.versions
            .read()
            .get(idx.row_id)
            .map(|chain| (idx.row_id, chain.version.data.clone()))
    }

    /// Get a single column value from a row index WITHOUT full row materialization
    ///
    /// This is the key optimization for ORDER BY + LIMIT queries:
    /// - Load only the sort column, not all columns
    /// - Enables sorting indices by column value before materializing
    ///
    /// # Performance
    /// For `SELECT * FROM t ORDER BY col LIMIT 10` on 100K rows with 20 columns:
    /// - Old: Clone 100K rows (2M values), sort, take 10
    /// - New: Load 100K single values, sort indices, clone 10 rows (200 values)
    #[inline]
    pub fn get_column_value(&self, idx: &RowIndex, col_idx: usize) -> Option<Value> {
        if let Some(arena_idx) = idx.arena_idx() {
            let arena_guard = self.arena.read_guard();
            let arena_data = arena_guard.data();
            if let Some(arc_row) = arena_data.get(arena_idx) {
                return arc_row.get(col_idx).cloned();
            }
        }

        // Slow path: look up in version chain (returns HEAD — same caveat as materialize_row)
        self.versions
            .read()
            .get(idx.row_id)
            .and_then(|entry| entry.version.data.get(col_idx).cloned())
    }

    /// Batch get column values for multiple indices
    ///
    /// Optimized for ORDER BY: loads sort key values for all indices at once.
    #[inline]
    pub fn get_column_values_batch(
        &self,
        indices: &[RowIndex],
        col_idx: usize,
    ) -> Vec<Option<Value>> {
        if indices.is_empty() {
            return Vec::new();
        }

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();
        let arena_len = arena_guard.len();
        indices
            .iter()
            .map(|idx| {
                if let Some(arena_idx) = idx.arena_idx() {
                    if arena_idx < arena_len {
                        if let Some(arc_row) = arena_data.get(arena_idx) {
                            return arc_row.get(col_idx).cloned();
                        }
                    }
                }
                // Slow path: version chain lookup (returns HEAD — same caveat as materialize_row)
                versions
                    .get(idx.row_id)
                    .and_then(|entry| entry.version.data.get(col_idx).cloned())
            })
            .collect()
    }

    /// Optimized ORDER BY + LIMIT scan using deferred materialization
    ///
    /// This is the FAST PATH for queries like `SELECT * FROM t ORDER BY col LIMIT 10`:
    /// 1. Get row indices (no cloning)
    /// 2. Load only sort column values (not full rows)
    /// 3. Sort indices by sort values
    /// 4. Take top N indices
    /// 5. Materialize only N rows
    ///
    /// # Performance
    /// For 100K rows with 20 columns, LIMIT 10:
    /// - Old: Clone 2M values, sort 100K rows, take 10 → ~100ms
    /// - New: Load 100K values, sort indices, clone 200 values → ~10ms
    ///
    /// # Arguments
    /// * `txn_id` - Transaction ID for visibility
    /// * `sort_col_idx` - Column index to sort by
    /// * `ascending` - Sort direction
    /// * `limit` - Maximum rows to return
    /// * `offset` - Rows to skip before collecting
    pub fn get_visible_rows_sorted_limit(
        &self,
        txn_id: i64,
        sort_col_idx: usize,
        ascending: bool,
        limit: usize,
        offset: usize,
    ) -> RowVec {
        if limit == 0 {
            return RowVec::new();
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return RowVec::new(),
        };

        // Steps 1+2 combined: collect (RowIndex, sort_value) in a single lock scope
        let mut paired: Vec<(RowIndex, Option<Value>)>;

        // FAST PATH: Scan arena directly when no uncommitted writes.
        // SAFETY: Only valid under ReadCommitted (see get_visible_row_indices).
        let uncommitted_empty = self.uncommitted_writes.read().is_empty();
        if uncommitted_empty && !checker.needs_snapshot_isolation(txn_id) {
            let arena_guard = self.arena.read_guard();
            let arena_meta = arena_guard.meta();
            let arena_data = arena_guard.data();
            let arena_len = arena_guard.len();

            if arena_len > 0 {
                paired = Vec::with_capacity(arena_len);
                for (idx, meta) in arena_meta.iter().enumerate() {
                    if meta.txn_id != 0
                        && meta.deleted_at_txn_id == 0
                        && checker.is_visible(meta.txn_id, txn_id)
                    {
                        let sort_val = arena_data
                            .get(idx)
                            .and_then(|row| row.get(sort_col_idx).cloned());
                        paired.push((RowIndex::new(meta.row_id, Some(idx)), sort_val));
                    }
                }

                if paired.is_empty() {
                    return RowVec::new();
                }

                // Drop arena guard before sort and materialize
                drop(arena_guard);

                // Sort, take limit, materialize
                paired.sort_by(|(_, a), (_, b)| {
                    let cmp = match (a, b) {
                        (None, None) => std::cmp::Ordering::Equal,
                        (None, Some(_)) => std::cmp::Ordering::Less,
                        (Some(_), None) => std::cmp::Ordering::Greater,
                        (Some(va), Some(vb)) => va.compare(vb).unwrap_or(std::cmp::Ordering::Equal),
                    };
                    if ascending {
                        cmp
                    } else {
                        cmp.reverse()
                    }
                });

                let selected: Vec<RowIndex> = paired
                    .into_iter()
                    .skip(offset)
                    .take(limit)
                    .map(|(idx, _)| idx)
                    .collect();

                return self.materialize_rows(&selected);
            }
        }

        // SLOW PATH: CowBTree iteration — materialize during collection because
        // the visible version may be a chain entry (not HEAD), and RowIndex-based
        // deferred materialization would lose track of which version was visible.
        let versions = self.versions.read().clone();
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        let mut materialized: Vec<(i64, Row, Option<Value>)> = Vec::with_capacity(versions.len());

        for (&row_id, chain) in versions.iter() {
            let mut current: Option<&VersionChainEntry> = Some(chain);
            while let Some(e) = current {
                if checker.is_visible(e.version.txn_id, txn_id) {
                    if e.version.deleted_at_txn_id == 0
                        || !checker.is_visible(e.version.deleted_at_txn_id, txn_id)
                    {
                        let row = if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                            if let Some(arc_row) = arena_data.get(idx) {
                                Row::from_arc(CompactArc::clone(arc_row))
                            } else {
                                e.version.data.clone()
                            }
                        } else {
                            e.version.data.clone()
                        };
                        let sort_val = row.get(sort_col_idx).cloned();
                        materialized.push((row_id, row, sort_val));
                    }
                    break;
                }
                current = e.prev.as_ref().map(|b| b.as_ref());
            }
        }

        // Drop locks before sort
        drop(arena_guard);
        drop(versions);

        if materialized.is_empty() {
            return RowVec::new();
        }

        // Sort by sort values
        materialized.sort_by(|(_, _, a), (_, _, b)| {
            let cmp = match (a, b) {
                (None, None) => std::cmp::Ordering::Equal,
                (None, Some(_)) => std::cmp::Ordering::Less,
                (Some(_), None) => std::cmp::Ordering::Greater,
                (Some(va), Some(vb)) => va.compare(vb).unwrap_or(std::cmp::Ordering::Equal),
            };
            if ascending {
                cmp
            } else {
                cmp.reverse()
            }
        });

        // Apply offset/limit and strip sort values
        materialized
            .into_iter()
            .skip(offset)
            .take(limit)
            .map(|(row_id, row, _)| (row_id, row))
            .collect()
    }

    /// Compute COUNT(*) without materializing any rows
    ///
    /// This is the FASTEST path for `SELECT COUNT(*) FROM table`:
    /// - No row data is loaded
    /// - Only visibility checks are performed
    /// - O(n) time, O(1) memory
    #[inline]
    pub fn count_visible(&self, txn_id: i64) -> usize {
        self.count_visible_rows(txn_id)
    }

    /// Compute SUM(column) without materializing full rows
    ///
    /// OPTIMIZATION: Single-pass approach that combines visibility checking with summing.
    /// Avoids Vec allocation and second iteration over indices.
    /// Returns (sum, count_non_null) for proper NULL handling.
    pub fn sum_column(&self, txn_id: i64, col_idx: usize) -> (f64, usize) {
        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return (0.0, 0),
        };

        // OPTIMIZATION: Separate accumulators to avoid i64->f64 conversion per row.
        // Uses i128 for integer accumulation to prevent overflow (i128 holds sum of
        // 2^63 rows of i64::MAX without overflow).
        let mut int_sum = 0i128;
        let mut float_sum = 0.0f64;
        let mut count = 0usize;

        // Helper to accumulate numeric value
        #[inline(always)]
        fn accumulate_sum(int_sum: &mut i128, float_sum: &mut f64, count: &mut usize, val: &Value) {
            match val {
                Value::Integer(i) => {
                    *int_sum += *i as i128;
                    *count += 1;
                }
                Value::Float(f) => {
                    *float_sum += *f;
                    *count += 1;
                }
                _ => {} // NULL or non-numeric
            }
        }

        // FAST PATH: If uncommitted_writes is empty, scan arena directly (single pass).
        // SAFETY: Only valid under ReadCommitted (see get_visible_row_indices).
        //
        // LOCK ORDERING: Check uncommitted_writes BEFORE acquiring arena to maintain
        // consistent ordering with truncate_all (uncommitted_writes → arena).
        let uncommitted_empty = self.uncommitted_writes.read().is_empty();

        {
            let arena_guard = self.arena.read_guard();
            let arena_meta = arena_guard.meta();
            let arena_data = arena_guard.data();
            let arena_len = arena_guard.len();

            if uncommitted_empty && arena_len > 0 && !checker.needs_snapshot_isolation(txn_id) {
                // OPTIMIZATION: Cache visibility result for repeated txn_ids
                // When rows are inserted in batches, consecutive rows often have the same txn_id.
                // Caching avoids repeated thread-local access overhead (~27ms per 100K rows).
                let mut last_txn_id: i64 = 0;
                let mut last_visible: bool = false;

                let mut idx = 0;
                while idx < arena_len {
                    let meta = &arena_meta[idx];
                    if meta.txn_id != 0 && meta.deleted_at_txn_id == 0 {
                        // Check visibility with cache
                        let version_txn_id = meta.txn_id;
                        let is_vis = if version_txn_id == last_txn_id {
                            last_visible
                        } else {
                            let vis = checker.is_visible(version_txn_id, txn_id);
                            last_txn_id = version_txn_id;
                            last_visible = vis;
                            vis
                        };

                        if is_vis {
                            if let Some(val) = arena_data[idx].get(col_idx) {
                                accumulate_sum(&mut int_sum, &mut float_sum, &mut count, val);
                            }
                        }
                    }
                    idx += 1;
                }
                return (int_sum as f64 + float_sum, count);
            }
            // arena_guard dropped here — no need to hold it for slow path
        }

        // SLOW PATH: Full iteration over version chains (single pass)
        let versions = self.versions.read().clone();

        // Cache visibility for slow path too
        let mut last_txn_id: i64 = 0;
        let mut last_visible: bool = false;

        for chain in versions.values() {
            let mut current: Option<&VersionChainEntry> = Some(chain);

            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                let is_vis = if version_txn_id == last_txn_id {
                    last_visible
                } else {
                    let vis = checker.is_visible(version_txn_id, txn_id);
                    last_txn_id = version_txn_id;
                    last_visible = vis;
                    vis
                };

                if is_vis {
                    if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
                        // This version is visible and not deleted - accumulate its value
                        if let Some(val) = e.version.data.get(col_idx) {
                            accumulate_sum(&mut int_sum, &mut float_sum, &mut count, val);
                        }
                    }
                    break; // Always break on first visible version
                }

                current = e.prev.as_deref();
            }
        }

        (int_sum as f64 + float_sum, count)
    }

    /// Compute MIN(column) without materializing full rows
    ///
    /// OPTIMIZATION: Single-pass approach that combines visibility checking with min computation.
    pub fn min_column(&self, txn_id: i64, col_idx: usize) -> Option<Value> {
        let checker = self.visibility_checker.as_ref()?;

        let mut min_val: Option<Value> = None;

        // Helper to update min value
        #[inline(always)]
        fn update_min(min_val: &mut Option<Value>, val: &Value) {
            if !val.is_null() {
                match min_val {
                    None => *min_val = Some(val.clone()),
                    Some(ref current) => {
                        if let Ok(std::cmp::Ordering::Less) = val.compare(current) {
                            *min_val = Some(val.clone());
                        }
                    }
                }
            }
        }

        // FAST PATH: If uncommitted_writes is empty, scan arena directly (single pass).
        // SAFETY: Only valid under ReadCommitted (see get_visible_row_indices).
        //
        // LOCK ORDERING: Check uncommitted_writes BEFORE acquiring arena to maintain
        // consistent ordering with truncate_all (uncommitted_writes → arena).
        let uncommitted_empty = self.uncommitted_writes.read().is_empty();

        {
            let arena_guard = self.arena.read_guard();
            let arena_meta = arena_guard.meta();
            let arena_data = arena_guard.data();
            let arena_len = arena_guard.len();

            if uncommitted_empty && arena_len > 0 && !checker.needs_snapshot_isolation(txn_id) {
                // OPTIMIZATION: Cache visibility result for repeated txn_ids
                let mut last_txn_id: i64 = 0;
                let mut last_visible: bool = false;

                let mut idx = 0;
                while idx < arena_len {
                    let meta = &arena_meta[idx];
                    if meta.txn_id != 0 && meta.deleted_at_txn_id == 0 {
                        let version_txn_id = meta.txn_id;
                        let is_vis = if version_txn_id == last_txn_id {
                            last_visible
                        } else {
                            let vis = checker.is_visible(version_txn_id, txn_id);
                            last_txn_id = version_txn_id;
                            last_visible = vis;
                            vis
                        };

                        if is_vis {
                            if let Some(val) = arena_data[idx].get(col_idx) {
                                update_min(&mut min_val, val);
                            }
                        }
                    }
                    idx += 1;
                }
                return min_val;
            }
            // arena_guard dropped here — no need to hold it for slow path
        }

        // SLOW PATH: Full iteration over version chains (single pass)
        let versions = self.versions.read().clone();

        // Cache visibility for slow path too
        let mut last_txn_id: i64 = 0;
        let mut last_visible: bool = false;

        for chain in versions.values() {
            let mut current: Option<&VersionChainEntry> = Some(chain);

            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                let is_vis = if version_txn_id == last_txn_id {
                    last_visible
                } else {
                    let vis = checker.is_visible(version_txn_id, txn_id);
                    last_txn_id = version_txn_id;
                    last_visible = vis;
                    vis
                };

                if is_vis {
                    if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
                        if let Some(val) = e.version.data.get(col_idx) {
                            update_min(&mut min_val, val);
                        }
                    }
                    break; // Always break on first visible version
                }

                current = e.prev.as_deref();
            }
        }

        min_val
    }

    /// Compute MAX(column) without materializing full rows
    ///
    /// OPTIMIZATION: Single-pass approach that combines visibility checking with max computation.
    pub fn max_column(&self, txn_id: i64, col_idx: usize) -> Option<Value> {
        let checker = self.visibility_checker.as_ref()?;

        let mut max_val: Option<Value> = None;

        // Helper to update max value
        #[inline(always)]
        fn update_max(max_val: &mut Option<Value>, val: &Value) {
            if !val.is_null() {
                match max_val {
                    None => *max_val = Some(val.clone()),
                    Some(ref current) => {
                        if let Ok(std::cmp::Ordering::Greater) = val.compare(current) {
                            *max_val = Some(val.clone());
                        }
                    }
                }
            }
        }

        // FAST PATH: If uncommitted_writes is empty, scan arena directly (single pass).
        // SAFETY: Only valid under ReadCommitted (see get_visible_row_indices).
        //
        // LOCK ORDERING: Check uncommitted_writes BEFORE acquiring arena to maintain
        // consistent ordering with truncate_all (uncommitted_writes → arena).
        let uncommitted_empty = self.uncommitted_writes.read().is_empty();

        {
            let arena_guard = self.arena.read_guard();
            let arena_meta = arena_guard.meta();
            let arena_data = arena_guard.data();
            let arena_len = arena_guard.len();

            if uncommitted_empty && arena_len > 0 && !checker.needs_snapshot_isolation(txn_id) {
                // OPTIMIZATION: Cache visibility result for repeated txn_ids
                let mut last_txn_id: i64 = 0;
                let mut last_visible: bool = false;

                let mut idx = 0;
                while idx < arena_len {
                    let meta = &arena_meta[idx];
                    if meta.txn_id != 0 && meta.deleted_at_txn_id == 0 {
                        let version_txn_id = meta.txn_id;
                        let is_vis = if version_txn_id == last_txn_id {
                            last_visible
                        } else {
                            let vis = checker.is_visible(version_txn_id, txn_id);
                            last_txn_id = version_txn_id;
                            last_visible = vis;
                            vis
                        };

                        if is_vis {
                            if let Some(val) = arena_data[idx].get(col_idx) {
                                update_max(&mut max_val, val);
                            }
                        }
                    }
                    idx += 1;
                }
                return max_val;
            }
            // arena_guard dropped here — no need to hold it for slow path
        }

        // SLOW PATH: Full iteration over version chains (single pass)
        let versions = self.versions.read().clone();

        // Cache visibility for slow path too
        let mut last_txn_id: i64 = 0;
        let mut last_visible: bool = false;

        for chain in versions.values() {
            let mut current: Option<&VersionChainEntry> = Some(chain);

            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                let is_vis = if version_txn_id == last_txn_id {
                    last_visible
                } else {
                    let vis = checker.is_visible(version_txn_id, txn_id);
                    last_txn_id = version_txn_id;
                    last_visible = vis;
                    vis
                };

                if is_vis {
                    if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id) {
                        if let Some(val) = e.version.data.get(col_idx) {
                            update_max(&mut max_val, val);
                        }
                    }
                    break; // Always break on first visible version
                }

                current = e.prev.as_deref();
            }
        }

        max_val
    }

    /// Compute multiple column aggregates in a single pass
    ///
    /// This is the most efficient path for queries like:
    /// `SELECT SUM(a), AVG(b), MIN(c), MAX(d) FROM table`
    ///
    /// Returns aggregates in the order requested.
    pub fn compute_aggregates(
        &self,
        txn_id: i64,
        aggregates: &[(AggregateOp, usize)], // (operation, column_index)
    ) -> Vec<AggregateResult> {
        let empty_result = || {
            aggregates
                .iter()
                .map(|(op, _)| match op {
                    AggregateOp::Count | AggregateOp::CountStar => AggregateResult::Count(0),
                    AggregateOp::Sum => AggregateResult::Sum(0.0, 0),
                    AggregateOp::Min => AggregateResult::Min(None),
                    AggregateOp::Max => AggregateResult::Max(None),
                    AggregateOp::Avg => AggregateResult::Avg(0.0, 0),
                })
                .collect()
        };

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return empty_result(),
        };

        // Initialize accumulators
        let mut results: Vec<AggregateAccumulator> = aggregates
            .iter()
            .map(|(op, _)| match op {
                AggregateOp::Count | AggregateOp::CountStar => AggregateAccumulator::Count(0),
                AggregateOp::Sum => AggregateAccumulator::Sum(0, 0.0, 0),
                AggregateOp::Min => AggregateAccumulator::Min(None),
                AggregateOp::Max => AggregateAccumulator::Max(None),
                AggregateOp::Avg => AggregateAccumulator::Avg(0, 0.0, 0),
            })
            .collect();

        // Helper to update accumulator with a value
        fn update_accumulator(acc: &mut AggregateAccumulator, op: &AggregateOp, val: &Value) {
            match (acc, op) {
                (AggregateAccumulator::Count(c), AggregateOp::Count) => {
                    if !val.is_null() {
                        *c += 1;
                    }
                }
                (AggregateAccumulator::Count(c), AggregateOp::CountStar) => {
                    *c += 1;
                }
                (AggregateAccumulator::Sum(int_sum, float_sum, cnt), AggregateOp::Sum) => match val
                {
                    Value::Integer(i) => {
                        *int_sum += *i as i128;
                        *cnt += 1;
                    }
                    Value::Float(f) => {
                        *float_sum += *f;
                        *cnt += 1;
                    }
                    _ => {}
                },
                (AggregateAccumulator::Min(min), AggregateOp::Min) => {
                    if !val.is_null() {
                        match min {
                            None => *min = Some(val.clone()),
                            Some(current) => {
                                if let Ok(std::cmp::Ordering::Less) = val.compare(current) {
                                    *min = Some(val.clone());
                                }
                            }
                        }
                    }
                }
                (AggregateAccumulator::Max(max), AggregateOp::Max) => {
                    if !val.is_null() {
                        match max {
                            None => *max = Some(val.clone()),
                            Some(current) => {
                                if let Ok(std::cmp::Ordering::Greater) = val.compare(current) {
                                    *max = Some(val.clone());
                                }
                            }
                        }
                    }
                }
                (AggregateAccumulator::Avg(int_sum, float_sum, cnt), AggregateOp::Avg) => match val
                {
                    Value::Integer(i) => {
                        *int_sum += *i as i128;
                        *cnt += 1;
                    }
                    Value::Float(f) => {
                        *float_sum += *f;
                        *cnt += 1;
                    }
                    _ => {}
                },
                _ => {}
            }
        }

        // Helper macro: accumulate values from a row-like source by column index
        macro_rules! accumulate_from {
            ($src:expr, $results:expr, $aggregates:expr) => {
                for (i, (op, col_idx)) in $aggregates.iter().enumerate() {
                    if let Some(val) = $src.get(*col_idx) {
                        update_accumulator(&mut $results[i], op, val);
                    }
                }
            };
        }

        // FAST PATH: Scan arena directly when no uncommitted writes.
        // SAFETY: Only valid under ReadCommitted (see get_visible_row_indices).
        let uncommitted_empty = self.uncommitted_writes.read().is_empty();
        if uncommitted_empty && !checker.needs_snapshot_isolation(txn_id) {
            let arena_guard = self.arena.read_guard();
            let arena_meta = arena_guard.meta();
            let arena_data = arena_guard.data();
            let arena_len = arena_guard.len();

            if arena_len > 0 {
                for (idx, meta) in arena_meta.iter().enumerate() {
                    if meta.txn_id != 0
                        && meta.deleted_at_txn_id == 0
                        && checker.is_visible(meta.txn_id, txn_id)
                    {
                        if let Some(arc_row) = arena_data.get(idx) {
                            accumulate_from!(arc_row, results, aggregates);
                        }
                    }
                }

                return results
                    .into_iter()
                    .map(|acc| match acc {
                        AggregateAccumulator::Count(c) => AggregateResult::Count(c),
                        AggregateAccumulator::Sum(is, fs, c) => {
                            AggregateResult::Sum(is as f64 + fs, c)
                        }
                        AggregateAccumulator::Min(v) => AggregateResult::Min(v),
                        AggregateAccumulator::Max(v) => AggregateResult::Max(v),
                        AggregateAccumulator::Avg(is, fs, c) => {
                            AggregateResult::Avg(is as f64 + fs, c)
                        }
                    })
                    .collect();
            }
        }

        // SLOW PATH: CowBTree iteration with arena data retrieval
        let versions = self.versions.read().clone();
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();

        for chain in versions.values() {
            let mut current: Option<&VersionChainEntry> = Some(chain);
            while let Some(e) = current {
                if checker.is_visible(e.version.txn_id, txn_id) {
                    if e.version.deleted_at_txn_id == 0
                        || !checker.is_visible(e.version.deleted_at_txn_id, txn_id)
                    {
                        if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                            if let Some(arc_row) = arena_data.get(idx) {
                                accumulate_from!(arc_row, results, aggregates);
                            } else {
                                accumulate_from!(e.version.data, results, aggregates);
                            }
                        } else {
                            accumulate_from!(e.version.data, results, aggregates);
                        }
                    }
                    break;
                }
                current = e.prev.as_ref().map(|b| b.as_ref());
            }
        }

        // Convert accumulators to results
        results
            .into_iter()
            .map(|acc| match acc {
                AggregateAccumulator::Count(c) => AggregateResult::Count(c),
                AggregateAccumulator::Sum(is, fs, c) => AggregateResult::Sum(is as f64 + fs, c),
                AggregateAccumulator::Min(v) => AggregateResult::Min(v),
                AggregateAccumulator::Max(v) => AggregateResult::Max(v),
                AggregateAccumulator::Avg(is, fs, c) => AggregateResult::Avg(is as f64 + fs, c),
            })
            .collect()
    }

    /// Returns a zero-copy streaming iterator over visible rows
    ///
    /// This is the TRUE zero-copy approach that leverages Rust's borrowing system.
    /// Instead of cloning row data, it:
    /// 1. Pre-computes visible row indices in a single pass
    /// 2. Holds arena locks for the duration of iteration
    /// 3. Yields &[Value] slices directly from arena memory
    ///
    /// # Performance
    /// - Eliminates ALL cloning during iteration
    /// - Single lock acquisition for entire scan
    /// - Cache-friendly contiguous memory access
    /// - Fast path: O(n) arena scan when all rows are visible (skips version map)
    ///
    /// # Usage
    /// ```ignore
    /// let mut stream = version_store.stream_visible_rows(txn_id);
    /// while stream.next() {
    ///     let values: &[Value] = stream.row_slice(); // Zero-copy!
    ///     let row_id = stream.row_id();
    ///     // Process values...
    /// }
    /// ```
    pub fn stream_visible_rows(&self, txn_id: i64) -> StreamingResult<'_> {
        let checker = self.visibility_checker.as_ref();

        // Clone CowBTree to release read lock early, allowing concurrent commits
        let versions = self.versions.read().clone();

        // LOCK ORDERING: Check uncommitted_writes BEFORE acquiring arena to maintain
        // consistent ordering with truncate_all (uncommitted_writes → arena).
        let uncommitted_empty = if checker.is_some() {
            self.uncommitted_writes.read().is_empty()
        } else {
            false
        };

        // Pre-acquire arena lock (after uncommitted_writes check)
        let arena_guard = self.arena.read_guard();
        let arena_len = arena_guard.len();

        // Get column names from schema
        let columns: Vec<String> = self
            .schema
            .read()
            .columns
            .iter()
            .map(|c| c.name.clone())
            .collect();

        // FAST PATH: If we can iterate arena directly without version map lookup
        // This is possible when:
        // 1. No active uncommitted writes
        // 2. ReadCommitted isolation (all committed versions are visible)
        // SAFETY: Only valid under ReadCommitted (see get_visible_row_indices).
        if let Some(checker) = checker {
            if uncommitted_empty && arena_len > 0 && !checker.needs_snapshot_isolation(txn_id) {
                // Fast path: scan arena directly, skip version map iteration
                let mut visible_indices: Vec<VisibleRowInfo> = Vec::with_capacity(arena_len);

                for (idx, meta) in arena_guard.meta().iter().enumerate() {
                    // Check visibility and deleted status
                    if meta.txn_id != 0
                        && meta.deleted_at_txn_id == 0
                        && checker.is_visible(meta.txn_id, txn_id)
                    {
                        visible_indices.push(VisibleRowInfo {
                            row_id: meta.row_id,
                            arena_idx: idx,
                        });
                    }
                }

                // Sort by row_id for consistent ordering
                visible_indices.sort_unstable_by_key(|info| info.row_id);

                return StreamingResult::new(arena_guard, visible_indices, columns);
            }
        }

        // SLOW PATH: Full iteration (includes chain entries for snapshot isolation)
        let mut visible_indices: Vec<VisibleRowInfo> = Vec::with_capacity(versions.len());
        let mut fallback_data: Vec<CompactArc<[crate::core::Value]>> = Vec::new();

        if let Some(checker) = checker {
            for (&row_id, chain) in versions.iter() {
                let mut current: Option<&VersionChainEntry> = Some(chain);

                while let Some(e) = current {
                    let version_txn_id = e.version.txn_id;
                    let deleted_at_txn_id = e.version.deleted_at_txn_id;

                    if checker.is_visible(version_txn_id, txn_id) {
                        if deleted_at_txn_id == 0 || !checker.is_visible(deleted_at_txn_id, txn_id)
                        {
                            // Found visible, non-deleted version
                            if let Some(idx) = unpack_arena_idx(e.arena_idx) {
                                if idx < arena_len {
                                    visible_indices.push(VisibleRowInfo {
                                        row_id,
                                        arena_idx: idx,
                                    });
                                } else {
                                    // Arena index out of bounds — use fallback
                                    let fb_idx = arena_len + fallback_data.len();
                                    fallback_data.push(e.version.data.clone().into_arc());
                                    visible_indices.push(VisibleRowInfo {
                                        row_id,
                                        arena_idx: fb_idx,
                                    });
                                }
                            } else {
                                // Chain entry without arena index — store data in fallback
                                let fb_idx = arena_len + fallback_data.len();
                                fallback_data.push(e.version.data.clone().into_arc());
                                visible_indices.push(VisibleRowInfo {
                                    row_id,
                                    arena_idx: fb_idx,
                                });
                            }
                        }
                        break;
                    }
                    current = e.prev.as_ref().map(|b| b.as_ref());
                }
            }
        }
        drop(versions); // Release lock before returning

        StreamingResult::new_with_fallback(arena_guard, visible_indices, fallback_data, columns)
    }

    /// Returns the count of rows
    pub fn row_count(&self) -> usize {
        self.versions.read().len()
    }
    /// Tries to claim a row for update (dirty write prevention)
    pub fn try_claim_row(&self, row_id: i64, txn_id: i64) -> Result<(), Error> {
        use crate::common::i64_map::Entry;

        let mut map = self.uncommitted_writes.write();
        match map.entry(row_id) {
            Entry::Occupied(e) => {
                let existing_txn = *e.get();
                if existing_txn != txn_id {
                    return Err(Error::internal(format!(
                        "row {} has uncommitted changes from transaction {}",
                        row_id, existing_txn
                    )));
                }
                Ok(())
            }
            Entry::Vacant(e) => {
                e.insert(txn_id);
                Ok(())
            }
        }
    }

    /// Releases a row claim
    pub fn release_row_claim(&self, row_id: i64, txn_id: i64) {
        let mut map = self.uncommitted_writes.write();
        if let Some(&v) = map.get(row_id) {
            if v == txn_id {
                map.remove(row_id);
            }
        }
    }

    /// Releases multiple row claims in batch
    /// OPTIMIZATION: Single lock acquisition for all removals
    #[inline]
    pub fn release_row_claims_batch(&self, row_ids: &[i64], txn_id: i64) {
        let mut map = self.uncommitted_writes.write();
        for &row_id in row_ids {
            if let Some(&v) = map.get(row_id) {
                if v == txn_id {
                    map.remove(row_id);
                }
            }
        }
    }

    /// Check if an index exists
    pub fn index_exists(&self, index_name: &str) -> bool {
        let indexes = self.indexes.read();
        indexes.contains_key(index_name)
    }

    /// List all indexes
    pub fn list_indexes(&self) -> Vec<String> {
        let indexes = self.indexes.read();
        indexes
            .iter()
            .filter(|(_, idx)| idx.index_type() != crate::core::IndexType::PrimaryKey)
            .map(|(name, _)| name.clone())
            .collect()
    }

    /// Check if there are any indexes
    #[inline]
    pub fn has_indexes(&self) -> bool {
        let indexes = self.indexes.read();
        !indexes.is_empty()
    }

    /// Iterate over all indexes, calling the provided function for each
    /// OPTIMIZATION: Avoids collecting index names and allows early exit on error
    pub fn for_each_index<F>(&self, mut f: F) -> crate::core::Result<()>
    where
        F: FnMut(&Arc<dyn Index>) -> crate::core::Result<()>,
    {
        let indexes = self.indexes.read();
        for index in indexes.values() {
            f(index)?;
        }
        Ok(())
    }

    /// Iterate over unique indexes only, calling the provided function for each
    /// OPTIMIZATION: Avoids collecting index names and allows early exit on error
    pub fn for_each_unique_index<F>(&self, mut f: F) -> crate::core::Result<()>
    where
        F: FnMut(&str, &Arc<dyn Index>) -> crate::core::Result<()>,
    {
        let indexes = self.indexes.read();
        for (name, index) in indexes.iter() {
            if index.is_unique() {
                f(name, index)?;
            }
        }
        Ok(())
    }

    /// Acquire the per-table upsert mutex. Returns an owned guard.
    /// Only used for ON CONFLICT statements to serialize check+insert+commit.
    #[inline]
    pub fn acquire_upsert_lock(&self) -> parking_lot::ArcMutexGuard<parking_lot::RawMutex, ()> {
        parking_lot::Mutex::lock_arc(&self.upsert_mutex)
    }

    /// Add an index
    pub fn add_index(&self, name: String, index: Arc<dyn Index>) {
        let mut indexes = self.indexes.write();
        indexes.insert(name, index);
    }

    /// Remove an index
    pub fn remove_index(&self, name: &str) -> Option<Arc<dyn Index>> {
        let mut indexes = self.indexes.write();
        indexes.remove(name)
    }

    /// Get an index by name
    pub fn get_index(&self, name: &str) -> Option<Arc<dyn Index>> {
        let indexes = self.indexes.read();
        indexes.get(name).cloned()
    }

    /// Get an index by column name (single-column indexes only)
    pub fn get_index_by_column(&self, column_name: &str) -> Option<Arc<dyn Index>> {
        let indexes = self.indexes.read();
        for index in indexes.values() {
            let column_names = index.column_names();
            if column_names.len() == 1 && column_names[0] == column_name {
                return Some(index.clone());
            }
        }
        None
    }

    /// Find the best multi-column index that matches a set of predicate columns.
    /// Returns the index if predicate columns cover a prefix of the index columns (leftmost prefix rule).
    /// For example, an index on (a, b, c) can be used for queries that include (a), (a, b), or (a, b, c).
    /// The predicate columns don't need to be in the same order as the index columns.
    pub fn get_multi_column_index(
        &self,
        predicate_columns: &[&str],
    ) -> Option<(Arc<dyn Index>, usize)> {
        if predicate_columns.is_empty() {
            return None;
        }

        let indexes = self.indexes.read();
        let mut best_match: Option<(Arc<dyn Index>, usize)> = None;

        // Create a set of predicate columns for O(1) lookup
        let pred_set: FxHashSet<&str> = predicate_columns.iter().copied().collect();

        for index in indexes.values() {
            let index_columns = index.column_names();
            if index_columns.len() < 2 {
                continue; // Skip single-column indexes
            }

            // Count how many of the leading index columns are in the predicate set.
            // This implements the leftmost prefix rule: we can only use the index
            // if we have predicates on a contiguous prefix of the index columns.
            let mut matched = 0;
            for idx_col in index_columns.iter() {
                if pred_set.contains(idx_col.as_str()) {
                    matched += 1;
                } else {
                    // Stop at the first index column not in predicates
                    break;
                }
            }

            // Use composite index if predicate covers a leftmost prefix
            if matched >= 1 {
                // Prefer index with more matching columns
                if best_match.is_none() || matched > best_match.as_ref().unwrap().1 {
                    best_match = Some((index.clone(), matched));
                }
            }
        }

        best_match
    }

    /// Get all indexes as Arc clones - avoids String allocation and repeated lookups
    /// OPTIMIZATION: Arc clones are cheap (atomic increment), uses SmallVec for ≤4 indexes
    #[inline]
    pub fn get_all_indexes(&self) -> SmallVec<[Arc<dyn Index>; 4]> {
        let indexes = self.indexes.read();
        indexes.values().cloned().collect()
    }

    /// Borrow the indexes map under a read lock — no Arc clones, no allocations.
    #[inline]
    pub fn indexes_read(
        &self,
    ) -> parking_lot::RwLockReadGuard<'_, FxHashMap<String, Arc<dyn Index>>> {
        self.indexes.read()
    }

    /// Get column indices and names for all non-PK unique indexes.
    /// Used by commit-time cold revalidation.
    pub fn get_unique_non_pk_index_columns(&self) -> Vec<(Vec<usize>, Vec<String>)> {
        let schema = self.schema();
        let pk_col = schema
            .pk_column_index()
            .map(|i| &schema.columns[i].name_lower);
        let indexes = self.indexes.read();
        let mut result = Vec::new();
        for idx in indexes.values() {
            if !idx.is_unique() {
                continue;
            }
            let names = idx.column_names();
            if names.len() == 1 {
                if let Some(pk) = pk_col {
                    if names[0].eq_ignore_ascii_case(pk) {
                        continue;
                    }
                }
            }
            let col_indices: Vec<usize> = names
                .iter()
                .filter_map(|name| schema.columns.iter().position(|c| c.name_lower == *name))
                .collect();
            if col_indices.len() == names.len() {
                result.push((col_indices, names.to_vec()));
            }
        }
        result
    }

    // =========================================================================
    // Zone Map Operations (Statistics for Segment Pruning)
    // =========================================================================

    /// Sets the zone maps for this table
    ///
    /// Zone maps contain min/max statistics per segment, enabling the query
    /// executor to skip entire segments when predicates fall outside the range.
    pub fn set_zone_maps(&self, zone_maps: crate::storage::mvcc::zonemap::TableZoneMap) {
        let mut guard = self.zone_maps.write();
        *guard = Some(Arc::new(zone_maps));
    }

    /// Gets the zone maps for this table
    ///
    /// Returns None if zone maps have not been built (ANALYZE not run)
    /// Uses Arc to avoid expensive cloning on high QPS workloads
    pub fn get_zone_maps(&self) -> Option<Arc<crate::storage::mvcc::zonemap::TableZoneMap>> {
        let guard = self.zone_maps.read();
        guard.clone()
    }

    /// Gets the segments that need to be scanned for a given predicate
    ///
    /// Uses zone maps to determine which segments can be pruned (skipped)
    pub fn get_segments_to_scan(
        &self,
        column: &str,
        operator: crate::core::Operator,
        value: &crate::core::Value,
    ) -> Option<Vec<u32>> {
        let guard = self.zone_maps.read();
        guard
            .as_ref()
            .and_then(|zm| zm.get_segments_to_scan(column, operator, value))
    }

    /// Gets prune statistics for a single-column predicate
    pub fn get_prune_stats(
        &self,
        column: &str,
        operator: crate::core::Operator,
        value: &crate::core::Value,
    ) -> Option<crate::storage::mvcc::zonemap::PruneStats> {
        let guard = self.zone_maps.read();
        guard
            .as_ref()
            .and_then(|zm| zm.get_prune_stats(column, operator, value))
    }

    /// Marks zone maps as stale (needing rebuild after data changes)
    pub fn mark_zone_maps_stale(&self) {
        let guard = self.zone_maps.read();
        if let Some(ref zm) = *guard {
            zm.mark_stale();
        }
    }

    /// Close the version store
    pub fn close(&self) {
        self.closed.store(true, Ordering::Release);
    }

    /// Check if the version store is closed
    pub fn is_closed(&self) -> bool {
        self.closed.load(Ordering::Acquire)
    }

    // =========================================================================
    // Recovery Functions (for WAL replay)
    // =========================================================================

    /// Apply a recovered row version during WAL replay
    ///
    /// This is used during database recovery to apply row versions from the WAL.
    /// Unlike normal operations, this directly adds the version without visibility checks.
    /// Also updates any existing indexes with the new row data.
    /// Also updates the auto_increment counter if row_id is higher than current.
    ///
    /// Duplicate Detection: If the row already exists with identical data (same values),
    /// the version is skipped to avoid duplicate entries in the version chain. This can
    /// occur when snapshot and WAL both contain the same committed data due to race
    /// conditions during snapshot creation.
    pub fn apply_recovered_version(&self, row_id: i64, version: RowVersion) {
        let is_deleted = version.is_deleted();
        let row_data = version.data.clone();

        // Check for duplicate: if row already exists with identical data, skip adding
        // This prevents duplicate version chain entries when both snapshot and WAL
        // contain the same row data (can happen due to race conditions during snapshot)
        // No need to clone tree for single-row lookup - just hold read guard
        {
            let versions = self.versions.read();
            if let Some(existing_entry) = versions.get(row_id) {
                let existing = &existing_entry.version;
                // Check if data is identical (both deleted status and row data)
                if existing.is_deleted() == is_deleted && existing.data == row_data {
                    // Identical data already exists, skip to avoid duplicate
                    // Still update auto_increment counter
                    if row_id > 0 {
                        self.set_auto_increment_counter(row_id);
                    }
                    return;
                }
            }
        }

        // Add the version to the store
        self.add_version(row_id, version);

        // Update auto_increment counter if this row_id is higher
        // This ensures the counter is restored to at least the max seen row_id
        if row_id > 0 {
            self.set_auto_increment_counter(row_id);
        }

        // Update indexes with the new row data (if not deleted)
        if !is_deleted {
            let indexes = self.indexes.read();
            for index in indexes.values() {
                let column_ids = index.column_ids();
                if column_ids.is_empty() {
                    continue;
                }
                if column_ids.len() == 1 {
                    let col_id = column_ids[0] as usize;
                    if let Some(value) = row_data.get(col_id) {
                        let _ = index.add(std::slice::from_ref(value), row_id, row_id);
                    }
                } else {
                    let values: Vec<crate::core::Value> = column_ids
                        .iter()
                        .map(|&col_id| {
                            row_data
                                .get(col_id as usize)
                                .cloned()
                                .unwrap_or(crate::core::Value::Null(crate::core::DataType::Null))
                        })
                        .collect();
                    let _ = index.add(&values, row_id, row_id);
                }
            }
        }
    }

    /// Mark a row as deleted during WAL replay
    ///
    /// This creates a deleted version for the row during recovery.
    /// Also removes the row from any existing indexes.
    pub fn mark_deleted(&self, row_id: i64, txn_id: i64) {
        // Get the old row data for index removal BEFORE creating the deleted version
        let old_row = self
            .get_visible_version(row_id, txn_id)
            .map(|v| v.data.clone());

        // Create a deleted version (empty data with deleted flag)
        let deleted_version = RowVersion {
            txn_id,
            deleted_at_txn_id: txn_id,
            data: Row::new(),
            create_time: crate::common::time_compat::SystemTime::now()
                .duration_since(crate::common::time_compat::UNIX_EPOCH)
                .map(|d| d.as_nanos() as i64)
                .unwrap_or(0),
        };
        self.add_version(row_id, deleted_version);

        // Remove from indexes using old row data
        if let Some(old_data) = old_row {
            let indexes = self.indexes.read();
            for index in indexes.values() {
                let column_ids = index.column_ids();
                if column_ids.is_empty() {
                    continue;
                }
                if column_ids.len() == 1 {
                    let col_id = column_ids[0] as usize;
                    if let Some(value) = old_data.get(col_id) {
                        let _ = index.remove(std::slice::from_ref(value), row_id, row_id);
                    }
                } else {
                    let values: Vec<crate::core::Value> = column_ids
                        .iter()
                        .map(|&col_id| {
                            old_data
                                .get(col_id as usize)
                                .cloned()
                                .unwrap_or(crate::core::Value::Null(crate::core::DataType::Null))
                        })
                        .collect();
                    let _ = index.remove(&values, row_id, row_id);
                }
            }
        }
    }

    /// Drop an index by name (alias for remove_index)
    pub fn drop_index(&self, name: &str) -> Option<Arc<dyn Index>> {
        self.remove_index(name)
    }

    /// Create an index from persistence metadata during WAL replay
    ///
    /// This recreates an index from its persisted metadata.
    ///
    /// # Arguments
    /// * `meta` - Index metadata from WAL
    /// * `skip_population` - If true, creates the index structure without populating it.
    ///   This is used during batch recovery to defer population to a single-pass scan.
    pub fn create_index_from_metadata(
        &self,
        meta: &crate::storage::mvcc::persistence::IndexMetadata,
        skip_population: bool,
    ) -> crate::core::Result<()> {
        self.create_index_from_metadata_with_graph(meta, skip_population, None)
    }

    /// Create an index from persisted metadata, optionally loading HNSW graph from disk.
    /// `hnsw_graph_path` is the full path to the HNSW graph file (timestamped to match the
    /// loaded data snapshot, ensuring consistency during fallback recovery).
    pub fn create_index_from_metadata_with_graph(
        &self,
        meta: &crate::storage::mvcc::persistence::IndexMetadata,
        skip_population: bool,
        hnsw_graph_path: Option<&std::path::Path>,
    ) -> crate::core::Result<()> {
        use crate::core::IndexType;
        use crate::storage::index::{BitmapIndex, HashIndex};

        // Check if we have the required column information
        if meta.column_names.is_empty() {
            return Err(crate::core::Error::internal(
                "index metadata must have at least one column",
            ));
        }

        // Check if index already exists
        if self.index_exists(&meta.name) {
            return Ok(()); // Already recovered, skip
        }

        // Skip if a PkIndex already covers this single column
        if meta.column_names.len() == 1 {
            if let Some(existing) = self.get_index_by_column(&meta.column_names[0]) {
                if existing.index_type() == IndexType::PrimaryKey {
                    return Ok(()); // PK column covered by PkIndex
                }
            }
        }

        // Get row count for capacity hint
        // No need to clone tree just for len()
        let expected_rows = self.versions.read().len();

        if meta.column_names.len() == 1 {
            // Single-column index
            let column_name = &meta.column_names[0];
            let column_id = meta.column_ids.first().copied().unwrap_or(0);
            let data_type = meta
                .data_types
                .first()
                .copied()
                .unwrap_or(crate::core::DataType::Null);

            // Create index based on stored index_type
            let index: Arc<dyn crate::storage::Index> = match meta.index_type {
                IndexType::Hash => {
                    let idx = HashIndex::new(
                        meta.name.clone(),
                        meta.table_name.clone(),
                        vec![column_name.clone()],
                        vec![column_id],
                        vec![data_type],
                        meta.is_unique,
                        expected_rows,
                    );
                    Arc::new(idx)
                }
                IndexType::Bitmap => {
                    let idx = BitmapIndex::new(
                        meta.name.clone(),
                        meta.table_name.clone(),
                        vec![column_name.clone()],
                        vec![column_id],
                        vec![data_type],
                        meta.is_unique,
                        expected_rows,
                    );
                    Arc::new(idx)
                }
                IndexType::BTree => {
                    // BTree uses BTreeIndex implementation
                    let idx = crate::storage::index::BTreeIndex::new(
                        meta.name.clone(),
                        meta.table_name.clone(),
                        column_id,
                        column_name.clone(),
                        data_type,
                        meta.is_unique,
                        expected_rows,
                    );
                    Arc::new(idx)
                }
                IndexType::MultiColumn => {
                    // MultiColumn uses MultiColumnIndex implementation
                    let idx = crate::storage::index::MultiColumnIndex::new(
                        meta.name.clone(),
                        meta.table_name.clone(),
                        meta.column_names.clone(),
                        meta.column_ids.clone(),
                        meta.data_types.clone(),
                        meta.is_unique,
                        expected_rows,
                    );
                    Arc::new(idx)
                }
                IndexType::PrimaryKey => {
                    // PrimaryKey indexes are auto-created, never persisted via CREATE INDEX
                    return Ok(());
                }
                IndexType::Hnsw => {
                    // Get vector dimensions from schema
                    let schema = self.schema();
                    let dims = schema
                        .find_column(column_name)
                        .map(|(_, col)| col.vector_dimensions as usize)
                        .unwrap_or(0);
                    if dims == 0 {
                        return Ok(()); // Cannot rebuild without dimension info
                    }
                    let m = meta
                        .hnsw_m
                        .map(|v| v as usize)
                        .unwrap_or_else(|| crate::storage::index::default_m_for_dims(dims));
                    let ef_construction = meta
                        .hnsw_ef_construction
                        .map(|v| v as usize)
                        .unwrap_or_else(|| crate::storage::index::default_ef_construction(m));
                    let ef_search = meta
                        .hnsw_ef_search
                        .map(|v| v as usize)
                        .unwrap_or_else(|| crate::storage::index::default_ef_search(m));

                    // Try loading saved HNSW graph from snapshot file
                    if let Some(graph_path) = hnsw_graph_path {
                        if let Ok(Some(mut loaded)) = crate::storage::index::HnswIndex::load_graph(
                            graph_path,
                            meta.name.clone(),
                            meta.table_name.clone(),
                            column_name.clone(),
                            column_id,
                            dims,
                            m,
                            ef_construction,
                            ef_search,
                        ) {
                            loaded.set_unique(meta.is_unique);
                            Arc::new(loaded)
                        } else {
                            let mut idx = crate::storage::index::HnswIndex::new(
                                meta.name.clone(),
                                meta.table_name.clone(),
                                column_name.clone(),
                                column_id,
                                dims,
                                m,
                                ef_construction,
                                ef_search,
                                crate::storage::index::HnswDistanceMetric::from_u8(
                                    meta.hnsw_distance_metric.unwrap_or(0),
                                )
                                .unwrap_or(crate::storage::index::HnswDistanceMetric::L2),
                            );
                            idx.set_unique(meta.is_unique);
                            Arc::new(idx)
                        }
                    } else {
                        let mut idx = crate::storage::index::HnswIndex::new(
                            meta.name.clone(),
                            meta.table_name.clone(),
                            column_name.clone(),
                            column_id,
                            dims,
                            m,
                            ef_construction,
                            ef_search,
                            crate::storage::index::HnswDistanceMetric::from_u8(
                                meta.hnsw_distance_metric.unwrap_or(0),
                            )
                            .unwrap_or(crate::storage::index::HnswDistanceMetric::L2),
                        );
                        idx.set_unique(meta.is_unique);
                        Arc::new(idx)
                    }
                }
            };

            // Populate the index with existing data unless deferred
            // Uses batch_slice for better performance
            if !skip_population {
                let col_idx = column_id as usize;
                let versions = self.versions.read().clone();
                let mut entries: Vec<(i64, Vec<crate::core::Value>)> = Vec::new();
                for (&row_id, version_chain) in versions.iter() {
                    let version = &version_chain.version;
                    if !version.is_deleted() {
                        if let Some(value) = version.data.get(col_idx) {
                            entries.push((row_id, vec![value.clone()]));
                        }
                    }
                }
                if !entries.is_empty() {
                    let entry_refs: Vec<(i64, &[crate::core::Value])> = entries
                        .iter()
                        .map(|(row_id, values)| (*row_id, values.as_slice()))
                        .collect();
                    let _ = index.add_batch_slice(&entry_refs);
                }
            }

            self.add_index(meta.name.clone(), index);
        } else {
            // Multi-column index: use MultiColumnIndex
            let index = crate::storage::index::MultiColumnIndex::new(
                meta.name.clone(),
                meta.table_name.clone(),
                meta.column_names.clone(),
                meta.column_ids.clone(),
                meta.data_types.clone(),
                meta.is_unique,
                expected_rows,
            );

            let index = Arc::new(index);

            // Populate the index with existing data unless deferred
            // Uses batch_slice for better performance
            if !skip_population {
                let col_indices: Vec<usize> =
                    meta.column_ids.iter().map(|&id| id as usize).collect();
                let versions = self.versions.read().clone();
                let mut entries: Vec<(i64, Vec<crate::core::Value>)> = Vec::new();
                for (&row_id, version_chain) in versions.iter() {
                    let version = &version_chain.version;
                    if !version.is_deleted() {
                        let values: Vec<crate::core::Value> =
                            col_indices
                                .iter()
                                .map(|&idx| {
                                    version.data.get(idx).cloned().unwrap_or(
                                        crate::core::Value::Null(crate::core::DataType::Null),
                                    )
                                })
                                .collect();
                        entries.push((row_id, values));
                    }
                }
                if !entries.is_empty() {
                    let entry_refs: Vec<(i64, &[crate::core::Value])> = entries
                        .iter()
                        .map(|(row_id, values)| (*row_id, values.as_slice()))
                        .collect();
                    let _ = index.add_batch_slice(&entry_refs);
                }
            }

            self.add_index(meta.name.clone(), index);
        }

        Ok(())
    }

    /// Populate all indexes in a single pass over the version store
    ///
    /// This is O(N + M) where N = number of rows and M = number of indexes,
    /// compared to O(N * M) when populating each index separately.
    ///
    /// OPTIMIZATION: Uses batch_slice operations to reduce lock acquisitions
    /// from O(rows × indexes) to O(indexes).
    ///
    /// Call this after WAL replay completes with skip_population=true.
    pub fn populate_all_indexes(&self) {
        let indexes = self.indexes.read();
        if indexes.is_empty() {
            return;
        }

        // Collect index info: (column_ids as Vec<usize>, index_arc)
        // Supports both single-column and multi-column indexes
        // HNSW indexes loaded from graph are included — HnswInner::insert skips duplicate row_ids
        let index_infos: Vec<(Vec<usize>, Arc<dyn Index>)> = indexes
            .values()
            .filter_map(|idx| {
                let col_ids = idx.column_ids();
                if col_ids.is_empty() {
                    return None;
                }
                let col_indices: Vec<usize> = col_ids.iter().map(|&id| id as usize).collect();
                Some((col_indices, Arc::clone(idx)))
            })
            .collect();

        drop(indexes); // Release lock before iteration

        if index_infos.is_empty() {
            return;
        }

        // Pre-allocate per-index batch vectors
        let num_indexes = index_infos.len();
        let mut batches: Vec<Vec<(i64, Vec<crate::core::Value>)>> =
            (0..num_indexes).map(|_| Vec::new()).collect();

        // First pass: Collect all entries per index
        let versions = self.versions.read().clone();
        for (&row_id, version_chain) in versions.iter() {
            let version = &version_chain.version;

            if version.is_deleted() {
                continue;
            }

            // Collect entries for each index
            for (idx, (col_indices, _)) in index_infos.iter().enumerate() {
                if col_indices.len() == 1 {
                    // Single-column index
                    if let Some(value) = version.data.get(col_indices[0]) {
                        batches[idx].push((row_id, vec![value.clone()]));
                    }
                } else {
                    // Multi-column index
                    let values: Vec<crate::core::Value> =
                        col_indices
                            .iter()
                            .map(|&col_idx| {
                                version.data.get(col_idx).cloned().unwrap_or(
                                    crate::core::Value::Null(crate::core::DataType::Null),
                                )
                            })
                            .collect();
                    batches[idx].push((row_id, values));
                }
            }
        }

        // Second pass: Apply batch operations per index
        // This reduces lock acquisitions from O(rows × indexes) to O(indexes)
        for (idx, (_, index)) in index_infos.iter().enumerate() {
            if !batches[idx].is_empty() {
                let entry_refs: Vec<(i64, &[crate::core::Value])> = batches[idx]
                    .iter()
                    .map(|(row_id, values)| (*row_id, values.as_slice()))
                    .collect();
                let _ = index.add_batch_slice(&entry_refs);
            }
        }
    }

    /// Populate HNSW indexes from external rows (e.g., cold segment data).
    ///
    /// Regular indexes (B-tree, Hash, Bitmap) are hot-only by design and use
    /// zone maps/bloom filters for cold data. HNSW indexes are different:
    /// vector similarity search cannot fall back to zone maps, so HNSW must
    /// contain all rows (hot + cold) to return correct results.
    ///
    /// HnswInner::insert skips duplicate row_ids, so calling this after
    /// populate_all_indexes() is safe (hot rows already in the index).
    pub fn populate_hnsw_from_rows(&self, rows: &[(i64, crate::core::Row)]) {
        let indexes = self.indexes.read();
        if indexes.is_empty() || rows.is_empty() {
            return;
        }

        // Collect only HNSW indexes with their column indices
        let hnsw_infos: Vec<(Vec<usize>, Arc<dyn Index>)> = indexes
            .values()
            .filter(|idx| idx.index_type() == crate::core::IndexType::Hnsw)
            .filter_map(|idx| {
                let col_ids = idx.column_ids();
                if col_ids.is_empty() {
                    return None;
                }
                let col_indices: Vec<usize> = col_ids.iter().map(|&id| id as usize).collect();
                Some((col_indices, Arc::clone(idx)))
            })
            .collect();

        drop(indexes);

        if hnsw_infos.is_empty() {
            return;
        }

        // Pre-allocate per-index batch vectors
        let mut batches: Vec<Vec<(i64, Vec<crate::core::Value>)>> = (0..hnsw_infos.len())
            .map(|_| Vec::with_capacity(rows.len()))
            .collect();

        for &(row_id, ref row) in rows {
            for (idx, (col_indices, _)) in hnsw_infos.iter().enumerate() {
                if col_indices.len() == 1 {
                    if let Some(value) = row.get(col_indices[0]) {
                        batches[idx].push((row_id, vec![value.clone()]));
                    }
                } else {
                    let values: Vec<crate::core::Value> = col_indices
                        .iter()
                        .map(|&col_idx| {
                            row.get(col_idx)
                                .cloned()
                                .unwrap_or(crate::core::Value::Null(crate::core::DataType::Null))
                        })
                        .collect();
                    batches[idx].push((row_id, values));
                }
            }
        }

        for (idx, (_, index)) in hnsw_infos.iter().enumerate() {
            if !batches[idx].is_empty() {
                let entry_refs: Vec<(i64, &[crate::core::Value])> = batches[idx]
                    .iter()
                    .map(|(row_id, values)| (*row_id, values.as_slice()))
                    .collect();
                let _ = index.add_batch_slice(&entry_refs);
            }
        }
    }

    // =========================================================================
    // Cleanup Functions
    // =========================================================================

    /// Remove sealed rows from the hot version store (phase 1 of seal).
    /// Removes version data and arena slots but KEEPS hot index entries.
    /// The stale index entries act as a safety net: unique constraint checks
    /// still find them, preventing duplicate inserts during the seal window
    /// when the row has moved to cold but might not yet be visible to a
    /// cold check that took a snapshot before register_volume.
    ///
    /// Callers MUST also call `subtract_committed_row_count(n)` with the
    /// returned count to keep the committed row count accurate.
    ///
    /// `extraction_snapshot` is an O(1) CowBTree clone taken at the moment
    /// rows were extracted. For each row_id, the removal compares the
    /// current head version's `txn_id` against the extraction snapshot's
    /// `txn_id`. If they differ, a concurrent commit published a newer
    /// version after extraction — that row is skipped (stays in hot,
    /// sealed next cycle). This prevents discarding concurrent updates
    /// that the sealed volume doesn't contain.
    /// Returns `(removed_count, index_cleanup, skipped_row_ids)`.
    /// Skipped row_ids are rows that were modified after extraction — the
    /// caller must tombstone them so recovery and row_count are correct.
    pub fn remove_sealed_rows(
        &self,
        row_ids: &[i64],
        extraction_snapshot: &ExtractionSnapshot,
    ) -> (usize, SealedIndexCleanup, Vec<i64>) {
        if row_ids.is_empty() {
            return (0, SealedIndexCleanup::default(), Vec::new());
        }

        // Snapshot version data BEFORE removal for later index cleanup.
        // O(1) Arc clone of CowBTree.
        let snapshot = self.versions.read().clone();

        // Remove rows in small sub-batches to reduce write lock hold time.
        // Each sub-batch acquires versions.write() briefly, then releases it,
        // giving concurrent commits a chance to proceed between sub-batches.
        // Without this, a 50K-row seal batch holds the write lock for the
        // entire removal, blocking all commits on this table for ~100ms+.
        const SUB_BATCH_SIZE: usize = 2_000;
        let mut removed_ids: Vec<i64> = Vec::with_capacity(row_ids.len());
        let mut skipped_ids: Vec<i64> = Vec::new();
        let mut arena_indices_to_clear: Vec<usize> = Vec::new();

        for chunk in row_ids.chunks(SUB_BATCH_SIZE) {
            let uncommitted = self.uncommitted_writes.read();
            let mut versions = self.versions.write();
            for &row_id in chunk {
                if uncommitted.contains_key(row_id) {
                    skipped_ids.push(row_id);
                    continue;
                }
                if let Some(entry) = versions.get(row_id) {
                    // Compare current txn_id against extraction-time txn_id.
                    // If they differ, a concurrent commit changed this row
                    // after we extracted it — the sealed volume has stale data
                    // for this row. Keep the newer version in hot.
                    let extracted_txn_id = extraction_snapshot
                        .inner
                        .get(row_id)
                        .map(|e| e.version.txn_id)
                        .unwrap_or(0);
                    if entry.version.txn_id != extracted_txn_id {
                        skipped_ids.push(row_id);
                        continue;
                    }
                    if let Some(idx) = unpack_arena_idx(entry.arena_idx) {
                        arena_indices_to_clear.push(idx);
                    }
                    versions.remove(row_id);
                    removed_ids.push(row_id);
                }
            }
            // Locks released here — concurrent commits can proceed
        }

        // Invalidate sealed arena slots so speculative probes don't return
        // stale data. This sets row_id=0 in the meta, making the probe fail.
        if !arena_indices_to_clear.is_empty() {
            self.arena.clear_batch(&arena_indices_to_clear);
        }

        let count = removed_ids.len();
        (
            count,
            SealedIndexCleanup {
                removed_ids,
                snapshot: Some(snapshot),
            },
            skipped_ids,
        )
    }

    /// Remove stale hot index entries for sealed rows (phase 2 of seal).
    /// Called while the table's seal fence is still held so INSERT cannot race
    /// between cold constraint checks and hot-index cleanup.
    pub fn remove_sealed_index_entries(&self, cleanup: SealedIndexCleanup) {
        if cleanup.removed_ids.is_empty() {
            return;
        }

        let Some(ref snap) = cleanup.snapshot else {
            return;
        };

        let indexes = self.indexes.read();
        let hot_only_indexes: Vec<_> = indexes
            .values()
            .filter(|idx| idx.index_type() != crate::core::IndexType::Hnsw)
            .cloned()
            .collect();
        drop(indexes);

        if hot_only_indexes.is_empty() {
            return;
        }

        for index in &hot_only_indexes {
            let col_ids = index.column_ids();
            let mut owned_entries: Vec<(i64, Vec<crate::core::Value>)> =
                Vec::with_capacity(cleanup.removed_ids.len());

            for &row_id in &cleanup.removed_ids {
                let Some(entry) = snap.get(row_id) else {
                    continue;
                };
                let row = &entry.version.data;
                let values: Vec<crate::core::Value> = col_ids
                    .iter()
                    .map(|&col_id| {
                        row.get(col_id as usize)
                            .cloned()
                            .unwrap_or(crate::core::Value::Null(crate::core::DataType::Null))
                    })
                    .collect();
                owned_entries.push((row_id, values));
            }

            if owned_entries.is_empty() {
                continue;
            }

            let borrowed_entries: Vec<(i64, &[crate::core::Value])> = owned_entries
                .iter()
                .map(|(row_id, values)| (*row_id, values.as_slice()))
                .collect();
            let _ = index.remove_batch_slice(&borrowed_entries);
        }
    }

    /// Cleanup deleted rows that are older than the retention period.
    ///
    /// This removes soft-deleted rows that are no longer visible to any active
    /// transaction and are older than the specified retention period.
    /// Also clears the arena slots to release memory.
    pub fn cleanup_deleted_rows(&self, retention_period: std::time::Duration) -> i32 {
        if self.closed.load(Ordering::Acquire) {
            return 0;
        }

        let now = get_fast_timestamp();
        let cutoff_time = now - retention_period.as_nanos() as i64;

        let mut rows_to_delete = Vec::new();

        // Clone CowBTree once, reuse for index cleanup (O(1) Arc clone)
        let versions = self.versions.read().clone();

        // First pass: identify deleted rows older than retention period
        for (&row_id, chain) in versions.iter() {
            let version = &chain.version;
            // Only process rows that are actually deleted and old enough
            if version.is_deleted() && version.create_time < cutoff_time {
                // Check if safe to remove (no active transaction can see it)
                if self.can_safely_remove(version) {
                    rows_to_delete.push(row_id);
                }
            }
        }

        if rows_to_delete.is_empty() {
            return 0;
        }

        // Second pass: acquire write lock and re-validate before removing.
        // Between the snapshot (first pass) and now, a concurrent transaction may have
        // committed a new version for a row_id we marked for deletion (e.g., re-INSERT
        // with the same PK value). We must re-check that each row is still deleted
        // before removing it to prevent data loss.
        // Arena indices are read directly from the live entry under the write lock
        // to avoid index misalignment with the rows_to_delete vector.
        let mut actually_deleted = Vec::with_capacity(rows_to_delete.len());
        let mut actual_arena_indices = Vec::with_capacity(rows_to_delete.len());
        {
            let mut versions = self.versions.write();
            for &row_id in &rows_to_delete {
                // Re-check: the row must still exist AND still be deleted
                if let Some(entry) = versions.get(row_id) {
                    if entry.version.is_deleted() {
                        if let Some(idx) = unpack_arena_idx(entry.arena_idx) {
                            actual_arena_indices.push(idx);
                        }
                        versions.remove(row_id);
                        actually_deleted.push(row_id);
                    }
                }
            }
        }

        if actually_deleted.is_empty() {
            return 0;
        }

        // Third pass: remove from indexes using batch operations (single lock per index)
        // This runs AFTER the version store removal so we use the snapshot data
        // (which is still valid for the rows we confirmed were deleted).
        {
            let indexes = self.indexes.read();

            for index in indexes.values() {
                let column_ids = index.column_ids();
                if column_ids.is_empty() {
                    continue;
                }

                // Collect all entries for this index
                let mut entries: Vec<(i64, Vec<crate::core::Value>)> =
                    Vec::with_capacity(actually_deleted.len());

                for &row_id in &actually_deleted {
                    if let Some(entry) = versions.get(row_id) {
                        let version = &entry.version;
                        if column_ids.len() == 1 {
                            // Single-column index
                            let col_id = column_ids[0] as usize;
                            if let Some(value) = version.data.get(col_id) {
                                entries.push((row_id, vec![value.clone()]));
                            }
                        } else {
                            // Multi-column index
                            let values: Vec<crate::core::Value> = column_ids
                                .iter()
                                .map(|&col_id| {
                                    version.data.get(col_id as usize).cloned().unwrap_or(
                                        crate::core::Value::Null(crate::core::DataType::Null),
                                    )
                                })
                                .collect();
                            entries.push((row_id, values));
                        }
                    }
                }

                if !entries.is_empty() {
                    // Convert to slice format for remove_batch_slice
                    let batch: Vec<(i64, &[crate::core::Value])> = entries
                        .iter()
                        .map(|(row_id, values)| (*row_id, values.as_slice()))
                        .collect();
                    let _ = index.remove_batch_slice(&batch);
                }

                // Let index-specific maintenance run (e.g., HNSW graph compaction)
                let _ = index.cleanup();
            }
        }

        // Clear arena slots only for rows we actually removed
        self.arena.clear_batch(&actual_arena_indices);

        actually_deleted.len() as i32
    }

    /// Check if a version can be safely removed (not visible to any active transaction)
    fn can_safely_remove(&self, version: &RowVersion) -> bool {
        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return true, // No checker, assume safe
        };

        // Get all active transaction IDs
        let active_txns = checker.get_active_transaction_ids();

        // If no active transactions, safe to remove
        if active_txns.is_empty() {
            return true;
        }

        // Check if any active transaction can still see this version
        for txn_id in active_txns {
            if checker.is_visible(version.txn_id, txn_id) {
                return false; // Still visible to at least one transaction
            }
        }

        true
    }

    /// Cleanup old previous versions that are no longer needed
    ///
    /// This prunes old version chains, keeping only versions that are:
    /// 1. Needed by active transactions
    /// 2. Within the retention period (for AS OF TIMESTAMP queries)
    pub fn cleanup_old_previous_versions(&self) -> i32 {
        // Default 24-hour retention for background cleanup
        self.cleanup_old_previous_versions_with_retention(std::time::Duration::from_secs(
            24 * 60 * 60,
        ))
    }

    pub fn cleanup_old_previous_versions_with_retention(
        &self,
        retention_period: std::time::Duration,
    ) -> i32 {
        if self.closed.load(Ordering::Acquire) {
            return 0;
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return 0, // Need visibility checker for cleanup
        };

        let now = get_fast_timestamp();
        let retention_cutoff = now - retention_period.as_nanos() as i64;

        // Get active transaction IDs
        let active_txns = checker.get_active_transaction_ids();

        // First pass (read lock): identify row_ids that MAY need pruning
        let mut candidate_row_ids: Vec<i64> = Vec::new();
        {
            let versions = self.versions.read().clone();
            for (&row_id, chain_entry) in versions.iter() {
                // Quick check: does this entry have any prev versions?
                if chain_entry.prev.is_none() {
                    continue;
                }
                candidate_row_ids.push(row_id);
            }
        }

        if candidate_row_ids.is_empty() {
            return 0;
        }

        // Second pass (write lock): re-read each entry from live data and prune in-place.
        // This prevents the race where a concurrent commit adds a new HEAD between
        // the snapshot read and the write — we always work on the current live entry.
        let mut cleaned = 0;
        let mut versions = self.versions.write();

        for row_id in candidate_row_ids {
            let Some(chain_entry) = versions.get(row_id) else {
                continue; // Row was removed between passes
            };

            // Collect previous versions from the LIVE entry
            let mut prev_versions: Vec<Arc<VersionChainEntry>> = Vec::new();
            let mut current = chain_entry.prev.as_ref();
            while let Some(prev_entry) = current {
                prev_versions.push(prev_entry.clone());
                current = prev_entry.prev.as_ref();
            }

            if prev_versions.is_empty() {
                continue;
            }

            // Check each previous version independently — do NOT assume monotonic
            // visibility. With rapid updates, a newer prev version may be invisible
            // to an active txn while an older one IS visible (e.g., HEAD seq=120,
            // prev_0 seq=110, prev_1 seq=80, active txn snapshot at seq=100 needs prev_1).
            let mut keep_count = 0;
            for (i, prev_entry) in prev_versions.iter().enumerate() {
                let mut keep = false;

                // Rule 1: Keep if needed by any active transaction
                for &txn_id in &active_txns {
                    if checker.is_visible(prev_entry.version.txn_id, txn_id) {
                        keep = true;
                        break;
                    }
                }

                // Rule 2: Keep if within retention period
                if !keep && prev_entry.version.create_time >= retention_cutoff {
                    keep = true;
                }

                if keep {
                    // Keep this version and all newer ones (indices 0..=i)
                    keep_count = i + 1;
                }
            }

            // If we need to prune some versions, modify the live entry
            if keep_count < prev_versions.len() {
                let to_remove = prev_versions.len() - keep_count;
                cleaned += to_remove as i32;

                // Clone the LIVE entry (not stale snapshot) and modify
                let mut modified_entry = chain_entry.clone();

                if keep_count == 0 {
                    modified_entry.prev = None;
                } else {
                    // Rebuild chain with only kept versions
                    let kept_versions: Vec<_> =
                        prev_versions.into_iter().take(keep_count).collect();

                    // Build chain from oldest to newest (reversed)
                    let mut new_prev: Option<Arc<VersionChainEntry>> = None;
                    for entry in kept_versions.into_iter().rev() {
                        let mut cloned = (*entry).clone();
                        cloned.prev = new_prev;
                        new_prev = Some(Arc::new(cloned));
                    }
                    modified_entry.prev = new_prev;
                }

                versions.insert(row_id, modified_entry);
            }
        }

        cleaned
    }

    /// Iterate over all committed (non-deleted) versions for snapshot creation
    ///
    /// This method iterates over all rows that are visible to a snapshot transaction
    /// (i.e., all committed, non-deleted rows). The callback receives the row_id and
    /// a reference to the RowVersion. Return false from the callback to stop iteration.
    ///
    /// This is designed for creating point-in-time snapshots to disk.
    pub fn for_each_committed_version<F>(&self, callback: F)
    where
        F: FnMut(i64, &RowVersion) -> bool,
    {
        // Delegate to the cutoff version with no cutoff (0 means no filtering)
        self.for_each_committed_version_with_cutoff(callback, 0);
    }

    /// Iterate over committed versions with a commit sequence cutoff for consistent snapshots
    ///
    /// This is the same as `for_each_committed_version` but only includes transactions
    /// that were committed before the given `commit_seq_cutoff`. This ensures consistent
    /// point-in-time snapshots even when new transactions commit during iteration.
    ///
    /// # Arguments
    /// * `callback` - Called for each visible, non-deleted version
    /// * `commit_seq_cutoff` - Only include transactions with commit_seq < cutoff (0 = no filter)
    pub fn for_each_committed_version_with_cutoff<F>(&self, mut callback: F, commit_seq_cutoff: i64)
    where
        F: FnMut(i64, &RowVersion) -> bool,
    {
        if self.closed.load(Ordering::Acquire) {
            return;
        }

        // Get visibility checker for determining committed status
        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return,
        };

        // Use a very high txn_id to see all committed rows
        let snapshot_txn_id = i64::MAX;
        let use_cutoff = commit_seq_cutoff > 0;

        // Iterate all versions
        let versions = self.versions.read().clone();
        for (&row_id, chain_entry) in versions.iter() {
            // Walk the version chain to find the visible version
            let mut current: Option<&VersionChainEntry> = Some(chain_entry);

            while let Some(e) = current {
                let version_txn_id = e.version.txn_id;
                let deleted_at_txn_id = e.version.deleted_at_txn_id;

                // Check if this version is visible
                if checker.is_visible(version_txn_id, snapshot_txn_id) {
                    // When cutoff is specified, only include versions from transactions
                    // that committed before the cutoff to ensure snapshot consistency.
                    // CRITICAL: Do NOT walk the prev chain. The extraction snapshot
                    // captures the HEAD txn_id, and remove_sealed_rows compares against
                    // it. If we extract an older version but the HEAD txn_id matches,
                    // the HEAD is removed from hot, permanently losing the newer version.
                    // The row stays entirely in hot where MVCC handles visibility.
                    if use_cutoff && !checker.is_committed_before(version_txn_id, commit_seq_cutoff)
                    {
                        break;
                    }

                    // Skip if deleted and deletion is visible (and within cutoff if specified)
                    if deleted_at_txn_id != 0
                        && checker.is_visible(deleted_at_txn_id, snapshot_txn_id)
                        && (!use_cutoff
                            || checker.is_committed_before(deleted_at_txn_id, commit_seq_cutoff))
                    {
                        break; // Row is deleted, skip
                    }

                    // Found visible, non-deleted version
                    if !callback(row_id, &e.version) {
                        return; // Callback wants to stop
                    }
                    break;
                }

                // Try older version
                current = e.prev.as_ref().map(|arc| arc.as_ref());
            }
        }
    }

    /// Get the count of committed (non-deleted) versions for statistics
    pub fn count_committed_versions(&self) -> usize {
        if self.closed.load(Ordering::Acquire) {
            return 0;
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return 0,
        };

        let snapshot_txn_id = i64::MAX;
        let mut count = 0;

        let versions = self.versions.read().clone();
        for (_, chain_entry) in versions.iter() {
            let mut current: Option<&VersionChainEntry> = Some(chain_entry);

            while let Some(e) = current {
                if checker.is_visible(e.version.txn_id, snapshot_txn_id) {
                    if e.version.deleted_at_txn_id == 0
                        || !checker.is_visible(e.version.deleted_at_txn_id, snapshot_txn_id)
                    {
                        count += 1;
                    }
                    break;
                }
                current = e.prev.as_ref().map(|arc| arc.as_ref());
            }
        }

        count
    }

    /// Get the transaction ID of the latest committed version for a row.
    ///
    /// This retrieves the "head" of the version chain, effectively checking
    /// the most recently committed change. This is critical for conflict
    /// detection (First-Committer-Wins).
    ///
    /// Returns:
    /// - Some(txn_id) if the row exists
    /// - None if the row does not exist
    pub fn get_latest_version_id(&self, row_id: i64) -> Option<i64> {
        if self.closed.load(Ordering::Acquire) {
            return None;
        }

        let versions = self.versions.read();
        versions.get(row_id).map(|entry| entry.version.txn_id)
    }

    /// Compute grouped aggregates directly from arena storage.
    ///
    /// This method performs GROUP BY aggregation at the storage level without
    /// materializing Row objects. It uses Arc::clone for group keys (O(1))
    /// instead of Value::clone (deep copy), significantly reducing allocations.
    ///
    /// # Arguments
    /// * `txn_id` - Transaction ID for visibility checks
    /// * `group_by_indices` - Column indices to group by
    /// * `aggregates` - List of (operation, column_index) pairs
    ///
    /// # Returns
    /// Vector of grouped aggregate results, or empty if optimization not possible
    pub fn compute_grouped_aggregates(
        &self,
        txn_id: i64,
        group_by_indices: &[usize],
        aggregates: &[(AggregateOp, usize)],
    ) -> Option<Vec<GroupedAggregateResult>> {
        if self.closed.load(Ordering::Acquire) {
            return Some(Vec::new());
        }

        let checker = match self.visibility_checker.as_ref() {
            Some(c) => c,
            None => return Some(Vec::new()),
        };

        // Guard: arena-only path requires ReadCommitted and no uncommitted writes.
        // Under snapshot isolation, HEAD versions in the arena may have been committed
        // after the viewer's snapshot. The correct behavior requires walking version
        // chains to find older visible versions, which the arena path doesn't support.
        // Return None to let the caller fall back to regular GROUP BY aggregation.
        let uncommitted_empty = self.uncommitted_writes.read().is_empty();
        if !uncommitted_empty || checker.needs_snapshot_isolation(txn_id) {
            return None;
        }

        // Accumulator for each group: (count, sum, min, max) per aggregate
        #[derive(Clone)]
        struct Accum {
            count: i64,
            int_sum: i128,
            float_sum: f64,
            min: Option<Value>,
            max: Option<Value>,
        }

        impl Default for Accum {
            fn default() -> Self {
                Self {
                    count: 0,
                    int_sum: 0,
                    float_sum: 0.0,
                    min: None,
                    max: None,
                }
            }
        }

        // Pre-acquire arena lock ONCE
        let arena_guard = self.arena.read_guard();
        let arena_data = arena_guard.data();
        let arena_meta = arena_guard.meta();

        // Helper to update accumulators
        #[inline(always)]
        fn update_accums(
            accums: &mut [Accum],
            aggregates: &[(AggregateOp, usize)],
            row_data: &[Value],
        ) {
            for (agg_idx, (op, col_idx)) in aggregates.iter().enumerate() {
                let accum = &mut accums[agg_idx];

                match op {
                    AggregateOp::CountStar => {
                        accum.count += 1;
                    }
                    AggregateOp::Count => {
                        if *col_idx < row_data.len() && !row_data[*col_idx].is_null() {
                            accum.count += 1;
                        }
                    }
                    AggregateOp::Sum | AggregateOp::Avg => {
                        if *col_idx < row_data.len() {
                            match &row_data[*col_idx] {
                                Value::Integer(i) => {
                                    accum.int_sum += *i as i128;
                                    accum.count += 1;
                                }
                                Value::Float(f) => {
                                    accum.float_sum += *f;
                                    accum.count += 1;
                                }
                                _ => {}
                            }
                        }
                    }
                    AggregateOp::Min => {
                        if *col_idx < row_data.len() {
                            let val = &row_data[*col_idx];
                            if !val.is_null() {
                                match &accum.min {
                                    None => accum.min = Some(val.clone()),
                                    Some(current) => {
                                        if val < current {
                                            accum.min = Some(val.clone());
                                        }
                                    }
                                }
                            }
                        }
                    }
                    AggregateOp::Max => {
                        if *col_idx < row_data.len() {
                            let val = &row_data[*col_idx];
                            if !val.is_null() {
                                match &accum.max {
                                    None => accum.max = Some(val.clone()),
                                    Some(current) => {
                                        if val > current {
                                            accum.max = Some(val.clone());
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // Helper to compute final aggregate values
        fn compute_aggregate_values(
            aggregates: &[(AggregateOp, usize)],
            accums: &[Accum],
        ) -> Vec<Value> {
            aggregates
                .iter()
                .zip(accums.iter())
                .map(|((op, _), accum)| match op {
                    AggregateOp::Count | AggregateOp::CountStar => Value::Integer(accum.count),
                    AggregateOp::Sum => {
                        if accum.count > 0 {
                            Value::Float(accum.int_sum as f64 + accum.float_sum)
                        } else {
                            Value::Null(DataType::Float)
                        }
                    }
                    AggregateOp::Avg => {
                        if accum.count > 0 {
                            Value::Float(
                                (accum.int_sum as f64 + accum.float_sum) / accum.count as f64,
                            )
                        } else {
                            Value::Null(DataType::Float)
                        }
                    }
                    AggregateOp::Min => accum.min.clone().unwrap_or(Value::Null(DataType::Null)),
                    AggregateOp::Max => accum.max.clone().unwrap_or(Value::Null(DataType::Null)),
                })
                .collect()
        }

        // FAST PATH: Single-column GROUP BY with primitive types (Integer, Float, Boolean)
        // Uses I64Map directly for ~3x faster hashing and comparison
        // Avoids GroupKey allocation entirely for primitive types and NULL
        if group_by_indices.len() == 1 {
            let col_idx = group_by_indices[0];

            // Track key type: 0=Integer, 1=Float, 2=Boolean, 3=Mixed/Other
            let mut key_type: u8 = 255; // uninitialized
            let mut int_groups: I64Map<Vec<Accum>> = I64Map::new();
            // Separate NULL accumulator - avoids creating GroupKey for NULL
            let mut null_accums: Option<Vec<Accum>> = None;
            // Only used for String/other types that can't be mapped to i64
            let mut other_groups: GroupKeyMap<Vec<Accum>> = GroupKeyMap::default();

            for (idx, meta) in arena_meta.iter().enumerate() {
                // Visibility check (standard pattern: check creation, then deletion)
                if meta.txn_id == 0 || !checker.is_visible(meta.txn_id, txn_id) {
                    continue;
                }
                if meta.deleted_at_txn_id != 0 && checker.is_visible(meta.deleted_at_txn_id, txn_id)
                {
                    continue;
                }

                let row_data = match arena_data.get(idx) {
                    Some(data) => data,
                    None => continue,
                };

                let row_slice = row_data.as_ref();
                let val = if col_idx < row_slice.len() {
                    &row_slice[col_idx]
                } else {
                    // NULL - track separately without GroupKey allocation
                    let accums =
                        null_accums.get_or_insert_with(|| vec![Accum::default(); aggregates.len()]);
                    update_accums(accums, aggregates, row_slice);
                    continue;
                };

                // Try to extract i64 key for primitive types.
                // Only route to int_groups when the type matches key_type.
                // Mixed-type columns route mismatched types to other_groups
                // to avoid reconstructing Float bits as Integer or vice versa.
                let i64_key = match val {
                    Value::Integer(i) => {
                        if key_type == 255 {
                            key_type = 0;
                        }
                        if key_type == 0 {
                            Some(*i)
                        } else {
                            None // Type mismatch → other_groups
                        }
                    }
                    Value::Float(f) => {
                        if key_type == 255 {
                            key_type = 1;
                        }
                        if key_type == 1 {
                            let bits = f.to_bits() as i64;
                            // i64::MIN is I64Map's empty sentinel — route to other_groups
                            // This only affects -0.0 (bits = 0x8000000000000000)
                            if bits == i64::MIN {
                                None
                            } else {
                                Some(bits)
                            }
                        } else {
                            None // Type mismatch → other_groups
                        }
                    }
                    Value::Boolean(b) => {
                        if key_type == 255 {
                            key_type = 2;
                        }
                        if key_type == 2 {
                            Some(if *b { 1 } else { 0 })
                        } else {
                            None // Type mismatch → other_groups
                        }
                    }
                    Value::Null(_) => {
                        // NULL value in the column - track separately
                        let accums = null_accums
                            .get_or_insert_with(|| vec![Accum::default(); aggregates.len()]);
                        update_accums(accums, aggregates, row_slice);
                        continue;
                    }
                    _ => None,
                };

                if let Some(key) = i64_key {
                    let accums = int_groups
                        .entry(key)
                        .or_insert_with(|| vec![Accum::default(); aggregates.len()]);
                    update_accums(accums, aggregates, row_slice);
                } else {
                    let accums = other_groups
                        .entry(GroupKey::Single(CompactArc::new(val.clone())))
                        .or_insert_with(|| vec![Accum::default(); aggregates.len()]);
                    update_accums(accums, aggregates, row_slice);
                }
            }

            // Convert to results
            let has_null = null_accums.is_some();
            let mut results: Vec<GroupedAggregateResult> = Vec::with_capacity(
                int_groups.len() + other_groups.len() + if has_null { 1 } else { 0 },
            );

            // Convert int_groups based on key_type
            for (key, accums) in int_groups.iter() {
                let group_value = match key_type {
                    0 => Value::Integer(key),                      // Integer
                    1 => Value::Float(f64::from_bits(key as u64)), // Float
                    2 => Value::Boolean(key != 0),                 // Boolean
                    _ => Value::Integer(key),                      // Fallback
                };
                results.push(GroupedAggregateResult {
                    group_values: vec![group_value],
                    aggregate_values: compute_aggregate_values(aggregates, accums),
                });
            }

            // Add NULL group if present
            if let Some(accums) = null_accums {
                results.push(GroupedAggregateResult {
                    group_values: vec![Value::Null(DataType::Null)],
                    aggregate_values: compute_aggregate_values(aggregates, &accums),
                });
            }

            // Convert other_groups (strings, etc.)
            for (group_key, accums) in other_groups {
                let group_values = match group_key {
                    GroupKey::Single(v) => vec![(*v).clone()],
                    GroupKey::Multi(vs) => vs.iter().map(|v| (**v).clone()).collect(),
                };
                results.push(GroupedAggregateResult {
                    group_values,
                    aggregate_values: compute_aggregate_values(aggregates, &accums),
                });
            }

            return Some(results);
        }

        // SLOW PATH: Multi-column GROUP BY (currently not used from try_storage_aggregation)
        let mut groups: GroupKeyMap<Vec<Accum>> = GroupKeyMap::default();

        for (idx, meta) in arena_meta.iter().enumerate() {
            // Visibility check (standard pattern: check creation, then deletion)
            if meta.txn_id == 0 || !checker.is_visible(meta.txn_id, txn_id) {
                continue;
            }
            if meta.deleted_at_txn_id != 0 && checker.is_visible(meta.deleted_at_txn_id, txn_id) {
                continue;
            }

            let row_data = match arena_data.get(idx) {
                Some(data) => data,
                None => continue,
            };
            let row_slice = row_data.as_ref();

            let key_values: Vec<CompactArc<Value>> = group_by_indices
                .iter()
                .map(|&col_idx| {
                    if col_idx < row_slice.len() {
                        CompactArc::new(row_slice[col_idx].clone())
                    } else {
                        CompactArc::new(Value::Null(DataType::Null))
                    }
                })
                .collect();
            let group_key = GroupKey::Multi(key_values);

            let accums = groups
                .entry(group_key)
                .or_insert_with(|| vec![Accum::default(); aggregates.len()]);
            update_accums(accums, aggregates, row_slice);
        }

        // Convert to results
        let mut results: Vec<GroupedAggregateResult> = Vec::with_capacity(groups.len());

        for (group_key, accums) in groups {
            let group_values = match group_key {
                GroupKey::Single(v) => vec![(*v).clone()],
                GroupKey::Multi(vs) => vs.iter().map(|v| (**v).clone()).collect(),
            };
            results.push(GroupedAggregateResult {
                group_values,
                aggregate_values: compute_aggregate_values(aggregates, &accums),
            });
        }

        Some(results)
    }
}

impl Clone for VersionChainEntry {
    fn clone(&self) -> Self {
        Self {
            version: self.version.clone(),
            prev: self.prev.clone(), // Arc clone is O(1)
            arena_idx: self.arena_idx,
        }
    }
}

impl fmt::Debug for VersionStore {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("VersionStore")
            .field("table_name", &self.table_name)
            .field("row_count", &self.row_count())
            .field("closed", &self.closed.load(Ordering::Acquire))
            .finish()
    }
}

/// Transaction-local version store for uncommitted changes
pub struct TransactionVersionStore {
    /// Local versions for this transaction - stores version history per row for savepoint support
    /// The list is ordered by create_time (oldest first, newest last)
    /// Lazily allocated on first write to avoid allocation overhead for read-only queries.
    /// Uses SmallVec<[RowVersion; 1]> to avoid heap allocation for single-version rows.
    local_versions: Option<I64Map<VersionList>>,
    /// Parent (shared) version store
    parent_store: Arc<VersionStore>,
    /// This transaction's ID
    txn_id: i64,
    /// Write set for conflict detection
    /// Lazily allocated on first write to avoid allocation overhead for read-only queries
    write_set: Option<I64Map<WriteSetEntry>>,
}

impl TransactionVersionStore {
    /// Creates a new transaction-local version store
    ///
    /// Uses lazy allocation for local_versions and write_set maps to avoid
    /// allocation overhead for read-only queries. These are only allocated
    /// when the first write operation occurs.
    pub fn new(parent_store: Arc<VersionStore>, txn_id: i64) -> Self {
        Self {
            // Lazy allocation - maps are created on first write
            local_versions: None,
            parent_store,
            txn_id,
            write_set: None,
        }
    }

    /// Returns the transaction ID
    pub fn txn_id(&self) -> i64 {
        self.txn_id
    }

    /// Read-only access to local versions for rollback cleanup.
    pub fn local_versions_ref(&self) -> Option<&I64Map<VersionList>> {
        self.local_versions.as_ref()
    }

    /// Read-only access to write set for commit-time cold revalidation.
    pub fn write_set_ref(&self) -> Option<&I64Map<WriteSetEntry>> {
        self.write_set.as_ref()
    }

    /// Ensures local_versions map is allocated, returning a mutable reference.
    /// Uses pooled maps when available to reduce allocation overhead.
    #[inline]
    fn ensure_local_versions(&mut self) -> &mut I64Map<VersionList> {
        self.local_versions.get_or_insert_with(get_version_list_map)
    }

    /// Ensures write_set map is allocated, returning a mutable reference.
    /// Uses pooled maps when available to reduce allocation overhead.
    #[inline]
    fn ensure_write_set(&mut self) -> &mut I64Map<WriteSetEntry> {
        self.write_set.get_or_insert_with(get_write_set_map)
    }

    /// Put adds or updates a row in the transaction's local store
    pub fn put(&mut self, row_id: i64, data: Row, is_delete: bool) -> Result<(), Error> {
        // Convert to Shared (Arc) storage immediately for efficient Arc sharing:
        // - get_arc() will return cheap Arc clones (no value cloning)
        // - into_arc() at commit time returns the existing Arc (no clone)
        let data = Row::from_arc(data.into_arc());

        // Get timestamp once at the start (avoids calling SystemTime::now() inside RowVersion::new)
        let timestamp = get_fast_timestamp();

        // Create the row version with pre-computed timestamp
        let mut rv = RowVersion::new_with_timestamp(self.txn_id, data, timestamp);
        if is_delete {
            rv.deleted_at_txn_id = self.txn_id;
        }

        // Check if we already have a local version for this row
        let has_local = self
            .local_versions
            .as_ref()
            .is_some_and(|lv| lv.contains_key(row_id));

        if has_local {
            // Already have local version - just append
            self.ensure_local_versions()
                .get_mut(row_id)
                .unwrap()
                .push(rv);
        } else {
            // New row - need to check write-set and parent store
            let needs_write_set_entry = self
                .write_set
                .as_ref()
                .is_none_or(|ws| !ws.contains_key(row_id));

            if needs_write_set_entry {
                let read_version = self.parent_store.get_visible_version(row_id, self.txn_id);
                let row_exists = read_version.is_some();

                let read_version_seq = self
                    .parent_store
                    .visibility_checker
                    .as_ref()
                    .map(|c| c.get_current_sequence())
                    .unwrap_or(0);

                self.ensure_write_set().insert(
                    row_id,
                    WriteSetEntry {
                        read_version,
                        read_version_seq,
                    },
                );

                // For existing rows, try to claim them
                if row_exists {
                    self.parent_store.try_claim_row(row_id, self.txn_id)?;
                }
            }
            self.ensure_local_versions().insert(row_id, smallvec![rv]);
        }
        Ok(())
    }

    /// Batch put for UPDATE operations where we already have the row data
    ///
    /// This is used for rows that are already tracked in local_versions (updates within same txn)
    /// or when we don't have pre-fetched original versions.
    pub fn put_batch_for_update(&mut self, rows: RowVec) -> Result<(), Error> {
        for (row_id, data) in rows {
            self.put(row_id, data, false)?;
        }
        Ok(())
    }

    /// Optimized single-row put with pre-fetched original version
    ///
    /// This avoids redundant get_visible_version() calls by accepting the original
    /// version that was already fetched during the read phase.
    /// Used for PK-based UPDATE operations.
    #[inline]
    pub fn put_with_original(
        &mut self,
        row_id: i64,
        data: Row,
        original_version: RowVersion,
        is_delete: bool,
    ) -> Result<(), Error> {
        // Convert to Shared (Arc) storage immediately for efficient Arc sharing
        let data = Row::from_arc(data.into_arc());

        // Get timestamp once at the start (avoids calling SystemTime::now() inside RowVersion::new)
        let timestamp = get_fast_timestamp();

        // Create the new row version with pre-computed timestamp
        let mut rv = RowVersion::new_with_timestamp(self.txn_id, data, timestamp);
        if is_delete {
            rv.deleted_at_txn_id = self.txn_id;
        }

        // Check if we already have a local version for this row
        let has_local = self
            .local_versions
            .as_ref()
            .is_some_and(|lv| lv.contains_key(row_id));

        if has_local {
            // Already have local version - just append
            self.ensure_local_versions()
                .get_mut(row_id)
                .unwrap()
                .push(rv);
        } else {
            // Track in write-set using the pre-fetched original version
            let needs_write_set_entry = self
                .write_set
                .as_ref()
                .is_none_or(|ws| !ws.contains_key(row_id));

            if needs_write_set_entry {
                let read_version_seq = self
                    .parent_store
                    .visibility_checker
                    .as_ref()
                    .map(|c| c.get_current_sequence())
                    .unwrap_or(0);

                self.ensure_write_set().insert(
                    row_id,
                    WriteSetEntry {
                        read_version: Some(original_version),
                        read_version_seq,
                    },
                );

                // Claim the row for update
                self.parent_store.try_claim_row(row_id, self.txn_id)?;
            }
            self.ensure_local_versions().insert(row_id, smallvec![rv]);
        }
        Ok(())
    }

    /// Optimized batch put for UPDATE operations with pre-fetched original versions
    ///
    /// This avoids redundant get_visible_version() calls by accepting the original
    /// versions that were already fetched during the batch read.
    ///
    /// Parameters:
    /// - rows: Vec of (row_id, new_row_data, original_version)
    ///   Note: read_version_seq is obtained from visibility_checker, not from create_time
    pub fn put_batch_with_originals(
        &mut self,
        rows: Vec<(i64, Row, RowVersion)>,
    ) -> Result<(), Error> {
        let now = get_fast_timestamp();

        for (row_id, data, original_version) in rows {
            // Convert to Shared (Arc) storage immediately for efficient Arc sharing
            let data = Row::from_arc(data.into_arc());

            // Create the new row version with pre-computed timestamp (avoids wasteful
            // get_fast_timestamp() call inside RowVersion::new that would be overwritten)
            let rv = RowVersion::new_with_timestamp(self.txn_id, data, now);

            // Check if already in local versions (already processed in this transaction)
            if let Some(versions) = self.ensure_local_versions().get_mut(row_id) {
                // Append new version to history
                versions.push(rv);
                continue;
            }

            // Track in write-set using the pre-fetched original version
            let needs_write_set_entry = self
                .write_set
                .as_ref()
                .is_none_or(|ws| !ws.contains_key(row_id));

            if needs_write_set_entry {
                // Get current sequence for conflict detection
                // Note: We get a fresh sequence instead of relying on create_time because
                // callers may use get_visible_version() which doesn't set create_time to the sequence
                let read_version_seq = self
                    .parent_store
                    .visibility_checker
                    .as_ref()
                    .map(|c| c.get_current_sequence())
                    .unwrap_or(0);
                self.ensure_write_set().insert(
                    row_id,
                    WriteSetEntry {
                        read_version: Some(original_version),
                        read_version_seq,
                    },
                );

                // Claim the row for update
                self.parent_store.try_claim_row(row_id, self.txn_id)?;
            }

            // Insert new version history for this row
            self.ensure_local_versions().insert(row_id, smallvec![rv]);
        }
        Ok(())
    }

    /// Optimized batch delete for DELETE operations
    ///
    /// This marks multiple rows as deleted in a single operation, avoiding
    /// the overhead of individual put() calls with lock acquisitions per row.
    ///
    /// Parameters:
    /// - rows: RowVec of (row_id, row_data) to mark as deleted
    pub fn put_batch_deleted(&mut self, rows: RowVec) -> Result<(), Error> {
        // Get timestamp once for all rows in the batch
        let timestamp = get_fast_timestamp();

        for (row_id, data) in rows {
            // Check if we already have a local version
            let has_local = self
                .local_versions
                .as_ref()
                .is_some_and(|lv| lv.contains_key(row_id));

            if !has_local {
                // Check if this row exists in parent store and track in write-set
                let needs_write_set_entry = self
                    .write_set
                    .as_ref()
                    .is_none_or(|ws| !ws.contains_key(row_id));

                if needs_write_set_entry {
                    let read_version = self.parent_store.get_visible_version(row_id, self.txn_id);
                    let row_exists = read_version.is_some();

                    let read_version_seq = self
                        .parent_store
                        .visibility_checker
                        .as_ref()
                        .map(|c| c.get_current_sequence())
                        .unwrap_or(0);

                    self.ensure_write_set().insert(
                        row_id,
                        WriteSetEntry {
                            read_version,
                            read_version_seq,
                        },
                    );

                    // For existing rows, try to claim them
                    if row_exists {
                        self.parent_store.try_claim_row(row_id, self.txn_id)?;
                    }
                }
            }

            // Create deleted row version with pre-computed timestamp
            let mut rv = RowVersion::new_with_timestamp(self.txn_id, data, timestamp);
            rv.deleted_at_txn_id = self.txn_id;

            // Append to version history for this row
            let local_versions = self.ensure_local_versions();
            if let Some(versions) = local_versions.get_mut(row_id) {
                versions.push(rv);
            } else {
                local_versions.insert(row_id, smallvec![rv]);
            }
        }
        Ok(())
    }

    /// Optimized batch delete with pre-fetched original versions
    ///
    /// This avoids redundant get_visible_version() calls by accepting the original
    /// versions that were already fetched during the read phase.
    /// Used for PK range DELETE operations.
    pub fn put_batch_deleted_with_originals(
        &mut self,
        rows: Vec<(i64, Row, RowVersion)>,
    ) -> Result<(), Error> {
        // Get timestamp once for all rows in the batch
        let timestamp = get_fast_timestamp();

        for (row_id, data, original_version) in rows {
            // Create deleted row version with pre-computed timestamp
            let mut rv = RowVersion::new_with_timestamp(self.txn_id, data, timestamp);
            rv.deleted_at_txn_id = self.txn_id;

            // Check if already in local versions (already processed in this transaction)
            if let Some(versions) = self.ensure_local_versions().get_mut(row_id) {
                versions.push(rv);
                continue;
            }

            // Track in write-set using the pre-fetched original version
            let needs_write_set_entry = self
                .write_set
                .as_ref()
                .is_none_or(|ws| !ws.contains_key(row_id));

            if needs_write_set_entry {
                let read_version_seq = self
                    .parent_store
                    .visibility_checker
                    .as_ref()
                    .map(|c| c.get_current_sequence())
                    .unwrap_or(0);

                self.ensure_write_set().insert(
                    row_id,
                    WriteSetEntry {
                        read_version: Some(original_version),
                        read_version_seq,
                    },
                );

                // Claim the row for delete
                self.parent_store.try_claim_row(row_id, self.txn_id)?;
            }

            // Insert deleted version for this row
            self.ensure_local_versions().insert(row_id, smallvec![rv]);
        }
        Ok(())
    }

    /// Check if we have local changes for a row
    pub fn has_locally_seen(&self, row_id: i64) -> bool {
        self.local_versions
            .as_ref()
            .is_some_and(|lv| lv.contains_key(row_id))
    }

    /// Returns true if this transaction has any uncommitted local changes
    pub fn has_local_changes(&self) -> bool {
        self.local_versions
            .as_ref()
            .is_some_and(|lv| !lv.is_empty())
    }

    /// Returns the number of local changes (distinct row IDs)
    pub fn local_count(&self) -> usize {
        self.local_versions.as_ref().map_or(0, |lv| lv.len())
    }

    /// Get the latest local version for a specific row, if any.
    #[inline]
    pub fn get_latest_local(&self, row_id: i64) -> Option<&RowVersion> {
        self.local_versions
            .as_ref()
            .and_then(|lv| lv.get(row_id))
            .and_then(|versions| versions.last())
    }

    /// Iterate over local versions (returns most recent version per row)
    pub fn iter_local(&self) -> impl Iterator<Item = (i64, &RowVersion)> {
        self.local_versions
            .iter()
            .flat_map(|lv| lv.iter())
            .filter_map(|(k, versions)| versions.last().map(|v| (k, v)))
    }

    /// Iterate over local versions with their original (old) versions for index updates
    /// Returns (row_id, new_version, old_row_option)
    pub fn iter_local_with_old(&self) -> impl Iterator<Item = (i64, &RowVersion, Option<&Row>)> {
        let write_set_ref = self.write_set.as_ref();
        self.local_versions
            .iter()
            .flat_map(|lv| lv.iter())
            .filter_map(move |(row_id, versions)| {
                versions.last().map(|version| {
                    let old_row = write_set_ref
                        .and_then(|ws| ws.get(row_id))
                        .and_then(|entry| entry.read_version.as_ref())
                        .filter(|v| !v.is_deleted())
                        .map(|v| &v.data);
                    (row_id, version, old_row)
                })
            })
    }

    /// Get the local version for a row (without checking parent)
    /// Returns the most recent version in the transaction's history
    pub fn get_local_version(&self, row_id: i64) -> Option<&RowVersion> {
        self.local_versions
            .as_ref()
            .and_then(|lv| lv.get(row_id))
            .and_then(|versions| versions.last())
    }

    /// Get a row, checking local versions first then parent store
    pub fn get(&self, row_id: i64) -> Option<Row> {
        // Check local versions first (get most recent)
        if let Some(lv) = self.local_versions.as_ref() {
            if let Some(versions) = lv.get(row_id) {
                if let Some(local_version) = versions.last() {
                    if local_version.is_deleted() {
                        return None;
                    }
                    return Some(local_version.data.clone());
                }
            }
        }

        // Check parent store
        self.parent_store
            .get_visible_version(row_id, self.txn_id)
            .map(|v| v.data.clone())
    }

    /// OCC validation that tolerates seal-removed rows.
    ///
    /// If a row is missing from the hot B-tree, it was moved to cold by seal.
    /// This is not a conflict for READ COMMITTED transactions. The transaction
    /// read the row data into its local txn_versions before seal removed it.
    /// On commit, the updated version is re-inserted into hot, and the
    /// skip-set dedup mechanism ensures the stale cold version is shadowed.
    /// Seal also skips rows claimed by active transactions (uncommitted_writes).
    ///
    /// For INSERT paths (read_version is None), we still check whether another
    /// transaction concurrently inserted the same row_id to prevent lost writes
    /// when explicit PK values are used.
    pub fn detect_conflicts_safe(&self) -> Result<(), Error> {
        let Some(write_set) = self.write_set.as_ref() else {
            return Ok(());
        };

        for (row_id, write_entry) in write_set.iter() {
            if let Some(read_version) = &write_entry.read_version {
                // UPDATE path: check that the row hasn't been modified concurrently
                match self.parent_store.get_latest_version_id(row_id) {
                    Some(latest_txn_id) => {
                        if latest_txn_id != read_version.txn_id {
                            return Err(Error::internal(format!(
                                "write conflict: row {} was modified by another transaction",
                                row_id
                            )));
                        }
                    }
                    None => {
                        // Row is absent from the hot B-tree. This happens when seal
                        // moved the row to cold storage between our read and commit.
                        // This is NOT a real conflict — the row data is unchanged,
                        // just relocated. Skip the conflict check for this row.
                        // The commit will add the updated version to hot, and the
                        // skip-set mechanism at scan time handles dedup with cold.
                    }
                }
            } else {
                // INSERT path: check that no other transaction concurrently inserted
                // the same row_id. This guards against TOCTOU races with explicit PK values.
                // For auto-increment IDs this is effectively a no-op (AtomicI64 guarantees
                // uniqueness), but it is the safety net for user-supplied PKs.
                if self
                    .parent_store
                    .has_any_visible_version(&[row_id], self.txn_id)
                    .is_some()
                {
                    return Err(Error::internal(format!(
                        "write conflict: row {} was concurrently inserted by another transaction",
                        row_id
                    )));
                }
            }
        }

        Ok(())
    }

    /// Prepare commit - returns list of versions to commit (most recent per row)
    pub fn prepare_commit(&self) -> Vec<(i64, RowVersion)> {
        let Some(local_versions) = self.local_versions.as_ref() else {
            return Vec::new();
        };

        let mut versions = Vec::new();
        for (row_id, version_history) in local_versions.iter() {
            // Only commit the most recent version per row
            if let Some(version) = version_history.last() {
                versions.push((row_id, version.clone()));
            }
        }
        versions
    }

    /// Commit local changes to parent store
    ///
    /// Performance: This method drains local_versions to take ownership of
    /// RowVersion values, avoiding expensive clones. The transaction is
    /// consumed after commit anyway, so this is safe.
    pub fn commit(&mut self) -> Result<(), Error> {
        // OCC validation: detect concurrent write conflicts.
        // Rows removed by seal (missing from hot B-tree) are not conflicts —
        // they were moved to cold segments, not modified by another transaction.
        self.detect_conflicts_safe()?;

        // Update indexes BEFORE committing versions
        self.update_indexes_on_commit()?;

        // Commit local versions to parent store
        if let Some(local_versions) = self.local_versions.as_mut() {
            if local_versions.len() == 1 {
                // Single-row fast path: avoid Vec allocation
                if let Some((row_id, mut versions)) = local_versions.drain().next() {
                    if let Some(version) = versions.pop() {
                        self.parent_store.add_version_single(row_id, version);
                    }
                }
            } else {
                // Multi-row path: collect into Vec
                let mut batch: Vec<(i64, RowVersion)> = local_versions
                    .drain()
                    .filter_map(|(row_id, mut versions)| versions.pop().map(|v| (row_id, v)))
                    .collect();

                // Sort by row_id to ensure deterministic locking order
                batch.sort_by_key(|(row_id, _)| *row_id);

                self.parent_store.add_versions_batch(batch);
            }
        }

        // Release ALL claims from write_set (includes both local and external claims).
        // Must drain the entire write_set — external claims from track_external_claim()
        // have no corresponding local_versions entry.
        if let Some(write_set) = self.write_set.as_mut() {
            if write_set.len() == 1 {
                // Single-claim fast path: avoid Vec allocation
                if let Some((row_id, _)) = write_set.drain().next() {
                    self.parent_store.release_row_claim(row_id, self.txn_id);
                }
            } else {
                let mut row_ids: Vec<i64> = write_set.drain().map(|(row_id, _)| row_id).collect();
                // Sort by row_id to ensure deterministic locking order
                row_ids.sort_unstable();
                self.parent_store
                    .release_row_claims_batch(&row_ids, self.txn_id);
            }
        }

        Ok(())
    }

    /// Update indexes during commit
    ///
    /// This method updates all indexes with the changes from this transaction.
    /// For each row being committed:
    /// - If there's an old version (UPDATE/DELETE), remove the old indexed values
    /// - If the new version is not deleted (INSERT/UPDATE), add the new indexed values
    ///
    /// Uses two paths:
    /// - Single-row fast path: SmallVec + immediate apply (zero heap allocation for 1-2 column indexes)
    /// - Multi-row batch path: Collects changes, then applies in batch (reduces lock acquisitions)
    ///
    /// Returns an error if a unique constraint is violated.
    fn update_indexes_on_commit(&self) -> Result<(), Error> {
        // Early exit if no local changes
        let Some(local_versions) = self.local_versions.as_ref() else {
            return Ok(());
        };

        if local_versions.is_empty() {
            return Ok(());
        }

        // Get all indexes - early exit if none
        let indexes: SmallVec<[Arc<dyn Index>; 4]> =
            self.parent_store.get_all_indexes().into_iter().collect();
        if indexes.is_empty() {
            return Ok(());
        }

        // FAST PATH: Single-row commit (most common case for auto-commit INSERT/UPDATE/DELETE)
        // Uses SmallVec to avoid heap allocation for 1-2 column indexes
        if local_versions.len() == 1 {
            return self.update_indexes_single_row(&indexes);
        }

        // BATCH PATH: Multi-row commit
        // Sort indexes by name for deterministic lock ordering (prevents deadlocks)
        let mut indexes: Vec<_> = indexes.into_vec();
        indexes.sort_by(|a, b| a.name().cmp(b.name()));

        let num_indexes = indexes.len();

        // Pre-allocate per-index batch vectors
        let mut add_batches: Vec<Vec<(i64, Vec<crate::core::Value>)>> =
            (0..num_indexes).map(|_| Vec::new()).collect();
        let mut remove_batches: Vec<Vec<(i64, Vec<crate::core::Value>)>> =
            (0..num_indexes).map(|_| Vec::new()).collect();

        // Collect index updates for each row
        for (row_id, versions) in local_versions.iter() {
            // Get the latest version for this row (last in the list)
            let Some(new_version) = versions.last() else {
                continue;
            };

            let is_deleted = new_version.is_deleted();
            let new_row = &new_version.data;

            // Get old version from write_set (if exists)
            let old_row: Option<&crate::core::Row> = self
                .write_set
                .as_ref()
                .and_then(|ws| ws.get(row_id))
                .and_then(|entry| entry.read_version.as_ref())
                .map(|rv| &rv.data);

            for (idx, index) in indexes.iter().enumerate() {
                let column_ids = index.column_ids();
                if column_ids.is_empty() {
                    continue;
                }

                // OPTIMIZATION: For UPDATEs, check if any indexed column changed
                // BEFORE allocating Vecs
                if let Some(old_r) = old_row {
                    if !is_deleted {
                        // UPDATE case: check if indexed columns differ
                        let any_changed = column_ids.iter().any(|&col_id| {
                            let col_idx = col_id as usize;
                            let old_val = old_r.get(col_idx);
                            let new_val = new_row.get(col_idx);
                            old_val != new_val
                        });

                        if !any_changed {
                            // Indexed columns unchanged - skip this index
                            continue;
                        }

                        // Values differ - collect old and new values
                        let old_values: Vec<crate::core::Value> = column_ids
                            .iter()
                            .map(|&col_id| {
                                old_r.get(col_id as usize).cloned().unwrap_or(
                                    crate::core::Value::Null(crate::core::DataType::Null),
                                )
                            })
                            .collect();

                        let new_values: Vec<crate::core::Value> = column_ids
                            .iter()
                            .map(|&col_id| {
                                new_row.get(col_id as usize).cloned().unwrap_or(
                                    crate::core::Value::Null(crate::core::DataType::Null),
                                )
                            })
                            .collect();

                        remove_batches[idx].push((row_id, old_values));
                        add_batches[idx].push((row_id, new_values));
                        continue;
                    }
                }

                if is_deleted {
                    // DELETE: collect values to remove from index
                    // Use old_row if available, otherwise fall back to new_row
                    let source_row = old_row.unwrap_or(new_row);
                    let values_to_remove: Vec<crate::core::Value> = column_ids
                        .iter()
                        .map(|&col_id| {
                            source_row
                                .get(col_id as usize)
                                .cloned()
                                .unwrap_or(crate::core::Value::Null(crate::core::DataType::Null))
                        })
                        .collect();
                    remove_batches[idx].push((row_id, values_to_remove));
                } else {
                    // INSERT: collect values to add to index
                    // (old_row.is_some() cases with !is_deleted are handled above with continue)
                    let new_values: Vec<crate::core::Value> = column_ids
                        .iter()
                        .map(|&col_id| {
                            new_row
                                .get(col_id as usize)
                                .cloned()
                                .unwrap_or(crate::core::Value::Null(crate::core::DataType::Null))
                        })
                        .collect();
                    add_batches[idx].push((row_id, new_values));
                }
            }
        }

        // PHASE 1: Pre-validate ALL unique indexes before modifying ANY index
        // This prevents index pollution when a later index fails
        for (idx, index) in indexes.iter().enumerate() {
            if !index.is_unique() || add_batches[idx].is_empty() {
                continue;
            }

            // Build a set of row_ids being removed for O(1) conflict resolution
            // For unique indexes, if a row_id is being removed, its current value is being removed.
            // We don't need to track values - just knowing the row_id is sufficient.
            let removals_set: crate::common::I64Set = remove_batches[idx]
                .iter()
                .map(|(row_id, _)| *row_id)
                .collect();

            // Check for intra-batch duplicates first
            let mut seen: ahash::AHashMap<&[crate::core::Value], i64> =
                ahash::AHashMap::with_capacity(add_batches[idx].len());

            let is_hnsw = index.index_type() == crate::core::IndexType::Hnsw;

            for (row_id, values) in &add_batches[idx] {
                // Skip NULLs - they don't violate uniqueness
                if values.iter().any(|v| v.is_null()) {
                    continue;
                }

                // Check intra-batch duplicates (applies to all index types, including HNSW)
                if let Some(&existing_row_id) = seen.get(values.as_slice()) {
                    if existing_row_id != *row_id {
                        let values_str: Vec<String> =
                            values.iter().map(|v| format!("{:?}", v)).collect();
                        return Err(Error::unique_constraint(
                            index.name(),
                            index.column_names().join(", "),
                            format!("[{}]", values_str.join(", ")),
                        ));
                    }
                }
                seen.insert(values.as_slice(), *row_id);

                if is_hnsw {
                    // HNSW uniqueness: use exact vector-byte duplicate check.
                    // This is metric-independent and avoids threshold-based false negatives.
                    let Some(hnsw_index) = index
                        .as_any()
                        .downcast_ref::<crate::storage::index::HnswIndex>()
                    else {
                        return Err(Error::internal(format!(
                            "index '{}' advertised HNSW type but cannot be downcast",
                            index.name()
                        )));
                    };

                    if let Some(value) = values.first() {
                        if let Some(existing_row_id) =
                            hnsw_index.find_exact_duplicate(value, *row_id, Some(&removals_set))
                        {
                            let dims = value.as_vector_f32().map_or(0, |v| v.len());
                            return Err(Error::unique_constraint(
                                index.name(),
                                index.column_names().join(", "),
                                format!(
                                    "<vector({} dims)> conflicts with row_id {}",
                                    dims, existing_row_id
                                ),
                            ));
                        }
                    }
                } else {
                    // Non-HNSW: standard equality-based uniqueness check
                    // Check against existing index entries
                    let existing = index.get_row_ids_equal(values);

                    // Identify potential conflicts (exclude self and rows being removed)
                    // O(1) lookup using removals_set instead of O(N) scan
                    let has_real_conflict = existing.iter().any(|&conflict_id| {
                        conflict_id != *row_id && !removals_set.contains(conflict_id)
                    });

                    if has_real_conflict {
                        let values_str: Vec<String> =
                            values.iter().map(|v| format!("{:?}", v)).collect();
                        return Err(Error::unique_constraint(
                            index.name(),
                            index.column_names().join(", "),
                            format!("[{}]", values_str.join(", ")),
                        ));
                    }
                }
            }
        }

        // PHASE 2: All validations passed - now modify all indexes
        // Track modifications for rollback if a later index fails (race condition protection)
        // Between Phase 1 validation and Phase 2 modification, another transaction could commit,
        // causing a unique constraint violation that wasn't detected in Phase 1.
        let mut completed_removals: Vec<usize> = Vec::new();
        let mut completed_additions: Vec<usize> = Vec::new();

        for (idx, index) in indexes.iter().enumerate() {
            // Remove old entries first (for UPDATE correctness)
            if !remove_batches[idx].is_empty() {
                let batch: Vec<(i64, &[crate::core::Value])> = remove_batches[idx]
                    .iter()
                    .map(|(row_id, values)| (*row_id, values.as_slice()))
                    .collect();
                // Note: remove_batch_slice can fail (e.g., I/O error), but typically succeeds
                if index.remove_batch_slice(&batch).is_ok() {
                    completed_removals.push(idx);
                }
            }

            // Add new entries
            if !add_batches[idx].is_empty() {
                let batch: Vec<(i64, &[crate::core::Value])> = add_batches[idx]
                    .iter()
                    .map(|(row_id, values)| (*row_id, values.as_slice()))
                    .collect();

                if let Err(e) = index.add_batch_slice(&batch) {
                    // ROLLBACK: A later index failed - undo all previous modifications
                    // This prevents index corruption where indexes point to uncommitted rows

                    // 1. Remove additions we made to earlier indexes
                    for &rollback_idx in &completed_additions {
                        let rollback_batch: Vec<(i64, &[crate::core::Value])> = add_batches
                            [rollback_idx]
                            .iter()
                            .map(|(row_id, values)| (*row_id, values.as_slice()))
                            .collect();
                        let _ = indexes[rollback_idx].remove_batch_slice(&rollback_batch);
                    }

                    // 2. Re-add removals we made to earlier indexes
                    for &rollback_idx in &completed_removals {
                        let rollback_batch: Vec<(i64, &[crate::core::Value])> = remove_batches
                            [rollback_idx]
                            .iter()
                            .map(|(row_id, values)| (*row_id, values.as_slice()))
                            .collect();
                        // Rollback additions - ignore errors as we're in error recovery
                        let _ = indexes[rollback_idx].add_batch_slice(&rollback_batch);
                    }

                    return Err(e);
                }
                completed_additions.push(idx);
            }
        }

        Ok(())
    }

    /// Fast path for single-row index updates
    ///
    /// Uses SmallVec to avoid heap allocation for indexes with 1-2 columns (most common case).
    /// Applies changes immediately without batching overhead.
    fn update_indexes_single_row(&self, indexes: &[Arc<dyn Index>]) -> Result<(), Error> {
        let local_versions = self.local_versions.as_ref().unwrap();

        // Get the single row entry
        let Some((row_id, versions)) = local_versions.iter().next() else {
            return Ok(());
        };

        let Some(new_version) = versions.last() else {
            return Ok(());
        };

        let is_deleted = new_version.is_deleted();
        let new_row = &new_version.data;

        // Get old version from write_set (if exists)
        let old_row: Option<&Row> = self
            .write_set
            .as_ref()
            .and_then(|ws| ws.get(row_id))
            .and_then(|entry| entry.read_version.as_ref())
            .map(|rv| &rv.data);

        // Track what we've done for rollback on error
        let mut completed_ops: SmallVec<[(usize, bool); 4]> = SmallVec::new(); // (index_idx, is_add)

        for (idx, index) in indexes.iter().enumerate() {
            let column_ids = index.column_ids();
            if column_ids.is_empty() {
                continue;
            }

            // OPTIMIZATION: For UPDATEs, check if any indexed column changed BEFORE allocating
            if let Some(old_r) = old_row {
                if !is_deleted {
                    // UPDATE case: check if indexed columns differ (no allocation)
                    let any_changed = column_ids.iter().any(|&col_id| {
                        let col_idx = col_id as usize;
                        old_r.get(col_idx) != new_row.get(col_idx)
                    });

                    if !any_changed {
                        // Indexed columns unchanged - skip this index entirely
                        continue;
                    }

                    // Values differ - collect and update using SmallVec (stack allocation for 1-2 cols)
                    let old_values: SmallVec<[Value; 2]> = column_ids
                        .iter()
                        .map(|&col_id| {
                            old_r
                                .get(col_id as usize)
                                .cloned()
                                .unwrap_or(Value::Null(DataType::Null))
                        })
                        .collect();

                    let new_values: SmallVec<[Value; 2]> = column_ids
                        .iter()
                        .map(|&col_id| {
                            new_row
                                .get(col_id as usize)
                                .cloned()
                                .unwrap_or(Value::Null(DataType::Null))
                        })
                        .collect();

                    // Remove old, add new
                    let _ = index.remove(&old_values, row_id, row_id);
                    completed_ops.push((idx, false)); // false = removal

                    if let Err(e) = index.add(&new_values, row_id, row_id) {
                        // Rollback: re-add the old value we removed
                        let _ = index.add(&old_values, row_id, row_id);
                        // Rollback previous indexes
                        self.rollback_single_row_ops(
                            &completed_ops,
                            indexes,
                            row_id,
                            old_row,
                            new_row,
                        );
                        return Err(e);
                    }
                    completed_ops.push((idx, true)); // true = addition
                    continue;
                }
            }

            if is_deleted {
                // DELETE: remove from index
                let source_row = old_row.unwrap_or(new_row);
                let values_to_remove: SmallVec<[Value; 2]> = column_ids
                    .iter()
                    .map(|&col_id| {
                        source_row
                            .get(col_id as usize)
                            .cloned()
                            .unwrap_or(Value::Null(DataType::Null))
                    })
                    .collect();
                let _ = index.remove(&values_to_remove, row_id, row_id);
                completed_ops.push((idx, false));
            } else {
                // INSERT: add values to index
                let new_values: SmallVec<[Value; 2]> = column_ids
                    .iter()
                    .map(|&col_id| {
                        new_row
                            .get(col_id as usize)
                            .cloned()
                            .unwrap_or(Value::Null(DataType::Null))
                    })
                    .collect();

                if let Err(e) = index.add(&new_values, row_id, row_id) {
                    // Rollback previous indexes
                    self.rollback_single_row_ops(&completed_ops, indexes, row_id, old_row, new_row);
                    return Err(e);
                }
                completed_ops.push((idx, true));
            }
        }

        Ok(())
    }

    /// Rollback helper for single-row operations
    #[inline]
    fn rollback_single_row_ops(
        &self,
        completed_ops: &[(usize, bool)],
        indexes: &[Arc<dyn Index>],
        row_id: i64,
        old_row: Option<&Row>,
        new_row: &Row,
    ) {
        for &(idx, is_add) in completed_ops.iter().rev() {
            let index = &indexes[idx];
            let column_ids = index.column_ids();

            if is_add {
                // We added new values - remove them
                let values: SmallVec<[Value; 2]> = column_ids
                    .iter()
                    .map(|&col_id| {
                        new_row
                            .get(col_id as usize)
                            .cloned()
                            .unwrap_or(Value::Null(DataType::Null))
                    })
                    .collect();
                let _ = index.remove(&values, row_id, row_id);
            } else {
                // We removed old values - re-add them
                let source = old_row.unwrap_or(new_row);
                let values: SmallVec<[Value; 2]> = column_ids
                    .iter()
                    .map(|&col_id| {
                        source
                            .get(col_id as usize)
                            .cloned()
                            .unwrap_or(Value::Null(DataType::Null))
                    })
                    .collect();
                let _ = index.add(&values, row_id, row_id);
            }
        }
    }

    /// Rollback - discard local changes and release claims
    pub fn rollback(&self) {
        self.release_all_claims();
    }

    /// Rollback to a specific timestamp (for savepoint support)
    ///
    /// Discards all local changes that were made after the given timestamp.
    /// For rows with version history, keeps versions at or before the timestamp.
    /// Row claims are released only if all versions for that row are discarded.
    pub fn rollback_to_timestamp(&mut self, timestamp: i64) {
        let Some(local_versions) = self.local_versions.as_mut() else {
            return;
        };

        let mut rows_to_remove_completely: Vec<i64> = Vec::new();

        // For each row, remove versions with create_time > timestamp
        for (row_id, versions) in local_versions.iter_mut() {
            // Keep only versions at or before the timestamp
            versions.retain(|v| v.create_time <= timestamp);

            // If all versions are removed, mark for complete removal
            if versions.is_empty() {
                rows_to_remove_completely.push(row_id);
            }
        }

        // Remove rows with no remaining versions and release their claims
        for row_id in &rows_to_remove_completely {
            local_versions.remove(*row_id);
            self.parent_store.release_row_claim(*row_id, self.txn_id);
            if let Some(write_set) = self.write_set.as_mut() {
                write_set.remove(*row_id);
            }
        }
    }

    /// Track a claim made directly on the parent VersionStore (not through put()).
    /// Used by SegmentedTable for cold row UPDATE/DELETE claims that bypass
    /// TransactionVersionStore's put methods. Without tracking, these claims
    /// leak because commit() only releases claims found in write_set.
    pub fn track_external_claim(&mut self, row_id: i64) {
        let write_set = self.ensure_write_set();
        // Only add if not already tracked (idempotent).
        // Use empty read_version since this is a cold-only claim — the actual
        // row data lives in cold storage, not in the hot version store.
        use crate::common::i64_map::Entry;
        if let Entry::Vacant(e) = write_set.entry(row_id) {
            e.insert(WriteSetEntry {
                read_version: None,
                read_version_seq: 0,
            });
        }
    }

    /// Release all row claims held by this transaction
    fn release_all_claims(&self) {
        let Some(write_set) = self.write_set.as_ref() else {
            return;
        };
        // OPTIMIZATION: Collect row_ids first, then batch release
        // Avoids holding write_set iterator while accessing parent_store
        let mut row_ids: Vec<i64> = write_set.keys().collect();
        // Sort by row_id to ensure deterministic locking order
        row_ids.sort_unstable();
        self.parent_store
            .release_row_claims_batch(&row_ids, self.txn_id);
    }
}

impl Drop for TransactionVersionStore {
    fn drop(&mut self) {
        // Release any row claims still held by this transaction.
        // This is a safety net for cases where drop happens without explicit
        // commit/rollback (e.g., transaction panics, implicit drop on scope exit).
        // Without this, claims in uncommitted_writes would leak permanently,
        // blocking future UPDATE/DELETE on those rows.
        self.release_all_claims();

        // Return maps to the pool for reuse by future transactions.
        // This reduces allocation overhead from ~5.5KB per transaction to near zero
        // for bulk insert workloads where many short-lived transactions are created.
        if let Some(map) = self.local_versions.take() {
            return_version_list_map(map);
        }
        if let Some(map) = self.write_set.take() {
            return_write_set_map(map);
        }
    }
}

impl fmt::Debug for TransactionVersionStore {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TransactionVersionStore")
            .field("txn_id", &self.txn_id)
            .field(
                "local_version_count",
                &self.local_versions.as_ref().map_or(0, |lv| lv.len()),
            )
            .field(
                "write_set_count",
                &self.write_set.as_ref().map_or(0, |ws| ws.len()),
            )
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::Value;
    use std::sync::atomic::AtomicI64;

    /// Simple visibility checker for testing
    struct TestVisibilityChecker {
        current_seq: AtomicI64,
    }

    impl TestVisibilityChecker {
        fn new() -> Self {
            Self {
                current_seq: AtomicI64::new(0),
            }
        }
    }

    impl VisibilityChecker for TestVisibilityChecker {
        fn is_visible(&self, version_txn_id: i64, viewing_txn_id: i64) -> bool {
            // Simple rule: a version is visible if it was created by a transaction
            // with a lower or equal ID (simplified for testing)
            version_txn_id <= viewing_txn_id
        }

        fn get_current_sequence(&self) -> i64 {
            self.current_seq.fetch_add(1, Ordering::AcqRel)
        }

        fn get_active_transaction_ids(&self) -> Vec<i64> {
            // No active transactions in test
            Vec::new()
        }
    }

    /// Visibility checker that reports snapshot isolation, causing arena fast
    /// paths to be bypassed and version chain traversal to be used.
    struct SnapshotVisibilityChecker {
        current_seq: AtomicI64,
    }

    impl SnapshotVisibilityChecker {
        fn new() -> Self {
            Self {
                current_seq: AtomicI64::new(0),
            }
        }
    }

    impl VisibilityChecker for SnapshotVisibilityChecker {
        fn is_visible(&self, version_txn_id: i64, viewing_txn_id: i64) -> bool {
            version_txn_id <= viewing_txn_id
        }

        fn get_current_sequence(&self) -> i64 {
            self.current_seq.fetch_add(1, Ordering::AcqRel)
        }

        fn get_active_transaction_ids(&self) -> Vec<i64> {
            Vec::new()
        }

        fn needs_snapshot_isolation(&self, _txn_id: i64) -> bool {
            true
        }
    }

    #[test]
    fn test_row_version_creation() {
        let row = Row::from(vec![Value::from(1), Value::from("test")]);
        let version = RowVersion::new(1, row);

        assert_eq!(version.txn_id, 1);
        assert!(!version.is_deleted());
        assert!(version.create_time > 0);
    }

    #[test]
    fn test_row_version_deleted() {
        let row = Row::from(vec![Value::from(1)]);
        let version = RowVersion::new_deleted(1, row);

        assert!(version.is_deleted());
        assert_eq!(version.deleted_at_txn_id, 1);
    }

    use crate::core::SchemaBuilder;

    fn test_schema() -> Schema {
        SchemaBuilder::new("test_table").build()
    }

    #[test]
    fn test_version_store_auto_increment() {
        let store = VersionStore::new("test_table".to_string(), test_schema());

        assert_eq!(store.get_current_auto_increment_value(), 0);
        assert_eq!(store.get_next_auto_increment_id(), 1);
        assert_eq!(store.get_next_auto_increment_id(), 2);
        assert_eq!(store.get_current_auto_increment_value(), 2);
    }

    #[test]
    fn test_version_store_set_auto_increment() {
        let store = VersionStore::new("test_table".to_string(), test_schema());

        assert!(store.set_auto_increment_counter(10));
        assert_eq!(store.get_current_auto_increment_value(), 10);

        // Should not go backwards
        assert!(!store.set_auto_increment_counter(5));
        assert_eq!(store.get_current_auto_increment_value(), 10);

        // Should update if higher
        assert!(store.set_auto_increment_counter(20));
        assert_eq!(store.get_current_auto_increment_value(), 20);
    }

    #[test]
    fn test_version_store_add_and_get() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        let row = Row::from(vec![Value::from(42)]);
        let version = RowVersion::new(1, row);

        store.add_version(100, version);

        // Transaction 2 should see version from transaction 1
        let visible = store.get_visible_version(100, 2);
        assert!(visible.is_some());
        assert_eq!(visible.unwrap().txn_id, 1);
    }

    #[test]
    fn test_version_store_visibility() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add version from transaction 5
        let row = Row::from(vec![Value::from(42)]);
        let version = RowVersion::new(5, row);
        store.add_version(100, version);

        // Transaction 3 should NOT see version from transaction 5
        let visible = store.get_visible_version(100, 3);
        assert!(visible.is_none());

        // Transaction 5 should see its own version
        let visible = store.get_visible_version(100, 5);
        assert!(visible.is_some());

        // Transaction 10 should see version from transaction 5
        let visible = store.get_visible_version(100, 10);
        assert!(visible.is_some());
    }

    #[test]
    fn test_version_store_deleted_row() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add version from transaction 1
        let row = Row::from(vec![Value::from(42)]);
        let version = RowVersion::new(1, row.clone());
        store.add_version(100, version);

        // Delete in transaction 2
        let deleted_version = RowVersion::new_deleted(2, row);
        store.add_version(100, deleted_version);

        // Transaction 1 should still see the row (delete not visible)
        let visible = store.get_visible_version(100, 1);
        assert!(visible.is_some());

        // Transaction 3 should NOT see the deleted row
        let visible = store.get_visible_version(100, 3);
        assert!(visible.is_none());
    }

    #[test]
    fn test_version_store_row_ids() {
        let store = VersionStore::new("test_table".to_string(), test_schema());

        let row = Row::from(vec![Value::from(1)]);
        store.add_version(100, RowVersion::new(1, row.clone()));
        store.add_version(200, RowVersion::new(1, row.clone()));
        store.add_version(300, RowVersion::new(1, row));

        let row_ids = store.get_all_row_ids();
        assert_eq!(row_ids.len(), 3);
        assert!(row_ids.contains(&100));
        assert!(row_ids.contains(&200));
        assert!(row_ids.contains(&300));
    }

    #[test]
    fn test_version_store_close() {
        let store = VersionStore::new("test_table".to_string(), test_schema());

        assert!(!store.is_closed());
        store.close();
        assert!(store.is_closed());

        // Operations should be no-ops when closed
        let row = Row::from(vec![Value::from(1)]);
        store.add_version(100, RowVersion::new(1, row));
        assert_eq!(store.row_count(), 0);
    }

    #[test]
    fn test_transaction_version_store_basic() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store = Arc::new(VersionStore::with_visibility_checker(
            "test_table".to_string(),
            test_schema(),
            checker,
        ));

        let mut tvs = TransactionVersionStore::new(store, 1);

        // Put a new row
        let row = Row::from(vec![Value::from(42)]);
        tvs.put(100, row, false).unwrap();

        // Should see it locally
        assert!(tvs.has_locally_seen(100));
        let got = tvs.get(100);
        assert!(got.is_some());
    }

    #[test]
    fn test_transaction_version_store_commit() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store = Arc::new(VersionStore::with_visibility_checker(
            "test_table".to_string(),
            test_schema(),
            checker,
        ));

        let mut tvs = TransactionVersionStore::new(Arc::clone(&store), 1);

        // Put a new row
        let row = Row::from(vec![Value::from(42)]);
        tvs.put(100, row, false).unwrap();

        // Commit
        tvs.commit().unwrap();

        // Should be visible in parent store now
        let visible = store.get_visible_version(100, 2);
        assert!(visible.is_some());
    }

    #[test]
    fn test_transaction_version_store_rollback() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store = Arc::new(VersionStore::with_visibility_checker(
            "test_table".to_string(),
            test_schema(),
            checker,
        ));

        let mut tvs = TransactionVersionStore::new(Arc::clone(&store), 1);

        // Put a new row
        let row = Row::from(vec![Value::from(42)]);
        tvs.put(100, row, false).unwrap();

        // Rollback
        tvs.rollback();

        // Should NOT be visible in parent store
        let visible = store.get_visible_version(100, 2);
        assert!(visible.is_none());
    }

    #[test]
    fn test_version_history_limit_default() {
        let store = VersionStore::new("test_table".to_string(), test_schema());
        // Default limit is 10
        assert_eq!(store.max_version_history(), 10);
    }

    #[test]
    fn test_version_history_limit_drop() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let mut store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Set a small limit for testing
        store.set_max_version_history(3);

        let row_id = 100;

        // Add 5 versions to the same row
        // v1: depth=1, v2: depth=2, v3: depth=3
        // v4: depth=4 > 3, triggers drop -> depth=2
        // v5: depth=3
        for txn_id in 1..=5 {
            let row = Row::from(vec![Value::from(txn_id)]);
            let version = RowVersion::new(txn_id, row);
            store.add_version(row_id, version);
        }

        // After 5 versions with limit 3:
        // v4 triggered drop (4 > 3), so chain was: v4 -> v3 -> None (depth=2)
        // v5 added: v5 -> v4 -> v3 -> None (depth=3)
        let versions = store.versions.read().clone();
        let entry = versions.get(row_id).expect("Row should exist");

        // Count actual chain length
        let chain_depth = count_chain_depth(entry);

        // Chain depth should be at most limit + 1 (oscillates between 2 and limit+1)
        assert!(
            chain_depth <= 4,
            "Chain depth {} exceeds limit+1",
            chain_depth
        );
    }

    #[test]
    fn test_version_history_drop_cycles() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let mut store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Set limit to 5
        store.set_max_version_history(5);

        let row_id = 100;

        // Add 20 versions - drop should happen multiple times
        // Pattern: 1,2,3,4,5,6(drop->2),3,4,5,6(drop->2),...
        for txn_id in 1..=20 {
            let row = Row::from(vec![Value::from(txn_id)]);
            let version = RowVersion::new(txn_id, row);
            store.add_version(row_id, version);
        }

        // Verify chain is bounded (between 2 and limit+1)
        let versions = store.versions.read().clone();
        let entry = versions.get(row_id).expect("Row should exist");

        let mut count = 1;
        let mut current = entry.prev.as_ref();
        while let Some(prev) = current {
            count += 1;
            current = prev.prev.as_ref();
        }

        assert!(
            count <= 6,
            "Chain length {} exceeds limit+1 (6) after 20 updates",
            count
        );
        assert!(count >= 2, "Chain length {} should be at least 2", count);
    }

    #[test]
    fn test_version_history_unlimited() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let mut store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Set to unlimited (0)
        store.set_max_version_history(0);

        let row_id = 100;

        // Add 15 versions
        for txn_id in 1..=15 {
            let row = Row::from(vec![Value::from(txn_id)]);
            let version = RowVersion::new(txn_id, row);
            store.add_version(row_id, version);
        }

        // Count chain length - should be 15 (unlimited)
        let versions = store.versions.read().clone();
        let entry = versions.get(row_id).expect("Row should exist");

        let mut count = 1;
        let mut current = entry.prev.as_ref();
        while let Some(prev) = current {
            count += 1;
            current = prev.prev.as_ref();
        }

        assert_eq!(count, 15, "Unlimited mode should keep all 15 versions");
    }

    #[test]
    fn test_version_history_batch_drop() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let mut store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Set limit to 3
        store.set_max_version_history(3);

        let row_id = 100;

        // First add some versions individually (depth reaches 3)
        for txn_id in 1..=3 {
            let row = Row::from(vec![Value::from(txn_id)]);
            let version = RowVersion::new(txn_id, row);
            store.add_version(row_id, version);
        }

        // Now add more via batch - each will trigger drop when exceeding limit
        let batch: Vec<(i64, RowVersion)> = (4..=7)
            .map(|txn_id| {
                let row = Row::from(vec![Value::from(txn_id)]);
                (row_id, RowVersion::new(txn_id, row))
            })
            .collect();

        store.add_versions_batch(batch);

        // Verify chain is bounded (at most limit+1)
        // Chain can be as short as 1 right after pruning (when new_depth > limit)
        let versions = store.versions.read().clone();
        let entry = versions.get(row_id).expect("Row should exist");

        let mut count = 1;
        let mut current = entry.prev.as_ref();
        while let Some(prev) = current {
            count += 1;
            current = prev.prev.as_ref();
        }

        assert!(
            count <= 4,
            "Chain length {} exceeds limit+1 (4) after batch",
            count
        );
        // With consistent logic: after 7 versions with limit 3,
        // the 7th version triggers pruning (depth 4 > 3), resulting in depth = 1
        assert!(count >= 1, "Chain length {} should be at least 1", count);
    }

    #[test]
    fn test_row_version_with_timestamp() {
        let row = Row::from(vec![Value::from(1)]);
        let timestamp = 12345678;
        let version = RowVersion::new_with_timestamp(1, row, timestamp);

        assert_eq!(version.txn_id, 1);
        assert_eq!(version.create_time, timestamp);
        assert!(!version.is_deleted());
    }

    #[test]
    fn test_row_version_deleted_with_timestamp() {
        let row = Row::from(vec![Value::from(1)]);
        let timestamp = 87654321;
        let version = RowVersion::new_deleted_with_timestamp(1, row, timestamp);

        assert_eq!(version.txn_id, 1);
        assert_eq!(version.deleted_at_txn_id, 1);
        assert_eq!(version.create_time, timestamp);
        assert!(version.is_deleted());
    }

    #[test]
    fn test_row_version_debug_display() {
        let row = Row::from(vec![Value::from(42)]);
        let version = RowVersion::new(1, row);

        let debug = format!("{:?}", version);
        assert!(debug.contains("RowVersion"));
        assert!(debug.contains("txn_id: 1"));
        assert!(debug.contains("create_time"));

        let display = format!("{}", version);
        assert!(display.contains("TxnID: 1"));
        assert!(display.contains("CreateTime"));
    }

    #[test]
    fn test_write_set_entry_clone() {
        let row = Row::from(vec![Value::from(1)]);
        let version = RowVersion::new(1, row);

        let entry = WriteSetEntry {
            read_version: Some(version),
            read_version_seq: 42,
        };

        let cloned = entry.clone();
        assert!(cloned.read_version.is_some());
        assert_eq!(cloned.read_version_seq, 42);

        // Test with None
        let empty_entry = WriteSetEntry {
            read_version: None,
            read_version_seq: 0,
        };
        let cloned_empty = empty_entry.clone();
        assert!(cloned_empty.read_version.is_none());
    }

    #[test]
    fn test_row_index() {
        let idx = RowIndex::new(100, Some(5));

        // Test Copy trait
        let copied = idx;
        assert_eq!(copied.row_id, 100);
        assert_eq!(copied.arena_idx(), Some(5));

        // Test Clone trait (use Clone::clone to avoid clone_on_copy warning)
        let cloned = Clone::clone(&idx);
        assert_eq!(cloned.row_id, 100);

        // Test with None arena_idx
        let idx_none = RowIndex::new(200, None);
        assert!(idx_none.arena_idx().is_none());

        // Test Debug
        let debug = format!("{:?}", idx);
        assert!(debug.contains("RowIndex"));
        assert!(debug.contains("100"));

        // Test memory optimization: RowIndex should be 16 bytes (not 24)
        assert_eq!(std::mem::size_of::<RowIndex>(), 16);
    }

    #[test]
    fn test_aggregate_op() {
        // Test equality
        assert_eq!(AggregateOp::Count, AggregateOp::Count);
        assert_ne!(AggregateOp::Count, AggregateOp::Sum);

        // Test all variants
        let ops = [
            AggregateOp::Count,
            AggregateOp::Sum,
            AggregateOp::Min,
            AggregateOp::Max,
            AggregateOp::Avg,
        ];

        for op in ops {
            let debug = format!("{:?}", op);
            assert!(!debug.is_empty());

            // Test Clone and Copy (use Clone::clone to avoid clone_on_copy warning)
            let copied = op;
            let cloned = Clone::clone(&op);
            assert_eq!(copied, cloned);
        }
    }

    #[test]
    fn test_version_store_with_capacity() {
        let store = VersionStore::with_capacity("test_table".to_string(), test_schema(), None, 100);

        assert_eq!(store.table_name(), "test_table");
        assert_eq!(store.row_count(), 0);
    }

    #[test]
    fn test_version_store_quick_check_row_existence() {
        let store = VersionStore::new("test_table".to_string(), test_schema());

        // Row doesn't exist
        assert!(!store.quick_check_row_existence(100));

        // Add a row
        let row = Row::from(vec![Value::from(42)]);
        let version = RowVersion::new(1, row);
        store.add_version(100, version);

        // Row exists
        assert!(store.quick_check_row_existence(100));
        assert!(!store.quick_check_row_existence(200));
    }

    #[test]
    fn test_version_store_get_visible_versions_batch() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add rows
        for i in 1..=5 {
            let row = Row::from(vec![Value::from(i * 10)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        // Batch query
        let row_ids = vec![1, 3, 5, 99]; // 99 doesn't exist
        let results = store.get_visible_versions_batch(&row_ids, 2);

        assert_eq!(results.len(), 3); // Only 1, 3, 5 exist
    }

    #[test]
    fn test_version_store_count_visible_versions_batch() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add rows
        for i in 1..=5 {
            let row = Row::from(vec![Value::from(i)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        let count = store.count_visible_versions_batch(&[1, 2, 3, 99, 100], 2);
        assert_eq!(count, 3); // Only 1, 2, 3 exist
    }

    #[test]
    fn test_version_store_count_visible_rows() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        assert_eq!(store.count_visible_rows(1), 0);

        // Add rows
        for i in 1..=10 {
            let row = Row::from(vec![Value::from(i)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        assert_eq!(store.count_visible_rows(2), 10);
    }

    #[test]
    fn test_version_store_mark_deleted() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add a row
        let row = Row::from(vec![Value::from(42)]);
        let version = RowVersion::new(1, row);
        store.add_version(100, version);

        // Mark it deleted
        store.mark_deleted(100, 2);

        // Transaction 1 should still see it
        assert!(store.get_visible_version(100, 1).is_some());

        // Transaction 3 should not see deleted row
        assert!(store.get_visible_version(100, 3).is_none());

        // Mark non-existent row as deleted (no-op)
        store.mark_deleted(999, 2);
    }

    #[test]
    fn test_version_store_get_visible_rows_with_limit() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add 20 rows
        for i in 1..=20 {
            let row = Row::from(vec![Value::from(i)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        // Get with limit (txn_id, limit, offset)
        let results = store.get_visible_rows_with_limit(2, 5, 0);
        assert_eq!(results.len(), 5);

        // Test with offset
        let results_offset = store.get_visible_rows_with_limit(2, 5, 10);
        assert_eq!(results_offset.len(), 5);
    }

    #[test]
    fn test_version_store_as_of_transaction() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add version from transaction 5
        let row = Row::from(vec![Value::from(100)]);
        let version = RowVersion::new(5, row);
        store.add_version(1, version);

        // Add updated version from transaction 10
        let row2 = Row::from(vec![Value::from(200)]);
        let version2 = RowVersion::new(10, row2);
        store.add_version(1, version2);

        // AS OF transaction 7 should see the first version
        let result = store.get_visible_version_as_of_transaction(1, 7);
        assert!(result.is_some());
        let rv = result.unwrap();
        assert_eq!(rv.txn_id, 5);

        // AS OF transaction 15 should see the second version
        let result = store.get_visible_version_as_of_transaction(1, 15);
        assert!(result.is_some());
        let rv = result.unwrap();
        assert_eq!(rv.txn_id, 10);

        // AS OF transaction 3 should see nothing
        let result = store.get_visible_version_as_of_transaction(1, 3);
        assert!(result.is_none());

        // Non-existent row
        let result = store.get_visible_version_as_of_transaction(999, 10);
        assert!(result.is_none());
    }

    #[test]
    fn test_version_store_as_of_timestamp() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add version with specific timestamp
        let row = Row::from(vec![Value::from(100)]);
        let version = RowVersion::new_with_timestamp(1, row, 1000);
        store.add_version(1, version);

        // Add version with later timestamp
        let row2 = Row::from(vec![Value::from(200)]);
        let version2 = RowVersion::new_with_timestamp(2, row2, 2000);
        store.add_version(1, version2);

        // AS OF timestamp 1500 should see first version
        let result = store.get_visible_version_as_of_timestamp(1, 1500);
        assert!(result.is_some());
        assert_eq!(result.unwrap().create_time, 1000);

        // AS OF timestamp 2500 should see second version
        let result = store.get_visible_version_as_of_timestamp(1, 2500);
        assert!(result.is_some());
        assert_eq!(result.unwrap().create_time, 2000);

        // AS OF timestamp 500 should see nothing
        let result = store.get_visible_version_as_of_timestamp(1, 500);
        assert!(result.is_none());
    }

    #[test]
    fn test_version_store_sum_column() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add rows with integer values
        for i in 1..=5 {
            let row = Row::from(vec![Value::from(i * 10)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        let (sum, count) = store.sum_column(2, 0);
        assert_eq!(sum, 150.0); // 10 + 20 + 30 + 40 + 50
        assert_eq!(count, 5);
    }

    #[test]
    fn test_version_store_sum_column_with_floats() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add rows with float values
        let values = [1.5, 2.5, 3.5];
        for (i, v) in values.iter().enumerate() {
            let row = Row::from(vec![Value::from(*v)]);
            let version = RowVersion::new(1, row);
            store.add_version((i + 1) as i64, version);
        }

        let (sum, count) = store.sum_column(2, 0);
        assert!((sum - 7.5).abs() < 0.001);
        assert_eq!(count, 3);
    }

    #[test]
    fn test_version_store_min_max_column() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add rows
        for i in [30, 10, 50, 20, 40] {
            let row = Row::from(vec![Value::from(i)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        let min = store.min_column(2, 0);
        assert_eq!(min, Some(Value::from(10)));

        let max = store.max_column(2, 0);
        assert_eq!(max, Some(Value::from(50)));
    }

    #[test]
    fn test_version_store_min_max_empty() {
        let store = VersionStore::new("test_table".to_string(), test_schema());

        let min = store.min_column(1, 0);
        assert!(min.is_none());

        let max = store.max_column(1, 0);
        assert!(max.is_none());
    }

    #[test]
    fn test_version_store_compute_aggregates() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add rows
        for i in 1..=5 {
            let row = Row::from(vec![Value::from(i * 10)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        // Compute multiple aggregates at once
        let ops = vec![
            (AggregateOp::Count, 0),
            (AggregateOp::Sum, 0),
            (AggregateOp::Min, 0),
            (AggregateOp::Max, 0),
            (AggregateOp::Avg, 0),
        ];

        let results = store.compute_aggregates(2, &ops);
        assert_eq!(results.len(), 5);

        // Check count
        match &results[0] {
            AggregateResult::Count(c) => assert_eq!(*c, 5),
            _ => panic!("Expected Count"),
        }

        // Check sum
        match &results[1] {
            AggregateResult::Sum(s, _) => assert_eq!(*s, 150.0),
            _ => panic!("Expected Sum"),
        }

        // Check min
        match &results[2] {
            AggregateResult::Min(Some(v)) => assert_eq!(*v, Value::from(10)),
            _ => panic!("Expected Min"),
        }

        // Check max
        match &results[3] {
            AggregateResult::Max(Some(v)) => assert_eq!(*v, Value::from(50)),
            _ => panic!("Expected Max"),
        }

        // Check avg (returns sum, count - caller computes sum/count)
        match &results[4] {
            AggregateResult::Avg(sum, count) => {
                let avg = sum / *count as f64;
                assert!((avg - 30.0).abs() < 0.001);
            }
            _ => panic!("Expected Avg"),
        }
    }

    #[test]
    fn test_version_store_stream_visible_rows() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add rows
        for i in 1..=3 {
            let row = Row::from(vec![Value::from(i * 10)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        let mut stream = store.stream_visible_rows(2);
        let mut count = 0;

        while stream.next() {
            count += 1;
            let slice = stream.row_arc_slice().unwrap();
            assert!(!slice.is_empty());
        }

        assert_eq!(count, 3);
    }

    #[test]
    fn test_version_store_row_claim() {
        let store = VersionStore::new("test_table".to_string(), test_schema());

        // Claim a row
        assert!(store.try_claim_row(100, 1).is_ok());

        // Same transaction can claim again
        assert!(store.try_claim_row(100, 1).is_ok());

        // Different transaction should fail
        assert!(store.try_claim_row(100, 2).is_err());

        // Release and another transaction can claim
        store.release_row_claim(100, 1);
        assert!(store.try_claim_row(100, 2).is_ok());
    }

    #[test]
    fn test_version_store_index_operations() {
        use crate::core::types::DataType;
        use crate::storage::index::HashIndex;

        let store = VersionStore::new("test_table".to_string(), test_schema());

        // No indexes initially
        assert!(!store.index_exists("idx_test"));
        assert!(store.list_indexes().is_empty());

        // Add an index with all required parameters
        let index = Arc::new(HashIndex::new(
            "idx_test".to_string(),
            "test_table".to_string(),
            vec!["test_col".to_string()],
            vec![0],
            vec![DataType::Integer],
            false,
            0,
        ));
        store.add_index("idx_test".to_string(), index);

        assert!(store.index_exists("idx_test"));
        assert_eq!(store.list_indexes().len(), 1);

        // Get index
        assert!(store.get_index("idx_test").is_some());
        assert!(store.get_index("nonexistent").is_none());

        // Get by column
        assert!(store.get_index_by_column("test_col").is_some());

        // Remove index
        let removed = store.remove_index("idx_test");
        assert!(removed.is_some());
        assert!(!store.index_exists("idx_test"));
    }

    #[test]
    fn test_version_store_get_current_sequence() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Sequence should increment
        let seq1 = store.get_current_sequence();
        let seq2 = store.get_current_sequence();
        assert!(seq2 > seq1);
    }

    #[test]
    fn test_version_store_get_visible_row_indices() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add rows
        for i in 1..=5 {
            let row = Row::from(vec![Value::from(i)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        let indices = store.get_visible_row_indices(2);
        assert_eq!(indices.len(), 5);

        // Verify indices can be materialized
        let materialized = store.materialize_rows(&indices);
        assert_eq!(materialized.len(), 5);
    }

    #[test]
    fn test_version_store_get_column_value() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add a row with multiple columns
        let row = Row::from(vec![Value::from(42), Value::from("test")]);
        let version = RowVersion::new(1, row);
        store.add_version(1, version);

        let indices = store.get_visible_row_indices(2);
        assert_eq!(indices.len(), 1);

        // Get column values
        let val = store.get_column_value(&indices[0], 0);
        assert_eq!(val, Some(Value::from(42)));

        let val = store.get_column_value(&indices[0], 1);
        assert_eq!(val, Some(Value::from("test")));

        // Out of bounds column
        let val = store.get_column_value(&indices[0], 99);
        assert!(val.is_none());
    }

    #[test]
    fn test_version_store_apply_recovered_version() {
        let store = VersionStore::new("test_table".to_string(), test_schema());

        // Apply a recovered version
        let row = Row::from(vec![Value::from(42)]);
        let version = RowVersion::new(1, row);
        store.apply_recovered_version(100, version);

        assert_eq!(store.row_count(), 1);
        assert!(store.quick_check_row_existence(100));
    }

    #[test]
    fn test_version_store_schema_operations() {
        let store = VersionStore::new("test_table".to_string(), test_schema());

        // Get schema
        let schema = store.schema();
        assert_eq!(schema.table_name, "test_table");

        // Modify schema through mutable reference
        {
            let schema_guard = store.schema_mut();
            // Just verify we can get mutable access
            assert_eq!(schema_guard.table_name, "test_table");
        }
    }

    #[test]
    fn test_version_store_visibility_checker_setter() {
        let mut store = VersionStore::new("test_table".to_string(), test_schema());

        // Set a new visibility checker
        let checker = Arc::new(TestVisibilityChecker::new());
        store.set_visibility_checker(checker);

        // Verify it works with the new checker
        let row = Row::from(vec![Value::from(42)]);
        let version = RowVersion::new(1, row);
        store.add_version(100, version);

        let visible = store.get_visible_version(100, 2);
        assert!(visible.is_some());
    }

    #[test]
    fn test_transaction_version_store_update() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store = Arc::new(VersionStore::with_visibility_checker(
            "test_table".to_string(),
            test_schema(),
            checker,
        ));

        // Add a row first
        let row = Row::from(vec![Value::from(42)]);
        let version = RowVersion::new(1, row);
        store.add_version(100, version);

        // Start a new transaction and update
        let mut tvs = TransactionVersionStore::new(Arc::clone(&store), 2);

        // Update the row (is_delete = false for updates)
        let new_row = Row::from(vec![Value::from(99)]);
        tvs.put(100, new_row, false).unwrap();

        // Should see updated value locally
        let got = tvs.get(100);
        assert!(got.is_some());
        let data = got.unwrap();
        assert_eq!(data.get(0), Some(&Value::from(99)));

        // Commit
        tvs.commit().unwrap();

        // Updated value should be visible
        let visible = store.get_visible_version(100, 3);
        assert!(visible.is_some());
        assert_eq!(visible.unwrap().data.get(0), Some(&Value::from(99)));
    }

    #[test]
    fn test_transaction_version_store_delete() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store = Arc::new(VersionStore::with_visibility_checker(
            "test_table".to_string(),
            test_schema(),
            checker,
        ));

        // Add a row first
        let row = Row::from(vec![Value::from(42)]);
        let version = RowVersion::new(1, row);
        store.add_version(100, version);

        // Start a new transaction and delete
        let mut tvs = TransactionVersionStore::new(Arc::clone(&store), 2);

        // Delete the row by using put with is_delete=true
        let delete_row = Row::from(vec![Value::from(42)]);
        tvs.put(100, delete_row, true).unwrap(); // is_delete = true

        // Should see it as deleted locally
        let got = tvs.get(100);
        assert!(got.is_none());

        // Commit
        tvs.commit().unwrap();

        // Should not be visible after commit
        let visible = store.get_visible_version(100, 3);
        assert!(visible.is_none());
    }

    #[test]
    fn test_get_all_visible_rows() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add rows
        for i in 1..=5 {
            let row = Row::from(vec![Value::from(i * 10)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        let rows = store.get_all_visible_rows(2);
        assert_eq!(rows.len(), 5);

        // Verify values
        let values: Vec<i64> = rows
            .iter()
            .map(|(_, row)| match row.get(0) {
                Some(Value::Integer(i)) => *i,
                _ => panic!("Expected integer"),
            })
            .collect();

        assert!(values.contains(&10));
        assert!(values.contains(&50));
    }

    #[test]
    fn test_get_all_visible_row_ids() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add rows
        for i in 1..=5 {
            let row = Row::from(vec![Value::from(i)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        let row_ids = store.get_all_visible_row_ids(2);
        assert_eq!(row_ids.len(), 5);
        assert!(row_ids.contains(&1));
        assert!(row_ids.contains(&5));
    }

    #[test]
    fn test_collect_rows_pk_ordered() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add rows in non-sequential order
        for i in [5, 3, 1, 4, 2] {
            let row = Row::from(vec![Value::from(i * 10)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        // Get rows in ascending PK order
        let rows = store.collect_rows_pk_ordered(2, true, 3, 0);
        assert!(rows.is_some());
        let rows = rows.unwrap();
        assert_eq!(rows.len(), 3);

        // First 3 rows in ascending PK order should be row_ids 1, 2, 3
        // With values 10, 20, 30
        assert_eq!(rows[0].1.get(0), Some(&Value::from(10)));
        assert_eq!(rows[1].1.get(0), Some(&Value::from(20)));
        assert_eq!(rows[2].1.get(0), Some(&Value::from(30)));

        // Test descending order
        let rows_desc = store.collect_rows_pk_ordered(2, false, 3, 0);
        assert!(rows_desc.is_some());
        let rows_desc = rows_desc.unwrap();
        assert_eq!(rows_desc.len(), 3);

        // First 3 rows in descending PK order should be row_ids 5, 4, 3
        // With values 50, 40, 30
        assert_eq!(rows_desc[0].1.get(0), Some(&Value::from(50)));
        assert_eq!(rows_desc[1].1.get(0), Some(&Value::from(40)));
        assert_eq!(rows_desc[2].1.get(0), Some(&Value::from(30)));

        // Test with offset
        let rows_offset = store.collect_rows_pk_ordered(2, true, 2, 2);
        assert!(rows_offset.is_some());
        let rows_offset = rows_offset.unwrap();
        assert_eq!(rows_offset.len(), 2);
        // Skip first 2 (values 10, 20), get next 2 (values 30, 40)
        assert_eq!(rows_offset[0].1.get(0), Some(&Value::from(30)));
        assert_eq!(rows_offset[1].1.get(0), Some(&Value::from(40)));
    }

    #[test]
    fn test_count_visible() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        assert_eq!(store.count_visible(1), 0);

        // Add rows
        for i in 1..=10 {
            let row = Row::from(vec![Value::from(i)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        assert_eq!(store.count_visible(2), 10);
    }

    #[test]
    fn test_materialize_single_row() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add a row
        let row = Row::from(vec![Value::from(42)]);
        let version = RowVersion::new(1, row);
        store.add_version(100, version);

        let indices = store.get_visible_row_indices(2);
        assert_eq!(indices.len(), 1);

        // Materialize single row
        let result = store.materialize_row(&indices[0]);
        assert!(result.is_some());
        let (row_id, row) = result.unwrap();
        assert_eq!(row_id, 100);
        assert_eq!(row.get(0), Some(&Value::from(42)));

        // Invalid index
        let invalid_idx = RowIndex::new(999, None);
        let result = store.materialize_row(&invalid_idx);
        assert!(result.is_none());
    }

    // =========================================================================
    // Cleanup Tests
    // =========================================================================

    #[test]
    fn test_cleanup_deleted_rows_basic() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add 10 rows from transaction 1
        for i in 1..=10 {
            let row = Row::from(vec![Value::from(i)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        // Delete all rows in transaction 2
        for i in 1..=10 {
            let row = Row::from(vec![Value::from(i)]);
            let mut version = RowVersion::new(1, row);
            version.deleted_at_txn_id = 2;
            store.add_version(i, version);
        }

        // Cleanup with 0 retention (immediate)
        let cleaned = store.cleanup_deleted_rows(std::time::Duration::from_secs(0));

        // All 10 rows should be cleaned
        assert_eq!(cleaned, 10, "Expected 10 rows to be cleaned");

        // Verify versions map is empty
        assert_eq!(
            store.versions.read().len(),
            0,
            "Versions map should be empty"
        );
    }

    #[test]
    fn test_cleanup_respects_retention_period() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add and delete a row
        let row = Row::from(vec![Value::from(1)]);
        let version = RowVersion::new(1, row.clone());
        store.add_version(1, version);

        let mut deleted_version = RowVersion::new(1, row);
        deleted_version.deleted_at_txn_id = 2;
        store.add_version(1, deleted_version);

        // Cleanup with very long retention - should not clean
        let cleaned = store.cleanup_deleted_rows(std::time::Duration::from_secs(3600));
        assert_eq!(cleaned, 0, "Should not clean rows within retention period");

        // Verify row still exists
        assert_eq!(
            store.versions.read().len(),
            1,
            "Row should still exist in versions"
        );
    }

    #[test]
    fn test_cleanup_only_deleted_rows() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add 5 rows, delete only 3
        for i in 1..=5 {
            let row = Row::from(vec![Value::from(i)]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        // Delete rows 1, 3, 5
        for i in [1, 3, 5] {
            let row = Row::from(vec![Value::from(i)]);
            let mut version = RowVersion::new(1, row);
            version.deleted_at_txn_id = 2;
            store.add_version(i, version);
        }

        // Cleanup
        let cleaned = store.cleanup_deleted_rows(std::time::Duration::from_secs(0));

        assert_eq!(cleaned, 3, "Should clean only 3 deleted rows");
        assert_eq!(
            store.versions.read().len(),
            2,
            "2 non-deleted rows should remain"
        );
    }

    #[test]
    fn test_cleanup_arena_memory_released() {
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Add rows with data
        for i in 1..=5 {
            let row = Row::from(vec![Value::from(i), Value::text(format!("data_{}", i))]);
            let version = RowVersion::new(1, row);
            store.add_version(i, version);
        }

        // Record initial arena length
        let initial_arena_len = store.arena.len();
        assert_eq!(initial_arena_len, 5);

        // Delete all rows
        for i in 1..=5 {
            let row = Row::from(vec![Value::from(i), Value::text(format!("data_{}", i))]);
            let mut version = RowVersion::new(1, row);
            version.deleted_at_txn_id = 2;
            store.add_version(i, version);
        }

        // Cleanup
        let cleaned = store.cleanup_deleted_rows(std::time::Duration::from_secs(0));
        assert_eq!(cleaned, 5);

        // Arena slots should be cleared (data replaced with empty)
        // The slots remain but data is released
        let guard = store.arena.read_guard();
        for i in 0..5 {
            // Cleared slots have row_id = 0
            assert_eq!(guard.meta()[i].row_id, 0, "Slot {} should be cleared", i);
            // Data should be empty
            assert!(
                guard.data()[i].is_empty(),
                "Slot {} data should be empty",
                i
            );
        }
    }

    #[test]
    fn test_cleanup_does_not_remove_reinserted_row() {
        // Regression test for race condition: if a deleted row_id gets a new live
        // version committed between the cleanup snapshot and the removal pass,
        // cleanup must NOT remove the new live version.
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Tx 1: insert row_id=100
        let row = Row::from(vec![Value::from(42)]);
        let version = RowVersion::new(1, row);
        store.add_version(100, version);

        // Tx 2: delete row_id=100
        let row = Row::from(vec![Value::from(42)]);
        let mut deleted = RowVersion::new(1, row);
        deleted.deleted_at_txn_id = 2;
        store.add_version(100, deleted);

        // Simulate the cleanup's first pass: snapshot and identify row for deletion
        assert!(store.versions.read().get(100).unwrap().version.is_deleted());

        // Now, BEFORE cleanup's removal pass, tx 3 re-inserts a live version at row_id=100
        let row = Row::from(vec![Value::from(99)]);
        let live_version = RowVersion::new(3, row);
        store.add_version(100, live_version);

        // The row should now be live (not deleted)
        assert!(!store.versions.read().get(100).unwrap().version.is_deleted());

        // Run cleanup — it should see the row is no longer deleted and skip it
        let cleaned = store.cleanup_deleted_rows(std::time::Duration::from_secs(0));
        assert_eq!(cleaned, 0, "Should not clean a row that was re-inserted");

        // Row must still exist with the new live value
        let versions = store.versions.read();
        let entry = versions.get(100);
        assert!(entry.is_some(), "Row 100 must still exist");
        let version = &entry.unwrap().version;
        assert!(!version.is_deleted(), "Row 100 must be live, not deleted");
        assert_eq!(
            version.data.get(0),
            Some(&Value::from(99)),
            "Row 100 must have the re-inserted value"
        );
    }

    #[test]
    fn test_cleanup_mixed_reinserted_and_deleted() {
        // Some rows are still deleted (should be cleaned), some were re-inserted (should be kept).
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Insert and delete rows 1..=4
        for i in 1..=4 {
            let row = Row::from(vec![Value::from(i)]);
            store.add_version(i, RowVersion::new(1, row));
        }
        for i in 1..=4 {
            let row = Row::from(vec![Value::from(i)]);
            let mut del = RowVersion::new(1, row);
            del.deleted_at_txn_id = 2;
            store.add_version(i, del);
        }

        // Re-insert rows 2 and 4 with new values (simulating concurrent commit)
        for &i in &[2, 4] {
            let row = Row::from(vec![Value::from(i * 100)]);
            store.add_version(i, RowVersion::new(3, row));
        }

        // Cleanup should only remove rows 1 and 3 (still deleted)
        let cleaned = store.cleanup_deleted_rows(std::time::Duration::from_secs(0));
        assert_eq!(cleaned, 2, "Should clean only rows 1 and 3");

        let versions = store.versions.read();
        assert!(versions.get(1).is_none(), "Row 1 should be removed");
        assert!(versions.get(3).is_none(), "Row 3 should be removed");
        assert!(versions.get(2).is_some(), "Row 2 should still exist");
        assert!(versions.get(4).is_some(), "Row 4 should still exist");

        // Verify re-inserted values
        assert_eq!(
            versions.get(2).unwrap().version.data.get(0),
            Some(&Value::from(200))
        );
        assert_eq!(
            versions.get(4).unwrap().version.data.get(0),
            Some(&Value::from(400))
        );
    }

    #[test]
    fn test_descending_order_returns_correct_version_under_snapshot_isolation() {
        // Reproducer: under snapshot isolation, the descending path uses deferred
        // materialization (RowIndex → materialize_rows). For chain entries where HEAD
        // is not visible, materialization falls back to versions.get(row_id) which
        // returns HEAD data — not the correct older visible version.
        //
        // TestVisibilityChecker: is_visible(txn_id, viewer) = txn_id <= viewer.
        // So viewer=2 sees txn_id=1 but NOT txn_id=3. This simulates snapshot isolation.
        let checker = Arc::new(TestVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Tx 1: insert row_id=1 with value=100, row_id=2 with value=200
        let row1 = Row::from(vec![Value::from(100)]);
        store.add_version(1, RowVersion::new(1, row1));
        let row2 = Row::from(vec![Value::from(200)]);
        store.add_version(2, RowVersion::new(1, row2));

        // Tx 3: update row_id=1 with new value=999
        // This creates a version chain: HEAD(txn=3, val=999) -> prev(txn=1, val=100)
        let updated_row = Row::from(vec![Value::from(999)]);
        store.add_version(1, RowVersion::new(3, updated_row));

        // Viewer txn_id=2: sees txn<=2, so sees txn=1 but NOT txn=3
        // Expected: row_id=1 should have value=100, row_id=2 should have value=200

        // Ascending order — materializes during iteration (should be correct)
        let asc = store.collect_rows_pk_ordered(2, true, 100, 0).unwrap();
        assert_eq!(asc.len(), 2, "Should see 2 rows ascending");
        // row_id=1 should have the original value (100), NOT the update (999)
        let (rid1, data1) = &asc[0];
        assert_eq!(*rid1, 1);
        assert_eq!(
            data1.get(0),
            Some(&Value::from(100)),
            "Ascending: row_id=1 should have original value 100, not updated 999"
        );

        // Descending order
        let desc = store.collect_rows_pk_ordered(2, false, 100, 0).unwrap();
        assert_eq!(desc.len(), 2, "Should see 2 rows descending");
        // row_id=1 is at index 1 in descending order (row_id=2 comes first)
        let (rid1_desc, data1_desc) = &desc[1];
        assert_eq!(*rid1_desc, 1);
        assert_eq!(
            data1_desc.get(0),
            Some(&Value::from(100)),
            "Descending: row_id=1 should have original value 100, not updated 999"
        );
    }

    #[test]
    fn test_sorted_limit_returns_correct_version_under_snapshot_isolation() {
        // Same scenario as descending test but via get_visible_rows_sorted_limit.
        // The slow path (SnapshotIsolation) must materialize during iteration,
        // not defer to materialize_rows which reads HEAD data.
        // Uses SnapshotVisibilityChecker so arena fast paths are bypassed.
        let checker = Arc::new(SnapshotVisibilityChecker::new());
        let store =
            VersionStore::with_visibility_checker("test_table".to_string(), test_schema(), checker);

        // Tx 1: insert row_id=1 with value=100, row_id=2 with value=200
        let row1 = Row::from(vec![Value::from(100)]);
        store.add_version(1, RowVersion::new(1, row1));
        let row2 = Row::from(vec![Value::from(200)]);
        store.add_version(2, RowVersion::new(1, row2));

        // Tx 3: update row_id=1 with new value=999
        let updated_row = Row::from(vec![Value::from(999)]);
        store.add_version(1, RowVersion::new(3, updated_row));

        // Viewer txn_id=2: sees txn<=2
        // Sort by column 0, ascending — row_id=1 (val=100) should come first
        let sorted = store.get_visible_rows_sorted_limit(2, 0, true, 100, 0);
        assert_eq!(sorted.len(), 2, "Should see 2 rows");

        // First row should be row_id=1 with value=100 (not 999)
        let (rid, data) = &sorted[0];
        assert_eq!(*rid, 1);
        assert_eq!(
            data.get(0),
            Some(&Value::from(100)),
            "Sorted ascending: row_id=1 should have value 100, not 999"
        );

        // Descending — row_id=2 (val=200) first, then row_id=1 (val=100)
        let sorted_desc = store.get_visible_rows_sorted_limit(2, 0, false, 100, 0);
        assert_eq!(sorted_desc.len(), 2);
        let (rid2, data2) = &sorted_desc[1];
        assert_eq!(*rid2, 1);
        assert_eq!(
            data2.get(0),
            Some(&Value::from(100)),
            "Sorted descending: row_id=1 should have value 100, not 999"
        );
    }

    #[test]
    fn test_pack_arena_idx_no_overflow() {
        // Verify that NonZeroU64 handles all valid indices correctly without overflow

        // Case 1: Small index
        let small = 100usize;
        let packed = pack_arena_idx(small);
        assert!(packed.is_some());
        assert_eq!(unpack_arena_idx(packed), Some(small));

        // Case 2: Max u32 value (previously caused issues with NonZeroU32)
        let max_u32 = u32::MAX as usize;
        let packed_u32 = pack_arena_idx(max_u32);
        assert!(packed_u32.is_some());
        assert_eq!(unpack_arena_idx(packed_u32), Some(max_u32));

        // Case 3: Beyond u32::MAX (previously caused corruption with NonZeroU32)
        let beyond_u32 = u32::MAX as usize + 1;
        let packed_beyond = pack_arena_idx(beyond_u32);
        assert!(packed_beyond.is_some());
        assert_eq!(unpack_arena_idx(packed_beyond), Some(beyond_u32)); // Now correct!

        // Case 4: Large index (5 billion - would have corrupted with u32)
        let large = 5_000_000_000usize;
        let packed_large = pack_arena_idx(large);
        assert!(packed_large.is_some());
        assert_eq!(unpack_arena_idx(packed_large), Some(large)); // Now correct!

        // Case 5: Zero index
        let zero = 0usize;
        let packed_zero = pack_arena_idx(zero);
        assert!(packed_zero.is_some());
        assert_eq!(unpack_arena_idx(packed_zero), Some(zero));

        assert_eq!(unpack_arena_idx(None), None);
    }

    #[test]
    fn test_unique_constraint_swap() {
        use crate::core::DataType;
        use crate::storage::index::HashIndex;

        // Setup: Table with unique index on column 'u' (index 1)
        let schema = crate::core::SchemaBuilder::new("test_swap")
            .column("id", DataType::Integer, false, true) // nullable=false, pk=true
            .column("u", DataType::Integer, true, false) // nullable=true, pk=false
            .build();

        let checker = Arc::new(TestVisibilityChecker::new());
        let store = Arc::new(VersionStore::with_visibility_checker(
            "test_swap".to_string(),
            schema.clone(),
            checker,
        ));

        // Create Unique Hash Index on 'u'
        let index = Arc::new(HashIndex::new(
            "idx_u".to_string(),
            "test_swap".to_string(),
            vec!["u".to_string()],
            vec![1],
            vec![DataType::Integer],
            true, // is_unique
            0,
        ));
        store.add_index("idx_u".to_string(), index);

        // Initial data: (1, 10), (2, 20)
        let mut txn1 = TransactionVersionStore::new(Arc::clone(&store), 1);
        txn1.put(1, Row::from(vec![Value::from(1), Value::from(10)]), false)
            .unwrap();
        txn1.put(2, Row::from(vec![Value::from(2), Value::from(20)]), false)
            .unwrap();
        txn1.commit().unwrap();

        // Swap: (1, 20), (2, 10) in Single Transaction
        let mut txn2 = TransactionVersionStore::new(Arc::clone(&store), 2);

        // Update row 1: 10 -> 20 (conflict with row 2's old value)
        txn2.put(1, Row::from(vec![Value::from(1), Value::from(20)]), false)
            .unwrap();

        // Update row 2: 20 -> 10 (taking row 1's old value)
        txn2.put(2, Row::from(vec![Value::from(2), Value::from(10)]), false)
            .unwrap();

        // Commit should succeed
        let result = txn2.commit();
        assert!(result.is_ok(), "Commit failed: {:?}", result.err());

        // Verify values
        let v1_new = store.get_visible_version(1, 3).unwrap();
        assert_eq!(v1_new.data.get(1).unwrap(), &Value::from(20));

        let v2_new = store.get_visible_version(2, 3).unwrap();
        assert_eq!(v2_new.data.get(1).unwrap(), &Value::from(10));
    }

    #[test]
    fn test_unique_constraint_performance_bulk_update() {
        use crate::core::DataType;
        use crate::storage::index::HashIndex;
        use std::time::Instant;

        // Setup: Table with unique index on column 'u' (index 1)
        let schema = crate::core::SchemaBuilder::new("test_perf")
            .column("id", DataType::Integer, false, true) // nullable=false, pk=true
            .column("u", DataType::Integer, true, false) // nullable=true, pk=false
            .build();

        let checker = Arc::new(TestVisibilityChecker::new());
        let store = Arc::new(VersionStore::with_visibility_checker(
            "test_perf".to_string(),
            schema.clone(),
            checker,
        ));

        // Create Unique Hash Index on 'u'
        let index = Arc::new(HashIndex::new(
            "idx_u".to_string(),
            "test_perf".to_string(),
            vec!["u".to_string()],
            vec![1],
            vec![DataType::Integer],
            true, // is_unique
            0,
        ));
        store.add_index("idx_u".to_string(), index);

        let row_count = 30000;
        let mut txn1 = TransactionVersionStore::new(Arc::clone(&store), 1);
        for i in 0..row_count {
            txn1.put(
                i as i64,
                Row::from(vec![Value::from(i), Value::from(i)]),
                false,
            )
            .unwrap();
        }
        txn1.commit().unwrap();

        let mut txn2 = TransactionVersionStore::new(Arc::clone(&store), 2);
        // Bulk update: shift every value by 1 (e.g., 0->1, 1->2, ... 4999->5000)
        // This will cause every row to conflict with the next one's old value
        for i in 0..row_count {
            txn2.put(
                i as i64,
                Row::from(vec![Value::from(i), Value::from(i + 1)]),
                false,
            )
            .unwrap();
        }

        let start = Instant::now();
        txn2.commit().unwrap();
        let duration = start.elapsed();
        println!("Commit time for {} rows: {:?}", row_count, duration);

        // If it's O(N^2), 30000 rows would take several seconds.
        // If it's O(N), it should be < 200ms normally.
        // Coverage instrumentation (llvm-cov) adds ~2-3x overhead, so use 2000ms.
        assert!(
            duration.as_millis() < 2000,
            "Performance regression detected! Commit took {:?}",
            duration
        );
    }

    /// Verify that truncate_all() holds uncommitted_writes(W) during the
    /// entire check-and-clear sequence, preventing the TOCTOU race where
    /// a concurrent try_claim_row() could add a claim between the check
    /// and the clear.
    ///
    /// With the fix, truncate_all() holds uncommitted_writes(W) for the
    /// entire duration, so any concurrent try_claim_row() blocks until
    /// truncate completes — at which point truncate has already cleared
    /// the table and the UPDATE will operate on a clean state.
    #[test]
    fn test_truncate_blocks_concurrent_claims() {
        use std::sync::{Arc, Barrier};
        use std::thread;

        let checker = Arc::new(TestVisibilityChecker::new());
        let store = Arc::new(VersionStore::with_visibility_checker(
            "test_table".to_string(),
            test_schema(),
            checker,
        ));

        // Setup: 10 committed rows
        for i in 1..=10 {
            let row = Row::from(vec![Value::from(i)]);
            store.add_version(i, RowVersion::new(1, row));
        }
        assert_eq!(store.committed_row_count.load(Ordering::Relaxed), 10);

        // Pre-claim a row to simulate an in-progress UPDATE
        store
            .try_claim_row(5, 99)
            .expect("claim should succeed on fresh store");

        // truncate_all must fail because uncommitted_writes is not empty
        let result = store.truncate_all();
        assert!(
            result.is_err(),
            "truncate must fail when uncommitted writes exist"
        );

        // Verify data was NOT destroyed
        assert_eq!(
            store.committed_row_count.load(Ordering::Relaxed),
            10,
            "row count must be unchanged after failed truncate"
        );
        assert_eq!(store.versions.read().len(), 10);

        // Release the claim, then truncate should succeed
        store.release_row_claim(5, 99);
        let count = store.truncate_all().expect("truncate should succeed now");
        assert_eq!(count, 10);
        assert!(store.versions.read().is_empty());
        assert!(store.uncommitted_writes.read().is_empty());

        // Concurrent test: truncate holds the lock, blocking try_claim_row
        // Insert fresh data for next round
        for i in 1..=5 {
            let row = Row::from(vec![Value::from(i)]);
            store.add_version(i, RowVersion::new(1, row));
        }
        // Force committed_row_count to reflect the 5 rows
        store.committed_row_count.store(5, Ordering::Relaxed);

        let barrier = Arc::new(Barrier::new(2));
        let store2 = Arc::clone(&store);
        let barrier2 = Arc::clone(&barrier);

        // Thread: try to claim a row concurrently with truncate
        let handle = thread::spawn(move || {
            barrier2.wait();
            // This will either:
            // a) Execute BEFORE truncate's lock → claim succeeds, truncate sees
            //    non-empty uncommitted_writes → truncate fails (correct!)
            // b) Execute AFTER truncate's lock → claim succeeds on empty store
            //    (correct, no data to corrupt)
            store2.try_claim_row(3, 200)
        });

        barrier.wait();
        let truncate_result = store.truncate_all();
        let claim_result = handle.join().unwrap();

        // Both operations complete without panic.
        // Either truncate succeeded (claim was after) or failed (claim was before).
        // In no case should a ghost row appear.
        if truncate_result.is_ok() {
            // Truncate completed first — table is empty, claim is on empty store
            assert!(store.versions.read().is_empty() || store.versions.read().len() <= 1);
        } else {
            // Claim was first — truncate correctly rejected
            assert!(claim_result.is_ok());
        }
    }
}