PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
joinrels.c File Reference
#include "postgres.h"
#include "miscadmin.h"
#include "optimizer/appendinfo.h"
#include "optimizer/cost.h"
#include "optimizer/joininfo.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/planner.h"
#include "partitioning/partbounds.h"
#include "utils/memutils.h"
Include dependency graph for joinrels.c:

Go to the source code of this file.

Functions

static void make_rels_by_clause_joins (PlannerInfo *root, RelOptInfo *old_rel, List *other_rels, int first_rel_idx)
 
static void make_rels_by_clauseless_joins (PlannerInfo *root, RelOptInfo *old_rel, List *other_rels)
 
static bool has_join_restriction (PlannerInfo *root, RelOptInfo *rel)
 
static bool has_legal_joinclause (PlannerInfo *root, RelOptInfo *rel)
 
static bool restriction_is_constant_false (List *restrictlist, RelOptInfo *joinrel, bool only_pushed_down)
 
static void make_grouped_join_rel (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *sjinfo, List *restrictlist)
 
static void populate_joinrel_with_paths (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *sjinfo, List *restrictlist)
 
static void try_partitionwise_join (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, List *parent_restrictlist)
 
static SpecialJoinInfobuild_child_join_sjinfo (PlannerInfo *root, SpecialJoinInfo *parent_sjinfo, Relids left_relids, Relids right_relids)
 
static void free_child_join_sjinfo (SpecialJoinInfo *child_sjinfo, SpecialJoinInfo *parent_sjinfo)
 
static void compute_partition_bounds (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, List **parts1, List **parts2)
 
static void get_matching_part_pairs (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, List **parts1, List **parts2)
 
void join_search_one_level (PlannerInfo *root, int level)
 
static bool join_is_legal (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, Relids joinrelids, SpecialJoinInfo **sjinfo_p, bool *reversed_p)
 
void init_dummy_sjinfo (SpecialJoinInfo *sjinfo, Relids left_relids, Relids right_relids)
 
RelOptInfomake_join_rel (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 
Relids add_outer_joins_to_relids (PlannerInfo *root, Relids input_relids, SpecialJoinInfo *sjinfo, List **pushed_down_joins)
 
bool have_join_order_restriction (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 
bool is_dummy_rel (RelOptInfo *rel)
 
void mark_dummy_rel (RelOptInfo *rel)
 

Function Documentation

◆ add_outer_joins_to_relids()

Relids add_outer_joins_to_relids ( PlannerInfo root,
Relids  input_relids,
SpecialJoinInfo sjinfo,
List **  pushed_down_joins 
)

Definition at line 800 of file joinrels.c.

803{
804 /* Nothing to do if this isn't an outer join with an assigned relid. */
805 if (sjinfo == NULL || sjinfo->ojrelid == 0)
806 return input_relids;
807
808 /*
809 * If it's not a left join, we have no rules that would permit executing
810 * it in non-syntactic order, so just form the syntactic relid set. (This
811 * is just a quick-exit test; we'd come to the same conclusion anyway,
812 * since its commute_below_l and commute_above_l sets must be empty.)
813 */
814 if (sjinfo->jointype != JOIN_LEFT)
815 return bms_add_member(input_relids, sjinfo->ojrelid);
816
817 /*
818 * We cannot add the OJ relid if this join has been pushed into the RHS of
819 * a syntactically-lower left join per OJ identity 3. (If it has, then we
820 * cannot claim that its outputs represent the final state of its RHS.)
821 * There will not be any other OJs that can be added either, so we're
822 * done.
823 */
824 if (!bms_is_subset(sjinfo->commute_below_l, input_relids))
825 return input_relids;
826
827 /* OK to add OJ's own relid */
828 input_relids = bms_add_member(input_relids, sjinfo->ojrelid);
829
830 /*
831 * Contrariwise, if we are now forming the final result of such a commuted
832 * pair of OJs, it's time to add the relid(s) of the pushed-down join(s).
833 * We can skip this if this join was never a candidate to be pushed up.
834 */
835 if (sjinfo->commute_above_l)
836 {
837 Relids commute_above_rels = bms_copy(sjinfo->commute_above_l);
838 ListCell *lc;
839
840 /*
841 * The current join could complete the nulling of more than one
842 * pushed-down join, so we have to examine all the SpecialJoinInfos.
843 * Because join_info_list was built in bottom-up order, it's
844 * sufficient to traverse it once: an ojrelid we add in one loop
845 * iteration would not have affected decisions of earlier iterations.
846 */
847 foreach(lc, root->join_info_list)
848 {
849 SpecialJoinInfo *othersj = (SpecialJoinInfo *) lfirst(lc);
850
851 if (othersj == sjinfo ||
852 othersj->ojrelid == 0 || othersj->jointype != JOIN_LEFT)
853 continue; /* definitely not interesting */
854
855 if (!bms_is_member(othersj->ojrelid, commute_above_rels))
856 continue;
857
858 /* Add it if not already present but conditions now satisfied */
859 if (!bms_is_member(othersj->ojrelid, input_relids) &&
860 bms_is_subset(othersj->min_lefthand, input_relids) &&
861 bms_is_subset(othersj->min_righthand, input_relids) &&
862 bms_is_subset(othersj->commute_below_l, input_relids))
863 {
864 input_relids = bms_add_member(input_relids, othersj->ojrelid);
865 /* report such pushed down outer joins, if asked */
866 if (pushed_down_joins != NULL)
867 *pushed_down_joins = lappend(*pushed_down_joins, othersj);
868
869 /*
870 * We must also check any joins that othersj potentially
871 * commutes with. They likewise must appear later in
872 * join_info_list than othersj itself, so we can visit them
873 * later in this loop.
874 */
875 commute_above_rels = bms_add_members(commute_above_rels,
876 othersj->commute_above_l);
877 }
878 }
879 }
880
881 return input_relids;
882}
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:412
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:917
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:122
List * lappend(List *list, void *datum)
Definition: list.c:339
@ JOIN_LEFT
Definition: nodes.h:304
#define lfirst(lc)
Definition: pg_list.h:172
tree ctl root
Definition: radixtree.h:1857
Relids min_righthand
Definition: pathnodes.h:3121
Relids commute_above_l
Definition: pathnodes.h:3126
JoinType jointype
Definition: pathnodes.h:3124
Relids commute_below_l
Definition: pathnodes.h:3128
Relids min_lefthand
Definition: pathnodes.h:3120

References bms_add_member(), bms_add_members(), bms_copy(), bms_is_member(), bms_is_subset(), SpecialJoinInfo::commute_above_l, SpecialJoinInfo::commute_below_l, JOIN_LEFT, SpecialJoinInfo::jointype, lappend(), lfirst, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, SpecialJoinInfo::ojrelid, and root.

Referenced by generate_join_implied_equalities(), and make_join_rel().

◆ build_child_join_sjinfo()

static SpecialJoinInfo * build_child_join_sjinfo ( PlannerInfo root,
SpecialJoinInfo parent_sjinfo,
Relids  left_relids,
Relids  right_relids 
)
static

Definition at line 1837 of file joinrels.c.

1839{
1841 AppendRelInfo **left_appinfos;
1842 int left_nappinfos;
1843 AppendRelInfo **right_appinfos;
1844 int right_nappinfos;
1845
1846 /* Dummy SpecialJoinInfos can be created without any translation. */
1847 if (parent_sjinfo->jointype == JOIN_INNER)
1848 {
1849 Assert(parent_sjinfo->ojrelid == 0);
1850 init_dummy_sjinfo(sjinfo, left_relids, right_relids);
1851 return sjinfo;
1852 }
1853
1854 memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
1855 left_appinfos = find_appinfos_by_relids(root, left_relids,
1856 &left_nappinfos);
1857 right_appinfos = find_appinfos_by_relids(root, right_relids,
1858 &right_nappinfos);
1859
1861 left_nappinfos, left_appinfos);
1863 right_nappinfos,
1864 right_appinfos);
1866 left_nappinfos, left_appinfos);
1868 right_nappinfos,
1869 right_appinfos);
1870 /* outer-join relids need no adjustment */
1872 (Node *) sjinfo->semi_rhs_exprs,
1873 right_nappinfos,
1874 right_appinfos);
1875
1876 pfree(left_appinfos);
1877 pfree(right_appinfos);
1878
1879 return sjinfo;
1880}
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:804
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:200
Relids adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:625
Assert(PointerIsAligned(start, uint64))
void init_dummy_sjinfo(SpecialJoinInfo *sjinfo, Relids left_relids, Relids right_relids)
Definition: joinrels.c:664
void pfree(void *pointer)
Definition: mcxt.c:1594
#define makeNode(_type_)
Definition: nodes.h:161
@ JOIN_INNER
Definition: nodes.h:303
Definition: pg_list.h:54
Definition: nodes.h:135
Relids syn_lefthand
Definition: pathnodes.h:3122
List * semi_rhs_exprs
Definition: pathnodes.h:3135
Relids syn_righthand
Definition: pathnodes.h:3123

References adjust_appendrel_attrs(), adjust_child_relids(), Assert(), find_appinfos_by_relids(), init_dummy_sjinfo(), JOIN_INNER, SpecialJoinInfo::jointype, makeNode, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, SpecialJoinInfo::ojrelid, pfree(), root, SpecialJoinInfo::semi_rhs_exprs, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by try_partitionwise_join().

◆ compute_partition_bounds()

static void compute_partition_bounds ( PlannerInfo root,
RelOptInfo rel1,
RelOptInfo rel2,
RelOptInfo joinrel,
SpecialJoinInfo parent_sjinfo,
List **  parts1,
List **  parts2 
)
static

Definition at line 1933 of file joinrels.c.

