1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
|
/*-------------------------------------------------------------------------
*
* pathnode.c
* Routines to manipulate pathlists and create path nodes
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Portions Copyright (c) 2012-2014, TransLattice, Inc.
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/optimizer/util/pathnode.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <math.h>
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/restrictinfo.h"
#include "optimizer/var.h"
#include "parser/parsetree.h"
#include "utils/lsyscache.h"
#include "utils/selfuncs.h"
#ifdef XCP
#include "access/heapam.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "pgxc/locator.h"
#include "pgxc/nodemgr.h"
#include "utils/rel.h"
#endif
typedef enum
{
COSTS_EQUAL, /* path costs are fuzzily equal */
COSTS_BETTER1, /* first path is cheaper than second */
COSTS_BETTER2, /* second path is cheaper than first */
COSTS_DIFFERENT /* neither path dominates the other on cost */
} PathCostComparison;
static List *translate_sub_tlist(List *tlist, int relid);
#ifdef XCP
static void restrict_distribution(PlannerInfo *root, RestrictInfo *ri,
Path *pathnode);
static Path *redistribute_path(Path *subpath, char distributionType,
Bitmapset *nodes, Bitmapset *restrictNodes,
Node* distributionExpr);
static void set_scanpath_distribution(PlannerInfo *root, RelOptInfo *rel, Path *pathnode);
static List *set_joinpath_distribution(PlannerInfo *root, JoinPath *pathnode);
#endif
/*****************************************************************************
* MISC. PATH UTILITIES
*****************************************************************************/
/*
* compare_path_costs
* Return -1, 0, or +1 according as path1 is cheaper, the same cost,
* or more expensive than path2 for the specified criterion.
*/
int
compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
{
if (criterion == STARTUP_COST)
{
if (path1->startup_cost < path2->startup_cost)
return -1;
if (path1->startup_cost > path2->startup_cost)
return +1;
/*
* If paths have the same startup cost (not at all unlikely), order
* them by total cost.
*/
if (path1->total_cost < path2->total_cost)
return -1;
if (path1->total_cost > path2->total_cost)
return +1;
}
else
{
if (path1->total_cost < path2->total_cost)
return -1;
if (path1->total_cost > path2->total_cost)
return +1;
/*
* If paths have the same total cost, order them by startup cost.
*/
if (path1->startup_cost < path2->startup_cost)
return -1;
if (path1->startup_cost > path2->startup_cost)
return +1;
}
return 0;
}
/*
* compare_path_fractional_costs
* Return -1, 0, or +1 according as path1 is cheaper, the same cost,
* or more expensive than path2 for fetching the specified fraction
* of the total tuples.
*
* If fraction is <= 0 or > 1, we interpret it as 1, ie, we select the
* path with the cheaper total_cost.
*/
int
compare_fractional_path_costs(Path *path1, Path *path2,
double fraction)
{
Cost cost1,
cost2;
if (fraction <= 0.0 || fraction >= 1.0)
return compare_path_costs(path1, path2, TOTAL_COST);
cost1 = path1->startup_cost +
fraction * (path1->total_cost - path1->startup_cost);
cost2 = path2->startup_cost +
fraction * (path2->total_cost - path2->startup_cost);
if (cost1 < cost2)
return -1;
if (cost1 > cost2)
return +1;
return 0;
}
/*
* compare_path_costs_fuzzily
* Compare the costs of two paths to see if either can be said to
* dominate the other.
*
* We use fuzzy comparisons so that add_path() can avoid keeping both of
* a pair of paths that really have insignificantly different cost.
*
* The fuzz_factor argument must be 1.0 plus delta, where delta is the
* fraction of the smaller cost that is considered to be a significant
* difference. For example, fuzz_factor = 1.01 makes the fuzziness limit
* be 1% of the smaller cost.
*
* The two paths are said to have "equal" costs if both startup and total
* costs are fuzzily the same. Path1 is said to be better than path2 if
* it has fuzzily better startup cost and fuzzily no worse total cost,
* or if it has fuzzily better total cost and fuzzily no worse startup cost.
* Path2 is better than path1 if the reverse holds. Finally, if one path
* is fuzzily better than the other on startup cost and fuzzily worse on
* total cost, we just say that their costs are "different", since neither
* dominates the other across the whole performance spectrum.
*
* If consider_startup is false, then we don't care about keeping paths with
* good startup cost, so we'll never return COSTS_DIFFERENT.
*
* This function also includes special hacks to support a policy enforced
* by its sole caller, add_path(): paths that have any parameterization
* cannot win comparisons on the grounds of having cheaper startup cost,
* since we deem only total cost to be of interest for a parameterized path.
* (Unparameterized paths are more common, so we check for this case last.)
*/
static PathCostComparison
compare_path_costs_fuzzily(Path *path1, Path *path2, double fuzz_factor,
bool consider_startup)
{
/*
* Check total cost first since it's more likely to be different; many
* paths have zero startup cost.
*/
if (path1->total_cost > path2->total_cost * fuzz_factor)
{
/* path1 fuzzily worse on total cost */
if (consider_startup &&
path2->startup_cost > path1->startup_cost * fuzz_factor &&
path1->param_info == NULL)
{
/* ... but path2 fuzzily worse on startup, so DIFFERENT */
return COSTS_DIFFERENT;
}
/* else path2 dominates */
return COSTS_BETTER2;
}
if (path2->total_cost > path1->total_cost * fuzz_factor)
{
/* path2 fuzzily worse on total cost */
if (consider_startup &&
path1->startup_cost > path2->startup_cost * fuzz_factor &&
path2->param_info == NULL)
{
/* ... but path1 fuzzily worse on startup, so DIFFERENT */
return COSTS_DIFFERENT;
}
/* else path1 dominates */
return COSTS_BETTER1;
}
/* fuzzily the same on total cost */
/* (so we may as well compare startup cost, even if !consider_startup) */
if (path1->startup_cost > path2->startup_cost * fuzz_factor &&
path2->param_info == NULL)
{
/* ... but path1 fuzzily worse on startup, so path2 wins */
return COSTS_BETTER2;
}
if (path2->startup_cost > path1->startup_cost * fuzz_factor &&
path1->param_info == NULL)
{
/* ... but path2 fuzzily worse on startup, so path1 wins */
return COSTS_BETTER1;
}
/* fuzzily the same on both costs */
return COSTS_EQUAL;
}
/*
* set_cheapest
* Find the minimum-cost paths from among a relation's paths,
* and save them in the rel's cheapest-path fields.
*
* cheapest_total_path is normally the cheapest-total-cost unparameterized
* path; but if there are no unparameterized paths, we assign it to be the
* best (cheapest least-parameterized) parameterized path. However, only
* unparameterized paths are considered candidates for cheapest_startup_path,
* so that will be NULL if there are no unparameterized paths.
*
* The cheapest_parameterized_paths list collects all parameterized paths
* that have survived the add_path() tournament for this relation. (Since
* add_path ignores pathkeys and startup cost for a parameterized path,
* these will be paths that have best total cost or best row count for their
* parameterization.) cheapest_parameterized_paths always includes the
* cheapest-total unparameterized path, too, if there is one; the users of
* that list find it more convenient if that's included.
*
* This is normally called only after we've finished constructing the path
* list for the rel node.
*/
void
set_cheapest(RelOptInfo *parent_rel)
{
Path *cheapest_startup_path;
Path *cheapest_total_path;
Path *best_param_path;
List *parameterized_paths;
ListCell *p;
Assert(IsA(parent_rel, RelOptInfo));
if (parent_rel->pathlist == NIL)
elog(ERROR, "could not devise a query plan for the given query");
cheapest_startup_path = cheapest_total_path = best_param_path = NULL;
parameterized_paths = NIL;
foreach(p, parent_rel->pathlist)
{
Path *path = (Path *) lfirst(p);
int cmp;
if (path->param_info)
{
/* Parameterized path, so add it to parameterized_paths */
parameterized_paths = lappend(parameterized_paths, path);
/*
* If we have an unparameterized cheapest-total, we no longer care
* about finding the best parameterized path, so move on.
*/
if (cheapest_total_path)
continue;
/*
* Otherwise, track the best parameterized path, which is the one
* with least total cost among those of the minimum
* parameterization.
*/
if (best_param_path == NULL)
best_param_path = path;
else
{
switch (bms_subset_compare(PATH_REQ_OUTER(path),
PATH_REQ_OUTER(best_param_path)))
{
case BMS_EQUAL:
/* keep the cheaper one */
if (compare_path_costs(path, best_param_path,
TOTAL_COST) < 0)
best_param_path = path;
break;
case BMS_SUBSET1:
/* new path is less-parameterized */
best_param_path = path;
break;
case BMS_SUBSET2:
/* old path is less-parameterized, keep it */
break;
case BMS_DIFFERENT:
/*
* This means that neither path has the least possible
* parameterization for the rel. We'll sit on the old
* path until something better comes along.
*/
break;
}
}
}
else
{
/* Unparameterized path, so consider it for cheapest slots */
if (cheapest_total_path == NULL)
{
cheapest_startup_path = cheapest_total_path = path;
continue;
}
/*
* If we find two paths of identical costs, try to keep the
* better-sorted one. The paths might have unrelated sort
* orderings, in which case we can only guess which might be
* better to keep, but if one is superior then we definitely
* should keep that one.
*/
cmp = compare_path_costs(cheapest_startup_path, path, STARTUP_COST);
if (cmp > 0 ||
(cmp == 0 &&
compare_pathkeys(cheapest_startup_path->pathkeys,
path->pathkeys) == PATHKEYS_BETTER2))
cheapest_startup_path = path;
cmp = compare_path_costs(cheapest_total_path, path, TOTAL_COST);
if (cmp > 0 ||
(cmp == 0 &&
compare_pathkeys(cheapest_total_path->pathkeys,
path->pathkeys) == PATHKEYS_BETTER2))
cheapest_total_path = path;
}
}
/* Add cheapest unparameterized path, if any, to parameterized_paths */
if (cheapest_total_path)
parameterized_paths = lcons(cheapest_total_path, parameterized_paths);
/*
* If there is no unparameterized path, use the best parameterized path as
* cheapest_total_path (but not as cheapest_startup_path).
*/
if (cheapest_total_path == NULL)
cheapest_total_path = best_param_path;
Assert(cheapest_total_path != NULL);
parent_rel->cheapest_startup_path = cheapest_startup_path;
parent_rel->cheapest_total_path = cheapest_total_path;
parent_rel->cheapest_unique_path = NULL; /* computed only if needed */
parent_rel->cheapest_parameterized_paths = parameterized_paths;
}
/*
* add_path
* Consider a potential implementation path for the specified parent rel,
* and add it to the rel's pathlist if it is worthy of consideration.
* A path is worthy if it has a better sort order (better pathkeys) or
* cheaper cost (on either dimension), or generates fewer rows, than any
* existing path that has the same or superset parameterization rels.
*
* We also remove from the rel's pathlist any old paths that are dominated
* by new_path --- that is, new_path is cheaper, at least as well ordered,
* generates no more rows, and requires no outer rels not required by the
* old path.
*
* In most cases, a path with a superset parameterization will generate
* fewer rows (since it has more join clauses to apply), so that those two
* figures of merit move in opposite directions; this means that a path of
* one parameterization can seldom dominate a path of another. But such
* cases do arise, so we make the full set of checks anyway.
*
* There are two policy decisions embedded in this function, along with
* its sibling add_path_precheck: we treat all parameterized paths as
* having NIL pathkeys, and we ignore their startup costs, so that they
* compete only on parameterization, total cost and rowcount. This is to
* reduce the number of parameterized paths that are kept. See discussion
* in src/backend/optimizer/README.
*
* Another policy that is enforced here is that we only consider cheap
* startup cost to be interesting if parent_rel->consider_startup is true.
*
* The pathlist is kept sorted by total_cost, with cheaper paths
* at the front. Within this routine, that's simply a speed hack:
* doing it that way makes it more likely that we will reject an inferior
* path after a few comparisons, rather than many comparisons.
* However, add_path_precheck relies on this ordering to exit early
* when possible.
*
* NOTE: discarded Path objects are immediately pfree'd to reduce planner
* memory consumption. We dare not try to free the substructure of a Path,
* since much of it may be shared with other Paths or the query tree itself;
* but just recycling discarded Path nodes is a very useful savings in
* a large join tree. We can recycle the List nodes of pathlist, too.
*
* BUT: we do not pfree IndexPath objects, since they may be referenced as
* children of BitmapHeapPaths as well as being paths in their own right.
*
* 'parent_rel' is the relation entry to which the path corresponds.
* 'new_path' is a potential path for parent_rel.
*
* Returns nothing, but modifies parent_rel->pathlist.
*/
void
add_path(RelOptInfo *parent_rel, Path *new_path)
{
bool accept_new = true; /* unless we find a superior old path */
ListCell *insert_after = NULL; /* where to insert new item */
List *new_path_pathkeys;
ListCell *p1;
ListCell *p1_prev;
ListCell *p1_next;
/*
* This is a convenient place to check for query cancel --- no part of the
* planner goes very long without calling add_path().
*/
CHECK_FOR_INTERRUPTS();
/* Pretend parameterized paths have no pathkeys, per comment above */
new_path_pathkeys = new_path->param_info ? NIL : new_path->pathkeys;
/*
* Loop to check proposed new path against old paths. Note it is possible
* for more than one old path to be tossed out because new_path dominates
* it.
*
* We can't use foreach here because the loop body may delete the current
* list cell.
*/
p1_prev = NULL;
for (p1 = list_head(parent_rel->pathlist); p1 != NULL; p1 = p1_next)
{
Path *old_path = (Path *) lfirst(p1);
bool remove_old = false; /* unless new proves superior */
PathCostComparison costcmp;
PathKeysComparison keyscmp;
BMS_Comparison outercmp;
p1_next = lnext(p1);
/*
* Do a fuzzy cost comparison with 1% fuzziness limit. (XXX does this
* percentage need to be user-configurable?)
*/
costcmp = compare_path_costs_fuzzily(new_path, old_path, 1.01,
parent_rel->consider_startup);
/*
* If the two paths compare differently for startup and total cost,
* then we want to keep both, and we can skip comparing pathkeys and
* required_outer rels. If they compare the same, proceed with the
* other comparisons. Row count is checked last. (We make the tests
* in this order because the cost comparison is most likely to turn
* out "different", and the pathkeys comparison next most likely. As
* explained above, row count very seldom makes a difference, so even
* though it's cheap to compare there's not much point in checking it
* earlier.)
*/
if (costcmp != COSTS_DIFFERENT)
{
/* Similarly check to see if either dominates on pathkeys */
List *old_path_pathkeys;
old_path_pathkeys = old_path->param_info ? NIL : old_path->pathkeys;
keyscmp = compare_pathkeys(new_path_pathkeys,
old_path_pathkeys);
if (keyscmp != PATHKEYS_DIFFERENT)
{
switch (costcmp)
{
case COSTS_EQUAL:
outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
PATH_REQ_OUTER(old_path));
if (keyscmp == PATHKEYS_BETTER1)
{
if ((outercmp == BMS_EQUAL ||
outercmp == BMS_SUBSET1) &&
new_path->rows <= old_path->rows)
remove_old = true; /* new dominates old */
}
else if (keyscmp == PATHKEYS_BETTER2)
{
if ((outercmp == BMS_EQUAL ||
outercmp == BMS_SUBSET2) &&
new_path->rows >= old_path->rows)
accept_new = false; /* old dominates new */
}
else /* keyscmp == PATHKEYS_EQUAL */
{
if (outercmp == BMS_EQUAL)
{
/*
* Same pathkeys and outer rels, and fuzzily
* the same cost, so keep just one; to decide
* which, first check rows and then do a fuzzy
* cost comparison with very small fuzz limit.
* (We used to do an exact cost comparison,
* but that results in annoying
* platform-specific plan variations due to
* roundoff in the cost estimates.) If things
* are still tied, arbitrarily keep only the
* old path. Notice that we will keep only
* the old path even if the less-fuzzy
* comparison decides the startup and total
* costs compare differently.
*/
if (new_path->rows < old_path->rows)
remove_old = true; /* new dominates old */
else if (new_path->rows > old_path->rows)
accept_new = false; /* old dominates new */
else if (compare_path_costs_fuzzily(new_path,
old_path,
1.0000000001,
parent_rel->consider_startup) == COSTS_BETTER1)
remove_old = true; /* new dominates old */
else
accept_new = false; /* old equals or
* dominates new */
}
else if (outercmp == BMS_SUBSET1 &&
new_path->rows <= old_path->rows)
remove_old = true; /* new dominates old */
else if (outercmp == BMS_SUBSET2 &&
new_path->rows >= old_path->rows)
accept_new = false; /* old dominates new */
/* else different parameterizations, keep both */
}
break;
case COSTS_BETTER1:
if (keyscmp != PATHKEYS_BETTER2)
{
outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
PATH_REQ_OUTER(old_path));
if ((outercmp == BMS_EQUAL ||
outercmp == BMS_SUBSET1) &&
new_path->rows <= old_path->rows)
remove_old = true; /* new dominates old */
}
break;
case COSTS_BETTER2:
if (keyscmp != PATHKEYS_BETTER1)
{
outercmp = bms_subset_compare(PATH_REQ_OUTER(new_path),
PATH_REQ_OUTER(old_path));
if ((outercmp == BMS_EQUAL ||
outercmp == BMS_SUBSET2) &&
new_path->rows >= old_path->rows)
accept_new = false; /* old dominates new */
}
break;
case COSTS_DIFFERENT:
/*
* can't get here, but keep this case to keep compiler
* quiet
*/
break;
}
}
}
/*
* Remove current element from pathlist if dominated by new.
*/
if (remove_old)
{
parent_rel->pathlist = list_delete_cell(parent_rel->pathlist,
p1, p1_prev);
/*
* Delete the data pointed-to by the deleted cell, if possible
*/
if (!IsA(old_path, IndexPath))
pfree(old_path);
/* p1_prev does not advance */
}
else
{
/* new belongs after this old path if it has cost >= old's */
if (new_path->total_cost >= old_path->total_cost)
insert_after = p1;
/* p1_prev advances */
p1_prev = p1;
}
/*
* If we found an old path that dominates new_path, we can quit
* scanning the pathlist; we will not add new_path, and we assume
* new_path cannot dominate any other elements of the pathlist.
*/
if (!accept_new)
break;
}
if (accept_new)
{
/* Accept the new path: insert it at proper place in pathlist */
if (insert_after)
lappend_cell(parent_rel->pathlist, insert_after, new_path);
else
parent_rel->pathlist = lcons(new_path, parent_rel->pathlist);
}
else
{
/* Reject and recycle the new path */
if (!IsA(new_path, IndexPath))
pfree(new_path);
}
}
/*
* add_path_precheck
* Check whether a proposed new path could possibly get accepted.
* We assume we know the path's pathkeys and parameterization accurately,
* and have lower bounds for its costs.
*
* Note that we do not know the path's rowcount, since getting an estimate for
* that is too expensive to do before prechecking. We assume here that paths
* of a superset parameterization will generate fewer rows; if that holds,
* then paths with different parameterizations cannot dominate each other
* and so we can simply ignore existing paths of another parameterization.
* (In the infrequent cases where that rule of thumb fails, add_path will
* get rid of the inferior path.)
*
* At the time this is called, we haven't actually built a Path structure,
* so the required information has to be passed piecemeal.
*/
bool
add_path_precheck(RelOptInfo *parent_rel,
Cost startup_cost, Cost total_cost,
List *pathkeys, Relids required_outer)
{
List *new_path_pathkeys;
ListCell *p1;
/* Pretend parameterized paths have no pathkeys, per add_path policy */
new_path_pathkeys = required_outer ? NIL : pathkeys;
foreach(p1, parent_rel->pathlist)
{
Path *old_path = (Path *) lfirst(p1);
PathKeysComparison keyscmp;
/*
* We are looking for an old_path with the same parameterization (and
* by assumption the same rowcount) that dominates the new path on
* pathkeys as well as both cost metrics. If we find one, we can
* reject the new path.
*
* For speed, we make exact rather than fuzzy cost comparisons. If an
* old path dominates the new path exactly on both costs, it will
* surely do so fuzzily.
*/
if (total_cost >= old_path->total_cost)
{
/* can win on startup cost only if unparameterized */
if (startup_cost >= old_path->startup_cost || required_outer)
{
/* new path does not win on cost, so check pathkeys... */
List *old_path_pathkeys;
old_path_pathkeys = old_path->param_info ? NIL : old_path->pathkeys;
keyscmp = compare_pathkeys(new_path_pathkeys,
old_path_pathkeys);
if (keyscmp == PATHKEYS_EQUAL ||
keyscmp == PATHKEYS_BETTER2)
{
/* new path does not win on pathkeys... */
if (bms_equal(required_outer, PATH_REQ_OUTER(old_path)))
{
/* Found an old path that dominates the new one */
return false;
}
}
}
}
else
{
/*
* Since the pathlist is sorted by total_cost, we can stop looking
* once we reach a path with a total_cost larger than the new
* path's.
*/
break;
}
}
return true;
}
/*****************************************************************************
* PATH NODE CREATION ROUTINES
*****************************************************************************/
#ifdef XCP
/*
* restrict_distribution
* Analyze the RestrictInfo and decide if it is possible to restrict
* distribution nodes
*/
static void
restrict_distribution(PlannerInfo *root, RestrictInfo *ri,
Path *pathnode)
{
Distribution *distribution = pathnode->distribution;
Oid keytype;
Const *constExpr = NULL;
bool found_key = false;
/*
* Can not restrict - not distributed or key is not defined
*/
if (distribution == NULL ||
distribution->distributionExpr == NULL)
return;
/*
* We do not support OR'ed conditions yet
*/
if (ri->orclause)
return;
keytype = exprType(distribution->distributionExpr);
if (ri->left_ec)
{
EquivalenceClass *ec = ri->left_ec;
ListCell *lc;
foreach(lc, ec->ec_members)
{
EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
if (equal(em->em_expr, distribution->distributionExpr))
found_key = true;
else if (bms_is_empty(em->em_relids))
{
Expr *cexpr = (Expr *) eval_const_expressions(root,
(Node *) em->em_expr);
if (IsA(cexpr, Const) &&
((Const *) cexpr)->consttype == keytype)
constExpr = (Const *) cexpr;
}
}
}
if (ri->right_ec)
{
EquivalenceClass *ec = ri->right_ec;
ListCell *lc;
foreach(lc, ec->ec_members)
{
EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
if (equal(em->em_expr, distribution->distributionExpr))
found_key = true;
else if (bms_is_empty(em->em_relids))
{
Expr *cexpr = (Expr *) eval_const_expressions(root,
(Node *) em->em_expr);
if (IsA(cexpr, Const) &&
((Const *) cexpr)->consttype == keytype)
constExpr = (Const *) cexpr;
}
}
}
if (IsA(ri->clause, OpExpr))
{
OpExpr *opexpr = (OpExpr *) ri->clause;
if (opexpr->args->length == 2 &&
op_mergejoinable(opexpr->opno, exprType(linitial(opexpr->args))))
{
Expr *arg1 = (Expr *) linitial(opexpr->args);
Expr *arg2 = (Expr *) lsecond(opexpr->args);
Expr *other = NULL;
if (equal(arg1, distribution->distributionExpr))
other = arg2;
else if (equal(arg2, distribution->distributionExpr))
other = arg1;
if (other)
{
found_key = true;
other = (Expr *) eval_const_expressions(root, (Node *) other);
if (IsA(other, Const) &&
((Const *) other)->consttype == keytype)
constExpr = (Const *) other;
}
}
}
if (found_key && constExpr)
{
List *nodeList = NIL;
Bitmapset *tmpset = bms_copy(distribution->nodes);
Bitmapset *restrictinfo = NULL;
Locator *locator;
int *nodenums;
int i, count;
while((i = bms_first_member(tmpset)) >= 0)
nodeList = lappend_int(nodeList, i);
bms_free(tmpset);
locator = createLocator(distribution->distributionType,
RELATION_ACCESS_READ,
keytype,
LOCATOR_LIST_LIST,
0,
(void *) nodeList,
(void **) &nodenums,
false);
count = GET_NODES(locator, constExpr->constvalue,
constExpr->constisnull, NULL);
for (i = 0; i < count; i++)
restrictinfo = bms_add_member(restrictinfo, nodenums[i]);
if (distribution->restrictNodes)
distribution->restrictNodes = bms_intersect(distribution->restrictNodes,
restrictinfo);
else
distribution->restrictNodes = restrictinfo;
list_free(nodeList);
freeLocator(locator);
}
}
/*
* set_scanpath_distribution
* Assign distribution to the path which is a base relation scan.
*/
static void
set_scanpath_distribution(PlannerInfo *root, RelOptInfo *rel, Path *pathnode)
{
RangeTblEntry *rte;
RelationLocInfo *rel_loc_info;
rte = planner_rt_fetch(rel->relid, root);
rel_loc_info = GetRelationLocInfo(rte->relid);
if (rel_loc_info)
{
ListCell *lc;
Distribution *distribution = makeNode(Distribution);
distribution->distributionType = rel_loc_info->locatorType;
foreach(lc, rel_loc_info->nodeList)
distribution->nodes = bms_add_member(distribution->nodes,
lfirst_int(lc));
distribution->restrictNodes = NULL;
/*
* Distribution expression of the base relation is Var representing
* respective attribute.
*/
distribution->distributionExpr = NULL;
if (rel_loc_info->partAttrNum)
{
Var *var = NULL;
ListCell *lc;
/* Look if the Var is already in the target list */
foreach (lc, rel->reltargetlist)
{
var = (Var *) lfirst(lc);
if (IsA(var, Var) && var->varno == rel->relid &&
var->varattno == rel_loc_info->partAttrNum)
break;
}
/* If not found we should look up the attribute and make the Var */
if (!lc)
{
Relation relation = heap_open(rte->relid, NoLock);
TupleDesc tdesc = RelationGetDescr(relation);
Form_pg_attribute att_tup;
att_tup = tdesc->attrs[rel_loc_info->partAttrNum - 1];
var = makeVar(rel->relid, rel_loc_info->partAttrNum,
att_tup->atttypid, att_tup->atttypmod,
att_tup->attcollation, 0);
heap_close(relation, NoLock);
}
distribution->distributionExpr = (Node *) var;
}
pathnode->distribution = distribution;
}
}
/*
* Set a RemoteSubPath on top of the specified node and set specified
* distribution to it
*/
static Path *
redistribute_path(Path *subpath, char distributionType,
Bitmapset *nodes, Bitmapset *restrictNodes,
Node* distributionExpr)
{
Distribution *distribution = NULL;
RelOptInfo *rel = subpath->parent;
RemoteSubPath *pathnode;
if (distributionType != LOCATOR_TYPE_NONE)
{
distribution = makeNode(Distribution);
distribution->distributionType = distributionType;
distribution->nodes = nodes;
distribution->restrictNodes = restrictNodes;
distribution->distributionExpr = distributionExpr;
}
/*
* If inner path node is a MaterialPath pull it up to store tuples on
* the destination nodes and avoid sending them over the network.
*/
if (IsA(subpath, MaterialPath))
{
MaterialPath *mpath = (MaterialPath *) subpath;
/* If subpath is already a RemoteSubPath, just replace distribution */
if (IsA(mpath->subpath, RemoteSubPath))
{
pathnode = (RemoteSubPath *) mpath->subpath;
}
else
{
pathnode = makeNode(RemoteSubPath);
pathnode->path.pathtype = T_RemoteSubplan;
pathnode->path.parent = rel;
pathnode->path.param_info = subpath->param_info;
pathnode->path.pathkeys = subpath->pathkeys;
pathnode->subpath = mpath->subpath;
mpath->subpath = (Path *) pathnode;
}
subpath = pathnode->subpath;
pathnode->path.distribution = distribution;
mpath->path.distribution = (Distribution *) copyObject(distribution);
/* (re)calculate costs */
cost_remote_subplan((Path *) pathnode, subpath->startup_cost,
subpath->total_cost, subpath->rows, rel->width,
IsLocatorReplicated(distributionType) ?
bms_num_members(nodes) : 1);
mpath->subpath = (Path *) pathnode;
cost_material(&mpath->path,
pathnode->path.startup_cost,
pathnode->path.total_cost,
pathnode->path.rows,
rel->width);
return (Path *) mpath;
}
else
{
pathnode = makeNode(RemoteSubPath);
pathnode->path.pathtype = T_RemoteSubplan;
pathnode->path.parent = rel;
pathnode->path.param_info = subpath->param_info;
pathnode->path.pathkeys = subpath->pathkeys;
pathnode->subpath = subpath;
pathnode->path.distribution = distribution;
cost_remote_subplan((Path *) pathnode, subpath->startup_cost,
subpath->total_cost, subpath->rows, rel->width,
IsLocatorReplicated(distributionType) ?
bms_num_members(nodes) : 1);
return (Path *) pathnode;
}
}
static JoinPath *
flatCopyJoinPath(JoinPath *pathnode)
{
JoinPath *newnode;
size_t size = 0;
switch(nodeTag(pathnode))
{
case T_NestPath:
size = sizeof(NestPath);
break;
case T_MergePath:
size = sizeof(MergePath);
break;
case T_HashPath:
size = sizeof(HashPath);
break;
default:
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(pathnode));
break;
}
newnode = (JoinPath *) palloc(size);
memcpy(newnode, pathnode, size);
return newnode;
}
/*
* Analyze join parameters and set distribution of the join node.
* If there are possible alternate distributions the respective pathes are
* returned as a list so caller can cost all of them and choose cheapest to
* continue.
*/
static List *
set_joinpath_distribution(PlannerInfo *root, JoinPath *pathnode)
{
Distribution *innerd = pathnode->innerjoinpath->distribution;
Distribution *outerd = pathnode->outerjoinpath->distribution;
Distribution *targetd;
List *alternate = NIL;
/* Catalog join */
if (innerd == NULL && outerd == NULL)
return NIL;
/*
* If both subpaths are distributed by replication, the resulting
* distribution will be replicated on smallest common set of nodes.
* Catalog tables are the same on all nodes, so treat them as replicated
* on all nodes.
*/
if ((innerd && IsLocatorReplicated(innerd->distributionType)) &&
(outerd && IsLocatorReplicated(outerd->distributionType)))
{
/* Determine common nodes */
Bitmapset *common;
common = bms_intersect(innerd->nodes, outerd->nodes);
if (bms_is_empty(common))
goto not_allowed_join;
/*
* Join result is replicated on common nodes. Running query on any
* of them produce correct result.
*/
targetd = makeNode(Distribution);
targetd->distributionType = LOCATOR_TYPE_REPLICATED;
targetd->nodes = common;
targetd->restrictNodes = NULL;
pathnode->path.distribution = targetd;
return alternate;
}
/*
* Check if we have inner replicated
* The "both replicated" case is already checked, so if innerd
* is replicated, then outerd is not replicated and it is not NULL.
* This case is not acceptable for some join types. If outer relation is
* nullable data nodes will produce joined rows with NULLs for cases when
* matching row exists, but on other data node.
*/
if ((innerd && IsLocatorReplicated(innerd->distributionType)) &&
(pathnode->jointype == JOIN_INNER ||
pathnode->jointype == JOIN_LEFT ||
pathnode->jointype == JOIN_SEMI ||
pathnode->jointype == JOIN_ANTI))
{
/* We need inner relation is defined on all nodes where outer is */
if (!outerd || !bms_is_subset(outerd->nodes, innerd->nodes))
goto not_allowed_join;
targetd = makeNode(Distribution);
targetd->distributionType = outerd->distributionType;
targetd->nodes = bms_copy(outerd->nodes);
targetd->restrictNodes = bms_copy(outerd->restrictNodes);
targetd->distributionExpr = outerd->distributionExpr;
pathnode->path.distribution = targetd;
return alternate;
}
/*
* Check if we have outer replicated
* The "both replicated" case is already checked, so if outerd
* is replicated, then innerd is not replicated and it is not NULL.
* This case is not acceptable for some join types. If inner relation is
* nullable data nodes will produce joined rows with NULLs for cases when
* matching row exists, but on other data node.
*/
if ((outerd && IsLocatorReplicated(outerd->distributionType)) &&
(pathnode->jointype == JOIN_INNER ||
pathnode->jointype == JOIN_RIGHT))
{
/* We need outer relation is defined on all nodes where inner is */
if (!innerd || !bms_is_subset(innerd->nodes, outerd->nodes))
goto not_allowed_join;
targetd = makeNode(Distribution);
targetd->distributionType = innerd->distributionType;
targetd->nodes = bms_copy(innerd->nodes);
targetd->restrictNodes = bms_copy(innerd->restrictNodes);
targetd->distributionExpr = innerd->distributionExpr;
pathnode->path.distribution = targetd;
return alternate;
}
/*
* This join is still allowed if inner and outer paths have
* equivalent distribution and joined along the distribution keys.
*/
if (innerd && outerd &&
innerd->distributionType == outerd->distributionType &&
innerd->distributionExpr &&
outerd->distributionExpr &&
bms_equal(innerd->nodes, outerd->nodes))
{
ListCell *lc;
/*
* Make sure distribution functions are the same, for now they depend
* on data type
*/
if (exprType((Node *) innerd->distributionExpr) != exprType((Node *) outerd->distributionExpr))
goto not_allowed_join;
/*
* Planner already did necessary work and if there is a join
* condition like left.key=right.key the key expressions
* will be members of the same equivalence class, and both
* sides of the corresponding RestrictInfo will refer that
* Equivalence Class.
* Try to figure out if such restriction exists.
*/
foreach(lc, pathnode->joinrestrictinfo)
{
RestrictInfo *ri = (RestrictInfo *) lfirst(lc);
ListCell *emc;
bool found_outer, found_inner;
/*
* Restriction operator is not equality operator ?
*/
if (ri->left_ec == NULL || ri->right_ec == NULL)
continue;
/*
* A restriction with OR may be compatible if all OR'ed
* conditions are compatible. For the moment we do not
* check this and skip restriction. The case if multiple
* OR'ed conditions are compatible is rare and probably
* do not worth doing at all.
*/
if (ri->orclause)
continue;
found_outer = false;
found_inner = false;
/*
* If parts belong to the same equivalence member check
* if both distribution keys are members of the class.
*/
if (ri->left_ec == ri->right_ec)
{
foreach(emc, ri->left_ec->ec_members)
{
EquivalenceMember *em = (EquivalenceMember *) lfirst(emc);
Expr *var = (Expr *)em->em_expr;
if (IsA(var, RelabelType))
var = ((RelabelType *) var)->arg;
if (!found_outer)
found_outer = equal(var, outerd->distributionExpr);
if (!found_inner)
found_inner = equal(var, innerd->distributionExpr);
}
if (found_outer && found_inner)
{
ListCell *tlc, *emc;
targetd = makeNode(Distribution);
targetd->distributionType = innerd->distributionType;
targetd->nodes = bms_copy(innerd->nodes);
targetd->restrictNodes = bms_copy(innerd->restrictNodes);
targetd->distributionExpr = NULL;
pathnode->path.distribution = targetd;
/*
* Each member of the equivalence class may be a
* distribution expression, but we prefer some from the
* target list.
*/
foreach(tlc, pathnode->path.parent->reltargetlist)
{
Expr *var = (Expr *) lfirst(tlc);
foreach(emc, ri->left_ec->ec_members)
{
EquivalenceMember *em;
Expr *emvar;
em = (EquivalenceMember *) lfirst(emc);
emvar = (Expr *)em->em_expr;
if (IsA(emvar, RelabelType))
emvar = ((RelabelType *) emvar)->arg;
if (equal(var, emvar))
{
targetd->distributionExpr = (Node *) var;
return alternate;
}
}
}
/* Not found, take any */
targetd->distributionExpr = innerd->distributionExpr;
return alternate;
}
}
/*
* Check clause, if both arguments are distribution keys and
* operator is an equality operator
*/
else
{
OpExpr *op_exp;
Expr *arg1,
*arg2;
op_exp = (OpExpr *) ri->clause;
if (!IsA(op_exp, OpExpr) || list_length(op_exp->args) != 2)
continue;
arg1 = (Expr *) linitial(op_exp->args);
arg2 = (Expr *) lsecond(op_exp->args);
found_outer = equal(arg1, outerd->distributionExpr) || equal(arg2, outerd->distributionExpr);
found_inner = equal(arg1, innerd->distributionExpr) || equal(arg2, innerd->distributionExpr);
if (found_outer && found_inner)
{
targetd = makeNode(Distribution);
targetd->distributionType = innerd->distributionType;
targetd->nodes = bms_copy(innerd->nodes);
targetd->restrictNodes = bms_copy(innerd->restrictNodes);
pathnode->path.distribution = targetd;
/*
* In case of outer join distribution key should not refer
* distribution key of nullable part.
*/
if (pathnode->jointype == JOIN_FULL)
/* both parts are nullable */
targetd->distributionExpr = NULL;
else if (pathnode->jointype == JOIN_RIGHT)
targetd->distributionExpr = innerd->distributionExpr;
else
targetd->distributionExpr = outerd->distributionExpr;
return alternate;
}
}
}
}
/*
* If we could not determine the distribution redistribute the subpathes.
*/
not_allowed_join:
/*
* If redistribution is required, sometimes the cheapest path would be if
* one of the subplan is replicated. If replication of any or all subplans
* is possible, return resulting plans as alternates. Try to distribute all
* by has as main variant.
*/
#ifdef NOT_USED
/* These join types allow replicated inner */
if (outerd &&
(pathnode->jointype == JOIN_INNER ||
pathnode->jointype == JOIN_LEFT ||
pathnode->jointype == JOIN_SEMI ||
pathnode->jointype == JOIN_ANTI))
{
/*
* Since we discard all alternate pathes except one it is OK if all they
* reference the same objects
*/
JoinPath *altpath = flatCopyJoinPath(pathnode);
/* Redistribute inner subquery */
altpath->innerjoinpath = redistribute_path(
altpath->innerjoinpath,
LOCATOR_TYPE_REPLICATED,
bms_copy(outerd->nodes),
bms_copy(outerd->restrictNodes),
NULL);
targetd = makeNode(Distribution);
targetd->distributionType = outerd->distributionType;
targetd->nodes = bms_copy(outerd->nodes);
targetd->restrictNodes = bms_copy(outerd->restrictNodes);
targetd->distributionExpr = outerd->distributionExpr;
altpath->path.distribution = targetd;
alternate = lappend(alternate, altpath);
}
/* These join types allow replicated outer */
if (innerd &&
(pathnode->jointype == JOIN_INNER ||
pathnode->jointype == JOIN_RIGHT))
{
/*
* Since we discard all alternate pathes except one it is OK if all they
* reference the same objects
*/
JoinPath *altpath = flatCopyJoinPath(pathnode);
/* Redistribute inner subquery */
altpath->outerjoinpath = redistribute_path(
altpath->outerjoinpath,
LOCATOR_TYPE_REPLICATED,
bms_copy(innerd->nodes),
bms_copy(innerd->restrictNodes),
NULL);
targetd = makeNode(Distribution);
targetd->distributionType = innerd->distributionType;
targetd->nodes = bms_copy(innerd->nodes);
targetd->restrictNodes = bms_copy(innerd->restrictNodes);
targetd->distributionExpr = innerd->distributionExpr;
altpath->path.distribution = targetd;
alternate = lappend(alternate, altpath);
}
#endif
/*
* Redistribute subplans to make them compatible.
* If any of the subplans is a coordinator subplan skip this stuff and do
* coordinator join.
*/
if (innerd && outerd)
{
RestrictInfo *preferred = NULL;
Expr *new_inner_key = NULL;
Expr *new_outer_key = NULL;
char distType = LOCATOR_TYPE_NONE;
ListCell *lc;
/*
* Look through the join restrictions to find one that is a hashable
* operator on two arguments. Choose best restriction acoording to
* following criteria:
* 1. one argument is already a partitioning key of one subplan.
* 2. restriction is cheaper to calculate
*/
foreach(lc, pathnode->joinrestrictinfo)
{
RestrictInfo *ri = (RestrictInfo *) lfirst(lc);
/* can not handle ORed conditions */
if (ri->orclause)
continue;
if (IsA(ri->clause, OpExpr))
{
OpExpr *expr = (OpExpr *) ri->clause;
if (list_length(expr->args) == 2 &&
op_hashjoinable(expr->opno, exprType(linitial(expr->args))))
{
Expr *left = (Expr *) linitial(expr->args);
Expr *right = (Expr *) lsecond(expr->args);
Oid leftType = exprType((Node *) left);
Oid rightType = exprType((Node *) right);
Relids inner_rels = pathnode->innerjoinpath->parent->relids;
Relids outer_rels = pathnode->outerjoinpath->parent->relids;
QualCost cost;
/*
* Check if both parts are of the same data type and choose
* distribution type to redistribute.
* XXX We may want more sophisticated algorithm to choose
* the best condition to redistribute parts along.
* For now use simple but reliable approach.
*/
if (leftType != rightType)
continue;
/*
* Evaluation cost will be needed to choose preferred
* distribution
*/
cost_qual_eval_node(&cost, (Node *) ri, root);
if (outerd->distributionExpr)
{
/*
* If left side is distribution key of outer subquery
* and right expression refers only inner subquery
*/
if (equal(outerd->distributionExpr, left) &&
bms_is_subset(ri->right_relids, inner_rels))
{
if (!preferred || /* no preferred restriction yet found */
(new_inner_key && new_outer_key) || /* preferred restriction require redistribution of both parts */
(cost.per_tuple < preferred->eval_cost.per_tuple)) /* current restriction is cheaper */
{
/* set new preferred restriction */
preferred = ri;
new_inner_key = right;
new_outer_key = NULL; /* no need to change */
distType = outerd->distributionType;
}
continue;
}
/*
* If right side is distribution key of outer subquery
* and left expression refers only inner subquery
*/
if (equal(outerd->distributionExpr, right) &&
bms_is_subset(ri->left_relids, inner_rels))
{
if (!preferred || /* no preferred restriction yet found */
(new_inner_key && new_outer_key) || /* preferred restriction require redistribution of both parts */
(cost.per_tuple < preferred->eval_cost.per_tuple)) /* current restriction is cheaper */
{
/* set new preferred restriction */
preferred = ri;
new_inner_key = left;
new_outer_key = NULL; /* no need to change */
distType = outerd->distributionType;
}
continue;
}
}
if (innerd->distributionExpr)
{
/*
* If left side is distribution key of inner subquery
* and right expression refers only outer subquery
*/
if (equal(innerd->distributionExpr, left) &&
bms_is_subset(ri->right_relids, outer_rels))
{
if (!preferred || /* no preferred restriction yet found */
(new_inner_key && new_outer_key) || /* preferred restriction require redistribution of both parts */
(cost.per_tuple < preferred->eval_cost.per_tuple)) /* current restriction is cheaper */
{
/* set new preferred restriction */
preferred = ri;
new_inner_key = NULL; /* no need to change */
new_outer_key = right;
distType = innerd->distributionType;
}
continue;
}
/*
* If right side is distribution key of inner subquery
* and left expression refers only outer subquery
*/
if (equal(innerd->distributionExpr, right) &&
bms_is_subset(ri->left_relids, outer_rels))
{
if (!preferred || /* no preferred restriction yet found */
(new_inner_key && new_outer_key) || /* preferred restriction require redistribution of both parts */
(cost.per_tuple < preferred->eval_cost.per_tuple)) /* current restriction is cheaper */
{
/* set new preferred restriction */
preferred = ri;
new_inner_key = NULL; /* no need to change */
new_outer_key = left;
distType = innerd->distributionType;
}
continue;
}
}
/*
* Current restriction recuire redistribution of both parts.
* If preferred restriction require redistribution of one,
* keep it.
*/
if (preferred &&
(new_inner_key == NULL || new_outer_key == NULL))
continue;
/*
* Skip this condition if the data type of the expressions
* does not allow either HASH or MODULO distribution.
* HASH distribution is preferrable.
*/
if (IsTypeHashDistributable(leftType))
distType = LOCATOR_TYPE_HASH;
else if (IsTypeModuloDistributable(leftType))
distType = LOCATOR_TYPE_MODULO;
else
continue;
/*
* If this restriction the first or easier to calculate
* then preferred, try to store it as new preferred
* restriction to redistribute along it.
*/
if (preferred == NULL ||
(cost.per_tuple < preferred->eval_cost.per_tuple))
{
/*
* Left expression depends only on outer subpath and
* right expression depends only on inner subpath, so
* we can redistribute both and make left expression the
* distribution key of outer subplan and right
* expression the distribution key of inner subplan
*/
if (bms_is_subset(ri->left_relids, outer_rels) &&
bms_is_subset(ri->right_relids, inner_rels))
{
preferred = ri;
new_outer_key = left;
new_inner_key = right;
}
/*
* Left expression depends only on inner subpath and
* right expression depends only on outer subpath, so
* we can redistribute both and make left expression the
* distribution key of inner subplan and right
* expression the distribution key of outer subplan
*/
if (bms_is_subset(ri->left_relids, inner_rels) &&
bms_is_subset(ri->right_relids, outer_rels))
{
preferred = ri;
new_inner_key = left;
new_outer_key = right;
}
}
}
}
}
/* If we have suitable restriction we can repartition accordingly */
if (preferred)
{
Bitmapset *nodes = NULL;
Bitmapset *restrictNodes = NULL;
/* If we redistribute both parts do join on all nodes ... */
if (new_inner_key && new_outer_key)
{
int i;
for (i = 0; i < NumDataNodes; i++)
nodes = bms_add_member(nodes, i);
}
/*
* ... if we do only one of them redistribute it on the same nodes
* as other.
*/
else if (new_inner_key)
{
nodes = bms_copy(outerd->nodes);
restrictNodes = bms_copy(outerd->restrictNodes);
}
else /*if (new_outer_key)*/
{
nodes = bms_copy(innerd->nodes);
restrictNodes = bms_copy(innerd->restrictNodes);
}
/*
* Redistribute join by hash, and, if jointype allows, create
* alternate path where inner subplan is distributed by replication
*/
if (new_inner_key)
{
/* Redistribute inner subquery */
pathnode->innerjoinpath = redistribute_path(
pathnode->innerjoinpath,
distType,
nodes,
restrictNodes,
(Node *) new_inner_key);
}
/*
* Redistribute join by hash, and, if jointype allows, create
* alternate path where outer subplan is distributed by replication
*/
if (new_outer_key)
{
/* Redistribute outer subquery */
pathnode->outerjoinpath = redistribute_path(
pathnode->outerjoinpath,
distType,
nodes,
restrictNodes,
(Node *) new_outer_key);
}
targetd = makeNode(Distribution);
targetd->distributionType = distType;
targetd->nodes = nodes;
targetd->restrictNodes = NULL;
pathnode->path.distribution = targetd;
/*
* In case of outer join distribution key should not refer
* distribution key of nullable part.
* NB: we should not refer innerd and outerd here, subpathes are
* redistributed already
*/
if (pathnode->jointype == JOIN_FULL)
/* both parts are nullable */
targetd->distributionExpr = NULL;
else if (pathnode->jointype == JOIN_RIGHT)
targetd->distributionExpr =
pathnode->innerjoinpath->distribution->distributionExpr;
else
targetd->distributionExpr =
pathnode->outerjoinpath->distribution->distributionExpr;
return alternate;
}
}
/*
* Build cartesian product, if no hasheable restrictions is found.
* Perform coordinator join in such cases. If this join would be a part of
* larger join, it will be handled as replicated.
* To do that leave join distribution NULL and place a RemoteSubPath node on
* top of each subpath to provide access to joined result sets.
* Do not redistribute pathes that already have NULL distribution, this is
* possible if performing outer join on a coordinator and a datanode
* relations.
*/
if (innerd)
pathnode->innerjoinpath = redistribute_path(pathnode->innerjoinpath,
LOCATOR_TYPE_NONE,
NULL,
NULL,
NULL);
if (outerd)
pathnode->outerjoinpath = redistribute_path(pathnode->outerjoinpath,
LOCATOR_TYPE_NONE,
NULL,
NULL,
NULL);
return alternate;
}
#endif
/*
* create_seqscan_path
* Creates a path corresponding to a sequential scan, returning the
* pathnode.
*/
Path *
create_seqscan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
{
Path *pathnode = makeNode(Path);
pathnode->pathtype = T_SeqScan;
pathnode->parent = rel;
pathnode->param_info = get_baserel_parampathinfo(root, rel,
required_outer);
pathnode->pathkeys = NIL; /* seqscan has unordered result */
#ifdef XCP
set_scanpath_distribution(root, rel, pathnode);
if (rel->baserestrictinfo)
{
ListCell *lc;
foreach (lc, rel->baserestrictinfo)
{
RestrictInfo *ri = (RestrictInfo *) lfirst(lc);
restrict_distribution(root, ri, pathnode);
}
}
#endif
cost_seqscan(pathnode, root, rel, pathnode->param_info);
return pathnode;
}
/*
* create_index_path
* Creates a path node for an index scan.
*
* 'index' is a usable index.
* 'indexclauses' is a list of RestrictInfo nodes representing clauses
* to be used as index qual conditions in the scan.
* 'indexclausecols' is an integer list of index column numbers (zero based)
* the indexclauses can be used with.
* 'indexorderbys' is a list of bare expressions (no RestrictInfos)
* to be used as index ordering operators in the scan.
* 'indexorderbycols' is an integer list of index column numbers (zero based)
* the ordering operators can be used with.
* 'pathkeys' describes the ordering of the path.
* 'indexscandir' is ForwardScanDirection or BackwardScanDirection
* for an ordered index, or NoMovementScanDirection for
* an unordered index.
* 'indexonly' is true if an index-only scan is wanted.
* 'required_outer' is the set of outer relids for a parameterized path.
* 'loop_count' is the number of repetitions of the indexscan to factor into
* estimates of caching behavior.
*
* Returns the new path node.
*/
IndexPath *
create_index_path(PlannerInfo *root,
IndexOptInfo *index,
List *indexclauses,
List *indexclausecols,
List *indexorderbys,
List *indexorderbycols,
List *pathkeys,
ScanDirection indexscandir,
bool indexonly,
Relids required_outer,
double loop_count)
{
IndexPath *pathnode = makeNode(IndexPath);
RelOptInfo *rel = index->rel;
List *indexquals,
*indexqualcols;
pathnode->path.pathtype = indexonly ? T_IndexOnlyScan : T_IndexScan;
pathnode->path.parent = rel;
pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
required_outer);
pathnode->path.pathkeys = pathkeys;
/* Convert clauses to indexquals the executor can handle */
expand_indexqual_conditions(index, indexclauses, indexclausecols,
&indexquals, &indexqualcols);
/* Fill in the pathnode */
pathnode->indexinfo = index;
pathnode->indexclauses = indexclauses;
pathnode->indexquals = indexquals;
pathnode->indexqualcols = indexqualcols;
pathnode->indexorderbys = indexorderbys;
pathnode->indexorderbycols = indexorderbycols;
pathnode->indexscandir = indexscandir;
#ifdef XCP
set_scanpath_distribution(root, rel, (Path *) pathnode);
if (indexclauses)
{
ListCell *lc;
foreach (lc, indexclauses)
{
RestrictInfo *ri = (RestrictInfo *) lfirst(lc);
restrict_distribution(root, ri, (Path *) pathnode);
}
}
#endif
cost_index(pathnode, root, loop_count);
return pathnode;
}
/*
* create_bitmap_heap_path
* Creates a path node for a bitmap scan.
*
* 'bitmapqual' is a tree of IndexPath, BitmapAndPath, and BitmapOrPath nodes.
* 'required_outer' is the set of outer relids for a parameterized path.
* 'loop_count' is the number of repetitions of the indexscan to factor into
* estimates of caching behavior.
*
* loop_count should match the value used when creating the component
* IndexPaths.
*/
BitmapHeapPath *
create_bitmap_heap_path(PlannerInfo *root,
RelOptInfo *rel,
Path *bitmapqual,
Relids required_outer,
double loop_count)
{
BitmapHeapPath *pathnode = makeNode(BitmapHeapPath);
pathnode->path.pathtype = T_BitmapHeapScan;
pathnode->path.parent = rel;
pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
required_outer);
pathnode->path.pathkeys = NIL; /* always unordered */
pathnode->bitmapqual = bitmapqual;
#ifdef XCP
set_scanpath_distribution(root, rel, (Path *) pathnode);
if (rel->baserestrictinfo)
{
ListCell *lc;
foreach (lc, rel->baserestrictinfo)
{
RestrictInfo *ri = (RestrictInfo *) lfirst(lc);
restrict_distribution(root, ri, (Path *) pathnode);
}
}
#endif
cost_bitmap_heap_scan(&pathnode->path, root, rel,
pathnode->path.param_info,
bitmapqual, loop_count);
return pathnode;
}
/*
* create_bitmap_and_path
* Creates a path node representing a BitmapAnd.
*/
BitmapAndPath *
create_bitmap_and_path(PlannerInfo *root,
RelOptInfo *rel,
List *bitmapquals)
{
BitmapAndPath *pathnode = makeNode(BitmapAndPath);
pathnode->path.pathtype = T_BitmapAnd;
pathnode->path.parent = rel;
pathnode->path.param_info = NULL; /* not used in bitmap trees */
pathnode->path.pathkeys = NIL; /* always unordered */
pathnode->bitmapquals = bitmapquals;
#ifdef XCP
set_scanpath_distribution(root, rel, (Path *) pathnode);
#endif
/* this sets bitmapselectivity as well as the regular cost fields: */
cost_bitmap_and_node(pathnode, root);
return pathnode;
}
/*
* create_bitmap_or_path
* Creates a path node representing a BitmapOr.
*/
BitmapOrPath *
create_bitmap_or_path(PlannerInfo *root,
RelOptInfo *rel,
List *bitmapquals)
{
BitmapOrPath *pathnode = makeNode(BitmapOrPath);
pathnode->path.pathtype = T_BitmapOr;
pathnode->path.parent = rel;
pathnode->path.param_info = NULL; /* not used in bitmap trees */
pathnode->path.pathkeys = NIL; /* always unordered */
pathnode->bitmapquals = bitmapquals;
#ifdef XCP
set_scanpath_distribution(root, rel, (Path *) pathnode);
#endif
/* this sets bitmapselectivity as well as the regular cost fields: */
cost_bitmap_or_node(pathnode, root);
return pathnode;
}
/*
* create_tidscan_path
* Creates a path corresponding to a scan by TID, returning the pathnode.
*/
TidPath *
create_tidscan_path(PlannerInfo *root, RelOptInfo *rel, List *tidquals,
Relids required_outer)
{
TidPath *pathnode = makeNode(TidPath);
pathnode->path.pathtype = T_TidScan;
pathnode->path.parent = rel;
pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
required_outer);
pathnode->path.pathkeys = NIL; /* always unordered */
pathnode->tidquals = tidquals;
#ifdef XCP
set_scanpath_distribution(root, rel, (Path *) pathnode);
/* We may need to pass info about target node to support */
if (pathnode->path.distribution)
elog(ERROR, "could not perform TID scan on remote relation");
#endif
cost_tidscan(&pathnode->path, root, rel, tidquals,
pathnode->path.param_info);
return pathnode;
}
/*
* create_append_path
* Creates a path corresponding to an Append plan, returning the
* pathnode.
*
* Note that we must handle subpaths = NIL, representing a dummy access path.
*/
AppendPath *
create_append_path(RelOptInfo *rel, List *subpaths, Relids required_outer)
{
AppendPath *pathnode = makeNode(AppendPath);
ListCell *l;
#ifdef XCP
Distribution *distribution;
Path *subpath;
#endif
pathnode->path.pathtype = T_Append;
pathnode->path.parent = rel;
pathnode->path.param_info = get_appendrel_parampathinfo(rel,
required_outer);
pathnode->path.pathkeys = NIL; /* result is always considered
* unsorted */
#ifdef XCP
/*
* Append path is used to implement scans of inherited tables and some
* "set" operations, like UNION ALL. While all inherited tables should
* have the same distribution, UNION'ed queries may have different.
* When paths being appended have the same distribution it is OK to push
* Append down to the data nodes. If not, perform "coordinator" Append.
*/
/* Special case of the dummy relation, if the subpaths list is empty */
if (subpaths)
{
/* Take distribution of the first node */
l = list_head(subpaths);
subpath = (Path *) lfirst(l);
distribution = copyObject(subpath->distribution);
/*
* Check remaining subpaths, if all distributions equal to the first set
* it as a distribution of the Append path; otherwise make up coordinator
* Append
*/
while ((l = lnext(l)))
{
subpath = (Path *) lfirst(l);
if (equal(distribution, subpath->distribution))
{
/*
* Both distribution and subpath->distribution may be NULL at
* this point, or they both are not null.
*/
if (distribution && subpath->distribution->restrictNodes)
distribution->restrictNodes = bms_union(
distribution->restrictNodes,
subpath->distribution->restrictNodes);
}
else
{
break;
}
}
if (l)
{
List *newsubpaths = NIL;
foreach(l, subpaths)
{
subpath = (Path *) lfirst(l);
if (subpath->distribution)
subpath = redistribute_path(subpath, LOCATOR_TYPE_NONE,
NULL, NULL, NULL);
newsubpaths = lappend(newsubpaths, subpath);
}
subpaths = newsubpaths;
pathnode->path.distribution = NULL;
}
else
pathnode->path.distribution = distribution;
}
#endif
pathnode->subpaths = subpaths;
/*
* We don't bother with inventing a cost_append(), but just do it here.
*
* Compute rows and costs as sums of subplan rows and costs. We charge
* nothing extra for the Append itself, which perhaps is too optimistic,
* but since it doesn't do any selection or projection, it is a pretty
* cheap node. If you change this, see also make_append().
*/
pathnode->path.rows = 0;
pathnode->path.startup_cost = 0;
pathnode->path.total_cost = 0;
foreach(l, subpaths)
{
Path *subpath = (Path *) lfirst(l);
pathnode->path.rows += subpath->rows;
if (l == list_head(subpaths)) /* first node? */
pathnode->path.startup_cost = subpath->startup_cost;
pathnode->path.total_cost += subpath->total_cost;
/* All child paths must have same parameterization */
Assert(bms_equal(PATH_REQ_OUTER(subpath), required_outer));
}
return pathnode;
}
/*
* create_merge_append_path
* Creates a path corresponding to a MergeAppend plan, returning the
* pathnode.
*/
MergeAppendPath *
create_merge_append_path(PlannerInfo *root,
RelOptInfo *rel,
List *subpaths,
List *pathkeys,
Relids required_outer)
{
MergeAppendPath *pathnode = makeNode(MergeAppendPath);
Cost input_startup_cost;
Cost input_total_cost;
ListCell *l;
#ifdef XCP
Distribution *distribution = NULL;
Path *subpath;
#endif
pathnode->path.pathtype = T_MergeAppend;
pathnode->path.parent = rel;
#ifdef XCP
/*
* It is safe to push down MergeAppend if all subpath distributions
* are the same and these distributions are Replicated or distribution key
* is the expression of the first pathkey.
*/
/* Take distribution of the first node */
l = list_head(subpaths);
subpath = (Path *) lfirst(l);
distribution = copyObject(subpath->distribution);
/*
* Verify if it is safe to push down MergeAppend with this distribution.
* TODO implement check of the second condition (distribution key is the
* first pathkey)
*/
if (distribution == NULL || IsLocatorReplicated(distribution->distributionType))
{
/*
* Check remaining subpaths, if all distributions equal to the first set
* it as a distribution of the Append path; otherwise make up coordinator
* Append
*/
while ((l = lnext(l)))
{
subpath = (Path *) lfirst(l);
if (distribution && equal(distribution, subpath->distribution))
{
if (subpath->distribution->restrictNodes)
distribution->restrictNodes = bms_union(
distribution->restrictNodes,
subpath->distribution->restrictNodes);
}
else
{
break;
}
}
}
if (l)
{
List *newsubpaths = NIL;
foreach(l, subpaths)
{
subpath = (Path *) lfirst(l);
if (subpath->distribution)
subpath = redistribute_path(subpath, LOCATOR_TYPE_NONE,
NULL, NULL, NULL);
newsubpaths = lappend(newsubpaths, subpath);
}
subpaths = newsubpaths;
pathnode->path.distribution = NULL;
}
else
pathnode->path.distribution = distribution;
#endif
pathnode->path.param_info = get_appendrel_parampathinfo(rel,
required_outer);
pathnode->path.pathkeys = pathkeys;
pathnode->subpaths = subpaths;
/*
* Apply query-wide LIMIT if known and path is for sole base relation.
* (Handling this at this low level is a bit klugy.)
*/
if (bms_equal(rel->relids, root->all_baserels))
pathnode->limit_tuples = root->limit_tuples;
else
pathnode->limit_tuples = -1.0;
/*
* Add up the sizes and costs of the input paths.
*/
pathnode->path.rows = 0;
input_startup_cost = 0;
input_total_cost = 0;
foreach(l, subpaths)
{
Path *subpath = (Path *) lfirst(l);
pathnode->path.rows += subpath->rows;
if (pathkeys_contained_in(pathkeys, subpath->pathkeys))
{
/* Subpath is adequately ordered, we won't need to sort it */
input_startup_cost += subpath->startup_cost;
input_total_cost += subpath->total_cost;
}
else
{
/* We'll need to insert a Sort node, so include cost for that */
Path sort_path; /* dummy for result of cost_sort */
cost_sort(&sort_path,
root,
pathkeys,
subpath->total_cost,
subpath->parent->tuples,
subpath->parent->width,
0.0,
work_mem,
pathnode->limit_tuples);
input_startup_cost += sort_path.startup_cost;
input_total_cost += sort_path.total_cost;
}
/* All child paths must have same parameterization */
Assert(bms_equal(PATH_REQ_OUTER(subpath), required_outer));
}
/* Now we can compute total costs of the MergeAppend */
cost_merge_append(&pathnode->path, root,
pathkeys, list_length(subpaths),
input_startup_cost, input_total_cost,
rel->tuples);
return pathnode;
}
/*
* create_result_path
* Creates a path representing a Result-and-nothing-else plan.
* This is only used for the case of a query with an empty jointree.
*/
ResultPath *
create_result_path(List *quals)
{
ResultPath *pathnode = makeNode(ResultPath);
pathnode->path.pathtype = T_Result;
pathnode->path.parent = NULL;
pathnode->path.param_info = NULL; /* there are no other rels... */
pathnode->path.pathkeys = NIL;
pathnode->quals = quals;
/* Hardly worth defining a cost_result() function ... just do it */
pathnode->path.rows = 1;
pathnode->path.startup_cost = 0;
pathnode->path.total_cost = cpu_tuple_cost;
/*
* In theory we should include the qual eval cost as well, but at present
* that doesn't accomplish much except duplicate work that will be done
* again in make_result; since this is only used for degenerate cases,
* nothing interesting will be done with the path cost values...
*/
return pathnode;
}
/*
* create_material_path
* Creates a path corresponding to a Material plan, returning the
* pathnode.
*/
MaterialPath *
create_material_path(RelOptInfo *rel, Path *subpath)
{
MaterialPath *pathnode = makeNode(MaterialPath);
Assert(subpath->parent == rel);
pathnode->path.pathtype = T_Material;
pathnode->path.parent = rel;
pathnode->path.param_info = subpath->param_info;
pathnode->path.pathkeys = subpath->pathkeys;
pathnode->subpath = subpath;
#ifdef XCP
pathnode->path.distribution = (Distribution *) copyObject(subpath->distribution);
#endif
cost_material(&pathnode->path,
subpath->startup_cost,
subpath->total_cost,
subpath->rows,
rel->width);
return pathnode;
}
/*
* create_unique_path
* Creates a path representing elimination of distinct rows from the
* input data. Distinct-ness is defined according to the needs of the
* semijoin represented by sjinfo. If it is not possible to identify
* how to make the data unique, NULL is returned.
*
* If used at all, this is likely to be called repeatedly on the same rel;
* and the input subpath should always be the same (the cheapest_total path
* for the rel). So we cache the result.
*/
UniquePath *
create_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
SpecialJoinInfo *sjinfo)
{
UniquePath *pathnode;
Path sort_path; /* dummy for result of cost_sort */
Path agg_path; /* dummy for result of cost_agg */
MemoryContext oldcontext;
int numCols;
/* Caller made a mistake if subpath isn't cheapest_total ... */
Assert(subpath == rel->cheapest_total_path);
Assert(subpath->parent == rel);
/* ... or if SpecialJoinInfo is the wrong one */
Assert(sjinfo->jointype == JOIN_SEMI);
Assert(bms_equal(rel->relids, sjinfo->syn_righthand));
/* If result already cached, return it */
if (rel->cheapest_unique_path)
return (UniquePath *) rel->cheapest_unique_path;
/* If it's not possible to unique-ify, return NULL */
if (!(sjinfo->semi_can_btree || sjinfo->semi_can_hash))
return NULL;
/*
* We must ensure path struct and subsidiary data are allocated in main
* planning context; otherwise GEQO memory management causes trouble.
*/
oldcontext = MemoryContextSwitchTo(root->planner_cxt);
pathnode = makeNode(UniquePath);
pathnode->path.pathtype = T_Unique;
pathnode->path.parent = rel;
pathnode->path.param_info = subpath->param_info;
/*
* Assume the output is unsorted, since we don't necessarily have pathkeys
* to represent it. (This might get overridden below.)
*/
pathnode->path.pathkeys = NIL;
pathnode->subpath = subpath;
pathnode->in_operators = sjinfo->semi_operators;
pathnode->uniq_exprs = sjinfo->semi_rhs_exprs;
#ifdef XCP
/* distribution is the same as in the subpath */
pathnode->path.distribution = (Distribution *) copyObject(subpath->distribution);
#endif
/*
* If the input is a relation and it has a unique index that proves the
* semi_rhs_exprs are unique, then we don't need to do anything. Note
* that relation_has_unique_index_for automatically considers restriction
* clauses for the rel, as well.
*/
if (rel->rtekind == RTE_RELATION && sjinfo->semi_can_btree &&
relation_has_unique_index_for(root, rel, NIL,
sjinfo->semi_rhs_exprs,
sjinfo->semi_operators))
{
pathnode->umethod = UNIQUE_PATH_NOOP;
pathnode->path.rows = rel->rows;
pathnode->path.startup_cost = subpath->startup_cost;
pathnode->path.total_cost = subpath->total_cost;
pathnode->path.pathkeys = subpath->pathkeys;
rel->cheapest_unique_path = (Path *) pathnode;
MemoryContextSwitchTo(oldcontext);
return pathnode;
}
/*
* If the input is a subquery whose output must be unique already, then we
* don't need to do anything. The test for uniqueness has to consider
* exactly which columns we are extracting; for example "SELECT DISTINCT
* x,y" doesn't guarantee that x alone is distinct. So we cannot check for
* this optimization unless semi_rhs_exprs consists only of simple Vars
* referencing subquery outputs. (Possibly we could do something with
* expressions in the subquery outputs, too, but for now keep it simple.)
*/
if (rel->rtekind == RTE_SUBQUERY)
{
RangeTblEntry *rte = planner_rt_fetch(rel->relid, root);
if (query_supports_distinctness(rte->subquery))
{
List *sub_tlist_colnos;
sub_tlist_colnos = translate_sub_tlist(sjinfo->semi_rhs_exprs,
rel->relid);
if (sub_tlist_colnos &&
query_is_distinct_for(rte->subquery,
sub_tlist_colnos,
sjinfo->semi_operators))
{
pathnode->umethod = UNIQUE_PATH_NOOP;
pathnode->path.rows = rel->rows;
pathnode->path.startup_cost = subpath->startup_cost;
pathnode->path.total_cost = subpath->total_cost;
pathnode->path.pathkeys = subpath->pathkeys;
rel->cheapest_unique_path = (Path *) pathnode;
MemoryContextSwitchTo(oldcontext);
return pathnode;
}
}
}
/* Estimate number of output rows */
pathnode->path.rows = estimate_num_groups(root,
sjinfo->semi_rhs_exprs,
rel->rows);
numCols = list_length(sjinfo->semi_rhs_exprs);
if (sjinfo->semi_can_btree)
{
/*
* Estimate cost for sort+unique implementation
*/
cost_sort(&sort_path, root, NIL,
subpath->total_cost,
rel->rows,
rel->width,
0.0,
work_mem,
-1.0);
/*
* Charge one cpu_operator_cost per comparison per input tuple. We
* assume all columns get compared at most of the tuples. (XXX
* probably this is an overestimate.) This should agree with
* make_unique.
*/
sort_path.total_cost += cpu_operator_cost * rel->rows * numCols;
}
if (sjinfo->semi_can_hash)
{
/*
* Estimate the overhead per hashtable entry at 64 bytes (same as in
* planner.c).
*/
int hashentrysize = rel->width + 64;
if (hashentrysize * pathnode->path.rows > work_mem * 1024L)
{
/*
* We should not try to hash. Hack the SpecialJoinInfo to
* remember this, in case we come through here again.
*/
sjinfo->semi_can_hash = false;
}
else
cost_agg(&agg_path, root,
AGG_HASHED, NULL,
numCols, pathnode->path.rows,
subpath->startup_cost,
subpath->total_cost,
rel->rows);
}
if (sjinfo->semi_can_btree && sjinfo->semi_can_hash)
{
if (agg_path.total_cost < sort_path.total_cost)
pathnode->umethod = UNIQUE_PATH_HASH;
else
pathnode->umethod = UNIQUE_PATH_SORT;
}
else if (sjinfo->semi_can_btree)
pathnode->umethod = UNIQUE_PATH_SORT;
else if (sjinfo->semi_can_hash)
pathnode->umethod = UNIQUE_PATH_HASH;
else
{
/* we can get here only if we abandoned hashing above */
MemoryContextSwitchTo(oldcontext);
return NULL;
}
if (pathnode->umethod == UNIQUE_PATH_HASH)
{
pathnode->path.startup_cost = agg_path.startup_cost;
pathnode->path.total_cost = agg_path.total_cost;
}
else
{
pathnode->path.startup_cost = sort_path.startup_cost;
pathnode->path.total_cost = sort_path.total_cost;
}
rel->cheapest_unique_path = (Path *) pathnode;
MemoryContextSwitchTo(oldcontext);
return pathnode;
}
/*
* translate_sub_tlist - get subquery column numbers represented by tlist
*
* The given targetlist usually contains only Vars referencing the given relid.
* Extract their varattnos (ie, the column numbers of the subquery) and return
* as an integer List.
*
* If any of the tlist items is not a simple Var, we cannot determine whether
* the subquery's uniqueness condition (if any) matches ours, so punt and
* return NIL.
*/
static List *
translate_sub_tlist(List *tlist, int relid)
{
List *result = NIL;
ListCell *l;
foreach(l, tlist)
{
Var *var = (Var *) lfirst(l);
if (!var || !IsA(var, Var) ||
var->varno != relid)
return NIL; /* punt */
result = lappend_int(result, var->varattno);
}
return result;
}
/*
* create_subqueryscan_path
* Creates a path corresponding to a sequential scan of a subquery,
* returning the pathnode.
*/
Path *
#ifdef XCP
create_subqueryscan_path(PlannerInfo *root, RelOptInfo *rel,
List *pathkeys, Relids required_outer,
Distribution *distribution)
#else
create_subqueryscan_path(PlannerInfo *root, RelOptInfo *rel,
List *pathkeys, Relids required_outer)
#endif
{
Path *pathnode = makeNode(Path);
pathnode->pathtype = T_SubqueryScan;
pathnode->parent = rel;
pathnode->param_info = get_baserel_parampathinfo(root, rel,
required_outer);
pathnode->pathkeys = pathkeys;
#ifdef XCP
pathnode->distribution = distribution;
#endif
cost_subqueryscan(pathnode, root, rel, pathnode->param_info);
return pathnode;
}
/*
* create_functionscan_path
* Creates a path corresponding to a sequential scan of a function,
* returning the pathnode.
*/
Path *
create_functionscan_path(PlannerInfo *root, RelOptInfo *rel,
List *pathkeys, Relids required_outer)
{
Path *pathnode = makeNode(Path);
pathnode->pathtype = T_FunctionScan;
pathnode->parent = rel;
pathnode->param_info = get_baserel_parampathinfo(root, rel,
required_outer);
pathnode->pathkeys = pathkeys;
cost_functionscan(pathnode, root, rel, pathnode->param_info);
return pathnode;
}
/*
* create_valuesscan_path
* Creates a path corresponding to a scan of a VALUES list,
* returning the pathnode.
*/
Path *
create_valuesscan_path(PlannerInfo *root, RelOptInfo *rel,
Relids required_outer)
{
Path *pathnode = makeNode(Path);
pathnode->pathtype = T_ValuesScan;
pathnode->parent = rel;
pathnode->param_info = get_baserel_parampathinfo(root, rel,
required_outer);
pathnode->pathkeys = NIL; /* result is always unordered */
cost_valuesscan(pathnode, root, rel, pathnode->param_info);
return pathnode;
}
/*
* create_ctescan_path
* Creates a path corresponding to a scan of a non-self-reference CTE,
* returning the pathnode.
*/
Path *
create_ctescan_path(PlannerInfo *root, RelOptInfo *rel, Relids required_outer)
{
Path *pathnode = makeNode(Path);
pathnode->pathtype = T_CteScan;
pathnode->parent = rel;
pathnode->param_info = get_baserel_parampathinfo(root, rel,
required_outer);
pathnode->pathkeys = NIL; /* XXX for now, result is always unordered */
cost_ctescan(pathnode, root, rel, pathnode->param_info);
return pathnode;
}
/*
* create_worktablescan_path
* Creates a path corresponding to a scan of a self-reference CTE,
* returning the pathnode.
*/
Path *
create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel,
Relids required_outer)
{
Path *pathnode = makeNode(Path);
pathnode->pathtype = T_WorkTableScan;
pathnode->parent = rel;
pathnode->param_info = get_baserel_parampathinfo(root, rel,
required_outer);
pathnode->pathkeys = NIL; /* result is always unordered */
/* Cost is the same as for a regular CTE scan */
cost_ctescan(pathnode, root, rel, pathnode->param_info);
return pathnode;
}
#ifdef PGXC
#ifndef XCP
/*
* create_remotequery_path
* Creates a path corresponding to a scan of a remote query,
* returning the pathnode.
*/
Path *
create_remotequery_path(PlannerInfo *root, RelOptInfo *rel)
{
Path *pathnode = makeNode(Path);
pathnode->pathtype = T_RemoteQuery;
pathnode->parent = rel;
pathnode->param_info = NULL; /* never parameterized at present */
pathnode->pathkeys = NIL; /* result is always unordered */
/* PGXCTODO - set cost properly */
cost_seqscan(pathnode, root, rel, pathnode->param_info);
return pathnode;
}
#endif /* XCP */
#endif /* PGXC */
/*
* create_foreignscan_path
* Creates a path corresponding to a scan of a foreign table,
* returning the pathnode.
*
* This function is never called from core Postgres; rather, it's expected
* to be called by the GetForeignPaths function of a foreign data wrapper.
* We make the FDW supply all fields of the path, since we do not have any
* way to calculate them in core.
*/
ForeignPath *
create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel,
double rows, Cost startup_cost, Cost total_cost,
List *pathkeys,
Relids required_outer,
List *fdw_private)
{
ForeignPath *pathnode = makeNode(ForeignPath);
pathnode->path.pathtype = T_ForeignScan;
pathnode->path.parent = rel;
pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
required_outer);
pathnode->path.rows = rows;
pathnode->path.startup_cost = startup_cost;
pathnode->path.total_cost = total_cost;
pathnode->path.pathkeys = pathkeys;
pathnode->fdw_private = fdw_private;
return pathnode;
}
/*
* calc_nestloop_required_outer
* Compute the required_outer set for a nestloop join path
*
* Note: result must not share storage with either input
*/
Relids
calc_nestloop_required_outer(Path *outer_path, Path *inner_path)
{
Relids outer_paramrels = PATH_REQ_OUTER(outer_path);
Relids inner_paramrels = PATH_REQ_OUTER(inner_path);
Relids required_outer;
/* inner_path can require rels from outer path, but not vice versa */
Assert(!bms_overlap(outer_paramrels, inner_path->parent->relids));
/* easy case if inner path is not parameterized */
if (!inner_paramrels)
return bms_copy(outer_paramrels);
/* else, form the union ... */
required_outer = bms_union(outer_paramrels, inner_paramrels);
/* ... and remove any mention of now-satisfied outer rels */
required_outer = bms_del_members(required_outer,
outer_path->parent->relids);
/* maintain invariant that required_outer is exactly NULL if empty */
if (bms_is_empty(required_outer))
{
bms_free(required_outer);
required_outer = NULL;
}
return required_outer;
}
/*
* calc_non_nestloop_required_outer
* Compute the required_outer set for a merge or hash join path
*
* Note: result must not share storage with either input
*/
Relids
calc_non_nestloop_required_outer(Path *outer_path, Path *inner_path)
{
Relids outer_paramrels = PATH_REQ_OUTER(outer_path);
Relids inner_paramrels = PATH_REQ_OUTER(inner_path);
Relids required_outer;
/* neither path can require rels from the other */
Assert(!bms_overlap(outer_paramrels, inner_path->parent->relids));
Assert(!bms_overlap(inner_paramrels, outer_path->parent->relids));
/* form the union ... */
required_outer = bms_union(outer_paramrels, inner_paramrels);
/* we do not need an explicit test for empty; bms_union gets it right */
return required_outer;
}
/*
* create_nestloop_path
* Creates a pathnode corresponding to a nestloop join between two
* relations.
*
* 'joinrel' is the join relation.
* 'jointype' is the type of join required
* 'workspace' is the result from initial_cost_nestloop
* 'sjinfo' is extra info about the join for selectivity estimation
* 'semifactors' contains valid data if jointype is SEMI or ANTI
* 'outer_path' is the outer path
* 'inner_path' is the inner path
* 'restrict_clauses' are the RestrictInfo nodes to apply at the join
* 'pathkeys' are the path keys of the new join path
* 'required_outer' is the set of required outer rels
*
* Returns the resulting path node.
*/
NestPath *
create_nestloop_path(PlannerInfo *root,
RelOptInfo *joinrel,
JoinType jointype,
JoinCostWorkspace *workspace,
SpecialJoinInfo *sjinfo,
SemiAntiJoinFactors *semifactors,
Path *outer_path,
Path *inner_path,
List *restrict_clauses,
List *pathkeys,
Relids required_outer)
{
NestPath *pathnode = makeNode(NestPath);
#ifdef XCP
List *alternate;
ListCell *lc;
#endif
Relids inner_req_outer = PATH_REQ_OUTER(inner_path);
/*
* If the inner path is parameterized by the outer, we must drop any
* restrict_clauses that are due to be moved into the inner path. We have
* to do this now, rather than postpone the work till createplan time,
* because the restrict_clauses list can affect the size and cost
* estimates for this path.
*/
if (bms_overlap(inner_req_outer, outer_path->parent->relids))
{
Relids inner_and_outer = bms_union(inner_path->parent->relids,
inner_req_outer);
List *jclauses = NIL;
ListCell *lc;
foreach(lc, restrict_clauses)
{
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
if (!join_clause_is_movable_into(rinfo,
inner_path->parent->relids,
inner_and_outer))
jclauses = lappend(jclauses, rinfo);
}
restrict_clauses = jclauses;
}
pathnode->path.pathtype = T_NestLoop;
pathnode->path.parent = joinrel;
pathnode->path.param_info =
get_joinrel_parampathinfo(root,
joinrel,
outer_path,
inner_path,
sjinfo,
required_outer,
&restrict_clauses);
pathnode->path.pathkeys = pathkeys;
pathnode->jointype = jointype;
pathnode->outerjoinpath = outer_path;
pathnode->innerjoinpath = inner_path;
pathnode->joinrestrictinfo = restrict_clauses;
#ifdef XCP
alternate = set_joinpath_distribution(root, pathnode);
#endif
final_cost_nestloop(root, pathnode, workspace, sjinfo, semifactors);
#ifdef XCP
/*
* Also calculate costs of all alternates and return cheapest path
*/
foreach(lc, alternate)
{
NestPath *altpath = (NestPath *) lfirst(lc);
final_cost_nestloop(root, altpath, workspace, sjinfo, semifactors);
if (altpath->path.total_cost < pathnode->path.total_cost)
pathnode = altpath;
}
#endif
return pathnode;
}
/*
* create_mergejoin_path
* Creates a pathnode corresponding to a mergejoin join between
* two relations
*
* 'joinrel' is the join relation
* 'jointype' is the type of join required
* 'workspace' is the result from initial_cost_mergejoin
* 'sjinfo' is extra info about the join for selectivity estimation
* 'outer_path' is the outer path
* 'inner_path' is the inner path
* 'restrict_clauses' are the RestrictInfo nodes to apply at the join
* 'pathkeys' are the path keys of the new join path
* 'required_outer' is the set of required outer rels
* 'mergeclauses' are the RestrictInfo nodes to use as merge clauses
* (this should be a subset of the restrict_clauses list)
* 'outersortkeys' are the sort varkeys for the outer relation
* 'innersortkeys' are the sort varkeys for the inner relation
*/
MergePath *
create_mergejoin_path(PlannerInfo *root,
RelOptInfo *joinrel,
JoinType jointype,
JoinCostWorkspace *workspace,
SpecialJoinInfo *sjinfo,
Path *outer_path,
Path *inner_path,
List *restrict_clauses,
List *pathkeys,
Relids required_outer,
List *mergeclauses,
List *outersortkeys,
List *innersortkeys)
{
MergePath *pathnode = makeNode(MergePath);
#ifdef XCP
List *alternate;
ListCell *lc;
#endif
pathnode->jpath.path.pathtype = T_MergeJoin;
pathnode->jpath.path.parent = joinrel;
pathnode->jpath.path.param_info =
get_joinrel_parampathinfo(root,
joinrel,
outer_path,
inner_path,
sjinfo,
required_outer,
&restrict_clauses);
pathnode->jpath.path.pathkeys = pathkeys;
pathnode->jpath.jointype = jointype;
pathnode->jpath.outerjoinpath = outer_path;
pathnode->jpath.innerjoinpath = inner_path;
pathnode->jpath.joinrestrictinfo = restrict_clauses;
pathnode->path_mergeclauses = mergeclauses;
pathnode->outersortkeys = outersortkeys;
pathnode->innersortkeys = innersortkeys;
#ifdef XCP
alternate = set_joinpath_distribution(root, (JoinPath *) pathnode);
#endif
/* pathnode->materialize_inner will be set by final_cost_mergejoin */
final_cost_mergejoin(root, pathnode, workspace, sjinfo);
#ifdef XCP
/*
* Also calculate costs of all alternates and return cheapest path
*/
foreach(lc, alternate)
{
MergePath *altpath = (MergePath *) lfirst(lc);
final_cost_mergejoin(root, altpath, workspace, sjinfo);
if (altpath->jpath.path.total_cost < pathnode->jpath.path.total_cost)
pathnode = altpath;
}
#endif
return pathnode;
}
/*
* create_hashjoin_path
* Creates a pathnode corresponding to a hash join between two relations.
*
* 'joinrel' is the join relation
* 'jointype' is the type of join required
* 'workspace' is the result from initial_cost_hashjoin
* 'sjinfo' is extra info about the join for selectivity estimation
* 'semifactors' contains valid data if jointype is SEMI or ANTI
* 'outer_path' is the cheapest outer path
* 'inner_path' is the cheapest inner path
* 'restrict_clauses' are the RestrictInfo nodes to apply at the join
* 'required_outer' is the set of required outer rels
* 'hashclauses' are the RestrictInfo nodes to use as hash clauses
* (this should be a subset of the restrict_clauses list)
*/
HashPath *
create_hashjoin_path(PlannerInfo *root,
RelOptInfo *joinrel,
JoinType jointype,
JoinCostWorkspace *workspace,
SpecialJoinInfo *sjinfo,
SemiAntiJoinFactors *semifactors,
Path *outer_path,
Path *inner_path,
List *restrict_clauses,
Relids required_outer,
List *hashclauses)
{
HashPath *pathnode = makeNode(HashPath);
#ifdef XCP
List *alternate;
ListCell *lc;
#endif
pathnode->jpath.path.pathtype = T_HashJoin;
pathnode->jpath.path.parent = joinrel;
pathnode->jpath.path.param_info =
get_joinrel_parampathinfo(root,
joinrel,
outer_path,
inner_path,
sjinfo,
required_outer,
&restrict_clauses);
/*
* A hashjoin never has pathkeys, since its output ordering is
* unpredictable due to possible batching. XXX If the inner relation is
* small enough, we could instruct the executor that it must not batch,
* and then we could assume that the output inherits the outer relation's
* ordering, which might save a sort step. However there is considerable
* downside if our estimate of the inner relation size is badly off. For
* the moment we don't risk it. (Note also that if we wanted to take this
* seriously, joinpath.c would have to consider many more paths for the
* outer rel than it does now.)
*/
pathnode->jpath.path.pathkeys = NIL;
pathnode->jpath.jointype = jointype;
pathnode->jpath.outerjoinpath = outer_path;
pathnode->jpath.innerjoinpath = inner_path;
pathnode->jpath.joinrestrictinfo = restrict_clauses;
pathnode->path_hashclauses = hashclauses;
#ifdef XCP
alternate = set_joinpath_distribution(root, (JoinPath *) pathnode);
#endif
/* final_cost_hashjoin will fill in pathnode->num_batches */
final_cost_hashjoin(root, pathnode, workspace, sjinfo, semifactors);
#ifdef XCP
/*
* Calculate costs of all alternates and return cheapest path
*/
foreach(lc, alternate)
{
HashPath *altpath = (HashPath *) lfirst(lc);
final_cost_hashjoin(root, altpath, workspace, sjinfo, semifactors);
if (altpath->jpath.path.total_cost < pathnode->jpath.path.total_cost)
pathnode = altpath;
}
#endif
return pathnode;
}
/*
* reparameterize_path
* Attempt to modify a Path to have greater parameterization
*
* We use this to attempt to bring all child paths of an appendrel to the
* same parameterization level, ensuring that they all enforce the same set
* of join quals (and thus that that parameterization can be attributed to
* an append path built from such paths). Currently, only a few path types
* are supported here, though more could be added at need. We return NULL
* if we can't reparameterize the given path.
*
* Note: we intentionally do not pass created paths to add_path(); it would
* possibly try to delete them on the grounds of being cost-inferior to the
* paths they were made from, and we don't want that. Paths made here are
* not necessarily of general-purpose usefulness, but they can be useful
* as members of an append path.
*/
Path *
reparameterize_path(PlannerInfo *root, Path *path,
Relids required_outer,
double loop_count)
{
RelOptInfo *rel = path->parent;
/* Can only increase, not decrease, path's parameterization */
if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
return NULL;
switch (path->pathtype)
{
case T_SeqScan:
return create_seqscan_path(root, rel, required_outer);
case T_IndexScan:
case T_IndexOnlyScan:
{
IndexPath *ipath = (IndexPath *) path;
IndexPath *newpath = makeNode(IndexPath);
/*
* We can't use create_index_path directly, and would not want
* to because it would re-compute the indexqual conditions
* which is wasted effort. Instead we hack things a bit:
* flat-copy the path node, revise its param_info, and redo
* the cost estimate.
*/
memcpy(newpath, ipath, sizeof(IndexPath));
newpath->path.param_info =
get_baserel_parampathinfo(root, rel, required_outer);
cost_index(newpath, root, loop_count);
return (Path *) newpath;
}
case T_BitmapHeapScan:
{
BitmapHeapPath *bpath = (BitmapHeapPath *) path;
return (Path *) create_bitmap_heap_path(root,
rel,
bpath->bitmapqual,
required_outer,
loop_count);
}
case T_SubqueryScan:
#ifdef XCP
return create_subqueryscan_path(root, rel, path->pathkeys,
required_outer, path->distribution);
#else
return create_subqueryscan_path(root, rel, path->pathkeys,
required_outer);
#endif
default:
break;
}
return NULL;
}
|