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/lsyscache.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 int match_expr_to_partition_keys (Expr *expr, RelOptInfo *rel, bool strict_op)
 
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)
 
bool have_partkey_equi_join (RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
 

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

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().

1543 {
1545  AppendRelInfo **left_appinfos;
1546  int left_nappinfos;
1547  AppendRelInfo **right_appinfos;
1548  int right_nappinfos;
1549 
1550  memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
1551  left_appinfos = find_appinfos_by_relids(root, left_relids,
1552  &left_nappinfos);
1553  right_appinfos = find_appinfos_by_relids(root, right_relids,
1554  &right_nappinfos);
1555 
1556  sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand,
1557  left_nappinfos, left_appinfos);
1559  right_nappinfos,
1560  right_appinfos);
1561  sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand,
1562  left_nappinfos, left_appinfos);
1564  right_nappinfos,
1565  right_appinfos);
1566  sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root,
1567  (Node *) sjinfo->semi_rhs_exprs,
1568  right_nappinfos,
1569  right_appinfos);
1570 
1571  pfree(left_appinfos);
1572  pfree(right_appinfos);
1573 
1574  return sjinfo;
1575 }
Relids min_righthand
Definition: pathnodes.h:2134
Definition: nodes.h:524
Relids syn_lefthand
Definition: pathnodes.h:2135
Relids syn_righthand
Definition: pathnodes.h:2136
void pfree(void *pointer)
Definition: mcxt.c:1031
List * semi_rhs_exprs
Definition: pathnodes.h:2144
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:709
#define makeNode(_type_)
Definition: nodes.h:572
Relids adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:522
Definition: pg_list.h:50
Relids min_lefthand
Definition: pathnodes.h:2133
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:180

◆ has_join_restriction()

static bool has_join_restriction ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 1060 of file joinrels.c.

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().

1061 {
1062  ListCell *l;
1063 
1064  if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
1065  return true;
1066 
1067  foreach(l, root->placeholder_list)
1068  {
1069  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1070 
1071  if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
1072  !bms_equal(rel->relids, phinfo->ph_eval_at))
1073  return true;
1074  }
1075 
1076  foreach(l, root->join_info_list)
1077  {
1078  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1079 
1080  /* ignore full joins --- other mechanisms preserve their ordering */
1081  if (sjinfo->jointype == JOIN_FULL)
1082  continue;
1083 
1084  /* ignore if SJ is already contained in rel */
1085  if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
1086  bms_is_subset(sjinfo->min_righthand, rel->relids))
1087  continue;
1088 
1089  /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
1090  if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
1091  bms_overlap(sjinfo->min_righthand, rel->relids))
1092  return true;
1093  }
1094 
1095  return false;
1096 }
Relids ph_eval_at
Definition: pathnodes.h:2261
List * join_info_list
Definition: pathnodes.h:281
Relids min_righthand
Definition: pathnodes.h:2134
Relids lateral_relids
Definition: pathnodes.h:666
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids relids
Definition: pathnodes.h:641
Relids lateral_referencers
Definition: pathnodes.h:677
#define lfirst(lc)
Definition: pg_list.h:190
JoinType jointype
Definition: pathnodes.h:2137
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
List * placeholder_list
Definition: pathnodes.h:292
Relids min_lefthand
Definition: pathnodes.h:2133
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ has_legal_joinclause()

static bool has_legal_joinclause ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 1116 of file joinrels.c.

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

Referenced by have_join_order_restriction().