1937{
1938 /*
1939 * If we don't have the partition bounds for the join rel yet, try to
1940 * compute those along with pairs of partitions to be joined.
1941 */
1942 if (joinrel->nparts == -1)
1943 {
1944 PartitionScheme part_scheme = joinrel->part_scheme;
1945 PartitionBoundInfo boundinfo = NULL;
1946 int nparts = 0;
1947
1948 Assert(joinrel->boundinfo == NULL);
1949 Assert(joinrel->part_rels == NULL);
1950
1951 /*
1952 * See if the partition bounds for inputs are exactly the same, in
1953 * which case we don't need to work hard: the join rel will have the
1954 * same partition bounds as inputs, and the partitions with the same
1955 * cardinal positions will form the pairs.
1956 *
1957 * Note: even in cases where one or both inputs have merged bounds, it
1958 * would be possible for both the bounds to be exactly the same, but
1959 * it seems unlikely to be worth the cycles to check.
1960 */
1961 if (!rel1->partbounds_merged &&
1962 !rel2->partbounds_merged &&
1963 rel1->nparts == rel2->nparts &&
1964 partition_bounds_equal(part_scheme->partnatts,
1965 part_scheme->parttyplen,
1966 part_scheme->parttypbyval,
1967 rel1->boundinfo, rel2->boundinfo))
1968 {
1969 boundinfo = rel1->boundinfo;
1970 nparts = rel1->nparts;
1971 }
1972 else
1973 {
1974 /* Try merging the partition bounds for inputs. */
1975 boundinfo = partition_bounds_merge(part_scheme->partnatts,
1976 part_scheme->partsupfunc,
1977 part_scheme->partcollation,
1978 rel1, rel2,
1979 parent_sjinfo->jointype,
1980 parts1, parts2);
1981 if (boundinfo == NULL)
1982 {
1983 joinrel->nparts = 0;
1984 return;
1985 }
1986 nparts = list_length(*parts1);
1987 joinrel->partbounds_merged = true;
1988 }
1989
1990 Assert(nparts > 0);
1991 joinrel->boundinfo = boundinfo;
1992 joinrel->nparts = nparts;
1993 joinrel->part_rels =
1994 (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * nparts);
1995 }
1996 else
1997 {
1998 Assert(joinrel->nparts > 0);
1999 Assert(joinrel->boundinfo);
2000 Assert(joinrel->part_rels);
2001
2002 /*
2003 * If the join rel's partbounds_merged flag is true, it means inputs
2004 * are not guaranteed to have the same partition bounds, therefore we
2005 * can't assume that the partitions at the same cardinal positions
2006 * form the pairs; let get_matching_part_pairs() generate the pairs.
2007 * Otherwise, nothing to do since we can assume that.
2008 */
2009 if (joinrel->partbounds_merged)
2010 {
2011 get_matching_part_pairs(root, joinrel, rel1, rel2,
2012 parts1, parts2);
2013 Assert(list_length(*parts1) == joinrel->nparts);
2014 Assert(list_length(*parts2) == joinrel->nparts);
2015 }
2016 }
2017}
static void get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, List **parts1, List **parts2)
Definition: joinrels.c:2024
void * palloc0(Size size)
Definition: mcxt.c:1395
bool partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval, PartitionBoundInfo b1, PartitionBoundInfo b2)
Definition: partbounds.c:896
PartitionBoundInfo partition_bounds_merge(int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype, List **outer_parts, List **inner_parts)
Definition: partbounds.c:1119
static int list_length(const List *l)
Definition: pg_list.h:152
struct FmgrInfo * partsupfunc
Definition: pathnodes.h:644
bool partbounds_merged
Definition: pathnodes.h:1097

References Assert(), get_matching_part_pairs(), SpecialJoinInfo::jointype, list_length(), RelOptInfo::nparts, palloc0(), RelOptInfo::partbounds_merged, PartitionSchemeData::partcollation, partition_bounds_equal(), partition_bounds_merge(), PartitionSchemeData::partnatts, PartitionSchemeData::partsupfunc, PartitionSchemeData::parttypbyval, PartitionSchemeData::parttyplen, and root.

Referenced by try_partitionwise_join().

◆ free_child_join_sjinfo()

static void free_child_join_sjinfo ( SpecialJoinInfo child_sjinfo,
SpecialJoinInfo parent_sjinfo 
)
static

Definition at line 1891 of file joinrels.c.

1893{
1894 /*
1895 * Dummy SpecialJoinInfos of inner joins do not have any translated fields
1896 * and hence no fields that to be freed.
1897 */
1898 if (child_sjinfo->jointype != JOIN_INNER)
1899 {
1900 if (child_sjinfo->min_lefthand != parent_sjinfo->min_lefthand)
1901 bms_free(child_sjinfo->min_lefthand);
1902
1903 if (child_sjinfo->min_righthand != parent_sjinfo->min_righthand)
1904 bms_free(child_sjinfo->min_righthand);
1905
1906 if (child_sjinfo->syn_lefthand != parent_sjinfo->syn_lefthand)
1907 bms_free(child_sjinfo->syn_lefthand);
1908
1909 if (child_sjinfo->syn_righthand != parent_sjinfo->syn_righthand)
1910 bms_free(child_sjinfo->syn_righthand);
1911
1912 Assert(child_sjinfo->commute_above_l == parent_sjinfo->commute_above_l);
1913 Assert(child_sjinfo->commute_above_r == parent_sjinfo->commute_above_r);
1914 Assert(child_sjinfo->commute_below_l == parent_sjinfo->commute_below_l);
1915 Assert(child_sjinfo->commute_below_r == parent_sjinfo->commute_below_r);
1916
1917 Assert(child_sjinfo->semi_operators == parent_sjinfo->semi_operators);
1918
1919 /*
1920 * semi_rhs_exprs may in principle be freed, but a simple pfree() does
1921 * not suffice, so we leave it alone.
1922 */
1923 }
1924
1925 pfree(child_sjinfo);
1926}
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
Relids commute_above_r
Definition: pathnodes.h:3127
Relids commute_below_r
Definition: pathnodes.h:3129
List * semi_operators
Definition: pathnodes.h:3134

References Assert(), bms_free(), SpecialJoinInfo::commute_above_l, SpecialJoinInfo::commute_above_r, SpecialJoinInfo::commute_below_l, SpecialJoinInfo::commute_below_r, JOIN_INNER, SpecialJoinInfo::jointype, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, pfree(), SpecialJoinInfo::semi_operators, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by try_partitionwise_join().

◆ get_matching_part_pairs()

static void get_matching_part_pairs ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo rel1,
RelOptInfo rel2,
List **  parts1,
List **  parts2 
)
static

Definition at line 2024 of file joinrels.c.

2027{
2028 bool rel1_is_simple = IS_SIMPLE_REL(rel1);
2029 bool rel2_is_simple = IS_SIMPLE_REL(rel2);
2030 int cnt_parts;
2031
2032 *parts1 = NIL;
2033 *parts2 = NIL;
2034
2035 for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
2036 {
2037 RelOptInfo *child_joinrel = joinrel->part_rels[cnt_parts];
2038 RelOptInfo *child_rel1;
2039 RelOptInfo *child_rel2;
2040 Relids child_relids1;
2041 Relids child_relids2;
2042
2043 /*
2044 * If this segment of the join is empty, it means that this segment
2045 * was ignored when previously creating child-join paths for it in
2046 * try_partitionwise_join() as it would not contribute to the join
2047 * result, due to one or both inputs being empty; add NULL to each of
2048 * the given lists so that this segment will be ignored again in that
2049 * function.
2050 */
2051 if (!child_joinrel)
2052 {
2053 *parts1 = lappend(*parts1, NULL);
2054 *parts2 = lappend(*parts2, NULL);
2055 continue;
2056 }
2057
2058 /*
2059 * Get a relids set of partition(s) involved in this join segment that
2060 * are from the rel1 side.
2061 */
2062 child_relids1 = bms_intersect(child_joinrel->relids,
2063 rel1->all_partrels);
2064 Assert(bms_num_members(child_relids1) == bms_num_members(rel1->relids));
2065
2066 /*
2067 * Get a child rel for rel1 with the relids. Note that we should have
2068 * the child rel even if rel1 is a join rel, because in that case the
2069 * partitions specified in the relids would have matching/overlapping
2070 * boundaries, so the specified partitions should be considered as
2071 * ones to be joined when planning partitionwise joins of rel1,
2072 * meaning that the child rel would have been built by the time we get
2073 * here.
2074 */
2075 if (rel1_is_simple)
2076 {
2077 int varno = bms_singleton_member(child_relids1);
2078
2079 child_rel1 = find_base_rel(root, varno);
2080 }
2081 else
2082 child_rel1 = find_join_rel(root, child_relids1);
2083 Assert(child_rel1);
2084
2085 /*
2086 * Get a relids set of partition(s) involved in this join segment that
2087 * are from the rel2 side.
2088 */
2089 child_relids2 = bms_intersect(child_joinrel->relids,
2090 rel2->all_partrels);
2091 Assert(bms_num_members(child_relids2) == bms_num_members(rel2->relids));
2092
2093 /*
2094 * Get a child rel for rel2 with the relids. See above comments.
2095 */
2096 if (rel2_is_simple)
2097 {
2098 int varno = bms_singleton_member(child_relids2);
2099
2100 child_rel2 = find_base_rel(root, varno);
2101 }
2102 else
2103 child_rel2 = find_join_rel(root, child_relids2);
2104 Assert(child_rel2);
2105
2106 /*
2107 * The join of rel1 and rel2 is legal, so is the join of the child
2108 * rels obtained above; add them to the given lists as a join pair
2109 * producing this join segment.
2110 */
2111 *parts1 = lappend(*parts1, child_rel1);
2112 *parts2 = lappend(*parts2, child_rel2);
2113 }
2114}
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:292
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:672
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:751
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:898
#define NIL
Definition: pg_list.h:68
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:529
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:642
Relids relids
Definition: pathnodes.h:930
Relids all_partrels
Definition: pathnodes.h:1113

References RelOptInfo::all_partrels, Assert(), bms_intersect(), bms_num_members(), bms_singleton_member(), find_base_rel(), find_join_rel(), IS_SIMPLE_REL, lappend(), NIL, RelOptInfo::nparts, RelOptInfo::relids, and root.

Referenced by compute_partition_bounds().

◆ has_join_restriction()

static bool has_join_restriction ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 1367 of file joinrels.c.

