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)
 
Relids add_outer_joins_to_relids (PlannerInfo *root, Relids input_relids, SpecialJoinInfo *sjinfo, List **pushed_down_joins)
 
bool have_join_order_restriction (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 
bool 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

◆ add_outer_joins_to_relids()

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

Definition at line 802 of file joinrels.c.

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

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

Referenced by build_child_join_rel(), generate_join_implied_equalities(), make_join_rel(), and try_partitionwise_join().

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

1689 {
1691  AppendRelInfo **left_appinfos;
1692  int left_nappinfos;
1693  AppendRelInfo **right_appinfos;
1694  int right_nappinfos;
1695 
1696  memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
1697  left_appinfos = find_appinfos_by_relids(root, left_relids,
1698  &left_nappinfos);
1699  right_appinfos = find_appinfos_by_relids(root, right_relids,
1700  &right_nappinfos);
1701 
1702  sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand,
1703  left_nappinfos, left_appinfos);
1705  right_nappinfos,
1706  right_appinfos);
1707  sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand,
1708  left_nappinfos, left_appinfos);
1710  right_nappinfos,
1711  right_appinfos);
1712  /* outer-join relids need no adjustment */
1713  sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root,
1714  (Node *) sjinfo->semi_rhs_exprs,
1715  right_nappinfos,
1716  right_appinfos);
1717 
1718  pfree(left_appinfos);
1719  pfree(right_appinfos);
1720 
1721  return sjinfo;
1722 }
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:733
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:196
Relids adjust_child_relids(Relids relids, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:554
void pfree(void *pointer)
Definition: mcxt.c:1456
#define makeNode(_type_)
Definition: nodes.h:176
Definition: pg_list.h:54
Definition: nodes.h:129
Relids syn_lefthand
Definition: pathnodes.h:2842
List * semi_rhs_exprs
Definition: pathnodes.h:2855
Relids syn_righthand
Definition: pathnodes.h:2843

References adjust_appendrel_attrs(), adjust_child_relids(), find_appinfos_by_relids(), makeNode, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, pfree(), SpecialJoinInfo::semi_rhs_exprs, SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by try_partitionwise_join().

◆ compute_partition_bounds()

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

Definition at line 1729 of file joinrels.c.

1733 {
1734  /*
1735  * If we don't have the partition bounds for the join rel yet, try to
1736  * compute those along with pairs of partitions to be joined.
1737  */
1738  if (joinrel->nparts == -1)
1739  {
1740  PartitionScheme part_scheme = joinrel->part_scheme;
1741  PartitionBoundInfo boundinfo = NULL;
1742  int nparts = 0;
1743 
1744  Assert(joinrel->boundinfo == NULL);
1745  Assert(joinrel->part_rels == NULL);
1746 
1747  /*
1748  * See if the partition bounds for inputs are exactly the same, in
1749  * which case we don't need to work hard: the join rel will have the
1750  * same partition bounds as inputs, and the partitions with the same
1751  * cardinal positions will form the pairs.
1752  *
1753  * Note: even in cases where one or both inputs have merged bounds, it
1754  * would be possible for both the bounds to be exactly the same, but
1755  * it seems unlikely to be worth the cycles to check.
1756  */
1757  if (!rel1->partbounds_merged &&
1758  !rel2->partbounds_merged &&
1759  rel1->nparts == rel2->nparts &&
1760  partition_bounds_equal(part_scheme->partnatts,
1761  part_scheme->parttyplen,
1762  part_scheme->parttypbyval,
1763  rel1->boundinfo, rel2->boundinfo))
1764  {
1765  boundinfo = rel1->boundinfo;
1766  nparts = rel1->nparts;
1767  }
1768  else
1769  {
1770  /* Try merging the partition bounds for inputs. */
1771  boundinfo = partition_bounds_merge(part_scheme->partnatts,
1772  part_scheme->partsupfunc,
1773  part_scheme->partcollation,
1774  rel1, rel2,
1775  parent_sjinfo->jointype,
1776  parts1, parts2);
1777  if (boundinfo == NULL)
1778  {
1779  joinrel->nparts = 0;
1780  return;
1781  }
1782  nparts = list_length(*parts1);
1783  joinrel->partbounds_merged = true;
1784  }
1785 
1786  Assert(nparts > 0);
1787  joinrel->boundinfo = boundinfo;
1788  joinrel->nparts = nparts;
1789  joinrel->part_rels =
1790  (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * nparts);
1791  }
1792  else
1793  {
1794  Assert(joinrel->nparts > 0);
1795  Assert(joinrel->boundinfo);
1796  Assert(joinrel->part_rels);
1797 
1798  /*
1799  * If the join rel's partbounds_merged flag is true, it means inputs
1800  * are not guaranteed to have the same partition bounds, therefore we
1801  * can't assume that the partitions at the same cardinal positions
1802  * form the pairs; let get_matching_part_pairs() generate the pairs.
1803  * Otherwise, nothing to do since we can assume that.
1804  */
1805  if (joinrel->partbounds_merged)
1806  {
1807  get_matching_part_pairs(root, joinrel, rel1, rel2,
1808  parts1, parts2);
1809  Assert(list_length(*parts1) == joinrel->nparts);
1810  Assert(list_length(*parts2) == joinrel->nparts);
1811  }
1812  }
1813 }
static void get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, List **parts1, List **parts2)
Definition: joinrels.c:1820
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc0(Size size)
Definition: mcxt.c:1257
bool partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval, PartitionBoundInfo b1, PartitionBoundInfo b2)
Definition: partbounds.c:897
PartitionBoundInfo partition_bounds_merge(int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype, List **outer_parts, List **inner_parts)
Definition: partbounds.c:1119
static int list_length(const List *l)
Definition: pg_list.h:152
struct FmgrInfo * partsupfunc
Definition: pathnodes.h:586
bool partbounds_merged
Definition: pathnodes.h:1004

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