1117 {
1118  ListCell *lc;
1119 
1120  foreach(lc, root->initial_rels)
1121  {
1122  RelOptInfo *rel2 = (RelOptInfo *) lfirst(lc);
1123 
1124  /* ignore rels that are already in "rel" */
1125  if (bms_overlap(rel->relids, rel2->relids))
1126  continue;
1127 
1128  if (have_relevant_joinclause(root, rel, rel2))
1129  {
1130  Relids joinrelids;
1131  SpecialJoinInfo *sjinfo;
1132  bool reversed;
1133 
1134  /* join_is_legal needs relids of the union */
1135  joinrelids = bms_union(rel->relids, rel2->relids);
1136 
1137  if (join_is_legal(root, rel, rel2, joinrelids,
1138  &sjinfo, &reversed))
1139  {
1140  /* Yes, this will work */
1141  bms_free(joinrelids);
1142  return true;
1143  }
1144 
1145  bms_free(joinrelids);
1146  }
1147  }
1148 
1149  return false;
1150 }
static bool join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, Relids joinrelids, SpecialJoinInfo **sjinfo_p, bool *reversed_p)
Definition: joinrels.c:360
Relids relids
Definition: pathnodes.h:641
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define lfirst(lc)
Definition: pg_list.h:190
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
bool have_relevant_joinclause(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joininfo.c:36
List * initial_rels
Definition: pathnodes.h:306

◆ have_dangerous_phv()

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

Definition at line 1180 of file joinrels.c.

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

Referenced by join_is_legal(), and try_nestloop_path().

1182 {
1183  ListCell *lc;
1184 
1185  foreach(lc, root->placeholder_list)
1186  {
1187  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
1188 
1189  if (!bms_is_subset(phinfo->ph_eval_at, inner_params))
1190  continue; /* ignore, could not be a nestloop param */
1191  if (!bms_overlap(phinfo->ph_eval_at, outer_relids))
1192  continue; /* ignore, not relevant to this join */
1193  if (bms_is_subset(phinfo->ph_eval_at, outer_relids))
1194  continue; /* safe, it can be eval'd within outerrel */
1195  /* Otherwise, it's potentially unsafe, so reject the join */
1196  return true;
1197  }
1198 
1199  /* OK to perform the join */
1200  return false;
1201 }
Relids ph_eval_at
Definition: pathnodes.h:2261
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
#define lfirst(lc)
Definition: pg_list.h:190
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
List * placeholder_list
Definition: pathnodes.h:292

◆ have_join_order_restriction()

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

Definition at line 947 of file joinrels.c.

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().

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

◆ have_partkey_equi_join()

bool have_partkey_equi_join ( RelOptInfo joinrel,
RelOptInfo rel1,
RelOptInfo rel2,
JoinType  jointype,
List restrictlist 
)

Definition at line 1582 of file joinrels.c.

References OpExpr::args, Assert, bms_is_subset(), RestrictInfo::can_join, castNode, RestrictInfo::clause, RestrictInfo::hashjoinoperator, IS_OUTER_JOIN, RestrictInfo::left_relids, lfirst_node, linitial, list_member_oid(), lsecond, match_expr_to_partition_keys(), RestrictInfo::mergeopfamilies, OidIsValid, op_in_opfamily(), op_strict(), OpExpr::opno, RelOptInfo::part_scheme, PARTITION_MAX_KEYS, PARTITION_STRATEGY_HASH, PartitionSchemeData::partnatts, PartitionSchemeData::partopfamily, RelOptInfo::relids, RestrictInfo::right_relids, RINFO_IS_PUSHED_DOWN, and PartitionSchemeData::strategy.

Referenced by build_joinrel_partition_info().

1585 {
1586  PartitionScheme part_scheme = rel1->part_scheme;
1587  ListCell *lc;
1588  int cnt_pks;
1589  bool pk_has_clause[PARTITION_MAX_KEYS];
1590  bool strict_op;
1591 
1592  /*
1593  * This function should be called when the joining relations have same
1594  * partitioning scheme.
1595  */
1596  Assert(rel1->part_scheme == rel2->part_scheme);
1597  Assert(part_scheme);
1598 
1599  memset(pk_has_clause, 0, sizeof(pk_has_clause));
1600  foreach(lc, restrictlist)
1601  {
1602  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1603  OpExpr *opexpr;
1604  Expr *expr1;
1605  Expr *expr2;
1606  int ipk1;
1607  int ipk2;
1608 
1609  /* If processing an outer join, only use its own join clauses. */
1610  if (IS_OUTER_JOIN(jointype) &&
1611  RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
1612  continue;
1613 
1614  /* Skip clauses which can not be used for a join. */
1615  if (!rinfo->can_join)
1616  continue;
1617 
1618  /* Skip clauses which are not equality conditions. */
1619  if (!rinfo->mergeopfamilies && !OidIsValid(rinfo->hashjoinoperator))
1620  continue;
1621 
1622  opexpr = castNode(OpExpr, rinfo->clause);
1623 
1624  /*
1625  * The equi-join between partition keys is strict if equi-join between
1626  * at least one partition key is using a strict operator. See
1627  * explanation about outer join reordering identity 3 in
1628  * optimizer/README
1629  */
1630  strict_op = op_strict(opexpr->opno);
1631 
1632  /* Match the operands to the relation. */
1633  if (bms_is_subset(rinfo->left_relids, rel1->relids) &&
1634  bms_is_subset(rinfo->right_relids, rel2->relids))
1635  {
1636  expr1 = linitial(opexpr->args);
1637  expr2 = lsecond(opexpr->args);
1638  }
1639  else if (bms_is_subset(rinfo->left_relids, rel2->relids) &&
1640  bms_is_subset(rinfo->right_relids, rel1->relids))
1641  {
1642  expr1 = lsecond(opexpr->args);
1643  expr2 = linitial(opexpr->args);
1644  }
1645  else
1646  continue;
1647 
1648  /*
1649  * Only clauses referencing the partition keys are useful for
1650  * partitionwise join.
1651  */
1652  ipk1 = match_expr_to_partition_keys(expr1, rel1, strict_op);
1653  if (ipk1 < 0)
1654  continue;
1655  ipk2 = match_expr_to_partition_keys(expr2, rel2, strict_op);
1656  if (ipk2 < 0)
1657  continue;
1658 
1659  /*
1660  * If the clause refers to keys at different ordinal positions, it can
1661  * not be used for partitionwise join.
1662  */
1663  if (ipk1 != ipk2)
1664  continue;
1665 
1666  /*
1667  * The clause allows partitionwise join if only it uses the same
1668  * operator family as that specified by the partition key.
1669  */
1671  {
1672  if (!op_in_opfamily(rinfo->hashjoinoperator,
1673  part_scheme->partopfamily[ipk1]))
1674  continue;
1675  }
1676  else if (!list_member_oid(rinfo->mergeopfamilies,
1677  part_scheme->partopfamily[ipk1]))
1678  continue;
1679 
1680  /* Mark the partition key as having an equi-join clause. */
1681  pk_has_clause[ipk1] = true;
1682  }
1683 
1684  /* Check whether every partition key has an equi-join condition. */
1685  for (cnt_pks = 0; cnt_pks < part_scheme->partnatts; cnt_pks++)
1686  {
1687  if (!pk_has_clause[cnt_pks])
1688  return false;
1689  }
1690 
1691  return true;
1692 }
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:63
bool op_strict(Oid opno)
Definition: lsyscache.c:1279
#define castNode(_type_, nodeptr)
Definition: nodes.h:593
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:740
#define PARTITION_MAX_KEYS
Relids left_relids
Definition: pathnodes.h:1970
#define OidIsValid(objectId)
Definition: c.h:638
List * mergeopfamilies
Definition: pathnodes.h:1988
#define lsecond(l)
Definition: pg_list.h:200
#define linitial(l)
Definition: pg_list.h:195
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
#define lfirst_node(type, lc)
Definition: pg_list.h:193
static int match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel, bool strict_op)
Definition: joinrels.c:1699
Relids relids
Definition: pathnodes.h:641
Expr * clause
Definition: pathnodes.h:1943
#define PARTITION_STRATEGY_HASH
Definition: parsenodes.h:798
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2020
Relids right_relids
Definition: pathnodes.h:1971
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:674
#define Assert(condition)
Definition: c.h:732
Oid hashjoinoperator
Definition: pathnodes.h:2001
PartitionScheme part_scheme
Definition: pathnodes.h:718
Oid opno
Definition: primnodes.h:502
List * args
Definition: primnodes.h:508

◆ is_dummy_rel()

bool is_dummy_rel ( RelOptInfo rel)

Definition at line 1208 of file joinrels.c.

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

Referenced by make_join_rel(), mark_dummy_rel(), and populate_joinrel_with_paths().

1209 {
1210  Path *path;
1211 
1212  /*
1213  * A rel that is known dummy will have just one path that is a childless
1214  * Append. (Even if somehow it has more paths, a childless Append will
1215  * have cost zero and hence should be at the front of the pathlist.)
1216  */
1217  if (rel->pathlist == NIL)
1218  return false;
1219  path = (Path *) linitial(rel->pathlist);
1220 
1221  /*
1222  * Initially, a dummy path will just be a childless Append. But in later
1223  * planning stages we might stick a ProjectSetPath and/or ProjectionPath
1224  * on top, since Append can't project. Rather than make assumptions about
1225  * which combinations can occur, just descend through whatever we find.
1226  */
1227  for (;;)
1228  {
1229  if (IsA(path, ProjectionPath))
1230  path = ((ProjectionPath *) path)->subpath;
1231  else if (IsA(path, ProjectSetPath))
1232  path = ((ProjectSetPath *) path)->subpath;
1233  else
1234  break;
1235  }
1236  if (IS_DUMMY_APPEND(path))
1237  return true;
1238  return false;
1239 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:575
#define linitial(l)
Definition: pg_list.h:195
#define IS_DUMMY_APPEND(p)
Definition: pathnodes.h:1380
List * pathlist
Definition: pathnodes.h:655
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241

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

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().

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

◆ join_search_one_level()

void join_search_one_level ( PlannerInfo root,
int  level 
)

Definition at line 67 of file joinrels.c.

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, PlannerInfo::join_rel_level, 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().

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

◆ make_join_rel()

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

Definition at line 682 of file joinrels.c.

References Assert, bms_free(), bms_overlap(), bms_union(), build_join_rel(), SpecialJoinInfo::delay_upper_joins, is_dummy_rel(), JOIN_INNER, join_is_legal(), SpecialJoinInfo::jointype, SpecialJoinInfo::lhs_strict, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, NIL, populate_joinrel_with_paths(), RelOptInfo::relids, SpecialJoinInfo::semi_can_btree, SpecialJoinInfo::semi_can_hash, SpecialJoinInfo::semi_operators, SpecialJoinInfo::semi_rhs_exprs, SpecialJoinInfo::syn_lefthand, SpecialJoinInfo::syn_righthand, T_SpecialJoinInfo, and SpecialJoinInfo::type.

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

683 {
684  Relids joinrelids;
685  SpecialJoinInfo *sjinfo;
686  bool reversed;
687  SpecialJoinInfo sjinfo_data;
688  RelOptInfo *joinrel;
689  List *restrictlist;
690 
691  /* We should never try to join two overlapping sets of rels. */
692  Assert(!bms_overlap(rel1->relids, rel2->relids));
693 
694  /* Construct Relids set that identifies the joinrel. */
695  joinrelids = bms_union(rel1->relids, rel2->relids);
696 
697  /* Check validity and determine join type. */
698  if (!join_is_legal(root, rel1, rel2, joinrelids,
699  &sjinfo, &reversed))
700  {
701  /* invalid join path */
702  bms_free(joinrelids);
703  return NULL;
704  }
705 
706  /* Swap rels if needed to match the join info. */
707  if (reversed)
708  {
709  RelOptInfo *trel = rel1;
710 
711  rel1 = rel2;
712  rel2 = trel;
713  }
714 
715  /*
716  * If it's a plain inner join, then we won't have found anything in
717  * join_info_list. Make up a SpecialJoinInfo so that selectivity
718  * estimation functions will know what's being joined.
719  */
720  if (sjinfo == NULL)
721  {
722  sjinfo = &sjinfo_data;
723  sjinfo->type = T_SpecialJoinInfo;
724  sjinfo->min_lefthand = rel1->relids;
725  sjinfo->min_righthand = rel2->relids;
726  sjinfo->syn_lefthand = rel1->relids;
727  sjinfo->syn_righthand = rel2->relids;
728  sjinfo->jointype = JOIN_INNER;
729  /* we don't bother trying to make the remaining fields valid */
730  sjinfo->lhs_strict = false;
731  sjinfo->delay_upper_joins = false;
732  sjinfo->semi_can_btree = false;
733  sjinfo->semi_can_hash = false;
734  sjinfo->semi_operators = NIL;
735  sjinfo->semi_rhs_exprs = NIL;
736  }
737 
738  /*
739  * Find or build the join RelOptInfo, and compute the restrictlist that
740  * goes with this particular joining.
741  */
742  joinrel = build_join_rel(root, joinrelids, rel1, rel2, sjinfo,
743  &restrictlist);
744 
745  /*
746  * If we've already proven this join is empty, we needn't consider any
747  * more paths for it.
748  */
749  if (is_dummy_rel(joinrel))
750  {
751  bms_free(joinrelids);
752  return joinrel;
753  }
754 
755  /* Add paths to the join relation. */
756  populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
757  restrictlist);
758 
759  bms_free(joinrelids);
760 
761  return joinrel;
762 }
#define NIL
Definition: pg_list.h:65
static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: joinrels.c:772
Relids min_righthand
Definition: pathnodes.h:2134
Relids syn_lefthand
Definition: pathnodes.h:2135
Relids syn_righthand
Definition: pathnodes.h:2136
List * semi_rhs_exprs
Definition: pathnodes.h:2144
static bool join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, Relids joinrelids, SpecialJoinInfo **sjinfo_p, bool *reversed_p)
Definition: joinrels.c:360
Relids relids
Definition: pathnodes.h:641
bool delay_upper_joins
Definition: pathnodes.h:2139
RelOptInfo * build_join_rel(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List **restrictlist_ptr)
Definition: relnode.c:566
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define Assert(condition)
Definition: c.h:732
bool is_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1208
JoinType jointype
Definition: pathnodes.h:2137
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
List * semi_operators
Definition: pathnodes.h:2143
Definition: pg_list.h:50
Relids min_lefthand
Definition: pathnodes.h:2133

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

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().