1368{
1369 ListCell *l;
1370
1371 if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
1372 return true;
1373
1374 foreach(l, root->placeholder_list)
1375 {
1376 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1377
1378 if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
1379 !bms_equal(rel->relids, phinfo->ph_eval_at))
1380 return true;
1381 }
1382
1383 foreach(l, root->join_info_list)
1384 {
1385 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1386
1387 /* ignore full joins --- other mechanisms preserve their ordering */
1388 if (sjinfo->jointype == JOIN_FULL)
1389 continue;
1390
1391 /* ignore if SJ is already contained in rel */
1392 if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
1393 bms_is_subset(sjinfo->min_righthand, rel->relids))
1394 continue;
1395
1396 /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
1397 if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
1398 bms_overlap(sjinfo->min_righthand, rel->relids))
1399 return true;
1400 }
1401
1402 return false;
1403}
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:142
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:582
@ JOIN_FULL
Definition: nodes.h:305
Relids ph_eval_at
Definition: pathnodes.h:3314
Relids lateral_relids
Definition: pathnodes.h:971
Relids lateral_referencers
Definition: pathnodes.h:996

References bms_equal(), bms_is_subset(), bms_overlap(), JOIN_FULL, SpecialJoinInfo::jointype, RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, lfirst, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, PlaceHolderInfo::ph_eval_at, RelOptInfo::relids, and root.

Referenced by join_search_one_level().

◆ has_legal_joinclause()

static bool has_legal_joinclause ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 1423 of file joinrels.c.

1424{
1425 ListCell *lc;
1426
1427 foreach(lc, root->initial_rels)
1428 {
1429 RelOptInfo *rel2 = (RelOptInfo *) lfirst(lc);
1430
1431 /* ignore rels that are already in "rel" */
1432 if (bms_overlap(rel->relids, rel2->relids))
1433 continue;
1434
1435 if (have_relevant_joinclause(root, rel, rel2))
1436 {
1437 Relids joinrelids;
1438 SpecialJoinInfo *sjinfo;
1439 bool reversed;
1440
1441 /* join_is_legal needs relids of the union */
1442 joinrelids = bms_union(rel->relids, rel2->relids);
1443
1444 if (join_is_legal(root, rel, rel2, joinrelids,
1445 &sjinfo, &reversed))
1446 {
1447 /* Yes, this will work */
1448 bms_free(joinrelids);
1449 return true;
1450 }
1451
1452 bms_free(joinrelids);
1453 }
1454 }
1455
1456 return false;
1457}
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:251
bool have_relevant_joinclause(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joininfo.c:39
static bool join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, Relids joinrelids, SpecialJoinInfo **sjinfo_p, bool *reversed_p)
Definition: joinrels.c:355

References bms_free(), bms_overlap(), bms_union(), have_relevant_joinclause(), join_is_legal(), lfirst, RelOptInfo::relids, and root.

Referenced by have_join_order_restriction().

◆ have_join_order_restriction()

bool have_join_order_restriction ( PlannerInfo root,
RelOptInfo rel1,
RelOptInfo rel2 
)

Definition at line 1254 of file joinrels.c.

1256{
1257 bool result = false;
1258 ListCell *l;
1259
1260 /*
1261 * If either side has a direct lateral reference to the other, attempt the
1262 * join regardless of outer-join considerations.
1263 */
1264 if (bms_overlap(rel1->relids, rel2->direct_lateral_relids) ||
1266 return true;
1267
1268 /*
1269 * Likewise, if both rels are needed to compute some PlaceHolderVar,
1270 * attempt the join regardless of outer-join considerations. (This is not
1271 * very desirable, because a PHV with a large eval_at set will cause a lot
1272 * of probably-useless joins to be considered, but failing to do this can
1273 * cause us to fail to construct a plan at all.)
1274 */
1275 foreach(l, root->placeholder_list)
1276 {
1277 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1278
1279 if (bms_is_subset(rel1->relids, phinfo->ph_eval_at) &&
1280 bms_is_subset(rel2->relids, phinfo->ph_eval_at))
1281 return true;
1282 }
1283
1284 /*
1285 * It's possible that the rels correspond to the left and right sides of a
1286 * degenerate outer join, that is, one with no joinclause mentioning the
1287 * non-nullable side; in which case we should force the join to occur.
1288 *
1289 * Also, the two rels could represent a clauseless join that has to be
1290 * completed to build up the LHS or RHS of an outer join.
1291 */
1292 foreach(l, root->join_info_list)
1293 {
1294 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1295
1296 /* ignore full joins --- other mechanisms handle them */
1297 if (sjinfo->jointype == JOIN_FULL)
1298 continue;
1299
1300 /* Can we perform the SJ with these rels? */
1301 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
1302 bms_is_subset(sjinfo->min_righthand, rel2->relids))
1303 {
1304 result = true;
1305 break;
1306 }
1307 if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
1308 bms_is_subset(sjinfo->min_righthand, rel1->relids))
1309 {
1310 result = true;
1311 break;
1312 }
1313
1314 /*
1315 * Might we need to join these rels to complete the RHS? We have to
1316 * use "overlap" tests since either rel might include a lower SJ that
1317 * has been proven to commute with this one.
1318 */
1319 if (bms_overlap(sjinfo->min_righthand, rel1->relids) &&
1320 bms_overlap(sjinfo->min_righthand, rel2->relids))
1321 {
1322 result = true;
1323 break;
1324 }
1325
1326 /* Likewise for the LHS. */
1327 if (bms_overlap(sjinfo->min_lefthand, rel1->relids) &&
1328 bms_overlap(sjinfo->min_lefthand, rel2->relids))
1329 {
1330 result = true;
1331 break;
1332 }
1333 }
1334
1335 /*
1336 * We do not force the join to occur if either input rel can legally be
1337 * joined to anything else using joinclauses. This essentially means that
1338 * clauseless bushy joins are put off as long as possible. The reason is
1339 * that when there is a join order restriction high up in the join tree
1340 * (that is, with many rels inside the LHS or RHS), we would otherwise
1341 * expend lots of effort considering very stupid join combinations within
1342 * its LHS or RHS.
1343 */
1344 if (result)
1345 {
1346 if (has_legal_joinclause(root, rel1) ||
1348 result = false;
1349 }
1350
1351 return result;
1352}
static bool has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel)
Definition: joinrels.c:1423
Relids direct_lateral_relids
Definition: pathnodes.h:969

References bms_is_subset(), bms_overlap(), RelOptInfo::direct_lateral_relids, has_legal_joinclause(), JOIN_FULL, SpecialJoinInfo::jointype, lfirst, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, PlaceHolderInfo::ph_eval_at, RelOptInfo::relids, and root.

Referenced by desirable_join(), join_search_one_level(), and make_rels_by_clause_joins().

◆ init_dummy_sjinfo()

void init_dummy_sjinfo ( SpecialJoinInfo sjinfo,
Relids  left_relids,
Relids  right_relids 
)

Definition at line 664 of file joinrels.c.

666{
667 sjinfo->type = T_SpecialJoinInfo;
668 sjinfo->min_lefthand = left_relids;
669 sjinfo->min_righthand = right_relids;
670 sjinfo->syn_lefthand = left_relids;
671 sjinfo->syn_righthand = right_relids;
672 sjinfo->jointype = JOIN_INNER;
673 sjinfo->ojrelid = 0;
674 sjinfo->commute_above_l = NULL;
675 sjinfo->commute_above_r = NULL;
676 sjinfo->commute_below_l = NULL;
677 sjinfo->commute_below_r = NULL;
678 /* we don't bother trying to make the remaining fields valid */
679 sjinfo->lhs_strict = false;
680 sjinfo->semi_can_btree = false;
681 sjinfo->semi_can_hash = false;
682 sjinfo->semi_operators = NIL;
683 sjinfo->semi_rhs_exprs = NIL;
684}

References SpecialJoinInfo::commute_above_l, SpecialJoinInfo::commute_above_r, SpecialJoinInfo::commute_below_l, SpecialJoinInfo::commute_below_r, JOIN_INNER, SpecialJoinInfo::jointype, SpecialJoinInfo::lhs_strict, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, NIL, SpecialJoinInfo::ojrelid, SpecialJoinInfo::semi_can_btree, SpecialJoinInfo::semi_can_hash, SpecialJoinInfo::semi_operators, SpecialJoinInfo::semi_rhs_exprs, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by approx_tuple_count(), build_child_join_sjinfo(), compute_semi_anti_join_factors(), consider_new_or_clause(), and make_join_rel().

◆ is_dummy_rel()

bool is_dummy_rel ( RelOptInfo rel)

Definition at line 1464 of file joinrels.c.

1465{
1466 Path *path;
1467
1468 /*
1469 * A rel that is known dummy will have just one path that is a childless
1470 * Append. (Even if somehow it has more paths, a childless Append will
1471 * have cost zero and hence should be at the front of the pathlist.)
1472 */
1473 if (rel->pathlist == NIL)
1474 return false;
1475 path = (Path *) linitial(rel->pathlist);
1476
1477 /*
1478 * Initially, a dummy path will just be a childless Append. But in later
1479 * planning stages we might stick a ProjectSetPath and/or ProjectionPath
1480 * on top, since Append can't project. Rather than make assumptions about
1481 * which combinations can occur, just descend through whatever we find.
1482 */
1483 for (;;)
1484 {
1485 if (IsA(path, ProjectionPath))
1486 path = ((ProjectionPath *) path)->subpath;
1487 else if (IsA(path, ProjectSetPath))
1488 path = ((ProjectSetPath *) path)->subpath;
1489 else
1490 break;
1491 }
1492 if (IS_DUMMY_APPEND(path))
1493 return true;
1494 return false;
1495}
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:311
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define IS_DUMMY_APPEND(p)
Definition: pathnodes.h:2188
#define linitial(l)
Definition: pg_list.h:178
List * pathlist
Definition: pathnodes.h:957

References IS_DUMMY_APPEND, IsA, linitial, NIL, RelOptInfo::pathlist, and subpath().

Referenced by build_setop_child_paths(), generate_nonunion_paths(), generate_union_paths(), make_join_rel(), mark_dummy_rel(), and populate_joinrel_with_paths().

◆ join_is_legal()

static bool join_is_legal ( PlannerInfo root,
RelOptInfo rel1,
RelOptInfo rel2,
Relids  joinrelids,
SpecialJoinInfo **  sjinfo_p,
bool *  reversed_p 
)
static

Definition at line 355 of file joinrels.c.

