PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
joinpath.c File Reference
#include "postgres.h"
#include <math.h>
#include "executor/executor.h"
#include "foreign/fdwapi.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
Include dependency graph for joinpath.c:

Go to the source code of this file.

Macros

#define PATH_PARAM_BY_REL(path, rel)   ((path)->param_info && bms_overlap(PATH_REQ_OUTER(path), (rel)->relids))
 

Functions

static void try_partial_mergejoin_path (PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *pathkeys, List *mergeclauses, List *outersortkeys, List *innersortkeys, JoinType jointype, JoinPathExtraData *extra)
 
static void sort_inner_and_outer (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra)
 
static void match_unsorted_outer (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra)
 
static void consider_parallel_nestloop (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra)
 
static void consider_parallel_mergejoin (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra, Path *inner_cheapest_total)
 
static void hash_inner_and_outer (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra)
 
static Listselect_mergejoin_clauses (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, List *restrictlist, JoinType jointype, bool *mergejoin_allowed)
 
static void generate_mergejoin_paths (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *innerrel, Path *outerpath, JoinType jointype, JoinPathExtraData *extra, bool useallclauses, Path *inner_cheapest_total, List *merge_pathkeys, bool is_partial)
 
void add_paths_to_joinrel (PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, SpecialJoinInfo *sjinfo, List *restrictlist)
 
static bool allow_star_schema_join (PlannerInfo *root, Relids outerrelids, Relids inner_paramrels)
 
static void try_nestloop_path (PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *pathkeys, JoinType jointype, JoinPathExtraData *extra)
 
static void try_partial_nestloop_path (PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *pathkeys, JoinType jointype, JoinPathExtraData *extra)
 
static void try_mergejoin_path (PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *pathkeys, List *mergeclauses, List *outersortkeys, List *innersortkeys, JoinType jointype, JoinPathExtraData *extra, bool is_partial)
 
static void try_hashjoin_path (PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *hashclauses, JoinType jointype, JoinPathExtraData *extra)
 
static void try_partial_hashjoin_path (PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *hashclauses, JoinType jointype, JoinPathExtraData *extra)
 
static bool clause_sides_match_join (RestrictInfo *rinfo, RelOptInfo *outerrel, RelOptInfo *innerrel)
 

Variables

set_join_pathlist_hook_type set_join_pathlist_hook = NULL
 

Macro Definition Documentation

#define PATH_PARAM_BY_REL (   path,
  rel 
)    ((path)->param_info && bms_overlap(PATH_REQ_OUTER(path), (rel)->relids))

Definition at line 29 of file joinpath.c.

Referenced by hash_inner_and_outer(), match_unsorted_outer(), and sort_inner_and_outer().

Function Documentation

void add_paths_to_joinrel ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outerrel,
RelOptInfo innerrel,
JoinType  jointype,
SpecialJoinInfo sjinfo,
List restrictlist 
)

Definition at line 107 of file joinpath.c.

References PlannerInfo::all_baserels, bms_add_members(), bms_difference(), bms_is_subset(), bms_join(), bms_overlap(), compute_semi_anti_join_factors(), enable_hashjoin, enable_mergejoin, RelOptInfo::fdwroutine, FdwRoutine::GetForeignJoinPaths, hash_inner_and_outer(), JoinPathExtraData::inner_unique, innerrel_is_unique(), JOIN_ANTI, JOIN_FULL, PlannerInfo::join_info_list, JOIN_INNER, JOIN_SEMI, JOIN_UNIQUE_INNER, JOIN_UNIQUE_OUTER, SpecialJoinInfo::jointype, RelOptInfo::lateral_relids, lfirst, match_unsorted_outer(), JoinPathExtraData::mergeclause_list, SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, NIL, JoinPathExtraData::param_source_rels, RelOptInfo::relids, JoinPathExtraData::restrictlist, select_mergejoin_clauses(), JoinPathExtraData::semifactors, set_join_pathlist_hook, JoinPathExtraData::sjinfo, and sort_inner_and_outer().

Referenced by populate_joinrel_with_paths().

114 {
115  JoinPathExtraData extra;
116  bool mergejoin_allowed = true;
117  ListCell *lc;
118 
119  extra.restrictlist = restrictlist;
120  extra.mergeclause_list = NIL;
121  extra.sjinfo = sjinfo;
122  extra.param_source_rels = NULL;
123 
124  /*
125  * See if the inner relation is provably unique for this outer rel.
126  *
127  * We have some special cases: for JOIN_SEMI and JOIN_ANTI, it doesn't
128  * matter since the executor can make the equivalent optimization anyway;
129  * we need not expend planner cycles on proofs. For JOIN_UNIQUE_INNER, we
130  * must be considering a semijoin whose inner side is not provably unique
131  * (else reduce_unique_semijoins would've simplified it), so there's no
132  * point in calling innerrel_is_unique. However, if the LHS covers all of
133  * the semijoin's min_lefthand, then it's appropriate to set inner_unique
134  * because the path produced by create_unique_path will be unique relative
135  * to the LHS. (If we have an LHS that's only part of the min_lefthand,
136  * that is *not* true.) For JOIN_UNIQUE_OUTER, pass JOIN_INNER to avoid
137  * letting that value escape this module.
138  */
139  switch (jointype)
140  {
141  case JOIN_SEMI:
142  case JOIN_ANTI:
143  extra.inner_unique = false; /* well, unproven */
144  break;
145  case JOIN_UNIQUE_INNER:
146  extra.inner_unique = bms_is_subset(sjinfo->min_lefthand,
147  outerrel->relids);
148  break;
149  case JOIN_UNIQUE_OUTER:
150  extra.inner_unique = innerrel_is_unique(root,
151  outerrel->relids,
152  innerrel,
153  JOIN_INNER,
154  restrictlist,
155  false);
156  break;
157  default:
158  extra.inner_unique = innerrel_is_unique(root,
159  outerrel->relids,
160  innerrel,
161  jointype,
162  restrictlist,
163  false);
164  break;
165  }
166 
167  /*
168  * Find potential mergejoin clauses. We can skip this if we are not
169  * interested in doing a mergejoin. However, mergejoin may be our only
170  * way of implementing a full outer join, so override enable_mergejoin if
171  * it's a full join.
172  */
173  if (enable_mergejoin || jointype == JOIN_FULL)
175  joinrel,
176  outerrel,
177  innerrel,
178  restrictlist,
179  jointype,
180  &mergejoin_allowed);
181 
182  /*
183  * If it's SEMI, ANTI, or inner_unique join, compute correction factors
184  * for cost estimation. These will be the same for all paths.
185  */
186  if (jointype == JOIN_SEMI || jointype == JOIN_ANTI || extra.inner_unique)
187  compute_semi_anti_join_factors(root, outerrel, innerrel,
188  jointype, sjinfo, restrictlist,
189  &extra.semifactors);
190 
191  /*
192  * Decide whether it's sensible to generate parameterized paths for this
193  * joinrel, and if so, which relations such paths should require. There
194  * is usually no need to create a parameterized result path unless there
195  * is a join order restriction that prevents joining one of our input rels
196  * directly to the parameter source rel instead of joining to the other
197  * input rel. (But see allow_star_schema_join().) This restriction
198  * reduces the number of parameterized paths we have to deal with at
199  * higher join levels, without compromising the quality of the resulting
200  * plan. We express the restriction as a Relids set that must overlap the
201  * parameterization of any proposed join path.
202  */
203  foreach(lc, root->join_info_list)
204  {
205  SpecialJoinInfo *sjinfo2 = (SpecialJoinInfo *) lfirst(lc);
206 
207  /*
208  * SJ is relevant to this join if we have some part of its RHS
209  * (possibly not all of it), and haven't yet joined to its LHS. (This
210  * test is pretty simplistic, but should be sufficient considering the
211  * join has already been proven legal.) If the SJ is relevant, it
212  * presents constraints for joining to anything not in its RHS.
213  */
214  if (bms_overlap(joinrel->relids, sjinfo2->min_righthand) &&
215  !bms_overlap(joinrel->relids, sjinfo2->min_lefthand))
218  sjinfo2->min_righthand));
219 
220  /* full joins constrain both sides symmetrically */
221  if (sjinfo2->jointype == JOIN_FULL &&
222  bms_overlap(joinrel->relids, sjinfo2->min_lefthand) &&
223  !bms_overlap(joinrel->relids, sjinfo2->min_righthand))
226  sjinfo2->min_lefthand));
227  }
228 
229  /*
230  * However, when a LATERAL subquery is involved, there will simply not be
231  * any paths for the joinrel that aren't parameterized by whatever the
232  * subquery is parameterized by, unless its parameterization is resolved
233  * within the joinrel. So we might as well allow additional dependencies
234  * on whatever residual lateral dependencies the joinrel will have.
235  */
237  joinrel->lateral_relids);
238 
239  /*
240  * 1. Consider mergejoin paths where both relations must be explicitly
241  * sorted. Skip this if we can't mergejoin.
242  */
243  if (mergejoin_allowed)
244  sort_inner_and_outer(root, joinrel, outerrel, innerrel,
245  jointype, &extra);
246 
247  /*
248  * 2. Consider paths where the outer relation need not be explicitly
249  * sorted. This includes both nestloops and mergejoins where the outer
250  * path is already ordered. Again, skip this if we can't mergejoin.
251  * (That's okay because we know that nestloop can't handle right/full
252  * joins at all, so it wouldn't work in the prohibited cases either.)
253  */
254  if (mergejoin_allowed)
255  match_unsorted_outer(root, joinrel, outerrel, innerrel,
256  jointype, &extra);
257 
258 #ifdef NOT_USED
259 
260  /*
261  * 3. Consider paths where the inner relation need not be explicitly
262  * sorted. This includes mergejoins only (nestloops were already built in
263  * match_unsorted_outer).
264  *
265  * Diked out as redundant 2/13/2000 -- tgl. There isn't any really
266  * significant difference between the inner and outer side of a mergejoin,
267  * so match_unsorted_inner creates no paths that aren't equivalent to
268  * those made by match_unsorted_outer when add_paths_to_joinrel() is
269  * invoked with the two rels given in the other order.
270  */
271  if (mergejoin_allowed)
272  match_unsorted_inner(root, joinrel, outerrel, innerrel,
273  jointype, &extra);
274 #endif
275 
276  /*
277  * 4. Consider paths where both outer and inner relations must be hashed
278  * before being joined. As above, disregard enable_hashjoin for full
279  * joins, because there may be no other alternative.
280  */
281  if (enable_hashjoin || jointype == JOIN_FULL)
282  hash_inner_and_outer(root, joinrel, outerrel, innerrel,
283  jointype, &extra);
284 
285  /*
286  * 5. If inner and outer relations are foreign tables (or joins) belonging
287  * to the same server and assigned to the same user to check access
288  * permissions as, give the FDW a chance to push down joins.
289  */
290  if (joinrel->fdwroutine &&
292  joinrel->fdwroutine->GetForeignJoinPaths(root, joinrel,
293  outerrel, innerrel,
294  jointype, &extra);
295 
296  /*
297  * 6. Finally, give extensions a chance to manipulate the path list.
298  */
300  set_join_pathlist_hook(root, joinrel, outerrel, innerrel,
301  jointype, &extra);
302 }
#define NIL
Definition: pg_list.h:69
SemiAntiJoinFactors semifactors
Definition: relation.h:2240
List * join_info_list
Definition: relation.h:250
Relids min_righthand
Definition: relation.h:1974
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
static void hash_inner_and_outer(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra)
Definition: joinpath.c:1584
Relids lateral_relids
Definition: relation.h:596
SpecialJoinInfo * sjinfo
Definition: relation.h:2239
static void sort_inner_and_outer(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra)
Definition: joinpath.c:786
Relids all_baserels
Definition: relation.h:196
GetForeignJoinPaths_function GetForeignJoinPaths
Definition: fdwapi.h:191
Relids param_source_rels
Definition: relation.h:2241
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:838
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
struct FdwRoutine * fdwroutine
Definition: relation.h:622
List * mergeclause_list
Definition: relation.h:2237
Relids relids
Definition: relation.h:571
bool innerrel_is_unique(PlannerInfo *root, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
Definition: analyzejoins.c:970
static List * select_mergejoin_clauses(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, List *restrictlist, JoinType jointype, bool *mergejoin_allowed)
Definition: joinpath.c:1815
List * restrictlist
Definition: relation.h:2236
set_join_pathlist_hook_type set_join_pathlist_hook
Definition: joinpath.c:27
bool enable_mergejoin
Definition: costsize.c:127
#define lfirst(lc)
Definition: pg_list.h:106
void compute_semi_anti_join_factors(PlannerInfo *root, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, SpecialJoinInfo *sjinfo, List *restrictlist, SemiAntiJoinFactors *semifactors)
Definition: costsize.c:3780
JoinType jointype
Definition: relation.h:1977
bool enable_hashjoin
Definition: costsize.c:128
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
Relids min_lefthand
Definition: relation.h:1973
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:755
static void match_unsorted_outer(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra)
Definition: joinpath.c:1238
static bool allow_star_schema_join ( PlannerInfo root,
Relids  outerrelids,
Relids  inner_paramrels 
)
inlinestatic

Definition at line 320 of file joinpath.c.

References bms_nonempty_difference(), and bms_overlap().

Referenced by try_nestloop_path().

323 {
324  /*
325  * It's a star-schema case if the outer rel provides some but not all of
326  * the inner rel's parameterization.
327  */
328  return (bms_overlap(inner_paramrels, outerrelids) &&
329  bms_nonempty_difference(inner_paramrels, outerrelids));
330 }
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
static bool clause_sides_match_join ( RestrictInfo rinfo,
RelOptInfo outerrel,
RelOptInfo innerrel 
)
inlinestatic

Definition at line 754 of file joinpath.c.

References bms_is_subset(), RestrictInfo::left_relids, RestrictInfo::outer_is_left, RelOptInfo::relids, and RestrictInfo::right_relids.

Referenced by hash_inner_and_outer(), and select_mergejoin_clauses().

756 {
757  if (bms_is_subset(rinfo->left_relids, outerrel->relids) &&
758  bms_is_subset(rinfo->right_relids, innerrel->relids))
759  {
760  /* lefthand side is outer */
761  rinfo->outer_is_left = true;
762  return true;
763  }
764  else if (bms_is_subset(rinfo->left_relids, innerrel->relids) &&
765  bms_is_subset(rinfo->right_relids, outerrel->relids))
766  {
767  /* righthand side is outer */
768  rinfo->outer_is_left = false;
769  return true;
770  }
771  return false; /* no good for these input relations */
772 }
Relids left_relids
Definition: relation.h:1828
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
bool outer_is_left
Definition: relation.h:1856
Relids relids
Definition: relation.h:571
Relids right_relids
Definition: relation.h:1829
static void consider_parallel_mergejoin ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outerrel,
RelOptInfo innerrel,
JoinType  jointype,
JoinPathExtraData extra,
Path inner_cheapest_total 
)
static