297 {
298  ListCell *l;
299 
300  for_each_cell(l, other_rels_list, other_rels)
301  {
302  RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
303 
304  if (!bms_overlap(old_rel->relids, other_rel->relids) &&
305  (have_relevant_joinclause(root, old_rel, other_rel) ||
306  have_join_order_restriction(root, old_rel, other_rel)))
307  {
308  (void) make_join_rel(root, old_rel, other_rel);
309  }
310  }
311 }
#define for_each_cell(cell, lst, initcell)
Definition: pg_list.h:390
bool have_join_order_restriction(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joinrels.c:947
Relids relids
Definition: pathnodes.h:641
RelOptInfo * make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joinrels.c:682
#define lfirst(lc)
Definition: pg_list.h:190
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
bool have_relevant_joinclause(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joininfo.c:36

◆ make_rels_by_clauseless_joins()

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

Definition at line 327 of file joinrels.c.

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

Referenced by join_search_one_level().

330 {
331  ListCell *l;
332 
333  foreach(l, other_rels)
334  {
335  RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
336 
337  if (!bms_overlap(other_rel->relids, old_rel->relids))
338  {
339  (void) make_join_rel(root, old_rel, other_rel);
340  }
341  }
342 }
Relids relids
Definition: pathnodes.h:641
RelOptInfo * make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joinrels.c:682
#define lfirst(lc)
Definition: pg_list.h:190
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494

◆ mark_dummy_rel()

void mark_dummy_rel ( RelOptInfo rel)

Definition at line 1257 of file joinrels.c.

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().

1258 {
1259  MemoryContext oldcontext;
1260 
1261  /* Already marked? */
1262  if (is_dummy_rel(rel))
1263  return;
1264 
1265  /* No, so choose correct context to make the dummy path in */
1266  oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
1267 
1268  /* Set dummy size estimate */
1269  rel->rows = 0;
1270 
1271  /* Evict any previously chosen paths */
1272  rel->pathlist = NIL;
1273  rel->partial_pathlist = NIL;
1274 
1275  /* Set up the dummy path */
1276  add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL,
1277  NIL, rel->lateral_relids,
1278  0, false, NIL, -1));
1279 
1280  /* Set or update cheapest_total_path and related fields */
1281  set_cheapest(rel);
1282 
1283  MemoryContextSwitchTo(oldcontext);
1284 }
#define NIL
Definition: pg_list.h:65
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:423
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
List * partial_pathlist
Definition: pathnodes.h:657
Relids lateral_relids
Definition: pathnodes.h:666
AppendPath * create_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *partial_subpaths, List *pathkeys, Relids required_outer, int parallel_workers, bool parallel_aware, List *partitioned_rels, double rows)
Definition: pathnode.c:1184
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:245
double rows
Definition: pathnodes.h:644
bool is_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1208
List * pathlist
Definition: pathnodes.h:655
static MemoryContext GetMemoryChunkContext(void *pointer)
Definition: memutils.h:112

