PostgreSQL Source Code  git master
setrefs.c File Reference
#include "postgres.h"
#include "access/transam.h"
#include "catalog/pg_type.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
#include "optimizer/pathnode.h"
#include "optimizer/planmain.h"
#include "optimizer/planner.h"
#include "optimizer/tlist.h"
#include "tcop/utility.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
Include dependency graph for setrefs.c:

Go to the source code of this file.

Data Structures

struct  tlist_vinfo
 
struct  indexed_tlist
 
struct  fix_scan_expr_context
 
struct  fix_join_expr_context
 
struct  fix_upper_expr_context
 

Macros

#define NUM_EXEC_TLIST(parentplan)   ((parentplan)->plan_rows)
 
#define NUM_EXEC_QUAL(parentplan)   ((parentplan)->plan_rows * 2.0)
 
#define ISREGCLASSCONST(con)
 
#define fix_scan_list(root, lst, rtoffset, num_exec)   ((List *) fix_scan_expr(root, (Node *) (lst), rtoffset, num_exec))
 

Functions

static void add_rtes_to_flat_rtable (PlannerInfo *root, bool recursing)
 
static void flatten_unplanned_rtes (PlannerGlobal *glob, RangeTblEntry *rte)
 
static bool flatten_rtes_walker (Node *node, PlannerGlobal *glob)
 
static void add_rte_to_flat_rtable (PlannerGlobal *glob, RangeTblEntry *rte)
 
static Planset_plan_refs (PlannerInfo *root, Plan *plan, int rtoffset)
 
static Planset_indexonlyscan_references (PlannerInfo *root, IndexOnlyScan *plan, int rtoffset)
 
static Planset_subqueryscan_references (PlannerInfo *root, SubqueryScan *plan, int rtoffset)
 
static bool trivial_subqueryscan (SubqueryScan *plan)
 
static Planclean_up_removed_plan_level (Plan *parent, Plan *child)
 
static void set_foreignscan_references (PlannerInfo *root, ForeignScan *fscan, int rtoffset)
 
static void set_customscan_references (PlannerInfo *root, CustomScan *cscan, int rtoffset)
 
static Planset_append_references (PlannerInfo *root, Append *aplan, int rtoffset)
 
static Planset_mergeappend_references (PlannerInfo *root, MergeAppend *mplan, int rtoffset)
 
static void set_hash_references (PlannerInfo *root, Plan *plan, int rtoffset)
 
static Relids offset_relid_set (Relids relids, int rtoffset)
 
static Nodefix_scan_expr (PlannerInfo *root, Node *node, int rtoffset, double num_exec)
 
static Nodefix_scan_expr_mutator (Node *node, fix_scan_expr_context *context)
 
static bool fix_scan_expr_walker (Node *node, fix_scan_expr_context *context)
 
static void set_join_references (PlannerInfo *root, Join *join, int rtoffset)
 
static void set_upper_references (PlannerInfo *root, Plan *plan, int rtoffset)
 
static void set_param_references (PlannerInfo *root, Plan *plan)
 
static Nodeconvert_combining_aggrefs (Node *node, void *context)
 
static void set_dummy_tlist_references (Plan *plan, int rtoffset)
 
static indexed_tlistbuild_tlist_index (List *tlist)
 
static Varsearch_indexed_tlist_for_var (Var *var, indexed_tlist *itlist, Index newvarno, int rtoffset)
 
static Varsearch_indexed_tlist_for_non_var (Expr *node, indexed_tlist *itlist, Index newvarno)
 
static Varsearch_indexed_tlist_for_sortgroupref (Expr *node, Index sortgroupref, indexed_tlist *itlist, Index newvarno)
 
static Listfix_join_expr (PlannerInfo *root, List *clauses, indexed_tlist *outer_itlist, indexed_tlist *inner_itlist, Index acceptable_rel, int rtoffset, double num_exec)
 
static Nodefix_join_expr_mutator (Node *node, fix_join_expr_context *context)
 
static Nodefix_upper_expr (PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, Index newvarno, int rtoffset, double num_exec)
 
static Nodefix_upper_expr_mutator (Node *node, fix_upper_expr_context *context)
 
static Listset_returning_clause_references (PlannerInfo *root, List *rlist, Plan *topplan, Index resultRelation, int rtoffset)
 
Planset_plan_references (PlannerInfo *root, Plan *plan)
 
static VarcopyVar (Var *var)
 
static void fix_expr_common (PlannerInfo *root, Node *node)
 
static Nodefix_param_node (PlannerInfo *root, Param *p)
 
static Nodefix_alternative_subplan (PlannerInfo *root, AlternativeSubPlan *asplan, double num_exec)
 
static indexed_tlistbuild_tlist_index_other_vars (List *tlist, Index ignore_rel)
 
void record_plan_function_dependency (PlannerInfo *root, Oid funcid)
 
void record_plan_type_dependency (PlannerInfo *root, Oid typid)
 
void extract_query_dependencies (Node *query, List **relationOids, List **invalItems, bool *hasRowSecurity)
 
bool extract_query_dependencies_walker (Node *node, PlannerInfo *context)
 

Macro Definition Documentation

◆ fix_scan_list

#define fix_scan_list (   root,
  lst,
  rtoffset,
  num_exec 
)    ((List *) fix_scan_expr(root, (Node *) (lst), rtoffset, num_exec))

◆ ISREGCLASSCONST

#define ISREGCLASSCONST (   con)
Value:
(((con)->consttype == REGCLASSOID || (con)->consttype == OIDOID) && \
!(con)->constisnull)

Definition at line 100 of file setrefs.c.

Referenced by fix_expr_common().

◆ NUM_EXEC_QUAL

#define NUM_EXEC_QUAL (   parentplan)    ((parentplan)->plan_rows * 2.0)

◆ NUM_EXEC_TLIST

Function Documentation

◆ add_rte_to_flat_rtable()

static void add_rte_to_flat_rtable ( PlannerGlobal glob,
RangeTblEntry rte 
)
static

Definition at line 446 of file setrefs.c.

References RangeTblEntry::colcollations, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, ereport, errcode(), errmsg(), ERROR, PlannerGlobal::finalrtable, RangeTblEntry::functions, IS_SPECIAL_VARNO, RangeTblEntry::join_using_alias, RangeTblEntry::joinaliasvars, RangeTblEntry::joinleftcols, RangeTblEntry::joinrightcols, lappend(), lappend_oid(), list_length(), NIL, palloc(), PlannerGlobal::relationOids, RangeTblEntry::relid, RTE_RELATION, RangeTblEntry::rtekind, RangeTblEntry::securityQuals, RangeTblEntry::subquery, RangeTblEntry::tablefunc, RangeTblEntry::tablesample, and RangeTblEntry::values_lists.

Referenced by add_rtes_to_flat_rtable(), and flatten_rtes_walker().

447 {
448  RangeTblEntry *newrte;
449 
450  /* flat copy to duplicate all the scalar fields */
451  newrte = (RangeTblEntry *) palloc(sizeof(RangeTblEntry));
452  memcpy(newrte, rte, sizeof(RangeTblEntry));
453 
454  /* zap unneeded sub-structure */
455  newrte->tablesample = NULL;
456  newrte->subquery = NULL;
457  newrte->joinaliasvars = NIL;
458  newrte->joinleftcols = NIL;
459  newrte->joinrightcols = NIL;
460  newrte->join_using_alias = NULL;
461  newrte->functions = NIL;
462  newrte->tablefunc = NULL;
463  newrte->values_lists = NIL;
464  newrte->coltypes = NIL;
465  newrte->coltypmods = NIL;
466  newrte->colcollations = NIL;
467  newrte->securityQuals = NIL;
468 
469  glob->finalrtable = lappend(glob->finalrtable, newrte);
470 
471  /*
472  * Check for RT index overflow; it's very unlikely, but if it did happen,
473  * the executor would get confused by varnos that match the special varno
474  * values.
475  */
477  ereport(ERROR,
478  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
479  errmsg("too many range table entries")));
480 
481  /*
482  * If it's a plain relation RTE, add the table to relationOids.
483  *
484  * We do this even though the RTE might be unreferenced in the plan tree;
485  * this would correspond to cases such as views that were expanded, child
486  * tables that were eliminated by constraint exclusion, etc. Schema
487  * invalidation on such a rel must still force rebuilding of the plan.
488  *
489  * Note we don't bother to avoid making duplicate list entries. We could,
490  * but it would probably cost more cycles than it would save.
491  */
492  if (newrte->rtekind == RTE_RELATION)
493  glob->relationOids = lappend_oid(glob->relationOids, newrte->relid);
494 }
#define NIL
Definition: pg_list.h:65
List * joinaliasvars
Definition: parsenodes.h:1070
List * securityQuals
Definition: parsenodes.h:1151
List * coltypmods
Definition: parsenodes.h:1128
int errcode(int sqlerrcode)
Definition: elog.c:698
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
List * colcollations
Definition: parsenodes.h:1129
List * values_lists
Definition: parsenodes.h:1100
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:180
#define ERROR
Definition: elog.h:46
TableFunc * tablefunc
Definition: parsenodes.h:1095
List * joinrightcols
Definition: parsenodes.h:1072
List * lappend(List *list, void *datum)
Definition: list.c:336
#define ereport(elevel,...)
Definition: elog.h:157
List * joinleftcols
Definition: parsenodes.h:1071
Alias * join_using_alias
Definition: parsenodes.h:1079
List * functions
Definition: parsenodes.h:1089
static int list_length(const List *l)
Definition: pg_list.h:149
List * relationOids
Definition: pathnodes.h:109
RTEKind rtekind
Definition: parsenodes.h:995
List * finalrtable
Definition: pathnodes.h:101
Query * subquery
Definition: parsenodes.h:1030
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
List * coltypes
Definition: parsenodes.h:1127
struct TableSampleClause * tablesample
Definition: parsenodes.h:1025

◆ add_rtes_to_flat_rtable()

static void add_rtes_to_flat_rtable ( PlannerInfo root,
bool  recursing 
)
static

Definition at line 314 of file setrefs.c.

References add_rte_to_flat_rtable(), Assert, fetch_upper_rel(), flatten_unplanned_rtes(), PlannerInfo::glob, RangeTblEntry::inh, IS_DUMMY_REL, lfirst, PlannerInfo::parse, RelOptInfo::relid, Query::rtable, RTE_RELATION, RTE_SUBQUERY, RangeTblEntry::rtekind, PlannerInfo::simple_rel_array, RelOptInfo::subroot, and UPPERREL_FINAL.

Referenced by set_plan_references().

315 {
316  PlannerGlobal *glob = root->glob;
317  Index rti;
318  ListCell *lc;
319 
320  /*
321  * Add the query's own RTEs to the flattened rangetable.
322  *
323  * At top level, we must add all RTEs so that their indexes in the
324  * flattened rangetable match up with their original indexes. When
325  * recursing, we only care about extracting relation RTEs.
326  */
327  foreach(lc, root->parse->rtable)
328  {
329  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
330 
331  if (!recursing || rte->rtekind == RTE_RELATION)
332  add_rte_to_flat_rtable(glob, rte);
333  }
334 
335  /*
336  * If there are any dead subqueries, they are not referenced in the Plan
337  * tree, so we must add RTEs contained in them to the flattened rtable
338  * separately. (If we failed to do this, the executor would not perform
339  * expected permission checks for tables mentioned in such subqueries.)
340  *
341  * Note: this pass over the rangetable can't be combined with the previous
342  * one, because that would mess up the numbering of the live RTEs in the
343  * flattened rangetable.
344  */
345  rti = 1;
346  foreach(lc, root->parse->rtable)
347  {
348  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
349 
350  /*
351  * We should ignore inheritance-parent RTEs: their contents have been
352  * pulled up into our rangetable already. Also ignore any subquery
353  * RTEs without matching RelOptInfos, as they likewise have been
354  * pulled up.
355  */
356  if (rte->rtekind == RTE_SUBQUERY && !rte->inh &&
357  rti < root->simple_rel_array_size)
358  {
359  RelOptInfo *rel = root->simple_rel_array[rti];
360 
361  if (rel != NULL)
362  {
363  Assert(rel->relid == rti); /* sanity check on array */
364 
365  /*
366  * The subquery might never have been planned at all, if it
367  * was excluded on the basis of self-contradictory constraints
368  * in our query level. In this case apply
369  * flatten_unplanned_rtes.
370  *
371  * If it was planned but the result rel is dummy, we assume
372  * that it has been omitted from our plan tree (see
373  * set_subquery_pathlist), and recurse to pull up its RTEs.
374  *
375  * Otherwise, it should be represented by a SubqueryScan node
376  * somewhere in our plan tree, and we'll pull up its RTEs when
377  * we process that plan node.
378  *
379  * However, if we're recursing, then we should pull up RTEs
380  * whether the subquery is dummy or not, because we've found
381  * that some upper query level is treating this one as dummy,
382  * and so we won't scan this level's plan tree at all.
383  */
384  if (rel->subroot == NULL)
385  flatten_unplanned_rtes(glob, rte);
386  else if (recursing ||
388  UPPERREL_FINAL, NULL)))
389  add_rtes_to_flat_rtable(rel->subroot, true);
390  }
391  }
392  rti++;
393  }
394 }
Query * parse
Definition: pathnodes.h:161
static void flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte)
Definition: setrefs.c:400
static void add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte)
Definition: setrefs.c:446
struct RelOptInfo ** simple_rel_array
Definition: pathnodes.h:185
PlannerInfo * subroot
Definition: pathnodes.h:720
List * rtable
Definition: parsenodes.h:147
#define IS_DUMMY_REL(r)
Definition: pathnodes.h:1458
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1207
PlannerGlobal * glob
Definition: pathnodes.h:163
Index relid
Definition: pathnodes.h:704
unsigned int Index
Definition: c.h:549
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
static void add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing)
Definition: setrefs.c:314
RTEKind rtekind
Definition: parsenodes.h:995

◆ build_tlist_index()

static indexed_tlist * build_tlist_index ( List tlist)
static

Definition at line 2368 of file setrefs.c.

References TargetEntry::expr, indexed_tlist::has_non_vars, indexed_tlist::has_ph_vars, IsA, lfirst, list_length(), indexed_tlist::num_vars, offsetof, palloc(), tlist_vinfo::resno, TargetEntry::resno, indexed_tlist::tlist, tlist_vinfo::varattno, Var::varattno, tlist_vinfo::varno, Var::varno, and indexed_tlist::vars.

Referenced by set_customscan_references(), set_foreignscan_references(), set_hash_references(), set_indexonlyscan_references(), set_join_references(), set_plan_refs(), and set_upper_references().

2369 {
2370  indexed_tlist *itlist;
2371  tlist_vinfo *vinfo;
2372  ListCell *l;
2373 
2374  /* Create data structure with enough slots for all tlist entries */
2375  itlist = (indexed_tlist *)
2377  list_length(tlist) * sizeof(tlist_vinfo));
2378 
2379  itlist->tlist = tlist;
2380  itlist->has_ph_vars = false;
2381  itlist->has_non_vars = false;
2382 
2383  /* Find the Vars and fill in the index array */
2384  vinfo = itlist->vars;
2385  foreach(l, tlist)
2386  {
2387  TargetEntry *tle = (TargetEntry *) lfirst(l);
2388 
2389  if (tle->expr && IsA(tle->expr, Var))
2390  {
2391  Var *var = (Var *) tle->expr;
2392 
2393  vinfo->varno = var->varno;
2394  vinfo->varattno = var->varattno;
2395  vinfo->resno = tle->resno;
2396  vinfo++;
2397  }
2398  else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2399  itlist->has_ph_vars = true;
2400  else
2401  itlist->has_non_vars = true;
2402  }
2403 
2404  itlist->num_vars = (vinfo - itlist->vars);
2405 
2406  return itlist;
2407 }
Index varno
Definition: setrefs.c:34
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
tlist_vinfo vars[FLEXIBLE_ARRAY_MEMBER]
Definition: setrefs.c:45
AttrNumber resno
Definition: setrefs.c:36
AttrNumber varattno
Definition: primnodes.h:191
Definition: primnodes.h:186
AttrNumber varattno
Definition: setrefs.c:35
bool has_ph_vars
Definition: setrefs.c:43
int num_vars
Definition: setrefs.c:42
AttrNumber resno
Definition: primnodes.h:1445
Index varno
Definition: primnodes.h:189
bool has_non_vars
Definition: setrefs.c:44
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1444
static int list_length(const List *l)
Definition: pg_list.h:149
void * palloc(Size size)
Definition: mcxt.c:1062
Definition: regcomp.c:237
#define offsetof(type, field)
Definition: c.h:727
List * tlist
Definition: setrefs.c:41

◆ build_tlist_index_other_vars()

static indexed_tlist* build_tlist_index_other_vars ( List tlist,
Index  ignore_rel 
)
static

Definition at line 2418 of file setrefs.c.

References TargetEntry::expr, indexed_tlist::has_non_vars, indexed_tlist::has_ph_vars, IsA, lfirst, list_length(), indexed_tlist::num_vars, offsetof, palloc(), tlist_vinfo::resno, TargetEntry::resno, indexed_tlist::tlist, tlist_vinfo::varattno, Var::varattno, tlist_vinfo::varno, Var::varno, and indexed_tlist::vars.

Referenced by set_returning_clause_references().

