Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
-- Smoke regression for DBMS_STATS.
SELECT dbms_stats_get_version();
 dbms_stats_get_version 
------------------------
 0.5.1
(1 row)
 
-- == gather_table_stats tests ==
-- Test basic lifecycle: create table, insert data, gather stats, verify
CREATE TABLE dbms_stats_test(id INT, val TEXT);
-- Initially no statistics: reltuples should be 0 or negative (unknown)
SELECT reltuples, relpages
  FROM pg_class
 WHERE relname = 'dbms_stats_test';
 reltuples | relpages 
-----------+----------
        -1 |        0
(1 row)
 
INSERT INTO dbms_stats_test VALUES (1, 'a'), (2, 'b'), (3, 'c');
-- Call procedure with only ownname and tabname, all other args default to NULL
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_test');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
-- After gather, reltuples should reflect the row count
SELECT reltuples, relpages
  FROM pg_class
 WHERE relname = 'dbms_stats_test';
 reltuples | relpages 
-----------+----------
         3 |        1
(1 row)
 
-- pg_statistic should have entries for the table's columns
SELECT count(*) > 0 AS has_column_stats
  FROM pg_statistic
 WHERE starelid = 'dbms_stats_test'::regclass;
 has_column_stats 
------------------
 t
(1 row)
 
-- Test with explicit schema (ownname)
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_test');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
DROP TABLE dbms_stats_test;
-- == gather_index_stats tests ==
-- Test basic lifecycle: create table + index, insert data, gather index stats, verify
CREATE TABLE dbms_stats_idx_test(id INT, val TEXT);
CREATE INDEX dbms_stats_idx_test_idx ON dbms_stats_idx_test(id);
-- Initially no index statistics: reltuples should be 0 or negative (unknown)
SELECT reltuples, relpages
  FROM pg_class
 WHERE relname = 'dbms_stats_idx_test_idx';
 reltuples | relpages 
-----------+----------
         0 |        1
(1 row)
 
INSERT INTO dbms_stats_idx_test VALUES (1, 'a'), (2, 'b'), (3, 'c');
-- Call gather_index_stats with ownname and indname
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_idx_test_idx');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
 dbms_stats_gather_index_stats 
-------------------------------
 
(1 row)
 
-- After gather, index reltuples and relpages should reflect the data
SELECT reltuples, relpages
  FROM pg_class
 WHERE relname = 'dbms_stats_idx_test_idx';
 reltuples | relpages 
-----------+----------
         3 |        2
(1 row)
 
DROP TABLE dbms_stats_idx_test;
-- == privilege check tests ==
-- Ensure that an unprivileged role cannot gather stats (permission denied)
CREATE TABLE dbms_stats_priv_test(id INT);
INSERT INTO dbms_stats_priv_test VALUES (1), (2);
CREATE INDEX dbms_stats_priv_test_idx ON dbms_stats_priv_test(id);
CREATE ROLE dbms_stats_priv_test_user;
GRANT USAGE ON SCHEMA public TO dbms_stats_priv_test_user; -- not enough to analyze
SET ROLE dbms_stats_priv_test_user;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_priv_test');
ERROR:  TABLE "public"."dbms_stats_priv_test" does not exist or insufficient privileges
RESET ROLE;
SET ROLE dbms_stats_priv_test_user;
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_priv_test_idx');
ERROR:  INDEX "public"."dbms_stats_priv_test_idx" does not exist or insufficient privileges
RESET ROLE;
DROP TABLE dbms_stats_priv_test;
REVOKE ALL ON SCHEMA public FROM dbms_stats_priv_test_user;
DROP ROLE dbms_stats_priv_test_user;
-- == gather_schema_stats tests ==
-- Test basic lifecycle: two tables + a materialized view in public, gather stats, verify
CREATE TABLE dbms_stats_schema_test_1(id INT, val TEXT);
CREATE INDEX dbms_stats_schema_test_1_idx ON dbms_stats_schema_test_1(id);
CREATE TABLE dbms_stats_schema_test_2(id INT, val TEXT);
INSERT INTO dbms_stats_schema_test_1 VALUES (1, 'a'), (2, 'b'), (3, 'c');
INSERT INTO dbms_stats_schema_test_2 VALUES (1, 'x'), (2, 'y'), (3, 'z'), (4, 'w');
CREATE MATERIALIZED VIEW dbms_stats_schema_test_mv AS SELECT id, val FROM dbms_stats_schema_test_1;
-- Initially no statistics: reltuples should be -1 (unknown) for the tables
SELECT relname, reltuples
  FROM pg_class
 WHERE relname IN ('dbms_stats_schema_test_1', 'dbms_stats_schema_test_2', 'dbms_stats_schema_test_mv')
 ORDER BY relname;
          relname          | reltuples 
---------------------------+-----------
 dbms_stats_schema_test_1  |        -1
 dbms_stats_schema_test_2  |        -1
 dbms_stats_schema_test_mv |        -1
(3 rows)
 
-- Call with explicit schema
SELECT dbms_stats_gather_schema_stats('public');
 dbms_stats_gather_schema_stats 
--------------------------------
 
(1 row)
 
-- After gather, reltuples should reflect the row counts for tables, index, and matview
SELECT relname, reltuples
  FROM pg_class
 WHERE relname IN ('dbms_stats_schema_test_1', 'dbms_stats_schema_test_2',
                   'dbms_stats_schema_test_mv', 'dbms_stats_schema_test_1_idx')
 ORDER BY relname;
           relname            | reltuples 
------------------------------+-----------
 dbms_stats_schema_test_1     |         3
 dbms_stats_schema_test_1_idx |         3
 dbms_stats_schema_test_2     |         4
 dbms_stats_schema_test_mv    |         3
(4 rows)
 
-- Call with NULL ownname: should target the current session schema (public)
SELECT dbms_stats_gather_schema_stats(NULL);
 dbms_stats_gather_schema_stats 
--------------------------------
 
(1 row)
 
-- Empty schema: completes without error and returns NULL
CREATE SCHEMA dbms_stats_empty_schema;
SELECT dbms_stats_gather_schema_stats('dbms_stats_empty_schema');
 dbms_stats_gather_schema_stats 
--------------------------------
 
(1 row)
 
DROP SCHEMA dbms_stats_empty_schema;
DROP MATERIALIZED VIEW dbms_stats_schema_test_mv;
DROP TABLE dbms_stats_schema_test_1;
DROP TABLE dbms_stats_schema_test_2;
-- Schema privilege check: a role without USAGE on a schema receives an error
CREATE SCHEMA dbms_stats_secured_schema;
CREATE ROLE dbms_stats_schema_priv_user;
GRANT USAGE ON SCHEMA public TO dbms_stats_schema_priv_user;
SET ROLE dbms_stats_schema_priv_user;
SELECT dbms_stats_gather_schema_stats('dbms_stats_secured_schema');
ERROR:  Schema "dbms_stats_secured_schema" does not exist or insufficient privileges
RESET ROLE;
DROP SCHEMA dbms_stats_secured_schema;
REVOKE ALL ON SCHEMA public FROM dbms_stats_schema_priv_user;
DROP ROLE dbms_stats_schema_priv_user;
-- == get_table_stats tests ==
-- Test reading table-level statistics via the three Oracle overloads
CREATE TABLE dbms_stats_get_test(id INT, val TEXT);
INSERT INTO dbms_stats_get_test VALUES (1, 'a'), (2, 'b'), (3, 'c');
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_get_test');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
-- Overload 1: basic metrics (numrows, numblks, avgrlen)
SELECT numrows, numblks, avgrlen
  FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_get_test');
 numrows | numblks | avgrlen 
---------+---------+---------
 3       | 1       | 2731
(1 row)
 
-- Overload 2: In-Memory metrics are always NULL
SELECT numrows, numblks, avgrlen,
       im_imcu_count,
       im_block_count,
       scanrate
  FROM dbms_stats_get_table_stats_ovr2('public', 'dbms_stats_get_test');
 numrows | numblks | avgrlen | im_imcu_count | im_block_count | scanrate 
---------+---------+---------+---------------+----------------+----------
 3       | 1       | 2731    |               |                | 
(1 row)
 
-- Overload 3: cache metrics are always NULL
SELECT numrows, numblks, avgrlen,
       cachedblk,
       cachehit
  FROM dbms_stats_get_table_stats_ovr3('public', 'dbms_stats_get_test');
NOTICE:  User-defined statistics are not currently supported in this version
 numrows | numblks | avgrlen | cachedblk | cachehit 
---------+---------+---------+-----------+----------
 3       | 1       | 2731    |           | 
(1 row)
 
-- Table without gathered statistics: numrows and avgrlen are 0
CREATE TABLE dbms_stats_get_empty(id INT);
SELECT numrows, numblks, avgrlen
  FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_get_empty');
 numrows | numblks | avgrlen 
---------+---------+---------
 0       | 0       | 0
(1 row)
 
-- Missing table raises an error
SELECT * FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_no_such_table');
ERROR:  TABLE "public"."dbms_stats_no_such_table" does not exist or insufficient privileges
DROP TABLE dbms_stats_get_empty;
DROP TABLE dbms_stats_get_test;
-- Privilege check: a role without privileges on the table receives an error
CREATE TABLE dbms_stats_get_priv(id INT);
INSERT INTO dbms_stats_get_priv VALUES (1);
CREATE ROLE dbms_stats_get_priv_user;
GRANT USAGE ON SCHEMA public TO dbms_stats_get_priv_user;
SET ROLE dbms_stats_get_priv_user;
SELECT * FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_get_priv');
ERROR:  TABLE "public"."dbms_stats_get_priv" does not exist or insufficient privileges
RESET ROLE;
DROP TABLE dbms_stats_get_priv;
REVOKE ALL ON SCHEMA public FROM dbms_stats_get_priv_user;
DROP ROLE dbms_stats_get_priv_user;
-- == get_column_stats tests ==
-- Test reading column-level statistics via the two Oracle overloads
CREATE TABLE dbms_stats_col_test(id INT, cat INT, nullable TEXT);
INSERT INTO dbms_stats_col_test
  SELECT g, (g % 5) + 1, CASE WHEN g % 2 = 0 THEN NULL ELSE 'x' END
    FROM generate_series(1, 100) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_col_test');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
-- Overload 1: scalar metrics for a low-cardinality column (cat: 5 distinct, no nulls)
SELECT distcnt, density, nullcnt, avgclen
  FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'cat');
 distcnt | density | nullcnt | avgclen 
---------+---------+---------+---------
 5       | 0.2     | 0       | 4
(1 row)
 
-- Overload 1: nullable column (1 distinct non-null value, half the rows NULL)
SELECT distcnt, nullcnt
  FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'nullable');
 distcnt | nullcnt 
---------+---------
 1       | 50
(1 row)
 
-- Overload 1: unique column (negative stadistinct decoded into a row count)
SELECT distcnt AS distcnt_is_rowcount
  FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'id');
 distcnt_is_rowcount 
---------------------
 100
(1 row)
 
-- Overload 1: srec reflects the histogram for a high-cardinality column
SELECT srec
  FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'id');
                                                                                                                                                                                                                                                                                                                           srec                                                                                                                                                                                                                                                                                                                            
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {"min": "1", "max": "100", "histogram": ["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"]}
(1 row)
 
-- Overload 1: no histogram (MCV covers all values) -> srec is NULL
SELECT srec
  FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'cat');
 srec 
------
 
(1 row)
 
-- Overload 2: user-defined statistics triple is always NULL
SELECT ext_stats,
       stattypown,
       stattypname
  FROM dbms_stats_get_column_stats_ovr2('public', 'dbms_stats_col_test', 'cat');
 ext_stats | stattypown | stattypname 
-----------+------------+-------------
           |            | 
(1 row)
 
-- Never-analyzed column raises an ORA-20000-style error
CREATE TABLE dbms_stats_col_empty(id INT);
SELECT * FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_empty', 'id');
ERROR:  No statistics have been stored for requested object
-- Missing column raises an error
SELECT * FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'no_such_col');
ERROR:  Unable to get values for column "no_such_col"
DROP TABLE dbms_stats_col_empty;
-- Privilege check: a role without privileges on the table receives an error
CREATE ROLE dbms_stats_col_priv_user;
GRANT USAGE ON SCHEMA public TO dbms_stats_col_priv_user;
SET ROLE dbms_stats_col_priv_user;
SELECT * FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_col_test', 'cat');
ERROR:  TABLE "public"."dbms_stats_col_test" does not exist or insufficient privileges
RESET ROLE;
DROP TABLE dbms_stats_col_test;
REVOKE ALL ON SCHEMA public FROM dbms_stats_col_priv_user;
DROP ROLE dbms_stats_col_priv_user;
-- == partition statistics tests ==
-- Gather and read statistics for one partition without touching its siblings
CREATE TABLE dbms_stats_part_test(id INT, val TEXT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_part_test_p1 PARTITION OF dbms_stats_part_test
  FOR VALUES FROM (1) TO (101);
CREATE TABLE dbms_stats_part_test_p2 PARTITION OF dbms_stats_part_test
  FOR VALUES FROM (101) TO (201);
INSERT INTO dbms_stats_part_test
  SELECT g, 'x' FROM generate_series(1, 100) g;
INSERT INTO dbms_stats_part_test
  SELECT g, 'y' FROM generate_series(101, 200) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_part_test',
                                     'dbms_stats_part_test_p1');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
-- Only the requested partition gained statistics
SELECT relname, reltuples
  FROM pg_class
 WHERE relname IN ('dbms_stats_part_test',
                   'dbms_stats_part_test_p1',
                   'dbms_stats_part_test_p2')
 ORDER BY relname;
         relname         | reltuples 
-------------------------+-----------
 dbms_stats_part_test    |        -1
 dbms_stats_part_test_p1 |       100
 dbms_stats_part_test_p2 |        -1
(3 rows)
 
-- Overload 1 reads the requested partition, not the global table
SELECT (numrows)::int AS numrows,
       numblks,
       avgrlen,
       (numrows)::int = (SELECT reltuples::int FROM pg_class
                          WHERE relname = 'dbms_stats_part_test_p1')
         AS numrows_matches,
       numblks = (SELECT relpages FROM pg_class
                   WHERE relname = 'dbms_stats_part_test_p1')
         AS numblks_matches,
       avgrlen = trunc(avgrlen) AS avgrlen_is_integer
  FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_part_test',
                                       'dbms_stats_part_test_p1');
 numrows | numblks | avgrlen | numrows_matches | numblks_matches | avgrlen_is_integer 
---------+---------+---------+-----------------+-----------------+--------------------
     100 | 1       | 82      | t               | t               | t
(1 row)
 
-- Overloads 2 and 3 use the same partition target
SELECT (numrows)::int AS numrows,
       avgrlen,
       im_imcu_count,
       im_block_count,
       scanrate
  FROM dbms_stats_get_table_stats_ovr2('public', 'dbms_stats_part_test',
                                       'dbms_stats_part_test_p1');
 numrows | avgrlen | im_imcu_count | im_block_count | scanrate 
---------+---------+---------------+----------------+----------
     100 | 82      |               |                | 
(1 row)
 
SELECT (numrows)::int AS numrows,
       avgrlen,
       cachedblk,
       cachehit
  FROM dbms_stats_get_table_stats_ovr3('public', 'dbms_stats_part_test',
                                       'dbms_stats_part_test_p1');
NOTICE:  User-defined statistics are not currently supported in this version
 numrows | avgrlen | cachedblk | cachehit 
---------+---------+-----------+----------
     100 | 82      |           | 
(1 row)
 
-- Column statistics are read from the requested partition
SELECT distcnt,
       nullcnt,
       avgclen,
       distcnt = (SELECT count(*) FROM dbms_stats_part_test_p1)
         AS distcnt_matches
  FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_part_test',
                                        'id', 'dbms_stats_part_test_p1');
 distcnt | nullcnt | avgclen | distcnt_matches 
---------+---------+---------+-----------------
 100     | 0       | 4       | t
(1 row)
 
-- Global gathering still covers the whole table and differs from one partition
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_part_test');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT (numrows)::int AS partition_numrows,
       (SELECT reltuples::int FROM pg_class
         WHERE relname = 'dbms_stats_part_test') AS global_numrows
  FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_part_test',
                                       'dbms_stats_part_test_p1');
 partition_numrows | global_numrows 
-------------------+----------------
               100 |            200
(1 row)
 
-- == subpartition statistics tests ==
-- Nested partition names resolve to the matching subpartition
CREATE TABLE dbms_stats_subpart_test(id INT, val INT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_subpart_test_p1 PARTITION OF dbms_stats_subpart_test
  FOR VALUES FROM (1) TO (101) PARTITION BY RANGE (val);
CREATE TABLE dbms_stats_subpart_test_p1_s1 PARTITION OF dbms_stats_subpart_test_p1
  FOR VALUES FROM (1) TO (51);
CREATE TABLE dbms_stats_subpart_test_p1_s2 PARTITION OF dbms_stats_subpart_test_p1
  FOR VALUES FROM (51) TO (101);
INSERT INTO dbms_stats_subpart_test
  SELECT g, g FROM generate_series(1, 50) g;
INSERT INTO dbms_stats_subpart_test
  SELECT g, g FROM generate_series(51, 100) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_subpart_test',
                                     'dbms_stats_subpart_test_p1_s1');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples
  FROM pg_class
 WHERE relname IN ('dbms_stats_subpart_test_p1_s1',
                   'dbms_stats_subpart_test_p1_s2')
 ORDER BY relname;
            relname            | reltuples 
