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

1562 {
1564  AppendRelInfo **left_appinfos;
1565  int left_nappinfos;
1566  AppendRelInfo **right_appinfos;
1567  int right_nappinfos;
1568 
1569  memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
1570  left_appinfos = find_appinfos_by_relids(root, left_relids,
1571  &left_nappinfos);
1572  right_appinfos = find_appinfos_by_relids(root, right_relids,
1573  &right_nappinfos);
1574 
1575  sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand,
1576  left_nappinfos, left_appinfos);
1578  right_nappinfos,
1579  right_appinfos);
1580  sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand,
1581  left_nappinfos, left_appinfos);
1583  right_nappinfos,
1584  right_appinfos);
1585  sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root,
1586  (Node *) sjinfo->semi_rhs_exprs,
1587  right_nappinfos,
1588  right_appinfos);
1589 
1590  pfree(left_appinfos);
1591  pfree(right_appinfos);
1592 
1593  return sjinfo;
1594 }
Relids min_righthand
Definition: pathnodes.h:2185
Definition: nodes.h:527
Relids syn_lefthand
Definition: pathnodes.h:2186
Relids syn_righthand
Definition: pathnodes.h:2187
void pfree(void *pointer)
Definition: mcxt.c:1057
List * semi_rhs_exprs
Definition: pathnodes.h:2195
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:728
#define makeNode(_type_)
Definition: nodes.h:575
Relids adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:541
Definition: pg_list.h:50
Relids min_lefthand
Definition: pathnodes.h:2184
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:194

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

References Assert, RelOptInfo::boundinfo, get_matching_part_pairs(), SpecialJoinInfo::jointype, list_length(), RelOptInfo::nparts, palloc0(), RelOptInfo::part_rels, RelOptInfo::part_scheme, 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().

1605 {
1606  /*
1607  * If we don't have the partition bounds for the join rel yet, try to
1608  * compute those along with pairs of partitions to be joined.
1609  */
1610  if (joinrel->nparts == -1)
1611  {
1612  PartitionScheme part_scheme = joinrel->part_scheme;
1613  PartitionBoundInfo boundinfo = NULL;
1614  int nparts = 0;
1615 
1616  Assert(joinrel->boundinfo == NULL);
1617  Assert(joinrel->part_rels == NULL);
1618 
1619  /*
1620  * See if the partition bounds for inputs are exactly the same, in
1621  * which case we don't need to work hard: the join rel have the same
1622  * partition bounds as inputs, and the partitions with the same
1623  * cardinal positions form the pairs.
1624  *
1625  * Note: even in cases where one or both inputs have merged bounds, it
1626  * would be possible for both the bounds to be exactly the same, but
1627  * it seems unlikely to be worth the cycles to check.
1628  */
1629  if (!rel1->partbounds_merged &&
1630  !rel2->partbounds_merged &&
1631  rel1->nparts == rel2->nparts &&
1632  partition_bounds_equal(part_scheme->partnatts,
1633  part_scheme->parttyplen,
1634  part_scheme->parttypbyval,
1635  rel1->boundinfo, rel2->boundinfo))
1636  {
1637  boundinfo = rel1->boundinfo;
1638  nparts = rel1->nparts;
1639  }
1640  else
1641  {
1642  /* Try merging the partition bounds for inputs. */
1643  boundinfo = partition_bounds_merge(part_scheme->partnatts,
1644  part_scheme->partsupfunc,
1645  part_scheme->partcollation,
1646  rel1, rel2,
1647  parent_sjinfo->jointype,
1648  parts1, parts2);
1649  if (boundinfo == NULL)
1650  {
1651  joinrel->nparts = 0;
1652  return;
1653  }
1654  nparts = list_length(*parts1);
1655  joinrel->partbounds_merged = true;
1656  }
1657 
1658  Assert(nparts > 0);
1659  joinrel->boundinfo = boundinfo;
1660  joinrel->nparts = nparts;
1661  joinrel->part_rels =
1662  (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * nparts);
1663  }
1664  else
1665  {
1666  Assert(joinrel->nparts > 0);
1667  Assert(joinrel->boundinfo);
1668  Assert(joinrel->part_rels);
1669 
1670  /*
1671  * If the join rel's partbounds_merged flag is true, it means inputs
1672  * are not guaranteed to have the same partition bounds, therefore we
1673  * can't assume that the partitions at the same cardinal positions
1674  * form the pairs; let get_matching_part_pairs() generate the pairs.
1675  * Otherwise, nothing to do since we can assume that.
1676  */
1677  if (joinrel->partbounds_merged)
1678  {
1679  get_matching_part_pairs(root, joinrel, rel1, rel2,
1680  parts1, parts2);
1681  Assert(list_length(*parts1) == joinrel->nparts);
1682  Assert(list_length(*parts2) == joinrel->nparts);
1683  }
1684  }
1685 }
static void get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, List **parts1, List **parts2)
Definition: joinrels.c:1692
int nparts
Definition: pathnodes.h:744
bool partbounds_merged
Definition: pathnodes.h:748
void * palloc0(Size size)
Definition: mcxt.c:981
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:747
#define Assert(condition)
Definition: c.h:800
struct FmgrInfo * partsupfunc
Definition: pathnodes.h:408
JoinType jointype
Definition: pathnodes.h:2188
struct RelOptInfo ** part_rels
Definition: pathnodes.h:751
static int list_length(const List *l)
Definition: pg_list.h:149
bool partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval, PartitionBoundInfo b1, PartitionBoundInfo b2)
Definition: partbounds.c:799
PartitionScheme part_scheme
Definition: pathnodes.h:743
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:1017

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

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