◆ match_expr_to_partition_keys()

static int match_expr_to_partition_keys ( Expr expr,
RelOptInfo rel,
bool  strict_op 
)
static

Definition at line 1699 of file joinrels.c.

References arg, Assert, castNode, equal(), IsA, lfirst, RelOptInfo::nullable_partexprs, RelOptInfo::part_scheme, RelOptInfo::partexprs, and PartitionSchemeData::partnatts.

Referenced by have_partkey_equi_join().

1700 {
1701  int cnt;
1702 
1703  /* This function should be called only for partitioned relations. */
1704  Assert(rel->part_scheme);
1705 
1706  /* Remove any relabel decorations. */
1707  while (IsA(expr, RelabelType))
1708  expr = (Expr *) (castNode(RelabelType, expr))->arg;
1709 
1710  for (cnt = 0; cnt < rel->part_scheme->partnatts; cnt++)
1711  {
1712  ListCell *lc;
1713 
1714  Assert(rel->partexprs);
1715  foreach(lc, rel->partexprs[cnt])
1716  {
1717  if (equal(lfirst(lc), expr))
1718  return cnt;
1719  }
1720 
1721  if (!strict_op)
1722  continue;
1723 
1724  /*
1725  * If it's a strict equi-join a NULL partition key on one side will
1726  * not join a NULL partition key on the other side. So, rows with NULL
1727  * partition key from a partition on one side can not join with those
1728  * from a non-matching partition on the other side. So, search the
1729  * nullable partition keys as well.
1730  */
1731  Assert(rel->nullable_partexprs);
1732  foreach(lc, rel->nullable_partexprs[cnt])
1733  {
1734  if (equal(lfirst(lc), expr))
1735  return cnt;
1736  }
1737  }
1738 
1739  return -1;
1740 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:575
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2998
#define castNode(_type_, nodeptr)
Definition: nodes.h:593
List ** nullable_partexprs
Definition: pathnodes.h:725
List ** partexprs
Definition: pathnodes.h:724
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
void * arg
PartitionScheme part_scheme
Definition: pathnodes.h:718

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

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().

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

◆ restriction_is_constant_false()

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

Definition at line 1300 of file joinrels.c.

References RestrictInfo::clause, Const::constisnull, Const::constvalue, DatumGetBool, IsA, lfirst_node, RelOptInfo::relids, and RINFO_IS_PUSHED_DOWN.

Referenced by populate_joinrel_with_paths().

1303 {
1304  ListCell *lc;
1305 
1306  /*
1307  * Despite the above comment, the restriction list we see here might
1308  * possibly have other members besides the FALSE constant, since other
1309  * quals could get "pushed down" to the outer join level. So we check
1310  * each member of the list.
1311  */
1312  foreach(lc, restrictlist)
1313  {
1314  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1315 
1316  if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
1317  continue;
1318 
1319  if (rinfo->clause && IsA(rinfo->clause, Const))
1320  {
1321  Const *con = (Const *) rinfo->clause;
1322 
1323  /* constant NULL is as good as constant FALSE for our purposes */
1324  if (con->constisnull)
1325  return true;
1326  if (!DatumGetBool(con->constvalue))
1327  return true;
1328  }
1329  }
1330  return false;
1331 }
Datum constvalue
Definition: primnodes.h:200
#define IsA(nodeptr, _type_)
Definition: nodes.h:575
#define lfirst_node(type, lc)
Definition: pg_list.h:193
#define DatumGetBool(X)
Definition: postgres.h:393
Relids relids
Definition: pathnodes.h:641
Expr * clause
Definition: pathnodes.h:1943
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2020
bool constisnull
Definition: primnodes.h:201

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

References adjust_appendrel_attrs(), Assert, bms_equal(), bms_overlap(), bms_union(), RelOptInfo::boundinfo, build_child_join_rel(), build_child_join_sjinfo(), check_stack_depth(), 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, RelOptInfo::nparts, RelOptInfo::part_rels, RelOptInfo::part_scheme, partition_bounds_equal(), PartitionSchemeData::partnatts, PartitionSchemeData::parttypbyval, PartitionSchemeData::parttyplen, 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().

1357 {
1358  bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1359  bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1360  int nparts;
1361  int cnt_parts;
1362 
1363  /* Guard against stack overflow due to overly deep partition hierarchy. */
1365 
1366  /* Nothing to do, if the join relation is not partitioned. */
1367  if (!IS_PARTITIONED_REL(joinrel))
1368  return;
1369 
1370  /* The join relation should have consider_partitionwise_join set. */
1372 
1373  /*
1374  * Since this join relation is partitioned, all the base relations
1375  * participating in this join must be partitioned and so are all the
1376  * intermediate join relations.
1377  */
1380 
1381  /* The joining relations should have consider_partitionwise_join set. */
1384 
1385  /*
1386  * The partition scheme of the join relation should match that of the
1387  * joining relations.
1388  */
1389  Assert(joinrel->part_scheme == rel1->part_scheme &&
1390  joinrel->part_scheme == rel2->part_scheme);
1391 
1392  /*
1393  * Since we allow partitionwise join only when the partition bounds of the
1394  * joining relations exactly match, the partition bounds of the join
1395  * should match those of the joining relations.
1396  */
1398  joinrel->part_scheme->parttyplen,
1399  joinrel->part_scheme->parttypbyval,
1400  joinrel->boundinfo, rel1->boundinfo));
1402  joinrel->part_scheme->parttyplen,
1403  joinrel->part_scheme->parttypbyval,
1404  joinrel->boundinfo, rel2->boundinfo));
1405 
1406  nparts = joinrel->nparts;
1407 
1408  /*
1409  * Create child-join relations for this partitioned join, if those don't
1410  * exist. Add paths to child-joins for a pair of child relations
1411  * corresponding to the given pair of parent relations.
1412  */
1413  for (cnt_parts = 0; cnt_parts < nparts; cnt_parts++)
1414  {
1415  RelOptInfo *child_rel1 = rel1->part_rels[cnt_parts];
1416  RelOptInfo *child_rel2 = rel2->part_rels[cnt_parts];
1417  bool rel1_empty = (child_rel1 == NULL ||
1418  IS_DUMMY_REL(child_rel1));
1419  bool rel2_empty = (child_rel2 == NULL ||
1420  IS_DUMMY_REL(child_rel2));
1421  SpecialJoinInfo *child_sjinfo;
1422  List *child_restrictlist;
1423  RelOptInfo *child_joinrel;
1424  Relids child_joinrelids;
1425  AppendRelInfo **appinfos;
1426  int nappinfos;
1427 
1428  /*
1429  * Check for cases where we can prove that this segment of the join
1430  * returns no rows, due to one or both inputs being empty (including
1431  * inputs that have been pruned away entirely). If so just ignore it.
1432  * These rules are equivalent to populate_joinrel_with_paths's rules
1433  * for dummy input relations.
1434  */
1435  switch (parent_sjinfo->jointype)
1436  {
1437  case JOIN_INNER:
1438  case JOIN_SEMI:
1439  if (rel1_empty || rel2_empty)
1440  continue; /* ignore this join segment */
1441  break;
1442  case JOIN_LEFT:
1443  case JOIN_ANTI:
1444  if (rel1_empty)
1445  continue; /* ignore this join segment */
1446  break;
1447  case JOIN_FULL:
1448  if (rel1_empty && rel2_empty)
1449  continue; /* ignore this join segment */
1450  break;
1451  default:
1452  /* other values not expected here */
1453  elog(ERROR, "unrecognized join type: %d",
1454  (int) parent_sjinfo->jointype);
1455  break;
1456  }
1457 
1458  /*
1459  * If a child has been pruned entirely then we can't generate paths
1460  * for it, so we have to reject partitionwise joining unless we were
1461  * able to eliminate this partition above.
1462  */
1463  if (child_rel1 == NULL || child_rel2 == NULL)
1464  {
1465  /*
1466  * Mark the joinrel as unpartitioned so that later functions treat
1467  * it correctly.
1468  */
1469  joinrel->nparts = 0;
1470  return;
1471  }
1472 
1473  /*
1474  * If a leaf relation has consider_partitionwise_join=false, it means
1475  * that it's a dummy relation for which we skipped setting up tlist
1476  * expressions and adding EC members in set_append_rel_size(), so
1477  * again we have to fail here.
1478  */
1479  if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
1480  {
1481  Assert(child_rel1->reloptkind == RELOPT_OTHER_MEMBER_REL);
1482  Assert(IS_DUMMY_REL(child_rel1));
1483  joinrel->nparts = 0;
1484  return;
1485  }
1486  if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
1487  {
1488  Assert(child_rel2->reloptkind == RELOPT_OTHER_MEMBER_REL);
1489  Assert(IS_DUMMY_REL(child_rel2));
1490  joinrel->nparts = 0;
1491  return;
1492  }
1493 
1494  /* We should never try to join two overlapping sets of rels. */
1495  Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
1496  child_joinrelids = bms_union(child_rel1->relids, child_rel2->relids);
1497  appinfos = find_appinfos_by_relids(root, child_joinrelids, &nappinfos);
1498 
1499  /*
1500  * Construct SpecialJoinInfo from parent join relations's
1501  * SpecialJoinInfo.
1502  */
1503  child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
1504  child_rel1->relids,
1505  child_rel2->relids);
1506 
1507  /*
1508  * Construct restrictions applicable to the child join from those
1509  * applicable to the parent join.
1510  */
1511  child_restrictlist =
1512  (List *) adjust_appendrel_attrs(root,
1513  (Node *) parent_restrictlist,
1514  nappinfos, appinfos);
1515  pfree(appinfos);
1516 
1517  child_joinrel = joinrel->part_rels[cnt_parts];
1518  if (!child_joinrel)
1519  {
1520  child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
1521  joinrel, child_restrictlist,
1522  child_sjinfo,
1523  child_sjinfo->jointype);
1524  joinrel->part_rels[cnt_parts] = child_joinrel;
1525  }
1526 
1527  Assert(bms_equal(child_joinrel->relids, child_joinrelids));
1528 
1529  populate_joinrel_with_paths(root, child_rel1, child_rel2,
1530  child_joinrel, child_sjinfo,
1531  child_restrictlist);
1532  }
1533 }
RelOptKind reloptkind
Definition: pathnodes.h:638
static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: joinrels.c:772
Definition: nodes.h:524
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:614
static SpecialJoinInfo * build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo, Relids left_relids, Relids right_relids)
Definition: joinrels.c:1541
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1388
RelOptInfo * build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel, RelOptInfo *inner_rel, RelOptInfo *parent_joinrel, List *restrictlist, SpecialJoinInfo *sjinfo, JoinType jointype)
Definition: relnode.c:772
void check_stack_depth(void)
Definition: postgres.c:3262
int nparts
Definition: pathnodes.h:719
Relids relids
Definition: pathnodes.h:641
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:709
bool consider_partitionwise_join
Definition: pathnodes.h:712
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:720
#define Assert(condition)
Definition: c.h:732
JoinType jointype
Definition: pathnodes.h:2137
struct RelOptInfo ** part_rels
Definition: pathnodes.h:722
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
bool partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval, PartitionBoundInfo b1, PartitionBoundInfo b2)
Definition: partbounds.c:668
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:737
#define REL_HAS_ALL_PART_PROPS(rel)
Definition: pathnodes.h:745
#define elog(elevel,...)
Definition: elog.h:226
PartitionScheme part_scheme
Definition: pathnodes.h:718
Definition: pg_list.h:50
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:180