2419 {
2420  indexed_tlist *itlist;
2421  tlist_vinfo *vinfo;
2422  ListCell *l;
2423 
2424  /* Create data structure with enough slots for all tlist entries */
2425  itlist = (indexed_tlist *)
2427  list_length(tlist) * sizeof(tlist_vinfo));
2428 
2429  itlist->tlist = tlist;
2430  itlist->has_ph_vars = false;
2431  itlist->has_non_vars = false;
2432 
2433  /* Find the desired Vars and fill in the index array */
2434  vinfo = itlist->vars;
2435  foreach(l, tlist)
2436  {
2437  TargetEntry *tle = (TargetEntry *) lfirst(l);
2438 
2439  if (tle->expr && IsA(tle->expr, Var))
2440  {
2441  Var *var = (Var *) tle->expr;
2442 
2443  if (var->varno != ignore_rel)
2444  {
2445  vinfo->varno = var->varno;
2446  vinfo->varattno = var->varattno;
2447  vinfo->resno = tle->resno;
2448  vinfo++;
2449  }
2450  }
2451  else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2452  itlist->has_ph_vars = true;
2453  }
2454 
2455  itlist->num_vars = (vinfo - itlist->vars);
2456 
2457  return itlist;
2458 }
Index varno
Definition: setrefs.c:34
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
tlist_vinfo vars[FLEXIBLE_ARRAY_MEMBER]
Definition: setrefs.c:45
AttrNumber resno
Definition: setrefs.c:36
AttrNumber varattno
Definition: primnodes.h:191
Definition: primnodes.h:186
AttrNumber varattno
Definition: setrefs.c:35
bool has_ph_vars
Definition: setrefs.c:43
int num_vars
Definition: setrefs.c:42
AttrNumber resno
Definition: primnodes.h:1445
Index varno
Definition: primnodes.h:189
bool has_non_vars
Definition: setrefs.c:44
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1444
static int list_length(const List *l)
Definition: pg_list.h:149
void * palloc(Size size)
Definition: mcxt.c:1062
Definition: regcomp.c:237
#define offsetof(type, field)
Definition: c.h:727
List * tlist
Definition: setrefs.c:41

◆ clean_up_removed_plan_level()

static Plan * clean_up_removed_plan_level ( Plan parent,
Plan child 
)
static

Definition at line 1262 of file setrefs.c.

References apply_tlist_labeling(), Plan::initPlan, list_concat(), and Plan::targetlist.

Referenced by set_append_references(), set_mergeappend_references(), and set_subqueryscan_references().

1263 {
1264  /* We have to be sure we don't lose any initplans */
1265  child->initPlan = list_concat(parent->initPlan,
1266  child->initPlan);
1267 
1268  /*
1269  * We also have to transfer the parent's column labeling info into the
1270  * child, else columns sent to client will be improperly labeled if this
1271  * is the topmost plan level. resjunk and so on may be important too.
1272  */
1273  apply_tlist_labeling(child->targetlist, parent->targetlist);
1274 
1275  return child;
1276 }
void apply_tlist_labeling(List *dest_tlist, List *src_tlist)
Definition: tlist.c:307
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
List * targetlist
Definition: plannodes.h:141
List * initPlan
Definition: plannodes.h:145

◆ convert_combining_aggrefs()

static Node * convert_combining_aggrefs ( Node node,
void *  context 
)
static

Definition at line 2232 of file setrefs.c.

References Aggref::aggdistinct, Aggref::aggfilter, Aggref::aggorder, AGGSPLIT_FINAL_DESERIAL, AGGSPLIT_INITIAL_SERIAL, Aggref::args, Assert, copyObject, expression_tree_mutator(), IsA, list_make1, makeNode, makeTargetEntry(), mark_partial_aggref(), and NIL.

Referenced by set_plan_refs().

2233 {
2234  if (node == NULL)
2235  return NULL;
2236  if (IsA(node, Aggref))
2237  {
2238  Aggref *orig_agg = (Aggref *) node;
2239  Aggref *child_agg;
2240  Aggref *parent_agg;
2241 
2242  /* Assert we've not chosen to partial-ize any unsupported cases */
2243  Assert(orig_agg->aggorder == NIL);
2244  Assert(orig_agg->aggdistinct == NIL);
2245 
2246  /*
2247  * Since aggregate calls can't be nested, we needn't recurse into the
2248  * arguments. But for safety, flat-copy the Aggref node itself rather
2249  * than modifying it in-place.
2250  */
2251  child_agg = makeNode(Aggref);
2252  memcpy(child_agg, orig_agg, sizeof(Aggref));
2253 
2254  /*
2255  * For the parent Aggref, we want to copy all the fields of the
2256  * original aggregate *except* the args list, which we'll replace
2257  * below, and the aggfilter expression, which should be applied only
2258  * by the child not the parent. Rather than explicitly knowing about
2259  * all the other fields here, we can momentarily modify child_agg to
2260  * provide a suitable source for copyObject.
2261  */
2262  child_agg->args = NIL;
2263  child_agg->aggfilter = NULL;
2264  parent_agg = copyObject(child_agg);
2265  child_agg->args = orig_agg->args;
2266  child_agg->aggfilter = orig_agg->aggfilter;
2267 
2268  /*
2269  * Now, set up child_agg to represent the first phase of partial
2270  * aggregation. For now, assume serialization is required.
2271  */
2273 
2274  /*
2275  * And set up parent_agg to represent the second phase.
2276  */
2277  parent_agg->args = list_make1(makeTargetEntry((Expr *) child_agg,
2278  1, NULL, false));
2280 
2281  return (Node *) parent_agg;
2282  }
2284  (void *) context);
2285 }
List * aggdistinct
Definition: primnodes.h:332
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2598
void mark_partial_aggref(Aggref *agg, AggSplit aggsplit)
Definition: planner.c:4894
Definition: nodes.h:539
List * args
Definition: primnodes.h:330
#define list_make1(x1)
Definition: pg_list.h:206
List * aggorder
Definition: primnodes.h:331
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
static Node * convert_combining_aggrefs(Node *node, void *context)
Definition: setrefs.c:2232
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
Expr * aggfilter
Definition: primnodes.h:333
#define copyObject(obj)
Definition: nodes.h:655

◆ copyVar()

static Var* copyVar ( Var var)
inlinestatic

Definition at line 1617 of file setrefs.c.

References palloc().

Referenced by fix_join_expr_mutator(), fix_scan_expr_mutator(), and search_indexed_tlist_for_var().

1618 {
1619  Var *newvar = (Var *) palloc(sizeof(Var));
1620 
1621  *newvar = *var;
1622  return newvar;
1623 }
Definition: primnodes.h:186
void * palloc(Size size)
Definition: mcxt.c:1062

◆ extract_query_dependencies()

void extract_query_dependencies ( Node query,
List **  relationOids,
List **  invalItems,
bool hasRowSecurity 
)

Definition at line 3024 of file setrefs.c.

References PlannerGlobal::dependsOnRole, extract_query_dependencies_walker(), PlannerInfo::glob, PlannerGlobal::invalItems, MemSet, NIL, PlannerGlobal::relationOids, T_PlannerGlobal, T_PlannerInfo, PlannerGlobal::type, and PlannerInfo::type.

Referenced by CompleteCachedPlan(), and RevalidateCachedQuery().

3028 {
3029  PlannerGlobal glob;
3030  PlannerInfo root;
3031 
3032  /* Make up dummy planner state so we can use this module's machinery */
3033  MemSet(&glob, 0, sizeof(glob));
3034  glob.type = T_PlannerGlobal;
3035  glob.relationOids = NIL;
3036  glob.invalItems = NIL;
3037  /* Hack: we use glob.dependsOnRole to collect hasRowSecurity flags */
3038  glob.dependsOnRole = false;
3039 
3040  MemSet(&root, 0, sizeof(root));
3041  root.type = T_PlannerInfo;
3042  root.glob = &glob;
3043 
3044  (void) extract_query_dependencies_walker(query, &root);
3045 
3046  *relationOids = glob.relationOids;
3047  *invalItems = glob.invalItems;
3048  *hasRowSecurity = glob.dependsOnRole;
3049 }
#define NIL
Definition: pg_list.h:65
#define MemSet(start, val, len)
Definition: c.h:1008
bool dependsOnRole
Definition: pathnodes.h:123
PlannerGlobal * glob
Definition: pathnodes.h:163
List * invalItems
Definition: pathnodes.h:111
NodeTag type
Definition: pathnodes.h:159
bool extract_query_dependencies_walker(Node *node, PlannerInfo *context)
Definition: setrefs.c:3060
NodeTag type
Definition: pathnodes.h:91
List * relationOids
Definition: pathnodes.h:109

◆ extract_query_dependencies_walker()

bool extract_query_dependencies_walker ( Node node,
PlannerInfo context 
)

Definition at line 3060 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().

3061 {
3062  if (node == NULL)
3063  return false;
3064  Assert(!IsA(node, PlaceHolderVar));
3065  if (IsA(node, Query))
3066  {
3067  Query *query = (Query *) node;
3068  ListCell *lc;
3069 
3070  if (query->commandType == CMD_UTILITY)
3071  {
3072  /*
3073  * Ignore utility statements, except those (such as EXPLAIN) that
3074  * contain a parsed-but-not-planned query.
3075  */
3076  query = UtilityContainsQuery(query->utilityStmt);
3077  if (query == NULL)
3078  return false;
3079  }
3080 
3081  /* Remember if any Query has RLS quals applied by rewriter */
3082  if (query->hasRowSecurity)
3083  context->glob->dependsOnRole = true;
3084 
3085  /* Collect relation OIDs in this Query's rtable */
3086  foreach(lc, query->rtable)
3087  {
3088  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
3089 
3090  if (rte->rtekind == RTE_RELATION)
3091  context->glob->relationOids =
3092  lappend_oid(context->glob->relationOids, rte->relid);
3093  else if (rte->rtekind == RTE_NAMEDTUPLESTORE &&
3094  OidIsValid(rte->relid))
3095  context->glob->relationOids =
3096  lappend_oid(context->glob->relationOids,
3097  rte->relid);
3098  }
3099 
3100  /* And recurse into the query's subexpressions */
3102  (void *) context, 0);
3103  }
3104  /* Extract function dependencies and check for regclass Consts */
3105  fix_expr_common(context, node);
3107  (void *) context);
3108 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Node * utilityStmt
Definition: parsenodes.h:128
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define OidIsValid(objectId)
Definition: c.h:710
Query * UtilityContainsQuery(Node *parsetree)
Definition: utility.c:2118
bool dependsOnRole
Definition: pathnodes.h:123
List * rtable
Definition: parsenodes.h:147
PlannerGlobal * glob
Definition: pathnodes.h:163
bool extract_query_dependencies_walker(Node *node, PlannerInfo *context)
Definition: setrefs.c:3060
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1639
CmdType commandType
Definition: parsenodes.h:120
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
List * relationOids
Definition: pathnodes.h:109
RTEKind rtekind
Definition: parsenodes.h:995
bool hasRowSecurity
Definition: parsenodes.h:141

◆ fix_alternative_subplan()

static Node* fix_alternative_subplan ( PlannerInfo root,
AlternativeSubPlan asplan,
double  num_exec 
)
static

Definition at line 1763 of file setrefs.c.

References Assert, PlannerInfo::glob, lfirst, list_nth_cell(), NIL, SubPlan::per_call_cost, SubPlan::plan_id, SubPlan::startup_cost, PlannerGlobal::subplans, and AlternativeSubPlan::subplans.

Referenced by fix_join_expr_mutator(), fix_scan_expr_mutator(), and fix_upper_expr_mutator().

1765 {
1766  SubPlan *bestplan = NULL;
1767  Cost bestcost = 0;
1768  ListCell *lc;
1769 
1770  /*
1771  * Compute the estimated cost of each subplan assuming num_exec
1772  * executions, and keep the cheapest one. Replace discarded subplans with
1773  * NULL pointers in the global subplans list. In event of exact equality
1774  * of estimates, we prefer the later plan; this is a bit arbitrary, but in
1775  * current usage it biases us to break ties against fast-start subplans.
1776  */
1777  Assert(asplan->subplans != NIL);
1778 
1779  foreach(lc, asplan->subplans)
1780  {
1781  SubPlan *curplan = (SubPlan *) lfirst(lc);
1782  Cost curcost;
1783 
1784  curcost = curplan->startup_cost + num_exec * curplan->per_call_cost;
1785  if (bestplan == NULL)
1786  {
1787  bestplan = curplan;
1788  bestcost = curcost;
1789  }
1790  else if (curcost <= bestcost)
1791  {
1792  /* drop old bestplan */
1793  ListCell *lc2 = list_nth_cell(root->glob->subplans,
1794  bestplan->plan_id - 1);
1795 
1796  lfirst(lc2) = NULL;
1797  bestplan = curplan;
1798  bestcost = curcost;
1799  }
1800  else
1801  {
1802  /* drop curplan */
1803  ListCell *lc2 = list_nth_cell(root->glob->subplans,
1804  curplan->plan_id - 1);
1805 
1806  lfirst(lc2) = NULL;
1807  }
1808  }
1809 
1810  return (Node *) bestplan;
1811 }
#define NIL
Definition: pg_list.h:65
int plan_id
Definition: primnodes.h:742
static ListCell * list_nth_cell(const List *list, int n)
Definition: pg_list.h:256
Definition: nodes.h:539
List * subplans
Definition: pathnodes.h:95
PlannerGlobal * glob
Definition: pathnodes.h:163
Cost per_call_cost
Definition: primnodes.h:766
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Cost startup_cost
Definition: primnodes.h:765
double Cost
Definition: nodes.h:673

◆ fix_expr_common()

static void fix_expr_common ( PlannerInfo root,
Node node 
)
static

Definition at line 1639 of file setrefs.c.

References Assert, GroupingFunc::cols, Const::constvalue, DatumGetObjectId, equal(), PlannerInfo::glob, PlannerInfo::grouping_map, ScalarArrayOpExpr::hashfuncid, IsA, ISREGCLASSCONST, lappend_int(), lappend_oid(), lfirst_int, NIL, OidIsValid, ScalarArrayOpExpr::opfuncid, record_plan_function_dependency(), GroupingFunc::refs, PlannerGlobal::relationOids, set_opfuncid(), and set_sa_opfuncid().

Referenced by extract_query_dependencies_walker(), fix_join_expr_mutator(), fix_scan_expr_mutator(), fix_scan_expr_walker(), and fix_upper_expr_mutator().

1640 {
1641  /* We assume callers won't call us on a NULL pointer */
1642  if (IsA(node, Aggref))
1643  {
1645  ((Aggref *) node)->aggfnoid);
1646  }
1647  else if (IsA(node, WindowFunc))
1648  {
1650  ((WindowFunc *) node)->winfnoid);
1651  }
1652  else if (IsA(node, FuncExpr))
1653  {
1655  ((FuncExpr *) node)->funcid);
1656  }
1657  else if (IsA(node, OpExpr))
1658  {
1659  set_opfuncid((OpExpr *) node);
1661  ((OpExpr *) node)->opfuncid);
1662  }
1663  else if (IsA(node, DistinctExpr))
1664  {
1665  set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1667  ((DistinctExpr *) node)->opfuncid);
1668  }
1669  else if (IsA(node, NullIfExpr))
1670  {
1671  set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1673  ((NullIfExpr *) node)->opfuncid);
1674  }
1675  else if (IsA(node, ScalarArrayOpExpr))
1676  {
1677  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) node;
1678 
1679  set_sa_opfuncid(saop);
1681 
1682  if (!OidIsValid(saop->hashfuncid))
1684  }
1685  else if (IsA(node, Const))
1686  {
1687  Const *con = (Const *) node;
1688 
1689  /* Check for regclass reference */
1690  if (ISREGCLASSCONST(con))
1691  root->glob->relationOids =
1692  lappend_oid(root->glob->relationOids,
1694  }
1695  else if (IsA(node, GroupingFunc))
1696  {
1697  GroupingFunc *g = (GroupingFunc *) node;
1698  AttrNumber *grouping_map = root->grouping_map;
1699 
1700  /* If there are no grouping sets, we don't need this. */
1701 
1702  Assert(grouping_map || g->cols == NIL);
1703 
1704  if (grouping_map)
1705  {
1706  ListCell *lc;
1707  List *cols = NIL;
1708 
1709  foreach(lc, g->refs)
1710  {
1711  cols = lappend_int(cols, grouping_map[lfirst_int(lc)]);
1712  }
1713 
1714  Assert(!g->cols || equal(cols, g->cols));
1715 
1716  if (!g->cols)
1717  g->cols = cols;
1718  }
1719  }
1720 }
Datum constvalue
Definition: primnodes.h:219
#define NIL
Definition: pg_list.h:65
#define ISREGCLASSCONST(con)
Definition: setrefs.c:100
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
List * refs
Definition: primnodes.h:374
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3113
#define DatumGetObjectId(X)
Definition: postgres.h:544
AttrNumber * grouping_map
Definition: pathnodes.h:331
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define OidIsValid(objectId)
Definition: c.h:710
#define lfirst_int(lc)
Definition: pg_list.h:170
PlannerGlobal * glob
Definition: pathnodes.h:163
List * lappend_int(List *list, int datum)
Definition: list.c:354
List * cols
Definition: primnodes.h:375
void record_plan_function_dependency(PlannerInfo *root, Oid funcid)
Definition: setrefs.c:2943
#define Assert(condition)
Definition: c.h:804
List * relationOids
Definition: pathnodes.h:109
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1683
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
void set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
Definition: nodeFuncs.c:1694

◆ fix_join_expr()

static List * fix_join_expr ( PlannerInfo root,
List clauses,
indexed_tlist outer_itlist,
indexed_tlist inner_itlist,
Index  acceptable_rel,
int  rtoffset,
double  num_exec 
)
static

Definition at line 2614 of file setrefs.c.

References fix_join_expr_context::acceptable_rel, fix_join_expr_mutator(), fix_join_expr_context::inner_itlist, fix_join_expr_context::num_exec, fix_join_expr_context::outer_itlist, fix_join_expr_context::root, and fix_join_expr_context::rtoffset.

Referenced by set_join_references(), set_plan_refs(), and set_returning_clause_references().

2621 {
2622  fix_join_expr_context context;
2623 
2624  context.root = root;
2625  context.outer_itlist = outer_itlist;
2626  context.inner_itlist = inner_itlist;
2627  context.acceptable_rel = acceptable_rel;
2628  context.rtoffset = rtoffset;
2629  context.num_exec = num_exec;
2630  return (List *) fix_join_expr_mutator((Node *) clauses, &context);
2631 }
indexed_tlist * inner_itlist
Definition: setrefs.c:59
Definition: nodes.h:539
static Node * fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
Definition: setrefs.c:2634
PlannerInfo * root
Definition: setrefs.c:57
indexed_tlist * outer_itlist
Definition: setrefs.c:58
Definition: pg_list.h:50

