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