heliosdb-nano 4.6.2

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
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
//! ART Index Manager
//!
//! Manages the lifecycle of ART indexes with automatic creation for:
//! - Primary Keys (PKs)
//! - Foreign Keys (FKs)
//! - Unique Constraints
//!
//! The manager handles:
//! - Automatic index creation during DDL operations
//! - Constraint enforcement (uniqueness, referential integrity)
//! - Index maintenance on DML operations (INSERT/UPDATE/DELETE)
//! - Index persistence and recovery

use super::art_index::{AdaptiveRadixTree, ArtIndexError, ArtIndexStats, ArtIndexType, ArtResult};
use super::art_node::RowId;
use super::index_snapshot::ArtIndexSnapshot;
use crate::{DataType, Schema, Tuple, Value};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, RwLock, RwLockReadGuard};

/// Shared handle to a single ART index tree.
///
/// Callers must keep the read/write lock scope as tight as possible and must
/// follow the locking rules documented on [`ArtIndexManager`].
pub type SharedArtIndex = Arc<RwLock<AdaptiveRadixTree>>;

/// A registered index: per-registration metadata plus the shared tree handle.
///
/// The metadata mirrors the immutable identity fields inside the tree
/// (`table`, `columns`, `index_type`) so hot paths can answer "which indexes
/// belong to this table?" without taking any tree lock. The metadata is only
/// mutated while holding the global `indexes` WRITE lock (table rename).
#[derive(Debug, Clone)]
struct IndexEntry {
    /// Table this index belongs to (mirrors `tree.table()`).
    table: String,
    /// Indexed columns (mirrors `tree.columns()`).
    columns: Vec<String>,
    /// Index kind (mirrors `tree.index_type()`).
    index_type: ArtIndexType,
    /// The actual tree, individually locked.
    tree: SharedArtIndex,
}

impl IndexEntry {
    fn new(tree: AdaptiveRadixTree) -> Self {
        Self {
            table: tree.table().to_string(),
            columns: tree.columns().to_vec(),
            index_type: tree.index_type(),
            tree: Arc::new(RwLock::new(tree)),
        }
    }
}

/// Metadata about a foreign key constraint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForeignKeyInfo {
    /// Name of the foreign key constraint
    pub name: String,
    /// Source table
    pub table: String,
    /// Source columns
    pub columns: Vec<String>,
    /// Referenced table
    pub ref_table: String,
    /// Referenced columns
    pub ref_columns: Vec<String>,
    /// Index name for this FK
    pub index_name: String,
}

/// Statistics for the ART manager
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ArtManagerStats {
    /// Total number of indexes
    pub total_indexes: u64,
    /// Number of PK indexes
    pub pk_indexes: u64,
    /// Number of FK indexes
    pub fk_indexes: u64,
    /// Number of UNIQUE indexes
    pub unique_indexes: u64,
    /// Number of manual indexes
    pub manual_indexes: u64,
    /// Total constraint checks performed
    pub constraint_checks: u64,
    /// Number of constraint violations caught
    pub violations_caught: u64,
    /// Number of index renames performed
    pub index_renames: u64,
}

#[derive(Debug, Default)]
struct AtomicArtManagerStats {
    total_indexes: AtomicU64,
    pk_indexes: AtomicU64,
    fk_indexes: AtomicU64,
    unique_indexes: AtomicU64,
    manual_indexes: AtomicU64,
    constraint_checks: AtomicU64,
    violations_caught: AtomicU64,
    index_renames: AtomicU64,
}

impl AtomicArtManagerStats {
    fn snapshot(&self) -> ArtManagerStats {
        ArtManagerStats {
            total_indexes: self.total_indexes.load(Ordering::Relaxed),
            pk_indexes: self.pk_indexes.load(Ordering::Relaxed),
            fk_indexes: self.fk_indexes.load(Ordering::Relaxed),
            unique_indexes: self.unique_indexes.load(Ordering::Relaxed),
            manual_indexes: self.manual_indexes.load(Ordering::Relaxed),
            constraint_checks: self.constraint_checks.load(Ordering::Relaxed),
            violations_caught: self.violations_caught.load(Ordering::Relaxed),
            index_renames: self.index_renames.load(Ordering::Relaxed),
        }
    }

    fn add_index(&self, index_type: ArtIndexType) {
        self.total_indexes.fetch_add(1, Ordering::Relaxed);
        match index_type {
            ArtIndexType::PrimaryKey => {
                self.pk_indexes.fetch_add(1, Ordering::Relaxed);
            }
            ArtIndexType::ForeignKey => {
                self.fk_indexes.fetch_add(1, Ordering::Relaxed);
            }
            ArtIndexType::Unique => {
                self.unique_indexes.fetch_add(1, Ordering::Relaxed);
            }
            ArtIndexType::Manual => {
                self.manual_indexes.fetch_add(1, Ordering::Relaxed);
            }
        }
    }

    fn remove_index(&self, index_type: ArtIndexType) {
        self.total_indexes.fetch_sub(1, Ordering::Relaxed);
        match index_type {
            ArtIndexType::PrimaryKey => {
                self.pk_indexes.fetch_sub(1, Ordering::Relaxed);
            }
            ArtIndexType::ForeignKey => {
                self.fk_indexes.fetch_sub(1, Ordering::Relaxed);
            }
            ArtIndexType::Unique => {
                self.unique_indexes.fetch_sub(1, Ordering::Relaxed);
            }
            ArtIndexType::Manual => {
                self.manual_indexes.fetch_sub(1, Ordering::Relaxed);
            }
        }
    }
}

/// ART Index Manager
///
/// Thread-safe manager for all ART indexes in the database.
///
/// # Locking rules (deadlock safety)
///
/// Each index tree carries its own `RwLock` (see [`SharedArtIndex`]) so that
/// writers on one table no longer block readers/writers on unrelated tables.
///
/// 1. The global `indexes` map lock is taken as READ on all DML/lookup paths
///    (concurrent across tables). It is taken as WRITE only for registry
///    changes: create, drop, and rename.
/// 2. Lock order is strictly `global map lock -> (at most one) tree lock`.
///    A tree lock may be acquired while holding the global lock, but the
///    global lock must NEVER be acquired while holding a tree lock.
/// 3. INVARIANT: never hold two tree locks simultaneously. Iteration loops
///    (`on_insert*`, `on_delete*`, constraint checks, `clear_table_indexes`)
///    lock one tree, release it, then lock the next. In particular, the FK
///    existence check (`check_fk_constraints` / `unique_key_exists` reading
///    another table's PK index) acquires the referenced tree's lock only
///    while holding no other tree lock. Because no thread ever waits on a
///    tree lock while holding another tree lock, no lock-cycle (deadlock)
///    can form regardless of table/FK topology.
/// 4. The name maps (`pk_indexes`, `fk_indexes`, `fk_info`, `unique_indexes`,
///    `table_indexes`) are leaf locks: clone the names you need and release
///    them before taking the global map lock or any tree lock. In particular
///    `table_indexes` is NEVER held while `indexes` (or a tree) is held, so it
///    adds no new lock rank — a writer must not hold `indexes` and
///    `table_indexes` at once (register/drop/rename take each in its own
///    block), which is what keeps the leaf discipline cycle-free.
/// 5. Per-table filtering uses the metadata cached in [`IndexEntry`]
///    (no tree lock needed). The DML mutation loops (`on_insert*`,
///    `on_delete*`) resolve a table's complete index set from `table_indexes`
///    (leaf lock: clone the names, release, then look each entry up in
///    `indexes`) — an O(own indexes) lookup that DOES cover Manual (plain
///    secondary) indexes, unlike the partial `pk_indexes`/`fk_indexes`/
///    `unique_indexes` maps. DDL / snapshot scans (`drop_table_indexes`,
///    `rename_table_indexes`, `export_table_snapshot`, `list_table_indexes`)
///    still filter the entry map by `entry.table`.
#[derive(Debug)]
pub struct ArtIndexManager {
    /// All indexes by name
    indexes: RwLock<HashMap<String, IndexEntry>>,
    /// Primary key index name by table
    pk_indexes: RwLock<HashMap<String, String>>,
    /// Foreign key indexes by table (table -> list of FK index names)
    fk_indexes: RwLock<HashMap<String, Vec<String>>>,
    /// Foreign key metadata by index name
    fk_info: RwLock<HashMap<String, ForeignKeyInfo>>,
    /// Unique constraint indexes by table (table -> list of unique index names)
    unique_indexes: RwLock<HashMap<String, Vec<String>>>,
    /// W3.4 §3.2: complete per-table index name list — PK, FK, Unique, AND
    /// Manual. The DML mutation loops resolve a table's own indexes here in
    /// O(own indexes) instead of scanning the global `indexes` map, whose cost
    /// is O(all registered indexes system-wide) (the "many-table scaling
    /// cliff"). It is a DERIVED index of `indexes`, kept in lock-step at every
    /// register/drop/rename choke point so it can never drift (asserted by the
    /// register/drop/rename/clear consistency test). TRUNCATE
    /// (`clear_table_indexes`) does NOT touch it — the registrations survive,
    /// only the trees are emptied. Leaf lock (see locking rules #4/#5).
    table_indexes: RwLock<HashMap<String, Vec<String>>>,
    /// Statistics
    stats: AtomicArtManagerStats,
    /// R4.2 durable-snapshot validity tracking. `true` means the persisted
    /// snapshot markers (if any) still describe the current in-memory state.
    /// The FIRST mutation after a checkpoint flips it and fires
    /// `snapshot_invalidation_hook` exactly once, which durably deletes the
    /// markers — so a crash can never leave a stale snapshot trusted.
    /// Hot-path cost: one relaxed atomic load per index mutation.
    snapshot_clean: AtomicBool,
    /// Engine-wired callback that deletes the ART snapshot markers in RocksDB.
    snapshot_invalidation_hook: RwLock<Option<InvalidationHook>>,
}

/// Opaque wrapper so the manager can keep `#[derive(Debug)]`.
#[derive(Clone)]
pub struct InvalidationHook(pub Arc<dyn Fn() + Send + Sync>);

impl std::fmt::Debug for InvalidationHook {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("InvalidationHook")
    }
}

impl Default for ArtIndexManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Stable on-disk tag for [`ArtIndexType`] (snapshot format).
pub(crate) fn index_type_tag(index_type: ArtIndexType) -> u8 {
    match index_type {
        ArtIndexType::PrimaryKey => 0,
        ArtIndexType::ForeignKey => 1,
        ArtIndexType::Unique => 2,
        ArtIndexType::Manual => 3,
    }
}

/// Decode a sign-flipped big-endian integer key (encoding v2 — the inverse of
/// `ArtIndexManager::encode_value_into` for `Int2/Int4/Int8`).
fn decode_int_key(key: &[u8], width: usize) -> Option<i64> {
    match width {
        2 if key.len() == 2 => Some(i64::from((u16::from_be_bytes([key[0], key[1]]) ^ 0x8000) as i16)),
        4 if key.len() == 4 => Some(i64::from(
            (u32::from_be_bytes([key[0], key[1], key[2], key[3]]) ^ 0x8000_0000) as i32,
        )),
        8 if key.len() == 8 => Some(
            (u64::from_be_bytes([key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7]])
                ^ 0x8000_0000_0000_0000) as i64,
        ),
        _ => None,
    }
}