Definition at line 1472 of file joinpath.c.

References build_join_pathkeys(), generate_mergejoin_paths(), lfirst, RelOptInfo::partial_pathlist, and Path::pathkeys.

Referenced by match_unsorted_outer().

1479 {
1480  ListCell *lc1;
1481 
1482  /* generate merge join path for each partial outer path */
1483  foreach(lc1, outerrel->partial_pathlist)
1484  {
1485  Path *outerpath = (Path *) lfirst(lc1);
1486  List *merge_pathkeys;
1487 
1488  /*
1489  * Figure out what useful ordering any paths we create will have.
1490  */
1491  merge_pathkeys = build_join_pathkeys(root, joinrel, jointype,
1492  outerpath->pathkeys);
1493 
1494  generate_mergejoin_paths(root, joinrel, innerrel, outerpath, jointype,
1495  extra, false, inner_cheapest_total,
1496  merge_pathkeys, true);
1497  }
1498 }
List * partial_pathlist
Definition: relation.h:587
List * build_join_pathkeys(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, List *outer_pathkeys)
Definition: pathkeys.c:822
static void generate_mergejoin_paths(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *innerrel, Path *outerpath, JoinType jointype, JoinPathExtraData *extra, bool useallclauses, Path *inner_cheapest_total, List *merge_pathkeys, bool is_partial)
Definition: joinpath.c:988
List * pathkeys
Definition: relation.h:1022
#define lfirst(lc)
Definition: pg_list.h:106
Definition: pg_list.h:45
static void consider_parallel_nestloop ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outerrel,
RelOptInfo innerrel,
JoinType  jointype,
JoinPathExtraData extra 
)
static

Definition at line 1512 of file joinpath.c.

References Assert, build_join_pathkeys(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_total_path, create_unique_path(), JOIN_INNER, JOIN_UNIQUE_INNER, lfirst, Path::parallel_safe, RelOptInfo::partial_pathlist, Path::pathkeys, JoinPathExtraData::sjinfo, and try_partial_nestloop_path().

Referenced by match_unsorted_outer().

1518 {
1519  JoinType save_jointype = jointype;
1520  ListCell *lc1;
1521 
1522  if (jointype == JOIN_UNIQUE_INNER)
1523  jointype = JOIN_INNER;
1524 
1525  foreach(lc1, outerrel->partial_pathlist)
1526  {
1527  Path *outerpath = (Path *) lfirst(lc1);
1528  List *pathkeys;
1529  ListCell *lc2;
1530 
1531  /* Figure out what useful ordering any paths we create will have. */
1532  pathkeys = build_join_pathkeys(root, joinrel, jointype,
1533  outerpath->pathkeys);
1534 
1535  /*
1536  * Try the cheapest parameterized paths; only those which will produce
1537  * an unparameterized path when joined to this outerrel will survive
1538  * try_partial_nestloop_path. The cheapest unparameterized path is
1539  * also in this list.
1540  */
1541  foreach(lc2, innerrel->cheapest_parameterized_paths)
1542  {
1543  Path *innerpath = (Path *) lfirst(lc2);
1544 
1545  /* Can't join to an inner path that is not parallel-safe */
1546  if (!innerpath->parallel_safe)
1547  continue;
1548 
1549  /*
1550  * If we're doing JOIN_UNIQUE_INNER, we can only use the inner's
1551  * cheapest_total_path, and we have to unique-ify it. (We might
1552  * be able to relax this to allow other safe, unparameterized
1553  * inner paths, but right now create_unique_path is not on board
1554  * with that.)
1555  */
1556  if (save_jointype == JOIN_UNIQUE_INNER)
1557  {
1558  if (innerpath != innerrel->cheapest_total_path)
1559  continue;
1560  innerpath = (Path *) create_unique_path(root, innerrel,
1561  innerpath,
1562  extra->sjinfo);
1563  Assert(innerpath);
1564  }
1565 
1566  try_partial_nestloop_path(root, joinrel, outerpath, innerpath,
1567  pathkeys, jointype, extra);
1568  }
1569  }
1570 }
List * partial_pathlist
Definition: relation.h:587
List * cheapest_parameterized_paths
Definition: relation.h:591
UniquePath * create_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, SpecialJoinInfo *sjinfo)
Definition: pathnode.c:1427
List * build_join_pathkeys(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, List *outer_pathkeys)
Definition: pathkeys.c:822
JoinType
Definition: nodes.h:673
SpecialJoinInfo * sjinfo
Definition: relation.h:2239
struct Path * cheapest_total_path
Definition: relation.h:589
static void try_partial_nestloop_path(PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *pathkeys, JoinType jointype, JoinPathExtraData *extra)
Definition: joinpath.c:415
List * pathkeys
Definition: relation.h:1022
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
bool parallel_safe
Definition: relation.h:1014
Definition: pg_list.h:45
static void generate_mergejoin_paths ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo innerrel,
Path outerpath,
JoinType  jointype,
JoinPathExtraData extra,
bool  useallclauses,
Path inner_cheapest_total,
List merge_pathkeys,
bool  is_partial 
)
static

Definition at line 988 of file joinpath.c.

References Assert, compare_path_costs(), find_mergeclauses_for_pathkeys(), get_cheapest_path_for_pathkeys(), JOIN_FULL, JOIN_INNER, JOIN_UNIQUE_INNER, JOIN_UNIQUE_OUTER, list_copy(), list_length(), list_truncate(), make_inner_pathkeys_for_merge(), JoinPathExtraData::mergeclause_list, NIL, Path::pathkeys, pathkeys_contained_in(), RelOptInfo::pathlist, STARTUP_COST, TOTAL_COST, and try_mergejoin_path().

Referenced by consider_parallel_mergejoin(), and match_unsorted_outer().