Referenced by compute_partition_bounds().

1695 {
1696  bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1697  bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1698  int cnt_parts;
1699 
1700  *parts1 = NIL;
1701  *parts2 = NIL;
1702 
1703  for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1704  {
1705  RelOptInfo *child_joinrel = joinrel->part_rels[cnt_parts];
1706  RelOptInfo *child_rel1;
1707  RelOptInfo *child_rel2;
1708  Relids child_relids1;
1709  Relids child_relids2;
1710 
1711  /*
1712  * If this segment of the join is empty, it means that this segment
1713  * was ignored when previously creating child-join paths for it in
1714  * try_partitionwise_join() as it would not contribute to the join
1715  * result, due to one or both inputs being empty; add NULL to each of
1716  * the given lists so that this segment will be ignored again in that
1717  * function.
1718  */
1719  if (!child_joinrel)
1720  {
1721  *parts1 = lappend(*parts1, NULL);
1722  *parts2 = lappend(*parts2, NULL);
1723  continue;
1724  }
1725 
1726  /*
1727  * Get a relids set of partition(s) involved in this join segment that
1728  * are from the rel1 side.
1729  */
1730  child_relids1 = bms_intersect(child_joinrel->relids,
1731  rel1->all_partrels);
1732  Assert(bms_num_members(child_relids1) == bms_num_members(rel1->relids));
1733 
1734  /*
1735  * Get a child rel for rel1 with the relids. Note that we should have
1736  * the child rel even if rel1 is a join rel, because in that case the
1737  * partitions specified in the relids would have matching/overlapping
1738  * boundaries, so the specified partitions should be considered as
1739  * ones to be joined when planning partitionwise joins of rel1,
1740  * meaning that the child rel would have been built by the time we get
1741  * here.
1742  */
1743  if (rel1_is_simple)
1744  {
1745  int varno = bms_singleton_member(child_relids1);
1746 
1747  child_rel1 = find_base_rel(root, varno);
1748  }
1749  else
1750  child_rel1 = find_join_rel(root, child_relids1);
1751  Assert(child_rel1);
1752 
1753  /*
1754  * Get a relids set of partition(s) involved in this join segment that
1755  * are from the rel2 side.
1756  */
1757  child_relids2 = bms_intersect(child_joinrel->relids,
1758  rel2->all_partrels);
1759  Assert(bms_num_members(child_relids2) == bms_num_members(rel2->relids));
1760 
1761  /*
1762  * Get a child rel for rel2 with the relids. See above comments.
1763  */
1764  if (rel2_is_simple)
1765  {
1766  int varno = bms_singleton_member(child_relids2);
1767 
1768  child_rel2 = find_base_rel(root, varno);
1769  }
1770  else
1771  child_rel2 = find_join_rel(root, child_relids2);
1772  Assert(child_rel2);
1773 
1774  /*
1775  * The join of rel1 and rel2 is legal, so is the join of the child
1776  * rels obtained above; add them to the given lists as a join pair
1777  * producing this join segment.
1778  */
1779  *parts1 = lappend(*parts1, child_rel1);
1780  *parts2 = lappend(*parts2, child_rel2);
1781  }
1782 }
#define NIL
Definition: pg_list.h:65
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:438
Relids all_partrels
Definition: pathnodes.h:753
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:639
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
int nparts
Definition: pathnodes.h:744
Relids relids
Definition: pathnodes.h:666
List * lappend(List *list, void *datum)
Definition: list.c:321
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:577
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
#define Assert(condition)
Definition: c.h:800
struct RelOptInfo ** part_rels
Definition: pathnodes.h:751
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:373