358{
359 SpecialJoinInfo *match_sjinfo;
360 bool reversed;
361 bool unique_ified;
362 bool must_be_leftjoin;
363 ListCell *l;
364
365 /*
366 * Ensure output params are set on failure return. This is just to
367 * suppress uninitialized-variable warnings from overly anal compilers.
368 */
369 *sjinfo_p = NULL;
370 *reversed_p = false;
371
372 /*
373 * If we have any special joins, the proposed join might be illegal; and
374 * in any case we have to determine its join type. Scan the join info
375 * list for matches and conflicts.
376 */
377 match_sjinfo = NULL;
378 reversed = false;
379 unique_ified = false;
380 must_be_leftjoin = false;
381
382 foreach(l, root->join_info_list)
383 {
384 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
385
386 /*
387 * This special join is not relevant unless its RHS overlaps the
388 * proposed join. (Check this first as a fast path for dismissing
389 * most irrelevant SJs quickly.)
390 */
391 if (!bms_overlap(sjinfo->min_righthand, joinrelids))
392 continue;
393
394 /*
395 * Also, not relevant if proposed join is fully contained within RHS
396 * (ie, we're still building up the RHS).
397 */
398 if (bms_is_subset(joinrelids, sjinfo->min_righthand))
399 continue;
400
401 /*
402 * Also, not relevant if SJ is already done within either input.
403 */
404 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
405 bms_is_subset(sjinfo->min_righthand, rel1->relids))
406 continue;
407 if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
408 bms_is_subset(sjinfo->min_righthand, rel2->relids))
409 continue;
410
411 /*
412 * If it's a semijoin and we already joined the RHS to any other rels
413 * within either input, then we must have unique-ified the RHS at that
414 * point (see below). Therefore the semijoin is no longer relevant in
415 * this join path.
416 */
417 if (sjinfo->jointype == JOIN_SEMI)
418 {
419 if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
420 !bms_equal(sjinfo->syn_righthand, rel1->relids))
421 continue;
422 if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
423 !bms_equal(sjinfo->syn_righthand, rel2->relids))
424 continue;
425 }
426
427 /*
428 * If one input contains min_lefthand and the other contains
429 * min_righthand, then we can perform the SJ at this join.
430 *
431 * Reject if we get matches to more than one SJ; that implies we're
432 * considering something that's not really valid.
433 */
434 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
435 bms_is_subset(sjinfo->min_righthand, rel2->relids))
436 {
437 if (match_sjinfo)
438 return false; /* invalid join path */
439 match_sjinfo = sjinfo;
440 reversed = false;
441 }
442 else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
443 bms_is_subset(sjinfo->min_righthand, rel1->relids))
444 {
445 if (match_sjinfo)
446 return false; /* invalid join path */
447 match_sjinfo = sjinfo;
448 reversed = true;
449 }
450 else if (sjinfo->jointype == JOIN_SEMI &&
451 bms_equal(sjinfo->syn_righthand, rel2->relids) &&
452 create_unique_paths(root, rel2, sjinfo) != NULL)
453 {
454 /*----------
455 * For a semijoin, we can join the RHS to anything else by
456 * unique-ifying the RHS (if the RHS can be unique-ified).
457 * We will only get here if we have the full RHS but less
458 * than min_lefthand on the LHS.
459 *
460 * The reason to consider such a join path is exemplified by
461 * SELECT ... FROM a,b WHERE (a.x,b.y) IN (SELECT c1,c2 FROM c)
462 * If we insist on doing this as a semijoin we will first have
463 * to form the cartesian product of A*B. But if we unique-ify
464 * C then the semijoin becomes a plain innerjoin and we can join
465 * in any order, eg C to A and then to B. When C is much smaller
466 * than A and B this can be a huge win. So we allow C to be
467 * joined to just A or just B here, and then make_join_rel has
468 * to handle the case properly.
469 *
470 * Note that actually we'll allow unique-ified C to be joined to
471 * some other relation D here, too. That is legal, if usually not
472 * very sane, and this routine is only concerned with legality not
473 * with whether the join is good strategy.
474 *----------
475 */
476 if (match_sjinfo)
477 return false; /* invalid join path */
478 match_sjinfo = sjinfo;
479 reversed = false;
480 unique_ified = true;
481 }
482 else if (sjinfo->jointype == JOIN_SEMI &&
483 bms_equal(sjinfo->syn_righthand, rel1->relids) &&
484 create_unique_paths(root, rel1, sjinfo) != NULL)
485 {
486 /* Reversed semijoin case */
487 if (match_sjinfo)
488 return false; /* invalid join path */
489 match_sjinfo = sjinfo;
490 reversed = true;
491 unique_ified = true;
492 }
493 else
494 {
495 /*
496 * Otherwise, the proposed join overlaps the RHS but isn't a valid
497 * implementation of this SJ. But don't panic quite yet: the RHS
498 * violation might have occurred previously, in one or both input
499 * relations, in which case we must have previously decided that
500 * it was OK to commute some other SJ with this one. If we need
501 * to perform this join to finish building up the RHS, rejecting
502 * it could lead to not finding any plan at all. (This can occur
503 * because of the heuristics elsewhere in this file that postpone
504 * clauseless joins: we might not consider doing a clauseless join
505 * within the RHS until after we've performed other, validly
506 * commutable SJs with one or both sides of the clauseless join.)
507 * This consideration boils down to the rule that if both inputs
508 * overlap the RHS, we can allow the join --- they are either
509 * fully within the RHS, or represent previously-allowed joins to
510 * rels outside it.
511 */
512 if (bms_overlap(rel1->relids, sjinfo->min_righthand) &&
513 bms_overlap(rel2->relids, sjinfo->min_righthand))
514 continue; /* assume valid previous violation of RHS */
515
516 /*
517 * The proposed join could still be legal, but only if we're
518 * allowed to associate it into the RHS of this SJ. That means
519 * this SJ must be a LEFT join (not SEMI or ANTI, and certainly
520 * not FULL) and the proposed join must not overlap the LHS.
521 */
522 if (sjinfo->jointype != JOIN_LEFT ||
523 bms_overlap(joinrelids, sjinfo->min_lefthand))
524 return false; /* invalid join path */
525
526 /*
527 * To be valid, the proposed join must be a LEFT join; otherwise
528 * it can't associate into this SJ's RHS. But we may not yet have
529 * found the SpecialJoinInfo matching the proposed join, so we
530 * can't test that yet. Remember the requirement for later.
531 */
532 must_be_leftjoin = true;
533 }
534 }
535
536 /*
537 * Fail if violated any SJ's RHS and didn't match to a LEFT SJ: the
538 * proposed join can't associate into an SJ's RHS.
539 *
540 * Also, fail if the proposed join's predicate isn't strict; we're
541 * essentially checking to see if we can apply outer-join identity 3, and
542 * that's a requirement. (This check may be redundant with checks in
543 * make_outerjoininfo, but I'm not quite sure, and it's cheap to test.)
544 */
545 if (must_be_leftjoin &&
546 (match_sjinfo == NULL ||
547 match_sjinfo->jointype != JOIN_LEFT ||
548 !match_sjinfo->lhs_strict))
549 return false; /* invalid join path */
550
551 /*
552 * We also have to check for constraints imposed by LATERAL references.
553 */
554 if (root->hasLateralRTEs)
555 {
556 bool lateral_fwd;
557 bool lateral_rev;
558 Relids join_lateral_rels;
559
560 /*
561 * The proposed rels could each contain lateral references to the
562 * other, in which case the join is impossible. If there are lateral
563 * references in just one direction, then the join has to be done with
564 * a nestloop with the lateral referencer on the inside. If the join
565 * matches an SJ that cannot be implemented by such a nestloop, the
566 * join is impossible.
567 *
568 * Also, if the lateral reference is only indirect, we should reject
569 * the join; whatever rel(s) the reference chain goes through must be
570 * joined to first.
571 */
572 lateral_fwd = bms_overlap(rel1->relids, rel2->lateral_relids);
573 lateral_rev = bms_overlap(rel2->relids, rel1->lateral_relids);
574 if (lateral_fwd && lateral_rev)
575 return false; /* have lateral refs in both directions */
576 if (lateral_fwd)
577 {
578 /* has to be implemented as nestloop with rel1 on left */
579 if (match_sjinfo &&
580 (reversed ||
581 unique_ified ||
582 match_sjinfo->jointype == JOIN_FULL))
583 return false; /* not implementable as nestloop */
584 /* check there is a direct reference from rel2 to rel1 */
585 if (!bms_overlap(rel1->relids, rel2->direct_lateral_relids))
586 return false; /* only indirect refs, so reject */
587 }
588 else if (lateral_rev)
589 {
590 /* has to be implemented as nestloop with rel2 on left */
591 if (match_sjinfo &&
592 (!reversed ||
593 unique_ified ||
594 match_sjinfo->jointype == JOIN_FULL))
595 return false; /* not implementable as nestloop */
596 /* check there is a direct reference from rel1 to rel2 */
597 if (!bms_overlap(rel2->relids, rel1->direct_lateral_relids))
598 return false; /* only indirect refs, so reject */
599 }
600
601 /*
602 * LATERAL references could also cause problems later on if we accept
603 * this join: if the join's minimum parameterization includes any rels
604 * that would have to be on the inside of an outer join with this join
605 * rel, then it's never going to be possible to build the complete
606 * query using this join. We should reject this join not only because
607 * it'll save work, but because if we don't, the clauseless-join
608 * heuristics might think that legality of this join means that some
609 * other join rel need not be formed, and that could lead to failure
610 * to find any plan at all. We have to consider not only rels that
611 * are directly on the inner side of an OJ with the joinrel, but also
612 * ones that are indirectly so, so search to find all such rels.
613 */
614 join_lateral_rels = min_join_parameterization(root, joinrelids,
615 rel1, rel2);
616 if (join_lateral_rels)
617 {
618 Relids join_plus_rhs = bms_copy(joinrelids);
619 bool more;
620
621 do
622 {
623 more = false;
624 foreach(l, root->join_info_list)
625 {
626 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
627
628 /* ignore full joins --- their ordering is predetermined */
629 if (sjinfo->jointype == JOIN_FULL)
630 continue;
631
632 if (bms_overlap(sjinfo->min_lefthand, join_plus_rhs) &&
633 !bms_is_subset(sjinfo->min_righthand, join_plus_rhs))
634 {
635 join_plus_rhs = bms_add_members(join_plus_rhs,
636 sjinfo->min_righthand);
637 more = true;
638 }
639 }
640 } while (more);
641 if (bms_overlap(join_plus_rhs, join_lateral_rels))
642 return false; /* will not be able to join to some RHS rel */
643 }
644 }
645
646 /* Otherwise, it's a valid join */
647 *sjinfo_p = match_sjinfo;
648 *reversed_p = reversed;
649 return true;
650}
@ JOIN_SEMI
Definition: nodes.h:317
RelOptInfo * create_unique_paths(PlannerInfo *root, RelOptInfo *rel, SpecialJoinInfo *sjinfo)
Definition: planner.c:8392
Relids min_join_parameterization(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1145

References bms_add_members(), bms_copy(), bms_equal(), bms_is_subset(), bms_overlap(), create_unique_paths(), RelOptInfo::direct_lateral_relids, JOIN_FULL, JOIN_LEFT, JOIN_SEMI, SpecialJoinInfo::jointype, RelOptInfo::lateral_relids, lfirst, SpecialJoinInfo::lhs_strict, min_join_parameterization(), SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, RelOptInfo::relids, root, and SpecialJoinInfo::syn_righthand.

Referenced by has_legal_joinclause(), and make_join_rel().

◆ join_search_one_level()

void join_search_one_level ( PlannerInfo root,
int  level 
)

Definition at line 78 of file joinrels.c.

79{
80 List **joinrels = root->join_rel_level;
81 ListCell *r;
82 int k;
83
84 Assert(joinrels[level] == NIL);
85
86 /* Set join_cur_level so that new joinrels are added to proper list */
87 root->join_cur_level = level;
88
89 /*
90 * First, consider left-sided and right-sided plans, in which rels of
91 * exactly level-1 member relations are joined against initial relations.
92 * We prefer to join using join clauses, but if we find a rel of level-1
93 * members that has no join clauses, we will generate Cartesian-product
94 * joins against all initial rels not already contained in it.
95 */
96 foreach(r, joinrels[level - 1])
97 {
98 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
99
100 if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
101 has_join_restriction(root, old_rel))
102 {
103 int first_rel;
104
105 /*
106 * There are join clauses or join order restrictions relevant to
107 * this rel, so consider joins between this rel and (only) those
108 * initial rels it is linked to by a clause or restriction.
109 *
110 * At level 2 this condition is symmetric, so there is no need to
111 * look at initial rels before this one in the list; we already
112 * considered such joins when we were at the earlier rel. (The
113 * mirror-image joins are handled automatically by make_join_rel.)
114 * In later passes (level > 2), we join rels of the previous level
115 * to each initial rel they don't already include but have a join
116 * clause or restriction with.
117 */
118 if (level == 2) /* consider remaining initial rels */
119 first_rel = foreach_current_index(r) + 1;
120 else
121 first_rel = 0;
122
123 make_rels_by_clause_joins(root, old_rel, joinrels[1], first_rel);
124 }
125 else
126 {
127 /*
128 * Oops, we have a relation that is not joined to any other
129 * relation, either directly or by join-order restrictions.
130 * Cartesian product time.
131 *
132 * We consider a cartesian product with each not-already-included
133 * initial rel, whether it has other join clauses or not. At
134 * level 2, if there are two or more clauseless initial rels, we
135 * will redundantly consider joining them in both directions; but
136 * such cases aren't common enough to justify adding complexity to
137 * avoid the duplicated effort.
138 */
140 old_rel,
141 joinrels[1]);
142 }
143 }
144
145 /*
146 * Now, consider "bushy plans" in which relations of k initial rels are
147 * joined to relations of level-k initial rels, for 2 <= k <= level-2.
148 *
149 * We only consider bushy-plan joins for pairs of rels where there is a
150 * suitable join clause (or join order restriction), in order to avoid
151 * unreasonable growth of planning time.
152 */
153 for (k = 2;; k++)
154 {
155 int other_level = level - k;
156
157 /*
158 * Since make_join_rel(x, y) handles both x,y and y,x cases, we only
159 * need to go as far as the halfway point.
160 */
161 if (k > other_level)
162 break;
163
164 foreach(r, joinrels[k])
165 {
166 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
167 int first_rel;
168 ListCell *r2;
169
170 /*
171 * We can ignore relations without join clauses here, unless they
172 * participate in join-order restrictions --- then we might have
173 * to force a bushy join plan.
174 */
175 if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
176 !has_join_restriction(root, old_rel))
177 continue;
178
179 if (k == other_level) /* only consider remaining rels */
180 first_rel = foreach_current_index(r) + 1;
181 else
182 first_rel = 0;
183
184 for_each_from(r2, joinrels[other_level], first_rel)
185 {
186 RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
187
188 if (!bms_overlap(old_rel->relids, new_rel->relids))
189 {
190 /*
191 * OK, we can build a rel of the right level from this
192 * pair of rels. Do so if there is at least one relevant
193 * join clause or join order restriction.
194 */
195 if (have_relevant_joinclause(root, old_rel, new_rel) ||
196 have_join_order_restriction(root, old_rel, new_rel))
197 {
198 (void) make_join_rel(root, old_rel, new_rel);
199 }
200 }
201 }
202 }
203 }
204
205 /*----------
206 * Last-ditch effort: if we failed to find any usable joins so far, force
207 * a set of cartesian-product joins to be generated. This handles the
208 * special case where all the available rels have join clauses but we
209 * cannot use any of those clauses yet. This can only happen when we are
210 * considering a join sub-problem (a sub-joinlist) and all the rels in the
211 * sub-problem have only join clauses with rels outside the sub-problem.
212 * An example is
213 *
214 * SELECT ... FROM a INNER JOIN b ON TRUE, c, d, ...
215 * WHERE a.w = c.x and b.y = d.z;
216 *
217 * If the "a INNER JOIN b" sub-problem does not get flattened into the
218 * upper level, we must be willing to make a cartesian join of a and b;
219 * but the code above will not have done so, because it thought that both
220 * a and b have joinclauses. We consider only left-sided and right-sided
221 * cartesian joins in this case (no bushy).
222 *----------
223 */
224 if (joinrels[level] == NIL)
225 {
226 /*
227 * This loop is just like the first one, except we always call
228 * make_rels_by_clauseless_joins().
229 */
230 foreach(r, joinrels[level - 1])
231 {
232 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
233
235 old_rel,
236 joinrels[1]);
237 }
238
239 /*----------
240 * When special joins are involved, there may be no legal way
241 * to make an N-way join for some values of N. For example consider
242 *
243 * SELECT ... FROM t1 WHERE
244 * x IN (SELECT ... FROM t2,t3 WHERE ...) AND
245 * y IN (SELECT ... FROM t4,t5 WHERE ...)
246 *
247 * We will flatten this query to a 5-way join problem, but there are
248 * no 4-way joins that join_is_legal() will consider legal. We have
249 * to accept failure at level 4 and go on to discover a workable
250 * bushy plan at level 5.
251 *
252 * However, if there are no special joins and no lateral references
253 * then join_is_legal() should never fail, and so the following sanity
254 * check is useful.
255 *----------
256 */
257 if (joinrels[level] == NIL &&
258 root->join_info_list == NIL &&
259 !root->hasLateralRTEs)
260 elog(ERROR, "failed to build any %d-way joins", level);
261 }
262}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
static void make_rels_by_clauseless_joins(PlannerInfo *root, RelOptInfo *old_rel, List *other_rels)
Definition: joinrels.c:319
RelOptInfo * make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joinrels.c:699
bool have_join_order_restriction(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joinrels.c:1254
static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
Definition: joinrels.c:1367
static void make_rels_by_clause_joins(PlannerInfo *root, RelOptInfo *old_rel, List *other_rels, int first_rel_idx)
Definition: joinrels.c:285
#define foreach_current_index(var_or_cell)
Definition: pg_list.h:403
#define for_each_from(cell, lst, N)
Definition: pg_list.h:414
List * joininfo
Definition: pathnodes.h:1055
bool has_eclass_joins
Definition: pathnodes.h:1057

References Assert(), bms_overlap(), elog, ERROR, for_each_from, foreach_current_index, RelOptInfo::has_eclass_joins, has_join_restriction(), have_join_order_restriction(), have_relevant_joinclause(), RelOptInfo::joininfo, lfirst, make_join_rel(), make_rels_by_clause_joins(), make_rels_by_clauseless_joins(), NIL, RelOptInfo::relids, and root.

Referenced by standard_join_search().

◆ make_grouped_join_rel()

static void make_grouped_join_rel ( PlannerInfo root,
RelOptInfo rel1,
RelOptInfo rel2,
RelOptInfo joinrel,
SpecialJoinInfo sjinfo,
List restrictlist 
)
static

Definition at line 916 of file joinrels.c.

919{
920 RelOptInfo *grouped_rel;
921 RelOptInfo *grouped_rel1;
922 RelOptInfo *grouped_rel2;
923 bool rel1_empty;
924 bool rel2_empty;
925 Relids agg_apply_at;
926
927 /*
928 * If there are no aggregate expressions or grouping expressions, eager
929 * aggregation is not possible.
930 */
931 if (root->agg_clause_list == NIL ||
932 root->group_expr_list == NIL)
933 return;
934
935 /* Retrieve the grouped relations for the two input rels */
936 grouped_rel1 = rel1->grouped_rel;
937 grouped_rel2 = rel2->grouped_rel;
938
939 rel1_empty = (grouped_rel1 == NULL || IS_DUMMY_REL(grouped_rel1));
940 rel2_empty = (grouped_rel2 == NULL || IS_DUMMY_REL(grouped_rel2));
941
942 /* Find or construct a grouped joinrel for this joinrel */
943 grouped_rel = joinrel->grouped_rel;
944 if (grouped_rel == NULL)
945 {
946 RelAggInfo *agg_info = NULL;
947
948 /*
949 * Prepare the information needed to create grouped paths for this
950 * join relation.
951 */
952 agg_info = create_rel_agg_info(root, joinrel, rel1_empty == rel2_empty);
953 if (agg_info == NULL)
954 return;
955
956 /*
957 * If grouped paths for the given join relation are not considered
958 * useful, and no grouped paths can be built by joining grouped input
959 * relations, skip building the grouped join relation.
960 */
961 if (!agg_info->agg_useful &&
962 (rel1_empty == rel2_empty))
963 return;
964
965 /* build the grouped relation */
966 grouped_rel = build_grouped_rel(root, joinrel);
967 grouped_rel->reltarget = agg_info->target;
968
969 if (rel1_empty != rel2_empty)
970 {
971 /*
972 * If there is exactly one grouped input relation, then we can
973 * build grouped paths by joining the input relations. Set size
974 * estimates for the grouped join relation based on the input
975 * relations, and update the set of relids where partial
976 * aggregation is applied to that of the grouped input relation.
977 */
978 set_joinrel_size_estimates(root, grouped_rel,
979 rel1_empty ? rel1 : grouped_rel1,
980 rel2_empty ? rel2 : grouped_rel2,
981 sjinfo, restrictlist);
982 agg_info->apply_at = rel1_empty ?
983 grouped_rel2->agg_info->apply_at :
984 grouped_rel1->agg_info->apply_at;
985 }
986 else
987 {
988 /*
989 * Otherwise, grouped paths can be built by applying partial
990 * aggregation on top of existing non-grouped join paths. Set
991 * size estimates for the grouped join relation based on the
992 * estimated number of groups, and track the set of relids where
993 * partial aggregation is applied. Note that these values may be
994 * updated later if it is determined that grouped paths can be
995 * constructed by joining other input relations.
996 */
997 grouped_rel->rows = agg_info->grouped_rows;
998 agg_info->apply_at = bms_copy(joinrel->relids);
999 }
1000
1001 grouped_rel->agg_info = agg_info;
1002 joinrel->grouped_rel = grouped_rel;
1003 }
1004
1005 Assert(IS_GROUPED_REL(grouped_rel));
1006
1007 /* We may have already proven this grouped join relation to be dummy. */
1008 if (IS_DUMMY_REL(grouped_rel))
1009 return;
1010
1011 /*
1012 * Nothing to do if there's no grouped input relation. Also, joining two
1013 * grouped relations is not currently supported.
1014 */
1015 if (rel1_empty == rel2_empty)
1016 return;
1017
1018 /*
1019 * Get the set of relids where partial aggregation is applied among the
1020 * given input relations.
1021 */
1022 agg_apply_at = rel1_empty ?
1023 grouped_rel2->agg_info->apply_at :
1024 grouped_rel1->agg_info->apply_at;
1025
1026 /*
1027 * If it's not the designated level, skip building grouped paths.
1028 *
1029 * One exception is when it is a subset of the previously recorded level.
1030 * In that case, we need to update the designated level to this one, and
1031 * adjust the size estimates for the grouped join relation accordingly.
1032 * For example, suppose partial aggregation can be applied on top of (B
1033 * JOIN C). If we first construct the join as ((A JOIN B) JOIN C), we'd
1034 * record the designated level as including all three relations (A B C).
1035 * Later, when we consider (A JOIN (B JOIN C)), we encounter the smaller
1036 * (B C) join level directly. Since this is a subset of the previous
1037 * level and still valid for partial aggregation, we update the designated
1038 * level to (B C), and adjust the size estimates accordingly.
1039 */
1040 if (!bms_equal(agg_apply_at, grouped_rel->agg_info->apply_at))
1041 {
1042 if (bms_is_subset(agg_apply_at, grouped_rel->agg_info->apply_at))
1043 {
1044 /* Adjust the size estimates for the grouped join relation. */
1045 set_joinrel_size_estimates(root, grouped_rel,
1046 rel1_empty ? rel1 : grouped_rel1,
1047 rel2_empty ? rel2 : grouped_rel2,
1048 sjinfo, restrictlist);
1049 grouped_rel->agg_info->apply_at = agg_apply_at;
1050 }
1051 else
1052 return;
1053 }
1054
1055 /* Make paths for the grouped join relation. */
1057 rel1_empty ? rel1 : grouped_rel1,
1058 rel2_empty ? rel2 : grouped_rel2,
1059 grouped_rel,
1060 sjinfo,
1061 restrictlist);
1062}
void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: costsize.c:5463
static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: joinrels.c:1072
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:2196
#define IS_GROUPED_REL(rel)
Definition: pathnodes.h:1164
RelOptInfo * build_grouped_rel(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:484
RelAggInfo * create_rel_agg_info(PlannerInfo *root, RelOptInfo *rel, bool calculate_grouped_rows)
Definition: relnode.c:2655
bool agg_useful
Definition: pathnodes.h:1215
Cardinality grouped_rows
Definition: pathnodes.h:1212
Relids apply_at
Definition: pathnodes.h:1209
struct PathTarget * target
Definition: pathnodes.h:1198
struct PathTarget * reltarget
Definition: pathnodes.h:952
struct RelAggInfo * agg_info
Definition: pathnodes.h:1069
struct RelOptInfo * grouped_rel
Definition: pathnodes.h:1071
Cardinality rows
Definition: pathnodes.h:936

References RelOptInfo::agg_info, RelAggInfo::agg_useful, RelAggInfo::apply_at, Assert(), bms_copy(), bms_equal(), bms_is_subset(), build_grouped_rel(), create_rel_agg_info(), RelOptInfo::grouped_rel, RelAggInfo::grouped_rows, IS_DUMMY_REL, IS_GROUPED_REL, NIL, populate_joinrel_with_paths(), RelOptInfo::relids, RelOptInfo::reltarget, root, RelOptInfo::rows, set_joinrel_size_estimates(), and RelAggInfo::target.

Referenced by make_join_rel(), and try_partitionwise_join().

◆ make_join_rel()

RelOptInfo * make_join_rel ( PlannerInfo root,
RelOptInfo rel1,
RelOptInfo rel2 
)

Definition at line 699 of file joinrels.c.

700{
701 Relids joinrelids;
702 SpecialJoinInfo *sjinfo;
703 bool reversed;
704 List *pushed_down_joins = NIL;
705 SpecialJoinInfo sjinfo_data;
706 RelOptInfo *joinrel;
707 List *restrictlist;
708
709 /* We should never try to join two overlapping sets of rels. */
710 Assert(!bms_overlap(rel1->relids, rel2->relids));
711
712 /* Construct Relids set that identifies the joinrel (without OJ as yet). */
713 joinrelids = bms_union(rel1->relids, rel2->relids);
714
715 /* Check validity and determine join type. */
716 if (!join_is_legal(root, rel1, rel2, joinrelids,
717 &sjinfo, &reversed))
718 {
719 /* invalid join path */
720 bms_free(joinrelids);
721 return NULL;
722 }
723
724 /*
725 * Add outer join relid(s) to form the canonical relids. Any added outer
726 * joins besides sjinfo itself are appended to pushed_down_joins.
727 */
728 joinrelids = add_outer_joins_to_relids(root, joinrelids, sjinfo,
729 &pushed_down_joins);
730
731 /* Swap rels if needed to match the join info. */
732 if (reversed)
733 {
734 RelOptInfo *trel = rel1;
735
736 rel1 = rel2;
737 rel2 = trel;
738 }
739
740 /*
741 * If it's a plain inner join, then we won't have found anything in
742 * join_info_list. Make up a SpecialJoinInfo so that selectivity
743 * estimation functions will know what's being joined.
744 */
745 if (sjinfo == NULL)
746 {
747 sjinfo = &sjinfo_data;
748 init_dummy_sjinfo(sjinfo, rel1->relids, rel2->relids);
749 }
750
751 /*
752 * Find or build the join RelOptInfo, and compute the restrictlist that
753 * goes with this particular joining.
754 */
755 joinrel = build_join_rel(root, joinrelids, rel1, rel2,
756 sjinfo, pushed_down_joins,
757 &restrictlist);
758
759 /*
760 * If we've already proven this join is empty, we needn't consider any
761 * more paths for it.
762 */
763 if (is_dummy_rel(joinrel))
764 {
765 bms_free(joinrelids);
766 return joinrel;
767 }
768
769 /* Build a grouped join relation for 'joinrel' if possible. */
770 make_grouped_join_rel(root, rel1, rel2, joinrel, sjinfo,
771 restrictlist);
772
773 /* Add paths to the join relation. */
774 populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
775 restrictlist);
776
777 bms_free(joinrelids);
778
779 return joinrel;
780}
bool is_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1464
Relids add_outer_joins_to_relids(PlannerInfo *root, Relids input_relids, SpecialJoinInfo *sjinfo, List **pushed_down_joins)
Definition: joinrels.c:800
static void make_grouped_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: joinrels.c:916
RelOptInfo * build_join_rel(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *pushed_down_joins, List **restrictlist_ptr)
Definition: relnode.c:780

