PostgreSQL Source Code  git master
planmain.h File Reference
#include "nodes/pathnodes.h"
#include "nodes/plannodes.h"
Include dependency graph for planmain.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define DEFAULT_CURSOR_TUPLE_FRACTION   0.1
 

Typedefs

typedef void(* query_pathkeys_callback) (PlannerInfo *root, void *extra)
 

Functions

RelOptInfoquery_planner (PlannerInfo *root, query_pathkeys_callback qp_callback, void *qp_extra)
 
void preprocess_minmax_aggregates (PlannerInfo *root)
 
Plancreate_plan (PlannerInfo *root, Path *best_path)
 
ForeignScanmake_foreignscan (List *qptlist, List *qpqual, Index scanrelid, List *fdw_exprs, List *fdw_private, List *fdw_scan_tlist, List *fdw_recheck_quals, Plan *outer_plan)
 
Planchange_plan_targetlist (Plan *subplan, List *tlist, bool tlist_parallel_safe)
 
Planmaterialize_finished_plan (Plan *subplan)
 
bool is_projection_capable_path (Path *path)
 
bool is_projection_capable_plan (Plan *plan)
 
Sortmake_sort_from_sortclauses (List *sortcls, Plan *lefttree)
 
Aggmake_agg (List *tlist, List *qual, AggStrategy aggstrategy, AggSplit aggsplit, int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations, List *groupingSets, List *chain, double dNumGroups, Size transitionSpace, Plan *lefttree)
 
Limitmake_limit (Plan *lefttree, Node *limitOffset, Node *limitCount, LimitOption limitOption, int uniqNumCols, AttrNumber *uniqColIdx, Oid *uniqOperators, Oid *uniqCollations)
 
void add_base_rels_to_query (PlannerInfo *root, Node *jtnode)
 
void add_other_rels_to_query (PlannerInfo *root)
 
void build_base_rel_tlists (PlannerInfo *root, List *final_tlist)
 
void add_vars_to_targetlist (PlannerInfo *root, List *vars, Relids where_needed, bool create_new_ph)
 
void find_lateral_references (PlannerInfo *root)
 
void create_lateral_join_info (PlannerInfo *root)
 
Listdeconstruct_jointree (PlannerInfo *root)
 
void distribute_restrictinfo_to_rels (PlannerInfo *root, RestrictInfo *restrictinfo)
 
RestrictInfoprocess_implied_equality (PlannerInfo *root, Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level, bool below_outer_join, bool both_const)
 
RestrictInfobuild_implied_join_equality (Oid opno, Oid collation, Expr *item1, Expr *item2, Relids qualscope, Relids nullable_relids, Index security_level)
 
void match_foreign_keys_to_quals (PlannerInfo *root)
 
Listremove_useless_joins (PlannerInfo *root, List *joinlist)
 
void reduce_unique_semijoins (PlannerInfo *root)
 
bool query_supports_distinctness (Query *query)
 
bool query_is_distinct_for (Query *query, List *colnos, List *opids)
 
bool innerrel_is_unique (PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
 
Planset_plan_references (PlannerInfo *root, Plan *plan)
 
void record_plan_function_dependency (PlannerInfo *root, Oid funcid)
 
void record_plan_type_dependency (PlannerInfo *root, Oid typid)
 
bool extract_query_dependencies_walker (Node *node, PlannerInfo *root)
 

Variables

double cursor_tuple_fraction
 
int from_collapse_limit
 
int join_collapse_limit
 

Macro Definition Documentation

◆ DEFAULT_CURSOR_TUPLE_FRACTION

#define DEFAULT_CURSOR_TUPLE_FRACTION   0.1

Definition at line 21 of file planmain.h.

Typedef Documentation

◆ query_pathkeys_callback

typedef void(* query_pathkeys_callback) (PlannerInfo *root, void *extra)

Definition at line 25 of file planmain.h.

Function Documentation

◆ add_base_rels_to_query()

void add_base_rels_to_query ( PlannerInfo root,
Node jtnode 
)

Definition at line 103 of file initsplan.c.

References add_base_rels_to_query(), build_simple_rel(), elog, ERROR, FromExpr::fromlist, IsA, JoinExpr::larg, lfirst, nodeTag, and JoinExpr::rarg.

Referenced by add_base_rels_to_query(), and query_planner().

104 {
105  if (jtnode == NULL)
106  return;
107  if (IsA(jtnode, RangeTblRef))
108  {
109  int varno = ((RangeTblRef *) jtnode)->rtindex;
110 
111  (void) build_simple_rel(root, varno, NULL);
112  }
113  else if (IsA(jtnode, FromExpr))
114  {
115  FromExpr *f = (FromExpr *) jtnode;
116  ListCell *l;
117 
118  foreach(l, f->fromlist)
119  add_base_rels_to_query(root, lfirst(l));
120  }
121  else if (IsA(jtnode, JoinExpr))
122  {
123  JoinExpr *j = (JoinExpr *) jtnode;
124 
125  add_base_rels_to_query(root, j->larg);
126  add_base_rels_to_query(root, j->rarg);
127  }
128  else
129  elog(ERROR, "unrecognized node type: %d",
130  (int) nodeTag(jtnode));
131 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
void add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
Definition: initsplan.c:103
List * fromlist
Definition: primnodes.h:1525
Node * larg
Definition: primnodes.h:1505
#define ERROR
Definition: elog.h:43
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Definition: relnode.c:194
Node * rarg
Definition: primnodes.h:1506
#define lfirst(lc)
Definition: pg_list.h:169
#define nodeTag(nodeptr)
Definition: nodes.h:532
#define elog(elevel,...)
Definition: elog.h:228

◆ add_other_rels_to_query()

void add_other_rels_to_query ( PlannerInfo root)

Definition at line 141 of file initsplan.c.

References expand_inherited_rtentry(), RangeTblEntry::inh, RELOPT_BASEREL, RelOptInfo::reloptkind, PlannerInfo::simple_rel_array, PlannerInfo::simple_rel_array_size, and PlannerInfo::simple_rte_array.

Referenced by query_planner().

142 {
143  int rti;
144 
145  for (rti = 1; rti < root->simple_rel_array_size; rti++)
146  {
147  RelOptInfo *rel = root->simple_rel_array[rti];
148  RangeTblEntry *rte = root->simple_rte_array[rti];
149 
150  /* there may be empty slots corresponding to non-baserel RTEs */
151  if (rel == NULL)
152  continue;
153 
154  /* Ignore any "otherrels" that were already added. */
155  if (rel->reloptkind != RELOPT_BASEREL)
156  continue;
157 
158  /* If it's marked as inheritable, look for children. */
159  if (rte->inh)
160  expand_inherited_rtentry(root, rel, rte, rti);
161  }
162 }
RelOptKind reloptkind
Definition: pathnodes.h:663
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:197
int simple_rel_array_size
Definition: pathnodes.h:198
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:205
void expand_inherited_rtentry(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte, Index rti)
Definition: inherit.c:79

◆ add_vars_to_targetlist()

void add_vars_to_targetlist ( PlannerInfo root,
List vars,
Relids  where_needed,
bool  create_new_ph 
)

Definition at line 227 of file initsplan.c.

References Assert, RelOptInfo::attr_needed, bms_add_members(), bms_is_empty(), bms_is_subset(), copyObject, elog, ERROR, PathTarget::exprs, find_base_rel(), find_placeholder_info(), IsA, lappend(), lfirst, RelOptInfo::min_attr, nodeTag, PlaceHolderInfo::ph_needed, RelOptInfo::relids, RelOptInfo::reltarget, Var::varattno, and Var::varno.

Referenced by build_base_rel_tlists(), distribute_qual_to_rels(), expand_inherited_rtentry(), extract_lateral_references(), fix_placeholder_input_needed_levels(), generate_base_implied_equalities_no_const(), and process_implied_equality().

229 {
230  ListCell *temp;
231 
232  Assert(!bms_is_empty(where_needed));
233 
234  foreach(temp, vars)
235  {
236  Node *node = (Node *) lfirst(temp);
237 
238  if (IsA(node, Var))
239  {
240  Var *var = (Var *) node;
241  RelOptInfo *rel = find_base_rel(root, var->varno);
242  int attno = var->varattno;
243 
244  if (bms_is_subset(where_needed, rel->relids))
245  continue;
246  Assert(attno >= rel->min_attr && attno <= rel->max_attr);
247  attno -= rel->min_attr;
248  if (rel->attr_needed[attno] == NULL)
249  {
250  /* Variable not yet requested, so add to rel's targetlist */
251  /* XXX is copyObject necessary here? */
252  rel->reltarget->exprs = lappend(rel->reltarget->exprs,
253  copyObject(var));
254  /* reltarget cost and width will be computed later */
255  }
256  rel->attr_needed[attno] = bms_add_members(rel->attr_needed[attno],
257  where_needed);
258  }
259  else if (IsA(node, PlaceHolderVar))
260  {
261  PlaceHolderVar *phv = (PlaceHolderVar *) node;
262  PlaceHolderInfo *phinfo = find_placeholder_info(root, phv,
263  create_new_ph);
264 
265  phinfo->ph_needed = bms_add_members(phinfo->ph_needed,
266  where_needed);
267  }
268  else
269  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
270  }
271 }
Relids ph_needed
Definition: pathnodes.h:2319
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
Relids * attr_needed
Definition: pathnodes.h:699
Definition: nodes.h:527
AttrNumber varattno
Definition: primnodes.h:186
Definition: primnodes.h:181
#define ERROR
Definition: elog.h:43
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv, bool create_new_ph)
Definition: placeholder.c:69
Relids relids
Definition: pathnodes.h:666
List * lappend(List *list, void *datum)
Definition: list.c:321
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
Index varno
Definition: primnodes.h:184
List * exprs
Definition: pathnodes.h:1078
#define Assert(condition)
Definition: c.h:800
#define lfirst(lc)
Definition: pg_list.h:169
#define nodeTag(nodeptr)
Definition: nodes.h:532
#define elog(elevel,...)
Definition: elog.h:228
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:373
#define copyObject(obj)
Definition: nodes.h:643
struct PathTarget * reltarget
Definition: pathnodes.h:677
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793
AttrNumber min_attr
Definition: pathnodes.h:697

◆ build_base_rel_tlists()

void build_base_rel_tlists ( PlannerInfo root,
List final_tlist 
)

Definition at line 180 of file initsplan.c.

References add_vars_to_targetlist(), bms_make_singleton(), Query::havingQual, list_free(), NIL, PlannerInfo::parse, pull_var_clause(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, and PVC_RECURSE_WINDOWFUNCS.

Referenced by query_planner().

181 {
182  List *tlist_vars = pull_var_clause((Node *) final_tlist,
186 
187  if (tlist_vars != NIL)
188  {
189  add_vars_to_targetlist(root, tlist_vars, bms_make_singleton(0), true);
190  list_free(tlist_vars);
191  }
192 
193  /*
194  * If there's a HAVING clause, we'll need the Vars it uses, too. Note
195  * that HAVING can contain Aggrefs but not WindowFuncs.
196  */
197  if (root->parse->havingQual)
198  {
199  List *having_vars = pull_var_clause(root->parse->havingQual,
202 
203  if (having_vars != NIL)
204  {
205  add_vars_to_targetlist(root, having_vars,
206  bms_make_singleton(0), true);
207  list_free(having_vars);
208  }
209  }
210 }
#define NIL
Definition: pg_list.h:65
Query * parse
Definition: pathnodes.h:173
Definition: nodes.h:527
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed, bool create_new_ph)
Definition: initsplan.c:227
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:181
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:180
void list_free(List *list)
Definition: list.c:1376
Node * havingQual
Definition: parsenodes.h:152
Definition: pg_list.h:50
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:178

◆ build_implied_join_equality()

RestrictInfo* build_implied_join_equality ( Oid  opno,
Oid  collation,
Expr item1,
Expr item2,
Relids  qualscope,
Relids  nullable_relids,
Index  security_level 
)

Definition at line 2410 of file initsplan.c.

References check_hashjoinable(), check_mergejoinable(), copyObject, InvalidOid, make_opclause(), and make_restrictinfo().

Referenced by create_join_clause(), reconsider_full_join_clause(), and reconsider_outer_join_clause().

2417 {
2418  RestrictInfo *restrictinfo;
2419  Expr *clause;
2420 
2421  /*
2422  * Build the new clause. Copy to ensure it shares no substructure with
2423  * original (this is necessary in case there are subselects in there...)
2424  */
2425  clause = make_opclause(opno,
2426  BOOLOID, /* opresulttype */
2427  false, /* opretset */
2428  copyObject(item1),
2429  copyObject(item2),
2430  InvalidOid,
2431  collation);
2432 
2433  /*
2434  * Build the RestrictInfo node itself.
2435  */
2436  restrictinfo = make_restrictinfo(clause,
2437  true, /* is_pushed_down */
2438  false, /* outerjoin_delayed */
2439  false, /* pseudoconstant */
2440  security_level, /* security_level */
2441  qualscope, /* required_relids */
2442  NULL, /* outer_relids */
2443  nullable_relids); /* nullable_relids */
2444 
2445  /* Set mergejoinability/hashjoinability flags */
2446  check_mergejoinable(restrictinfo);
2447  check_hashjoinable(restrictinfo);
2448 
2449  return restrictinfo;
2450 }
RestrictInfo * make_restrictinfo(Expr *clause, bool is_pushed_down, bool outerjoin_delayed, bool pseudoconstant, Index security_level, Relids required_relids, Relids outer_relids, Relids nullable_relids)
Definition: restrictinfo.c:59
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: makefuncs.c:610
#define InvalidOid
Definition: postgres_ext.h:36
static void check_mergejoinable(RestrictInfo *restrictinfo)
Definition: initsplan.c:2638
static void check_hashjoinable(RestrictInfo *restrictinfo)
Definition: initsplan.c:2675
#define copyObject(obj)
Definition: nodes.h:643

◆ change_plan_targetlist()

Plan* change_plan_targetlist ( Plan subplan,
List tlist,
bool  tlist_parallel_safe 
)

Definition at line 1954 of file createplan.c.

References inject_projection_plan(), is_projection_capable_plan(), Plan::parallel_safe, Plan::targetlist, and tlist_same_exprs().

Referenced by create_unique_plan(), and postgresGetForeignPlan().

1955 {
1956  /*
1957  * If the top plan node can't do projections and its existing target list
1958  * isn't already what we need, we need to add a Result node to help it
1959  * along.
1960  */
1961  if (!is_projection_capable_plan(subplan) &&
1962  !tlist_same_exprs(tlist, subplan->targetlist))
1963  subplan = inject_projection_plan(subplan, tlist,
1964  subplan->parallel_safe &&
1965  tlist_parallel_safe);
1966  else
1967  {
1968  /* Else we can just replace the plan node's tlist */
1969  subplan->targetlist = tlist;
1970  subplan->parallel_safe &= tlist_parallel_safe;
1971  }
1972  return subplan;
1973 }
bool is_projection_capable_plan(Plan *plan)
Definition: createplan.c:6970
static Plan * inject_projection_plan(Plan *subplan, List *tlist, bool parallel_safe)
Definition: createplan.c:1922
bool tlist_same_exprs(List *tlist1, List *tlist2)
Definition: tlist.c:240
List * targetlist
Definition: plannodes.h:136
bool parallel_safe
Definition: plannodes.h:130

◆ create_lateral_join_info()

void create_lateral_join_info ( PlannerInfo root)

Definition at line 447 of file initsplan.c.

References Assert, bms_add_member(), bms_add_members(), bms_copy(), bms_get_singleton_member(), bms_is_empty(), bms_is_member(), bms_next_member(), RelOptInfo::direct_lateral_relids, find_base_rel(), find_placeholder_info(), PlannerInfo::hasLateralRTEs, IsA, RelOptInfo::lateral_referencers, RelOptInfo::lateral_relids, RelOptInfo::lateral_vars, lfirst, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_lateral, PlannerInfo::placeholder_list, RelOptInfo::relid, RELOPT_BASEREL, RelOptInfo::reloptkind, PlannerInfo::simple_rel_array, PlannerInfo::simple_rel_array_size, and Var::varno.

Referenced by query_planner().

448 {
449  bool found_laterals = false;
450  Index rti;
451  ListCell *lc;
452 
453  /* We need do nothing if the query contains no LATERAL RTEs */
454  if (!root->hasLateralRTEs)
455  return;
456 
457  /*
458  * Examine all baserels (the rel array has been set up by now).
459  */
460  for (rti = 1; rti < root->simple_rel_array_size; rti++)
461  {
462  RelOptInfo *brel = root->simple_rel_array[rti];
463  Relids lateral_relids;
464 
465  /* there may be empty slots corresponding to non-baserel RTEs */
466  if (brel == NULL)
467  continue;
468 
469  Assert(brel->relid == rti); /* sanity check on array */
470 
471  /* ignore RTEs that are "other rels" */
472  if (brel->reloptkind != RELOPT_BASEREL)
473  continue;
474 
475  lateral_relids = NULL;
476 
477  /* consider each laterally-referenced Var or PHV */
478  foreach(lc, brel->lateral_vars)
479  {
480  Node *node = (Node *) lfirst(lc);
481 
482  if (IsA(node, Var))
483  {
484  Var *var = (Var *) node;
485 
486  found_laterals = true;
487  lateral_relids = bms_add_member(lateral_relids,
488  var->varno);
489  }
490  else if (IsA(node, PlaceHolderVar))
491  {
492  PlaceHolderVar *phv = (PlaceHolderVar *) node;
493  PlaceHolderInfo *phinfo = find_placeholder_info(root, phv,
494  false);
495 
496  found_laterals = true;
497  lateral_relids = bms_add_members(lateral_relids,
498  phinfo->ph_eval_at);
499  }
500  else
501  Assert(false);
502  }
503 
504  /* We now have all the simple lateral refs from this rel */
505  brel->direct_lateral_relids = lateral_relids;
506  brel->lateral_relids = bms_copy(lateral_relids);
507  }
508 
509  /*
510  * Now check for lateral references within PlaceHolderVars, and mark their
511  * eval_at rels as having lateral references to the source rels.
512  *
513  * For a PHV that is due to be evaluated at a baserel, mark its source(s)
514  * as direct lateral dependencies of the baserel (adding onto the ones
515  * recorded above). If it's due to be evaluated at a join, mark its
516  * source(s) as indirect lateral dependencies of each baserel in the join,
517  * ie put them into lateral_relids but not direct_lateral_relids. This is
518  * appropriate because we can't put any such baserel on the outside of a
519  * join to one of the PHV's lateral dependencies, but on the other hand we
520  * also can't yet join it directly to the dependency.
521  */
522  foreach(lc, root->placeholder_list)
523  {
524  PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
525  Relids eval_at = phinfo->ph_eval_at;
526  int varno;
527 
528  if (phinfo->ph_lateral == NULL)
529  continue; /* PHV is uninteresting if no lateral refs */
530 
531  found_laterals = true;
532 
533  if (bms_get_singleton_member(eval_at, &varno))
534  {
535  /* Evaluation site is a baserel */
536  RelOptInfo *brel = find_base_rel(root, varno);
537 
538  brel->direct_lateral_relids =
540  phinfo->ph_lateral);
541  brel->lateral_relids =
543  phinfo->ph_lateral);
544  }
545  else
546  {
547  /* Evaluation site is a join */
548  varno = -1;
549  while ((varno = bms_next_member(eval_at, varno)) >= 0)
550  {
551  RelOptInfo *brel = find_base_rel(root, varno);
552 
554  phinfo->ph_lateral);
555  }
556  }
557  }
558 
559  /*
560  * If we found no actual lateral references, we're done; but reset the
561  * hasLateralRTEs flag to avoid useless work later.
562  */
563  if (!found_laterals)
564  {
565  root->hasLateralRTEs = false;
566  return;
567  }
568 
569  /*
570  * Calculate the transitive closure of the lateral_relids sets, so that
571  * they describe both direct and indirect lateral references. If relation
572  * X references Y laterally, and Y references Z laterally, then we will
573  * have to scan X on the inside of a nestloop with Z, so for all intents
574  * and purposes X is laterally dependent on Z too.
575  *
576  * This code is essentially Warshall's algorithm for transitive closure.
577  * The outer loop considers each baserel, and propagates its lateral
578  * dependencies to those baserels that have a lateral dependency on it.
579  */
580  for (rti = 1; rti < root->simple_rel_array_size; rti++)
581  {
582  RelOptInfo *brel = root->simple_rel_array[rti];
583  Relids outer_lateral_relids;
584  Index rti2;
585 
586  if (brel == NULL || brel->reloptkind != RELOPT_BASEREL)
587  continue;
588 
589  /* need not consider baserel further if it has no lateral refs */
590  outer_lateral_relids = brel->lateral_relids;
591  if (outer_lateral_relids == NULL)
592  continue;
593 
594  /* else scan all baserels */
595  for (rti2 = 1; rti2 < root->simple_rel_array_size; rti2++)
596  {
597  RelOptInfo *brel2 = root->simple_rel_array[rti2];
598 
599  if (brel2 == NULL || brel2->reloptkind != RELOPT_BASEREL)
600  continue;
601 
602  /* if brel2 has lateral ref to brel, propagate brel's refs */
603  if (bms_is_member(rti, brel2->lateral_relids))
605  outer_lateral_relids);
606  }
607  }
608 
609  /*
610  * Now that we've identified all lateral references, mark each baserel
611  * with the set of relids of rels that reference it laterally (possibly
612  * indirectly) --- that is, the inverse mapping of lateral_relids.
613  */
614  for (rti = 1; rti < root->simple_rel_array_size; rti++)
615  {
616  RelOptInfo *brel = root->simple_rel_array[rti];
617  Relids lateral_relids;
618  int rti2;
619 
620  if (brel == NULL || brel->reloptkind != RELOPT_BASEREL)
621  continue;
622 
623  /* Nothing to do at rels with no lateral refs */
624  lateral_relids = brel->lateral_relids;
625  if (lateral_relids == NULL)
626  continue;
627 
628  /*
629  * We should not have broken the invariant that lateral_relids is
630  * exactly NULL if empty.
631  */
632  Assert(!bms_is_empty(lateral_relids));
633 
634  /* Also, no rel should have a lateral dependency on itself */
635  Assert(!bms_is_member(rti, lateral_relids));
636 
637  /* Mark this rel's referencees */
638  rti2 = -1;
639  while ((rti2 = bms_next_member(lateral_relids, rti2)) >= 0)
640  {
641  RelOptInfo *brel2 = root->simple_rel_array[rti2];
642 
643  Assert(brel2 != NULL && brel2->reloptkind == RELOPT_BASEREL);
644  brel2->lateral_referencers =
645  bms_add_member(brel2->lateral_referencers, rti);
646  }
647  }
648 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
Relids ph_eval_at
Definition: pathnodes.h:2317
RelOptKind reloptkind
Definition: pathnodes.h:663
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Definition: nodes.h:527
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:615
Definition: primnodes.h:181
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:197
Relids lateral_relids
Definition: pathnodes.h:691
bool hasLateralRTEs
Definition: pathnodes.h:340
PlaceHolderInfo * find_placeholder_info(PlannerInfo *root, PlaceHolderVar *phv, bool create_new_ph)
Definition: placeholder.c:69
int simple_rel_array_size
Definition: pathnodes.h:198
Index relid
Definition: pathnodes.h:694
Relids lateral_referencers
Definition: pathnodes.h:702
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
Index varno
Definition: primnodes.h:184
Relids direct_lateral_relids
Definition: pathnodes.h:690
Relids ph_lateral
Definition: pathnodes.h:2318
unsigned int Index
Definition: c.h:537
#define Assert(condition)
Definition: c.h:800
#define lfirst(lc)
Definition: pg_list.h:169
List * lateral_vars
Definition: pathnodes.h:701
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
List * placeholder_list
Definition: pathnodes.h:288
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:373
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793

