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, int first_rel_idx)
 
static void make_rels_by_clauseless_joins (PlannerInfo *root, RelOptInfo *old_rel, List *other_rels)
 
static bool has_join_restriction (PlannerInfo *root, RelOptInfo *rel)
 
static bool has_legal_joinclause (PlannerInfo *root, RelOptInfo *rel)
 
static bool restriction_is_constant_false (List *restrictlist, RelOptInfo *joinrel, bool only_pushed_down)
 
static void populate_joinrel_with_paths (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *sjinfo, List *restrictlist)
 
static void try_partitionwise_join (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, List *parent_restrictlist)
 
static SpecialJoinInfobuild_child_join_sjinfo (PlannerInfo *root, SpecialJoinInfo *parent_sjinfo, Relids left_relids, Relids right_relids)
 
static void free_child_join_sjinfo (SpecialJoinInfo *child_sjinfo)
 
static void compute_partition_bounds (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo, List **parts1, List **parts2)
 
static void get_matching_part_pairs (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, List **parts1, List **parts2)
 
void join_search_one_level (PlannerInfo *root, int level)
 
static bool join_is_legal (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2, Relids joinrelids, SpecialJoinInfo **sjinfo_p, bool *reversed_p)
 
void init_dummy_sjinfo (SpecialJoinInfo *sjinfo, Relids left_relids, Relids right_relids)
 
RelOptInfomake_join_rel (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 
Relids add_outer_joins_to_relids (PlannerInfo *root, Relids input_relids, SpecialJoinInfo *sjinfo, List **pushed_down_joins)
 
bool have_join_order_restriction (PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 
bool 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 801 of file joinrels.c.

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

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

Referenced by generate_join_implied_equalities(), and make_join_rel().

◆ build_child_join_sjinfo()

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

Definition at line 1693 of file joinrels.c.

1695 {
1697  AppendRelInfo **left_appinfos;
1698  int left_nappinfos;
1699  AppendRelInfo **right_appinfos;
1700  int right_nappinfos;
1701 
1702  /* Dummy SpecialJoinInfos can be created without any translation. */
1703  if (parent_sjinfo->jointype == JOIN_INNER)
1704  {
1705  Assert(parent_sjinfo->ojrelid == 0);
1706  init_dummy_sjinfo(sjinfo, left_relids, right_relids);
1707  return sjinfo;
1708  }
1709 
1710  memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
1711  left_appinfos = find_appinfos_by_relids(root, left_relids,
1712  &left_nappinfos);
1713  right_appinfos = find_appinfos_by_relids(root, right_relids,
1714  &right_nappinfos);
1715 
1716  sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand,
1717  left_nappinfos, left_appinfos);
1719  right_nappinfos,
1720  right_appinfos);
1721  sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand,
1722  left_nappinfos, left_appinfos);
1724  right_nappinfos,
1725  right_appinfos);
1726  /* outer-join relids need no adjustment */
1728  (Node *) sjinfo->semi_rhs_exprs,
1729  right_nappinfos,
1730  right_appinfos);
1731 
1732  pfree(left_appinfos);
1733  pfree(right_appinfos);
1734 
1735  return sjinfo;
1736 }
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
#define Assert(condition)
Definition: c.h:858
void init_dummy_sjinfo(SpecialJoinInfo *sjinfo, Relids left_relids, Relids right_relids)
Definition: joinrels.c:669
void pfree(void *pointer)
Definition: mcxt.c:1520
#define makeNode(_type_)
Definition: nodes.h:155
@ JOIN_INNER
Definition: nodes.h:293
Definition: pg_list.h:54
Definition: nodes.h:129
Relids syn_lefthand
Definition: pathnodes.h:2884
List * semi_rhs_exprs
Definition: pathnodes.h:2897
Relids syn_righthand
Definition: pathnodes.h:2885

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

Referenced by try_partitionwise_join().

◆ compute_partition_bounds()

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

Definition at line 1774 of file joinrels.c.

