heliosdb-nano 3.23.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
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
//! PostgreSQL-compatible System Views (pg_catalog)
//!
//! Comprehensive implementation of PostgreSQL system catalog views for HeliosDB-Lite.
//! Ensures all v2.0 and v2.1 features are introspectable via SQL queries.
//!
//! Implemented views:
//! - Core catalog: pg_tables, pg_views, pg_indexes, pg_columns, pg_attribute
//! - Database info: pg_database, pg_namespace, pg_class, pg_type
//! - Session/Activity: pg_stat_activity, pg_stat_database, pg_settings
//! - v2.0 Features: pg_branches, pg_materialized_views, pg_snapshots
//! - v2.1 Features: pg_stat_ssl, pg_authid, pg_stat_optimizer, pg_compression_stats

#![allow(unused_variables)]

use crate::{Result, Error, Schema, Column, DataType, Value, Tuple};
use crate::storage::StorageEngine;
use crate::storage::GlobalStatsCollector;
use crate::sql::SessionRegistry;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use chrono::{DateTime, Utc};
use lru::LruCache;
use std::time::{Duration, Instant};
use std::num::NonZeroUsize;

/// System view definition
#[derive(Debug, Clone)]
pub struct SystemView {
    pub name: String,
    pub schema: Schema,
    pub description: String,
    pub category: ViewCategory,
}

/// System view category
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ViewCategory {
    Core,
    Session,
    Feature,
    Statistics,
}

/// Cached query result with TTL
#[derive(Debug, Clone)]
struct CachedResult {
    /// Cached tuples
    tuples: Vec<Tuple>,
    /// Timestamp when cached
    cached_at: Instant,
    /// Time-to-live duration
    ttl: Duration,
}

impl CachedResult {
    /// Create a new cached result
    fn new(tuples: Vec<Tuple>, ttl: Duration) -> Self {
        Self {
            tuples,
            cached_at: Instant::now(),
            ttl,
        }
    }

    /// Check if the cached result is still valid
    fn is_valid(&self) -> bool {
        self.cached_at.elapsed() < self.ttl
    }
}

/// Cache key for system view queries
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct CacheKey {
    /// View name
    view_name: String,
    /// Optional query hash for parameterized queries (future use)
    query_hash: u64,
}

impl CacheKey {
    /// Create a cache key for a simple view query
    fn new(view_name: &str) -> Self {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        view_name.hash(&mut hasher);

        Self {
            view_name: view_name.to_string(),
            query_hash: hasher.finish(),
        }
    }
}

/// Central registry for all PostgreSQL-compatible system views
pub struct SystemViewRegistry {
    views: HashMap<String, SystemView>,
    session_registry: Option<Arc<SessionRegistry>>,
    /// Global statistics collector for query history and transaction stats
    stats_collector: Option<Arc<GlobalStatsCollector>>,
    /// LRU cache for system view results (view_name -> cached result)
    /// Key: (view_name, query_hash), Value: cached tuples with TTL
    cache: Arc<Mutex<LruCache<CacheKey, CachedResult>>>,
    /// Default TTL for system view cache (5 seconds as per spec)
    default_ttl: Duration,
}

impl SystemViewRegistry {
    /// Create a new system view registry
    pub fn new() -> Self {
        // SAFETY: 500 is a compile-time constant and is guaranteed to be non-zero
        // Performance optimization: Increased from 100 to 500 entries for better cache hit rates
        // in larger deployments with many concurrent queries to system views
        #[allow(clippy::expect_used)]
        let cache_size = NonZeroUsize::new(500).expect("500 is non-zero");
        let mut registry = Self {
            views: HashMap::new(),
            session_registry: None,
            stats_collector: None,
            cache: Arc::new(Mutex::new(LruCache::new(cache_size))),
            default_ttl: Duration::from_secs(5),
        };

        registry.register_all_views();
        registry
    }

    /// Create with session registry for activity tracking
    pub fn with_session_registry(session_registry: Arc<SessionRegistry>) -> Self {
        let mut registry = Self::new();
        registry.session_registry = Some(session_registry);
        registry
    }

    /// Create with statistics collector for query history and transaction stats
    pub fn with_stats_collector(stats_collector: Arc<GlobalStatsCollector>) -> Self {
        let mut registry = Self::new();
        registry.stats_collector = Some(stats_collector);
        registry
    }

    /// Set the statistics collector
    pub fn set_stats_collector(&mut self, stats_collector: Arc<GlobalStatsCollector>) {
        self.stats_collector = Some(stats_collector);
    }

    /// Create with custom cache configuration
    ///
    /// # Errors
    ///
    /// Returns an error if cache_size is zero.
    pub fn with_cache_config(cache_size: usize, ttl_seconds: u64) -> Result<Self> {
        let cache_size_nz = NonZeroUsize::new(cache_size)
            .ok_or_else(|| Error::config("Cache size must be non-zero"))?;
        let mut registry = Self {
            views: HashMap::new(),
            session_registry: None,
            stats_collector: None,
            cache: Arc::new(Mutex::new(LruCache::new(cache_size_nz))),
            default_ttl: Duration::from_secs(ttl_seconds),
        };

        registry.register_all_views();
        Ok(registry)
    }

    /// Register all system views
    fn register_all_views(&mut self) {
        self.register_core_catalog_views();
        self.register_session_activity_views();
        self.register_v2_feature_views();
        self.register_v2_1_feature_views();
        self.register_v2_3_monitoring_views();
        self.register_ha_views();
    }