998 {
999  List *mergeclauses;
1000  List *innersortkeys;
1001  List *trialsortkeys;
1002  Path *cheapest_startup_inner;
1003  Path *cheapest_total_inner;
1004  JoinType save_jointype = jointype;
1005  int num_sortkeys;
1006  int sortkeycnt;
1007 
1008  if (jointype == JOIN_UNIQUE_OUTER || jointype == JOIN_UNIQUE_INNER)
1009  jointype = JOIN_INNER;
1010 
1011  /* Look for useful mergeclauses (if any) */
1012  mergeclauses = find_mergeclauses_for_pathkeys(root,
1013  outerpath->pathkeys,
1014  true,
1015  extra->mergeclause_list);
1016 
1017  /*
1018  * Done with this outer path if no chance for a mergejoin.
1019  *
1020  * Special corner case: for "x FULL JOIN y ON true", there will be no join
1021  * clauses at all. Ordinarily we'd generate a clauseless nestloop path,
1022  * but since mergejoin is our only join type that supports FULL JOIN
1023  * without any join clauses, it's necessary to generate a clauseless
1024  * mergejoin path instead.
1025  */
1026  if (mergeclauses == NIL)
1027  {
1028  if (jointype == JOIN_FULL)
1029  /* okay to try for mergejoin */ ;
1030  else
1031  return;
1032  }
1033  if (useallclauses &&
1034  list_length(mergeclauses) != list_length(extra->mergeclause_list))
1035  return;
1036 
1037  /* Compute the required ordering of the inner path */
1038  innersortkeys = make_inner_pathkeys_for_merge(root,
1039  mergeclauses,
1040  outerpath->pathkeys);
1041 
1042  /*
1043  * Generate a mergejoin on the basis of sorting the cheapest inner. Since
1044  * a sort will be needed, only cheapest total cost matters. (But
1045  * try_mergejoin_path will do the right thing if inner_cheapest_total is
1046  * already correctly sorted.)
1047  */
1048  try_mergejoin_path(root,
1049  joinrel,
1050  outerpath,
1051  inner_cheapest_total,
1052  merge_pathkeys,
1053  mergeclauses,
1054  NIL,
1055  innersortkeys,
1056  jointype,
1057  extra,
1058  is_partial);
1059 
1060  /* Can't do anything else if inner path needs to be unique'd */
1061  if (save_jointype == JOIN_UNIQUE_INNER)
1062  return;
1063 
1064  /*
1065  * Look for presorted inner paths that satisfy the innersortkey list ---
1066  * or any truncation thereof, if we are allowed to build a mergejoin using
1067  * a subset of the merge clauses. Here, we consider both cheap startup
1068  * cost and cheap total cost.
1069  *
1070  * Currently we do not consider parameterized inner paths here. This
1071  * interacts with decisions elsewhere that also discriminate against
1072  * mergejoins with parameterized inputs; see comments in
1073  * src/backend/optimizer/README.
1074  *
1075  * As we shorten the sortkey list, we should consider only paths that are
1076  * strictly cheaper than (in particular, not the same as) any path found
1077  * in an earlier iteration. Otherwise we'd be intentionally using fewer
1078  * merge keys than a given path allows (treating the rest as plain
1079  * joinquals), which is unlikely to be a good idea. Also, eliminating
1080  * paths here on the basis of compare_path_costs is a lot cheaper than
1081  * building the mergejoin path only to throw it away.
1082  *
1083  * If inner_cheapest_total is well enough sorted to have not required a
1084  * sort in the path made above, we shouldn't make a duplicate path with
1085  * it, either. We handle that case with the same logic that handles the
1086  * previous consideration, by initializing the variables that track
1087  * cheapest-so-far properly. Note that we do NOT reject
1088  * inner_cheapest_total if we find it matches some shorter set of
1089  * pathkeys. That case corresponds to using fewer mergekeys to avoid
1090  * sorting inner_cheapest_total, whereas we did sort it above, so the
1091  * plans being considered are different.
1092  */
1093  if (pathkeys_contained_in(innersortkeys,
1094  inner_cheapest_total->pathkeys))
1095  {
1096  /* inner_cheapest_total didn't require a sort */
1097  cheapest_startup_inner = inner_cheapest_total;
1098  cheapest_total_inner = inner_cheapest_total;
1099  }
1100  else
1101  {
1102  /* it did require a sort, at least for the full set of keys */
1103  cheapest_startup_inner = NULL;
1104  cheapest_total_inner = NULL;
1105  }
1106  num_sortkeys = list_length(innersortkeys);
1107  if (num_sortkeys > 1 && !useallclauses)
1108  trialsortkeys = list_copy(innersortkeys); /* need modifiable copy */
1109  else
1110  trialsortkeys = innersortkeys; /* won't really truncate */
1111 
1112  for (sortkeycnt = num_sortkeys; sortkeycnt > 0; sortkeycnt--)
1113  {
1114  Path *innerpath;
1115  List *newclauses = NIL;
1116 
1117  /*
1118  * Look for an inner path ordered well enough for the first
1119  * 'sortkeycnt' innersortkeys. NB: trialsortkeys list is modified
1120  * destructively, which is why we made a copy...
1121  */
1122  trialsortkeys = list_truncate(trialsortkeys, sortkeycnt);
1123  innerpath = get_cheapest_path_for_pathkeys(innerrel->pathlist,
1124  trialsortkeys,
1125  NULL,
1126  TOTAL_COST,
1127  is_partial);
1128  if (innerpath != NULL &&
1129  (cheapest_total_inner == NULL ||
1130  compare_path_costs(innerpath, cheapest_total_inner,
1131  TOTAL_COST) < 0))
1132  {
1133  /* Found a cheap (or even-cheaper) sorted path */
1134  /* Select the right mergeclauses, if we didn't already */
1135  if (sortkeycnt < num_sortkeys)
1136  {
1137  newclauses =
1139  trialsortkeys,
1140  false,
1141  mergeclauses);
1142  Assert(newclauses != NIL);
1143  }
1144  else
1145  newclauses = mergeclauses;
1146  try_mergejoin_path(root,
1147  joinrel,
1148  outerpath,
1149  innerpath,
1150  merge_pathkeys,
1151  newclauses,
1152  NIL,
1153  NIL,
1154  jointype,
1155  extra,
1156  is_partial);
1157  cheapest_total_inner = innerpath;
1158  }
1159  /* Same on the basis of cheapest startup cost ... */
1160  innerpath = get_cheapest_path_for_pathkeys(innerrel->pathlist,
1161  trialsortkeys,
1162  NULL,
1163  STARTUP_COST,
1164  is_partial);
1165  if (innerpath != NULL &&
1166  (cheapest_startup_inner == NULL ||
1167  compare_path_costs(innerpath, cheapest_startup_inner,
1168  STARTUP_COST) < 0))
1169  {
1170  /* Found a cheap (or even-cheaper) sorted path */
1171  if (innerpath != cheapest_total_inner)
1172  {
1173  /*
1174  * Avoid rebuilding clause list if we already made one; saves
1175  * memory in big join trees...
1176  */
1177  if (newclauses == NIL)
1178  {
1179  if (sortkeycnt < num_sortkeys)
1180  {
1181  newclauses =
1183  trialsortkeys,
1184  false,
1185  mergeclauses);
1186  Assert(newclauses != NIL);
1187  }
1188  else
1189  newclauses = mergeclauses;
1190  }
1191  try_mergejoin_path(root,
1192  joinrel,
1193  outerpath,
1194  innerpath,
1195  merge_pathkeys,
1196  newclauses,
1197  NIL,
1198  NIL,
1199  jointype,
1200  extra,
1201  is_partial);
1202  }
1203  cheapest_startup_inner = innerpath;
1204  }
1205 
1206  /*
1207  * Don't consider truncated sortkeys if we need all clauses.
1208  */
1209  if (useallclauses)
1210  break;
1211  }
1212 }
Path * get_cheapest_path_for_pathkeys(List *paths, List *pathkeys, Relids required_outer, CostSelector cost_criterion, bool require_parallel_safe)
Definition: pathkeys.c:343
static void try_mergejoin_path(PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *pathkeys, List *mergeclauses, List *outersortkeys, List *innersortkeys, JoinType jointype, JoinPathExtraData *extra, bool is_partial)
Definition: joinpath.c:469
#define NIL
Definition: pg_list.h:69
List * list_truncate(List *list, int new_size)
Definition: list.c:350
List * list_copy(const List *oldlist)
Definition: list.c:1160
List * make_inner_pathkeys_for_merge(PlannerInfo *root, List *mergeclauses, List *outer_pathkeys)
Definition: pathkeys.c:1292
JoinType
Definition: nodes.h:673
int compare_path_costs(Path *path1, Path *path2, CostSelector criterion)
Definition: pathnode.c:61
List * mergeclause_list
Definition: relation.h:2237
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
List * pathkeys
Definition: relation.h:1022
#define Assert(condition)
Definition: c.h:664
static int list_length(const List *l)
Definition: pg_list.h:89
List * find_mergeclauses_for_pathkeys(PlannerInfo *root, List *pathkeys, bool outer_keys, List *restrictinfos)
Definition: pathkeys.c:1003
List * pathlist
Definition: relation.h:585
Definition: pg_list.h:45
static void hash_inner_and_outer ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outerrel,
RelOptInfo innerrel,
JoinType  jointype,
JoinPathExtraData extra 
)
static

Definition at line 1584 of file joinpath.c.

References Assert, bms_is_empty(), RestrictInfo::can_join, RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_startup_path, RelOptInfo::cheapest_total_path, clause_sides_match_join(), RelOptInfo::consider_parallel, create_unique_path(), get_cheapest_parallel_safe_total_inner(), RestrictInfo::hashjoinoperator, InvalidOid, IS_OUTER_JOIN, RestrictInfo::is_pushed_down, JOIN_FULL, JOIN_INNER, JOIN_RIGHT, JOIN_UNIQUE_INNER, JOIN_UNIQUE_OUTER, lappend(), RelOptInfo::lateral_relids, lfirst, linitial, NIL, Path::parallel_safe, RelOptInfo::partial_pathlist, PATH_PARAM_BY_REL, RelOptInfo::pathlist, JoinPathExtraData::restrictlist, JoinPathExtraData::sjinfo, try_hashjoin_path(), and try_partial_hashjoin_path().

Referenced by add_paths_to_joinrel().