-------------------------------+-----------
 dbms_stats_subpart_test_p1_s1 |        50
 dbms_stats_subpart_test_p1_s2 |        -1
(2 rows)
 
SELECT (numrows)::int AS subpartition_numrows, avgrlen
  FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_subpart_test',
                                       'dbms_stats_subpart_test_p1_s1');
 subpartition_numrows | avgrlen 
----------------------+---------
                   50 | 164
(1 row)
 
SELECT distcnt
  FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_subpart_test',
                                        'id',
                                        'dbms_stats_subpart_test_p1_s1');
 distcnt 
---------
 50
(1 row)
 
-- Gathering an intermediate partition follows ANALYZE recursion
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_subpart_test',
                                     'dbms_stats_subpart_test_p1');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples
  FROM pg_class
 WHERE relname IN ('dbms_stats_subpart_test_p1_s1',
                   'dbms_stats_subpart_test_p1_s2')
 ORDER BY relname;
            relname            | reltuples 
-------------------------------+-----------
 dbms_stats_subpart_test_p1_s1 |        50
 dbms_stats_subpart_test_p1_s2 |        50
(2 rows)
 
-- == partition error handling ==
CREATE TABLE dbms_stats_part_plain(id INT);
INSERT INTO dbms_stats_part_plain VALUES (1);
-- Missing partition
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_part_test',
                                     'dbms_stats_no_such_partition');
ERROR:  dbms_stats_no_such_partition invalid partition name
SELECT * FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_part_test',
                                              'dbms_stats_no_such_partition');
ERROR:  dbms_stats_no_such_partition invalid partition name
-- Partition of another table
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_part_test',
                                     'dbms_stats_subpart_test_p1');
ERROR:  dbms_stats_subpart_test_p1 invalid partition name
SELECT * FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_part_test',
                                              'dbms_stats_subpart_test_p1');
ERROR:  dbms_stats_subpart_test_p1 invalid partition name
SELECT * FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_part_test',
                                               'id',
                                               'dbms_stats_subpart_test_p1');
ERROR:  dbms_stats_subpart_test_p1 invalid partition name
-- Partition name on a non-partitioned table
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_part_plain',
                                     'dbms_stats_part_test_p1');
ERROR:  dbms_stats_part_test_p1 invalid partition name
SELECT * FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_part_plain',
                                              'dbms_stats_part_test_p1');
ERROR:  dbms_stats_part_test_p1 invalid partition name
DROP TABLE dbms_stats_part_plain;
DROP TABLE dbms_stats_subpart_test;
DROP TABLE dbms_stats_part_test;
-- == granularity tests: ALL, AUTO, DEFAULT ==
-- AUTO, ALL, and DEFAULT all mean "native whole-table ANALYZE": PostgreSQL
-- recursively analyzes the parent, every partition, and every subpartition.
CREATE TABLE dbms_stats_gran_all(id INT, val TEXT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_gran_all_p1 PARTITION OF dbms_stats_gran_all
  FOR VALUES FROM (1) TO (101);
CREATE TABLE dbms_stats_gran_all_p2 PARTITION OF dbms_stats_gran_all
  FOR VALUES FROM (101) TO (201);
INSERT INTO dbms_stats_gran_all SELECT g, 'x' FROM generate_series(1, 100) g;
INSERT INTO dbms_stats_gran_all SELECT g, 'y' FROM generate_series(101, 200) g;
-- Initial layout: 100 rows in p1 and 100 in p2. ALL analyzes the parent
-- recursively, so the root becomes 200 and both partitions become 100.
-- Every analyzed relation has one data page (relpages = 1). The partitioned
-- parent has relpages = -1 because it has no storage; ANALYZE only maintains
-- its aggregated reltuples.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_all',
                                     granularity => 'ALL');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_all',
                   'dbms_stats_gran_all_p1',
                   'dbms_stats_gran_all_p2')
 ORDER BY relname;
        relname         | reltuples | relpages 
------------------------+-----------+----------
 dbms_stats_gran_all    |       200 |       -1
 dbms_stats_gran_all_p1 |       100 |        1
 dbms_stats_gran_all_p2 |       100 |        1
(3 rows)
 
-- Add 50 rows to p1 and 25 to p2. AUTO must behave like ALL and refresh the
-- whole hierarchy: root becomes 275, p1 150, p2 125.
INSERT INTO dbms_stats_gran_all SELECT g, 'z' FROM generate_series(1, 50) g;
INSERT INTO dbms_stats_gran_all SELECT g, 'w' FROM generate_series(101, 125) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_all',
                                     granularity => 'AUTO');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_all',
                   'dbms_stats_gran_all_p1',
                   'dbms_stats_gran_all_p2')
 ORDER BY relname;
        relname         | reltuples | relpages 
------------------------+-----------+----------
 dbms_stats_gran_all    |       275 |       -1
 dbms_stats_gran_all_p1 |       150 |        1
 dbms_stats_gran_all_p2 |       125 |        1
(3 rows)
 
-- Add 10 rows to p1 and 5 to p2. DEFAULT is an alias for AUTO/ALL:
-- root becomes 290, p1 160, p2 130.
INSERT INTO dbms_stats_gran_all SELECT g, 'v' FROM generate_series(1, 10) g;
INSERT INTO dbms_stats_gran_all SELECT g, 'u' FROM generate_series(101, 105) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_all',
                                     granularity => 'DEFAULT');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_all',
                   'dbms_stats_gran_all_p1',
                   'dbms_stats_gran_all_p2')
 ORDER BY relname;
        relname         | reltuples | relpages 
------------------------+-----------+----------
 dbms_stats_gran_all    |       290 |       -1
 dbms_stats_gran_all_p1 |       160 |        1
 dbms_stats_gran_all_p2 |       130 |        1
(3 rows)
 
-- == granularity tests: PARTITION ==
-- PARTITION without partname selects only the direct partitions. The parent's
-- global statistics must stay untouched, so the root remains -1.
CREATE TABLE dbms_stats_gran_part(id INT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_gran_part_p1 PARTITION OF dbms_stats_gran_part
  FOR VALUES FROM (1) TO (101);
CREATE TABLE dbms_stats_gran_part_p2 PARTITION OF dbms_stats_gran_part
  FOR VALUES FROM (101) TO (201);
INSERT INTO dbms_stats_gran_part SELECT g FROM generate_series(1, 100) g;
INSERT INTO dbms_stats_gran_part SELECT g FROM generate_series(101, 200) g;
-- Initial layout: p1=100 rows, p2=100 rows. The gather analyzes p1 and p2
-- only: their reltuples become 100, while the root stays -1.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_part',
                                     granularity => 'PARTITION');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_part',
                   'dbms_stats_gran_part_p1',
                   'dbms_stats_gran_part_p2')
 ORDER BY relname;
         relname         | reltuples | relpages 
-------------------------+-----------+----------
 dbms_stats_gran_part    |        -1 |        0
 dbms_stats_gran_part_p1 |       100 |        1
 dbms_stats_gran_part_p2 |       100 |        1
(3 rows)
 
-- Add 50 rows to p1 and 20 to p2. With partname, PARTITION targets only the
-- named partition: p1 becomes 150, p2 keeps its previous 100, root stays -1.
INSERT INTO dbms_stats_gran_part SELECT g FROM generate_series(1, 50) g;
INSERT INTO dbms_stats_gran_part SELECT g FROM generate_series(101, 120) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_part',
                                     granularity => 'PARTITION',
                                     partname => 'dbms_stats_gran_part_p1');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_part',
                   'dbms_stats_gran_part_p1',
                   'dbms_stats_gran_part_p2')
 ORDER BY relname;
         relname         | reltuples | relpages 
-------------------------+-----------+----------
 dbms_stats_gran_part    |        -1 |        0
 dbms_stats_gran_part_p1 |       150 |        1
 dbms_stats_gran_part_p2 |       100 |        1
(3 rows)
 
-- Add 10 rows to both partitions. Lowercase 'partition' is accepted and,
-- without partname, refreshes both direct partitions: p1=160, p2=130, root=-1.
INSERT INTO dbms_stats_gran_part SELECT g FROM generate_series(1, 10) g;
INSERT INTO dbms_stats_gran_part SELECT g FROM generate_series(101, 110) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_part',
                                     granularity => 'partition');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_part',
                   'dbms_stats_gran_part_p1',
                   'dbms_stats_gran_part_p2')
 ORDER BY relname;
         relname         | reltuples | relpages 
-------------------------+-----------+----------
 dbms_stats_gran_part    |        -1 |        0
 dbms_stats_gran_part_p1 |       160 |        1
 dbms_stats_gran_part_p2 |       130 |        1
(3 rows)
 
-- == granularity tests: SUBPARTITION without subpartitions ==
-- This table has two direct partitions but no second-level subpartitions.
-- SUBPARTITION therefore resolves to an empty target list: the procedure
-- succeeds without running ANALYZE, so every reltuples stays -1 and relpages 0.
CREATE TABLE dbms_stats_gran_nosub(id INT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_gran_nosub_p1 PARTITION OF dbms_stats_gran_nosub
  FOR VALUES FROM (1) TO (101);
CREATE TABLE dbms_stats_gran_nosub_p2 PARTITION OF dbms_stats_gran_nosub
  FOR VALUES FROM (101) TO (201);
INSERT INTO dbms_stats_gran_nosub SELECT g FROM generate_series(1, 200) g;
-- The call is intentionally a no-op: no target exists at the subpartition level.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_nosub',
                                     granularity => 'SUBPARTITION');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_nosub',
                   'dbms_stats_gran_nosub_p1',
                   'dbms_stats_gran_nosub_p2')
 ORDER BY relname;
         relname          | reltuples | relpages 
--------------------------+-----------+----------
 dbms_stats_gran_nosub    |        -1 |        0
 dbms_stats_gran_nosub_p1 |        -1 |        0
 dbms_stats_gran_nosub_p2 |        -1 |        0
(3 rows)
 
-- == granularity tests: SUBPARTITION ==
-- Composite table: p1/p2 each contain two leaf subpartitions. The inserts put
-- 25 rows in every leaf, so p1/p2 hold 50 rows each and the root holds 100.
-- SUBPARTITION without partname analyzes only the second-level leaves:
-- root, p1, and p2 stay -1; all four leaves become 25.
CREATE TABLE dbms_stats_gran_sub(id INT, val INT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_gran_sub_p1 PARTITION OF dbms_stats_gran_sub
  FOR VALUES FROM (1) TO (101) PARTITION BY RANGE (val);
CREATE TABLE dbms_stats_gran_sub_p1_s1 PARTITION OF dbms_stats_gran_sub_p1
  FOR VALUES FROM (1) TO (51);
CREATE TABLE dbms_stats_gran_sub_p1_s2 PARTITION OF dbms_stats_gran_sub_p1
  FOR VALUES FROM (51) TO (101);
CREATE TABLE dbms_stats_gran_sub_p2 PARTITION OF dbms_stats_gran_sub
  FOR VALUES FROM (101) TO (201) PARTITION BY RANGE (val);
CREATE TABLE dbms_stats_gran_sub_p2_s1 PARTITION OF dbms_stats_gran_sub_p2
  FOR VALUES FROM (1) TO (51);
CREATE TABLE dbms_stats_gran_sub_p2_s2 PARTITION OF dbms_stats_gran_sub_p2
  FOR VALUES FROM (51) TO (101);
INSERT INTO dbms_stats_gran_sub SELECT g, g FROM generate_series(1, 25) g;
INSERT INTO dbms_stats_gran_sub SELECT g, g + 50 FROM generate_series(26, 50) g;
INSERT INTO dbms_stats_gran_sub SELECT g, g - 100 FROM generate_series(101, 125) g;
INSERT INTO dbms_stats_gran_sub SELECT g, g - 50 FROM generate_series(126, 150) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_sub',
                                     granularity => 'SUBPARTITION');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_sub',
                   'dbms_stats_gran_sub_p1',
                   'dbms_stats_gran_sub_p2',
                   'dbms_stats_gran_sub_p1_s1',
                   'dbms_stats_gran_sub_p1_s2',
                   'dbms_stats_gran_sub_p2_s1',
                   'dbms_stats_gran_sub_p2_s2')
 ORDER BY relname;
          relname          | reltuples | relpages 
---------------------------+-----------+----------
 dbms_stats_gran_sub       |        -1 |        0
 dbms_stats_gran_sub_p1    |        -1 |        0
 dbms_stats_gran_sub_p1_s1 |        25 |        1
 dbms_stats_gran_sub_p1_s2 |        25 |        1
 dbms_stats_gran_sub_p2    |        -1 |        0
 dbms_stats_gran_sub_p2_s1 |        25 |        1
 dbms_stats_gran_sub_p2_s2 |        25 |        1
(7 rows)
 
-- Add 5 rows to p1's first leaf and 5 to p2's first leaf. SUBPARTITION with
-- partname p1 targets only subpartitions of p1: p1_s1 becomes 30, p1_s2 stays
-- 25, and both p2 leaves stay 25. p1 and the root stay unanalyzed.
INSERT INTO dbms_stats_gran_sub SELECT g, g FROM generate_series(1, 5) g;
INSERT INTO dbms_stats_gran_sub SELECT g, g - 100 FROM generate_series(101, 105) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_sub',
                                     granularity => 'SUBPARTITION',
                                     partname => 'dbms_stats_gran_sub_p1');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_sub',
                   'dbms_stats_gran_sub_p1',
                   'dbms_stats_gran_sub_p2',
                   'dbms_stats_gran_sub_p1_s1',
                   'dbms_stats_gran_sub_p1_s2',
                   'dbms_stats_gran_sub_p2_s1',
                   'dbms_stats_gran_sub_p2_s2')
 ORDER BY relname;
          relname          | reltuples | relpages 
---------------------------+-----------+----------
 dbms_stats_gran_sub       |        -1 |        0
 dbms_stats_gran_sub_p1    |        -1 |        0
 dbms_stats_gran_sub_p1_s1 |        30 |        1
 dbms_stats_gran_sub_p1_s2 |        25 |        1
 dbms_stats_gran_sub_p2    |        -1 |        0
 dbms_stats_gran_sub_p2_s1 |        25 |        1
 dbms_stats_gran_sub_p2_s2 |        25 |        1
(7 rows)
 
-- == granularity tests: GLOBAL AND PARTITION ==
-- Without partname this level analyzes the whole table, so global and
-- partition statistics are refreshed together.
CREATE TABLE dbms_stats_gran_gp(id INT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_gran_gp_p1 PARTITION OF dbms_stats_gran_gp
  FOR VALUES FROM (1) TO (101);
CREATE TABLE dbms_stats_gran_gp_p2 PARTITION OF dbms_stats_gran_gp
  FOR VALUES FROM (101) TO (201);
INSERT INTO dbms_stats_gran_gp SELECT g FROM generate_series(1, 100) g;
INSERT INTO dbms_stats_gran_gp SELECT g FROM generate_series(101, 200) g;
-- Initial layout: 100 rows per partition. Result: root=200, p1=100, p2=100.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_gp',
                                     granularity => 'GLOBAL AND PARTITION');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_gp',
                   'dbms_stats_gran_gp_p1',
                   'dbms_stats_gran_gp_p2')
 ORDER BY relname;
        relname        | reltuples | relpages 
-----------------------+-----------+----------
 dbms_stats_gran_gp    |       200 |       -1
 dbms_stats_gran_gp_p1 |       100 |        1
 dbms_stats_gran_gp_p2 |       100 |        1
(3 rows)
 
-- Add 30 rows to p1 and 40 to p2. With partname the behavior is PARTITION:
-- only p1 is analyzed, so p1=130; p2 keeps 100 and the root keeps 200.
INSERT INTO dbms_stats_gran_gp SELECT g FROM generate_series(1, 30) g;
INSERT INTO dbms_stats_gran_gp SELECT g FROM generate_series(101, 140) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_gp',
                                     granularity => 'GLOBAL AND PARTITION',
                                     partname => 'dbms_stats_gran_gp_p1');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_gp',
                   'dbms_stats_gran_gp_p1',
                   'dbms_stats_gran_gp_p2')
 ORDER BY relname;
        relname        | reltuples | relpages 
-----------------------+-----------+----------
 dbms_stats_gran_gp    |       200 |       -1
 dbms_stats_gran_gp_p1 |       130 |        1
 dbms_stats_gran_gp_p2 |       100 |        1
(3 rows)
 
-- == granularity tests: non-partitioned fallback ==
-- Oracle ignores granularity for non-partitioned tables, so every supported
-- value degrades to a normal whole-table ANALYZE.
CREATE TABLE dbms_stats_gran_plain(id INT);
INSERT INTO dbms_stats_gran_plain SELECT g FROM generate_series(1, 10) g;
-- PARTITION on a plain table must analyze the table: reltuples=10.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_plain',
                                     granularity => 'PARTITION');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT reltuples, relpages FROM pg_class WHERE relname = 'dbms_stats_gran_plain';
 reltuples | relpages 