◆ has_join_restriction()

static bool has_join_restriction ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 1064 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().

1065 {
1066  ListCell *l;
1067 
1068  if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
1069  return true;
1070 
1071  foreach(l, root->placeholder_list)
1072  {
1073  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1074 
1075  if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
1076  !bms_equal(rel->relids, phinfo->ph_eval_at))
1077  return true;
1078  }
1079 
1080  foreach(l, root->join_info_list)
1081  {
1082  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1083 
1084  /* ignore full joins --- other mechanisms preserve their ordering */
1085  if (sjinfo->jointype == JOIN_FULL)
1086  continue;
1087 
1088  /* ignore if SJ is already contained in rel */
1089  if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
1090  bms_is_subset(sjinfo->min_righthand, rel->relids))
1091  continue;
1092 
1093  /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
1094  if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
1095  bms_overlap(sjinfo->min_righthand, rel->relids))
1096  return true;
1097  }
1098 
1099  return false;
1100 }
Relids ph_eval_at
Definition: pathnodes.h:2317
List * join_info_list
Definition: pathnodes.h:277
Relids min_righthand
Definition: pathnodes.h:2185
Relids lateral_relids
Definition: pathnodes.h:691
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids relids
Definition: pathnodes.h:666
Relids lateral_referencers
Definition: pathnodes.h:702
#define lfirst(lc)
Definition: pg_list.h:169
JoinType jointype
Definition: pathnodes.h:2188
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
List * placeholder_list
Definition: pathnodes.h:288
Relids min_lefthand
Definition: pathnodes.h:2184
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 1120 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().

1121 {
1122  ListCell *lc;
1123 
1124  foreach(lc, root->initial_rels)
1125  {
1126  RelOptInfo *rel2 = (RelOptInfo *) lfirst(lc);
1127 
1128  /* ignore rels that are already in "rel" */
1129  if (bms_overlap(rel->relids, rel2->relids))
1130  continue;
1131 
1132  if (have_relevant_joinclause(root, rel, rel2))
1133  {
1134  Relids joinrelids;
1135  SpecialJoinInfo *sjinfo;
1136  bool reversed;
1137 
1138  /* join_is_legal needs relids of the union */
1139  joinrelids = bms_union(rel->relids, rel2->relids);
1140 
1141  if (join_is_legal(root, rel, rel2, joinrelids,
1142  &sjinfo, &reversed))
1143  {
1144  /* Yes, this will work */
1145  bms_free(joinrelids);
1146  return true;
1147  }
1148 
1149  bms_free(joinrelids);
1150  }
1151  }
1152 
1153  return false;
1154 }
static bool join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, Relids joinrelids, SpecialJoinInfo **sjinfo_p, bool *reversed_p)
Definition: joinrels.c:364
Relids relids
Definition: pathnodes.h:666
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define lfirst(lc)
Definition: pg_list.h:169
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:302

