PostgreSQL Source Code  git master
joinrels.c File Reference
#include "postgres.h"
#include "miscadmin.h"
#include "optimizer/appendinfo.h"
#include "optimizer/joininfo.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.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_list, ListCell *other_rels)
 
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 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 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)
 
RelOptInfomake_join_rel (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 
bool have_join_order_restriction (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 
bool have_dangerous_phv (PlannerInfo *root, Relids outer_relids, Relids inner_params)
 
bool is_dummy_rel (RelOptInfo *rel)
 
void mark_dummy_rel (RelOptInfo *rel)
 

Function Documentation

◆ 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 1577 of file joinrels.c.

1579 {
1581  AppendRelInfo **left_appinfos;
1582  int left_nappinfos;
1583  AppendRelInfo **right_appinfos;
1584  int right_nappinfos;
1585 
1586  memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
1587  left_appinfos = find_appinfos_by_relids(root, left_relids,
1588  &left_nappinfos);
1589  right_appinfos = find_appinfos_by_relids(root, right_relids,
1590  &right_nappinfos);
1591 
1592  sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand,
1593  left_nappinfos, left_appinfos);
1595  right_nappinfos,
1596  right_appinfos);
1597  sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand,
1598  left_nappinfos, left_appinfos);
1600  right_nappinfos,
1601  right_appinfos);
1602  /* outer-join relids need no adjustment */
1603  sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root,
1604  (Node *) sjinfo->semi_rhs_exprs,
1605  right_nappinfos,
1606  right_appinfos);
1607 
1608  pfree(left_appinfos);
1609  pfree(right_appinfos);
1610 
1611  return sjinfo;
1612 }
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:732
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:195
Relids adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:553
void pfree(void *pointer)
Definition: mcxt.c:1436
#define makeNode(_type_)
Definition: nodes.h:176
Definition: pg_list.h:54
Definition: nodes.h:129
Relids syn_lefthand
Definition: pathnodes.h:2836
Relids min_righthand
Definition: pathnodes.h:2835
List * semi_rhs_exprs
Definition: pathnodes.h:2848
Relids min_lefthand
Definition: pathnodes.h:2834
Relids syn_righthand
Definition: pathnodes.h:2837

References adjust_appendrel_attrs(), adjust_child_relids(), find_appinfos_by_relids(), makeNode, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, pfree(), 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 1619 of file joinrels.c.

1623 {
1624  /*
1625  * If we don't have the partition bounds for the join rel yet, try to
1626  * compute those along with pairs of partitions to be joined.
1627  */
1628  if (joinrel->nparts == -1)
1629  {
1630  PartitionScheme part_scheme = joinrel->part_scheme;
1631  PartitionBoundInfo boundinfo = NULL;
1632  int nparts = 0;
1633 
1634  Assert(joinrel->boundinfo == NULL);
1635  Assert(joinrel->part_rels == NULL);
1636 
1637  /*
1638  * See if the partition bounds for inputs are exactly the same, in
1639  * which case we don't need to work hard: the join rel will have the
1640  * same partition bounds as inputs, and the partitions with the same
1641  * cardinal positions will form the pairs.
1642  *
1643  * Note: even in cases where one or both inputs have merged bounds, it
1644  * would be possible for both the bounds to be exactly the same, but
1645  * it seems unlikely to be worth the cycles to check.
1646  */
1647  if (!rel1->partbounds_merged &&
1648  !rel2->partbounds_merged &&
1649  rel1->nparts == rel2->nparts &&
1650  partition_bounds_equal(part_scheme->partnatts,
1651  part_scheme->parttyplen,
1652  part_scheme->parttypbyval,
1653  rel1->boundinfo, rel2->boundinfo))
1654  {
1655  boundinfo = rel1->boundinfo;
1656  nparts = rel1->nparts;
1657  }
1658  else
1659  {
1660  /* Try merging the partition bounds for inputs. */
1661  boundinfo = partition_bounds_merge(part_scheme->partnatts,
1662  part_scheme->partsupfunc,
1663  part_scheme->partcollation,
1664  rel1, rel2,
1665  parent_sjinfo->jointype,
1666  parts1, parts2);
1667  if (boundinfo == NULL)
1668  {
1669  joinrel->nparts = 0;
1670  return;
1671  }
1672  nparts = list_length(*parts1);
1673  joinrel->partbounds_merged = true;
1674  }
1675 
1676  Assert(nparts > 0);
1677  joinrel->boundinfo = boundinfo;
1678  joinrel->nparts = nparts;
1679  joinrel->part_rels =
1680  (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * nparts);
1681  }
1682  else
1683  {
1684  Assert(joinrel->nparts > 0);
1685  Assert(joinrel->boundinfo);
1686  Assert(joinrel->part_rels);
1687 
1688  /*
1689  * If the join rel's partbounds_merged flag is true, it means inputs
1690  * are not guaranteed to have the same partition bounds, therefore we
1691  * can't assume that the partitions at the same cardinal positions
1692  * form the pairs; let get_matching_part_pairs() generate the pairs.
1693  * Otherwise, nothing to do since we can assume that.
1694  */
1695  if (joinrel->partbounds_merged)
1696  {
1697  get_matching_part_pairs(root, joinrel, rel1, rel2,
1698  parts1, parts2);
1699  Assert(list_length(*parts1) == joinrel->nparts);
1700  Assert(list_length(*parts2) == joinrel->nparts);
1701  }
1702  }
1703 }
static void get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, List **parts1, List **parts2)
Definition: joinrels.c:1710
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc0(Size size)
Definition: mcxt.c:1241
bool partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval, PartitionBoundInfo b1, PartitionBoundInfo b2)
Definition: partbounds.c:897
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:592
bool partbounds_merged
Definition: pathnodes.h:1014
JoinType jointype
Definition: pathnodes.h:2838

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, and PartitionSchemeData::parttyplen.

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 1710 of file joinrels.c.

