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