◆ create_plan()

Plan* create_plan ( PlannerInfo root,
Path best_path 
)

Definition at line 321 of file createplan.c.

References apply_tlist_labeling(), Assert, CP_EXACT_TLIST, create_plan_recurse(), PlannerInfo::curOuterParams, PlannerInfo::curOuterRels, elog, ERROR, IsA, NIL, PlannerInfo::plan_params, PlannerInfo::processed_tlist, SS_attach_initplans(), and Plan::targetlist.

Referenced by create_minmaxagg_plan(), create_subqueryscan_plan(), make_subplan(), SS_process_ctes(), and standard_planner().

322 {
323  Plan *plan;
324 
325  /* plan_params should not be in use in current query level */
326  Assert(root->plan_params == NIL);
327 
328  /* Initialize this module's workspace in PlannerInfo */
329  root->curOuterRels = NULL;
330  root->curOuterParams = NIL;
331 
332  /* Recursively process the path tree, demanding the correct tlist result */
333  plan = create_plan_recurse(root, best_path, CP_EXACT_TLIST);
334 
335  /*
336  * Make sure the topmost plan node's targetlist exposes the original
337  * column names and other decorative info. Targetlists generated within
338  * the planner don't bother with that stuff, but we must have it on the
339  * top-level tlist seen at execution time. However, ModifyTable plan
340  * nodes don't have a tlist matching the querytree targetlist.
341  */
342  if (!IsA(plan, ModifyTable))
344 
345  /*
346  * Attach any initPlans created in this query level to the topmost plan
347  * node. (In principle the initplans could go in any plan node at or
348  * above where they're referenced, but there seems no reason to put them
349  * any lower than the topmost node for the query level. Also, see
350  * comments for SS_finalize_plan before you try to change this.)
351  */
352  SS_attach_initplans(root, plan);
353 
354  /* Check we successfully assigned all NestLoopParams to plan nodes */
355  if (root->curOuterParams != NIL)
356  elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
357 
358  /*
359  * Reset plan_params to ensure param IDs used for nestloop params are not
360  * re-used later
361  */
362  root->plan_params = NIL;
363 
364  return plan;
365 }
#define NIL
Definition: pg_list.h:65
void apply_tlist_labeling(List *dest_tlist, List *src_tlist)
Definition: tlist.c:340
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
List * plan_params
Definition: pathnodes.h:187
Relids curOuterRels
Definition: pathnodes.h:361
static Plan * create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
Definition: createplan.c:372
#define ERROR
Definition: elog.h:43
List * curOuterParams
Definition: pathnodes.h:362
#define Assert(condition)
Definition: c.h:800
void SS_attach_initplans(PlannerInfo *root, Plan *plan)
Definition: subselect.c:2184
List * targetlist
Definition: plannodes.h:136
#define elog(elevel,...)
Definition: elog.h:228
List * processed_tlist
Definition: pathnodes.h:319
#define CP_EXACT_TLIST
Definition: createplan.c:68

◆ deconstruct_jointree()

List* deconstruct_jointree ( PlannerInfo root)

Definition at line 684 of file initsplan.c.

References Assert, deconstruct_recurse(), IsA, Query::jointree, NIL, PlannerInfo::nullable_baserels, and PlannerInfo::parse.

Referenced by query_planner().