    /// Register core PostgreSQL catalog views
    fn register_core_catalog_views(&mut self) {
        // pg_tables - All user tables
        self.register(SystemView {
            name: "pg_tables".to_string(),
            category: ViewCategory::Core,
            description: "Lists all user tables in the database".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("schemaname", DataType::Text),
                    Column::new("tablename", DataType::Text),
                    Column::new("tableowner", DataType::Text),
                    Column::new("tablespace", DataType::Text),
                    Column::new("hasindexes", DataType::Boolean),
                    Column::new("hasrules", DataType::Boolean),
                    Column::new("hastriggers", DataType::Boolean),
                    Column::new("rowsecurity", DataType::Boolean),
                ],
            },
        });

        // pg_views - All views
        self.register(SystemView {
            name: "pg_views".to_string(),
            category: ViewCategory::Core,
            description: "Lists all views in the database".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("schemaname", DataType::Text),
                    Column::new("viewname", DataType::Text),
                    Column::new("viewowner", DataType::Text),
                    Column::new("definition", DataType::Text),
                ],
            },
        });

        // pg_indexes - All indexes
        self.register(SystemView {
            name: "pg_indexes".to_string(),
            category: ViewCategory::Core,
            description: "Lists all indexes in the database".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("schemaname", DataType::Text),
                    Column::new("tablename", DataType::Text),
                    Column::new("indexname", DataType::Text),
                    Column::new("tablespace", DataType::Text),
                    Column::new("indexdef", DataType::Text),
                ],
            },
        });

        // pg_attribute / pg_columns - All columns
        self.register(SystemView {
            name: "pg_attribute".to_string(),
            category: ViewCategory::Core,
            description: "Lists all table columns with detailed attributes".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("attrelid", DataType::Int4),
                    Column::new("attname", DataType::Text),
                    Column::new("atttypid", DataType::Int4),
                    Column::new("attnum", DataType::Int2),
                    Column::new("attlen", DataType::Int2),
                    Column::new("attnotnull", DataType::Boolean),
                    Column::new("atthasdef", DataType::Boolean),
                ],
            },
        });

        // pg_database - Database information
        self.register(SystemView {
            name: "pg_database".to_string(),
            category: ViewCategory::Core,
            description: "Lists database information".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("datname", DataType::Text),
                    Column::new("datdba", DataType::Int4),
                    Column::new("encoding", DataType::Int4),
                    Column::new("datcollate", DataType::Text),
                    Column::new("datctype", DataType::Text),
                    Column::new("datistemplate", DataType::Boolean),
                    Column::new("datallowconn", DataType::Boolean),
                ],
            },
        });

        // pg_namespace - Schema information
        self.register(SystemView {
            name: "pg_namespace".to_string(),
            category: ViewCategory::Core,
            description: "Lists database schemas/namespaces".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("nspname", DataType::Text),
                    Column::new("nspowner", DataType::Int4),
                ],
            },
        });

        // pg_class - Tables, indexes, views, etc.
        self.register(SystemView {
            name: "pg_class".to_string(),
            category: ViewCategory::Core,
            description: "Lists all relations (tables, indexes, views)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("relname", DataType::Text),
                    Column::new("relnamespace", DataType::Int4),
                    Column::new("relkind", DataType::Char(1)),
                    Column::new("relowner", DataType::Int4),
                    Column::new("relam", DataType::Int4),
                    Column::new("relpages", DataType::Int4),
                    Column::new("reltuples", DataType::Float4),
                ],
            },
        });

        // pg_type - Data types
        self.register(SystemView {
            name: "pg_type".to_string(),
            category: ViewCategory::Core,
            description: "Lists all data types".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("typname", DataType::Text),
                    Column::new("typnamespace", DataType::Int4),
                    Column::new("typowner", DataType::Int4),
                    Column::new("typlen", DataType::Int2),
                    Column::new("typtype", DataType::Char(1)),
                    Column::new("typcategory", DataType::Char(1)),
                ],
            },
        });

        // sqlite_master - SQLite-shaped catalog for drop-in compatibility
        // (only the columns sqlite3 apps actually read).
        self.register(SystemView {
            name: "sqlite_master".to_string(),
            category: ViewCategory::Core,
            description: "SQLite-compatible schema catalog".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("type", DataType::Text),
                    Column::new("name", DataType::Text),
                    Column::new("tbl_name", DataType::Text),
                    Column::new("rootpage", DataType::Int4),
                    Column::new("sql", DataType::Text),
                ],
            },
        });
    }

    /// Register session and activity views
    fn register_session_activity_views(&mut self) {
        // pg_stat_activity - Active queries and sessions
        self.register(SystemView {
            name: "pg_stat_activity".to_string(),
            category: ViewCategory::Session,
            description: "Shows information about current database sessions".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("datid", DataType::Int4),
                    Column::new("datname", DataType::Text),
                    Column::new("pid", DataType::Int4),
                    Column::new("usesysid", DataType::Int4),
                    Column::new("usename", DataType::Text),
                    Column::new("application_name", DataType::Text),
                    Column::new("client_addr", DataType::Text),
                    Column::new("client_port", DataType::Int4),
                    Column::new("backend_start", DataType::Timestamptz),
                    Column::new("state_change", DataType::Timestamptz),
                    Column::new("state", DataType::Text),
                    Column::new("query", DataType::Text),
                ],
            },
        });

        // pg_stat_database - Database statistics
        self.register(SystemView {
            name: "pg_stat_database".to_string(),
            category: ViewCategory::Statistics,
            description: "Shows database-wide statistics".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("datid", DataType::Int4),
                    Column::new("datname", DataType::Text),
                    Column::new("numbackends", DataType::Int4),
                    Column::new("xact_commit", DataType::Int8),
                    Column::new("xact_rollback", DataType::Int8),
                    Column::new("blks_read", DataType::Int8),
                    Column::new("blks_hit", DataType::Int8),
                    Column::new("tup_returned", DataType::Int8),
                    Column::new("tup_fetched", DataType::Int8),
                    Column::new("tup_inserted", DataType::Int8),
                    Column::new("tup_updated", DataType::Int8),
                    Column::new("tup_deleted", DataType::Int8),
                ],
            },
        });

        // pg_settings - Configuration settings
        self.register(SystemView {
            name: "pg_settings".to_string(),
            category: ViewCategory::Core,
            description: "Shows current database configuration settings".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("name", DataType::Text),
                    Column::new("setting", DataType::Text),
                    Column::new("unit", DataType::Text),
                    Column::new("category", DataType::Text),
                    Column::new("short_desc", DataType::Text),
                    Column::new("context", DataType::Text),
                    Column::new("vartype", DataType::Text),
                    Column::new("source", DataType::Text),
                    Column::new("min_val", DataType::Text),
                    Column::new("max_val", DataType::Text),
                ],
            },
        });
    }

    /// Register v2.0 feature views
    fn register_v2_feature_views(&mut self) {
        // pg_branches - Database branches (custom)
        self.register(SystemView {
            name: "pg_branches".to_string(),
            category: ViewCategory::Feature,
            description: "Lists all database branches (HeliosDB extension)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("branch_id", DataType::Int8),
                    Column::new("branch_name", DataType::Text),
                    Column::new("parent_id", DataType::Int8),
                    Column::new("parent_name", DataType::Text),
                    Column::new("created_at", DataType::Timestamptz),
                    Column::new("fork_point_lsn", DataType::Int8),
                    Column::new("state", DataType::Text),
                    Column::new("size_bytes", DataType::Int8),
                    Column::new("num_commits", DataType::Int8),
                ],
            },
        });

        // pg_materialized_views - Materialized view status
        self.register(SystemView {
            name: "pg_matviews".to_string(),
            category: ViewCategory::Feature,
            description: "Lists all materialized views with status".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("schemaname", DataType::Text),
                    Column::new("matviewname", DataType::Text),
                    Column::new("matviewowner", DataType::Text),
                    Column::new("definition", DataType::Text),
                    Column::new("ispopulated", DataType::Boolean),
                    Column::new("created_at", DataType::Timestamptz),
                    Column::new("last_refresh", DataType::Timestamptz),
                    Column::new("row_count", DataType::Int8),
                    Column::new("refresh_strategy", DataType::Text),
                    Column::new("base_tables", DataType::Text),
                ],
            },
        });

        // pg_snapshots - Time-travel snapshots (custom)
        self.register(SystemView {
            name: "pg_snapshots".to_string(),
            category: ViewCategory::Feature,
            description: "Lists all time-travel snapshots (HeliosDB extension)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("snapshot_id", DataType::Int8),
                    Column::new("created_at", DataType::Timestamptz),
                    Column::new("scn", DataType::Int8),
                    Column::new("transaction_id", DataType::Int8),
                    Column::new("description", DataType::Text),
                    Column::new("size_bytes", DataType::Int8),
                    Column::new("is_automatic", DataType::Boolean),
                ],
            },
        });

        // pg_transaction_map - Transaction ID to snapshot mapping
        self.register(SystemView {
            name: "pg_transaction_map".to_string(),
            category: ViewCategory::Feature,
            description: "Maps transaction IDs to snapshot timestamps (HeliosDB extension)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("transaction_id", DataType::Int8),
                    Column::new("snapshot_timestamp", DataType::Int8),
                    Column::new("scn", DataType::Int8),
                    Column::new("created_at", DataType::Timestamptz),
                ],
            },
        });

        // pg_scn_map - System Change Number to snapshot mapping
        self.register(SystemView {
            name: "pg_scn_map".to_string(),
            category: ViewCategory::Feature,
            description: "Maps SCN values to snapshot timestamps (HeliosDB extension)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("scn", DataType::Int8),
                    Column::new("snapshot_timestamp", DataType::Int8),
                    Column::new("transaction_id", DataType::Int8),
                    Column::new("created_at", DataType::Timestamptz),
                ],
            },
        });

        // heliosdb_art_indexes - Adaptive Radix Tree (ART) indexes
        self.register(SystemView {
            name: "heliosdb_art_indexes".to_string(),
            category: ViewCategory::Feature,
            description: "Lists all ART indexes for PK/FK/UNIQUE constraints (HeliosDB extension)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("index_name", DataType::Text),
                    Column::new("table_name", DataType::Text),
                    Column::new("index_type", DataType::Text),
                    Column::new("columns", DataType::Text),
                    Column::new("key_count", DataType::Int8),
                    Column::new("memory_bytes", DataType::Int8),
                    Column::new("constraint_checks", DataType::Int8),
                    Column::new("insertions", DataType::Int8),
                    Column::new("deletions", DataType::Int8),
                ],
            },
        });

        // heliosdb_simd_capabilities - CPU SIMD feature detection
        self.register(SystemView {
            name: "heliosdb_simd_capabilities".to_string(),
            category: ViewCategory::Statistics,
            description: "Shows detected CPU SIMD capabilities and filter statistics (HeliosDB extension)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("feature", DataType::Text),
                    Column::new("available", DataType::Boolean),
                    Column::new("description", DataType::Text),
                ],
            },
        });
    }

    /// Register v2.1 feature views
    fn register_v2_1_feature_views(&mut self) {
        // pg_stat_ssl - SSL/TLS connection statistics
        self.register(SystemView {
            name: "pg_stat_ssl".to_string(),
            category: ViewCategory::Statistics,
            description: "Shows SSL/TLS connection information".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("pid", DataType::Int4),
                    Column::new("ssl", DataType::Boolean),
                    Column::new("version", DataType::Text),
                    Column::new("cipher", DataType::Text),
                    Column::new("bits", DataType::Int4),
                    Column::new("client_dn", DataType::Text),
                    Column::new("client_serial", DataType::Text),
                    Column::new("issuer_dn", DataType::Text),
                ],
            },
        });

        // pg_authid - User authentication info
        self.register(SystemView {
            name: "pg_authid".to_string(),
            category: ViewCategory::Core,
            description: "Lists authentication identities (users and roles)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("rolname", DataType::Text),
                    Column::new("rolsuper", DataType::Boolean),
                    Column::new("rolinherit", DataType::Boolean),
                    Column::new("rolcreaterole", DataType::Boolean),
                    Column::new("rolcreatedb", DataType::Boolean),
                    Column::new("rolcanlogin", DataType::Boolean),
                    Column::new("rolconnlimit", DataType::Int4),
                    Column::new("rolvaliduntil", DataType::Timestamptz),
                ],
            },
        });

        // pg_stat_optimizer - Query optimizer statistics (custom)
        self.register(SystemView {
            name: "pg_stat_optimizer".to_string(),
            category: ViewCategory::Statistics,
            description: "Shows query optimizer statistics (HeliosDB extension)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("query_hash", DataType::Text),
                    Column::new("plan_type", DataType::Text),
                    Column::new("execution_count", DataType::Int8),
                    Column::new("total_time_ms", DataType::Float8),
                    Column::new("avg_time_ms", DataType::Float8),
                    Column::new("min_time_ms", DataType::Float8),
                    Column::new("max_time_ms", DataType::Float8),
                    Column::new("rows_estimate", DataType::Int8),
                    Column::new("rows_actual", DataType::Int8),
                    Column::new("last_execution", DataType::Timestamptz),
                ],
            },
        });

        // pg_compression_stats - Compression statistics (custom)
        self.register(SystemView {
            name: "pg_compression_stats".to_string(),
            category: ViewCategory::Statistics,
            description: "Shows compression statistics per table (HeliosDB extension)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("schemaname", DataType::Text),
                    Column::new("tablename", DataType::Text),
                    Column::new("compression_type", DataType::Text),
                    Column::new("uncompressed_bytes", DataType::Int8),
                    Column::new("compressed_bytes", DataType::Int8),
                    Column::new("compression_ratio", DataType::Float8),
                    Column::new("num_chunks", DataType::Int8),
                    Column::new("avg_chunk_size", DataType::Int8),
                    Column::new("last_updated", DataType::Timestamptz),
                ],
            },
        });
    }

    /// Register v2.3.0 monitoring and sync views
    fn register_v2_3_monitoring_views(&mut self) {
        // pg_stat_replication - Replication status (if sync enabled)
        self.register(SystemView {
            name: "pg_stat_replication".to_string(),
            category: ViewCategory::Statistics,
            description: "Shows replication status and synchronization state".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("client_id", DataType::Text),
                    Column::new("application_name", DataType::Text),
                    Column::new("state", DataType::Text), // 'streaming', 'catchup', 'offline'
                    Column::new("sent_lsn", DataType::Int8),
                    Column::new("write_lsn", DataType::Int8),
                    Column::new("flush_lsn", DataType::Int8),
                    Column::new("replay_lsn", DataType::Int8),
                    Column::new("sync_priority", DataType::Int4),
                    Column::new("sync_state", DataType::Text), // 'sync', 'async'
                    Column::new("connected_at", DataType::Timestamptz),
                    Column::new("last_sync_time", DataType::Timestamptz),
                ],
            },
        });

        // pg_stat_progress_vacuum - Maintenance progress tracking
        self.register(SystemView {
            name: "pg_stat_progress_vacuum".to_string(),
            category: ViewCategory::Statistics,
            description: "Shows progress of ongoing maintenance operations".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("pid", DataType::Int4),
                    Column::new("datid", DataType::Int4),
                    Column::new("datname", DataType::Text),
                    Column::new("relid", DataType::Int4),
                    Column::new("relname", DataType::Text),
                    Column::new("phase", DataType::Text), // 'scanning', 'vacuuming', 'cleaning'
                    Column::new("heap_blks_total", DataType::Int8),
                    Column::new("heap_blks_scanned", DataType::Int8),
                    Column::new("heap_blks_vacuumed", DataType::Int8),
                    Column::new("index_vacuum_count", DataType::Int8),
                    Column::new("current_free_pages", DataType::Int8),
                ],
            },
        });

        // helios_sync_status - HeliosDB-specific sync metrics
        self.register(SystemView {
            name: "helios_sync_status".to_string(),
            category: ViewCategory::Feature,
            description: "Shows HeliosDB sync and replication metrics (v2.3.0)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("node_id", DataType::Text),
                    Column::new("node_name", DataType::Text),
                    Column::new("is_primary", DataType::Boolean),
                    Column::new("connected_replicas", DataType::Int4),
                    Column::new("total_changes_sent", DataType::Int8),
                    Column::new("total_changes_received", DataType::Int8),
                    Column::new("last_sync_time", DataType::Timestamptz),
                    Column::new("avg_sync_latency_ms", DataType::Float8),
                    Column::new("max_sync_latency_ms", DataType::Float8),
                    Column::new("sync_errors", DataType::Int8),
                    Column::new("replication_lag_bytes", DataType::Int8),
                ],
            },
        });

        // helios_query_history - Query execution history
        self.register(SystemView {
            name: "helios_query_history".to_string(),
            category: ViewCategory::Statistics,
            description: "Shows recent query execution history and metrics (v2.3.0)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("query_id", DataType::Int8),
                    Column::new("query_hash", DataType::Text),
                    Column::new("query_text", DataType::Text),
                    Column::new("start_time", DataType::Timestamptz),
                    Column::new("end_time", DataType::Timestamptz),
                    Column::new("duration_ms", DataType::Float8),
                    Column::new("rows_returned", DataType::Int8),
                    Column::new("rows_examined", DataType::Int8),
                    Column::new("cpu_time_ms", DataType::Float8),
                    Column::new("io_time_ms", DataType::Float8),
                    Column::new("status", DataType::Text), // 'success', 'error', 'timeout'
                    Column::new("error_message", DataType::Text),
                    Column::new("user_name", DataType::Text),
                ],
            },
        });

        // helios_table_memory_stats - Per-table memory usage
        self.register(SystemView {
            name: "helios_table_memory_stats".to_string(),
            category: ViewCategory::Statistics,
            description: "Shows per-table memory usage and cache statistics (v2.3.0)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("schemaname", DataType::Text),
                    Column::new("tablename", DataType::Text),
                    Column::new("heap_size_bytes", DataType::Int8),
                    Column::new("cache_size_bytes", DataType::Int8),
                    Column::new("index_size_bytes", DataType::Int8),
                    Column::new("total_size_bytes", DataType::Int8),
                    Column::new("cache_hit_ratio", DataType::Float8),
                    Column::new("cache_accesses", DataType::Int8),
                    Column::new("cache_hits", DataType::Int8),
                    Column::new("last_analyzed", DataType::Timestamptz),
                ],
            },
        });

        // helios_transaction_stats - Transaction statistics
        self.register(SystemView {
            name: "helios_transaction_stats".to_string(),
            category: ViewCategory::Statistics,
            description: "Shows transaction statistics and ACID metrics (v2.3.0)".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("transaction_id", DataType::Int8),
                    Column::new("start_time", DataType::Timestamptz),
                    Column::new("end_time", DataType::Timestamptz),
                    Column::new("duration_ms", DataType::Float8),
                    Column::new("isolation_level", DataType::Text), // 'READ_UNCOMMITTED', 'READ_COMMITTED', etc.
                    Column::new("operations_count", DataType::Int8),
                    Column::new("rows_read", DataType::Int8),
                    Column::new("rows_written", DataType::Int8),
                    Column::new("status", DataType::Text), // 'committed', 'aborted', 'active'
                    Column::new("is_distributed", DataType::Boolean),
                ],
            },
        });

        // heliosdb_compression_stats - Compression statistics by algorithm
        self.register(SystemView {
            name: "heliosdb_compression_stats".to_string(),
            category: ViewCategory::Statistics,
            description: "Shows compression statistics grouped by algorithm".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("algorithm", DataType::Text),
                    Column::new("uses", DataType::Int8),
                    Column::new("avg_ratio", DataType::Float8),
                    Column::new("avg_compress_us", DataType::Float8),
                    Column::new("avg_decompress_us", DataType::Float8),
                    Column::new("total_bytes_in", DataType::Int8),
                    Column::new("total_bytes_out", DataType::Int8),
                ],
            },
        });

        // heliosdb_pattern_stats - Pattern detection statistics
        self.register(SystemView {
            name: "heliosdb_pattern_stats".to_string(),
            category: ViewCategory::Statistics,
            description: "Shows pattern detection statistics for compression".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("pattern", DataType::Text),
                    Column::new("detections", DataType::Int8),
                    Column::new("best_algorithm", DataType::Text),
                    Column::new("avg_ratio", DataType::Float8),
                ],
            },
        });

        // heliosdb_compression_events - Recent compression events
        self.register(SystemView {
            name: "heliosdb_compression_events".to_string(),
            category: ViewCategory::Statistics,
            description: "Shows recent compression events".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("timestamp", DataType::Timestamptz),
                    Column::new("table_name", DataType::Text),
                    Column::new("column_name", DataType::Text),
                    Column::new("algorithm", DataType::Text),
                    Column::new("ratio", DataType::Float8),
                    Column::new("input_bytes", DataType::Int8),
                    Column::new("output_bytes", DataType::Int8),
                    Column::new("duration_us", DataType::Int8),
                ],
            },
        });

        // heliosdb_config - Configuration settings
        self.register(SystemView {
            name: "heliosdb_config".to_string(),
            category: ViewCategory::Core,
            description: "Shows HeliosDB configuration settings".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("key", DataType::Text),
                    Column::new("value", DataType::Text),
                    Column::new("description", DataType::Text),
                ],
            },
        });
    }

    /// Register HA (High Availability) Tier 1 views
    fn register_ha_views(&mut self) {
        // helios_topology - Cluster topology with health information
        self.register(SystemView {
            name: "helios_topology".to_string(),
            category: ViewCategory::Feature,
            description: "Shows cluster topology with node status and health information".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("node_id", DataType::Text),
                    Column::new("alias", DataType::Text),
                    Column::new("role", DataType::Text),
                    Column::new("client_addr", DataType::Text),
                    Column::new("replication_addr", DataType::Text),
                    Column::new("healthy", DataType::Boolean),
                    Column::new("health_msg", DataType::Text),
                    Column::new("last_seen_secs", DataType::Int8),
                    Column::new("lsn", DataType::Int8),
                    Column::new("lag_ms", DataType::Int8),
                    Column::new("priority", DataType::Int4),
                    Column::new("weight", DataType::Int4),
                ],
            },
        });

        // helios_node_aliases - Node alias mappings
        self.register(SystemView {
            name: "helios_node_aliases".to_string(),
            category: ViewCategory::Feature,
            description: "Shows node alias to UUID mappings".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("alias", DataType::Text),
                    Column::new("node_id", DataType::Text),
                    Column::new("role", DataType::Text),
                    Column::new("healthy", DataType::Boolean),
                ],
            },
        });

        // helios_ha_status - HA cluster status summary
        self.register(SystemView {
            name: "helios_ha_status".to_string(),
            category: ViewCategory::Feature,
            description: "Shows HA cluster status summary".to_string(),
            schema: Schema {
                columns: vec![
                    Column::new("cluster_state", DataType::Text),
                    Column::new("primary_node", DataType::Text),
                    Column::new("primary_alias", DataType::Text),
                    Column::new("standby_count", DataType::Int4),
                    Column::new("healthy_standbys", DataType::Int4),
                    Column::new("current_lsn", DataType::Int8),
                    Column::new("oldest_standby_lsn", DataType::Int8),
                    Column::new("max_lag_ms", DataType::Int8),
                    Column::new("sync_mode", DataType::Text),
                ],
            },
        });
    }

    /// Register a system view
    fn register(&mut self, view: SystemView) {
        self.views.insert(view.name.clone(), view);
    }

    /// Check if a view name is a system view
    pub fn is_system_view(&self, name: &str) -> bool {
        self.views.contains_key(name)
    }

    /// Get system view schema
    pub fn get_schema(&self, name: &str) -> Option<&Schema> {
        self.views.get(name).map(|v| &v.schema)
    }

    /// Get system view definition
    pub fn get_view(&self, name: &str) -> Option<&SystemView> {
        self.views.get(name)
    }

    /// List all system views
    pub fn list_views(&self) -> Vec<&str> {
        self.views.keys().map(|s| s.as_str()).collect()
    }

    /// List views by category
    pub fn list_views_by_category(&self, category: ViewCategory) -> Vec<&str> {
        self.views
            .values()
            .filter(|v| v.category == category)
            .map(|v| v.name.as_str())
            .collect()
    }

    /// Invalidate cache for a specific view
    pub fn invalidate_view(&self, view_name: &str) -> Result<()> {
        let cache_key = CacheKey::new(view_name);
        let mut cache_guard = self.cache.lock().map_err(|e| {
            Error::query_execution(format!("Cache lock error: {}", e))
        })?;

        cache_guard.pop(&cache_key);
        tracing::debug!("Invalidated system view cache for '{}'", view_name);
        Ok(())
    }

    /// Invalidate entire cache (e.g., after DDL operations)
    pub fn invalidate_all(&self) -> Result<()> {
        let mut cache_guard = self.cache.lock().map_err(|e| {
            Error::query_execution(format!("Cache lock error: {}", e))
        })?;

        cache_guard.clear();
        tracing::info!("Invalidated entire system view cache");
        Ok(())
    }

    /// Invalidate cache for views in a specific category
    pub fn invalidate_category(&self, category: ViewCategory) -> Result<()> {
        let view_names: Vec<String> = self.views
            .values()
            .filter(|v| v.category == category)
            .map(|v| v.name.clone())
            .collect();

        for view_name in view_names {
            self.invalidate_view(&view_name)?;
        }

        tracing::debug!("Invalidated system view cache for category {:?}", category);
        Ok(())
    }

    /// Get cache statistics
    pub fn cache_stats(&self) -> Result<(usize, usize)> {
        let cache_guard = self.cache.lock().map_err(|e| {
            Error::query_execution(format!("Cache lock error: {}", e))
        })?;

        Ok((cache_guard.len(), cache_guard.cap().get()))
    }

    /// Execute a system view query with caching
    pub fn execute(&self, view_name: &str, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        if !self.is_system_view(view_name) {
            return Err(Error::query_execution(format!(
                "Unknown system view: {}",
                view_name
            )));
        }

        // Create cache key
        let cache_key = CacheKey::new(view_name);

        // Try to get from cache first
        {
            let cache_guard = self.cache.lock().map_err(|e| {
                Error::query_execution(format!("Cache lock error: {}", e))
            })?;

            if let Some(cached) = cache_guard.peek(&cache_key) {
                if cached.is_valid() {
                    tracing::debug!(
                        "System view cache HIT for '{}' (age: {:?}, ttl: {:?})",
                        view_name,
                        cached.cached_at.elapsed(),
                        cached.ttl
                    );
                    return Ok(cached.tuples.clone());
                } else {
                    tracing::debug!(
                        "System view cache EXPIRED for '{}' (age: {:?} > ttl: {:?})",
                        view_name,
                        cached.cached_at.elapsed(),
                        cached.ttl
                    );
                }
            } else {
                tracing::debug!("System view cache MISS for '{}'", view_name);
            }
        }

        // Cache miss or expired - execute the query
        let tuples = self.execute_uncached(view_name, storage)?;

        // Store in cache
        {
            let mut cache_guard = self.cache.lock().map_err(|e| {
                Error::query_execution(format!("Cache lock error: {}", e))
            })?;

            let cached_result = CachedResult::new(tuples.clone(), self.default_ttl);
            cache_guard.put(cache_key, cached_result);

            tracing::debug!(
                "System view cached '{}' ({} tuples, ttl: {:?})",
                view_name,
                tuples.len(),
                self.default_ttl
            );
        }

        Ok(tuples)
    }

    /// Execute a system view query without caching (internal use)
    fn execute_uncached(&self, view_name: &str, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        match view_name {
            // Core catalog
            "pg_tables" => self.execute_pg_tables(storage),
            "pg_views" => self.execute_pg_views(storage),
            "pg_indexes" => self.execute_pg_indexes(storage),
            "pg_attribute" => self.execute_pg_attribute(storage),
            "pg_database" => self.execute_pg_database(storage),
            "pg_namespace" => self.execute_pg_namespace(storage),
            "pg_class" => self.execute_pg_class(storage),
            "pg_type" => self.execute_pg_type(storage),

            // SQLite-compatibility catalog
            "sqlite_master" => self.execute_sqlite_master(storage),

            // Session/Activity
            "pg_stat_activity" => self.execute_pg_stat_activity(storage),
            "pg_stat_database" => self.execute_pg_stat_database(storage),
            "pg_settings" => self.execute_pg_settings(storage),

            // v2.0 Features
            "pg_branches" => self.execute_pg_branches(storage),
            "pg_matviews" => self.execute_pg_matviews(storage),
            "pg_snapshots" => self.execute_pg_snapshots(storage),
            "pg_transaction_map" => self.execute_pg_transaction_map(storage),
            "pg_scn_map" => self.execute_pg_scn_map(storage),

            // v2.1 Features
            "pg_stat_ssl" => self.execute_pg_stat_ssl(storage),
            "pg_authid" => self.execute_pg_authid(storage),
            "pg_stat_optimizer" => self.execute_pg_stat_optimizer(storage),
            "pg_compression_stats" => self.execute_pg_compression_stats(storage),

            // v2.3.0 Monitoring and Sync Views
            "pg_stat_replication" => self.execute_pg_stat_replication(storage),
            "pg_stat_progress_vacuum" => self.execute_pg_stat_progress_vacuum(storage),
            "helios_sync_status" => self.execute_helios_sync_status(storage),
            "helios_query_history" => self.execute_helios_query_history(storage),
            "helios_table_memory_stats" => self.execute_helios_table_memory_stats(storage),
            "helios_transaction_stats" => self.execute_helios_transaction_stats(storage),

            // Compression Monitoring Views (from docs)
            "heliosdb_compression_stats" => self.execute_heliosdb_compression_stats(storage),
            "heliosdb_pattern_stats" => self.execute_heliosdb_pattern_stats(storage),
            "heliosdb_compression_events" => self.execute_heliosdb_compression_events(storage),
            "heliosdb_config" => self.execute_heliosdb_config(storage),

            // HA (High Availability) Tier 1 Views
            "helios_topology" => self.execute_helios_topology(storage),
            "helios_node_aliases" => self.execute_helios_node_aliases(storage),
            "helios_ha_status" => self.execute_helios_ha_status(storage),

            // ART Index View
            "heliosdb_art_indexes" => self.execute_heliosdb_art_indexes(storage),

            // SIMD Capabilities View
            "heliosdb_simd_capabilities" => self.execute_heliosdb_simd_capabilities(storage),

            _ => Ok(vec![]),
        }
    }

    // === Core Catalog View Executors ===

    fn execute_pg_tables(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        let catalog = storage.catalog();
        let tables = catalog.list_tables()?;
        let mut results = Vec::new();

        for table_name in tables {
            // Skip system tables and materialized views
            if table_name.starts_with("helios_") || table_name.starts_with("mv_") {
                continue;
            }

            // Schema-namespacing surface (#188): the flat-prefix
            // `_hdb_code_*` / `_hdb_graph_*` tables are reported
            // under the `_hdb_code` / `_hdb_graph` schemas in
            // pg_catalog, even though the canonical storage key
            // stays flat.  Everything else lives in `public`.
            let (schema, bare) = if let Some(rest) = table_name.strip_prefix("_hdb_code_") {
                ("_hdb_code".to_string(), rest.to_string())
            } else if let Some(rest) = table_name.strip_prefix("_hdb_graph_") {
                ("_hdb_graph".to_string(), rest.to_string())
            } else {
                ("public".to_string(), table_name.clone())
            };
            let tuple = Tuple::new(vec![
                Value::String(schema),
                Value::String(bare),
                Value::String("heliosdb".to_string()),
                Value::Null, // tablespace
                Value::Boolean(false), // hasindexes (simplified)
                Value::Boolean(false), // hasrules
                Value::Boolean(false), // hastriggers
                Value::Boolean(false), // rowsecurity
            ]);
            results.push(tuple);
        }

        Ok(results)
    }

    fn execute_pg_views(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Currently no regular views, only materialized views
        Ok(vec![])
    }

    fn execute_pg_indexes(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        let vector_indexes = storage.vector_indexes();
        let all_metadata = vector_indexes.list_all_metadata();
        let mut results = Vec::new();

        for metadata in all_metadata {
            let tuple = Tuple::new(vec![
                Value::String("public".to_string()),
                Value::String(metadata.table_name.clone()),
                Value::String(metadata.name.clone()),
                Value::Null, // tablespace
                Value::String(format!(
                    "CREATE INDEX {} ON {} USING hnsw ({})",
                    metadata.name, metadata.table_name, metadata.column_name
                )),
            ]);
            results.push(tuple);
        }

        Ok(results)
    }

    fn execute_pg_attribute(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        let catalog = storage.catalog();
        let tables = catalog.list_tables()?;
        let mut results = Vec::new();

        for (table_idx, table_name) in tables.iter().enumerate() {
            if table_name.starts_with("helios_") {
                continue;
            }

            let schema = catalog.get_table_schema(table_name)?;
            for (col_idx, column) in schema.columns.iter().enumerate() {
                let tuple = Tuple::new(vec![
                    Value::Int4(table_idx as i32), // attrelid
                    Value::String(column.name.clone()), // attname
                    Value::Int4(Self::data_type_to_oid(&column.data_type)), // atttypid
                    Value::Int2(col_idx as i16), // attnum
                    Value::Int2(-1), // attlen (variable)
                    Value::Boolean(!column.nullable), // attnotnull
                    Value::Boolean(false), // atthasdef
                ]);
                results.push(tuple);
            }
        }

        Ok(results)
    }

    fn execute_pg_database(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Return single database entry
        let tuple = Tuple::new(vec![
            Value::String("heliosdb".to_string()),
            Value::Int4(1), // datdba
            Value::Int4(6), // UTF8
            Value::String("en_US.UTF-8".to_string()),
            Value::String("en_US.UTF-8".to_string()),
            Value::Boolean(false), // datistemplate
            Value::Boolean(true), // datallowconn
        ]);

        Ok(vec![tuple])
    }

    fn execute_pg_namespace(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Return public schema
        let tuple = Tuple::new(vec![
            Value::String("public".to_string()),
            Value::Int4(1), // nspowner
        ]);

        Ok(vec![tuple])
    }

    fn execute_pg_class(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        let catalog = storage.catalog();
        let tables = catalog.list_tables()?;
        let mut results = Vec::new();

        for (idx, table_name) in tables.iter().enumerate() {
            // Determine relation kind
            let relkind = if table_name.starts_with("mv_") {
                'm' // materialized view
            } else {
                'r' // regular table
            };

            let tuple = Tuple::new(vec![
                Value::String(table_name.clone()),
                Value::Int4(1), // relnamespace (public)
                Value::String(relkind.to_string()),
                Value::Int4(1), // relowner
                Value::Int4(0), // relam
                Value::Int4(0), // relpages
                Value::Float4(0.0), // reltuples
            ]);
            results.push(tuple);
        }

        Ok(results)
    }

    /// SQLite-shaped catalog: one row per user table (type='table'), one per
    /// materialized view (type='view'). The `sql` column is best-effort —
    /// most sqlite3 apps only filter on `type` and `name`.
    fn execute_sqlite_master(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        let catalog = storage.catalog();
        let tables = catalog.list_tables()?;
        let mut results = Vec::new();
        for table_name in tables {
            // Skip internal helios_* bookkeeping tables — sqlite3 apps don't expect them.
            if table_name.starts_with("helios_") {
                continue;
            }
            let (kind, sql_decl) = if let Some(rest) = table_name.strip_prefix("mv_") {
                ("view", format!("CREATE MATERIALIZED VIEW {rest} AS ..."))
            } else {
                ("table", format!("CREATE TABLE {table_name} (...)"))
            };
            results.push(Tuple::new(vec![
                Value::String(kind.to_string()),
                Value::String(table_name.clone()),
                Value::String(table_name),
                Value::Int4(0), // rootpage - irrelevant; SQLite returns physical page id
                Value::String(sql_decl),
            ]));
        }
        Ok(results)
    }

    fn execute_pg_type(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Return built-in types
        let types = vec![
            ("bool", 16, 'b', 'B'),
            ("int2", 21, 'b', 'N'),
            ("int4", 23, 'b', 'N'),
            ("int8", 20, 'b', 'N'),
            ("float4", 700, 'b', 'N'),
            ("float8", 701, 'b', 'N'),
            ("text", 25, 'b', 'S'),
            ("varchar", 1043, 'b', 'S'),
            ("timestamp", 1114, 'b', 'D'),
            ("timestamptz", 1184, 'b', 'D'),
            ("bytea", 17, 'b', 'U'),
            ("json", 114, 'b', 'U'),
            ("jsonb", 3802, 'b', 'U'),
        ];

        let mut results = Vec::new();
        for (name, oid, typtype, typcategory) in types {
            let tuple = Tuple::new(vec![
                Value::String(name.to_string()),
                Value::Int4(1), // typnamespace
                Value::Int4(1), // typowner
                Value::Int2(-1), // typlen
                Value::String(typtype.to_string()),
                Value::String(typcategory.to_string()),
            ]);
            results.push(tuple);
        }

        Ok(results)
    }

    // === Session/Activity View Executors ===

    fn execute_pg_stat_activity(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        let sessions = match &self.session_registry {
            Some(registry) => registry.get_all_sessions()?,
            None => return Ok(vec![]),
        };

        let mut results = Vec::new();
        for session in sessions {
            let tuple = Tuple::new(vec![
                Value::Int4(1), // datid
                Value::String("heliosdb".to_string()),
                Value::Int4(session.session_id as i32), // pid
                Value::Int4(1), // usesysid
                Value::String(session.username.clone()),
                Value::String(session.protocol.as_str().to_string()),
                Value::String(session.client_address.clone()),
                Value::Int4(session.client_port),
                Value::Timestamp(DateTime::from_timestamp(session.connect_time, 0).unwrap_or_else(Utc::now)),
                Value::Timestamp(DateTime::from_timestamp(session.last_activity, 0).unwrap_or_else(Utc::now)),
                Value::String(session.state.as_str().to_string()),
                session.current_query.map(Value::String).unwrap_or(Value::Null),
            ]);
            results.push(tuple);
        }

        Ok(results)
    }

    fn execute_pg_stat_database(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Get session count
        let num_backends = match &self.session_registry {
            Some(registry) => registry.get_all_sessions()?.len() as i32,
            None => 0,
        };

        // Get database statistics
        let stats = storage.stats();
        let snapshot = stats.snapshot();

        // Return single database stats row
        let tuple = Tuple::new(vec![
            Value::Int4(1), // datid
            Value::String("heliosdb".to_string()),
            Value::Int4(num_backends),
            Value::Int8(snapshot.xact_commit as i64),
            Value::Int8(snapshot.xact_rollback as i64),
            Value::Int8(snapshot.blks_read as i64),
            Value::Int8(snapshot.blks_hit as i64),
            Value::Int8(snapshot.tup_returned as i64),
            Value::Int8(snapshot.tup_fetched as i64),
            Value::Int8(snapshot.tup_inserted as i64),
            Value::Int8(snapshot.tup_updated as i64),
            Value::Int8(snapshot.tup_deleted as i64),
        ]);

        Ok(vec![tuple])
    }

    fn execute_pg_settings(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        let config = storage.config();
        let mut results = Vec::new();

        // WAL enabled
        results.push(Self::setting_tuple(
            "wal_enabled",
            &config.storage.wal_enabled.to_string(),
            "",
            "Write-Ahead Logging",
            "Enables write-ahead logging for durability",
            "postmaster",
            "bool",
        ));

        // Time travel enabled
        results.push(Self::setting_tuple(
            "time_travel_enabled",
            &config.storage.time_travel_enabled.to_string(),
            "",
            "Time Travel",
            "Enables automatic time-travel versioning",
            "postmaster",
            "bool",
        ));

        // Query timeout
        results.push(Self::setting_tuple(
            "query_timeout_ms",
            &config.storage.query_timeout_ms.map_or("unlimited".to_string(), |v| v.to_string()),
            "ms",
            "Query Execution",
            "Maximum query execution time in milliseconds",
            "user",
            "integer",
        ));

        // Slow query threshold
        results.push(Self::setting_tuple(
            "slow_query_threshold_ms",
            &config.storage.slow_query_threshold_ms.map_or("disabled".to_string(), |v| v.to_string()),
            "ms",
            "Query Execution",
            "Queries exceeding this threshold are logged at WARN level",
            "user",
            "integer",
        ));

        // Cache size
        results.push(Self::setting_tuple(
            "cache_size",
            &(config.storage.cache_size / (1024 * 1024)).to_string(),
            "MB",
            "Memory",
            "Memory cache size",
            "postmaster",
            "integer",
        ));

        Ok(results)
    }

    // === v2.0 Feature View Executors ===

    fn execute_pg_branches(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        let branches = storage.list_branches()?;
        let mut results = Vec::new();

        for branch in branches {
            // Lookup parent branch name if parent_id exists
            let parent_name = if let Some(_parent_id) = branch.parent_id {
                // For now, we don't have a direct method to get parent branch name by ID
                // The parent information is already in branch.parent_id
                Value::Null
            } else {
                Value::Null
            };

            let tuple = Tuple::new(vec![
                Value::Int8(branch.branch_id as i64),
                Value::String(branch.name.clone()),
                branch.parent_id.map(|id| Value::Int8(id as i64)).unwrap_or(Value::Null),
                parent_name,
                Value::Timestamp(DateTime::from_timestamp(branch.created_at as i64, 0).unwrap_or_else(Utc::now)),
                Value::Int8(branch.created_from_snapshot as i64),
                Value::String(format!("{:?}", branch.state)),
                Value::Int8(branch.stats.storage_bytes as i64),
                Value::Int8(branch.stats.commit_count as i64),
            ]);
            results.push(tuple);
        }

        Ok(results)
    }

    fn execute_pg_matviews(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        let mv_catalog = storage.mv_catalog();
        let view_names = mv_catalog.list_views()?;
        let mut results = Vec::new();

        for view_name in view_names {
            let metadata = mv_catalog.get_view(&view_name)?;

            let tuple = Tuple::new(vec![
                Value::String("public".to_string()),
                Value::String(metadata.view_name.clone()),
                Value::String("heliosdb".to_string()),
                Value::String(metadata.query_text.clone()),
                Value::Boolean(metadata.last_refresh.is_some()),
                Value::Timestamp(metadata.created_at),
                metadata.last_refresh.map(Value::Timestamp).unwrap_or(Value::Null),
                metadata.row_count.map(|c| Value::Int8(c as i64)).unwrap_or(Value::Null),
                Value::String(metadata.refresh_strategy.clone()),
                Value::String(format!("{:?}", metadata.base_tables)),
            ]);
            results.push(tuple);
        }

        Ok(results)
    }

    fn execute_pg_snapshots(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        let snapshot_manager = storage.snapshot_manager();
        let snapshots = snapshot_manager.list_snapshots()?;
        let mut results = Vec::new();

        for snapshot in snapshots {
            // Calculate snapshot size (may be slow for large snapshots)
            let size_bytes = snapshot_manager
                .calculate_snapshot_size(snapshot.timestamp)
                .unwrap_or(0);

            let tuple = Tuple::new(vec![
                Value::Int8(snapshot.timestamp as i64),
                Value::Timestamp(DateTime::from_timestamp(snapshot.timestamp as i64, 0).unwrap_or_else(Utc::now)),
                Value::Int8(snapshot.scn as i64),
                Value::Int8(snapshot.transaction_id as i64),
                Value::String(snapshot.wall_clock_time.clone()),
                Value::Int8(size_bytes as i64),
                Value::Boolean(!snapshot.gc_eligible), // is_automatic if not gc_eligible
            ]);
            results.push(tuple);
        }

        Ok(results)
    }

    fn execute_pg_transaction_map(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        let snapshot_manager = storage.snapshot_manager();
        let snapshots = snapshot_manager.list_snapshots()?;
        let mut results = Vec::new();

        for snapshot in snapshots {
            let created_at = DateTime::parse_from_rfc3339(&snapshot.wall_clock_time)
                .ok()
                .and_then(|dt| DateTime::from_timestamp(dt.timestamp(), 0))
                .unwrap_or_else(Utc::now);

            let tuple = Tuple::new(vec![
                Value::Int8(snapshot.transaction_id as i64),
                Value::Int8(snapshot.timestamp as i64),
                Value::Int8(snapshot.scn as i64),
                Value::Timestamp(created_at),
            ]);
            results.push(tuple);
        }

        // Sort by transaction_id for consistent ordering
        results.sort_by(|a, b| {
            let tx_a = match a.values.first() {
                Some(Value::Int8(v)) => *v,
                _ => 0,
            };
            let tx_b = match b.values.first() {
                Some(Value::Int8(v)) => *v,
                _ => 0,
            };
            tx_a.cmp(&tx_b)
        });

        Ok(results)
    }

    fn execute_pg_scn_map(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        let snapshot_manager = storage.snapshot_manager();
        let snapshots = snapshot_manager.list_snapshots()?;
        let mut results = Vec::new();

        for snapshot in snapshots {
            let created_at = DateTime::parse_from_rfc3339(&snapshot.wall_clock_time)
                .ok()
                .and_then(|dt| DateTime::from_timestamp(dt.timestamp(), 0))
                .unwrap_or_else(Utc::now);

            let tuple = Tuple::new(vec![
                Value::Int8(snapshot.scn as i64),
                Value::Int8(snapshot.timestamp as i64),
                Value::Int8(snapshot.transaction_id as i64),
                Value::Timestamp(created_at),
            ]);
            results.push(tuple);
        }

        // Sort by SCN for consistent ordering
        results.sort_by(|a, b| {
            let scn_a = match a.values.first() {
                Some(Value::Int8(v)) => *v,
                _ => 0,
            };
            let scn_b = match b.values.first() {
                Some(Value::Int8(v)) => *v,
                _ => 0,
            };
            scn_a.cmp(&scn_b)
        });

        Ok(results)
    }

    // === v2.1 Feature View Executors ===

    fn execute_pg_stat_ssl(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Get SSL info from sessions
        let sessions = match &self.session_registry {
            Some(registry) => registry.get_all_sessions()?,
            None => return Ok(vec![]),
        };

        let mut results = Vec::new();
        for session in sessions {
            // Simplified: assume no SSL for now
            let tuple = Tuple::new(vec![
                Value::Int4(session.session_id as i32),
                Value::Boolean(false), // ssl
                Value::Null, // version
                Value::Null, // cipher
                Value::Null, // bits
                Value::Null, // client_dn
                Value::Null, // client_serial
                Value::Null, // issuer_dn
            ]);
            results.push(tuple);
        }

        Ok(results)
    }

    fn execute_pg_authid(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Return default user
        let tuple = Tuple::new(vec![
            Value::String("heliosdb".to_string()),
            Value::Boolean(true), // rolsuper
            Value::Boolean(true), // rolinherit
            Value::Boolean(true), // rolcreaterole
            Value::Boolean(true), // rolcreatedb
            Value::Boolean(true), // rolcanlogin
            Value::Int4(-1), // rolconnlimit (unlimited)
            Value::Null, // rolvaliduntil
        ]);

        Ok(vec![tuple])
    }

    fn execute_pg_stat_optimizer(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Return table statistics for optimizer
        let catalog = storage.catalog();
        let tables = catalog.list_tables()?;
        let mut results = Vec::new();

        for table_name in tables {
            if let Some(stats) = catalog.get_table_statistics(&table_name)? {
                // Create a tuple for the table with aggregate statistics
                let tuple = Tuple::new(vec![
                    Value::String(format!("table:{}", table_name)), // query_hash
                    Value::String("SeqScan".to_string()), // plan_type
                    Value::Int8(stats.row_count as i64), // execution_count (using row_count as proxy)
                    Value::Float8(0.0), // total_time_ms (not tracked yet)
                    Value::Float8(0.0), // avg_time_ms
                    Value::Float8(0.0), // min_time_ms
                    Value::Float8(0.0), // max_time_ms
                    Value::Int8(stats.row_count as i64), // rows_estimate
                    Value::Int8(stats.row_count as i64), // rows_actual
                    Value::Timestamp(stats.last_analyzed), // last_execution
                ]);
                results.push(tuple);

                // Add per-column statistics entries
                for (col_name, col_stats) in &stats.columns {
                    let col_tuple = Tuple::new(vec![
                        Value::String(format!("{}:{}", table_name, col_name)), // query_hash
                        Value::String("ColumnScan".to_string()), // plan_type
                        Value::Int8(stats.row_count as i64), // execution_count
                        Value::Float8(0.0), // total_time_ms
                        Value::Float8(0.0), // avg_time_ms
                        Value::Float8(0.0), // min_time_ms
                        Value::Float8(0.0), // max_time_ms
                        Value::Int8(col_stats.n_distinct as i64), // rows_estimate (distinct values)
                        Value::Int8(col_stats.n_distinct as i64), // rows_actual
                        Value::Timestamp(stats.last_analyzed), // last_execution
                    ]);
                    results.push(col_tuple);
                }
            }
        }

        Ok(results)
    }

    fn execute_pg_compression_stats(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        let catalog = storage.catalog();
        let tables = catalog.list_tables()?;
        let mut results = Vec::new();

        for table_name in tables {
            if let Some(stats) = catalog.get_compression_stats(&table_name)? {
                // Determine dominant compression type from column stats
                let compression_type = if !stats.column_stats.is_empty() {
                    "mixed".to_string() // Multiple columns may have different types
                } else {
                    "none".to_string()
                };

                let num_columns = stats.column_stats.len();
                let avg_chunk_size = if num_columns > 0 {
                    stats.total_compressed_size / num_columns
                } else {
                    0
                };

                let tuple = Tuple::new(vec![
                    Value::String("public".to_string()),
                    Value::String(table_name.clone()),
                    Value::String(compression_type),
                    Value::Int8(stats.total_original_size as i64),
                    Value::Int8(stats.total_compressed_size as i64),
                    Value::Float8(stats.overall_ratio),
                    Value::Int8(num_columns as i64),
                    Value::Int8(avg_chunk_size as i64),
                    Value::Timestamp(Utc::now()), // last_updated
                ]);
                results.push(tuple);
            }
        }

        Ok(results)
    }

    // === v2.3.0 Monitoring View Executors ===

    fn execute_pg_stat_replication(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Returns replication status information
        // If replication is not enabled, returns empty result set

        let mut results = Vec::new();

        if let Some(ref stats) = self.stats_collector {
            let replicas = stats.replication.get_replicas();

            for replica in replicas {
                let tuple = Tuple::new(vec![
                    // pid (backend process ID - simulated)
                    Value::Int4(std::process::id() as i32),
                    // usesysid (user OID)
                    Value::Int4(10), // postgres user
                    // usename
                    Value::String("replicator".to_string()),
                    // application_name
                    replica.application_name.clone().map(Value::String).unwrap_or(Value::Null),
                    // client_addr
                    Value::String(replica.host.clone()),
                    // client_hostname
                    Value::Null,
                    // client_port
                    Value::Int4(replica.port as i32),
                    // backend_start
                    Value::Timestamp(replica.last_msg_time),
                    // backend_xmin
                    Value::Null,
                    // state
                    Value::String(replica.state.to_string()),
                    // sent_lsn
                    Value::String(replica.current_lsn.clone()),
                    // write_lsn
                    Value::String(replica.replay_lsn.clone()),
                    // flush_lsn
                    Value::String(replica.replay_lsn.clone()),
                    // replay_lsn
                    Value::String(replica.replay_lsn.clone()),
                    // write_lag (interval)
                    Value::Null,
                    // flush_lag (interval)
                    Value::Null,
                    // replay_lag (interval)
                    Value::Null,
                    // sync_priority
                    Value::Int4(0),
                    // sync_state
                    Value::String("async".to_string()),
                ]);
                results.push(tuple);
            }
        }

        Ok(results)
    }

    fn execute_pg_stat_progress_vacuum(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Returns progress of ongoing vacuum/maintenance operations
        // Returns empty result if no vacuum is in progress

        // Placeholder: would connect to maintenance scheduler to track progress
        // In a full implementation, this would query the maintenance task scheduler
        // and return progress information for each active maintenance operation

        Ok(vec![])
    }

    fn execute_helios_sync_status(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Returns HeliosDB sync and replication status
        // Provides metrics about data synchronization between nodes
        let mut results = Vec::new();

        if let Some(ref stats) = self.stats_collector {
            let summary = stats.replication.get_summary();
            let replicas = stats.replication.get_replicas();

            // Node summary tuple
            let tuple = Tuple::new(vec![
                Value::String("local".to_string()), // node_id
                Value::String("default".to_string()), // node_name
                Value::Boolean(summary.role == crate::storage::ReplicationRole::Primary), // is_primary
                Value::Int4(summary.replica_count as i32), // connected_replicas
                Value::Int8(summary.bytes_sent as i64), // total_changes_sent
                Value::Int8(summary.bytes_received as i64), // total_changes_received
                Value::Timestamp(Utc::now()), // last_sync_time
                Value::Float8(0.0), // avg_sync_latency_ms
                Value::Float8(summary.max_replica_lag_ms as f64), // max_sync_latency_ms
                Value::Int8(0), // sync_errors
                Value::Int8(replicas.iter().map(|r| r.bytes_lag).max().unwrap_or(0) as i64), // replication_lag_bytes
            ]);
            results.push(tuple);

            // Per-replica status
            for replica in replicas {
                let tuple = Tuple::new(vec![
                    Value::String(replica.replica_id.clone()), // node_id
                    Value::String(format!("replica-{}", replica.replica_id)), // node_name
                    Value::Boolean(false), // is_primary
                    Value::Int4(0), // connected_replicas
                    Value::Int8(0), // total_changes_sent
                    Value::Int8(replica.bytes_lag as i64), // total_changes_received (bytes behind)
                    Value::Timestamp(replica.last_msg_time), // last_sync_time
                    Value::Float8(replica.time_lag_ms as f64), // avg_sync_latency_ms
                    Value::Float8(replica.time_lag_ms as f64), // max_sync_latency_ms
                    Value::Int8(0), // sync_errors
                    Value::Int8(replica.bytes_lag as i64), // replication_lag_bytes
                ]);
                results.push(tuple);
            }
        } else {
            // Default standalone node tuple
            let tuple = Tuple::new(vec![
                Value::String("local".to_string()), // node_id
                Value::String("default".to_string()), // node_name
                Value::Boolean(true), // is_primary
                Value::Int4(0), // connected_replicas
                Value::Int8(0), // total_changes_sent
                Value::Int8(0), // total_changes_received
                Value::Timestamp(Utc::now()),
                Value::Float8(0.0), // avg_sync_latency_ms
                Value::Float8(0.0), // max_sync_latency_ms
                Value::Int8(0), // sync_errors
                Value::Int8(0), // replication_lag_bytes
            ]);
            results.push(tuple);
        }

        Ok(results)
    }

    fn execute_helios_query_history(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Returns recent query execution history
        // Provides performance metrics for queries

        let mut results = Vec::new();

        if let Some(ref stats) = self.stats_collector {
            // Get recent query history (last 1000 queries)
            let history = stats.query_history.get_recent(1000);

            for entry in history {
                let tuple = Tuple::new(vec![
                    // query_id
                    Value::Int8(entry.query_id as i64),
                    // query_hash
                    Value::Int8(entry.query_hash as i64),
                    // query_text
                    Value::String(entry.query_text.clone()),
                    // query_type
                    Value::String(entry.query_type.clone()),
                    // start_time
                    Value::Timestamp(entry.start_time),
                    // end_time
                    entry.end_time.map(Value::Timestamp).unwrap_or(Value::Null),
                    // duration_ms
                    entry.duration_ms.map(|d| Value::Int8(d as i64)).unwrap_or(Value::Null),
                    // rows_returned
                    Value::Int8(entry.rows_returned as i64),
                    // rows_examined
                    Value::Int8(entry.rows_examined as i64),
                    // status
                    Value::String(entry.status.to_string()),
                    // error_message
                    entry.error_message.clone().map(Value::String).unwrap_or(Value::Null),
                    // user_name
                    Value::String(entry.user_name.clone()),
                    // database_name
                    Value::String(entry.database_name.clone()),
                    // client_addr
                    entry.client_addr.clone().map(Value::String).unwrap_or(Value::Null),
                    // application_name
                    entry.application_name.clone().map(Value::String).unwrap_or(Value::Null),
                    // is_prepared
                    Value::Boolean(entry.is_prepared),
                    // plan_time_ms
                    entry.plan_time_ms.map(Value::Float8).unwrap_or(Value::Null),
                    // exec_time_ms
                    entry.exec_time_ms.map(Value::Float8).unwrap_or(Value::Null),
                    // shared_blks_hit
                    Value::Int8(entry.shared_blks_hit as i64),
                    // shared_blks_read
                    Value::Int8(entry.shared_blks_read as i64),
                    // shared_blks_written
                    Value::Int8(entry.shared_blks_written as i64),
                    // temp_blks_read
                    Value::Int8(entry.temp_blks_read as i64),
                    // temp_blks_written
                    Value::Int8(entry.temp_blks_written as i64),
                ]);
                results.push(tuple);
            }

            // Also include currently running queries
            let running = stats.query_history.get_running();
            for entry in running {
                let tuple = Tuple::new(vec![
                    Value::Int8(entry.query_id as i64),
                    Value::Int8(entry.query_hash as i64),
                    Value::String(entry.query_text.clone()),
                    Value::String(entry.query_type.clone()),
                    Value::Timestamp(entry.start_time),
                    Value::Null, // end_time not set for running queries
                    Value::Null, // duration_ms not set for running queries
                    Value::Int8(0), // rows_returned
                    Value::Int8(0), // rows_examined
                    Value::String("running".to_string()),
                    Value::Null, // error_message
                    Value::String(entry.user_name.clone()),
                    Value::String(entry.database_name.clone()),
                    entry.client_addr.clone().map(Value::String).unwrap_or(Value::Null),
                    entry.application_name.clone().map(Value::String).unwrap_or(Value::Null),
                    Value::Boolean(entry.is_prepared),
                    Value::Null, // plan_time_ms
                    Value::Null, // exec_time_ms
                    Value::Int8(0), // shared_blks_hit
                    Value::Int8(0), // shared_blks_read
                    Value::Int8(0), // shared_blks_written
                    Value::Int8(0), // temp_blks_read
                    Value::Int8(0), // temp_blks_written
                ]);
                results.push(tuple);
            }
        }

        Ok(results)
    }

    fn execute_helios_table_memory_stats(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Returns per-table memory usage and cache statistics
        let catalog = storage.catalog();
        let tables = catalog.list_tables()?;
        let mut results = Vec::new();

        for table_name in tables {
            // Skip internal tables
            if table_name.starts_with("helios_") || table_name.starts_with("mv_") {
                continue;
            }

            // Get table statistics if available
            let heap_size = if let Ok(Some(stats)) = catalog.get_table_statistics(&table_name) {
                (stats.row_count as i64) * 100 // Rough estimate: 100 bytes per row
            } else {
                0
            };

            let tuple = Tuple::new(vec![
                Value::String("public".to_string()), // schemaname
                Value::String(table_name.clone()), // tablename
                Value::Int8(heap_size), // heap_size_bytes
                Value::Int8(0), // cache_size_bytes (not tracked)
                Value::Int8(0), // index_size_bytes
                Value::Int8(heap_size), // total_size_bytes
                Value::Float8(0.0), // cache_hit_ratio
                Value::Int8(0), // cache_accesses
                Value::Int8(0), // cache_hits
                Value::Timestamp(Utc::now()),
            ]);
            results.push(tuple);
        }

        Ok(results)
    }

    fn execute_helios_transaction_stats(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Returns transaction statistics
        // Provides metrics about ACID compliance and transaction processing

        let mut results = Vec::new();

        if let Some(ref stats) = self.stats_collector {
            // Get transaction tracker statistics
            let tx_stats = stats.transactions.get_stats();
            let db_stats = stats.database_stats.snapshot();
            let repl_summary = stats.replication.get_summary();

            // Summary row with overall transaction statistics
            let tuple = Tuple::new(vec![
                // database_name
                Value::String("heliosdb".to_string()),
                // xact_commit - total committed transactions
                Value::Int8(tx_stats.total_committed as i64),
                // xact_rollback - total rolled back transactions
                Value::Int8(tx_stats.total_rolled_back as i64),
                // xact_active - currently active transactions
                Value::Int8(tx_stats.active_transactions as i64),
                // xact_total - total transactions started
                Value::Int8(tx_stats.total_started as i64),
                // deadlocks - total deadlocks detected
                Value::Int8(tx_stats.total_deadlocks as i64),
                // blks_read - blocks read from disk
                Value::Int8(db_stats.blks_read as i64),
                // blks_hit - blocks found in cache
                Value::Int8(db_stats.blks_hit as i64),
                // tup_returned - tuples returned by queries
                Value::Int8(db_stats.tup_returned as i64),
                // tup_fetched - tuples fetched
                Value::Int8(db_stats.tup_fetched as i64),
                // tup_inserted - tuples inserted
                Value::Int8(db_stats.tup_inserted as i64),
                // tup_updated - tuples updated
                Value::Int8(db_stats.tup_updated as i64),
                // tup_deleted - tuples deleted
                Value::Int8(db_stats.tup_deleted as i64),
                // conflicts - replication conflicts (0 if not replicating)
                Value::Int8(0),
                // temp_files - temporary files created
                Value::Int8(0),
                // temp_bytes - temporary bytes written
                Value::Int8(0),
                // blk_read_time - time spent reading blocks (ms)
                Value::Float8(0.0),
                // blk_write_time - time spent writing blocks (ms)
                Value::Float8(0.0),
                // stats_reset - when stats were last reset
                Value::Timestamp(stats.stats_reset_time),
                // replication_role
                Value::String(repl_summary.role.to_string()),
                // replication_state
                repl_summary.state.map(|s| Value::String(s.to_string())).unwrap_or(Value::Null),
            ]);
            results.push(tuple);

            // Also include active transaction details
            let active_transactions = stats.transactions.get_active();
            for tx in active_transactions {
                let tuple = Tuple::new(vec![
                    // database_name
                    Value::String(tx.database_name.clone()),
                    // xact_id
                    Value::Int8(tx.xact_id as i64),
                    // user_name
                    Value::String(tx.user_name.clone()),
                    // state
                    Value::String(tx.state.to_string()),
                    // start_time
                    Value::Timestamp(tx.start_time),
                    // duration_ms
                    Value::Int8(tx.duration_ms()),
                    // statement_count
                    Value::Int8(tx.statement_count as i64),
                    // current_query
                    tx.current_query.clone().map(Value::String).unwrap_or(Value::Null),
                    // backend_pid
                    Value::Int4(tx.backend_pid as i32),
                    // wait_event_type
                    tx.wait_event_type.clone().map(Value::String).unwrap_or(Value::Null),
                    // wait_event
                    tx.wait_event.clone().map(Value::String).unwrap_or(Value::Null),
                    // client_addr
                    tx.client_addr.clone().map(Value::String).unwrap_or(Value::Null),
                    // application_name
                    tx.application_name.clone().map(Value::String).unwrap_or(Value::Null),
                    // is_prepared
                    Value::Boolean(tx.is_prepared),
                    // Additional fields to match schema - pad with nulls
                    Value::Null,
                    Value::Null,
                    Value::Null,
                    Value::Null,
                    Value::Null,
                    Value::Null,
                    Value::Null,
                ]);
                results.push(tuple);
            }
        }

        Ok(results)
    }

    // === Compression Monitoring View Executors ===

    fn execute_heliosdb_compression_stats(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Aggregate compression statistics by algorithm
        let catalog = storage.catalog();
        let tables = catalog.list_tables()?;
        let mut results = Vec::new();

        // Aggregate stats by codec
        let mut alp_stats = (0i64, 0i64, 0i64, 0.0f64);  // (uses, bytes_in, bytes_out, total_ratio)
        let mut fsst_stats = (0i64, 0i64, 0i64, 0.0f64);
        let mut none_stats = (0i64, 0i64, 0i64, 0.0f64);

        for table_name in &tables {
            if let Some(stats) = catalog.get_compression_stats(table_name)? {
                for (col_name, col_stats) in &stats.column_stats {
                    let codec_name = format!("{:?}", col_stats.codec);
                    match codec_name.as_str() {
                        "ALP" => {
                            alp_stats.0 += col_stats.value_count as i64;
                            alp_stats.1 += col_stats.original_size as i64;
                            alp_stats.2 += col_stats.compressed_size as i64;
                            alp_stats.3 += col_stats.compression_ratio;
                        }
                        "FSST" => {
                            fsst_stats.0 += col_stats.value_count as i64;
                            fsst_stats.1 += col_stats.original_size as i64;
                            fsst_stats.2 += col_stats.compressed_size as i64;
                            fsst_stats.3 += col_stats.compression_ratio;
                        }
                        _ => {
                            none_stats.0 += col_stats.value_count as i64;
                            none_stats.1 += col_stats.original_size as i64;
                            none_stats.2 += col_stats.compressed_size as i64;
                        }
                    }
                }
            }
        }

        // Add ALP row if used
        if alp_stats.0 > 0 {
            results.push(Tuple::new(vec![
                Value::String("ALP".to_string()),
                Value::Int8(alp_stats.0),
                Value::Float8(if alp_stats.2 > 0 { alp_stats.1 as f64 / alp_stats.2 as f64 } else { 1.0 }),
                Value::Float8(0.0), // avg_compress_us (not tracked yet)
                Value::Float8(0.0), // avg_decompress_us
                Value::Int8(alp_stats.1),
                Value::Int8(alp_stats.2),
            ]));
        }

        // Add FSST row if used
        if fsst_stats.0 > 0 {
            results.push(Tuple::new(vec![
                Value::String("FSST".to_string()),
                Value::Int8(fsst_stats.0),
                Value::Float8(if fsst_stats.2 > 0 { fsst_stats.1 as f64 / fsst_stats.2 as f64 } else { 1.0 }),
                Value::Float8(0.0),
                Value::Float8(0.0),
                Value::Int8(fsst_stats.1),
                Value::Int8(fsst_stats.2),
            ]));
        }

        // Add None row if exists
        if none_stats.0 > 0 {
            results.push(Tuple::new(vec![
                Value::String("None".to_string()),
                Value::Int8(none_stats.0),
                Value::Float8(1.0),
                Value::Float8(0.0),
                Value::Float8(0.0),
                Value::Int8(none_stats.1),
                Value::Int8(none_stats.2),
            ]));
        }

        Ok(results)
    }

    fn execute_heliosdb_pattern_stats(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Pattern detection statistics
        // Currently returns predefined patterns based on data type affinity
        let results = vec![
            Tuple::new(vec![
                Value::String("FloatingPointData".to_string()),
                Value::Int8(0), // Will be populated when pattern detection is tracked
                Value::String("ALP".to_string()),
                Value::Float8(3.8),
            ]),
            Tuple::new(vec![
                Value::String("StringData".to_string()),
                Value::Int8(0),
                Value::String("FSST".to_string()),
                Value::Float8(6.2),
            ]),
        ];
        Ok(results)
    }

    fn execute_heliosdb_compression_events(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Recent compression events - shows per-table/column compression info
        let catalog = storage.catalog();
        let tables = catalog.list_tables()?;
        let mut results = Vec::new();

        for table_name in &tables {
            if table_name.starts_with("helios_") || table_name.starts_with("mv_") {
                continue;
            }

            if let Some(stats) = catalog.get_compression_stats(table_name)? {
                for (col_name, col_stats) in &stats.column_stats {
                    let tuple = Tuple::new(vec![
                        Value::Timestamp(Utc::now()), // timestamp
                        Value::String(table_name.clone()),
                        Value::String(col_name.clone()),
                        Value::String(format!("{:?}", col_stats.codec)),
                        Value::Float8(col_stats.compression_ratio),
                        Value::Int8(col_stats.original_size as i64),
                        Value::Int8(col_stats.compressed_size as i64),
                        Value::Int8(0), // duration_us (not tracked)
                    ]);
                    results.push(tuple);
                }
            }
        }

        Ok(results)
    }

    fn execute_heliosdb_config(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        // Returns compression-related configuration settings
        let catalog = storage.catalog();
        let tables = catalog.list_tables()?;
        let mut results = Vec::new();

        // Global compression settings
        results.push(Tuple::new(vec![
            Value::String("compression.enabled".to_string()),
            Value::String("true".to_string()),
            Value::String("Enable automatic compression".to_string()),
        ]));

        results.push(Tuple::new(vec![
            Value::String("compression.algorithm".to_string()),
            Value::String("auto".to_string()),
            Value::String("Default compression algorithm (auto selects best)".to_string()),
        ]));

        results.push(Tuple::new(vec![
            Value::String("compression.auto.min_rows".to_string()),
            Value::String("1000".to_string()),
            Value::String("Minimum rows before compression is applied".to_string()),
        ]));

        results.push(Tuple::new(vec![
            Value::String("compression.auto.min_data_size".to_string()),
            Value::String("1024".to_string()),
            Value::String("Minimum data size in bytes for compression".to_string()),
        ]));

        results.push(Tuple::new(vec![
            Value::String("compression.level".to_string()),
            Value::String("6".to_string()),
            Value::String("Compression level (1-9, higher = better ratio)".to_string()),
        ]));

        // Add per-table compression configs
        for table_name in &tables {
            if table_name.starts_with("helios_") || table_name.starts_with("mv_") {
                continue;
            }

            if let Some(config) = catalog.get_compression_config(table_name)? {
                results.push(Tuple::new(vec![
                    Value::String(format!("compression.table.{}.enabled", table_name)),
                    Value::String(config.enabled.to_string()),
                    Value::String(format!("Compression enabled for table {}", table_name)),
                ]));

                results.push(Tuple::new(vec![
                    Value::String(format!("compression.table.{}.level", table_name)),
                    Value::String(config.compression_level.to_string()),
                    Value::String(format!("Compression level for table {}", table_name)),
                ]));
            }
        }

        Ok(results)
    }

    // === Helper Functions ===

    fn setting_tuple(name: &str, value: &str, unit: &str, category: &str, desc: &str, context: &str, vartype: &str) -> Tuple {
        Tuple::new(vec![
            Value::String(name.to_string()),
            Value::String(value.to_string()),
            Value::String(unit.to_string()),
            Value::String(category.to_string()),
            Value::String(desc.to_string()),
            Value::String(context.to_string()),
            Value::String(vartype.to_string()),
            Value::String("configuration file".to_string()),
            Value::Null, // min_val
            Value::Null, // max_val
        ])
    }

    // === HA (High Availability) View Executors ===

    /// Execute helios_topology - shows cluster topology with health info
    fn execute_helios_topology(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        #[cfg(feature = "ha-tier1")]
        {
            use crate::replication::ha_state::{ha_state, HARole};
            use crate::replication::topology_manager;

            let ha_registry = ha_state();
            let topology = topology_manager();
            let mut results = Vec::new();

            // Helper to get alias for a node
            let get_alias = |node_id: uuid::Uuid| -> Value {
                topology.get_node(node_id)
                    .and_then(|n| n.alias.clone())
                    .map(Value::String)
                    .unwrap_or(Value::Null)
            };

            // Helper to get node info from topology
            let get_topology_info = |node_id: uuid::Uuid| -> (u32, u32, Option<String>) {
                topology.get_node(node_id)
                    .map(|n| (n.priority, n.weight, n.health_message.clone()))
                    .unwrap_or((100, 100, None))
            };

            // Add local node info
            if let Some(config) = ha_registry.get_config() {
                let role_str = match ha_registry.get_role() {
                    HARole::Primary => "Primary",
                    HARole::Standby => "Standby",
                    HARole::Standalone => "Standalone",
                    HARole::Observer => "Observer",
                };

                let alias = get_alias(config.node_id);
                let (priority, weight, health_msg) = get_topology_info(config.node_id);

                results.push(Tuple::new(vec![
                    Value::String(config.node_id.to_string()),
                    alias,
                    Value::String(role_str.to_string()),
                    Value::String(config.listen_addr.clone()),
                    Value::String(format!("{}:{}", config.listen_addr, config.replication_port)),
                    Value::Boolean(true), // Local node is healthy from its perspective
                    Value::String(health_msg.unwrap_or_else(|| "OK".to_string())),
                    Value::Int8(0), // last seen
                    Value::Int8(ha_registry.get_lsn() as i64),
                    Value::Int8(0), // No lag for self
                    Value::Int4(priority as i32),
                    Value::Int4(weight as i32),
                ]));
            }

            // Add standby info
            for standby in ha_registry.get_standbys() {
                let alias = get_alias(standby.node_id);
                let (priority, weight, health_msg) = get_topology_info(standby.node_id);

                results.push(Tuple::new(vec![
                    Value::String(standby.node_id.to_string()),
                    alias,
                    Value::String("Standby".to_string()),
                    Value::String(standby.address.clone()),
                    Value::String(standby.address.clone()),
                    Value::Boolean(true), // Connected standbys are healthy
                    Value::String(health_msg.unwrap_or_else(|| "Connected".to_string())),
                    Value::Int8(0),
                    Value::Int8(standby.apply_lsn as i64),
                    Value::Int8(standby.lag_ms as i64),
                    Value::Int4(priority as i32),
                    Value::Int4(weight as i32),
                ]));
            }

            Ok(results)
        }

        #[cfg(not(feature = "ha-tier1"))]
        {
            // Return empty when HA is not enabled
            Ok(vec![])
        }
    }

    /// Execute helios_node_aliases - shows alias to UUID mappings
    fn execute_helios_node_aliases(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        #[cfg(feature = "ha-tier1")]
        {
            use crate::replication::ha_state::{ha_state, HARole};
            use crate::replication::topology_manager;

            let topology = topology_manager();
            let ha_registry = ha_state();
            let mut results = Vec::new();

            // Get all aliases
            let aliases = topology.get_all_aliases();

            for (alias, node_id) in aliases {
                // Determine role from HA registry
                let role = if let Some(config) = ha_registry.get_config() {
                    if config.node_id == node_id {
                        match ha_registry.get_role() {
                            HARole::Primary => "Primary",
                            HARole::Standby => "Standby",
                            HARole::Standalone => "Standalone",
                            HARole::Observer => "Observer",
                        }
                    } else if ha_registry.get_standbys().iter().any(|s| s.node_id == node_id) {
                        "Standby"
                    } else {
                        "Unknown"
                    }
                } else {
                    "Unknown"
                };

                let healthy = topology.get_node(node_id)
                    .map(|n| n.is_healthy)
                    .unwrap_or(false);

                results.push(Tuple::new(vec![
                    Value::String(alias),
                    Value::String(node_id.to_string()),
                    Value::String(role.to_string()),
                    Value::Boolean(healthy),
                ]));
            }

            Ok(results)
        }

        #[cfg(not(feature = "ha-tier1"))]
        {
            Ok(vec![])
        }
    }

    /// Execute helios_ha_status - shows HA cluster summary
    fn execute_helios_ha_status(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        #[cfg(feature = "ha-tier1")]
        {
            use crate::replication::ha_state::{ha_state, HARole, SyncMode};
            use crate::replication::topology_manager;

            let ha_registry = ha_state();
            let topology = topology_manager();

            let standbys = ha_registry.get_standbys();
            let standby_count = standbys.len() as i32;
            let healthy_standbys = standbys.iter().filter(|_| true).count() as i32; // All connected are healthy

            let current_lsn = ha_registry.get_lsn();
            let oldest_lsn = standbys.iter().map(|s| s.apply_lsn).min().unwrap_or(current_lsn);
            let max_lag = standbys.iter().map(|s| s.lag_ms).max().unwrap_or(0);

            let cluster_state = match ha_registry.get_role() {
                HARole::Primary => if standby_count > 0 { "healthy" } else { "standalone" },
                HARole::Standby => "standby",
                HARole::Standalone => "standalone",
                HARole::Observer => "observer",
            };

            let (primary_node, primary_alias) = if let Some(config) = ha_registry.get_config() {
                if ha_registry.get_role() == HARole::Primary {
                    let alias = topology.get_node(config.node_id)
                        .and_then(|n| n.alias.clone());
                    (Value::String(config.node_id.to_string()), alias.map(Value::String).unwrap_or(Value::Null))
                } else {
                    (Value::Null, Value::Null)
                }
            } else {
                (Value::Null, Value::Null)
            };

            let sync_mode = ha_registry.get_config()
                .map(|c| match c.sync_mode {
                    SyncMode::Async => "async",
                    SyncMode::Sync => "sync",
                    SyncMode::SemiSync { .. } => "semi-sync",
                })
                .unwrap_or("unknown");

            let tuple = Tuple::new(vec![
                Value::String(cluster_state.to_string()),
                primary_node,
                primary_alias,
                Value::Int4(standby_count),
                Value::Int4(healthy_standbys),
                Value::Int8(current_lsn as i64),
                Value::Int8(oldest_lsn as i64),
                Value::Int8(max_lag as i64),
                Value::String(sync_mode.to_string()),
            ]);

            Ok(vec![tuple])
        }

        #[cfg(not(feature = "ha-tier1"))]
        {
            // Return a single row indicating HA is not enabled
            let tuple = Tuple::new(vec![
                Value::String("disabled".to_string()),
                Value::Null,
                Value::Null,
                Value::Int4(0),
                Value::Int4(0),
                Value::Int8(0),
                Value::Int8(0),
                Value::Int8(0),
                Value::String("none".to_string()),
            ]);
            Ok(vec![tuple])
        }
    }

    /// Execute heliosdb_art_indexes - shows ART index information
    fn execute_heliosdb_art_indexes(&self, storage: &StorageEngine) -> Result<Vec<Tuple>> {
        use crate::storage::ArtIndexType;

        let art_manager = storage.art_indexes();
        let indexes = art_manager.list_indexes();
        let mut results = Vec::new();

        for (index_name, table_name, index_type, columns) in indexes {
            let type_str = match index_type {
                ArtIndexType::PrimaryKey => "PRIMARY KEY",
                ArtIndexType::ForeignKey => "FOREIGN KEY",
                ArtIndexType::Unique => "UNIQUE",
                ArtIndexType::Manual => "MANUAL",
            };

            // Get individual index stats if available
            let (key_count, memory_bytes, constraint_checks, insertions, deletions) =
                if let Some(stats) = art_manager.index_stats(&index_name) {
                    (
                        stats.key_count as i64,
                        stats.memory_bytes as i64,
                        stats.lookup_count as i64,
                        stats.insert_count as i64,
                        stats.delete_count as i64,
                    )
                } else {
                    (0, 0, 0, 0, 0)
                };

            let columns_str = columns.join(", ");

            results.push(Tuple::new(vec![
                Value::String(index_name),
                Value::String(table_name),
                Value::String(type_str.to_string()),
                Value::String(columns_str),
                Value::Int8(key_count),
                Value::Int8(memory_bytes),
                Value::Int8(constraint_checks),
                Value::Int8(insertions),
                Value::Int8(deletions),
            ]));
        }

        Ok(results)
    }

    /// Execute heliosdb_simd_capabilities - shows CPU SIMD features
    fn execute_heliosdb_simd_capabilities(&self, _storage: &StorageEngine) -> Result<Vec<Tuple>> {
        use crate::storage::simd_capabilities;

        let caps = simd_capabilities();
        let mut results = Vec::new();

        // AVX-512
        results.push(Tuple::new(vec![
            Value::String("AVX-512".to_string()),
            Value::Boolean(caps.avx512f),
            Value::String("512-bit SIMD (16 floats/i32s per operation)".to_string()),
        ]));

        // AVX2
        results.push(Tuple::new(vec![
            Value::String("AVX2".to_string()),
            Value::Boolean(caps.avx2),
            Value::String("256-bit SIMD (8 floats/i32s per operation)".to_string()),
        ]));

        // SSE4.1
        results.push(Tuple::new(vec![
            Value::String("SSE4.1".to_string()),
            Value::Boolean(caps.sse41),
            Value::String("128-bit SIMD (4 floats/i32s per operation)".to_string()),
        ]));

        // Best available level
        let best = caps.best_level();
        let best_desc = match best {
            crate::storage::SimdLevel::Avx512 => "AVX-512 (fastest)",
            crate::storage::SimdLevel::Avx2 => "AVX2 (fast)",
            crate::storage::SimdLevel::Sse41 => "SSE4.1 (moderate)",
            crate::storage::SimdLevel::Scalar => "Scalar (no SIMD)",
        };
        results.push(Tuple::new(vec![
            Value::String("Best Level".to_string()),
            Value::Boolean(true),
            Value::String(best_desc.to_string()),
        ]));

        Ok(results)
    }

    fn data_type_to_oid(data_type: &DataType) -> i32 {
        match data_type {
            DataType::Boolean => 16,
            DataType::Int2 => 21,
            DataType::Int4 => 23,
            DataType::Int8 => 20,
            DataType::Float4 => 700,
            DataType::Float8 => 701,
            DataType::Text => 25,
            DataType::Varchar(_) => 1043,
            DataType::Timestamp => 1114,
            DataType::Timestamptz => 1184,
            DataType::Bytea => 17,
            DataType::Json => 114,
            DataType::Jsonb => 3802,
            _ => 0,
        }
    }
}

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

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

    #[test]
    fn test_registry_creation() {
        let registry = SystemViewRegistry::new();

        // Core views
        assert!(registry.is_system_view("pg_tables"));
        assert!(registry.is_system_view("pg_views"));
        assert!(registry.is_system_view("pg_indexes"));
        assert!(registry.is_system_view("pg_attribute"));
        assert!(registry.is_system_view("pg_database"));
        assert!(registry.is_system_view("pg_namespace"));
        assert!(registry.is_system_view("pg_class"));
        assert!(registry.is_system_view("pg_type"));

        // Session views
        assert!(registry.is_system_view("pg_stat_activity"));
        assert!(registry.is_system_view("pg_stat_database"));
        assert!(registry.is_system_view("pg_settings"));

        // Feature views
        assert!(registry.is_system_view("pg_branches"));
        assert!(registry.is_system_view("pg_matviews"));
        assert!(registry.is_system_view("pg_snapshots"));
        assert!(registry.is_system_view("pg_stat_ssl"));
        assert!(registry.is_system_view("pg_authid"));
        assert!(registry.is_system_view("pg_stat_optimizer"));
        assert!(registry.is_system_view("pg_compression_stats"));

        assert!(!registry.is_system_view("nonexistent"));
    }

    #[test]
    fn test_get_schema() {
        let registry = SystemViewRegistry::new();

        let schema = registry.get_schema("pg_tables").unwrap();
        assert_eq!(schema.columns.len(), 8);
        assert_eq!(schema.columns[0].name, "schemaname");
        assert_eq!(schema.columns[1].name, "tablename");
    }

    #[test]
    fn test_list_views_by_category() {
        let registry = SystemViewRegistry::new();

        let core_views = registry.list_views_by_category(ViewCategory::Core);
        assert!(core_views.len() >= 6);

        let session_views = registry.list_views_by_category(ViewCategory::Session);
        assert!(session_views.len() >= 1);

        let feature_views = registry.list_views_by_category(ViewCategory::Feature);
        assert!(feature_views.len() >= 3);
    }

    #[test]
    fn test_execute_pg_tables() {
        let config = Config::in_memory();
        let storage = StorageEngine::open_in_memory(&config).unwrap();

        // Create test tables
        use crate::Schema;
        let schema = Schema::new(vec![
            crate::Column::new("id", DataType::Int4),
        ]);
        storage.catalog().create_table("test_table", schema).unwrap();

        let registry = SystemViewRegistry::new();
        let results = registry.execute("pg_tables", &storage).unwrap();

        assert!(results.len() >= 1);
        assert_eq!(results[0].values.len(), 8);
    }

    #[test]
    fn test_execute_pg_database() {
        let config = Config::in_memory();
        let storage = StorageEngine::open_in_memory(&config).unwrap();

        let registry = SystemViewRegistry::new();
        let results = registry.execute("pg_database", &storage).unwrap();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].values.len(), 7);
    }

    #[test]
    fn test_execute_pg_settings() {
        let config = Config::in_memory();
        let storage = StorageEngine::open_in_memory(&config).unwrap();

        let registry = SystemViewRegistry::new();
        let results = registry.execute("pg_settings", &storage).unwrap();

        assert!(results.len() >= 4);
    }

    #[test]
    fn test_v2_3_views_registration() {
        let registry = SystemViewRegistry::new();

        // Check v2.3.0 views are registered
        assert!(registry.is_system_view("pg_stat_replication"));
        assert!(registry.is_system_view("pg_stat_progress_vacuum"));
        assert!(registry.is_system_view("helios_sync_status"));
        assert!(registry.is_system_view("helios_query_history"));
        assert!(registry.is_system_view("helios_table_memory_stats"));
        assert!(registry.is_system_view("helios_transaction_stats"));

        // HA Tier 1 views
        assert!(registry.is_system_view("helios_topology"));
        assert!(registry.is_system_view("helios_node_aliases"));
        assert!(registry.is_system_view("helios_ha_status"));
    }

    #[test]
    fn test_execute_pg_stat_replication() {
        let config = Config::in_memory();
        let storage = StorageEngine::open_in_memory(&config).unwrap();

        let registry = SystemViewRegistry::new();
        let results = registry.execute("pg_stat_replication", &storage).unwrap();

        // Should return empty when replication is disabled
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_execute_pg_stat_progress_vacuum() {
        let config = Config::in_memory();
        let storage = StorageEngine::open_in_memory(&config).unwrap();

        let registry = SystemViewRegistry::new();
        let results = registry.execute("pg_stat_progress_vacuum", &storage).unwrap();

        // Should return empty when no vacuum is in progress
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_execute_helios_sync_status() {
        let config = Config::in_memory();
        let storage = StorageEngine::open_in_memory(&config).unwrap();

        let registry = SystemViewRegistry::new();
        let results = registry.execute("helios_sync_status", &storage).unwrap();

        // Should return exactly 1 row with local node information
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].values.len(), 11);
    }

    #[test]
    fn test_execute_helios_query_history() {
        let config = Config::in_memory();
        let storage = StorageEngine::open_in_memory(&config).unwrap();

        let registry = SystemViewRegistry::new();
        let results = registry.execute("helios_query_history", &storage).unwrap();

        // Should return empty when no queries have been executed
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_execute_helios_table_memory_stats() {
        let config = Config::in_memory();
        let storage = StorageEngine::open_in_memory(&config).unwrap();

        // Create a test table
        let schema = crate::Schema::new(vec![
            crate::Column::new("id", DataType::Int4),
            crate::Column::new("name", DataType::Text),
        ]);
        storage.catalog().create_table("test_table", schema).unwrap();

        let registry = SystemViewRegistry::new();
        let results = registry.execute("helios_table_memory_stats", &storage).unwrap();

        // Should return stats for the created table
        assert!(results.len() >= 1);
        assert_eq!(results[0].values.len(), 10);
    }

    #[test]
    fn test_execute_helios_transaction_stats() {
        let config = Config::in_memory();
        let storage = StorageEngine::open_in_memory(&config).unwrap();

        let registry = SystemViewRegistry::new();
        let results = registry.execute("helios_transaction_stats", &storage).unwrap();

        // Should return empty when no transactions have been tracked
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_v2_3_views_schemas() {
        let registry = SystemViewRegistry::new();

        // Check pg_stat_replication schema
        let schema = registry.get_schema("pg_stat_replication").unwrap();
        assert_eq!(schema.columns.len(), 11);

        // Check pg_stat_progress_vacuum schema
        let schema = registry.get_schema("pg_stat_progress_vacuum").unwrap();
        assert_eq!(schema.columns.len(), 11);

        // Check helios_sync_status schema
        let schema = registry.get_schema("helios_sync_status").unwrap();
        assert_eq!(schema.columns.len(), 11);

        // Check helios_query_history schema
        let schema = registry.get_schema("helios_query_history").unwrap();
        assert_eq!(schema.columns.len(), 13);

        // Check helios_table_memory_stats schema
        let schema = registry.get_schema("helios_table_memory_stats").unwrap();
        assert_eq!(schema.columns.len(), 10);

        // Check helios_transaction_stats schema
        let schema = registry.get_schema("helios_transaction_stats").unwrap();
        assert_eq!(schema.columns.len(), 10);
    }

    #[test]
    fn test_v2_3_views_list() {
        let registry = SystemViewRegistry::new();
        let all_views = registry.list_views();

        // Check that all v2.3.0 views are in the list
        assert!(all_views.contains(&"pg_stat_replication"));
        assert!(all_views.contains(&"pg_stat_progress_vacuum"));
        assert!(all_views.contains(&"helios_sync_status"));
        assert!(all_views.contains(&"helios_query_history"));
        assert!(all_views.contains(&"helios_table_memory_stats"));
        assert!(all_views.contains(&"helios_transaction_stats"));

        // Check that previous versions' views are still present
        assert!(all_views.contains(&"pg_tables"));
        assert!(all_views.contains(&"pg_stat_activity"));
        assert!(all_views.contains(&"pg_compression_stats"));
    }
}