1590 {
1591  JoinType save_jointype = jointype;
1592  bool isouterjoin = IS_OUTER_JOIN(jointype);
1593  List *hashclauses;
1594  ListCell *l;
1595 
1596  /*
1597  * We need to build only one hashclauses list for any given pair of outer
1598  * and inner relations; all of the hashable clauses will be used as keys.
1599  *
1600  * Scan the join's restrictinfo list to find hashjoinable clauses that are
1601  * usable with this pair of sub-relations.
1602  */
1603  hashclauses = NIL;
1604  foreach(l, extra->restrictlist)
1605  {
1606  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
1607 
1608  /*
1609  * If processing an outer join, only use its own join clauses for
1610  * hashing. For inner joins we need not be so picky.
1611  */
1612  if (isouterjoin && restrictinfo->is_pushed_down)
1613  continue;
1614 
1615  if (!restrictinfo->can_join ||
1616  restrictinfo->hashjoinoperator == InvalidOid)
1617  continue; /* not hashjoinable */
1618 
1619  /*
1620  * Check if clause has the form "outer op inner" or "inner op outer".
1621  */
1622  if (!clause_sides_match_join(restrictinfo, outerrel, innerrel))
1623  continue; /* no good for these input relations */
1624 
1625  hashclauses = lappend(hashclauses, restrictinfo);
1626  }
1627 
1628  /* If we found any usable hashclauses, make paths */
1629  if (hashclauses)
1630  {
1631  /*
1632  * We consider both the cheapest-total-cost and cheapest-startup-cost
1633  * outer paths. There's no need to consider any but the
1634  * cheapest-total-cost inner path, however.
1635  */
1636  Path *cheapest_startup_outer = outerrel->cheapest_startup_path;
1637  Path *cheapest_total_outer = outerrel->cheapest_total_path;
1638  Path *cheapest_total_inner = innerrel->cheapest_total_path;
1639 
1640  /*
1641  * If either cheapest-total path is parameterized by the other rel, we
1642  * can't use a hashjoin. (There's no use looking for alternative
1643  * input paths, since these should already be the least-parameterized
1644  * available paths.)
1645  */
1646  if (PATH_PARAM_BY_REL(cheapest_total_outer, innerrel) ||
1647  PATH_PARAM_BY_REL(cheapest_total_inner, outerrel))
1648  return;
1649 
1650  /* Unique-ify if need be; we ignore parameterized possibilities */
1651  if (jointype == JOIN_UNIQUE_OUTER)
1652  {
1653  cheapest_total_outer = (Path *)
1654  create_unique_path(root, outerrel,
1655  cheapest_total_outer, extra->sjinfo);
1656  Assert(cheapest_total_outer);
1657  jointype = JOIN_INNER;
1658  try_hashjoin_path(root,
1659  joinrel,
1660  cheapest_total_outer,
1661  cheapest_total_inner,
1662  hashclauses,
1663  jointype,
1664  extra);
1665  /* no possibility of cheap startup here */
1666  }
1667  else if (jointype == JOIN_UNIQUE_INNER)
1668  {
1669  cheapest_total_inner = (Path *)
1670  create_unique_path(root, innerrel,
1671  cheapest_total_inner, extra->sjinfo);
1672  Assert(cheapest_total_inner);
1673  jointype = JOIN_INNER;
1674  try_hashjoin_path(root,
1675  joinrel,
1676  cheapest_total_outer,
1677  cheapest_total_inner,
1678  hashclauses,
1679  jointype,
1680  extra);
1681  if (cheapest_startup_outer != NULL &&
1682  cheapest_startup_outer != cheapest_total_outer)
1683  try_hashjoin_path(root,
1684  joinrel,
1685  cheapest_startup_outer,
1686  cheapest_total_inner,
1687  hashclauses,
1688  jointype,
1689  extra);
1690  }
1691  else
1692  {
1693  /*
1694  * For other jointypes, we consider the cheapest startup outer
1695  * together with the cheapest total inner, and then consider
1696  * pairings of cheapest-total paths including parameterized ones.
1697  * There is no use in generating parameterized paths on the basis
1698  * of possibly cheap startup cost, so this is sufficient.
1699  */
1700  ListCell *lc1;
1701  ListCell *lc2;
1702 
1703  if (cheapest_startup_outer != NULL)
1704  try_hashjoin_path(root,
1705  joinrel,
1706  cheapest_startup_outer,
1707  cheapest_total_inner,
1708  hashclauses,
1709  jointype,
1710  extra);
1711 
1712  foreach(lc1, outerrel->cheapest_parameterized_paths)
1713  {
1714  Path *outerpath = (Path *) lfirst(lc1);
1715 
1716  /*
1717  * We cannot use an outer path that is parameterized by the
1718  * inner rel.
1719  */
1720  if (PATH_PARAM_BY_REL(outerpath, innerrel))
1721  continue;
1722 
1723  foreach(lc2, innerrel->cheapest_parameterized_paths)
1724  {
1725  Path *innerpath = (Path *) lfirst(lc2);
1726 
1727  /*
1728  * We cannot use an inner path that is parameterized by
1729  * the outer rel, either.
1730  */
1731  if (PATH_PARAM_BY_REL(innerpath, outerrel))
1732  continue;
1733 
1734  if (outerpath == cheapest_startup_outer &&
1735  innerpath == cheapest_total_inner)
1736  continue; /* already tried it */
1737 
1738  try_hashjoin_path(root,
1739  joinrel,
1740  outerpath,
1741  innerpath,
1742  hashclauses,
1743  jointype,
1744  extra);
1745  }
1746  }
1747  }
1748 
1749  /*
1750  * If the joinrel is parallel-safe, we may be able to consider a
1751  * partial hash join. However, we can't handle JOIN_UNIQUE_OUTER,
1752  * because the outer path will be partial, and therefore we won't be
1753  * able to properly guarantee uniqueness. Similarly, we can't handle
1754  * JOIN_FULL and JOIN_RIGHT, because they can produce false null
1755  * extended rows. Also, the resulting path must not be parameterized.
1756  */
1757  if (joinrel->consider_parallel &&
1758  save_jointype != JOIN_UNIQUE_OUTER &&
1759  save_jointype != JOIN_FULL &&
1760  save_jointype != JOIN_RIGHT &&
1761  outerrel->partial_pathlist != NIL &&
1762  bms_is_empty(joinrel->lateral_relids))
1763  {
1764  Path *cheapest_partial_outer;
1765  Path *cheapest_safe_inner = NULL;
1766 
1767  cheapest_partial_outer =
1768  (Path *) linitial(outerrel->partial_pathlist);
1769 
1770  /*
1771  * Normally, given that the joinrel is parallel-safe, the cheapest
1772  * total inner path will also be parallel-safe, but if not, we'll
1773  * have to search for the cheapest safe, unparameterized inner
1774  * path. If doing JOIN_UNIQUE_INNER, we can't use any alternative
1775  * inner path.
1776  */
1777  if (cheapest_total_inner->parallel_safe)
1778  cheapest_safe_inner = cheapest_total_inner;
1779  else if (save_jointype != JOIN_UNIQUE_INNER)
1780  cheapest_safe_inner =
1782 
1783  if (cheapest_safe_inner != NULL)
1784  try_partial_hashjoin_path(root, joinrel,
1785  cheapest_partial_outer,
1786  cheapest_safe_inner,
1787  hashclauses, jointype, extra);
1788  }
1789  }
1790 }
#define NIL
Definition: pg_list.h:69
static void try_hashjoin_path(PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *hashclauses, JoinType jointype, JoinPathExtraData *extra)
Definition: joinpath.c:634
struct Path * cheapest_startup_path
Definition: relation.h:588
static void try_partial_hashjoin_path(PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *hashclauses, JoinType jointype, JoinPathExtraData *extra)
Definition: joinpath.c:695
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:722
List * partial_pathlist
Definition: relation.h:587
List * cheapest_parameterized_paths
Definition: relation.h:591
UniquePath * create_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, SpecialJoinInfo *sjinfo)
Definition: pathnode.c:1427
JoinType
Definition: nodes.h:673
Relids lateral_relids
Definition: relation.h:596
SpecialJoinInfo * sjinfo
Definition: relation.h:2239
#define linitial(l)
Definition: pg_list.h:111
bool can_join
Definition: relation.h:1807
struct Path * cheapest_total_path
Definition: relation.h:589
Path * get_cheapest_parallel_safe_total_inner(List *paths)
Definition: pathkeys.c:421
List * lappend(List *list, void *datum)
Definition: list.c:128
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
List * restrictlist
Definition: relation.h:2236
#define InvalidOid
Definition: postgres_ext.h:36
bool is_pushed_down
Definition: relation.h:1803
#define PATH_PARAM_BY_REL(path, rel)
Definition: joinpath.c:29
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
bool parallel_safe
Definition: relation.h:1014
static bool clause_sides_match_join(RestrictInfo *rinfo, RelOptInfo *outerrel, RelOptInfo *innerrel)
Definition: joinpath.c:754
Oid hashjoinoperator
Definition: relation.h:1859
bool consider_parallel
Definition: relation.h:579
List * pathlist
Definition: relation.h:585
Definition: pg_list.h:45
static void match_unsorted_outer ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outerrel,
RelOptInfo innerrel,
JoinType  jointype,
JoinPathExtraData extra 
)
static

Definition at line 1238 of file joinpath.c.

References Assert, bms_is_empty(), build_join_pathkeys(), RelOptInfo::cheapest_parameterized_paths, RelOptInfo::cheapest_total_path, RelOptInfo::consider_parallel, consider_parallel_mergejoin(), consider_parallel_nestloop(), create_material_path(), create_unique_path(), elog, enable_material, ERROR, ExecMaterializesOutput(), generate_mergejoin_paths(), get_cheapest_parallel_safe_total_inner(), JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_SEMI, JOIN_UNIQUE_INNER, JOIN_UNIQUE_OUTER, RelOptInfo::lateral_relids, lfirst, NIL, Path::parallel_safe, RelOptInfo::partial_pathlist, PATH_PARAM_BY_REL, Path::pathkeys, RelOptInfo::pathlist, Path::pathtype, JoinPathExtraData::sjinfo, and try_nestloop_path().

Referenced by add_paths_to_joinrel().