1713 {
1714  bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1715  bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1716  int cnt_parts;
1717 
1718  *parts1 = NIL;
1719  *parts2 = NIL;
1720 
1721  for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1722  {
1723  RelOptInfo *child_joinrel = joinrel->part_rels[cnt_parts];
1724  RelOptInfo *child_rel1;
1725  RelOptInfo *child_rel2;
1726  Relids child_relids1;
1727  Relids child_relids2;
1728 
1729  /*
1730  * If this segment of the join is empty, it means that this segment
1731  * was ignored when previously creating child-join paths for it in
1732  * try_partitionwise_join() as it would not contribute to the join
1733  * result, due to one or both inputs being empty; add NULL to each of
1734  * the given lists so that this segment will be ignored again in that
1735  * function.
1736  */
1737  if (!child_joinrel)
1738  {
1739  *parts1 = lappend(*parts1, NULL);
1740  *parts2 = lappend(*parts2, NULL);
1741  continue;
1742  }
1743 
1744  /*
1745  * Get a relids set of partition(s) involved in this join segment that
1746  * are from the rel1 side.
1747  */
1748  child_relids1 = bms_intersect(child_joinrel->relids,
1749  rel1->all_partrels);
1750  Assert(bms_num_members(child_relids1) == bms_num_members(rel1->relids));
1751 
1752  /*
1753  * Get a child rel for rel1 with the relids. Note that we should have
1754  * the child rel even if rel1 is a join rel, because in that case the
1755  * partitions specified in the relids would have matching/overlapping
1756  * boundaries, so the specified partitions should be considered as
1757  * ones to be joined when planning partitionwise joins of rel1,
1758  * meaning that the child rel would have been built by the time we get
1759  * here.
1760  */
1761  if (rel1_is_simple)
1762  {
1763  int varno = bms_singleton_member(child_relids1);
1764 
1765  child_rel1 = find_base_rel(root, varno);
1766  }
1767  else
1768  child_rel1 = find_join_rel(root, child_relids1);
1769  Assert(child_rel1);
1770 
1771  /*
1772  * Get a relids set of partition(s) involved in this join segment that
1773  * are from the rel2 side.
1774  */
1775  child_relids2 = bms_intersect(child_joinrel->relids,
1776  rel2->all_partrels);
1777  Assert(bms_num_members(child_relids2) == bms_num_members(rel2->relids));
1778 
1779  /*
1780  * Get a child rel for rel2 with the relids. See above comments.
1781  */
1782  if (rel2_is_simple)
1783  {
1784  int varno = bms_singleton_member(child_relids2);
1785 
1786  child_rel2 = find_base_rel(root, varno);
1787  }
1788  else
1789  child_rel2 = find_join_rel(root, child_relids2);
1790  Assert(child_rel2);
1791 
1792  /*
1793  * The join of rel1 and rel2 is legal, so is the join of the child
1794  * rels obtained above; add them to the given lists as a join pair
1795  * producing this join segment.
1796  */
1797  *parts1 = lappend(*parts1, child_rel1);
1798  *parts2 = lappend(*parts2, child_rel2);
1799  }
1800 }
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:580
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:649
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:260
List * lappend(List *list, void *datum)
Definition: list.c:338
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:834
#define NIL
Definition: pg_list.h:68
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:393
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:495
Relids relids
Definition: pathnodes.h:866
Relids all_partrels
Definition: pathnodes.h:1030

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, and RelOptInfo::relids.

Referenced by compute_partition_bounds().

◆ has_join_restriction()

static bool has_join_restriction ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 1074 of file joinrels.c.

1075 {
1076  ListCell *l;
1077 
1078  if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
1079  return true;
1080 
1081  foreach(l, root->placeholder_list)
1082  {
1083  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1084 
1085  if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
1086  !bms_equal(rel->relids, phinfo->ph_eval_at))
1087  return true;
1088  }
1089 
1090  foreach(l, root->join_info_list)
1091  {
1092  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1093 
1094  /* ignore full joins --- other mechanisms preserve their ordering */
1095  if (sjinfo->jointype == JOIN_FULL)
1096  continue;
1097 
1098  /* ignore if SJ is already contained in rel */
1099  if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
1100  bms_is_subset(sjinfo->min_righthand, rel->relids))
1101  continue;
1102 
1103  /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
1104  if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
1105  bms_overlap(sjinfo->min_righthand, rel->relids))
1106  return true;
1107  }
1108 
1109  return false;
1110 }
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:316
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:495
@ JOIN_FULL
Definition: nodes.h:306
#define lfirst(lc)
Definition: pg_list.h:172
Relids ph_eval_at
Definition: pathnodes.h:3025
List * placeholder_list
Definition: pathnodes.h:374
List * join_info_list
Definition: pathnodes.h:340
Relids lateral_relids
Definition: pathnodes.h:908
Relids lateral_referencers
Definition: pathnodes.h:931

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