References add_outer_joins_to_relids(), Assert(), bms_free(), bms_overlap(), bms_union(), build_join_rel(), init_dummy_sjinfo(), is_dummy_rel(), join_is_legal(), make_grouped_join_rel(), NIL, populate_joinrel_with_paths(), RelOptInfo::relids, and root.

Referenced by join_search_one_level(), make_rels_by_clause_joins(), make_rels_by_clauseless_joins(), and merge_clump().

◆ make_rels_by_clause_joins()

static void make_rels_by_clause_joins ( PlannerInfo root,
RelOptInfo old_rel,
List other_rels,
int  first_rel_idx 
)
static

Definition at line 285 of file joinrels.c.

289{
290 ListCell *l;
291
292 for_each_from(l, other_rels, first_rel_idx)
293 {
294 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
295
296 if (!bms_overlap(old_rel->relids, other_rel->relids) &&
297 (have_relevant_joinclause(root, old_rel, other_rel) ||
298 have_join_order_restriction(root, old_rel, other_rel)))
299 {
300 (void) make_join_rel(root, old_rel, other_rel);
301 }
302 }
303}

References bms_overlap(), for_each_from, have_join_order_restriction(), have_relevant_joinclause(), lfirst, make_join_rel(), RelOptInfo::relids, and root.

