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