Referenced by try_partitionwise_join().

◆ get_matching_part_pairs()

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

Definition at line 1820 of file joinrels.c.

1823 {
1824  bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1825  bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1826  int cnt_parts;
1827 
1828  *parts1 = NIL;
1829  *parts2 = NIL;
1830 
1831  for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1832  {
1833  RelOptInfo *child_joinrel = joinrel->part_rels[cnt_parts];
1834  RelOptInfo *child_rel1;
1835  RelOptInfo *child_rel2;
1836  Relids child_relids1;
1837  Relids child_relids2;
1838 
1839  /*
1840  * If this segment of the join is empty, it means that this segment
1841  * was ignored when previously creating child-join paths for it in
1842  * try_partitionwise_join() as it would not contribute to the join
1843  * result, due to one or both inputs being empty; add NULL to each of
1844  * the given lists so that this segment will be ignored again in that
1845  * function.
1846  */
1847  if (!child_joinrel)
1848  {
1849  *parts1 = lappend(*parts1, NULL);
1850  *parts2 = lappend(*parts2, NULL);
1851  continue;
1852  }
1853 
1854  /*
1855  * Get a relids set of partition(s) involved in this join segment that
1856  * are from the rel1 side.
1857  */
1858  child_relids1 = bms_intersect(child_joinrel->relids,
1859  rel1->all_partrels);
1860  Assert(bms_num_members(child_relids1) == bms_num_members(rel1->relids));
1861 
1862  /*
1863  * Get a child rel for rel1 with the relids. Note that we should have
1864  * the child rel even if rel1 is a join rel, because in that case the
1865  * partitions specified in the relids would have matching/overlapping
1866  * boundaries, so the specified partitions should be considered as
1867  * ones to be joined when planning partitionwise joins of rel1,
1868  * meaning that the child rel would have been built by the time we get
1869  * here.
1870  */
1871  if (rel1_is_simple)
1872  {
1873  int varno = bms_singleton_member(child_relids1);
1874 
1875  child_rel1 = find_base_rel(root, varno);
1876  }
1877  else
1878  child_rel1 = find_join_rel(root, child_relids1);
1879  Assert(child_rel1);
1880 
1881  /*
1882  * Get a relids set of partition(s) involved in this join segment that
1883  * are from the rel2 side.
1884  */
1885  child_relids2 = bms_intersect(child_joinrel->relids,
1886  rel2->all_partrels);
1887  Assert(bms_num_members(child_relids2) == bms_num_members(rel2->relids));
1888 
1889  /*
1890  * Get a child rel for rel2 with the relids. See above comments.
1891  */
1892  if (rel2_is_simple)
1893  {
1894  int varno = bms_singleton_member(child_relids2);
1895 
1896  child_rel2 = find_base_rel(root, varno);
1897  }
1898  else
1899  child_rel2 = find_join_rel(root, child_relids2);
1900  Assert(child_rel2);
1901 
1902  /*
1903  * The join of rel1 and rel2 is legal, so is the join of the child
1904  * rels obtained above; add them to the given lists as a join pair
1905  * producing this join segment.
1906  */
1907  *parts1 = lappend(*parts1, child_rel1);
1908  *parts2 = lappend(*parts2, child_rel2);
1909  }
1910 }
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:596
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:665
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:260
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:824
#define NIL
Definition: pg_list.h:68
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:405
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:507
Relids relids
Definition: pathnodes.h:856
Relids all_partrels
Definition: pathnodes.h:1020

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

Referenced by compute_partition_bounds().

◆ has_join_restriction()

static bool has_join_restriction ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 1185 of file joinrels.c.

1186 {
1187  ListCell *l;
1188 
1189  if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
1190  return true;
1191 
1192  foreach(l, root->placeholder_list)
1193  {
1194  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1195 
1196  if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
1197  !bms_equal(rel->relids, phinfo->ph_eval_at))
1198  return true;
1199  }
1200 
1201  foreach(l, root->join_info_list)
1202  {
1203  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1204 
1205  /* ignore full joins --- other mechanisms preserve their ordering */
1206  if (sjinfo->jointype == JOIN_FULL)
1207  continue;
1208 
1209  /* ignore if SJ is already contained in rel */
1210  if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
1211  bms_is_subset(sjinfo->min_righthand, rel->relids))
1212  continue;
1213 
1214  /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
1215  if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
1216  bms_overlap(sjinfo->min_righthand, rel->relids))
1217  return true;
1218  }
1219 
1220  return false;
1221 }
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:511
@ JOIN_FULL
Definition: nodes.h:306
Relids ph_eval_at
Definition: pathnodes.h:3034
List * placeholder_list
Definition: pathnodes.h:371
Relids lateral_relids
Definition: pathnodes.h:898
Relids lateral_referencers
Definition: pathnodes.h:921

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