1778 {
1779  /*
1780  * If we don't have the partition bounds for the join rel yet, try to
1781  * compute those along with pairs of partitions to be joined.
1782  */
1783  if (joinrel->nparts == -1)
1784  {
1785  PartitionScheme part_scheme = joinrel->part_scheme;
1786  PartitionBoundInfo boundinfo = NULL;
1787  int nparts = 0;
1788 
1789  Assert(joinrel->boundinfo == NULL);
1790  Assert(joinrel->part_rels == NULL);
1791 
1792  /*
1793  * See if the partition bounds for inputs are exactly the same, in
1794  * which case we don't need to work hard: the join rel will have the
1795  * same partition bounds as inputs, and the partitions with the same
1796  * cardinal positions will form the pairs.
1797  *
1798  * Note: even in cases where one or both inputs have merged bounds, it
1799  * would be possible for both the bounds to be exactly the same, but
1800  * it seems unlikely to be worth the cycles to check.
1801  */
1802  if (!rel1->partbounds_merged &&
1803  !rel2->partbounds_merged &&
1804  rel1->nparts == rel2->nparts &&
1805  partition_bounds_equal(part_scheme->partnatts,
1806  part_scheme->parttyplen,
1807  part_scheme->parttypbyval,
1808  rel1->boundinfo, rel2->boundinfo))
1809  {
1810  boundinfo = rel1->boundinfo;
1811  nparts = rel1->nparts;
1812  }
1813  else
1814  {
1815  /* Try merging the partition bounds for inputs. */
1816  boundinfo = partition_bounds_merge(part_scheme->partnatts,
1817  part_scheme->partsupfunc,
1818  part_scheme->partcollation,
1819  rel1, rel2,
1820  parent_sjinfo->jointype,
1821  parts1, parts2);
1822  if (boundinfo == NULL)
1823  {
1824  joinrel->nparts = 0;
1825  return;
1826  }
1827  nparts = list_length(*parts1);
1828  joinrel->partbounds_merged = true;
1829  }
1830 
1831  Assert(nparts > 0);
1832  joinrel->boundinfo = boundinfo;
1833  joinrel->nparts = nparts;
1834  joinrel->part_rels =
1835  (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * nparts);
1836  }
1837  else
1838  {
1839  Assert(joinrel->nparts > 0);
1840  Assert(joinrel->boundinfo);
1841  Assert(joinrel->part_rels);
1842 
1843  /*
1844  * If the join rel's partbounds_merged flag is true, it means inputs
1845  * are not guaranteed to have the same partition bounds, therefore we
1846  * can't assume that the partitions at the same cardinal positions
1847  * form the pairs; let get_matching_part_pairs() generate the pairs.
1848  * Otherwise, nothing to do since we can assume that.
1849  */
1850  if (joinrel->partbounds_merged)
1851  {
1852  get_matching_part_pairs(root, joinrel, rel1, rel2,
1853  parts1, parts2);
1854  Assert(list_length(*parts1) == joinrel->nparts);
1855  Assert(list_length(*parts2) == joinrel->nparts);
1856  }
1857  }
1858 }
static void get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, List **parts1, List **parts2)
Definition: joinrels.c:1865
void * palloc0(Size size)
Definition: mcxt.c:1346
bool partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval, PartitionBoundInfo b1, PartitionBoundInfo b2)
Definition: partbounds.c:896
PartitionBoundInfo partition_bounds_merge(int partnatts, FmgrInfo *partsupfunc, Oid *partcollation, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype, List **outer_parts, List **inner_parts)
Definition: partbounds.c:1118
static int list_length(const List *l)
Definition: pg_list.h:152
struct FmgrInfo * partsupfunc
Definition: pathnodes.h:591
bool partbounds_merged
Definition: pathnodes.h:1015

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

Referenced by try_partitionwise_join().

◆ free_child_join_sjinfo()

static void free_child_join_sjinfo ( SpecialJoinInfo child_sjinfo)
static

Definition at line 1747 of file joinrels.c.

1748 {
1749  /*
1750  * Dummy SpecialJoinInfos of inner joins do not have any translated fields
1751  * and hence no fields that to be freed.
1752  */
1753  if (sjinfo->jointype != JOIN_INNER)
1754  {
1755  bms_free(sjinfo->min_lefthand);
1756  bms_free(sjinfo->min_righthand);
1757  bms_free(sjinfo->syn_lefthand);
1758  bms_free(sjinfo->syn_righthand);
1759 
1760  /*
1761  * semi_rhs_exprs may in principle be freed, but a simple pfree() does
1762  * not suffice, so we leave it alone.
1763  */
1764  }
1765 
1766  pfree(sjinfo);
1767 }
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239

