PostgreSQL Source Code  git master
relnode.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * relnode.c
4  * Relation-node lookup/construction routines
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/optimizer/util/relnode.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include <limits.h>
18 
19 #include "miscadmin.h"
20 #include "nodes/nodeFuncs.h"
21 #include "optimizer/appendinfo.h"
22 #include "optimizer/clauses.h"
23 #include "optimizer/cost.h"
24 #include "optimizer/inherit.h"
25 #include "optimizer/pathnode.h"
26 #include "optimizer/paths.h"
27 #include "optimizer/placeholder.h"
28 #include "optimizer/plancat.h"
29 #include "optimizer/restrictinfo.h"
30 #include "optimizer/tlist.h"
31 #include "utils/hsearch.h"
32 #include "utils/lsyscache.h"
33 
34 
35 typedef struct JoinHashEntry
36 {
37  Relids join_relids; /* hash key --- MUST BE FIRST */
40 
41 static void build_joinrel_tlist(PlannerInfo *root, RelOptInfo *joinrel,
42  RelOptInfo *input_rel);
44  RelOptInfo *joinrel,
45  RelOptInfo *outer_rel,
46  RelOptInfo *inner_rel);
47 static void build_joinrel_joinlist(RelOptInfo *joinrel,
48  RelOptInfo *outer_rel,
49  RelOptInfo *inner_rel);
51  List *joininfo_list,
52  List *new_restrictlist);
54  List *joininfo_list,
55  List *new_joininfo);
56 static void set_foreign_rel_properties(RelOptInfo *joinrel,
57  RelOptInfo *outer_rel, RelOptInfo *inner_rel);
58 static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel);
59 static void build_joinrel_partition_info(RelOptInfo *joinrel,
60  RelOptInfo *outer_rel, RelOptInfo *inner_rel,
61  List *restrictlist, JoinType jointype);
62 static bool have_partkey_equi_join(RelOptInfo *joinrel,
63  RelOptInfo *rel1, RelOptInfo *rel2,
64  JoinType jointype, List *restrictlist);
65 static int match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel,
66  bool strict_op);
67 static void set_joinrel_partition_key_exprs(RelOptInfo *joinrel,
68  RelOptInfo *outer_rel, RelOptInfo *inner_rel,
69  JoinType jointype);
70 static void build_child_join_reltarget(PlannerInfo *root,
71  RelOptInfo *parentrel,
72  RelOptInfo *childrel,
73  int nappinfos,
74  AppendRelInfo **appinfos);
75 
76 
77 /*
78  * setup_simple_rel_arrays
79  * Prepare the arrays we use for quickly accessing base relations
80  * and AppendRelInfos.
81  */
82 void
84 {
85  int size;
86  Index rti;
87  ListCell *lc;
88 
89  /* Arrays are accessed using RT indexes (1..N) */
90  size = list_length(root->parse->rtable) + 1;
91  root->simple_rel_array_size = size;
92 
93  /*
94  * simple_rel_array is initialized to all NULLs, since no RelOptInfos
95  * exist yet. It'll be filled by later calls to build_simple_rel().
96  */
97  root->simple_rel_array = (RelOptInfo **)
98  palloc0(size * sizeof(RelOptInfo *));
99 
100  /* simple_rte_array is an array equivalent of the rtable list */
101  root->simple_rte_array = (RangeTblEntry **)
102  palloc0(size * sizeof(RangeTblEntry *));
103  rti = 1;
104  foreach(lc, root->parse->rtable)
105  {
106  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
107 
108  root->simple_rte_array[rti++] = rte;
109  }
110 
111  /* append_rel_array is not needed if there are no AppendRelInfos */
112  if (root->append_rel_list == NIL)
113  {
114  root->append_rel_array = NULL;
115  return;
116  }
117 
118  root->append_rel_array = (AppendRelInfo **)
119  palloc0(size * sizeof(AppendRelInfo *));
120 
121  /*
122  * append_rel_array is filled with any already-existing AppendRelInfos,
123  * which currently could only come from UNION ALL flattening. We might
124  * add more later during inheritance expansion, but it's the
125  * responsibility of the expansion code to update the array properly.
126  */
127  foreach(lc, root->append_rel_list)
128  {
129  AppendRelInfo *appinfo = lfirst_node(AppendRelInfo, lc);
130  int child_relid = appinfo->child_relid;
131 
132  /* Sanity check */
133  Assert(child_relid < size);
134 
135  if (root->append_rel_array[child_relid])
136  elog(ERROR, "child relation already exists");
137 
138  root->append_rel_array[child_relid] = appinfo;
139  }
140 }
141 
142 /*
143  * expand_planner_arrays
144  * Expand the PlannerInfo's per-RTE arrays by add_size members
145  * and initialize the newly added entries to NULLs
146  *
147  * Note: this causes the append_rel_array to become allocated even if
148  * it was not before. This is okay for current uses, because we only call
149  * this when adding child relations, which always have AppendRelInfos.
150  */
151 void
153 {
154  int new_size;
155 
156  Assert(add_size > 0);
157 
158  new_size = root->simple_rel_array_size + add_size;
159 
160  root->simple_rel_array = (RelOptInfo **)
162  sizeof(RelOptInfo *) * new_size);
164  0, sizeof(RelOptInfo *) * add_size);
165 
166  root->simple_rte_array = (RangeTblEntry **)
168  sizeof(RangeTblEntry *) * new_size);
170  0, sizeof(RangeTblEntry *) * add_size);
171 
172  if (root->append_rel_array)
173  {
174  root->append_rel_array = (AppendRelInfo **)
176  sizeof(AppendRelInfo *) * new_size);
178  0, sizeof(AppendRelInfo *) * add_size);
179  }
180  else
181  {
182  root->append_rel_array = (AppendRelInfo **)
183  palloc0(sizeof(AppendRelInfo *) * new_size);
184  }
185 
186  root->simple_rel_array_size = new_size;
187 }
188 
189 /*
190  * build_simple_rel
191  * Construct a new RelOptInfo for a base relation or 'other' relation.
192  */
193 RelOptInfo *
194 build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
195 {
196  RelOptInfo *rel;
197  RangeTblEntry *rte;
198 
199  /* Rel should not exist already */
200  Assert(relid > 0 && relid < root->simple_rel_array_size);
201  if (root->simple_rel_array[relid] != NULL)
202  elog(ERROR, "rel %d already exists", relid);
203 
204  /* Fetch RTE for relation */
205  rte = root->simple_rte_array[relid];
206  Assert(rte != NULL);
207 
208  rel = makeNode(RelOptInfo);
210  rel->relids = bms_make_singleton(relid);
211  rel->rows = 0;
212  /* cheap startup cost is interesting iff not all tuples to be retrieved */
213  rel->consider_startup = (root->tuple_fraction > 0);
214  rel->consider_param_startup = false; /* might get changed later */
215  rel->consider_parallel = false; /* might get changed later */
217  rel->pathlist = NIL;
218  rel->ppilist = NIL;
219  rel->partial_pathlist = NIL;
220  rel->cheapest_startup_path = NULL;
221  rel->cheapest_total_path = NULL;
222  rel->cheapest_unique_path = NULL;
224  rel->relid = relid;
225  rel->rtekind = rte->rtekind;
226  /* min_attr, max_attr, attr_needed, attr_widths are set below */
227  rel->lateral_vars = NIL;
228  rel->indexlist = NIL;
229  rel->statlist = NIL;
230  rel->pages = 0;
231  rel->tuples = 0;
232  rel->allvisfrac = 0;
233  rel->eclass_indexes = NULL;
234  rel->subroot = NULL;
235  rel->subplan_params = NIL;
236  rel->rel_parallel_workers = -1; /* set up in get_relation_info */
237  rel->amflags = 0;
238  rel->serverid = InvalidOid;
239  rel->userid = rte->checkAsUser;
240  rel->useridiscurrent = false;
241  rel->fdwroutine = NULL;
242  rel->fdw_private = NULL;
243  rel->unique_for_rels = NIL;
244  rel->non_unique_for_rels = NIL;
245  rel->baserestrictinfo = NIL;
246  rel->baserestrictcost.startup = 0;
247  rel->baserestrictcost.per_tuple = 0;
248  rel->baserestrict_min_security = UINT_MAX;
249  rel->joininfo = NIL;
250  rel->has_eclass_joins = false;
251  rel->consider_partitionwise_join = false; /* might get changed later */
252  rel->part_scheme = NULL;
253  rel->nparts = -1;
254  rel->boundinfo = NULL;
255  rel->partbounds_merged = false;
256  rel->partition_qual = NIL;
257  rel->part_rels = NULL;
258  rel->all_partrels = NULL;
259  rel->partexprs = NULL;
260  rel->nullable_partexprs = NULL;
261 
262  /*
263  * Pass assorted information down the inheritance hierarchy.
264  */
265  if (parent)
266  {
267  /*
268  * Each direct or indirect child wants to know the relids of its
269  * topmost parent.
270  */
271  if (parent->top_parent_relids)
272  rel->top_parent_relids = parent->top_parent_relids;
273  else
274  rel->top_parent_relids = bms_copy(parent->relids);
275 
276  /*
277  * Also propagate lateral-reference information from appendrel parent
278  * rels to their child rels. We intentionally give each child rel the
279  * same minimum parameterization, even though it's quite possible that
280  * some don't reference all the lateral rels. This is because any
281  * append path for the parent will have to have the same
282  * parameterization for every child anyway, and there's no value in
283  * forcing extra reparameterize_path() calls. Similarly, a lateral
284  * reference to the parent prevents use of otherwise-movable join rels
285  * for each child.
286  *
287  * It's possible for child rels to have their own children, in which
288  * case the topmost parent's lateral info propagates all the way down.
289  */
291  rel->lateral_relids = parent->lateral_relids;
293  }
294  else
295  {
296  rel->top_parent_relids = NULL;
297  rel->direct_lateral_relids = NULL;
298  rel->lateral_relids = NULL;
299  rel->lateral_referencers = NULL;
300  }
301 
302  /* Check type of rtable entry */
303  switch (rte->rtekind)
304  {
305  case RTE_RELATION:
306  /* Table --- retrieve statistics from the system catalogs */
307  get_relation_info(root, rte->relid, rte->inh, rel);
308  break;
309  case RTE_SUBQUERY:
310  case RTE_FUNCTION:
311  case RTE_TABLEFUNC:
312  case RTE_VALUES:
313  case RTE_CTE:
314  case RTE_NAMEDTUPLESTORE:
315 
316  /*
317  * Subquery, function, tablefunc, values list, CTE, or ENR --- set
318  * up attr range and arrays
319  *
320  * Note: 0 is included in range to support whole-row Vars
321  */
322  rel->min_attr = 0;
323  rel->max_attr = list_length(rte->eref->colnames);
324  rel->attr_needed = (Relids *)
325  palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(Relids));
326  rel->attr_widths = (int32 *)
327  palloc0((rel->max_attr - rel->min_attr + 1) * sizeof(int32));
328  break;
329  case RTE_RESULT:
330  /* RTE_RESULT has no columns, nor could it have whole-row Var */
331  rel->min_attr = 0;
332  rel->max_attr = -1;
333  rel->attr_needed = NULL;
334  rel->attr_widths = NULL;
335  break;
336  default:
337  elog(ERROR, "unrecognized RTE kind: %d",
338  (int) rte->rtekind);
339  break;
340  }
341 
342  /*
343  * Copy the parent's quals to the child, with appropriate substitution of
344  * variables. If any constant false or NULL clauses turn up, we can mark
345  * the child as dummy right away. (We must do this immediately so that
346  * pruning works correctly when recursing in expand_partitioned_rtentry.)
347  */
348  if (parent)
349  {
350  AppendRelInfo *appinfo = root->append_rel_array[relid];
351 
352  Assert(appinfo != NULL);
353  if (!apply_child_basequals(root, parent, rel, rte, appinfo))
354  {
355  /*
356  * Some restriction clause reduced to constant FALSE or NULL after
357  * substitution, so this child need not be scanned.
358  */
359  mark_dummy_rel(rel);
360  }
361  }
362 
363  /* Save the finished struct in the query's simple_rel_array */
364  root->simple_rel_array[relid] = rel;
365 
366  return rel;
367 }
368 
369 /*
370  * find_base_rel
371  * Find a base or other relation entry, which must already exist.
372  */
373 RelOptInfo *
374 find_base_rel(PlannerInfo *root, int relid)
375 {
376  RelOptInfo *rel;
377 
378  Assert(relid > 0);
379 
380  if (relid < root->simple_rel_array_size)
381  {
382  rel = root->simple_rel_array[relid];
383  if (rel)
384  return rel;
385  }
386 
387  elog(ERROR, "no relation entry for relid %d", relid);
388 
389  return NULL; /* keep compiler quiet */
390 }
391 
392 /*
393  * build_join_rel_hash
394  * Construct the auxiliary hash table for join relations.
395  */
396 static void
398 {
399  HTAB *hashtab;
400  HASHCTL hash_ctl;
401  ListCell *l;
402 
403  /* Create the hash table */
404  hash_ctl.keysize = sizeof(Relids);
405  hash_ctl.entrysize = sizeof(JoinHashEntry);
406  hash_ctl.hash = bitmap_hash;
407  hash_ctl.match = bitmap_match;
408  hash_ctl.hcxt = CurrentMemoryContext;
409  hashtab = hash_create("JoinRelHashTable",
410  256L,
411  &hash_ctl,
413 
414  /* Insert all the already-existing joinrels */
415  foreach(l, root->join_rel_list)
416  {
417  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
418  JoinHashEntry *hentry;
419  bool found;
420 
421  hentry = (JoinHashEntry *) hash_search(hashtab,
422  &(rel->relids),
423  HASH_ENTER,
424  &found);
425  Assert(!found);
426  hentry->join_rel = rel;
427  }
428 
429  root->join_rel_hash = hashtab;
430 }
431 
432 /*
433  * find_join_rel
434  * Returns relation entry corresponding to 'relids' (a set of RT indexes),
435  * or NULL if none exists. This is for join relations.
436  */
437 RelOptInfo *
439 {
440  /*
441  * Switch to using hash lookup when list grows "too long". The threshold
442  * is arbitrary and is known only here.
443  */
444  if (!root->join_rel_hash && list_length(root->join_rel_list) > 32)
445  build_join_rel_hash(root);
446 
447  /*
448  * Use either hashtable lookup or linear search, as appropriate.
449  *
450  * Note: the seemingly redundant hashkey variable is used to avoid taking
451  * the address of relids; unless the compiler is exceedingly smart, doing
452  * so would force relids out of a register and thus probably slow down the
453  * list-search case.
454  */
455  if (root->join_rel_hash)
456  {
457  Relids hashkey = relids;
458  JoinHashEntry *hentry;
459 
460  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
461  &hashkey,
462  HASH_FIND,
463  NULL);
464  if (hentry)
465  return hentry->join_rel;
466  }
467  else
468  {
469  ListCell *l;
470 
471  foreach(l, root->join_rel_list)
472  {
473  RelOptInfo *rel = (RelOptInfo *) lfirst(l);
474 
475  if (bms_equal(rel->relids, relids))
476  return rel;
477  }
478  }
479 
480  return NULL;
481 }
482 
483 /*
484  * set_foreign_rel_properties
485  * Set up foreign-join fields if outer and inner relation are foreign
486  * tables (or joins) belonging to the same server and assigned to the same
487  * user to check access permissions as.
488  *
489  * In addition to an exact match of userid, we allow the case where one side
490  * has zero userid (implying current user) and the other side has explicit
491  * userid that happens to equal the current user; but in that case, pushdown of
492  * the join is only valid for the current user. The useridiscurrent field
493  * records whether we had to make such an assumption for this join or any
494  * sub-join.
495  *
496  * Otherwise these fields are left invalid, so GetForeignJoinPaths will not be
497  * called for the join relation.
498  *
499  */
500 static void
502  RelOptInfo *inner_rel)
503 {
504  if (OidIsValid(outer_rel->serverid) &&
505  inner_rel->serverid == outer_rel->serverid)
506  {
507  if (inner_rel->userid == outer_rel->userid)
508  {
509  joinrel->serverid = outer_rel->serverid;
510  joinrel->userid = outer_rel->userid;
511  joinrel->useridiscurrent = outer_rel->useridiscurrent || inner_rel->useridiscurrent;
512  joinrel->fdwroutine = outer_rel->fdwroutine;
513  }
514  else if (!OidIsValid(inner_rel->userid) &&
515  outer_rel->userid == GetUserId())
516  {
517  joinrel->serverid = outer_rel->serverid;
518  joinrel->userid = outer_rel->userid;
519  joinrel->useridiscurrent = true;
520  joinrel->fdwroutine = outer_rel->fdwroutine;
521  }
522  else if (!OidIsValid(outer_rel->userid) &&
523  inner_rel->userid == GetUserId())
524  {
525  joinrel->serverid = outer_rel->serverid;
526  joinrel->userid = inner_rel->userid;
527  joinrel->useridiscurrent = true;
528  joinrel->fdwroutine = outer_rel->fdwroutine;
529  }
530  }
531 }
532 
533 /*
534  * add_join_rel
535  * Add given join relation to the list of join relations in the given
536  * PlannerInfo. Also add it to the auxiliary hashtable if there is one.
537  */
538 static void
540 {
541  /* GEQO requires us to append the new joinrel to the end of the list! */
542  root->join_rel_list = lappend(root->join_rel_list, joinrel);
543 
544  /* store it into the auxiliary hashtable if there is one. */
545  if (root->join_rel_hash)
546  {
547  JoinHashEntry *hentry;
548  bool found;
549 
550  hentry = (JoinHashEntry *) hash_search(root->join_rel_hash,
551  &(joinrel->relids),
552  HASH_ENTER,
553  &found);
554  Assert(!found);
555  hentry->join_rel = joinrel;
556  }
557 }
558 
559 /*
560  * build_join_rel
561  * Returns relation entry corresponding to the union of two given rels,
562  * creating a new relation entry if none already exists.
563  *
564  * 'joinrelids' is the Relids set that uniquely identifies the join
565  * 'outer_rel' and 'inner_rel' are relation nodes for the relations to be
566  * joined
567  * 'sjinfo': join context info
568  * 'restrictlist_ptr': result variable. If not NULL, *restrictlist_ptr
569  * receives the list of RestrictInfo nodes that apply to this
570  * particular pair of joinable relations.
571  *
572  * restrictlist_ptr makes the routine's API a little grotty, but it saves
573  * duplicated calculation of the restrictlist...
574  */
575 RelOptInfo *
577  Relids joinrelids,
578  RelOptInfo *outer_rel,
579  RelOptInfo *inner_rel,
580  SpecialJoinInfo *sjinfo,
581  List **restrictlist_ptr)
582 {
583  RelOptInfo *joinrel;
584  List *restrictlist;
585 
586  /* This function should be used only for join between parents. */
587  Assert(!IS_OTHER_REL(outer_rel) && !IS_OTHER_REL(inner_rel));
588 
589  /*
590  * See if we already have a joinrel for this set of base rels.
591  */
592  joinrel = find_join_rel(root, joinrelids);
593 
594  if (joinrel)
595  {
596  /*
597  * Yes, so we only need to figure the restrictlist for this particular
598  * pair of component relations.
599  */
600  if (restrictlist_ptr)
601  *restrictlist_ptr = build_joinrel_restrictlist(root,
602  joinrel,
603  outer_rel,
604  inner_rel);
605  return joinrel;
606  }
607 
608  /*
609  * Nope, so make one.
610  */
611  joinrel = makeNode(RelOptInfo);
612  joinrel->reloptkind = RELOPT_JOINREL;
613  joinrel->relids = bms_copy(joinrelids);
614  joinrel->rows = 0;
615  /* cheap startup cost is interesting iff not all tuples to be retrieved */
616  joinrel->consider_startup = (root->tuple_fraction > 0);
617  joinrel->consider_param_startup = false;
618  joinrel->consider_parallel = false;
619  joinrel->reltarget = create_empty_pathtarget();
620  joinrel->pathlist = NIL;
621  joinrel->ppilist = NIL;
622  joinrel->partial_pathlist = NIL;
623  joinrel->cheapest_startup_path = NULL;
624  joinrel->cheapest_total_path = NULL;
625  joinrel->cheapest_unique_path = NULL;
627  /* init direct_lateral_relids from children; we'll finish it up below */
628  joinrel->direct_lateral_relids =
629  bms_union(outer_rel->direct_lateral_relids,
630  inner_rel->direct_lateral_relids);
631  joinrel->lateral_relids = min_join_parameterization(root, joinrel->relids,
632  outer_rel, inner_rel);
633  joinrel->relid = 0; /* indicates not a baserel */
634  joinrel->rtekind = RTE_JOIN;
635  joinrel->min_attr = 0;
636  joinrel->max_attr = 0;
637  joinrel->attr_needed = NULL;
638  joinrel->attr_widths = NULL;
639  joinrel->lateral_vars = NIL;
640  joinrel->lateral_referencers = NULL;
641  joinrel->indexlist = NIL;
642  joinrel->statlist = NIL;
643  joinrel->pages = 0;
644  joinrel->tuples = 0;
645  joinrel->allvisfrac = 0;
646  joinrel->eclass_indexes = NULL;
647  joinrel->subroot = NULL;
648  joinrel->subplan_params = NIL;
649  joinrel->rel_parallel_workers = -1;
650  joinrel->amflags = 0;
651  joinrel->serverid = InvalidOid;
652  joinrel->userid = InvalidOid;
653  joinrel->useridiscurrent = false;
654  joinrel->fdwroutine = NULL;
655  joinrel->fdw_private = NULL;
656  joinrel->unique_for_rels = NIL;
657  joinrel->non_unique_for_rels = NIL;
658  joinrel->baserestrictinfo = NIL;
659  joinrel->baserestrictcost.startup = 0;
660  joinrel->baserestrictcost.per_tuple = 0;
661  joinrel->baserestrict_min_security = UINT_MAX;
662  joinrel->joininfo = NIL;
663  joinrel->has_eclass_joins = false;
664  joinrel->consider_partitionwise_join = false; /* might get changed later */
665  joinrel->top_parent_relids = NULL;
666  joinrel->part_scheme = NULL;
667  joinrel->nparts = -1;
668  joinrel->boundinfo = NULL;
669  joinrel->partbounds_merged = false;
670  joinrel->partition_qual = NIL;
671  joinrel->part_rels = NULL;
672  joinrel->all_partrels = NULL;
673  joinrel->partexprs = NULL;
674  joinrel->nullable_partexprs = NULL;
675 
676  /* Compute information relevant to the foreign relations. */
677  set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
678 
679  /*
680  * Create a new tlist containing just the vars that need to be output from
681  * this join (ie, are needed for higher joinclauses or final output).
682  *
683  * NOTE: the tlist order for a join rel will depend on which pair of outer
684  * and inner rels we first try to build it from. But the contents should
685  * be the same regardless.
686  */
687  build_joinrel_tlist(root, joinrel, outer_rel);
688  build_joinrel_tlist(root, joinrel, inner_rel);
689  add_placeholders_to_joinrel(root, joinrel, outer_rel, inner_rel);
690 
691  /*
692  * add_placeholders_to_joinrel also took care of adding the ph_lateral
693  * sets of any PlaceHolderVars computed here to direct_lateral_relids, so
694  * now we can finish computing that. This is much like the computation of
695  * the transitively-closed lateral_relids in min_join_parameterization,
696  * except that here we *do* have to consider the added PHVs.
697  */
698  joinrel->direct_lateral_relids =
699  bms_del_members(joinrel->direct_lateral_relids, joinrel->relids);
700  if (bms_is_empty(joinrel->direct_lateral_relids))
701  joinrel->direct_lateral_relids = NULL;
702 
703  /*
704  * Construct restrict and join clause lists for the new joinrel. (The
705  * caller might or might not need the restrictlist, but I need it anyway
706  * for set_joinrel_size_estimates().)
707  */
708  restrictlist = build_joinrel_restrictlist(root, joinrel,
709  outer_rel, inner_rel);
710  if (restrictlist_ptr)
711  *restrictlist_ptr = restrictlist;
712  build_joinrel_joinlist(joinrel, outer_rel, inner_rel);
713 
714  /*
715  * This is also the right place to check whether the joinrel has any
716  * pending EquivalenceClass joins.
717  */
718  joinrel->has_eclass_joins = has_relevant_eclass_joinclause(root, joinrel);
719 
720  /* Store the partition information. */
721  build_joinrel_partition_info(joinrel, outer_rel, inner_rel, restrictlist,
722  sjinfo->jointype);
723 
724  /*
725  * Set estimates of the joinrel's size.
726  */
727  set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
728  sjinfo, restrictlist);
729 
730  /*
731  * Set the consider_parallel flag if this joinrel could potentially be
732  * scanned within a parallel worker. If this flag is false for either
733  * inner_rel or outer_rel, then it must be false for the joinrel also.
734  * Even if both are true, there might be parallel-restricted expressions
735  * in the targetlist or quals.
736  *
737  * Note that if there are more than two rels in this relation, they could
738  * be divided between inner_rel and outer_rel in any arbitrary way. We
739  * assume this doesn't matter, because we should hit all the same baserels
740  * and joinclauses while building up to this joinrel no matter which we
741  * take; therefore, we should make the same decision here however we get
742  * here.
743  */
744  if (inner_rel->consider_parallel && outer_rel->consider_parallel &&
745  is_parallel_safe(root, (Node *) restrictlist) &&
746  is_parallel_safe(root, (Node *) joinrel->reltarget->exprs))
747  joinrel->consider_parallel = true;
748 
749  /* Add the joinrel to the PlannerInfo. */
750  add_join_rel(root, joinrel);
751 
752  /*
753  * Also, if dynamic-programming join search is active, add the new joinrel
754  * to the appropriate sublist. Note: you might think the Assert on number
755  * of members should be for equality, but some of the level 1 rels might
756  * have been joinrels already, so we can only assert <=.
757  */
758  if (root->join_rel_level)
759  {
760  Assert(root->join_cur_level > 0);
761  Assert(root->join_cur_level <= bms_num_members(joinrel->relids));
762  root->join_rel_level[root->join_cur_level] =
763  lappend(root->join_rel_level[root->join_cur_level], joinrel);
764  }
765 
766  return joinrel;
767 }
768 
769 /*
770  * build_child_join_rel
771  * Builds RelOptInfo representing join between given two child relations.
772  *
773  * 'outer_rel' and 'inner_rel' are the RelOptInfos of child relations being
774  * joined
775  * 'parent_joinrel' is the RelOptInfo representing the join between parent
776  * relations. Some of the members of new RelOptInfo are produced by
777  * translating corresponding members of this RelOptInfo
778  * 'sjinfo': child-join context info
779  * 'restrictlist': list of RestrictInfo nodes that apply to this particular
780  * pair of joinable relations
781  * 'jointype' is the join type (inner, left, full, etc)
782  */
783 RelOptInfo *
785  RelOptInfo *inner_rel, RelOptInfo *parent_joinrel,
786  List *restrictlist, SpecialJoinInfo *sjinfo,
787  JoinType jointype)
788 {
789  RelOptInfo *joinrel = makeNode(RelOptInfo);
790  AppendRelInfo **appinfos;
791  int nappinfos;
792 
793  /* Only joins between "other" relations land here. */
794  Assert(IS_OTHER_REL(outer_rel) && IS_OTHER_REL(inner_rel));
795 
796  /* The parent joinrel should have consider_partitionwise_join set. */
797  Assert(parent_joinrel->consider_partitionwise_join);
798 
799  joinrel->reloptkind = RELOPT_OTHER_JOINREL;
800  joinrel->relids = bms_union(outer_rel->relids, inner_rel->relids);
801  joinrel->rows = 0;
802  /* cheap startup cost is interesting iff not all tuples to be retrieved */
803  joinrel->consider_startup = (root->tuple_fraction > 0);
804  joinrel->consider_param_startup = false;
805  joinrel->consider_parallel = false;
806  joinrel->reltarget = create_empty_pathtarget();
807  joinrel->pathlist = NIL;
808  joinrel->ppilist = NIL;
809  joinrel->partial_pathlist = NIL;
810  joinrel->cheapest_startup_path = NULL;
811  joinrel->cheapest_total_path = NULL;
812  joinrel->cheapest_unique_path = NULL;
814  joinrel->direct_lateral_relids = NULL;
815  joinrel->lateral_relids = NULL;
816  joinrel->relid = 0; /* indicates not a baserel */
817  joinrel->rtekind = RTE_JOIN;
818  joinrel->min_attr = 0;
819  joinrel->max_attr = 0;
820  joinrel->attr_needed = NULL;
821  joinrel->attr_widths = NULL;
822  joinrel->lateral_vars = NIL;
823  joinrel->lateral_referencers = NULL;
824  joinrel->indexlist = NIL;
825  joinrel->pages = 0;
826  joinrel->tuples = 0;
827  joinrel->allvisfrac = 0;
828  joinrel->eclass_indexes = NULL;
829  joinrel->subroot = NULL;
830  joinrel->subplan_params = NIL;
831  joinrel->amflags = 0;
832  joinrel->serverid = InvalidOid;
833  joinrel->userid = InvalidOid;
834  joinrel->useridiscurrent = false;
835  joinrel->fdwroutine = NULL;
836  joinrel->fdw_private = NULL;
837  joinrel->baserestrictinfo = NIL;
838  joinrel->baserestrictcost.startup = 0;
839  joinrel->baserestrictcost.per_tuple = 0;
840  joinrel->joininfo = NIL;
841  joinrel->has_eclass_joins = false;
842  joinrel->consider_partitionwise_join = false; /* might get changed later */
843  joinrel->top_parent_relids = NULL;
844  joinrel->part_scheme = NULL;
845  joinrel->nparts = -1;
846  joinrel->boundinfo = NULL;
847  joinrel->partbounds_merged = false;
848  joinrel->partition_qual = NIL;
849  joinrel->part_rels = NULL;
850  joinrel->all_partrels = NULL;
851  joinrel->partexprs = NULL;
852  joinrel->nullable_partexprs = NULL;
853 
854  joinrel->top_parent_relids = bms_union(outer_rel->top_parent_relids,
855  inner_rel->top_parent_relids);
856 
857  /* Compute information relevant to foreign relations. */
858  set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
859 
860  /* Compute information needed for mapping Vars to the child rel */
861  appinfos = find_appinfos_by_relids(root, joinrel->relids, &nappinfos);
862 
863  /* Set up reltarget struct */
864  build_child_join_reltarget(root, parent_joinrel, joinrel,
865  nappinfos, appinfos);
866 
867  /* Construct joininfo list. */
868  joinrel->joininfo = (List *) adjust_appendrel_attrs(root,
869  (Node *) parent_joinrel->joininfo,
870  nappinfos,
871  appinfos);
872 
873  /*
874  * Lateral relids referred in child join will be same as that referred in
875  * the parent relation.
876  */
877  joinrel->direct_lateral_relids = (Relids) bms_copy(parent_joinrel->direct_lateral_relids);
878  joinrel->lateral_relids = (Relids) bms_copy(parent_joinrel->lateral_relids);
879 
880  /*
881  * If the parent joinrel has pending equivalence classes, so does the
882  * child.
883  */
884  joinrel->has_eclass_joins = parent_joinrel->has_eclass_joins;
885 
886  /* Is the join between partitions itself partitioned? */
887  build_joinrel_partition_info(joinrel, outer_rel, inner_rel, restrictlist,
888  jointype);
889 
890  /* Child joinrel is parallel safe if parent is parallel safe. */
891  joinrel->consider_parallel = parent_joinrel->consider_parallel;
892 
893  /* Set estimates of the child-joinrel's size. */
894  set_joinrel_size_estimates(root, joinrel, outer_rel, inner_rel,
895  sjinfo, restrictlist);
896 
897  /* We build the join only once. */
898  Assert(!find_join_rel(root, joinrel->relids));
899 
900  /* Add the relation to the PlannerInfo. */
901  add_join_rel(root, joinrel);
902 
903  /*
904  * We might need EquivalenceClass members corresponding to the child join,
905  * so that we can represent sort pathkeys for it. As with children of
906  * baserels, we shouldn't need this unless there are relevant eclass joins
907  * (implying that a merge join might be possible) or pathkeys to sort by.
908  */
909  if (joinrel->has_eclass_joins || has_useful_pathkeys(root, parent_joinrel))
911  nappinfos, appinfos,
912  parent_joinrel, joinrel);
913 
914  pfree(appinfos);
915 
916  return joinrel;
917 }
918 
919 /*
920  * min_join_parameterization
921  *
922  * Determine the minimum possible parameterization of a joinrel, that is, the
923  * set of other rels it contains LATERAL references to. We save this value in
924  * the join's RelOptInfo. This function is split out of build_join_rel()
925  * because join_is_legal() needs the value to check a prospective join.
926  */
927 Relids
929  Relids joinrelids,
930  RelOptInfo *outer_rel,
931  RelOptInfo *inner_rel)
932 {
933  Relids result;
934 
935  /*
936  * Basically we just need the union of the inputs' lateral_relids, less
937  * whatever is already in the join.
938  *
939  * It's not immediately obvious that this is a valid way to compute the
940  * result, because it might seem that we're ignoring possible lateral refs
941  * of PlaceHolderVars that are due to be computed at the join but not in
942  * either input. However, because create_lateral_join_info() already
943  * charged all such PHV refs to each member baserel of the join, they'll
944  * be accounted for already in the inputs' lateral_relids. Likewise, we
945  * do not need to worry about doing transitive closure here, because that
946  * was already accounted for in the original baserel lateral_relids.
947  */
948  result = bms_union(outer_rel->lateral_relids, inner_rel->lateral_relids);
949  result = bms_del_members(result, joinrelids);
950 
951  /* Maintain invariant that result is exactly NULL if empty */
952  if (bms_is_empty(result))
953  result = NULL;
954 
955  return result;
956 }
957 
958 /*
959  * build_joinrel_tlist
960  * Builds a join relation's target list from an input relation.
961  * (This is invoked twice to handle the two input relations.)
962  *
963  * The join's targetlist includes all Vars of its member relations that
964  * will still be needed above the join. This subroutine adds all such
965  * Vars from the specified input rel's tlist to the join rel's tlist.
966  *
967  * We also compute the expected width of the join's output, making use
968  * of data that was cached at the baserel level by set_rel_width().
969  */
970 static void
972  RelOptInfo *input_rel)
973 {
974  Relids relids = joinrel->relids;
975  ListCell *vars;
976 
977  foreach(vars, input_rel->reltarget->exprs)
978  {
979  Var *var = (Var *) lfirst(vars);
980  RelOptInfo *baserel;
981  int ndx;
982 
983  /*
984  * Ignore PlaceHolderVars in the input tlists; we'll make our own
985  * decisions about whether to copy them.
986  */
987  if (IsA(var, PlaceHolderVar))
988  continue;
989 
990  /*
991  * Otherwise, anything in a baserel or joinrel targetlist ought to be
992  * a Var. (More general cases can only appear in appendrel child
993  * rels, which will never be seen here.)
994  */
995  if (!IsA(var, Var))
996  elog(ERROR, "unexpected node type in rel targetlist: %d",
997  (int) nodeTag(var));
998 
999  /* Get the Var's original base rel */
1000  baserel = find_base_rel(root, var->varno);
1001 
1002  /* Is it still needed above this joinrel? */
1003  ndx = var->varattno - baserel->min_attr;
1004  if (bms_nonempty_difference(baserel->attr_needed[ndx], relids))
1005  {
1006  /* Yup, add it to the output */
1007  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs, var);
1008  /* Vars have cost zero, so no need to adjust reltarget->cost */
1009  joinrel->reltarget->width += baserel->attr_widths[ndx];
1010  }
1011  }
1012 }
1013 
1014 /*
1015  * build_joinrel_restrictlist
1016  * build_joinrel_joinlist
1017  * These routines build lists of restriction and join clauses for a
1018  * join relation from the joininfo lists of the relations it joins.
1019  *
1020  * These routines are separate because the restriction list must be
1021  * built afresh for each pair of input sub-relations we consider, whereas
1022  * the join list need only be computed once for any join RelOptInfo.
1023  * The join list is fully determined by the set of rels making up the
1024  * joinrel, so we should get the same results (up to ordering) from any
1025  * candidate pair of sub-relations. But the restriction list is whatever
1026  * is not handled in the sub-relations, so it depends on which
1027  * sub-relations are considered.
1028  *
1029  * If a join clause from an input relation refers to base rels still not
1030  * present in the joinrel, then it is still a join clause for the joinrel;
1031  * we put it into the joininfo list for the joinrel. Otherwise,
1032  * the clause is now a restrict clause for the joined relation, and we
1033  * return it to the caller of build_joinrel_restrictlist() to be stored in
1034  * join paths made from this pair of sub-relations. (It will not need to
1035  * be considered further up the join tree.)
1036  *
1037  * In many cases we will find the same RestrictInfos in both input
1038  * relations' joinlists, so be careful to eliminate duplicates.
1039  * Pointer equality should be a sufficient test for dups, since all
1040  * the various joinlist entries ultimately refer to RestrictInfos
1041  * pushed into them by distribute_restrictinfo_to_rels().
1042  *
1043  * 'joinrel' is a join relation node
1044  * 'outer_rel' and 'inner_rel' are a pair of relations that can be joined
1045  * to form joinrel.
1046  *
1047  * build_joinrel_restrictlist() returns a list of relevant restrictinfos,
1048  * whereas build_joinrel_joinlist() stores its results in the joinrel's
1049  * joininfo list. One or the other must accept each given clause!
1050  *
1051  * NB: Formerly, we made deep(!) copies of each input RestrictInfo to pass
1052  * up to the join relation. I believe this is no longer necessary, because
1053  * RestrictInfo nodes are no longer context-dependent. Instead, just include
1054  * the original nodes in the lists made for the join relation.
1055  */
1056 static List *
1058  RelOptInfo *joinrel,
1059  RelOptInfo *outer_rel,
1060  RelOptInfo *inner_rel)
1061 {
1062  List *result;
1063 
1064  /*
1065  * Collect all the clauses that syntactically belong at this level,
1066  * eliminating any duplicates (important since we will see many of the
1067  * same clauses arriving from both input relations).
1068  */
1069  result = subbuild_joinrel_restrictlist(joinrel, outer_rel->joininfo, NIL);
1070  result = subbuild_joinrel_restrictlist(joinrel, inner_rel->joininfo, result);
1071 
1072  /*
1073  * Add on any clauses derived from EquivalenceClasses. These cannot be
1074  * redundant with the clauses in the joininfo lists, so don't bother
1075  * checking.
1076  */
1077  result = list_concat(result,
1079  joinrel->relids,
1080  outer_rel->relids,
1081  inner_rel));
1082 
1083  return result;
1084 }
1085 
1086 static void
1088  RelOptInfo *outer_rel,
1089  RelOptInfo *inner_rel)
1090 {
1091  List *result;
1092 
1093  /*
1094  * Collect all the clauses that syntactically belong above this level,
1095  * eliminating any duplicates (important since we will see many of the
1096  * same clauses arriving from both input relations).
1097  */
1098  result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
1099  result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
1100 
1101  joinrel->joininfo = result;
1102 }
1103 
1104 static List *
1106  List *joininfo_list,
1107  List *new_restrictlist)
1108 {
1109  ListCell *l;
1110 
1111  foreach(l, joininfo_list)
1112  {
1113  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1114 
1115  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1116  {
1117  /*
1118  * This clause becomes a restriction clause for the joinrel, since
1119  * it refers to no outside rels. Add it to the list, being
1120  * careful to eliminate duplicates. (Since RestrictInfo nodes in
1121  * different joinlists will have been multiply-linked rather than
1122  * copied, pointer equality should be a sufficient test.)
1123  */
1124  new_restrictlist = list_append_unique_ptr(new_restrictlist, rinfo);
1125  }
1126  else
1127  {
1128  /*
1129  * This clause is still a join clause at this level, so we ignore
1130  * it in this routine.
1131  */
1132  }
1133  }
1134 
1135  return new_restrictlist;
1136 }
1137 
1138 static List *
1140  List *joininfo_list,
1141  List *new_joininfo)
1142 {
1143  ListCell *l;
1144 
1145  /* Expected to be called only for join between parent relations. */
1146  Assert(joinrel->reloptkind == RELOPT_JOINREL);
1147 
1148  foreach(l, joininfo_list)
1149  {
1150  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1151 
1152  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1153  {
1154  /*
1155  * This clause becomes a restriction clause for the joinrel, since
1156  * it refers to no outside rels. So we can ignore it in this
1157  * routine.
1158  */
1159  }
1160  else
1161  {
1162  /*
1163  * This clause is still a join clause at this level, so add it to
1164  * the new joininfo list, being careful to eliminate duplicates.
1165  * (Since RestrictInfo nodes in different joinlists will have been
1166  * multiply-linked rather than copied, pointer equality should be
1167  * a sufficient test.)
1168  */
1169  new_joininfo = list_append_unique_ptr(new_joininfo, rinfo);
1170  }
1171  }
1172 
1173  return new_joininfo;
1174 }
1175 
1176 
1177 /*
1178  * fetch_upper_rel
1179  * Build a RelOptInfo describing some post-scan/join query processing,
1180  * or return a pre-existing one if somebody already built it.
1181  *
1182  * An "upper" relation is identified by an UpperRelationKind and a Relids set.
1183  * The meaning of the Relids set is not specified here, and very likely will
1184  * vary for different relation kinds.
1185  *
1186  * Most of the fields in an upper-level RelOptInfo are not used and are not
1187  * set here (though makeNode should ensure they're zeroes). We basically only
1188  * care about fields that are of interest to add_path() and set_cheapest().
1189  */
1190 RelOptInfo *
1192 {
1193  RelOptInfo *upperrel;
1194  ListCell *lc;
1195 
1196  /*
1197  * For the moment, our indexing data structure is just a List for each
1198  * relation kind. If we ever get so many of one kind that this stops
1199  * working well, we can improve it. No code outside this function should
1200  * assume anything about how to find a particular upperrel.
1201  */
1202 
1203  /* If we already made this upperrel for the query, return it */
1204  foreach(lc, root->upper_rels[kind])
1205  {
1206  upperrel = (RelOptInfo *) lfirst(lc);
1207 
1208  if (bms_equal(upperrel->relids, relids))
1209  return upperrel;
1210  }
1211 
1212  upperrel = makeNode(RelOptInfo);
1213  upperrel->reloptkind = RELOPT_UPPER_REL;
1214  upperrel->relids = bms_copy(relids);
1215 
1216  /* cheap startup cost is interesting iff not all tuples to be retrieved */
1217  upperrel->consider_startup = (root->tuple_fraction > 0);
1218  upperrel->consider_param_startup = false;
1219  upperrel->consider_parallel = false; /* might get changed later */
1220  upperrel->reltarget = create_empty_pathtarget();
1221  upperrel->pathlist = NIL;
1222  upperrel->cheapest_startup_path = NULL;
1223  upperrel->cheapest_total_path = NULL;
1224  upperrel->cheapest_unique_path = NULL;
1225  upperrel->cheapest_parameterized_paths = NIL;
1226 
1227  root->upper_rels[kind] = lappend(root->upper_rels[kind], upperrel);
1228 
1229  return upperrel;
1230 }
1231 
1232 
1233 /*
1234  * find_childrel_parents
1235  * Compute the set of parent relids of an appendrel child rel.
1236  *
1237  * Since appendrels can be nested, a child could have multiple levels of
1238  * appendrel ancestors. This function computes a Relids set of all the
1239  * parent relation IDs.
1240  */
1241 Relids
1243 {
1244  Relids result = NULL;
1245 
1247  Assert(rel->relid > 0 && rel->relid < root->simple_rel_array_size);
1248 
1249  do
1250  {
1251  AppendRelInfo *appinfo = root->append_rel_array[rel->relid];
1252  Index prelid = appinfo->parent_relid;
1253 
1254  result = bms_add_member(result, prelid);
1255 
1256  /* traverse up to the parent rel, loop if it's also a child rel */
1257  rel = find_base_rel(root, prelid);
1258  } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1259 
1260  Assert(rel->reloptkind == RELOPT_BASEREL);
1261 
1262  return result;
1263 }
1264 
1265 
1266 /*
1267  * get_baserel_parampathinfo
1268  * Get the ParamPathInfo for a parameterized path for a base relation,
1269  * constructing one if we don't have one already.
1270  *
1271  * This centralizes estimating the rowcounts for parameterized paths.
1272  * We need to cache those to be sure we use the same rowcount for all paths
1273  * of the same parameterization for a given rel. This is also a convenient
1274  * place to determine which movable join clauses the parameterized path will
1275  * be responsible for evaluating.
1276  */
1277 ParamPathInfo *
1279  Relids required_outer)
1280 {
1281  ParamPathInfo *ppi;
1282  Relids joinrelids;
1283  List *pclauses;
1284  double rows;
1285  ListCell *lc;
1286 
1287  /* If rel has LATERAL refs, every path for it should account for them */
1288  Assert(bms_is_subset(baserel->lateral_relids, required_outer));
1289 
1290  /* Unparameterized paths have no ParamPathInfo */
1291  if (bms_is_empty(required_outer))
1292  return NULL;
1293 
1294  Assert(!bms_overlap(baserel->relids, required_outer));
1295 
1296  /* If we already have a PPI for this parameterization, just return it */
1297  if ((ppi = find_param_path_info(baserel, required_outer)))
1298  return ppi;
1299 
1300  /*
1301  * Identify all joinclauses that are movable to this base rel given this
1302  * parameterization.
1303  */
1304  joinrelids = bms_union(baserel->relids, required_outer);
1305  pclauses = NIL;
1306  foreach(lc, baserel->joininfo)
1307  {
1308  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1309 
1310  if (join_clause_is_movable_into(rinfo,
1311  baserel->relids,
1312  joinrelids))
1313  pclauses = lappend(pclauses, rinfo);
1314  }
1315 
1316  /*
1317  * Add in joinclauses generated by EquivalenceClasses, too. (These
1318  * necessarily satisfy join_clause_is_movable_into.)
1319  */
1320  pclauses = list_concat(pclauses,
1322  joinrelids,
1323  required_outer,
1324  baserel));
1325 
1326  /* Estimate the number of rows returned by the parameterized scan */
1327  rows = get_parameterized_baserel_size(root, baserel, pclauses);
1328 
1329  /* And now we can build the ParamPathInfo */
1330  ppi = makeNode(ParamPathInfo);
1331  ppi->ppi_req_outer = required_outer;
1332  ppi->ppi_rows = rows;
1333  ppi->ppi_clauses = pclauses;
1334  baserel->ppilist = lappend(baserel->ppilist, ppi);
1335 
1336  return ppi;
1337 }
1338 
1339 /*
1340  * get_joinrel_parampathinfo
1341  * Get the ParamPathInfo for a parameterized path for a join relation,
1342  * constructing one if we don't have one already.
1343  *
1344  * This centralizes estimating the rowcounts for parameterized paths.
1345  * We need to cache those to be sure we use the same rowcount for all paths
1346  * of the same parameterization for a given rel. This is also a convenient
1347  * place to determine which movable join clauses the parameterized path will
1348  * be responsible for evaluating.
1349  *
1350  * outer_path and inner_path are a pair of input paths that can be used to
1351  * construct the join, and restrict_clauses is the list of regular join
1352  * clauses (including clauses derived from EquivalenceClasses) that must be
1353  * applied at the join node when using these inputs.
1354  *
1355  * Unlike the situation for base rels, the set of movable join clauses to be
1356  * enforced at a join varies with the selected pair of input paths, so we
1357  * must calculate that and pass it back, even if we already have a matching
1358  * ParamPathInfo. We handle this by adding any clauses moved down to this
1359  * join to *restrict_clauses, which is an in/out parameter. (The addition
1360  * is done in such a way as to not modify the passed-in List structure.)
1361  *
1362  * Note: when considering a nestloop join, the caller must have removed from
1363  * restrict_clauses any movable clauses that are themselves scheduled to be
1364  * pushed into the right-hand path. We do not do that here since it's
1365  * unnecessary for other join types.
1366  */
1367 ParamPathInfo *
1369  Path *outer_path,
1370  Path *inner_path,
1371  SpecialJoinInfo *sjinfo,
1372  Relids required_outer,
1373  List **restrict_clauses)
1374 {
1375  ParamPathInfo *ppi;
1376  Relids join_and_req;
1377  Relids outer_and_req;
1378  Relids inner_and_req;
1379  List *pclauses;
1380  List *eclauses;
1381  List *dropped_ecs;
1382  double rows;
1383  ListCell *lc;
1384 
1385  /* If rel has LATERAL refs, every path for it should account for them */
1386  Assert(bms_is_subset(joinrel->lateral_relids, required_outer));
1387 
1388  /* Unparameterized paths have no ParamPathInfo or extra join clauses */
1389  if (bms_is_empty(required_outer))
1390  return NULL;
1391 
1392  Assert(!bms_overlap(joinrel->relids, required_outer));
1393 
1394  /*
1395  * Identify all joinclauses that are movable to this join rel given this
1396  * parameterization. These are the clauses that are movable into this
1397  * join, but not movable into either input path. Treat an unparameterized
1398  * input path as not accepting parameterized clauses (because it won't,
1399  * per the shortcut exit above), even though the joinclause movement rules
1400  * might allow the same clauses to be moved into a parameterized path for
1401  * that rel.
1402  */
1403  join_and_req = bms_union(joinrel->relids, required_outer);
1404  if (outer_path->param_info)
1405  outer_and_req = bms_union(outer_path->parent->relids,
1406  PATH_REQ_OUTER(outer_path));
1407  else
1408  outer_and_req = NULL; /* outer path does not accept parameters */
1409  if (inner_path->param_info)
1410  inner_and_req = bms_union(inner_path->parent->relids,
1411  PATH_REQ_OUTER(inner_path));
1412  else
1413  inner_and_req = NULL; /* inner path does not accept parameters */
1414 
1415  pclauses = NIL;
1416  foreach(lc, joinrel->joininfo)
1417  {
1418  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1419 
1420  if (join_clause_is_movable_into(rinfo,
1421  joinrel->relids,
1422  join_and_req) &&
1424  outer_path->parent->relids,
1425  outer_and_req) &&
1427  inner_path->parent->relids,
1428  inner_and_req))
1429  pclauses = lappend(pclauses, rinfo);
1430  }
1431 
1432  /* Consider joinclauses generated by EquivalenceClasses, too */
1433  eclauses = generate_join_implied_equalities(root,
1434  join_and_req,
1435  required_outer,
1436  joinrel);
1437  /* We only want ones that aren't movable to lower levels */
1438  dropped_ecs = NIL;
1439  foreach(lc, eclauses)
1440  {
1441  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1442 
1443  /*
1444  * In principle, join_clause_is_movable_into() should accept anything
1445  * returned by generate_join_implied_equalities(); but because its
1446  * analysis is only approximate, sometimes it doesn't. So we
1447  * currently cannot use this Assert; instead just assume it's okay to
1448  * apply the joinclause at this level.
1449  */
1450 #ifdef NOT_USED
1452  joinrel->relids,
1453  join_and_req));
1454 #endif
1455  if (join_clause_is_movable_into(rinfo,
1456  outer_path->parent->relids,
1457  outer_and_req))
1458  continue; /* drop if movable into LHS */
1459  if (join_clause_is_movable_into(rinfo,
1460  inner_path->parent->relids,
1461  inner_and_req))
1462  {
1463  /* drop if movable into RHS, but remember EC for use below */
1464  Assert(rinfo->left_ec == rinfo->right_ec);
1465  dropped_ecs = lappend(dropped_ecs, rinfo->left_ec);
1466  continue;
1467  }
1468  pclauses = lappend(pclauses, rinfo);
1469  }
1470 
1471  /*
1472  * EquivalenceClasses are harder to deal with than we could wish, because
1473  * of the fact that a given EC can generate different clauses depending on
1474  * context. Suppose we have an EC {X.X, Y.Y, Z.Z} where X and Y are the
1475  * LHS and RHS of the current join and Z is in required_outer, and further
1476  * suppose that the inner_path is parameterized by both X and Z. The code
1477  * above will have produced either Z.Z = X.X or Z.Z = Y.Y from that EC,
1478  * and in the latter case will have discarded it as being movable into the
1479  * RHS. However, the EC machinery might have produced either Y.Y = X.X or
1480  * Y.Y = Z.Z as the EC enforcement clause within the inner_path; it will
1481  * not have produced both, and we can't readily tell from here which one
1482  * it did pick. If we add no clause to this join, we'll end up with
1483  * insufficient enforcement of the EC; either Z.Z or X.X will fail to be
1484  * constrained to be equal to the other members of the EC. (When we come
1485  * to join Z to this X/Y path, we will certainly drop whichever EC clause
1486  * is generated at that join, so this omission won't get fixed later.)
1487  *
1488  * To handle this, for each EC we discarded such a clause from, try to
1489  * generate a clause connecting the required_outer rels to the join's LHS
1490  * ("Z.Z = X.X" in the terms of the above example). If successful, and if
1491  * the clause can't be moved to the LHS, add it to the current join's
1492  * restriction clauses. (If an EC cannot generate such a clause then it
1493  * has nothing that needs to be enforced here, while if the clause can be
1494  * moved into the LHS then it should have been enforced within that path.)
1495  *
1496  * Note that we don't need similar processing for ECs whose clause was
1497  * considered to be movable into the LHS, because the LHS can't refer to
1498  * the RHS so there is no comparable ambiguity about what it might
1499  * actually be enforcing internally.
1500  */
1501  if (dropped_ecs)
1502  {
1503  Relids real_outer_and_req;
1504 
1505  real_outer_and_req = bms_union(outer_path->parent->relids,
1506  required_outer);
1507  eclauses =
1509  dropped_ecs,
1510  real_outer_and_req,
1511  required_outer,
1512  outer_path->parent);
1513  foreach(lc, eclauses)
1514  {
1515  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1516 
1517  /* As above, can't quite assert this here */
1518 #ifdef NOT_USED
1520  outer_path->parent->relids,
1521  real_outer_and_req));
1522 #endif
1523  if (!join_clause_is_movable_into(rinfo,
1524  outer_path->parent->relids,
1525  outer_and_req))
1526  pclauses = lappend(pclauses, rinfo);
1527  }
1528  }
1529 
1530  /*
1531  * Now, attach the identified moved-down clauses to the caller's
1532  * restrict_clauses list. By using list_concat in this order, we leave
1533  * the original list structure of restrict_clauses undamaged.
1534  */
1535  *restrict_clauses = list_concat(pclauses, *restrict_clauses);
1536 
1537  /* If we already have a PPI for this parameterization, just return it */
1538  if ((ppi = find_param_path_info(joinrel, required_outer)))
1539  return ppi;
1540 
1541  /* Estimate the number of rows returned by the parameterized join */
1542  rows = get_parameterized_joinrel_size(root, joinrel,
1543  outer_path,
1544  inner_path,
1545  sjinfo,
1546  *restrict_clauses);
1547 
1548  /*
1549  * And now we can build the ParamPathInfo. No point in saving the
1550  * input-pair-dependent clause list, though.
1551  *
1552  * Note: in GEQO mode, we'll be called in a temporary memory context, but
1553  * the joinrel structure is there too, so no problem.
1554  */
1555  ppi = makeNode(ParamPathInfo);
1556  ppi->ppi_req_outer = required_outer;
1557  ppi->ppi_rows = rows;
1558  ppi->ppi_clauses = NIL;
1559  joinrel->ppilist = lappend(joinrel->ppilist, ppi);
1560 
1561  return ppi;
1562 }
1563 
1564 /*
1565  * get_appendrel_parampathinfo
1566  * Get the ParamPathInfo for a parameterized path for an append relation.
1567  *
1568  * For an append relation, the rowcount estimate will just be the sum of
1569  * the estimates for its children. However, we still need a ParamPathInfo
1570  * to flag the fact that the path requires parameters. So this just creates
1571  * a suitable struct with zero ppi_rows (and no ppi_clauses either, since
1572  * the Append node isn't responsible for checking quals).
1573  */
1574 ParamPathInfo *
1575 get_appendrel_parampathinfo(RelOptInfo *appendrel, Relids required_outer)
1576 {
1577  ParamPathInfo *ppi;
1578 
1579  /* If rel has LATERAL refs, every path for it should account for them */
1580  Assert(bms_is_subset(appendrel->lateral_relids, required_outer));
1581 
1582  /* Unparameterized paths have no ParamPathInfo */
1583  if (bms_is_empty(required_outer))
1584  return NULL;
1585 
1586  Assert(!bms_overlap(appendrel->relids, required_outer));
1587 
1588  /* If we already have a PPI for this parameterization, just return it */
1589  if ((ppi = find_param_path_info(appendrel, required_outer)))
1590  return ppi;
1591 
1592  /* Else build the ParamPathInfo */
1593  ppi = makeNode(ParamPathInfo);
1594  ppi->ppi_req_outer = required_outer;
1595  ppi->ppi_rows = 0;
1596  ppi->ppi_clauses = NIL;
1597  appendrel->ppilist = lappend(appendrel->ppilist, ppi);
1598 
1599  return ppi;
1600 }
1601 
1602 /*
1603  * Returns a ParamPathInfo for the parameterization given by required_outer, if
1604  * already available in the given rel. Returns NULL otherwise.
1605  */
1606 ParamPathInfo *
1608 {
1609  ListCell *lc;
1610 
1611  foreach(lc, rel->ppilist)
1612  {
1613  ParamPathInfo *ppi = (ParamPathInfo *) lfirst(lc);
1614 
1615  if (bms_equal(ppi->ppi_req_outer, required_outer))
1616  return ppi;
1617  }
1618 
1619  return NULL;
1620 }
1621 
1622 /*
1623  * build_joinrel_partition_info
1624  * Checks if the two relations being joined can use partitionwise join
1625  * and if yes, initialize partitioning information of the resulting
1626  * partitioned join relation.
1627  */
1628 static void
1630  RelOptInfo *inner_rel, List *restrictlist,
1631  JoinType jointype)
1632 {
1633  PartitionScheme part_scheme;
1634 
1635  /* Nothing to do if partitionwise join technique is disabled. */
1637  {
1638  Assert(!IS_PARTITIONED_REL(joinrel));
1639  return;
1640  }
1641 
1642  /*
1643  * We can only consider this join as an input to further partitionwise
1644  * joins if (a) the input relations are partitioned and have
1645  * consider_partitionwise_join=true, (b) the partition schemes match, and
1646  * (c) we can identify an equi-join between the partition keys. Note that
1647  * if it were possible for have_partkey_equi_join to return different
1648  * answers for the same joinrel depending on which join ordering we try
1649  * first, this logic would break. That shouldn't happen, though, because
1650  * of the way the query planner deduces implied equalities and reorders
1651  * the joins. Please see optimizer/README for details.
1652  */
1653  if (outer_rel->part_scheme == NULL || inner_rel->part_scheme == NULL ||
1654  !outer_rel->consider_partitionwise_join ||
1655  !inner_rel->consider_partitionwise_join ||
1656  outer_rel->part_scheme != inner_rel->part_scheme ||
1657  !have_partkey_equi_join(joinrel, outer_rel, inner_rel,
1658  jointype, restrictlist))
1659  {
1660  Assert(!IS_PARTITIONED_REL(joinrel));
1661  return;
1662  }
1663 
1664  part_scheme = outer_rel->part_scheme;
1665 
1666  /*
1667  * This function will be called only once for each joinrel, hence it
1668  * should not have partitioning fields filled yet.
1669  */
1670  Assert(!joinrel->part_scheme && !joinrel->partexprs &&
1671  !joinrel->nullable_partexprs && !joinrel->part_rels &&
1672  !joinrel->boundinfo);
1673 
1674  /*
1675  * If the join relation is partitioned, it uses the same partitioning
1676  * scheme as the joining relations.
1677  *
1678  * Note: we calculate the partition bounds, number of partitions, and
1679  * child-join relations of the join relation in try_partitionwise_join().
1680  */
1681  joinrel->part_scheme = part_scheme;
1682  set_joinrel_partition_key_exprs(joinrel, outer_rel, inner_rel, jointype);
1683 
1684  /*
1685  * Set the consider_partitionwise_join flag.
1686  */
1687  Assert(outer_rel->consider_partitionwise_join);
1688  Assert(inner_rel->consider_partitionwise_join);
1689  joinrel->consider_partitionwise_join = true;
1690 }
1691 
1692 /*
1693  * have_partkey_equi_join
1694  *
1695  * Returns true if there exist equi-join conditions involving pairs
1696  * of matching partition keys of the relations being joined for all
1697  * partition keys.
1698  */
1699 static bool
1701  RelOptInfo *rel1, RelOptInfo *rel2,
1702  JoinType jointype, List *restrictlist)
1703 {
1704  PartitionScheme part_scheme = rel1->part_scheme;
1705  ListCell *lc;
1706  int cnt_pks;
1707  bool pk_has_clause[PARTITION_MAX_KEYS];
1708  bool strict_op;
1709 
1710  /*
1711  * This function must only be called when the joined relations have same
1712  * partitioning scheme.
1713  */
1714  Assert(rel1->part_scheme == rel2->part_scheme);
1715  Assert(part_scheme);
1716 
1717  memset(pk_has_clause, 0, sizeof(pk_has_clause));
1718  foreach(lc, restrictlist)
1719  {
1720  RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1721  OpExpr *opexpr;
1722  Expr *expr1;
1723  Expr *expr2;
1724  int ipk1;
1725  int ipk2;
1726 
1727  /* If processing an outer join, only use its own join clauses. */
1728  if (IS_OUTER_JOIN(jointype) &&
1729  RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
1730  continue;
1731 
1732  /* Skip clauses which can not be used for a join. */
1733  if (!rinfo->can_join)
1734  continue;
1735 
1736  /* Skip clauses which are not equality conditions. */
1737  if (!rinfo->mergeopfamilies && !OidIsValid(rinfo->hashjoinoperator))
1738  continue;
1739 
1740  /* Should be OK to assume it's an OpExpr. */
1741  opexpr = castNode(OpExpr, rinfo->clause);
1742 
1743  /* Match the operands to the relation. */
1744  if (bms_is_subset(rinfo->left_relids, rel1->relids) &&
1745  bms_is_subset(rinfo->right_relids, rel2->relids))
1746  {
1747  expr1 = linitial(opexpr->args);
1748  expr2 = lsecond(opexpr->args);
1749  }
1750  else if (bms_is_subset(rinfo->left_relids, rel2->relids) &&
1751  bms_is_subset(rinfo->right_relids, rel1->relids))
1752  {
1753  expr1 = lsecond(opexpr->args);
1754  expr2 = linitial(opexpr->args);
1755  }
1756  else
1757  continue;
1758 
1759  /*
1760  * Now we need to know whether the join operator is strict; see
1761  * comments in pathnodes.h.
1762  */
1763  strict_op = op_strict(opexpr->opno);
1764 
1765  /*
1766  * Only clauses referencing the partition keys are useful for
1767  * partitionwise join.
1768  */
1769  ipk1 = match_expr_to_partition_keys(expr1, rel1, strict_op);
1770  if (ipk1 < 0)
1771  continue;
1772  ipk2 = match_expr_to_partition_keys(expr2, rel2, strict_op);
1773  if (ipk2 < 0)
1774  continue;
1775 
1776  /*
1777  * If the clause refers to keys at different ordinal positions, it can
1778  * not be used for partitionwise join.
1779  */
1780  if (ipk1 != ipk2)
1781  continue;
1782 
1783  /*
1784  * The clause allows partitionwise join only if it uses the same
1785  * operator family as that specified by the partition key.
1786  */
1788  {
1789  if (!OidIsValid(rinfo->hashjoinoperator) ||
1791  part_scheme->partopfamily[ipk1]))
1792  continue;
1793  }
1794  else if (!list_member_oid(rinfo->mergeopfamilies,
1795  part_scheme->partopfamily[ipk1]))
1796  continue;
1797 
1798  /* Mark the partition key as having an equi-join clause. */
1799  pk_has_clause[ipk1] = true;
1800  }
1801 
1802  /* Check whether every partition key has an equi-join condition. */
1803  for (cnt_pks = 0; cnt_pks < part_scheme->partnatts; cnt_pks++)
1804  {
1805  if (!pk_has_clause[cnt_pks])
1806  return false;
1807  }
1808 
1809  return true;
1810 }
1811 
1812 /*
1813  * match_expr_to_partition_keys
1814  *
1815  * Tries to match an expression to one of the nullable or non-nullable
1816  * partition keys of "rel". Returns the matched key's ordinal position,
1817  * or -1 if the expression could not be matched to any of the keys.
1818  *
1819  * strict_op must be true if the expression will be compared with the
1820  * partition key using a strict operator. This allows us to consider
1821  * nullable as well as nonnullable partition keys.
1822  */
1823 static int
1824 match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel, bool strict_op)
1825 {
1826  int cnt;
1827 
1828  /* This function should be called only for partitioned relations. */
1829  Assert(rel->part_scheme);
1830  Assert(rel->partexprs);
1831  Assert(rel->nullable_partexprs);
1832 
1833  /* Remove any relabel decorations. */
1834  while (IsA(expr, RelabelType))
1835  expr = (Expr *) (castNode(RelabelType, expr))->arg;
1836 
1837  for (cnt = 0; cnt < rel->part_scheme->partnatts; cnt++)
1838  {
1839  ListCell *lc;
1840 
1841  /* We can always match to the non-nullable partition keys. */
1842  foreach(lc, rel->partexprs[cnt])
1843  {
1844  if (equal(lfirst(lc), expr))
1845  return cnt;
1846  }
1847 
1848  if (!strict_op)
1849  continue;
1850 
1851  /*
1852  * If it's a strict join operator then a NULL partition key on one
1853  * side will not join to any partition key on the other side, and in
1854  * particular such a row can't join to a row from a different
1855  * partition on the other side. So, it's okay to search the nullable
1856  * partition keys as well.
1857  */
1858  foreach(lc, rel->nullable_partexprs[cnt])
1859  {
1860  if (equal(lfirst(lc), expr))
1861  return cnt;
1862  }
1863  }
1864 
1865  return -1;
1866 }
1867 
1868 /*
1869  * set_joinrel_partition_key_exprs
1870  * Initialize partition key expressions for a partitioned joinrel.
1871  */
1872 static void
1874  RelOptInfo *outer_rel, RelOptInfo *inner_rel,
1875  JoinType jointype)
1876 {
1877  PartitionScheme part_scheme = joinrel->part_scheme;
1878  int partnatts = part_scheme->partnatts;
1879 
1880  joinrel->partexprs = (List **) palloc0(sizeof(List *) * partnatts);
1881  joinrel->nullable_partexprs =
1882  (List **) palloc0(sizeof(List *) * partnatts);
1883 
1884  /*
1885  * The joinrel's partition expressions are the same as those of the input
1886  * rels, but we must properly classify them as nullable or not in the
1887  * joinrel's output. (Also, we add some more partition expressions if
1888  * it's a FULL JOIN.)
1889  */
1890  for (int cnt = 0; cnt < partnatts; cnt++)
1891  {
1892  /* mark these const to enforce that we copy them properly */
1893  const List *outer_expr = outer_rel->partexprs[cnt];
1894  const List *outer_null_expr = outer_rel->nullable_partexprs[cnt];
1895  const List *inner_expr = inner_rel->partexprs[cnt];
1896  const List *inner_null_expr = inner_rel->nullable_partexprs[cnt];
1897  List *partexpr = NIL;
1898  List *nullable_partexpr = NIL;
1899  ListCell *lc;
1900 
1901  switch (jointype)
1902  {
1903  /*
1904  * A join relation resulting from an INNER join may be
1905  * regarded as partitioned by either of the inner and outer
1906  * relation keys. For example, A INNER JOIN B ON A.a = B.b
1907  * can be regarded as partitioned on either A.a or B.b. So we
1908  * add both keys to the joinrel's partexpr lists. However,
1909  * anything that was already nullable still has to be treated
1910  * as nullable.
1911  */
1912  case JOIN_INNER:
1913  partexpr = list_concat_copy(outer_expr, inner_expr);
1914  nullable_partexpr = list_concat_copy(outer_null_expr,
1915  inner_null_expr);
1916  break;
1917 
1918  /*
1919  * A join relation resulting from a SEMI or ANTI join may be
1920  * regarded as partitioned by the outer relation keys. The
1921  * inner relation's keys are no longer interesting; since they
1922  * aren't visible in the join output, nothing could join to
1923  * them.
1924  */
1925  case JOIN_SEMI:
1926  case JOIN_ANTI:
1927  partexpr = list_copy(outer_expr);
1928  nullable_partexpr = list_copy(outer_null_expr);
1929  break;
1930 
1931  /*
1932  * A join relation resulting from a LEFT OUTER JOIN likewise
1933  * may be regarded as partitioned on the (non-nullable) outer
1934  * relation keys. The inner (nullable) relation keys are okay
1935  * as partition keys for further joins as long as they involve
1936  * strict join operators.
1937  */
1938  case JOIN_LEFT:
1939  partexpr = list_copy(outer_expr);
1940  nullable_partexpr = list_concat_copy(inner_expr,
1941  outer_null_expr);
1942  nullable_partexpr = list_concat(nullable_partexpr,
1943  inner_null_expr);
1944  break;
1945 
1946  /*
1947  * For FULL OUTER JOINs, both relations are nullable, so the
1948  * resulting join relation may be regarded as partitioned on
1949  * either of inner and outer relation keys, but only for joins
1950  * that involve strict join operators.
1951  */
1952  case JOIN_FULL:
1953  nullable_partexpr = list_concat_copy(outer_expr,
1954  inner_expr);
1955  nullable_partexpr = list_concat(nullable_partexpr,
1956  outer_null_expr);
1957  nullable_partexpr = list_concat(nullable_partexpr,
1958  inner_null_expr);
1959 
1960  /*
1961  * Also add CoalesceExprs corresponding to each possible
1962  * full-join output variable (that is, left side coalesced to
1963  * right side), so that we can match equijoin expressions
1964  * using those variables. We really only need these for
1965  * columns merged by JOIN USING, and only with the pairs of
1966  * input items that correspond to the data structures that
1967  * parse analysis would build for such variables. But it's
1968  * hard to tell which those are, so just make all the pairs.
1969  * Extra items in the nullable_partexprs list won't cause big
1970  * problems. (It's possible that such items will get matched
1971  * to user-written COALESCEs, but it should still be valid to
1972  * partition on those, since they're going to be either the
1973  * partition column or NULL; it's the same argument as for
1974  * partitionwise nesting of any outer join.) We assume no
1975  * type coercions are needed to make the coalesce expressions,
1976  * since columns of different types won't have gotten
1977  * classified as the same PartitionScheme.
1978  */
1979  foreach(lc, list_concat_copy(outer_expr, outer_null_expr))
1980  {
1981  Node *larg = (Node *) lfirst(lc);
1982  ListCell *lc2;
1983 
1984  foreach(lc2, list_concat_copy(inner_expr, inner_null_expr))
1985  {
1986  Node *rarg = (Node *) lfirst(lc2);
1988 
1989  c->coalescetype = exprType(larg);
1990  c->coalescecollid = exprCollation(larg);
1991  c->args = list_make2(larg, rarg);
1992  c->location = -1;
1993  nullable_partexpr = lappend(nullable_partexpr, c);
1994  }
1995  }
1996  break;
1997 
1998  default:
1999  elog(ERROR, "unrecognized join type: %d", (int) jointype);
2000  }
2001 
2002  joinrel->partexprs[cnt] = partexpr;
2003  joinrel->nullable_partexprs[cnt] = nullable_partexpr;
2004  }
2005 }
2006 
2007 /*
2008  * build_child_join_reltarget
2009  * Set up a child-join relation's reltarget from a parent-join relation.
2010  */
2011 static void
2013  RelOptInfo *parentrel,
2014  RelOptInfo *childrel,
2015  int nappinfos,
2016  AppendRelInfo **appinfos)
2017 {
2018  /* Build the targetlist */
2019  childrel->reltarget->exprs = (List *)
2021  (Node *) parentrel->reltarget->exprs,
2022  nappinfos, appinfos);
2023 
2024  /* Set the cost and width fields */
2025  childrel->reltarget->cost.startup = parentrel->reltarget->cost.startup;
2026  childrel->reltarget->cost.per_tuple = parentrel->reltarget->cost.per_tuple;
2027  childrel->reltarget->width = parentrel->reltarget->width;
2028 }
#define list_make2(x1, x2)
Definition: pg_list.h:208
bool has_eclass_joins
Definition: pathnodes.h:740
struct Path * cheapest_unique_path
Definition: pathnodes.h:689
PathTarget * create_empty_pathtarget(void)
Definition: tlist.c:691
#define NIL
Definition: pg_list.h:65
bool op_in_opfamily(Oid opno, Oid opfamily)
Definition: lsyscache.c:64
ParamPathInfo * find_param_path_info(RelOptInfo *rel, Relids required_outer)
Definition: relnode.c:1607
UpperRelationKind
Definition: pathnodes.h:67
int join_cur_level
Definition: pathnodes.h:251
List * unique_for_rels
Definition: pathnodes.h:729
bool op_strict(Oid opno)
Definition: lsyscache.c:1448
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
Query * parse
Definition: pathnodes.h:173
List * statlist
Definition: pathnodes.h:708
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
ParamPathInfo * get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel, Relids required_outer)
Definition: relnode.c:1278
RelOptKind reloptkind
Definition: pathnodes.h:667
Relids * attr_needed
Definition: pathnodes.h:703
Relids required_relids
Definition: pathnodes.h:2024
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
List * colnames
Definition: primnodes.h:43
MemoryContext hcxt
Definition: hsearch.h:86
RelOptInfo * find_join_rel(PlannerInfo *root, Relids relids)
Definition: relnode.c:438
ParamPathInfo * get_joinrel_parampathinfo(PlannerInfo *root, RelOptInfo *joinrel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, Relids required_outer, List **restrict_clauses)
Definition: relnode.c:1368
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3073
Oid GetUserId(void)
Definition: miscinit.c:478
#define castNode(_type_, nodeptr)
Definition: nodes.h:602
static List * subbuild_joinrel_restrictlist(RelOptInfo *joinrel, List *joininfo_list, List *new_restrictlist)
Definition: relnode.c:1105
static void set_joinrel_partition_key_exprs(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, JoinType jointype)
Definition: relnode.c:1873
#define IS_OTHER_REL(rel)
Definition: pathnodes.h:658
void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List *restrictlist)
Definition: costsize.c:4825
struct Path * cheapest_startup_path
Definition: pathnodes.h:687
Oid userid
Definition: pathnodes.h:722
Relids join_relids
Definition: relnode.c:37
double tuples
Definition: pathnodes.h:710
List * baserestrictinfo
Definition: pathnodes.h:734
bool consider_param_startup
Definition: pathnodes.h:677
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:749
ParamPathInfo * param_info
Definition: pathnodes.h:1157
Size entrysize
Definition: hsearch.h:76
List * list_copy(const List *oldlist)
Definition: list.c:1418
Definition: nodes.h:533
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
List * partial_pathlist
Definition: pathnodes.h:686
#define PARTITION_MAX_KEYS
#define MemSet(start, val, len)
Definition: c.h:1008
Relids left_relids
Definition: pathnodes.h:2033
AttrNumber varattno
Definition: primnodes.h:186
List * join_rel_list
Definition: pathnodes.h:240
EquivalenceClass * right_ec
Definition: pathnodes.h:2055
List * cheapest_parameterized_paths
Definition: pathnodes.h:690
Index baserestrict_min_security
Definition: pathnodes.h:736
Relids all_partrels
Definition: pathnodes.h:759
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
bool useridiscurrent
Definition: pathnodes.h:723
List ** nullable_partexprs
Definition: pathnodes.h:761
Definition: primnodes.h:181
#define OidIsValid(objectId)
Definition: c.h:710
void add_placeholders_to_joinrel(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: placeholder.c:413
List * mergeopfamilies
Definition: pathnodes.h:2051
#define lsecond(l)
Definition: pg_list.h:179
Cost startup
Definition: pathnodes.h:45
double allvisfrac
Definition: pathnodes.h:711
signed int int32
Definition: c.h:429
JoinType
Definition: nodes.h:700
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:197
void add_child_join_rel_equivalences(PlannerInfo *root, int nappinfos, AppendRelInfo **appinfos, RelOptInfo *parent_joinrel, RelOptInfo *child_joinrel)
Definition: equivclass.c:2524
PlannerInfo * subroot
Definition: pathnodes.h:714
bool consider_startup
Definition: pathnodes.h:676
Definition: dynahash.c:219
bool apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, RangeTblEntry *childRTE, AppendRelInfo *appinfo)
Definition: inherit.c:693
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:567
Relids lateral_relids
Definition: pathnodes.h:695
Cost per_tuple
Definition: pathnodes.h:46
static void set_foreign_rel_properties(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:501
uint32 amflags
Definition: pathnodes.h:717
double tuple_fraction
Definition: pathnodes.h:330
struct JoinHashEntry JoinHashEntry
void pfree(void *pointer)
Definition: mcxt.c:1057
List ** partexprs
Definition: pathnodes.h:760
#define linitial(l)
Definition: pg_list.h:174
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:45
static void build_joinrel_tlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *input_rel)
Definition: relnode.c:971
List * list_append_unique_ptr(List *list, void *datum)
Definition: list.c:1208
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1249
List * args
Definition: primnodes.h:1110
Relids min_join_parameterization(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:928
RelOptInfo * build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel, RelOptInfo *inner_rel, RelOptInfo *parent_joinrel, List *restrictlist, SpecialJoinInfo *sjinfo, JoinType jointype)
Definition: relnode.c:784
RelOptInfo * parent
Definition: pathnodes.h:1154
RelOptInfo * join_rel
Definition: relnode.c:38
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1191
#define lfirst_node(type, lc)
Definition: pg_list.h:172
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
double get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel, Path *outer_path, Path *inner_path, SpecialJoinInfo *sjinfo, List *restrict_clauses)
Definition: costsize.c:4857
struct Path * cheapest_total_path
Definition: pathnodes.h:688
static List * build_joinrel_restrictlist(PlannerInfo *root, RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1057
#define PATH_REQ_OUTER(path)
Definition: pathnodes.h:1173
char * c
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
List * joininfo
Definition: pathnodes.h:738
static int match_expr_to_partition_keys(Expr *expr, RelOptInfo *rel, bool strict_op)
Definition: relnode.c:1824
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:567
struct FdwRoutine * fdwroutine
Definition: pathnodes.h:725
int nparts
Definition: pathnodes.h:750
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
Relids relids
Definition: pathnodes.h:670
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Definition: relnode.c:194
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
bool partbounds_merged
Definition: pathnodes.h:754
bool has_relevant_eclass_joinclause(PlannerInfo *root, RelOptInfo *rel1)
Definition: equivclass.c:2875
int simple_rel_array_size
Definition: pathnodes.h:198
List * non_unique_for_rels
Definition: pathnodes.h:731
bool join_clause_is_movable_into(RestrictInfo *rinfo, Relids currentrelids, Relids current_and_outer)
Definition: restrictinfo.c:588
List * ppilist
Definition: pathnodes.h:685
Index relid
Definition: pathnodes.h:698
List * lappend(List *list, void *datum)
Definition: list.c:336
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:205
AppendRelInfo ** find_appinfos_by_relids(PlannerInfo *root, Relids relids, int *nappinfos)
Definition: appendinfo.c:728
Relids lateral_referencers
Definition: pathnodes.h:706
Expr * clause
Definition: pathnodes.h:2006
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
Index varno
Definition: primnodes.h:184
Oid serverid
Definition: pathnodes.h:721
List * exprs
Definition: pathnodes.h:1084
Relids direct_lateral_relids
Definition: pathnodes.h:694
bool consider_partitionwise_join
Definition: pathnodes.h:743
int bitmap_match(const void *key1, const void *key2, Size keysize)
Definition: bitmapset.c:1187
void * palloc0(Size size)
Definition: mcxt.c:981
List * generate_join_implied_equalities_for_ecs(PlannerInfo *root, List *eclasses, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1327
void mark_dummy_rel(RelOptInfo *rel)
Definition: joinrels.c:1261
int rel_parallel_workers
Definition: pathnodes.h:716
List * append_rel_list
Definition: pathnodes.h:284
struct AppendRelInfo ** append_rel_array
Definition: pathnodes.h:213
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
double get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel, List *param_clauses)
Definition: costsize.c:4776
struct PartitionBoundInfoData * boundinfo
Definition: pathnodes.h:753
Size keysize
Definition: hsearch.h:75
RelOptInfo * build_join_rel(PlannerInfo *root, Relids joinrelids, RelOptInfo *outer_rel, RelOptInfo *inner_rel, SpecialJoinInfo *sjinfo, List **restrictlist_ptr)
Definition: relnode.c:576
unsigned int Index
Definition: c.h:549
HashCompareFunc match
Definition: hsearch.h:80
#define PARTITION_STRATEGY_HASH
Definition: parsenodes.h:801
RTEKind rtekind
Definition: pathnodes.h:700
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1242
List * indexlist
Definition: pathnodes.h:707
#define RINFO_IS_PUSHED_DOWN(rinfo, joinrelids)
Definition: pathnodes.h:2083
double rows
Definition: pathnodes.h:673
#define InvalidOid
Definition: postgres_ext.h:36
static void build_join_rel_hash(PlannerInfo *root)
Definition: relnode.c:397
void * fdw_private
Definition: pathnodes.h:726
#define makeNode(_type_)
Definition: nodes.h:581
Relids right_relids
Definition: pathnodes.h:2034
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:689
BlockNumber pages
Definition: pathnodes.h:709
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List ** join_rel_level
Definition: pathnodes.h:250
void setup_simple_rel_arrays(PlannerInfo *root)
Definition: relnode.c:83
List * lateral_vars
Definition: pathnodes.h:705
#define HASH_COMPARE
Definition: hsearch.h:99
JoinType jointype
Definition: pathnodes.h:2200
List * ppi_clauses
Definition: pathnodes.h:1115
struct RelOptInfo ** part_rels
Definition: pathnodes.h:757
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
Oid hashjoinoperator
Definition: pathnodes.h:2064
bool enable_partitionwise_join
Definition: costsize.c:145
QualCost cost
Definition: pathnodes.h:1086
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
struct HTAB * join_rel_hash
Definition: pathnodes.h:241
bool consider_parallel
Definition: pathnodes.h:678
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
Bitmapset * Relids
Definition: pathnodes.h:28
#define nodeTag(nodeptr)
Definition: nodes.h:538
double ppi_rows
Definition: pathnodes.h:1114
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:928
RTEKind rtekind
Definition: parsenodes.h:981
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
AttrNumber max_attr
Definition: pathnodes.h:702
void expand_planner_arrays(PlannerInfo *root, int add_size)
Definition: relnode.c:152
EquivalenceClass * left_ec
Definition: pathnodes.h:2054
static List * subbuild_joinrel_joinlist(RelOptInfo *joinrel, List *joininfo_list, List *new_joininfo)
Definition: relnode.c:1139
#define IS_PARTITIONED_REL(rel)
Definition: pathnodes.h:772
#define elog(elevel,...)
Definition: elog.h:227
static void build_joinrel_partition_info(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, List *restrictlist, JoinType jointype)
Definition: relnode.c:1629
Oid coalescetype
Definition: primnodes.h:1108
uint32 bitmap_hash(const void *key, Size keysize)
Definition: bitmapset.c:1177
void * arg
bool has_useful_pathkeys(PlannerInfo *root, RelOptInfo *rel)
Definition: pathkeys.c:1910
PartitionScheme part_scheme
Definition: pathnodes.h:749
List * pathlist
Definition: pathnodes.h:684
Oid opno
Definition: primnodes.h:537
Relids ppi_req_outer
Definition: pathnodes.h:1113
Index child_relid
Definition: pathnodes.h:2251
ParamPathInfo * get_appendrel_parampathinfo(RelOptInfo *appendrel, Relids required_outer)
Definition: relnode.c:1575
Alias * eref
Definition: parsenodes.h:1120
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:374
List * partition_qual
Definition: pathnodes.h:756
List * args
Definition: primnodes.h:543
Index parent_relid
Definition: pathnodes.h:2250
int32 * attr_widths
Definition: pathnodes.h:704
Definition: regcomp.c:238
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:681
QualCost baserestrictcost
Definition: pathnodes.h:735
static void add_join_rel(PlannerInfo *root, RelOptInfo *joinrel)
Definition: relnode.c:539
List * subplan_params
Definition: pathnodes.h:715
static void build_child_join_reltarget(PlannerInfo *root, RelOptInfo *parentrel, RelOptInfo *childrel, int nappinfos, AppendRelInfo **appinfos)
Definition: relnode.c:2012
Bitmapset * eclass_indexes
Definition: pathnodes.h:712
Oid coalescecollid
Definition: primnodes.h:1109
static void build_joinrel_joinlist(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1087
void get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel)
Definition: plancat.c:113
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
static bool have_partkey_equi_join(RelOptInfo *joinrel, RelOptInfo *rel1, RelOptInfo *rel2, JoinType jointype, List *restrictlist)
Definition: relnode.c:1700
Node * adjust_appendrel_attrs(PlannerInfo *root, Node *node, int nappinfos, AppendRelInfo **appinfos)
Definition: appendinfo.c:194
Relids top_parent_relids
Definition: pathnodes.h:745
bool bms_nonempty_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:545
HashValueFunc hash
Definition: hsearch.h:78
#define HASH_FUNCTION
Definition: hsearch.h:98
List * upper_rels[UPPERREL_FINAL+1]
Definition: pathnodes.h:305
AttrNumber min_attr
Definition: pathnodes.h:701