Referenced by join_search_one_level().

◆ has_legal_joinclause()

static bool has_legal_joinclause ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 1241 of file joinrels.c.

1242 {
1243  ListCell *lc;
1244 
1245  foreach(lc, root->initial_rels)
1246  {
1247  RelOptInfo *rel2 = (RelOptInfo *) lfirst(lc);
1248 
1249  /* ignore rels that are already in "rel" */
1250  if (bms_overlap(rel->relids, rel2->relids))
1251  continue;
1252 
1253  if (have_relevant_joinclause(root, rel, rel2))
1254  {
1255  Relids joinrelids;
1256  SpecialJoinInfo *sjinfo;
1257  bool reversed;
1258 
1259  /* join_is_legal needs relids of the union */
1260  joinrelids = bms_union(rel->relids, rel2->relids);
1261 
1262  if (join_is_legal(root, rel, rel2, joinrelids,
1263  &sjinfo, &reversed))
1264  {
1265  /* Yes, this will work */
1266  bms_free(joinrelids);
1267  return true;
1268  }
1269 
1270  bms_free(joinrelids);
1271  }
1272  }
1273 
1274  return false;
1275 }
void bms_free(Bitmapset *a)
Definition: bitmapset.c:209
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:226
bool have_relevant_joinclause(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
Definition: joininfo.c:36
static bool join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, Relids joinrelids, SpecialJoinInfo **sjinfo_p, bool *reversed_p)
Definition: joinrels.c:367

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

Referenced by have_join_order_restriction().

◆ have_dangerous_phv()

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

Definition at line 1305 of file joinrels.c.

1307 {
1308  ListCell *lc;
1309 
1310  foreach(lc, root->placeholder_list)
1311  {
1312  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
1313 
1314  if (!bms_is_subset(phinfo->ph_eval_at, inner_params))
1315  continue; /* ignore, could not be a nestloop param */
1316  if (!bms_overlap(phinfo->ph_eval_at, outer_relids))
1317  continue; /* ignore, not relevant to this join */
1318  if (bms_is_subset(phinfo->ph_eval_at, outer_relids))
1319  continue; /* safe, it can be eval'd within outerrel */
1320  /* Otherwise, it's potentially unsafe, so reject the join */
1321  return true;
1322  }
1323 
1324  /* OK to perform the join */
1325  return false;
1326 }

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

Referenced by join_is_legal(), and try_nestloop_path().

◆ have_join_order_restriction()

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

Definition at line 1072 of file joinrels.c.

1074 {
1075  bool result = false;
1076  ListCell *l;
1077 
1078  /*
1079  * If either side has a direct lateral reference to the other, attempt the
1080  * join regardless of outer-join considerations.
1081  */
1082  if (bms_overlap(rel1->relids, rel2->direct_lateral_relids) ||
1084  return true;
1085 
1086  /*
1087  * Likewise, if both rels are needed to compute some PlaceHolderVar,
1088  * attempt the join regardless of outer-join considerations. (This is not
1089  * very desirable, because a PHV with a large eval_at set will cause a lot
1090  * of probably-useless joins to be considered, but failing to do this can
1091  * cause us to fail to construct a plan at all.)
1092  */
1093  foreach(l, root->placeholder_list)
1094  {
1095  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1096 
1097  if (bms_is_subset(rel1->relids, phinfo->ph_eval_at) &&
1098  bms_is_subset(rel2->relids, phinfo->ph_eval_at))
1099  return true;
1100  }
1101 
1102  /*
1103  * It's possible that the rels correspond to the left and right sides of a
1104  * degenerate outer join, that is, one with no joinclause mentioning the
1105  * non-nullable side; in which case we should force the join to occur.
1106  *
1107  * Also, the two rels could represent a clauseless join that has to be
1108  * completed to build up the LHS or RHS of an outer join.
1109  */
1110  foreach(l, root->join_info_list)
1111  {
1112  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1113 
1114  /* ignore full joins --- other mechanisms handle them */
1115  if (sjinfo->jointype == JOIN_FULL)
1116  continue;
1117 
1118  /* Can we perform the SJ with these rels? */
1119  if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
1120  bms_is_subset(sjinfo->min_righthand, rel2->relids))
1121  {
1122  result = true;
1123  break;
1124  }
1125  if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
1126  bms_is_subset(sjinfo->min_righthand, rel1->relids))
1127  {
1128  result = true;
1129  break;
1130  }
1131 
1132  /*
1133  * Might we need to join these rels to complete the RHS? We have to
1134  * use "overlap" tests since either rel might include a lower SJ that
1135  * has been proven to commute with this one.
1136  */
1137  if (bms_overlap(sjinfo->min_righthand, rel1->relids) &&
1138  bms_overlap(sjinfo->min_righthand, rel2->relids))
1139  {
1140  result = true;
1141  break;
1142  }
1143 
1144  /* Likewise for the LHS. */
1145  if (bms_overlap(sjinfo->min_lefthand, rel1->relids) &&
1146  bms_overlap(sjinfo->min_lefthand, rel2->relids))
1147  {
1148  result = true;
1149  break;
1150  }
1151  }
1152 
1153  /*
1154  * We do not force the join to occur if either input rel can legally be
1155  * joined to anything else using joinclauses. This essentially means that
1156  * clauseless bushy joins are put off as long as possible. The reason is
1157  * that when there is a join order restriction high up in the join tree
1158  * (that is, with many rels inside the LHS or RHS), we would otherwise
1159  * expend lots of effort considering very stupid join combinations within
1160  * its LHS or RHS.
1161  */
1162  if (result)
1163  {
1164  if (has_legal_joinclause(root, rel1) ||
1165  has_legal_joinclause(root, rel2))
1166  result = false;
1167  }
1168 
1169  return result;
1170 }
static bool has_legal_joinclause(PlannerInfo *root, RelOptInfo *rel)
Definition: joinrels.c:1241
Relids direct_lateral_relids
Definition: pathnodes.h:896

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

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