-----------+----------
        10 |        1
(1 row)
 
-- Add 5 rows; SUBPARTITION must also analyze the whole table: reltuples=15.
INSERT INTO dbms_stats_gran_plain SELECT g FROM generate_series(1, 5) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_plain',
                                     granularity => 'SUBPARTITION');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT reltuples, relpages FROM pg_class WHERE relname = 'dbms_stats_gran_plain';
 reltuples | relpages 
-----------+----------
        15 |        1
(1 row)
 
-- == granularity error handling ==
-- Every call in this section must fail before ANALYZE, so the table and its
-- partitions stay unanalyzed. The final query proves reltuples are still -1.
CREATE TABLE dbms_stats_gran_err(id INT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_gran_err_p1 PARTITION OF dbms_stats_gran_err
  FOR VALUES FROM (1) TO (101);
CREATE TABLE dbms_stats_gran_err_p2 PARTITION OF dbms_stats_gran_err
  FOR VALUES FROM (101) TO (201);
INSERT INTO dbms_stats_gran_err SELECT g FROM generate_series(1, 200) g;
-- GLOBAL and APPROX_GLOBAL require core support; an unknown keyword is
-- invalid; and a nonexistent partname is rejected by partition resolution.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_err',
                                     granularity => 'GLOBAL');
ERROR:  granularity "GLOBAL" is not supported in this version
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_err',
                                     granularity => 'APPROX_GLOBAL');
ERROR:  granularity "APPROX_GLOBAL" is not supported in this version
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_err',
                                     granularity => 'NONSENSE');
ERROR:  Illegal granularity "NONSENSE": must be AUTO | ALL | GLOBAL | PARTITION | SUBPARTITION | GLOBAL AND PARTITION | APPROX_GLOBAL AND PARTITION
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_err',
                                     granularity => 'PARTITION',
                                     partname => 'dbms_stats_no_such_partition');
ERROR:  dbms_stats_no_such_partition invalid partition name
-- The role may analyze the parent (MAINTAIN on the parent) but not its
-- partitions. PARTITION selects the partitions, so the preflight privilege
-- check fails before any target is analyzed.
CREATE ROLE dbms_stats_gran_priv_user;
GRANT USAGE ON SCHEMA public TO dbms_stats_gran_priv_user;
GRANT MAINTAIN ON dbms_stats_gran_err TO dbms_stats_gran_priv_user;
SET ROLE dbms_stats_gran_priv_user;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_gran_err',
                                     granularity => 'PARTITION');
ERROR:  TABLE "public"."dbms_stats_gran_err_p1" does not exist or insufficient privileges
RESET ROLE;
REVOKE ALL ON dbms_stats_gran_err FROM dbms_stats_gran_priv_user;
REVOKE ALL ON SCHEMA public FROM dbms_stats_gran_priv_user;
DROP ROLE dbms_stats_gran_priv_user;
-- Confirm none of the failed calls collected statistics.
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_err',
                   'dbms_stats_gran_err_p1',
                   'dbms_stats_gran_err_p2')
 ORDER BY relname;
        relname         | reltuples | relpages 
------------------------+-----------+----------
 dbms_stats_gran_err    |        -1 |        0
 dbms_stats_gran_err_p1 |        -1 |        0
 dbms_stats_gran_err_p2 |        -1 |        0
(3 rows)
 
-- == schema granularity tests ==
-- A dedicated schema isolates schema-level gathering from public. plain holds
-- 10 rows. comp is composite: p1/p2 each hold 50 rows in two leaves of 25.
CREATE SCHEMA dbms_stats_gran_schema;
CREATE TABLE dbms_stats_gran_schema.plain(id INT);
INSERT INTO dbms_stats_gran_schema.plain SELECT g FROM generate_series(1, 10) g;
CREATE TABLE dbms_stats_gran_schema.comp(id INT, val INT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_gran_schema.comp_p1 PARTITION OF dbms_stats_gran_schema.comp
  FOR VALUES FROM (1) TO (101) PARTITION BY RANGE (val);
CREATE TABLE dbms_stats_gran_schema.comp_p1_s1 PARTITION OF dbms_stats_gran_schema.comp_p1
  FOR VALUES FROM (1) TO (51);
CREATE TABLE dbms_stats_gran_schema.comp_p1_s2 PARTITION OF dbms_stats_gran_schema.comp_p1
  FOR VALUES FROM (51) TO (101);
CREATE TABLE dbms_stats_gran_schema.comp_p2 PARTITION OF dbms_stats_gran_schema.comp
  FOR VALUES FROM (101) TO (201) PARTITION BY RANGE (val);
CREATE TABLE dbms_stats_gran_schema.comp_p2_s1 PARTITION OF dbms_stats_gran_schema.comp_p2
  FOR VALUES FROM (1) TO (51);
CREATE TABLE dbms_stats_gran_schema.comp_p2_s2 PARTITION OF dbms_stats_gran_schema.comp_p2
  FOR VALUES FROM (51) TO (101);
INSERT INTO dbms_stats_gran_schema.comp
  SELECT g, g FROM generate_series(1, 25) g;
INSERT INTO dbms_stats_gran_schema.comp
  SELECT g, g + 50 FROM generate_series(26, 50) g;
INSERT INTO dbms_stats_gran_schema.comp
  SELECT g, g - 100 FROM generate_series(101, 125) g;
INSERT INTO dbms_stats_gran_schema.comp
  SELECT g, g - 50 FROM generate_series(126, 150) g;
-- ALL keeps the full relation list, so comp and all descendants are analyzed:
-- comp=100, p1/p2=50, every leaf=25, plain=10.
SELECT dbms_stats_gather_schema_stats('dbms_stats_gran_schema',
                                      granularity => 'ALL');
 dbms_stats_gather_schema_stats 
--------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class c
  JOIN pg_namespace n ON n.oid = c.relnamespace
 WHERE n.nspname = 'dbms_stats_gran_schema'
 ORDER BY relname;
  relname   | reltuples | relpages 
------------+-----------+----------
 comp       |       100 |       -1
 comp_p1    |        50 |       -1
 comp_p1_s1 |        25 |        1
 comp_p1_s2 |        25 |        1
 comp_p2    |        50 |       -1
 comp_p2_s1 |        25 |        1
 comp_p2_s2 |        25 |        1
 plain      |        10 |        1
(8 rows)
 
-- Add 10 rows to comp_p1_s1, 10 to comp_p2_s1, and 10 to plain. PARTITION drops
-- the root, analyzes depth-1 partitions and standalone tables: p1/p2=60,
-- plain=20, root comp keeps 100. Native recursion into p1/p2 also refreshes
-- their leaves, so p1_s1/p2_s1 become 35; this is documented PostgreSQL
-- behavior, not a separate target chosen by the procedure.
INSERT INTO dbms_stats_gran_schema.comp
  SELECT g, g FROM generate_series(1, 10) g;
INSERT INTO dbms_stats_gran_schema.comp
  SELECT g, g - 100 FROM generate_series(101, 110) g;
INSERT INTO dbms_stats_gran_schema.plain
  SELECT g FROM generate_series(1, 10) g;
SELECT dbms_stats_gather_schema_stats('dbms_stats_gran_schema',
                                      granularity => 'PARTITION');
 dbms_stats_gather_schema_stats 
--------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class c
  JOIN pg_namespace n ON n.oid = c.relnamespace
 WHERE n.nspname = 'dbms_stats_gran_schema'
 ORDER BY relname;
  relname   | reltuples | relpages 
------------+-----------+----------
 comp       |       100 |       -1
 comp_p1    |        60 |       -1
 comp_p1_s1 |        35 |        1
 comp_p1_s2 |        25 |        1
 comp_p2    |        60 |       -1
 comp_p2_s1 |        35 |        1
 comp_p2_s2 |        25 |        1
 plain      |        20 |        1
(8 rows)
 
-- Add 5 rows to comp_p1_s2, 5 to comp_p2_s2, and 5 to plain. SUBPARTITION
-- analyzes depth-2 leaves and standalone tables: p1_s2/p2_s2=30, plain=25,
-- while comp, p1, and p2 keep their previous values.
INSERT INTO dbms_stats_gran_schema.comp
  SELECT g, g + 50 FROM generate_series(26, 30) g;
INSERT INTO dbms_stats_gran_schema.comp
  SELECT g, g - 50 FROM generate_series(126, 130) g;
INSERT INTO dbms_stats_gran_schema.plain
  SELECT g FROM generate_series(1, 5) g;
SELECT dbms_stats_gather_schema_stats('dbms_stats_gran_schema',
                                      granularity => 'SUBPARTITION');
 dbms_stats_gather_schema_stats 
--------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class c
  JOIN pg_namespace n ON n.oid = c.relnamespace
 WHERE n.nspname = 'dbms_stats_gran_schema'
 ORDER BY relname;
  relname   | reltuples | relpages 
------------+-----------+----------
 comp       |       100 |       -1
 comp_p1    |        60 |       -1
 comp_p1_s1 |        35 |        1
 comp_p1_s2 |        30 |        1
 comp_p2    |        60 |       -1
 comp_p2_s1 |        35 |        1
 comp_p2_s2 |        30 |        1
 plain      |        25 |        1
(8 rows)
 
-- Unsupported granularity must fail before any relation is analyzed; the
-- snapshot below must be identical to the previous one.
SELECT dbms_stats_gather_schema_stats('dbms_stats_gran_schema',
                                      granularity => 'GLOBAL');
ERROR:  granularity "GLOBAL" is not supported in this version
SELECT relname, reltuples, relpages
  FROM pg_class c
  JOIN pg_namespace n ON n.oid = c.relnamespace
 WHERE n.nspname = 'dbms_stats_gran_schema'
 ORDER BY relname;
  relname   | reltuples | relpages 
------------+-----------+----------
 comp       |       100 |       -1
 comp_p1    |        60 |       -1
 comp_p1_s1 |        35 |        1
 comp_p1_s2 |        30 |        1
 comp_p2    |        60 |       -1
 comp_p2_s1 |        35 |        1
 comp_p2_s2 |        30 |        1
 plain      |        25 |        1
(8 rows)
 
-- == index granularity and partname tests ==
-- Index statistics are gathered by analyzing tables, so all indexes on a
-- selected table are refreshed. A partitioned index root reports reltuples=0;
-- unanalyzed child indexes also report 0 (not -1).
CREATE TABLE dbms_stats_gran_idx(id INT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_gran_idx_p1 PARTITION OF dbms_stats_gran_idx
  FOR VALUES FROM (1) TO (101);
CREATE TABLE dbms_stats_gran_idx_p2 PARTITION OF dbms_stats_gran_idx
  FOR VALUES FROM (101) TO (201);
CREATE INDEX dbms_stats_gran_idx_idx ON dbms_stats_gran_idx(id);
INSERT INTO dbms_stats_gran_idx SELECT g FROM generate_series(1, 100) g;
INSERT INTO dbms_stats_gran_idx SELECT g FROM generate_series(101, 200) g;
-- Nothing has been analyzed yet: tables and partitions are -1, index root 0.
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_idx',
                   'dbms_stats_gran_idx_p1',
                   'dbms_stats_gran_idx_p2',
                   'dbms_stats_gran_idx_idx')
 ORDER BY relname;
         relname         | reltuples | relpages 
-------------------------+-----------+----------
 dbms_stats_gran_idx     |        -1 |        0
 dbms_stats_gran_idx_idx |         0 |        0
 dbms_stats_gran_idx_p1  |        -1 |        0
 dbms_stats_gran_idx_p2  |        -1 |        0
(4 rows)
 
-- partname selects only p1's table. p1 becomes 100 and its child index 100.
-- Sibling p2 stays -1, its child index stays 0, and the index root stays 0.
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_idx',
                                     partname => 'dbms_stats_gran_idx_p1');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
 dbms_stats_gather_index_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_idx',
                   'dbms_stats_gran_idx_p1',
                   'dbms_stats_gran_idx_p2',
                   'dbms_stats_gran_idx_idx')
 ORDER BY relname;
         relname         | reltuples | relpages 
-------------------------+-----------+----------
 dbms_stats_gran_idx     |        -1 |        0
 dbms_stats_gran_idx_idx |         0 |        0
 dbms_stats_gran_idx_p1  |       100 |        1
 dbms_stats_gran_idx_p2  |        -1 |        0
(4 rows)
 
SELECT c.relname, c.reltuples::int AS reltuples
  FROM pg_class c
  JOIN pg_inherits i ON i.inhrelid = c.oid
 WHERE i.inhparent = 'dbms_stats_gran_idx_idx'::regclass
 ORDER BY c.relname;
            relname            | reltuples 
-------------------------------+-----------
 dbms_stats_gran_idx_p1_id_idx |       100
 dbms_stats_gran_idx_p2_id_idx |         0
(2 rows)
 
-- Add 50 rows to each partition. Lowercase 'partition' analyzes both direct
-- partitions: p1/p2=150 and both child indexes=150. The table root and the
-- partitioned index root keep their previous statistics.
INSERT INTO dbms_stats_gran_idx SELECT g FROM generate_series(1, 50) g;
INSERT INTO dbms_stats_gran_idx SELECT g FROM generate_series(101, 150) g;
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_idx',
                                     granularity => 'partition');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
 dbms_stats_gather_index_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_idx',
                   'dbms_stats_gran_idx_p1',
                   'dbms_stats_gran_idx_p2',
                   'dbms_stats_gran_idx_idx')
 ORDER BY relname;
         relname         | reltuples | relpages 
-------------------------+-----------+----------
 dbms_stats_gran_idx     |        -1 |        0
 dbms_stats_gran_idx_idx |         0 |        0
 dbms_stats_gran_idx_p1  |       150 |        1
 dbms_stats_gran_idx_p2  |       150 |        1
(4 rows)
 
SELECT c.relname, c.reltuples::int AS reltuples
  FROM pg_class c
  JOIN pg_inherits i ON i.inhrelid = c.oid
 WHERE i.inhparent = 'dbms_stats_gran_idx_idx'::regclass
 ORDER BY c.relname;
            relname            | reltuples 
-------------------------------+-----------
 dbms_stats_gran_idx_p1_id_idx |       150
 dbms_stats_gran_idx_p2_id_idx |       150
(2 rows)
 
-- There are no second-level subpartitions, so SUBPARTITION selects an empty
-- target set: the call succeeds without ANALYZE and every statistic below is
-- unchanged from the previous result.
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_idx',
                                     granularity => 'SUBPARTITION');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
 dbms_stats_gather_index_stats 
-------------------------------
 
(1 row)
 
SELECT relname, reltuples, relpages
  FROM pg_class
 WHERE relname IN ('dbms_stats_gran_idx',
                   'dbms_stats_gran_idx_p1',
                   'dbms_stats_gran_idx_p2',
                   'dbms_stats_gran_idx_idx')
 ORDER BY relname;
         relname         | reltuples | relpages 
-------------------------+-----------+----------
 dbms_stats_gran_idx     |        -1 |        0
 dbms_stats_gran_idx_idx |         0 |        0
 dbms_stats_gran_idx_p1  |       150 |        1
 dbms_stats_gran_idx_p2  |       150 |        1
(4 rows)
 
-- Composite table with a partitioned index. Each leaf holds 25 rows.
CREATE TABLE dbms_stats_gran_idx_sub(id INT, val INT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_gran_idx_sub_p1 PARTITION OF dbms_stats_gran_idx_sub
  FOR VALUES FROM (1) TO (101) PARTITION BY RANGE (val);
CREATE TABLE dbms_stats_gran_idx_sub_p1_s1 PARTITION OF dbms_stats_gran_idx_sub_p1
  FOR VALUES FROM (1) TO (51);
CREATE TABLE dbms_stats_gran_idx_sub_p1_s2 PARTITION OF dbms_stats_gran_idx_sub_p1
  FOR VALUES FROM (51) TO (101);
CREATE TABLE dbms_stats_gran_idx_sub_p2 PARTITION OF dbms_stats_gran_idx_sub
  FOR VALUES FROM (101) TO (201) PARTITION BY RANGE (val);
CREATE TABLE dbms_stats_gran_idx_sub_p2_s1 PARTITION OF dbms_stats_gran_idx_sub_p2
  FOR VALUES FROM (1) TO (51);
CREATE TABLE dbms_stats_gran_idx_sub_p2_s2 PARTITION OF dbms_stats_gran_idx_sub_p2
  FOR VALUES FROM (51) TO (101);
CREATE INDEX dbms_stats_gran_idx_sub_idx ON dbms_stats_gran_idx_sub(id);
INSERT INTO dbms_stats_gran_idx_sub SELECT g, g FROM generate_series(1, 25) g;
INSERT INTO dbms_stats_gran_idx_sub SELECT g, g + 50 FROM generate_series(26, 50) g;
INSERT INTO dbms_stats_gran_idx_sub SELECT g, g - 100 FROM generate_series(101, 125) g;
INSERT INTO dbms_stats_gran_idx_sub SELECT g, g - 50 FROM generate_series(126, 150) g;
-- SUBPARTITION targets only the leaves. Leaf tables and their child indexes
-- become 25; the root, p1, p2, and their indexes stay at 0/-1.
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_sub_idx',
                                     granularity => 'SUBPARTITION');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
 dbms_stats_gather_index_stats 
