aboutsummaryrefslogtreecommitdiff
path: root/src/test/regress/expected/partition_prune.out
blob: 9762cc97482104593d77b52982968a6f5f221a3d (plain)
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
--
-- Test partitioning planner code
--
create table lp (a char) partition by list (a);
create table lp_default partition of lp default;
create table lp_ef partition of lp for values in ('e', 'f');
create table lp_ad partition of lp for values in ('a', 'd');
create table lp_bc partition of lp for values in ('b', 'c');
create table lp_g partition of lp for values in ('g');
create table lp_null partition of lp for values in (null);
explain (costs off) select * from lp;
          QUERY PLAN          
------------------------------
 Append
   ->  Seq Scan on lp_ad
   ->  Seq Scan on lp_bc
   ->  Seq Scan on lp_ef
   ->  Seq Scan on lp_g
   ->  Seq Scan on lp_null
   ->  Seq Scan on lp_default
(7 rows)

explain (costs off) select * from lp where a > 'a' and a < 'd';
                        QUERY PLAN                         
-----------------------------------------------------------
 Append
   ->  Seq Scan on lp_bc
         Filter: ((a > 'a'::bpchar) AND (a < 'd'::bpchar))
   ->  Seq Scan on lp_default
         Filter: ((a > 'a'::bpchar) AND (a < 'd'::bpchar))
(5 rows)

explain (costs off) select * from lp where a > 'a' and a <= 'd';
                         QUERY PLAN                         
------------------------------------------------------------
 Append
   ->  Seq Scan on lp_ad
         Filter: ((a > 'a'::bpchar) AND (a <= 'd'::bpchar))
   ->  Seq Scan on lp_bc
         Filter: ((a > 'a'::bpchar) AND (a <= 'd'::bpchar))
   ->  Seq Scan on lp_default
         Filter: ((a > 'a'::bpchar) AND (a <= 'd'::bpchar))
(7 rows)

explain (costs off) select * from lp where a = 'a';
            QUERY PLAN             
-----------------------------------
 Append
   ->  Seq Scan on lp_ad
         Filter: (a = 'a'::bpchar)
(3 rows)

explain (costs off) select * from lp where 'a' = a;	/* commuted */
            QUERY PLAN             
-----------------------------------
 Append
   ->  Seq Scan on lp_ad
         Filter: ('a'::bpchar = a)
(3 rows)

explain (costs off) select * from lp where a is not null;
           QUERY PLAN            
---------------------------------
 Append
   ->  Seq Scan on lp_ad
         Filter: (a IS NOT NULL)
   ->  Seq Scan on lp_bc
         Filter: (a IS NOT NULL)
   ->  Seq Scan on lp_ef
         Filter: (a IS NOT NULL)
   ->  Seq Scan on lp_g
         Filter: (a IS NOT NULL)
   ->  Seq Scan on lp_default
         Filter: (a IS NOT NULL)
(11 rows)

explain (costs off) select * from lp where a is null;
         QUERY PLAN          
-----------------------------
 Append
   ->  Seq Scan on lp_null
         Filter: (a IS NULL)
(3 rows)

explain (costs off) select * from lp where a = 'a' or a = 'c';
                        QUERY PLAN                        
----------------------------------------------------------
 Append
   ->  Seq Scan on lp_ad
         Filter: ((a = 'a'::bpchar) OR (a = 'c'::bpchar))
   ->  Seq Scan on lp_bc
         Filter: ((a = 'a'::bpchar) OR (a = 'c'::bpchar))
(5 rows)

explain (costs off) select * from lp where a is not null and (a = 'a' or a = 'c');
                                   QUERY PLAN                                   
--------------------------------------------------------------------------------
 Append
   ->  Seq Scan on lp_ad
         Filter: ((a IS NOT NULL) AND ((a = 'a'::bpchar) OR (a = 'c'::bpchar)))
   ->  Seq Scan on lp_bc
         Filter: ((a IS NOT NULL) AND ((a = 'a'::bpchar) OR (a = 'c'::bpchar)))
(5 rows)

explain (costs off) select * from lp where a <> 'g';
             QUERY PLAN             
------------------------------------
 Append
   ->  Seq Scan on lp_ad
         Filter: (a <> 'g'::bpchar)
   ->  Seq Scan on lp_bc
         Filter: (a <> 'g'::bpchar)
   ->  Seq Scan on lp_ef
         Filter: (a <> 'g'::bpchar)
   ->  Seq Scan on lp_default
         Filter: (a <> 'g'::bpchar)
(9 rows)

explain (costs off) select * from lp where a <> 'a' and a <> 'd';
                         QUERY PLAN                          
-------------------------------------------------------------
 Append
   ->  Seq Scan on lp_bc
         Filter: ((a <> 'a'::bpchar) AND (a <> 'd'::bpchar))
   ->  Seq Scan on lp_ef
         Filter: ((a <> 'a'::bpchar) AND (a <> 'd'::bpchar))
   ->  Seq Scan on lp_g
         Filter: ((a <> 'a'::bpchar) AND (a <> 'd'::bpchar))
   ->  Seq Scan on lp_default
         Filter: ((a <> 'a'::bpchar) AND (a <> 'd'::bpchar))
(9 rows)

explain (costs off) select * from lp where a not in ('a', 'd');
                   QUERY PLAN                   
------------------------------------------------
 Append
   ->  Seq Scan on lp_bc
         Filter: (a <> ALL ('{a,d}'::bpchar[]))
   ->  Seq Scan on lp_ef
         Filter: (a <> ALL ('{a,d}'::bpchar[]))
   ->  Seq Scan on lp_g
         Filter: (a <> ALL ('{a,d}'::bpchar[]))
   ->  Seq Scan on lp_default
         Filter: (a <> ALL ('{a,d}'::bpchar[]))
(9 rows)

-- collation matches the partitioning collation, pruning works
create table coll_pruning (a text collate "C") partition by list (a);
create table coll_pruning_a partition of coll_pruning for values in ('a');
create table coll_pruning_b partition of coll_pruning for values in ('b');
create table coll_pruning_def partition of coll_pruning default;
explain (costs off) select * from coll_pruning where a collate "C" = 'a' collate "C";
                 QUERY PLAN                  
---------------------------------------------
 Append
   ->  Seq Scan on coll_pruning_a
         Filter: (a = 'a'::text COLLATE "C")
(3 rows)

-- collation doesn't match the partitioning collation, no pruning occurs
explain (costs off) select * from coll_pruning where a collate "POSIX" = 'a' collate "POSIX";
                       QUERY PLAN                        
---------------------------------------------------------
 Append
   ->  Seq Scan on coll_pruning_a
         Filter: ((a)::text = 'a'::text COLLATE "POSIX")
   ->  Seq Scan on coll_pruning_b
         Filter: ((a)::text = 'a'::text COLLATE "POSIX")
   ->  Seq Scan on coll_pruning_def
         Filter: ((a)::text = 'a'::text COLLATE "POSIX")
(7 rows)

create table rlp (a int, b varchar) partition by range (a);
create table rlp_default partition of rlp default partition by list (a);
create table rlp_default_default partition of rlp_default default;
create table rlp_default_10 partition of rlp_default for values in (10);
create table rlp_default_30 partition of rlp_default for values in (30);
create table rlp_default_null partition of rlp_default for values in (null);
create table rlp1 partition of rlp for values from (minvalue) to (1);
create table rlp2 partition of rlp for values from (1) to (10);
create table rlp3 (b varchar, a int) partition by list (b varchar_ops);
create table rlp3_default partition of rlp3 default;
create table rlp3abcd partition of rlp3 for values in ('ab', 'cd');
create table rlp3efgh partition of rlp3 for values in ('ef', 'gh');
create table rlp3nullxy partition of rlp3 for values in (null, 'xy');
alter table rlp attach partition rlp3 for values from (15) to (20);
create table rlp4 partition of rlp for values from (20) to (30) partition by range (a);
create table rlp4_default partition of rlp4 default;
create table rlp4_1 partition of rlp4 for values from (20) to (25);
create table rlp4_2 partition of rlp4 for values from (25) to (29);
create table rlp5 partition of rlp for values from (31) to (maxvalue) partition by range (a);
create table rlp5_default partition of rlp5 default;
create table rlp5_1 partition of rlp5 for values from (31) to (40);
explain (costs off) select * from rlp where a < 1;
       QUERY PLAN        
-------------------------
 Append
   ->  Seq Scan on rlp1
         Filter: (a < 1)
(3 rows)

explain (costs off) select * from rlp where 1 > a;	/* commuted */
       QUERY PLAN        
-------------------------
 Append
   ->  Seq Scan on rlp1
         Filter: (1 > a)
(3 rows)

explain (costs off) select * from rlp where a <= 1;
        QUERY PLAN        
--------------------------
 Append
   ->  Seq Scan on rlp1
         Filter: (a <= 1)
   ->  Seq Scan on rlp2
         Filter: (a <= 1)
(5 rows)

explain (costs off) select * from rlp where a = 1;
       QUERY PLAN        
-------------------------
 Append
   ->  Seq Scan on rlp2
         Filter: (a = 1)
(3 rows)

explain (costs off) select * from rlp where a = 1::bigint;		/* same as above */
            QUERY PLAN             
-----------------------------------
 Append
   ->  Seq Scan on rlp2
         Filter: (a = '1'::bigint)
(3 rows)

explain (costs off) select * from rlp where a = 1::numeric;		/* no pruning */
                  QUERY PLAN                   
-----------------------------------------------
 Append
   ->  Seq Scan on rlp1
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp2
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp3abcd
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp3efgh
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp3nullxy
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp3_default
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp4_1
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp4_2
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp4_default
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp5_1
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp5_default
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp_default_10
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp_default_30
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp_default_null
         Filter: ((a)::numeric = '1'::numeric)
   ->  Seq Scan on rlp_default_default
         Filter: ((a)::numeric = '1'::numeric)
(31 rows)

explain (costs off) select * from rlp where a <= 10;
              QUERY PLAN               
---------------------------------------
 Append
   ->  Seq Scan on rlp1
         Filter: (a <= 10)
   ->  Seq Scan on rlp2
         Filter: (a <= 10)
   ->  Seq Scan on rlp_default_10
         Filter: (a <= 10)
   ->  Seq Scan on rlp_default_default
         Filter: (a <= 10)
(9 rows)

explain (costs off) select * from rlp where a > 10;
              QUERY PLAN               
---------------------------------------
 Append
   ->  Seq Scan on rlp3abcd
         Filter: (a > 10)
   ->  Seq Scan on rlp3efgh
         Filter: (a > 10)
   ->  Seq Scan on rlp3nullxy
         Filter: (a > 10)
   ->  Seq Scan on rlp3_default
         Filter: (a > 10)
   ->  Seq Scan on rlp4_1
         Filter: (a > 10)
   ->  Seq Scan on rlp4_2
         Filter: (a > 10)
   ->  Seq Scan on rlp4_default
         Filter: (a > 10)
   ->  Seq Scan on rlp5_1
         Filter: (a > 10)
   ->  Seq Scan on rlp5_default
         Filter: (a > 10)
   ->  Seq Scan on rlp_default_30
         Filter: (a > 10)
   ->  Seq Scan on rlp_default_default
         Filter: (a > 10)
(23 rows)