impl ArtIndexManager {
    /// Create a new ART index manager
    pub fn new() -> Self {
        Self {
            indexes: RwLock::new(HashMap::new()),
            pk_indexes: RwLock::new(HashMap::new()),
            fk_indexes: RwLock::new(HashMap::new()),
            fk_info: RwLock::new(HashMap::new()),
            unique_indexes: RwLock::new(HashMap::new()),
            table_indexes: RwLock::new(HashMap::new()),
            stats: AtomicArtManagerStats::default(),
            // Armed at startup: markers from a previous clean shutdown must
            // be invalidated by the first mutation of this process.
            snapshot_clean: AtomicBool::new(true),
            snapshot_invalidation_hook: RwLock::new(None),
        }
    }

    // =========================================================================
    // R4.2: DURABLE SNAPSHOT SUPPORT
    // =========================================================================

    /// Wire the callback that durably deletes the persisted snapshot markers.
    /// Called once by the storage engine at open.
    pub fn set_snapshot_invalidation_hook(&self, hook: Arc<dyn Fn() + Send + Sync>) {
        *self
            .snapshot_invalidation_hook
            .write()
            .unwrap_or_else(|e| e.into_inner()) = Some(InvalidationHook(hook));
    }

    /// Re-arm the validity flag after a checkpoint wrote fresh markers.
    pub fn mark_snapshot_clean(&self) {
        self.snapshot_clean.store(true, Ordering::Release);
    }

    /// True when no index mutation happened since the last checkpoint /
    /// `mark_snapshot_clean`. The checkpoint writer uses this to detect a
    /// concurrent mutation racing the marker write.
    pub fn snapshot_is_clean(&self) -> bool {
        self.snapshot_clean.load(Ordering::Acquire)
    }

    /// Record an index mutation. The first call after a checkpoint fires the
    /// invalidation hook (durable marker delete); subsequent calls are a
    /// single relaxed atomic load.
    #[inline]
    fn note_mutation(&self) {
        if self.snapshot_clean.load(Ordering::Relaxed) && self.snapshot_clean.swap(false, Ordering::AcqRel) {
            let hook = self
                .snapshot_invalidation_hook
                .read()
                .unwrap_or_else(|e| e.into_inner())
                .clone();
            if let Some(hook) = hook {
                (hook.0)();
            }
        }
    }

    /// Export every ART index registered on `table` as snapshot entries.
    /// Each tree is read-locked one at a time (manager locking rules hold).
    pub fn export_table_snapshot(&self, table: &str) -> Vec<ArtIndexSnapshot> {
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        let mut out = Vec::new();
        for (name, entry) in indexes.iter() {
            if entry.table != table {
                continue;
            }
            let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
            let mut entries: Vec<(Vec<u8>, Vec<u64>)> = Vec::new();
            for (key, row_id) in tree.iter() {
                match entries.last_mut() {
                    Some((last_key, ids)) if *last_key == key => ids.push(row_id),
                    _ => entries.push((key, vec![row_id])),
                }
            }
            out.push(ArtIndexSnapshot {
                name: name.clone(),
                table: entry.table.clone(),
                columns: entry.columns.clone(),
                index_type: index_type_tag(entry.index_type),
                entries,
            });
        }
        out.sort_by(|a, b| a.name.cmp(&b.name));
        out
    }

    /// Bulk-load snapshot entries into an already-registered (empty) index.
    ///
    /// `dense_int_width` carries the byte width of a single-column integer
    /// primary key so the dense-int range-count stats are restored exactly as
    /// the scan path's `on_insert` would have built them.
    pub fn load_index_entries(
        &self,
        name: &str,
        entries: &[(Vec<u8>, Vec<u64>)],
        dense_int_width: Option<usize>,
    ) -> ArtResult<usize> {
        let entry = {
            let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
            indexes
                .get(name)
                .cloned()
                .ok_or_else(|| ArtIndexError::IndexNotFound(name.to_string()))?
        };
        let mut tree = entry.tree.write().unwrap_or_else(|e| e.into_inner());
        if tree.len() != 0 {
            return Err(ArtIndexError::Internal(format!(
                "Refusing to load snapshot into non-empty index '{}' ({} entries)",
                name,
                tree.len()
            )));
        }
        let mut loaded = 0usize;
        for (key, row_ids) in entries {
            for row_id in row_ids {
                tree.insert(key, *row_id)?;
                loaded += 1;
            }
            if entry.index_type == ArtIndexType::PrimaryKey {
                if let Some(width) = dense_int_width {
                    if key.len() == width {
                        if let Some(value) = decode_int_key(key, width) {
                            tree.record_dense_int_insert(width, value);
                        }
                    }
                }
            }
        }
        Ok(loaded)
    }

    /// Generate index name for a primary key
    fn pk_index_name(table: &str) -> String {
        format!("{}_pkey", table)
    }

    /// Generate index name for a foreign key
    fn fk_index_name(table: &str, columns: &[String]) -> String {
        format!("{}_{}_fkey", table, columns.join("_"))
    }

    /// Generate index name for a unique constraint
    fn unique_index_name(table: &str, columns: &[String]) -> String {
        format!("{}_{}_key", table, columns.join("_"))
    }

    /// W3.4 §3.2: record a newly-registered index in the per-table entry list.
    /// Called at every `indexes` registration choke point AFTER the entry lands
    /// in `indexes`, so the two never disagree. Leaf lock (rules #4/#5): taken
    /// alone, never while holding `indexes` or a tree lock.
    fn table_index_add(&self, table: &str, name: &str) {
        let mut table_indexes = self.table_indexes.write().unwrap_or_else(|e| e.into_inner());
        table_indexes
            .entry(table.to_string())
            .or_insert_with(Vec::new)
            .push(name.to_string());
    }

    /// W3.4 §3.2: drop an index name from the per-table entry list, removing
    /// the table key once its last index is gone (so the map stays byte-for-byte
    /// identical to a full `indexes` filter — the §3.2 consistency invariant).
    fn table_index_remove(&self, table: &str, name: &str) {
        let mut table_indexes = self.table_indexes.write().unwrap_or_else(|e| e.into_inner());
        if let Some(list) = table_indexes.get_mut(table) {
            list.retain(|n| n != name);
            if list.is_empty() {
                table_indexes.remove(table);
            }
        }
    }

    // =========================================================================
    // INDEX CREATION
    // =========================================================================

    /// Create a primary key index (auto-called on CREATE TABLE with PRIMARY KEY)
    pub fn create_pk_index(&self, table: &str, columns: &[String]) -> ArtResult<String> {
        self.note_mutation();
        let index_name = Self::pk_index_name(table);

        // Check if PK already exists for this table
        {
            let pk_indexes = self.pk_indexes.read().unwrap_or_else(|e| e.into_inner());
            if pk_indexes.contains_key(table) {
                return Err(ArtIndexError::IndexAlreadyExists(format!(
                    "Primary key already exists for table '{}'",
                    table
                )));
            }
        }

        // Create the index
        let index = AdaptiveRadixTree::new(&index_name, table, columns.to_vec(), ArtIndexType::PrimaryKey);

        // Register the index
        {
            let mut indexes = self.indexes.write().unwrap_or_else(|e| e.into_inner());
            indexes.insert(index_name.clone(), IndexEntry::new(index));
        }

        {
            let mut pk_indexes = self.pk_indexes.write().unwrap_or_else(|e| e.into_inner());
            pk_indexes.insert(table.to_string(), index_name.clone());
        }

        self.table_index_add(table, &index_name);
        self.stats.add_index(ArtIndexType::PrimaryKey);

        Ok(index_name)
    }

    /// Create a foreign key index (auto-called on ALTER TABLE ADD FOREIGN KEY)
    pub fn create_fk_index(
        &self,
        table: &str,
        columns: &[String],
        ref_table: &str,
        ref_columns: &[String],
        constraint_name: Option<&str>,
    ) -> ArtResult<String> {
        self.note_mutation();
        let index_name = constraint_name
            .map(|n| n.to_string())
            .unwrap_or_else(|| Self::fk_index_name(table, columns));

        // Check if index already exists
        {
            let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
            if indexes.contains_key(&index_name) {
                return Err(ArtIndexError::IndexAlreadyExists(index_name));
            }
        }

        // Verify that the referenced table has a PK or unique constraint on ref_columns
        // (This would be checked during DDL execution)

        // Create the index
        let index = AdaptiveRadixTree::new(&index_name, table, columns.to_vec(), ArtIndexType::ForeignKey);

        // Create FK info
        let fk_info = ForeignKeyInfo {
            name: index_name.clone(),
            table: table.to_string(),
            columns: columns.to_vec(),
            ref_table: ref_table.to_string(),
            ref_columns: ref_columns.to_vec(),
            index_name: index_name.clone(),
        };

        // Register everything
        {
            let mut indexes = self.indexes.write().unwrap_or_else(|e| e.into_inner());
            indexes.insert(index_name.clone(), IndexEntry::new(index));
        }

        {
            let mut fk_indexes = self.fk_indexes.write().unwrap_or_else(|e| e.into_inner());
            fk_indexes
                .entry(table.to_string())
                .or_insert_with(Vec::new)
                .push(index_name.clone());
        }

        {
            let mut fk_info_map = self.fk_info.write().unwrap_or_else(|e| e.into_inner());
            fk_info_map.insert(index_name.clone(), fk_info);
        }

        self.table_index_add(table, &index_name);
        self.stats.add_index(ArtIndexType::ForeignKey);

        Ok(index_name)
    }

    /// Create a unique constraint index (auto-called on CREATE TABLE UNIQUE or ALTER TABLE ADD UNIQUE)
    pub fn create_unique_index(
        &self,
        table: &str,
        columns: &[String],
        constraint_name: Option<&str>,
    ) -> ArtResult<String> {
        self.note_mutation();
        let index_name = constraint_name
            .map(|n| n.to_string())
            .unwrap_or_else(|| Self::unique_index_name(table, columns));

        // Check if index already exists
        {
            let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
            if indexes.contains_key(&index_name) {
                return Err(ArtIndexError::IndexAlreadyExists(index_name));
            }
        }

        // Create the index
        let index = AdaptiveRadixTree::new(&index_name, table, columns.to_vec(), ArtIndexType::Unique);

        // Register the index
        {
            let mut indexes = self.indexes.write().unwrap_or_else(|e| e.into_inner());
            indexes.insert(index_name.clone(), IndexEntry::new(index));
        }

        {
            let mut unique_indexes = self.unique_indexes.write().unwrap_or_else(|e| e.into_inner());
            unique_indexes
                .entry(table.to_string())
                .or_insert_with(Vec::new)
                .push(index_name.clone());
        }

        self.table_index_add(table, &index_name);
        self.stats.add_index(ArtIndexType::Unique);

        Ok(index_name)
    }

    /// Create a manual index (via CREATE INDEX ... USING ART)
    pub fn create_manual_index(&self, name: &str, table: &str, columns: &[String]) -> ArtResult<String> {
        self.note_mutation();
        // Check if index already exists
        {
            let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
            if indexes.contains_key(name) {
                return Err(ArtIndexError::IndexAlreadyExists(name.to_string()));
            }
        }

        // Create the index
        let index = AdaptiveRadixTree::new(name, table, columns.to_vec(), ArtIndexType::Manual);

        // Register the index
        {
            let mut indexes = self.indexes.write().unwrap_or_else(|e| e.into_inner());
            indexes.insert(name.to_string(), IndexEntry::new(index));
        }

        self.table_index_add(table, name);
        self.stats.add_index(ArtIndexType::Manual);

        Ok(name.to_string())
    }