-------------------------------
 
(1 row)
 
SELECT c.relname, c.reltuples::int AS reltuples
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_gran_idx_sub',
                     'dbms_stats_gran_idx_sub_p1',
                     'dbms_stats_gran_idx_sub_p2',
                     'dbms_stats_gran_idx_sub_p1_s1',
                     'dbms_stats_gran_idx_sub_p1_s2',
                     'dbms_stats_gran_idx_sub_p2_s1',
                     'dbms_stats_gran_idx_sub_p2_s2')
 ORDER BY c.relname;
            relname            | reltuples 
-------------------------------+-----------
 dbms_stats_gran_idx_sub       |        -1
 dbms_stats_gran_idx_sub_p1    |        -1
 dbms_stats_gran_idx_sub_p1_s1 |        25
 dbms_stats_gran_idx_sub_p1_s2 |        25
 dbms_stats_gran_idx_sub_p2    |        -1
 dbms_stats_gran_idx_sub_p2_s1 |        25
 dbms_stats_gran_idx_sub_p2_s2 |        25
(7 rows)
 
SELECT t.relname AS table_name, c.reltuples::int AS index_tuples
  FROM pg_class t
  JOIN pg_index ix ON ix.indrelid = t.oid
  JOIN pg_class c ON c.oid = ix.indexrelid
 WHERE t.relname LIKE 'dbms_stats_gran_idx_sub%'
 ORDER BY t.relname;
          table_name           | index_tuples 
-------------------------------+--------------
 dbms_stats_gran_idx_sub       |            0
 dbms_stats_gran_idx_sub_p1    |            0
 dbms_stats_gran_idx_sub_p1_s1 |           25
 dbms_stats_gran_idx_sub_p1_s2 |           25
 dbms_stats_gran_idx_sub_p2    |            0
 dbms_stats_gran_idx_sub_p2_s1 |           25
 dbms_stats_gran_idx_sub_p2_s2 |           25
(7 rows)
 
-- Add 5 rows to sub_p1_s1 and 5 to sub_p2_s1. SUBPARTITION with partname p1
-- targets only p1's leaves: p1_s1 and its index become 30; p1_s2, p2 leaves,
-- and their indexes stay 25; parent/index-root statistics stay unchanged.
INSERT INTO dbms_stats_gran_idx_sub SELECT g, g FROM generate_series(1, 5) g;
INSERT INTO dbms_stats_gran_idx_sub SELECT g, g - 100 FROM generate_series(101, 105) g;
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_sub_idx',
                                     granularity => 'SUBPARTITION',
                                     partname => 'dbms_stats_gran_idx_sub_p1');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
 dbms_stats_gather_index_stats 
-------------------------------
 
(1 row)
 
SELECT c.relname, c.reltuples::int AS reltuples
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_gran_idx_sub',
                     'dbms_stats_gran_idx_sub_p1',
                     'dbms_stats_gran_idx_sub_p2',
                     'dbms_stats_gran_idx_sub_p1_s1',
                     'dbms_stats_gran_idx_sub_p1_s2',
                     'dbms_stats_gran_idx_sub_p2_s1',
                     'dbms_stats_gran_idx_sub_p2_s2')
 ORDER BY c.relname;
            relname            | reltuples 
-------------------------------+-----------
 dbms_stats_gran_idx_sub       |        -1
 dbms_stats_gran_idx_sub_p1    |        -1
 dbms_stats_gran_idx_sub_p1_s1 |        30
 dbms_stats_gran_idx_sub_p1_s2 |        25
 dbms_stats_gran_idx_sub_p2    |        -1
 dbms_stats_gran_idx_sub_p2_s1 |        25
 dbms_stats_gran_idx_sub_p2_s2 |        25
(7 rows)
 
SELECT t.relname AS table_name, c.reltuples::int AS index_tuples
  FROM pg_class t
  JOIN pg_index ix ON ix.indrelid = t.oid
  JOIN pg_class c ON c.oid = ix.indexrelid
 WHERE t.relname LIKE 'dbms_stats_gran_idx_sub%'
 ORDER BY t.relname;
          table_name           | index_tuples 
-------------------------------+--------------
 dbms_stats_gran_idx_sub       |            0
 dbms_stats_gran_idx_sub_p1    |            0
 dbms_stats_gran_idx_sub_p1_s1 |           30
 dbms_stats_gran_idx_sub_p1_s2 |           25
 dbms_stats_gran_idx_sub_p2    |            0
 dbms_stats_gran_idx_sub_p2_s1 |           25
 dbms_stats_gran_idx_sub_p2_s2 |           25
(7 rows)
 
-- == index granularity error handling ==
-- GLOBAL requires core support; missing and foreign partnames are rejected by
-- partition resolution. Both must fail before any ANALYZE.
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_idx',
                                     granularity => 'GLOBAL');
ERROR:  granularity "GLOBAL" is not supported in this version
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_idx',
                                     partname => 'dbms_stats_no_such_partition');
ERROR:  dbms_stats_no_such_partition invalid partition name
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_idx',
                                     partname => 'dbms_stats_gran_idx_sub_p1');
ERROR:  dbms_stats_gran_idx_sub_p1 invalid partition name
-- A non-partitioned index has no index partitions, so partname is invalid.
CREATE TABLE dbms_stats_gran_idx_plain(id INT);
CREATE INDEX dbms_stats_gran_idx_plain_idx ON dbms_stats_gran_idx_plain(id);
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_plain_idx',
                                     partname => 'dbms_stats_gran_idx_plain');