explain (costs off) select * from rlp where a < 15;
              QUERY PLAN               
---------------------------------------
 Append
   ->  Seq Scan on rlp1
         Filter: (a < 15)
   ->  Seq Scan on rlp2
         Filter: (a < 15)
   ->  Seq Scan on rlp_default_10
         Filter: (a < 15)
   ->  Seq Scan on rlp_default_default
         Filter: (a < 15)
(9 rows)

explain (costs off) select * from rlp where a <= 15;
              QUERY PLAN               
---------------------------------------
 Append
   ->  Seq Scan on rlp1
         Filter: (a <= 15)
   ->  Seq Scan on rlp2
         Filter: (a <= 15)
   ->  Seq Scan on rlp3abcd
         Filter: (a <= 15)
   ->  Seq Scan on rlp3efgh
         Filter: (a <= 15)
   ->  Seq Scan on rlp3nullxy
         Filter: (a <= 15)
   ->  Seq Scan on rlp3_default
         Filter: (a <= 15)
   ->  Seq Scan on rlp_default_10
         Filter: (a <= 15)
   ->  Seq Scan on rlp_default_default
         Filter: (a <= 15)
(17 rows)

explain (costs off) select * from rlp where a > 15 and b = 'ab';
                       QUERY PLAN                        
---------------------------------------------------------
 Append
   ->  Seq Scan on rlp3abcd
         Filter: ((a > 15) AND ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp4_1
         Filter: ((a > 15) AND ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp4_2
         Filter: ((a > 15) AND ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp4_default
         Filter: ((a > 15) AND ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp5_1
         Filter: ((a > 15) AND ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp5_default
         Filter: ((a > 15) AND ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp_default_30
         Filter: ((a > 15) AND ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp_default_default
         Filter: ((a > 15) AND ((b)::text = 'ab'::text))
(17 rows)

explain (costs off) select * from rlp where a = 16;
           QUERY PLAN           
--------------------------------
 Append
   ->  Seq Scan on rlp3abcd
         Filter: (a = 16)
   ->  Seq Scan on rlp3efgh
         Filter: (a = 16)
   ->  Seq Scan on rlp3nullxy
         Filter: (a = 16)
   ->  Seq Scan on rlp3_default
         Filter: (a = 16)
(9 rows)

explain (costs off) select * from rlp where a = 16 and b in ('not', 'in', 'here');
                                 QUERY PLAN                                 
----------------------------------------------------------------------------
 Append
   ->  Seq Scan on rlp3_default
         Filter: ((a = 16) AND ((b)::text = ANY ('{not,in,here}'::text[])))
(3 rows)

explain (costs off) select * from rlp where a = 16 and b < 'ab';
                       QUERY PLAN                        
---------------------------------------------------------
 Append
   ->  Seq Scan on rlp3_default
         Filter: (((b)::text < 'ab'::text) AND (a = 16))
(3 rows)

explain (costs off) select * from rlp where a = 16 and b <= 'ab';
                        QUERY PLAN                        
----------------------------------------------------------
 Append
   ->  Seq Scan on rlp3abcd
         Filter: (((b)::text <= 'ab'::text) AND (a = 16))
   ->  Seq Scan on rlp3_default
         Filter: (((b)::text <= 'ab'::text) AND (a = 16))
(5 rows)

explain (costs off) select * from rlp where a = 16 and b is null;
                 QUERY PLAN                 
--------------------------------------------
 Append
   ->  Seq Scan on rlp3nullxy
         Filter: ((b IS NULL) AND (a = 16))
(3 rows)

explain (costs off) select * from rlp where a = 16 and b is not null;
                   QUERY PLAN                   
------------------------------------------------
 Append
   ->  Seq Scan on rlp3abcd
         Filter: ((b IS NOT NULL) AND (a = 16))
   ->  Seq Scan on rlp3efgh
         Filter: ((b IS NOT NULL) AND (a = 16))
   ->  Seq Scan on rlp3nullxy
         Filter: ((b IS NOT NULL) AND (a = 16))
   ->  Seq Scan on rlp3_default
         Filter: ((b IS NOT NULL) AND (a = 16))
(9 rows)

explain (costs off) select * from rlp where a is null;
             QUERY PLAN             
------------------------------------
 Append
   ->  Seq Scan on rlp_default_null
         Filter: (a IS NULL)
(3 rows)

explain (costs off) select * from rlp where a is not null;
              QUERY PLAN               
---------------------------------------
 Append
   ->  Seq Scan on rlp1
         Filter: (a IS NOT NULL)
   ->  Seq Scan on rlp2
         Filter: (a IS NOT NULL)
   ->  Seq Scan on rlp3abcd
         Filter: (a IS NOT NULL)
   ->  Seq Scan on rlp3efgh
         Filter: (a IS NOT NULL)
   ->  Seq Scan on rlp3nullxy
         Filter: (a IS NOT NULL)
   ->  Seq Scan on rlp3_default
         Filter: (a IS NOT NULL)
   ->  Seq Scan on rlp4_1
         Filter: (a IS NOT NULL)
   ->  Seq Scan on rlp4_2
         Filter: (a IS NOT NULL)
   ->  Seq Scan on rlp4_default
         Filter: (a IS NOT NULL)
   ->  Seq Scan on rlp5_1
         Filter: (a IS NOT NULL)
   ->  Seq Scan on rlp5_default
         Filter: (a IS NOT NULL)
   ->  Seq Scan on rlp_default_10
         Filter: (a IS NOT NULL)
   ->  Seq Scan on rlp_default_30
         Filter: (a IS NOT NULL)
   ->  Seq Scan on rlp_default_default
         Filter: (a IS NOT NULL)
(29 rows)

explain (costs off) select * from rlp where a > 30;
              QUERY PLAN               
---------------------------------------
 Append
   ->  Seq Scan on rlp5_1
         Filter: (a > 30)
   ->  Seq Scan on rlp5_default
         Filter: (a > 30)
   ->  Seq Scan on rlp_default_default
         Filter: (a > 30)
(7 rows)

explain (costs off) select * from rlp where a = 30;	/* only default is scanned */
            QUERY PLAN            
----------------------------------
 Append
   ->  Seq Scan on rlp_default_30
         Filter: (a = 30)
(3 rows)

explain (costs off) select * from rlp where a <= 31;
              QUERY PLAN               
---------------------------------------
 Append
   ->  Seq Scan on rlp1
         Filter: (a <= 31)
   ->  Seq Scan on rlp2
         Filter: (a <= 31)
   ->  Seq Scan on rlp3abcd
         Filter: (a <= 31)
   ->  Seq Scan on rlp3efgh
         Filter: (a <= 31)
   ->  Seq Scan on rlp3nullxy
         Filter: (a <= 31)
   ->  Seq Scan on rlp3_default
         Filter: (a <= 31)
   ->  Seq Scan on rlp4_1
         Filter: (a <= 31)
   ->  Seq Scan on rlp4_2
         Filter: (a <= 31)
   ->  Seq Scan on rlp4_default
         Filter: (a <= 31)
   ->  Seq Scan on rlp5_1
         Filter: (a <= 31)
   ->  Seq Scan on rlp5_default
         Filter: (a <= 31)
   ->  Seq Scan on rlp_default_10
         Filter: (a <= 31)
   ->  Seq Scan on rlp_default_30
         Filter: (a <= 31)
   ->  Seq Scan on rlp_default_default
         Filter: (a <= 31)
(29 rows)

explain (costs off) select * from rlp where a = 1 or a = 7;
              QUERY PLAN              
--------------------------------------
 Append
   ->  Seq Scan on rlp2
         Filter: ((a = 1) OR (a = 7))
(3 rows)

explain (costs off) select * from rlp where a = 1 or b = 'ab';
                      QUERY PLAN                       
-------------------------------------------------------
 Append
   ->  Seq Scan on rlp1
         Filter: ((a = 1) OR ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp2
         Filter: ((a = 1) OR ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp3abcd
         Filter: ((a = 1) OR ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp4_1
         Filter: ((a = 1) OR ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp4_2
         Filter: ((a = 1) OR ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp4_default
         Filter: ((a = 1) OR ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp5_1
         Filter: ((a = 1) OR ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp5_default
         Filter: ((a = 1) OR ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp_default_10
         Filter: ((a = 1) OR ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp_default_30
         Filter: ((a = 1) OR ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp_default_null
         Filter: ((a = 1) OR ((b)::text = 'ab'::text))
   ->  Seq Scan on rlp_default_default
         Filter: ((a = 1) OR ((b)::text = 'ab'::text))
(25 rows)

explain (costs off) select * from rlp where a > 20 and a < 27;
               QUERY PLAN                
-----------------------------------------
 Append
   ->  Seq Scan on rlp4_1
         Filter: ((a > 20) AND (a < 27))
   ->  Seq Scan on rlp4_2
         Filter: ((a > 20) AND (a < 27))
   ->  Seq Scan on rlp4_default
         Filter: ((a > 20) AND (a < 27))
   ->  Seq Scan on rlp_default_default
         Filter: ((a > 20) AND (a < 27))
(9 rows)

explain (costs off) select * from rlp where a = 29;
           QUERY PLAN           
--------------------------------
 Append
   ->  Seq Scan on rlp4_default
         Filter: (a = 29)
(3 rows)

explain (costs off) select * from rlp where a >= 29;
              QUERY PLAN               
---------------------------------------
 Append
   ->  Seq Scan on rlp4_default
         Filter: (a >= 29)
   ->  Seq Scan on rlp5_1
         Filter: (a >= 29)
   ->  Seq Scan on rlp5_default
         Filter: (a >= 29)
   ->  Seq Scan on rlp_default_30
         Filter: (a >= 29)
   ->  Seq Scan on rlp_default_default
         Filter: (a >= 29)
(11 rows)

-- redundant clauses are eliminated
explain (costs off) select * from rlp where a > 1 and a = 10;	/* only default */
               QUERY PLAN               
----------------------------------------
 Append
   ->  Seq Scan on rlp_default_10
         Filter: ((a > 1) AND (a = 10))
(3 rows)

explain (costs off) select * from rlp where a > 1 and a >=15;	/* rlp3 onwards, including default */
               QUERY PLAN                
-----------------------------------------
 Append
   ->  Seq Scan on rlp3abcd
         Filter: ((a > 1) AND (a >= 15))
   ->  Seq Scan on rlp3efgh
         Filter: ((a > 1) AND (a >= 15))
   ->  Seq Scan on rlp3nullxy
         Filter: ((a > 1) AND (a >= 15))
   ->  Seq Scan on rlp3_default
         Filter: ((a > 1) AND (a >= 15))
   ->  Seq Scan on rlp4_1
         Filter: ((a > 1) AND (a >= 15))
   ->  Seq Scan on rlp4_2
         Filter: ((a > 1) AND (a >= 15))
   ->  Seq Scan on rlp4_default
         Filter: ((a > 1) AND (a >= 15))
   ->  Seq Scan on rlp5_1
         Filter: ((a > 1) AND (a >= 15))
   ->  Seq Scan on rlp5_default
         Filter: ((a > 1) AND (a >= 15))
   ->  Seq Scan on rlp_default_30
         Filter: ((a > 1) AND (a >= 15))
   ->  Seq Scan on rlp_default_default
         Filter: ((a > 1) AND (a >= 15))
(23 rows)

explain (costs off) select * from rlp where a = 1 and a = 3;	/* empty */
        QUERY PLAN        
--------------------------
 Result
   One-Time Filter: false
(2 rows)

explain (costs off) select * from rlp where (a = 1 and a = 3) or (a > 1 and a = 15);
                            QUERY PLAN                             
-------------------------------------------------------------------
 Append
   ->  Seq Scan on rlp2
         Filter: (((a = 1) AND (a = 3)) OR ((a > 1) AND (a = 15)))
   ->  Seq Scan on rlp3abcd
         Filter: (((a = 1) AND (a = 3)) OR ((a > 1) AND (a = 15)))
   ->  Seq Scan on rlp3efgh
         Filter: (((a = 1) AND (a = 3)) OR ((a > 1) AND (a = 15)))
   ->  Seq Scan on rlp3nullxy
         Filter: (((a = 1) AND (a = 3)) OR ((a > 1) AND (a = 15)))
   ->  Seq Scan on rlp3_default
         Filter: (((a = 1) AND (a = 3)) OR ((a > 1) AND (a = 15)))
(11 rows)

-- multi-column keys
create table mc3p (a int, b int, c int) partition by range (a, abs(b), c);
create table mc3p_default partition of mc3p default;
create table mc3p0 partition of mc3p for values from (minvalue, minvalue, minvalue) to (1, 1, 1);
create table mc3p1 partition of mc3p for values from (1, 1, 1) to (10, 5, 10);
create table mc3p2 partition of mc3p for values from (10, 5, 10) to (10, 10, 10);
create table mc3p3 partition of mc3p for values from (10, 10, 10) to (10, 10, 20);
create table mc3p4 partition of mc3p for values from (10, 10, 20) to (10, maxvalue, maxvalue);
create table mc3p5 partition of mc3p for values from (11, 1, 1) to (20, 10, 10);
create table mc3p6 partition of mc3p for values from (20, 10, 10) to (20, 20, 20);
create table mc3p7 partition of mc3p for values from (20, 20, 20) to (maxvalue, maxvalue, maxvalue);
explain (costs off) select * from mc3p where a = 1;
           QUERY PLAN           
--------------------------------
 Append
   ->  Seq Scan on mc3p0
         Filter: (a = 1)
   ->  Seq Scan on mc3p1
         Filter: (a = 1)
   ->  Seq Scan on mc3p_default
         Filter: (a = 1)
(7 rows)

explain (costs off) select * from mc3p where a = 1 and abs(b) < 1;
                 QUERY PLAN                 
--------------------------------------------
 Append
   ->  Seq Scan on mc3p0
         Filter: ((a = 1) AND (abs(b) < 1))
   ->  Seq Scan on mc3p_default
         Filter: ((a = 1) AND (abs(b) < 1))
(5 rows)

explain (costs off) select * from mc3p where a = 1 and abs(b) = 1;
                 QUERY PLAN                 
--------------------------------------------
 Append
   ->  Seq Scan on mc3p0
         Filter: ((a = 1) AND (abs(b) = 1))
   ->  Seq Scan on mc3p1
         Filter: ((a = 1) AND (abs(b) = 1))
   ->  Seq Scan on mc3p_default
         Filter: ((a = 1) AND (abs(b) = 1))
(7 rows)

explain (costs off) select * from mc3p where a = 1 and abs(b) = 1 and c < 8;
                       QUERY PLAN                       
--------------------------------------------------------
 Append
   ->  Seq Scan on mc3p0
         Filter: ((c < 8) AND (a = 1) AND (abs(b) = 1))
   ->  Seq Scan on mc3p1
         Filter: ((c < 8) AND (a = 1) AND (abs(b) = 1))
(5 rows)

explain (costs off) select * from mc3p where a = 10 and abs(b) between 5 and 35;
                           QUERY PLAN                            
-----------------------------------------------------------------
 Append
   ->  Seq Scan on mc3p1
         Filter: ((a = 10) AND (abs(b) >= 5) AND (abs(b) <= 35))
   ->  Seq Scan on mc3p2
         Filter: ((a = 10) AND (abs(b) >= 5) AND (abs(b) <= 35))
   ->  Seq Scan on mc3p3
         Filter: ((a = 10) AND (abs(b) >= 5) AND (abs(b) <= 35))
   ->  Seq Scan on mc3p4
         Filter: ((a = 10) AND (abs(b) >= 5) AND (abs(b) <= 35))
   ->  Seq Scan on mc3p_default
         Filter: ((a = 10) AND (abs(b) >= 5) AND (abs(b) <= 35))
(11 rows)

explain (costs off) select * from mc3p where a > 10;
           QUERY PLAN           
--------------------------------
 Append
   ->  Seq Scan on mc3p5
         Filter: (a > 10)
   ->  Seq Scan on mc3p6
         Filter: (a > 10)
   ->  Seq Scan on mc3p7
         Filter: (a > 10)
   ->  Seq Scan on mc3p_default
         Filter: (a > 10)
(9 rows)

explain (costs off) select * from mc3p where a >= 10;
           QUERY PLAN           
--------------------------------
 Append
   ->  Seq Scan on mc3p1
         Filter: (a >= 10)
   ->  Seq Scan on mc3p2
         Filter: (a >= 10)
   ->  Seq Scan on mc3p3
         Filter: (a >= 10)
   ->  Seq Scan on mc3p4
         Filter: (a >= 10)
   ->  Seq Scan on mc3p5
         Filter: (a >= 10)
   ->  Seq Scan on mc3p6
         Filter: (a >= 10)
   ->  Seq Scan on mc3p7
         Filter: (a >= 10)
   ->  Seq Scan on mc3p_default
         Filter: (a >= 10)
(17 rows)

explain (costs off) select * from mc3p where a < 10;
           QUERY PLAN           
--------------------------------
 Append
   ->  Seq Scan on mc3p0
         Filter: (a < 10)
   ->  Seq Scan on mc3p1
         Filter: (a < 10)
   ->  Seq Scan on mc3p_default
         Filter: (a < 10)
(7 rows)

explain (costs off) select * from mc3p where a <= 10 and abs(b) < 10;
                  QUERY PLAN                   
-----------------------------------------------
 Append
   ->  Seq Scan on mc3p0
         Filter: ((a <= 10) AND (abs(b) < 10))
   ->  Seq Scan on mc3p1
         Filter: ((a <= 10) AND (abs(b) < 10))
   ->  Seq Scan on mc3p2
         Filter: ((a <= 10) AND (abs(b) < 10))
   ->  Seq Scan on mc3p_default
         Filter: ((a <= 10) AND (abs(b) < 10))
(9 rows)

explain (costs off) select * from mc3p where a = 11 and abs(b) = 0;
                 QUERY PLAN                  
---------------------------------------------
 Append
   ->  Seq Scan on mc3p_default
         Filter: ((a = 11) AND (abs(b) = 0))
(3 rows)

explain (costs off) select * from mc3p where a = 20 and abs(b) = 10 and c = 100;
                         QUERY PLAN                         
------------------------------------------------------------
 Append
   ->  Seq Scan on mc3p6
         Filter: ((a = 20) AND (c = 100) AND (abs(b) = 10))
(3 rows)

explain (costs off) select * from mc3p where a > 20;
           QUERY PLAN           
--------------------------------
 Append
   ->  Seq Scan on mc3p7
         Filter: (a > 20)
   ->  Seq Scan on mc3p_default
         Filter: (a > 20)
(5 rows)

explain (costs off) select * from mc3p where a >= 20;
           QUERY PLAN           
--------------------------------
 Append
   ->  Seq Scan on mc3p5
         Filter: (a >= 20)
   ->  Seq Scan on mc3p6
         Filter: (a >= 20)
   ->  Seq Scan on mc3p7
         Filter: (a >= 20)
   ->  Seq Scan on mc3p_default
         Filter: (a >= 20)
(9 rows)

explain (costs off) select * from mc3p where (a = 1 and abs(b) = 1 and c = 1) or (a = 10 and abs(b) = 5 and c = 10) or (a > 11 and a < 20);
                                                           QUERY PLAN                                                            
---------------------------------------------------------------------------------------------------------------------------------
 Append
   ->  Seq Scan on mc3p1
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)))
   ->  Seq Scan on mc3p2
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)))
   ->  Seq Scan on mc3p5
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)))
   ->  Seq Scan on mc3p_default
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)))
(9 rows)

explain (costs off) select * from mc3p where (a = 1 and abs(b) = 1 and c = 1) or (a = 10 and abs(b) = 5 and c = 10) or (a > 11 and a < 20) or a < 1;
                                                                 QUERY PLAN                                                                 
--------------------------------------------------------------------------------------------------------------------------------------------
 Append
   ->  Seq Scan on mc3p0
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)) OR (a < 1))
   ->  Seq Scan on mc3p1
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)) OR (a < 1))
   ->  Seq Scan on mc3p2
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)) OR (a < 1))
   ->  Seq Scan on mc3p5
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)) OR (a < 1))
   ->  Seq Scan on mc3p_default
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)) OR (a < 1))
(11 rows)