685 {
686  List *result;
687  Relids qualscope;
688  Relids inner_join_rels;
689  List *postponed_qual_list = NIL;
690 
691  /* Start recursion at top of jointree */
692  Assert(root->parse->jointree != NULL &&
693  IsA(root->parse->jointree, FromExpr));
694 
695  /* this is filled as we scan the jointree */
696  root->nullable_baserels = NULL;
697 
698  result = deconstruct_recurse(root, (Node *) root->parse->jointree, false,
699  &qualscope, &inner_join_rels,
700  &postponed_qual_list);
701 
702  /* Shouldn't be any leftover quals */
703  Assert(postponed_qual_list == NIL);
704 
705  return result;
706 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
Query * parse
Definition: pathnodes.h:173
FromExpr * jointree
Definition: parsenodes.h:138
Definition: nodes.h:527
#define Assert(condition)
Definition: c.h:800
static List * deconstruct_recurse(PlannerInfo *root, Node *jtnode, bool below_outer_join, Relids *qualscope, Relids *inner_join_rels, List **postponed_qual_list)
Definition: initsplan.c:730
Relids nullable_baserels
Definition: pathnodes.h:229
Definition: pg_list.h:50

◆ distribute_restrictinfo_to_rels()

void distribute_restrictinfo_to_rels ( PlannerInfo root,
RestrictInfo restrictinfo 
)

Definition at line 2173 of file initsplan.c.

References add_join_clause_to_rels(), RelOptInfo::baserestrict_min_security, RelOptInfo::baserestrictinfo, bms_membership(), BMS_MULTIPLE, BMS_SINGLETON, bms_singleton_member(), check_hashjoinable(), elog, ERROR, find_base_rel(), lappend(), Min, PostponedQual::relids, RestrictInfo::required_relids, and RestrictInfo::security_level.

Referenced by distribute_qual_to_rels(), generate_base_implied_equalities_broken(), generate_base_implied_equalities_const(), process_implied_equality(), reconsider_outer_join_clauses(), and remove_rel_from_query().

2175 {
2176  Relids relids = restrictinfo->required_relids;
2177  RelOptInfo *rel;
2178 
2179  switch (bms_membership(relids))
2180  {
2181  case BMS_SINGLETON:
2182 
2183  /*
2184  * There is only one relation participating in the clause, so it
2185  * is a restriction clause for that relation.
2186  */
2187  rel = find_base_rel(root, bms_singleton_member(relids));
2188 
2189  /* Add clause to rel's restriction list */
2191  restrictinfo);
2192  /* Update security level info */
2194  restrictinfo->security_level);
2195  break;
2196  case BMS_MULTIPLE:
2197 
2198  /*
2199  * The clause is a join clause, since there is more than one rel
2200  * in its relid set.
2201  */
2202 
2203  /*
2204  * Check for hashjoinable operators. (We don't bother setting the
2205  * hashjoin info except in true join clauses.)
2206  */
2207  check_hashjoinable(restrictinfo);
2208 
2209  /*
2210  * Add clause to the join lists of all the relevant relations.
2211  */
2212  add_join_clause_to_rels(root, restrictinfo, relids);
2213  break;
2214  default:
2215 
2216  /*
2217  * clause references no rels, and therefore we have no place to
2218  * attach it. Shouldn't get here if callers are working properly.
2219  */
2220  elog(ERROR, "cannot cope with variable-free clause");
2221  break;
2222  }
2223 }
Index security_level
Definition: pathnodes.h:2006
Relids required_relids
Definition: pathnodes.h:2012
List * baserestrictinfo
Definition: pathnodes.h:728
#define Min(x, y)
Definition: c.h:982
Index baserestrict_min_security
Definition: pathnodes.h:730
#define ERROR
Definition: elog.h:43
List * lappend(List *list, void *datum)
Definition: list.c:321
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:577
void add_join_clause_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo, Relids join_relids)
Definition: joininfo.c:95
static void check_hashjoinable(RestrictInfo *restrictinfo)
Definition: initsplan.c:2675
#define elog(elevel,...)
Definition: elog.h:228
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:373

◆ extract_query_dependencies_walker()

bool extract_query_dependencies_walker ( Node node,
PlannerInfo root 
)

Definition at line 3006 of file setrefs.c.

References Assert, CMD_UTILITY, Query::commandType, PlannerGlobal::dependsOnRole, expression_tree_walker(), extract_query_dependencies_walker(), fix_expr_common(), PlannerInfo::glob, Query::hasRowSecurity, IsA, lappend_oid(), lfirst, OidIsValid, query_tree_walker(), PlannerGlobal::relationOids, RangeTblEntry::relid, Query::rtable, RTE_NAMEDTUPLESTORE, RTE_RELATION, RangeTblEntry::rtekind, UtilityContainsQuery(), and Query::utilityStmt.

Referenced by expression_planner_with_deps(), extract_query_dependencies(), and extract_query_dependencies_walker().

3007 {
3008  if (node == NULL)
3009  return false;
3010  Assert(!IsA(node, PlaceHolderVar));
3011  if (IsA(node, Query))
3012  {
3013  Query *query = (Query *) node;
3014  ListCell *lc;
3015 
3016  if (query->commandType == CMD_UTILITY)
3017  {
3018  /*
3019  * Ignore utility statements, except those (such as EXPLAIN) that
3020  * contain a parsed-but-not-planned query.
3021  */
3022  query = UtilityContainsQuery(query->utilityStmt);
3023  if (query == NULL)
3024  return false;
3025  }
3026 
3027  /* Remember if any Query has RLS quals applied by rewriter */
3028  if (query->hasRowSecurity)
3029  context->glob->dependsOnRole = true;
3030 
3031  /* Collect relation OIDs in this Query's rtable */
3032  foreach(lc, query->rtable)
3033  {
3034  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
3035 
3036  if (rte->rtekind == RTE_RELATION)
3037  context->glob->relationOids =
3038  lappend_oid(context->glob->relationOids, rte->relid);
3039  else if (rte->rtekind == RTE_NAMEDTUPLESTORE &&
3040  OidIsValid(rte->relid))
3041  context->glob->relationOids =
3042  lappend_oid(context->glob->relationOids,
3043  rte->relid);
3044  }
3045 
3046  /* And recurse into the query's subexpressions */
3048  (void *) context, 0);
3049  }
3050  /* Extract function dependencies and check for regclass Consts */
3051  fix_expr_common(context, node);
3053  (void *) context);
3054 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2322
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
Node * utilityStmt
Definition: parsenodes.h:120
List * lappend_oid(List *list, Oid datum)
Definition: list.c:357
#define OidIsValid(objectId)
Definition: c.h:706
Query * UtilityContainsQuery(Node *parsetree)
Definition: utility.c:2105
List * rtable
Definition: parsenodes.h:137
bool extract_query_dependencies_walker(Node *node, PlannerInfo *context)
Definition: setrefs.c:3006
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1598
CmdType commandType
Definition: parsenodes.h:112
#define Assert(condition)
Definition: c.h:800
#define lfirst(lc)
Definition: pg_list.h:169
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1888
RTEKind rtekind
Definition: parsenodes.h:982
bool hasRowSecurity
Definition: parsenodes.h:133

◆ find_lateral_references()

void find_lateral_references ( PlannerInfo root)

Definition at line 301 of file initsplan.c.

References Assert, extract_lateral_references(), PlannerInfo::hasLateralRTEs, RelOptInfo::relid, RELOPT_BASEREL, RelOptInfo::reloptkind, PlannerInfo::simple_rel_array, and PlannerInfo::simple_rel_array_size.

Referenced by query_planner().

302 {
303  Index rti;
304 
305  /* We need do nothing if the query contains no LATERAL RTEs */
306  if (!root->hasLateralRTEs)
307  return;
308 
309  /*
310  * Examine all baserels (the rel array has been set up by now).
311  */
312  for (rti = 1; rti < root->simple_rel_array_size; rti++)
313  {
314  RelOptInfo *brel = root->simple_rel_array[rti];
315 
316  /* there may be empty slots corresponding to non-baserel RTEs */
317  if (brel == NULL)
318  continue;
319 
320  Assert(brel->relid == rti); /* sanity check on array */
321 
322  /*
323  * This bit is less obvious than it might look. We ignore appendrel
324  * otherrels and consider only their parent baserels. In a case where
325  * a LATERAL-containing UNION ALL subquery was pulled up, it is the
326  * otherrel that is actually going to be in the plan. However, we
327  * want to mark all its lateral references as needed by the parent,
328  * because it is the parent's relid that will be used for join
329  * planning purposes. And the parent's RTE will contain all the
330  * lateral references we need to know, since the pulled-up member is
331  * nothing but a copy of parts of the original RTE's subquery. We
332  * could visit the parent's children instead and transform their
333  * references back to the parent's relid, but it would be much more
334  * complicated for no real gain. (Important here is that the child
335  * members have not yet received any processing beyond being pulled
336  * up.) Similarly, in appendrels created by inheritance expansion,
337  * it's sufficient to look at the parent relation.
338  */
339 
340  /* ignore RTEs that are "other rels" */
341  if (brel->reloptkind != RELOPT_BASEREL)
342  continue;
343 
344  extract_lateral_references(root, brel, rti);
345  }
346 }
RelOptKind reloptkind
Definition: pathnodes.h:663
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:197
bool hasLateralRTEs
Definition: pathnodes.h:340
int simple_rel_array_size
Definition: pathnodes.h:198
Index relid
Definition: pathnodes.h:694
unsigned int Index
Definition: c.h:537
#define Assert(condition)
Definition: c.h:800
static void extract_lateral_references(PlannerInfo *root, RelOptInfo *brel, Index rtindex)
Definition: initsplan.c:349

◆ innerrel_is_unique()

bool innerrel_is_unique ( PlannerInfo root,
Relids  joinrelids,
Relids  outerrelids,
RelOptInfo innerrel,
JoinType  jointype,
List restrictlist,
bool  force_cache 
)

Definition at line 964 of file analyzejoins.c.

References bms_copy(), bms_is_subset(), is_innerrel_unique_for(), PlannerInfo::join_search_private, lappend(), lfirst, MemoryContextSwitchTo(), NIL, RelOptInfo::non_unique_for_rels, PlannerInfo::planner_cxt, rel_supports_distinctness(), and RelOptInfo::unique_for_rels.

Referenced by add_paths_to_joinrel(), and reduce_unique_semijoins().

971 {
972  MemoryContext old_context;
973  ListCell *lc;
974 
975  /* Certainly can't prove uniqueness when there are no joinclauses */
976  if (restrictlist == NIL)
977  return false;
978 
979  /*
980  * Make a quick check to eliminate cases in which we will surely be unable
981  * to prove uniqueness of the innerrel.
982  */
983  if (!rel_supports_distinctness(root, innerrel))
984  return false;
985 
986  /*
987  * Query the cache to see if we've managed to prove that innerrel is
988  * unique for any subset of this outerrel. We don't need an exact match,
989  * as extra outerrels can't make the innerrel any less unique (or more
990  * formally, the restrictlist for a join to a superset outerrel must be a
991  * superset of the conditions we successfully used before).
992  */
993  foreach(lc, innerrel->unique_for_rels)
994  {
995  Relids unique_for_rels = (Relids) lfirst(lc);
996 
997  if (bms_is_subset(unique_for_rels, outerrelids))
998  return true; /* Success! */
999  }
1000 
1001  /*
1002  * Conversely, we may have already determined that this outerrel, or some
1003  * superset thereof, cannot prove this innerrel to be unique.
1004  */
1005  foreach(lc, innerrel->non_unique_for_rels)
1006  {
1007  Relids unique_for_rels = (Relids) lfirst(lc);
1008 
1009  if (bms_is_subset(outerrelids, unique_for_rels))
1010  return false;
1011  }
1012 
1013  /* No cached information, so try to make the proof. */
1014  if (is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel,
1015  jointype, restrictlist))
1016  {
1017  /*
1018  * Cache the positive result for future probes, being sure to keep it
1019  * in the planner_cxt even if we are working in GEQO.
1020  *
1021  * Note: one might consider trying to isolate the minimal subset of
1022  * the outerrels that proved the innerrel unique. But it's not worth
1023  * the trouble, because the planner builds up joinrels incrementally
1024  * and so we'll see the minimally sufficient outerrels before any
1025  * supersets of them anyway.
1026  */
1027  old_context = MemoryContextSwitchTo(root->planner_cxt);
1028  innerrel->unique_for_rels = lappend(innerrel->unique_for_rels,
1029  bms_copy(outerrelids));
1030  MemoryContextSwitchTo(old_context);
1031 
1032  return true; /* Success! */
1033  }
1034  else
1035  {
1036  /*
1037  * None of the join conditions for outerrel proved innerrel unique, so
1038  * we can safely reject this outerrel or any subset of it in future
1039  * checks.
1040  *
1041  * However, in normal planning mode, caching this knowledge is totally
1042  * pointless; it won't be queried again, because we build up joinrels
1043  * from smaller to larger. It is useful in GEQO mode, where the
1044  * knowledge can be carried across successive planning attempts; and
1045  * it's likely to be useful when using join-search plugins, too. Hence
1046  * cache when join_search_private is non-NULL. (Yeah, that's a hack,
1047  * but it seems reasonable.)
1048  *
1049  * Also, allow callers to override that heuristic and force caching;
1050  * that's useful for reduce_unique_semijoins, which calls here before
1051  * the normal join search starts.
1052  */
1053  if (force_cache || root->join_search_private)
1054  {
1055  old_context = MemoryContextSwitchTo(root->planner_cxt);
1056  innerrel->non_unique_for_rels =
1057  lappend(innerrel->non_unique_for_rels,
1058  bms_copy(outerrelids));
1059  MemoryContextSwitchTo(old_context);
1060  }
1061 
1062  return false;
1063  }
1064 }
static bool is_innerrel_unique_for(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist)
#define NIL
Definition: pg_list.h:65
List * unique_for_rels
Definition: pathnodes.h:723
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
void * join_search_private
Definition: pathnodes.h:365
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
List * non_unique_for_rels
Definition: pathnodes.h:725
List * lappend(List *list, void *datum)
Definition: list.c:321
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * Relids
Definition: pathnodes.h:28
MemoryContext planner_cxt
Definition: pathnodes.h:325
static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel)
Definition: analyzejoins.c:585

◆ is_projection_capable_path()

bool is_projection_capable_path ( Path path)

Definition at line 6925 of file createplan.c.

References IS_DUMMY_APPEND, Path::pathtype, T_Append, T_Hash, T_IncrementalSort, T_Limit, T_LockRows, T_Material, T_MergeAppend, T_ModifyTable, T_ProjectSet, T_RecursiveUnion, T_SetOp, T_Sort, and T_Unique.

Referenced by apply_projection_to_path(), create_projection_path(), and create_projection_plan().

6926 {
6927  /* Most plan types can project, so just list the ones that can't */
6928  switch (path->pathtype)
6929  {
6930  case T_Hash:
6931  case T_Material:
6932  case T_Sort:
6933  case T_IncrementalSort:
6934  case T_Unique:
6935  case T_SetOp:
6936  case T_LockRows:
6937  case T_Limit:
6938  case T_ModifyTable:
6939  case T_MergeAppend:
6940  case T_RecursiveUnion:
6941  return false;
6942  case T_Append:
6943 
6944  /*
6945  * Append can't project, but if an AppendPath is being used to
6946  * represent a dummy path, what will actually be generated is a
6947  * Result which can project.
6948  */
6949  return IS_DUMMY_APPEND(path);
6950  case T_ProjectSet:
6951 
6952  /*
6953  * Although ProjectSet certainly projects, say "no" because we
6954  * don't want the planner to randomly replace its tlist with
6955  * something else; the SRFs have to stay at top level. This might
6956  * get relaxed later.
6957  */
6958  return false;
6959  default:
6960  break;
6961  }
6962  return true;
6963 }
Definition: nodes.h:81
Definition: nodes.h:49
Definition: nodes.h:76
NodeTag pathtype
Definition: pathnodes.h:1146
Definition: nodes.h:84
#define IS_DUMMY_APPEND(p)
Definition: pathnodes.h:1415
Definition: nodes.h:85
Definition: nodes.h:87

◆ is_projection_capable_plan()

bool is_projection_capable_plan ( Plan plan)

Definition at line 6970 of file createplan.c.