ERROR:  INDEX "public"."dbms_stats_gran_idx_plain_idx" is not partitioned
-- ON ONLY creates a partitioned index without child indexes. p1 has no child
-- index for this index, so the request is rejected before ANALYZE.
CREATE TABLE dbms_stats_gran_idx_only(id INT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_gran_idx_only_p1 PARTITION OF dbms_stats_gran_idx_only
  FOR VALUES FROM (1) TO (101);
CREATE INDEX dbms_stats_gran_idx_only_idx ON ONLY dbms_stats_gran_idx_only(id);
SELECT dbms_stats_gather_index_stats('public', 'dbms_stats_gran_idx_only_idx',
                                     partname => 'dbms_stats_gran_idx_only_p1');
ERROR:  partition "dbms_stats_gran_idx_only_p1" does not belong to INDEX "public"."dbms_stats_gran_idx_only_idx"
DROP TABLE dbms_stats_gran_idx_only;
DROP TABLE dbms_stats_gran_idx_plain;
DROP TABLE dbms_stats_gran_idx_sub;
DROP TABLE dbms_stats_gran_idx;
DROP SCHEMA dbms_stats_gran_schema CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to table dbms_stats_gran_schema.plain
drop cascades to table dbms_stats_gran_schema.comp
DROP TABLE dbms_stats_gran_err;
DROP TABLE dbms_stats_gran_plain;
DROP TABLE dbms_stats_gran_gp;
DROP TABLE dbms_stats_gran_sub;
DROP TABLE dbms_stats_gran_nosub;
DROP TABLE dbms_stats_gran_part;
DROP TABLE dbms_stats_gran_all;
-- == delete_table_stats tests ==
-- PostgreSQL splits Oracle's statistics across two catalogs: table estimates
-- (reltuples, relpages) live in pg_class, while column statistics live in
-- pg_statistic. The queries below show both, so each cascade flag can be
-- verified independently.
--
-- Data layout: p1 holds ids 1..100, p2 holds ids 101..200, and the partitioned
-- index has one child index per partition. A whole-table gather therefore
-- gives root reltuples=200, each partition reltuples=100, and each child index
-- reltuples=100. Partitioned parents have no storage, so the root table gets
-- relpages=-1 and the partitioned index root stays reltuples=0, relpages=0.
--
-- Deleting table statistics resets reltuples to -1 and relpages to 0 (or -1 for
-- a partitioned parent), which is PostgreSQL's "not analyzed" state. Deleting
-- column statistics removes pg_statistic rows; deleting index statistics resets
-- the index pg_class entry. Every case re-gathers first, so each deletion starts
-- from the known values described above.
CREATE TABLE dbms_stats_del_test(id INT, val TEXT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_del_test_p1 PARTITION OF dbms_stats_del_test
  FOR VALUES FROM (1) TO (101);
CREATE TABLE dbms_stats_del_test_p2 PARTITION OF dbms_stats_del_test
  FOR VALUES FROM (101) TO (201);
CREATE INDEX dbms_stats_del_test_idx ON dbms_stats_del_test(id);
INSERT INTO dbms_stats_del_test
  SELECT g, 'x' FROM generate_series(1, 100) g;
INSERT INTO dbms_stats_del_test
  SELECT g, 'y' FROM generate_series(101, 200) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
-- Baseline after gather: root=200, p1=100, p2=100. Every relation has two
-- pg_statistic rows because both id and val were analyzed.
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
       (SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
         AS column_stats
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY c.relname;
        relname         | reltuples | relpages | column_stats 
------------------------+-----------+----------+--------------
 dbms_stats_del_test    |       200 |       -1 |            2
 dbms_stats_del_test_p1 |       100 |        1 |            2
 dbms_stats_del_test_p2 |       100 |        1 |            2
(3 rows)
 
-- Baseline index estimates: the partitioned index root is 0/0, while each
-- child index reports the 100 rows and 2 index pages of its partition.
SELECT t.relname AS table_name, c.relname AS index_name,
       c.reltuples::int AS reltuples, c.relpages
  FROM pg_class t
  JOIN pg_index ix ON ix.indrelid = t.oid
  JOIN pg_class c ON c.oid = ix.indexrelid
 WHERE t.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY t.relname, c.relname;
       table_name       |          index_name           | reltuples | relpages 
------------------------+-------------------------------+-----------+----------
 dbms_stats_del_test    | dbms_stats_del_test_idx       |         0 |        0
 dbms_stats_del_test_p1 | dbms_stats_del_test_p1_id_idx |       100 |        2
 dbms_stats_del_test_p2 | dbms_stats_del_test_p2_id_idx |       100 |        2
(3 rows)
 
-- Case 1: global delete with cascade_parts=false, cascade_columns=false, and
-- cascade_indexes=false. Only the root table estimates change: reltuples=-1,
-- relpages=-1. Partitions, column statistics, and all indexes stay unchanged.
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
                                     cascade_parts => false,
                                     cascade_columns => false,
                                     cascade_indexes => false,
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
 dbms_stats_delete_table_stats 
-------------------------------
 
(1 row)
 
-- get_table_stats observes the reset global estimates: numrows=0 because
-- reltuples is negative, and numblks=-1 is the raw relpages value stored for a
-- partitioned parent. avgrlen is 0 because there is no positive row estimate.
SELECT numrows, numblks, avgrlen
  FROM dbms_stats_get_table_stats_ovr1('public', 'dbms_stats_del_test');
 numrows | numblks | avgrlen 
---------+---------+---------
 0       | -1      | 0
(1 row)
 
-- Only the root row changed. p1/p2 still have reltuples=100 and both column
-- statistics, because cascade_parts and cascade_columns are false.
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
       (SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
         AS column_stats
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY c.relname;
        relname         | reltuples | relpages | column_stats 
------------------------+-----------+----------+--------------
 dbms_stats_del_test    |        -1 |       -1 |            2
 dbms_stats_del_test_p1 |       100 |        1 |            2
 dbms_stats_del_test_p2 |       100 |        1 |            2
(3 rows)
 
-- Indexes were not cascaded, so the root index and both child indexes keep
-- their pre-delete estimates (root 0/0, children 100/2).
SELECT t.relname AS table_name, c.relname AS index_name,
       c.reltuples::int AS reltuples, c.relpages
  FROM pg_class t
  JOIN pg_index ix ON ix.indrelid = t.oid
  JOIN pg_class c ON c.oid = ix.indexrelid
 WHERE t.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY t.relname, c.relname;
       table_name       |          index_name           | reltuples | relpages 
------------------------+-------------------------------+-----------+----------
 dbms_stats_del_test    | dbms_stats_del_test_idx       |         0 |        0
 dbms_stats_del_test_p1 | dbms_stats_del_test_p1_id_idx |       100 |        2
 dbms_stats_del_test_p2 | dbms_stats_del_test_p2_id_idx |       100 |        2
(3 rows)
 
-- Case 2: partname=dbms_stats_del_test_p1 with cascade_parts=true. Oracle
-- applies cascade_parts only when partname is NULL, so exactly p1 is targeted.
-- p1 table estimates become -1/0; root (200), p2 (100), all column stats, and
-- all index estimates remain unchanged.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
                                     partname => 'dbms_stats_del_test_p1',
                                     cascade_parts => true,
                                     cascade_columns => false,
                                     cascade_indexes => false,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
 dbms_stats_delete_table_stats 
-------------------------------
 
(1 row)
 
-- Only p1 changes: root=200, p1=-1/0, p2=100/1. All column_stats stay 2
-- because cascade_columns=false.
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
       (SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
         AS column_stats
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY c.relname;
        relname         | reltuples | relpages | column_stats 
------------------------+-----------+----------+--------------
 dbms_stats_del_test    |       200 |       -1 |            2
 dbms_stats_del_test_p1 |        -1 |        0 |            2
 dbms_stats_del_test_p2 |       100 |        1 |            2
(3 rows)
 
-- cascade_indexes=false keeps the root index at 0/0 and both child indexes at
-- 100/2, including p1's child index.
SELECT t.relname AS table_name, c.relname AS index_name,
       c.reltuples::int AS reltuples, c.relpages
  FROM pg_class t
  JOIN pg_index ix ON ix.indrelid = t.oid
  JOIN pg_class c ON c.oid = ix.indexrelid
 WHERE t.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY t.relname, c.relname;
       table_name       |          index_name           | reltuples | relpages 
------------------------+-------------------------------+-----------+----------
 dbms_stats_del_test    | dbms_stats_del_test_idx       |         0 |        0
 dbms_stats_del_test_p1 | dbms_stats_del_test_p1_id_idx |       100 |        2
 dbms_stats_del_test_p2 | dbms_stats_del_test_p2_id_idx |       100 |        2
(3 rows)
 
-- Case 3: partname=p1 with cascade_columns=true and cascade_indexes=true.
-- The single p1 target loses table estimates (-1/0), all column statistics
-- (column_stats=0), and its child index estimates (-1/0). Everything outside
-- p1 remains unchanged.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
                                     partname => 'dbms_stats_del_test_p1',
                                     cascade_columns => true,
                                     cascade_indexes => true,
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
 dbms_stats_delete_table_stats 
-------------------------------
 
(1 row)
 
-- Expected: root=200 with 2 column stats, p1=-1/0 with 0 column stats,
-- p2=100/1 with 2 column stats.
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
       (SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
         AS column_stats
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY c.relname;
        relname         | reltuples | relpages | column_stats 
------------------------+-----------+----------+--------------
 dbms_stats_del_test    |       200 |       -1 |            2
 dbms_stats_del_test_p1 |        -1 |        0 |            0
 dbms_stats_del_test_p2 |       100 |        1 |            2
(3 rows)
 
-- p1's child index is now -1/0; the root index (0/0) and p2 child (100/2)
-- were not selected.
SELECT t.relname AS table_name, c.relname AS index_name,
       c.reltuples::int AS reltuples, c.relpages
  FROM pg_class t
  JOIN pg_index ix ON ix.indrelid = t.oid
  JOIN pg_class c ON c.oid = ix.indexrelid
 WHERE t.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY t.relname, c.relname;
       table_name       |          index_name           | reltuples | relpages 
------------------------+-------------------------------+-----------+----------
 dbms_stats_del_test    | dbms_stats_del_test_idx       |         0 |        0
 dbms_stats_del_test_p1 | dbms_stats_del_test_p1_id_idx |        -1 |        0
 dbms_stats_del_test_p2 | dbms_stats_del_test_p2_id_idx |       100 |        2
(3 rows)
 
-- Case 4: cascade_parts=true, cascade_columns=false, cascade_indexes=false.
-- All table relations are reset (root reltuples=-1 with relpages=-1, partitions
-- reltuples=-1 with relpages=0), but column statistics stay 2 and indexes keep
-- their 100/2 estimates.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
                                     cascade_parts => true,
                                     cascade_columns => false,
                                     cascade_indexes => false,
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
 dbms_stats_delete_table_stats 
-------------------------------
 
(1 row)
 
-- Expected: every table reltuples is -1; column_stats is still 2 everywhere.
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
       (SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
         AS column_stats
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY c.relname;
        relname         | reltuples | relpages | column_stats 
------------------------+-----------+----------+--------------
 dbms_stats_del_test    |        -1 |       -1 |            2
 dbms_stats_del_test_p1 |        -1 |        0 |            2
 dbms_stats_del_test_p2 |        -1 |        0 |            2
(3 rows)
 
-- cascade_indexes=false preserves the root index (0/0) and both children
-- (100/2).
SELECT t.relname AS table_name, c.relname AS index_name,
       c.reltuples::int AS reltuples, c.relpages
  FROM pg_class t
  JOIN pg_index ix ON ix.indrelid = t.oid
  JOIN pg_class c ON c.oid = ix.indexrelid
 WHERE t.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY t.relname, c.relname;
       table_name       |          index_name           | reltuples | relpages 
------------------------+-------------------------------+-----------+----------
 dbms_stats_del_test    | dbms_stats_del_test_idx       |         0 |        0
 dbms_stats_del_test_p1 | dbms_stats_del_test_p1_id_idx |       100 |        2
 dbms_stats_del_test_p2 | dbms_stats_del_test_p2_id_idx |       100 |        2
(3 rows)
 
-- Case 5: full cascade. Every table relation becomes reltuples=-1 (root
-- relpages=-1, partitions 0), all column statistics are removed (0), and all
-- index estimates are reset to -1/0.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
                                     cascade_parts => true,
                                     cascade_columns => true,
                                     cascade_indexes => true,
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
 dbms_stats_delete_table_stats 
-------------------------------
 
(1 row)
 
-- Expected: all three table relations are -1 with column_stats=0.
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
       (SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
         AS column_stats
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY c.relname;
        relname         | reltuples | relpages | column_stats 
------------------------+-----------+----------+--------------
 dbms_stats_del_test    |        -1 |       -1 |            0
 dbms_stats_del_test_p1 |        -1 |        0 |            0
 dbms_stats_del_test_p2 |        -1 |        0 |            0
(3 rows)
 
-- The partitioned index root and both child indexes are now -1/0.
SELECT t.relname AS table_name, c.relname AS index_name,
       c.reltuples::int AS reltuples, c.relpages
  FROM pg_class t
  JOIN pg_index ix ON ix.indrelid = t.oid
  JOIN pg_class c ON c.oid = ix.indexrelid
 WHERE t.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY t.relname, c.relname;
       table_name       |          index_name           | reltuples | relpages 
------------------------+-------------------------------+-----------+----------
 dbms_stats_del_test    | dbms_stats_del_test_idx       |        -1 |        0
 dbms_stats_del_test_p1 | dbms_stats_del_test_p1_id_idx |        -1 |        0
 dbms_stats_del_test_p2 | dbms_stats_del_test_p2_id_idx |        -1 |        0
(3 rows)
 
-- Cases 6a/6b: stat_category is a comma-separated Oracle category list.
-- SYNOPSES has no PostgreSQL source, so it only emits a NOTICE. When it is the
-- only category, OBJECT_STATS is absent and the call is a no-op. When it is
-- combined with OBJECT_STATS, the NOTICE is still emitted and the deletion runs.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
                                     cascade_parts => false,
                                     cascade_columns => false,
                                     cascade_indexes => false,
                                     stat_category => 'SYNOPSES',
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL);
NOTICE:  stat_category value "SYNOPSES" is not currently supported
 dbms_stats_delete_table_stats 
-------------------------------
 
(1 row)
 
-- Case 6a expected: root stays 200 and both partitions stay 100 because no
-- OBJECT_STATS category was requested.
SELECT c.relname, c.reltuples::int AS reltuples
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY c.relname;
        relname         | reltuples 
------------------------+-----------
 dbms_stats_del_test    |       200
 dbms_stats_del_test_p1 |       100
 dbms_stats_del_test_p2 |       100
(3 rows)
 
-- Case 6b: OBJECT_STATS is present, so the global table delete runs after the
-- ignored SYNOPSES notice. Expected: root=-1; p1/p2 remain 100.
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
                                     cascade_parts => false,
                                     cascade_columns => false,
                                     cascade_indexes => false,
                                     stat_category => 'OBJECT_STATS, SYNOPSES',
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL);
NOTICE:  stat_category value "SYNOPSES" is not currently supported
 dbms_stats_delete_table_stats 
-------------------------------
 
(1 row)
 
SELECT c.relname, c.reltuples::int AS reltuples
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_del_test',
                     'dbms_stats_del_test_p1',
                     'dbms_stats_del_test_p2')
 ORDER BY c.relname;
        relname         | reltuples 
------------------------+-----------
 dbms_stats_del_test    |        -1
 dbms_stats_del_test_p1 |       100
 dbms_stats_del_test_p2 |       100
(3 rows)
 
-- == delete_table_stats subpartition tests ==
-- Composite layout: root -> p1/p2, and each pN -> s1/s2. Every leaf holds 25
-- rows, so a whole-table gather gives root=100, p1=50, p2=50, and every leaf=25.
-- p1/p2 are partitioned parents, so their relpages is -1; leaves use relpages=1.
-- These cases check that a named subpartition is selected alone and that
-- cascade_parts walks the full hierarchy only when no partname is supplied.
CREATE TABLE dbms_stats_del_sub(id INT, val INT) PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_del_sub_p1 PARTITION OF dbms_stats_del_sub
  FOR VALUES FROM (1) TO (101) PARTITION BY RANGE (val);
CREATE TABLE dbms_stats_del_sub_p1_s1 PARTITION OF dbms_stats_del_sub_p1
  FOR VALUES FROM (1) TO (51);
CREATE TABLE dbms_stats_del_sub_p1_s2 PARTITION OF dbms_stats_del_sub_p1
  FOR VALUES FROM (51) TO (101);
CREATE TABLE dbms_stats_del_sub_p2 PARTITION OF dbms_stats_del_sub
  FOR VALUES FROM (101) TO (201) PARTITION BY RANGE (val);
CREATE TABLE dbms_stats_del_sub_p2_s1 PARTITION OF dbms_stats_del_sub_p2
  FOR VALUES FROM (1) TO (51);
CREATE TABLE dbms_stats_del_sub_p2_s2 PARTITION OF dbms_stats_del_sub_p2
  FOR VALUES FROM (51) TO (101);
INSERT INTO dbms_stats_del_sub SELECT g, g FROM generate_series(1, 25) g;
INSERT INTO dbms_stats_del_sub SELECT g, g + 50 FROM generate_series(26, 50) g;
INSERT INTO dbms_stats_del_sub SELECT g, g - 100 FROM generate_series(101, 125) g;
INSERT INTO dbms_stats_del_sub SELECT g, g - 50 FROM generate_series(126, 150) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_sub');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
-- Case 1: delete subpartition p1_s1. partname selects exactly one relation, so
-- cascade_parts=true is ignored. Only p1_s1's table estimates become -1/0. Its
-- column statistics stay because cascade_columns=false, and p1_s2, all p2
-- leaves, p1/p2, and the root keep their gathered values.
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_sub',
                                     partname => 'dbms_stats_del_sub_p1_s1',
                                     cascade_parts => true,
                                     cascade_columns => false,
                                     cascade_indexes => false,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
 dbms_stats_delete_table_stats 
-------------------------------
 
(1 row)
 
-- Expected: p1_s1=-1/0; every other relation keeps 25/50/100 or -1 for
-- partitioned parents. All column_stats remain 2.
SELECT c.relname, c.reltuples::int AS reltuples, c.relpages,
       (SELECT count(*) FROM pg_statistic s WHERE s.starelid = c.oid)
         AS column_stats
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_del_sub',
                     'dbms_stats_del_sub_p1',
                     'dbms_stats_del_sub_p2',
                     'dbms_stats_del_sub_p1_s1',
                     'dbms_stats_del_sub_p1_s2',
                     'dbms_stats_del_sub_p2_s1',
                     'dbms_stats_del_sub_p2_s2')
 ORDER BY c.relname;
         relname          | reltuples | relpages | column_stats 
--------------------------+-----------+----------+--------------
 dbms_stats_del_sub       |       100 |       -1 |            2
 dbms_stats_del_sub_p1    |        50 |       -1 |            2
 dbms_stats_del_sub_p1_s1 |        -1 |        0 |            2
 dbms_stats_del_sub_p1_s2 |        25 |        1 |            2
 dbms_stats_del_sub_p2    |        50 |       -1 |            2
 dbms_stats_del_sub_p2_s1 |        25 |        1 |            2
 dbms_stats_del_sub_p2_s2 |        25 |        1 |            2
(7 rows)
 
-- Case 2: cascade_parts=false at the root. Only the global root entry is reset
-- (reltuples=-1); both partitions and all four leaves keep their estimates.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_sub');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_sub',
                                     cascade_parts => false,
                                     cascade_columns => false,
                                     cascade_indexes => false,
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
 dbms_stats_delete_table_stats 
-------------------------------
 
(1 row)
 
-- Expected: root=-1; p1/p2=50; all four leaves=25.
SELECT c.relname, c.reltuples::int AS reltuples
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_del_sub',
                     'dbms_stats_del_sub_p1',
                     'dbms_stats_del_sub_p2',
                     'dbms_stats_del_sub_p1_s1',
                     'dbms_stats_del_sub_p1_s2',
                     'dbms_stats_del_sub_p2_s1',
                     'dbms_stats_del_sub_p2_s2')
 ORDER BY c.relname;
         relname          | reltuples 
--------------------------+-----------
 dbms_stats_del_sub       |        -1
 dbms_stats_del_sub_p1    |        50
 dbms_stats_del_sub_p1_s1 |        25
 dbms_stats_del_sub_p1_s2 |        25
 dbms_stats_del_sub_p2    |        50
 dbms_stats_del_sub_p2_s1 |        25
 dbms_stats_del_sub_p2_s2 |        25
(7 rows)
 
-- Case 3: cascade_parts=true at the root expands through p1/p2 into every
-- subpartition. Table estimates are reset for root, partitions, and leaves.
-- cascade_columns/cascade_indexes stay false, so only table estimates change.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_sub');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_sub',
                                     cascade_parts => true,
                                     cascade_columns => false,
                                     cascade_indexes => false,
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
 dbms_stats_delete_table_stats 
-------------------------------
 
(1 row)
 
-- Expected: every relation reports reltuples=-1; column_stats is unchanged.
SELECT c.relname, c.reltuples::int AS reltuples
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_del_sub',
                     'dbms_stats_del_sub_p1',
                     'dbms_stats_del_sub_p2',
                     'dbms_stats_del_sub_p1_s1',
                     'dbms_stats_del_sub_p1_s2',
                     'dbms_stats_del_sub_p2_s1',
                     'dbms_stats_del_sub_p2_s2')
 ORDER BY c.relname;
         relname          | reltuples 
--------------------------+-----------
 dbms_stats_del_sub       |        -1
 dbms_stats_del_sub_p1    |        -1
 dbms_stats_del_sub_p1_s1 |        -1
 dbms_stats_del_sub_p1_s2 |        -1
 dbms_stats_del_sub_p2    |        -1
 dbms_stats_del_sub_p2_s1 |        -1
 dbms_stats_del_sub_p2_s2 |        -1
(7 rows)
 
-- == delete_column_stats tests ==
-- Same composite layout as above, with columns id, val, and note. The gather
-- creates one pg_statistic row per column per relation, so the baseline below
-- shows id_stats=1 and val_stats=1 everywhere. reltuples is included after
-- deletion to prove that removing a column does not reset table estimates.
CREATE TABLE dbms_stats_del_col(id INT, val INT, note TEXT)
  PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_del_col_p1 PARTITION OF dbms_stats_del_col
  FOR VALUES FROM (1) TO (101) PARTITION BY RANGE (val);
CREATE TABLE dbms_stats_del_col_p1_s1 PARTITION OF dbms_stats_del_col_p1
  FOR VALUES FROM (1) TO (51);
CREATE TABLE dbms_stats_del_col_p1_s2 PARTITION OF dbms_stats_del_col_p1
  FOR VALUES FROM (51) TO (101);
CREATE TABLE dbms_stats_del_col_p2 PARTITION OF dbms_stats_del_col
  FOR VALUES FROM (101) TO (201) PARTITION BY RANGE (val);
CREATE TABLE dbms_stats_del_col_p2_s1 PARTITION OF dbms_stats_del_col_p2
  FOR VALUES FROM (1) TO (51);
CREATE TABLE dbms_stats_del_col_p2_s2 PARTITION OF dbms_stats_del_col_p2
  FOR VALUES FROM (51) TO (101);
INSERT INTO dbms_stats_del_col
  SELECT g, g, 'n' FROM generate_series(1, 25) g;
INSERT INTO dbms_stats_del_col
  SELECT g, g + 50, 'n' FROM generate_series(26, 50) g;
INSERT INTO dbms_stats_del_col
  SELECT g, g - 100, 'n' FROM generate_series(101, 125) g;
INSERT INTO dbms_stats_del_col
  SELECT g, g - 50, 'n' FROM generate_series(126, 150) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_col');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
-- Baseline: every relation has one id row and one val row.
SELECT c.relname,
       (SELECT count(*) FROM pg_statistic s
         JOIN pg_attribute a
           ON a.attrelid = s.starelid AND a.attnum = s.staattnum
        WHERE s.starelid = c.oid AND a.attname = 'id') AS id_stats,
       (SELECT count(*) FROM pg_statistic s
         JOIN pg_attribute a
           ON a.attrelid = s.starelid AND a.attnum = s.staattnum
        WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
  FROM pg_class c
 WHERE c.relname LIKE 'dbms_stats_del_col%'
 ORDER BY c.relname;
         relname          | id_stats | val_stats 
--------------------------+----------+-----------
 dbms_stats_del_col       |        1 |         1
 dbms_stats_del_col_p1    |        1 |         1
 dbms_stats_del_col_p1_s1 |        1 |         1
 dbms_stats_del_col_p1_s2 |        1 |         1
 dbms_stats_del_col_p2    |        1 |         1
 dbms_stats_del_col_p2_s1 |        1 |         1
 dbms_stats_del_col_p2_s2 |        1 |         1
(7 rows)
 
-- Case 1: global delete of val with cascade_parts=false. Only the root val
-- statistics are removed. Root reltuples stays 100, id stats stay 1, and every
-- partition/subpartition val row stays 1.
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_col', 'val',
                                      cascade_parts => false,
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     col_stat_type => NULL);
 dbms_stats_delete_column_stats 
--------------------------------
 
(1 row)
 
-- Expected: root val_stats=0; all other val_stats stay 1; id_stats stay 1;
-- reltuples stays 100/50/25 because column deletion does not touch pg_class.
SELECT c.relname, c.reltuples::int AS reltuples,
       (SELECT count(*) FROM pg_statistic s
         JOIN pg_attribute a
           ON a.attrelid = s.starelid AND a.attnum = s.staattnum
        WHERE s.starelid = c.oid AND a.attname = 'id') AS id_stats,
       (SELECT count(*) FROM pg_statistic s
         JOIN pg_attribute a
           ON a.attrelid = s.starelid AND a.attnum = s.staattnum
        WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
  FROM pg_class c
 WHERE c.relname LIKE 'dbms_stats_del_col%'
 ORDER BY c.relname;
         relname          | reltuples | id_stats | val_stats 
--------------------------+-----------+----------+-----------
 dbms_stats_del_col       |       100 |        1 |         0
 dbms_stats_del_col_p1    |        50 |        1 |         1
 dbms_stats_del_col_p1_s1 |        25 |        1 |         1
 dbms_stats_del_col_p1_s2 |        25 |        1 |         1
 dbms_stats_del_col_p2    |        50 |        1 |         1
 dbms_stats_del_col_p2_s1 |        25 |        1 |         1
 dbms_stats_del_col_p2_s2 |        25 |        1 |         1
(7 rows)
 
-- get_column_stats must now fail for the deleted root val statistics. This is
-- the observable effect of the missing pg_statistic row.
SELECT * FROM dbms_stats_get_column_stats_ovr1('public', 'dbms_stats_del_col',
                                               'val');
ERROR:  No statistics have been stored for requested object
-- Case 2: partname=p1 with cascade_parts=true. Because partname is supplied,
-- cascade_parts does not descend into p1_s1/p1_s2: only p1's own val stats are
-- removed. Root, both p1 subpartitions, and every p2 relation keep val_stats=1;
-- id stats and reltuples are untouched.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_col');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_col', 'val',
                                      partname => 'dbms_stats_del_col_p1',
                                      cascade_parts => true,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     col_stat_type => NULL);
 dbms_stats_delete_column_stats 
--------------------------------
 
(1 row)
 
-- Expected: p1 val_stats=0 only; all other val_stats and id_stats stay 1.
SELECT c.relname,
       (SELECT count(*) FROM pg_statistic s
         JOIN pg_attribute a
           ON a.attrelid = s.starelid AND a.attnum = s.staattnum
        WHERE s.starelid = c.oid AND a.attname = 'id') AS id_stats,
       (SELECT count(*) FROM pg_statistic s
         JOIN pg_attribute a
           ON a.attrelid = s.starelid AND a.attnum = s.staattnum
        WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
  FROM pg_class c
 WHERE c.relname LIKE 'dbms_stats_del_col%'
 ORDER BY c.relname;
         relname          | id_stats | val_stats 
--------------------------+----------+-----------
 dbms_stats_del_col       |        1 |         1
 dbms_stats_del_col_p1    |        1 |         0
 dbms_stats_del_col_p1_s1 |        1 |         1
 dbms_stats_del_col_p1_s2 |        1 |         1
 dbms_stats_del_col_p2    |        1 |         1
 dbms_stats_del_col_p2_s1 |        1 |         1
 dbms_stats_del_col_p2_s2 |        1 |         1
(7 rows)
 
-- Case 3: partname=p1_s1 and col_stat_type='all' (case-insensitive). Only the
-- p1_s1 val row is removed; every other val/id row remains 1.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_col');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_col', 'val',
                                      partname => 'dbms_stats_del_col_p1_s1',
                                      col_stat_type => 'all',
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL);
 dbms_stats_delete_column_stats 
--------------------------------
 
(1 row)
 
-- Expected: p1_s1 val_stats=0 only.
SELECT c.relname,
       (SELECT count(*) FROM pg_statistic s
         JOIN pg_attribute a
           ON a.attrelid = s.starelid AND a.attnum = s.staattnum
        WHERE s.starelid = c.oid AND a.attname = 'id') AS id_stats,
       (SELECT count(*) FROM pg_statistic s
         JOIN pg_attribute a
           ON a.attrelid = s.starelid AND a.attnum = s.staattnum
        WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
  FROM pg_class c
 WHERE c.relname LIKE 'dbms_stats_del_col%'
 ORDER BY c.relname;
         relname          | id_stats | val_stats 
--------------------------+----------+-----------
 dbms_stats_del_col       |        1 |         1
 dbms_stats_del_col_p1    |        1 |         1
 dbms_stats_del_col_p1_s1 |        1 |         0
 dbms_stats_del_col_p1_s2 |        1 |         1
 dbms_stats_del_col_p2    |        1 |         1
 dbms_stats_del_col_p2_s1 |        1 |         1
 dbms_stats_del_col_p2_s2 |        1 |         1
(7 rows)
 
-- Case 4: cascade_parts=true without partname. The root, both partitions, and
-- all four subpartitions lose val stats. id stats stay 1 and reltuples is
-- unchanged, proving only the requested column was removed.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_col');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_col', 'val',
                                      cascade_parts => true,
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     col_stat_type => NULL);
 dbms_stats_delete_column_stats 
--------------------------------
 
(1 row)
 
-- Expected: val_stats=0 everywhere; id_stats=1 everywhere; reltuples stays
-- 100 at the root and 50/25 at partitions/subpartitions.
SELECT c.relname, c.reltuples::int AS reltuples,
       (SELECT count(*) FROM pg_statistic s
         JOIN pg_attribute a
           ON a.attrelid = s.starelid AND a.attnum = s.staattnum
        WHERE s.starelid = c.oid AND a.attname = 'id') AS id_stats,
       (SELECT count(*) FROM pg_statistic s
         JOIN pg_attribute a
           ON a.attrelid = s.starelid AND a.attnum = s.staattnum
        WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
  FROM pg_class c
 WHERE c.relname LIKE 'dbms_stats_del_col%'
 ORDER BY c.relname;
         relname          | reltuples | id_stats | val_stats 
--------------------------+-----------+----------+-----------
 dbms_stats_del_col       |       100 |        1 |         0
 dbms_stats_del_col_p1    |        50 |        1 |         0
 dbms_stats_del_col_p1_s1 |        25 |        1 |         0
 dbms_stats_del_col_p1_s2 |        25 |        1 |         0
 dbms_stats_del_col_p2    |        50 |        1 |         0
 dbms_stats_del_col_p2_s1 |        25 |        1 |         0
 dbms_stats_del_col_p2_s2 |        25 |        1 |         0
(7 rows)
 
-- == NULL ownname tests ==
-- NULL ownname must resolve through the current search_path, matching the
-- DBMS_STATS get procedures. Pin search_path to public so the first schema is
-- deterministic, then verify both delete procedures against that schema.
SET search_path = public;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
-- delete_table_stats with ownname=NULL resolves dbms_stats_del_test in public.
-- Only the root is targeted (cascade_parts=false), so the table estimate should
-- become -1 while p1/p2 and all column/index statistics remain untouched.
SELECT dbms_stats_delete_table_stats(NULL, 'dbms_stats_del_test',
                                     cascade_parts => false,
                                     cascade_columns => false,
                                     cascade_indexes => false,
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
 dbms_stats_delete_table_stats 
-------------------------------
 
(1 row)
 
-- Expected: the resolved public.dbms_stats_del_test has reltuples=-1.
SELECT relname, reltuples::int AS reltuples
  FROM pg_class
 WHERE relname = 'dbms_stats_del_test';
       relname       | reltuples 
---------------------+-----------
 dbms_stats_del_test |        -1
(1 row)
 
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_col');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
-- delete_column_stats with ownname=NULL resolves dbms_stats_del_col in public
-- and removes only the root val row (cascade_parts=false).
SELECT dbms_stats_delete_column_stats(NULL, 'dbms_stats_del_col', 'val',
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => false,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     col_stat_type => NULL);
 dbms_stats_delete_column_stats 
--------------------------------
 
(1 row)
 
-- Expected: the resolved public.dbms_stats_del_col has val_stats=0.
SELECT c.relname,
       (SELECT count(*) FROM pg_statistic s
         JOIN pg_attribute a
           ON a.attrelid = s.starelid AND a.attnum = s.staattnum
        WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
  FROM pg_class c
 WHERE c.relname = 'dbms_stats_del_col';
      relname       | val_stats 
--------------------+-----------
 dbms_stats_del_col |         0
(1 row)
 
RESET search_path;
-- == delete error handling ==
-- Snapshot objects with known statistics so failed calls can be checked for
-- accidental mutation.
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_test');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_col');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
CREATE TABLE dbms_stats_del_plain(id INT, val INT);
INSERT INTO dbms_stats_del_plain SELECT g, g FROM generate_series(1, 10) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_plain');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
-- Missing table: delete_table_stats fails during relation resolution with
-- TABLE ... does not exist or insufficient privileges.
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_no_such',
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     cascade_columns => NULL,
                                     cascade_indexes => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
ERROR:  TABLE "public"."dbms_stats_del_no_such" does not exist or insufficient privileges
-- Missing table for column deletion: the table lookup fails before the column
-- is examined and raises the same TABLE error.
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_no_such',
                                      'id',
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     col_stat_type => NULL);
ERROR:  TABLE "public"."dbms_stats_del_no_such" does not exist or insufficient privileges
-- Missing column: the table exists, but get_attnum fails and reports
-- "<column> invalid column name".
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_plain',
                                      'no_such_col', cascade_parts => false,
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     col_stat_type => NULL);
ERROR:  no_such_col invalid column name
-- Missing, foreign, and plain-table partition names. In all three cases
-- resolve_partition_oid rejects the name instead of falling back to the global
-- table level.
-- Missing partition: no descendant with this name exists.
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
                                     partname => 'dbms_stats_del_no_such_part',
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     cascade_columns => NULL,
                                     cascade_indexes => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
ERROR:  dbms_stats_del_no_such_part invalid partition name
-- Foreign partition: dbms_stats_del_col_p1_s1 belongs to another table.
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_test',
                                     partname => 'dbms_stats_del_col_p1_s1',
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     cascade_columns => NULL,
                                     cascade_indexes => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
ERROR:  dbms_stats_del_col_p1_s1 invalid partition name
-- partname on a non-partitioned table is invalid.
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_plain',
                                     partname => 'dbms_stats_del_test_p1',
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     cascade_columns => NULL,
                                     cascade_indexes => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
ERROR:  dbms_stats_del_test_p1 invalid partition name
-- The same three partition errors apply to column deletion: missing name,
-- foreign partition, and partname on a plain table.
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_col', 'val',
                                      partname => 'dbms_stats_del_no_such_part',
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     col_stat_type => NULL);
ERROR:  dbms_stats_del_no_such_part invalid partition name
-- Foreign partition for column deletion.
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_col', 'val',
                                      partname => 'dbms_stats_del_test_p1',
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     col_stat_type => NULL);
ERROR:  dbms_stats_del_test_p1 invalid partition name
-- partname on a non-partitioned table for column deletion.
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_plain', 'val',
                                      partname => 'dbms_stats_del_test_p1',
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     col_stat_type => NULL);
ERROR:  dbms_stats_del_test_p1 invalid partition name
-- Invalid stat_category and col_stat_type. stat_category accepts only Oracle
-- category names; col_stat_type accepts ALL but rejects HISTOGRAM as unsupported.
-- Unknown stat_category token: invalid parameter error.
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_plain',
                                     stat_category => 'NONSENSE',
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     cascade_columns => NULL,
                                     cascade_indexes => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL);