explain (costs off) select * from mc3p where (a = 1 and abs(b) = 1 and c = 1) or (a = 10 and abs(b) = 5 and c = 10) or (a > 11 and a < 20) or a < 1 or a = 1;
                                                                      QUERY PLAN                                                                       
-------------------------------------------------------------------------------------------------------------------------------------------------------
 Append
   ->  Seq Scan on mc3p0
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)) OR (a < 1) OR (a = 1))
   ->  Seq Scan on mc3p1
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)) OR (a < 1) OR (a = 1))
   ->  Seq Scan on mc3p2
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)) OR (a < 1) OR (a = 1))
   ->  Seq Scan on mc3p5
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)) OR (a < 1) OR (a = 1))
   ->  Seq Scan on mc3p_default
         Filter: (((a = 1) AND (abs(b) = 1) AND (c = 1)) OR ((a = 10) AND (abs(b) = 5) AND (c = 10)) OR ((a > 11) AND (a < 20)) OR (a < 1) OR (a = 1))
(11 rows)

explain (costs off) select * from mc3p where a = 1 or abs(b) = 1 or c = 1;
                      QUERY PLAN                      
------------------------------------------------------
 Append
   ->  Seq Scan on mc3p0
         Filter: ((a = 1) OR (abs(b) = 1) OR (c = 1))
   ->  Seq Scan on mc3p1
         Filter: ((a = 1) OR (abs(b) = 1) OR (c = 1))
   ->  Seq Scan on mc3p2
         Filter: ((a = 1) OR (abs(b) = 1) OR (c = 1))
   ->  Seq Scan on mc3p3
         Filter: ((a = 1) OR (abs(b) = 1) OR (c = 1))
   ->  Seq Scan on mc3p4
         Filter: ((a = 1) OR (abs(b) = 1) OR (c = 1))
   ->  Seq Scan on mc3p5
         Filter: ((a = 1) OR (abs(b) = 1) OR (c = 1))
   ->  Seq Scan on mc3p6
         Filter: ((a = 1) OR (abs(b) = 1) OR (c = 1))
   ->  Seq Scan on mc3p7
         Filter: ((a = 1) OR (abs(b) = 1) OR (c = 1))
   ->  Seq Scan on mc3p_default
         Filter: ((a = 1) OR (abs(b) = 1) OR (c = 1))