1244 {
1245  JoinType save_jointype = jointype;
1246  bool nestjoinOK;
1247  bool useallclauses;
1248  Path *inner_cheapest_total = innerrel->cheapest_total_path;
1249  Path *matpath = NULL;
1250  ListCell *lc1;
1251 
1252  /*
1253  * Nestloop only supports inner, left, semi, and anti joins. Also, if we
1254  * are doing a right or full mergejoin, we must use *all* the mergeclauses
1255  * as join clauses, else we will not have a valid plan. (Although these
1256  * two flags are currently inverses, keep them separate for clarity and
1257  * possible future changes.)
1258  */
1259  switch (jointype)
1260  {
1261  case JOIN_INNER:
1262  case JOIN_LEFT:
1263  case JOIN_SEMI:
1264  case JOIN_ANTI:
1265  nestjoinOK = true;
1266  useallclauses = false;
1267  break;
1268  case JOIN_RIGHT:
1269  case JOIN_FULL:
1270  nestjoinOK = false;
1271  useallclauses = true;
1272  break;
1273  case JOIN_UNIQUE_OUTER:
1274  case JOIN_UNIQUE_INNER:
1275  jointype = JOIN_INNER;
1276  nestjoinOK = true;
1277  useallclauses = false;
1278  break;
1279  default:
1280  elog(ERROR, "unrecognized join type: %d",
1281  (int) jointype);
1282  nestjoinOK = false; /* keep compiler quiet */
1283  useallclauses = false;
1284  break;
1285  }
1286 
1287  /*
1288  * If inner_cheapest_total is parameterized by the outer rel, ignore it;
1289  * we will consider it below as a member of cheapest_parameterized_paths,
1290  * but the other possibilities considered in this routine aren't usable.
1291  */
1292  if (PATH_PARAM_BY_REL(inner_cheapest_total, outerrel))
1293  inner_cheapest_total = NULL;
1294 
1295  /*
1296  * If we need to unique-ify the inner path, we will consider only the
1297  * cheapest-total inner.
1298  */
1299  if (save_jointype == JOIN_UNIQUE_INNER)
1300  {
1301  /* No way to do this with an inner path parameterized by outer rel */
1302  if (inner_cheapest_total == NULL)
1303  return;
1304  inner_cheapest_total = (Path *)
1305  create_unique_path(root, innerrel, inner_cheapest_total, extra->sjinfo);
1306  Assert(inner_cheapest_total);
1307  }
1308  else if (nestjoinOK)
1309  {
1310  /*
1311  * Consider materializing the cheapest inner path, unless
1312  * enable_material is off or the path in question materializes its
1313  * output anyway.
1314  */
1315  if (enable_material && inner_cheapest_total != NULL &&
1316  !ExecMaterializesOutput(inner_cheapest_total->pathtype))
1317  matpath = (Path *)
1318  create_material_path(innerrel, inner_cheapest_total);
1319  }
1320 
1321  foreach(lc1, outerrel->pathlist)
1322  {
1323  Path *outerpath = (Path *) lfirst(lc1);
1324  List *merge_pathkeys;
1325 
1326  /*
1327  * We cannot use an outer path that is parameterized by the inner rel.
1328  */
1329  if (PATH_PARAM_BY_REL(outerpath, innerrel))
1330  continue;
1331 
1332  /*
1333  * If we need to unique-ify the outer path, it's pointless to consider
1334  * any but the cheapest outer. (XXX we don't consider parameterized
1335  * outers, nor inners, for unique-ified cases. Should we?)
1336  */
1337  if (save_jointype == JOIN_UNIQUE_OUTER)
1338  {
1339  if (outerpath != outerrel->cheapest_total_path)
1340  continue;
1341  outerpath = (Path *) create_unique_path(root, outerrel,
1342  outerpath, extra->sjinfo);
1343  Assert(outerpath);
1344  }
1345 
1346  /*
1347  * The result will have this sort order (even if it is implemented as
1348  * a nestloop, and even if some of the mergeclauses are implemented by
1349  * qpquals rather than as true mergeclauses):
1350  */
1351  merge_pathkeys = build_join_pathkeys(root, joinrel, jointype,
1352  outerpath->pathkeys);
1353 
1354  if (save_jointype == JOIN_UNIQUE_INNER)
1355  {
1356  /*
1357  * Consider nestloop join, but only with the unique-ified cheapest
1358  * inner path
1359  */
1360  try_nestloop_path(root,
1361  joinrel,
1362  outerpath,
1363  inner_cheapest_total,
1364  merge_pathkeys,
1365  jointype,
1366  extra);
1367  }
1368  else if (nestjoinOK)
1369  {
1370  /*
1371  * Consider nestloop joins using this outer path and various
1372  * available paths for the inner relation. We consider the
1373  * cheapest-total paths for each available parameterization of the
1374  * inner relation, including the unparameterized case.
1375  */
1376  ListCell *lc2;
1377 
1378  foreach(lc2, innerrel->cheapest_parameterized_paths)
1379  {
1380  Path *innerpath = (Path *) lfirst(lc2);
1381 
1382  try_nestloop_path(root,
1383  joinrel,
1384  outerpath,
1385  innerpath,
1386  merge_pathkeys,
1387  jointype,
1388  extra);
1389  }
1390 
1391  /* Also consider materialized form of the cheapest inner path */
1392  if (matpath != NULL)
1393  try_nestloop_path(root,
1394  joinrel,
1395  outerpath,
1396  matpath,
1397  merge_pathkeys,
1398  jointype,
1399  extra);
1400  }
1401 
1402  /* Can't do anything else if outer path needs to be unique'd */
1403  if (save_jointype == JOIN_UNIQUE_OUTER)
1404  continue;
1405 
1406  /* Can't do anything else if inner rel is parameterized by outer */
1407  if (inner_cheapest_total == NULL)
1408  continue;
1409 
1410  /* Generate merge join paths */
1411  generate_mergejoin_paths(root, joinrel, innerrel, outerpath,
1412  save_jointype, extra, useallclauses,
1413  inner_cheapest_total, merge_pathkeys,
1414  false);
1415  }
1416 
1417  /*
1418  * Consider partial nestloop and mergejoin plan if outerrel has any
1419  * partial path and the joinrel is parallel-safe. However, we can't
1420  * handle JOIN_UNIQUE_OUTER, because the outer path will be partial, and
1421  * therefore we won't be able to properly guarantee uniqueness. Nor can
1422  * we handle extra_lateral_rels, since partial paths must not be
1423  * parameterized. Similarly, we can't handle JOIN_FULL and JOIN_RIGHT,
1424  * because they can produce false null extended rows.
1425  */
1426  if (joinrel->consider_parallel &&
1427  save_jointype != JOIN_UNIQUE_OUTER &&
1428  save_jointype != JOIN_FULL &&
1429  save_jointype != JOIN_RIGHT &&
1430  outerrel->partial_pathlist != NIL &&
1431  bms_is_empty(joinrel->lateral_relids))
1432  {
1433  if (nestjoinOK)
1434  consider_parallel_nestloop(root, joinrel, outerrel, innerrel,
1435  save_jointype, extra);
1436 
1437  /*
1438  * If inner_cheapest_total is NULL or non parallel-safe then find the
1439  * cheapest total parallel safe path. If doing JOIN_UNIQUE_INNER, we
1440  * can't use any alternative inner path.
1441  */
1442  if (inner_cheapest_total == NULL ||
1443  !inner_cheapest_total->parallel_safe)
1444  {
1445  if (save_jointype == JOIN_UNIQUE_INNER)
1446  return;
1447 
1448  inner_cheapest_total = get_cheapest_parallel_safe_total_inner(
1449  innerrel->pathlist);
1450  }
1451 
1452  if (inner_cheapest_total)
1453  consider_parallel_mergejoin(root, joinrel, outerrel, innerrel,
1454  save_jointype, extra,
1455  inner_cheapest_total);
1456  }
1457 }
#define NIL
Definition: pg_list.h:69
static void try_nestloop_path(PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *pathkeys, JoinType jointype, JoinPathExtraData *extra)
Definition: joinpath.c:338
MaterialPath * create_material_path(RelOptInfo *rel, Path *subpath)
Definition: pathnode.c:1388
List * partial_pathlist
Definition: relation.h:587
List * cheapest_parameterized_paths
Definition: relation.h:591
UniquePath * create_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, SpecialJoinInfo *sjinfo)
Definition: pathnode.c:1427
List * build_join_pathkeys(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, List *outer_pathkeys)
Definition: pathkeys.c:822
JoinType
Definition: nodes.h:673
NodeTag pathtype
Definition: relation.h:1006
Relids lateral_relids
Definition: relation.h:596
SpecialJoinInfo * sjinfo
Definition: relation.h:2239
#define ERROR
Definition: elog.h:43
struct Path * cheapest_total_path
Definition: relation.h:589
static void generate_mergejoin_paths(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *innerrel, Path *outerpath, JoinType jointype, JoinPathExtraData *extra, bool useallclauses, Path *inner_cheapest_total, List *merge_pathkeys, bool is_partial)
Definition: joinpath.c:988
Path * get_cheapest_parallel_safe_total_inner(List *paths)
Definition: pathkeys.c:421
static void consider_parallel_nestloop(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra)
Definition: joinpath.c:1512
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
static void consider_parallel_mergejoin(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outerrel, RelOptInfo *innerrel, JoinType jointype, JoinPathExtraData *extra, Path *inner_cheapest_total)
Definition: joinpath.c:1472
#define PATH_PARAM_BY_REL(path, rel)
Definition: joinpath.c:29
List * pathkeys
Definition: relation.h:1022
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
bool parallel_safe
Definition: relation.h:1014
bool consider_parallel
Definition: relation.h:579
bool ExecMaterializesOutput(NodeTag plantype)
Definition: execAmi.c:577
List * pathlist
Definition: relation.h:585
#define elog
Definition: elog.h:219
Definition: pg_list.h:45
bool enable_material
Definition: costsize.c:126
static List * select_mergejoin_clauses ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outerrel,
RelOptInfo innerrel,
List restrictlist,
JoinType  jointype,
bool mergejoin_allowed 
)
static

Definition at line 1815 of file joinpath.c.

References RestrictInfo::can_join, RestrictInfo::clause, clause_sides_match_join(), EC_MUST_BE_REDUNDANT, IS_OUTER_JOIN, RestrictInfo::is_pushed_down, IsA, JOIN_FULL, JOIN_RIGHT, lappend(), RestrictInfo::left_ec, lfirst, RestrictInfo::mergeopfamilies, NIL, RestrictInfo::right_ec, and update_mergeclause_eclasses().

Referenced by add_paths_to_joinrel().