Referenced by join_search_one_level().

◆ has_legal_joinclause()

static bool has_legal_joinclause ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 1130 of file joinrels.c.

1131 {
1132  ListCell *lc;
1133 
1134  foreach(lc, root->initial_rels)
1135  {
1136  RelOptInfo *rel2 = (RelOptInfo *) lfirst(lc);
1137 
1138  /* ignore rels that are already in "rel" */
1139  if (bms_overlap(rel->relids, rel2->relids))
1140  continue;
1141 
1142  if (have_relevant_joinclause(root, rel, rel2))
1143  {
1144  Relids joinrelids;
1145  SpecialJoinInfo *sjinfo;
1146  bool reversed;
1147 
1148  /* join_is_legal needs relids of the union */
1149  joinrelids = bms_union(rel->relids, rel2->relids);
1150 
1151  if (join_is_legal(root, rel, rel2, joinrelids,
1152  &sjinfo, &reversed))
1153  {
1154  /* Yes, this will work */
1155  bms_free(joinrelids);
1156  return true;
1157  }
1158 
1159  bms_free(joinrelids);
1160  }
1161  }
1162 
1163  return false;
1164 }
void bms_free(Bitmapset *a)
Definition: bitmapset.c:209
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:226
bool have_relevant_joinclause(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joininfo.c:36
static bool join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, Relids joinrelids, SpecialJoinInfo **sjinfo_p, bool *reversed_p)
Definition: joinrels.c:367

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

Referenced by have_join_order_restriction().

◆ have_dangerous_phv()

bool have_dangerous_phv ( PlannerInfo root,
Relids  outer_relids,
Relids  inner_params 
)

Definition at line 1194 of file joinrels.c.

1196 {
1197  ListCell *lc;
1198 
1199  foreach(lc, root->placeholder_list)
1200  {
1201  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
1202 
1203  if (!bms_is_subset(phinfo->ph_eval_at, inner_params))
1204  continue; /* ignore, could not be a nestloop param */
1205  if (!bms_overlap(phinfo->ph_eval_at, outer_relids))
1206  continue; /* ignore, not relevant to this join */
1207  if (bms_is_subset(phinfo->ph_eval_at, outer_relids))
1208  continue; /* safe, it can be eval'd within outerrel */
1209  /* Otherwise, it's potentially unsafe, so reject the join */
1210  return true;
1211  }
1212 
1213  /* OK to perform the join */
1214  return false;
1215 }

References bms_is_subset(), bms_overlap(), lfirst, PlaceHolderInfo::ph_eval_at, and PlannerInfo::placeholder_list.

Referenced by join_is_legal(), and try_nestloop_path().

◆ have_join_order_restriction()

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

Definition at line 961 of file joinrels.c.

963 {
964  bool result = false;
965  ListCell *l;
966 
967  /*
968  * If either side has a direct lateral reference to the other, attempt the
969  * join regardless of outer-join considerations.
970  */
971  if (bms_overlap(rel1->relids, rel2->direct_lateral_relids) ||
973  return true;
974 
975  /*
976  * Likewise, if both rels are needed to compute some PlaceHolderVar,
977  * attempt the join regardless of outer-join considerations. (This is not
978  * very desirable, because a PHV with a large eval_at set will cause a lot
979  * of probably-useless joins to be considered, but failing to do this can
980  * cause us to fail to construct a plan at all.)
981  */
982  foreach(l, root->placeholder_list)
983  {
984  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
985 
986  if (bms_is_subset(rel1->relids, phinfo->ph_eval_at) &&
987  bms_is_subset(rel2->relids, phinfo->ph_eval_at))
988  return true;
989  }
990 
991  /*
992  * It's possible that the rels correspond to the left and right sides of a
993  * degenerate outer join, that is, one with no joinclause mentioning the
994  * non-nullable side; in which case we should force the join to occur.
995  *
996  * Also, the two rels could represent a clauseless join that has to be
997  * completed to build up the LHS or RHS of an outer join.
998  */
999  foreach(l, root->join_info_list)
1000  {
1001  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1002 
1003  /* ignore full joins --- other mechanisms handle them */
1004  if (sjinfo->jointype == JOIN_FULL)
1005  continue;
1006 
1007  /* Can we perform the SJ with these rels? */
1008  if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
1009  bms_is_subset(sjinfo->min_righthand, rel2->relids))
1010  {
1011  result = true;
1012  break;
1013  }
1014  if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
1015  bms_is_subset(sjinfo->min_righthand, rel1->relids))
1016  {
1017  result = true;
1018  break;
1019  }
1020 
1021  /*
1022  * Might we need to join these rels to complete the RHS? We have to
1023  * use "overlap" tests since either rel might include a lower SJ that
1024  * has been proven to commute with this one.
1025  */
1026  if (bms_overlap(sjinfo->min_righthand, rel1->relids) &&
1027  bms_overlap(sjinfo->min_righthand, rel2->relids))
1028  {
1029  result = true;
1030  break;
1031  }
1032 
1033  /* Likewise for the LHS. */
1034  if (bms_overlap(sjinfo->min_lefthand, rel1->relids) &&
1035  bms_overlap(sjinfo->min_lefthand, rel2->relids))
1036  {
1037  result = true;
1038  break;
1039  }
1040  }
1041 
1042  /*
1043  * We do not force the join to occur if either input rel can legally be
1044  * joined to anything else using joinclauses. This essentially means that
1045  * clauseless bushy joins are put off as long as possible. The reason is
1046  * that when there is a join order restriction high up in the join tree
1047  * (that is, with many rels inside the LHS or RHS), we would otherwise
1048  * expend lots of effort considering very stupid join combinations within
1049  * its LHS or RHS.
1050  */
1051  if (result)
1052  {
1053  if (has_legal_joinclause(root, rel1) ||
1054  has_legal_joinclause(root, rel2))
1055  result = false;
1056  }
1057 
1058  return result;
1059 }
static bool has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel)
Definition: joinrels.c:1130
Relids direct_lateral_relids
Definition: pathnodes.h:906

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

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