References nodeTag, T_Append, T_Hash, T_Limit, T_LockRows, T_Material, T_MergeAppend, T_ModifyTable, T_ProjectSet, T_RecursiveUnion, T_SetOp, T_Sort, and T_Unique.

Referenced by change_plan_targetlist(), and prepare_sort_from_pathkeys().

6971 {
6972  /* Most plan types can project, so just list the ones that can't */
6973  switch (nodeTag(plan))
6974  {
6975  case T_Hash:
6976  case T_Material:
6977  case T_Sort:
6978  case T_Unique:
6979  case T_SetOp:
6980  case T_LockRows:
6981  case T_Limit:
6982  case T_ModifyTable:
6983  case T_Append:
6984  case T_MergeAppend:
6985  case T_RecursiveUnion:
6986  return false;
6987  case T_ProjectSet:
6988 
6989  /*
6990  * Although ProjectSet certainly projects, say "no" because we
6991  * don't want the planner to randomly replace its tlist with
6992  * something else; the SRFs have to stay at top level. This might
6993  * get relaxed later.
6994  */
6995  return false;
6996  default:
6997  break;
6998  }
6999  return true;
7000 }
Definition: nodes.h:81
Definition: nodes.h:49
Definition: nodes.h:76
Definition: nodes.h:84
#define nodeTag(nodeptr)
Definition: nodes.h:532
Definition: nodes.h:85
Definition: nodes.h:87

◆ make_agg()

Agg* make_agg ( List tlist,
List qual,
AggStrategy  aggstrategy,
AggSplit  aggsplit,
int  numGroupCols,
AttrNumber grpColIdx,
Oid grpOperators,
Oid grpCollations,
List groupingSets,
List chain,
double  dNumGroups,
Size  transitionSpace,
Plan lefttree 
)

Definition at line 6347 of file createplan.c.

References Agg::aggParams, Agg::aggsplit, Agg::aggstrategy, Agg::chain, Agg::groupingSets, Agg::grpColIdx, Agg::grpCollations, Agg::grpOperators, Plan::lefttree, makeNode, Min, Agg::numCols, Agg::numGroups, Agg::plan, Plan::qual, Plan::righttree, Plan::targetlist, and Agg::transitionSpace.

Referenced by create_agg_plan(), create_groupingsets_plan(), and create_unique_plan().

6352 {
6353  Agg *node = makeNode(Agg);
6354  Plan *plan = &node->plan;
6355  long numGroups;
6356 
6357  /* Reduce to long, but 'ware overflow! */
6358  numGroups = (long) Min(dNumGroups, (double) LONG_MAX);
6359 
6360  node->aggstrategy = aggstrategy;
6361  node->aggsplit = aggsplit;
6362  node->numCols = numGroupCols;
6363  node->grpColIdx = grpColIdx;
6364  node->grpOperators = grpOperators;
6365  node->grpCollations = grpCollations;
6366  node->numGroups = numGroups;
6367  node->transitionSpace = transitionSpace;
6368  node->aggParams = NULL; /* SS_finalize_plan() will fill this */
6369  node->groupingSets = groupingSets;
6370  node->chain = chain;
6371 
6372  plan->qual = qual;
6373  plan->targetlist = tlist;
6374  plan->lefttree = lefttree;
6375  plan->righttree = NULL;
6376 
6377  return node;
6378 }
int numCols
Definition: plannodes.h:821
List * qual
Definition: plannodes.h:137
AttrNumber * grpColIdx
Definition: plannodes.h:822
uint64 transitionSpace
Definition: plannodes.h:826
Oid * grpCollations
Definition: plannodes.h:824
#define Min(x, y)
Definition: c.h:982
struct Plan * righttree
Definition: plannodes.h:139
AggStrategy aggstrategy
Definition: plannodes.h:819
Bitmapset * aggParams
Definition: plannodes.h:827
Plan plan
Definition: plannodes.h:818
List * groupingSets
Definition: plannodes.h:829
#define makeNode(_type_)
Definition: nodes.h:575
AggSplit aggsplit
Definition: plannodes.h:820
long numGroups
Definition: plannodes.h:825
struct Plan * lefttree
Definition: plannodes.h:138
List * targetlist
Definition: plannodes.h:136
Oid * grpOperators
Definition: plannodes.h:823
List * chain
Definition: plannodes.h:830
Definition: plannodes.h:816

◆ make_foreignscan()

ForeignScan* make_foreignscan ( List qptlist,
List qpqual,
Index  scanrelid,
List fdw_exprs,
List fdw_private,
List fdw_scan_tlist,
List fdw_recheck_quals,
Plan outer_plan 
)

Definition at line 5513 of file createplan.c.

References CMD_SELECT, ForeignScan::fdw_exprs, ForeignScan::fdw_private, ForeignScan::fdw_recheck_quals, ForeignScan::fdw_scan_tlist, ForeignScan::fs_relids, ForeignScan::fs_server, ForeignScan::fsSystemCol, InvalidOid, Plan::lefttree, makeNode, ForeignScan::operation, Scan::plan, Plan::qual, ForeignScan::resultRelation, Plan::righttree, ForeignScan::scan, Scan::scanrelid, and Plan::targetlist.

Referenced by fileGetForeignPlan(), and postgresGetForeignPlan().

5521 {
5522  ForeignScan *node = makeNode(ForeignScan);
5523  Plan *plan = &node->scan.plan;
5524 
5525  /* cost will be filled in by create_foreignscan_plan */
5526  plan->targetlist = qptlist;
5527  plan->qual = qpqual;
5528  plan->lefttree = outer_plan;
5529  plan->righttree = NULL;
5530  node->scan.scanrelid = scanrelid;
5531 
5532  /* these may be overridden by the FDW's PlanDirectModify callback. */
5533  node->operation = CMD_SELECT;
5534  node->resultRelation = 0;
5535 
5536  /* fs_server will be filled in by create_foreignscan_plan */
5537  node->fs_server = InvalidOid;
5538  node->fdw_exprs = fdw_exprs;
5539  node->fdw_private = fdw_private;
5540  node->fdw_scan_tlist = fdw_scan_tlist;
5541  node->fdw_recheck_quals = fdw_recheck_quals;
5542  /* fs_relids will be filled in by create_foreignscan_plan */
5543  node->fs_relids = NULL;
5544  /* fsSystemCol will be filled in by create_foreignscan_plan */
5545  node->fsSystemCol = false;
5546 
5547  return node;
5548 }
List * qual
Definition: plannodes.h:137
Plan plan
Definition: plannodes.h:336
Index scanrelid
Definition: plannodes.h:337
Oid fs_server
Definition: plannodes.h:616
List * fdw_exprs
Definition: plannodes.h:617
List * fdw_private
Definition: plannodes.h:618
List * fdw_scan_tlist
Definition: plannodes.h:619
CmdType operation
Definition: plannodes.h:614
struct Plan * righttree
Definition: plannodes.h:139
List * fdw_recheck_quals
Definition: plannodes.h:620
Index resultRelation
Definition: plannodes.h:615
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:575
struct Plan * lefttree
Definition: plannodes.h:138
List * targetlist
Definition: plannodes.h:136
bool fsSystemCol
Definition: plannodes.h:622
Bitmapset * fs_relids
Definition: plannodes.h:621

◆ make_limit()

Limit* make_limit ( Plan lefttree,
Node limitOffset,
Node limitCount,
LimitOption  limitOption,
int  uniqNumCols,
AttrNumber uniqColIdx,
Oid uniqOperators,
Oid uniqCollations 
)

Definition at line 6711 of file createplan.c.

References Plan::lefttree, Limit::limitCount, Limit::limitOffset, Limit::limitOption, makeNode, NIL, Limit::plan, Plan::qual, Plan::righttree, Plan::targetlist, Limit::uniqColIdx, Limit::uniqCollations, Limit::uniqNumCols, and Limit::uniqOperators.

Referenced by create_limit_plan(), and create_minmaxagg_plan().