ERROR:  Invalid or inconsistent input values
-- HISTOGRAM is rejected because PostgreSQL has no supported API for removing
-- only histogram components while preserving base column statistics.
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_plain', 'val',
                                      col_stat_type => 'HISTOGRAM',
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL);
ERROR:  col_stat_type "HISTOGRAM" is not supported in this version
-- Unknown col_stat_type token: invalid parameter error.
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_plain', 'val',
                                      col_stat_type => 'NONSENSE',
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL);
ERROR:  Illegal col_stat_type NONSENSE: must be ALL | HISTOGRAM
-- Every invalid call above must abort before mutation. The plain table still
-- has its 10-row/1-page estimates, and the snapshot gathers of del_test and
-- del_col still show root reltuples 200/100 with one val statistic.
SELECT reltuples::int AS reltuples, relpages
  FROM pg_class WHERE relname = 'dbms_stats_del_plain';
 reltuples | relpages 
-----------+----------
        10 |        1
(1 row)
 
SELECT c.relname, c.reltuples::int AS reltuples,
       (SELECT count(*) FROM pg_statistic s
         JOIN pg_attribute a
           ON a.attrelid = s.starelid AND a.attnum = s.staattnum
        WHERE s.starelid = c.oid AND a.attname = 'val') AS val_stats
  FROM pg_class c
 WHERE c.relname IN ('dbms_stats_del_test', 'dbms_stats_del_col')
 ORDER BY c.relname;
       relname       | reltuples | val_stats 
---------------------+-----------+-----------
 dbms_stats_del_col  |       100 |         1
 dbms_stats_del_test |       200 |         1
(2 rows)
 
-- Insufficient privileges for both procedures. The new role gets schema USAGE
-- but neither table ownership nor MAINTAIN, so both calls must fail without
-- changing the gathered statistics.
CREATE TABLE dbms_stats_del_priv(id INT, val INT);
INSERT INTO dbms_stats_del_priv SELECT g, g FROM generate_series(1, 10) g;
SELECT dbms_stats_gather_table_stats('public', 'dbms_stats_del_priv');
NOTICE:  estimate_percent is accepted but ANALYZE uses PostgreSQL's default sampling
NOTICE:  cascade=false is not supported — ANALYZE always includes index statistics
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
CREATE ROLE dbms_stats_del_priv_user;
GRANT USAGE ON SCHEMA public TO dbms_stats_del_priv_user;
SET ROLE dbms_stats_del_priv_user;
-- Table deletion requires ownership or MAINTAIN; schema USAGE is not enough.
SELECT dbms_stats_delete_table_stats('public', 'dbms_stats_del_priv',
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     cascade_columns => NULL,
                                     cascade_indexes => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     stat_category => NULL);
ERROR:  TABLE "public"."dbms_stats_del_priv" does not exist or insufficient privileges
-- Column deletion uses the same table-privilege check and must also fail.
SELECT dbms_stats_delete_column_stats('public', 'dbms_stats_del_priv', 'val',
                                     partname => NULL,
                                     stattab => NULL,
                                     statid => NULL,
                                     cascade_parts => NULL,
                                     statown => NULL,
                                     no_invalidate => NULL,
                                     force => NULL,
                                     col_stat_type => NULL);
ERROR:  TABLE "public"."dbms_stats_del_priv" does not exist or insufficient privileges
RESET ROLE;
REVOKE ALL ON SCHEMA public FROM dbms_stats_del_priv_user;
DROP ROLE dbms_stats_del_priv_user;
DROP TABLE dbms_stats_del_priv;
DROP TABLE dbms_stats_del_col;
DROP TABLE dbms_stats_del_sub;
DROP TABLE dbms_stats_del_test;
DROP TABLE dbms_stats_del_plain;
-- == create_stat_table tests ==
-- CREATE_STAT_TABLE creates Oracle's generic statistics table, refuses
-- duplicates and missing schemas, honors tablespace, and requires CREATE.
CREATE SCHEMA dbms_stats_stattab_schema;
CREATE ROLE dbms_stats_stattab_limited;
GRANT USAGE ON SCHEMA dbms_stats_stattab_schema TO dbms_stats_stattab_limited;
-- Two-argument call creates an empty table in the named schema.
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'dbms_stats_stattab_1');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT n.nspname, c.relname, c.relkind, c.relpersistence
  FROM pg_class c
  JOIN pg_namespace n ON n.oid = c.relnamespace
 WHERE n.nspname = 'dbms_stats_stattab_schema'
   AND c.relname = 'dbms_stats_stattab_1';
          nspname          |       relname        | relkind | relpersistence 
---------------------------+----------------------+---------+----------------
 dbms_stats_stattab_schema | dbms_stats_stattab_1 | r       | p
(1 row)
 
SELECT count(*) AS row_count
  FROM dbms_stats_stattab_schema.dbms_stats_stattab_1;
 row_count 
-----------
         0
(1 row)
 
-- Exact generic layout: 27 columns in Oracle order with expected types.
SELECT attnum, attname, format_type(atttypid, atttypmod) AS type,
       attnotnull
  FROM pg_attribute
 WHERE attrelid = 'dbms_stats_stattab_schema.dbms_stats_stattab_1'::regclass
   AND attnum > 0 AND NOT attisdropped
 ORDER BY attnum;
 attnum | attname |              type              | attnotnull 
--------+---------+--------------------------------+------------
      1 | statid  | varchar2(30)                   | f
      2 | type    | character(1)                   | f
      3 | version | number                         | f
      4 | flags   | number                         | f
      5 | c1      | varchar2(63)                   | f
      6 | c2      | varchar2(63)                   | f
      7 | c3      | varchar2(63)                   | f
      8 | c4      | varchar2(63)                   | f
      9 | c5      | varchar2(63)                   | f
     10 | n1      | number                         | f
     11 | n2      | number                         | f
     12 | n3      | number                         | f
     13 | n4      | number                         | f
     14 | n5      | number                         | f
     15 | n6      | number                         | f
     16 | n7      | number                         | f
     17 | n8      | number                         | f
     18 | n9      | number                         | f
     19 | n10     | number                         | f
     20 | n11     | number                         | f
     21 | n12     | number                         | f
     22 | n13     | number                         | f
     23 | d1      | timestamp(0) without time zone | f
     24 | r1      | raw(1000)                      | f
     25 | r2      | raw(1000)                      | f
     26 | r3      | raw(1000)                      | f
     27 | ch1     | varchar2(1000)                 | f
(27 rows)
 
SELECT count(*) AS column_count,
       count(*) FILTER (WHERE attnotnull) AS not_null_columns
  FROM pg_attribute
 WHERE attrelid = 'dbms_stats_stattab_schema.dbms_stats_stattab_1'::regclass
   AND attnum > 0 AND NOT attisdropped;
 column_count | not_null_columns 
--------------+------------------
           27 |                0
(1 row)
 
SELECT count(*) AS index_count
  FROM pg_index
 WHERE indrelid = 'dbms_stats_stattab_schema.dbms_stats_stattab_1'::regclass;
 index_count 
-------------
           0
(1 row)
 
SELECT count(*) AS constraint_count
  FROM pg_constraint
 WHERE conrelid = 'dbms_stats_stattab_schema.dbms_stats_stattab_1'::regclass;
 constraint_count 
------------------
                0
(1 row)
 
SELECT count(*) AS information_schema_columns,
       count(*) FILTER (WHERE is_nullable = 'YES') AS nullable_columns
  FROM information_schema.columns
 WHERE table_schema = 'dbms_stats_stattab_schema'
   AND table_name = 'dbms_stats_stattab_1';
 information_schema_columns | nullable_columns 
----------------------------+------------------
                         27 |               27
(1 row)
 
-- NULL ownname resolves to the first schema in the search path (public).
SELECT dbms_stats_create_stat_table(NULL, 'dbms_stats_stattab_public');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT n.nspname, c.relname
  FROM pg_class c
  JOIN pg_namespace n ON n.oid = c.relnamespace
 WHERE c.relname = 'dbms_stats_stattab_public';
 nspname |          relname          
---------+---------------------------
 public  | dbms_stats_stattab_public
(1 row)
 
-- Quoted mixed-case name is preserved.
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'MixedCase Stat Table');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT relname
  FROM pg_class
 WHERE relnamespace = 'dbms_stats_stattab_schema'::regnamespace
   AND relname = 'MixedCase Stat Table';
       relname        
----------------------
 MixedCase Stat Table
(1 row)
 
-- Three- and four-argument forms use the declared defaults.
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'dbms_stats_stattab_three_args', NULL);
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'dbms_stats_stattab_four_args', NULL, false);
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
-- Tablespace: an existing tablespace is applied, NULL uses the default.
SET allow_in_place_tablespaces = true;
CREATE TABLESPACE dbms_stats_stattab_space LOCATION '';
RESET allow_in_place_tablespaces;
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'dbms_stats_stattab_ts',
                                  'dbms_stats_stattab_space');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT c.reltablespace = (SELECT oid FROM pg_tablespace
                           WHERE spcname = 'dbms_stats_stattab_space')
         AS in_stattab_tablespace
  FROM pg_class c
 WHERE c.oid = 'dbms_stats_stattab_schema.dbms_stats_stattab_ts'::regclass;
 in_stattab_tablespace 