◆ fix_join_expr_mutator()

static Node * fix_join_expr_mutator ( Node node,
fix_join_expr_context context 
)
static

Definition at line 2634 of file setrefs.c.

References fix_join_expr_context::acceptable_rel, copyVar(), elog, ERROR, expression_tree_mutator(), fix_alternative_subplan(), fix_expr_common(), fix_param_node(), indexed_tlist::has_non_vars, indexed_tlist::has_ph_vars, fix_join_expr_context::inner_itlist, INNER_VAR, IsA, fix_join_expr_context::num_exec, fix_join_expr_context::outer_itlist, OUTER_VAR, PlaceHolderVar::phexpr, fix_join_expr_context::root, fix_join_expr_context::rtoffset, search_indexed_tlist_for_non_var(), search_indexed_tlist_for_var(), Var::varno, and Var::varnosyn.

Referenced by fix_join_expr().

2635 {
2636  Var *newvar;
2637 
2638  if (node == NULL)
2639  return NULL;
2640  if (IsA(node, Var))
2641  {
2642  Var *var = (Var *) node;
2643 
2644  /* Look for the var in the input tlists, first in the outer */
2645  if (context->outer_itlist)
2646  {
2647  newvar = search_indexed_tlist_for_var(var,
2648  context->outer_itlist,
2649  OUTER_VAR,
2650  context->rtoffset);
2651  if (newvar)
2652  return (Node *) newvar;
2653  }
2654 
2655  /* then in the inner. */
2656  if (context->inner_itlist)
2657  {
2658  newvar = search_indexed_tlist_for_var(var,
2659  context->inner_itlist,
2660  INNER_VAR,
2661  context->rtoffset);
2662  if (newvar)
2663  return (Node *) newvar;
2664  }
2665 
2666  /* If it's for acceptable_rel, adjust and return it */
2667  if (var->varno == context->acceptable_rel)
2668  {
2669  var = copyVar(var);
2670  var->varno += context->rtoffset;
2671  if (var->varnosyn > 0)
2672  var->varnosyn += context->rtoffset;
2673  return (Node *) var;
2674  }
2675 
2676  /* No referent found for Var */
2677  elog(ERROR, "variable not found in subplan target lists");
2678  }
2679  if (IsA(node, PlaceHolderVar))
2680  {
2681  PlaceHolderVar *phv = (PlaceHolderVar *) node;
2682 
2683  /* See if the PlaceHolderVar has bubbled up from a lower plan node */
2684  if (context->outer_itlist && context->outer_itlist->has_ph_vars)
2685  {
2686  newvar = search_indexed_tlist_for_non_var((Expr *) phv,
2687  context->outer_itlist,
2688  OUTER_VAR);
2689  if (newvar)
2690  return (Node *) newvar;
2691  }
2692  if (context->inner_itlist && context->inner_itlist->has_ph_vars)
2693  {
2694  newvar = search_indexed_tlist_for_non_var((Expr *) phv,
2695  context->inner_itlist,
2696  INNER_VAR);
2697  if (newvar)
2698  return (Node *) newvar;
2699  }
2700 
2701  /* If not supplied by input plans, evaluate the contained expr */
2702  return fix_join_expr_mutator((Node *) phv->phexpr, context);
2703  }
2704  /* Try matching more complex expressions too, if tlists have any */
2705  if (context->outer_itlist && context->outer_itlist->has_non_vars)
2706  {
2707  newvar = search_indexed_tlist_for_non_var((Expr *) node,
2708  context->outer_itlist,
2709  OUTER_VAR);
2710  if (newvar)
2711  return (Node *) newvar;
2712  }
2713  if (context->inner_itlist && context->inner_itlist->has_non_vars)
2714  {
2715  newvar = search_indexed_tlist_for_non_var((Expr *) node,
2716  context->inner_itlist,
2717  INNER_VAR);
2718  if (newvar)
2719  return (Node *) newvar;
2720  }
2721  /* Special cases (apply only AFTER failing to match to lower tlist) */
2722  if (IsA(node, Param))
2723  return fix_param_node(context->root, (Param *) node);
2724  if (IsA(node, AlternativeSubPlan))
2726  (AlternativeSubPlan *) node,
2727  context->num_exec),
2728  context);
2729  fix_expr_common(context->root, node);
2730  return expression_tree_mutator(node,
2732  (void *) context);
2733 }
static Node * fix_alternative_subplan(PlannerInfo *root, AlternativeSubPlan *asplan, double num_exec)
Definition: setrefs.c:1763
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
indexed_tlist * inner_itlist
Definition: setrefs.c:59
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2598
static Node * fix_param_node(PlannerInfo *root, Param *p)
Definition: setrefs.c:1731
Definition: nodes.h:539
Definition: primnodes.h:186
static Node * fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
Definition: setrefs.c:2634
bool has_ph_vars
Definition: setrefs.c:43
#define ERROR
Definition: elog.h:46
static Var * copyVar(Var *var)
Definition: setrefs.c:1617
PlannerInfo * root
Definition: setrefs.c:57
Index varnosyn
Definition: primnodes.h:199
Index varno
Definition: primnodes.h:189
bool has_non_vars
Definition: setrefs.c:44
static Var * search_indexed_tlist_for_non_var(Expr *node, indexed_tlist *itlist, Index newvarno)
Definition: setrefs.c:2509
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1639
#define INNER_VAR
Definition: primnodes.h:175
static Var * search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist, Index newvarno, int rtoffset)
Definition: setrefs.c:2469
#define elog(elevel,...)
Definition: elog.h:232
indexed_tlist * outer_itlist
Definition: setrefs.c:58
#define OUTER_VAR
Definition: primnodes.h:176

◆ fix_param_node()

static Node* fix_param_node ( PlannerInfo root,
Param p 
)
static

Definition at line 1731 of file setrefs.c.

References copyObject, elog, ERROR, list_length(), list_nth(), PlannerInfo::multiexpr_params, PARAM_MULTIEXPR, Param::paramid, and Param::paramkind.

Referenced by fix_join_expr_mutator(), fix_scan_expr_mutator(), and fix_upper_expr_mutator().

1732 {
1733  if (p->paramkind == PARAM_MULTIEXPR)
1734  {
1735  int subqueryid = p->paramid >> 16;
1736  int colno = p->paramid & 0xFFFF;
1737  List *params;
1738 
1739  if (subqueryid <= 0 ||
1740  subqueryid > list_length(root->multiexpr_params))
1741  elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
1742  params = (List *) list_nth(root->multiexpr_params, subqueryid - 1);
1743  if (colno <= 0 || colno > list_length(params))
1744  elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
1745  return copyObject(list_nth(params, colno - 1));
1746  }
1747  return (Node *) copyObject(p);
1748 }
ParamKind paramkind
Definition: primnodes.h:267
Definition: nodes.h:539
List * multiexpr_params
Definition: pathnodes.h:245
#define ERROR
Definition: elog.h:46
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
int paramid
Definition: primnodes.h:268
static int list_length(const List *l)
Definition: pg_list.h:149
#define elog(elevel,...)
Definition: elog.h:232
#define copyObject(obj)
Definition: nodes.h:655
Definition: pg_list.h:50

◆ fix_scan_expr()

static Node * fix_scan_expr ( PlannerInfo root,
Node node,
int  rtoffset,
double  num_exec 
)
static

Definition at line 1832 of file setrefs.c.

References fix_scan_expr_mutator(), fix_scan_expr_walker(), PlannerInfo::glob, PlannerInfo::hasAlternativeSubPlans, PlannerGlobal::lastPHId, PlannerInfo::minmax_aggs, PlannerInfo::multiexpr_params, NIL, fix_scan_expr_context::num_exec, fix_scan_expr_context::root, and fix_scan_expr_context::rtoffset.

Referenced by set_plan_refs().

1833 {
1834  fix_scan_expr_context context;
1835 
1836  context.root = root;
1837  context.rtoffset = rtoffset;
1838  context.num_exec = num_exec;
1839 
1840  if (rtoffset != 0 ||
1841  root->multiexpr_params != NIL ||
1842  root->glob->lastPHId != 0 ||
1843  root->minmax_aggs != NIL ||
1844  root->hasAlternativeSubPlans)
1845  {
1846  return fix_scan_expr_mutator(node, &context);
1847  }
1848  else
1849  {
1850  /*
1851  * If rtoffset == 0, we don't need to change any Vars, and if there
1852  * are no MULTIEXPR subqueries then we don't need to replace
1853  * PARAM_MULTIEXPR Params, and if there are no placeholders anywhere
1854  * we won't need to remove them, and if there are no minmax Aggrefs we
1855  * won't need to replace them, and if there are no AlternativeSubPlans
1856  * we won't need to remove them. Then it's OK to just scribble on the
1857  * input node tree instead of copying (since the only change, filling
1858  * in any unset opfuncid fields, is harmless). This saves just enough
1859  * cycles to be noticeable on trivial queries.
1860  */
1861  (void) fix_scan_expr_walker(node, &context);
1862  return node;
1863  }
1864 }
#define NIL
Definition: pg_list.h:65
PlannerInfo * root
Definition: setrefs.c:50
List * minmax_aggs
Definition: pathnodes.h:332
List * multiexpr_params
Definition: pathnodes.h:245
bool hasAlternativeSubPlans
Definition: pathnodes.h:350
PlannerGlobal * glob
Definition: pathnodes.h:163
static bool fix_scan_expr_walker(Node *node, fix_scan_expr_context *context)
Definition: setrefs.c:1942
static Node * fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
Definition: setrefs.c:1867
Index lastPHId
Definition: pathnodes.h:115

◆ fix_scan_expr_mutator()

static Node * fix_scan_expr_mutator ( Node node,
fix_scan_expr_context context 
)
static

Definition at line 1867 of file setrefs.c.

References Aggref::aggfnoid, MinMaxAggInfo::aggfnoid, Aggref::args, Assert, copyObject, copyVar(), CurrentOfExpr::cvarno, equal(), TargetEntry::expr, expression_tree_mutator(), fix_alternative_subplan(), fix_expr_common(), fix_param_node(), INNER_VAR, IS_SPECIAL_VARNO, IsA, lfirst, linitial, list_length(), PlannerInfo::minmax_aggs, NIL, fix_scan_expr_context::num_exec, OUTER_VAR, MinMaxAggInfo::param, PlaceHolderVar::phexpr, fix_scan_expr_context::root, ROWID_VAR, fix_scan_expr_context::rtoffset, MinMaxAggInfo::target, Var::varlevelsup, Var::varno, and Var::varnosyn.

Referenced by fix_scan_expr().

1868 {
1869  if (node == NULL)
1870  return NULL;
1871  if (IsA(node, Var))
1872  {
1873  Var *var = copyVar((Var *) node);
1874 
1875  Assert(var->varlevelsup == 0);
1876 
1877  /*
1878  * We should not see Vars marked INNER_VAR, OUTER_VAR, or ROWID_VAR.
1879  * But an indexqual expression could contain INDEX_VAR Vars.
1880  */
1881  Assert(var->varno != INNER_VAR);
1882  Assert(var->varno != OUTER_VAR);
1883  Assert(var->varno != ROWID_VAR);
1884  if (!IS_SPECIAL_VARNO(var->varno))
1885  var->varno += context->rtoffset;
1886  if (var->varnosyn > 0)
1887  var->varnosyn += context->rtoffset;
1888  return (Node *) var;
1889  }
1890  if (IsA(node, Param))
1891  return fix_param_node(context->root, (Param *) node);
1892  if (IsA(node, Aggref))
1893  {
1894  Aggref *aggref = (Aggref *) node;
1895 
1896  /* See if the Aggref should be replaced by a Param */
1897  if (context->root->minmax_aggs != NIL &&
1898  list_length(aggref->args) == 1)
1899  {
1900  TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
1901  ListCell *lc;
1902 
1903  foreach(lc, context->root->minmax_aggs)
1904  {
1905  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
1906 
1907  if (mminfo->aggfnoid == aggref->aggfnoid &&
1908  equal(mminfo->target, curTarget->expr))
1909  return (Node *) copyObject(mminfo->param);
1910  }
1911  }
1912  /* If no match, just fall through to process it normally */
1913  }
1914  if (IsA(node, CurrentOfExpr))
1915  {
1916  CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
1917 
1918  Assert(cexpr->cvarno != INNER_VAR);
1919  Assert(cexpr->cvarno != OUTER_VAR);
1920  if (!IS_SPECIAL_VARNO(cexpr->cvarno))
1921  cexpr->cvarno += context->rtoffset;
1922  return (Node *) cexpr;
1923  }
1924  if (IsA(node, PlaceHolderVar))
1925  {
1926  /* At scan level, we should always just evaluate the contained expr */
1927  PlaceHolderVar *phv = (PlaceHolderVar *) node;
1928 
1929  return fix_scan_expr_mutator((Node *) phv->phexpr, context);
1930  }
1931  if (IsA(node, AlternativeSubPlan))
1933  (AlternativeSubPlan *) node,
1934  context->num_exec),
1935  context);
1936  fix_expr_common(context->root, node);
1938  (void *) context);
1939 }
#define NIL
Definition: pg_list.h:65
static Node * fix_alternative_subplan(PlannerInfo *root, AlternativeSubPlan *asplan, double num_exec)
Definition: setrefs.c:1763
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Index varlevelsup
Definition: primnodes.h:196
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2598
static Node * fix_param_node(PlannerInfo *root, Param *p)
Definition: setrefs.c:1731
#define ROWID_VAR
Definition: primnodes.h:178
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3113
Param * param
Definition: pathnodes.h:2423
PlannerInfo * root
Definition: setrefs.c:50
Definition: nodes.h:539
List * args
Definition: primnodes.h:330
List * minmax_aggs
Definition: pathnodes.h:332
Definition: primnodes.h:186
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:180
#define linitial(l)
Definition: pg_list.h:174
static Var * copyVar(Var *var)
Definition: setrefs.c:1617
Index varnosyn
Definition: primnodes.h:199
Index varno
Definition: primnodes.h:189
static Node * fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
Definition: setrefs.c:1867
Oid aggfnoid
Definition: primnodes.h:323
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1639
#define INNER_VAR
Definition: primnodes.h:175
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Expr * target
Definition: pathnodes.h:2419
Expr * expr
Definition: primnodes.h:1444
static int list_length(const List *l)
Definition: pg_list.h:149
#define copyObject(obj)
Definition: nodes.h:655
#define OUTER_VAR
Definition: primnodes.h:176

◆ fix_scan_expr_walker()

static bool fix_scan_expr_walker ( Node node,
fix_scan_expr_context context 
)
static

Definition at line 1942 of file setrefs.c.

References Assert, expression_tree_walker(), fix_expr_common(), IsA, fix_scan_expr_context::root, and ROWID_VAR.

Referenced by fix_scan_expr().

1943 {
1944  if (node == NULL)
1945  return false;
1946  Assert(!(IsA(node, Var) && ((Var *) node)->varno == ROWID_VAR));
1947  Assert(!IsA(node, PlaceHolderVar));
1948  Assert(!IsA(node, AlternativeSubPlan));
1949  fix_expr_common(context->root, node);
1951  (void *) context);
1952 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
#define ROWID_VAR
Definition: primnodes.h:178
PlannerInfo * root
Definition: setrefs.c:50
Definition: primnodes.h:186
static bool fix_scan_expr_walker(Node *node, fix_scan_expr_context *context)
Definition: setrefs.c:1942
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1639
#define Assert(condition)
Definition: c.h:804
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904

◆ fix_upper_expr()

static Node * fix_upper_expr ( PlannerInfo root,
Node node,
indexed_tlist subplan_itlist,
Index  newvarno,
int  rtoffset,
double  num_exec 
)
static

Definition at line 2767 of file setrefs.c.

References fix_upper_expr_mutator(), fix_upper_expr_context::newvarno, fix_upper_expr_context::num_exec, fix_upper_expr_context::root, fix_upper_expr_context::rtoffset, and fix_upper_expr_context::subplan_itlist.

Referenced by set_customscan_references(), set_foreignscan_references(), set_hash_references(), set_indexonlyscan_references(), set_join_references(), and set_upper_references().

2773 {
2774  fix_upper_expr_context context;
2775 
2776  context.root = root;
2777  context.subplan_itlist = subplan_itlist;
2778  context.newvarno = newvarno;
2779  context.rtoffset = rtoffset;
2780  context.num_exec = num_exec;
2781  return fix_upper_expr_mutator(node, &context);
2782 }
static Node * fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
Definition: setrefs.c:2785
PlannerInfo * root
Definition: setrefs.c:67
indexed_tlist * subplan_itlist
Definition: setrefs.c:68

◆ fix_upper_expr_mutator()

static Node * fix_upper_expr_mutator ( Node node,
fix_upper_expr_context context 
)
static

Definition at line 2785 of file setrefs.c.

References Aggref::aggfnoid, MinMaxAggInfo::aggfnoid, Aggref::args, copyObject, elog, equal(), ERROR, TargetEntry::expr, expression_tree_mutator(), fix_alternative_subplan(), fix_expr_common(), fix_param_node(), indexed_tlist::has_non_vars, indexed_tlist::has_ph_vars, IsA, lfirst, linitial, list_length(), PlannerInfo::minmax_aggs, fix_upper_expr_context::newvarno, NIL, fix_upper_expr_context::num_exec, MinMaxAggInfo::param, PlaceHolderVar::phexpr, fix_upper_expr_context::root, fix_upper_expr_context::rtoffset, search_indexed_tlist_for_non_var(), search_indexed_tlist_for_var(), fix_upper_expr_context::subplan_itlist, and MinMaxAggInfo::target.