6714 {
6715  Limit *node = makeNode(Limit);
6716  Plan *plan = &node->plan;
6717 
6718  plan->targetlist = lefttree->targetlist;
6719  plan->qual = NIL;
6720  plan->lefttree = lefttree;
6721  plan->righttree = NULL;
6722 
6723  node->limitOffset = limitOffset;
6724  node->limitCount = limitCount;
6725  node->limitOption = limitOption;
6726  node->uniqNumCols = uniqNumCols;
6727  node->uniqColIdx = uniqColIdx;
6728  node->uniqOperators = uniqOperators;
6729  node->uniqCollations = uniqCollations;
6730 
6731  return node;
6732 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:137
Plan plan
Definition: plannodes.h:982
Oid * uniqOperators
Definition: plannodes.h:988
Node * limitOffset
Definition: plannodes.h:983
Oid * uniqCollations
Definition: plannodes.h:989
struct Plan * righttree
Definition: plannodes.h:139
LimitOption limitOption
Definition: plannodes.h:985
Node * limitCount
Definition: plannodes.h:984
int uniqNumCols
Definition: plannodes.h:986
#define makeNode(_type_)
Definition: nodes.h:575
struct Plan * lefttree
Definition: plannodes.h:138
List * targetlist
Definition: plannodes.h:136
AttrNumber * uniqColIdx
Definition: plannodes.h:987

◆ make_sort_from_sortclauses()

Sort* make_sort_from_sortclauses ( List sortcls,
Plan lefttree 
)

Definition at line 6201 of file createplan.c.

References TargetEntry::expr, exprCollation(), get_sortgroupclause_tle(), lfirst, list_length(), make_sort(), SortGroupClause::nulls_first, palloc(), TargetEntry::resno, SortGroupClause::sortop, and Plan::targetlist.

Referenced by create_unique_plan().

6202 {
6203  List *sub_tlist = lefttree->targetlist;
6204  ListCell *l;
6205  int numsortkeys;
6206  AttrNumber *sortColIdx;
6207  Oid *sortOperators;
6208  Oid *collations;
6209  bool *nullsFirst;
6210 
6211  /* Convert list-ish representation to arrays wanted by executor */
6212  numsortkeys = list_length(sortcls);
6213  sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
6214  sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
6215  collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
6216  nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
6217 
6218  numsortkeys = 0;
6219  foreach(l, sortcls)
6220  {
6221  SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
6222  TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
6223 
6224  sortColIdx[numsortkeys] = tle->resno;
6225  sortOperators[numsortkeys] = sortcl->sortop;
6226  collations[numsortkeys] = exprCollation((Node *) tle->expr);
6227  nullsFirst[numsortkeys] = sortcl->nulls_first;
6228  numsortkeys++;
6229  }
6230 
6231  return make_sort(lefttree, numsortkeys,
6232  sortColIdx, sortOperators,
6233  collations, nullsFirst);
6234 }
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:389
Definition: nodes.h:527
unsigned int Oid
Definition: postgres_ext.h:31
AttrNumber resno
Definition: primnodes.h:1423
static Sort * make_sort(Plan *lefttree, int numCols, AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst)
Definition: createplan.c:5757
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1422
static int list_length(const List *l)
Definition: pg_list.h:149
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:768
List * targetlist
Definition: plannodes.h:136
void * palloc(Size size)
Definition: mcxt.c:950
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21

◆ match_foreign_keys_to_quals()

void match_foreign_keys_to_quals ( PlannerInfo root)

Definition at line 2468 of file initsplan.c.

References OpExpr::args, RestrictInfo::clause, ForeignKeyOptInfo::con_relid, ForeignKeyOptInfo::confkey, ForeignKeyOptInfo::conkey, ForeignKeyOptInfo::conpfeqop, EquivalenceClass::ec_has_const, PlannerInfo::fkey_list, get_commutator(), get_leftop(), get_rightop(), InvalidOid, IsA, RelOptInfo::joininfo, lappend(), lfirst, list_length(), match_eclasses_to_foreign_key_col(), ForeignKeyOptInfo::nconst_ec, NIL, ForeignKeyOptInfo::nkeys, ForeignKeyOptInfo::nmatched_ec, ForeignKeyOptInfo::nmatched_rcols, ForeignKeyOptInfo::nmatched_ri, OidIsValid, OpExpr::opno, RestrictInfo::outerjoin_delayed, ForeignKeyOptInfo::ref_relid, RELOPT_BASEREL, RelOptInfo::reloptkind, ForeignKeyOptInfo::rinfos, PlannerInfo::simple_rel_array, and PlannerInfo::simple_rel_array_size.

Referenced by query_planner().

2469 {
2470  List *newlist = NIL;
2471  ListCell *lc;
2472 
2473  foreach(lc, root->fkey_list)
2474  {
2475  ForeignKeyOptInfo *fkinfo = (ForeignKeyOptInfo *) lfirst(lc);
2476  RelOptInfo *con_rel;
2477  RelOptInfo *ref_rel;
2478  int colno;
2479 
2480  /*
2481  * Either relid might identify a rel that is in the query's rtable but
2482  * isn't referenced by the jointree so won't have a RelOptInfo. Hence
2483  * don't use find_base_rel() here. We can ignore such FKs.
2484  */
2485  if (fkinfo->con_relid >= root->simple_rel_array_size ||
2486  fkinfo->ref_relid >= root->simple_rel_array_size)
2487  continue; /* just paranoia */
2488  con_rel = root->simple_rel_array[fkinfo->con_relid];
2489  if (con_rel == NULL)
2490  continue;
2491  ref_rel = root->simple_rel_array[fkinfo->ref_relid];
2492  if (ref_rel == NULL)
2493  continue;
2494 
2495  /*
2496  * Ignore FK unless both rels are baserels. This gets rid of FKs that
2497  * link to inheritance child rels (otherrels) and those that link to
2498  * rels removed by join removal (dead rels).
2499  */
2500  if (con_rel->reloptkind != RELOPT_BASEREL ||
2501  ref_rel->reloptkind != RELOPT_BASEREL)
2502  continue;
2503 
2504  /*
2505  * Scan the columns and try to match them to eclasses and quals.
2506  *
2507  * Note: for simple inner joins, any match should be in an eclass.
2508  * "Loose" quals that syntactically match an FK equality must have
2509  * been rejected for EC status because they are outer-join quals or
2510  * similar. We can still consider them to match the FK if they are
2511  * not outerjoin_delayed.
2512  */
2513  for (colno = 0; colno < fkinfo->nkeys; colno++)
2514  {
2515  EquivalenceClass *ec;
2516  AttrNumber con_attno,
2517  ref_attno;
2518  Oid fpeqop;
2519  ListCell *lc2;
2520 
2521  ec = match_eclasses_to_foreign_key_col(root, fkinfo, colno);
2522  /* Don't bother looking for loose quals if we got an EC match */
2523  if (ec != NULL)
2524  {
2525  fkinfo->nmatched_ec++;
2526  if (ec->ec_has_const)
2527  fkinfo->nconst_ec++;
2528  continue;
2529  }
2530 
2531  /*
2532  * Scan joininfo list for relevant clauses. Either rel's joininfo
2533  * list would do equally well; we use con_rel's.
2534  */
2535  con_attno = fkinfo->conkey[colno];
2536  ref_attno = fkinfo->confkey[colno];
2537  fpeqop = InvalidOid; /* we'll look this up only if needed */
2538 
2539  foreach(lc2, con_rel->joininfo)
2540  {
2541  RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc2);
2542  OpExpr *clause = (OpExpr *) rinfo->clause;
2543  Var *leftvar;
2544  Var *rightvar;
2545 
2546  /* Ignore outerjoin-delayed clauses */
2547  if (rinfo->outerjoin_delayed)
2548  continue;
2549 
2550  /* Only binary OpExprs are useful for consideration */
2551  if (!IsA(clause, OpExpr) ||
2552  list_length(clause->args) != 2)
2553  continue;
2554  leftvar = (Var *) get_leftop((Expr *) clause);
2555  rightvar = (Var *) get_rightop((Expr *) clause);
2556 
2557  /* Operands must be Vars, possibly with RelabelType */
2558  while (leftvar && IsA(leftvar, RelabelType))
2559  leftvar = (Var *) ((RelabelType *) leftvar)->arg;
2560  if (!(leftvar && IsA(leftvar, Var)))
2561  continue;
2562  while (rightvar && IsA(rightvar, RelabelType))
2563  rightvar = (Var *) ((RelabelType *) rightvar)->arg;
2564  if (!(rightvar && IsA(rightvar, Var)))
2565  continue;
2566 
2567  /* Now try to match the vars to the current foreign key cols */
2568  if (fkinfo->ref_relid == leftvar->varno &&
2569  ref_attno == leftvar->varattno &&
2570  fkinfo->con_relid == rightvar->varno &&
2571  con_attno == rightvar->varattno)
2572  {
2573  /* Vars match, but is it the right operator? */
2574  if (clause->opno == fkinfo->conpfeqop[colno])
2575  {
2576  fkinfo->rinfos[colno] = lappend(fkinfo->rinfos[colno],
2577  rinfo);
2578  fkinfo->nmatched_ri++;
2579  }
2580  }
2581  else if (fkinfo->ref_relid == rightvar->varno &&
2582  ref_attno == rightvar->varattno &&
2583  fkinfo->con_relid == leftvar->varno &&
2584  con_attno == leftvar->varattno)
2585  {
2586  /*
2587  * Reverse match, must check commutator operator. Look it
2588  * up if we didn't already. (In the worst case we might
2589  * do multiple lookups here, but that would require an FK
2590  * equality operator without commutator, which is
2591  * unlikely.)
2592  */
2593  if (!OidIsValid(fpeqop))
2594  fpeqop = get_commutator(fkinfo->conpfeqop[colno]);
2595  if (clause->opno == fpeqop)
2596  {
2597  fkinfo->rinfos[colno] = lappend(fkinfo->rinfos[colno],
2598  rinfo);
2599  fkinfo->nmatched_ri++;
2600  }
2601  }
2602  }
2603  /* If we found any matching loose quals, count col as matched */
2604  if (fkinfo->rinfos[colno])
2605  fkinfo->nmatched_rcols++;
2606  }
2607 
2608  /*
2609  * Currently, we drop multicolumn FKs that aren't fully matched to the
2610  * query. Later we might figure out how to derive some sort of
2611  * estimate from them, in which case this test should be weakened to
2612  * "if ((fkinfo->nmatched_ec + fkinfo->nmatched_rcols) > 0)".
2613  */
2614  if ((fkinfo->nmatched_ec + fkinfo->nmatched_rcols) == fkinfo->nkeys)
2615  newlist = lappend(newlist, fkinfo);
2616  }
2617  /* Replace fkey_list, thereby discarding any useless entries */
2618  root->fkey_list = newlist;
2619 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1426
RelOptKind reloptkind
Definition: pathnodes.h:663
unsigned int Oid
Definition: postgres_ext.h:31
Definition: primnodes.h:181
List * fkey_list
Definition: pathnodes.h:290
#define OidIsValid(objectId)
Definition: c.h:706
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:197
Oid conpfeqop[INDEX_MAX_KEYS]
Definition: pathnodes.h:890
bool outerjoin_delayed
Definition: pathnodes.h:1998
List * joininfo
Definition: pathnodes.h:732
static Node * get_leftop(const void *clause)
Definition: nodeFuncs.h:73
int simple_rel_array_size
Definition: pathnodes.h:198
List * lappend(List *list, void *datum)
Definition: list.c:321
Expr * clause
Definition: pathnodes.h:1994
AttrNumber conkey[INDEX_MAX_KEYS]
Definition: pathnodes.h:888
EquivalenceClass * match_eclasses_to_foreign_key_col(PlannerInfo *root, ForeignKeyOptInfo *fkinfo, int colno)
Definition: equivclass.c:2262
static Node * get_rightop(const void *clause)
Definition: nodeFuncs.h:85
#define InvalidOid
Definition: postgres_ext.h:36
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
Oid opno
Definition: primnodes.h:528
List * args
Definition: primnodes.h:534
Definition: pg_list.h:50
AttrNumber confkey[INDEX_MAX_KEYS]
Definition: pathnodes.h:889
int16 AttrNumber
Definition: attnum.h:21
List * rinfos[INDEX_MAX_KEYS]
Definition: pathnodes.h:902

◆ materialize_finished_plan()

Plan* materialize_finished_plan ( Plan subplan)

Definition at line 6313 of file createplan.c.

References cost_material(), Plan::initPlan, make_material(), NIL, Plan::parallel_aware, Plan::parallel_safe, Plan::plan_rows, Plan::plan_width, Plan::startup_cost, Path::startup_cost, Plan::total_cost, and Path::total_cost.

Referenced by build_subplan(), and standard_planner().

6314 {
6315  Plan *matplan;
6316  Path matpath; /* dummy for result of cost_material */
6317 
6318  matplan = (Plan *) make_material(subplan);
6319 
6320  /*
6321  * XXX horrid kluge: if there are any initPlans attached to the subplan,
6322  * move them up to the Material node, which is now effectively the top
6323  * plan node in its query level. This prevents failure in
6324  * SS_finalize_plan(), which see for comments. We don't bother adjusting
6325  * the subplan's cost estimate for this.
6326  */
6327  matplan->initPlan = subplan->initPlan;
6328  subplan->initPlan = NIL;
6329 
6330  /* Set cost data */
6331  cost_material(&matpath,
6332  subplan->startup_cost,
6333  subplan->total_cost,
6334  subplan->plan_rows,
6335  subplan->plan_width);
6336  matplan->startup_cost = matpath.startup_cost;
6337  matplan->total_cost = matpath.total_cost;
6338  matplan->plan_rows = subplan->plan_rows;
6339  matplan->plan_width = subplan->plan_width;
6340  matplan->parallel_aware = false;
6341  matplan->parallel_safe = subplan->parallel_safe;
6342 
6343  return matplan;
6344 }
#define NIL
Definition: pg_list.h:65
double plan_rows
Definition: plannodes.h:123
static Material * make_material(Plan *lefttree)
Definition: createplan.c:6291
Cost startup_cost
Definition: pathnodes.h:1159
Cost startup_cost
Definition: plannodes.h:117
bool parallel_aware
Definition: plannodes.h:129
Cost total_cost
Definition: pathnodes.h:1160
void cost_material(Path *path, Cost input_startup_cost, Cost input_total_cost, double tuples, int width)
Definition: costsize.c:2266
int plan_width
Definition: plannodes.h:124
List * initPlan
Definition: plannodes.h:140
Cost total_cost
Definition: plannodes.h:118
bool parallel_safe
Definition: plannodes.h:130

◆ preprocess_minmax_aggregates()

void preprocess_minmax_aggregates ( PlannerInfo root)

Definition at line 73 of file planagg.c.

References add_path(), MinMaxAggInfo::aggsortop, Assert, build_minmax_path(), can_minmax_aggs(), create_minmaxagg_path(), create_pathtarget, Query::cteList, elog, ERROR, exprCollation(), exprType(), fetch_upper_rel(), FromExpr::fromlist, get_equality_op_for_ordering_op(), Query::groupClause, Query::groupingSets, Query::hasAggs, Query::hasWindowFuncs, Query::havingQual, RangeTblEntry::inh, IsA, Query::jointree, lfirst, linitial, list_length(), PlannerInfo::minmax_aggs, NIL, OidIsValid, MinMaxAggInfo::param, parse(), PlannerInfo::parse, planner_rt_fetch, PlannerInfo::processed_tlist, Query::rowMarks, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, RangeTblRef::rtindex, Query::setOperations, SS_make_initplan_output_param(), MinMaxAggInfo::target, and UPPERREL_GROUP_AGG.

Referenced by grouping_planner().

74 {
75  Query *parse = root->parse;
76  FromExpr *jtnode;
77  RangeTblRef *rtr;
78  RangeTblEntry *rte;
79  List *aggs_list;
80  RelOptInfo *grouped_rel;
81  ListCell *lc;
82 
83  /* minmax_aggs list should be empty at this point */
84  Assert(root->minmax_aggs == NIL);
85 
86  /* Nothing to do if query has no aggregates */
87  if (!parse->hasAggs)
88  return;
89 
90  Assert(!parse->setOperations); /* shouldn't get here if a setop */
91  Assert(parse->rowMarks == NIL); /* nor if FOR UPDATE */
92 
93  /*
94  * Reject unoptimizable cases.
95  *
96  * We don't handle GROUP BY or windowing, because our current
97  * implementations of grouping require looking at all the rows anyway, and
98  * so there's not much point in optimizing MIN/MAX.
99  */
100  if (parse->groupClause || list_length(parse->groupingSets) > 1 ||
101  parse->hasWindowFuncs)
102  return;
103 
104  /*
105  * Reject if query contains any CTEs; there's no way to build an indexscan
106  * on one so we couldn't succeed here. (If the CTEs are unreferenced,
107  * that's not true, but it doesn't seem worth expending cycles to check.)
108  */
109  if (parse->cteList)
110  return;
111 
112  /*
113  * We also restrict the query to reference exactly one table, since join
114  * conditions can't be handled reasonably. (We could perhaps handle a
115  * query containing cartesian-product joins, but it hardly seems worth the
116  * trouble.) However, the single table could be buried in several levels
117  * of FromExpr due to subqueries. Note the "single" table could be an
118  * inheritance parent, too, including the case of a UNION ALL subquery
119  * that's been flattened to an appendrel.
120  */
121  jtnode = parse->jointree;
122  while (IsA(jtnode, FromExpr))
123  {
124  if (list_length(jtnode->fromlist) != 1)
125  return;
126  jtnode = linitial(jtnode->fromlist);
127  }
128  if (!IsA(jtnode, RangeTblRef))
129  return;
130  rtr = (RangeTblRef *) jtnode;
131  rte = planner_rt_fetch(rtr->rtindex, root);
132  if (rte->rtekind == RTE_RELATION)
133  /* ordinary relation, ok */ ;
134  else if (rte->rtekind == RTE_SUBQUERY && rte->inh)
135  /* flattened UNION ALL subquery, ok */ ;
136  else
137  return;
138 
139  /*
140  * Scan the tlist and HAVING qual to find all the aggregates and verify
141  * all are MIN/MAX aggregates. Stop as soon as we find one that isn't.
142  */
143  aggs_list = NIL;
144  if (!can_minmax_aggs(root, &aggs_list))
145  return;
146 
147  /*
148  * OK, there is at least the possibility of performing the optimization.
149  * Build an access path for each aggregate. If any of the aggregates
150  * prove to be non-indexable, give up; there is no point in optimizing
151  * just some of them.
152  */
153  foreach(lc, aggs_list)
154  {
155  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
156  Oid eqop;
157  bool reverse;
158 
159  /*
160  * We'll need the equality operator that goes with the aggregate's
161  * ordering operator.
162  */
163  eqop = get_equality_op_for_ordering_op(mminfo->aggsortop, &reverse);
164  if (!OidIsValid(eqop)) /* shouldn't happen */
165  elog(ERROR, "could not find equality operator for ordering operator %u",
166  mminfo->aggsortop);
167 
168  /*
169  * We can use either an ordering that gives NULLS FIRST or one that
170  * gives NULLS LAST; furthermore there's unlikely to be much
171  * performance difference between them, so it doesn't seem worth
172  * costing out both ways if we get a hit on the first one. NULLS
173  * FIRST is more likely to be available if the operator is a
174  * reverse-sort operator, so try that first if reverse.
175  */
176  if (build_minmax_path(root, mminfo, eqop, mminfo->aggsortop, reverse))
177  continue;
178  if (build_minmax_path(root, mminfo, eqop, mminfo->aggsortop, !reverse))
179  continue;
180 
181  /* No indexable path for this aggregate, so fail */
182  return;
183  }
184 
185  /*
186  * OK, we can do the query this way. Prepare to create a MinMaxAggPath
187  * node.
188  *
189  * First, create an output Param node for each agg. (If we end up not
190  * using the MinMaxAggPath, we'll waste a PARAM_EXEC slot for each agg,
191  * which is not worth worrying about. We can't wait till create_plan time
192  * to decide whether to make the Param, unfortunately.)
193  */
194  foreach(lc, aggs_list)
195  {
196  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
197 
198  mminfo->param =
200  exprType((Node *) mminfo->target),
201  -1,
202  exprCollation((Node *) mminfo->target));
203  }
204 
205  /*
206  * Create a MinMaxAggPath node with the appropriate estimated costs and
207  * other needed data, and add it to the UPPERREL_GROUP_AGG upperrel, where
208  * it will compete against the standard aggregate implementation. (It
209  * will likely always win, but we need not assume that here.)
210  *
211  * Note: grouping_planner won't have created this upperrel yet, but it's
212  * fine for us to create it first. We will not have inserted the correct
213  * consider_parallel value in it, but MinMaxAggPath paths are currently
214  * never parallel-safe anyway, so that doesn't matter. Likewise, it
215  * doesn't matter that we haven't filled FDW-related fields in the rel.
216  * Also, because there are no rowmarks, we know that the processed_tlist
217  * doesn't need to change anymore, so making the pathtarget now is safe.
218  */
219  grouped_rel = fetch_upper_rel(root, UPPERREL_GROUP_AGG, NULL);
220  add_path(grouped_rel, (Path *)
221  create_minmaxagg_path(root, grouped_rel,
222  create_pathtarget(root,
223  root->processed_tlist),
224  aggs_list,
225  (List *) parse->havingQual));
226 }
#define NIL
Definition: pg_list.h:65
MinMaxAggPath * create_minmaxagg_path(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, List *mmaggregates, List *quals)
Definition: pathnode.c:3234
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
Query * parse
Definition: pathnodes.h:173
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
FromExpr * jointree
Definition: parsenodes.h:138
bool hasAggs
Definition: parsenodes.h:125
Oid get_equality_op_for_ordering_op(Oid opno, bool *reverse)
Definition: lsyscache.c:265
Param * param
Definition: pathnodes.h:2338
List * groupingSets
Definition: parsenodes.h:150
Definition: nodes.h:527
List * minmax_aggs
Definition: pathnodes.h:323
List * fromlist
Definition: primnodes.h:1525
unsigned int Oid
Definition: postgres_ext.h:31
List * rowMarks
Definition: parsenodes.h:164
#define OidIsValid(objectId)
Definition: c.h:706
static bool can_minmax_aggs(PlannerInfo *root, List **context)
Definition: planagg.c:241
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:43
#define planner_rt_fetch(rti, root)
Definition: pathnodes.h:377
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1189
#define create_pathtarget(root, tlist)
Definition: tlist.h:54
#define Assert(condition)
Definition: c.h:800
#define lfirst(lc)
Definition: pg_list.h:169
Expr * target
Definition: pathnodes.h:2334
bool hasWindowFuncs
Definition: parsenodes.h:126
Param * SS_make_initplan_output_param(PlannerInfo *root, Oid resulttype, int32 resulttypmod, Oid resultcollation)
Definition: subselect.c:2940
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:768
static bool build_minmax_path(PlannerInfo *root, MinMaxAggInfo *mminfo, Oid eqop, Oid sortop, bool nulls_first)
Definition: planagg.c:317
RTEKind rtekind
Definition: parsenodes.h:982
List * cteList
Definition: parsenodes.h:135
Node * setOperations
Definition: parsenodes.h:166
List * groupClause
Definition: parsenodes.h:148
#define elog(elevel,...)
Definition: elog.h:228
Node * havingQual
Definition: parsenodes.h:152
List * processed_tlist
Definition: pathnodes.h:319
Definition: pg_list.h:50
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:648