◆ is_dummy_rel()

bool is_dummy_rel ( RelOptInfo rel)

Definition at line 1222 of file joinrels.c.

1223 {
1224  Path *path;
1225 
1226  /*
1227  * A rel that is known dummy will have just one path that is a childless
1228  * Append. (Even if somehow it has more paths, a childless Append will
1229  * have cost zero and hence should be at the front of the pathlist.)
1230  */
1231  if (rel->pathlist == NIL)
1232  return false;
1233  path = (Path *) linitial(rel->pathlist);
1234 
1235  /*
1236  * Initially, a dummy path will just be a childless Append. But in later
1237  * planning stages we might stick a ProjectSetPath and/or ProjectionPath
1238  * on top, since Append can't project. Rather than make assumptions about
1239  * which combinations can occur, just descend through whatever we find.
1240  */
1241  for (;;)
1242  {
1243  if (IsA(path, ProjectionPath))
1244  path = ((ProjectionPath *) path)->subpath;
1245  else if (IsA(path, ProjectSetPath))
1246  path = ((ProjectSetPath *) path)->subpath;
1247  else
1248  break;
1249  }
1250  if (IS_DUMMY_APPEND(path))
1251  return true;
1252  return false;
1253 }
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
#define IS_DUMMY_APPEND(p)
Definition: pathnodes.h:1906
#define linitial(l)
Definition: pg_list.h:178
List * pathlist
Definition: pathnodes.h:893

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

Referenced by 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 367 of file joinrels.c.

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

References bms_add_members(), bms_copy(), bms_equal(), bms_is_subset(), bms_overlap(), RelOptInfo::cheapest_total_path, create_unique_path(), RelOptInfo::direct_lateral_relids, PlannerInfo::hasLateralRTEs, have_dangerous_phv(), JOIN_FULL, PlannerInfo::join_info_list, JOIN_LEFT, JOIN_SEMI, SpecialJoinInfo::jointype, RelOptInfo::lateral_relids, lfirst, SpecialJoinInfo::lhs_strict, min_join_parameterization(), SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, RelOptInfo::relids, 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 71 of file joinrels.c.

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

References Assert(), bms_overlap(), elog(), ERROR, for_each_cell, RelOptInfo::has_eclass_joins, has_join_restriction(), PlannerInfo::hasLateralRTEs, have_join_order_restriction(), have_relevant_joinclause(), PlannerInfo::join_cur_level, PlannerInfo::join_info_list, RelOptInfo::joininfo, lfirst, list_head(), lnext(), make_join_rel(), make_rels_by_clause_joins(), make_rels_by_clauseless_joins(), NIL, and RelOptInfo::relids.

Referenced by standard_join_search().

◆ make_join_rel()

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

Definition at line 689 of file joinrels.c.

