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
|
/*-------------------------------------------------------------------------
*
* analyzejoins.c
* Routines for simplifying joins after initial query analysis
*
* While we do a great deal of join simplification in prep/prepjointree.c,
* certain optimizations cannot be performed at that stage for lack of
* detailed information about the query. The routines here are invoked
* after initsplan.c has done its work, and can do additional join removal
* and simplification steps based on the information extracted. The penalty
* is that we have to work harder to clean up after ourselves when we modify
* the query, since the derived data structures have to be updated too.
*
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/optimizer/plan/analyzejoins.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "catalog/pg_class.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/joininfo.h"
#include "optimizer/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/restrictinfo.h"
#include "utils/lsyscache.h"
/*
* The struct containing self-join candidate. Used to find duplicate reloids.
*/
typedef struct
{
int relid;
Oid reloid;
} SelfJoinCandidate;
bool enable_self_join_removal;
/* local functions */
static bool join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo);
static void remove_leftjoinrel_from_query(PlannerInfo *root, int relid,
SpecialJoinInfo *sjinfo);
static void remove_rel_from_restrictinfo(RestrictInfo *rinfo,
int relid, int ojrelid);
static void remove_rel_from_eclass(EquivalenceClass *ec,
int relid, int ojrelid);
static List *remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved);
static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel);
static bool rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel,
List *clause_list, List **extra_clauses);
static Oid distinct_col_search(int colno, List *colnos, List *opids);
static bool is_innerrel_unique_for(PlannerInfo *root,
Relids joinrelids,
Relids outerrelids,
RelOptInfo *innerrel,
JoinType jointype,
List *restrictlist,
List **extra_clauses);
static void replace_varno(Node *node, int from, int to);
static Bitmapset *replace_relid(Relids relids, int oldId, int newId);
static int self_join_candidates_cmp(const void *a, const void *b);
/*
* remove_useless_joins
* Check for relations that don't actually need to be joined at all,
* and remove them from the query.
*
* We are passed the current joinlist and return the updated list. Other
* data structures that have to be updated are accessible via "root".
*/
List *
remove_useless_joins(PlannerInfo *root, List *joinlist)
{
ListCell *lc;
/*
* We are only interested in relations that are left-joined to, so we can
* scan the join_info_list to find them easily.
*/
restart:
foreach(lc, root->join_info_list)
{
SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
int innerrelid;
int nremoved;
/* Skip if not removable */
if (!join_is_removable(root, sjinfo))
continue;
/*
* Currently, join_is_removable can only succeed when the sjinfo's
* righthand is a single baserel. Remove that rel from the query and
* joinlist.
*/
innerrelid = bms_singleton_member(sjinfo->min_righthand);
remove_leftjoinrel_from_query(root, innerrelid, sjinfo);
/* We verify that exactly one reference gets removed from joinlist */
nremoved = 0;
joinlist = remove_rel_from_joinlist(joinlist, innerrelid, &nremoved);
if (nremoved != 1)
elog(ERROR, "failed to find relation %d in joinlist", innerrelid);
/*
* We can delete this SpecialJoinInfo from the list too, since it's no
* longer of interest. (Since we'll restart the foreach loop
* immediately, we don't bother with foreach_delete_current.)
*/
root->join_info_list = list_delete_cell(root->join_info_list, lc);
/*
* Restart the scan. This is necessary to ensure we find all
* removable joins independently of ordering of the join_info_list
* (note that removal of attr_needed bits may make a join appear
* removable that did not before).
*/
goto restart;
}
return joinlist;
}
/*
* clause_sides_match_join
* Determine whether a join clause is of the right form to use in this join.
*
* We already know that the clause is a binary opclause referencing only the
* rels in the current join. The point here is to check whether it has the
* form "outerrel_expr op innerrel_expr" or "innerrel_expr op outerrel_expr",
* rather than mixing outer and inner vars on either side. If it matches,
* we set the transient flag outer_is_left to identify which side is which.
*/
static inline bool
clause_sides_match_join(RestrictInfo *rinfo, Relids outerrelids,
Relids innerrelids)
{
if (bms_is_subset(rinfo->left_relids, outerrelids) &&
bms_is_subset(rinfo->right_relids, innerrelids))
{
/* lefthand side is outer */
rinfo->outer_is_left = true;
return true;
}
else if (bms_is_subset(rinfo->left_relids, innerrelids) &&
bms_is_subset(rinfo->right_relids, outerrelids))
{
/* righthand side is outer */
rinfo->outer_is_left = false;
return true;
}
return false; /* no good for these input relations */
}
/*
* join_is_removable
* Check whether we need not perform this special join at all, because
* it will just duplicate its left input.
*
* This is true for a left join for which the join condition cannot match
* more than one inner-side row. (There are other possibly interesting
* cases, but we don't have the infrastructure to prove them.) We also
* have to check that the inner side doesn't generate any variables needed
* above the join.
*/
static bool
join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
{
int innerrelid;
RelOptInfo *innerrel;
Relids inputrelids;
Relids joinrelids;
List *clause_list = NIL;
ListCell *l;
int attroff;
/*
* Must be a left join to a single baserel, else we aren't going to be
* able to do anything with it.
*/
if (sjinfo->jointype != JOIN_LEFT)
return false;
if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid))
return false;
/*
* Never try to eliminate a left join to the query result rel. Although
* the case is syntactically impossible in standard SQL, MERGE will build
* a join tree that looks exactly like that.
*/
if (innerrelid == root->parse->resultRelation)
return false;
innerrel = find_base_rel(root, innerrelid);
/*
* Before we go to the effort of checking whether any innerrel variables
* are needed above the join, make a quick check to eliminate cases in
* which we will surely be unable to prove uniqueness of the innerrel.
*/
if (!rel_supports_distinctness(root, innerrel))
return false;
/* Compute the relid set for the join we are considering */
inputrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
Assert(sjinfo->ojrelid != 0);
joinrelids = bms_copy(inputrelids);
joinrelids = bms_add_member(joinrelids, sjinfo->ojrelid);
/*
* We can't remove the join if any inner-rel attributes are used above the
* join. Here, "above" the join includes pushed-down conditions, so we
* should reject if attr_needed includes the OJ's own relid; therefore,
* compare to inputrelids not joinrelids.
*
* As a micro-optimization, it seems better to start with max_attr and
* count down rather than starting with min_attr and counting up, on the
* theory that the system attributes are somewhat less likely to be wanted
* and should be tested last.
*/
for (attroff = innerrel->max_attr - innerrel->min_attr;
attroff >= 0;
attroff--)
{
if (!bms_is_subset(innerrel->attr_needed[attroff], inputrelids))
return false;
}
/*
* Similarly check that the inner rel isn't needed by any PlaceHolderVars
* that will be used above the join. The PHV case is a little bit more
* complicated, because PHVs may have been assigned a ph_eval_at location
* that includes the innerrel, yet their contained expression might not
* actually reference the innerrel (it could be just a constant, for
* instance). If such a PHV is due to be evaluated above the join then it
* needn't prevent join removal.
*/
foreach(l, root->placeholder_list)
{
PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
if (bms_overlap(phinfo->ph_lateral, innerrel->relids))
return false; /* it references innerrel laterally */
if (!bms_overlap(phinfo->ph_eval_at, innerrel->relids))
continue; /* it definitely doesn't reference innerrel */
if (bms_is_subset(phinfo->ph_needed, inputrelids))
continue; /* PHV is not used above the join */
if (!bms_is_member(sjinfo->ojrelid, phinfo->ph_eval_at))
return false; /* it has to be evaluated below the join */
/*
* We need to be sure there will still be a place to evaluate the PHV
* if we remove the join, ie that ph_eval_at wouldn't become empty.
*/
if (!bms_overlap(sjinfo->min_lefthand, phinfo->ph_eval_at))
return false; /* there isn't any other place to eval PHV */
/* Check contained expression last, since this is a bit expensive */
if (bms_overlap(pull_varnos(root, (Node *) phinfo->ph_var->phexpr),
innerrel->relids))
return false; /* contained expression references innerrel */
}
/*
* Search for mergejoinable clauses that constrain the inner rel against
* either the outer rel or a pseudoconstant. If an operator is
* mergejoinable then it behaves like equality for some btree opclass, so
* it's what we want. The mergejoinability test also eliminates clauses
* containing volatile functions, which we couldn't depend on.
*/
foreach(l, innerrel->joininfo)
{
RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
/*
* If the current join commutes with some other outer join(s) via
* outer join identity 3, there will be multiple clones of its join
* clauses in the joininfo list. We want to consider only the
* has_clone form of such clauses. Processing more than one form
* would be wasteful, and also some of the others would confuse the
* RINFO_IS_PUSHED_DOWN test below.
*/
if (restrictinfo->is_clone)
continue; /* ignore it */
/*
* If it's not a join clause for this outer join, we can't use it.
* Note that if the clause is pushed-down, then it is logically from
* above the outer join, even if it references no other rels (it might
* be from WHERE, for example).
*/
if (RINFO_IS_PUSHED_DOWN(restrictinfo, joinrelids))
continue; /* ignore; not useful here */
/* Ignore if it's not a mergejoinable clause */
if (!restrictinfo->can_join ||
restrictinfo->mergeopfamilies == NIL)
continue; /* not mergejoinable */
/*
* Check if clause has the form "outer op inner" or "inner op outer",
* and if so mark which side is inner.
*/
if (!clause_sides_match_join(restrictinfo, sjinfo->min_lefthand,
innerrel->relids))
continue; /* no good for these input relations */
/* OK, add to list */
clause_list = lappend(clause_list, restrictinfo);
}
/*
* Now that we have the relevant equality join clauses, try to prove the
* innerrel distinct.
*/
if (rel_is_distinct_for(root, innerrel, clause_list, NULL))
return true;
/*
* Some day it would be nice to check for other methods of establishing
* distinctness.
*/
return false;
}
/*
* Remove the target rel->relid and references to the target join from the
* planner's data structures, having determined that there is no need
* to include them in the query. Optionally replace them with subst if subst
* is non-negative.
*
* This function updates only parts needed for both left-join removal and
* self-join removal.
*/
static void
remove_rel_from_query(PlannerInfo *root, RelOptInfo *rel,
int subst, SpecialJoinInfo *sjinfo,
Relids joinrelids)
{
int relid = rel->relid;
int ojrelid = (sjinfo != NULL) ? sjinfo->ojrelid : -1;
Index rti;
ListCell *l;
/*
* Remove references to the rel from other baserels' attr_needed arrays
* and lateral_vars lists.
*/
for (rti = 1; rti < root->simple_rel_array_size; rti++)
{
RelOptInfo *otherrel = root->simple_rel_array[rti];
int attroff;
/* there may be empty slots corresponding to non-baserel RTEs */
if (otherrel == NULL)
continue;
Assert(otherrel->relid == rti); /* sanity check on array */
/* no point in processing target rel itself */
if (otherrel == rel)
continue;
for (attroff = otherrel->max_attr - otherrel->min_attr;
attroff >= 0;
attroff--)
{
otherrel->attr_needed[attroff] =
replace_relid(otherrel->attr_needed[attroff], relid, subst);
otherrel->attr_needed[attroff] =
replace_relid(otherrel->attr_needed[attroff], ojrelid, subst);
}
/* Update lateral_vars list. */
replace_varno((Node *) otherrel->lateral_vars, relid, subst);
}
/*
* Update all_baserels and related relid sets.
*/
root->all_baserels = replace_relid(root->all_baserels, relid, subst);
root->outer_join_rels = replace_relid(root->outer_join_rels, ojrelid, subst);
root->all_query_rels = replace_relid(root->all_query_rels, relid, subst);
root->all_query_rels = replace_relid(root->all_query_rels, ojrelid, subst);
/*
* Likewise remove references from SpecialJoinInfo data structures.
*
* This is relevant in case the outer join we're deleting is nested inside
* other outer joins: the upper joins' relid sets have to be adjusted. The
* RHS of the target outer join will be made empty here, but that's OK
* since caller will delete that SpecialJoinInfo entirely.
*/
foreach(l, root->join_info_list)
{
SpecialJoinInfo *sjinf = (SpecialJoinInfo *) lfirst(l);
sjinf->min_lefthand = replace_relid(sjinf->min_lefthand, relid, subst);
sjinf->min_righthand = replace_relid(sjinf->min_righthand, relid, subst);
sjinf->syn_lefthand = replace_relid(sjinf->syn_lefthand, relid, subst);
sjinf->syn_righthand = replace_relid(sjinf->syn_righthand, relid, subst);
sjinf->min_lefthand = replace_relid(sjinf->min_lefthand, ojrelid, subst);
sjinf->min_righthand = replace_relid(sjinf->min_righthand, ojrelid, subst);
sjinf->syn_lefthand = replace_relid(sjinf->syn_lefthand, ojrelid, subst);
sjinf->syn_righthand = replace_relid(sjinf->syn_righthand, ojrelid, subst);
/* relid cannot appear in these fields, but ojrelid can: */
sjinf->commute_above_l = replace_relid(sjinf->commute_above_l, ojrelid, subst);
sjinf->commute_above_r = replace_relid(sjinf->commute_above_r, ojrelid, subst);
sjinf->commute_below_l = replace_relid(sjinf->commute_below_l, ojrelid, subst);
sjinf->commute_below_r = replace_relid(sjinf->commute_below_r, ojrelid, subst);
replace_varno((Node *) sjinf->semi_rhs_exprs, relid, subst);
}
/*
* Likewise remove references from PlaceHolderVar data structures,
* removing any no-longer-needed placeholders entirely.
*
* Removal is a bit trickier than it might seem: we can remove PHVs that
* are used at the target rel and/or in the join qual, but not those that
* are used at join partner rels or above the join. It's not that easy to
* distinguish PHVs used at partner rels from those used in the join qual,
* since they will both have ph_needed sets that are subsets of
* joinrelids. However, a PHV used at a partner rel could not have the
* target rel in ph_eval_at, so we check that while deciding whether to
* remove or just update the PHV. There is no corresponding test in
* join_is_removable because it doesn't need to distinguish those cases.
*/
foreach(l, root->placeholder_list)
{
PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
Assert(sjinfo == NULL || !bms_is_member(relid, phinfo->ph_lateral));
if (bms_is_subset(phinfo->ph_needed, joinrelids) &&
bms_is_member(relid, phinfo->ph_eval_at) &&
(sjinfo == NULL || !bms_is_member(ojrelid, phinfo->ph_eval_at)))
{
root->placeholder_list = foreach_delete_current(root->placeholder_list,
l);
root->placeholder_array[phinfo->phid] = NULL;
}
else
{
PlaceHolderVar *phv = phinfo->ph_var;
phinfo->ph_eval_at = replace_relid(phinfo->ph_eval_at, relid, subst);
phinfo->ph_eval_at = replace_relid(phinfo->ph_eval_at, ojrelid, subst);
Assert(!bms_is_empty(phinfo->ph_eval_at)); /* checked previously */
phinfo->ph_needed = replace_relid(phinfo->ph_needed, relid, subst);
phinfo->ph_needed = replace_relid(phinfo->ph_needed, ojrelid, subst);
/* ph_needed might or might not become empty */
phinfo->ph_lateral = replace_relid(phinfo->ph_lateral, relid, subst);
/* ph_lateral might or might not be empty */
phv->phrels = replace_relid(phv->phrels, relid, subst);
phv->phrels = replace_relid(phv->phrels, ojrelid, subst);
Assert(!bms_is_empty(phv->phrels));
replace_varno((Node *) phv->phexpr, relid, subst);
Assert(phv->phnullingrels == NULL); /* no need to adjust */
}
}
}
/*
* Remove the target relid and references to the target join from the
* planner's data structures, having determined that there is no need
* to include them in the query.
*
* We are not terribly thorough here. We only bother to update parts of
* the planner's data structures that will actually be consulted later.
*/
static void
remove_leftjoinrel_from_query(PlannerInfo *root, int relid,
SpecialJoinInfo *sjinfo)
{
List *joininfos;
int ojrelid = sjinfo->ojrelid;
RelOptInfo *rel = find_base_rel(root, relid);
Relids join_plus_commute;
Relids joinrelids;
ListCell *l;
/* Compute the relid set for the join we are considering */
joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
Assert(ojrelid != 0);
joinrelids = bms_add_member(joinrelids, ojrelid);
remove_rel_from_query(root, rel, -1, sjinfo, joinrelids);
/*
* Remove any joinquals referencing the rel from the joininfo lists.
*
* In some cases, a joinqual has to be put back after deleting its
* reference to the target rel. This can occur for pseudoconstant and
* outerjoin-delayed quals, which can get marked as requiring the rel in
* order to force them to be evaluated at or above the join. We can't
* just discard them, though. Only quals that logically belonged to the
* outer join being discarded should be removed from the query.
*
* We might encounter a qual that is a clone of a deletable qual with some
* outer-join relids added (see deconstruct_distribute_oj_quals). To
* ensure we get rid of such clones as well, add the relids of all OJs
* commutable with this one to the set we test against for
* pushed-down-ness.
*/
join_plus_commute = bms_union(joinrelids,
sjinfo->commute_above_r);
join_plus_commute = bms_add_members(join_plus_commute,
sjinfo->commute_below_l);
/*
* We must make a copy of the rel's old joininfo list before starting the
* loop, because otherwise remove_join_clause_from_rels would destroy the
* list while we're scanning it.
*/
joininfos = list_copy(rel->joininfo);
foreach(l, joininfos)
{
RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
remove_join_clause_from_rels(root, rinfo, rinfo->required_relids);
if (RINFO_IS_PUSHED_DOWN(rinfo, join_plus_commute))
{
/*
* There might be references to relid or ojrelid in the
* RestrictInfo's relid sets, as a consequence of PHVs having had
* ph_eval_at sets that include those. We already checked above
* that any such PHV is safe (and updated its ph_eval_at), so we
* can just drop those references.
*/
remove_rel_from_restrictinfo(rinfo, relid, ojrelid);
/*
* Cross-check that the clause itself does not reference the
* target rel or join.
*/
#ifdef USE_ASSERT_CHECKING
{
Relids clause_varnos = pull_varnos(root,
(Node *) rinfo->clause);
Assert(!bms_is_member(relid, clause_varnos));
Assert(!bms_is_member(ojrelid, clause_varnos));
}
#endif
/* Now throw it back into the joininfo lists */
distribute_restrictinfo_to_rels(root, rinfo);
}
}
/*
* Likewise remove references from EquivalenceClasses.
*/
foreach(l, root->eq_classes)
{
EquivalenceClass *ec = (EquivalenceClass *) lfirst(l);
if (bms_is_member(relid, ec->ec_relids) ||
bms_is_member(ojrelid, ec->ec_relids))
remove_rel_from_eclass(ec, relid, ojrelid);
}
/*
* There may be references to the rel in root->fkey_list, but if so,
* match_foreign_keys_to_quals() will get rid of them.
*/
/*
* Finally, remove the rel from the baserel array to prevent it from being
* referenced again. (We can't do this earlier because
* remove_join_clause_from_rels will touch it.)
*/
root->simple_rel_array[relid] = NULL;
/* And nuke the RelOptInfo, just in case there's another access path */
pfree(rel);
}
/*
* Remove any references to relid or ojrelid from the RestrictInfo.
*
* We only bother to clean out bits in clause_relids and required_relids,
* not nullingrel bits in contained Vars and PHVs. (This might have to be
* improved sometime.) However, if the RestrictInfo contains an OR clause
* we have to also clean up the sub-clauses.
*/
static void
remove_rel_from_restrictinfo(RestrictInfo *rinfo, int relid, int ojrelid)
{
/*
* The clause_relids probably aren't shared with anything else, but let's
* copy them just to be sure.
*/
rinfo->clause_relids = bms_copy(rinfo->clause_relids);
rinfo->clause_relids = bms_del_member(rinfo->clause_relids, relid);
rinfo->clause_relids = bms_del_member(rinfo->clause_relids, ojrelid);
/* Likewise for required_relids */
rinfo->required_relids = bms_copy(rinfo->required_relids);
rinfo->required_relids = bms_del_member(rinfo->required_relids, relid);
rinfo->required_relids = bms_del_member(rinfo->required_relids, ojrelid);
/* If it's an OR, recurse to clean up sub-clauses */
if (restriction_is_or_clause(rinfo))
{
ListCell *lc;
Assert(is_orclause(rinfo->orclause));
foreach(lc, ((BoolExpr *) rinfo->orclause)->args)
{
Node *orarg = (Node *) lfirst(lc);
/* OR arguments should be ANDs or sub-RestrictInfos */
if (is_andclause(orarg))
{
List *andargs = ((BoolExpr *) orarg)->args;
ListCell *lc2;
foreach(lc2, andargs)
{
RestrictInfo *rinfo2 = lfirst_node(RestrictInfo, lc2);
remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
}
}
else
{
RestrictInfo *rinfo2 = castNode(RestrictInfo, orarg);
remove_rel_from_restrictinfo(rinfo2, relid, ojrelid);
}
}
}
}
/*
* Remove any references to relid or ojrelid from the EquivalenceClass.
*
* Like remove_rel_from_restrictinfo, we don't worry about cleaning out
* any nullingrel bits in contained Vars and PHVs. (This might have to be
* improved sometime.) We do need to fix the EC and EM relid sets to ensure
* that implied join equalities will be generated at the appropriate join
* level(s).
*/
static void
remove_rel_from_eclass(EquivalenceClass *ec, int relid, int ojrelid)
{
ListCell *lc;
/* Fix up the EC's overall relids */
ec->ec_relids = bms_del_member(ec->ec_relids, relid);
ec->ec_relids = bms_del_member(ec->ec_relids, ojrelid);
/*
* Fix up the member expressions. Any non-const member that ends with
* empty em_relids must be a Var or PHV of the removed relation. We don't
* need it anymore, so we can drop it.
*/
foreach(lc, ec->ec_members)
{
EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
if (bms_is_member(relid, cur_em->em_relids) ||
bms_is_member(ojrelid, cur_em->em_relids))
{
Assert(!cur_em->em_is_const);
cur_em->em_relids = bms_del_member(cur_em->em_relids, relid);
cur_em->em_relids = bms_del_member(cur_em->em_relids, ojrelid);
if (bms_is_empty(cur_em->em_relids))
ec->ec_members = foreach_delete_current(ec->ec_members, lc);
}
}
/* Fix up the source clauses, in case we can re-use them later */
foreach(lc, ec->ec_sources)
{
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
remove_rel_from_restrictinfo(rinfo, relid, ojrelid);
}
/*
* Rather than expend code on fixing up any already-derived clauses, just
* drop them. (At this point, any such clauses would be base restriction
* clauses, which we'd not need anymore anyway.)
*/
ec->ec_derives = NIL;
}
/*
* Remove any occurrences of the target relid from a joinlist structure.
*
* It's easiest to build a whole new list structure, so we handle it that
* way. Efficiency is not a big deal here.
*
* *nremoved is incremented by the number of occurrences removed (there
* should be exactly one, but the caller checks that).
*/
static List *
remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved)
{
List *result = NIL;
ListCell *jl;
foreach(jl, joinlist)
{
Node *jlnode = (Node *) lfirst(jl);
if (IsA(jlnode, RangeTblRef))
{
int varno = ((RangeTblRef *) jlnode)->rtindex;
if (varno == relid)
(*nremoved)++;
else
result = lappend(result, jlnode);
}
else if (IsA(jlnode, List))
{
/* Recurse to handle subproblem */
List *sublist;
sublist = remove_rel_from_joinlist((List *) jlnode,
relid, nremoved);
/* Avoid including empty sub-lists in the result */
if (sublist)
result = lappend(result, sublist);
}
else
{
elog(ERROR, "unrecognized joinlist node type: %d",
(int) nodeTag(jlnode));
}
}
return result;
}
/*
* reduce_unique_semijoins
* Check for semijoins that can be simplified to plain inner joins
* because the inner relation is provably unique for the join clauses.
*
* Ideally this would happen during reduce_outer_joins, but we don't have
* enough information at that point.
*
* To perform the strength reduction when applicable, we need only delete
* the semijoin's SpecialJoinInfo from root->join_info_list. (We don't
* bother fixing the join type attributed to it in the query jointree,
* since that won't be consulted again.)
*/
void
reduce_unique_semijoins(PlannerInfo *root)
{
ListCell *lc;
/*
* Scan the join_info_list to find semijoins.
*/
foreach(lc, root->join_info_list)
{
SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
int innerrelid;
RelOptInfo *innerrel;
Relids joinrelids;
List *restrictlist;
/*
* Must be a semijoin to a single baserel, else we aren't going to be
* able to do anything with it.
*/
if (sjinfo->jointype != JOIN_SEMI)
continue;
if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid))
continue;
innerrel = find_base_rel(root, innerrelid);
/*
* Before we trouble to run generate_join_implied_equalities, make a
* quick check to eliminate cases in which we will surely be unable to
* prove uniqueness of the innerrel.
*/
if (!rel_supports_distinctness(root, innerrel))
continue;
/* Compute the relid set for the join we are considering */
joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
Assert(sjinfo->ojrelid == 0); /* SEMI joins don't have RT indexes */
/*
* Since we're only considering a single-rel RHS, any join clauses it
* has must be clauses linking it to the semijoin's min_lefthand. We
* can also consider EC-derived join clauses.
*/
restrictlist =
list_concat(generate_join_implied_equalities(root,
joinrelids,
sjinfo->min_lefthand,
innerrel,
NULL),
innerrel->joininfo);
/* Test whether the innerrel is unique for those clauses. */
if (!innerrel_is_unique(root,
joinrelids, sjinfo->min_lefthand, innerrel,
JOIN_SEMI, restrictlist, true))
continue;
/* OK, remove the SpecialJoinInfo from the list. */
root->join_info_list = foreach_delete_current(root->join_info_list, lc);
}
}
/*
* rel_supports_distinctness
* Could the relation possibly be proven distinct on some set of columns?
*
* This is effectively a pre-checking function for rel_is_distinct_for().
* It must return true if rel_is_distinct_for() could possibly return true
* with this rel, but it should not expend a lot of cycles. The idea is
* that callers can avoid doing possibly-expensive processing to compute
* rel_is_distinct_for()'s argument lists if the call could not possibly
* succeed.
*/
static bool
rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel)
{
/* We only know about baserels ... */
if (rel->reloptkind != RELOPT_BASEREL)
return false;
if (rel->rtekind == RTE_RELATION)
{
/*
* For a plain relation, we only know how to prove uniqueness by
* reference to unique indexes. Make sure there's at least one
* suitable unique index. It must be immediately enforced, and not a
* partial index. (Keep these conditions in sync with
* relation_has_unique_index_for!)
*/
ListCell *lc;
foreach(lc, rel->indexlist)
{
IndexOptInfo *ind = (IndexOptInfo *) lfirst(lc);
if (ind->unique && ind->immediate && ind->indpred == NIL)
return true;
}
}
else if (rel->rtekind == RTE_SUBQUERY)
{
Query *subquery = root->simple_rte_array[rel->relid]->subquery;
/* Check if the subquery has any qualities that support distinctness */
if (query_supports_distinctness(subquery))
return true;
}
/* We have no proof rules for any other rtekinds. */
return false;
}
/*
* rel_is_distinct_for
* Does the relation return only distinct rows according to clause_list?
*
* clause_list is a list of join restriction clauses involving this rel and
* some other one. Return true if no two rows emitted by this rel could
* possibly join to the same row of the other rel.
*
* The caller must have already determined that each condition is a
* mergejoinable equality with an expression in this relation on one side, and
* an expression not involving this relation on the other. The transient
* outer_is_left flag is used to identify which side references this relation:
* left side if outer_is_left is false, right side if it is true.
*
* Note that the passed-in clause_list may be destructively modified! This
* is OK for current uses, because the clause_list is built by the caller for
* the sole purpose of passing to this function.
*
* outer_exprs contains the right sides of baserestrictinfo clauses looking
* like x = const if distinctness is derived from such clauses, not joininfo
* clause. Pass NULL to the outer_exprs, if its value is not needed.
*/
static bool
rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list,
List **extra_clauses)
{
/*
* We could skip a couple of tests here if we assume all callers checked
* rel_supports_distinctness first, but it doesn't seem worth taking any
* risk for.
*/
if (rel->reloptkind != RELOPT_BASEREL)
return false;
if (rel->rtekind == RTE_RELATION)
{
/*
* Examine the indexes to see if we have a matching unique index.
* relation_has_unique_index_ext automatically adds any usable
* restriction clauses for the rel, so we needn't do that here.
*/
if (relation_has_unique_index_ext(root, rel, clause_list, NIL, NIL,
extra_clauses))
return true;
}
else if (rel->rtekind == RTE_SUBQUERY)
{
Index relid = rel->relid;
Query *subquery = root->simple_rte_array[relid]->subquery;
List *colnos = NIL;
List *opids = NIL;
ListCell *l;
/*
* Build the argument lists for query_is_distinct_for: a list of
* output column numbers that the query needs to be distinct over, and
* a list of equality operators that the output columns need to be
* distinct according to.
*
* (XXX we are not considering restriction clauses attached to the
* subquery; is that worth doing?)
*/
foreach(l, clause_list)
{
RestrictInfo *rinfo = lfirst_node(RestrictInfo, l);
Oid op;
Var *var;
/*
* Get the equality operator we need uniqueness according to.
* (This might be a cross-type operator and thus not exactly the
* same operator the subquery would consider; that's all right
* since query_is_distinct_for can resolve such cases.) The
* caller's mergejoinability test should have selected only
* OpExprs.
*/
op = castNode(OpExpr, rinfo->clause)->opno;
/* caller identified the inner side for us */
if (rinfo->outer_is_left)
var = (Var *) get_rightop(rinfo->clause);
else
var = (Var *) get_leftop(rinfo->clause);
/*
* We may ignore any RelabelType node above the operand. (There
* won't be more than one, since eval_const_expressions() has been
* applied already.)
*/
if (var && IsA(var, RelabelType))
var = (Var *) ((RelabelType *) var)->arg;
/*
* If inner side isn't a Var referencing a subquery output column,
* this clause doesn't help us.
*/
if (!var || !IsA(var, Var) ||
var->varno != relid || var->varlevelsup != 0)
continue;
colnos = lappend_int(colnos, var->varattno);
opids = lappend_oid(opids, op);
}
if (query_is_distinct_for(subquery, colnos, opids))
return true;
}
return false;
}
/*
* query_supports_distinctness - could the query possibly be proven distinct
* on some set of output columns?
*
* This is effectively a pre-checking function for query_is_distinct_for().
* It must return true if query_is_distinct_for() could possibly return true
* with this query, but it should not expend a lot of cycles. The idea is
* that callers can avoid doing possibly-expensive processing to compute
* query_is_distinct_for()'s argument lists if the call could not possibly
* succeed.
*/
bool
query_supports_distinctness(Query *query)
{
/* SRFs break distinctness except with DISTINCT, see below */
if (query->hasTargetSRFs && query->distinctClause == NIL)
return false;
/* check for features we can prove distinctness with */
if (query->distinctClause != NIL ||
query->groupClause != NIL ||
query->groupingSets != NIL ||
query->hasAggs ||
query->havingQual ||
query->setOperations)
return true;
return false;
}
/*
* query_is_distinct_for - does query never return duplicates of the
* specified columns?
*
* query is a not-yet-planned subquery (in current usage, it's always from
* a subquery RTE, which the planner avoids scribbling on).
*
* colnos is an integer list of output column numbers (resno's). We are
* interested in whether rows consisting of just these columns are certain
* to be distinct. "Distinctness" is defined according to whether the
* corresponding upper-level equality operators listed in opids would think
* the values are distinct. (Note: the opids entries could be cross-type
* operators, and thus not exactly the equality operators that the subquery
* would use itself. We use equality_ops_are_compatible() to check
* compatibility. That looks at btree or hash opfamily membership, and so
* should give trustworthy answers for all operators that we might need
* to deal with here.)
*/
bool
query_is_distinct_for(Query *query, List *colnos, List *opids)
{
ListCell *l;
Oid opid;
Assert(list_length(colnos) == list_length(opids));
/*
* DISTINCT (including DISTINCT ON) guarantees uniqueness if all the
* columns in the DISTINCT clause appear in colnos and operator semantics
* match. This is true even if there are SRFs in the DISTINCT columns or
* elsewhere in the tlist.
*/
if (query->distinctClause)
{
foreach(l, query->distinctClause)
{
SortGroupClause *sgc = (SortGroupClause *) lfirst(l);
TargetEntry *tle = get_sortgroupclause_tle(sgc,
query->targetList);
opid = distinct_col_search(tle->resno, colnos, opids);
if (!OidIsValid(opid) ||
!equality_ops_are_compatible(opid, sgc->eqop))
break; /* exit early if no match */
}
if (l == NULL) /* had matches for all? */
return true;
}
/*
* Otherwise, a set-returning function in the query's targetlist can
* result in returning duplicate rows, despite any grouping that might
* occur before tlist evaluation. (If all tlist SRFs are within GROUP BY
* columns, it would be safe because they'd be expanded before grouping.
* But it doesn't currently seem worth the effort to check for that.)
*/
if (query->hasTargetSRFs)
return false;
/*
* Similarly, GROUP BY without GROUPING SETS guarantees uniqueness if all
* the grouped columns appear in colnos and operator semantics match.
*/
if (query->groupClause && !query->groupingSets)
{
foreach(l, query->groupClause)
{
SortGroupClause *sgc = (SortGroupClause *) lfirst(l);
TargetEntry *tle = get_sortgroupclause_tle(sgc,
query->targetList);
opid = distinct_col_search(tle->resno, colnos, opids);
if (!OidIsValid(opid) ||
!equality_ops_are_compatible(opid, sgc->eqop))
break; /* exit early if no match */
}
if (l == NULL) /* had matches for all? */
return true;
}
else if (query->groupingSets)
{
/*
* If we have grouping sets with expressions, we probably don't have
* uniqueness and analysis would be hard. Punt.
*/
if (query->groupClause)
return false;
/*
* If we have no groupClause (therefore no grouping expressions), we
* might have one or many empty grouping sets. If there's just one,
* then we're returning only one row and are certainly unique. But
* otherwise, we know we're certainly not unique.
*/
if (list_length(query->groupingSets) == 1 &&
((GroupingSet *) linitial(query->groupingSets))->kind == GROUPING_SET_EMPTY)
return true;
else
return false;
}
else
{
/*
* If we have no GROUP BY, but do have aggregates or HAVING, then the
* result is at most one row so it's surely unique, for any operators.
*/
if (query->hasAggs || query->havingQual)
return true;
}
/*
* UNION, INTERSECT, EXCEPT guarantee uniqueness of the whole output row,
* except with ALL.
*/
if (query->setOperations)
{
SetOperationStmt *topop = castNode(SetOperationStmt, query->setOperations);
Assert(topop->op != SETOP_NONE);
if (!topop->all)
{
ListCell *lg;
/* We're good if all the nonjunk output columns are in colnos */
lg = list_head(topop->groupClauses);
foreach(l, query->targetList)
{
TargetEntry *tle = (TargetEntry *) lfirst(l);
SortGroupClause *sgc;
if (tle->resjunk)
continue; /* ignore resjunk columns */
/* non-resjunk columns should have grouping clauses */
Assert(lg != NULL);
sgc = (SortGroupClause *) lfirst(lg);
lg = lnext(topop->groupClauses, lg);
opid = distinct_col_search(tle->resno, colnos, opids);
if (!OidIsValid(opid) ||
!equality_ops_are_compatible(opid, sgc->eqop))
break; /* exit early if no match */
}
if (l == NULL) /* had matches for all? */
return true;
}
}
/*
* XXX Are there any other cases in which we can easily see the result
* must be distinct?
*
* If you do add more smarts to this function, be sure to update
* query_supports_distinctness() to match.
*/
return false;
}
/*
* distinct_col_search - subroutine for query_is_distinct_for
*
* If colno is in colnos, return the corresponding element of opids,
* else return InvalidOid. (Ordinarily colnos would not contain duplicates,
* but if it does, we arbitrarily select the first match.)
*/
static Oid
distinct_col_search(int colno, List *colnos, List *opids)
{
ListCell *lc1,
*lc2;
forboth(lc1, colnos, lc2, opids)
{
if (colno == lfirst_int(lc1))
return lfirst_oid(lc2);
}
return InvalidOid;
}
/*
* innerrel_is_unique
* Check if the innerrel provably contains at most one tuple matching any
* tuple from the outerrel, based on join clauses in the 'restrictlist'.
*
* We need an actual RelOptInfo for the innerrel, but it's sufficient to
* identify the outerrel by its Relids. This asymmetry supports use of this
* function before joinrels have been built. (The caller is expected to
* also supply the joinrelids, just to save recalculating that.)
*
* The proof must be made based only on clauses that will be "joinquals"
* rather than "otherquals" at execution. For an inner join there's no
* difference; but if the join is outer, we must ignore pushed-down quals,
* as those will become "otherquals". Note that this means the answer might
* vary depending on whether IS_OUTER_JOIN(jointype); since we cache the
* answer without regard to that, callers must take care not to call this
* with jointypes that would be classified differently by IS_OUTER_JOIN().
*
* The actual proof is undertaken by is_innerrel_unique_for(); this function
* is a frontend that is mainly concerned with caching the answers.
* In particular, the force_cache argument allows overriding the internal
* heuristic about whether to cache negative answers; it should be "true"
* if making an inquiry that is not part of the normal bottom-up join search
* sequence.
*/
bool
innerrel_is_unique(PlannerInfo *root,
Relids joinrelids,
Relids outerrelids,
RelOptInfo *innerrel,
JoinType jointype,
List *restrictlist,
bool force_cache)
{
return innerrel_is_unique_ext(root, joinrelids, outerrelids, innerrel,
jointype, restrictlist, force_cache, NULL);
}
/*
* innerrel_is_unique_ext
* Do the same as innerrel_is_unique(), but also set to '*extra_clauses'
* additional clauses from a baserestrictinfo list that were used to prove
* uniqueness. A non NULL 'extra_clauses' indicates that we're checking
* for self-join and correspondingly dealing with filtered clauses.
*/
bool
innerrel_is_unique_ext(PlannerInfo *root,
Relids joinrelids,
Relids outerrelids,
RelOptInfo *innerrel,
JoinType jointype,
List *restrictlist,
bool force_cache,
List **extra_clauses)
{
MemoryContext old_context;
ListCell *lc;
UniqueRelInfo *uniqueRelInfo;
List *outer_exprs = NIL;
bool self_join = (extra_clauses != NULL);
/* Certainly can't prove uniqueness when there are no joinclauses */
if (restrictlist == NIL)
return false;
/*
* Make a quick check to eliminate cases in which we will surely be unable
* to prove uniqueness of the innerrel.
*/
if (!rel_supports_distinctness(root, innerrel))
return false;
/*
* Query the cache to see if we've managed to prove that innerrel is
* unique for any subset of this outerrel. For non self-join search, we
* don't need an exact match, as extra outerrels can't make the innerrel
* any less unique (or more formally, the restrictlist for a join to a
* superset outerrel must be a superset of the conditions we successfully
* used before). For self-join search, we require an exact match of
* outerrels, because we need extra clauses to be valid for our case.
* Also, for self-join checking we've filtered the clauses list. Thus,
* for a self-join search, we can match only the result cached for another
* self-join check.
*/
foreach(lc, innerrel->unique_for_rels)
{
uniqueRelInfo = (UniqueRelInfo *) lfirst(lc);
if ((!self_join && bms_is_subset(uniqueRelInfo->outerrelids, outerrelids)) ||
(self_join && bms_equal(uniqueRelInfo->outerrelids, outerrelids) &&
uniqueRelInfo->self_join))
{
if (extra_clauses)
*extra_clauses = uniqueRelInfo->extra_clauses;
return true; /* Success! */
}
}
/*
* Conversely, we may have already determined that this outerrel, or some
* superset thereof, cannot prove this innerrel to be unique.
*/
foreach(lc, innerrel->non_unique_for_rels)
{
Relids unique_for_rels = (Relids) lfirst(lc);
if (bms_is_subset(outerrelids, unique_for_rels))
return false;
}
/* No cached information, so try to make the proof. */
if (is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel,
jointype, restrictlist,
self_join ? &outer_exprs : NULL))
{
/*
* Cache the positive result for future probes, being sure to keep it
* in the planner_cxt even if we are working in GEQO.
*
* Note: one might consider trying to isolate the minimal subset of
* the outerrels that proved the innerrel unique. But it's not worth
* the trouble, because the planner builds up joinrels incrementally
* and so we'll see the minimally sufficient outerrels before any
* supersets of them anyway.
*/
old_context = MemoryContextSwitchTo(root->planner_cxt);
uniqueRelInfo = makeNode(UniqueRelInfo);
uniqueRelInfo->outerrelids = bms_copy(outerrelids);
uniqueRelInfo->self_join = self_join;
uniqueRelInfo->extra_clauses = outer_exprs;
innerrel->unique_for_rels = lappend(innerrel->unique_for_rels,
uniqueRelInfo);
MemoryContextSwitchTo(old_context);
if (extra_clauses)
*extra_clauses = outer_exprs;
return true; /* Success! */
}
else
{
/*
* None of the join conditions for outerrel proved innerrel unique, so
* we can safely reject this outerrel or any subset of it in future
* checks.
*
* However, in normal planning mode, caching this knowledge is totally
* pointless; it won't be queried again, because we build up joinrels
* from smaller to larger. It is useful in GEQO mode, where the
* knowledge can be carried across successive planning attempts; and
* it's likely to be useful when using join-search plugins, too. Hence
* cache when join_search_private is non-NULL. (Yeah, that's a hack,
* but it seems reasonable.)
*
* Also, allow callers to override that heuristic and force caching;
* that's useful for reduce_unique_semijoins, which calls here before
* the normal join search starts.
*/
if (force_cache || root->join_search_private)
{
old_context = MemoryContextSwitchTo(root->planner_cxt);
innerrel->non_unique_for_rels =
lappend(innerrel->non_unique_for_rels,
bms_copy(outerrelids));
MemoryContextSwitchTo(old_context);
}
return false;
}
}
/*
* is_innerrel_unique_for
* Check if the innerrel provably contains at most one tuple matching any
* tuple from the outerrel, based on join clauses in the 'restrictlist'.
*/
static bool
is_innerrel_unique_for(PlannerInfo *root,
Relids joinrelids,
Relids outerrelids,
RelOptInfo *innerrel,
JoinType jointype,
List *restrictlist,
List **extra_clauses)
{
List *clause_list = NIL;
ListCell *lc;
/*
* Search for mergejoinable clauses that constrain the inner rel against
* the outer rel. If an operator is mergejoinable then it behaves like
* equality for some btree opclass, so it's what we want. The
* mergejoinability test also eliminates clauses containing volatile
* functions, which we couldn't depend on.
*/
foreach(lc, restrictlist)
{
RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(lc);
/*
* As noted above, if it's a pushed-down clause and we're at an outer
* join, we can't use it.
*/
if (IS_OUTER_JOIN(jointype) &&
RINFO_IS_PUSHED_DOWN(restrictinfo, joinrelids))
continue;
/* Ignore if it's not a mergejoinable clause */
if (!restrictinfo->can_join ||
restrictinfo->mergeopfamilies == NIL)
continue; /* not mergejoinable */
/*
* Check if the clause has the form "outer op inner" or "inner op
* outer", and if so mark which side is inner.
*/
if (!clause_sides_match_join(restrictinfo, outerrelids,
innerrel->relids))
continue; /* no good for these input relations */
/* OK, add to the list */
clause_list = lappend(clause_list, restrictinfo);
}
/* Let rel_is_distinct_for() do the hard work */
return rel_is_distinct_for(root, innerrel, clause_list, extra_clauses);
}
/*
* replace_varno - find in the given tree any Vars, PlaceHolderVar, and Relids
* that reference the removing relid, and change them to the reference to
* the replacement relid.
*
* NOTE: although this has the form of a walker, we cheat and modify the
* nodes in-place.
*/
typedef struct
{
int from;
int to;
int sublevels_up;
} ReplaceVarnoContext;
static bool
replace_varno_walker(Node *node, ReplaceVarnoContext *ctx)
{
if (node == NULL)
return false;
if (IsA(node, Var))
{
Var *var = (Var *) node;
if (var->varno == ctx->from &&
var->varlevelsup == ctx->sublevels_up)
{
var->varno = ctx->to;
var->varnosyn = ctx->to;
}
return false;
}
else if (IsA(node, PlaceHolderVar))
{
PlaceHolderVar *phv = (PlaceHolderVar *) node;
if (phv->phlevelsup == ctx->sublevels_up)
{
phv->phrels =
replace_relid(phv->phrels, ctx->from, ctx->to);
phv->phnullingrels =
replace_relid(phv->phnullingrels, ctx->from, ctx->to);
}
/* fall through to recurse into the placeholder's expression */
}
else if (IsA(node, Query))
{
/* Recurse into subselects */
bool result;
ctx->sublevels_up++;
result = query_tree_walker((Query *) node,
replace_varno_walker,
(void *) ctx,
QTW_EXAMINE_SORTGROUP);
ctx->sublevels_up--;
return result;
}
else if (IsA(node, RestrictInfo))
{
RestrictInfo *rinfo = (RestrictInfo *) node;
int relid = -1;
bool is_req_equal =
(rinfo->required_relids == rinfo->clause_relids);
if (bms_is_member(ctx->from, rinfo->clause_relids))
{
replace_varno((Node *) rinfo->clause, ctx->from, ctx->to);
replace_varno((Node *) rinfo->orclause, ctx->from, ctx->to);
rinfo->clause_relids =
replace_relid(rinfo->clause_relids, ctx->from, ctx->to);
rinfo->left_relids =
replace_relid(rinfo->left_relids, ctx->from, ctx->to);
rinfo->right_relids =
replace_relid(rinfo->right_relids, ctx->from, ctx->to);
}
if (is_req_equal)
rinfo->required_relids = rinfo->clause_relids;
else
rinfo->required_relids =
replace_relid(rinfo->required_relids, ctx->from, ctx->to);
rinfo->outer_relids =
replace_relid(rinfo->outer_relids, ctx->from, ctx->to);
rinfo->incompatible_relids =
replace_relid(rinfo->incompatible_relids, ctx->from, ctx->to);
if (rinfo->mergeopfamilies &&
bms_get_singleton_member(rinfo->clause_relids, &relid) &&
relid == ctx->to && IsA(rinfo->clause, OpExpr))
{
Expr *leftOp;
Expr *rightOp;
leftOp = (Expr *) get_leftop(rinfo->clause);
rightOp = (Expr *) get_rightop(rinfo->clause);
if (leftOp != NULL && equal(leftOp, rightOp))
{
NullTest *ntest = makeNode(NullTest);
ntest->arg = leftOp;
ntest->nulltesttype = IS_NOT_NULL;
ntest->argisrow = false;
ntest->location = -1;
rinfo->clause = (Expr *) ntest;
rinfo->mergeopfamilies = NIL;
}
Assert(rinfo->orclause == NULL);
}
return false;
}
return expression_tree_walker(node, replace_varno_walker,
(void *) ctx);
}
static void
replace_varno(Node *node, int from, int to)
{
ReplaceVarnoContext ctx;
if (to <= 0)
return;
ctx.from = from;
ctx.to = to;
ctx.sublevels_up = 0;
/*
* Must be prepared to start with a Query or a bare expression tree.
*/
query_or_expression_tree_walker(node,
replace_varno_walker,
(void *) &ctx,
QTW_EXAMINE_SORTGROUP);
}
/*
* Substitute newId by oldId in relids.
*
* We must make a copy of the original Bitmapset before making any
* modifications, because the same pointer to it might be shared among
* different places.
*/
static Bitmapset *
replace_relid(Relids relids, int oldId, int newId)
{
if (oldId < 0)
return relids;
/* Delete relid without substitution. */
if (newId < 0)
return bms_del_member(bms_copy(relids), oldId);
/* Substitute newId for oldId. */
if (bms_is_member(oldId, relids))
return bms_add_member(bms_del_member(bms_copy(relids), oldId), newId);
return relids;
}
/*
* Update EC members to point to the remaining relation instead of the removed
* one, removing duplicates.
*
* Restriction clauses for base relations are already distributed to
* the respective baserestrictinfo lists (see
* generate_implied_equalities_for_column). The above code has already processed
* this list, and updated these clauses to reference the remaining
* relation, so we can skip them here based on their relids.
*
* Likewise, we have already processed the join clauses that join the
* removed relation to the remaining one.
*
* Finally, there are join clauses that join the removed relation to
* some third relation. We can't just delete the source clauses and
* regenerate them from the EC because the corresponding equality
* operators might be missing (see the handling of ec_broken).
* Therefore, we will update the references in the source clauses.
*
* Derived clauses can be generated again, so it is simpler to just
* delete them.
*/
static void
update_eclasses(EquivalenceClass *ec, int from, int to)
{
List *new_members = NIL;
List *new_sources = NIL;
ListCell *lc;
ListCell *lc1;
foreach(lc, ec->ec_members)
{
EquivalenceMember *em = lfirst_node(EquivalenceMember, lc);
bool is_redundant = false;
if (!bms_is_member(from, em->em_relids))
{
new_members = lappend(new_members, em);
continue;
}
em->em_relids = replace_relid(em->em_relids, from, to);
em->em_jdomain->jd_relids = replace_relid(em->em_jdomain->jd_relids, from, to);
/* We only process inner joins */
replace_varno((Node *) em->em_expr, from, to);
foreach(lc1, new_members)
{
EquivalenceMember *other = lfirst_node(EquivalenceMember, lc1);
if (!equal(em->em_relids, other->em_relids))
continue;
if (equal(em->em_expr, other->em_expr))
{
is_redundant = true;
break;
}
}
if (!is_redundant)
new_members = lappend(new_members, em);
}
list_free(ec->ec_members);
ec->ec_members = new_members;
list_free(ec->ec_derives);
ec->ec_derives = NULL;
/* Update EC source expressions */
foreach(lc, ec->ec_sources)
{
RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
bool is_redundant = false;
if (!bms_is_member(from, rinfo->required_relids))
{
new_sources = lappend(new_sources, rinfo);
continue;
}
replace_varno((Node *) rinfo, from, to);
/*
* After switching the clause to the remaining relation, check it for
* redundancy with existing ones. We don't have to check for
* redundancy with derived clauses, because we've just deleted them.
*/
foreach(lc1, new_sources)
{
RestrictInfo *other = lfirst_node(RestrictInfo, lc1);
if (!equal(rinfo->clause_relids, other->clause_relids))
continue;
if (equal(rinfo->clause, other->clause))
{
is_redundant = true;
break;
}
}
if (!is_redundant)
new_sources = lappend(new_sources, rinfo);
}
list_free(ec->ec_sources);
ec->ec_sources = new_sources;
ec->ec_relids = replace_relid(ec->ec_relids, from, to);
}
/*
* "Logically" compares two RestrictInfo's ignoring the 'rinfo_serial' field,
* which makes almost every RestrictInfo unique. This type of comparison is
* useful when removing duplicates while moving RestrictInfo's from removed
* relation to remaining relation during self-join elimination.
*
* XXX: In the future, we might remove the 'rinfo_serial' field completely and
* get rid of this function.
*/
static bool
restrict_infos_logically_equal(RestrictInfo *a, RestrictInfo *b)
{
int saved_rinfo_serial = a->rinfo_serial;
bool result;
a->rinfo_serial = b->rinfo_serial;
result = equal(a, b);
a->rinfo_serial = saved_rinfo_serial;
return result;
}
/*
* Remove a relation after we have proven that it participates only in an
* unneeded unique self join.
*
* Replace any links in planner info structures.
*
* Transfer join and restriction clauses from the removed relation to the
* remaining one. We change the Vars of the clause to point to the
* remaining relation instead of the removed one. The clauses that require
* a subset of joinrelids become restriction clauses of the remaining
* relation, and others remain join clauses. We append them to
* baserestrictinfo and joininfo respectively, trying not to introduce
* duplicates.
*
* We also have to process the 'joinclauses' list here, because it
* contains EC-derived join clauses which must become filter clauses. It
* is not enough to just correct the ECs because the EC-derived
* restrictions are generated before join removal (see
* generate_base_implied_equalities).
*/
static void
remove_self_join_rel(PlannerInfo *root, PlanRowMark *kmark, PlanRowMark *rmark,
RelOptInfo *toKeep, RelOptInfo *toRemove,
List *restrictlist)
{
List *joininfos;
ListCell *lc;
int i;
List *jinfo_candidates = NIL;
List *binfo_candidates = NIL;
Assert(toKeep->relid != -1);
/*
* Replace the index of the removing table with the keeping one. The
* technique of removing/distributing restrictinfo is used here to attach
* just appeared (for keeping relation) join clauses and avoid adding
* duplicates of those that already exist in the joininfo list.
*/
joininfos = list_copy(toRemove->joininfo);
foreach(lc, joininfos)
{
RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
remove_join_clause_from_rels(root, rinfo, rinfo->required_relids);
replace_varno((Node *) rinfo, toRemove->relid, toKeep->relid);
if (bms_membership(rinfo->required_relids) == BMS_MULTIPLE)
jinfo_candidates = lappend(jinfo_candidates, rinfo);
else
binfo_candidates = lappend(binfo_candidates, rinfo);
}
/*
* Concatenate restrictlist to the list of base restrictions of the
* removing table just to simplify the replacement procedure: all of them
* weren't connected to any keeping relations and need to be added to some
* rels.
*/
toRemove->baserestrictinfo = list_concat(toRemove->baserestrictinfo,
restrictlist);
foreach(lc, toRemove->baserestrictinfo)
{
RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
replace_varno((Node *) rinfo, toRemove->relid, toKeep->relid);
if (bms_membership(rinfo->required_relids) == BMS_MULTIPLE)
jinfo_candidates = lappend(jinfo_candidates, rinfo);
else
binfo_candidates = lappend(binfo_candidates, rinfo);
}
/*
* Now, add all non-redundant clauses to the keeping relation.
* Contradictory operation. On the one side, we reduce the length of
* restrict lists that can impact planning or executing time.
* Additionally, we improve the accuracy of cardinality estimation. On the
* other side, it is one more place that can make planning time much
* longer in specific cases. It would have been better to avoid calling
* the equal() function here, but it's the only way to detect duplicated
* inequality expressions.
*/
foreach(lc, binfo_candidates)
{
RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
ListCell *olc = NULL;
bool is_redundant = false;
Assert(!bms_is_member(toRemove->relid, rinfo->required_relids));
foreach(olc, toKeep->baserestrictinfo)
{
RestrictInfo *src = lfirst_node(RestrictInfo, olc);
if (!bms_equal(src->clause_relids, rinfo->clause_relids))
/* Const and non-const expressions can't be equal */
continue;
if (src == rinfo ||
(rinfo->parent_ec != NULL
&& src->parent_ec == rinfo->parent_ec)
|| restrict_infos_logically_equal(rinfo, src))
{
is_redundant = true;
break;
}
}
if (!is_redundant)
distribute_restrictinfo_to_rels(root, rinfo);
}
foreach(lc, jinfo_candidates)
{
RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
ListCell *olc = NULL;
bool is_redundant = false;
Assert(!bms_is_member(toRemove->relid, rinfo->required_relids));
foreach(olc, toKeep->joininfo)
{
RestrictInfo *src = lfirst_node(RestrictInfo, olc);
if (!bms_equal(src->clause_relids, rinfo->clause_relids))
/* Can't compare trivially different clauses */
continue;
if (src == rinfo ||
(rinfo->parent_ec != NULL
&& src->parent_ec == rinfo->parent_ec)
|| restrict_infos_logically_equal(rinfo, src))
{
is_redundant = true;
break;
}
}
if (!is_redundant)
distribute_restrictinfo_to_rels(root, rinfo);
}
list_free(binfo_candidates);
list_free(jinfo_candidates);
/*
* Arrange equivalence classes, mentioned removing a table, with the
* keeping one: varno of removing table should be replaced in members and
* sources lists. Also, remove duplicated elements if this replacement
* procedure created them.
*/
i = -1;
while ((i = bms_next_member(toRemove->eclass_indexes, i)) >= 0)
{
EquivalenceClass *ec = (EquivalenceClass *) list_nth(root->eq_classes, i);
update_eclasses(ec, toRemove->relid, toKeep->relid);
toKeep->eclass_indexes = bms_add_member(toKeep->eclass_indexes, i);
}
/*
* Transfer the targetlist and attr_needed flags.
*/
foreach(lc, toRemove->reltarget->exprs)
{
Node *node = lfirst(lc);
replace_varno(node, toRemove->relid, toKeep->relid);
if (!list_member(toKeep->reltarget->exprs, node))
toKeep->reltarget->exprs = lappend(toKeep->reltarget->exprs, node);
}
for (i = toKeep->min_attr; i <= toKeep->max_attr; i++)
{
int attno = i - toKeep->min_attr;
toRemove->attr_needed[attno] = replace_relid(toRemove->attr_needed[attno],
toRemove->relid, toKeep->relid);
toKeep->attr_needed[attno] = bms_add_members(toKeep->attr_needed[attno],
toRemove->attr_needed[attno]);
}
/*
* If the removed relation has a row mark, transfer it to the remaining
* one.
*
* If both rels have row marks, just keep the one corresponding to the
* remaining relation, because we verified earlier that they have the same
* strength.
*/
if (rmark)
{
if (kmark)
{
Assert(kmark->markType == rmark->markType);
root->rowMarks = list_delete_ptr(root->rowMarks, rmark);
}
else
{
/* Shouldn't have inheritance children here. */
Assert(rmark->rti == rmark->prti);
rmark->rti = rmark->prti = toKeep->relid;
}
}
/* Replace varno in all the query structures */
replace_varno((Node *) root->parse, toRemove->relid, toKeep->relid);
/* See remove_self_joins_one_group() */
Assert(root->parse->resultRelation != toRemove->relid);
Assert(root->parse->resultRelation != toKeep->relid);
/* Replace links in the planner info */
remove_rel_from_query(root, toRemove, toKeep->relid, NULL, NULL);
/* At last, replace varno in root targetlist and HAVING clause */
replace_varno((Node *) root->processed_tlist,
toRemove->relid, toKeep->relid);
replace_varno((Node *) root->processed_groupClause,
toRemove->relid, toKeep->relid);
replace_relid(root->all_result_relids, toRemove->relid, toKeep->relid);
replace_relid(root->leaf_result_relids, toRemove->relid, toKeep->relid);
/*
* There may be references to the rel in root->fkey_list, but if so,
* match_foreign_keys_to_quals() will get rid of them.
*/
/*
* Finally, remove the rel from the baserel array to prevent it from being
* referenced again. (We can't do this earlier because
* remove_join_clause_from_rels will touch it.)
*/
root->simple_rel_array[toRemove->relid] = NULL;
/* And nuke the RelOptInfo, just in case there's another access path */
pfree(toRemove);
}
/*
* split_selfjoin_quals
* Processes 'joinquals' by building two lists: one containing the quals
* where the columns/exprs are on either side of the join match, and
* another one containing the remaining quals.
*
* 'joinquals' must only contain quals for a RTE_RELATION being joined to
* itself.
*/
static void
split_selfjoin_quals(PlannerInfo *root, List *joinquals, List **selfjoinquals,
List **otherjoinquals, int from, int to)
{
ListCell *lc;
List *sjoinquals = NIL;
List *ojoinquals = NIL;
foreach(lc, joinquals)
{
RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
OpExpr *expr;
Node *leftexpr;
Node *rightexpr;
/* In general, clause looks like F(arg1) = G(arg2) */
if (!rinfo->mergeopfamilies ||
bms_num_members(rinfo->clause_relids) != 2 ||
bms_membership(rinfo->left_relids) != BMS_SINGLETON ||
bms_membership(rinfo->right_relids) != BMS_SINGLETON)
{
ojoinquals = lappend(ojoinquals, rinfo);
continue;
}
expr = (OpExpr *) rinfo->clause;
if (!IsA(expr, OpExpr) || list_length(expr->args) != 2)
{
ojoinquals = lappend(ojoinquals, rinfo);
continue;
}
leftexpr = get_leftop(rinfo->clause);
rightexpr = copyObject(get_rightop(rinfo->clause));
if (leftexpr && IsA(leftexpr, RelabelType))
leftexpr = (Node *) ((RelabelType *) leftexpr)->arg;
if (rightexpr && IsA(rightexpr, RelabelType))
rightexpr = (Node *) ((RelabelType *) rightexpr)->arg;
/*
* Quite an expensive operation, narrowing the use case. For example,
* when we have cast of the same var to different (but compatible)
* types.
*/
replace_varno(rightexpr, bms_singleton_member(rinfo->right_relids),
bms_singleton_member(rinfo->left_relids));
if (equal(leftexpr, rightexpr))
sjoinquals = lappend(sjoinquals, rinfo);
else
ojoinquals = lappend(ojoinquals, rinfo);
}
*selfjoinquals = sjoinquals;
*otherjoinquals = ojoinquals;
}
/*
* Check for a case when uniqueness is at least partly derived from a
* baserestrictinfo clause. In this case, we have a chance to return only
* one row (if such clauses on both sides of SJ are equal) or nothing (if they
* are different).
*/
static bool
match_unique_clauses(PlannerInfo *root, RelOptInfo *outer, List *uclauses,
Index relid)
{
ListCell *lc;
foreach(lc, uclauses)
{
RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
Expr *clause;
Node *iclause;
Node *c1;
bool matched = false;
ListCell *olc;
Assert(outer->relid > 0 && relid > 0);
/* Only filters like f(R.x1,...,R.xN) == expr we should consider. */
Assert(bms_is_empty(rinfo->left_relids) ^
bms_is_empty(rinfo->right_relids));
clause = (Expr *) copyObject(rinfo->clause);
replace_varno((Node *) clause, relid, outer->relid);
iclause = bms_is_empty(rinfo->left_relids) ? get_rightop(clause) :
get_leftop(clause);
c1 = bms_is_empty(rinfo->left_relids) ? get_leftop(clause) :
get_rightop(clause);
/*
* Compare these left and right sides with the corresponding sides of
* the outer's filters. If no one is detected - return immediately.
*/
foreach(olc, outer->baserestrictinfo)
{
RestrictInfo *orinfo = lfirst_node(RestrictInfo, olc);
Node *oclause;
Node *c2;
if (orinfo->mergeopfamilies == NIL)
/* Don't consider clauses which aren't similar to 'F(X)=G(Y)' */
continue;
Assert(is_opclause(orinfo->clause));
oclause = bms_is_empty(orinfo->left_relids) ?
get_rightop(orinfo->clause) : get_leftop(orinfo->clause);
c2 = (bms_is_empty(orinfo->left_relids) ?
get_leftop(orinfo->clause) : get_rightop(orinfo->clause));
if (equal(iclause, oclause) && equal(c1, c2))
{
matched = true;
break;
}
}
if (!matched)
return false;
}
return true;
}
/*
* Find and remove unique self joins in a group of base relations that have
* the same Oid.
*
* Returns a set of relids that were removed.
*/
static Relids
remove_self_joins_one_group(PlannerInfo *root, Relids relids)
{
Relids result = NULL;
int k; /* Index of kept relation */
int r = -1; /* Index of removed relation */
while ((r = bms_next_member(relids, r)) > 0)
{
RelOptInfo *inner = root->simple_rel_array[r];
/*
* We don't accept result relation as either source or target relation
* of SJE, because result relation has different behavior in
* EvalPlanQual() and RETURNING clause.
*/
if (root->parse->resultRelation == r)
continue;
k = r;
while ((k = bms_next_member(relids, k)) > 0)
{
Relids joinrelids = NULL;
RelOptInfo *outer = root->simple_rel_array[k];
List *restrictlist;
List *selfjoinquals;
List *otherjoinquals;
ListCell *lc;
bool jinfo_check = true;
PlanRowMark *omark = NULL;
PlanRowMark *imark = NULL;
List *uclauses = NIL;
if (root->parse->resultRelation == k)
continue;
/* A sanity check: the relations have the same Oid. */
Assert(root->simple_rte_array[k]->relid ==
root->simple_rte_array[r]->relid);
/*
* It is impossible to eliminate join of two relations if they
* belong to different rules of order. Otherwise planner can't be
* able to find any variants of correct query plan.
*/
foreach(lc, root->join_info_list)
{
SpecialJoinInfo *info = (SpecialJoinInfo *) lfirst(lc);
if ((bms_is_member(k, info->syn_lefthand) ^
bms_is_member(r, info->syn_lefthand)) ||
(bms_is_member(k, info->syn_righthand) ^
bms_is_member(r, info->syn_righthand)))
{
jinfo_check = false;
break;
}
}
if (!jinfo_check)
continue;
/*
* Check Row Marks equivalence. We can't remove the join if the
* relations have row marks of different strength (e.g. one is
* locked FOR UPDATE and another just has ROW_MARK_REFERENCE for
* EvalPlanQual rechecking).
*/
foreach(lc, root->rowMarks)
{
PlanRowMark *rowMark = (PlanRowMark *) lfirst(lc);
if (rowMark->rti == k)
{
Assert(imark == NULL);
imark = rowMark;
}
else if (rowMark->rti == r)
{
Assert(omark == NULL);
omark = rowMark;
}
if (omark && imark)
break;
}
if (omark && imark && omark->markType != imark->markType)
continue;
/*
* We only deal with base rels here, so their relids bitset
* contains only one member -- their relid.
*/
joinrelids = bms_add_member(joinrelids, r);
joinrelids = bms_add_member(joinrelids, k);
/*
* PHVs should not impose any constraints on removing self joins.
*/
/*
* At this stage, joininfo lists of inner and outer can contain
* only clauses, required for a superior outer join that can't
* influence this optimization. So, we can avoid to call the
* build_joinrel_restrictlist() routine.
*/
restrictlist = generate_join_implied_equalities(root, joinrelids,
inner->relids,
outer, NULL);
/*
* Process restrictlist to separate the self join quals out of the
* other quals. e.g x = x goes to selfjoinquals and a = b to
* otherjoinquals.
*/
split_selfjoin_quals(root, restrictlist, &selfjoinquals,
&otherjoinquals, inner->relid, outer->relid);
/*
* To enable SJE for the only degenerate case without any self
* join clauses at all, add baserestrictinfo to this list. The
* degenerate case works only if both sides have the same clause.
* So doesn't matter which side to add.
*/
selfjoinquals = list_concat(selfjoinquals, outer->baserestrictinfo);
/*
* Determine if the inner table can duplicate outer rows. We must
* bypass the unique rel cache here since we're possibly using a
* subset of join quals. We can use 'force_cache' == true when all
* join quals are self-join quals. Otherwise, we could end up
* putting false negatives in the cache.
*/
if (!innerrel_is_unique_ext(root, joinrelids, inner->relids,
outer, JOIN_INNER, selfjoinquals,
list_length(otherjoinquals) == 0,
&uclauses))
continue;
/*
* We have proven that for both relations, the same unique index
* guarantees that there is at most one row where columns equal
* given values. These values must be the same for both relations,
* or else we won't match the same row on each side of the join.
*/
if (!match_unique_clauses(root, inner, uclauses, outer->relid))
continue;
/*
* We can remove either relation, so remove the inner one in order
* to simplify this loop.
*/
remove_self_join_rel(root, omark, imark, outer, inner, restrictlist);
result = bms_add_member(result, r);
/* We have removed the outer relation, try the next one. */
break;
}
}
return result;
}
/*
* Gather indexes of base relations from the joinlist and try to eliminate self
* joins.
*/
static Relids
remove_self_joins_recurse(PlannerInfo *root, List *joinlist, Relids toRemove)
{
ListCell *jl;
Relids relids = NULL;
SelfJoinCandidate *candidates = NULL;
int i;
int j;
int numRels;
/* Collect indexes of base relations of the join tree */
foreach(jl, joinlist)
{
Node *jlnode = (Node *) lfirst(jl);
if (IsA(jlnode, RangeTblRef))
{
RangeTblRef *ref = (RangeTblRef *) jlnode;
RangeTblEntry *rte = root->simple_rte_array[ref->rtindex];
/*
* We only care about base relations from which we select
* something.
*/
if (rte->rtekind == RTE_RELATION &&
rte->relkind == RELKIND_RELATION &&
root->simple_rel_array[ref->rtindex] != NULL)
{
Assert(!bms_is_member(ref->rtindex, relids));
relids = bms_add_member(relids, ref->rtindex);
}
}
else if (IsA(jlnode, List))
/* Recursively go inside the sub-joinlist */
toRemove = remove_self_joins_recurse(root, (List *) jlnode,
toRemove);
else
elog(ERROR, "unrecognized joinlist node type: %d",
(int) nodeTag(jlnode));
}
numRels = bms_num_members(relids);
/* Need at least two relations for the join */
if (numRels < 2)
return toRemove;
/*
* In order to find relations with the same oid we first build an array of
* candidates and then sort it by oid.
*/
candidates = (SelfJoinCandidate *) palloc(sizeof(SelfJoinCandidate) *
numRels);
i = -1;
j = 0;
while ((i = bms_next_member(relids, i)) >= 0)
{
candidates[j].relid = i;
candidates[j].reloid = root->simple_rte_array[i]->relid;
j++;
}
qsort(candidates, numRels, sizeof(SelfJoinCandidate),
self_join_candidates_cmp);
/*
* Iteratively form a group of relation indexes with the same oid and
* launch the routine that detects self-joins in this group and removes
* excessive range table entries.
*
* At the end of the iteration, exclude the group from the overall relids
* list. So each next iteration of the cycle will involve less and less
* value of relids.
*/
i = 0;
for (j = 1; j < numRels + 1; j++)
{
if (j == numRels || candidates[j].reloid != candidates[i].reloid)
{
if (j - i >= 2)
{
/* Create a group of relation indexes with the same oid */
Relids group = NULL;
Relids removed;
while (i < j)
{
group = bms_add_member(group, candidates[i].relid);
i++;
}
relids = bms_del_members(relids, group);
/*
* Try to remove self-joins from a group of identical entries.
* Make the next attempt iteratively - if something is deleted
* from a group, changes in clauses and equivalence classes
* can give us a chance to find more candidates.
*/
do
{
Assert(!bms_overlap(group, toRemove));
removed = remove_self_joins_one_group(root, group);
toRemove = bms_add_members(toRemove, removed);
group = bms_del_members(group, removed);
} while (!bms_is_empty(removed) &&
bms_membership(group) == BMS_MULTIPLE);
bms_free(removed);
bms_free(group);
}
else
{
/* Single relation, just remove it from the set */
relids = bms_del_member(relids, candidates[i].relid);
i = j;
}
}
}
Assert(bms_is_empty(relids));
return toRemove;
}
/*
* Compare self-join candidates by their oids.
*/
static int
self_join_candidates_cmp(const void *a, const void *b)
{
const SelfJoinCandidate *ca = (const SelfJoinCandidate *) a;
const SelfJoinCandidate *cb = (const SelfJoinCandidate *) b;
if (ca->reloid != cb->reloid)
return (ca->reloid < cb->reloid ? -1 : 1);
else
return 0;
}
/*
* Find and remove useless self joins.
*
* Search for joins where a relation is joined to itself. If the join clause
* for each tuple from one side of the join is proven to match the same
* physical row (or nothing) on the other side, that self-join can be
* eliminated from the query. Suitable join clauses are assumed to be in the
* form of X = X, and can be replaced with NOT NULL clauses.
*
* For the sake of simplicity, we don't apply this optimization to special
* joins. Here is a list of what we could do in some particular cases:
* 'a a1 semi join a a2': is reduced to inner by reduce_unique_semijoins,
* and then removed normally.
* 'a a1 anti join a a2': could simplify to a scan with 'outer quals AND
* (IS NULL on join columns OR NOT inner quals)'.
* 'a a1 left join a a2': could simplify to a scan like inner but without
* NOT NULL conditions on join columns.
* 'a a1 left join (a a2 join b)': can't simplify this, because join to b
* can both remove rows and introduce duplicates.
*
* To search for removable joins, we order all the relations on their Oid,
* go over each set with the same Oid, and consider each pair of relations
* in this set.
*
* To remove the join, we mark one of the participating relations as dead
* and rewrite all references to it to point to the remaining relation.
* This includes modifying RestrictInfos, EquivalenceClasses, and
* EquivalenceMembers. We also have to modify the row marks. The join clauses
* of the removed relation become either restriction or join clauses, based on
* whether they reference any relations not participating in the removed join.
*
* 'targetlist' is the top-level targetlist of the query. If it has any
* references to the removed relations, we update them to point to the
* remaining ones.
*/
List *
remove_useless_self_joins(PlannerInfo *root, List *joinlist)
{
Relids toRemove = NULL;
int relid = -1;
if (!enable_self_join_removal || joinlist == NIL ||
(list_length(joinlist) == 1 && !IsA(linitial(joinlist), List)))
return joinlist;
/*
* Merge pairs of relations participated in self-join. Remove unnecessary
* range table entries.
*/
toRemove = remove_self_joins_recurse(root, joinlist, toRemove);
if (unlikely(toRemove != NULL))
{
int nremoved = 0;
/* At the end, remove orphaned relation links */
while ((relid = bms_next_member(toRemove, relid)) >= 0)
joinlist = remove_rel_from_joinlist(joinlist, relid, &nremoved);
}
return joinlist;
}
|