◆ have_dangerous_phv()

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

Definition at line 1184 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().

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

◆ have_join_order_restriction()

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

Definition at line 951 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().

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

◆ is_dummy_rel()

bool is_dummy_rel ( RelOptInfo rel)

Definition at line 1212 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().

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

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

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 }
bool has_eclass_joins
Definition: pathnodes.h:734
#define NIL
Definition: pg_list.h:65
int join_cur_level
Definition: pathnodes.h:251
List * join_info_list
Definition: pathnodes.h:277
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:310
#define for_each_cell(cell, lst, initcell)
Definition: pg_list.h:405
bool have_join_order_restriction(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joinrels.c:951
#define ERROR
Definition: elog.h:43
static bool has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
Definition: joinrels.c:1064
bool hasLateralRTEs
Definition: pathnodes.h:340
List * joininfo
Definition: pathnodes.h:732
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
Relids relids
Definition: pathnodes.h:666
static void make_rels_by_clauseless_joins(PlannerInfo *root, RelOptInfo *old_rel, List *other_rels)
Definition: joinrels.c:331
static void make_rels_by_clause_joins(PlannerInfo *root, RelOptInfo *old_rel, List *other_rels_list, ListCell *other_rels)
Definition: joinrels.c:297
RelOptInfo * make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joinrels.c:686
#define Assert(condition)
Definition: c.h:800
#define lfirst(lc)
Definition: pg_list.h:169
List ** join_rel_level
Definition: pathnodes.h:250
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
#define elog(elevel,...)
Definition: elog.h:228
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 686 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().

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

◆ 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.

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

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 }
#define for_each_cell(cell, lst, initcell)
Definition: pg_list.h:405
bool have_join_order_restriction(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joinrels.c:951
Relids relids
Definition: pathnodes.h:666
RelOptInfo * make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joinrels.c:686
#define lfirst(lc)
Definition: pg_list.h:169
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 331 of file joinrels.c.

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

Referenced by join_search_one_level().

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 }
Relids relids
Definition: pathnodes.h:666
RelOptInfo * make_join_rel(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joinrels.c:686
#define lfirst(lc)
Definition: pg_list.h:169
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 1261 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().

1262 {
1263  MemoryContext oldcontext;
1264 
1265  /* Already marked? */
1266  if (is_dummy_rel(rel))
1267  return;
1268 
1269  /* No, so choose correct context to make the dummy path in */
1270  oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
1271 
1272  /* Set dummy size estimate */
1273  rel->rows = 0;
1274 
1275  /* Evict any previously chosen paths */
1276  rel->pathlist = NIL;
1277  rel->partial_pathlist = NIL;
1278 
1279  /* Set up the dummy path */
1280  add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL,
1281  NIL, rel->lateral_relids,
1282  0, false, NIL, -1));
1283 
1284  /* Set or update cheapest_total_path and related fields */
1285  set_cheapest(rel);
1286 
1287  MemoryContextSwitchTo(oldcontext);
1288 }
#define NIL
Definition: pg_list.h:65
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
List * partial_pathlist
Definition: pathnodes.h:682
Relids lateral_relids
Definition: pathnodes.h:691
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:1215
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
double rows
Definition: pathnodes.h:669
bool is_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1212
List * pathlist
Definition: pathnodes.h:680
static MemoryContext GetMemoryChunkContext(void *pointer)
Definition: memutils.h:113

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

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

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

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

References adjust_appendrel_attrs(), RelOptInfo::all_partrels, Assert, 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(), lnext(), NIL, RelOptInfo::nparts, RelOptInfo::part_rels, RelOptInfo::part_scheme, 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().