References bms_free(), JOIN_INNER, SpecialJoinInfo::jointype, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, pfree(), SpecialJoinInfo::syn_lefthand, and SpecialJoinInfo::syn_righthand.

Referenced by try_partitionwise_join().

◆ get_matching_part_pairs()

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

Definition at line 1865 of file joinrels.c.

1868 {
1869  bool rel1_is_simple = IS_SIMPLE_REL(rel1);
1870  bool rel2_is_simple = IS_SIMPLE_REL(rel2);
1871  int cnt_parts;
1872 
1873  *parts1 = NIL;
1874  *parts2 = NIL;
1875 
1876  for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1877  {
1878  RelOptInfo *child_joinrel = joinrel->part_rels[cnt_parts];
1879  RelOptInfo *child_rel1;
1880  RelOptInfo *child_rel2;
1881  Relids child_relids1;
1882  Relids child_relids2;
1883 
1884  /*
1885  * If this segment of the join is empty, it means that this segment
1886  * was ignored when previously creating child-join paths for it in
1887  * try_partitionwise_join() as it would not contribute to the join
1888  * result, due to one or both inputs being empty; add NULL to each of
1889  * the given lists so that this segment will be ignored again in that
1890  * function.
1891  */
1892  if (!child_joinrel)
1893  {
1894  *parts1 = lappend(*parts1, NULL);
1895  *parts2 = lappend(*parts2, NULL);
1896  continue;
1897  }
1898 
1899  /*
1900  * Get a relids set of partition(s) involved in this join segment that
1901  * are from the rel1 side.
1902  */
1903  child_relids1 = bms_intersect(child_joinrel->relids,
1904  rel1->all_partrels);
1905  Assert(bms_num_members(child_relids1) == bms_num_members(rel1->relids));
1906 
1907  /*
1908  * Get a child rel for rel1 with the relids. Note that we should have
1909  * the child rel even if rel1 is a join rel, because in that case the
1910  * partitions specified in the relids would have matching/overlapping
1911  * boundaries, so the specified partitions should be considered as
1912  * ones to be joined when planning partitionwise joins of rel1,
1913  * meaning that the child rel would have been built by the time we get
1914  * here.
1915  */
1916  if (rel1_is_simple)
1917  {
1918  int varno = bms_singleton_member(child_relids1);
1919 
1920  child_rel1 = find_base_rel(root, varno);
1921  }
1922  else
1923  child_rel1 = find_join_rel(root, child_relids1);
1924  Assert(child_rel1);
1925 
1926  /*
1927  * Get a relids set of partition(s) involved in this join segment that
1928  * are from the rel2 side.
1929  */
1930  child_relids2 = bms_intersect(child_joinrel->relids,
1931  rel2->all_partrels);
1932  Assert(bms_num_members(child_relids2) == bms_num_members(rel2->relids));
1933 
1934  /*
1935  * Get a child rel for rel2 with the relids. See above comments.
1936  */
1937  if (rel2_is_simple)
1938  {
1939  int varno = bms_singleton_member(child_relids2);
1940 
1941  child_rel2 = find_base_rel(root, varno);
1942  }
1943  else
1944  child_rel2 = find_join_rel(root, child_relids2);
1945  Assert(child_rel2);
1946 
1947  /*
1948  * The join of rel1 and rel2 is legal, so is the join of the child
1949  * rels obtained above; add them to the given lists as a join pair
1950  * producing this join segment.
1951  */
1952  *parts1 = lappend(*parts1, child_rel1);
1953  *parts2 = lappend(*parts2, child_rel2);
1954  }
1955 }
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:672
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:751
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:292
#define IS_SIMPLE_REL(rel)
Definition: pathnodes.h:829
#define NIL
Definition: pg_list.h:68
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:414
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:527
Relids relids
Definition: pathnodes.h:861
Relids all_partrels
Definition: pathnodes.h:1031

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