◆ process_implied_equality()

RestrictInfo* process_implied_equality ( PlannerInfo root,
Oid  opno,
Oid  collation,
Expr item1,
Expr item2,
Relids  qualscope,
Relids  nullable_relids,
Index  security_level,
bool  below_outer_join,
bool  both_const 
)

Definition at line 2261 of file initsplan.c.

References add_vars_to_targetlist(), Assert, bms_copy(), bms_is_empty(), bms_is_subset(), bms_membership(), BMS_MULTIPLE, check_mergejoinable(), Const::constisnull, Const::consttype, Const::constvalue, contain_volatile_functions(), copyObject, DatumGetBool, distribute_restrictinfo_to_rels(), eval_const_expressions(), get_relids_in_jointree(), PlannerInfo::hasPseudoConstantQuals, InvalidOid, IsA, Query::jointree, list_free(), make_opclause(), make_restrictinfo(), PlannerInfo::parse, pull_var_clause(), pull_varnos(), PVC_INCLUDE_PLACEHOLDERS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_WINDOWFUNCS, and PostponedQual::relids.

Referenced by generate_base_implied_equalities_const(), and generate_base_implied_equalities_no_const().

2271 {
2272  RestrictInfo *restrictinfo;
2273  Node *clause;
2274  Relids relids;
2275  bool pseudoconstant = false;
2276 
2277  /*
2278  * Build the new clause. Copy to ensure it shares no substructure with
2279  * original (this is necessary in case there are subselects in there...)
2280  */
2281  clause = (Node *) make_opclause(opno,
2282  BOOLOID, /* opresulttype */
2283  false, /* opretset */
2284  copyObject(item1),
2285  copyObject(item2),
2286  InvalidOid,
2287  collation);
2288 
2289  /* If both constant, try to reduce to a boolean constant. */
2290  if (both_const)
2291  {
2292  clause = eval_const_expressions(root, clause);
2293 
2294  /* If we produced const TRUE, just drop the clause */
2295  if (clause && IsA(clause, Const))
2296  {
2297  Const *cclause = (Const *) clause;
2298 
2299  Assert(cclause->consttype == BOOLOID);
2300  if (!cclause->constisnull && DatumGetBool(cclause->constvalue))
2301  return NULL;
2302  }
2303  }
2304 
2305  /*
2306  * The rest of this is a very cut-down version of distribute_qual_to_rels.
2307  * We can skip most of the work therein, but there are a couple of special
2308  * cases we still have to handle.
2309  *
2310  * Retrieve all relids mentioned within the possibly-simplified clause.
2311  */
2312  relids = pull_varnos(clause);
2313  Assert(bms_is_subset(relids, qualscope));
2314 
2315  /*
2316  * If the clause is variable-free, our normal heuristic for pushing it
2317  * down to just the mentioned rels doesn't work, because there are none.
2318  * Apply at the given qualscope, or at the top of tree if it's nonvolatile
2319  * (which it very likely is, but we'll check, just to be sure).
2320  */
2321  if (bms_is_empty(relids))
2322  {
2323  /* eval at original syntactic level */
2324  relids = bms_copy(qualscope);
2325  if (!contain_volatile_functions(clause))
2326  {
2327  /* mark as gating qual */
2328  pseudoconstant = true;
2329  /* tell createplan.c to check for gating quals */
2330  root->hasPseudoConstantQuals = true;
2331  /* if not below outer join, push it to top of tree */
2332  if (!below_outer_join)
2333  {
2334  relids =
2336  false);
2337  }
2338  }
2339  }
2340 
2341  /*
2342  * Build the RestrictInfo node itself.
2343  */
2344  restrictinfo = make_restrictinfo((Expr *) clause,
2345  true, /* is_pushed_down */
2346  false, /* outerjoin_delayed */
2347  pseudoconstant,
2348  security_level,
2349  relids,
2350  NULL, /* outer_relids */
2351  nullable_relids);
2352 
2353  /*
2354  * If it's a join clause, add vars used in the clause to targetlists of
2355  * their relations, so that they will be emitted by the plan nodes that
2356  * scan those relations (else they won't be available at the join node!).
2357  *
2358  * Typically, we'd have already done this when the component expressions
2359  * were first seen by distribute_qual_to_rels; but it is possible that
2360  * some of the Vars could have missed having that done because they only
2361  * appeared in single-relation clauses originally. So do it here for
2362  * safety.
2363  */
2364  if (bms_membership(relids) == BMS_MULTIPLE)
2365  {
2366  List *vars = pull_var_clause(clause,
2370 
2371  add_vars_to_targetlist(root, vars, relids, false);
2372  list_free(vars);
2373  }
2374 
2375  /*
2376  * Check mergejoinability. This will usually succeed, since the op came
2377  * from an EquivalenceClass; but we could have reduced the original clause
2378  * to a constant.
2379  */
2380  check_mergejoinable(restrictinfo);
2381 
2382  /*
2383  * Note we don't do initialize_mergeclause_eclasses(); the caller can
2384  * handle that much more cheaply than we can. It's okay to call
2385  * distribute_restrictinfo_to_rels() before that happens.
2386  */
2387 
2388  /*
2389  * Push the new clause into all the appropriate restrictinfo lists.
2390  */
2391  distribute_restrictinfo_to_rels(root, restrictinfo);
2392 
2393  return restrictinfo;
2394 }
Datum constvalue
Definition: primnodes.h:214
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
Query * parse
Definition: pathnodes.h:173
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
RestrictInfo * make_restrictinfo(Expr *clause, bool is_pushed_down, bool outerjoin_delayed, bool pseudoconstant, Index security_level, Relids required_relids, Relids outer_relids, Relids nullable_relids)
Definition: restrictinfo.c:59
FromExpr * jointree
Definition: parsenodes.h:138
Definition: nodes.h:527
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:1999
List * pull_var_clause(Node *node, int flags)
Definition: var.c:535
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:436
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: makefuncs.c:610
void add_vars_to_targetlist(PlannerInfo *root, List *vars, Relids where_needed, bool create_new_ph)
Definition: initsplan.c:227
Oid consttype
Definition: primnodes.h:210
void distribute_restrictinfo_to_rels(PlannerInfo *root, RestrictInfo *restrictinfo)
Definition: initsplan.c:2173
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids get_relids_in_jointree(Node *jtnode, bool include_joins)
#define DatumGetBool(X)
Definition: postgres.h:393
Relids pull_varnos(Node *node)
Definition: var.c:95
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:181
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:672
bool hasPseudoConstantQuals
Definition: pathnodes.h:342
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:800
static void check_mergejoinable(RestrictInfo *restrictinfo)
Definition: initsplan.c:2638
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:180
void list_free(List *list)
Definition: list.c:1376
#define copyObject(obj)
Definition: nodes.h:643
Definition: regcomp.c:224
Definition: pg_list.h:50
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:178
bool constisnull
Definition: primnodes.h:215

◆ query_is_distinct_for()

bool query_is_distinct_for ( Query query,
List colnos,
List opids 
)

Definition at line 775 of file analyzejoins.c.

References SetOperationStmt::all, Assert, castNode, distinct_col_search(), Query::distinctClause, SortGroupClause::eqop, equality_ops_are_compatible(), get_sortgroupclause_tle(), Query::groupClause, SetOperationStmt::groupClauses, GROUPING_SET_EMPTY, Query::groupingSets, Query::hasAggs, Query::hasTargetSRFs, Query::havingQual, lfirst, linitial, list_head(), list_length(), lnext(), OidIsValid, SetOperationStmt::op, TargetEntry::resjunk, TargetEntry::resno, SETOP_NONE, Query::setOperations, and Query::targetList.

Referenced by create_unique_path(), and rel_is_distinct_for().

776 {
777  ListCell *l;
778  Oid opid;
779 
780  Assert(list_length(colnos) == list_length(opids));
781 
782  /*
783  * DISTINCT (including DISTINCT ON) guarantees uniqueness if all the
784  * columns in the DISTINCT clause appear in colnos and operator semantics
785  * match. This is true even if there are SRFs in the DISTINCT columns or
786  * elsewhere in the tlist.
787  */
788  if (query->distinctClause)
789  {
790  foreach(l, query->distinctClause)
791  {
792  SortGroupClause *sgc = (SortGroupClause *) lfirst(l);
794  query->targetList);
795 
796  opid = distinct_col_search(tle->resno, colnos, opids);
797  if (!OidIsValid(opid) ||
798  !equality_ops_are_compatible(opid, sgc->eqop))
799  break; /* exit early if no match */
800  }
801  if (l == NULL) /* had matches for all? */
802  return true;
803  }
804 
805  /*
806  * Otherwise, a set-returning function in the query's targetlist can
807  * result in returning duplicate rows, despite any grouping that might
808  * occur before tlist evaluation. (If all tlist SRFs are within GROUP BY
809  * columns, it would be safe because they'd be expanded before grouping.
810  * But it doesn't currently seem worth the effort to check for that.)
811  */
812  if (query->hasTargetSRFs)
813  return false;
814 
815  /*
816  * Similarly, GROUP BY without GROUPING SETS guarantees uniqueness if all
817  * the grouped columns appear in colnos and operator semantics match.
818  */
819  if (query->groupClause && !query->groupingSets)
820  {
821  foreach(l, query->groupClause)
822  {
823  SortGroupClause *sgc = (SortGroupClause *) lfirst(l);
825  query->targetList);
826 
827  opid = distinct_col_search(tle->resno, colnos, opids);
828  if (!OidIsValid(opid) ||
829  !equality_ops_are_compatible(opid, sgc->eqop))
830  break; /* exit early if no match */
831  }
832  if (l == NULL) /* had matches for all? */
833  return true;
834  }
835  else if (query->groupingSets)
836  {
837  /*
838  * If we have grouping sets with expressions, we probably don't have
839  * uniqueness and analysis would be hard. Punt.
840  */
841  if (query->groupClause)
842  return false;
843 
844  /*
845  * If we have no groupClause (therefore no grouping expressions), we
846  * might have one or many empty grouping sets. If there's just one,
847  * then we're returning only one row and are certainly unique. But
848  * otherwise, we know we're certainly not unique.
849  */
850  if (list_length(query->groupingSets) == 1 &&
851  ((GroupingSet *) linitial(query->groupingSets))->kind == GROUPING_SET_EMPTY)
852  return true;
853  else
854  return false;
855  }
856  else
857  {
858  /*
859  * If we have no GROUP BY, but do have aggregates or HAVING, then the
860  * result is at most one row so it's surely unique, for any operators.
861  */
862  if (query->hasAggs || query->havingQual)
863  return true;
864  }
865 
866  /*
867  * UNION, INTERSECT, EXCEPT guarantee uniqueness of the whole output row,
868  * except with ALL.
869  */
870  if (query->setOperations)
871  {
873 
874  Assert(topop->op != SETOP_NONE);
875 
876  if (!topop->all)
877  {
878  ListCell *lg;
879 
880  /* We're good if all the nonjunk output columns are in colnos */
881  lg = list_head(topop->groupClauses);
882  foreach(l, query->targetList)
883  {
884  TargetEntry *tle = (TargetEntry *) lfirst(l);
885  SortGroupClause *sgc;
886 
887  if (tle->resjunk)
888  continue; /* ignore resjunk columns */
889 
890  /* non-resjunk columns should have grouping clauses */
891  Assert(lg != NULL);
892  sgc = (SortGroupClause *) lfirst(lg);
893  lg = lnext(topop->groupClauses, lg);
894 
895  opid = distinct_col_search(tle->resno, colnos, opids);
896  if (!OidIsValid(opid) ||
897  !equality_ops_are_compatible(opid, sgc->eqop))
898  break; /* exit early if no match */
899  }
900  if (l == NULL) /* had matches for all? */
901  return true;
902  }
903  }
904 
905  /*
906  * XXX Are there any other cases in which we can easily see the result
907  * must be distinct?
908  *
909  * If you do add more smarts to this function, be sure to update
910  * query_supports_distinctness() to match.
911  */
912 
913  return false;
914 }
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Definition: tlist.c:389
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:310
#define castNode(_type_, nodeptr)
Definition: nodes.h:596
static Oid distinct_col_search(int colno, List *colnos, List *opids)
Definition: analyzejoins.c:924
bool hasAggs
Definition: parsenodes.h:125
List * groupingSets
Definition: parsenodes.h:150
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:706
List * targetList
Definition: parsenodes.h:140
bool resjunk
Definition: primnodes.h:1429
#define linitial(l)
Definition: pg_list.h:174
List * distinctClause
Definition: parsenodes.h:156
AttrNumber resno
Definition: primnodes.h:1423
static ListCell * list_head(const List *l)
Definition: pg_list.h:125
bool hasTargetSRFs
Definition: parsenodes.h:127
#define Assert(condition)
Definition: c.h:800
#define lfirst(lc)
Definition: pg_list.h:169
bool equality_ops_are_compatible(Oid opno1, Oid opno2)
Definition: lsyscache.c:696
static int list_length(const List *l)
Definition: pg_list.h:149
SetOperation op
Definition: parsenodes.h:1664
Node * setOperations
Definition: parsenodes.h:166
List * groupClause
Definition: parsenodes.h:148
Node * havingQual
Definition: parsenodes.h:152