1361 {
1362  bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1363  bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1364  List *parts1 = NIL;
1365  List *parts2 = NIL;
1366  ListCell *lcr1 = NULL;
1367  ListCell *lcr2 = NULL;
1368  int cnt_parts;
1369 
1370  /* Guard against stack overflow due to overly deep partition hierarchy. */
1372 
1373  /* Nothing to do, if the join relation is not partitioned. */
1374  if (joinrel->part_scheme == NULL || joinrel->nparts == 0)
1375  return;
1376 
1377  /* The join relation should have consider_partitionwise_join set. */
1379 
1380  /*
1381  * We can not perform partitionwise join if either of the joining
1382  * relations is not partitioned.
1383  */
1384  if (!IS_PARTITIONED_REL(rel1) || !IS_PARTITIONED_REL(rel2))
1385  return;
1386 
1388 
1389  /* The joining relations should have consider_partitionwise_join set. */
1392 
1393  /*
1394  * The partition scheme of the join relation should match that of the
1395  * joining relations.
1396  */
1397  Assert(joinrel->part_scheme == rel1->part_scheme &&
1398  joinrel->part_scheme == rel2->part_scheme);
1399 
1400  Assert(!(joinrel->partbounds_merged && (joinrel->nparts <= 0)));
1401 
1402  compute_partition_bounds(root, rel1, rel2, joinrel, parent_sjinfo,
1403  &parts1, &parts2);
1404 
1405  if (joinrel->partbounds_merged)
1406  {
1407  lcr1 = list_head(parts1);
1408  lcr2 = list_head(parts2);
1409  }
1410 
1411  /*
1412  * Create child-join relations for this partitioned join, if those don't
1413  * exist. Add paths to child-joins for a pair of child relations
1414  * corresponding to the given pair of parent relations.
1415  */
1416  for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1417  {
1418  RelOptInfo *child_rel1;
1419  RelOptInfo *child_rel2;
1420  bool rel1_empty;
1421  bool rel2_empty;
1422  SpecialJoinInfo *child_sjinfo;
1423  List *child_restrictlist;
1424  RelOptInfo *child_joinrel;
1425  Relids child_joinrelids;
1426  AppendRelInfo **appinfos;
1427  int nappinfos;
1428 
1429  if (joinrel->partbounds_merged)
1430  {
1431  child_rel1 = lfirst_node(RelOptInfo, lcr1);
1432  child_rel2 = lfirst_node(RelOptInfo, lcr2);
1433  lcr1 = lnext(parts1, lcr1);
1434  lcr2 = lnext(parts2, lcr2);
1435  }
1436  else
1437  {
1438  child_rel1 = rel1->part_rels[cnt_parts];
1439  child_rel2 = rel2->part_rels[cnt_parts];
1440  }
1441 
1442  rel1_empty = (child_rel1 == NULL || IS_DUMMY_REL(child_rel1));
1443  rel2_empty = (child_rel2 == NULL || IS_DUMMY_REL(child_rel2));
1444 
1445  /*
1446  * Check for cases where we can prove that this segment of the join
1447  * returns no rows, due to one or both inputs being empty (including
1448  * inputs that have been pruned away entirely). If so just ignore it.
1449  * These rules are equivalent to populate_joinrel_with_paths's rules
1450  * for dummy input relations.
1451  */
1452  switch (parent_sjinfo->jointype)
1453  {
1454  case JOIN_INNER:
1455  case JOIN_SEMI:
1456  if (rel1_empty || rel2_empty)
1457  continue; /* ignore this join segment */
1458  break;
1459  case JOIN_LEFT:
1460  case JOIN_ANTI:
1461  if (rel1_empty)
1462  continue; /* ignore this join segment */
1463  break;
1464  case JOIN_FULL:
1465  if (rel1_empty && rel2_empty)
1466  continue; /* ignore this join segment */
1467  break;
1468  default:
1469  /* other values not expected here */
1470  elog(ERROR, "unrecognized join type: %d",
1471  (int) parent_sjinfo->jointype);
1472  break;
1473  }
1474 
1475  /*
1476  * If a child has been pruned entirely then we can't generate paths
1477  * for it, so we have to reject partitionwise joining unless we were
1478  * able to eliminate this partition above.
1479  */
1480  if (child_rel1 == NULL || child_rel2 == NULL)
1481  {
1482  /*
1483  * Mark the joinrel as unpartitioned so that later functions treat
1484  * it correctly.
1485  */
1486  joinrel->nparts = 0;
1487  return;
1488  }
1489 
1490  /*
1491  * If a leaf relation has consider_partitionwise_join=false, it means
1492  * that it's a dummy relation for which we skipped setting up tlist
1493  * expressions and adding EC members in set_append_rel_size(), so
1494  * again we have to fail here.
1495  */
1496  if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
1497  {
1498  Assert(child_rel1->reloptkind == RELOPT_OTHER_MEMBER_REL);
1499  Assert(IS_DUMMY_REL(child_rel1));
1500  joinrel->nparts = 0;
1501  return;
1502  }
1503  if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
1504  {
1505  Assert(child_rel2->reloptkind == RELOPT_OTHER_MEMBER_REL);
1506  Assert(IS_DUMMY_REL(child_rel2));
1507  joinrel->nparts = 0;
1508  return;
1509  }
1510 
1511  /* We should never try to join two overlapping sets of rels. */
1512  Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
1513  child_joinrelids = bms_union(child_rel1->relids, child_rel2->relids);
1514  appinfos = find_appinfos_by_relids(root, child_joinrelids, &nappinfos);
1515 
1516  /*
1517  * Construct SpecialJoinInfo from parent join relations's
1518  * SpecialJoinInfo.
1519  */
1520  child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
1521  child_rel1->relids,
1522  child_rel2->relids);
1523 
1524  /*
1525  * Construct restrictions applicable to the child join from those
1526  * applicable to the parent join.
1527  */
1528  child_restrictlist =
1529  (List *) adjust_appendrel_attrs(root,
1530  (Node *) parent_restrictlist,
1531  nappinfos, appinfos);
1532  pfree(appinfos);
1533 
1534  child_joinrel = joinrel->part_rels[cnt_parts];
1535  if (!child_joinrel)
1536  {
1537  child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
1538  joinrel, child_restrictlist,
1539  child_sjinfo,
1540  child_sjinfo->jointype);
1541  joinrel->part_rels[cnt_parts] = child_joinrel;
1542  joinrel->all_partrels = bms_add_members(joinrel->all_partrels,
1543  child_joinrel->relids);
1544  }
1545 
1546  Assert(bms_equal(child_joinrel->relids, child_joinrelids));
1547 
1548  populate_joinrel_with_paths(root, child_rel1, child_rel2,
1549  child_joinrel, child_sjinfo,
1550  child_restrictlist);
1551  }
1552 }
#define NIL
Definition: pg_list.h:65
RelOptKind reloptkind
Definition: pathnodes.h:663
static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: joinrels.c:776
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:310
Definition: nodes.h:527
Relids all_partrels
Definition: pathnodes.h:753
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:639
static SpecialJoinInfo * build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo, Relids left_relids, Relids right_relids)
Definition: joinrels.c:1560
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1423
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:783
#define lfirst_node(type, lc)
Definition: pg_list.h:172
void check_stack_depth(void)
Definition: postgres.c:3317
int nparts
Definition: pathnodes.h:744
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
Relids relids
Definition: pathnodes.h:666
bool partbounds_merged
Definition: pathnodes.h:748
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:728
bool consider_partitionwise_join
Definition: pathnodes.h:737
#define Assert(condition)
Definition: c.h:800
JoinType jointype
Definition: pathnodes.h:2188
struct RelOptInfo ** part_rels
Definition: pathnodes.h:751
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
static void compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, List **parts1, List **parts2)
Definition: joinrels.c:1601
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:766
#define REL_HAS_ALL_PART_PROPS(rel)
Definition: pathnodes.h:774
#define elog(elevel,...)
Definition: elog.h:228
PartitionScheme part_scheme
Definition: pathnodes.h:743
Definition: pg_list.h:50
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793
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:194