Referenced by join_search_one_level().

◆ make_rels_by_clauseless_joins()

static void make_rels_by_clauseless_joins ( PlannerInfo root,
RelOptInfo old_rel,
List other_rels 
)
static

Definition at line 319 of file joinrels.c.

322{
323 ListCell *l;
324
325 foreach(l, other_rels)
326 {
327 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
328
329 if (!bms_overlap(other_rel->relids, old_rel->relids))
330 {
331 (void) make_join_rel(root, old_rel, other_rel);
332 }
333 }
334}

References bms_overlap(), lfirst, make_join_rel(), RelOptInfo::relids, and root.

Referenced by join_search_one_level().

◆ mark_dummy_rel()

void mark_dummy_rel ( RelOptInfo rel)

Definition at line 1513 of file joinrels.c.

1514{
1515 MemoryContext oldcontext;
1516
1517 /* Already marked? */
1518 if (is_dummy_rel(rel))
1519 return;
1520
1521 /* No, so choose correct context to make the dummy path in */
1523
1524 /* Set dummy size estimate */
1525 rel->rows = 0;
1526
1527 /* Evict any previously chosen paths */
1528 rel->pathlist = NIL;
1529 rel->partial_pathlist = NIL;
1530
1531 /* Set up the dummy path */
1532 add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL,
1533 NIL, rel->lateral_relids,
1534 0, false, -1));
1535
1536 /* Set or update cheapest_total_path and related fields */
1537 set_cheapest(rel);
1538
1539 MemoryContextSwitchTo(oldcontext);
1540}
MemoryContext GetMemoryChunkContext(void *pointer)
Definition: mcxt.c:753
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:269
AppendPath * create_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *partial_subpaths, List *pathkeys, Relids required_outer, int parallel_workers, bool parallel_aware, double rows)
Definition: pathnode.c:1299
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:460
List * partial_pathlist
Definition: pathnodes.h:959