◆ query_planner()

RelOptInfo* query_planner ( PlannerInfo root,
query_pathkeys_callback  qp_callback,
void *  qp_extra 
)

Definition at line 55 of file planmain.c.

References add_base_rels_to_query(), add_other_rels_to_query(), add_path(), add_placeholders_to_base_rels(), Assert, build_base_rel_tlists(), build_simple_rel(), PlannerInfo::canon_pathkeys, RelOptInfo::cheapest_total_path, RelOptInfo::consider_parallel, create_group_result_path(), create_lateral_join_info(), deconstruct_jointree(), PlannerInfo::ec_merging_done, elog, ERROR, extract_restriction_or_clauses(), find_lateral_references(), find_placeholders_in_jointree(), fix_placeholder_input_needed_levels(), PlannerInfo::fkey_list, force_parallel_mode, FORCE_PARALLEL_OFF, FromExpr::fromlist, PlannerInfo::full_join_clauses, generate_base_implied_equalities(), PlannerInfo::glob, PlannerInfo::initial_rels, is_parallel_safe(), IsA, PlannerInfo::join_cur_level, PlannerInfo::join_info_list, PlannerInfo::join_rel_hash, PlannerInfo::join_rel_level, PlannerInfo::join_rel_list, Query::jointree, PlannerInfo::left_join_clauses, linitial, list_length(), make_one_rel(), match_foreign_keys_to_quals(), NIL, PlannerGlobal::parallelModeOK, Path::param_info, parse(), PlannerInfo::parse, PlannerInfo::placeholder_list, PlannerInfo::processed_tlist, FromExpr::quals, reconsider_outer_join_clauses(), reduce_unique_semijoins(), RelOptInfo::reltarget, remove_useless_joins(), PlannerInfo::right_join_clauses, RTE_RESULT, RangeTblEntry::rtekind, set_cheapest(), setup_simple_rel_arrays(), and PlannerInfo::simple_rte_array.

Referenced by build_minmax_path(), and grouping_planner().

57 {
58  Query *parse = root->parse;
59  List *joinlist;
60  RelOptInfo *final_rel;
61 
62  /*
63  * Init planner lists to empty.
64  *
65  * NOTE: append_rel_list was set up by subquery_planner, so do not touch
66  * here.
67  */
68  root->join_rel_list = NIL;
69  root->join_rel_hash = NULL;
70  root->join_rel_level = NULL;
71  root->join_cur_level = 0;
72  root->canon_pathkeys = NIL;
73  root->left_join_clauses = NIL;
74  root->right_join_clauses = NIL;
75  root->full_join_clauses = NIL;
76  root->join_info_list = NIL;
77  root->placeholder_list = NIL;
78  root->fkey_list = NIL;
79  root->initial_rels = NIL;
80 
81  /*
82  * Set up arrays for accessing base relations and AppendRelInfos.
83  */
85 
86  /*
87  * In the trivial case where the jointree is a single RTE_RESULT relation,
88  * bypass all the rest of this function and just make a RelOptInfo and its
89  * one access path. This is worth optimizing because it applies for
90  * common cases like "SELECT expression" and "INSERT ... VALUES()".
91  */
92  Assert(parse->jointree->fromlist != NIL);
93  if (list_length(parse->jointree->fromlist) == 1)
94  {
95  Node *jtnode = (Node *) linitial(parse->jointree->fromlist);
96 
97  if (IsA(jtnode, RangeTblRef))
98  {
99  int varno = ((RangeTblRef *) jtnode)->rtindex;
100  RangeTblEntry *rte = root->simple_rte_array[varno];
101 
102  Assert(rte != NULL);
103  if (rte->rtekind == RTE_RESULT)
104  {
105  /* Make the RelOptInfo for it directly */
106  final_rel = build_simple_rel(root, varno, NULL);
107 
108  /*
109  * If query allows parallelism in general, check whether the
110  * quals are parallel-restricted. (We need not check
111  * final_rel->reltarget because it's empty at this point.
112  * Anything parallel-restricted in the query tlist will be
113  * dealt with later.) This is normally pretty silly, because
114  * a Result-only plan would never be interesting to
115  * parallelize. However, if force_parallel_mode is on, then
116  * we want to execute the Result in a parallel worker if
117  * possible, so we must do this.
118  */
119  if (root->glob->parallelModeOK &&
121  final_rel->consider_parallel =
122  is_parallel_safe(root, parse->jointree->quals);
123 
124  /*
125  * The only path for it is a trivial Result path. We cheat a
126  * bit here by using a GroupResultPath, because that way we
127  * can just jam the quals into it without preprocessing them.
128  * (But, if you hold your head at the right angle, a FROM-less
129  * SELECT is a kind of degenerate-grouping case, so it's not
130  * that much of a cheat.)
131  */
132  add_path(final_rel, (Path *)
133  create_group_result_path(root, final_rel,
134  final_rel->reltarget,
135  (List *) parse->jointree->quals));
136 
137  /* Select cheapest path (pretty easy in this case...) */
138  set_cheapest(final_rel);
139 
140  /*
141  * We don't need to run generate_base_implied_equalities, but
142  * we do need to pretend that EC merging is complete.
143  */
144  root->ec_merging_done = true;
145 
146  /*
147  * We still are required to call qp_callback, in case it's
148  * something like "SELECT 2+2 ORDER BY 1".
149  */
150  (*qp_callback) (root, qp_extra);
151 
152  return final_rel;
153  }
154  }
155  }
156 
157  /*
158  * Construct RelOptInfo nodes for all base relations used in the query.
159  * Appendrel member relations ("other rels") will be added later.
160  *
161  * Note: the reason we find the baserels by searching the jointree, rather
162  * than scanning the rangetable, is that the rangetable may contain RTEs
163  * for rels not actively part of the query, for example views. We don't
164  * want to make RelOptInfos for them.
165  */
166  add_base_rels_to_query(root, (Node *) parse->jointree);
167 
168  /*
169  * Examine the targetlist and join tree, adding entries to baserel
170  * targetlists for all referenced Vars, and generating PlaceHolderInfo
171  * entries for all referenced PlaceHolderVars. Restrict and join clauses
172  * are added to appropriate lists belonging to the mentioned relations. We
173  * also build EquivalenceClasses for provably equivalent expressions. The
174  * SpecialJoinInfo list is also built to hold information about join order
175  * restrictions. Finally, we form a target joinlist for make_one_rel() to
176  * work from.
177  */
179 
181 
183 
184  joinlist = deconstruct_jointree(root);
185 
186  /*
187  * Reconsider any postponed outer-join quals now that we have built up
188  * equivalence classes. (This could result in further additions or
189  * mergings of classes.)
190  */
192 
193  /*
194  * If we formed any equivalence classes, generate additional restriction
195  * clauses as appropriate. (Implied join clauses are formed on-the-fly
196  * later.)
197  */
199 
200  /*
201  * We have completed merging equivalence sets, so it's now possible to
202  * generate pathkeys in canonical form; so compute query_pathkeys and
203  * other pathkeys fields in PlannerInfo.
204  */
205  (*qp_callback) (root, qp_extra);
206 
207  /*
208  * Examine any "placeholder" expressions generated during subquery pullup.
209  * Make sure that the Vars they need are marked as needed at the relevant
210  * join level. This must be done before join removal because it might
211  * cause Vars or placeholders to be needed above a join when they weren't
212  * so marked before.
213  */
215 
216  /*
217  * Remove any useless outer joins. Ideally this would be done during
218  * jointree preprocessing, but the necessary information isn't available
219  * until we've built baserel data structures and classified qual clauses.
220  */
221  joinlist = remove_useless_joins(root, joinlist);
222 
223  /*
224  * Also, reduce any semijoins with unique inner rels to plain inner joins.
225  * Likewise, this can't be done until now for lack of needed info.
226  */
228 
229  /*
230  * Now distribute "placeholders" to base rels as needed. This has to be
231  * done after join removal because removal could change whether a
232  * placeholder is evaluable at a base rel.
233  */
235 
236  /*
237  * Construct the lateral reference sets now that we have finalized
238  * PlaceHolderVar eval levels.
239  */
241 
242  /*
243  * Match foreign keys to equivalence classes and join quals. This must be
244  * done after finalizing equivalence classes, and it's useful to wait till
245  * after join removal so that we can skip processing foreign keys
246  * involving removed relations.
247  */
249 
250  /*
251  * Look for join OR clauses that we can extract single-relation
252  * restriction OR clauses from.
253  */
255 
256  /*
257  * Now expand appendrels by adding "otherrels" for their children. We
258  * delay this to the end so that we have as much information as possible
259  * available for each baserel, including all restriction clauses. That
260  * let us prune away partitions that don't satisfy a restriction clause.
261  * Also note that some information such as lateral_relids is propagated
262  * from baserels to otherrels here, so we must have computed it already.
263  */
265 
266  /*
267  * Ready to do the primary planning.
268  */
269  final_rel = make_one_rel(root, joinlist);
270 
271  /* Check that we got at least one usable path */
272  if (!final_rel || !final_rel->cheapest_total_path ||
273  final_rel->cheapest_total_path->param_info != NULL)
274  elog(ERROR, "failed to construct the join relation");
275 
276  return final_rel;
277 }
#define NIL
Definition: pg_list.h:65
int join_cur_level
Definition: pathnodes.h:251
RelOptInfo * make_one_rel(PlannerInfo *root, List *joinlist)
Definition: allpaths.c:157
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
Query * parse
Definition: pathnodes.h:173
void add_path(RelOptInfo *parent_rel, Path *new_path)
Definition: pathnode.c:422
void reduce_unique_semijoins(PlannerInfo *root)
Definition: analyzejoins.c:508
bool ec_merging_done
Definition: pathnodes.h:262
void add_base_rels_to_query(PlannerInfo *root, Node *jtnode)
Definition: initsplan.c:103
List * join_info_list
Definition: pathnodes.h:277
FromExpr * jointree
Definition: parsenodes.h:138
void extract_restriction_or_clauses(PlannerInfo *root)
Definition: orclauses.c:76
List * deconstruct_jointree(PlannerInfo *root)
Definition: initsplan.c:684
ParamPathInfo * param_info
Definition: pathnodes.h:1151
Definition: nodes.h:527
List * join_rel_list
Definition: pathnodes.h:240
List * fromlist
Definition: primnodes.h:1525
List * fkey_list
Definition: pathnodes.h:290
void add_placeholders_to_base_rels(PlannerInfo *root)
Definition: placeholder.c:379
Node * quals
Definition: primnodes.h:1526
void add_other_rels_to_query(PlannerInfo *root)
Definition: initsplan.c:141
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:566
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:43
bool parallelModeOK
Definition: pathnodes.h:137
struct Path * cheapest_total_path
Definition: pathnodes.h:684
PlannerGlobal * glob
Definition: pathnodes.h:175
List * left_join_clauses
Definition: pathnodes.h:266
List * full_join_clauses
Definition: pathnodes.h:274
RelOptInfo * build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Definition: relnode.c:194
List * canon_pathkeys
Definition: pathnodes.h:264
RangeTblEntry ** simple_rte_array
Definition: pathnodes.h:205
void fix_placeholder_input_needed_levels(PlannerInfo *root)
Definition: placeholder.c:350
void set_cheapest(RelOptInfo *parent_rel)
Definition: pathnode.c:244
void find_placeholders_in_jointree(PlannerInfo *root)
Definition: placeholder.c:144
void generate_base_implied_equalities(PlannerInfo *root)
Definition: equivclass.c:917
void reconsider_outer_join_clauses(PlannerInfo *root)
Definition: equivclass.c:1812
int force_parallel_mode
Definition: planner.c:70
#define Assert(condition)
Definition: c.h:800
List ** join_rel_level
Definition: pathnodes.h:250
void setup_simple_rel_arrays(PlannerInfo *root)
Definition: relnode.c:83
List * remove_useless_joins(PlannerInfo *root, List *joinlist)
Definition: analyzejoins.c:61
static int list_length(const List *l)
Definition: pg_list.h:149
struct HTAB * join_rel_hash
Definition: pathnodes.h:241
void build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
Definition: initsplan.c:180
bool consider_parallel
Definition: pathnodes.h:674
RTEKind rtekind
Definition: parsenodes.h:982
void match_foreign_keys_to_quals(PlannerInfo *root)
Definition: initsplan.c:2468
#define elog(elevel,...)
Definition: elog.h:228
List * placeholder_list
Definition: pathnodes.h:288
GroupResultPath * create_group_result_path(PlannerInfo *root, RelOptInfo *rel, PathTarget *target, List *havingqual)
Definition: pathnode.c:1479
List * initial_rels
Definition: pathnodes.h:302
List * right_join_clauses
Definition: pathnodes.h:270
List * processed_tlist
Definition: pathnodes.h:319
Definition: pg_list.h:50
struct PathTarget * reltarget
Definition: pathnodes.h:677
void create_lateral_join_info(PlannerInfo *root)
Definition: initsplan.c:447
void find_lateral_references(PlannerInfo *root)
Definition: initsplan.c:301
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:648

◆ query_supports_distinctness()

bool query_supports_distinctness ( Query query)

Definition at line 738 of file analyzejoins.c.

References Query::distinctClause, Query::groupClause, Query::groupingSets, Query::hasAggs, Query::hasTargetSRFs, Query::havingQual, NIL, and Query::setOperations.

Referenced by create_unique_path(), and rel_supports_distinctness().

739 {
740  /* SRFs break distinctness except with DISTINCT, see below */
741  if (query->hasTargetSRFs && query->distinctClause == NIL)
742  return false;
743 
744  /* check for features we can prove distinctness with */
745  if (query->distinctClause != NIL ||
746  query->groupClause != NIL ||
747  query->groupingSets != NIL ||
748  query->hasAggs ||
749  query->havingQual ||
750  query->setOperations)
751  return true;
752 
753  return false;
754 }
#define NIL
Definition: pg_list.h:65
bool hasAggs
Definition: parsenodes.h:125
List * groupingSets
Definition: parsenodes.h:150
List * distinctClause
Definition: parsenodes.h:156
bool hasTargetSRFs
Definition: parsenodes.h:127
Node * setOperations
Definition: parsenodes.h:166
List * groupClause
Definition: parsenodes.h:148
Node * havingQual
Definition: parsenodes.h:152

◆ record_plan_function_dependency()

void record_plan_function_dependency ( PlannerInfo root,
Oid  funcid 
)

Definition at line 2889 of file setrefs.c.

References PlanInvalItem::cacheId, FirstBootstrapObjectId, GetSysCacheHashValue1, PlannerInfo::glob, PlanInvalItem::hashValue, PlannerGlobal::invalItems, lappend(), makeNode, ObjectIdGetDatum, and PROCOID.

Referenced by fix_expr_common(), inline_function(), and inline_set_returning_function().

2890 {
2891  /*
2892  * For performance reasons, we don't bother to track built-in functions;
2893  * we just assume they'll never change (or at least not in ways that'd
2894  * invalidate plans using them). For this purpose we can consider a
2895  * built-in function to be one with OID less than FirstBootstrapObjectId.
2896  * Note that the OID generator guarantees never to generate such an OID
2897  * after startup, even at OID wraparound.
2898  */
2899  if (funcid >= (Oid) FirstBootstrapObjectId)
2900  {
2901  PlanInvalItem *inval_item = makeNode(PlanInvalItem);
2902 
2903  /*
2904  * It would work to use any syscache on pg_proc, but the easiest is
2905  * PROCOID since we already have the function's OID at hand. Note
2906  * that plancache.c knows we use PROCOID.
2907  */
2908  inval_item->cacheId = PROCOID;
2909  inval_item->hashValue = GetSysCacheHashValue1(PROCOID,
2910  ObjectIdGetDatum(funcid));
2911 
2912  root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
2913  }
2914 }
#define GetSysCacheHashValue1(cacheId, key1)
Definition: syscache.h:201
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
PlannerGlobal * glob
Definition: pathnodes.h:175
#define FirstBootstrapObjectId
Definition: transam.h:189
List * lappend(List *list, void *datum)
Definition: list.c:321
List * invalItems
Definition: pathnodes.h:123
uint32 hashValue
Definition: plannodes.h:1248
#define makeNode(_type_)
Definition: nodes.h:575

◆ record_plan_type_dependency()

void record_plan_type_dependency ( PlannerInfo root,
Oid  typid 
)

Definition at line 2929 of file setrefs.c.

References PlanInvalItem::cacheId, FirstBootstrapObjectId, GetSysCacheHashValue1, PlannerInfo::glob, PlanInvalItem::hashValue, PlannerGlobal::invalItems, lappend(), makeNode, ObjectIdGetDatum, and TYPEOID.

Referenced by eval_const_expressions_mutator().

2930 {
2931  /*
2932  * As in record_plan_function_dependency, ignore the possibility that
2933  * someone would change a built-in domain.
2934  */
2935  if (typid >= (Oid) FirstBootstrapObjectId)
2936  {
2937  PlanInvalItem *inval_item = makeNode(PlanInvalItem);
2938 
2939  /*
2940  * It would work to use any syscache on pg_type, but the easiest is
2941  * TYPEOID since we already have the type's OID at hand. Note that
2942  * plancache.c knows we use TYPEOID.
2943  */
2944  inval_item->cacheId = TYPEOID;
2945  inval_item->hashValue = GetSysCacheHashValue1(TYPEOID,
2946  ObjectIdGetDatum(typid));
2947 
2948  root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
2949  }
2950 }
#define GetSysCacheHashValue1(cacheId, key1)
Definition: syscache.h:201
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
PlannerGlobal * glob
Definition: pathnodes.h:175
#define FirstBootstrapObjectId
Definition: transam.h:189
List * lappend(List *list, void *datum)
Definition: list.c:321
List * invalItems
Definition: pathnodes.h:123
uint32 hashValue
Definition: plannodes.h:1248
#define makeNode(_type_)
Definition: nodes.h:575

◆ reduce_unique_semijoins()

void reduce_unique_semijoins ( PlannerInfo root)

Definition at line 508 of file analyzejoins.c.

References bms_get_singleton_member(), bms_union(), SpecialJoinInfo::delay_upper_joins, find_base_rel(), foreach_delete_current, generate_join_implied_equalities(), innerrel_is_unique(), PlannerInfo::join_info_list, JOIN_SEMI, RelOptInfo::joininfo, SpecialJoinInfo::jointype, lfirst, list_concat(), SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, and rel_supports_distinctness().

Referenced by query_planner().

509 {
510  ListCell *lc;
511 
512  /*
513  * Scan the join_info_list to find semijoins.
514  */
515  foreach(lc, root->join_info_list)
516  {
517  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
518  int innerrelid;
519  RelOptInfo *innerrel;
520  Relids joinrelids;
521  List *restrictlist;
522 
523  /*
524  * Must be a non-delaying semijoin to a single baserel, else we aren't
525  * going to be able to do anything with it. (It's probably not
526  * possible for delay_upper_joins to be set on a semijoin, but we
527  * might as well check.)
528  */
529  if (sjinfo->jointype != JOIN_SEMI ||
530  sjinfo->delay_upper_joins)
531  continue;
532 
533  if (!bms_get_singleton_member(sjinfo->min_righthand, &innerrelid))
534  continue;
535 
536  innerrel = find_base_rel(root, innerrelid);
537 
538  /*
539  * Before we trouble to run generate_join_implied_equalities, make a
540  * quick check to eliminate cases in which we will surely be unable to
541  * prove uniqueness of the innerrel.
542  */
543  if (!rel_supports_distinctness(root, innerrel))
544  continue;
545 
546  /* Compute the relid set for the join we are considering */
547  joinrelids = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
548 
549  /*
550  * Since we're only considering a single-rel RHS, any join clauses it
551  * has must be clauses linking it to the semijoin's min_lefthand. We
552  * can also consider EC-derived join clauses.
553  */
554  restrictlist =
556  joinrelids,
557  sjinfo->min_lefthand,
558  innerrel),
559  innerrel->joininfo);
560 
561  /* Test whether the innerrel is unique for those clauses. */
562  if (!innerrel_is_unique(root,
563  joinrelids, sjinfo->min_lefthand, innerrel,
564  JOIN_SEMI, restrictlist, true))
565  continue;
566 
567  /* OK, remove the SpecialJoinInfo from the list. */
569  }
570 }
List * join_info_list
Definition: pathnodes.h:277
Relids min_righthand
Definition: pathnodes.h:2185
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
bool bms_get_singleton_member(const Bitmapset *a, int *member)
Definition: bitmapset.c:615
bool innerrel_is_unique(PlannerInfo *root, Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel, JoinType jointype, List *restrictlist, bool force_cache)
Definition: analyzejoins.c:964
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:357
List * generate_join_implied_equalities(PlannerInfo *root, Relids join_relids, Relids outer_relids, RelOptInfo *inner_rel)
Definition: equivclass.c:1250
List * joininfo
Definition: pathnodes.h:732
bool delay_upper_joins
Definition: pathnodes.h:2190
#define lfirst(lc)
Definition: pg_list.h:169
JoinType jointype
Definition: pathnodes.h:2188
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:373
static bool rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel)
Definition: analyzejoins.c:585
Definition: pg_list.h:50
Relids min_lefthand
Definition: pathnodes.h:2184