(19 rows)

explain (costs off) select * from mc3p where (a = 1 and abs(b) = 1) or (a = 10 and abs(b) = 10);
                                  QUERY PLAN                                  
------------------------------------------------------------------------------
 Append
   ->  Seq Scan on mc3p0
         Filter: (((a = 1) AND (abs(b) = 1)) OR ((a = 10) AND (abs(b) = 10)))
   ->  Seq Scan on mc3p1
         Filter: (((a = 1) AND (abs(b) = 1)) OR ((a = 10) AND (abs(b) = 10)))
   ->  Seq Scan on mc3p2
         Filter: (((a = 1) AND (abs(b) = 1)) OR ((a = 10) AND (abs(b) = 10)))
   ->  Seq Scan on mc3p3
         Filter: (((a = 1) AND (abs(b) = 1)) OR ((a = 10) AND (abs(b) = 10)))
   ->  Seq Scan on mc3p4
         Filter: (((a = 1) AND (abs(b) = 1)) OR ((a = 10) AND (abs(b) = 10)))
   ->  Seq Scan on mc3p_default
         Filter: (((a = 1) AND (abs(b) = 1)) OR ((a = 10) AND (abs(b) = 10)))
(13 rows)

explain (costs off) select * from mc3p where (a = 1 and abs(b) = 1) or (a = 10 and abs(b) = 9);
                                 QUERY PLAN                                  
-----------------------------------------------------------------------------
 Append
   ->  Seq Scan on mc3p0
         Filter: (((a = 1) AND (abs(b) = 1)) OR ((a = 10) AND (abs(b) = 9)))
   ->  Seq Scan on mc3p1
         Filter: (((a = 1) AND (abs(b) = 1)) OR ((a = 10) AND (abs(b) = 9)))
   ->  Seq Scan on mc3p2
         Filter: (((a = 1) AND (abs(b) = 1)) OR ((a = 10) AND (abs(b) = 9)))
   ->  Seq Scan on mc3p_default
         Filter: (((a = 1) AND (abs(b) = 1)) OR ((a = 10) AND (abs(b) = 9)))
(9 rows)

-- a simpler multi-column keys case
create table mc2p (a int, b int) partition by range (a, b);
create table mc2p_default partition of mc2p default;
create table mc2p0 partition of mc2p for values from (minvalue, minvalue) to (1, minvalue);
create table mc2p1 partition of mc2p for values from (1, minvalue) to (1, 1);
create table mc2p2 partition of mc2p for values from (1, 1) to (2, minvalue);
create table mc2p3 partition of mc2p for values from (2, minvalue) to (2, 1);
create table mc2p4 partition of mc2p for values from (2, 1) to (2, maxvalue);
create table mc2p5 partition of mc2p for values from (2, maxvalue) to (maxvalue, maxvalue);
explain (costs off) select * from mc2p where a < 2;
           QUERY PLAN           
--------------------------------
 Append
   ->  Seq Scan on mc2p0
         Filter: (a < 2)
   ->  Seq Scan on mc2p1
         Filter: (a < 2)
   ->  Seq Scan on mc2p2
         Filter: (a < 2)
   ->  Seq Scan on mc2p_default
         Filter: (a < 2)
(9 rows)

explain (costs off) select * from mc2p where a = 2 and b < 1;
              QUERY PLAN               
---------------------------------------
 Append
   ->  Seq Scan on mc2p3
         Filter: ((b < 1) AND (a = 2))
(3 rows)

explain (costs off) select * from mc2p where a > 1;
           QUERY PLAN           
--------------------------------
 Append
   ->  Seq Scan on mc2p2
         Filter: (a > 1)
   ->  Seq Scan on mc2p3
         Filter: (a > 1)
   ->  Seq Scan on mc2p4
         Filter: (a > 1)
   ->  Seq Scan on mc2p5
         Filter: (a > 1)
   ->  Seq Scan on mc2p_default
         Filter: (a > 1)
(11 rows)

explain (costs off) select * from mc2p where a = 1 and b > 1;
              QUERY PLAN               
---------------------------------------
 Append
   ->  Seq Scan on mc2p2
         Filter: ((b > 1) AND (a = 1))
(3 rows)

-- boolean partitioning
create table boolpart (a bool) partition by list (a);
create table boolpart_default partition of boolpart default;
create table boolpart_t partition of boolpart for values in ('true');
create table boolpart_f partition of boolpart for values in ('false');
explain (costs off) select * from boolpart where a in (true, false);
                   QUERY PLAN                   
------------------------------------------------
 Append
   ->  Seq Scan on boolpart_f
         Filter: (a = ANY ('{t,f}'::boolean[]))
   ->  Seq Scan on boolpart_t
         Filter: (a = ANY ('{t,f}'::boolean[]))
(5 rows)

explain (costs off) select * from boolpart where a = false;
          QUERY PLAN          
------------------------------
 Append
   ->  Seq Scan on boolpart_f
         Filter: (NOT a)
(3 rows)

explain (costs off) select * from boolpart where not a = false;
          QUERY PLAN          
------------------------------
 Append
   ->  Seq Scan on boolpart_t
         Filter: a
(3 rows)

explain (costs off) select * from boolpart where a is true or a is not true;
                    QUERY PLAN                    
--------------------------------------------------
 Append
   ->  Seq Scan on boolpart_f
         Filter: ((a IS TRUE) OR (a IS NOT TRUE))
   ->  Seq Scan on boolpart_t
         Filter: ((a IS TRUE) OR (a IS NOT TRUE))
(5 rows)

explain (costs off) select * from boolpart where a is not true;
           QUERY PLAN            
---------------------------------
 Append
   ->  Seq Scan on boolpart_f
         Filter: (a IS NOT TRUE)
(3 rows)

explain (costs off) select * from boolpart where a is not true and a is not false;
        QUERY PLAN        
--------------------------
 Result
   One-Time Filter: false
(2 rows)

explain (costs off) select * from boolpart where a is unknown;
             QUERY PLAN             
------------------------------------
 Append
   ->  Seq Scan on boolpart_f
         Filter: (a IS UNKNOWN)
   ->  Seq Scan on boolpart_t
         Filter: (a IS UNKNOWN)
   ->  Seq Scan on boolpart_default
         Filter: (a IS UNKNOWN)
(7 rows)

explain (costs off) select * from boolpart where a is not unknown;
             QUERY PLAN             
------------------------------------
 Append
   ->  Seq Scan on boolpart_f
         Filter: (a IS NOT UNKNOWN)
   ->  Seq Scan on boolpart_t
         Filter: (a IS NOT UNKNOWN)
   ->  Seq Scan on boolpart_default
         Filter: (a IS NOT UNKNOWN)
(7 rows)

--
-- some more cases
--
--
-- pruning for partitioned table appearing inside a sub-query
--
-- pruning won't work for mc3p, because some keys are Params
explain (costs off) select * from mc2p t1, lateral (select count(*) from mc3p t2 where t2.a = t1.b and abs(t2.b) = 1 and t2.c = 1) s where t1.a = 1;
                              QUERY PLAN                               
-----------------------------------------------------------------------
 Nested Loop
   ->  Append
         ->  Seq Scan on mc2p1 t1
               Filter: (a = 1)
         ->  Seq Scan on mc2p2 t1_1
               Filter: (a = 1)
         ->  Seq Scan on mc2p_default t1_2
               Filter: (a = 1)
   ->  Aggregate
         ->  Append
               ->  Seq Scan on mc3p0 t2
                     Filter: ((a = t1.b) AND (c = 1) AND (abs(b) = 1))
               ->  Seq Scan on mc3p1 t2_1
                     Filter: ((a = t1.b) AND (c = 1) AND (abs(b) = 1))
               ->  Seq Scan on mc3p2 t2_2
                     Filter: ((a = t1.b) AND (c = 1) AND (abs(b) = 1))
               ->  Seq Scan on mc3p3 t2_3
                     Filter: ((a = t1.b) AND (c = 1) AND (abs(b) = 1))
               ->  Seq Scan on mc3p4 t2_4
                     Filter: ((a = t1.b) AND (c = 1) AND (abs(b) = 1))
               ->  Seq Scan on mc3p5 t2_5
                     Filter: ((a = t1.b) AND (c = 1) AND (abs(b) = 1))
               ->  Seq Scan on mc3p6 t2_6
                     Filter: ((a = t1.b) AND (c = 1) AND (abs(b) = 1))
               ->  Seq Scan on mc3p7 t2_7
                     Filter: ((a = t1.b) AND (c = 1) AND (abs(b) = 1))
               ->  Seq Scan on mc3p_default t2_8
                     Filter: ((a = t1.b) AND (c = 1) AND (abs(b) = 1))
(28 rows)

-- pruning should work fine, because values for a prefix of keys (a, b) are
-- available
explain (costs off) select * from mc2p t1, lateral (select count(*) from mc3p t2 where t2.c = t1.b and abs(t2.b) = 1 and t2.a = 1) s where t1.a = 1;
                              QUERY PLAN                               
-----------------------------------------------------------------------
 Nested Loop
   ->  Append
         ->  Seq Scan on mc2p1 t1
               Filter: (a = 1)
         ->  Seq Scan on mc2p2 t1_1
               Filter: (a = 1)
         ->  Seq Scan on mc2p_default t1_2
               Filter: (a = 1)
   ->  Aggregate
         ->  Append
               ->  Seq Scan on mc3p0 t2
                     Filter: ((c = t1.b) AND (a = 1) AND (abs(b) = 1))
               ->  Seq Scan on mc3p1 t2_1
                     Filter: ((c = t1.b) AND (a = 1) AND (abs(b) = 1))
               ->  Seq Scan on mc3p_default t2_2
                     Filter: ((c = t1.b) AND (a = 1) AND (abs(b) = 1))
(16 rows)

-- also here, because values for all keys are provided
explain (costs off) select * from mc2p t1, lateral (select count(*) from mc3p t2 where t2.a = 1 and abs(t2.b) = 1 and t2.c = 1) s where t1.a = 1;
                             QUERY PLAN                             
--------------------------------------------------------------------
 Nested Loop
   ->  Aggregate
         ->  Append
               ->  Seq Scan on mc3p1 t2
                     Filter: ((a = 1) AND (c = 1) AND (abs(b) = 1))
   ->  Append
         ->  Seq Scan on mc2p1 t1
               Filter: (a = 1)
         ->  Seq Scan on mc2p2 t1_1
               Filter: (a = 1)
         ->  Seq Scan on mc2p_default t1_2
               Filter: (a = 1)
(12 rows)

--
-- pruning with clauses containing <> operator
--
-- doesn't prune range partitions
create table rp (a int) partition by range (a);
create table rp0 partition of rp for values from (minvalue) to (1);
create table rp1 partition of rp for values from (1) to (2);
create table rp2 partition of rp for values from (2) to (maxvalue);
explain (costs off) select * from rp where a <> 1;
        QUERY PLAN        
--------------------------
 Append
   ->  Seq Scan on rp0
         Filter: (a <> 1)
   ->  Seq Scan on rp1
         Filter: (a <> 1)
   ->  Seq Scan on rp2
         Filter: (a <> 1)
(7 rows)

explain (costs off) select * from rp where a <> 1 and a <> 2;
               QUERY PLAN                