1822 {
1823  List *result_list = NIL;
1824  bool isouterjoin = IS_OUTER_JOIN(jointype);
1825  bool have_nonmergeable_joinclause = false;
1826  ListCell *l;
1827 
1828  foreach(l, restrictlist)
1829  {
1830  RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
1831 
1832  /*
1833  * If processing an outer join, only use its own join clauses in the
1834  * merge. For inner joins we can use pushed-down clauses too. (Note:
1835  * we don't set have_nonmergeable_joinclause here because pushed-down
1836  * clauses will become otherquals not joinquals.)
1837  */
1838  if (isouterjoin && restrictinfo->is_pushed_down)
1839  continue;
1840 
1841  /* Check that clause is a mergeable operator clause */
1842  if (!restrictinfo->can_join ||
1843  restrictinfo->mergeopfamilies == NIL)
1844  {
1845  /*
1846  * The executor can handle extra joinquals that are constants, but
1847  * not anything else, when doing right/full merge join. (The
1848  * reason to support constants is so we can do FULL JOIN ON
1849  * FALSE.)
1850  */
1851  if (!restrictinfo->clause || !IsA(restrictinfo->clause, Const))
1852  have_nonmergeable_joinclause = true;
1853  continue; /* not mergejoinable */
1854  }
1855 
1856  /*
1857  * Check if clause has the form "outer op inner" or "inner op outer".
1858  */
1859  if (!clause_sides_match_join(restrictinfo, outerrel, innerrel))
1860  {
1861  have_nonmergeable_joinclause = true;
1862  continue; /* no good for these input relations */
1863  }
1864 
1865  /*
1866  * Insist that each side have a non-redundant eclass. This
1867  * restriction is needed because various bits of the planner expect
1868  * that each clause in a merge be associable with some pathkey in a
1869  * canonical pathkey list, but redundant eclasses can't appear in
1870  * canonical sort orderings. (XXX it might be worth relaxing this,
1871  * but not enough time to address it for 8.3.)
1872  *
1873  * Note: it would be bad if this condition failed for an otherwise
1874  * mergejoinable FULL JOIN clause, since that would result in
1875  * undesirable planner failure. I believe that is not possible
1876  * however; a variable involved in a full join could only appear in
1877  * below_outer_join eclasses, which aren't considered redundant.
1878  *
1879  * This case *can* happen for left/right join clauses: the outer-side
1880  * variable could be equated to a constant. Because we will propagate
1881  * that constant across the join clause, the loss of ability to do a
1882  * mergejoin is not really all that big a deal, and so it's not clear
1883  * that improving this is important.
1884  */
1885  update_mergeclause_eclasses(root, restrictinfo);
1886 
1887  if (EC_MUST_BE_REDUNDANT(restrictinfo->left_ec) ||
1888  EC_MUST_BE_REDUNDANT(restrictinfo->right_ec))
1889  {
1890  have_nonmergeable_joinclause = true;
1891  continue; /* can't handle redundant eclasses */
1892  }
1893 
1894  result_list = lappend(result_list, restrictinfo);
1895  }
1896 
1897  /*
1898  * Report whether mergejoin is allowed (see comment at top of function).
1899  */
1900  switch (jointype)
1901  {
1902  case JOIN_RIGHT:
1903  case JOIN_FULL:
1904  *mergejoin_allowed = !have_nonmergeable_joinclause;
1905  break;
1906  default:
1907  *mergejoin_allowed = true;
1908  break;
1909  }
1910 
1911  return result_list;
1912 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:722
EquivalenceClass * right_ec
Definition: relation.h:1850
List * mergeopfamilies
Definition: relation.h:1846
bool can_join
Definition: relation.h:1807
#define EC_MUST_BE_REDUNDANT(eclass)
Definition: relation.h:847
List * lappend(List *list, void *datum)
Definition: list.c:128
Expr * clause
Definition: relation.h:1801
bool is_pushed_down
Definition: relation.h:1803
#define lfirst(lc)
Definition: pg_list.h:106
static bool clause_sides_match_join(RestrictInfo *rinfo, RelOptInfo *outerrel, RelOptInfo *innerrel)
Definition: joinpath.c:754
EquivalenceClass * left_ec
Definition: relation.h:1849
Definition: pg_list.h:45
void update_mergeclause_eclasses(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: pathkeys.c:968
static void sort_inner_and_outer ( PlannerInfo root,
RelOptInfo joinrel,
RelOptInfo outerrel,
RelOptInfo innerrel,
JoinType  jointype,
JoinPathExtraData extra 
)
static

Definition at line 786 of file joinpath.c.

References Assert, bms_is_empty(), build_join_pathkeys(), RelOptInfo::cheapest_total_path, RelOptInfo::consider_parallel, create_unique_path(), find_mergeclauses_for_pathkeys(), get_cheapest_parallel_safe_total_inner(), JOIN_FULL, JOIN_INNER, JOIN_RIGHT, JOIN_UNIQUE_INNER, JOIN_UNIQUE_OUTER, RelOptInfo::lateral_relids, lcons(), lfirst, linitial, list_copy(), list_delete_ptr(), list_head(), list_length(), make_inner_pathkeys_for_merge(), JoinPathExtraData::mergeclause_list, NIL, Path::parallel_safe, RelOptInfo::partial_pathlist, PATH_PARAM_BY_REL, RelOptInfo::pathlist, select_outer_pathkeys_for_merge(), JoinPathExtraData::sjinfo, try_mergejoin_path(), and try_partial_mergejoin_path().

Referenced by add_paths_to_joinrel().

792 {
793  JoinType save_jointype = jointype;
794  Path *outer_path;
795  Path *inner_path;
796  Path *cheapest_partial_outer = NULL;
797  Path *cheapest_safe_inner = NULL;
798  List *all_pathkeys;
799  ListCell *l;
800 
801  /*
802  * We only consider the cheapest-total-cost input paths, since we are
803  * assuming here that a sort is required. We will consider
804  * cheapest-startup-cost input paths later, and only if they don't need a
805  * sort.
806  *
807  * This function intentionally does not consider parameterized input
808  * paths, except when the cheapest-total is parameterized. If we did so,
809  * we'd have a combinatorial explosion of mergejoin paths of dubious
810  * value. This interacts with decisions elsewhere that also discriminate
811  * against mergejoins with parameterized inputs; see comments in
812  * src/backend/optimizer/README.
813  */
814  outer_path = outerrel->cheapest_total_path;
815  inner_path = innerrel->cheapest_total_path;
816 
817  /*
818  * If either cheapest-total path is parameterized by the other rel, we
819  * can't use a mergejoin. (There's no use looking for alternative input
820  * paths, since these should already be the least-parameterized available
821  * paths.)
822  */
823  if (PATH_PARAM_BY_REL(outer_path, innerrel) ||
824  PATH_PARAM_BY_REL(inner_path, outerrel))
825  return;
826 
827  /*
828  * If unique-ification is requested, do it and then handle as a plain
829  * inner join.
830  */
831  if (jointype == JOIN_UNIQUE_OUTER)
832  {
833  outer_path = (Path *) create_unique_path(root, outerrel,
834  outer_path, extra->sjinfo);
835  Assert(outer_path);
836  jointype = JOIN_INNER;
837  }
838  else if (jointype == JOIN_UNIQUE_INNER)
839  {
840  inner_path = (Path *) create_unique_path(root, innerrel,
841  inner_path, extra->sjinfo);
842  Assert(inner_path);
843  jointype = JOIN_INNER;
844  }
845 
846  /*
847  * If the joinrel is parallel-safe, we may be able to consider a partial
848  * merge join. However, we can't handle JOIN_UNIQUE_OUTER, because the
849  * outer path will be partial, and therefore we won't be able to properly
850  * guarantee uniqueness. Similarly, we can't handle JOIN_FULL and
851  * JOIN_RIGHT, because they can produce false null extended rows. Also,
852  * the resulting path must not be parameterized.
853  */
854  if (joinrel->consider_parallel &&
855  save_jointype != JOIN_UNIQUE_OUTER &&
856  save_jointype != JOIN_FULL &&
857  save_jointype != JOIN_RIGHT &&
858  outerrel->partial_pathlist != NIL &&
859  bms_is_empty(joinrel->lateral_relids))
860  {
861  cheapest_partial_outer = (Path *) linitial(outerrel->partial_pathlist);
862 
863  if (inner_path->parallel_safe)
864  cheapest_safe_inner = inner_path;
865  else if (save_jointype != JOIN_UNIQUE_INNER)
866  cheapest_safe_inner =
868  }
869 
870  /*
871  * Each possible ordering of the available mergejoin clauses will generate
872  * a differently-sorted result path at essentially the same cost. We have
873  * no basis for choosing one over another at this level of joining, but
874  * some sort orders may be more useful than others for higher-level
875  * mergejoins, so it's worth considering multiple orderings.
876  *
877  * Actually, it's not quite true that every mergeclause ordering will
878  * generate a different path order, because some of the clauses may be
879  * partially redundant (refer to the same EquivalenceClasses). Therefore,
880  * what we do is convert the mergeclause list to a list of canonical
881  * pathkeys, and then consider different orderings of the pathkeys.
882  *
883  * Generating a path for *every* permutation of the pathkeys doesn't seem
884  * like a winning strategy; the cost in planning time is too high. For
885  * now, we generate one path for each pathkey, listing that pathkey first
886  * and the rest in random order. This should allow at least a one-clause
887  * mergejoin without re-sorting against any other possible mergejoin
888  * partner path. But if we've not guessed the right ordering of secondary
889  * keys, we may end up evaluating clauses as qpquals when they could have
890  * been done as mergeclauses. (In practice, it's rare that there's more
891  * than two or three mergeclauses, so expending a huge amount of thought
892  * on that is probably not worth it.)
893  *
894  * The pathkey order returned by select_outer_pathkeys_for_merge() has
895  * some heuristics behind it (see that function), so be sure to try it
896  * exactly as-is as well as making variants.
897  */
898  all_pathkeys = select_outer_pathkeys_for_merge(root,
899  extra->mergeclause_list,
900  joinrel);
901 
902  foreach(l, all_pathkeys)
903  {
904  List *front_pathkey = (List *) lfirst(l);
905  List *cur_mergeclauses;
906  List *outerkeys;
907  List *innerkeys;
908  List *merge_pathkeys;
909 
910  /* Make a pathkey list with this guy first */
911  if (l != list_head(all_pathkeys))
912  outerkeys = lcons(front_pathkey,
913  list_delete_ptr(list_copy(all_pathkeys),
914  front_pathkey));
915  else
916  outerkeys = all_pathkeys; /* no work at first one... */
917 
918  /* Sort the mergeclauses into the corresponding ordering */
919  cur_mergeclauses = find_mergeclauses_for_pathkeys(root,
920  outerkeys,
921  true,
922  extra->mergeclause_list);
923 
924  /* Should have used them all... */
925  Assert(list_length(cur_mergeclauses) == list_length(extra->mergeclause_list));
926 
927  /* Build sort pathkeys for the inner side */
928  innerkeys = make_inner_pathkeys_for_merge(root,
929  cur_mergeclauses,
930  outerkeys);
931 
932  /* Build pathkeys representing output sort order */
933  merge_pathkeys = build_join_pathkeys(root, joinrel, jointype,
934  outerkeys);
935 
936  /*
937  * And now we can make the path.
938  *
939  * Note: it's possible that the cheapest paths will already be sorted
940  * properly. try_mergejoin_path will detect that case and suppress an
941  * explicit sort step, so we needn't do so here.
942  */
943  try_mergejoin_path(root,
944  joinrel,
945  outer_path,
946  inner_path,
947  merge_pathkeys,
948  cur_mergeclauses,
949  outerkeys,
950  innerkeys,
951  jointype,
952  extra,
953  false);
954 
955  /*
956  * If we have partial outer and parallel safe inner path then try
957  * partial mergejoin path.
958  */
959  if (cheapest_partial_outer && cheapest_safe_inner)
961  joinrel,
962  cheapest_partial_outer,
963  cheapest_safe_inner,
964  merge_pathkeys,
965  cur_mergeclauses,
966  outerkeys,
967  innerkeys,
968  jointype,
969  extra);
970  }
971 }
static void try_mergejoin_path(PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *pathkeys, List *mergeclauses, List *outersortkeys, List *innersortkeys, JoinType jointype, JoinPathExtraData *extra, bool is_partial)
Definition: joinpath.c:469
#define NIL
Definition: pg_list.h:69
List * list_copy(const List *oldlist)
Definition: list.c:1160
List * make_inner_pathkeys_for_merge(PlannerInfo *root, List *mergeclauses, List *outer_pathkeys)
Definition: pathkeys.c:1292
List * partial_pathlist
Definition: relation.h:587
UniquePath * create_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, SpecialJoinInfo *sjinfo)
Definition: pathnode.c:1427
List * list_delete_ptr(List *list, void *datum)
Definition: list.c:590
List * build_join_pathkeys(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, List *outer_pathkeys)
Definition: pathkeys.c:822
JoinType
Definition: nodes.h:673
Relids lateral_relids
Definition: relation.h:596
SpecialJoinInfo * sjinfo
Definition: relation.h:2239
#define linitial(l)
Definition: pg_list.h:111
struct Path * cheapest_total_path
Definition: relation.h:589
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
List * mergeclause_list
Definition: relation.h:2237
Path * get_cheapest_parallel_safe_total_inner(List *paths)
Definition: pathkeys.c:421
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
List * select_outer_pathkeys_for_merge(PlannerInfo *root, List *mergeclauses, RelOptInfo *joinrel)
Definition: pathkeys.c:1120
#define PATH_PARAM_BY_REL(path, rel)
Definition: joinpath.c:29
List * lcons(void *datum, List *list)
Definition: list.c:259
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
bool parallel_safe
Definition: relation.h:1014
static int list_length(const List *l)
Definition: pg_list.h:89
bool consider_parallel
Definition: relation.h:579
List * find_mergeclauses_for_pathkeys(PlannerInfo *root, List *pathkeys, bool outer_keys, List *restrictinfos)
Definition: pathkeys.c:1003
static void try_partial_mergejoin_path(PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *pathkeys, List *mergeclauses, List *outersortkeys, List *innersortkeys, JoinType jointype, JoinPathExtraData *extra)
Definition: joinpath.c:564
List * pathlist
Definition: relation.h:585
Definition: pg_list.h:45
static void try_hashjoin_path ( PlannerInfo root,
RelOptInfo joinrel,
Path outer_path,
Path inner_path,
List hashclauses,
JoinType  jointype,
JoinPathExtraData extra 
)
static