References add_path(), create_append_path(), GetMemoryChunkContext(), is_dummy_rel(), RelOptInfo::lateral_relids, MemoryContextSwitchTo(), NIL, RelOptInfo::partial_pathlist, RelOptInfo::pathlist, RelOptInfo::rows, and set_cheapest().

Referenced by build_setop_child_paths(), build_simple_rel(), generate_grouped_paths(), generate_nonunion_paths(), generate_partitionwise_join_paths(), generate_union_paths(), and populate_joinrel_with_paths().

◆ populate_joinrel_with_paths()

static void populate_joinrel_with_paths ( PlannerInfo root,
RelOptInfo rel1,
RelOptInfo rel2,
RelOptInfo joinrel,
SpecialJoinInfo sjinfo,
List restrictlist 
)
static

Definition at line 1072 of file joinrels.c.

1075{
1076 RelOptInfo *unique_rel2;
1077
1078 /*
1079 * Consider paths using each rel as both outer and inner. Depending on
1080 * the join type, a provably empty outer or inner rel might mean the join
1081 * is provably empty too; in which case throw away any previously computed
1082 * paths and mark the join as dummy. (We do it this way since it's
1083 * conceivable that dummy-ness of a multi-element join might only be
1084 * noticeable for certain construction paths.)
1085 *
1086 * Also, a provably constant-false join restriction typically means that
1087 * we can skip evaluating one or both sides of the join. We do this by
1088 * marking the appropriate rel as dummy. For outer joins, a
1089 * constant-false restriction that is pushed down still means the whole
1090 * join is dummy, while a non-pushed-down one means that no inner rows
1091 * will join so we can treat the inner rel as dummy.
1092 *
1093 * We need only consider the jointypes that appear in join_info_list, plus
1094 * JOIN_INNER.
1095 */
1096 switch (sjinfo->jointype)
1097 {
1098 case JOIN_INNER:
1099 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
1100 restriction_is_constant_false(restrictlist, joinrel, false))
1101 {
1102 mark_dummy_rel(joinrel);
1103 break;
1104 }
1105 add_paths_to_joinrel(root, joinrel, rel1, rel2,
1106 JOIN_INNER, sjinfo,
1107 restrictlist);
1108 add_paths_to_joinrel(root, joinrel, rel2, rel1,
1109 JOIN_INNER, sjinfo,
1110 restrictlist);
1111 break;
1112 case JOIN_LEFT:
1113 if (is_dummy_rel(rel1) ||
1114 restriction_is_constant_false(restrictlist, joinrel, true))
1115 {
1116 mark_dummy_rel(joinrel);
1117 break;
1118 }
1119 if (restriction_is_constant_false(restrictlist, joinrel, false) &&
1120 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
1121 mark_dummy_rel(rel2);
1122 add_paths_to_joinrel(root, joinrel, rel1, rel2,
1123 JOIN_LEFT, sjinfo,
1124 restrictlist);
1125 add_paths_to_joinrel(root, joinrel, rel2, rel1,
1126 JOIN_RIGHT, sjinfo,
1127 restrictlist);
1128 break;
1129 case JOIN_FULL:
1130 if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
1131 restriction_is_constant_false(restrictlist, joinrel, true))
1132 {
1133 mark_dummy_rel(joinrel);
1134 break;
1135 }
1136 add_paths_to_joinrel(root, joinrel, rel1, rel2,
1137 JOIN_FULL, sjinfo,
1138 restrictlist);
1139 add_paths_to_joinrel(root, joinrel, rel2, rel1,
1140 JOIN_FULL, sjinfo,
1141 restrictlist);
1142
1143 /*
1144 * If there are join quals that aren't mergeable or hashable, we
1145 * may not be able to build any valid plan. Complain here so that
1146 * we can give a somewhat-useful error message. (Since we have no
1147 * flexibility of planning for a full join, there's no chance of
1148 * succeeding later with another pair of input rels.)
1149 */
1150 if (joinrel->pathlist == NIL)
1151 ereport(ERROR,
1152 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1153 errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
1154 break;
1155 case JOIN_SEMI:
1156
1157 /*
1158 * We might have a normal semijoin, or a case where we don't have
1159 * enough rels to do the semijoin but can unique-ify the RHS and
1160 * then do an innerjoin (see comments in join_is_legal). In the
1161 * latter case we can't apply JOIN_SEMI joining.
1162 */
1163 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
1164 bms_is_subset(sjinfo->min_righthand, rel2->relids))
1165 {
1166 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
1167 restriction_is_constant_false(restrictlist, joinrel, false))
1168 {
1169 mark_dummy_rel(joinrel);
1170 break;
1171 }
1172 add_paths_to_joinrel(root, joinrel, rel1, rel2,
1173 JOIN_SEMI, sjinfo,
1174 restrictlist);
1175 add_paths_to_joinrel(root, joinrel, rel2, rel1,
1176 JOIN_RIGHT_SEMI, sjinfo,
1177 restrictlist);
1178 }
1179
1180 /*
1181 * If we know how to unique-ify the RHS and one input rel is
1182 * exactly the RHS (not a superset) we can consider unique-ifying
1183 * it and then doing a regular join. (The create_unique_paths
1184 * check here is probably redundant with what join_is_legal did,
1185 * but if so the check is cheap because it's cached. So test
1186 * anyway to be sure.)
1187 */
1188 if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
1189 (unique_rel2 = create_unique_paths(root, rel2, sjinfo)) != NULL)
1190 {
1191 if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
1192 restriction_is_constant_false(restrictlist, joinrel, false))
1193 {
1194 mark_dummy_rel(joinrel);
1195 break;
1196 }
1197 add_paths_to_joinrel(root, joinrel, rel1, unique_rel2,
1198 JOIN_UNIQUE_INNER, sjinfo,
1199 restrictlist);
1200 add_paths_to_joinrel(root, joinrel, unique_rel2, rel1,
1201 JOIN_UNIQUE_OUTER, sjinfo,
1202 restrictlist);
1203 }
1204 break;
1205 case JOIN_ANTI:
1206 if (is_dummy_rel(rel1) ||
1207 restriction_is_constant_false(restrictlist, joinrel, true))
1208 {
1209 mark_dummy_rel(joinrel);
1210 break;
1211 }
1212 if (restriction_is_constant_false(restrictlist, joinrel, false) &&
1213 bms_is_subset(rel2->relids, sjinfo->syn_righthand))
1214 mark_dummy_rel(rel2);
1215 add_paths_to_joinrel(root, joinrel, rel1, rel2,
1216 JOIN_ANTI, sjinfo,
1217 restrictlist);
1218 add_paths_to_joinrel(root, joinrel, rel2, rel1,
1219 JOIN_RIGHT_ANTI, sjinfo,
1220 restrictlist);
1221 break;
1222 default:
1223 /* other values not expected here */
1224 elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
1225 break;
1226 }
1227
1228 /* Apply partitionwise join technique, if possible. */
1229 try_partitionwise_join(root, rel1, rel2, joinrel, sjinfo, restrictlist);
1230}
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ereport(elevel,...)
Definition: elog.h:150
void add_paths_to_joinrel(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: joinpath.c:124
static void try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, List *parent_restrictlist)
Definition: joinrels.c:1610
static bool restriction_is_constant_false(List *restrictlist, RelOptInfo *joinrel, bool only_pushed_down)
Definition: joinrels.c:1556
void mark_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1513
@ JOIN_RIGHT
Definition: nodes.h:306
@ JOIN_RIGHT_SEMI
Definition: nodes.h:319
@ JOIN_UNIQUE_OUTER
Definition: nodes.h:326
@ JOIN_RIGHT_ANTI
Definition: nodes.h:320
@ JOIN_UNIQUE_INNER
Definition: nodes.h:327
@ JOIN_ANTI
Definition: nodes.h:318

References add_paths_to_joinrel(), bms_equal(), bms_is_subset(), create_unique_paths(), elog, ereport, errcode(), errmsg(), ERROR, is_dummy_rel(), JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_RIGHT_ANTI, JOIN_RIGHT_SEMI, JOIN_SEMI, JOIN_UNIQUE_INNER, JOIN_UNIQUE_OUTER, SpecialJoinInfo::jointype, mark_dummy_rel(), SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, NIL, RelOptInfo::pathlist, RelOptInfo::relids, restriction_is_constant_false(), root, SpecialJoinInfo::syn_righthand, and try_partitionwise_join().

Referenced by make_grouped_join_rel(), make_join_rel(), and try_partitionwise_join().

◆ restriction_is_constant_false()

static bool restriction_is_constant_false ( List restrictlist,
RelOptInfo joinrel,
bool  only_pushed_down 
)
static

Definition at line 1556 of file joinrels.c.

1559{
1560 ListCell *lc;
1561
1562 /*
1563 * Despite the above comment, the restriction list we see here might
1564 * possibly have other members besides the FALSE constant, since other
1565 * quals could get "pushed down" to the outer join level. So we check
1566 * each member of the list.
1567 */
1568 foreach(lc, restrictlist)
1569 {
1571
1572 if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
1573 continue;
1574
1575 if (rinfo->clause && IsA(rinfo->clause, Const))
1576 {
1577 Const *con = (Const *) rinfo->clause;
1578
1579 /* constant NULL is as good as constant FALSE for our purposes */
1580 if (con->constisnull)
1581 return true;
1582 if (!DatumGetBool(con->constvalue))
1583 return true;
1584 }
1585 }
1586 return false;
1587}
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2952
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static bool DatumGetBool(Datum X)
Definition: postgres.h:100
Expr * clause
Definition: pathnodes.h:2795