-----------------------------------------
 Append
   ->  Seq Scan on rp0
         Filter: ((a <> 1) AND (a <> 2))
   ->  Seq Scan on rp1
         Filter: ((a <> 1) AND (a <> 2))
   ->  Seq Scan on rp2
         Filter: ((a <> 1) AND (a <> 2))
(7 rows)

-- null partition should be eliminated due to strict <> clause.
explain (costs off) select * from lp where a <> 'a';
             QUERY PLAN             
------------------------------------
 Append
   ->  Seq Scan on lp_ad
         Filter: (a <> 'a'::bpchar)
   ->  Seq Scan on lp_bc
         Filter: (a <> 'a'::bpchar)
   ->  Seq Scan on lp_ef
         Filter: (a <> 'a'::bpchar)
   ->  Seq Scan on lp_g
         Filter: (a <> 'a'::bpchar)
   ->  Seq Scan on lp_default
         Filter: (a <> 'a'::bpchar)
(11 rows)

-- ensure we detect contradictions in clauses; a can't be NULL and NOT NULL.
explain (costs off) select * from lp where a <> 'a' and a is null;
        QUERY PLAN        
--------------------------
 Result
   One-Time Filter: false
(2 rows)

explain (costs off) select * from lp where (a <> 'a' and a <> 'd') or a is null;
                                  QUERY PLAN                                  
------------------------------------------------------------------------------
 Append
   ->  Seq Scan on lp_bc
         Filter: (((a <> 'a'::bpchar) AND (a <> 'd'::bpchar)) OR (a IS NULL))
   ->  Seq Scan on lp_ef
         Filter: (((a <> 'a'::bpchar) AND (a <> 'd'::bpchar)) OR (a IS NULL))
   ->  Seq Scan on lp_g
         Filter: (((a <> 'a'::bpchar) AND (a <> 'd'::bpchar)) OR (a IS NULL))
   ->  Seq Scan on lp_null
         Filter: (((a <> 'a'::bpchar) AND (a <> 'd'::bpchar)) OR (a IS NULL))
   ->  Seq Scan on lp_default
         Filter: (((a <> 'a'::bpchar) AND (a <> 'd'::bpchar)) OR (a IS NULL))
(11 rows)

-- check that it also works for a partitioned table that's not root,
-- which in this case are partitions of rlp that are themselves
-- list-partitioned on b
explain (costs off) select * from rlp where a = 15 and b <> 'ab' and b <> 'cd' and b <> 'xy' and b is not null;
                                                                QUERY PLAN                                                                
------------------------------------------------------------------------------------------------------------------------------------------
 Append
   ->  Seq Scan on rlp3efgh
         Filter: ((b IS NOT NULL) AND ((b)::text <> 'ab'::text) AND ((b)::text <> 'cd'::text) AND ((b)::text <> 'xy'::text) AND (a = 15))
   ->  Seq Scan on rlp3_default
         Filter: ((b IS NOT NULL) AND ((b)::text <> 'ab'::text) AND ((b)::text <> 'cd'::text) AND ((b)::text <> 'xy'::text) AND (a = 15))
(5 rows)

--
-- different collations for different keys with same expression
--
create table coll_pruning_multi (a text) partition by range (substr(a, 1) collate "POSIX", substr(a, 1) collate "C");
create table coll_pruning_multi1 partition of coll_pruning_multi for values from ('a', 'a') to ('a', 'e');
create table coll_pruning_multi2 partition of coll_pruning_multi for values from ('a', 'e') to ('a', 'z');
create table coll_pruning_multi3 partition of coll_pruning_multi for values from ('b', 'a') to ('b', 'e');
-- no pruning, because no value for the leading key
explain (costs off) select * from coll_pruning_multi where substr(a, 1) = 'e' collate "C";
                       QUERY PLAN                       
--------------------------------------------------------
 Append
   ->  Seq Scan on coll_pruning_multi1
         Filter: (substr(a, 1) = 'e'::text COLLATE "C")
   ->  Seq Scan on coll_pruning_multi2
         Filter: (substr(a, 1) = 'e'::text COLLATE "C")
   ->  Seq Scan on coll_pruning_multi3
         Filter: (substr(a, 1) = 'e'::text COLLATE "C")
(7 rows)

-- pruning, with a value provided for the leading key
explain (costs off) select * from coll_pruning_multi where substr(a, 1) = 'a' collate "POSIX";
                         QUERY PLAN                         
------------------------------------------------------------
 Append
   ->  Seq Scan on coll_pruning_multi1
         Filter: (substr(a, 1) = 'a'::text COLLATE "POSIX")
   ->  Seq Scan on coll_pruning_multi2
         Filter: (substr(a, 1) = 'a'::text COLLATE "POSIX")
(5 rows)

-- pruning, with values provided for both keys
explain (costs off) select * from coll_pruning_multi where substr(a, 1) = 'e' collate "C" and substr(a, 1) = 'a' collate "POSIX";
                                               QUERY PLAN                                                
---------------------------------------------------------------------------------------------------------
 Append
   ->  Seq Scan on coll_pruning_multi2
         Filter: ((substr(a, 1) = 'e'::text COLLATE "C") AND (substr(a, 1) = 'a'::text COLLATE "POSIX"))
(3 rows)

--
-- LIKE operators don't prune
--
create table like_op_noprune (a text) partition by list (a);
create table like_op_noprune1 partition of like_op_noprune for values in ('ABC');
create table like_op_noprune2 partition of like_op_noprune for values in ('BCD');
explain (costs off) select * from like_op_noprune where a like '%BC';
             QUERY PLAN             
------------------------------------
 Append
   ->  Seq Scan on like_op_noprune1
         Filter: (a ~~ '%BC'::text)
   ->  Seq Scan on like_op_noprune2
         Filter: (a ~~ '%BC'::text)
(5 rows)

--
-- tests wherein clause value requires a cross-type comparison function
--
create table lparted_by_int2 (a smallint) partition by list (a);
create table lparted_by_int2_1 partition of lparted_by_int2 for values in (1);
create table lparted_by_int2_16384 partition of lparted_by_int2 for values in (16384);
explain (costs off) select * from lparted_by_int2 where a = 100000000000000;
        QUERY PLAN        
--------------------------
 Result
   One-Time Filter: false
(2 rows)

create table rparted_by_int2 (a smallint) partition by range (a);
create table rparted_by_int2_1 partition of rparted_by_int2 for values from (1) to (10);
create table rparted_by_int2_16384 partition of rparted_by_int2 for values from (10) to (16384);
-- all partitions pruned
explain (costs off) select * from rparted_by_int2 where a > 100000000000000;
        QUERY PLAN        
--------------------------
 Result
   One-Time Filter: false
(2 rows)

create table rparted_by_int2_maxvalue partition of rparted_by_int2 for values from (16384) to (maxvalue);
-- all partitions but rparted_by_int2_maxvalue pruned
explain (costs off) select * from rparted_by_int2 where a > 100000000000000;
                   QUERY PLAN                    
-------------------------------------------------
 Append
   ->  Seq Scan on rparted_by_int2_maxvalue
         Filter: (a > '100000000000000'::bigint)
(3 rows)

drop table lp, coll_pruning, rlp, mc3p, mc2p, boolpart, rp, coll_pruning_multi, like_op_noprune, lparted_by_int2, rparted_by_int2;
--
-- Test runtime partition pruning
--
create table ab (a int not null, b int not null) partition by list (a);
create table ab_a2 partition of ab for values in(2) partition by list (b);
create table ab_a2_b1 partition of ab_a2 for values in (1);
create table ab_a2_b2 partition of ab_a2 for values in (2);
create table ab_a2_b3 partition of ab_a2 for values in (3);
create table ab_a1 partition of ab for values in(1) partition by list (b);
create table ab_a1_b1 partition of ab_a1 for values in (1);
create table ab_a1_b2 partition of ab_a1 for values in (2);
create table ab_a1_b3 partition of ab_a1 for values in (3);
create table ab_a3 partition of ab for values in(3) partition by list (b);
create table ab_a3_b1 partition of ab_a3 for values in (1);
create table ab_a3_b2 partition of ab_a3 for values in (2);
create table ab_a3_b3 partition of ab_a3 for values in (3);
prepare ab_q1 (int, int, int) as
select * from ab where a between $1 and $2 and b <= $3;
-- Execute query 5 times to allow choose_custom_plan
-- to start considering a generic plan.
execute ab_q1 (1, 8, 3);
 a | b 
---+---
(0 rows)

execute ab_q1 (1, 8, 3);
 a | b 
---+---
(0 rows)

execute ab_q1 (1, 8, 3);
 a | b 
---+---
(0 rows)

execute ab_q1 (1, 8, 3);
 a | b 
---+---
(0 rows)

execute ab_q1 (1, 8, 3);
 a | b 
---+---
(0 rows)

explain (analyze, costs off, summary off, timing off) execute ab_q1 (2, 2, 3);
                       QUERY PLAN                        
---------------------------------------------------------
 Append (actual rows=0 loops=1)
   Subplans Removed: 6
   ->  Seq Scan on ab_a2_b1 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b <= $3))
   ->  Seq Scan on ab_a2_b2 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b <= $3))
   ->  Seq Scan on ab_a2_b3 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b <= $3))
(8 rows)

explain (analyze, costs off, summary off, timing off) execute ab_q1 (1, 2, 3);
                       QUERY PLAN                        
---------------------------------------------------------
 Append (actual rows=0 loops=1)
   Subplans Removed: 3
   ->  Seq Scan on ab_a1_b1 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b <= $3))
   ->  Seq Scan on ab_a1_b2 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b <= $3))
   ->  Seq Scan on ab_a1_b3 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b <= $3))
   ->  Seq Scan on ab_a2_b1 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b <= $3))
   ->  Seq Scan on ab_a2_b2 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b <= $3))
   ->  Seq Scan on ab_a2_b3 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b <= $3))
(14 rows)

deallocate ab_q1;
-- Runtime pruning after optimizer pruning
prepare ab_q1 (int, int) as
select a from ab where a between $1 and $2 and b < 3;
-- Execute query 5 times to allow choose_custom_plan
-- to start considering a generic plan.
execute ab_q1 (1, 8);
 a 
---
(0 rows)

execute ab_q1 (1, 8);
 a 
---
(0 rows)

execute ab_q1 (1, 8);
 a 
---
(0 rows)

execute ab_q1 (1, 8);
 a 
---
(0 rows)

execute ab_q1 (1, 8);
 a 
---
(0 rows)

explain (analyze, costs off, summary off, timing off) execute ab_q1 (2, 2);
                      QUERY PLAN                       
-------------------------------------------------------
 Append (actual rows=0 loops=1)
   Subplans Removed: 4
   ->  Seq Scan on ab_a2_b1 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b < 3))
   ->  Seq Scan on ab_a2_b2 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b < 3))
(6 rows)

explain (analyze, costs off, summary off, timing off) execute ab_q1 (2, 4);
                      QUERY PLAN                       
-------------------------------------------------------
 Append (actual rows=0 loops=1)
   Subplans Removed: 2
   ->  Seq Scan on ab_a2_b1 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b < 3))
   ->  Seq Scan on ab_a2_b2 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b < 3))
   ->  Seq Scan on ab_a3_b1 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b < 3))
   ->  Seq Scan on ab_a3_b2 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b < 3))