Definition at line 634 of file joinpath.c.

References add_path(), add_path_precheck(), bms_free(), bms_overlap(), calc_non_nestloop_required_outer(), create_hashjoin_path(), initial_cost_hashjoin(), NIL, JoinPathExtraData::param_source_rels, JoinPathExtraData::restrictlist, JoinCostWorkspace::startup_cost, and JoinCostWorkspace::total_cost.

Referenced by hash_inner_and_outer().

641 {
642  Relids required_outer;
643  JoinCostWorkspace workspace;
644 
645  /*
646  * Check to see if proposed path is still parameterized, and reject if the
647  * parameterization wouldn't be sensible.
648  */
649  required_outer = calc_non_nestloop_required_outer(outer_path,
650  inner_path);
651  if (required_outer &&
652  !bms_overlap(required_outer, extra->param_source_rels))
653  {
654  /* Waste no memory when we reject a path here */
655  bms_free(required_outer);
656  return;
657  }
658 
659  /*
660  * See comments in try_nestloop_path(). Also note that hashjoin paths
661  * never have any output pathkeys, per comments in create_hashjoin_path.
662  */
663  initial_cost_hashjoin(root, &workspace, jointype, hashclauses,
664  outer_path, inner_path, extra);
665 
666  if (add_path_precheck(joinrel,
667  workspace.startup_cost, workspace.total_cost,
668  NIL, required_outer))
669  {
670  add_path(joinrel, (Path *)
672  joinrel,
673  jointype,
674  &workspace,
675  extra,
676  outer_path,
677  inner_path,
678  extra->restrictlist,
679  required_outer,
680  hashclauses));
681  }
682  else
683  {
684  /* Waste no memory when we reject a path here */
685  bms_free(required_outer);
686  }
687 }
#define NIL
Definition: pg_list.h:69
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
bool add_path_precheck(RelOptInfo *parent_rel, Cost startup_cost, Cost total_cost, List *pathkeys, Relids required_outer)
Definition: pathnode.c:647
void initial_cost_hashjoin(PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, List *hashclauses, Path *outer_path, Path *inner_path, JoinPathExtraData *extra)
Definition: costsize.c:2920
Relids calc_non_nestloop_required_outer(Path *outer_path, Path *inner_path)
Definition: pathnode.c:2029
Relids param_source_rels
Definition: relation.h:2241
List * restrictlist
Definition: relation.h:2236
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
HashPath * create_hashjoin_path(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, JoinCostWorkspace *workspace, JoinPathExtraData *extra, Path *outer_path, Path *inner_path, List *restrict_clauses, Relids required_outer, List *hashclauses)
Definition: pathnode.c:2215
static void try_mergejoin_path ( PlannerInfo root,
RelOptInfo joinrel,
Path outer_path,
Path inner_path,
List pathkeys,
List mergeclauses,
List outersortkeys,
List innersortkeys,
JoinType  jointype,
JoinPathExtraData extra,
bool  is_partial 
)
static

Definition at line 469 of file joinpath.c.

References add_path(), add_path_precheck(), bms_free(), bms_overlap(), calc_non_nestloop_required_outer(), create_mergejoin_path(), initial_cost_mergejoin(), NIL, JoinPathExtraData::param_source_rels, Path::pathkeys, pathkeys_contained_in(), JoinPathExtraData::restrictlist, JoinCostWorkspace::startup_cost, JoinCostWorkspace::total_cost, and try_partial_mergejoin_path().

Referenced by generate_mergejoin_paths(), and sort_inner_and_outer().

480 {
481  Relids required_outer;
482  JoinCostWorkspace workspace;
483 
484  if (is_partial)
485  {
487  joinrel,
488  outer_path,
489  inner_path,
490  pathkeys,
491  mergeclauses,
492  outersortkeys,
493  innersortkeys,
494  jointype,
495  extra);
496  return;
497  }
498 
499  /*
500  * Check to see if proposed path is still parameterized, and reject if the
501  * parameterization wouldn't be sensible.
502  */
503  required_outer = calc_non_nestloop_required_outer(outer_path,
504  inner_path);
505  if (required_outer &&
506  !bms_overlap(required_outer, extra->param_source_rels))
507  {
508  /* Waste no memory when we reject a path here */
509  bms_free(required_outer);
510  return;
511  }
512 
513  /*
514  * If the given paths are already well enough ordered, we can skip doing
515  * an explicit sort.
516  */
517  if (outersortkeys &&
518  pathkeys_contained_in(outersortkeys, outer_path->pathkeys))
519  outersortkeys = NIL;
520  if (innersortkeys &&
521  pathkeys_contained_in(innersortkeys, inner_path->pathkeys))
522  innersortkeys = NIL;
523 
524  /*
525  * See comments in try_nestloop_path().
526  */
527  initial_cost_mergejoin(root, &workspace, jointype, mergeclauses,
528  outer_path, inner_path,
529  outersortkeys, innersortkeys,
530  extra);
531 
532  if (add_path_precheck(joinrel,
533  workspace.startup_cost, workspace.total_cost,
534  pathkeys, required_outer))
535  {
536  add_path(joinrel, (Path *)
538  joinrel,
539  jointype,
540  &workspace,
541  extra,
542  outer_path,
543  inner_path,
544  extra->restrictlist,
545  pathkeys,
546  required_outer,
547  mergeclauses,
548  outersortkeys,
549  innersortkeys));
550  }
551  else
552  {
553  /* Waste no memory when we reject a path here */
554  bms_free(required_outer);
555  }
556 }
#define NIL
Definition: pg_list.h:69
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
bool add_path_precheck(RelOptInfo *parent_rel, Cost startup_cost, Cost total_cost, List *pathkeys, Relids required_outer)
Definition: pathnode.c:647
Relids calc_non_nestloop_required_outer(Path *outer_path, Path *inner_path)
Definition: pathnode.c:2029
MergePath * create_mergejoin_path(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, JoinCostWorkspace *workspace, JoinPathExtraData *extra, Path *outer_path, Path *inner_path, List *restrict_clauses, List *pathkeys, Relids required_outer, List *mergeclauses, List *outersortkeys, List *innersortkeys)
Definition: pathnode.c:2150
void initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, List *mergeclauses, Path *outer_path, Path *inner_path, List *outersortkeys, List *innersortkeys, JoinPathExtraData *extra)
Definition: costsize.c:2369
Relids param_source_rels
Definition: relation.h:2241
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
List * restrictlist
Definition: relation.h:2236
List * pathkeys
Definition: relation.h:1022
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
static void try_partial_mergejoin_path(PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, List *pathkeys, List *mergeclauses, List *outersortkeys, List *innersortkeys, JoinType jointype, JoinPathExtraData *extra)
Definition: joinpath.c:564
static void try_nestloop_path ( PlannerInfo root,
RelOptInfo joinrel,
Path outer_path,
Path inner_path,
List pathkeys,
JoinType  jointype,
JoinPathExtraData extra 
)
static

Definition at line 338 of file joinpath.c.

References add_path(), add_path_precheck(), allow_star_schema_join(), bms_free(), bms_overlap(), calc_nestloop_required_outer(), create_nestloop_path(), have_dangerous_phv(), initial_cost_nestloop(), JoinPathExtraData::param_source_rels, Path::parent, PATH_REQ_OUTER, RelOptInfo::relids, JoinPathExtraData::restrictlist, JoinCostWorkspace::startup_cost, and JoinCostWorkspace::total_cost.

Referenced by match_unsorted_outer().

345 {
346  Relids required_outer;
347  JoinCostWorkspace workspace;
348  RelOptInfo *innerrel = inner_path->parent;
349  RelOptInfo *outerrel = outer_path->parent;
350  Relids innerrelids = innerrel->relids;
351  Relids outerrelids = outerrel->relids;
352  Relids inner_paramrels = PATH_REQ_OUTER(inner_path);
353  Relids outer_paramrels = PATH_REQ_OUTER(outer_path);
354 
355  /*
356  * Check to see if proposed path is still parameterized, and reject if the
357  * parameterization wouldn't be sensible --- unless allow_star_schema_join
358  * says to allow it anyway. Also, we must reject if have_dangerous_phv
359  * doesn't like the look of it, which could only happen if the nestloop is
360  * still parameterized.
361  */
362  required_outer = calc_nestloop_required_outer(outerrelids, outer_paramrels,
363  innerrelids, inner_paramrels);
364  if (required_outer &&
365  ((!bms_overlap(required_outer, extra->param_source_rels) &&
366  !allow_star_schema_join(root, outerrelids, inner_paramrels)) ||
367  have_dangerous_phv(root, outerrelids, inner_paramrels)))
368  {
369  /* Waste no memory when we reject a path here */
370  bms_free(required_outer);
371  return;
372  }
373 
374  /*
375  * Do a precheck to quickly eliminate obviously-inferior paths. We
376  * calculate a cheap lower bound on the path's cost and then use
377  * add_path_precheck() to see if the path is clearly going to be dominated
378  * by some existing path for the joinrel. If not, do the full pushup with
379  * creating a fully valid path structure and submitting it to add_path().
380  * The latter two steps are expensive enough to make this two-phase
381  * methodology worthwhile.
382  */
383  initial_cost_nestloop(root, &workspace, jointype,
384  outer_path, inner_path, extra);
385 
386  if (add_path_precheck(joinrel,
387  workspace.startup_cost, workspace.total_cost,
388  pathkeys, required_outer))
389  {
390  add_path(joinrel, (Path *)
392  joinrel,
393  jointype,
394  &workspace,
395  extra,
396  outer_path,
397  inner_path,
398  extra->restrictlist,
399  pathkeys,
400  required_outer));
401  }
402  else
403  {
404  /* Waste no memory when we reject a path here */
405  bms_free(required_outer);
406  }
407 }
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:412
bool add_path_precheck(RelOptInfo *parent_rel, Cost startup_cost, Cost total_cost, List *pathkeys, Relids required_outer)
Definition: pathnode.c:647
Relids param_source_rels
Definition: relation.h:2241
RelOptInfo * parent
Definition: relation.h:1008
NestPath * create_nestloop_path(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, JoinCostWorkspace *workspace, JoinPathExtraData *extra, Path *outer_path, Path *inner_path, List *restrict_clauses, List *pathkeys, Relids required_outer)
Definition: pathnode.c:2062
Relids relids
Definition: relation.h:571
List * restrictlist
Definition: relation.h:2236
void bms_free(Bitmapset *a)
Definition: bitmapset.c:201
#define PATH_REQ_OUTER(path)
Definition: relation.h:1027
Relids calc_nestloop_required_outer(Relids outerrelids, Relids outer_paramrels, Relids innerrelids, Relids inner_paramrels)
Definition: pathnode.c:1996
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:443
void initial_cost_nestloop(PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, Path *outer_path, Path *inner_path, JoinPathExtraData *extra)
Definition: costsize.c:2087
static bool allow_star_schema_join(PlannerInfo *root, Relids outerrelids, Relids inner_paramrels)
Definition: joinpath.c:320
bool have_dangerous_phv(PlannerInfo *root, Relids outer_relids, Relids inner_params)
Definition: joinrels.c:1152
static void try_partial_hashjoin_path ( PlannerInfo root,
RelOptInfo joinrel,
Path outer_path,
Path inner_path,
List hashclauses,
JoinType  jointype,
JoinPathExtraData extra 
)
static