◆ is_dummy_rel()

bool is_dummy_rel ( RelOptInfo rel)

Definition at line 1333 of file joinrels.c.

1334 {
1335  Path *path;
1336 
1337  /*
1338  * A rel that is known dummy will have just one path that is a childless
1339  * Append. (Even if somehow it has more paths, a childless Append will
1340  * have cost zero and hence should be at the front of the pathlist.)
1341  */
1342  if (rel->pathlist == NIL)
1343  return false;
1344  path = (Path *) linitial(rel->pathlist);
1345 
1346  /*
1347  * Initially, a dummy path will just be a childless Append. But in later
1348  * planning stages we might stick a ProjectSetPath and/or ProjectionPath
1349  * on top, since Append can't project. Rather than make assumptions about
1350  * which combinations can occur, just descend through whatever we find.
1351  */
1352  for (;;)
1353  {
1354  if (IsA(path, ProjectionPath))
1355  path = ((ProjectionPath *) path)->subpath;
1356  else if (IsA(path, ProjectSetPath))
1357  path = ((ProjectSetPath *) path)->subpath;
1358  else
1359  break;
1360  }
1361  if (IS_DUMMY_APPEND(path))
1362  return true;
1363  return false;
1364 }
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241
#define IsA(nodeptr, _type_)
Definition: nodes.h:179
#define IS_DUMMY_APPEND(p)
Definition: pathnodes.h:1893
#define linitial(l)
Definition: pg_list.h:178
List * pathlist
Definition: pathnodes.h:883

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

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

◆ join_is_legal()

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

Definition at line 367 of file joinrels.c.

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

References bms_add_members(), bms_copy(), bms_equal(), bms_is_subset(), bms_overlap(), RelOptInfo::cheapest_total_path, create_unique_path(), RelOptInfo::direct_lateral_relids, PlannerInfo::hasLateralRTEs, have_dangerous_phv(), JOIN_FULL, PlannerInfo::join_info_list, JOIN_LEFT, JOIN_SEMI, SpecialJoinInfo::jointype, RelOptInfo::lateral_relids, lfirst, SpecialJoinInfo::lhs_strict, min_join_parameterization(), SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, RelOptInfo::relids, and SpecialJoinInfo::syn_righthand.

Referenced by has_legal_joinclause(), and make_join_rel().

◆ join_search_one_level()

void join_search_one_level ( PlannerInfo root,
int  level 
)

Definition at line 71 of file joinrels.c.

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

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

Referenced by standard_join_search().

◆ make_join_rel()

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

Definition at line 689 of file joinrels.c.