-----------------------
 t
(1 row)
 
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'dbms_stats_stattab_default_ts', NULL);
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT reltablespace
  FROM pg_class
 WHERE oid = 'dbms_stats_stattab_schema.dbms_stats_stattab_default_ts'::regclass;
 reltablespace 
---------------
             0
(1 row)
 
-- Missing tablespace fails before creating the table.
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'dbms_stats_stattab_missing_ts',
                                  'dbms_stats_no_such_tablespace');
ERROR:  Tablespace dbms_stats_no_such_tablespace does not exist
-- global_temporary => TRUE emits a NOTICE and creates a regular table.
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'dbms_stats_stattab_gtt', NULL, true);
NOTICE:  global_temporary is not supported in this version
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT relkind, relpersistence
  FROM pg_class
 WHERE oid = 'dbms_stats_stattab_schema.dbms_stats_stattab_gtt'::regclass;
 relkind | relpersistence 
---------+----------------
 r       | p
(1 row)
 
-- Duplicate names: tables, indexes, and views all block creation.
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'dbms_stats_stattab_1');
ERROR:  Unable to create table dbms_stats_stattab_1: already exists
CREATE TABLE dbms_stats_stattab_schema.dbms_stats_stattab_idx_target(id int);
CREATE INDEX dbms_stats_stattab_idx_name
  ON dbms_stats_stattab_schema.dbms_stats_stattab_idx_target(id);
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'dbms_stats_stattab_idx_name');
ERROR:  Unable to create table dbms_stats_stattab_idx_name: already exists
CREATE VIEW dbms_stats_stattab_schema.dbms_stats_stattab_view AS SELECT 1 AS x;
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'dbms_stats_stattab_view');
ERROR:  Unable to create table dbms_stats_stattab_view: already exists
SELECT relkind
  FROM pg_class
 WHERE oid = 'dbms_stats_stattab_schema.dbms_stats_stattab_idx_name'::regclass;
 relkind 
---------
 i
(1 row)
 
SELECT relkind
  FROM pg_class
 WHERE oid = 'dbms_stats_stattab_schema.dbms_stats_stattab_view'::regclass;
 relkind 
---------
 v
(1 row)
 
-- Missing schema, empty name, overlong name.
SELECT dbms_stats_create_stat_table('dbms_stats_no_such_schema',
                                  'dbms_stats_stattab_x');
ERROR:  Schema "dbms_stats_no_such_schema" does not exist
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema', '');
ERROR:  Statistics table must be specified
SELECT dbms_stats_create_stat_table(
  'dbms_stats_stattab_schema',
  'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
ERROR:  Statistics table name is too long
-- USAGE without CREATE is insufficient.
SET ROLE dbms_stats_stattab_limited;
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'dbms_stats_stattab_denied');
ERROR:  no privileges on schema dbms_stats_stattab_schema
RESET ROLE;
-- Creation participates in the caller's transaction.
BEGIN;
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'dbms_stats_stattab_rollback');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT to_regclass('dbms_stats_stattab_schema.dbms_stats_stattab_rollback')
       IS NOT NULL AS exists_before_rollback;
 exists_before_rollback 
------------------------
 t
(1 row)
 
ROLLBACK;
SELECT to_regclass('dbms_stats_stattab_schema.dbms_stats_stattab_rollback')
       IS NULL AS gone_after_rollback;
 gone_after_rollback 
---------------------
 t
(1 row)
 
BEGIN;
SELECT dbms_stats_create_stat_table('dbms_stats_stattab_schema',
                                  'dbms_stats_stattab_commit');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
COMMIT;
SELECT to_regclass('dbms_stats_stattab_schema.dbms_stats_stattab_commit')
       IS NOT NULL AS exists_after_commit;
 exists_after_commit 
---------------------
 t
(1 row)
 
-- Cleanup.
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_1;
DROP TABLE dbms_stats_stattab_schema."MixedCase Stat Table";
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_three_args;
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_four_args;
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_ts;
DROP TABLESPACE dbms_stats_stattab_space;
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_default_ts;
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_gtt;
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_idx_target;
DROP VIEW dbms_stats_stattab_schema.dbms_stats_stattab_view;
DROP TABLE dbms_stats_stattab_schema.dbms_stats_stattab_commit;
DROP TABLE dbms_stats_stattab_public;
REVOKE ALL ON SCHEMA dbms_stats_stattab_schema FROM dbms_stats_stattab_limited;
DROP SCHEMA dbms_stats_stattab_schema;
DROP ROLE dbms_stats_stattab_limited;
-- == drop_stat_table tests ==
-- DROP_STAT_TABLE removes validated user statistics tables only, honors
-- ownership, and participates in the caller's transaction.
CREATE SCHEMA dbms_stats_drop_schema;
CREATE SCHEMA dbms_stats_drop_other_schema;
CREATE ROLE dbms_stats_drop_limited;
GRANT USAGE ON SCHEMA dbms_stats_drop_schema TO dbms_stats_drop_limited;
-- Named and NULL owners, with representative rows.
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
                                    'dbms_stats_drop_named');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
INSERT INTO dbms_stats_drop_schema.dbms_stats_drop_named(statid, type)
VALUES ('dropme', 'T');
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
                                  'dbms_stats_drop_named');
 dbms_stats_drop_stat_table 
----------------------------
 
(1 row)
 
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_named') IS NULL
       AS dropped_named;
 dropped_named 
---------------
 t
(1 row)
 
SET search_path = dbms_stats_drop_schema, public;
SELECT dbms_stats_create_stat_table(NULL, 'dbms_stats_drop_null_owner');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
INSERT INTO dbms_stats_drop_null_owner(statid, type) VALUES ('dropme', 'T');
SELECT dbms_stats_drop_stat_table(NULL, 'dbms_stats_drop_null_owner');
 dbms_stats_drop_stat_table 
----------------------------
 
(1 row)
 
SELECT to_regclass('dbms_stats_drop_null_owner') IS NULL
       AS dropped_null_owner;
 dropped_null_owner 
--------------------
 t
(1 row)
 
RESET search_path;
-- Only the requested table is dropped from the requested schema.
SELECT dbms_stats_create_stat_table('dbms_stats_drop_other_schema',
                                    'dbms_stats_drop_other');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
                                    'dbms_stats_drop_target');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
                                  'dbms_stats_drop_target');
 dbms_stats_drop_stat_table 
----------------------------
 
(1 row)
 
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_target') IS NULL
       AS target_gone,
       to_regclass('dbms_stats_drop_other_schema.dbms_stats_drop_other')
         IS NOT NULL AS other_schema_kept;
 target_gone | other_schema_kept 
-------------+-------------------
 t           | t
(1 row)
 
DROP TABLE dbms_stats_drop_other_schema.dbms_stats_drop_other;
-- Mixed-case quoted identifiers are preserved; the unquoted spelling misses.
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
                                    'MixedCase Stat Table');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
                                  'mixedcase stat table');
ERROR:  Table "dbms_stats_drop_schema"."mixedcase stat table" does not exist or insufficient privileges
SELECT to_regclass('dbms_stats_drop_schema."MixedCase Stat Table"')
       IS NOT NULL AS mixed_case_kept;
 mixed_case_kept 
-----------------
 t
(1 row)
 
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
                                  'MixedCase Stat Table');
 dbms_stats_drop_stat_table 
----------------------------
 
(1 row)
 
SELECT to_regclass('dbms_stats_drop_schema."MixedCase Stat Table"')
       IS NULL AS dropped_mixed_case;
 dropped_mixed_case 
--------------------
 t
(1 row)
 
-- Missing schema, missing table, non-table relations, and plain tables fail.
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_no_such_schema',
                                  'dbms_stats_drop_any');
ERROR:  Schema "dbms_stats_drop_no_such_schema" does not exist
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
                                  'dbms_stats_drop_missing');
ERROR:  Table "dbms_stats_drop_schema"."dbms_stats_drop_missing" does not exist or insufficient privileges
CREATE VIEW dbms_stats_drop_schema.dbms_stats_drop_view AS SELECT 1 AS x;
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
                                  'dbms_stats_drop_view');
ERROR:  Bad user statistics table "dbms_stats_drop_view"
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_view') IS NOT NULL
       AS view_kept;
 view_kept 
-----------
 t
(1 row)
 
CREATE TABLE dbms_stats_drop_schema.dbms_stats_drop_plain(id int);
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
                                  'dbms_stats_drop_plain');
ERROR:  Bad user statistics table "dbms_stats_drop_plain"
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_plain') IS NOT NULL
       AS plain_table_kept;
 plain_table_kept 
------------------
 t
(1 row)
 
CREATE INDEX dbms_stats_drop_idx
  ON dbms_stats_drop_schema.dbms_stats_drop_plain(id);
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
                                  'dbms_stats_drop_idx');
ERROR:  Bad user statistics table "dbms_stats_drop_idx"
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_idx') IS NOT NULL
       AS index_kept;
 index_kept 
------------
 t
(1 row)
 
-- Empty and overlong names are rejected before catalog lookup.
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema', '');
ERROR:  Statistics table must be specified
SELECT dbms_stats_drop_stat_table(
  'dbms_stats_drop_schema',
  'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
ERROR:  Statistics table name is too long
-- A caller that can see but does not own the table cannot drop it.
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
                                    'dbms_stats_drop_priv');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
GRANT SELECT ON dbms_stats_drop_schema.dbms_stats_drop_priv
  TO dbms_stats_drop_limited;
SET ROLE dbms_stats_drop_limited;
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
                                  'dbms_stats_drop_priv');
ERROR:  must be owner of table dbms_stats_drop_priv
CONTEXT:  SQL statement "DROP TABLE dbms_stats_drop_schema.dbms_stats_drop_priv"
RESET ROLE;
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_priv') IS NOT NULL
       AS privilege_table_kept;
 privilege_table_kept 
----------------------
 t
(1 row)
 
-- Rollback restores the dropped table and its rows.
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
                                    'dbms_stats_drop_rollback');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
INSERT INTO dbms_stats_drop_schema.dbms_stats_drop_rollback(statid)
VALUES ('rollback');
BEGIN;
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
                                  'dbms_stats_drop_rollback');
 dbms_stats_drop_stat_table 
----------------------------
 
(1 row)
 
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_rollback') IS NULL
       AS gone_inside_transaction;
 gone_inside_transaction 
-------------------------
 t
(1 row)
 
ROLLBACK;
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_rollback') IS NOT NULL
       AS restored_after_rollback,
       (SELECT count(*) FROM dbms_stats_drop_schema.dbms_stats_drop_rollback)
       AS restored_rows;
 restored_after_rollback | restored_rows 
-------------------------+---------------
 t                       |             1
(1 row)
 
-- Commit makes the drop durable.
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
                                    'dbms_stats_drop_commit');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
BEGIN;
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
                                  'dbms_stats_drop_commit');
 dbms_stats_drop_stat_table 
----------------------------
 
(1 row)
 
COMMIT;
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_commit') IS NULL
       AS gone_after_commit;
 gone_after_commit 
-------------------
 t
(1 row)
 
-- Dependent objects block the non-cascading drop.
SELECT dbms_stats_create_stat_table('dbms_stats_drop_schema',
                                    'dbms_stats_drop_dep');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
CREATE VIEW dbms_stats_drop_schema.dbms_stats_drop_dep_view AS
  SELECT count(*) AS n FROM dbms_stats_drop_schema.dbms_stats_drop_dep;
SELECT dbms_stats_drop_stat_table('dbms_stats_drop_schema',
                                  'dbms_stats_drop_dep');
ERROR:  cannot drop table dbms_stats_drop_schema.dbms_stats_drop_dep because other objects depend on it
DETAIL:  view dbms_stats_drop_schema.dbms_stats_drop_dep_view depends on table dbms_stats_drop_schema.dbms_stats_drop_dep
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
CONTEXT:  SQL statement "DROP TABLE dbms_stats_drop_schema.dbms_stats_drop_dep"
SELECT to_regclass('dbms_stats_drop_schema.dbms_stats_drop_dep') IS NOT NULL
       AS dependent_table_kept,
       to_regclass('dbms_stats_drop_schema.dbms_stats_drop_dep_view') IS NOT NULL
       AS dependent_view_kept;
 dependent_table_kept | dependent_view_kept 
----------------------+---------------------
 t                    | t
(1 row)
 
-- Cleanup.
DROP VIEW dbms_stats_drop_schema.dbms_stats_drop_view;
DROP VIEW dbms_stats_drop_schema.dbms_stats_drop_dep_view;
DROP TABLE dbms_stats_drop_schema.dbms_stats_drop_plain;
DROP TABLE dbms_stats_drop_schema.dbms_stats_drop_priv;
DROP TABLE dbms_stats_drop_schema.dbms_stats_drop_rollback;
DROP TABLE dbms_stats_drop_schema.dbms_stats_drop_dep;
REVOKE ALL ON SCHEMA dbms_stats_drop_schema FROM dbms_stats_drop_limited;
DROP SCHEMA dbms_stats_drop_schema;
DROP SCHEMA dbms_stats_drop_other_schema;
DROP ROLE dbms_stats_drop_limited;
-- == export_table_stats tests ==
CREATE SCHEMA dbms_stats_export_schema;
CREATE ROLE dbms_stats_export_limited;
GRANT USAGE ON SCHEMA dbms_stats_export_schema TO dbms_stats_export_limited;
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_test(
  id int, val text, unused int);
INSERT INTO dbms_stats_export_schema.dbms_stats_export_test
SELECT g, 'value_' || (g % 10), NULL FROM generate_series(1, 1000) g;
CREATE INDEX dbms_stats_export_test_idx
  ON dbms_stats_export_schema.dbms_stats_export_test(id);
SELECT dbms_stats_gather_table_stats('dbms_stats_export_schema',
                                     'dbms_stats_export_test',
                                     estimate_percent => NULL,
                                     cascade => true);
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_test
  ADD COLUMN late int;
SELECT dbms_stats_create_stat_table('dbms_stats_export_schema',
                                  'dbms_stats_export_stat');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
-- Successful non-partitioned export with cascade.
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  statid => 'set1',
  cascade => true,
  stat_category => 'OBJECT_STATS');
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
-- Export holds a writer-serializing lock on the statistics table.
BEGIN;
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  statid => 'lockcheck',
  cascade => false,
  stat_category => 'OBJECT_STATS');
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT mode
  FROM pg_locks
 WHERE relation = 'dbms_stats_export_schema.dbms_stats_export_stat'::regclass
   AND pid = pg_backend_pid()
 ORDER BY mode;
         mode          
-----------------------
 RowExclusiveLock
 ShareRowExclusiveLock
(2 rows)
 
ROLLBACK;
SELECT type, c1, c2, c3, c4, c5, version, flags, n1, n2, n3
  FROM dbms_stats_export_schema.dbms_stats_export_stat
 WHERE statid = 'set1'
 ORDER BY type, c2, c4 NULLS LAST;
 type |            c1            |             c2             | c3 |   c4   |           c5           | version | flags |  n1  |  n2   |  n3  
------+--------------------------+----------------------------+----+--------+------------------------+---------+-------+------+-------+------
 C    | dbms_stats_export_schema | dbms_stats_export_test     |    | id     |                        | 1       | 0     | 1000 | 0.001 | 0
 C    | dbms_stats_export_schema | dbms_stats_export_test     |    | unused |                        | 1       | 0     | 0    | 0     | 1000
 C    | dbms_stats_export_schema | dbms_stats_export_test     |    | val    |                        | 1       | 0     | 10   | 0.1   | 0
 I    | dbms_stats_export_schema | dbms_stats_export_test_idx |    |        | dbms_stats_export_test | 1       | 0     | 1000 | 5     | 
 T    | dbms_stats_export_schema | dbms_stats_export_test     |    |        |                        | 1       | 0     | 1000 | 6     | 49
(5 rows)
 
SELECT c4,
       ch1, r1, r2, n5 AS n5_is_buckets
  FROM dbms_stats_export_schema.dbms_stats_export_stat
 WHERE statid = 'set1' AND type = 'C'
 ORDER BY c4;
   c4   |                                                                                                                                                                                                                                                                                                                                                                                 ch1                                                                                                                                                                                                                                                                                                                                                                                 |  r1  |     r2     | n5_is_buckets 