(10 rows)

-- Ensure a mix of external and exec params work together at different
-- levels of partitioning.
prepare ab_q2 (int, int) as
select a from ab where a between $1 and $2 and b < (select 3);
execute ab_q2 (1, 8);
 a 
---
(0 rows)

execute ab_q2 (1, 8);
 a 
---
(0 rows)

execute ab_q2 (1, 8);
 a 
---
(0 rows)

execute ab_q2 (1, 8);
 a 
---
(0 rows)

execute ab_q2 (1, 8);
 a 
---
(0 rows)

explain (analyze, costs off, summary off, timing off) execute ab_q2 (2, 2);
                       QUERY PLAN                       
--------------------------------------------------------
 Append (actual rows=0 loops=1)
   InitPlan 1 (returns $0)
     ->  Result (actual rows=1 loops=1)
   Subplans Removed: 6
   ->  Seq Scan on ab_a2_b1 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b < $0))
   ->  Seq Scan on ab_a2_b2 (actual rows=0 loops=1)
         Filter: ((a >= $1) AND (a <= $2) AND (b < $0))
   ->  Seq Scan on ab_a2_b3 (never executed)
         Filter: ((a >= $1) AND (a <= $2) AND (b < $0))
(10 rows)

-- As above, but with swap the exec param to the first partition level
prepare ab_q3 (int, int) as
select a from ab where b between $1 and $2 and a < (select 3);
execute ab_q3 (1, 8);
 a 
---
(0 rows)

execute ab_q3 (1, 8);
 a 
---
(0 rows)

execute ab_q3 (1, 8);
 a 
---
(0 rows)

execute ab_q3 (1, 8);
 a 
---
(0 rows)

execute ab_q3 (1, 8);
 a 
---
(0 rows)

explain (analyze, costs off, summary off, timing off) execute ab_q3 (2, 2);
                       QUERY PLAN                       
--------------------------------------------------------
 Append (actual rows=0 loops=1)
   InitPlan 1 (returns $0)
     ->  Result (actual rows=1 loops=1)
   Subplans Removed: 6
   ->  Seq Scan on ab_a1_b2 (actual rows=0 loops=1)
         Filter: ((b >= $1) AND (b <= $2) AND (a < $0))
   ->  Seq Scan on ab_a2_b2 (actual rows=0 loops=1)
         Filter: ((b >= $1) AND (b <= $2) AND (a < $0))
   ->  Seq Scan on ab_a3_b2 (never executed)
         Filter: ((b >= $1) AND (b <= $2) AND (a < $0))
(10 rows)

-- Parallel append
prepare ab_q4 (int, int) as
select avg(a) from ab where a between $1 and $2 and b < 4;
-- Encourage use of parallel plans
set parallel_setup_cost = 0;
set parallel_tuple_cost = 0;
set min_parallel_table_scan_size = 0;
set max_parallel_workers_per_gather = 2;
-- Execute query 5 times to allow choose_custom_plan
-- to start considering a generic plan.
execute ab_q4 (1, 8);
 avg 
-----
    
(1 row)

execute ab_q4 (1, 8);
 avg 
-----
    
(1 row)

execute ab_q4 (1, 8);
 avg 
-----
    
(1 row)

execute ab_q4 (1, 8);
 avg 
-----
    
(1 row)

execute ab_q4 (1, 8);
 avg 
-----
    
(1 row)