References RestrictInfo::clause, DatumGetBool(), if(), IsA, lfirst_node, RelOptInfo::relids, and RINFO_IS_PUSHED_DOWN.

Referenced by populate_joinrel_with_paths().

◆ try_partitionwise_join()

static void try_partitionwise_join ( PlannerInfo root,
RelOptInfo rel1,
RelOptInfo rel2,
RelOptInfo joinrel,
SpecialJoinInfo parent_sjinfo,
List parent_restrictlist 
)
static

Definition at line 1610 of file joinrels.c.

1613{
1614 bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1615 bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1616 List *parts1 = NIL;
1617 List *parts2 = NIL;
1618 ListCell *lcr1 = NULL;
1619 ListCell *lcr2 = NULL;
1620 int cnt_parts;
1621
1622 /* Guard against stack overflow due to overly deep partition hierarchy. */
1624
1625 /* Nothing to do, if the join relation is not partitioned. */
1626 if (joinrel->part_scheme == NULL || joinrel->nparts == 0)
1627 return;
1628
1629 /* The join relation should have consider_partitionwise_join set. */
1631
1632 /*
1633 * We can not perform partitionwise join if either of the joining
1634 * relations is not partitioned.
1635 */
1636 if (!IS_PARTITIONED_REL(rel1) || !IS_PARTITIONED_REL(rel2))
1637 return;
1638
1640
1641 /* The joining relations should have consider_partitionwise_join set. */
1644
1645 /*
1646 * The partition scheme of the join relation should match that of the
1647 * joining relations.
1648 */
1649 Assert(joinrel->part_scheme == rel1->part_scheme &&
1650 joinrel->part_scheme == rel2->part_scheme);
1651
1652 Assert(!(joinrel->partbounds_merged && (joinrel->nparts <= 0)));
1653
1654 compute_partition_bounds(root, rel1, rel2, joinrel, parent_sjinfo,
1655 &parts1, &parts2);
1656
1657 if (joinrel->partbounds_merged)
1658 {
1659 lcr1 = list_head(parts1);
1660 lcr2 = list_head(parts2);
1661 }
1662
1663 /*
1664 * Create child-join relations for this partitioned join, if those don't
1665 * exist. Add paths to child-joins for a pair of child relations
1666 * corresponding to the given pair of parent relations.
1667 */
1668 for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1669 {
1670 RelOptInfo *child_rel1;
1671 RelOptInfo *child_rel2;
1672 bool rel1_empty;
1673 bool rel2_empty;
1674 SpecialJoinInfo *child_sjinfo;
1675 List *child_restrictlist;
1676 RelOptInfo *child_joinrel;
1677 AppendRelInfo **appinfos;
1678 int nappinfos;
1679 Relids child_relids;
1680
1681 if (joinrel->partbounds_merged)
1682 {
1683 child_rel1 = lfirst_node(RelOptInfo, lcr1);
1684 child_rel2 = lfirst_node(RelOptInfo, lcr2);
1685 lcr1 = lnext(parts1, lcr1);
1686 lcr2 = lnext(parts2, lcr2);
1687 }
1688 else
1689 {
1690 child_rel1 = rel1->part_rels[cnt_parts];
1691 child_rel2 = rel2->part_rels[cnt_parts];
1692 }
1693
1694 rel1_empty = (child_rel1 == NULL || IS_DUMMY_REL(child_rel1));
1695 rel2_empty = (child_rel2 == NULL || IS_DUMMY_REL(child_rel2));
1696
1697 /*
1698 * Check for cases where we can prove that this segment of the join
1699 * returns no rows, due to one or both inputs being empty (including
1700 * inputs that have been pruned away entirely). If so just ignore it.
1701 * These rules are equivalent to populate_joinrel_with_paths's rules
1702 * for dummy input relations.
1703 */
1704 switch (parent_sjinfo->jointype)
1705 {
1706 case JOIN_INNER:
1707 case JOIN_SEMI:
1708 if (rel1_empty || rel2_empty)
1709 continue; /* ignore this join segment */
1710 break;
1711 case JOIN_LEFT:
1712 case JOIN_ANTI:
1713 if (rel1_empty)
1714 continue; /* ignore this join segment */
1715 break;
1716 case JOIN_FULL:
1717 if (rel1_empty && rel2_empty)
1718 continue; /* ignore this join segment */
1719 break;
1720 default:
1721 /* other values not expected here */
1722 elog(ERROR, "unrecognized join type: %d",
1723 (int) parent_sjinfo->jointype);
1724 break;
1725 }
1726
1727 /*
1728 * If a child has been pruned entirely then we can't generate paths
1729 * for it, so we have to reject partitionwise joining unless we were
1730 * able to eliminate this partition above.
1731 */
1732 if (child_rel1 == NULL || child_rel2 == NULL)
1733 {
1734 /*
1735 * Mark the joinrel as unpartitioned so that later functions treat
1736 * it correctly.
1737 */
1738 joinrel->nparts = 0;
1739 return;
1740 }
1741
1742 /*
1743 * If a leaf relation has consider_partitionwise_join=false, it means
1744 * that it's a dummy relation for which we skipped setting up tlist
1745 * expressions and adding EC members in set_append_rel_size(), so
1746 * again we have to fail here.
1747 */
1748 if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
1749 {
1751 Assert(IS_DUMMY_REL(child_rel1));
1752 joinrel->nparts = 0;
1753 return;
1754 }
1755 if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
1756 {
1758 Assert(IS_DUMMY_REL(child_rel2));
1759 joinrel->nparts = 0;
1760 return;
1761 }
1762
1763 /* We should never try to join two overlapping sets of rels. */
1764 Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
1765
1766 /*
1767 * Construct SpecialJoinInfo from parent join relations's
1768 * SpecialJoinInfo.
1769 */
1770 child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
1771 child_rel1->relids,
1772 child_rel2->relids);
1773
1774 /* Find the AppendRelInfo structures */
1775 child_relids = bms_union(child_rel1->relids, child_rel2->relids);
1776 appinfos = find_appinfos_by_relids(root, child_relids,
1777 &nappinfos);
1778
1779 /*
1780 * Construct restrictions applicable to the child join from those
1781 * applicable to the parent join.
1782 */
1783 child_restrictlist =
1785 (Node *) parent_restrictlist,
1786 nappinfos, appinfos);
1787
1788 /* Find or construct the child join's RelOptInfo */
1789 child_joinrel = joinrel->part_rels[cnt_parts];
1790 if (!child_joinrel)
1791 {
1792 child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
1793 joinrel, child_restrictlist,
1794 child_sjinfo, nappinfos, appinfos);
1795 joinrel->part_rels[cnt_parts] = child_joinrel;
1796 joinrel->live_parts = bms_add_member(joinrel->live_parts, cnt_parts);
1797 joinrel->all_partrels = bms_add_members(joinrel->all_partrels,
1798 child_joinrel->relids);
1799 }
1800
1801 /* Assert we got the right one */
1802 Assert(bms_equal(child_joinrel->relids,
1803 adjust_child_relids(joinrel->relids,
1804 nappinfos, appinfos)));
1805
1806 /* Build a grouped join relation for 'child_joinrel' if possible */
1807 make_grouped_join_rel(root, child_rel1, child_rel2,
1808 child_joinrel, child_sjinfo,
1809 child_restrictlist);
1810
1811 /* And make paths for the child join */
1812 populate_joinrel_with_paths(root, child_rel1, child_rel2,
1813 child_joinrel, child_sjinfo,
1814 child_restrictlist);
1815
1816 /*
1817 * When there are thousands of partitions involved, this loop will
1818 * accumulate a significant amount of memory usage from objects that
1819 * are only needed within the loop. Free these local objects eagerly
1820 * at the end of each iteration.
1821 */
1822 pfree(appinfos);
1823 bms_free(child_relids);
1824 free_child_join_sjinfo(child_sjinfo, parent_sjinfo);
1825 }
1826}
static void compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, List **parts1, List **parts2)
Definition: joinrels.c:1933
static SpecialJoinInfo * build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo, Relids left_relids, Relids right_relids)
Definition: joinrels.c:1837
static void free_child_join_sjinfo(SpecialJoinInfo *child_sjinfo, SpecialJoinInfo *parent_sjinfo)
Definition: joinrels.c:1891
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:1138
#define REL_HAS_ALL_PART_PROPS(rel)
Definition: pathnodes.h:1146
@ RELOPT_OTHER_MEMBER_REL
Definition: pathnodes.h:888
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
RelOptInfo * build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel, RelOptInfo *inner_rel, RelOptInfo *parent_joinrel, List *restrictlist, SpecialJoinInfo *sjinfo, int nappinfos, AppendRelInfo **appinfos)
Definition: relnode.c:1001
void check_stack_depth(void)
Definition: stack_depth.c:95
RelOptKind reloptkind
Definition: pathnodes.h:924
Bitmapset * live_parts
Definition: pathnodes.h:1111
bool consider_partitionwise_join
Definition: pathnodes.h:1063

References adjust_appendrel_attrs(), adjust_child_relids(), RelOptInfo::all_partrels, Assert(), bms_add_member(), bms_add_members(), bms_equal(), bms_free(), bms_overlap(), bms_union(), build_child_join_rel(), build_child_join_sjinfo(), check_stack_depth(), compute_partition_bounds(), RelOptInfo::consider_partitionwise_join, elog, ERROR, find_appinfos_by_relids(), free_child_join_sjinfo(), IS_DUMMY_REL, IS_PARTITIONED_REL, IS_SIMPLE_REL, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_SEMI, SpecialJoinInfo::jointype, lfirst_node, list_head(), RelOptInfo::live_parts, lnext(), make_grouped_join_rel(), NIL, RelOptInfo::nparts, RelOptInfo::partbounds_merged, pfree(), populate_joinrel_with_paths(), REL_HAS_ALL_PART_PROPS, RelOptInfo::relids, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, and root.

Referenced by populate_joinrel_with_paths().