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  appinfos = find_appinfos_by_relids(root, joinrel->relids, &nappinfos);
847 
848  /* Set up reltarget struct */
849  build_child_join_reltarget(root, parent_joinrel, joinrel,
850  nappinfos, appinfos);
851 
852  /* Construct joininfo list. */
853  joinrel->joininfo = (List *) adjust_appendrel_attrs(root,
854  (Node *) parent_joinrel->joininfo,
855  nappinfos,
856  appinfos);
857  pfree(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  return joinrel;
890 }
891 
892 /*
893  * min_join_parameterization
894  *
895  * Determine the minimum possible parameterization of a joinrel, that is, the
896  * set of other rels it contains LATERAL references to. We save this value in
897  * the join's RelOptInfo. This function is split out of build_join_rel()
898  * because join_is_legal() needs the value to check a prospective join.
899  */
900 Relids
902  Relids joinrelids,
903  RelOptInfo *outer_rel,
904  RelOptInfo *inner_rel)
905 {
906  Relids result;
907 
908  /*
909  * Basically we just need the union of the inputs' lateral_relids, less
910  * whatever is already in the join.
911  *
912  * It's not immediately obvious that this is a valid way to compute the
913  * result, because it might seem that we're ignoring possible lateral refs
914  * of PlaceHolderVars that are due to be computed at the join but not in
915  * either input. However, because create_lateral_join_info() already
916  * charged all such PHV refs to each member baserel of the join, they'll
917  * be accounted for already in the inputs' lateral_relids. Likewise, we
918  * do not need to worry about doing transitive closure here, because that
919  * was already accounted for in the original baserel lateral_relids.
920  */
921  result = bms_union(outer_rel->lateral_relids, inner_rel->lateral_relids);
922  result = bms_del_members(result, joinrelids);
923 
924  /* Maintain invariant that result is exactly NULL if empty */
925  if (bms_is_empty(result))
926  result = NULL;
927 
928  return result;
929 }
930 
931 /*
932  * build_joinrel_tlist
933  * Builds a join relation's target list from an input relation.
934  * (This is invoked twice to handle the two input relations.)
935  *
936  * The join's targetlist includes all Vars of its member relations that
937  * will still be needed above the join. This subroutine adds all such
938  * Vars from the specified input rel's tlist to the join rel's tlist.
939  *
940  * We also compute the expected width of the join's output, making use
941  * of data that was cached at the baserel level by set_rel_width().
942  */
943 static void
945  RelOptInfo *input_rel)
946 {
947  Relids relids = joinrel->relids;
948  ListCell *vars;
949 
950  foreach(vars, input_rel->reltarget->exprs)
951  {
952  Var *var = (Var *) lfirst(vars);
953  RelOptInfo *baserel;
954  int ndx;
955 
956  /*
957  * Ignore PlaceHolderVars in the input tlists; we'll make our own
958  * decisions about whether to copy them.
959  */
960  if (IsA(var, PlaceHolderVar))
961  continue;
962 
963  /*
964  * Otherwise, anything in a baserel or joinrel targetlist ought to be
965  * a Var. (More general cases can only appear in appendrel child
966  * rels, which will never be seen here.)
967  */
968  if (!IsA(var, Var))
969  elog(ERROR, "unexpected node type in rel targetlist: %d",
970  (int) nodeTag(var));
971 
972  /* Get the Var's original base rel */
973  baserel = find_base_rel(root, var->varno);
974 
975  /* Is it still needed above this joinrel? */
976  ndx = var->varattno - baserel->min_attr;
977  if (bms_nonempty_difference(baserel->attr_needed[ndx], relids))
978  {
979  /* Yup, add it to the output */
980  joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs, var);
981  /* Vars have cost zero, so no need to adjust reltarget->cost */
982  joinrel->reltarget->width += baserel->attr_widths[ndx];
983  }
984  }
985 }
986 
987 /*
988  * build_joinrel_restrictlist
989  * build_joinrel_joinlist
990  * These routines build lists of restriction and join clauses for a
991  * join relation from the joininfo lists of the relations it joins.
992  *
993  * These routines are separate because the restriction list must be
994  * built afresh for each pair of input sub-relations we consider, whereas
995  * the join list need only be computed once for any join RelOptInfo.
996  * The join list is fully determined by the set of rels making up the
997  * joinrel, so we should get the same results (up to ordering) from any
998  * candidate pair of sub-relations. But the restriction list is whatever
999  * is not handled in the sub-relations, so it depends on which
1000  * sub-relations are considered.
1001  *
1002  * If a join clause from an input relation refers to base rels still not
1003  * present in the joinrel, then it is still a join clause for the joinrel;
1004  * we put it into the joininfo list for the joinrel. Otherwise,
1005  * the clause is now a restrict clause for the joined relation, and we
1006  * return it to the caller of build_joinrel_restrictlist() to be stored in
1007  * join paths made from this pair of sub-relations. (It will not need to
1008  * be considered further up the join tree.)
1009  *
1010  * In many case we will find the same RestrictInfos in both input
1011  * relations' joinlists, so be careful to eliminate duplicates.
1012  * Pointer equality should be a sufficient test for dups, since all
1013  * the various joinlist entries ultimately refer to RestrictInfos
1014  * pushed into them by distribute_restrictinfo_to_rels().
1015  *
1016  * 'joinrel' is a join relation node
1017  * 'outer_rel' and 'inner_rel' are a pair of relations that can be joined
1018  * to form joinrel.
1019  *
1020  * build_joinrel_restrictlist() returns a list of relevant restrictinfos,
1021  * whereas build_joinrel_joinlist() stores its results in the joinrel's
1022  * joininfo list. One or the other must accept each given clause!
1023  *
1024  * NB: Formerly, we made deep(!) copies of each input RestrictInfo to pass
1025  * up to the join relation. I believe this is no longer necessary, because
1026  * RestrictInfo nodes are no longer context-dependent. Instead, just include
1027  * the original nodes in the lists made for the join relation.
1028  */
1029 static List *
1031  RelOptInfo *joinrel,
1032  RelOptInfo *outer_rel,
1033  RelOptInfo *inner_rel)
1034 {
1035  List *result;
1036 
1037  /*
1038  * Collect all the clauses that syntactically belong at this level,
1039  * eliminating any duplicates (important since we will see many of the
1040  * same clauses arriving from both input relations).
1041  */
1042  result = subbuild_joinrel_restrictlist(joinrel, outer_rel->joininfo, NIL);
1043  result = subbuild_joinrel_restrictlist(joinrel, inner_rel->joininfo, result);
1044 
1045  /*
1046  * Add on any clauses derived from EquivalenceClasses. These cannot be
1047  * redundant with the clauses in the joininfo lists, so don't bother
1048  * checking.
1049  */
1050  result = list_concat(result,
1052  joinrel->relids,
1053  outer_rel->relids,
1054  inner_rel));
1055 
1056  return result;
1057 }
1058 
1059 static void
1061  RelOptInfo *outer_rel,
1062  RelOptInfo *inner_rel)
1063 {
1064  List *result;
1065 
1066  /*
1067  * Collect all the clauses that syntactically belong above this level,
1068  * eliminating any duplicates (important since we will see many of the
1069  * same clauses arriving from both input relations).
1070  */
1071  result = subbuild_joinrel_joinlist(joinrel, outer_rel->joininfo, NIL);
1072  result = subbuild_joinrel_joinlist(joinrel, inner_rel->joininfo, result);
1073 
1074  joinrel->joininfo = result;
1075 }
1076 
1077 static List *
1079  List *joininfo_list,
1080  List *new_restrictlist)
1081 {
1082  ListCell *l;
1083 
1084  foreach(l, joininfo_list)
1085  {
1086  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1087 
1088  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1089  {
1090  /*
1091  * This clause becomes a restriction clause for the joinrel, since
1092  * it refers to no outside rels. Add it to the list, being
1093  * careful to eliminate duplicates. (Since RestrictInfo nodes in
1094  * different joinlists will have been multiply-linked rather than
1095  * copied, pointer equality should be a sufficient test.)
1096  */
1097  new_restrictlist = list_append_unique_ptr(new_restrictlist, rinfo);
1098  }
1099  else
1100  {
1101  /*
1102  * This clause is still a join clause at this level, so we ignore
1103  * it in this routine.
1104  */
1105  }
1106  }
1107 
1108  return new_restrictlist;
1109 }
1110 
1111 static List *
1113  List *joininfo_list,
1114  List *new_joininfo)
1115 {
1116  ListCell *l;
1117 
1118  /* Expected to be called only for join between parent relations. */
1119  Assert(joinrel->reloptkind == RELOPT_JOINREL);
1120 
1121  foreach(l, joininfo_list)
1122  {
1123  RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1124 
1125  if (bms_is_subset(rinfo->required_relids, joinrel->relids))
1126  {
1127  /*
1128  * This clause becomes a restriction clause for the joinrel, since
1129  * it refers to no outside rels. So we can ignore it in this
1130  * routine.
1131  */
1132  }
1133  else
1134  {
1135  /*
1136  * This clause is still a join clause at this level, so add it to
1137  * the new joininfo list, being careful to eliminate duplicates.
1138  * (Since RestrictInfo nodes in different joinlists will have been
1139  * multiply-linked rather than copied, pointer equality should be
1140  * a sufficient test.)
1141  */
1142  new_joininfo = list_append_unique_ptr(new_joininfo, rinfo);
1143  }
1144  }
1145 
1146  return new_joininfo;
1147 }
1148 
1149 
1150 /*
1151  * fetch_upper_rel
1152  * Build a RelOptInfo describing some post-scan/join query processing,
1153  * or return a pre-existing one if somebody already built it.
1154  *
1155  * An "upper" relation is identified by an UpperRelationKind and a Relids set.
1156  * The meaning of the Relids set is not specified here, and very likely will
1157  * vary for different relation kinds.
1158  *
1159  * Most of the fields in an upper-level RelOptInfo are not used and are not
1160  * set here (though makeNode should ensure they're zeroes). We basically only
1161  * care about fields that are of interest to add_path() and set_cheapest().
1162  */
1163 RelOptInfo *
1165 {
1166  RelOptInfo *upperrel;
1167  ListCell *lc;
1168 
1169  /*
1170  * For the moment, our indexing data structure is just a List for each
1171  * relation kind. If we ever get so many of one kind that this stops
1172  * working well, we can improve it. No code outside this function should
1173  * assume anything about how to find a particular upperrel.
1174  */
1175 
1176  /* If we already made this upperrel for the query, return it */
1177  foreach(lc, root->upper_rels[kind])
1178  {
1179  upperrel = (RelOptInfo *) lfirst(lc);
1180 
1181  if (bms_equal(upperrel->relids, relids))
1182  return upperrel;
1183  }
1184 
1185  upperrel = makeNode(RelOptInfo);
1186  upperrel->reloptkind = RELOPT_UPPER_REL;
1187  upperrel->relids = bms_copy(relids);
1188 
1189  /* cheap startup cost is interesting iff not all tuples to be retrieved */
1190  upperrel->consider_startup = (root->tuple_fraction > 0);
1191  upperrel->consider_param_startup = false;
1192  upperrel->consider_parallel = false; /* might get changed later */
1193  upperrel->reltarget = create_empty_pathtarget();
1194  upperrel->pathlist = NIL;
1195  upperrel->cheapest_startup_path = NULL;
1196  upperrel->cheapest_total_path = NULL;
1197  upperrel->cheapest_unique_path = NULL;
1198  upperrel->cheapest_parameterized_paths = NIL;
1199 
1200  root->upper_rels[kind] = lappend(root->upper_rels[kind], upperrel);
1201 
1202  return upperrel;
1203 }
1204 
1205 
1206 /*
1207  * find_childrel_parents
1208  * Compute the set of parent relids of an appendrel child rel.
1209  *
1210  * Since appendrels can be nested, a child could have multiple levels of
1211  * appendrel ancestors. This function computes a Relids set of all the
1212  * parent relation IDs.
1213  */
1214 Relids
1216 {
1217  Relids result = NULL;
1218 
1220  Assert(rel->relid > 0 && rel->relid < root->simple_rel_array_size);
1221 
1222  do
1223  {
1224  AppendRelInfo *appinfo = root->append_rel_array[rel->relid];
1225  Index prelid = appinfo->parent_relid;
1226 
1227  result = bms_add_member(result, prelid);
1228 
1229  /* traverse up to the parent rel, loop if it's also a child rel */
1230  rel = find_base_rel(root, prelid);
1231  } while (rel->reloptkind == RELOPT_OTHER_MEMBER_REL);
1232 
1233  Assert(rel->reloptkind == RELOPT_BASEREL);
1234 
1235  return result;
1236 }
1237 
1238 
1239 /*
1240  * get_baserel_parampathinfo
1241  * Get the ParamPathInfo for a parameterized path for a base relation,
1242  * constructing one if we don't have one already.
1243  *
1244  * This centralizes estimating the rowcounts for parameterized paths.
1245  * We need to cache those to be sure we use the same rowcount for all paths
1246  * of the same parameterization for a given rel. This is also a convenient
1247  * place to determine which movable join clauses the parameterized path will
1248  * be responsible for evaluating.
1249  */
1250 ParamPathInfo *
1252  Relids required_outer)
1253 {
1254  ParamPathInfo *ppi;
1255  Relids joinrelids;
1256  List *pclauses;
1257  double rows;
1258  ListCell *lc;
1259 
1260  /* If rel has LATERAL refs, every path for it should account for them */
1261  Assert(bms_is_subset(baserel->lateral_relids, required_outer));
1262 
1263  /* Unparameterized paths have no ParamPathInfo */
1264  if (bms_is_empty(required_outer))
1265  return NULL;
1266 
1267  Assert(!bms_overlap(baserel->relids, required_outer));
1268 
1269  /* If we already have a PPI for this parameterization, just return it */
1270  if ((ppi = find_param_path_info(baserel, required_outer)))
1271  return ppi;
1272 
1273  /*
1274  * Identify all joinclauses that are movable to this base rel given this
1275  * parameterization.
1276  */
1277  joinrelids = bms_union(baserel->relids, required_outer);
1278  pclauses = NIL;
1279  foreach(lc, baserel->joininfo)
1280  {
1281  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1282 
1283  if (join_clause_is_movable_into(rinfo,
1284  baserel->relids,
1285  joinrelids))
1286  pclauses = lappend(pclauses, rinfo);
1287  }
1288 
1289  /*
1290  * Add in joinclauses generated by EquivalenceClasses, too. (These
1291  * necessarily satisfy join_clause_is_movable_into.)
1292  */
1293  pclauses = list_concat(pclauses,
1295  joinrelids,
1296  required_outer,
1297  baserel));
1298 
1299  /* Estimate the number of rows returned by the parameterized scan */
1300  rows = get_parameterized_baserel_size(root, baserel, pclauses);
1301 
1302  /* And now we can build the ParamPathInfo */
1303  ppi = makeNode(ParamPathInfo);
1304  ppi->ppi_req_outer = required_outer;
1305  ppi->ppi_rows = rows;
1306  ppi->ppi_clauses = pclauses;
1307  baserel->ppilist = lappend(baserel->ppilist, ppi);
1308 
1309  return ppi;
1310 }
1311 
1312 /*
1313  * get_joinrel_parampathinfo
1314  * Get the ParamPathInfo for a parameterized path for a join relation,
1315  * constructing one if we don't have one already.
1316  *
1317  * This centralizes estimating the rowcounts for parameterized paths.
1318  * We need to cache those to be sure we use the same rowcount for all paths
1319  * of the same parameterization for a given rel. This is also a convenient
1320  * place to determine which movable join clauses the parameterized path will
1321  * be responsible for evaluating.
1322  *
1323  * outer_path and inner_path are a pair of input paths that can be used to
1324  * construct the join, and restrict_clauses is the list of regular join
1325  * clauses (including clauses derived from EquivalenceClasses) that must be
1326  * applied at the join node when using these inputs.
1327  *
1328  * Unlike the situation for base rels, the set of movable join clauses to be
1329  * enforced at a join varies with the selected pair of input paths, so we
1330  * must calculate that and pass it back, even if we already have a matching
1331  * ParamPathInfo. We handle this by adding any clauses moved down to this
1332  * join to *restrict_clauses, which is an in/out parameter. (The addition
1333  * is done in such a way as to not modify the passed-in List structure.)
1334  *
1335  * Note: when considering a nestloop join, the caller must have removed from
1336  * restrict_clauses any movable clauses that are themselves scheduled to be
1337  * pushed into the right-hand path. We do not do that here since it's
1338  * unnecessary for other join types.
1339  */
1340 ParamPathInfo *
1342  Path *outer_path,
1343  Path *inner_path,
1344  SpecialJoinInfo *sjinfo,
1345  Relids required_outer,
1346  List **restrict_clauses)
1347 {
1348  ParamPathInfo *ppi;
1349  Relids join_and_req;
1350  Relids outer_and_req;
1351  Relids inner_and_req;
1352  List *pclauses;
1353  List *eclauses;
1354  List *dropped_ecs;
1355  double rows;
1356  ListCell *lc;
1357 
1358  /* If rel has LATERAL refs, every path for it should account for them */
1359  Assert(bms_is_subset(joinrel->lateral_relids, required_outer));
1360 
1361  /* Unparameterized paths have no ParamPathInfo or extra join clauses */
1362  if (bms_is_empty(required_outer))
1363  return NULL;
1364 
1365  Assert(!bms_overlap(joinrel->relids, required_outer));
1366 
1367  /*
1368  * Identify all joinclauses that are movable to this join rel given this
1369  * parameterization. These are the clauses that are movable into this
1370  * join, but not movable into either input path. Treat an unparameterized
1371  * input path as not accepting parameterized clauses (because it won't,
1372  * per the shortcut exit above), even though the joinclause movement rules
1373  * might allow the same clauses to be moved into a parameterized path for
1374  * that rel.
1375  */
1376  join_and_req = bms_union(joinrel->relids, required_outer);
1377  if (outer_path->param_info)
1378  outer_and_req = bms_union(outer_path->parent->relids,
1379  PATH_REQ_OUTER(outer_path));
1380  else
1381  outer_and_req = NULL; /* outer path does not accept parameters */
1382  if (inner_path->param_info)
1383  inner_and_req = bms_union(inner_path->parent->relids,
1384  PATH_REQ_OUTER(inner_path));
1385  else
1386  inner_and_req = NULL; /* inner path does not accept parameters */
1387 
1388  pclauses = NIL;
1389  foreach(lc, joinrel->joininfo)
1390  {
1391  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1392 
1393  if (join_clause_is_movable_into(rinfo,
1394  joinrel->relids,
1395  join_and_req) &&
1397  outer_path->parent->relids,
1398  outer_and_req) &&
1400  inner_path->parent->relids,
1401  inner_and_req))
1402  pclauses = lappend(pclauses, rinfo);
1403  }
1404 
1405  /* Consider joinclauses generated by EquivalenceClasses, too */
1406  eclauses = generate_join_implied_equalities(root,
1407  join_and_req,
1408  required_outer,
1409  joinrel);
1410  /* We only want ones that aren't movable to lower levels */
1411  dropped_ecs = NIL;
1412  foreach(lc, eclauses)
1413  {
1414  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1415 
1416  /*
1417  * In principle, join_clause_is_movable_into() should accept anything
1418  * returned by generate_join_implied_equalities(); but because its
1419  * analysis is only approximate, sometimes it doesn't. So we
1420  * currently cannot use this Assert; instead just assume it's okay to
1421  * apply the joinclause at this level.
1422  */
1423 #ifdef NOT_USED
1425  joinrel->relids,
1426  join_and_req));
1427 #endif
1428  if (join_clause_is_movable_into(rinfo,
1429  outer_path->parent->relids,
1430  outer_and_req))
1431  continue; /* drop if movable into LHS */
1432  if (join_clause_is_movable_into(rinfo,
1433  inner_path->parent->relids,
1434  inner_and_req))
1435  {
1436  /* drop if movable into RHS, but remember EC for use below */
1437  Assert(rinfo->left_ec == rinfo->right_ec);
1438  dropped_ecs = lappend(dropped_ecs, rinfo->left_ec);
1439  continue;
1440  }
1441  pclauses = lappend(pclauses, rinfo);
1442  }
1443 
1444  /*
1445  * EquivalenceClasses are harder to deal with than we could wish, because
1446  * of the fact that a given EC can generate different clauses depending on
1447  * context. Suppose we have an EC {X.X, Y.Y, Z.Z} where X and Y are the
1448  * LHS and RHS of the current join and Z is in required_outer, and further
1449  * suppose that the inner_path is parameterized by both X and Z. The code
1450  * above will have produced either Z.Z = X.X or Z.Z = Y.Y from that EC,
1451  * and in the latter case will have discarded it as being movable into the
1452  * RHS. However, the EC machinery might have produced either Y.Y = X.X or
1453  * Y.Y = Z.Z as the EC enforcement clause within the inner_path; it will
1454  * not have produced both, and we can't readily tell from here which one
1455  * it did pick. If we add no clause to this join, we'll end up with
1456  * insufficient enforcement of the EC; either Z.Z or X.X will fail to be
1457  * constrained to be equal to the other members of the EC. (When we come
1458  * to join Z to this X/Y path, we will certainly drop whichever EC clause
1459  * is generated at that join, so this omission won't get fixed later.)
1460  *
1461  * To handle this, for each EC we discarded such a clause from, try to
1462  * generate a clause connecting the required_outer rels to the join's LHS
1463  * ("Z.Z = X.X" in the terms of the above example). If successful, and if
1464  * the clause can't be moved to the LHS, add it to the current join's
1465  * restriction clauses. (If an EC cannot generate such a clause then it
1466  * has nothing that needs to be enforced here, while if the clause can be
1467  * moved into the LHS then it should have been enforced within that path.)
1468  *
1469  * Note that we don't need similar processing for ECs whose clause was
1470  * considered to be movable into the LHS, because the LHS can't refer to
1471  * the RHS so there is no comparable ambiguity about what it might
1472  * actually be enforcing internally.
1473  */
1474  if (dropped_ecs)
1475  {
1476  Relids real_outer_and_req;
1477 
1478  real_outer_and_req = bms_union(outer_path->parent->relids,
1479  required_outer);
1480  eclauses =
1482  dropped_ecs,
1483  real_outer_and_req,
1484  required_outer,
1485  outer_path->parent);
1486  foreach(lc, eclauses)
1487  {
1488  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1489 
1490  /* As above, can't quite assert this here */
1491 #ifdef NOT_USED
1493  outer_path->parent->relids,
1494  real_outer_and_req));
1495 #endif
1496  if (!join_clause_is_movable_into(rinfo,
1497  outer_path->parent->relids,
1498  outer_and_req))
1499  pclauses = lappend(pclauses, rinfo);
1500  }
1501  }
1502 
1503  /*
1504  * Now, attach the identified moved-down clauses to the caller's
1505  * restrict_clauses list. By using list_concat in this order, we leave
1506  * the original list structure of restrict_clauses undamaged.
1507  */
1508  *restrict_clauses = list_concat(pclauses, *restrict_clauses);
1509 
1510  /* If we already have a PPI for this parameterization, just return it */
1511  if ((ppi = find_param_path_info(joinrel, required_outer)))
1512  return ppi;
1513 
1514  /* Estimate the number of rows returned by the parameterized join */
1515  rows = get_parameterized_joinrel_size(root, joinrel,
1516  outer_path,
1517  inner_path,
1518  sjinfo,
1519  *restrict_clauses);
1520 
1521  /*
1522  * And now we can build the ParamPathInfo. No point in saving the
1523  * input-pair-dependent clause list, though.
1524  *
1525  * Note: in GEQO mode, we'll be called in a temporary memory context, but
1526  * the joinrel structure is there too, so no problem.
1527  */
1528  ppi = makeNode(ParamPathInfo);
1529  ppi->ppi_req_outer = required_outer;
1530  ppi->ppi_rows = rows;
1531  ppi->ppi_clauses = NIL;
1532  joinrel->ppilist = lappend(joinrel->ppilist, ppi);
1533 
1534  return ppi;
1535 }
1536 
1537 /*
1538  * get_appendrel_parampathinfo
1539  * Get the ParamPathInfo for a parameterized path for an append relation.
1540  *
1541  * For an append relation, the rowcount estimate will just be the sum of
1542  * the estimates for its children. However, we still need a ParamPathInfo
1543  * to flag the fact that the path requires parameters. So this just creates
1544  * a suitable struct with zero ppi_rows (and no ppi_clauses either, since
1545  * the Append node isn't responsible for checking quals).
1546  */
1547 ParamPathInfo *
1548 get_appendrel_parampathinfo(RelOptInfo *appendrel, Relids required_outer)
1549 {
1550  ParamPathInfo *ppi;
1551 
1552  /* If rel has LATERAL refs, every path for it should account for them */
1553  Assert(bms_is_subset(appendrel->lateral_relids, required_outer));
1554 
1555  /* Unparameterized paths have no ParamPathInfo */
1556  if (bms_is_empty(required_outer))
1557  return NULL;
1558 
1559  Assert(!bms_overlap(appendrel->relids, required_outer));
1560 
1561  /* If we already have a PPI for this parameterization, just return it */
1562  if ((ppi = find_param_path_info(appendrel, required_outer)))
1563  return ppi;
1564 
1565  /* Else build the ParamPathInfo */
1566  ppi = makeNode(ParamPathInfo);
1567  ppi->ppi_req_outer = required_outer;
1568  ppi->ppi_rows = 0;
1569  ppi->ppi_clauses = NIL;
1570  appendrel->ppilist = lappend(appendrel->ppilist, ppi);
1571 
1572  return ppi;
1573 }
1574 
1575 /*
1576  * Returns a ParamPathInfo for the parameterization given by required_outer, if
1577  * already available in the given rel. Returns NULL otherwise.
1578  */
1579 ParamPathInfo *
1581 {
1582  ListCell *lc;
1583 
1584  foreach(lc, rel->ppilist)
1585  {
1586  ParamPathInfo *ppi = (ParamPathInfo *) lfirst(lc);
1587 
1588  if (bms_equal(ppi->ppi_req_outer, required_outer))
1589  return ppi;
1590  }
1591 
1592  return NULL;
1593 }
1594 
1595 /*
1596  * build_joinrel_partition_info
1597  * If the two relations have same partitioning scheme, their join may be
1598  * partitioned and will follow the same partitioning scheme as the joining
1599  * relations. Set the partition scheme and partition key expressions in
1600  * the join relation.
1601  */
1602 static void
1604  RelOptInfo *inner_rel, List *restrictlist,
1605  JoinType jointype)
1606 {
1607  int partnatts;
1608  int cnt;
1609  PartitionScheme part_scheme;
1610 
1611  /* Nothing to do if partitionwise join technique is disabled. */
1613  {
1614  Assert(!IS_PARTITIONED_REL(joinrel));
1615  return;
1616  }
1617 
1618  /*
1619  * We can only consider this join as an input to further partitionwise
1620  * joins if (a) the input relations are partitioned and have
1621  * consider_partitionwise_join=true, (b) the partition schemes match, and
1622  * (c) we can identify an equi-join between the partition keys. Note that
1623  * if it were possible for have_partkey_equi_join to return different
1624  * answers for the same joinrel depending on which join ordering we try
1625  * first, this logic would break. That shouldn't happen, though, because
1626  * of the way the query planner deduces implied equalities and reorders
1627  * the joins. Please see optimizer/README for details.
1628  */
1629  if (!IS_PARTITIONED_REL(outer_rel) || !IS_PARTITIONED_REL(inner_rel) ||
1630  !outer_rel->consider_partitionwise_join ||
1631  !inner_rel->consider_partitionwise_join ||
1632  outer_rel->part_scheme != inner_rel->part_scheme ||
1633  !have_partkey_equi_join(joinrel, outer_rel, inner_rel,
1634  jointype, restrictlist))
1635  {
1636  Assert(!IS_PARTITIONED_REL(joinrel));
1637  return;
1638  }
1639 
1640  part_scheme = outer_rel->part_scheme;
1641 
1642  Assert(REL_HAS_ALL_PART_PROPS(outer_rel) &&
1643  REL_HAS_ALL_PART_PROPS(inner_rel));
1644 
1645  /*
1646  * For now, our partition matching algorithm can match partitions only
1647  * when the partition bounds of the joining relations are exactly same.
1648  * So, bail out otherwise.
1649  */
1650  if (outer_rel->nparts != inner_rel->nparts ||
1651  !partition_bounds_equal(part_scheme->partnatts,
1652  part_scheme->parttyplen,
1653  part_scheme->parttypbyval,
1654  outer_rel->boundinfo, inner_rel->boundinfo))
1655  {
1656  Assert(!IS_PARTITIONED_REL(joinrel));
1657  return;
1658  }
1659 
1660  /*
1661  * This function will be called only once for each joinrel, hence it
1662  * should not have partition scheme, partition bounds, partition key
1663  * expressions and array for storing child relations set.
1664  */
1665  Assert(!joinrel->part_scheme && !joinrel->partexprs &&
1666  !joinrel->nullable_partexprs && !joinrel->part_rels &&
1667  !joinrel->boundinfo);
1668 
1669  /*
1670  * Join relation is partitioned using the same partitioning scheme as the
1671  * joining relations and has same bounds.
1672  */
1673  joinrel->part_scheme = part_scheme;
1674  joinrel->boundinfo = outer_rel->boundinfo;
1675  partnatts = joinrel->part_scheme->partnatts;
1676  joinrel->partexprs = (List **) palloc0(sizeof(List *) * partnatts);
1677  joinrel->nullable_partexprs =
1678  (List **) palloc0(sizeof(List *) * partnatts);
1679  joinrel->nparts = outer_rel->nparts;
1680  joinrel->part_rels =
1681  (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * joinrel->nparts);
1682 
1683  /*
1684  * Set the consider_partitionwise_join flag.
1685  */
1686  Assert(outer_rel->consider_partitionwise_join);
1687  Assert(inner_rel->consider_partitionwise_join);
1688  joinrel->consider_partitionwise_join = true;
1689 
1690  /*
1691  * Construct partition keys for the join.
1692  *
1693  * An INNER join between two partitioned relations can be regarded as
1694  * partitioned by either key expression. For example, A INNER JOIN B ON
1695  * A.a = B.b can be regarded as partitioned on A.a or on B.b; they are
1696  * equivalent.
1697  *
1698  * For a SEMI or ANTI join, the result can only be regarded as being
1699  * partitioned in the same manner as the outer side, since the inner
1700  * columns are not retained.
1701  *
1702  * An OUTER join like (A LEFT JOIN B ON A.a = B.b) may produce rows with
1703  * B.b NULL. These rows may not fit the partitioning conditions imposed on
1704  * B.b. Hence, strictly speaking, the join is not partitioned by B.b and
1705  * thus partition keys of an OUTER join should include partition key
1706  * expressions from the OUTER side only. However, because all
1707  * commonly-used comparison operators are strict, the presence of nulls on
1708  * the outer side doesn't cause any problem; they can't match anything at
1709  * future join levels anyway. Therefore, we track two sets of
1710  * expressions: those that authentically partition the relation
1711  * (partexprs) and those that partition the relation with the exception
1712  * that extra nulls may be present (nullable_partexprs). When the
1713  * comparison operator is strict, the latter is just as good as the
1714  * former.
1715  */
1716  for (cnt = 0; cnt < partnatts; cnt++)
1717  {
1718  /* mark these const to enforce that we copy them properly */
1719  const List *outer_expr = outer_rel->partexprs[cnt];
1720  const List *outer_null_expr = outer_rel->nullable_partexprs[cnt];
1721  const List *inner_expr = inner_rel->partexprs[cnt];
1722  const List *inner_null_expr = inner_rel->nullable_partexprs[cnt];
1723  List *partexpr = NIL;
1724  List *nullable_partexpr = NIL;
1725 
1726  switch (jointype)
1727  {
1728  case JOIN_INNER:
1729  partexpr = list_concat_copy(outer_expr, inner_expr);
1730  nullable_partexpr = list_concat_copy(outer_null_expr,
1731  inner_null_expr);
1732  break;
1733 
1734  case JOIN_SEMI:
1735  case JOIN_ANTI:
1736  partexpr = list_copy(outer_expr);
1737  nullable_partexpr = list_copy(outer_null_expr);
1738  break;
1739 
1740  case JOIN_LEFT:
1741  partexpr = list_copy(outer_expr);
1742  nullable_partexpr = list_concat_copy(inner_expr,
1743  outer_null_expr);
1744  nullable_partexpr = list_concat(nullable_partexpr,
1745  inner_null_expr);
1746  break;
1747 
1748  case JOIN_FULL:
1749  nullable_partexpr = list_concat_copy(outer_expr,
1750  inner_expr);
1751  nullable_partexpr = list_concat(nullable_partexpr,
1752  outer_null_expr);
1753  nullable_partexpr = list_concat(nullable_partexpr,
1754  inner_null_expr);
1755  break;
1756 
1757  default:
1758  elog(ERROR, "unrecognized join type: %d", (int) jointype);
1759 
1760  }
1761 
1762  joinrel->partexprs[cnt] = partexpr;
1763  joinrel->nullable_partexprs[cnt] = nullable_partexpr;
1764  }
1765 }
1766 
1767 /*
1768  * build_child_join_reltarget
1769  * Set up a child-join relation's reltarget from a parent-join relation.
1770  */
1771 static void
1773  RelOptInfo *parentrel,
1774  RelOptInfo *childrel,
1775  int nappinfos,
1776  AppendRelInfo **appinfos)
1777 {
1778  /* Build the targetlist */
1779  childrel->reltarget->exprs = (List *)
1781  (Node *) parentrel->reltarget->exprs,
1782  nappinfos, appinfos);
1783 
1784  /* Set the cost and width fields */
1785  childrel->reltarget->cost.startup = parentrel->reltarget->cost.startup;
1786  childrel->reltarget->cost.per_tuple = parentrel->reltarget->cost.per_tuple;
1787  childrel->reltarget->width = parentrel->reltarget->width;
1788 }
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:1580
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:1251
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:1341
Oid GetUserId(void)
Definition: miscinit.c:380
static List * subbuild_joinrel_restrictlist(RelOptInfo *joinrel, List *joininfo_list, List *new_restrictlist)
Definition: relnode.c:1078
#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:955
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:638
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:346
JoinType
Definition: nodes.h:692
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:201
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:627
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:944
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:901
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:1164
#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:1030
#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:2559
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:709
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:475
HashCompareFunc match
Definition: hsearch.h:75
RTEKind rtekind
Definition: pathnodes.h:671
Relids find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
Definition: relnode.c:1215
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:732
#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:1112
bool partition_bounds_equal(int partnatts, int16 *parttyplen, bool *parttypbyval, PartitionBoundInfo b1, PartitionBoundInfo b2)
Definition: partbounds.c:668
#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:226
static void build_joinrel_partition_info(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel, List *restrictlist, JoinType jointype)
Definition: relnode.c:1603
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:2192
ParamPathInfo * get_appendrel_parampathinfo(RelOptInfo *appendrel, Relids required_outer)
Definition: relnode.c:1548
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:2191
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:1772
Bitmapset * eclass_indexes
Definition: pathnodes.h:683
static void build_joinrel_joinlist(RelOptInfo *joinrel, RelOptInfo *outer_rel, RelOptInfo *inner_rel)
Definition: relnode.c:1060
void get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel)
Definition: plancat.c:114
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:180
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