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