690 {
691  Relids joinrelids;
692  SpecialJoinInfo *sjinfo;
693  bool reversed;
694  List *pushed_down_joins = NIL;
695  SpecialJoinInfo sjinfo_data;
696  RelOptInfo *joinrel;
697  List *restrictlist;
698 
699  /* We should never try to join two overlapping sets of rels. */
700  Assert(!bms_overlap(rel1->relids, rel2->relids));
701 
702  /* Construct Relids set that identifies the joinrel (without OJ as yet). */
703  joinrelids = bms_union(rel1->relids, rel2->relids);
704 
705  /* Check validity and determine join type. */
706  if (!join_is_legal(root, rel1, rel2, joinrelids,
707  &sjinfo, &reversed))
708  {
709  /* invalid join path */
710  bms_free(joinrelids);
711  return NULL;
712  }
713 
714  /*
715  * Add outer join relid(s) to form the canonical relids. Any added outer
716  * joins besides sjinfo itself are appended to pushed_down_joins.
717  */
718  joinrelids = add_outer_joins_to_relids(root, joinrelids, sjinfo,
719  &pushed_down_joins);
720 
721  /* Swap rels if needed to match the join info. */
722  if (reversed)
723  {
724  RelOptInfo *trel = rel1;
725 
726  rel1 = rel2;
727  rel2 = trel;
728  }
729 
730  /*
731  * If it's a plain inner join, then we won't have found anything in
732  * join_info_list. Make up a SpecialJoinInfo so that selectivity
733  * estimation functions will know what's being joined.
734  */
735  if (sjinfo == NULL)
736  {
737  sjinfo = &sjinfo_data;
738  sjinfo->type = T_SpecialJoinInfo;
739  sjinfo->min_lefthand = rel1->relids;
740  sjinfo->min_righthand = rel2->relids;
741  sjinfo->syn_lefthand = rel1->relids;
742  sjinfo->syn_righthand = rel2->relids;
743  sjinfo->jointype = JOIN_INNER;
744  sjinfo->ojrelid = 0;
745  sjinfo->commute_above_l = NULL;
746  sjinfo->commute_above_r = NULL;
747  sjinfo->commute_below_l = NULL;
748  sjinfo->commute_below_r = NULL;
749  /* we don't bother trying to make the remaining fields valid */
750  sjinfo->lhs_strict = false;
751  sjinfo->semi_can_btree = false;
752  sjinfo->semi_can_hash = false;
753  sjinfo->semi_operators = NIL;
754  sjinfo->semi_rhs_exprs = NIL;
755  }
756 
757  /*
758  * Find or build the join RelOptInfo, and compute the restrictlist that
759  * goes with this particular joining.
760  */
761  joinrel = build_join_rel(root, joinrelids, rel1, rel2,
762  sjinfo, pushed_down_joins,
763  &restrictlist);
764 
765  /*
766  * If we've already proven this join is empty, we needn't consider any
767  * more paths for it.
768  */
769  if (is_dummy_rel(joinrel))
770  {
771  bms_free(joinrelids);
772  return joinrel;
773  }
774 
775  /* Add paths to the join relation. */
776  populate_joinrel_with_paths(root, rel1, rel2, joinrel, sjinfo,
777  restrictlist);
778 
779  bms_free(joinrelids);
780 
781  return joinrel;
782 }
static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: joinrels.c:894
bool is_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1333
Relids add_outer_joins_to_relids(PlannerInfo *root, Relids input_relids, SpecialJoinInfo *sjinfo, List **pushed_down_joins)
Definition: joinrels.c:802
@ JOIN_INNER
Definition: nodes.h:304
RelOptInfo * build_join_rel(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *pushed_down_joins, List **restrictlist_ptr)
Definition: relnode.c:645
Relids commute_above_r
Definition: pathnodes.h:2847
Relids commute_below_r
Definition: pathnodes.h:2849
List * semi_operators
Definition: pathnodes.h:2854

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

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

◆ make_rels_by_clause_joins()

static void make_rels_by_clause_joins ( PlannerInfo root,
RelOptInfo old_rel,
List other_rels_list,
ListCell other_rels 
)
static

Definition at line 297 of file joinrels.c.

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

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

Referenced by join_search_one_level().

◆ make_rels_by_clauseless_joins()

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

Definition at line 331 of file joinrels.c.

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

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

Referenced by join_search_one_level().

◆ mark_dummy_rel()

void mark_dummy_rel ( RelOptInfo rel)

Definition at line 1382 of file joinrels.c.

1383 {
1384  MemoryContext oldcontext;
1385 
1386  /* Already marked? */
1387  if (is_dummy_rel(rel))
1388  return;
1389 
1390  /* No, so choose correct context to make the dummy path in */
1391  oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
1392 
1393  /* Set dummy size estimate */
1394  rel->rows = 0;
1395 
1396  /* Evict any previously chosen paths */
1397  rel->pathlist = NIL;
1398  rel->partial_pathlist = NIL;
1399 
1400  /* Set up the dummy path */
1401  add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL,
1402  NIL, rel->lateral_relids,
1403  0, false, -1));
1404 
1405  /* Set or update cheapest_total_path and related fields */
1406  set_cheapest(rel);
1407 
1408  MemoryContextSwitchTo(oldcontext);
1409 }
MemoryContext GetMemoryChunkContext(void *pointer)
Definition: mcxt.c:616
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
AppendPath * create_append_path(PlannerInfo *root, RelOptInfo *rel, List *subpaths, List *partial_subpaths, List *pathkeys, Relids required_outer, int parallel_workers, bool parallel_aware, double rows)
Definition: pathnode.c:1242
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
List * partial_pathlist
Definition: pathnodes.h:885
Cardinality rows
Definition: pathnodes.h:862

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

Referenced by build_simple_rel(), generate_partitionwise_join_paths(), and populate_joinrel_with_paths().

◆ populate_joinrel_with_paths()

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

Definition at line 894 of file joinrels.c.