690 {
691  Relids joinrelids;
692  SpecialJoinInfo *sjinfo;
693  bool reversed;
694  SpecialJoinInfo sjinfo_data;
695  RelOptInfo *joinrel;
696  List *restrictlist;
697 
698  /* We should never try to join two overlapping sets of rels. */
699  Assert(!bms_overlap(rel1->relids, rel2->relids));
700 
701  /* Construct Relids set that identifies the joinrel (without OJ as yet). */
702  joinrelids = bms_union(rel1->relids, rel2->relids);
703 
704  /* Check validity and determine join type. */
705  if (!join_is_legal(root, rel1, rel2, joinrelids,
706  &sjinfo, &reversed))
707  {
708  /* invalid join path */
709  bms_free(joinrelids);
710  return NULL;
711  }
712 
713  /* If we have an outer join, add its RTI to form the canonical relids. */
714  if (sjinfo && sjinfo->ojrelid != 0)
715  joinrelids = bms_add_member(joinrelids, sjinfo->ojrelid);
716 
717  /* Swap rels if needed to match the join info. */
718  if (reversed)
719  {
720  RelOptInfo *trel = rel1;
721 
722  rel1 = rel2;
723  rel2 = trel;
724  }
725 
726  /*
727  * If it's a plain inner join, then we won't have found anything in
728  * join_info_list. Make up a SpecialJoinInfo so that selectivity
729  * estimation functions will know what's being joined.
730  */
731  if (sjinfo == NULL)
732  {
733  sjinfo = &sjinfo_data;
734  sjinfo->type = T_SpecialJoinInfo;
735  sjinfo->min_lefthand = rel1->relids;
736  sjinfo->min_righthand = rel2->relids;
737  sjinfo->syn_lefthand = rel1->relids;
738  sjinfo->syn_righthand = rel2->relids;
739  sjinfo->jointype = JOIN_INNER;
740  sjinfo->ojrelid = 0;
741  sjinfo->commute_above_l = NULL;
742  sjinfo->commute_above_r = NULL;
743  sjinfo->commute_below = NULL;
744  /* we don't bother trying to make the remaining fields valid */
745  sjinfo->lhs_strict = false;
746  sjinfo->semi_can_btree = false;
747  sjinfo->semi_can_hash = false;
748  sjinfo->semi_operators = NIL;
749  sjinfo->semi_rhs_exprs = NIL;
750  }
751 
752  /*
753  * Find or build the join RelOptInfo, and compute the restrictlist that
754  * goes with this particular joining.
755  */
756  joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
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  /* Add paths to the join relation. */
770  populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
771  restrictlist);
772 
773  bms_free(joinrelids);
774 
775  return joinrel;
776 }
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:739
static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: joinrels.c:786
bool is_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1222
@ JOIN_INNER
Definition: nodes.h:304
RelOptInfo * build_join_rel(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List **restrictlist_ptr)
Definition: relnode.c:632
Relids commute_above_r
Definition: pathnodes.h:2841
Relids commute_above_l
Definition: pathnodes.h:2840
Relids commute_below
Definition: pathnodes.h:2842
List * semi_operators
Definition: pathnodes.h:2847

References Assert(), bms_add_member(), bms_free(), bms_overlap(), bms_union(), build_join_rel(), SpecialJoinInfo::commute_above_l, SpecialJoinInfo::commute_above_r, SpecialJoinInfo::commute_below, is_dummy_rel(), JOIN_INNER, join_is_legal(), SpecialJoinInfo::jointype, SpecialJoinInfo::lhs_strict, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, NIL, SpecialJoinInfo::ojrelid, populate_joinrel_with_paths(), RelOptInfo::relids, SpecialJoinInfo::semi_can_btree, SpecialJoinInfo::semi_can_hash, SpecialJoinInfo::semi_operators, SpecialJoinInfo::semi_rhs_exprs, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

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_list,
ListCell other_rels 
)
static

Definition at line 297 of file joinrels.c.

301 {
302  ListCell *l;
303 
304  for_each_cell(l, other_rels_list, other_rels)
305  {
306  RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
307 
308  if (!bms_overlap(old_rel->relids, other_rel->relids) &&
309  (have_relevant_joinclause(root, old_rel, other_rel) ||
310  have_join_order_restriction(root, old_rel, other_rel)))
311  {
312  (void) make_join_rel(root, old_rel, other_rel);
313  }
314  }
315 }

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

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 331 of file joinrels.c.

334 {
335  ListCell *l;
336 
337  foreach(l, other_rels)
338  {
339  RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
340 
341  if (!bms_overlap(other_rel->relids, old_rel->relids))
342  {
343  (void) make_join_rel(root, old_rel, other_rel);
344  }
345  }
346 }

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

Referenced by join_search_one_level().

◆ mark_dummy_rel()

void mark_dummy_rel ( RelOptInfo rel)

Definition at line 1271 of file joinrels.c.

1272 {
1273  MemoryContext oldcontext;
1274 
1275  /* Already marked? */
1276  if (is_dummy_rel(rel))
1277  return;
1278 
1279  /* No, so choose correct context to make the dummy path in */
1280  oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
1281 
1282  /* Set dummy size estimate */
1283  rel->rows = 0;
1284 
1285  /* Evict any previously chosen paths */
1286  rel->pathlist = NIL;
1287  rel->partial_pathlist = NIL;
1288 
1289  /* Set up the dummy path */
1290  add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL,
1291  NIL, rel->lateral_relids,
1292  0, false, -1));
1293 
1294  /* Set or update cheapest_total_path and related fields */
1295  set_cheapest(rel);
1296 
1297  MemoryContextSwitchTo(oldcontext);
1298 }
MemoryContext GetMemoryChunkContext(void *pointer)
Definition: mcxt.c:600
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
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:1244
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
List * partial_pathlist
Definition: pathnodes.h:895
Cardinality rows
Definition: pathnodes.h:872

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_simple_rel(), generate_partitionwise_join_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 786 of file joinrels.c.