--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------+------------+---------------
 id     | {"min": "1", "max": "1000", "histogram": ["1", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100", "110", "120", "130", "140", "150", "160", "170", "180", "190", "200", "210", "220", "230", "240", "250", "260", "270", "280", "290", "300", "310", "320", "330", "340", "350", "360", "370", "380", "390", "400", "410", "420", "430", "440", "450", "460", "470", "480", "490", "500", "510", "520", "530", "540", "550", "560", "570", "580", "590", "600", "610", "620", "630", "640", "650", "660", "670", "680", "690", "700", "710", "720", "730", "740", "750", "760", "770", "780", "790", "800", "810", "820", "830", "840", "850", "860", "870", "880", "890", "900", "910", "920", "930", "940", "950", "960", "970", "980", "990", "1000"]} | \x31 | \x31303030 | 101
 unused |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |      |            | 0
 val    |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |      |            | 0
(3 rows)
 
-- NULL ownname uses the first schema in the search path.
SET search_path = dbms_stats_export_schema, public;
SELECT dbms_stats_export_table_stats(
  NULL,
  'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  statid => 'nullown',
  cascade => false,
  stat_category => 'OBJECT_STATS');
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
RESET search_path;
SELECT c1, c2
  FROM dbms_stats_export_schema.dbms_stats_export_stat
 WHERE statid = 'nullown';
            c1            |           c2           
--------------------------+------------------------
 dbms_stats_export_schema | dbms_stats_export_test
(1 row)
 
-- statown selects a statistics table from another schema.
CREATE SCHEMA dbms_stats_export_stat_schema;
SELECT dbms_stats_create_stat_table('dbms_stats_export_stat_schema',
                                  'dbms_stats_export_other_stat');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_other_stat',
  statown => 'dbms_stats_export_stat_schema',
  statid => 'other',
  cascade => false,
  stat_category => 'OBJECT_STATS');
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT c1, c2
  FROM dbms_stats_export_stat_schema.dbms_stats_export_other_stat
 WHERE statid = 'other';
            c1            |           c2           
--------------------------+------------------------
 dbms_stats_export_schema | dbms_stats_export_test
(1 row)
 
-- Cascade false exports only table rows.
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  statid => 'tableonly',
  cascade => false,
  stat_category => 'OBJECT_STATS');
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT type, count(*)
  FROM dbms_stats_export_schema.dbms_stats_export_stat
 WHERE statid = 'tableonly'
 GROUP BY type
 ORDER BY type;
 type | count 
------+-------
 T    |     1
(1 row)
 
-- A table with no collected statistics still yields a table row.
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_empty(id int, val text);
SELECT dbms_stats_create_stat_table('dbms_stats_export_schema',
                                  'dbms_stats_export_empty_stat');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_empty',
  stattab => 'dbms_stats_export_empty_stat',
  statid => 'empty',
  cascade => true,
  stat_category => 'OBJECT_STATS');
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT type, n1, n2, n3
  FROM dbms_stats_export_schema.dbms_stats_export_empty_stat
 WHERE statid = 'empty'
 ORDER BY type;
 type | n1 | n2 | n3 
------+----+----+----
 T    | 0  | 0  | 0
(1 row)
 
-- Partitioned and subpartitioned export.
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_part(id int, val int)
  PARTITION BY RANGE (id);
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_part_p1
  PARTITION OF dbms_stats_export_schema.dbms_stats_export_part
  FOR VALUES FROM (0) TO (100) PARTITION BY HASH (val);
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_part_p1_s0
  PARTITION OF dbms_stats_export_schema.dbms_stats_export_part_p1
  FOR VALUES WITH (MODULUS 2, REMAINDER 0);
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_part_p1_s1
  PARTITION OF dbms_stats_export_schema.dbms_stats_export_part_p1
  FOR VALUES WITH (MODULUS 2, REMAINDER 1);
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_part_p2
  PARTITION OF dbms_stats_export_schema.dbms_stats_export_part
  FOR VALUES FROM (100) TO (200);
INSERT INTO dbms_stats_export_schema.dbms_stats_export_part
SELECT g % 200, g FROM generate_series(1, 1000) g;
SELECT dbms_stats_gather_table_stats('dbms_stats_export_schema',
                                     'dbms_stats_export_part',
                                     estimate_percent => NULL,
                                     cascade => true);
 dbms_stats_gather_table_stats 
-------------------------------
 
(1 row)
 
SELECT dbms_stats_create_stat_table('dbms_stats_export_schema',
                                  'dbms_stats_export_part_stat');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_part',
  stattab => 'dbms_stats_export_part_stat',
  statid => 'partall',
  cascade => false,
  stat_category => 'OBJECT_STATS');
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT type, c1, c2, c3, c4, c5, version, flags, n1, n2, n3
  FROM dbms_stats_export_schema.dbms_stats_export_part_stat
 WHERE statid = 'partall'
 ORDER BY c3 NULLS FIRST;
 type |            c1            |           c2           |              c3              | c4 | c5 | version | flags |  n1  | n2 | n3 
------+--------------------------+------------------------+------------------------------+----+----+---------+-------+------+----+----
 T    | dbms_stats_export_schema | dbms_stats_export_part |                              |    |    | 1       | 0     | 1000 | -1 | 0
 T    | dbms_stats_export_schema | dbms_stats_export_part | dbms_stats_export_part_p1    |    |    | 1       | 0     | 500  | -1 | 0
 T    | dbms_stats_export_schema | dbms_stats_export_part | dbms_stats_export_part_p1_s0 |    |    | 1       | 0     | 259  | 2  | 63
 T    | dbms_stats_export_schema | dbms_stats_export_part | dbms_stats_export_part_p1_s1 |    |    | 1       | 0     | 241  | 2  | 68
 T    | dbms_stats_export_schema | dbms_stats_export_part | dbms_stats_export_part_p2    |    |    | 1       | 0     | 500  | 3  | 49
(5 rows)
 
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_part',
  partname => 'dbms_stats_export_part_p1',
  stattab => 'dbms_stats_export_part_stat',
  statid => 'partone',
  cascade => false,
  stat_category => 'OBJECT_STATS');
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT type, c1, c2, c3, c4, c5, version, flags, n1, n2, n3
  FROM dbms_stats_export_schema.dbms_stats_export_part_stat
 WHERE statid = 'partone'
 ORDER BY c3 NULLS LAST;
 type |            c1            |           c2           |            c3             | c4 | c5 | version | flags | n1  | n2 | n3 
------+--------------------------+------------------------+---------------------------+----+----+---------+-------+-----+----+----
 T    | dbms_stats_export_schema | dbms_stats_export_part | dbms_stats_export_part_p1 |    |    | 1       | 0     | 500 | -1 | 0
(1 row)
 
-- Re-export replaces the same statid; cascade=false preserves C/I rows.
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  statid => 'set1',
  cascade => false,
  stat_category => 'OBJECT_STATS');
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT type, count(*)
  FROM dbms_stats_export_schema.dbms_stats_export_stat
 WHERE statid = 'set1'
 GROUP BY type
 ORDER BY type;
 type | count 
------+-------
 C    |     3
 I    |     1
 T    |     1
(3 rows)
 
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  statid => 'set1',
  cascade => true,
  stat_category => 'OBJECT_STATS');
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT count(*) AS set1_rows
  FROM dbms_stats_export_schema.dbms_stats_export_stat
 WHERE statid = 'set1';
 set1_rows 
-----------
         5
(1 row)
 
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  statid => 'set2',
  cascade => true,
  stat_category => 'OBJECT_STATS');
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT statid, count(*)
  FROM dbms_stats_export_schema.dbms_stats_export_stat
 WHERE statid IN ('set1', 'set2')
 GROUP BY statid
 ORDER BY statid;
 statid | count 
--------+-------
 set1   |     5
 set2   |     5
(2 rows)
 
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  statid => NULL,
  cascade => false,
  stat_category => 'OBJECT_STATS');
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  statid => NULL,
  cascade => false,
  stat_category => 'OBJECT_STATS');
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT count(*) AS null_statid_rows
  FROM dbms_stats_export_schema.dbms_stats_export_stat
 WHERE statid IS NULL;
 null_statid_rows 
------------------
                1
(1 row)
 
-- A failed replacement rolls back to the previous export.
CREATE FUNCTION dbms_stats_export_schema.dbms_stats_export_fail()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
  IF NEW.statid = 'set1' THEN
    RAISE EXCEPTION 'forced export failure';
  END IF;
  RETURN NEW;
END$$;
CREATE TRIGGER dbms_stats_export_fail
  BEFORE INSERT ON dbms_stats_export_schema.dbms_stats_export_stat
  FOR EACH ROW EXECUTE FUNCTION
  dbms_stats_export_schema.dbms_stats_export_fail();
BEGIN;
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  statid => 'set1',
  cascade => true,
  stat_category => 'OBJECT_STATS');
ERROR:  forced export failure
CONTEXT:  PL/pgSQL function dbms_stats_export_schema.dbms_stats_export_fail() line 4 at RAISE
SQL statement "INSERT INTO dbms_stats_export_schema.dbms_stats_export_stat (statid, "type", version, flags, c1, c2, c3, c4, c5, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, d1, r1, r2, r3, ch1) VALUES ($1, $2, 1, 0, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25)"
ROLLBACK;
SELECT count(*) AS set1_rows_after_failed_replace
  FROM dbms_stats_export_schema.dbms_stats_export_stat
 WHERE statid = 'set1';
 set1_rows_after_failed_replace 
--------------------------------
                              5
(1 row)
 
DROP TRIGGER dbms_stats_export_fail
  ON dbms_stats_export_schema.dbms_stats_export_stat;
DROP FUNCTION dbms_stats_export_schema.dbms_stats_export_fail();
-- Category handling.
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  statid => 'catset',
  cascade => false,
  stat_category => 'OBJECT_STATS, SYNOPSES');
NOTICE:  stat_category value "SYNOPSES" is not currently supported
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT statid, count(*)
  FROM dbms_stats_export_schema.dbms_stats_export_stat
 WHERE statid = 'catset'
 GROUP BY statid;
 statid | count 
--------+-------
 catset |     1
(1 row)
 
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  statid => 'synonly',
  cascade => false,
  stat_category => 'SYNOPSES');
NOTICE:  stat_category value "SYNOPSES" is not currently supported
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT count(*) AS synonly_rows
  FROM dbms_stats_export_schema.dbms_stats_export_stat
 WHERE statid = 'synonly';
 synonly_rows 
--------------
            0
(1 row)
 
-- Error paths.
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => NULL);
ERROR:  Statistics table must be specified
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => '');
ERROR:  Statistics table must be specified
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_no_such_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat');
ERROR:  TABLE "dbms_stats_no_such_schema"."dbms_stats_export_test" does not exist or insufficient privileges
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_no_such_table',
  stattab => 'dbms_stats_export_stat');
ERROR:  TABLE "dbms_stats_export_schema"."dbms_stats_no_such_table" does not exist or insufficient privileges
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_part',
  partname => 'dbms_stats_no_such_part',
  stattab => 'dbms_stats_export_part_stat');
ERROR:  dbms_stats_no_such_part invalid partition name
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_missing_stat');
ERROR:  Table "dbms_stats_export_schema"."dbms_stats_export_missing_stat" does not exist or insufficient privileges
CREATE VIEW dbms_stats_export_schema.dbms_stats_export_view AS SELECT 1 AS x;
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_view');
ERROR:  Bad user statistics table "dbms_stats_export_view"
SELECT dbms_stats_create_stat_table('dbms_stats_export_schema',
                                  'dbms_stats_export_narrow');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_narrow
  ALTER COLUMN c1 TYPE varchar2(30);
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_narrow
  ALTER COLUMN c2 TYPE varchar2(30);
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_narrow
  ALTER COLUMN c3 TYPE varchar2(30);
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_narrow
  ALTER COLUMN c4 TYPE varchar2(30);
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_narrow
  ALTER COLUMN c5 TYPE varchar2(30);
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_narrow');
ERROR:  Bad user statistics table "dbms_stats_export_narrow"
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  stat_category => 'BOGUS');
ERROR:  Invalid or inconsistent input values
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat',
  statid => repeat('a', 31));
ERROR:  numeric or value error: character string buffer too small
-- Insufficient source privilege fails before touching the statistics table.
SET ROLE dbms_stats_export_limited;
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_test',
  stattab => 'dbms_stats_export_stat');
ERROR:  TABLE "dbms_stats_export_schema"."dbms_stats_export_test" does not exist or insufficient privileges
RESET ROLE;
-- A writer without statistics-table privileges is rejected.
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_limited_tbl(
  id int, val int);
INSERT INTO dbms_stats_export_schema.dbms_stats_export_limited_tbl
SELECT g, g FROM generate_series(1, 10) g;
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_limited_tbl
  OWNER TO dbms_stats_export_limited;
SET ROLE dbms_stats_export_limited;
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_limited_tbl',
  stattab => 'dbms_stats_export_stat');
ERROR:  TABLE "dbms_stats_export_schema"."dbms_stats_export_stat" does not exist or insufficient privileges
RESET ROLE;
-- Full-length PostgreSQL identifiers are stored untruncated.
SELECT dbms_stats_create_stat_table('dbms_stats_export_schema',
                                  'dbms_stats_export_long_stat');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
DO $$
DECLARE
  t name := repeat('t', 63);
  c name := repeat('c', 63);
  i name := repeat('i', 63);
BEGIN
  EXECUTE format('CREATE TABLE dbms_stats_export_schema.%I (%I int)', t, c);
  EXECUTE format('INSERT INTO dbms_stats_export_schema.%I '
                 'SELECT generate_series(1, 100)', t);
  EXECUTE format('CREATE INDEX %I ON dbms_stats_export_schema.%I (%I)',
                 i, t, c);
  EXECUTE format('ANALYZE dbms_stats_export_schema.%I', t);
  PERFORM dbms_stats_export_table_stats(
    ownname => 'dbms_stats_export_schema',
    tabname => t::varchar2,
    stattab => 'dbms_stats_export_long_stat',
    statid => 'long',
    cascade => true,
    stat_category => 'OBJECT_STATS');
END$$;
SELECT type, length(c1), length(c2), length(c4), length(c5)
  FROM dbms_stats_export_schema.dbms_stats_export_long_stat
 WHERE statid = 'long'
 ORDER BY type;
 type | length | length | length | length 
------+--------+--------+--------+--------
 C    |     24 |     63 |     63 |       
 I    |     24 |     63 |        |     63
 T    |     24 |     63 |        |       
(3 rows)
 
-- Oversized histogram payload becomes NULL with a NOTICE.
CREATE TABLE dbms_stats_export_schema.dbms_stats_export_hist(
  id int, big text);
INSERT INTO dbms_stats_export_schema.dbms_stats_export_hist
SELECT g, repeat(md5(g::text), 4) FROM generate_series(1, 1000) g;
ALTER TABLE dbms_stats_export_schema.dbms_stats_export_hist
  ALTER COLUMN big SET STATISTICS 100;
ANALYZE dbms_stats_export_schema.dbms_stats_export_hist;
SELECT dbms_stats_create_stat_table('dbms_stats_export_schema',
                                  'dbms_stats_export_hist_stat');
 dbms_stats_create_stat_table 
------------------------------
 
(1 row)
 
SELECT dbms_stats_export_table_stats(
  ownname => 'dbms_stats_export_schema',
  tabname => 'dbms_stats_export_hist',
  stattab => 'dbms_stats_export_hist_stat',
  statid => 'hist',
  cascade => true,
  stat_category => 'OBJECT_STATS');
NOTICE:  column "big" histogram payload exceeds varchar2(1000); stored as NULL
 dbms_stats_export_table_stats 
-------------------------------
 
(1 row)
 
SELECT type, c4, n5, ch1 IS NULL AS ch1_null, r1 IS NULL AS r1_null,
       r2 IS NULL AS r2_null
  FROM dbms_stats_export_schema.dbms_stats_export_hist_stat
 WHERE statid = 'hist' AND type = 'C' AND c4 = 'big';
 type | c4  | n5  | ch1_null | r1_null | r2_null 
------+-----+-----+----------+---------+---------
 C    | big | 101 | t        | f       | f
(1 row)
 
-- Cleanup.
DROP VIEW dbms_stats_export_schema.dbms_stats_export_view;
DROP TABLE dbms_stats_export_schema.dbms_stats_export_test;
DROP TABLE dbms_stats_export_schema.dbms_stats_export_empty;
DROP TABLE dbms_stats_export_schema.dbms_stats_export_part;
DROP TABLE dbms_stats_export_schema.dbms_stats_export_hist;
DROP TABLE dbms_stats_export_schema.dbms_stats_export_stat;
DROP TABLE dbms_stats_export_schema.dbms_stats_export_empty_stat;
DROP TABLE dbms_stats_export_schema.dbms_stats_export_part_stat;
DROP TABLE dbms_stats_export_schema.dbms_stats_export_narrow;
DROP TABLE dbms_stats_export_schema.dbms_stats_export_long_stat;
DROP TABLE dbms_stats_export_schema.dbms_stats_export_hist_stat;
DROP TABLE dbms_stats_export_stat_schema.dbms_stats_export_other_stat;
DROP SCHEMA dbms_stats_export_stat_schema;
DO $$
DECLARE
  t name := repeat('t', 63);
BEGIN
  EXECUTE format('DROP TABLE IF EXISTS dbms_stats_export_schema.%I', t);
END$$;
DROP OWNED BY dbms_stats_export_limited;
REVOKE ALL ON SCHEMA dbms_stats_export_schema FROM dbms_stats_export_limited;
DROP SCHEMA dbms_stats_export_schema;
DROP ROLE dbms_stats_export_limited;