Referenced by fix_upper_expr().

2786 {
2787  Var *newvar;
2788 
2789  if (node == NULL)
2790  return NULL;
2791  if (IsA(node, Var))
2792  {
2793  Var *var = (Var *) node;
2794 
2795  newvar = search_indexed_tlist_for_var(var,
2796  context->subplan_itlist,
2797  context->newvarno,
2798  context->rtoffset);
2799  if (!newvar)
2800  elog(ERROR, "variable not found in subplan target list");
2801  return (Node *) newvar;
2802  }
2803  if (IsA(node, PlaceHolderVar))
2804  {
2805  PlaceHolderVar *phv = (PlaceHolderVar *) node;
2806 
2807  /* See if the PlaceHolderVar has bubbled up from a lower plan node */
2808  if (context->subplan_itlist->has_ph_vars)
2809  {
2810  newvar = search_indexed_tlist_for_non_var((Expr *) phv,
2811  context->subplan_itlist,
2812  context->newvarno);
2813  if (newvar)
2814  return (Node *) newvar;
2815  }
2816  /* If not supplied by input plan, evaluate the contained expr */
2817  return fix_upper_expr_mutator((Node *) phv->phexpr, context);
2818  }
2819  /* Try matching more complex expressions too, if tlist has any */
2820  if (context->subplan_itlist->has_non_vars)
2821  {
2822  newvar = search_indexed_tlist_for_non_var((Expr *) node,
2823  context->subplan_itlist,
2824  context->newvarno);
2825  if (newvar)
2826  return (Node *) newvar;
2827  }
2828  /* Special cases (apply only AFTER failing to match to lower tlist) */
2829  if (IsA(node, Param))
2830  return fix_param_node(context->root, (Param *) node);
2831  if (IsA(node, Aggref))
2832  {
2833  Aggref *aggref = (Aggref *) node;
2834 
2835  /* See if the Aggref should be replaced by a Param */
2836  if (context->root->minmax_aggs != NIL &&
2837  list_length(aggref->args) == 1)
2838  {
2839  TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
2840  ListCell *lc;
2841 
2842  foreach(lc, context->root->minmax_aggs)
2843  {
2844  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2845 
2846  if (mminfo->aggfnoid == aggref->aggfnoid &&
2847  equal(mminfo->target, curTarget->expr))
2848  return (Node *) copyObject(mminfo->param);
2849  }
2850  }
2851  /* If no match, just fall through to process it normally */
2852  }
2853  if (IsA(node, AlternativeSubPlan))
2855  (AlternativeSubPlan *) node,
2856  context->num_exec),
2857  context);
2858  fix_expr_common(context->root, node);
2859  return expression_tree_mutator(node,
2861  (void *) context);
2862 }
#define NIL
Definition: pg_list.h:65
static Node * fix_alternative_subplan(PlannerInfo *root, AlternativeSubPlan *asplan, double num_exec)
Definition: setrefs.c:1763
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2598
static Node * fix_param_node(PlannerInfo *root, Param *p)
Definition: setrefs.c:1731
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3113
Param * param
Definition: pathnodes.h:2423
Definition: nodes.h:539
List * args
Definition: primnodes.h:330
List * minmax_aggs
Definition: pathnodes.h:332
Definition: primnodes.h:186
bool has_ph_vars
Definition: setrefs.c:43
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
static Node * fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
Definition: setrefs.c:2785
bool has_non_vars
Definition: setrefs.c:44
static Var * search_indexed_tlist_for_non_var(Expr *node, indexed_tlist *itlist, Index newvarno)
Definition: setrefs.c:2509
Oid aggfnoid
Definition: primnodes.h:323
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1639
#define lfirst(lc)
Definition: pg_list.h:169
Expr * target
Definition: pathnodes.h:2419
static Var * search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist, Index newvarno, int rtoffset)
Definition: setrefs.c:2469
Expr * expr
Definition: primnodes.h:1444
static int list_length(const List *l)
Definition: pg_list.h:149
#define elog(elevel,...)
Definition: elog.h:232
PlannerInfo * root
Definition: setrefs.c:67
#define copyObject(obj)
Definition: nodes.h:655
indexed_tlist * subplan_itlist
Definition: setrefs.c:68

◆ flatten_rtes_walker()

static bool flatten_rtes_walker ( Node node,
PlannerGlobal glob 
)
static

Definition at line 410 of file setrefs.c.

References add_rte_to_flat_rtable(), expression_tree_walker(), IsA, QTW_EXAMINE_RTES_BEFORE, query_tree_walker(), RTE_RELATION, and RangeTblEntry::rtekind.

Referenced by flatten_unplanned_rtes().

411 {
412  if (node == NULL)
413  return false;
414  if (IsA(node, RangeTblEntry))
415  {
416  RangeTblEntry *rte = (RangeTblEntry *) node;
417 
418  /* As above, we need only save relation RTEs */
419  if (rte->rtekind == RTE_RELATION)
420  add_rte_to_flat_rtable(glob, rte);
421  return false;
422  }
423  if (IsA(node, Query))
424  {
425  /* Recurse into subselects */
426  return query_tree_walker((Query *) node,
428  (void *) glob,
430  }
432  (void *) glob);
433 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
#define QTW_EXAMINE_RTES_BEFORE
Definition: nodeFuncs.h:25
static void add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte)
Definition: setrefs.c:446
static bool flatten_rtes_walker(Node *node, PlannerGlobal *glob)
Definition: setrefs.c:410
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
RTEKind rtekind
Definition: parsenodes.h:995

◆ flatten_unplanned_rtes()

static void flatten_unplanned_rtes ( PlannerGlobal glob,
RangeTblEntry rte 
)
static

Definition at line 400 of file setrefs.c.

References flatten_rtes_walker(), QTW_EXAMINE_RTES_BEFORE, query_tree_walker(), and RangeTblEntry::subquery.

Referenced by add_rtes_to_flat_rtable().