789 {
790  /*
791  * Consider paths using each rel as both outer and inner. Depending on
792  * the join type, a provably empty outer or inner rel might mean the join
793  * is provably empty too; in which case throw away any previously computed
794  * paths and mark the join as dummy. (We do it this way since it's
795  * conceivable that dummy-ness of a multi-element join might only be
796  * noticeable for certain construction paths.)
797  *
798  * Also, a provably constant-false join restriction typically means that
799  * we can skip evaluating one or both sides of the join. We do this by
800  * marking the appropriate rel as dummy. For outer joins, a
801  * constant-false restriction that is pushed down still means the whole
802  * join is dummy, while a non-pushed-down one means that no inner rows
803  * will join so we can treat the inner rel as dummy.
804  *
805  * We need only consider the jointypes that appear in join_info_list, plus
806  * JOIN_INNER.
807  */
808  switch (sjinfo->jointype)
809  {
810  case JOIN_INNER:
811  if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
812  restriction_is_constant_false(restrictlist, joinrel, false))
813  {
814  mark_dummy_rel(joinrel);
815  break;
816  }
817  add_paths_to_joinrel(root, joinrel, rel1, rel2,
818  JOIN_INNER, sjinfo,
819  restrictlist);
820  add_paths_to_joinrel(root, joinrel, rel2, rel1,
821  JOIN_INNER, sjinfo,
822  restrictlist);
823  break;
824  case JOIN_LEFT:
825  if (is_dummy_rel(rel1) ||
826  restriction_is_constant_false(restrictlist, joinrel, true))
827  {
828  mark_dummy_rel(joinrel);
829  break;
830  }
831  if (restriction_is_constant_false(restrictlist, joinrel, false) &&
832  bms_is_subset(rel2->relids, sjinfo->syn_righthand))
833  mark_dummy_rel(rel2);
834  add_paths_to_joinrel(root, joinrel, rel1, rel2,
835  JOIN_LEFT, sjinfo,
836  restrictlist);
837  add_paths_to_joinrel(root, joinrel, rel2, rel1,
838  JOIN_RIGHT, sjinfo,
839  restrictlist);
840  break;
841  case JOIN_FULL:
842  if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
843  restriction_is_constant_false(restrictlist, joinrel, true))
844  {
845  mark_dummy_rel(joinrel);
846  break;
847  }
848  add_paths_to_joinrel(root, joinrel, rel1, rel2,
849  JOIN_FULL, sjinfo,
850  restrictlist);
851  add_paths_to_joinrel(root, joinrel, rel2, rel1,
852  JOIN_FULL, sjinfo,
853  restrictlist);
854 
855  /*
856  * If there are join quals that aren't mergeable or hashable, we
857  * may not be able to build any valid plan. Complain here so that
858  * we can give a somewhat-useful error message. (Since we have no
859  * flexibility of planning for a full join, there's no chance of
860  * succeeding later with another pair of input rels.)
861  */
862  if (joinrel->pathlist == NIL)
863  ereport(ERROR,
864  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
865  errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
866  break;
867  case JOIN_SEMI:
868 
869  /*
870  * We might have a normal semijoin, or a case where we don't have
871  * enough rels to do the semijoin but can unique-ify the RHS and
872  * then do an innerjoin (see comments in join_is_legal). In the
873  * latter case we can't apply JOIN_SEMI joining.
874  */
875  if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
876  bms_is_subset(sjinfo->min_righthand, rel2->relids))
877  {
878  if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
879  restriction_is_constant_false(restrictlist, joinrel, false))
880  {
881  mark_dummy_rel(joinrel);
882  break;
883  }
884  add_paths_to_joinrel(root, joinrel, rel1, rel2,
885  JOIN_SEMI, sjinfo,
886  restrictlist);
887  }
888 
889  /*
890  * If we know how to unique-ify the RHS and one input rel is
891  * exactly the RHS (not a superset) we can consider unique-ifying
892  * it and then doing a regular join. (The create_unique_path
893  * check here is probably redundant with what join_is_legal did,
894  * but if so the check is cheap because it's cached. So test
895  * anyway to be sure.)
896  */
897  if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
898  create_unique_path(root, rel2, rel2->cheapest_total_path,
899  sjinfo) != NULL)
900  {
901  if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
902  restriction_is_constant_false(restrictlist, joinrel, false))
903  {
904  mark_dummy_rel(joinrel);
905  break;
906  }
907  add_paths_to_joinrel(root, joinrel, rel1, rel2,
908  JOIN_UNIQUE_INNER, sjinfo,
909  restrictlist);
910  add_paths_to_joinrel(root, joinrel, rel2, rel1,
911  JOIN_UNIQUE_OUTER, sjinfo,
912  restrictlist);
913  }
914  break;
915  case JOIN_ANTI:
916  if (is_dummy_rel(rel1) ||
917  restriction_is_constant_false(restrictlist, joinrel, true))
918  {
919  mark_dummy_rel(joinrel);
920  break;
921  }
922  if (restriction_is_constant_false(restrictlist, joinrel, false) &&
923  bms_is_subset(rel2->relids, sjinfo->syn_righthand))
924  mark_dummy_rel(rel2);
925  add_paths_to_joinrel(root, joinrel, rel1, rel2,
926  JOIN_ANTI, sjinfo,
927  restrictlist);
928  break;
929  default:
930  /* other values not expected here */
931  elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
932  break;
933  }
934 
935  /* Apply partitionwise join technique, if possible. */
936  try_partitionwise_join(root, rel1, rel2, joinrel, sjinfo, restrictlist);
937 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereport(elevel,...)
Definition: elog.h:149
void add_paths_to_joinrel(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: joinpath.c:123
static void try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, List *parent_restrictlist)
Definition: joinrels.c:1368
static bool restriction_is_constant_false(List *restrictlist, RelOptInfo *joinrel, bool only_pushed_down)
Definition: joinrels.c:1314
void mark_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1271
@ JOIN_RIGHT
Definition: nodes.h:307
@ JOIN_UNIQUE_OUTER
Definition: nodes.h:325
@ JOIN_UNIQUE_INNER
Definition: nodes.h:326
@ JOIN_ANTI
Definition: nodes.h:319