    /// Populate an existing manual index from already-materialized table rows.
    ///
    /// This is intentionally scoped to one named manual index. Calling the normal
    /// table insert maintenance path would also touch PK/UNIQUE indexes and hit
    /// duplicates for rows that were already present before CREATE INDEX.
    pub fn backfill_manual_index(&self, name: &str, schema: &Schema, tuples: &[Tuple]) -> ArtResult<usize> {
        self.note_mutation();
        // Global READ is enough: the registry is not changed, only one tree.
        let entry = {
            let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
            indexes
                .get(name)
                .cloned()
                .ok_or_else(|| ArtIndexError::IndexNotFound(name.to_string()))?
        };
        if entry.index_type != ArtIndexType::Manual {
            return Err(ArtIndexError::Internal(format!(
                "Index '{}' is not a manual secondary index",
                name
            )));
        }

        let mut index = entry.tree.write().unwrap_or_else(|e| e.into_inner());
        let mut inserted = 0usize;
        for tuple in tuples {
            let Some(row_id) = tuple.row_id else {
                return Err(ArtIndexError::Internal(format!(
                    "Cannot backfill index '{}' from tuple without row_id",
                    name
                )));
            };
            if let Some(values) = Self::index_value_refs_from_tuple(&entry.columns, schema, tuple) {
                let key = Self::encode_key_from_values(values.iter().copied());
                index.insert(&key, row_id)?;
                inserted += 1;
            }
        }

        Ok(inserted)
    }

    /// Backfill a foreign-key index from existing rows. Mirrors
    /// `backfill_manual_index` but for `ForeignKey` indexes. Needed when a
    /// foreign key is added to a table that already holds data: `create_fk_index`
    /// registers an empty tree, and the planner may answer `WHERE fk_col = …`
    /// (and FK-column joins) from it — so without this backfill the pre-existing
    /// rows are invisible and such lookups silently return zero matches.
    pub fn backfill_fk_index(&self, name: &str, schema: &Schema, tuples: &[Tuple]) -> ArtResult<usize> {
        self.note_mutation();
        let entry = {
            let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
            indexes
                .get(name)
                .cloned()
                .ok_or_else(|| ArtIndexError::IndexNotFound(name.to_string()))?
        };
        if entry.index_type != ArtIndexType::ForeignKey {
            return Err(ArtIndexError::Internal(format!(
                "Index '{}' is not a foreign-key index",
                name
            )));
        }

        let mut index = entry.tree.write().unwrap_or_else(|e| e.into_inner());
        let mut inserted = 0usize;
        for tuple in tuples {
            let Some(row_id) = tuple.row_id else {
                return Err(ArtIndexError::Internal(format!(
                    "Cannot backfill index '{}' from tuple without row_id",
                    name
                )));
            };
            if let Some(values) = Self::index_value_refs_from_tuple(&entry.columns, schema, tuple) {
                let key = Self::encode_key_from_values(values.iter().copied());
                index.insert(&key, row_id)?;
                inserted += 1;
            }
        }

        Ok(inserted)
    }

    // =========================================================================
    // INDEX REMOVAL
    // =========================================================================

    /// Drop an index by name
    pub fn drop_index(&self, name: &str) -> ArtResult<()> {
        self.note_mutation();
        let index_type;
        let table;

        // Remove from main index map
        {
            let mut indexes = self.indexes.write().unwrap_or_else(|e| e.into_inner());
            if let Some(entry) = indexes.remove(name) {
                index_type = entry.index_type;
                table = entry.table;
            } else {
                return Err(ArtIndexError::IndexNotFound(name.to_string()));
            }
        }

        // W3.4 §3.2: drop the name from the per-table entry list (leaf lock,
        // taken alone — never while `indexes` is held).
        self.table_index_remove(&table, name);

        // Remove from type-specific maps
        match index_type {
            ArtIndexType::PrimaryKey => {
                let mut pk_indexes = self.pk_indexes.write().unwrap_or_else(|e| e.into_inner());
                pk_indexes.retain(|_, v| v != name);
            }
            ArtIndexType::ForeignKey => {
                let mut fk_indexes = self.fk_indexes.write().unwrap_or_else(|e| e.into_inner());
                for fks in fk_indexes.values_mut() {
                    fks.retain(|n| n != name);
                }
                let mut fk_info = self.fk_info.write().unwrap_or_else(|e| e.into_inner());
                fk_info.remove(name);
            }
            ArtIndexType::Unique => {
                let mut unique_indexes = self.unique_indexes.write().unwrap_or_else(|e| e.into_inner());
                for uqs in unique_indexes.values_mut() {
                    uqs.retain(|n| n != name);
                }
            }
            ArtIndexType::Manual => {
                // No additional cleanup needed
            }
        }

        self.stats.remove_index(index_type);

        Ok(())
    }

    /// Drop all indexes for a table (called on DROP TABLE)
    pub fn drop_table_indexes(&self, table: &str) -> ArtResult<()> {
        let mut to_drop = Vec::new();

        // Collect all indexes for this table (metadata only, no tree locks)
        {
            let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
            for (name, entry) in indexes.iter() {
                if entry.table == table {
                    to_drop.push(name.clone());
                }
            }
        }

        // Drop each index
        for name in to_drop {
            self.drop_index(&name)?;
        }

        Ok(())
    }

    /// Rename all indexes for a table (called on RENAME TABLE)
    pub fn rename_table_indexes(&self, old_table: &str, new_table: &str) -> ArtResult<()> {
        self.note_mutation();
        // Move the entries under the global WRITE lock. Trees are renamed in
        // place (one tree lock at a time, see locking rules) — no tree clone.
        let mut renames: Vec<(String, String)> = Vec::new();

        {
            let mut indexes = self.indexes.write().unwrap_or_else(|e| e.into_inner());

            let matching: Vec<String> = indexes
                .iter()
                .filter(|(_, entry)| entry.table == old_table)
                .map(|(name, _)| name.clone())
                .collect();

            for old_name in matching {
                // Generate new index name by replacing table name
                let new_name = old_name
                    .replace(&format!("_{}_", old_table), &format!("_{}_", new_table))
                    .replace(&format!("pk_{}", old_table), &format!("pk_{}", new_table))
                    .replace(&format!("fk_{}", old_table), &format!("fk_{}", new_table))
                    .replace(&format!("unique_{}", old_table), &format!("unique_{}", new_table));

                if let Some(mut entry) = indexes.remove(&old_name) {
                    {
                        let mut tree = entry.tree.write().unwrap_or_else(|e| e.into_inner());
                        tree.rename(new_table.to_string(), new_name.clone());
                    }
                    entry.table = new_table.to_string();
                    indexes.insert(new_name.clone(), entry);
                    renames.push((old_name, new_name));
                }
            }
        }

        // W3.4 §3.2: move the per-table entry list to the new table key with
        // the renamed index names (leaf lock, taken alone — after the `indexes`
        // WRITE block above has been released).
        {
            let mut table_indexes = self.table_indexes.write().unwrap_or_else(|e| e.into_inner());
            if table_indexes.remove(old_table).is_some() {
                let new_names: Vec<String> = renames.iter().map(|(_, new_name)| new_name.clone()).collect();
                if !new_names.is_empty() {
                    table_indexes.insert(new_table.to_string(), new_names);
                }
            }
        }

        // Apply name-map updates (leaf locks, taken one at a time)
        for (old_name, new_name) in renames {
            // Update pk_indexes mapping
            {
                let mut pk_indexes = self.pk_indexes.write().unwrap_or_else(|e| e.into_inner());
                if pk_indexes.get(old_table) == Some(&old_name) {
                    pk_indexes.remove(old_table);
                    pk_indexes.insert(new_table.to_string(), new_name.clone());
                }
            }

            // Update fk_indexes mapping
            {
                let mut fk_indexes = self.fk_indexes.write().unwrap_or_else(|e| e.into_inner());
                if let Some(fks) = fk_indexes.remove(old_table) {
                    let new_fks: Vec<String> = fks
                        .iter()
                        .map(|n| if n == &old_name { new_name.clone() } else { n.clone() })
                        .collect();
                    fk_indexes.insert(new_table.to_string(), new_fks);
                }
            }

            // Update unique_indexes mapping
            {
                let mut unique_indexes = self.unique_indexes.write().unwrap_or_else(|e| e.into_inner());
                if let Some(uniques) = unique_indexes.remove(old_table) {
                    let new_uniques: Vec<String> = uniques
                        .iter()
                        .map(|n| if n == &old_name { new_name.clone() } else { n.clone() })
                        .collect();
                    unique_indexes.insert(new_table.to_string(), new_uniques);
                }
            }
        }

        self.stats.index_renames.fetch_add(1, Ordering::Relaxed);

        Ok(())
    }

    // =========================================================================
    // INDEX ACCESS
    // =========================================================================