Referenced by compute_partition_bounds().

◆ has_join_restriction()

static bool has_join_restriction ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 1184 of file joinrels.c.

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

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

Referenced by join_search_one_level().

◆ has_legal_joinclause()

static bool has_legal_joinclause ( PlannerInfo root,
RelOptInfo rel 
)
static

Definition at line 1240 of file joinrels.c.

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

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

Referenced by have_join_order_restriction().

◆ have_dangerous_phv()

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

Definition at line 1304 of file joinrels.c.

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

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

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

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

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

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

◆ init_dummy_sjinfo()

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

Definition at line 669 of file joinrels.c.

671 {
672  sjinfo->type = T_SpecialJoinInfo;
673  sjinfo->min_lefthand = left_relids;
674  sjinfo->min_righthand = right_relids;
675  sjinfo->syn_lefthand = left_relids;
676  sjinfo->syn_righthand = right_relids;
677  sjinfo->jointype = JOIN_INNER;
678  sjinfo->ojrelid = 0;
679  sjinfo->commute_above_l = NULL;
680  sjinfo->commute_above_r = NULL;
681  sjinfo->commute_below_l = NULL;
682  sjinfo->commute_below_r = NULL;
683  /* we don't bother trying to make the remaining fields valid */
684  sjinfo->lhs_strict = false;
685  sjinfo->semi_can_btree = false;
686  sjinfo->semi_can_hash = false;
687  sjinfo->semi_operators = NIL;
688  sjinfo->semi_rhs_exprs = NIL;
689 }
Relids commute_above_r
Definition: pathnodes.h:2889
Relids commute_below_r
Definition: pathnodes.h:2891
List * semi_operators
Definition: pathnodes.h:2896

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

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

◆ is_dummy_rel()

bool is_dummy_rel ( RelOptInfo rel)

Definition at line 1332 of file joinrels.c.

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

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

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

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

Referenced by has_legal_joinclause(), and make_join_rel().

◆ join_search_one_level()

void join_search_one_level ( PlannerInfo root,
int  level 
)

Definition at line 72 of file joinrels.c.

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

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

Referenced by standard_join_search().

◆ make_join_rel()

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

Definition at line 704 of file joinrels.c.

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

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

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

◆ make_rels_by_clause_joins()

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

Definition at line 279 of file joinrels.c.

283 {
284  ListCell *l;
285 
286  for_each_from(l, other_rels, first_rel_idx)
287  {
288  RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
289 
290  if (!bms_overlap(old_rel->relids, other_rel->relids) &&
291  (have_relevant_joinclause(root, old_rel, other_rel) ||
292  have_join_order_restriction(root, old_rel, other_rel)))
293  {
294  (void) make_join_rel(root, old_rel, other_rel);
295  }
296  }
297 }

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

Referenced by join_search_one_level().

◆ make_rels_by_clauseless_joins()

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

Definition at line 313 of file joinrels.c.

316 {
317  ListCell *l;
318 
319  foreach(l, other_rels)
320  {
321  RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
322 
323  if (!bms_overlap(other_rel->relids, old_rel->relids))
324  {
325  (void) make_join_rel(root, old_rel, other_rel);
326  }
327  }
328 }

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

Referenced by join_search_one_level().

◆ mark_dummy_rel()

void mark_dummy_rel ( RelOptInfo rel)

Definition at line 1381 of file joinrels.c.

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

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

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

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(), root, 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 1424 of file joinrels.c.

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

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

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

References adjust_appendrel_attrs(), adjust_child_relids(), 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(), free_child_join_sjinfo(), IS_DUMMY_REL, IS_PARTITIONED_REL, IS_SIMPLE_REL, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_SEMI, SpecialJoinInfo::jointype, lfirst_node, list_head(), RelOptInfo::live_parts, lnext(), NIL, RelOptInfo::nparts, RelOptInfo::partbounds_merged, pfree(), populate_joinrel_with_paths(), REL_HAS_ALL_PART_PROPS, RelOptInfo::relids, RELOPT_OTHER_MEMBER_REL, RelOptInfo::reloptkind, and root.

Referenced by populate_joinrel_with_paths().