References add_paths_to_joinrel(), bms_equal(), bms_is_subset(), RelOptInfo::cheapest_total_path, create_unique_path(), elog(), ereport, errcode(), errmsg(), ERROR, is_dummy_rel(), JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, 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(), SpecialJoinInfo::syn_righthand, and try_partitionwise_join().

Referenced by 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 1314 of file joinrels.c.

1317 {
1318  ListCell *lc;
1319 
1320  /*
1321  * Despite the above comment, the restriction list we see here might
1322  * possibly have other members besides the FALSE constant, since other
1323  * quals could get "pushed down" to the outer join level. So we check
1324  * each member of the list.
1325  */
1326  foreach(lc, restrictlist)
1327  {
1328  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1329 
1330  if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
1331  continue;
1332 
1333  if (rinfo->clause && IsA(rinfo->clause, Const))
1334  {
1335  Const *con = (Const *) rinfo->clause;
1336 
1337  /* constant NULL is as good as constant FALSE for our purposes */
1338  if (con->constisnull)
1339  return true;
1340  if (!DatumGetBool(con->constvalue))
1341  return true;
1342  }
1343  }
1344  return false;
1345 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2671
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
Expr * clause
Definition: pathnodes.h:2520

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 1368 of file joinrels.c.

1371 {
1372  bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1373  bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1374  List *parts1 = NIL;
1375  List *parts2 = NIL;
1376  ListCell *lcr1 = NULL;
1377  ListCell *lcr2 = NULL;
1378  int cnt_parts;
1379 
1380  /* Guard against stack overflow due to overly deep partition hierarchy. */
1382 
1383  /* Nothing to do, if the join relation is not partitioned. */
1384  if (joinrel->part_scheme == NULL || joinrel->nparts == 0)
1385  return;
1386 
1387  /* The join relation should have consider_partitionwise_join set. */
1389 
1390  /*
1391  * We can not perform partitionwise join if either of the joining
1392  * relations is not partitioned.
1393  */
1394  if (!IS_PARTITIONED_REL(rel1) || !IS_PARTITIONED_REL(rel2))
1395  return;
1396 
1398 
1399  /* The joining relations should have consider_partitionwise_join set. */
1402 
1403  /*
1404  * The partition scheme of the join relation should match that of the
1405  * joining relations.
1406  */
1407  Assert(joinrel->part_scheme == rel1->part_scheme &&
1408  joinrel->part_scheme == rel2->part_scheme);
1409 
1410  Assert(!(joinrel->partbounds_merged && (joinrel->nparts <= 0)));
1411 
1412  compute_partition_bounds(root, rel1, rel2, joinrel, parent_sjinfo,
1413  &parts1, &parts2);
1414 
1415  if (joinrel->partbounds_merged)
1416  {
1417  lcr1 = list_head(parts1);
1418  lcr2 = list_head(parts2);
1419  }
1420 
1421  /*
1422  * Create child-join relations for this partitioned join, if those don't
1423  * exist. Add paths to child-joins for a pair of child relations
1424  * corresponding to the given pair of parent relations.
1425  */
1426  for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1427  {
1428  RelOptInfo *child_rel1;
1429  RelOptInfo *child_rel2;
1430  bool rel1_empty;
1431  bool rel2_empty;
1432  SpecialJoinInfo *child_sjinfo;
1433  List *child_restrictlist;
1434  RelOptInfo *child_joinrel;
1435  Relids child_joinrelids;
1436  AppendRelInfo **appinfos;
1437  int nappinfos;
1438 
1439  if (joinrel->partbounds_merged)
1440  {
1441  child_rel1 = lfirst_node(RelOptInfo, lcr1);
1442  child_rel2 = lfirst_node(RelOptInfo, lcr2);
1443  lcr1 = lnext(parts1, lcr1);
1444  lcr2 = lnext(parts2, lcr2);
1445  }
1446  else
1447  {
1448  child_rel1 = rel1->part_rels[cnt_parts];
1449  child_rel2 = rel2->part_rels[cnt_parts];
1450  }
1451 
1452  rel1_empty = (child_rel1 == NULL || IS_DUMMY_REL(child_rel1));
1453  rel2_empty = (child_rel2 == NULL || IS_DUMMY_REL(child_rel2));
1454 
1455  /*
1456  * Check for cases where we can prove that this segment of the join
1457  * returns no rows, due to one or both inputs being empty (including
1458  * inputs that have been pruned away entirely). If so just ignore it.
1459  * These rules are equivalent to populate_joinrel_with_paths's rules
1460  * for dummy input relations.
1461  */
1462  switch (parent_sjinfo->jointype)
1463  {
1464  case JOIN_INNER:
1465  case JOIN_SEMI:
1466  if (rel1_empty || rel2_empty)
1467  continue; /* ignore this join segment */
1468  break;
1469  case JOIN_LEFT:
1470  case JOIN_ANTI:
1471  if (rel1_empty)
1472  continue; /* ignore this join segment */
1473  break;
1474  case JOIN_FULL:
1475  if (rel1_empty && rel2_empty)
1476  continue; /* ignore this join segment */
1477  break;
1478  default:
1479  /* other values not expected here */
1480  elog(ERROR, "unrecognized join type: %d",
1481  (int) parent_sjinfo->jointype);
1482  break;
1483  }
1484 
1485  /*
1486  * If a child has been pruned entirely then we can't generate paths
1487  * for it, so we have to reject partitionwise joining unless we were
1488  * able to eliminate this partition above.
1489  */
1490  if (child_rel1 == NULL || child_rel2 == NULL)
1491  {
1492  /*
1493  * Mark the joinrel as unpartitioned so that later functions treat
1494  * it correctly.
1495  */
1496  joinrel->nparts = 0;
1497  return;
1498  }
1499 
1500  /*
1501  * If a leaf relation has consider_partitionwise_join=false, it means
1502  * that it's a dummy relation for which we skipped setting up tlist
1503  * expressions and adding EC members in set_append_rel_size(), so
1504  * again we have to fail here.
1505  */
1506  if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
1507  {
1508  Assert(child_rel1->reloptkind == RELOPT_OTHER_MEMBER_REL);
1509  Assert(IS_DUMMY_REL(child_rel1));
1510  joinrel->nparts = 0;
1511  return;
1512  }
1513  if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
1514  {
1515  Assert(child_rel2->reloptkind == RELOPT_OTHER_MEMBER_REL);
1516  Assert(IS_DUMMY_REL(child_rel2));
1517  joinrel->nparts = 0;
1518  return;
1519  }
1520 
1521  /* We should never try to join two overlapping sets of rels. */
1522  Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
1523 
1524  /*
1525  * Construct SpecialJoinInfo from parent join relations's
1526  * SpecialJoinInfo.
1527  */
1528  child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
1529  child_rel1->relids,
1530  child_rel2->relids);
1531 
1532  /* Build correct join relids for child join */
1533  child_joinrelids = bms_union(child_rel1->relids, child_rel2->relids);
1534  if (child_sjinfo->ojrelid != 0)
1535  child_joinrelids = bms_add_member(child_joinrelids,
1536  child_sjinfo->ojrelid);
1537 
1538  /* Find the AppendRelInfo structures */
1539  appinfos = find_appinfos_by_relids(root, child_joinrelids, &nappinfos);
1540 
1541  /*
1542  * Construct restrictions applicable to the child join from those
1543  * applicable to the parent join.
1544  */
1545  child_restrictlist =
1546  (List *) adjust_appendrel_attrs(root,
1547  (Node *) parent_restrictlist,
1548  nappinfos, appinfos);
1549  pfree(appinfos);
1550 
1551  child_joinrel = joinrel->part_rels[cnt_parts];
1552  if (!child_joinrel)
1553  {
1554  child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
1555  joinrel, child_restrictlist,
1556  child_sjinfo);
1557  joinrel->part_rels[cnt_parts] = child_joinrel;
1558  joinrel->live_parts = bms_add_member(joinrel->live_parts, cnt_parts);
1559  joinrel->all_partrels = bms_add_members(joinrel->all_partrels,
1560  child_joinrel->relids);
1561  }
1562 
1563  Assert(bms_equal(child_joinrel->relids, child_joinrelids));
1564 
1565  populate_joinrel_with_paths(root, child_rel1, child_rel2,
1566  child_joinrel, child_sjinfo,
1567  child_restrictlist);
1568  }
1569 }
static void compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, List **parts1, List **parts2)
Definition: joinrels.c:1619
static SpecialJoinInfo * build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo, Relids left_relids, Relids right_relids)
Definition: joinrels.c:1577
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1914
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:1051
#define REL_HAS_ALL_PART_PROPS(rel)
Definition: pathnodes.h:1059
@ RELOPT_OTHER_MEMBER_REL
Definition: pathnodes.h:823
void check_stack_depth(void)
Definition: postgres.c:3454
RelOptInfo * build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel, RelOptInfo *inner_rel, RelOptInfo *parent_joinrel, List *restrictlist, SpecialJoinInfo *sjinfo)
Definition: relnode.c:846
RelOptKind reloptkind
Definition: pathnodes.h:860
Bitmapset * live_parts
Definition: pathnodes.h:1028
bool consider_partitionwise_join
Definition: pathnodes.h:988

References adjust_appendrel_attrs(), RelOptInfo::all_partrels, Assert(), bms_add_member(), bms_add_members(), bms_equal(), 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(), 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(), NIL, RelOptInfo::nparts, SpecialJoinInfo::ojrelid, RelOptInfo::partbounds_merged, pfree(), populate_joinrel_with_paths(), REL_HAS_ALL_PART_PROPS, RelOptInfo::relids, RELOPT_OTHER_MEMBER_REL, and RelOptInfo::reloptkind.

Referenced by populate_joinrel_with_paths().