◆ remove_useless_joins()

List* remove_useless_joins ( PlannerInfo root,
List joinlist 
)

Definition at line 61 of file analyzejoins.c.

References bms_singleton_member(), bms_union(), elog, ERROR, PlannerInfo::join_info_list, join_is_removable(), lfirst, list_delete_cell(), SpecialJoinInfo::min_lefthand, SpecialJoinInfo::min_righthand, remove_rel_from_joinlist(), and remove_rel_from_query().

Referenced by query_planner().

62 {
63  ListCell *lc;
64 
65  /*
66  * We are only interested in relations that are left-joined to, so we can
67  * scan the join_info_list to find them easily.
68  */
69 restart:
70  foreach(lc, root->join_info_list)
71  {
72  SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc);
73  int innerrelid;
74  int nremoved;
75 
76  /* Skip if not removable */
77  if (!join_is_removable(root, sjinfo))
78  continue;
79 
80  /*
81  * Currently, join_is_removable can only succeed when the sjinfo's
82  * righthand is a single baserel. Remove that rel from the query and
83  * joinlist.
84  */
85  innerrelid = bms_singleton_member(sjinfo->min_righthand);
86 
87  remove_rel_from_query(root, innerrelid,
88  bms_union(sjinfo->min_lefthand,
89  sjinfo->min_righthand));
90 
91  /* We verify that exactly one reference gets removed from joinlist */
92  nremoved = 0;
93  joinlist = remove_rel_from_joinlist(joinlist, innerrelid, &nremoved);
94  if (nremoved != 1)
95  elog(ERROR, "failed to find relation %d in joinlist", innerrelid);
96 
97  /*
98  * We can delete this SpecialJoinInfo from the list too, since it's no
99  * longer of interest. (Since we'll restart the foreach loop
100  * immediately, we don't bother with foreach_delete_current.)
101  */
103 
104  /*
105  * Restart the scan. This is necessary to ensure we find all
106  * removable joins independently of ordering of the join_info_list
107  * (note that removal of attr_needed bits may make a join appear
108  * removable that did not before).
109  */
110  goto restart;
111  }
112 
113  return joinlist;
114 }
List * join_info_list
Definition: pathnodes.h:277
Relids min_righthand
Definition: pathnodes.h:2185
static void remove_rel_from_query(PlannerInfo *root, int relid, Relids joinrelids)
Definition: analyzejoins.c:312
#define ERROR
Definition: elog.h:43
static bool join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
Definition: analyzejoins.c:159
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:577
List * list_delete_cell(List *list, ListCell *cell)
Definition: list.c:767
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
#define elog(elevel,...)
Definition: elog.h:228
static List * remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved)
Definition: analyzejoins.c:454
Relids min_lefthand
Definition: pathnodes.h:2184

◆ set_plan_references()

Plan* set_plan_references ( PlannerInfo root,
Plan plan 
)

Definition at line 250 of file setrefs.c.

References add_rtes_to_flat_rtable(), PlannerInfo::append_rel_list, PlannerGlobal::appendRelations, AppendRelInfo::child_relid, PlannerGlobal::finalrowmarks, PlannerGlobal::finalrtable, PlannerInfo::glob, lappend(), lfirst_node, list_length(), NIL, palloc(), AppendRelInfo::parent_relid, PlanRowMark::prti, PlannerInfo::rowMarks, PlanRowMark::rti, set_plan_refs(), and AppendRelInfo::translated_vars.

Referenced by set_subqueryscan_references(), and standard_planner().

251 {
252  PlannerGlobal *glob = root->glob;
253  int rtoffset = list_length(glob->finalrtable);
254  ListCell *lc;
255 
256  /*
257  * Add all the query's RTEs to the flattened rangetable. The live ones
258  * will have their rangetable indexes increased by rtoffset. (Additional
259  * RTEs, not referenced by the Plan tree, might get added after those.)
260  */
261  add_rtes_to_flat_rtable(root, false);
262 
263  /*
264  * Adjust RT indexes of PlanRowMarks and add to final rowmarks list
265  */
266  foreach(lc, root->rowMarks)
267  {
269  PlanRowMark *newrc;
270 
271  /* flat copy is enough since all fields are scalars */
272  newrc = (PlanRowMark *) palloc(sizeof(PlanRowMark));
273  memcpy(newrc, rc, sizeof(PlanRowMark));
274 
275  /* adjust indexes ... but *not* the rowmarkId */
276  newrc->rti += rtoffset;
277  newrc->prti += rtoffset;
278 
279  glob->finalrowmarks = lappend(glob->finalrowmarks, newrc);
280  }
281 
282  /*
283  * Adjust RT indexes of AppendRelInfos and add to final appendrels list.
284  * We assume the AppendRelInfos were built during planning and don't need
285  * to be copied.
286  */
287  foreach(lc, root->append_rel_list)
288  {
289  AppendRelInfo *appinfo = lfirst_node(AppendRelInfo, lc);
290 
291  /* adjust RT indexes */
292  appinfo->parent_relid += rtoffset;
293  appinfo->child_relid += rtoffset;
294 
295  /*
296  * Rather than adjust the translated_vars entries, just drop 'em.
297  * Neither the executor nor EXPLAIN currently need that data.
298  */
299  appinfo->translated_vars = NIL;
300 
301  glob->appendRelations = lappend(glob->appendRelations, appinfo);
302  }
303 
304  /* Now fix the Plan tree */
305  return set_plan_refs(root, plan, rtoffset);
306 }
#define NIL
Definition: pg_list.h:65
List * rowMarks
Definition: pathnodes.h:286
List * appendRelations
Definition: pathnodes.h:119
Index prti
Definition: plannodes.h:1078
List * translated_vars
Definition: pathnodes.h:2266
static Plan * set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:499
#define lfirst_node(type, lc)
Definition: pg_list.h:172
PlannerGlobal * glob
Definition: pathnodes.h:175
List * lappend(List *list, void *datum)
Definition: list.c:321
List * append_rel_list
Definition: pathnodes.h:284
static void add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing)
Definition: setrefs.c:314
static int list_length(const List *l)
Definition: pg_list.h:149
List * finalrtable
Definition: pathnodes.h:113
void * palloc(Size size)
Definition: mcxt.c:950
Index child_relid
Definition: pathnodes.h:2239
Index parent_relid
Definition: pathnodes.h:2238
List * finalrowmarks
Definition: pathnodes.h:115

Variable Documentation

◆ cursor_tuple_fraction

double cursor_tuple_fraction

Definition at line 69 of file planner.c.

Referenced by standard_planner().

◆ from_collapse_limit

int from_collapse_limit

Definition at line 38 of file initsplan.c.

Referenced by deconstruct_recurse().

◆ join_collapse_limit

int join_collapse_limit

Definition at line 39 of file initsplan.c.

Referenced by deconstruct_recurse().