Definition at line 695 of file joinpath.c.

References add_partial_path(), add_partial_path_precheck(), Assert, bms_is_empty(), create_hashjoin_path(), initial_cost_hashjoin(), RelOptInfo::lateral_relids, NIL, Path::param_info, ParamPathInfo::ppi_req_outer, JoinPathExtraData::restrictlist, and JoinCostWorkspace::total_cost.

Referenced by hash_inner_and_outer().

702 {
703  JoinCostWorkspace workspace;
704 
705  /*
706  * If the inner path is parameterized, the parameterization must be fully
707  * satisfied by the proposed outer path. Parameterized partial paths are
708  * not supported. The caller should already have verified that no
709  * extra_lateral_rels are required here.
710  */
712  if (inner_path->param_info != NULL)
713  {
714  Relids inner_paramrels = inner_path->param_info->ppi_req_outer;
715 
716  if (!bms_is_empty(inner_paramrels))
717  return;
718  }
719 
720  /*
721  * Before creating a path, get a quick lower bound on what it is likely to
722  * cost. Bail out right away if it looks terrible.
723  */
724  initial_cost_hashjoin(root, &workspace, jointype, hashclauses,
725  outer_path, inner_path, extra);
726  if (!add_partial_path_precheck(joinrel, workspace.total_cost, NIL))
727  return;
728 
729  /* Might be good enough to be worth trying, so let's try it. */
730  add_partial_path(joinrel, (Path *)
732  joinrel,
733  jointype,
734  &workspace,
735  extra,
736  outer_path,
737  inner_path,
738  extra->restrictlist,
739  NULL,
740  hashclauses));
741 }
#define NIL
Definition: pg_list.h:69
bool add_partial_path_precheck(RelOptInfo *parent_rel, Cost total_cost, List *pathkeys)
Definition: pathnode.c:876
ParamPathInfo * param_info
Definition: relation.h:1011
void initial_cost_hashjoin(PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, List *hashclauses, Path *outer_path, Path *inner_path, JoinPathExtraData *extra)
Definition: costsize.c:2920
Relids lateral_relids
Definition: relation.h:596
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
List * restrictlist
Definition: relation.h:2236
#define Assert(condition)
Definition: c.h:664
HashPath * create_hashjoin_path(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, JoinCostWorkspace *workspace, JoinPathExtraData *extra, Path *outer_path, Path *inner_path, List *restrict_clauses, Relids required_outer, List *hashclauses)
Definition: pathnode.c:2215
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:752
Relids ppi_req_outer
Definition: relation.h:967
static void try_partial_mergejoin_path ( PlannerInfo root,
RelOptInfo joinrel,
Path outer_path,
Path inner_path,
List pathkeys,
List mergeclauses,
List outersortkeys,
List innersortkeys,
JoinType  jointype,
JoinPathExtraData extra 
)
static

Definition at line 564 of file joinpath.c.

References add_partial_path(), add_partial_path_precheck(), Assert, bms_is_empty(), create_mergejoin_path(), initial_cost_mergejoin(), RelOptInfo::lateral_relids, NIL, Path::param_info, Path::pathkeys, pathkeys_contained_in(), ParamPathInfo::ppi_req_outer, JoinPathExtraData::restrictlist, and JoinCostWorkspace::total_cost.

Referenced by sort_inner_and_outer(), and try_mergejoin_path().

574 {
575  JoinCostWorkspace workspace;
576 
577  /*
578  * See comments in try_partial_hashjoin_path().
579  */
581  if (inner_path->param_info != NULL)
582  {
583  Relids inner_paramrels = inner_path->param_info->ppi_req_outer;
584 
585  if (!bms_is_empty(inner_paramrels))
586  return;
587  }
588 
589  /*
590  * If the given paths are already well enough ordered, we can skip doing
591  * an explicit sort.
592  */
593  if (outersortkeys &&
594  pathkeys_contained_in(outersortkeys, outer_path->pathkeys))
595  outersortkeys = NIL;
596  if (innersortkeys &&
597  pathkeys_contained_in(innersortkeys, inner_path->pathkeys))
598  innersortkeys = NIL;
599 
600  /*
601  * See comments in try_partial_nestloop_path().
602  */
603  initial_cost_mergejoin(root, &workspace, jointype, mergeclauses,
604  outer_path, inner_path,
605  outersortkeys, innersortkeys,
606  extra);
607 
608  if (!add_partial_path_precheck(joinrel, workspace.total_cost, pathkeys))
609  return;
610 
611  /* Might be good enough to be worth trying, so let's try it. */
612  add_partial_path(joinrel, (Path *)
614  joinrel,
615  jointype,
616  &workspace,
617  extra,
618  outer_path,
619  inner_path,
620  extra->restrictlist,
621  pathkeys,
622  NULL,
623  mergeclauses,
624  outersortkeys,
625  innersortkeys));
626 }
#define NIL
Definition: pg_list.h:69
bool add_partial_path_precheck(RelOptInfo *parent_rel, Cost total_cost, List *pathkeys)
Definition: pathnode.c:876
ParamPathInfo * param_info
Definition: relation.h:1011
MergePath * create_mergejoin_path(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, JoinCostWorkspace *workspace, JoinPathExtraData *extra, Path *outer_path, Path *inner_path, List *restrict_clauses, List *pathkeys, Relids required_outer, List *mergeclauses, List *outersortkeys, List *innersortkeys)
Definition: pathnode.c:2150
void initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, List *mergeclauses, Path *outer_path, Path *inner_path, List *outersortkeys, List *innersortkeys, JoinPathExtraData *extra)
Definition: costsize.c:2369
Relids lateral_relids
Definition: relation.h:596
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
bool pathkeys_contained_in(List *keys1, List *keys2)
Definition: pathkeys.c:317
List * restrictlist
Definition: relation.h:2236
List * pathkeys
Definition: relation.h:1022
#define Assert(condition)
Definition: c.h:664
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:752
Relids ppi_req_outer
Definition: relation.h:967
static void try_partial_nestloop_path ( PlannerInfo root,
RelOptInfo joinrel,
Path outer_path,
Path inner_path,
List pathkeys,
JoinType  jointype,
JoinPathExtraData extra 
)
static

Definition at line 415 of file joinpath.c.

References add_partial_path(), add_partial_path_precheck(), Assert, bms_is_empty(), bms_is_subset(), create_nestloop_path(), initial_cost_nestloop(), RelOptInfo::lateral_relids, Path::param_info, Path::parent, ParamPathInfo::ppi_req_outer, RelOptInfo::relids, JoinPathExtraData::restrictlist, and JoinCostWorkspace::total_cost.

Referenced by consider_parallel_nestloop().

422 {
423  JoinCostWorkspace workspace;
424 
425  /*
426  * If the inner path is parameterized, the parameterization must be fully
427  * satisfied by the proposed outer path. Parameterized partial paths are
428  * not supported. The caller should already have verified that no
429  * extra_lateral_rels are required here.
430  */
432  if (inner_path->param_info != NULL)
433  {
434  Relids inner_paramrels = inner_path->param_info->ppi_req_outer;
435 
436  if (!bms_is_subset(inner_paramrels, outer_path->parent->relids))
437  return;
438  }
439 
440  /*
441  * Before creating a path, get a quick lower bound on what it is likely to
442  * cost. Bail out right away if it looks terrible.
443  */
444  initial_cost_nestloop(root, &workspace, jointype,
445  outer_path, inner_path, extra);
446  if (!add_partial_path_precheck(joinrel, workspace.total_cost, pathkeys))
447  return;
448 
449  /* Might be good enough to be worth trying, so let's try it. */
450  add_partial_path(joinrel, (Path *)
452  joinrel,
453  jointype,
454  &workspace,
455  extra,
456  outer_path,
457  inner_path,
458  extra->restrictlist,
459  pathkeys,
460  NULL));
461 }
bool add_partial_path_precheck(RelOptInfo *parent_rel, Cost total_cost, List *pathkeys)
Definition: pathnode.c:876
ParamPathInfo * param_info
Definition: relation.h:1011
Relids lateral_relids
Definition: relation.h:596
RelOptInfo * parent
Definition: relation.h:1008
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:308
NestPath * create_nestloop_path(PlannerInfo *root, RelOptInfo *joinrel, JoinType jointype, JoinCostWorkspace *workspace, JoinPathExtraData *extra, Path *outer_path, Path *inner_path, List *restrict_clauses, List *pathkeys, Relids required_outer)
Definition: pathnode.c:2062
Relids relids
Definition: relation.h:571
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
List * restrictlist
Definition: relation.h:2236
#define Assert(condition)
Definition: c.h:664
void add_partial_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:752
void initial_cost_nestloop(PlannerInfo *root, JoinCostWorkspace *workspace, JoinType jointype, Path *outer_path, Path *inner_path, JoinPathExtraData *extra)
Definition: costsize.c:2087
Relids ppi_req_outer
Definition: relation.h:967

Variable Documentation

set_join_pathlist_hook_type set_join_pathlist_hook = NULL

Definition at line 27 of file joinpath.c.

Referenced by add_paths_to_joinrel().