explain (analyze, costs off, summary off, timing off) execute ab_q4 (2, 2);
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Finalize Aggregate (actual rows=1 loops=1)
   ->  Gather (actual rows=3 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         ->  Partial Aggregate (actual rows=1 loops=3)
               ->  Parallel Append (actual rows=0 loops=3)
                     Subplans Removed: 6
                     ->  Parallel Seq Scan on ab_a2_b1 (actual rows=0 loops=1)
                           Filter: ((a >= $1) AND (a <= $2) AND (b < 4))
                     ->  Parallel Seq Scan on ab_a2_b2 (actual rows=0 loops=1)
                           Filter: ((a >= $1) AND (a <= $2) AND (b < 4))
                     ->  Parallel Seq Scan on ab_a2_b3 (actual rows=0 loops=1)
                           Filter: ((a >= $1) AND (a <= $2) AND (b < 4))
(13 rows)

-- Test run-time pruning with IN lists.
prepare ab_q5 (int, int, int) as
select avg(a) from ab where a in($1,$2,$3) and b < 4;
-- Execute query 5 times to allow choose_custom_plan
-- to start considering a generic plan.
execute ab_q5 (1, 2, 3);
 avg 
-----
    
(1 row)

execute ab_q5 (1, 2, 3);
 avg 
-----
    
(1 row)

execute ab_q5 (1, 2, 3);
 avg 
-----
    
(1 row)

execute ab_q5 (1, 2, 3);
 avg 
-----
    
(1 row)

execute ab_q5 (1, 2, 3);
 avg 
-----
    
(1 row)

explain (analyze, costs off, summary off, timing off) execute ab_q5 (1, 1, 1);
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Finalize Aggregate (actual rows=1 loops=1)
   ->  Gather (actual rows=3 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         ->  Partial Aggregate (actual rows=1 loops=3)
               ->  Parallel Append (actual rows=0 loops=3)
                     Subplans Removed: 6
                     ->  Parallel Seq Scan on ab_a1_b1 (actual rows=0 loops=1)
                           Filter: ((b < 4) AND (a = ANY (ARRAY[$1, $2, $3])))
                     ->  Parallel Seq Scan on ab_a1_b2 (actual rows=0 loops=1)
                           Filter: ((b < 4) AND (a = ANY (ARRAY[$1, $2, $3])))
                     ->  Parallel Seq Scan on ab_a1_b3 (actual rows=0 loops=1)
                           Filter: ((b < 4) AND (a = ANY (ARRAY[$1, $2, $3])))
(13 rows)

explain (analyze, costs off, summary off, timing off) execute ab_q5 (2, 3, 3);
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Finalize Aggregate (actual rows=1 loops=1)
   ->  Gather (actual rows=3 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         ->  Partial Aggregate (actual rows=1 loops=3)
               ->  Parallel Append (actual rows=0 loops=3)
                     Subplans Removed: 3
                     ->  Parallel Seq Scan on ab_a2_b1 (actual rows=0 loops=1)
                           Filter: ((b < 4) AND (a = ANY (ARRAY[$1, $2, $3])))
                     ->  Parallel Seq Scan on ab_a2_b2 (actual rows=0 loops=1)
                           Filter: ((b < 4) AND (a = ANY (ARRAY[$1, $2, $3])))
                     ->  Parallel Seq Scan on ab_a2_b3 (actual rows=0 loops=1)
                           Filter: ((b < 4) AND (a = ANY (ARRAY[$1, $2, $3])))
                     ->  Parallel Seq Scan on ab_a3_b1 (actual rows=0 loops=1)
                           Filter: ((b < 4) AND (a = ANY (ARRAY[$1, $2, $3])))
                     ->  Parallel Seq Scan on ab_a3_b2 (actual rows=0 loops=1)
                           Filter: ((b < 4) AND (a = ANY (ARRAY[$1, $2, $3])))
                     ->  Parallel Seq Scan on ab_a3_b3 (actual rows=0 loops=1)
                           Filter: ((b < 4) AND (a = ANY (ARRAY[$1, $2, $3])))
(19 rows)

-- Try some params whose values do not belong to any partition.
-- We'll still get a single subplan in this case, but it should not be scanned.
explain (analyze, costs off, summary off, timing off) execute ab_q5 (33, 44, 55);
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Finalize Aggregate (actual rows=1 loops=1)
   ->  Gather (actual rows=3 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         ->  Partial Aggregate (actual rows=1 loops=3)
               ->  Parallel Append (actual rows=0 loops=3)
                     Subplans Removed: 8
                     ->  Parallel Seq Scan on ab_a1_b1 (never executed)
                           Filter: ((b < 4) AND (a = ANY (ARRAY[$1, $2, $3])))
(9 rows)

-- Test parallel Append with IN list and parameterized nested loops
create table lprt_a (a int not null);
-- Insert some values we won't find in ab
insert into lprt_a select 0 from generate_series(1,100);
-- and insert some values that we should find.
insert into lprt_a values(1),(1);
analyze lprt_a;
create index ab_a2_b1_a_idx on ab_a2_b1 (a);
create index ab_a2_b2_a_idx on ab_a2_b2 (a);
create index ab_a2_b3_a_idx on ab_a2_b3 (a);
create index ab_a1_b1_a_idx on ab_a1_b1 (a);
create index ab_a1_b2_a_idx on ab_a1_b2 (a);
create index ab_a1_b3_a_idx on ab_a1_b3 (a);
create index ab_a3_b1_a_idx on ab_a3_b1 (a);
create index ab_a3_b2_a_idx on ab_a3_b2 (a);
create index ab_a3_b3_a_idx on ab_a3_b3 (a);
set enable_hashjoin = 0;
set enable_mergejoin = 0;
prepare ab_q6 (int, int, int) as
select avg(ab.a) from ab inner join lprt_a a on ab.a = a.a where a.a in($1,$2,$3);
execute ab_q6 (1, 2, 3);
 avg 
-----
    
(1 row)

execute ab_q6 (1, 2, 3);
 avg 
-----
    
(1 row)

execute ab_q6 (1, 2, 3);
 avg 
-----
    
(1 row)

execute ab_q6 (1, 2, 3);
 avg 
-----
    
(1 row)

execute ab_q6 (1, 2, 3);
 avg 
-----
    
(1 row)

explain (analyze, costs off, summary off, timing off) execute ab_q6 (0, 0, 1);
                                               QUERY PLAN                                               
--------------------------------------------------------------------------------------------------------
 Finalize Aggregate (actual rows=1 loops=1)
   ->  Gather (actual rows=2 loops=1)
         Workers Planned: 1
         Workers Launched: 1
         ->  Partial Aggregate (actual rows=1 loops=2)
               ->  Nested Loop (actual rows=0 loops=2)
                     ->  Parallel Seq Scan on lprt_a a (actual rows=51 loops=2)
                           Filter: (a = ANY ('{0,0,1}'::integer[]))
                     ->  Append (actual rows=0 loops=102)
                           ->  Index Only Scan using ab_a1_b1_a_idx on ab_a1_b1 (actual rows=0 loops=2)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a1_b2_a_idx on ab_a1_b2 (actual rows=0 loops=2)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a1_b3_a_idx on ab_a1_b3 (actual rows=0 loops=2)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a2_b1_a_idx on ab_a2_b1 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a2_b2_a_idx on ab_a2_b2 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a2_b3_a_idx on ab_a2_b3 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a3_b1_a_idx on ab_a3_b1 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a3_b2_a_idx on ab_a3_b2 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a3_b3_a_idx on ab_a3_b3 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
(36 rows)

insert into lprt_a values(3),(3);
explain (analyze, costs off, summary off, timing off) execute ab_q6 (1, 0, 3);
                                               QUERY PLAN                                               
--------------------------------------------------------------------------------------------------------
 Finalize Aggregate (actual rows=1 loops=1)
   ->  Gather (actual rows=2 loops=1)
         Workers Planned: 1
         Workers Launched: 1
         ->  Partial Aggregate (actual rows=1 loops=2)
               ->  Nested Loop (actual rows=0 loops=2)
                     ->  Parallel Seq Scan on lprt_a a (actual rows=52 loops=2)
                           Filter: (a = ANY ('{1,0,3}'::integer[]))
                     ->  Append (actual rows=0 loops=104)
                           ->  Index Only Scan using ab_a1_b1_a_idx on ab_a1_b1 (actual rows=0 loops=2)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a1_b2_a_idx on ab_a1_b2 (actual rows=0 loops=2)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a1_b3_a_idx on ab_a1_b3 (actual rows=0 loops=2)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a2_b1_a_idx on ab_a2_b1 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a2_b2_a_idx on ab_a2_b2 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a2_b3_a_idx on ab_a2_b3 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a3_b1_a_idx on ab_a3_b1 (actual rows=0 loops=2)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a3_b2_a_idx on ab_a3_b2 (actual rows=0 loops=2)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a3_b3_a_idx on ab_a3_b3 (actual rows=0 loops=2)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
(36 rows)

explain (analyze, costs off, summary off, timing off) execute ab_q6 (1, 0, 0);
                                               QUERY PLAN                                               
--------------------------------------------------------------------------------------------------------
 Finalize Aggregate (actual rows=1 loops=1)
   ->  Gather (actual rows=2 loops=1)
         Workers Planned: 1
         Workers Launched: 1
         ->  Partial Aggregate (actual rows=1 loops=2)
               ->  Nested Loop (actual rows=0 loops=2)
                     ->  Parallel Seq Scan on lprt_a a (actual rows=51 loops=2)
                           Filter: (a = ANY ('{1,0,0}'::integer[]))
                           Rows Removed by Filter: 1
                     ->  Append (actual rows=0 loops=102)
                           ->  Index Only Scan using ab_a1_b1_a_idx on ab_a1_b1 (actual rows=0 loops=2)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a1_b2_a_idx on ab_a1_b2 (actual rows=0 loops=2)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a1_b3_a_idx on ab_a1_b3 (actual rows=0 loops=2)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a2_b1_a_idx on ab_a2_b1 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a2_b2_a_idx on ab_a2_b2 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a2_b3_a_idx on ab_a2_b3 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a3_b1_a_idx on ab_a3_b1 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a3_b2_a_idx on ab_a3_b2 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a3_b3_a_idx on ab_a3_b3 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
(37 rows)

delete from lprt_a where a = 1;
explain (analyze, costs off, summary off, timing off) execute ab_q6 (1, 0, 0);
                                           QUERY PLAN                                            
-------------------------------------------------------------------------------------------------
 Finalize Aggregate (actual rows=1 loops=1)
   ->  Gather (actual rows=2 loops=1)
         Workers Planned: 1
         Workers Launched: 1
         ->  Partial Aggregate (actual rows=1 loops=2)
               ->  Nested Loop (actual rows=0 loops=2)
                     ->  Parallel Seq Scan on lprt_a a (actual rows=50 loops=2)
                           Filter: (a = ANY ('{1,0,0}'::integer[]))
                           Rows Removed by Filter: 1
                     ->  Append (actual rows=0 loops=100)
                           ->  Index Only Scan using ab_a1_b1_a_idx on ab_a1_b1 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a1_b2_a_idx on ab_a1_b2 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a1_b3_a_idx on ab_a1_b3 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a2_b1_a_idx on ab_a2_b1 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a2_b2_a_idx on ab_a2_b2 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a2_b3_a_idx on ab_a2_b3 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a3_b1_a_idx on ab_a3_b1 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a3_b2_a_idx on ab_a3_b2 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
                           ->  Index Only Scan using ab_a3_b3_a_idx on ab_a3_b3 (never executed)
                                 Index Cond: (a = a.a)
                                 Heap Fetches: 0
(37 rows)

reset enable_hashjoin;
reset enable_mergejoin;
reset parallel_setup_cost;
reset parallel_tuple_cost;
reset min_parallel_table_scan_size;
reset max_parallel_workers_per_gather;
-- Test run-time partition pruning with an initplan
explain (analyze, costs off, summary off, timing off)
select * from ab where a = (select max(a) from lprt_a) and b = (select max(a)-1 from lprt_a);
                               QUERY PLAN                                
-------------------------------------------------------------------------
 Append (actual rows=0 loops=1)
   InitPlan 1 (returns $0)
     ->  Aggregate (actual rows=1 loops=1)
           ->  Seq Scan on lprt_a (actual rows=102 loops=1)
   InitPlan 2 (returns $1)
     ->  Aggregate (actual rows=1 loops=1)
           ->  Seq Scan on lprt_a lprt_a_1 (actual rows=102 loops=1)
   ->  Bitmap Heap Scan on ab_a1_b1 (never executed)
         Recheck Cond: (a = $0)
         Filter: (b = $1)
         ->  Bitmap Index Scan on ab_a1_b1_a_idx (never executed)
               Index Cond: (a = $0)
   ->  Bitmap Heap Scan on ab_a1_b2 (never executed)
         Recheck Cond: (a = $0)
         Filter: (b = $1)
         ->  Bitmap Index Scan on ab_a1_b2_a_idx (never executed)
               Index Cond: (a = $0)
   ->  Bitmap Heap Scan on ab_a1_b3 (never executed)
         Recheck Cond: (a = $0)
         Filter: (b = $1)
         ->  Bitmap Index Scan on ab_a1_b3_a_idx (never executed)
               Index Cond: (a = $0)
   ->  Bitmap Heap Scan on ab_a2_b1 (never executed)
         Recheck Cond: (a = $0)
         Filter: (b = $1)
         ->  Bitmap Index Scan on ab_a2_b1_a_idx (never executed)
               Index Cond: (a = $0)
   ->  Bitmap Heap Scan on ab_a2_b2 (never executed)
         Recheck Cond: (a = $0)
         Filter: (b = $1)
         ->  Bitmap Index Scan on ab_a2_b2_a_idx (never executed)
               Index Cond: (a = $0)
   ->  Bitmap Heap Scan on ab_a2_b3 (never executed)
         Recheck Cond: (a = $0)
         Filter: (b = $1)
         ->  Bitmap Index Scan on ab_a2_b3_a_idx (never executed)
               Index Cond: (a = $0)
   ->  Bitmap Heap Scan on ab_a3_b1 (never executed)
         Recheck Cond: (a = $0)
         Filter: (b = $1)
         ->  Bitmap Index Scan on ab_a3_b1_a_idx (never executed)
               Index Cond: (a = $0)
   ->  Bitmap Heap Scan on ab_a3_b2 (actual rows=0 loops=1)
         Recheck Cond: (a = $0)
         Filter: (b = $1)
         ->  Bitmap Index Scan on ab_a3_b2_a_idx (actual rows=0 loops=1)
               Index Cond: (a = $0)
   ->  Bitmap Heap Scan on ab_a3_b3 (never executed)
         Recheck Cond: (a = $0)
         Filter: (b = $1)
         ->  Bitmap Index Scan on ab_a3_b3_a_idx (never executed)
               Index Cond: (a = $0)
(52 rows)

deallocate ab_q1;
deallocate ab_q2;
deallocate ab_q3;
deallocate ab_q4;
deallocate ab_q5;
deallocate ab_q6;
drop table ab, lprt_a;
-- Join
create table tbl1(col1 int);
insert into tbl1 values (501), (505);
-- Basic table
create table tprt (col1 int) partition by range (col1);
create table tprt_1 partition of tprt for values from (1) to (501);
create table tprt_2 partition of tprt for values from (501) to (1001);
create table tprt_3 partition of tprt for values from (1001) to (2001);
create table tprt_4 partition of tprt for values from (2001) to (3001);
create table tprt_5 partition of tprt for values from (3001) to (4001);
create table tprt_6 partition of tprt for values from (4001) to (5001);
create index tprt1_idx on tprt_1 (col1);
create index tprt2_idx on tprt_2 (col1);
create index tprt3_idx on tprt_3 (col1);
create index tprt4_idx on tprt_4 (col1);
create index tprt5_idx on tprt_5 (col1);
create index tprt6_idx on tprt_6 (col1);
insert into tprt values (10), (20), (501), (502), (505), (1001), (4500);
set enable_hashjoin = off;
set enable_mergejoin = off;
explain (analyze, costs off, summary off, timing off)
select * from tbl1 join tprt on tbl1.col1 > tprt.col1;
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Nested Loop (actual rows=6 loops=1)
   ->  Seq Scan on tbl1 (actual rows=2 loops=1)
   ->  Append (actual rows=3 loops=2)
         ->  Index Only Scan using tprt1_idx on tprt_1 (actual rows=2 loops=2)
               Index Cond: (col1 < tbl1.col1)
               Heap Fetches: 4
         ->  Index Only Scan using tprt2_idx on tprt_2 (actual rows=2 loops=1)
               Index Cond: (col1 < tbl1.col1)
               Heap Fetches: 2
         ->  Index Only Scan using tprt3_idx on tprt_3 (never executed)
               Index Cond: (col1 < tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt4_idx on tprt_4 (never executed)
               Index Cond: (col1 < tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt5_idx on tprt_5 (never executed)
               Index Cond: (col1 < tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt6_idx on tprt_6 (never executed)
               Index Cond: (col1 < tbl1.col1)
               Heap Fetches: 0
(21 rows)

explain (analyze, costs off, summary off, timing off)
select * from tbl1 join tprt on tbl1.col1 = tprt.col1;
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Nested Loop (actual rows=2 loops=1)
   ->  Seq Scan on tbl1 (actual rows=2 loops=1)
   ->  Append (actual rows=1 loops=2)
         ->  Index Only Scan using tprt1_idx on tprt_1 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt2_idx on tprt_2 (actual rows=1 loops=2)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 2
         ->  Index Only Scan using tprt3_idx on tprt_3 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt4_idx on tprt_4 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt5_idx on tprt_5 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt6_idx on tprt_6 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
(21 rows)

select tbl1.col1, tprt.col1 from tbl1
inner join tprt on tbl1.col1 > tprt.col1
order by tbl1.col1, tprt.col1;
 col1 | col1 
------+------
  501 |   10
  501 |   20
  505 |   10
  505 |   20
  505 |  501
  505 |  502
(6 rows)

select tbl1.col1, tprt.col1 from tbl1
inner join tprt on tbl1.col1 = tprt.col1
order by tbl1.col1, tprt.col1;
 col1 | col1 
------+------
  501 |  501
  505 |  505
(2 rows)

-- Multiple partitions
insert into tbl1 values (1001), (1010), (1011);
explain (analyze, costs off, summary off, timing off)
select * from tbl1 inner join tprt on tbl1.col1 > tprt.col1;
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Nested Loop (actual rows=23 loops=1)
   ->  Seq Scan on tbl1 (actual rows=5 loops=1)
   ->  Append (actual rows=5 loops=5)
         ->  Index Only Scan using tprt1_idx on tprt_1 (actual rows=2 loops=5)
               Index Cond: (col1 < tbl1.col1)
               Heap Fetches: 10
         ->  Index Only Scan using tprt2_idx on tprt_2 (actual rows=3 loops=4)
               Index Cond: (col1 < tbl1.col1)
               Heap Fetches: 11
         ->  Index Only Scan using tprt3_idx on tprt_3 (actual rows=1 loops=2)
               Index Cond: (col1 < tbl1.col1)
               Heap Fetches: 2
         ->  Index Only Scan using tprt4_idx on tprt_4 (never executed)
               Index Cond: (col1 < tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt5_idx on tprt_5 (never executed)
               Index Cond: (col1 < tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt6_idx on tprt_6 (never executed)
               Index Cond: (col1 < tbl1.col1)
               Heap Fetches: 0
(21 rows)

explain (analyze, costs off, summary off, timing off)
select * from tbl1 inner join tprt on tbl1.col1 = tprt.col1;
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Nested Loop (actual rows=3 loops=1)
   ->  Seq Scan on tbl1 (actual rows=5 loops=1)
   ->  Append (actual rows=1 loops=5)
         ->  Index Only Scan using tprt1_idx on tprt_1 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt2_idx on tprt_2 (actual rows=1 loops=2)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 2
         ->  Index Only Scan using tprt3_idx on tprt_3 (actual rows=0 loops=3)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 1
         ->  Index Only Scan using tprt4_idx on tprt_4 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt5_idx on tprt_5 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt6_idx on tprt_6 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
(21 rows)

select tbl1.col1, tprt.col1 from tbl1
inner join tprt on tbl1.col1 > tprt.col1
order by tbl1.col1, tprt.col1;
 col1 | col1 
------+------
  501 |   10
  501 |   20
  505 |   10
  505 |   20
  505 |  501
  505 |  502
 1001 |   10
 1001 |   20
 1001 |  501
 1001 |  502
 1001 |  505
 1010 |   10
 1010 |   20
 1010 |  501
 1010 |  502
 1010 |  505
 1010 | 1001
 1011 |   10
 1011 |   20
 1011 |  501
 1011 |  502
 1011 |  505
 1011 | 1001
(23 rows)

select tbl1.col1, tprt.col1 from tbl1
inner join tprt on tbl1.col1 = tprt.col1
order by tbl1.col1, tprt.col1;
 col1 | col1 
------+------
  501 |  501
  505 |  505
 1001 | 1001
(3 rows)

-- Last partition
delete from tbl1;
insert into tbl1 values (4400);
explain (analyze, costs off, summary off, timing off)
select * from tbl1 join tprt on tbl1.col1 < tprt.col1;
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Nested Loop (actual rows=1 loops=1)
   ->  Seq Scan on tbl1 (actual rows=1 loops=1)
   ->  Append (actual rows=1 loops=1)
         ->  Index Only Scan using tprt1_idx on tprt_1 (never executed)
               Index Cond: (col1 > tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt2_idx on tprt_2 (never executed)
               Index Cond: (col1 > tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt3_idx on tprt_3 (never executed)
               Index Cond: (col1 > tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt4_idx on tprt_4 (never executed)
               Index Cond: (col1 > tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt5_idx on tprt_5 (never executed)
               Index Cond: (col1 > tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt6_idx on tprt_6 (actual rows=1 loops=1)
               Index Cond: (col1 > tbl1.col1)
               Heap Fetches: 1
(21 rows)

select tbl1.col1, tprt.col1 from tbl1
inner join tprt on tbl1.col1 < tprt.col1
order by tbl1.col1, tprt.col1;
 col1 | col1 
------+------
 4400 | 4500
(1 row)

-- No matching partition
delete from tbl1;
insert into tbl1 values (10000);
explain (analyze, costs off, summary off, timing off)
select * from tbl1 join tprt on tbl1.col1 = tprt.col1;
                               QUERY PLAN                               
------------------------------------------------------------------------
 Nested Loop (actual rows=0 loops=1)
   ->  Seq Scan on tbl1 (actual rows=1 loops=1)
   ->  Append (actual rows=0 loops=1)
         ->  Index Only Scan using tprt1_idx on tprt_1 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt2_idx on tprt_2 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt3_idx on tprt_3 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt4_idx on tprt_4 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt5_idx on tprt_5 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
         ->  Index Only Scan using tprt6_idx on tprt_6 (never executed)
               Index Cond: (col1 = tbl1.col1)
               Heap Fetches: 0
(21 rows)

select tbl1.col1, tprt.col1 from tbl1
inner join tprt on tbl1.col1 = tprt.col1
order by tbl1.col1, tprt.col1;
 col1 | col1 
------+------
(0 rows)

drop table tbl1, tprt;
-- Test with columns defined in varying orders between each level
create table part_abc (a int not null, b int not null, c int not null) partition by list (a);
create table part_bac (b int not null, a int not null, c int not null) partition by list (b);
create table part_cab (c int not null, a int not null, b int not null) partition by list (c);
create table part_abc_p1 (a int not null, b int not null, c int not null);
alter table part_abc attach partition part_bac for values in(1);
alter table part_bac attach partition part_cab for values in(2);
alter table part_cab attach partition part_abc_p1 for values in(3);
prepare part_abc_q1 (int, int, int) as
select * from part_abc where a = $1 and b = $2 and c = $3;
-- Execute query 5 times to allow choose_custom_plan
-- to start considering a generic plan.
execute part_abc_q1 (1, 2, 3);
 a | b | c 
---+---+---
(0 rows)

execute part_abc_q1 (1, 2, 3);
 a | b | c 
---+---+---
(0 rows)

execute part_abc_q1 (1, 2, 3);
 a | b | c 
---+---+---
(0 rows)

execute part_abc_q1 (1, 2, 3);
 a | b | c 
---+---+---
(0 rows)

execute part_abc_q1 (1, 2, 3);
 a | b | c 
---+---+---
(0 rows)

-- Single partition should be scanned.
explain (analyze, costs off, summary off, timing off) execute part_abc_q1 (1, 2, 3);
                      QUERY PLAN                       
-------------------------------------------------------
 Append (actual rows=0 loops=1)
   ->  Seq Scan on part_abc_p1 (actual rows=0 loops=1)
         Filter: ((a = $1) AND (b = $2) AND (c = $3))
(3 rows)

deallocate part_abc_q1;
drop table part_abc;
-- Ensure that an Append node properly handles a sub-partitioned table
-- matching without any of its leaf partitions matching the clause.
create table listp (a int, b int) partition by list (a);
create table listp_1 partition of listp for values in(1) partition by list (b);
create table listp_1_1 partition of listp_1 for values in(1);
create table listp_2 partition of listp for values in(2) partition by list (b);
create table listp_2_1 partition of listp_2 for values in(2);
select * from listp where b = 1;
 a | b 
---+---
(0 rows)

-- Ensure that an Append node properly can handle selection of all first level
-- partitions before finally detecting the correct set of 2nd level partitions
-- which match the given parameter.
prepare q1 (int,int) as select * from listp where b in ($1,$2);
execute q1 (1,2);
 a | b 
---+---
(0 rows)

execute q1 (1,2);
 a | b 
---+---
(0 rows)

execute q1 (1,2);
 a | b 
---+---
(0 rows)

execute q1 (1,2);
 a | b 
---+---
(0 rows)

execute q1 (1,2);
 a | b 
---+---
(0 rows)

explain (analyze, costs off, summary off, timing off)  execute q1 (1,1);
                     QUERY PLAN                      
-----------------------------------------------------
 Append (actual rows=0 loops=1)
   Subplans Removed: 1
   ->  Seq Scan on listp_1_1 (actual rows=0 loops=1)
         Filter: (b = ANY (ARRAY[$1, $2]))
(4 rows)

explain (analyze, costs off, summary off, timing off)  execute q1 (2,2);
                     QUERY PLAN                      
-----------------------------------------------------
 Append (actual rows=0 loops=1)
   Subplans Removed: 1
   ->  Seq Scan on listp_2_1 (actual rows=0 loops=1)
         Filter: (b = ANY (ARRAY[$1, $2]))
(4 rows)

-- Try with no matching partitions. One subplan should remain in this case,
-- but it shouldn't be executed.
explain (analyze, costs off, summary off, timing off)  execute q1 (0,0);
                  QUERY PLAN                  
----------------------------------------------
 Append (actual rows=0 loops=1)
   Subplans Removed: 1
   ->  Seq Scan on listp_1_1 (never executed)
         Filter: (b = ANY (ARRAY[$1, $2]))
(4 rows)

deallocate q1;
-- Test more complex cases where a not-equal condition further eliminates partitions.
prepare q1 (int,int,int,int) as select * from listp where b in($1,$2) and $3 <> b and $4 <> b;
execute q1 (1,2,3,4);
 a | b 
---+---
(0 rows)

execute q1 (1,2,3,4);
 a | b 
---+---
(0 rows)

execute q1 (1,2,3,4);
 a | b 
---+---
(0 rows)

execute q1 (1,2,3,4);
 a | b 
---+---
(0 rows)

execute q1 (1,2,3,4);
 a | b 
---+---
(0 rows)

-- Both partitions allowed by IN clause, but one disallowed by <> clause
explain (analyze, costs off, summary off, timing off)  execute q1 (1,2,2,0);
                               QUERY PLAN                                
-------------------------------------------------------------------------
 Append (actual rows=0 loops=1)
   Subplans Removed: 1
   ->  Seq Scan on listp_1_1 (actual rows=0 loops=1)
         Filter: ((b = ANY (ARRAY[$1, $2])) AND ($3 <> b) AND ($4 <> b))
(4 rows)

-- Both partitions allowed by IN clause, then both excluded again by <> clauses.
-- One subplan will remain in this case, but it should not be executed.
explain (analyze, costs off, summary off, timing off)  execute q1 (1,2,2,1);
                               QUERY PLAN                                
-------------------------------------------------------------------------
 Append (actual rows=0 loops=1)
   Subplans Removed: 1
   ->  Seq Scan on listp_1_1 (never executed)
         Filter: ((b = ANY (ARRAY[$1, $2])) AND ($3 <> b) AND ($4 <> b))
(4 rows)

drop table listp;
-- Ensure runtime pruning works with initplans params with boolean types
create table boolvalues (value bool not null);
insert into boolvalues values('t'),('f');
create table boolp (a bool) partition by list (a);
create table boolp_t partition of boolp for values in('t');
create table boolp_f partition of boolp for values in('f');
explain (analyze, costs off, summary off, timing off)
select * from boolp where a = (select value from boolvalues where value);
                       QUERY PLAN                       
--------------------------------------------------------
 Append (actual rows=0 loops=1)
   InitPlan 1 (returns $0)
     ->  Seq Scan on boolvalues (actual rows=1 loops=1)
           Filter: value
           Rows Removed by Filter: 1
   ->  Seq Scan on boolp_f (never executed)
         Filter: (a = $0)
   ->  Seq Scan on boolp_t (actual rows=0 loops=1)
         Filter: (a = $0)
(9 rows)

explain (analyze, costs off, summary off, timing off)
select * from boolp where a = (select value from boolvalues where not value);
                       QUERY PLAN                       
--------------------------------------------------------
 Append (actual rows=0 loops=1)
   InitPlan 1 (returns $0)
     ->  Seq Scan on boolvalues (actual rows=1 loops=1)
           Filter: (NOT value)
           Rows Removed by Filter: 1
   ->  Seq Scan on boolp_f (actual rows=0 loops=1)
         Filter: (a = $0)
   ->  Seq Scan on boolp_t (never executed)
         Filter: (a = $0)
(9 rows)

drop table boolp;