897 {
898  /*
899  * Consider paths using each rel as both outer and inner. Depending on
900  * the join type, a provably empty outer or inner rel might mean the join
901  * is provably empty too; in which case throw away any previously computed
902  * paths and mark the join as dummy. (We do it this way since it's
903  * conceivable that dummy-ness of a multi-element join might only be
904  * noticeable for certain construction paths.)
905  *
906  * Also, a provably constant-false join restriction typically means that
907  * we can skip evaluating one or both sides of the join. We do this by
908  * marking the appropriate rel as dummy. For outer joins, a
909  * constant-false restriction that is pushed down still means the whole
910  * join is dummy, while a non-pushed-down one means that no inner rows
911  * will join so we can treat the inner rel as dummy.
912  *
913  * We need only consider the jointypes that appear in join_info_list, plus
914  * JOIN_INNER.
915  */
916  switch (sjinfo->jointype)
917  {
918  case JOIN_INNER:
919  if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
920  restriction_is_constant_false(restrictlist, joinrel, false))
921  {
922  mark_dummy_rel(joinrel);
923  break;
924  }
925  add_paths_to_joinrel(root, joinrel, rel1, rel2,
926  JOIN_INNER, sjinfo,
927  restrictlist);
928  add_paths_to_joinrel(root, joinrel, rel2, rel1,
929  JOIN_INNER, sjinfo,
930  restrictlist);
931  break;
932  case JOIN_LEFT:
933  if (is_dummy_rel(rel1) ||
934  restriction_is_constant_false(restrictlist, joinrel, true))
935  {
936  mark_dummy_rel(joinrel);
937  break;
938  }
939  if (restriction_is_constant_false(restrictlist, joinrel, false) &&
940  bms_is_subset(rel2->relids, sjinfo->syn_righthand))
941  mark_dummy_rel(rel2);
942  add_paths_to_joinrel(root, joinrel, rel1, rel2,
943  JOIN_LEFT, sjinfo,
944  restrictlist);
945  add_paths_to_joinrel(root, joinrel, rel2, rel1,
946  JOIN_RIGHT, sjinfo,
947  restrictlist);
948  break;
949  case JOIN_FULL:
950  if ((is_dummy_rel(rel1) && is_dummy_rel(rel2)) ||
951  restriction_is_constant_false(restrictlist, joinrel, true))
952  {
953  mark_dummy_rel(joinrel);
954  break;
955  }
956  add_paths_to_joinrel(root, joinrel, rel1, rel2,
957  JOIN_FULL, sjinfo,
958  restrictlist);
959  add_paths_to_joinrel(root, joinrel, rel2, rel1,
960  JOIN_FULL, sjinfo,
961  restrictlist);
962 
963  /*
964  * If there are join quals that aren't mergeable or hashable, we
965  * may not be able to build any valid plan. Complain here so that
966  * we can give a somewhat-useful error message. (Since we have no
967  * flexibility of planning for a full join, there's no chance of
968  * succeeding later with another pair of input rels.)
969  */
970  if (joinrel->pathlist == NIL)
971  ereport(ERROR,
972  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
973  errmsg("FULL JOIN is only supported with merge-joinable or hash-joinable join conditions")));
974  break;
975  case JOIN_SEMI:
976 
977  /*
978  * We might have a normal semijoin, or a case where we don't have
979  * enough rels to do the semijoin but can unique-ify the RHS and
980  * then do an innerjoin (see comments in join_is_legal). In the
981  * latter case we can't apply JOIN_SEMI joining.
982  */
983  if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
984  bms_is_subset(sjinfo->min_righthand, rel2->relids))
985  {
986  if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
987  restriction_is_constant_false(restrictlist, joinrel, false))
988  {
989  mark_dummy_rel(joinrel);
990  break;
991  }
992  add_paths_to_joinrel(root, joinrel, rel1, rel2,
993  JOIN_SEMI, sjinfo,
994  restrictlist);
995  }
996 
997  /*
998  * If we know how to unique-ify the RHS and one input rel is
999  * exactly the RHS (not a superset) we can consider unique-ifying
1000  * it and then doing a regular join. (The create_unique_path
1001  * check here is probably redundant with what join_is_legal did,
1002  * but if so the check is cheap because it's cached. So test
1003  * anyway to be sure.)
1004  */
1005  if (bms_equal(sjinfo->syn_righthand, rel2->relids) &&
1006  create_unique_path(root, rel2, rel2->cheapest_total_path,
1007  sjinfo) != NULL)
1008  {
1009  if (is_dummy_rel(rel1) || is_dummy_rel(rel2) ||
1010  restriction_is_constant_false(restrictlist, joinrel, false))
1011  {
1012  mark_dummy_rel(joinrel);
1013  break;
1014  }
1015  add_paths_to_joinrel(root, joinrel, rel1, rel2,
1016  JOIN_UNIQUE_INNER, sjinfo,
1017  restrictlist);
1018  add_paths_to_joinrel(root, joinrel, rel2, rel1,
1019  JOIN_UNIQUE_OUTER, sjinfo,
1020  restrictlist);
1021  }
1022  break;
1023  case JOIN_ANTI:
1024  if (is_dummy_rel(rel1) ||
1025  restriction_is_constant_false(restrictlist, joinrel, true))
1026  {
1027  mark_dummy_rel(joinrel);
1028  break;
1029  }
1030  if (restriction_is_constant_false(restrictlist, joinrel, false) &&
1031  bms_is_subset(rel2->relids, sjinfo->syn_righthand))
1032  mark_dummy_rel(rel2);
1033  add_paths_to_joinrel(root, joinrel, rel1, rel2,
1034  JOIN_ANTI, sjinfo,
1035  restrictlist);
1036  add_paths_to_joinrel(root, joinrel, rel2, rel1,
1037  JOIN_RIGHT_ANTI, sjinfo,
1038  restrictlist);
1039  break;
1040  default:
1041  /* other values not expected here */
1042  elog(ERROR, "unrecognized join type: %d", (int) sjinfo->jointype);
1043  break;
1044  }
1045 
1046  /* Apply partitionwise join technique, if possible. */
1047  try_partitionwise_join(root, rel1, rel2, joinrel, sjinfo, restrictlist);
1048 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereport(elevel,...)
Definition: elog.h:149
void add_paths_to_joinrel(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: joinpath.c:123
static void try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, List *parent_restrictlist)
Definition: joinrels.c:1479
static bool restriction_is_constant_false(List *restrictlist, RelOptInfo *joinrel, bool only_pushed_down)
Definition: joinrels.c:1425
void mark_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1382
@ JOIN_RIGHT
Definition: nodes.h:307
@ JOIN_UNIQUE_OUTER
Definition: nodes.h:326
@ JOIN_RIGHT_ANTI
Definition: nodes.h:320
@ JOIN_UNIQUE_INNER
Definition: nodes.h:327
@ JOIN_ANTI
Definition: nodes.h:319

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