    /// Read-lock the index registry through the lock-census (W3.1). Off the
    /// `lock-census` feature this inlines to the previous
    /// `self.indexes.read().unwrap_or_else(poison)` at zero cost.
    #[inline]
    fn indexes_read(&self) -> RwLockReadGuard<'_, HashMap<String, IndexEntry>> {
        crate::lock_census::rwlock_read(crate::lock_census::Site::ArtIndexRegistry, &self.indexes)
    }

    /// Read-lock the table→PK-index-name map through the lock-census (W3.1).
    #[inline]
    fn pk_indexes_read(&self) -> RwLockReadGuard<'_, HashMap<String, String>> {
        crate::lock_census::rwlock_read(crate::lock_census::Site::ArtPkRegistry, &self.pk_indexes)
    }

    /// Get a shared handle to an index by name.
    ///
    /// Returns an `Arc<RwLock<…>>` handle instead of cloning the whole tree.
    /// Keep the lock scope on the returned handle as tight as possible.
    pub fn get_index(&self, name: &str) -> Option<SharedArtIndex> {
        let indexes = self.indexes_read();
        indexes.get(name).map(|entry| Arc::clone(&entry.tree))
    }

    /// Get the primary key index for a table
    pub fn get_pk_index(&self, table: &str) -> Option<SharedArtIndex> {
        let pk_name = {
            let pk_indexes = self.pk_indexes_read();
            pk_indexes.get(table).cloned()
        };

        pk_name.and_then(|name| self.get_index(&name))
    }

    /// Get all foreign key indexes for a table
    pub fn get_fk_indexes(&self, table: &str) -> Vec<SharedArtIndex> {
        let fk_names = {
            let fk_indexes = self.fk_indexes.read().unwrap_or_else(|e| e.into_inner());
            fk_indexes.get(table).cloned().unwrap_or_default()
        };

        fk_names.iter().filter_map(|name| self.get_index(name)).collect()
    }

    /// Get all unique indexes for a table
    pub fn get_unique_indexes(&self, table: &str) -> Vec<SharedArtIndex> {
        let unique_names = {
            let unique_indexes = self.unique_indexes.read().unwrap_or_else(|e| e.into_inner());
            unique_indexes.get(table).cloned().unwrap_or_default()
        };

        unique_names.iter().filter_map(|name| self.get_index(name)).collect()
    }

    /// Get FK info by index name
    pub fn get_fk_info(&self, index_name: &str) -> Option<ForeignKeyInfo> {
        let fk_info = self.fk_info.read().unwrap_or_else(|e| e.into_inner());
        fk_info.get(index_name).cloned()
    }

    /// List all indexes
    pub fn list_indexes(&self) -> Vec<(String, String, ArtIndexType, Vec<String>)> {
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        indexes
            .iter()
            .map(|(name, entry)| {
                (
                    name.clone(),
                    entry.table.clone(),
                    entry.index_type,
                    entry.columns.clone(),
                )
            })
            .collect()
    }

    /// Find an index for a specific column in a table (returns index name if found)
    pub fn find_column_index(&self, table: &str, column: &str) -> Option<String> {
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        for (name, entry) in indexes.iter() {
            if entry.table == table && entry.columns.len() == 1 {
                if let Some(col) = entry.columns.first() {
                    if col == column {
                        return Some(name.clone());
                    }
                }
            }
        }
        None
    }

    /// Look up all row_ids for a key in a named index (avoids cloning the entire tree)
    pub fn index_get_all(&self, index_name: &str, key: &[u8]) -> Vec<RowId> {
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        if let Some(entry) = indexes.get(index_name) {
            let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
            tree.get_all(key)
        } else {
            Vec::new()
        }
    }

    /// R4.4: ordered, bounded range scan over a named index.
    ///
    /// Bounds are ENCODED keys (`encode_key_from_values` of a single value of
    /// the indexed column's type — encoding v2 is order-preserving per type).
    /// Returns `(key, row_id)` pairs in ascending key order.
    pub fn index_range_scan(
        &self,
        index_name: &str,
        lower: Option<(&[u8], bool)>,
        upper: Option<(&[u8], bool)>,
        limit: Option<usize>,
    ) -> Vec<(Vec<u8>, RowId)> {
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        if let Some(entry) = indexes.get(index_name) {
            let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
            tree.range_scan(lower, upper, limit)
        } else {
            Vec::new()
        }
    }

    /// Total `(key, row_id)` entry count of a named index (one entry per
    /// indexed row, including NULL-keyed entries).
    pub fn index_entry_count(&self, index_name: &str) -> Option<u64> {
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        indexes.get(index_name).map(|entry| {
            let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
            tree.len()
        })
    }

    /// PK index point lookup without cloning the tree (~50μs saved vs get_pk_index)
    pub fn pk_index_lookup(&self, table: &str, key: &[u8]) -> Option<RowId> {
        let pk_name = {
            let pk_indexes = self.pk_indexes_read();
            pk_indexes.get(table).cloned()
        };
        pk_name.and_then(|name| {
            let indexes = self.indexes_read();
            indexes.get(&name).and_then(|entry| {
                let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
                tree.get(key)
            })
        })
    }

    /// Check if a PK value exists without cloning the tree
    pub fn pk_index_contains(&self, table: &str, key: &[u8]) -> Option<bool> {
        let pk_name = {
            let pk_indexes = self.pk_indexes_read();
            pk_indexes.get(table).cloned()
        };
        pk_name.map(|name| {
            let indexes = self.indexes_read();
            indexes.get(&name).is_some_and(|entry| {
                let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
                tree.contains(key)
            })
        })
    }

    /// Count how many encoded keys exist in a table's PK index while holding
    /// the index read lock once. Callers are responsible for SQL-level
    /// duplicate handling before passing keys.
    pub fn pk_index_count_keys(&self, table: &str, keys: &[Vec<u8>]) -> Option<usize> {
        let pk_name = {
            let pk_indexes = self.pk_indexes.read().unwrap_or_else(|e| e.into_inner());
            pk_indexes.get(table).cloned()
        }?;
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        let entry = indexes.get(&pk_name)?;
        let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
        Some(keys.iter().filter(|key| tree.contains(key)).count())
    }

    /// Return the number of live entries in a table's primary-key index.
    ///
    /// For a PK index this is the table row count. This avoids cloning the ART
    /// and lets COUNT(*) skip a RocksDB key-prefix walk on ordinary PK tables.
    pub fn pk_index_len(&self, table: &str) -> Option<usize> {
        let pk_name = {
            let pk_indexes = self.pk_indexes.read().unwrap_or_else(|e| e.into_inner());
            pk_indexes.get(table).cloned()
        }?;
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        indexes.get(&pk_name).map(|entry| {
            let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
            tree.len() as usize
        })
    }

    /// Return true when the table has exactly one ART index: a single-column
    /// primary-key index. Such tables can delete by PK without materializing
    /// the old tuple because no secondary index needs old column values.
    pub fn has_only_single_column_pk_index(&self, table: &str) -> bool {
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        let mut pk_count = 0usize;

        for entry in indexes.values().filter(|entry| entry.table == table) {
            match entry.index_type {
                ArtIndexType::PrimaryKey if entry.columns.len() == 1 => {
                    pk_count += 1;
                }
                _ => return false,
            }
        }

        pk_count == 1
    }

    /// Count rows in a single-column integer PK index that satisfy an optional
    /// numeric range. Iterates the in-memory ART only; it does not fetch or
    /// deserialize table rows.
    pub fn pk_index_count_int_range(
        &self,
        table: &str,
        pk_type: &DataType,
        lower: Option<(i64, bool)>,
        upper: Option<(i64, bool)>,
    ) -> Option<usize> {
        let key_width = match pk_type {
            DataType::Int2 => 2,
            DataType::Int4 => 4,
            DataType::Int8 => 8,
            _ => return None,
        };
        let pk_name = {
            let pk_indexes = self.pk_indexes.read().unwrap_or_else(|e| e.into_inner());
            pk_indexes.get(table).cloned()
        }?;
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        let entry = indexes.get(&pk_name)?;
        if entry.columns.len() != 1 {
            return None;
        }
        let index = entry.tree.read().unwrap_or_else(|e| e.into_inner());
        if let Some(count) = index.dense_int_count(key_width, lower, upper) {
            return Some(count);
        }

        Some(
            index
                .iter()
                .filter_map(|(key, _)| decode_int_key(&key, key_width))
                .filter(|value| {
                    lower.map_or(
                        true,
                        |(bound, inclusive)| {
                            if inclusive {
                                *value >= bound
                            } else {
                                *value > bound
                            }
                        },
                    ) && upper.map_or(
                        true,
                        |(bound, inclusive)| {
                            if inclusive {
                                *value <= bound
                            } else {
                                *value < bound
                            }
                        },
                    )
                })
                .count(),
        )
    }

    /// List indexes for a specific table
    pub fn list_table_indexes(&self, table: &str) -> Vec<(String, ArtIndexType, Vec<String>)> {
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        indexes
            .iter()
            .filter(|(_, entry)| entry.table == table)
            .map(|(name, entry)| (name.clone(), entry.index_type, entry.columns.clone()))
            .collect()
    }

    // =========================================================================
    // CONSTRAINT ENFORCEMENT
    // =========================================================================

    /// Encode a composite key from multiple values
    pub fn encode_key(values: &[Value]) -> Vec<u8> {
        Self::encode_key_from_values(values.iter())
    }

    /// Encode a composite key from borrowed values.
    ///
    /// Hot insert validation paths often already hold references into a tuple;
    /// this avoids cloning strings/arrays solely to call `encode_key`.
    ///
    /// R4.4 — encoding v2, ORDER-PRESERVING for the range-scannable types:
    /// unsigned byte-wise comparison of two encoded single-column keys of the
    /// same column type matches SQL value order. Concretely:
    /// - integers: sign-flipped big-endian (`v XOR MIN` reinterpreted
    ///   unsigned), so negatives sort before positives;
    /// - floats: IEEE-754 total-order transform (positive: flip sign bit;
    ///   negative: flip all bits);
    /// - TEXT/BYTEA: raw bytes (UTF-8 byte order == code-point order).
    /// Composite (multi-column) keys additionally escape `0x00 -> 0x00 0xFF`
    /// inside variable-length values so a value byte can never collide with
    /// the `0x00` column separator. Single-column keys are NEVER escaped —
    /// range scans rely on their raw byte order.
    ///
    /// Persisted index snapshots stamp this version
    /// (`index_snapshot::ART_KEY_ENCODING_VERSION`); a snapshot written with
    /// a different version is ignored and rebuilt from rows, so the encoding
    /// can evolve without an on-disk migration step.
    pub fn encode_key_from_values<'a>(values: impl IntoIterator<Item = &'a Value>) -> Vec<u8> {
        let mut key = Vec::new();
        let mut iter = values.into_iter();
        let Some(first) = iter.next() else {
            return key;
        };
        let Some(second) = iter.next() else {
            // Single-column key: no separator, no escaping.
            Self::encode_value_into(&mut key, first, false);
            return key;
        };
        Self::encode_value_into(&mut key, first, true);
        key.push(0); // Separator
        Self::encode_value_into(&mut key, second, true);
        for value in iter {
            key.push(0); // Separator
            Self::encode_value_into(&mut key, value, true);
        }
        key
    }

    /// Append one value's encoding to `key`. `escape` is true in composite
    /// (multi-column) keys: `0x00` bytes inside variable-length values are
    /// escaped as `0x00 0xFF` so they cannot collide with the separator.
    fn encode_value_into(key: &mut Vec<u8>, value: &Value, escape: bool) {
        match value {
            Value::Null => key.extend_from_slice(b"\x00"),
            Value::Boolean(b) => key.push(if *b { 1 } else { 0 }),
            // Sign-flip: maps signed integer order onto unsigned byte order.
            Value::Int2(v) => key.extend_from_slice(&((*v as u16) ^ 0x8000).to_be_bytes()),
            Value::Int4(v) => key.extend_from_slice(&((*v as u32) ^ 0x8000_0000).to_be_bytes()),
            Value::Int8(v) => key.extend_from_slice(&((*v as u64) ^ 0x8000_0000_0000_0000).to_be_bytes()),
            // IEEE-754 total order: positive floats get the sign bit set,
            // negative floats are bitwise inverted.
            Value::Float4(v) => {
                let bits = v.to_bits();
                let ordered = if bits & 0x8000_0000 == 0 {
                    bits ^ 0x8000_0000
                } else {
                    !bits
                };
                key.extend_from_slice(&ordered.to_be_bytes());
            }
            Value::Float8(v) => {
                let bits = v.to_bits();
                let ordered = if bits & 0x8000_0000_0000_0000 == 0 {
                    bits ^ 0x8000_0000_0000_0000
                } else {
                    !bits
                };
                key.extend_from_slice(&ordered.to_be_bytes());
            }
            Value::String(s) => Self::extend_maybe_escaped(key, s.as_bytes(), escape),
            Value::Bytes(b) => Self::extend_maybe_escaped(key, b, escape),
            Value::Uuid(u) => key.extend_from_slice(u.as_bytes()),
            Value::Numeric(d) => Self::extend_maybe_escaped(key, d.as_bytes(), escape),
            Value::Date(d) => key.extend_from_slice(d.to_string().as_bytes()),
            Value::Time(t) => key.extend_from_slice(t.to_string().as_bytes()),
            Value::Timestamp(ts) => key.extend_from_slice(ts.to_rfc3339().as_bytes()),
            Value::Array(arr) => {
                // Recursively encode array elements
                let nested = Self::encode_key_from_values(arr.iter());
                Self::extend_maybe_escaped(key, &nested, escape);
            }
            Value::Json(j) => Self::extend_maybe_escaped(key, j.as_bytes(), escape),
            Value::Vector(v) => {
                for f in v {
                    key.extend_from_slice(&f.to_be_bytes());
                }
            }
            // Handle storage mode references
            Value::DictRef { dict_id } => key.extend_from_slice(&dict_id.to_be_bytes()),
            Value::CasRef { hash } => key.extend_from_slice(hash),
            Value::ColumnarRef => {
                // Columnar reference doesn't have direct key encoding
                // The actual value should be resolved before indexing
                key.extend_from_slice(b"columnar_ref");
            }
            Value::Interval(iv) => key.extend_from_slice(&iv.to_be_bytes()), // Encode interval microseconds
        }
    }

    fn extend_maybe_escaped(key: &mut Vec<u8>, bytes: &[u8], escape: bool) {
        if !escape || !bytes.contains(&0) {
            key.extend_from_slice(bytes);
            return;
        }
        for &b in bytes {
            if b == 0 {
                key.push(0);
                key.push(0xFF);
            } else {
                key.push(b);
            }
        }
    }

    fn index_value_refs_from_tuple<'a>(
        columns: &[String],
        schema: &Schema,
        tuple: &'a Tuple,
    ) -> Option<Vec<&'a Value>> {
        let mut values = Vec::with_capacity(columns.len());
        for column in columns {
            let idx = schema.get_column_index(column)?;
            values.push(tuple.values.get(idx)?);
        }
        Some(values)
    }

    /// W3.4 §3.3 (encode-once): return the encoding of one column value,
    /// building it once per `(schema column index, escape)` and caching it in
    /// `cache` for reuse across the row's other indexes that share the column.
    ///
    /// The cache key includes `escape` because a value's encoding depends on
    /// it: single-column keys are never escaped while multi-column keys escape
    /// every value (`encode_key_from_values`), so a column shared by a
    /// single-column and a multi-column index needs two distinct fragments.
    /// `cache` holds row-scoped fragments and MUST be cleared between rows.
    fn encode_fragment<'c>(
        cache: &'c mut Vec<(usize, bool, Vec<u8>)>,
        col_idx: usize,
        value: &Value,
        escape: bool,
    ) -> &'c [u8] {
        let pos = match cache.iter().position(|(ci, esc, _)| *ci == col_idx && *esc == escape) {
            Some(pos) => pos,
            None => {
                let mut buf = Vec::new();
                Self::encode_value_into(&mut buf, value, escape);
                cache.push((col_idx, escape, buf));
                cache.len() - 1
            }
        };
        &cache[pos].2
    }

    /// W3.4 §3.3 (encode-once): build one index key from already-resolved
    /// `(schema column index, value)` pairs, reusing per-column encoded
    /// fragments from `cache`.
    ///
    /// BYTE-IDENTICAL to `encode_key_from_values(cols.iter().map(|(_, v)| v))`
    /// (ART keys are on-disk-durable via snapshots, so this is load-bearing):
    /// a single-column key is the column's unescaped fragment; a multi-column
    /// key escapes every fragment and joins them with the `0x00` separator —
    /// exactly the two branches of `encode_key_from_values`.
    fn encode_key_cached(cache: &mut Vec<(usize, bool, Vec<u8>)>, cols: &[(usize, &Value)]) -> Vec<u8> {
        let escape = cols.len() > 1;
        let mut key = Vec::new();
        for (i, (sidx, v)) in cols.iter().enumerate() {
            if i > 0 {
                key.push(0); // column separator (matches encode_key_from_values)
            }
            let frag = Self::encode_fragment(cache, *sidx, v, escape);
            key.extend_from_slice(frag);
        }
        key
    }

    /// W3.4 §3.2 + §3.3: maintain every index in `names` (the table's complete
    /// entry list, resolved by the caller) for one inserted row, reusing the
    /// row-scoped encode-once `frag_cache` (cleared per row by the caller).
    ///
    /// `names` are looked up in the already-read `indexes` map (rule 2: the
    /// global read lock is held by the caller, one tree write lock is taken at
    /// a time). A column that does not resolve against `schema`/`tuple` skips
    /// that index — matching `index_value_refs_from_tuple` returning `None`.
    fn insert_row_indexes(
        indexes: &HashMap<String, IndexEntry>,
        names: &[String],
        row_id: RowId,
        schema: &Schema,
        tuple: &Tuple,
        frag_cache: &mut Vec<(usize, bool, Vec<u8>)>,
        wv: bool,
    ) -> ArtResult<()> {
        // Encode-once only pays off when >1 index may share a column; a
        // single-index table takes the original direct encode (zero overhead).
        let multi_index = names.len() > 1;
        for name in names {
            let Some(entry) = indexes.get(name) else {
                continue;
            };
            let cols = &entry.columns;
            let mut resolved: Vec<(usize, &Value)> = Vec::with_capacity(cols.len());
            let mut missing = false;
            for column in cols {
                let Some(sidx) = schema.get_column_index(column) else {
                    missing = true;
                    break;
                };
                let Some(v) = tuple.values.get(sidx) else {
                    missing = true;
                    break;
                };
                resolved.push((sidx, v));
            }
            if missing {
                continue;
            }
            let key = if multi_index {
                Self::encode_key_cached(frag_cache, &resolved)
            } else {
                Self::encode_key_from_values(resolved.iter().map(|(_, v)| *v))
            };
            // W3.2: one ART entry = encoded key + the u64 row-id payload.
            if wv {
                crate::write_volume::add(crate::write_volume::Category::IndexKey, (key.len() + 8) as u64);
            }
            let mut index = entry.tree.write().unwrap_or_else(|e| e.into_inner());
            match entry.index_type {
                ArtIndexType::PrimaryKey | ArtIndexType::Unique => {
                    index.insert(&key, row_id)?;
                    if entry.index_type == ArtIndexType::PrimaryKey && cols.len() == 1 {
                        if let Some((value, key_width)) = Self::int_value_width(resolved[0].1) {
                            index.record_dense_int_insert(key_width, value);
                        }
                    }
                }
                ArtIndexType::ForeignKey | ArtIndexType::Manual => {
                    let _ = index.insert(&key, row_id);
                }
            }
        }
        Ok(())
    }

    /// Check primary key constraint before INSERT
    pub fn check_pk_constraint(&self, table: &str, key_values: &[Value]) -> ArtResult<()> {
        // Check for NULL values
        for v in key_values {
            if matches!(v, Value::Null) {
                return Err(ArtIndexError::NullPrimaryKey);
            }
        }

        let pk_name = {
            let pk_indexes = self.pk_indexes.read().unwrap_or_else(|e| e.into_inner());
            pk_indexes.get(table).cloned()
        };

        if let Some(pk_name) = pk_name {
            let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
            if let Some(entry) = indexes.get(&pk_name) {
                let key = Self::encode_key(key_values);
                let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
                if tree.contains(&key) {
                    return Err(ArtIndexError::DuplicateKey(format!(
                        "Duplicate key value violates PRIMARY KEY constraint \"{}\"",
                        pk_name
                    )));
                }
            }
        }

        self.stats.constraint_checks.fetch_add(1, Ordering::Relaxed);

        Ok(())
    }

    /// Check unique constraint before INSERT/UPDATE.
    ///
    /// Also checks the PRIMARY KEY index (which is stored separately from
    /// UNIQUE indexes) so that a single call covers both constraint kinds.
    pub fn check_unique_constraints(&self, table: &str, column_values: &HashMap<String, Value>) -> ArtResult<()> {
        // Resolve names from the leaf maps first (released before tree locks).
        let pk_name = {
            let pk_indexes = self.pk_indexes.read().unwrap_or_else(|e| e.into_inner());
            pk_indexes.get(table).cloned()
        };
        let unique_names: Vec<String> = {
            let unique_indexes = self.unique_indexes.read().unwrap_or_else(|e| e.into_inner());
            unique_indexes.get(table).cloned().unwrap_or_default()
        };

        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());

        // --- Check PK index (stored separately in pk_indexes) ---
        if let Some(pk_name) = pk_name {
            if let Some(entry) = indexes.get(&pk_name) {
                let columns = &entry.columns;
                let mut has_null = false;
                let mut values = Vec::new();

                for col in columns {
                    if let Some(v) = column_values.get(col) {
                        if matches!(v, Value::Null) {
                            has_null = true;
                            break;
                        }
                        values.push(v.clone());
                    }
                }

                if !has_null && values.len() == columns.len() {
                    let key = Self::encode_key(&values);
                    let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
                    if tree.contains(&key) {
                        return Err(ArtIndexError::DuplicateKey(format!(
                            "Duplicate key value violates PRIMARY KEY constraint \"{}\"",
                            pk_name
                        )));
                    }
                }
            }
        }

        // --- Check UNIQUE indexes (one tree lock at a time) ---
        for unique_name in &unique_names {
            if let Some(entry) = indexes.get(unique_name) {
                // Extract values for this unique constraint's columns
                let columns = &entry.columns;
                let mut has_null = false;
                let mut values = Vec::new();

                for col in columns {
                    if let Some(v) = column_values.get(col) {
                        if matches!(v, Value::Null) {
                            has_null = true;
                            break;
                        }
                        values.push(v.clone());
                    }
                }

                // NULL values are allowed in UNIQUE constraints
                if has_null {
                    continue;
                }

                if values.len() == columns.len() {
                    let key = Self::encode_key(&values);
                    let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
                    if tree.contains(&key) {
                        return Err(ArtIndexError::DuplicateKey(format!(
                            "Duplicate key value violates UNIQUE constraint \"{}\"",
                            unique_name
                        )));
                    }
                }
            }
        }

        self.stats.constraint_checks.fetch_add(1, Ordering::Relaxed);

        Ok(())
    }

    /// Tuple-backed PK/UNIQUE constraint check for storage fast paths.
    ///
    /// This avoids building a column-name map for every inserted row when the
    /// values are already available in schema order.
    pub fn check_unique_constraints_tuple(&self, table: &str, schema: &Schema, tuple: &Tuple) -> ArtResult<()> {
        // Resolve names from the leaf maps first (released before tree locks).
        let pk_name = {
            let pk_indexes = self.pk_indexes.read().unwrap_or_else(|e| e.into_inner());
            pk_indexes.get(table).cloned()
        };
        let unique_names: Vec<String> = {
            let unique_indexes = self.unique_indexes.read().unwrap_or_else(|e| e.into_inner());
            unique_indexes.get(table).cloned().unwrap_or_default()
        };

        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());

        if let Some(pk_name) = pk_name {
            if let Some(entry) = indexes.get(&pk_name) {
                if let Some(values) = Self::index_value_refs_from_tuple(&entry.columns, schema, tuple) {
                    if !values.iter().any(|v| matches!(**v, Value::Null)) {
                        let key = Self::encode_key_from_values(values.iter().copied());
                        let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
                        if tree.contains(&key) {
                            return Err(ArtIndexError::DuplicateKey(format!(
                                "Duplicate key value violates PRIMARY KEY constraint \"{}\"",
                                pk_name
                            )));
                        }
                    }
                }
            }
        }

        for unique_name in &unique_names {
            if let Some(entry) = indexes.get(unique_name) {
                if let Some(values) = Self::index_value_refs_from_tuple(&entry.columns, schema, tuple) {
                    if values.iter().any(|v| matches!(**v, Value::Null)) {
                        continue;
                    }
                    let key = Self::encode_key_from_values(values.iter().copied());
                    let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
                    if tree.contains(&key) {
                        return Err(ArtIndexError::DuplicateKey(format!(
                            "Duplicate key value violates UNIQUE constraint \"{}\"",
                            unique_name
                        )));
                    }
                }
            }
        }

        self.stats.constraint_checks.fetch_add(1, Ordering::Relaxed);

        Ok(())
    }

    /// Return whether a PK/UNIQUE ART index already contains this key.
    pub fn unique_key_exists(&self, table: &str, columns: &[String], values: &[Value]) -> bool {
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        let key = Self::encode_key(values);

        // Metadata filter first; tree locks taken one at a time.
        indexes.values().any(|entry| {
            entry.table == table
                && matches!(entry.index_type, ArtIndexType::PrimaryKey | ArtIndexType::Unique)
                && entry.columns == columns
                && {
                    let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
                    tree.contains(&key)
                }
        })
    }

    /// Check foreign key constraint before INSERT/UPDATE
    ///
    /// Deadlock safety: the referenced table's PK tree lock is the ONLY tree
    /// lock this function ever holds, and it is released before the next FK
    /// is checked. Leaf name maps are snapshotted up front and released
    /// before any tree lock is taken.
    pub fn check_fk_constraints(&self, table: &str, column_values: &HashMap<String, Value>) -> ArtResult<()> {
        // Snapshot FK metadata from the leaf maps (released before tree locks).
        let fk_names: Vec<String> = {
            let fk_indexes = self.fk_indexes.read().unwrap_or_else(|e| e.into_inner());
            fk_indexes.get(table).cloned().unwrap_or_default()
        };

        if !fk_names.is_empty() {
            let fk_infos: Vec<ForeignKeyInfo> = {
                let fk_info_map = self.fk_info.read().unwrap_or_else(|e| e.into_inner());
                fk_names
                    .iter()
                    .filter_map(|name| fk_info_map.get(name).cloned())
                    .collect()
            };

            for fk_info in &fk_infos {
                // Extract values for FK columns
                let mut values = Vec::new();
                let mut has_null = false;

                for col in &fk_info.columns {
                    if let Some(v) = column_values.get(col) {
                        if matches!(v, Value::Null) {
                            has_null = true;
                            break;
                        }
                        values.push(v.clone());
                    }
                }

                // NULL values in FK columns are allowed (no reference check)
                if has_null {
                    continue;
                }

                // Check if referenced row exists in parent table's PK index
                let ref_table = &fk_info.ref_table;
                let ref_pk_name = {
                    let pk_indexes = self.pk_indexes.read().unwrap_or_else(|e| e.into_inner());
                    pk_indexes.get(ref_table).cloned()
                };

                if let Some(ref_pk_name) = ref_pk_name {
                    let contains = {
                        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
                        indexes.get(&ref_pk_name).map(|entry| {
                            let key = Self::encode_key(&values);
                            let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
                            tree.contains(&key)
                        })
                    };
                    if contains == Some(false) {
                        self.stats.violations_caught.fetch_add(1, Ordering::Relaxed);
                        return Err(ArtIndexError::ForeignKeyViolation(format!(
                            "Key ({:?}) not present in table \"{}\"",
                            values, ref_table
                        )));
                    }
                }
            }
        }

        self.stats.constraint_checks.fetch_add(1, Ordering::Relaxed);

        Ok(())
    }

    // =========================================================================
    // INDEX MAINTENANCE
    // =========================================================================

    /// Update indexes after INSERT
    ///
    /// Takes the global map lock as READ (concurrent across tables) and the
    /// per-tree WRITE locks one at a time (serializes only same-index writers).
    pub fn on_insert(&self, table: &str, row_id: RowId, column_values: &HashMap<String, Value>) -> ArtResult<()> {
        self.note_mutation();
        // W3.4 §3.2: resolve the table's own indexes from the per-table entry
        // list (leaf lock: clone the names, release) instead of scanning the
        // whole `indexes` map.
        let names = {
            let table_indexes = self.table_indexes.read().unwrap_or_else(|e| e.into_inner());
            match table_indexes.get(table) {
                Some(list) if !list.is_empty() => list.clone(),
                _ => return Ok(()),
            }
        };
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());

        for name in &names {
            let Some(entry) = indexes.get(name) else {
                continue;
            };

            // Extract values for indexed columns
            let values: Vec<Value> = entry
                .columns
                .iter()
                .filter_map(|col| column_values.get(col).cloned())
                .collect();

            if values.len() == entry.columns.len() {
                let key = Self::encode_key(&values);
                // Note: Constraint checking should have already been done
                // For non-unique indexes, we allow "duplicates" (same key, different row_id)
                let mut index = entry.tree.write().unwrap_or_else(|e| e.into_inner());
                match entry.index_type {
                    ArtIndexType::PrimaryKey | ArtIndexType::Unique => {
                        // Already checked, just insert
                        index.insert(&key, row_id)?;
                        if entry.index_type == ArtIndexType::PrimaryKey && values.len() == 1 {
                            if let Some((value, key_width)) = Self::int_value_width(&values[0]) {
                                index.record_dense_int_insert(key_width, value);
                            }
                        }
                    }
                    ArtIndexType::ForeignKey | ArtIndexType::Manual => {
                        // These allow duplicates
                        let _ = index.insert(&key, row_id);
                    }
                }
            }
        }

        Ok(())
    }

    /// Update indexes after INSERT using an already-materialized tuple.
    pub fn on_insert_tuple(&self, table: &str, row_id: RowId, schema: &Schema, tuple: &Tuple) -> ArtResult<()> {
        self.note_mutation();
        // W3.2: hoist the write-volume census fast-out (one relaxed load for the
        // whole per-index loop). Attributed to the ambient INSERT class scope.
        let wv = crate::write_volume::enabled();
        // W3.4 §3.2: resolve the table's own indexes (leaf lock, cloned+released).
        let names = {
            let table_indexes = self.table_indexes.read().unwrap_or_else(|e| e.into_inner());
            match table_indexes.get(table) {
                Some(list) if !list.is_empty() => list.clone(),
                _ => return Ok(()),
            }
        };
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        let mut frag_cache: Vec<(usize, bool, Vec<u8>)> = Vec::new();
        Self::insert_row_indexes(&indexes, &names, row_id, schema, tuple, &mut frag_cache, wv)
    }

    /// W3.4 §3.2/§3.3: batch INSERT index maintenance for the COPY funnel.
    ///
    /// Resolves the table's complete index set ONCE (one `table_indexes` read +
    /// one `indexes` read for the whole batch) instead of per row, and reuses a
    /// single encode-once fragment cache across the batch's rows. Tree write
    /// locks are still taken one row at a time (rule 3); ART maintenance runs
    /// post-commit, so single-WriteBatch durability is untouched. A per-row
    /// maintenance error is logged and skipped (the durable row is already
    /// committed), matching the pre-batch per-row call site.
    pub fn on_insert_tuples(&self, table: &str, schema: &Schema, rows: &[(RowId, Tuple)]) -> ArtResult<()> {
        if rows.is_empty() {
            return Ok(());
        }
        self.note_mutation();
        let wv = crate::write_volume::enabled();
        let names = {
            let table_indexes = self.table_indexes.read().unwrap_or_else(|e| e.into_inner());
            match table_indexes.get(table) {
                Some(list) if !list.is_empty() => list.clone(),
                _ => return Ok(()),
            }
        };
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        // One row-scoped fragment cache, cleared per row (fragments are
        // row-specific but the buffer capacity is reused across the batch).
        let mut frag_cache: Vec<(usize, bool, Vec<u8>)> = Vec::new();
        for (row_id, tuple) in rows {
            frag_cache.clear();
            if let Err(e) = Self::insert_row_indexes(&indexes, &names, *row_id, schema, tuple, &mut frag_cache, wv) {
                tracing::debug!("ART index batch insert for table '{}': {}", table, e);
            }
        }
        Ok(())
    }

    /// Update indexes after INSERT using an already-materialized tuple and
    /// return only the indexed column values needed to undo the insert.
    pub fn on_insert_tuple_collect_index_values(
        &self,
        table: &str,
        row_id: RowId,
        schema: &Schema,
        tuple: &Tuple,
    ) -> ArtResult<HashMap<String, Value>> {
        self.note_mutation();
        // W3.2: census fast-out for the buffered/txn insert index path.
        let wv = crate::write_volume::enabled();
        // W3.4 §3.2: resolve the table's own indexes (leaf lock, cloned+released).
        let names = {
            let table_indexes = self.table_indexes.read().unwrap_or_else(|e| e.into_inner());
            match table_indexes.get(table) {
                Some(list) if !list.is_empty() => list.clone(),
                _ => return Ok(HashMap::new()),
            }
        };
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        let multi_index = names.len() > 1;
        let mut frag_cache: Vec<(usize, bool, Vec<u8>)> = Vec::new();
        let mut indexed_values = HashMap::new();

        for name in &names {
            let Some(entry) = indexes.get(name) else {
                continue;
            };

            let mut resolved: Vec<(usize, &Value)> = Vec::with_capacity(entry.columns.len());
            let mut missing = false;
            for column in &entry.columns {
                let Some(idx) = schema.get_column_index(column) else {
                    missing = true;
                    break;
                };
                let Some(value) = tuple.values.get(idx) else {
                    missing = true;
                    break;
                };
                // Record the value even for an index that later bails on a
                // missing column — the undo log needs every value we resolved
                // (byte-for-byte the pre-change accumulation order).
                indexed_values.entry(column.clone()).or_insert_with(|| value.clone());
                resolved.push((idx, value));
            }

            if !missing && resolved.len() == entry.columns.len() {
                let key = if multi_index {
                    Self::encode_key_cached(&mut frag_cache, &resolved)
                } else {
                    Self::encode_key_from_values(resolved.iter().map(|(_, v)| *v))
                };
                // W3.2: one ART entry = encoded key + the u64 row-id payload.
                if wv {
                    crate::write_volume::add(crate::write_volume::Category::IndexKey, (key.len() + 8) as u64);
                }
                let mut index = entry.tree.write().unwrap_or_else(|e| e.into_inner());
                match entry.index_type {
                    ArtIndexType::PrimaryKey | ArtIndexType::Unique => {
                        index.insert(&key, row_id)?;
                        if entry.index_type == ArtIndexType::PrimaryKey && entry.columns.len() == 1 {
                            if let Some((value, key_width)) = Self::int_value_width(resolved[0].1) {
                                index.record_dense_int_insert(key_width, value);
                            }
                        }
                    }
                    ArtIndexType::ForeignKey | ArtIndexType::Manual => {
                        let _ = index.insert(&key, row_id);
                    }
                }
            }
        }

        Ok(indexed_values)
    }

    /// Update indexes after DELETE
    ///
    /// Global map lock as READ + per-tree WRITE locks one at a time.
    pub fn on_delete(&self, table: &str, row_id: RowId, column_values: &HashMap<String, Value>) -> ArtResult<()> {
        self.note_mutation();
        // W3.4 §3.2: resolve the table's own indexes (leaf lock, cloned+released).
        let names = {
            let table_indexes = self.table_indexes.read().unwrap_or_else(|e| e.into_inner());
            match table_indexes.get(table) {
                Some(list) if !list.is_empty() => list.clone(),
                _ => return Ok(()),
            }
        };
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());

        for name in &names {
            let Some(entry) = indexes.get(name) else {
                continue;
            };

            let values: Vec<Value> = entry
                .columns
                .iter()
                .filter_map(|col| column_values.get(col).cloned())
                .collect();

            if values.len() == entry.columns.len() {
                let key = Self::encode_key(&values);
                let mut index = entry.tree.write().unwrap_or_else(|e| e.into_inner());
                match entry.index_type {
                    ArtIndexType::PrimaryKey | ArtIndexType::Unique => {
                        // Unique indexes: remove entire key entry
                        let removed = index.remove(&key)?.is_some();
                        if removed && entry.index_type == ArtIndexType::PrimaryKey && values.len() == 1 {
                            if let Some((value, _)) = Self::int_value_width(&values[0]) {
                                index.record_dense_int_delete(value);
                            }
                        }
                    }
                    ArtIndexType::ForeignKey | ArtIndexType::Manual => {
                        // Non-unique indexes: remove only the specific row_id
                        let _ = index.remove_value(&key, row_id);
                    }
                }
            }
        }

        Ok(())
    }

    /// Update indexes after DELETE using the already-materialized tuple.
    pub fn on_delete_tuple(&self, table: &str, row_id: RowId, schema: &Schema, tuple: &Tuple) -> ArtResult<()> {
        self.note_mutation();
        // W3.4 §3.2: resolve the table's own indexes (leaf lock, cloned+released).
        let names = {
            let table_indexes = self.table_indexes.read().unwrap_or_else(|e| e.into_inner());
            match table_indexes.get(table) {
                Some(list) if !list.is_empty() => list.clone(),
                _ => return Ok(()),
            }
        };
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        let multi_index = names.len() > 1;
        let mut frag_cache: Vec<(usize, bool, Vec<u8>)> = Vec::new();

        for name in &names {
            let Some(entry) = indexes.get(name) else {
                continue;
            };

            let mut resolved: Vec<(usize, &Value)> = Vec::with_capacity(entry.columns.len());
            let mut missing = false;
            for column in &entry.columns {
                let Some(sidx) = schema.get_column_index(column) else {
                    missing = true;
                    break;
                };
                let Some(v) = tuple.values.get(sidx) else {
                    missing = true;
                    break;
                };
                resolved.push((sidx, v));
            }
            if missing {
                continue;
            }

            let key = if multi_index {
                Self::encode_key_cached(&mut frag_cache, &resolved)
            } else {
                Self::encode_key_from_values(resolved.iter().map(|(_, v)| *v))
            };
            let mut index = entry.tree.write().unwrap_or_else(|e| e.into_inner());
            match entry.index_type {
                ArtIndexType::PrimaryKey | ArtIndexType::Unique => {
                    let removed = index.remove(&key)?.is_some();
                    if removed && entry.index_type == ArtIndexType::PrimaryKey && entry.columns.len() == 1 {
                        if let Some((value, _)) = Self::int_value_width(resolved[0].1) {
                            index.record_dense_int_delete(value);
                        }
                    }
                }
                ArtIndexType::ForeignKey | ArtIndexType::Manual => {
                    let _ = index.remove_value(&key, row_id);
                }
            }
        }

        Ok(())
    }

    /// Remove one single-column primary-key entry when the caller already has
    /// the encoded PK key. This avoids fetching/deserializing the old row for
    /// PK-only DELETE fast paths.
    pub fn remove_single_pk_key(&self, table: &str, key: &[u8], row_id: RowId, pk_value: &Value) -> ArtResult<bool> {
        self.note_mutation();
        let pk_name = {
            let pk_indexes = self.pk_indexes.read().unwrap_or_else(|e| e.into_inner());
            match pk_indexes.get(table) {
                Some(name) => name.clone(),
                None => return Ok(false),
            }
        };

        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        let Some(entry) = indexes.get(&pk_name) else {
            return Ok(false);
        };
        if !matches!(entry.index_type, ArtIndexType::PrimaryKey) || entry.columns.len() != 1 {
            return Ok(false);
        }

        let mut index = entry.tree.write().unwrap_or_else(|e| e.into_inner());
        if index.get(key) != Some(row_id) {
            return Ok(false);
        }

        let removed = index.remove(key)?.is_some();
        if removed {
            if let Some((value, _)) = Self::int_value_width(pk_value) {
                index.record_dense_int_delete(value);
            }
        }
        Ok(removed)
    }

    /// Update indexes after UPDATE
    pub fn on_update(
        &self,
        table: &str,
        row_id: RowId,
        old_values: &HashMap<String, Value>,
        new_values: &HashMap<String, Value>,
    ) -> ArtResult<()> {
        // Remove old index entries and add new ones
        self.on_delete(table, row_id, old_values)?;
        self.on_insert(table, row_id, new_values)?;
        Ok(())
    }

    /// Return true if a tuple update changes any column covered by an ART index.
    ///
    /// Most OLTP updates mutate payload columns while the PK/unique/manual index
    /// columns stay unchanged. In that case callers can skip the expensive
    /// delete+insert index maintenance path entirely.
    pub fn tuple_update_affects_indexes(
        &self,
        table: &str,
        schema: &Schema,
        old_tuple: &Tuple,
        new_tuple: &Tuple,
    ) -> bool {
        // Metadata-only check: no tree locks needed.
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());

        for entry in indexes.values() {
            if entry.table != table {
                continue;
            }

            for column_name in &entry.columns {
                let Some(idx) = schema.get_column_index(column_name) else {
                    return true;
                };
                if old_tuple.values.get(idx) != new_tuple.values.get(idx) {
                    return true;
                }
            }
        }

        false
    }

    /// Return true when any provided column participates in an ART index for
    /// this table. Fast UPDATE paths use this statement-level hint to avoid a
    /// per-row old/new tuple comparison for payload-only updates.
    pub fn columns_affect_indexes(&self, table: &str, column_names: &[String]) -> bool {
        if column_names.is_empty() {
            return false;
        }

        // Metadata-only check: no tree locks needed.
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        for entry in indexes.values() {
            if entry.table != table {
                continue;
            }

            for indexed_column in &entry.columns {
                if column_names
                    .iter()
                    .any(|name| name.eq_ignore_ascii_case(indexed_column))
                {
                    return true;
                }
            }
        }

        false
    }

    /// Clear all index data for a table without removing the index structures.
    ///
    /// This is used by TRUNCATE TABLE to reset index contents while keeping
    /// the PK/FK/UNIQUE/Manual index registrations intact. After clearing,
    /// the indexes are empty but still exist, so new inserts will correctly
    /// populate them.
    ///
    /// The registry itself is unchanged, so the global lock is taken as READ;
    /// each table tree is write-locked one at a time.
    pub fn clear_table_indexes(&self, table: &str) {
        self.note_mutation();
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        for entry in indexes.values() {
            if entry.table == table {
                let mut tree = entry.tree.write().unwrap_or_else(|e| e.into_inner());
                tree.clear();
            }
        }
    }

    fn int_value_width(value: &Value) -> Option<(i64, usize)> {
        match value {
            Value::Int2(v) => Some((i64::from(*v), 2)),
            Value::Int4(v) => Some((i64::from(*v), 4)),
            Value::Int8(v) => Some((*v, 8)),
            _ => None,
        }
    }

    // =========================================================================
    // STATISTICS
    // =========================================================================

    /// Get manager statistics
    pub fn stats(&self) -> ArtManagerStats {
        self.stats.snapshot()
    }

    /// Get statistics for a specific index
    pub fn index_stats(&self, name: &str) -> Option<ArtIndexStats> {
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        indexes.get(name).map(|entry| {
            let tree = entry.tree.read().unwrap_or_else(|e| e.into_inner());
            tree.stats().clone()
        })
    }

    /// Check if a table has foreign key indexes
    pub fn has_fk(&self, table: &str) -> bool {
        let fk_indexes = self.fk_indexes.read().unwrap_or_else(|e| e.into_inner());
        fk_indexes.get(table).is_some_and(|v| !v.is_empty())
    }

    /// Check if a table has a primary key
    pub fn has_pk(&self, table: &str) -> bool {
        let pk_indexes = self.pk_indexes.read().unwrap_or_else(|e| e.into_inner());
        pk_indexes.contains_key(table)
    }

    /// Check if a specific index exists
    pub fn index_exists(&self, name: &str) -> bool {
        let indexes = self.indexes.read().unwrap_or_else(|e| e.into_inner());
        indexes.contains_key(name)
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn test_create_pk_index() {
        let manager = ArtIndexManager::new();

        let result = manager.create_pk_index("users", &["id".to_string()]);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "users_pkey");

        // Duplicate should fail
        let result = manager.create_pk_index("users", &["id".to_string()]);
        assert!(result.is_err());
    }

    #[test]
    fn test_create_unique_index() {
        let manager = ArtIndexManager::new();

        let result = manager.create_unique_index("users", &["email".to_string()], None);
        assert!(result.is_ok());

        let result = manager.create_unique_index("users", &["username".to_string()], Some("users_username_unique"));
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "users_username_unique");
    }

    #[test]
    fn test_create_fk_index() {
        let manager = ArtIndexManager::new();

        // Create parent table PK
        manager.create_pk_index("departments", &["id".to_string()]).unwrap();

        // Create FK
        let result = manager.create_fk_index(
            "employees",
            &["dept_id".to_string()],
            "departments",
            &["id".to_string()],
            None,
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_pk_constraint_check() {
        let manager = ArtIndexManager::new();
        manager.create_pk_index("users", &["id".to_string()]).unwrap();

        // Insert first row
        let mut values = HashMap::new();
        values.insert("id".to_string(), Value::Int8(1));
        manager.check_pk_constraint("users", &[Value::Int8(1)]).unwrap();
        manager.on_insert("users", 1, &values).unwrap();

        // Duplicate should fail
        let result = manager.check_pk_constraint("users", &[Value::Int8(1)]);
        assert!(matches!(result, Err(ArtIndexError::DuplicateKey(_))));

        // Different key should succeed
        let result = manager.check_pk_constraint("users", &[Value::Int8(2)]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_pk_int_range_count_handles_negative_keys() {
        let manager = ArtIndexManager::new();
        manager.create_pk_index("events", &["id".to_string()]).unwrap();

        for (row_id, id) in [(-3_i64), -1, 0, 1, 4].into_iter().enumerate() {
            let mut values = HashMap::new();
            values.insert("id".to_string(), Value::Int8(id));
            manager.on_insert("events", row_id as u64 + 1, &values).unwrap();
        }

        assert_eq!(
            manager.pk_index_count_int_range("events", &DataType::Int8, Some((0, true)), None),
            Some(3)
        );
        assert_eq!(
            manager.pk_index_count_int_range("events", &DataType::Int8, None, Some((0, false))),
            Some(2)
        );
        assert_eq!(
            manager.pk_index_count_int_range("events", &DataType::Int8, Some((-1, true)), Some((1, true))),
            Some(3)
        );
    }

    #[test]
    fn test_dense_pk_int_range_count_stays_exact_after_edge_deletes() {
        let manager = ArtIndexManager::new();
        manager.create_pk_index("events", &["id".to_string()]).unwrap();

        for id in 0_i32..10 {
            let mut values = HashMap::new();
            values.insert("id".to_string(), Value::Int4(id));
            manager.on_insert("events", id as u64 + 1, &values).unwrap();
        }

        assert_eq!(
            manager.pk_index_count_int_range("events", &DataType::Int4, Some((3, true)), None),
            Some(7)
        );
        assert_eq!(
            manager.pk_index_count_int_range("events", &DataType::Int4, Some((2, true)), Some((5, true))),
            Some(4)
        );

        for id in [0_i32, 9] {
            let mut values = HashMap::new();
            values.insert("id".to_string(), Value::Int4(id));
            manager.on_delete("events", id as u64 + 1, &values).unwrap();
        }

        assert_eq!(
            manager.pk_index_count_int_range("events", &DataType::Int4, None, None),
            Some(8)
        );
        assert_eq!(
            manager.pk_index_count_int_range("events", &DataType::Int4, Some((1, true)), Some((8, true))),
            Some(8)
        );
    }

    #[test]
    fn test_dense_pk_int_range_count_falls_back_after_gap_delete() {
        let manager = ArtIndexManager::new();
        manager.create_pk_index("events", &["id".to_string()]).unwrap();

        for id in 0_i32..10 {
            let mut values = HashMap::new();
            values.insert("id".to_string(), Value::Int4(id));
            manager.on_insert("events", id as u64 + 1, &values).unwrap();
        }

        let mut values = HashMap::new();
        values.insert("id".to_string(), Value::Int4(5));
        manager.on_delete("events", 6, &values).unwrap();

        assert_eq!(
            manager.pk_index_count_int_range("events", &DataType::Int4, None, None),
            Some(9)
        );
        assert_eq!(
            manager.pk_index_count_int_range("events", &DataType::Int4, Some((4, true)), Some((6, true))),
            Some(2)
        );
    }

    #[test]
    fn test_unique_constraint_check() {
        let manager = ArtIndexManager::new();
        manager
            .create_unique_index("users", &["email".to_string()], None)
            .unwrap();

        // Insert first row
        let mut values = HashMap::new();
        values.insert("email".to_string(), Value::String("alice@example.com".to_string()));
        manager.check_unique_constraints("users", &values).unwrap();
        manager.on_insert("users", 1, &values).unwrap();

        // Duplicate should fail
        let result = manager.check_unique_constraints("users", &values);
        assert!(matches!(result, Err(ArtIndexError::DuplicateKey(_))));

        // NULL should be allowed
        let mut null_values = HashMap::new();
        null_values.insert("email".to_string(), Value::Null);
        let result = manager.check_unique_constraints("users", &null_values);
        assert!(result.is_ok());
    }

    #[test]
    fn test_tuple_update_affects_indexes_only_for_index_columns() {
        use crate::Column;

        let manager = ArtIndexManager::new();
        manager.create_pk_index("users", &["id".to_string()]).unwrap();
        manager
            .create_unique_index("users", &["email".to_string()], None)
            .unwrap();

        let schema = Schema::new(vec![
            Column::new("id", DataType::Int4).primary_key(),
            Column::new("email", DataType::Text).unique(),
            Column::new("balance", DataType::Int4),
        ]);

        let old_tuple = Tuple::new(vec![
            Value::Int4(1),
            Value::String("a@example.com".to_string()),
            Value::Int4(10),
        ]);
        let payload_update = Tuple::new(vec![
            Value::Int4(1),
            Value::String("a@example.com".to_string()),
            Value::Int4(11),
        ]);
        let unique_update = Tuple::new(vec![
            Value::Int4(1),
            Value::String("b@example.com".to_string()),
            Value::Int4(10),
        ]);
        let pk_update = Tuple::new(vec![
            Value::Int4(2),
            Value::String("a@example.com".to_string()),
            Value::Int4(10),
        ]);

        assert!(!manager.tuple_update_affects_indexes("users", &schema, &old_tuple, &payload_update));
        assert!(manager.tuple_update_affects_indexes("users", &schema, &old_tuple, &unique_update));
        assert!(manager.tuple_update_affects_indexes("users", &schema, &old_tuple, &pk_update));

        assert!(!manager.columns_affect_indexes("users", &["balance".to_string()]));
        assert!(manager.columns_affect_indexes("users", &["email".to_string()]));
        assert!(manager.columns_affect_indexes("users", &["ID".to_string()]));
    }

    #[test]
    fn test_tuple_backed_insert_constraints_and_index_update() {
        use crate::Column;

        let manager = ArtIndexManager::new();
        manager.create_pk_index("users", &["id".to_string()]).unwrap();
        manager
            .create_unique_index("users", &["email".to_string()], None)
            .unwrap();

        let schema = Schema::new(vec![
            Column::new("id", DataType::Int4).primary_key(),
            Column::new("email", DataType::Text).unique(),
            Column::new("balance", DataType::Int4),
        ]);
        let tuple = Tuple::new(vec![
            Value::Int4(1),
            Value::String("a@example.com".to_string()),
            Value::Int4(10),
        ]);

        manager
            .check_unique_constraints_tuple("users", &schema, &tuple)
            .unwrap();
        let indexed_values = manager
            .on_insert_tuple_collect_index_values("users", 1, &schema, &tuple)
            .unwrap();
        assert_eq!(indexed_values.len(), 2);
        assert_eq!(indexed_values.get("id"), Some(&Value::Int4(1)));
        assert_eq!(
            indexed_values.get("email"),
            Some(&Value::String("a@example.com".to_string()))
        );
        assert!(!indexed_values.contains_key("balance"));

        let dup_pk = Tuple::new(vec![
            Value::Int4(1),
            Value::String("b@example.com".to_string()),
            Value::Int4(20),
        ]);
        assert!(matches!(
            manager.check_unique_constraints_tuple("users", &schema, &dup_pk),
            Err(ArtIndexError::DuplicateKey(_))
        ));

        let dup_unique = Tuple::new(vec![
            Value::Int4(2),
            Value::String("a@example.com".to_string()),
            Value::Int4(20),
        ]);
        assert!(matches!(
            manager.check_unique_constraints_tuple("users", &schema, &dup_unique),
            Err(ArtIndexError::DuplicateKey(_))
        ));

        let null_unique = Tuple::new(vec![Value::Int4(2), Value::Null, Value::Int4(20)]);
        assert!(manager
            .check_unique_constraints_tuple("users", &schema, &null_unique)
            .is_ok());

        manager.on_delete("users", 1, &indexed_values).unwrap();
        assert!(manager
            .check_unique_constraints_tuple("users", &schema, &dup_pk)
            .is_ok());
    }

    #[test]
    fn test_drop_table_indexes() {
        let manager = ArtIndexManager::new();

        manager.create_pk_index("users", &["id".to_string()]).unwrap();
        manager
            .create_unique_index("users", &["email".to_string()], None)
            .unwrap();

        assert_eq!(manager.stats().total_indexes, 2);

        manager.drop_table_indexes("users").unwrap();

        assert_eq!(manager.stats().total_indexes, 0);
    }

    #[test]
    fn test_list_indexes() {
        let manager = ArtIndexManager::new();

        manager.create_pk_index("users", &["id".to_string()]).unwrap();
        manager
            .create_unique_index("users", &["email".to_string()], None)
            .unwrap();
        manager
            .create_manual_index("users_name_idx", "users", &["name".to_string()])
            .unwrap();

        let indexes = manager.list_indexes();
        assert_eq!(indexes.len(), 3);

        let table_indexes = manager.list_table_indexes("users");
        assert_eq!(table_indexes.len(), 3);
    }

    // W3.4 §3.2: the per-table entry list must stay byte-for-byte identical to
    // a full `indexes`-map filter after every register / drop / rename / clear.
    #[test]
    fn table_indexes_stays_consistent_across_register_drop_rename_clear() {
        // Expected = group the source-of-truth `indexes` map by table.
        fn expected(m: &ArtIndexManager) -> HashMap<String, Vec<String>> {
            let indexes = m.indexes.read().unwrap();
            let mut exp: HashMap<String, Vec<String>> = HashMap::new();
            for (name, entry) in indexes.iter() {
                exp.entry(entry.table.clone()).or_default().push(name.clone());
            }
            for v in exp.values_mut() {
                v.sort();
            }
            exp
        }
        // Actual = the derived `table_indexes` map.
        fn actual(m: &ArtIndexManager) -> HashMap<String, Vec<String>> {
            let ti = m.table_indexes.read().unwrap();
            let mut act: HashMap<String, Vec<String>> = ti.clone();
            for v in act.values_mut() {
                v.sort();
            }
            act
        }

        let m = ArtIndexManager::new();
        assert_eq!(expected(&m), actual(&m));

        // Register all four index kinds, incl. Manual (which the partial
        // pk/fk/unique maps do NOT cover — the whole reason for this map).
        m.create_pk_index("orders", &["id".to_string()]).unwrap();
        m.create_unique_index("orders", &["code".to_string()], None).unwrap();
        m.create_pk_index("cust", &["id".to_string()]).unwrap();
        m.create_fk_index("orders", &["cust_id".to_string()], "cust", &["id".to_string()], None)
            .unwrap();
        m.create_manual_index("orders_total_idx", "orders", &["total".to_string()])
            .unwrap();
        assert_eq!(expected(&m), actual(&m));
        assert!(actual(&m)["orders"].contains(&"orders_total_idx".to_string()));

        // Drop a single (Manual) index.
        m.drop_index("orders_total_idx").unwrap();
        assert_eq!(expected(&m), actual(&m));

        // TRUNCATE (clear): registrations survive → table_indexes UNCHANGED.
        let before_clear = actual(&m);
        m.clear_table_indexes("orders");
        assert_eq!(before_clear, actual(&m));
        assert_eq!(expected(&m), actual(&m));

        // Rename: the whole entry list moves to the new table key.
        m.rename_table_indexes("orders", "orders2").unwrap();
        assert_eq!(expected(&m), actual(&m));
        assert!(!actual(&m).contains_key("orders"));
        assert!(actual(&m).contains_key("orders2"));

        // Drop every index for a table → its key disappears entirely.
        m.drop_table_indexes("orders2").unwrap();
        assert_eq!(expected(&m), actual(&m));
        assert!(!actual(&m).contains_key("orders2"));
        // The untouched parent table's entry is still intact.
        assert_eq!(actual(&m).get("cust"), Some(&vec!["cust_pkey".to_string()]));
    }

    // W3.4 §3.3: the encode-once fragment path must produce BYTE-IDENTICAL keys
    // to `encode_key_from_values` for every DataType, single- and multi-column
    // (ART keys are on-disk-durable via snapshots — a divergence corrupts them).
    #[test]
    fn encode_once_is_byte_identical_to_encode_key_from_values() {
        let samples: Vec<Value> = vec![
            Value::Null,
            Value::Boolean(true),
            Value::Boolean(false),
            Value::Int2(-7),
            Value::Int2(12345),
            Value::Int4(0),
            Value::Int4(i32::MIN),
            Value::Int8(9_000_000_000),
            Value::Float4(-1.5),
            Value::Float4(0.0),
            Value::Float8(3.25),
            Value::String("hello".to_string()),
            Value::String(String::new()),
            // embedded 0x00: single-col (unescaped) vs multi-col (escaped) differ
            Value::String("has\u{0}nul\u{0}bytes".to_string()),
            Value::Bytes(vec![1, 0, 2, 0xFF, 0]),
            Value::Bytes(Vec::new()),
            Value::Numeric("123.45".to_string()),
            Value::Json("{\"k\":1}".to_string()),
            Value::Array(vec![Value::Int4(1), Value::Int4(2)]),
            Value::Array(vec![Value::String("a\u{0}b".to_string())]),
        ];

        // Single-column keys (escape = false).
        for v in &samples {
            let direct = ArtIndexManager::encode_key_from_values(std::iter::once(v));
            let mut cache: Vec<(usize, bool, Vec<u8>)> = Vec::new();
            let cached = ArtIndexManager::encode_key_cached(&mut cache, &[(0usize, v)]);
            assert_eq!(direct, cached, "single-column key mismatch for {:?}", v);
        }

        // Two-column keys (escape = true) — every ordered pair.
        for a in &samples {
            for b in &samples {
                let direct = ArtIndexManager::encode_key_from_values([a, b].iter().copied());
                let mut cache: Vec<(usize, bool, Vec<u8>)> = Vec::new();
                let cached = ArtIndexManager::encode_key_cached(&mut cache, &[(0usize, a), (1usize, b)]);
                assert_eq!(direct, cached, "two-column key mismatch for {:?} + {:?}", a, b);
            }
        }

        // Three columns reusing the SAME column index (shared-fragment reuse
        // must not corrupt later columns).
        for a in &samples {
            let direct = ArtIndexManager::encode_key_from_values([a, a, a].iter().copied());
            let mut cache: Vec<(usize, bool, Vec<u8>)> = Vec::new();
            let cached = ArtIndexManager::encode_key_cached(&mut cache, &[(0usize, a), (0usize, a), (0usize, a)]);
            assert_eq!(direct, cached, "three-column repeated key mismatch for {:?}", a);
        }

        // One column shared by a single-column (escape=false) and a two-column
        // (escape=true) index within one row must cache TWO distinct fragments
        // and stay byte-identical to each direct form.
        let shared = Value::String("x\u{0}y".to_string());
        let other = Value::Int4(7);
        let mut cache: Vec<(usize, bool, Vec<u8>)> = Vec::new();
        let single = ArtIndexManager::encode_key_cached(&mut cache, &[(0usize, &shared)]);
        let composite = ArtIndexManager::encode_key_cached(&mut cache, &[(0usize, &shared), (1usize, &other)]);
        assert_eq!(
            single,
            ArtIndexManager::encode_key_from_values(std::iter::once(&shared))
        );
        assert_eq!(
            composite,
            ArtIndexManager::encode_key_from_values([&shared, &other].iter().copied())
        );
        assert_eq!(
            cache.iter().filter(|(ci, _, _)| *ci == 0).count(),
            2,
            "shared column must cache one unescaped and one escaped fragment"
        );
    }
}