401 {
402  /* Use query_tree_walker to find all RTEs in the parse tree */
403  (void) query_tree_walker(rte->subquery,
405  (void *) glob,
407 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355
#define QTW_EXAMINE_RTES_BEFORE
Definition: nodeFuncs.h:25
static bool flatten_rtes_walker(Node *node, PlannerGlobal *glob)
Definition: setrefs.c:410
Query * subquery
Definition: parsenodes.h:1030

◆ offset_relid_set()

static Relids offset_relid_set ( Relids  relids,
int  rtoffset 
)
static

Definition at line 1595 of file setrefs.c.

References bms_add_member(), and bms_next_member().

Referenced by set_append_references(), set_customscan_references(), set_foreignscan_references(), and set_mergeappend_references().

1596 {
1597  Relids result = NULL;
1598  int rtindex;
1599 
1600  /* If there's no offset to apply, we needn't recompute the value */
1601  if (rtoffset == 0)
1602  return relids;
1603  rtindex = -1;
1604  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
1605  result = bms_add_member(result, rtindex + rtoffset);
1606  return result;
1607 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736

◆ record_plan_function_dependency()

void record_plan_function_dependency ( PlannerInfo root,
Oid  funcid 
)

Definition at line 2943 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().

2944 {
2945  /*
2946  * For performance reasons, we don't bother to track built-in functions;
2947  * we just assume they'll never change (or at least not in ways that'd
2948  * invalidate plans using them). For this purpose we can consider a
2949  * built-in function to be one with OID less than FirstBootstrapObjectId.
2950  * Note that the OID generator guarantees never to generate such an OID
2951  * after startup, even at OID wraparound.
2952  */
2953  if (funcid >= (Oid) FirstBootstrapObjectId)
2954  {
2955  PlanInvalItem *inval_item = makeNode(PlanInvalItem);
2956 
2957  /*
2958  * It would work to use any syscache on pg_proc, but the easiest is
2959  * PROCOID since we already have the function's OID at hand. Note
2960  * that plancache.c knows we use PROCOID.
2961  */
2962  inval_item->cacheId = PROCOID;
2963  inval_item->hashValue = GetSysCacheHashValue1(PROCOID,
2964  ObjectIdGetDatum(funcid));
2965 
2966  root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
2967  }
2968 }
#define GetSysCacheHashValue1(cacheId, key1)
Definition: syscache.h:202
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
PlannerGlobal * glob
Definition: pathnodes.h:163
#define FirstBootstrapObjectId
Definition: transam.h:189
List * lappend(List *list, void *datum)
Definition: list.c:336
List * invalItems
Definition: pathnodes.h:111
uint32 hashValue
Definition: plannodes.h:1289
#define makeNode(_type_)
Definition: nodes.h:587

◆ record_plan_type_dependency()

void record_plan_type_dependency ( PlannerInfo root,
Oid  typid 
)

Definition at line 2983 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().

2984 {
2985  /*
2986  * As in record_plan_function_dependency, ignore the possibility that
2987  * someone would change a built-in domain.
2988  */
2989  if (typid >= (Oid) FirstBootstrapObjectId)
2990  {
2991  PlanInvalItem *inval_item = makeNode(PlanInvalItem);
2992 
2993  /*
2994  * It would work to use any syscache on pg_type, but the easiest is
2995  * TYPEOID since we already have the type's OID at hand. Note that
2996  * plancache.c knows we use TYPEOID.
2997  */
2998  inval_item->cacheId = TYPEOID;
2999  inval_item->hashValue = GetSysCacheHashValue1(TYPEOID,
3000  ObjectIdGetDatum(typid));
3001 
3002  root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
3003  }
3004 }
#define GetSysCacheHashValue1(cacheId, key1)
Definition: syscache.h:202
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
PlannerGlobal * glob
Definition: pathnodes.h:163
#define FirstBootstrapObjectId
Definition: transam.h:189
List * lappend(List *list, void *datum)
Definition: list.c:336
List * invalItems
Definition: pathnodes.h:111
uint32 hashValue
Definition: plannodes.h:1289
#define makeNode(_type_)
Definition: nodes.h:587

◆ search_indexed_tlist_for_non_var()

static Var * search_indexed_tlist_for_non_var ( Expr node,
indexed_tlist itlist,
Index  newvarno 
)
static

Definition at line 2509 of file setrefs.c.

References IsA, makeVarFromTargetEntry(), indexed_tlist::tlist, tlist_member(), Var::varattnosyn, and Var::varnosyn.

Referenced by fix_join_expr_mutator(), and fix_upper_expr_mutator().

2511 {
2512  TargetEntry *tle;
2513 
2514  /*
2515  * If it's a simple Const, replacing it with a Var is silly, even if there
2516  * happens to be an identical Const below; a Var is more expensive to
2517  * execute than a Const. What's more, replacing it could confuse some
2518  * places in the executor that expect to see simple Consts for, eg,
2519  * dropped columns.
2520  */
2521  if (IsA(node, Const))
2522  return NULL;
2523 
2524  tle = tlist_member(node, itlist->tlist);
2525  if (tle)
2526  {
2527  /* Found a matching subplan output expression */
2528  Var *newvar;
2529 
2530  newvar = makeVarFromTargetEntry(newvarno, tle);
2531  newvar->varnosyn = 0; /* wasn't ever a plain Var */
2532  newvar->varattnosyn = 0;
2533  return newvar;
2534  }
2535  return NULL; /* no match */
2536 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Var * makeVarFromTargetEntry(Index varno, TargetEntry *tle)
Definition: makefuncs.c:103
Definition: primnodes.h:186
Index varnosyn
Definition: primnodes.h:199
AttrNumber varattnosyn
Definition: primnodes.h:200
TargetEntry * tlist_member(Expr *node, List *targetlist)
Definition: tlist.c:68
List * tlist
Definition: setrefs.c:41

◆ search_indexed_tlist_for_sortgroupref()

static Var * search_indexed_tlist_for_sortgroupref ( Expr node,
Index  sortgroupref,
indexed_tlist itlist,
Index  newvarno 
)
static

Definition at line 2549 of file setrefs.c.

References equal(), TargetEntry::expr, lfirst, makeVarFromTargetEntry(), TargetEntry::ressortgroupref, indexed_tlist::tlist, Var::varattnosyn, and Var::varnosyn.

Referenced by set_upper_references().

2553 {
2554  ListCell *lc;
2555 
2556  foreach(lc, itlist->tlist)
2557  {
2558  TargetEntry *tle = (TargetEntry *) lfirst(lc);
2559 
2560  /* The equal() check should be redundant, but let's be paranoid */
2561  if (tle->ressortgroupref == sortgroupref &&
2562  equal(node, tle->expr))
2563  {
2564  /* Found a matching subplan output expression */
2565  Var *newvar;
2566 
2567  newvar = makeVarFromTargetEntry(newvarno, tle);
2568  newvar->varnosyn = 0; /* wasn't ever a plain Var */
2569  newvar->varattnosyn = 0;
2570  return newvar;
2571  }
2572  }
2573  return NULL; /* no match */
2574 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3113
Var * makeVarFromTargetEntry(Index varno, TargetEntry *tle)
Definition: makefuncs.c:103
Definition: primnodes.h:186
Index varnosyn
Definition: primnodes.h:199
AttrNumber varattnosyn
Definition: primnodes.h:200
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1444
Index ressortgroupref
Definition: primnodes.h:1447
List * tlist
Definition: setrefs.c:41

◆ search_indexed_tlist_for_var()

static Var * search_indexed_tlist_for_var ( Var var,
indexed_tlist itlist,
Index  newvarno,
int  rtoffset 
)
static

Definition at line 2469 of file setrefs.c.

References copyVar(), i, indexed_tlist::num_vars, tlist_vinfo::resno, tlist_vinfo::varattno, Var::varattno, tlist_vinfo::varno, Var::varno, Var::varnosyn, and indexed_tlist::vars.

Referenced by fix_join_expr_mutator(), and fix_upper_expr_mutator().

2471 {
2472  Index varno = var->varno;
2473  AttrNumber varattno = var->varattno;
2474  tlist_vinfo *vinfo;
2475  int i;
2476 
2477  vinfo = itlist->vars;
2478  i = itlist->num_vars;
2479  while (i-- > 0)
2480  {
2481  if (vinfo->varno == varno && vinfo->varattno == varattno)
2482  {
2483  /* Found a match */
2484  Var *newvar = copyVar(var);
2485 
2486  newvar->varno = newvarno;
2487  newvar->varattno = vinfo->resno;
2488  if (newvar->varnosyn > 0)
2489  newvar->varnosyn += rtoffset;
2490  return newvar;
2491  }
2492  vinfo++;
2493  }
2494  return NULL; /* no match */
2495 }
Index varno
Definition: setrefs.c:34
tlist_vinfo vars[FLEXIBLE_ARRAY_MEMBER]
Definition: setrefs.c:45
AttrNumber resno
Definition: setrefs.c:36
AttrNumber varattno
Definition: primnodes.h:191
Definition: primnodes.h:186
AttrNumber varattno
Definition: setrefs.c:35
int num_vars
Definition: setrefs.c:42
static Var * copyVar(Var *var)
Definition: setrefs.c:1617
Index varnosyn
Definition: primnodes.h:199
Index varno
Definition: primnodes.h:189
unsigned int Index
Definition: c.h:549
int i
int16 AttrNumber
Definition: attnum.h:21

◆ set_append_references()

static Plan * set_append_references ( PlannerInfo root,
Append aplan,
int  rtoffset 
)
static

Definition at line 1438 of file setrefs.c.

References Append::appendplans, Assert, clean_up_removed_plan_level(), Plan::lefttree, lfirst, linitial, list_length(), NIL, offset_relid_set(), Append::plan, Plan::qual, Plan::righttree, PartitionedRelPruneInfo::rtindex, set_dummy_tlist_references(), and set_plan_refs().

Referenced by set_plan_refs().

1441 {
1442  ListCell *l;
1443 
1444  /*
1445  * Append, like Sort et al, doesn't actually evaluate its targetlist or
1446  * check quals. If it's got exactly one child plan, then it's not doing
1447  * anything useful at all, and we can strip it out.
1448  */
1449  Assert(aplan->plan.qual == NIL);
1450 
1451  /* First, we gotta recurse on the children */
1452  foreach(l, aplan->appendplans)
1453  {
1454  lfirst(l) = set_plan_refs(root, (Plan *) lfirst(l), rtoffset);
1455  }
1456 
1457  /* Now, if there's just one, forget the Append and return that child */
1458  if (list_length(aplan->appendplans) == 1)
1459  return clean_up_removed_plan_level((Plan *) aplan,
1460  (Plan *) linitial(aplan->appendplans));
1461 
1462  /*
1463  * Otherwise, clean up the Append as needed. It's okay to do this after
1464  * recursing to the children, because set_dummy_tlist_references doesn't
1465  * look at those.
1466  */
1467  set_dummy_tlist_references((Plan *) aplan, rtoffset);
1468 
1469  aplan->apprelids = offset_relid_set(aplan->apprelids, rtoffset);
1470 
1471  if (aplan->part_prune_info)
1472  {
1473  foreach(l, aplan->part_prune_info->prune_infos)
1474  {
1475  List *prune_infos = lfirst(l);
1476  ListCell *l2;
1477 
1478  foreach(l2, prune_infos)
1479  {
1480  PartitionedRelPruneInfo *pinfo = lfirst(l2);
1481 
1482  pinfo->rtindex += rtoffset;
1483  }
1484  }
1485  }
1486 
1487  /* We don't need to recurse to lefttree or righttree ... */
1488  Assert(aplan->plan.lefttree == NULL);
1489  Assert(aplan->plan.righttree == NULL);
1490 
1491  return (Plan *) aplan;
1492 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:142
static Relids offset_relid_set(Relids relids, int rtoffset)
Definition: setrefs.c:1595
static Plan * set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:500
List * appendplans
Definition: plannodes.h:253
struct Plan * righttree
Definition: plannodes.h:144
#define linitial(l)
Definition: pg_list.h:174
static Plan * clean_up_removed_plan_level(Plan *parent, Plan *child)
Definition: setrefs.c:1262
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
struct Plan * lefttree
Definition: plannodes.h:143
Definition: pg_list.h:50
static void set_dummy_tlist_references(Plan *plan, int rtoffset)
Definition: setrefs.c:2301
Plan plan
Definition: plannodes.h:251

◆ set_customscan_references()

static void set_customscan_references ( PlannerInfo root,
CustomScan cscan,
int  rtoffset 
)
static

Definition at line 1365 of file setrefs.c.

References build_tlist_index(), CustomScan::custom_plans, CustomScan::custom_relids, CustomScan::custom_scan_tlist, fix_scan_list, fix_upper_expr(), INDEX_VAR, lfirst, NIL, NUM_EXEC_QUAL, NUM_EXEC_TLIST, offset_relid_set(), pfree(), Scan::plan, Plan::qual, CustomScan::scan, Scan::scanrelid, set_plan_refs(), and Plan::targetlist.

Referenced by set_plan_refs().

1368 {
1369  ListCell *lc;
1370 
1371  /* Adjust scanrelid if it's valid */
1372  if (cscan->scan.scanrelid > 0)
1373  cscan->scan.scanrelid += rtoffset;
1374 
1375  if (cscan->custom_scan_tlist != NIL || cscan->scan.scanrelid == 0)
1376  {
1377  /* Adjust tlist, qual, custom_exprs to reference custom scan tuple */
1379 
1380  cscan->scan.plan.targetlist = (List *)
1381  fix_upper_expr(root,
1382  (Node *) cscan->scan.plan.targetlist,
1383  itlist,
1384  INDEX_VAR,
1385  rtoffset,
1386  NUM_EXEC_TLIST((Plan *) cscan));
1387  cscan->scan.plan.qual = (List *)
1388  fix_upper_expr(root,
1389  (Node *) cscan->scan.plan.qual,
1390  itlist,
1391  INDEX_VAR,
1392  rtoffset,
1393  NUM_EXEC_QUAL((Plan *) cscan));
1394  cscan->custom_exprs = (List *)
1395  fix_upper_expr(root,
1396  (Node *) cscan->custom_exprs,
1397  itlist,
1398  INDEX_VAR,
1399  rtoffset,
1400  NUM_EXEC_QUAL((Plan *) cscan));
1401  pfree(itlist);
1402  /* custom_scan_tlist itself just needs fix_scan_list() adjustments */
1403  cscan->custom_scan_tlist =
1404  fix_scan_list(root, cscan->custom_scan_tlist,
1405  rtoffset, NUM_EXEC_TLIST((Plan *) cscan));
1406  }
1407  else
1408  {
1409  /* Adjust tlist, qual, custom_exprs in the standard way */
1410  cscan->scan.plan.targetlist =
1411  fix_scan_list(root, cscan->scan.plan.targetlist,
1412  rtoffset, NUM_EXEC_TLIST((Plan *) cscan));
1413  cscan->scan.plan.qual =
1414  fix_scan_list(root, cscan->scan.plan.qual,
1415  rtoffset, NUM_EXEC_QUAL((Plan *) cscan));
1416  cscan->custom_exprs =
1417  fix_scan_list(root, cscan->custom_exprs,
1418  rtoffset, NUM_EXEC_QUAL((Plan *) cscan));
1419  }
1420 
1421  /* Adjust child plan-nodes recursively, if needed */
1422  foreach(lc, cscan->custom_plans)
1423  {
1424  lfirst(lc) = set_plan_refs(root, (Plan *) lfirst(lc), rtoffset);
1425  }
1426 
1427  cscan->custom_relids = offset_relid_set(cscan->custom_relids, rtoffset);
1428 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:142
#define fix_scan_list(root, lst, rtoffset, num_exec)
Definition: setrefs.c:104
Plan plan
Definition: plannodes.h:343
Index scanrelid
Definition: plannodes.h:344
Definition: nodes.h:539
List * custom_exprs
Definition: plannodes.h:666
static Node * fix_upper_expr(PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, Index newvarno, int rtoffset, double num_exec)
Definition: setrefs.c:2767
static Relids offset_relid_set(Relids relids, int rtoffset)
Definition: setrefs.c:1595
List * custom_plans
Definition: plannodes.h:665
static Plan * set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:500
void pfree(void *pointer)
Definition: mcxt.c:1169
#define NUM_EXEC_TLIST(parentplan)
Definition: setrefs.c:90
Scan scan
Definition: plannodes.h:662
#define NUM_EXEC_QUAL(parentplan)
Definition: setrefs.c:91
#define lfirst(lc)
Definition: pg_list.h:169
List * custom_scan_tlist
Definition: plannodes.h:668
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:2368
Bitmapset * custom_relids
Definition: plannodes.h:669
List * targetlist
Definition: plannodes.h:141
#define INDEX_VAR
Definition: primnodes.h:177
Definition: pg_list.h:50

◆ set_dummy_tlist_references()

static void set_dummy_tlist_references ( Plan plan,
int  rtoffset 
)
static

Definition at line 2301 of file setrefs.c.

References TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), flatCopyTargetEntry(), IsA, lappend(), lfirst, makeVar(), NIL, OUTER_VAR, TargetEntry::resno, and Plan::targetlist.

Referenced by set_append_references(), set_hash_references(), set_mergeappend_references(), and set_plan_refs().

2302 {
2303  List *output_targetlist;
2304  ListCell *l;
2305 
2306  output_targetlist = NIL;
2307  foreach(l, plan->targetlist)
2308  {
2309  TargetEntry *tle = (TargetEntry *) lfirst(l);
2310  Var *oldvar = (Var *) tle->expr;
2311  Var *newvar;
2312 
2313  /*
2314  * As in search_indexed_tlist_for_non_var(), we prefer to keep Consts
2315  * as Consts, not Vars referencing Consts. Here, there's no speed
2316  * advantage to be had, but it makes EXPLAIN output look cleaner, and
2317  * again it avoids confusing the executor.
2318  */
2319  if (IsA(oldvar, Const))
2320  {
2321  /* just reuse the existing TLE node */
2322  output_targetlist = lappend(output_targetlist, tle);
2323  continue;
2324  }
2325 
2326  newvar = makeVar(OUTER_VAR,
2327  tle->resno,
2328  exprType((Node *) oldvar),
2329  exprTypmod((Node *) oldvar),
2330  exprCollation((Node *) oldvar),
2331  0);
2332  if (IsA(oldvar, Var) &&
2333  oldvar->varnosyn > 0)
2334  {
2335  newvar->varnosyn = oldvar->varnosyn + rtoffset;
2336  newvar->varattnosyn = oldvar->varattnosyn;
2337  }
2338  else
2339  {
2340  newvar->varnosyn = 0; /* wasn't ever a plain Var */
2341  newvar->varattnosyn = 0;
2342  }
2343 
2344  tle = flatCopyTargetEntry(tle);
2345  tle->expr = (Expr *) newvar;
2346  output_targetlist = lappend(output_targetlist, tle);
2347  }
2348  plan->targetlist = output_targetlist;
2349 
2350  /* We don't touch plan->qual here */
2351 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
Definition: nodes.h:539
Definition: primnodes.h:186
AttrNumber resno
Definition: primnodes.h:1445
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
Definition: makefuncs.c:271
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
List * lappend(List *list, void *datum)
Definition: list.c:336
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1444
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
List * targetlist
Definition: plannodes.h:141
Definition: pg_list.h:50
#define OUTER_VAR
Definition: primnodes.h:176

◆ set_foreignscan_references()

static void set_foreignscan_references ( PlannerInfo root,
ForeignScan fscan,
int  rtoffset 
)
static

Definition at line 1283 of file setrefs.c.

References build_tlist_index(), ForeignScan::fdw_scan_tlist, fix_scan_list, fix_upper_expr(), ForeignScan::fs_relids, INDEX_VAR, NIL, NUM_EXEC_QUAL, NUM_EXEC_TLIST, offset_relid_set(), pfree(), Scan::plan, Plan::qual, ForeignScan::resultRelation, ForeignScan::scan, Scan::scanrelid, and Plan::targetlist.

Referenced by set_plan_refs().

1286 {
1287  /* Adjust scanrelid if it's valid */
1288  if (fscan->scan.scanrelid > 0)
1289  fscan->scan.scanrelid += rtoffset;
1290 
1291  if (fscan->fdw_scan_tlist != NIL || fscan->scan.scanrelid == 0)
1292  {
1293  /*
1294  * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals to reference
1295  * foreign scan tuple
1296  */
1298 
1299  fscan->scan.plan.targetlist = (List *)
1300  fix_upper_expr(root,
1301  (Node *) fscan->scan.plan.targetlist,
1302  itlist,
1303  INDEX_VAR,
1304  rtoffset,
1305  NUM_EXEC_TLIST((Plan *) fscan));
1306  fscan->scan.plan.qual = (List *)
1307  fix_upper_expr(root,
1308  (Node *) fscan->scan.plan.qual,
1309  itlist,
1310  INDEX_VAR,
1311  rtoffset,
1312  NUM_EXEC_QUAL((Plan *) fscan));
1313  fscan->fdw_exprs = (List *)
1314  fix_upper_expr(root,
1315  (Node *) fscan->fdw_exprs,
1316  itlist,
1317  INDEX_VAR,
1318  rtoffset,
1319  NUM_EXEC_QUAL((Plan *) fscan));
1320  fscan->fdw_recheck_quals = (List *)
1321  fix_upper_expr(root,
1322  (Node *) fscan->fdw_recheck_quals,
1323  itlist,
1324  INDEX_VAR,
1325  rtoffset,
1326  NUM_EXEC_QUAL((Plan *) fscan));
1327  pfree(itlist);
1328  /* fdw_scan_tlist itself just needs fix_scan_list() adjustments */
1329  fscan->fdw_scan_tlist =
1330  fix_scan_list(root, fscan->fdw_scan_tlist,
1331  rtoffset, NUM_EXEC_TLIST((Plan *) fscan));
1332  }
1333  else
1334  {
1335  /*
1336  * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals in the standard
1337  * way
1338  */
1339  fscan->scan.plan.targetlist =
1340  fix_scan_list(root, fscan->scan.plan.targetlist,
1341  rtoffset, NUM_EXEC_TLIST((Plan *) fscan));
1342  fscan->scan.plan.qual =
1343  fix_scan_list(root, fscan->scan.plan.qual,
1344  rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1345  fscan->fdw_exprs =
1346  fix_scan_list(root, fscan->fdw_exprs,
1347  rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1348  fscan->fdw_recheck_quals =
1349  fix_scan_list(root, fscan->fdw_recheck_quals,
1350  rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1351  }
1352 
1353  fscan->fs_relids = offset_relid_set(fscan->fs_relids, rtoffset);
1354 
1355  /* Adjust resultRelation if it's valid */
1356  if (fscan->resultRelation > 0)
1357  fscan->resultRelation += rtoffset;
1358 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:142
#define fix_scan_list(root, lst, rtoffset, num_exec)
Definition: setrefs.c:104
Plan plan
Definition: plannodes.h:343
Index scanrelid
Definition: plannodes.h:344
List * fdw_exprs
Definition: plannodes.h:637
Definition: nodes.h:539
static Node * fix_upper_expr(PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, Index newvarno, int rtoffset, double num_exec)
Definition: setrefs.c:2767
List * fdw_scan_tlist
Definition: plannodes.h:639
static Relids offset_relid_set(Relids relids, int rtoffset)
Definition: setrefs.c:1595
void pfree(void *pointer)
Definition: mcxt.c:1169
#define NUM_EXEC_TLIST(parentplan)
Definition: setrefs.c:90
List * fdw_recheck_quals
Definition: plannodes.h:640
Index resultRelation
Definition: plannodes.h:635
#define NUM_EXEC_QUAL(parentplan)
Definition: setrefs.c:91
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:2368
List * targetlist
Definition: plannodes.h:141
#define INDEX_VAR
Definition: primnodes.h:177
Definition: pg_list.h:50
Bitmapset * fs_relids
Definition: plannodes.h:641

◆ set_hash_references()

static void set_hash_references ( PlannerInfo root,
Plan plan,
int  rtoffset 
)
static

Definition at line 1563 of file setrefs.c.

References Assert, build_tlist_index(), fix_upper_expr(), Hash::hashkeys, Plan::lefttree, NIL, NUM_EXEC_QUAL, OUTER_VAR, Plan::qual, set_dummy_tlist_references(), and Plan::targetlist.

Referenced by set_plan_refs().

1564 {
1565  Hash *hplan = (Hash *) plan;
1566  Plan *outer_plan = plan->lefttree;
1567  indexed_tlist *outer_itlist;
1568 
1569  /*
1570  * Hash's hashkeys are used when feeding tuples into the hashtable,
1571  * therefore have them reference Hash's outer plan (which itself is the
1572  * inner plan of the HashJoin).
1573  */
1574  outer_itlist = build_tlist_index(outer_plan->targetlist);
1575  hplan->hashkeys = (List *)
1576  fix_upper_expr(root,
1577  (Node *) hplan->hashkeys,
1578  outer_itlist,
1579  OUTER_VAR,
1580  rtoffset,
1581  NUM_EXEC_QUAL(plan));
1582 
1583  /* Hash doesn't project */
1584  set_dummy_tlist_references(plan, rtoffset);
1585 
1586  /* Hash nodes don't have their own quals */
1587  Assert(plan->qual == NIL);
1588 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:142
Definition: nodes.h:539
static Node * fix_upper_expr(PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, Index newvarno, int rtoffset, double num_exec)
Definition: setrefs.c:2767
List * hashkeys
Definition: plannodes.h:971
#define NUM_EXEC_QUAL(parentplan)
Definition: setrefs.c:91
#define Assert(condition)
Definition: c.h:804
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:2368
struct Plan * lefttree
Definition: plannodes.h:143
List * targetlist
Definition: plannodes.h:141
Definition: pg_list.h:50
#define OUTER_VAR
Definition: primnodes.h:176
static void set_dummy_tlist_references(Plan *plan, int rtoffset)
Definition: setrefs.c:2301

◆ set_indexonlyscan_references()

static Plan * set_indexonlyscan_references ( PlannerInfo root,
IndexOnlyScan plan,
int  rtoffset 
)
static

Definition at line 1107 of file setrefs.c.

References build_tlist_index(), fix_scan_list, fix_upper_expr(), INDEX_VAR, IndexOnlyScan::indextlist, NUM_EXEC_QUAL, NUM_EXEC_TLIST, pfree(), Scan::plan, Plan::qual, IndexOnlyScan::scan, Scan::scanrelid, and Plan::targetlist.

Referenced by set_plan_refs().

1110 {
1111  indexed_tlist *index_itlist;
1112 
1113  index_itlist = build_tlist_index(plan->indextlist);
1114 
1115  plan->scan.scanrelid += rtoffset;
1116  plan->scan.plan.targetlist = (List *)
1117  fix_upper_expr(root,
1118  (Node *) plan->scan.plan.targetlist,
1119  index_itlist,
1120  INDEX_VAR,
1121  rtoffset,
1122  NUM_EXEC_TLIST((Plan *) plan));
1123  plan->scan.plan.qual = (List *)
1124  fix_upper_expr(root,
1125  (Node *) plan->scan.plan.qual,
1126  index_itlist,
1127  INDEX_VAR,
1128  rtoffset,
1129  NUM_EXEC_QUAL((Plan *) plan));
1130  /* indexqual is already transformed to reference index columns */
1131  plan->indexqual = fix_scan_list(root, plan->indexqual,
1132  rtoffset, 1);
1133  /* indexorderby is already transformed to reference index columns */
1134  plan->indexorderby = fix_scan_list(root, plan->indexorderby,
1135  rtoffset, 1);
1136  /* indextlist must NOT be transformed to reference index columns */
1137  plan->indextlist = fix_scan_list(root, plan->indextlist,
1138  rtoffset, NUM_EXEC_TLIST((Plan *) plan));
1139 
1140  pfree(index_itlist);
1141 
1142  return (Plan *) plan;
1143 }
List * qual
Definition: plannodes.h:142
#define fix_scan_list(root, lst, rtoffset, num_exec)
Definition: setrefs.c:104
Plan plan
Definition: plannodes.h:343
Index scanrelid
Definition: plannodes.h:344
Definition: nodes.h:539
static Node * fix_upper_expr(PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, Index newvarno, int rtoffset, double num_exec)
Definition: setrefs.c:2767
void pfree(void *pointer)
Definition: mcxt.c:1169
#define NUM_EXEC_TLIST(parentplan)
Definition: setrefs.c:90
#define NUM_EXEC_QUAL(parentplan)
Definition: setrefs.c:91
List * indextlist
Definition: plannodes.h:436
List * indexorderby
Definition: plannodes.h:435
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:2368
List * indexqual
Definition: plannodes.h:434
List * targetlist
Definition: plannodes.h:141
#define INDEX_VAR
Definition: primnodes.h:177
Definition: pg_list.h:50

◆ set_join_references()

static void set_join_references ( PlannerInfo root,
Join join,
int  rtoffset 
)
static

Definition at line 1963 of file setrefs.c.

References build_tlist_index(), elog, ERROR, fix_join_expr(), fix_upper_expr(), indexed_tlist::has_non_vars, HashJoin::hashclauses, HashJoin::hashkeys, IsA, JOIN_ANTI, JOIN_FULL, JOIN_LEFT, JOIN_RIGHT, JOIN_SEMI, Join::joinqual, Plan::lefttree, lfirst, MergeJoin::mergeclauses, NestLoop::nestParams, NUM_EXEC_QUAL, NUM_EXEC_TLIST, OUTER_VAR, NestLoopParam::paramval, pfree(), Join::plan, Plan::qual, Plan::righttree, Plan::targetlist, and Var::varno.

Referenced by set_plan_refs().

1964 {
1965  Plan *outer_plan = join->plan.lefttree;
1966  Plan *inner_plan = join->plan.righttree;
1967  indexed_tlist *outer_itlist;
1968  indexed_tlist *inner_itlist;
1969 
1970  outer_itlist = build_tlist_index(outer_plan->targetlist);
1971  inner_itlist = build_tlist_index(inner_plan->targetlist);
1972 
1973  /*
1974  * First process the joinquals (including merge or hash clauses). These
1975  * are logically below the join so they can always use all values
1976  * available from the input tlists. It's okay to also handle
1977  * NestLoopParams now, because those couldn't refer to nullable
1978  * subexpressions.
1979  */
1980  join->joinqual = fix_join_expr(root,
1981  join->joinqual,
1982  outer_itlist,
1983  inner_itlist,
1984  (Index) 0,
1985  rtoffset,
1986  NUM_EXEC_QUAL((Plan *) join));
1987 
1988  /* Now do join-type-specific stuff */
1989  if (IsA(join, NestLoop))
1990  {
1991  NestLoop *nl = (NestLoop *) join;
1992  ListCell *lc;
1993 
1994  foreach(lc, nl->nestParams)
1995  {
1996  NestLoopParam *nlp = (NestLoopParam *) lfirst(lc);
1997 
1998  nlp->paramval = (Var *) fix_upper_expr(root,
1999  (Node *) nlp->paramval,
2000  outer_itlist,
2001  OUTER_VAR,
2002  rtoffset,
2003  NUM_EXEC_TLIST(outer_plan));
2004  /* Check we replaced any PlaceHolderVar with simple Var */
2005  if (!(IsA(nlp->paramval, Var) &&
2006  nlp->paramval->varno == OUTER_VAR))
2007  elog(ERROR, "NestLoopParam was not reduced to a simple Var");
2008  }
2009  }
2010  else if (IsA(join, MergeJoin))
2011  {
2012  MergeJoin *mj = (MergeJoin *) join;
2013 
2014  mj->mergeclauses = fix_join_expr(root,
2015  mj->mergeclauses,
2016  outer_itlist,
2017  inner_itlist,
2018  (Index) 0,
2019  rtoffset,
2020  NUM_EXEC_QUAL((Plan *) join));
2021  }
2022  else if (IsA(join, HashJoin))
2023  {
2024  HashJoin *hj = (HashJoin *) join;
2025 
2026  hj->hashclauses = fix_join_expr(root,
2027  hj->hashclauses,
2028  outer_itlist,
2029  inner_itlist,
2030  (Index) 0,
2031  rtoffset,
2032  NUM_EXEC_QUAL((Plan *) join));
2033 
2034  /*
2035  * HashJoin's hashkeys are used to look for matching tuples from its
2036  * outer plan (not the Hash node!) in the hashtable.
2037  */
2038  hj->hashkeys = (List *) fix_upper_expr(root,
2039  (Node *) hj->hashkeys,
2040  outer_itlist,
2041  OUTER_VAR,
2042  rtoffset,
2043  NUM_EXEC_QUAL((Plan *) join));
2044  }
2045 
2046  /*
2047  * Now we need to fix up the targetlist and qpqual, which are logically
2048  * above the join. This means they should not re-use any input expression
2049  * that was computed in the nullable side of an outer join. Vars and
2050  * PlaceHolderVars are fine, so we can implement this restriction just by
2051  * clearing has_non_vars in the indexed_tlist structs.
2052  *
2053  * XXX This is a grotty workaround for the fact that we don't clearly
2054  * distinguish between a Var appearing below an outer join and the "same"
2055  * Var appearing above it. If we did, we'd not need to hack the matching
2056  * rules this way.
2057  */
2058  switch (join->jointype)
2059  {
2060  case JOIN_LEFT:
2061  case JOIN_SEMI:
2062  case JOIN_ANTI:
2063  inner_itlist->has_non_vars = false;
2064  break;
2065  case JOIN_RIGHT:
2066  outer_itlist->has_non_vars = false;
2067  break;
2068  case JOIN_FULL:
2069  outer_itlist->has_non_vars = false;
2070  inner_itlist->has_non_vars = false;
2071  break;
2072  default:
2073  break;
2074  }
2075 
2076  join->plan.targetlist = fix_join_expr(root,
2077  join->plan.targetlist,
2078  outer_itlist,
2079  inner_itlist,
2080  (Index) 0,
2081  rtoffset,
2082  NUM_EXEC_TLIST((Plan *) join));
2083  join->plan.qual = fix_join_expr(root,
2084  join->plan.qual,
2085  outer_itlist,
2086  inner_itlist,
2087  (Index) 0,
2088  rtoffset,
2089  NUM_EXEC_QUAL((Plan *) join));
2090 
2091  pfree(outer_itlist);
2092  pfree(inner_itlist);
2093 }
List * qual
Definition: plannodes.h:142
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
List * nestParams
Definition: plannodes.h:723
List * hashkeys
Definition: plannodes.h:771
List * hashclauses
Definition: plannodes.h:763
Definition: nodes.h:539
static Node * fix_upper_expr(PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, Index newvarno, int rtoffset, double num_exec)
Definition: setrefs.c:2767
Definition: primnodes.h:186
List * mergeclauses
Definition: plannodes.h:748
JoinType jointype
Definition: plannodes.h:704
struct Plan * righttree
Definition: plannodes.h:144
void pfree(void *pointer)
Definition: mcxt.c:1169
Var * paramval
Definition: plannodes.h:730
#define ERROR
Definition: elog.h:46
#define NUM_EXEC_TLIST(parentplan)
Definition: setrefs.c:90
Index varno
Definition: primnodes.h:189
bool has_non_vars
Definition: setrefs.c:44
#define NUM_EXEC_QUAL(parentplan)
Definition: setrefs.c:91
unsigned int Index
Definition: c.h:549
#define lfirst(lc)
Definition: pg_list.h:169
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:2368
struct Plan * lefttree
Definition: plannodes.h:143
List * targetlist
Definition: plannodes.h:141
#define elog(elevel,...)
Definition: elog.h:232
Definition: pg_list.h:50
#define OUTER_VAR
Definition: primnodes.h:176
List * joinqual
Definition: plannodes.h:706
static List * fix_join_expr(PlannerInfo *root, List *clauses, indexed_tlist *outer_itlist, indexed_tlist *inner_itlist, Index acceptable_rel, int rtoffset, double num_exec)
Definition: setrefs.c:2614
Plan plan
Definition: plannodes.h:703

◆ set_mergeappend_references()

static Plan * set_mergeappend_references ( PlannerInfo root,
MergeAppend mplan,
int  rtoffset 
)
static

Definition at line 1502 of file setrefs.c.

References Assert, clean_up_removed_plan_level(), Plan::lefttree, lfirst, linitial, list_length(), MergeAppend::mergeplans, NIL, offset_relid_set(), MergeAppend::plan, Plan::qual, Plan::righttree, PartitionedRelPruneInfo::rtindex, set_dummy_tlist_references(), and set_plan_refs().

Referenced by set_plan_refs().

1505 {
1506  ListCell *l;
1507 
1508  /*
1509  * MergeAppend, like Sort et al, doesn't actually evaluate its targetlist
1510  * or check quals. If it's got exactly one child plan, then it's not
1511  * doing anything useful at all, and we can strip it out.
1512  */
1513  Assert(mplan->plan.qual == NIL);
1514 
1515  /* First, we gotta recurse on the children */
1516  foreach(l, mplan->mergeplans)
1517  {
1518  lfirst(l) = set_plan_refs(root, (Plan *) lfirst(l), rtoffset);
1519  }
1520 
1521  /* Now, if there's just one, forget the MergeAppend and return that child */
1522  if (list_length(mplan->mergeplans) == 1)
1523  return clean_up_removed_plan_level((Plan *) mplan,
1524  (Plan *) linitial(mplan->mergeplans));
1525 
1526  /*
1527  * Otherwise, clean up the MergeAppend as needed. It's okay to do this
1528  * after recursing to the children, because set_dummy_tlist_references
1529  * doesn't look at those.
1530  */
1531  set_dummy_tlist_references((Plan *) mplan, rtoffset);
1532 
1533  mplan->apprelids = offset_relid_set(mplan->apprelids, rtoffset);
1534 
1535  if (mplan->part_prune_info)
1536  {
1537  foreach(l, mplan->part_prune_info->prune_infos)
1538  {
1539  List *prune_infos = lfirst(l);
1540  ListCell *l2;
1541 
1542  foreach(l2, prune_infos)
1543  {
1544  PartitionedRelPruneInfo *pinfo = lfirst(l2);
1545 
1546  pinfo->rtindex += rtoffset;
1547  }
1548  }
1549  }
1550 
1551  /* We don't need to recurse to lefttree or righttree ... */
1552  Assert(mplan->plan.lefttree == NULL);
1553  Assert(mplan->plan.righttree == NULL);
1554 
1555  return (Plan *) mplan;
1556 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:142
static Relids offset_relid_set(Relids relids, int rtoffset)
Definition: setrefs.c:1595
static Plan * set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:500
struct Plan * righttree
Definition: plannodes.h:144
#define linitial(l)
Definition: pg_list.h:174
static Plan * clean_up_removed_plan_level(Plan *parent, Plan *child)
Definition: setrefs.c:1262
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
struct Plan * lefttree
Definition: plannodes.h:143
List * mergeplans
Definition: plannodes.h:275
Definition: pg_list.h:50
static void set_dummy_tlist_references(Plan *plan, int rtoffset)
Definition: setrefs.c:2301

◆ set_param_references()

static void set_param_references ( PlannerInfo root,
Plan plan 
)
static

Definition at line 2177 of file setrefs.c.

References Assert, bms_add_member(), bms_intersect(), Plan::extParam, PlannerInfo::init_plans, IsA, Plan::lefttree, lfirst, lfirst_int, PlannerInfo::parent_root, and SubPlan::setParam.

Referenced by set_plan_refs().

2178 {
2179  Assert(IsA(plan, Gather) || IsA(plan, GatherMerge));
2180 
2181  if (plan->lefttree->extParam)
2182  {
2183  PlannerInfo *proot;
2184  Bitmapset *initSetParam = NULL;
2185  ListCell *l;
2186 
2187  for (proot = root; proot != NULL; proot = proot->parent_root)
2188  {
2189  foreach(l, proot->init_plans)
2190  {
2191  SubPlan *initsubplan = (SubPlan *) lfirst(l);
2192  ListCell *l2;
2193 
2194  foreach(l2, initsubplan->setParam)
2195  {
2196  initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
2197  }
2198  }
2199  }
2200 
2201  /*
2202  * Remember the list of all external initplan params that are used by
2203  * the children of Gather or Gather merge node.
2204  */
2205  if (IsA(plan, Gather))
2206  ((Gather *) plan)->initParam =
2207  bms_intersect(plan->lefttree->extParam, initSetParam);
2208  else
2209  ((GatherMerge *) plan)->initParam =
2210  bms_intersect(plan->lefttree->extParam, initSetParam);
2211  }
2212 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
PlannerInfo * parent_root
Definition: pathnodes.h:167
#define lfirst_int(lc)
Definition: pg_list.h:170
Bitmapset * bms_intersect(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:259
List * init_plans
Definition: pathnodes.h:241
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List * setParam
Definition: primnodes.h:760
Bitmapset * extParam
Definition: plannodes.h:159
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
struct Plan * lefttree
Definition: plannodes.h:143

◆ 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:287
List * appendRelations
Definition: pathnodes.h:107
Index prti
Definition: plannodes.h:1119
List * translated_vars
Definition: pathnodes.h:2323
static Plan * set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:500
#define lfirst_node(type, lc)
Definition: pg_list.h:172
PlannerGlobal * glob
Definition: pathnodes.h:163
List * lappend(List *list, void *datum)
Definition: list.c:336
List * append_rel_list
Definition: pathnodes.h:283
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:101
void * palloc(Size size)
Definition: mcxt.c:1062
Index child_relid
Definition: pathnodes.h:2296
Index parent_relid
Definition: pathnodes.h:2295
List * finalrowmarks
Definition: pathnodes.h:103

◆ set_plan_refs()

static Plan * set_plan_refs ( PlannerInfo root,
Plan plan,
int  rtoffset 
)
static

Definition at line 500 of file setrefs.c.

References Agg::aggsplit, Assert, BitmapAnd::bitmapplans, BitmapOr::bitmapplans, BitmapHeapScan::bitmapqualorig, build_tlist_index(), convert_combining_aggrefs(), copyObject, DO_AGGSPLIT_COMBINE, elog, WindowAgg::endOffset, ERROR, ModifyTable::exclRelRTI, ModifyTable::exclRelTlist, TargetEntry::expr, fix_join_expr(), fix_scan_expr(), fix_scan_list, forboth, FunctionScan::functions, PlannerInfo::glob, IndexScan::indexorderby, IndexScan::indexorderbyorig, IndexScan::indexqual, BitmapIndexScan::indexqual, IndexScan::indexqualorig, BitmapIndexScan::indexqualorig, IsA, lappend(), lappend_int(), PlannerGlobal::lastPlanNodeId, Plan::lefttree, lfirst, lfirst_int, Limit::limitCount, Limit::limitOffset, linitial, linitial_int, list_concat(), list_length(), makeNullConst(), NIL, nodeTag, ModifyTable::nominalRelation, NUM_EXEC_QUAL, NUM_EXEC_TLIST, ModifyTable::onConflictSet, ModifyTable::onConflictWhere, outerPlan, ResultCache::param_exprs, pfree(), Result::plan, ModifyTable::plan, BitmapAnd::plan, BitmapOr::plan, Scan::plan, LockRows::plan, Limit::plan, Plan::plan_node_id, PlanRowMark::prti, Plan::qual, Result::resconstantqual, PlannerGlobal::resultRelations, ModifyTable::resultRelations, ModifyTable::returningLists, Plan::righttree, ModifyTable::rootRelation, ROWID_VAR, ModifyTable::rowMarks, LockRows::rowMarks, PlanRowMark::rti, SampleScan::scan, IndexScan::scan, BitmapIndexScan::scan, BitmapHeapScan::scan, TidScan::scan, TidRangeScan::scan, FunctionScan::scan, ValuesScan::scan, TableFuncScan::scan, CteScan::scan, NamedTuplestoreScan::scan, WorkTableScan::scan, Scan::scanrelid, set_append_references(), set_customscan_references(), set_dummy_tlist_references(), set_foreignscan_references(), set_hash_references(), set_indexonlyscan_references(), set_join_references(), set_mergeappend_references(), set_param_references(), set_returning_clause_references(), set_subqueryscan_references(), set_upper_references(), splan, WindowAgg::startOffset, T_Agg, T_Append, T_BitmapAnd, T_BitmapHeapScan, T_BitmapIndexScan, T_BitmapOr, T_CteScan, T_CustomScan, T_ForeignScan, T_FunctionScan, T_Gather, T_GatherMerge, T_Group, T_Hash, T_HashJoin, T_IncrementalSort, T_IndexOnlyScan, T_IndexScan, T_Limit, T_LockRows, T_Material, T_MergeAppend, T_MergeJoin, T_ModifyTable, T_NamedTuplestoreScan, T_NestLoop, T_ProjectSet, T_RecursiveUnion, T_Result, T_ResultCache, T_SampleScan, T_SeqScan, T_SetOp, T_Sort, T_SubqueryScan, T_TableFuncScan, T_TidRangeScan, T_TidScan, T_Unique, T_ValuesScan, T_WindowAgg, T_WorkTableScan, TableFuncScan::tablefunc, SampleScan::tablesample, Plan::targetlist, TidScan::tidquals, TidRangeScan::tidrangequals, ValuesScan::values_lists, Var::varcollid, Var::varno, Var::vartype, Var::vartypmod, and ModifyTable::withCheckOptionLists.

Referenced by set_append_references(), set_customscan_references(), set_mergeappend_references(), and set_plan_references().

501 {
502  ListCell *l;
503 
504  if (plan == NULL)
505  return NULL;
506 
507  /* Assign this node a unique ID. */
508  plan->plan_node_id = root->glob->lastPlanNodeId++;
509 
510  /*
511  * Plan-type-specific fixes
512  */
513  switch (nodeTag(plan))
514  {
515  case T_SeqScan:
516  {
517  SeqScan *splan = (SeqScan *) plan;
518 
519  splan->scanrelid += rtoffset;
520  splan->plan.targetlist =
521  fix_scan_list(root, splan->plan.targetlist,
522  rtoffset, NUM_EXEC_TLIST(plan));
523  splan->plan.qual =
524  fix_scan_list(root, splan->plan.qual,
525  rtoffset, NUM_EXEC_QUAL(plan));
526  }
527  break;
528  case T_SampleScan:
529  {
530  SampleScan *splan = (SampleScan *) plan;
531 
532  splan->scan.scanrelid += rtoffset;
533  splan->scan.plan.targetlist =
534  fix_scan_list(root, splan->scan.plan.targetlist,
535  rtoffset, NUM_EXEC_TLIST(plan));
536  splan->scan.plan.qual =
537  fix_scan_list(root, splan->scan.plan.qual,
538  rtoffset, NUM_EXEC_QUAL(plan));
539  splan->tablesample = (TableSampleClause *)
540  fix_scan_expr(root, (Node *) splan->tablesample,
541  rtoffset, 1);
542  }
543  break;
544  case T_IndexScan:
545  {
546  IndexScan *splan = (IndexScan *) plan;
547 
548  splan->scan.scanrelid += rtoffset;
549  splan->scan.plan.targetlist =
550  fix_scan_list(root, splan->scan.plan.targetlist,
551  rtoffset, NUM_EXEC_TLIST(plan));
552  splan->scan.plan.qual =
553  fix_scan_list(root, splan->scan.plan.qual,
554  rtoffset, NUM_EXEC_QUAL(plan));
555  splan->indexqual =
556  fix_scan_list(root, splan->indexqual,
557  rtoffset, 1);
558  splan->indexqualorig =
559  fix_scan_list(root, splan->indexqualorig,
560  rtoffset, NUM_EXEC_QUAL(plan));
561  splan->indexorderby =
562  fix_scan_list(root, splan->indexorderby,
563  rtoffset, 1);
564  splan->indexorderbyorig =
565  fix_scan_list(root, splan->indexorderbyorig,
566  rtoffset, NUM_EXEC_QUAL(plan));
567  }
568  break;
569  case T_IndexOnlyScan:
570  {
571  IndexOnlyScan *splan = (IndexOnlyScan *) plan;
572 
573  return set_indexonlyscan_references(root, splan, rtoffset);
574  }
575  break;
576  case T_BitmapIndexScan:
577  {
578  BitmapIndexScan *splan = (BitmapIndexScan *) plan;
579 
580  splan->scan.scanrelid += rtoffset;
581  /* no need to fix targetlist and qual */
582  Assert(splan->scan.plan.targetlist == NIL);
583  Assert(splan->scan.plan.qual == NIL);
584  splan->indexqual =
585  fix_scan_list(root, splan->indexqual, rtoffset, 1);
586  splan->indexqualorig =
587  fix_scan_list(root, splan->indexqualorig,
588  rtoffset, NUM_EXEC_QUAL(plan));
589  }
590  break;
591  case T_BitmapHeapScan:
592  {
593  BitmapHeapScan *splan = (BitmapHeapScan *) plan;
594 
595  splan->scan.scanrelid += rtoffset;
596  splan->scan.plan.targetlist =
597  fix_scan_list(root, splan->scan.plan.targetlist,
598  rtoffset, NUM_EXEC_TLIST(plan));
599  splan->scan.plan.qual =
600  fix_scan_list(root, splan->scan.plan.qual,
601  rtoffset, NUM_EXEC_QUAL(plan));
602  splan->bitmapqualorig =
603  fix_scan_list(root, splan->bitmapqualorig,
604  rtoffset, NUM_EXEC_QUAL(plan));
605  }
606  break;
607  case T_TidScan:
608  {
609  TidScan *splan = (TidScan *) plan;
610 
611  splan->scan.scanrelid += rtoffset;
612  splan->scan.plan.targetlist =
613  fix_scan_list(root, splan->scan.plan.targetlist,
614  rtoffset, NUM_EXEC_TLIST(plan));
615  splan->scan.plan.qual =
616  fix_scan_list(root, splan->scan.plan.qual,
617  rtoffset, NUM_EXEC_QUAL(plan));
618  splan->tidquals =
619  fix_scan_list(root, splan->tidquals,
620  rtoffset, 1);
621  }
622  break;
623  case T_TidRangeScan:
624  {
625  TidRangeScan *splan = (TidRangeScan *) plan;
626 
627  splan->scan.scanrelid += rtoffset;
628  splan->scan.plan.targetlist =
629  fix_scan_list(root, splan->scan.plan.targetlist,
630  rtoffset, NUM_EXEC_TLIST(plan));
631  splan->scan.plan.qual =
632  fix_scan_list(root, splan->scan.plan.qual,
633  rtoffset, NUM_EXEC_QUAL(plan));
634  splan->tidrangequals =
635  fix_scan_list(root, splan->tidrangequals,
636  rtoffset, 1);
637  }
638  break;
639  case T_SubqueryScan:
640  /* Needs special treatment, see comments below */
641  return set_subqueryscan_references(root,
642  (SubqueryScan *) plan,
643  rtoffset);
644  case T_FunctionScan:
645  {
646  FunctionScan *splan = (FunctionScan *) plan;
647 
648  splan->scan.scanrelid += rtoffset;
649  splan->scan.plan.targetlist =
650  fix_scan_list(root, splan->scan.plan.targetlist,
651  rtoffset, NUM_EXEC_TLIST(plan));
652  splan->scan.plan.qual =
653  fix_scan_list(root, splan->scan.plan.qual,
654  rtoffset, NUM_EXEC_QUAL(plan));
655  splan->functions =
656  fix_scan_list(root, splan->functions, rtoffset, 1);
657  }
658  break;
659  case T_TableFuncScan:
660  {
661  TableFuncScan *splan = (TableFuncScan *) plan;
662 
663  splan->scan.scanrelid += rtoffset;
664  splan->scan.plan.targetlist =
665  fix_scan_list(root, splan->scan.plan.targetlist,
666  rtoffset, NUM_EXEC_TLIST(plan));
667  splan->scan.plan.qual =
668  fix_scan_list(root, splan->scan.plan.qual,
669  rtoffset, NUM_EXEC_QUAL(plan));
670  splan->tablefunc = (TableFunc *)
671  fix_scan_expr(root, (Node *) splan->tablefunc,
672  rtoffset, 1);
673  }
674  break;
675  case T_ValuesScan:
676  {
677  ValuesScan *splan = (ValuesScan *) plan;
678 
679  splan->scan.scanrelid += rtoffset;
680  splan->scan.plan.targetlist =
681  fix_scan_list(root, splan->scan.plan.targetlist,
682  rtoffset, NUM_EXEC_TLIST(plan));
683  splan->scan.plan.qual =
684  fix_scan_list(root, splan->scan.plan.qual,
685  rtoffset, NUM_EXEC_QUAL(plan));
686  splan->values_lists =
687  fix_scan_list(root, splan->values_lists,
688  rtoffset, 1);
689  }
690  break;
691  case T_CteScan:
692  {
693  CteScan *splan = (CteScan *) plan;
694 
695  splan->scan.scanrelid += rtoffset;
696  splan->scan.plan.targetlist =
697  fix_scan_list(root, splan->scan.plan.targetlist,
698  rtoffset, NUM_EXEC_TLIST(plan));
699  splan->scan.plan.qual =
700  fix_scan_list(root, splan->scan.plan.qual,
701  rtoffset, NUM_EXEC_QUAL(plan));
702  }
703  break;
705  {
706  NamedTuplestoreScan *splan = (NamedTuplestoreScan *) plan;
707 
708  splan->scan.scanrelid += rtoffset;
709  splan->scan.plan.targetlist =
710  fix_scan_list(root, splan->scan.plan.targetlist,
711  rtoffset, NUM_EXEC_TLIST(plan));
712  splan->scan.plan.qual =
713  fix_scan_list(root, splan->scan.plan.qual,
714  rtoffset, NUM_EXEC_QUAL(plan));
715  }
716  break;
717  case T_WorkTableScan:
718  {
719  WorkTableScan *splan = (WorkTableScan *) plan;
720 
721  splan->scan.scanrelid += rtoffset;
722  splan->scan.plan.targetlist =
723  fix_scan_list(root, splan->scan.plan.targetlist,
724  rtoffset, NUM_EXEC_TLIST(plan));
725  splan->scan.plan.qual =
726  fix_scan_list(root, splan->scan.plan.qual,
727  rtoffset, NUM_EXEC_QUAL(plan));
728  }
729  break;
730  case T_ForeignScan:
731  set_foreignscan_references(root, (ForeignScan *) plan, rtoffset);
732  break;
733  case T_CustomScan:
734  set_customscan_references(root, (CustomScan *) plan, rtoffset);
735  break;
736 
737  case T_NestLoop:
738  case T_MergeJoin:
739  case T_HashJoin:
740  set_join_references(root, (Join *) plan, rtoffset);
741  break;
742 
743  case T_Gather:
744  case T_GatherMerge:
745  {
746  set_upper_references(root, plan, rtoffset);
747  set_param_references(root, plan);
748  }
749  break;
750 
751  case T_Hash:
752  set_hash_references(root, plan, rtoffset);
753  break;
754 
755  case T_ResultCache:
756  {
757  ResultCache *rcplan = (ResultCache *) plan;
758 
759  rcplan->param_exprs = fix_scan_list(root, rcplan->param_exprs,
760  rtoffset,
761  NUM_EXEC_TLIST(plan));
762  break;
763  }
764 
765  case T_Material:
766  case T_Sort:
767  case T_IncrementalSort:
768  case T_Unique:
769  case T_SetOp:
770 
771  /*
772  * These plan types don't actually bother to evaluate their
773  * targetlists, because they just return their unmodified input
774  * tuples. Even though the targetlist won't be used by the
775  * executor, we fix it up for possible use by EXPLAIN (not to
776  * mention ease of debugging --- wrong varnos are very confusing).
777  */
778  set_dummy_tlist_references(plan, rtoffset);
779 
780  /*
781  * Since these plan types don't check quals either, we should not
782  * find any qual expression attached to them.
783  */
784  Assert(plan->qual == NIL);
785  break;
786  case T_LockRows:
787  {
788  LockRows *splan = (LockRows *) plan;
789 
790  /*
791  * Like the plan types above, LockRows doesn't evaluate its
792  * tlist or quals. But we have to fix up the RT indexes in
793  * its rowmarks.
794  */
795  set_dummy_tlist_references(plan, rtoffset);
796  Assert(splan->plan.qual == NIL);
797 
798  foreach(l, splan->rowMarks)
799  {
800  PlanRowMark *rc = (PlanRowMark *) lfirst(l);
801 
802  rc->rti += rtoffset;
803  rc->prti += rtoffset;
804  }
805  }
806  break;
807  case T_Limit:
808  {
809  Limit *splan = (Limit *) plan;
810 
811  /*
812  * Like the plan types above, Limit doesn't evaluate its tlist
813  * or quals. It does have live expressions for limit/offset,
814  * however; and those cannot contain subplan variable refs, so
815  * fix_scan_expr works for them.
816  */
817  set_dummy_tlist_references(plan, rtoffset);
818  Assert(splan->plan.qual == NIL);
819 
820  splan->limitOffset =
821  fix_scan_expr(root, splan->limitOffset, rtoffset, 1);
822  splan->limitCount =
823  fix_scan_expr(root, splan->limitCount, rtoffset, 1);
824  }
825  break;
826  case T_Agg:
827  {
828  Agg *agg = (Agg *) plan;
829 
830  /*
831  * If this node is combining partial-aggregation results, we
832  * must convert its Aggrefs to contain references to the
833  * partial-aggregate subexpressions that will be available
834  * from the child plan node.
835  */
836  if (DO_AGGSPLIT_COMBINE(agg->aggsplit))
837  {
838  plan->targetlist = (List *)
840  NULL);
841  plan->qual = (List *)
843  NULL);
844  }
845 
846  set_upper_references(root, plan, rtoffset);
847  }
848  break;
849  case T_Group:
850  set_upper_references(root, plan, rtoffset);
851  break;
852  case T_WindowAgg:
853  {
854  WindowAgg *wplan = (WindowAgg *) plan;
855 
856  set_upper_references(root, plan, rtoffset);
857 
858  /*
859  * Like Limit node limit/offset expressions, WindowAgg has
860  * frame offset expressions, which cannot contain subplan
861  * variable refs, so fix_scan_expr works for them.
862  */
863  wplan->startOffset =
864  fix_scan_expr(root, wplan->startOffset, rtoffset, 1);
865  wplan->endOffset =
866  fix_scan_expr(root, wplan->endOffset, rtoffset, 1);
867  }
868  break;
869  case T_Result:
870  {
871  Result *splan = (Result *) plan;
872 
873  /*
874  * Result may or may not have a subplan; if not, it's more
875  * like a scan node than an upper node.
876  */
877  if (splan->plan.lefttree != NULL)
878  set_upper_references(root, plan, rtoffset);
879  else
880  {
881  /*
882  * The tlist of a childless Result could contain
883  * unresolved ROWID_VAR Vars, in case it's representing a
884  * target relation which is completely empty because of
885  * constraint exclusion. Replace any such Vars by null
886  * constants, as though they'd been resolved for a leaf
887  * scan node that doesn't support them. We could have
888  * fix_scan_expr do this, but since the case is only
889  * expected to occur here, it seems safer to special-case
890  * it here and keep the assertions that ROWID_VARs
891  * shouldn't be seen by fix_scan_expr.
892  */
893  foreach(l, splan->plan.targetlist)
894  {
895  TargetEntry *tle = (TargetEntry *) lfirst(l);
896  Var *var = (Var *) tle->expr;
897 
898  if (var && IsA(var, Var) && var->varno == ROWID_VAR)
899  tle->expr = (Expr *) makeNullConst(var->vartype,
900  var->vartypmod,
901  var->varcollid);
902  }
903 
904  splan->plan.targetlist =
905  fix_scan_list(root, splan->plan.targetlist,
906  rtoffset, NUM_EXEC_TLIST(plan));
907  splan->plan.qual =
908  fix_scan_list(root, splan->plan.qual,
909  rtoffset, NUM_EXEC_QUAL(plan));
910  }
911  /* resconstantqual can't contain any subplan variable refs */
912  splan->resconstantqual =
913  fix_scan_expr(root, splan->resconstantqual, rtoffset, 1);
914  }
915  break;
916  case T_ProjectSet:
917  set_upper_references(root, plan, rtoffset);
918  break;
919  case T_ModifyTable:
920  {
921  ModifyTable *splan = (ModifyTable *) plan;
922 
923  Assert(splan->plan.targetlist == NIL);
924  Assert(splan->plan.qual == NIL);
925 
926  splan->withCheckOptionLists =
927  fix_scan_list(root, splan->withCheckOptionLists,
928  rtoffset, 1);
929 
930  if (splan->returningLists)
931  {
932  List *newRL = NIL;
933  Plan *subplan = outerPlan(splan);
934  ListCell *lcrl,
935  *lcrr;
936 
937  /*
938  * Pass each per-resultrel returningList through
939  * set_returning_clause_references().
940  */
942  forboth(lcrl, splan->returningLists,
943  lcrr, splan->resultRelations)
944  {
945  List *rlist = (List *) lfirst(lcrl);
946  Index resultrel = lfirst_int(lcrr);
947 
948  rlist = set_returning_clause_references(root,
949  rlist,
950  subplan,
951  resultrel,
952  rtoffset);
953  newRL = lappend(newRL, rlist);
954  }
955  splan->returningLists = newRL;
956 
957  /*
958  * Set up the visible plan targetlist as being the same as
959  * the first RETURNING list. This is for the use of
960  * EXPLAIN; the executor won't pay any attention to the
961  * targetlist. We postpone this step until here so that
962  * we don't have to do set_returning_clause_references()
963  * twice on identical targetlists.
964  */
965  splan->plan.targetlist = copyObject(linitial(newRL));
966  }
967 
968  /*
969  * We treat ModifyTable with ON CONFLICT as a form of 'pseudo
970  * join', where the inner side is the EXCLUDED tuple.
971  * Therefore use fix_join_expr to setup the relevant variables
972  * to INNER_VAR. We explicitly don't create any OUTER_VARs as
973  * those are already used by RETURNING and it seems better to
974  * be non-conflicting.
975  */
976  if (splan->onConflictSet)
977  {
978  indexed_tlist *itlist;
979 
980  itlist = build_tlist_index(splan->exclRelTlist);
981 
982  splan->onConflictSet =
983  fix_join_expr(root, splan->onConflictSet,
984  NULL, itlist,
986  rtoffset, NUM_EXEC_QUAL(plan));
987 
988  splan->onConflictWhere = (Node *)
989  fix_join_expr(root, (List *) splan->onConflictWhere,
990  NULL, itlist,
992  rtoffset, NUM_EXEC_QUAL(plan));
993 
994  pfree(itlist);
995 
996  splan->exclRelTlist =
997  fix_scan_list(root, splan->exclRelTlist, rtoffset, 1);
998  }
999 
1000  splan->nominalRelation += rtoffset;
1001  if (splan->rootRelation)
1002  splan->rootRelation += rtoffset;
1003  splan->exclRelRTI += rtoffset;
1004 
1005  foreach(l, splan->resultRelations)
1006  {
1007  lfirst_int(l) += rtoffset;
1008  }
1009  foreach(l, splan->rowMarks)
1010  {
1011  PlanRowMark *rc = (PlanRowMark *) lfirst(l);
1012 
1013  rc->rti += rtoffset;
1014  rc->prti += rtoffset;
1015  }
1016 
1017  /*
1018  * Append this ModifyTable node's final result relation RT
1019  * index(es) to the global list for the plan.
1020  */
1021  root->glob->resultRelations =
1023  splan->resultRelations);
1024  if (splan->rootRelation)
1025  {
1026  root->glob->resultRelations =
1028  splan->rootRelation);
1029  }
1030  }
1031  break;
1032  case T_Append:
1033  /* Needs special treatment, see comments below */
1034  return set_append_references(root,
1035  (Append *) plan,
1036  rtoffset);
1037  case T_MergeAppend:
1038  /* Needs special treatment, see comments below */
1039  return set_mergeappend_references(root,
1040  (MergeAppend *) plan,
1041  rtoffset);
1042  case T_RecursiveUnion:
1043  /* This doesn't evaluate targetlist or check quals either */
1044  set_dummy_tlist_references(plan, rtoffset);
1045  Assert(plan->qual == NIL);
1046  break;
1047  case T_BitmapAnd:
1048  {
1049  BitmapAnd *splan = (BitmapAnd *) plan;
1050 
1051  /* BitmapAnd works like Append, but has no tlist */
1052  Assert(splan->plan.targetlist == NIL);
1053  Assert(splan->plan.qual == NIL);
1054  foreach(l, splan->bitmapplans)
1055  {
1056  lfirst(l) = set_plan_refs(root,
1057  (Plan *) lfirst(l),
1058  rtoffset);
1059  }
1060  }
1061  break;
1062  case T_BitmapOr:
1063  {
1064  BitmapOr *splan = (BitmapOr *) plan;
1065 
1066  /* BitmapOr works like Append, but has no tlist */
1067  Assert(splan->plan.targetlist == NIL);
1068  Assert(splan->plan.qual == NIL);
1069  foreach(l, splan->bitmapplans)
1070  {
1071  lfirst(l) = set_plan_refs(root,
1072  (Plan *) lfirst(l),
1073  rtoffset);
1074  }
1075  }
1076  break;
1077  default:
1078  elog(ERROR, "unrecognized node type: %d",
1079  (int) nodeTag(plan));
1080  break;
1081  }
1082 
1083  /*
1084  * Now recurse into child plans, if any
1085  *
1086  * NOTE: it is essential that we recurse into child plans AFTER we set
1087  * subplan references in this plan's tlist and quals. If we did the
1088  * reference-adjustments bottom-up, then we would fail to match this
1089  * plan's var nodes against the already-modified nodes of the children.
1090  */
1091  plan->lefttree = set_plan_refs(root, plan->lefttree, rtoffset);
1092  plan->righttree = set_plan_refs(root, plan->righttree, rtoffset);
1093 
1094  return plan;
1095 }
List * tidrangequals
Definition: plannodes.h:505
List * bitmapplans
Definition: plannodes.h:318
#define NIL
Definition: pg_list.h:65
Plan plan
Definition: plannodes.h:187
List * qual
Definition: plannodes.h:142
Definition: nodes.h:81
#define fix_scan_list(root, lst, rtoffset, num_exec)
Definition: setrefs.c:104
Plan plan
Definition: plannodes.h:343
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Plan plan
Definition: plannodes.h:1023
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
Index nominalRelation
Definition: plannodes.h:222
Definition: nodes.h:83
Index scanrelid
Definition: plannodes.h:344
#define ROWID_VAR
Definition: primnodes.h:178
Plan plan
Definition: plannodes.h:1009
List * withCheckOptionLists
Definition: plannodes.h:227
List * functions
Definition: plannodes.h:537
static void set_join_references(PlannerInfo *root, Join *join, int rtoffset)
Definition: setrefs.c:1963
int lastPlanNodeId
Definition: pathnodes.h:119
List * indexqual
Definition: plannodes.h:462
List * tidquals
Definition: plannodes.h:492
TableFunc * tablefunc
Definition: plannodes.h:558
int plan_node_id
Definition: plannodes.h:140
List * resultRelations
Definition: pathnodes.h:105
List * indexqualorig
Definition: plannodes.h:406
struct TableSampleClause * tablesample
Definition: plannodes.h:361
Definition: nodes.h:539
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
Definition: nodes.h:49
Index prti
Definition: plannodes.h:1119
List * values_lists
Definition: plannodes.h:548
static void set_param_references(PlannerInfo *root, Plan *plan)
Definition: setrefs.c:2177
Definition: nodes.h:78
Definition: primnodes.h:186
Scan scan
Definition: plannodes.h:547
Node * limitOffset
Definition: plannodes.h:1024
#define DO_AGGSPLIT_COMBINE(as)
Definition: nodes.h:801
static Plan * set_append_references(PlannerInfo *root, Append *aplan, int rtoffset)
Definition: setrefs.c:1438
static Plan * set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:500
List * onConflictSet
Definition: plannodes.h:235
Index rootRelation
Definition: plannodes.h:223
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:337
List * resultRelations
Definition: plannodes.h:225
static void set_foreignscan_references(PlannerInfo *root, ForeignScan *fscan, int rtoffset)
Definition: setrefs.c:1283
#define linitial_int(l)
Definition: pg_list.h:175
Node * startOffset
Definition: plannodes.h:891
Node * resconstantqual
Definition: plannodes.h:188
List * rowMarks
Definition: plannodes.h:231
struct Plan * righttree
Definition: plannodes.h:144
void pfree(void *pointer)
Definition: mcxt.c:1169
#define linitial(l)
Definition: pg_list.h:174
Definition: nodes.h:46
#define ERROR
Definition: elog.h:46
List * indexorderbyorig
Definition: plannodes.h:408
#define lfirst_int(lc)
Definition: pg_list.h:170
Oid vartype
Definition: primnodes.h:193
Scan scan
Definition: plannodes.h:359
Scan scan
Definition: plannodes.h:567
Node * limitCount
Definition: plannodes.h:1025
Scan scan
Definition: plannodes.h:403
#define NUM_EXEC_TLIST(parentplan)
Definition: setrefs.c:90
Definition: nodes.h:80
PlannerGlobal * glob
Definition: pathnodes.h:163
Node * endOffset
Definition: plannodes.h:892
static SPIPlanPtr splan
Definition: regress.c:263
static void set_customscan_references(PlannerInfo *root, CustomScan *cscan, int rtoffset)
Definition: setrefs.c:1365
static Plan * set_subqueryscan_references(PlannerInfo *root, SubqueryScan *plan, int rtoffset)
Definition: setrefs.c:1153
List * indexqual
Definition: plannodes.h:405
List * bitmapqualorig
Definition: plannodes.h:478
List * lappend_int(List *list, int datum)
Definition: list.c:354
#define outerPlan(node)
Definition: plannodes.h:171
List * lappend(List *list, void *datum)
Definition: list.c:336
Plan plan
Definition: plannodes.h:331
Index varno
Definition: primnodes.h:189
static Plan * set_indexonlyscan_references(PlannerInfo *root, IndexOnlyScan *plan, int rtoffset)
Definition: setrefs.c:1107
List * bitmapplans
Definition: plannodes.h:333
static void set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:2114
unsigned int Index
Definition: c.h:549
#define NUM_EXEC_QUAL(parentplan)
Definition: setrefs.c:91
Definition: nodes.h:86
static Node * convert_combining_aggrefs(Node *node, void *context)
Definition: setrefs.c:2232
List * indexorderby
Definition: plannodes.h:407
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
AggSplit aggsplit
Definition: plannodes.h:861
Expr * expr
Definition: primnodes.h:1444
List * rowMarks
Definition: plannodes.h:1010
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:2368
Scan scan
Definition: plannodes.h:491
static int list_length(const List *l)
Definition: pg_list.h:149
List * param_exprs
Definition: plannodes.h:795
struct Plan * lefttree
Definition: plannodes.h:143
List * indexqualorig
Definition: plannodes.h:463
#define nodeTag(nodeptr)
Definition: nodes.h:544
List * targetlist
Definition: plannodes.h:141
Definition: nodes.h:87
static List * set_returning_clause_references(PlannerInfo *root, List *rlist, Plan *topplan, Index resultRelation, int rtoffset)
Definition: setrefs.c:2893
Definition: nodes.h:84
#define elog(elevel,...)
Definition: elog.h:232
Oid varcollid
Definition: primnodes.h:195
List * returningLists
Definition: plannodes.h:228
Plan plan
Definition: plannodes.h:317
Definition: plannodes.h:857
#define copyObject(obj)
Definition: nodes.h:655
Index exclRelRTI
Definition: plannodes.h:238
static Plan * set_mergeappend_references(PlannerInfo *root, MergeAppend *mplan, int rtoffset)
Definition: setrefs.c:1502
Definition: pg_list.h:50
static Node * fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset, double num_exec)
Definition: setrefs.c:1832
static void set_dummy_tlist_references(Plan *plan, int rtoffset)
Definition: setrefs.c:2301
List * exclRelTlist
Definition: plannodes.h:239
static void set_hash_references(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:1563
Node * onConflictWhere
Definition: plannodes.h:237
static List * fix_join_expr(PlannerInfo *root, List *clauses, indexed_tlist *outer_itlist, indexed_tlist *inner_itlist, Index acceptable_rel, int rtoffset, double num_exec)
Definition: setrefs.c:2614
Definition: nodes.h:89
int32 vartypmod
Definition: primnodes.h:194

◆ set_returning_clause_references()

static List * set_returning_clause_references ( PlannerInfo root,
List rlist,
Plan topplan,
Index  resultRelation,
int  rtoffset 
)
static

Definition at line 2893 of file setrefs.c.

References build_tlist_index_other_vars(), fix_join_expr(), NUM_EXEC_TLIST, pfree(), and Plan::targetlist.

Referenced by set_plan_refs().

2898 {
2899  indexed_tlist *itlist;
2900 
2901  /*
2902  * We can perform the desired Var fixup by abusing the fix_join_expr
2903  * machinery that formerly handled inner indexscan fixup. We search the
2904  * top plan's targetlist for Vars of non-result relations, and use
2905  * fix_join_expr to convert RETURNING Vars into references to those tlist
2906  * entries, while leaving result-rel Vars as-is.
2907  *
2908  * PlaceHolderVars will also be sought in the targetlist, but no
2909  * more-complex expressions will be. Note that it is not possible for a
2910  * PlaceHolderVar to refer to the result relation, since the result is
2911  * never below an outer join. If that case could happen, we'd have to be
2912  * prepared to pick apart the PlaceHolderVar and evaluate its contained
2913  * expression instead.
2914  */
2915  itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
2916 
2917  rlist = fix_join_expr(root,
2918  rlist,
2919  itlist,
2920  NULL,
2921  resultRelation,
2922  rtoffset,
2923  NUM_EXEC_TLIST(topplan));
2924 
2925  pfree(itlist);
2926 
2927  return rlist;
2928 }
void pfree(void *pointer)
Definition: mcxt.c:1169
#define NUM_EXEC_TLIST(parentplan)
Definition: setrefs.c:90
static indexed_tlist * build_tlist_index_other_vars(List *tlist, Index ignore_rel)
Definition: setrefs.c:2418
List * targetlist
Definition: plannodes.h:141
static List * fix_join_expr(PlannerInfo *root, List *clauses, indexed_tlist *outer_itlist, indexed_tlist *inner_itlist, Index acceptable_rel, int rtoffset, double num_exec)
Definition: setrefs.c:2614

◆ set_subqueryscan_references()

static Plan * set_subqueryscan_references ( PlannerInfo root,
SubqueryScan plan,
int  rtoffset 
)
static

Definition at line 1153 of file setrefs.c.

References clean_up_removed_plan_level(), find_base_rel(), fix_scan_list, NUM_EXEC_QUAL, NUM_EXEC_TLIST, Scan::plan, Plan::qual, SubqueryScan::scan, Scan::scanrelid, set_plan_references(), SubqueryScan::subplan, RelOptInfo::subroot, Plan::targetlist, and trivial_subqueryscan().

Referenced by set_plan_refs().

1156 {
1157  RelOptInfo *rel;
1158  Plan *result;
1159 
1160  /* Need to look up the subquery's RelOptInfo, since we need its subroot */
1161  rel = find_base_rel(root, plan->scan.scanrelid);
1162 
1163  /* Recursively process the subplan */
1164  plan->subplan = set_plan_references(rel->subroot, plan->subplan);
1165 
1166  if (trivial_subqueryscan(plan))
1167  {
1168  /*
1169  * We can omit the SubqueryScan node and just pull up the subplan.
1170  */
1171  result = clean_up_removed_plan_level((Plan *) plan, plan->subplan);
1172  }
1173  else
1174  {
1175  /*
1176  * Keep the SubqueryScan node. We have to do the processing that
1177  * set_plan_references would otherwise have done on it. Notice we do
1178  * not do set_upper_references() here, because a SubqueryScan will
1179  * always have been created with correct references to its subplan's
1180  * outputs to begin with.
1181  */
1182  plan->scan.scanrelid += rtoffset;
1183  plan->scan.plan.targetlist =
1184  fix_scan_list(root, plan->scan.plan.targetlist,
1185  rtoffset, NUM_EXEC_TLIST((Plan *) plan));
1186  plan->scan.plan.qual =
1187  fix_scan_list(root, plan->scan.plan.qual,
1188  rtoffset, NUM_EXEC_QUAL((Plan *) plan));
1189 
1190  result = (Plan *) plan;
1191  }
1192 
1193  return result;
1194 }
List * qual
Definition: plannodes.h:142
#define fix_scan_list(root, lst, rtoffset, num_exec)
Definition: setrefs.c:104
Plan plan
Definition: plannodes.h:343
Index scanrelid
Definition: plannodes.h:344
static bool trivial_subqueryscan(SubqueryScan *plan)
Definition: setrefs.c:1204
PlannerInfo * subroot
Definition: pathnodes.h:720
#define NUM_EXEC_TLIST(parentplan)
Definition: setrefs.c:90
static Plan * clean_up_removed_plan_level(Plan *parent, Plan *child)
Definition: setrefs.c:1262
#define NUM_EXEC_QUAL(parentplan)
Definition: setrefs.c:91
List * targetlist
Definition: plannodes.h:141
Plan * set_plan_references(PlannerInfo *root, Plan *plan)
Definition: setrefs.c:250
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:374
Plan * subplan
Definition: plannodes.h:527

◆ set_upper_references()

static void set_upper_references ( PlannerInfo root,
Plan plan,
int  rtoffset 
)
static

Definition at line 2114 of file setrefs.c.

References build_tlist_index(), TargetEntry::expr, fix_upper_expr(), flatCopyTargetEntry(), lappend(), Plan::lefttree, lfirst, NIL, NUM_EXEC_QUAL, NUM_EXEC_TLIST, OUTER_VAR, pfree(), Plan::qual, TargetEntry::ressortgroupref, search_indexed_tlist_for_sortgroupref(), and Plan::targetlist.

Referenced by set_plan_refs().

2115 {
2116  Plan *subplan = plan->lefttree;
2117  indexed_tlist *subplan_itlist;
2118  List *output_targetlist;
2119  ListCell *l;
2120 
2121  subplan_itlist = build_tlist_index(subplan->targetlist);
2122 
2123  output_targetlist = NIL;
2124  foreach(l, plan->targetlist)
2125  {
2126  TargetEntry *tle = (TargetEntry *) lfirst(l);
2127  Node *newexpr;
2128 
2129  /* If it's a sort/group item, first try to match by sortref */
2130  if (tle->ressortgroupref != 0)
2131  {
2132  newexpr = (Node *)
2134  tle->ressortgroupref,
2135  subplan_itlist,
2136  OUTER_VAR);
2137  if (!newexpr)
2138  newexpr = fix_upper_expr(root,
2139  (Node *) tle->expr,
2140  subplan_itlist,
2141  OUTER_VAR,
2142  rtoffset,
2143  NUM_EXEC_TLIST(plan));
2144  }
2145  else
2146  newexpr = fix_upper_expr(root,
2147  (Node *) tle->expr,
2148  subplan_itlist,
2149  OUTER_VAR,
2150  rtoffset,
2151  NUM_EXEC_TLIST(plan));
2152  tle = flatCopyTargetEntry(tle);
2153  tle->expr = (Expr *) newexpr;
2154  output_targetlist = lappend(output_targetlist, tle);
2155  }
2156  plan->targetlist = output_targetlist;
2157 
2158  plan->qual = (List *)
2159  fix_upper_expr(root,
2160  (Node *) plan->qual,
2161  subplan_itlist,
2162  OUTER_VAR,
2163  rtoffset,
2164  NUM_EXEC_QUAL(plan));
2165 
2166  pfree(subplan_itlist);
2167 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:142
Definition: nodes.h:539
static Node * fix_upper_expr(PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, Index newvarno, int rtoffset, double num_exec)
Definition: setrefs.c:2767
void pfree(void *pointer)
Definition: mcxt.c:1169
#define NUM_EXEC_TLIST(parentplan)
Definition: setrefs.c:90
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
Definition: makefuncs.c:271
List * lappend(List *list, void *datum)
Definition: list.c:336
#define NUM_EXEC_QUAL(parentplan)
Definition: setrefs.c:91
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1444
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:2368
struct Plan * lefttree
Definition: plannodes.h:143
List * targetlist
Definition: plannodes.h:141
Index ressortgroupref
Definition: primnodes.h:1447
static Var * search_indexed_tlist_for_sortgroupref(Expr *node, Index sortgroupref, indexed_tlist *itlist, Index newvarno)
Definition: setrefs.c:2549
Definition: pg_list.h:50
#define OUTER_VAR
Definition: primnodes.h:176

◆ trivial_subqueryscan()

static bool trivial_subqueryscan ( SubqueryScan plan)
static

Definition at line 1204 of file setrefs.c.

References Assert, equal(), TargetEntry::expr, forboth, IsA, lfirst, list_length(), NIL, Scan::plan, Plan::qual, TargetEntry::resjunk, SubqueryScan::scan, Scan::scanrelid, SubqueryScan::subplan, Plan::targetlist, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by set_subqueryscan_references().

1205 {
1206  int attrno;
1207  ListCell *lp,
1208  *lc;
1209 
1210  if (plan->scan.plan.qual != NIL)
1211  return false;
1212 
1213  if (list_length(plan->scan.plan.targetlist) !=
1214  list_length(plan->subplan->targetlist))
1215  return false; /* tlists not same length */
1216 
1217  attrno = 1;
1218  forboth(lp, plan->scan.plan.targetlist, lc, plan->subplan->targetlist)
1219  {
1220  TargetEntry *ptle = (TargetEntry *) lfirst(lp);
1221  TargetEntry *ctle = (TargetEntry *) lfirst(lc);
1222 
1223  if (ptle->resjunk != ctle->resjunk)
1224  return false; /* tlist doesn't match junk status */
1225 
1226  /*
1227  * We accept either a Var referencing the corresponding element of the
1228  * subplan tlist, or a Const equaling the subplan element. See
1229  * generate_setop_tlist() for motivation.
1230  */
1231  if (ptle->expr && IsA(ptle->expr, Var))
1232  {
1233  Var *var = (Var *) ptle->expr;
1234 
1235  Assert(var->varno == plan->scan.scanrelid);
1236  Assert(var->varlevelsup == 0);
1237  if (var->varattno != attrno)
1238  return false; /* out of order */
1239  }
1240  else if (ptle->expr && IsA(ptle->expr, Const))
1241  {
1242  if (!equal(ptle->expr, ctle->expr))
1243  return false;
1244  }
1245  else
1246  return false;
1247 
1248  attrno++;
1249  }
1250 
1251  return true;
1252 }
#define NIL
Definition: pg_list.h:65
List * qual
Definition: plannodes.h:142
Plan plan
Definition: plannodes.h:343
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Index varlevelsup
Definition: primnodes.h:196
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
Index scanrelid
Definition: plannodes.h:344
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3113
AttrNumber varattno
Definition: primnodes.h:191
Definition: primnodes.h:186
bool resjunk
Definition: primnodes.h:1451
Index varno
Definition: primnodes.h:189
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1444
static int list_length(const List *l)
Definition: pg_list.h:149
List * targetlist
Definition: plannodes.h:141
Plan * subplan
Definition: plannodes.h:527