Referenced by make_join_rel(), and try_partitionwise_join().

◆ restriction_is_constant_false()

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

Definition at line 1425 of file joinrels.c.

1428 {
1429  ListCell *lc;
1430 
1431  /*
1432  * Despite the above comment, the restriction list we see here might
1433  * possibly have other members besides the FALSE constant, since other
1434  * quals could get "pushed down" to the outer join level. So we check
1435  * each member of the list.
1436  */
1437  foreach(lc, restrictlist)
1438  {
1439  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1440 
1441  if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
1442  continue;
1443 
1444  if (rinfo->clause && IsA(rinfo->clause, Const))
1445  {
1446  Const *con = (Const *) rinfo->clause;
1447 
1448  /* constant NULL is as good as constant FALSE for our purposes */
1449  if (con->constisnull)
1450  return true;
1451  if (!DatumGetBool(con->constvalue))
1452  return true;
1453  }
1454  }
1455  return false;
1456 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2670
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
Expr * clause
Definition: pathnodes.h:2516

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

Referenced by populate_joinrel_with_paths().

◆ try_partitionwise_join()

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

Definition at line 1479 of file joinrels.c.

1482 {
1483  bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1484  bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1485  List *parts1 = NIL;
1486  List *parts2 = NIL;
1487  ListCell *lcr1 = NULL;
1488  ListCell *lcr2 = NULL;
1489  int cnt_parts;
1490 
1491  /* Guard against stack overflow due to overly deep partition hierarchy. */
1493 
1494  /* Nothing to do, if the join relation is not partitioned. */
1495  if (joinrel->part_scheme == NULL || joinrel->nparts == 0)
1496  return;
1497 
1498  /* The join relation should have consider_partitionwise_join set. */
1500 
1501  /*
1502  * We can not perform partitionwise join if either of the joining
1503  * relations is not partitioned.
1504  */
1505  if (!IS_PARTITIONED_REL(rel1) || !IS_PARTITIONED_REL(rel2))
1506  return;
1507 
1509 
1510  /* The joining relations should have consider_partitionwise_join set. */
1513 
1514  /*
1515  * The partition scheme of the join relation should match that of the
1516  * joining relations.
1517  */
1518  Assert(joinrel->part_scheme == rel1->part_scheme &&
1519  joinrel->part_scheme == rel2->part_scheme);
1520 
1521  Assert(!(joinrel->partbounds_merged && (joinrel->nparts <= 0)));
1522 
1523  compute_partition_bounds(root, rel1, rel2, joinrel, parent_sjinfo,
1524  &parts1, &parts2);
1525 
1526  if (joinrel->partbounds_merged)
1527  {
1528  lcr1 = list_head(parts1);
1529  lcr2 = list_head(parts2);
1530  }
1531 
1532  /*
1533  * Create child-join relations for this partitioned join, if those don't
1534  * exist. Add paths to child-joins for a pair of child relations
1535  * corresponding to the given pair of parent relations.
1536  */
1537  for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1538  {
1539  RelOptInfo *child_rel1;
1540  RelOptInfo *child_rel2;
1541  bool rel1_empty;
1542  bool rel2_empty;
1543  SpecialJoinInfo *child_sjinfo;
1544  List *child_restrictlist;
1545  RelOptInfo *child_joinrel;
1546  Relids child_joinrelids;
1547  AppendRelInfo **appinfos;
1548  int nappinfos;
1549 
1550  if (joinrel->partbounds_merged)
1551  {
1552  child_rel1 = lfirst_node(RelOptInfo, lcr1);
1553  child_rel2 = lfirst_node(RelOptInfo, lcr2);
1554  lcr1 = lnext(parts1, lcr1);
1555  lcr2 = lnext(parts2, lcr2);
1556  }
1557  else
1558  {
1559  child_rel1 = rel1->part_rels[cnt_parts];
1560  child_rel2 = rel2->part_rels[cnt_parts];
1561  }
1562 
1563  rel1_empty = (child_rel1 == NULL || IS_DUMMY_REL(child_rel1));
1564  rel2_empty = (child_rel2 == NULL || IS_DUMMY_REL(child_rel2));
1565 
1566  /*
1567  * Check for cases where we can prove that this segment of the join
1568  * returns no rows, due to one or both inputs being empty (including
1569  * inputs that have been pruned away entirely). If so just ignore it.
1570  * These rules are equivalent to populate_joinrel_with_paths's rules
1571  * for dummy input relations.
1572  */
1573  switch (parent_sjinfo->jointype)
1574  {
1575  case JOIN_INNER:
1576  case JOIN_SEMI:
1577  if (rel1_empty || rel2_empty)
1578  continue; /* ignore this join segment */
1579  break;
1580  case JOIN_LEFT:
1581  case JOIN_ANTI:
1582  if (rel1_empty)
1583  continue; /* ignore this join segment */
1584  break;
1585  case JOIN_FULL:
1586  if (rel1_empty && rel2_empty)
1587  continue; /* ignore this join segment */
1588  break;
1589  default:
1590  /* other values not expected here */
1591  elog(ERROR, "unrecognized join type: %d",
1592  (int) parent_sjinfo->jointype);
1593  break;
1594  }
1595 
1596  /*
1597  * If a child has been pruned entirely then we can't generate paths
1598  * for it, so we have to reject partitionwise joining unless we were
1599  * able to eliminate this partition above.
1600  */
1601  if (child_rel1 == NULL || child_rel2 == NULL)
1602  {
1603  /*
1604  * Mark the joinrel as unpartitioned so that later functions treat
1605  * it correctly.
1606  */
1607  joinrel->nparts = 0;
1608  return;
1609  }
1610 
1611  /*
1612  * If a leaf relation has consider_partitionwise_join=false, it means
1613  * that it's a dummy relation for which we skipped setting up tlist
1614  * expressions and adding EC members in set_append_rel_size(), so
1615  * again we have to fail here.
1616  */
1617  if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
1618  {
1619  Assert(child_rel1->reloptkind == RELOPT_OTHER_MEMBER_REL);
1620  Assert(IS_DUMMY_REL(child_rel1));
1621  joinrel->nparts = 0;
1622  return;
1623  }
1624  if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
1625  {
1626  Assert(child_rel2->reloptkind == RELOPT_OTHER_MEMBER_REL);
1627  Assert(IS_DUMMY_REL(child_rel2));
1628  joinrel->nparts = 0;
1629  return;
1630  }
1631 
1632  /* We should never try to join two overlapping sets of rels. */
1633  Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
1634 
1635  /*
1636  * Construct SpecialJoinInfo from parent join relations's
1637  * SpecialJoinInfo.
1638  */
1639  child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
1640  child_rel1->relids,
1641  child_rel2->relids);
1642 
1643  /* Build correct join relids for child join */
1644  child_joinrelids = bms_union(child_rel1->relids, child_rel2->relids);
1645  child_joinrelids = add_outer_joins_to_relids(root, child_joinrelids,
1646  child_sjinfo, NULL);
1647 
1648  /* Find the AppendRelInfo structures */
1649  appinfos = find_appinfos_by_relids(root, child_joinrelids, &nappinfos);
1650 
1651  /*
1652  * Construct restrictions applicable to the child join from those
1653  * applicable to the parent join.
1654  */
1655  child_restrictlist =
1656  (List *) adjust_appendrel_attrs(root,
1657  (Node *) parent_restrictlist,
1658  nappinfos, appinfos);
1659  pfree(appinfos);
1660 
1661  child_joinrel = joinrel->part_rels[cnt_parts];
1662  if (!child_joinrel)
1663  {
1664  child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
1665  joinrel, child_restrictlist,
1666  child_sjinfo);
1667  joinrel->part_rels[cnt_parts] = child_joinrel;
1668  joinrel->live_parts = bms_add_member(joinrel->live_parts, cnt_parts);
1669  joinrel->all_partrels = bms_add_members(joinrel->all_partrels,
1670  child_joinrel->relids);
1671  }
1672 
1673  Assert(bms_equal(child_joinrel->relids, child_joinrelids));
1674 
1675  populate_joinrel_with_paths(root, child_rel1, child_rel2,
1676  child_joinrel, child_sjinfo,
1677  child_restrictlist);
1678  }
1679 }
static void compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, List **parts1, List **parts2)
Definition: joinrels.c:1729
static SpecialJoinInfo * build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo, Relids left_relids, Relids right_relids)
Definition: joinrels.c:1687
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1901
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:1041
#define REL_HAS_ALL_PART_PROPS(rel)
Definition: pathnodes.h:1049
@ RELOPT_OTHER_MEMBER_REL
Definition: pathnodes.h:814
void check_stack_depth(void)
Definition: postgres.c:3508
RelOptInfo * build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel, RelOptInfo *inner_rel, RelOptInfo *parent_joinrel, List *restrictlist, SpecialJoinInfo *sjinfo)
Definition: relnode.c:860
RelOptKind reloptkind
Definition: pathnodes.h:850
Bitmapset * live_parts
Definition: pathnodes.h:1018
bool consider_partitionwise_join
Definition: pathnodes.h:978

References add_outer_joins_to_relids(), adjust_appendrel_attrs(), RelOptInfo::all_partrels, Assert(), bms_add_member(), bms_add_members(), bms_equal(), bms_overlap(), bms_union(), build_child_join_rel(), build_child_join_sjinfo(), check_stack_depth(), compute_partition_bounds(), RelOptInfo::consider_partitionwise_join, elog(), ERROR, find_appinfos_by_relids(), IS_DUMMY_REL, IS_PARTITIONED_REL, IS_SIMPLE_REL, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_SEMI, SpecialJoinInfo::jointype, lfirst_node, list_head(), RelOptInfo::live_parts, lnext(), NIL, RelOptInfo::nparts, 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().