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:1461
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1210
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 2377 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().

2378 {
2379  indexed_tlist *itlist;
2380  tlist_vinfo *vinfo;
2381  ListCell *l;
2382 
2383  /* Create data structure with enough slots for all tlist entries */
2384  itlist = (indexed_tlist *)
2386  list_length(tlist) * sizeof(tlist_vinfo));
2387 
2388  itlist->tlist = tlist;
2389  itlist->has_ph_vars = false;
2390  itlist->has_non_vars = false;
2391 
2392  /* Find the Vars and fill in the index array */
2393  vinfo = itlist->vars;
2394  foreach(l, tlist)
2395  {
2396  TargetEntry *tle = (TargetEntry *) lfirst(l);
2397 
2398  if (tle->expr && IsA(tle->expr, Var))
2399  {
2400  Var *var = (Var *) tle->expr;
2401 
2402  vinfo->varno = var->varno;
2403  vinfo->varattno = var->varattno;
2404  vinfo->resno = tle->resno;
2405  vinfo++;
2406  }
2407  else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2408  itlist->has_ph_vars = true;
2409  else
2410  itlist->has_non_vars = true;
2411  }
2412 
2413  itlist->num_vars = (vinfo - itlist->vars);
2414 
2415  return itlist;
2416 }
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:1455
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:1454
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 2427 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().

2428 {
2429  indexed_tlist *itlist;
2430  tlist_vinfo *vinfo;
2431  ListCell *l;
2432 
2433  /* Create data structure with enough slots for all tlist entries */
2434  itlist = (indexed_tlist *)
2436  list_length(tlist) * sizeof(tlist_vinfo));
2437 
2438  itlist->tlist = tlist;
2439  itlist->has_ph_vars = false;
2440  itlist->has_non_vars = false;
2441 
2442  /* Find the desired Vars and fill in the index array */
2443  vinfo = itlist->vars;
2444  foreach(l, tlist)
2445  {
2446  TargetEntry *tle = (TargetEntry *) lfirst(l);
2447 
2448  if (tle->expr && IsA(tle->expr, Var))
2449  {
2450  Var *var = (Var *) tle->expr;
2451 
2452  if (var->varno != ignore_rel)
2453  {
2454  vinfo->varno = var->varno;
2455  vinfo->varattno = var->varattno;
2456  vinfo->resno = tle->resno;
2457  vinfo++;
2458  }
2459  }
2460  else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2461  itlist->has_ph_vars = true;
2462  }
2463 
2464  itlist->num_vars = (vinfo - itlist->vars);
2465 
2466  return itlist;
2467 }
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:1455
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:1454
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 1268 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().

1269 {
1270  /* We have to be sure we don't lose any initplans */
1271  child->initPlan = list_concat(parent->initPlan,
1272  child->initPlan);
1273 
1274  /*
1275  * We also have to transfer the parent's column labeling info into the
1276  * child, else columns sent to client will be improperly labeled if this
1277  * is the topmost plan level. resjunk and so on may be important too.
1278  */
1279  apply_tlist_labeling(child->targetlist, parent->targetlist);
1280 
1281  return child;
1282 }
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 2241 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().

2242 {
2243  if (node == NULL)
2244  return NULL;
2245  if (IsA(node, Aggref))
2246  {
2247  Aggref *orig_agg = (Aggref *) node;
2248  Aggref *child_agg;
2249  Aggref *parent_agg;
2250 
2251  /* Assert we've not chosen to partial-ize any unsupported cases */
2252  Assert(orig_agg->aggorder == NIL);
2253  Assert(orig_agg->aggdistinct == NIL);
2254 
2255  /*
2256  * Since aggregate calls can't be nested, we needn't recurse into the
2257  * arguments. But for safety, flat-copy the Aggref node itself rather
2258  * than modifying it in-place.
2259  */
2260  child_agg = makeNode(Aggref);
2261  memcpy(child_agg, orig_agg, sizeof(Aggref));
2262 
2263  /*
2264  * For the parent Aggref, we want to copy all the fields of the
2265  * original aggregate *except* the args list, which we'll replace
2266  * below, and the aggfilter expression, which should be applied only
2267  * by the child not the parent. Rather than explicitly knowing about
2268  * all the other fields here, we can momentarily modify child_agg to
2269  * provide a suitable source for copyObject.
2270  */
2271  child_agg->args = NIL;
2272  child_agg->aggfilter = NULL;
2273  parent_agg = copyObject(child_agg);
2274  child_agg->args = orig_agg->args;
2275  child_agg->aggfilter = orig_agg->aggfilter;
2276 
2277  /*
2278  * Now, set up child_agg to represent the first phase of partial
2279  * aggregation. For now, assume serialization is required.
2280  */
2282 
2283  /*
2284  * And set up parent_agg to represent the second phase.
2285  */
2286  parent_agg->args = list_make1(makeTargetEntry((Expr *) child_agg,
2287  1, NULL, false));
2289 
2290  return (Node *) parent_agg;
2291  }
2293  (void *) context);
2294 }
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:2241
#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 1623 of file setrefs.c.

References palloc().

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

1624 {
1625  Var *newvar = (Var *) palloc(sizeof(Var));
1626 
1627  *newvar = *var;
1628  return newvar;
1629 }
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 3033 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().

3037 {
3038  PlannerGlobal glob;
3039  PlannerInfo root;
3040 
3041  /* Make up dummy planner state so we can use this module's machinery */
3042  MemSet(&glob, 0, sizeof(glob));
3043  glob.type = T_PlannerGlobal;
3044  glob.relationOids = NIL;
3045  glob.invalItems = NIL;
3046  /* Hack: we use glob.dependsOnRole to collect hasRowSecurity flags */
3047  glob.dependsOnRole = false;
3048 
3049  MemSet(&root, 0, sizeof(root));
3050  root.type = T_PlannerInfo;
3051  root.glob = &glob;
3052 
3053  (void) extract_query_dependencies_walker(query, &root);
3054 
3055  *relationOids = glob.relationOids;
3056  *invalItems = glob.invalItems;
3057  *hasRowSecurity = glob.dependsOnRole;
3058 }
#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:3069
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 3069 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().

3070 {
3071  if (node == NULL)
3072  return false;
3073  Assert(!IsA(node, PlaceHolderVar));
3074  if (IsA(node, Query))
3075  {
3076  Query *query = (Query *) node;
3077  ListCell *lc;
3078 
3079  if (query->commandType == CMD_UTILITY)
3080  {
3081  /*
3082  * Ignore utility statements, except those (such as EXPLAIN) that
3083  * contain a parsed-but-not-planned query.
3084  */
3085  query = UtilityContainsQuery(query->utilityStmt);
3086  if (query == NULL)
3087  return false;
3088  }
3089 
3090  /* Remember if any Query has RLS quals applied by rewriter */
3091  if (query->hasRowSecurity)
3092  context->glob->dependsOnRole = true;
3093 
3094  /* Collect relation OIDs in this Query's rtable */
3095  foreach(lc, query->rtable)
3096  {
3097  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
3098 
3099  if (rte->rtekind == RTE_RELATION)
3100  context->glob->relationOids =
3101  lappend_oid(context->glob->relationOids, rte->relid);
3102  else if (rte->rtekind == RTE_NAMEDTUPLESTORE &&
3103  OidIsValid(rte->relid))
3104  context->glob->relationOids =
3105  lappend_oid(context->glob->relationOids,
3106  rte->relid);
3107  }
3108 
3109  /* And recurse into the query's subexpressions */
3111  (void *) context, 0);
3112  }
3113  /* Extract function dependencies and check for regclass Consts */
3114  fix_expr_common(context, node);
3116  (void *) context);
3117 }
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:2137
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:3069
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1645
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 1772 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().

1774 {
1775  SubPlan *bestplan = NULL;
1776  Cost bestcost = 0;
1777  ListCell *lc;
1778 
1779  /*
1780  * Compute the estimated cost of each subplan assuming num_exec
1781  * executions, and keep the cheapest one. Replace discarded subplans with
1782  * NULL pointers in the global subplans list. In event of exact equality
1783  * of estimates, we prefer the later plan; this is a bit arbitrary, but in
1784  * current usage it biases us to break ties against fast-start subplans.
1785  */
1786  Assert(asplan->subplans != NIL);
1787 
1788  foreach(lc, asplan->subplans)
1789  {
1790  SubPlan *curplan = (SubPlan *) lfirst(lc);
1791  Cost curcost;
1792 
1793  curcost = curplan->startup_cost + num_exec * curplan->per_call_cost;
1794  if (bestplan == NULL)
1795  {
1796  bestplan = curplan;
1797  bestcost = curcost;
1798  }
1799  else if (curcost <= bestcost)
1800  {
1801  /* drop old bestplan */
1802  ListCell *lc2 = list_nth_cell(root->glob->subplans,
1803  bestplan->plan_id - 1);
1804 
1805  lfirst(lc2) = NULL;
1806  bestplan = curplan;
1807  bestcost = curcost;
1808  }
1809  else
1810  {
1811  /* drop curplan */
1812  ListCell *lc2 = list_nth_cell(root->glob->subplans,
1813  curplan->plan_id - 1);
1814 
1815  lfirst(lc2) = NULL;
1816  }
1817  }
1818 
1819  return (Node *) bestplan;
1820 }
#define NIL
Definition: pg_list.h:65
int plan_id
Definition: primnodes.h:752
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:776
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Cost startup_cost
Definition: primnodes.h:775
double Cost
Definition: nodes.h:673

◆ fix_expr_common()

static void fix_expr_common ( PlannerInfo root,
Node node 
)
static

Definition at line 1645 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, ScalarArrayOpExpr::negfuncid, 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().

1646 {
1647  /* We assume callers won't call us on a NULL pointer */
1648  if (IsA(node, Aggref))
1649  {
1651  ((Aggref *) node)->aggfnoid);
1652  }
1653  else if (IsA(node, WindowFunc))
1654  {
1656  ((WindowFunc *) node)->winfnoid);
1657  }
1658  else if (IsA(node, FuncExpr))
1659  {
1661  ((FuncExpr *) node)->funcid);
1662  }
1663  else if (IsA(node, OpExpr))
1664  {
1665  set_opfuncid((OpExpr *) node);
1667  ((OpExpr *) node)->opfuncid);
1668  }
1669  else if (IsA(node, DistinctExpr))
1670  {
1671  set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1673  ((DistinctExpr *) node)->opfuncid);
1674  }
1675  else if (IsA(node, NullIfExpr))
1676  {
1677  set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1679  ((NullIfExpr *) node)->opfuncid);
1680  }
1681  else if (IsA(node, ScalarArrayOpExpr))
1682  {
1683  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) node;
1684 
1685  set_sa_opfuncid(saop);
1687 
1688  if (!OidIsValid(saop->hashfuncid))
1690 
1691  if (!OidIsValid(saop->negfuncid))
1693  }
1694  else if (IsA(node, Const))
1695  {
1696  Const *con = (Const *) node;
1697 
1698  /* Check for regclass reference */
1699  if (ISREGCLASSCONST(con))
1700  root->glob->relationOids =
1701  lappend_oid(root->glob->relationOids,
1703  }
1704  else if (IsA(node, GroupingFunc))
1705  {
1706  GroupingFunc *g = (GroupingFunc *) node;
1707  AttrNumber *grouping_map = root->grouping_map;
1708 
1709  /* If there are no grouping sets, we don't need this. */
1710 
1711  Assert(grouping_map || g->cols == NIL);
1712 
1713  if (grouping_map)
1714  {
1715  ListCell *lc;
1716  List *cols = NIL;
1717 
1718  foreach(lc, g->refs)
1719  {
1720  cols = lappend_int(cols, grouping_map[lfirst_int(lc)]);
1721  }
1722 
1723  Assert(!g->cols || equal(cols, g->cols));
1724 
1725  if (!g->cols)
1726  g->cols = cols;
1727  }
1728  }
1729 }
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:3122
#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:2952
#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 2623 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().

2630 {
2631  fix_join_expr_context context;
2632 
2633  context.root = root;
2634  context.outer_itlist = outer_itlist;
2635  context.inner_itlist = inner_itlist;
2636  context.acceptable_rel = acceptable_rel;
2637  context.rtoffset = rtoffset;
2638  context.num_exec = num_exec;
2639  return (List *) fix_join_expr_mutator((Node *) clauses, &context);
2640 }
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:2643
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 2643 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().

2644 {
2645  Var *newvar;
2646 
2647  if (node == NULL)
2648  return NULL;
2649  if (IsA(node, Var))
2650  {
2651  Var *var = (Var *) node;
2652 
2653  /* Look for the var in the input tlists, first in the outer */
2654  if (context->outer_itlist)
2655  {
2656  newvar = search_indexed_tlist_for_var(var,
2657  context->outer_itlist,
2658  OUTER_VAR,
2659  context->rtoffset);
2660  if (newvar)
2661  return (Node *) newvar;
2662  }
2663 
2664  /* then in the inner. */
2665  if (context->inner_itlist)
2666  {
2667  newvar = search_indexed_tlist_for_var(var,
2668  context->inner_itlist,
2669  INNER_VAR,
2670  context->rtoffset);
2671  if (newvar)
2672  return (Node *) newvar;
2673  }
2674 
2675  /* If it's for acceptable_rel, adjust and return it */
2676  if (var->varno == context->acceptable_rel)
2677  {
2678  var = copyVar(var);
2679  var->varno += context->rtoffset;
2680  if (var->varnosyn > 0)
2681  var->varnosyn += context->rtoffset;
2682  return (Node *) var;
2683  }
2684 
2685  /* No referent found for Var */
2686  elog(ERROR, "variable not found in subplan target lists");
2687  }
2688  if (IsA(node, PlaceHolderVar))
2689  {
2690  PlaceHolderVar *phv = (PlaceHolderVar *) node;
2691 
2692  /* See if the PlaceHolderVar has bubbled up from a lower plan node */
2693  if (context->outer_itlist && context->outer_itlist->has_ph_vars)
2694  {
2695  newvar = search_indexed_tlist_for_non_var((Expr *) phv,
2696  context->outer_itlist,
2697  OUTER_VAR);
2698  if (newvar)
2699  return (Node *) newvar;
2700  }
2701  if (context->inner_itlist && context->inner_itlist->has_ph_vars)
2702  {
2703  newvar = search_indexed_tlist_for_non_var((Expr *) phv,
2704  context->inner_itlist,
2705  INNER_VAR);
2706  if (newvar)
2707  return (Node *) newvar;
2708  }
2709 
2710  /* If not supplied by input plans, evaluate the contained expr */
2711  return fix_join_expr_mutator((Node *) phv->phexpr, context);
2712  }
2713  /* Try matching more complex expressions too, if tlists have any */
2714  if (context->outer_itlist && context->outer_itlist->has_non_vars)
2715  {
2716  newvar = search_indexed_tlist_for_non_var((Expr *) node,
2717  context->outer_itlist,
2718  OUTER_VAR);
2719  if (newvar)
2720  return (Node *) newvar;
2721  }
2722  if (context->inner_itlist && context->inner_itlist->has_non_vars)
2723  {
2724  newvar = search_indexed_tlist_for_non_var((Expr *) node,
2725  context->inner_itlist,
2726  INNER_VAR);
2727  if (newvar)
2728  return (Node *) newvar;
2729  }
2730  /* Special cases (apply only AFTER failing to match to lower tlist) */
2731  if (IsA(node, Param))
2732  return fix_param_node(context->root, (Param *) node);
2733  if (IsA(node, AlternativeSubPlan))
2735  (AlternativeSubPlan *) node,
2736  context->num_exec),
2737  context);
2738  fix_expr_common(context->root, node);
2739  return expression_tree_mutator(node,
2741  (void *) context);
2742 }
static Node * fix_alternative_subplan(PlannerInfo *root, AlternativeSubPlan *asplan, double num_exec)
Definition: setrefs.c:1772
#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:1740
Definition: nodes.h:539
Definition: primnodes.h:186
static Node * fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
Definition: setrefs.c:2643
bool has_ph_vars
Definition: setrefs.c:43
#define ERROR
Definition: elog.h:46
static Var * copyVar(Var *var)
Definition: setrefs.c:1623
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:2518
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1645
#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:2478
#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 1740 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().

1741 {
1742  if (p->paramkind == PARAM_MULTIEXPR)
1743  {
1744  int subqueryid = p->paramid >> 16;
1745  int colno = p->paramid & 0xFFFF;
1746  List *params;
1747 
1748  if (subqueryid <= 0 ||
1749  subqueryid > list_length(root->multiexpr_params))
1750  elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
1751  params = (List *) list_nth(root->multiexpr_params, subqueryid - 1);
1752  if (colno <= 0 || colno > list_length(params))
1753  elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
1754  return copyObject(list_nth(params, colno - 1));
1755  }
1756  return (Node *) copyObject(p);
1757 }
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 1841 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().

1842 {
1843  fix_scan_expr_context context;
1844 
1845  context.root = root;
1846  context.rtoffset = rtoffset;
1847  context.num_exec = num_exec;
1848 
1849  if (rtoffset != 0 ||
1850  root->multiexpr_params != NIL ||
1851  root->glob->lastPHId != 0 ||
1852  root->minmax_aggs != NIL ||
1853  root->hasAlternativeSubPlans)
1854  {
1855  return fix_scan_expr_mutator(node, &context);
1856  }
1857  else
1858  {
1859  /*
1860  * If rtoffset == 0, we don't need to change any Vars, and if there
1861  * are no MULTIEXPR subqueries then we don't need to replace
1862  * PARAM_MULTIEXPR Params, and if there are no placeholders anywhere
1863  * we won't need to remove them, and if there are no minmax Aggrefs we
1864  * won't need to replace them, and if there are no AlternativeSubPlans
1865  * we won't need to remove them. Then it's OK to just scribble on the
1866  * input node tree instead of copying (since the only change, filling
1867  * in any unset opfuncid fields, is harmless). This saves just enough
1868  * cycles to be noticeable on trivial queries.
1869  */
1870  (void) fix_scan_expr_walker(node, &context);
1871  return node;
1872  }
1873 }
#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:1951
static Node * fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
Definition: setrefs.c:1876
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 1876 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().

1877 {
1878  if (node == NULL)
1879  return NULL;
1880  if (IsA(node, Var))
1881  {
1882  Var *var = copyVar((Var *) node);
1883 
1884  Assert(var->varlevelsup == 0);
1885 
1886  /*
1887  * We should not see Vars marked INNER_VAR, OUTER_VAR, or ROWID_VAR.
1888  * But an indexqual expression could contain INDEX_VAR Vars.
1889  */
1890  Assert(var->varno != INNER_VAR);
1891  Assert(var->varno != OUTER_VAR);
1892  Assert(var->varno != ROWID_VAR);
1893  if (!IS_SPECIAL_VARNO(var->varno))
1894  var->varno += context->rtoffset;
1895  if (var->varnosyn > 0)
1896  var->varnosyn += context->rtoffset;
1897  return (Node *) var;
1898  }
1899  if (IsA(node, Param))
1900  return fix_param_node(context->root, (Param *) node);
1901  if (IsA(node, Aggref))
1902  {
1903  Aggref *aggref = (Aggref *) node;
1904 
1905  /* See if the Aggref should be replaced by a Param */
1906  if (context->root->minmax_aggs != NIL &&
1907  list_length(aggref->args) == 1)
1908  {
1909  TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
1910  ListCell *lc;
1911 
1912  foreach(lc, context->root->minmax_aggs)
1913  {
1914  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
1915 
1916  if (mminfo->aggfnoid == aggref->aggfnoid &&
1917  equal(mminfo->target, curTarget->expr))
1918  return (Node *) copyObject(mminfo->param);
1919  }
1920  }
1921  /* If no match, just fall through to process it normally */
1922  }
1923  if (IsA(node, CurrentOfExpr))
1924  {
1925  CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
1926 
1927  Assert(cexpr->cvarno != INNER_VAR);
1928  Assert(cexpr->cvarno != OUTER_VAR);
1929  if (!IS_SPECIAL_VARNO(cexpr->cvarno))
1930  cexpr->cvarno += context->rtoffset;
1931  return (Node *) cexpr;
1932  }
1933  if (IsA(node, PlaceHolderVar))
1934  {
1935  /* At scan level, we should always just evaluate the contained expr */
1936  PlaceHolderVar *phv = (PlaceHolderVar *) node;
1937 
1938  return fix_scan_expr_mutator((Node *) phv->phexpr, context);
1939  }
1940  if (IsA(node, AlternativeSubPlan))
1942  (AlternativeSubPlan *) node,
1943  context->num_exec),
1944  context);
1945  fix_expr_common(context->root, node);
1947  (void *) context);
1948 }
#define NIL
Definition: pg_list.h:65
static Node * fix_alternative_subplan(PlannerInfo *root, AlternativeSubPlan *asplan, double num_exec)
Definition: setrefs.c:1772
#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:1740
#define ROWID_VAR
Definition: primnodes.h:178
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3122
Param * param
Definition: pathnodes.h:2426
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:1623
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:1876
Oid aggfnoid
Definition: primnodes.h:323
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1645
#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:2422
Expr * expr
Definition: primnodes.h:1454
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 1951 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().

1952 {
1953  if (node == NULL)
1954  return false;
1955  Assert(!(IsA(node, Var) && ((Var *) node)->varno == ROWID_VAR));
1956  Assert(!IsA(node, PlaceHolderVar));
1957  Assert(!IsA(node, AlternativeSubPlan));
1958  fix_expr_common(context->root, node);
1960  (void *) context);
1961 }
#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:1951
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1645
#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 2776 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().

2782 {
2783  fix_upper_expr_context context;
2784 
2785  context.root = root;
2786  context.subplan_itlist = subplan_itlist;
2787  context.newvarno = newvarno;
2788  context.rtoffset = rtoffset;
2789  context.num_exec = num_exec;
2790  return fix_upper_expr_mutator(node, &context);
2791 }
static Node * fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
Definition: setrefs.c:2794
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 2794 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().

2795 {
2796  Var *newvar;
2797 
2798  if (node == NULL)
2799  return NULL;
2800  if (IsA(node, Var))
2801  {
2802  Var *var = (Var *) node;
2803 
2804  newvar = search_indexed_tlist_for_var(var,
2805  context->subplan_itlist,
2806  context->newvarno,
2807  context->rtoffset);
2808  if (!newvar)
2809  elog(ERROR, "variable not found in subplan target list");
2810  return (Node *) newvar;
2811  }
2812  if (IsA(node, PlaceHolderVar))
2813  {
2814  PlaceHolderVar *phv = (PlaceHolderVar *) node;
2815 
2816  /* See if the PlaceHolderVar has bubbled up from a lower plan node */
2817  if (context->subplan_itlist->has_ph_vars)
2818  {
2819  newvar = search_indexed_tlist_for_non_var((Expr *) phv,
2820  context->subplan_itlist,
2821  context->newvarno);
2822  if (newvar)
2823  return (Node *) newvar;
2824  }
2825  /* If not supplied by input plan, evaluate the contained expr */
2826  return fix_upper_expr_mutator((Node *) phv->phexpr, context);
2827  }
2828  /* Try matching more complex expressions too, if tlist has any */
2829  if (context->subplan_itlist->has_non_vars)
2830  {
2831  newvar = search_indexed_tlist_for_non_var((Expr *) node,
2832  context->subplan_itlist,
2833  context->newvarno);
2834  if (newvar)
2835  return (Node *) newvar;
2836  }
2837  /* Special cases (apply only AFTER failing to match to lower tlist) */
2838  if (IsA(node, Param))
2839  return fix_param_node(context->root, (Param *) node);
2840  if (IsA(node, Aggref))
2841  {
2842  Aggref *aggref = (Aggref *) node;
2843 
2844  /* See if the Aggref should be replaced by a Param */
2845  if (context->root->minmax_aggs != NIL &&
2846  list_length(aggref->args) == 1)
2847  {
2848  TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
2849  ListCell *lc;
2850 
2851  foreach(lc, context->root->minmax_aggs)
2852  {
2853  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2854 
2855  if (mminfo->aggfnoid == aggref->aggfnoid &&
2856  equal(mminfo->target, curTarget->expr))
2857  return (Node *) copyObject(mminfo->param);
2858  }
2859  }
2860  /* If no match, just fall through to process it normally */
2861  }
2862  if (IsA(node, AlternativeSubPlan))
2864  (AlternativeSubPlan *) node,
2865  context->num_exec),
2866  context);
2867  fix_expr_common(context->root, node);
2868  return expression_tree_mutator(node,
2870  (void *) context);
2871 }
#define NIL
Definition: pg_list.h:65
static Node * fix_alternative_subplan(PlannerInfo *root, AlternativeSubPlan *asplan, double num_exec)
Definition: setrefs.c:1772
#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:1740
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3122
Param * param
Definition: pathnodes.h:2426
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:2794
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:2518
Oid aggfnoid
Definition: primnodes.h:323
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1645
#define lfirst(lc)
Definition: pg_list.h:169
Expr * target
Definition: pathnodes.h:2422
static Var * search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist, Index newvarno, int rtoffset)
Definition: setrefs.c:2478
Expr * expr
Definition: primnodes.h:1454
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 1601 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().

1602 {
1603  Relids result = NULL;
1604  int rtindex;
1605 
1606  /* If there's no offset to apply, we needn't recompute the value */
1607  if (rtoffset == 0)
1608  return relids;
1609  rtindex = -1;
1610  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
1611  result = bms_add_member(result, rtindex + rtoffset);
1612  return result;
1613 }
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 2952 of file setrefs.c.

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

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

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

◆ record_plan_type_dependency()

void record_plan_type_dependency ( PlannerInfo root,
Oid  typid 
)

Definition at line 2992 of file setrefs.c.

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

Referenced by eval_const_expressions_mutator().

2993 {
2994  /*
2995  * As in record_plan_function_dependency, ignore the possibility that
2996  * someone would change a built-in domain.
2997  */
2998  if (typid >= (Oid) FirstUnpinnedObjectId)
2999  {
3000  PlanInvalItem *inval_item = makeNode(PlanInvalItem);
3001 
3002  /*
3003  * It would work to use any syscache on pg_type, but the easiest is
3004  * TYPEOID since we already have the type's OID at hand. Note that
3005  * plancache.c knows we use TYPEOID.
3006  */
3007  inval_item->cacheId = TYPEOID;
3008  inval_item->hashValue = GetSysCacheHashValue1(TYPEOID,
3009  ObjectIdGetDatum(typid));
3010 
3011  root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
3012  }
3013 }
#define GetSysCacheHashValue1(cacheId, key1)
Definition: syscache.h:202
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define FirstUnpinnedObjectId
Definition: transam.h:196
PlannerGlobal * glob
Definition: pathnodes.h:163
List * lappend(List *list, void *datum)
Definition: list.c:336
List * invalItems
Definition: pathnodes.h:111
uint32 hashValue
Definition: plannodes.h:1288
#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 2518 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().

2520 {
2521  TargetEntry *tle;
2522 
2523  /*
2524  * If it's a simple Const, replacing it with a Var is silly, even if there
2525  * happens to be an identical Const below; a Var is more expensive to
2526  * execute than a Const. What's more, replacing it could confuse some
2527  * places in the executor that expect to see simple Consts for, eg,
2528  * dropped columns.
2529  */
2530  if (IsA(node, Const))
2531  return NULL;
2532 
2533  tle = tlist_member(node, itlist->tlist);
2534  if (tle)
2535  {
2536  /* Found a matching subplan output expression */
2537  Var *newvar;
2538 
2539  newvar = makeVarFromTargetEntry(newvarno, tle);
2540  newvar->varnosyn = 0; /* wasn't ever a plain Var */
2541  newvar->varattnosyn = 0;
2542  return newvar;
2543  }
2544  return NULL; /* no match */
2545 }
#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 2558 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().

2562 {
2563  ListCell *lc;
2564 
2565  foreach(lc, itlist->tlist)
2566  {
2567  TargetEntry *tle = (TargetEntry *) lfirst(lc);
2568 
2569  /* The equal() check should be redundant, but let's be paranoid */
2570  if (tle->ressortgroupref == sortgroupref &&
2571  equal(node, tle->expr))
2572  {
2573  /* Found a matching subplan output expression */
2574  Var *newvar;
2575 
2576  newvar = makeVarFromTargetEntry(newvarno, tle);
2577  newvar->varnosyn = 0; /* wasn't ever a plain Var */
2578  newvar->varattnosyn = 0;
2579  return newvar;
2580  }
2581  }
2582  return NULL; /* no match */
2583 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:3122
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:1454
Index ressortgroupref
Definition: primnodes.h:1457
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 2478 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().

2480 {
2481  Index varno = var->varno;
2482  AttrNumber varattno = var->varattno;
2483  tlist_vinfo *vinfo;
2484  int i;
2485 
2486  vinfo = itlist->vars;
2487  i = itlist->num_vars;
2488  while (i-- > 0)
2489  {
2490  if (vinfo->varno == varno && vinfo->varattno == varattno)
2491  {
2492  /* Found a match */
2493  Var *newvar = copyVar(var);
2494 
2495  newvar->varno = newvarno;
2496  newvar->varattno = vinfo->resno;
2497  if (newvar->varnosyn > 0)
2498  newvar->varnosyn += rtoffset;
2499  return newvar;
2500  }
2501  vinfo++;
2502  }
2503  return NULL; /* no match */
2504 }
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:1623
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 1444 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().

1447 {
1448  ListCell *l;
1449 
1450  /*
1451  * Append, like Sort et al, doesn't actually evaluate its targetlist or
1452  * check quals. If it's got exactly one child plan, then it's not doing
1453  * anything useful at all, and we can strip it out.
1454  */
1455  Assert(aplan->plan.qual == NIL);
1456 
1457  /* First, we gotta recurse on the children */
1458  foreach(l, aplan->appendplans)
1459  {
1460  lfirst(l) = set_plan_refs(root, (Plan *) lfirst(l), rtoffset);
1461  }
1462 
1463  /* Now, if there's just one, forget the Append and return that child */
1464  if (list_length(aplan->appendplans) == 1)
1465  return clean_up_removed_plan_level((Plan *) aplan,
1466  (Plan *) linitial(aplan->appendplans));
1467 
1468  /*
1469  * Otherwise, clean up the Append as needed. It's okay to do this after
1470  * recursing to the children, because set_dummy_tlist_references doesn't
1471  * look at those.
1472  */
1473  set_dummy_tlist_references((Plan *) aplan, rtoffset);
1474 
1475  aplan->apprelids = offset_relid_set(aplan->apprelids, rtoffset);
1476 
1477  if (aplan->part_prune_info)
1478  {
1479  foreach(l, aplan->part_prune_info->prune_infos)
1480  {
1481  List *prune_infos = lfirst(l);
1482  ListCell *l2;
1483 
1484  foreach(l2, prune_infos)
1485  {
1486  PartitionedRelPruneInfo *pinfo = lfirst(l2);
1487 
1488  pinfo->rtindex += rtoffset;
1489  }
1490  }
1491  }
1492 
1493  /* We don't need to recurse to lefttree or righttree ... */
1494  Assert(aplan->plan.lefttree == NULL);
1495  Assert(aplan->plan.righttree == NULL);
1496 
1497  return (Plan *) aplan;
1498 }
#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:1601
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:1268
#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:2310
Plan plan
Definition: plannodes.h:251

◆ set_customscan_references()

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

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

1374 {
1375  ListCell *lc;
1376 
1377  /* Adjust scanrelid if it's valid */
1378  if (cscan->scan.scanrelid > 0)
1379  cscan->scan.scanrelid += rtoffset;
1380 
1381  if (cscan->custom_scan_tlist != NIL || cscan->scan.scanrelid == 0)
1382  {
1383  /* Adjust tlist, qual, custom_exprs to reference custom scan tuple */
1385 
1386  cscan->scan.plan.targetlist = (List *)
1387  fix_upper_expr(root,
1388  (Node *) cscan->scan.plan.targetlist,
1389  itlist,
1390  INDEX_VAR,
1391  rtoffset,
1392  NUM_EXEC_TLIST((Plan *) cscan));
1393  cscan->scan.plan.qual = (List *)
1394  fix_upper_expr(root,
1395  (Node *) cscan->scan.plan.qual,
1396  itlist,
1397  INDEX_VAR,
1398  rtoffset,
1399  NUM_EXEC_QUAL((Plan *) cscan));
1400  cscan->custom_exprs = (List *)
1401  fix_upper_expr(root,
1402  (Node *) cscan->custom_exprs,
1403  itlist,
1404  INDEX_VAR,
1405  rtoffset,
1406  NUM_EXEC_QUAL((Plan *) cscan));
1407  pfree(itlist);
1408  /* custom_scan_tlist itself just needs fix_scan_list() adjustments */
1409  cscan->custom_scan_tlist =
1410  fix_scan_list(root, cscan->custom_scan_tlist,
1411  rtoffset, NUM_EXEC_TLIST((Plan *) cscan));
1412  }
1413  else
1414  {
1415  /* Adjust tlist, qual, custom_exprs in the standard way */
1416  cscan->scan.plan.targetlist =
1417  fix_scan_list(root, cscan->scan.plan.targetlist,
1418  rtoffset, NUM_EXEC_TLIST((Plan *) cscan));
1419  cscan->scan.plan.qual =
1420  fix_scan_list(root, cscan->scan.plan.qual,
1421  rtoffset, NUM_EXEC_QUAL((Plan *) cscan));
1422  cscan->custom_exprs =
1423  fix_scan_list(root, cscan->custom_exprs,
1424  rtoffset, NUM_EXEC_QUAL((Plan *) cscan));
1425  }
1426 
1427  /* Adjust child plan-nodes recursively, if needed */
1428  foreach(lc, cscan->custom_plans)
1429  {
1430  lfirst(lc) = set_plan_refs(root, (Plan *) lfirst(lc), rtoffset);
1431  }
1432 
1433  cscan->custom_relids = offset_relid_set(cscan->custom_relids, rtoffset);
1434 }
#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:2776
static Relids offset_relid_set(Relids relids, int rtoffset)
Definition: setrefs.c:1601
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:2377
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 2310 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().

2311 {
2312  List *output_targetlist;
2313  ListCell *l;
2314 
2315  output_targetlist = NIL;
2316  foreach(l, plan->targetlist)
2317  {
2318  TargetEntry *tle = (TargetEntry *) lfirst(l);
2319  Var *oldvar = (Var *) tle->expr;
2320  Var *newvar;
2321 
2322  /*
2323  * As in search_indexed_tlist_for_non_var(), we prefer to keep Consts
2324  * as Consts, not Vars referencing Consts. Here, there's no speed
2325  * advantage to be had, but it makes EXPLAIN output look cleaner, and
2326  * again it avoids confusing the executor.
2327  */
2328  if (IsA(oldvar, Const))
2329  {
2330  /* just reuse the existing TLE node */
2331  output_targetlist = lappend(output_targetlist, tle);
2332  continue;
2333  }
2334 
2335  newvar = makeVar(OUTER_VAR,
2336  tle->resno,
2337  exprType((Node *) oldvar),
2338  exprTypmod((Node *) oldvar),
2339  exprCollation((Node *) oldvar),
2340  0);
2341  if (IsA(oldvar, Var) &&
2342  oldvar->varnosyn > 0)
2343  {
2344  newvar->varnosyn = oldvar->varnosyn + rtoffset;
2345  newvar->varattnosyn = oldvar->varattnosyn;
2346  }
2347  else
2348  {
2349  newvar->varnosyn = 0; /* wasn't ever a plain Var */
2350  newvar->varattnosyn = 0;
2351  }
2352 
2353  tle = flatCopyTargetEntry(tle);
2354  tle->expr = (Expr *) newvar;
2355  output_targetlist = lappend(output_targetlist, tle);
2356  }
2357  plan->targetlist = output_targetlist;
2358 
2359  /* We don't touch plan->qual here */
2360 }
#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:1455
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:1454
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 1289 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().

1292 {
1293  /* Adjust scanrelid if it's valid */
1294  if (fscan->scan.scanrelid > 0)
1295  fscan->scan.scanrelid += rtoffset;
1296 
1297  if (fscan->fdw_scan_tlist != NIL || fscan->scan.scanrelid == 0)
1298  {
1299  /*
1300  * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals to reference
1301  * foreign scan tuple
1302  */
1304 
1305  fscan->scan.plan.targetlist = (List *)
1306  fix_upper_expr(root,
1307  (Node *) fscan->scan.plan.targetlist,
1308  itlist,
1309  INDEX_VAR,
1310  rtoffset,
1311  NUM_EXEC_TLIST((Plan *) fscan));
1312  fscan->scan.plan.qual = (List *)
1313  fix_upper_expr(root,
1314  (Node *) fscan->scan.plan.qual,
1315  itlist,
1316  INDEX_VAR,
1317  rtoffset,
1318  NUM_EXEC_QUAL((Plan *) fscan));
1319  fscan->fdw_exprs = (List *)
1320  fix_upper_expr(root,
1321  (Node *) fscan->fdw_exprs,
1322  itlist,
1323  INDEX_VAR,
1324  rtoffset,
1325  NUM_EXEC_QUAL((Plan *) fscan));
1326  fscan->fdw_recheck_quals = (List *)
1327  fix_upper_expr(root,
1328  (Node *) fscan->fdw_recheck_quals,
1329  itlist,
1330  INDEX_VAR,
1331  rtoffset,
1332  NUM_EXEC_QUAL((Plan *) fscan));
1333  pfree(itlist);
1334  /* fdw_scan_tlist itself just needs fix_scan_list() adjustments */
1335  fscan->fdw_scan_tlist =
1336  fix_scan_list(root, fscan->fdw_scan_tlist,
1337  rtoffset, NUM_EXEC_TLIST((Plan *) fscan));
1338  }
1339  else
1340  {
1341  /*
1342  * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals in the standard
1343  * way
1344  */
1345  fscan->scan.plan.targetlist =
1346  fix_scan_list(root, fscan->scan.plan.targetlist,
1347  rtoffset, NUM_EXEC_TLIST((Plan *) fscan));
1348  fscan->scan.plan.qual =
1349  fix_scan_list(root, fscan->scan.plan.qual,
1350  rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1351  fscan->fdw_exprs =
1352  fix_scan_list(root, fscan->fdw_exprs,
1353  rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1354  fscan->fdw_recheck_quals =
1355  fix_scan_list(root, fscan->fdw_recheck_quals,
1356  rtoffset, NUM_EXEC_QUAL((Plan *) fscan));
1357  }
1358 
1359  fscan->fs_relids = offset_relid_set(fscan->fs_relids, rtoffset);
1360 
1361  /* Adjust resultRelation if it's valid */
1362  if (fscan->resultRelation > 0)
1363  fscan->resultRelation += rtoffset;
1364 }
#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:2776
List * fdw_scan_tlist
Definition: plannodes.h:639
static Relids offset_relid_set(Relids relids, int rtoffset)
Definition: setrefs.c:1601
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:2377
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 1569 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().

1570 {
1571  Hash *hplan = (Hash *) plan;
1572  Plan *outer_plan = plan->lefttree;
1573  indexed_tlist *outer_itlist;
1574 
1575  /*
1576  * Hash's hashkeys are used when feeding tuples into the hashtable,
1577  * therefore have them reference Hash's outer plan (which itself is the
1578  * inner plan of the HashJoin).
1579  */
1580  outer_itlist = build_tlist_index(outer_plan->targetlist);
1581  hplan->hashkeys = (List *)
1582  fix_upper_expr(root,
1583  (Node *) hplan->hashkeys,
1584  outer_itlist,
1585  OUTER_VAR,
1586  rtoffset,
1587  NUM_EXEC_QUAL(plan));
1588 
1589  /* Hash doesn't project */
1590  set_dummy_tlist_references(plan, rtoffset);
1591 
1592  /* Hash nodes don't have their own quals */
1593  Assert(plan->qual == NIL);
1594 }
#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:2776
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:2377
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:2310

◆ set_indexonlyscan_references()

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

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

1116 {
1117  indexed_tlist *index_itlist;
1118 
1119  index_itlist = build_tlist_index(plan->indextlist);
1120 
1121  plan->scan.scanrelid += rtoffset;
1122  plan->scan.plan.targetlist = (List *)
1123  fix_upper_expr(root,
1124  (Node *) plan->scan.plan.targetlist,
1125  index_itlist,
1126  INDEX_VAR,
1127  rtoffset,
1128  NUM_EXEC_TLIST((Plan *) plan));
1129  plan->scan.plan.qual = (List *)
1130  fix_upper_expr(root,
1131  (Node *) plan->scan.plan.qual,
1132  index_itlist,
1133  INDEX_VAR,
1134  rtoffset,
1135  NUM_EXEC_QUAL((Plan *) plan));
1136  /* indexqual is already transformed to reference index columns */
1137  plan->indexqual = fix_scan_list(root, plan->indexqual,
1138  rtoffset, 1);
1139  /* indexorderby is already transformed to reference index columns */
1140  plan->indexorderby = fix_scan_list(root, plan->indexorderby,
1141  rtoffset, 1);
1142  /* indextlist must NOT be transformed to reference index columns */
1143  plan->indextlist = fix_scan_list(root, plan->indextlist,
1144  rtoffset, NUM_EXEC_TLIST((Plan *) plan));
1145 
1146  pfree(index_itlist);
1147 
1148  return (Plan *) plan;
1149 }
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:2776
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:2377
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 1972 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().

1973 {
1974  Plan *outer_plan = join->plan.lefttree;
1975  Plan *inner_plan = join->plan.righttree;
1976  indexed_tlist *outer_itlist;
1977  indexed_tlist *inner_itlist;
1978 
1979  outer_itlist = build_tlist_index(outer_plan->targetlist);
1980  inner_itlist = build_tlist_index(inner_plan->targetlist);
1981 
1982  /*
1983  * First process the joinquals (including merge or hash clauses). These
1984  * are logically below the join so they can always use all values
1985  * available from the input tlists. It's okay to also handle
1986  * NestLoopParams now, because those couldn't refer to nullable
1987  * subexpressions.
1988  */
1989  join->joinqual = fix_join_expr(root,
1990  join->joinqual,
1991  outer_itlist,
1992  inner_itlist,
1993  (Index) 0,
1994  rtoffset,
1995  NUM_EXEC_QUAL((Plan *) join));
1996 
1997  /* Now do join-type-specific stuff */
1998  if (IsA(join, NestLoop))
1999  {
2000  NestLoop *nl = (NestLoop *) join;
2001  ListCell *lc;
2002 
2003  foreach(lc, nl->nestParams)
2004  {
2005  NestLoopParam *nlp = (NestLoopParam *) lfirst(lc);
2006 
2007  nlp->paramval = (Var *) fix_upper_expr(root,
2008  (Node *) nlp->paramval,
2009  outer_itlist,
2010  OUTER_VAR,
2011  rtoffset,
2012  NUM_EXEC_TLIST(outer_plan));
2013  /* Check we replaced any PlaceHolderVar with simple Var */
2014  if (!(IsA(nlp->paramval, Var) &&
2015  nlp->paramval->varno == OUTER_VAR))
2016  elog(ERROR, "NestLoopParam was not reduced to a simple Var");
2017  }
2018  }
2019  else if (IsA(join, MergeJoin))
2020  {
2021  MergeJoin *mj = (MergeJoin *) join;
2022 
2023  mj->mergeclauses = fix_join_expr(root,
2024  mj->mergeclauses,
2025  outer_itlist,
2026  inner_itlist,
2027  (Index) 0,
2028  rtoffset,
2029  NUM_EXEC_QUAL((Plan *) join));
2030  }
2031  else if (IsA(join, HashJoin))
2032  {
2033  HashJoin *hj = (HashJoin *) join;
2034 
2035  hj->hashclauses = fix_join_expr(root,
2036  hj->hashclauses,
2037  outer_itlist,
2038  inner_itlist,
2039  (Index) 0,
2040  rtoffset,
2041  NUM_EXEC_QUAL((Plan *) join));
2042 
2043  /*
2044  * HashJoin's hashkeys are used to look for matching tuples from its
2045  * outer plan (not the Hash node!) in the hashtable.
2046  */
2047  hj->hashkeys = (List *) fix_upper_expr(root,
2048  (Node *) hj->hashkeys,
2049  outer_itlist,
2050  OUTER_VAR,
2051  rtoffset,
2052  NUM_EXEC_QUAL((Plan *) join));
2053  }
2054 
2055  /*
2056  * Now we need to fix up the targetlist and qpqual, which are logically
2057  * above the join. This means they should not re-use any input expression
2058  * that was computed in the nullable side of an outer join. Vars and
2059  * PlaceHolderVars are fine, so we can implement this restriction just by
2060  * clearing has_non_vars in the indexed_tlist structs.
2061  *
2062  * XXX This is a grotty workaround for the fact that we don't clearly
2063  * distinguish between a Var appearing below an outer join and the "same"
2064  * Var appearing above it. If we did, we'd not need to hack the matching
2065  * rules this way.
2066  */
2067  switch (join->jointype)
2068  {
2069  case JOIN_LEFT:
2070  case JOIN_SEMI:
2071  case JOIN_ANTI:
2072  inner_itlist->has_non_vars = false;
2073  break;
2074  case JOIN_RIGHT:
2075  outer_itlist->has_non_vars = false;
2076  break;
2077  case JOIN_FULL:
2078  outer_itlist->has_non_vars = false;
2079  inner_itlist->has_non_vars = false;
2080  break;
2081  default:
2082  break;
2083  }
2084 
2085  join->plan.targetlist = fix_join_expr(root,
2086  join->plan.targetlist,
2087  outer_itlist,
2088  inner_itlist,
2089  (Index) 0,
2090  rtoffset,
2091  NUM_EXEC_TLIST((Plan *) join));
2092  join->plan.qual = fix_join_expr(root,
2093  join->plan.qual,
2094  outer_itlist,
2095  inner_itlist,
2096  (Index) 0,
2097  rtoffset,
2098  NUM_EXEC_QUAL((Plan *) join));
2099 
2100  pfree(outer_itlist);
2101  pfree(inner_itlist);
2102 }
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:2776
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:2377
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:2623
Plan plan
Definition: plannodes.h:703

◆ set_mergeappend_references()

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

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

1511 {
1512  ListCell *l;
1513 
1514  /*
1515  * MergeAppend, like Sort et al, doesn't actually evaluate its targetlist
1516  * or check quals. If it's got exactly one child plan, then it's not
1517  * doing anything useful at all, and we can strip it out.
1518  */
1519  Assert(mplan->plan.qual == NIL);
1520 
1521  /* First, we gotta recurse on the children */
1522  foreach(l, mplan->mergeplans)
1523  {
1524  lfirst(l) = set_plan_refs(root, (Plan *) lfirst(l), rtoffset);
1525  }
1526 
1527  /* Now, if there's just one, forget the MergeAppend and return that child */
1528  if (list_length(mplan->mergeplans) == 1)
1529  return clean_up_removed_plan_level((Plan *) mplan,
1530  (Plan *) linitial(mplan->mergeplans));
1531 
1532  /*
1533  * Otherwise, clean up the MergeAppend as needed. It's okay to do this
1534  * after recursing to the children, because set_dummy_tlist_references
1535  * doesn't look at those.
1536  */
1537  set_dummy_tlist_references((Plan *) mplan, rtoffset);
1538 
1539  mplan->apprelids = offset_relid_set(mplan->apprelids, rtoffset);
1540 
1541  if (mplan->part_prune_info)
1542  {
1543  foreach(l, mplan->part_prune_info->prune_infos)
1544  {
1545  List *prune_infos = lfirst(l);
1546  ListCell *l2;
1547 
1548  foreach(l2, prune_infos)
1549  {
1550  PartitionedRelPruneInfo *pinfo = lfirst(l2);
1551 
1552  pinfo->rtindex += rtoffset;
1553  }
1554  }
1555  }
1556 
1557  /* We don't need to recurse to lefttree or righttree ... */
1558  Assert(mplan->plan.lefttree == NULL);
1559  Assert(mplan->plan.righttree == NULL);
1560 
1561  return (Plan *) mplan;
1562 }
#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:1601
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:1268
#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:2310

◆ set_param_references()

static void set_param_references ( PlannerInfo root,
Plan plan 
)
static

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

2187 {
2188  Assert(IsA(plan, Gather) || IsA(plan, GatherMerge));
2189 
2190  if (plan->lefttree->extParam)
2191  {
2192  PlannerInfo *proot;
2193  Bitmapset *initSetParam = NULL;
2194  ListCell *l;
2195 
2196  for (proot = root; proot != NULL; proot = proot->parent_root)
2197  {
2198  foreach(l, proot->init_plans)
2199  {
2200  SubPlan *initsubplan = (SubPlan *) lfirst(l);
2201  ListCell *l2;
2202 
2203  foreach(l2, initsubplan->setParam)
2204  {
2205  initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
2206  }
2207  }
2208  }
2209 
2210  /*
2211  * Remember the list of all external initplan params that are used by
2212  * the children of Gather or Gather merge node.
2213  */
2214  if (IsA(plan, Gather))
2215  ((Gather *) plan)->initParam =
2216  bms_intersect(plan->lefttree->extParam, initSetParam);
2217  else
2218  ((GatherMerge *) plan)->initParam =
2219  bms_intersect(plan->lefttree->extParam, initSetParam);
2220  }
2221 }
#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:770
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:1118
List * translated_vars
Definition: pathnodes.h:2326
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:2299
Index parent_relid
Definition: pathnodes.h:2298
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, Memoize::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_Memoize, T_MergeAppend, T_MergeJoin, T_ModifyTable, T_NamedTuplestoreScan, T_NestLoop, T_ProjectSet, T_RecursiveUnion, T_Result, 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_Memoize:
756  {
757  Memoize *mplan = (Memoize *) plan;
758 
759  /*
760  * Memoize does not evaluate its targetlist. It just uses the
761  * same targetlist from its outer subnode.
762  */
763  set_dummy_tlist_references(plan, rtoffset);
764 
765  mplan->param_exprs = fix_scan_list(root, mplan->param_exprs,
766  rtoffset,
767  NUM_EXEC_TLIST(plan));
768  break;
769  }
770 
771  case T_Material:
772  case T_Sort:
773  case T_IncrementalSort:
774  case T_Unique:
775  case T_SetOp:
776 
777  /*
778  * These plan types don't actually bother to evaluate their
779  * targetlists, because they just return their unmodified input
780  * tuples. Even though the targetlist won't be used by the
781  * executor, we fix it up for possible use by EXPLAIN (not to
782  * mention ease of debugging --- wrong varnos are very confusing).
783  */
784  set_dummy_tlist_references(plan, rtoffset);
785 
786  /*
787  * Since these plan types don't check quals either, we should not
788  * find any qual expression attached to them.
789  */
790  Assert(plan->qual == NIL);
791  break;
792  case T_LockRows:
793  {
794  LockRows *splan = (LockRows *) plan;
795 
796  /*
797  * Like the plan types above, LockRows doesn't evaluate its
798  * tlist or quals. But we have to fix up the RT indexes in
799  * its rowmarks.
800  */
801  set_dummy_tlist_references(plan, rtoffset);
802  Assert(splan->plan.qual == NIL);
803 
804  foreach(l, splan->rowMarks)
805  {
806  PlanRowMark *rc = (PlanRowMark *) lfirst(l);
807 
808  rc->rti += rtoffset;
809  rc->prti += rtoffset;
810  }
811  }
812  break;
813  case T_Limit:
814  {
815  Limit *splan = (Limit *) plan;
816 
817  /*
818  * Like the plan types above, Limit doesn't evaluate its tlist
819  * or quals. It does have live expressions for limit/offset,
820  * however; and those cannot contain subplan variable refs, so
821  * fix_scan_expr works for them.
822  */
823  set_dummy_tlist_references(plan, rtoffset);
824  Assert(splan->plan.qual == NIL);
825 
826  splan->limitOffset =
827  fix_scan_expr(root, splan->limitOffset, rtoffset, 1);
828  splan->limitCount =
829  fix_scan_expr(root, splan->limitCount, rtoffset, 1);
830  }
831  break;
832  case T_Agg:
833  {
834  Agg *agg = (Agg *) plan;
835 
836  /*
837  * If this node is combining partial-aggregation results, we
838  * must convert its Aggrefs to contain references to the
839  * partial-aggregate subexpressions that will be available
840  * from the child plan node.
841  */
842  if (DO_AGGSPLIT_COMBINE(agg->aggsplit))
843  {
844  plan->targetlist = (List *)
846  NULL);
847  plan->qual = (List *)
849  NULL);
850  }
851 
852  set_upper_references(root, plan, rtoffset);
853  }
854  break;
855  case T_Group:
856  set_upper_references(root, plan, rtoffset);
857  break;
858  case T_WindowAgg:
859  {
860  WindowAgg *wplan = (WindowAgg *) plan;
861 
862  set_upper_references(root, plan, rtoffset);
863 
864  /*
865  * Like Limit node limit/offset expressions, WindowAgg has
866  * frame offset expressions, which cannot contain subplan
867  * variable refs, so fix_scan_expr works for them.
868  */
869  wplan->startOffset =
870  fix_scan_expr(root, wplan->startOffset, rtoffset, 1);
871  wplan->endOffset =
872  fix_scan_expr(root, wplan->endOffset, rtoffset, 1);
873  }
874  break;
875  case T_Result:
876  {
877  Result *splan = (Result *) plan;
878 
879  /*
880  * Result may or may not have a subplan; if not, it's more
881  * like a scan node than an upper node.
882  */
883  if (splan->plan.lefttree != NULL)
884  set_upper_references(root, plan, rtoffset);
885  else
886  {
887  /*
888  * The tlist of a childless Result could contain
889  * unresolved ROWID_VAR Vars, in case it's representing a
890  * target relation which is completely empty because of
891  * constraint exclusion. Replace any such Vars by null
892  * constants, as though they'd been resolved for a leaf
893  * scan node that doesn't support them. We could have
894  * fix_scan_expr do this, but since the case is only
895  * expected to occur here, it seems safer to special-case
896  * it here and keep the assertions that ROWID_VARs
897  * shouldn't be seen by fix_scan_expr.
898  */
899  foreach(l, splan->plan.targetlist)
900  {
901  TargetEntry *tle = (TargetEntry *) lfirst(l);
902  Var *var = (Var *) tle->expr;
903 
904  if (var && IsA(var, Var) && var->varno == ROWID_VAR)
905  tle->expr = (Expr *) makeNullConst(var->vartype,
906  var->vartypmod,
907  var->varcollid);
908  }
909 
910  splan->plan.targetlist =
911  fix_scan_list(root, splan->plan.targetlist,
912  rtoffset, NUM_EXEC_TLIST(plan));
913  splan->plan.qual =
914  fix_scan_list(root, splan->plan.qual,
915  rtoffset, NUM_EXEC_QUAL(plan));
916  }
917  /* resconstantqual can't contain any subplan variable refs */
918  splan->resconstantqual =
919  fix_scan_expr(root, splan->resconstantqual, rtoffset, 1);
920  }
921  break;
922  case T_ProjectSet:
923  set_upper_references(root, plan, rtoffset);
924  break;
925  case T_ModifyTable:
926  {
927  ModifyTable *splan = (ModifyTable *) plan;
928 
929  Assert(splan->plan.targetlist == NIL);
930  Assert(splan->plan.qual == NIL);
931 
932  splan->withCheckOptionLists =
933  fix_scan_list(root, splan->withCheckOptionLists,
934  rtoffset, 1);
935 
936  if (splan->returningLists)
937  {
938  List *newRL = NIL;
939  Plan *subplan = outerPlan(splan);
940  ListCell *lcrl,
941  *lcrr;
942 
943  /*
944  * Pass each per-resultrel returningList through
945  * set_returning_clause_references().
946  */
948  forboth(lcrl, splan->returningLists,
949  lcrr, splan->resultRelations)
950  {
951  List *rlist = (List *) lfirst(lcrl);
952  Index resultrel = lfirst_int(lcrr);
953 
954  rlist = set_returning_clause_references(root,
955  rlist,
956  subplan,
957  resultrel,
958  rtoffset);
959  newRL = lappend(newRL, rlist);
960  }
961  splan->returningLists = newRL;
962 
963  /*
964  * Set up the visible plan targetlist as being the same as
965  * the first RETURNING list. This is for the use of
966  * EXPLAIN; the executor won't pay any attention to the
967  * targetlist. We postpone this step until here so that
968  * we don't have to do set_returning_clause_references()
969  * twice on identical targetlists.
970  */
971  splan->plan.targetlist = copyObject(linitial(newRL));
972  }
973 
974  /*
975  * We treat ModifyTable with ON CONFLICT as a form of 'pseudo
976  * join', where the inner side is the EXCLUDED tuple.
977  * Therefore use fix_join_expr to setup the relevant variables
978  * to INNER_VAR. We explicitly don't create any OUTER_VARs as
979  * those are already used by RETURNING and it seems better to
980  * be non-conflicting.
981  */
982  if (splan->onConflictSet)
983  {
984  indexed_tlist *itlist;
985 
986  itlist = build_tlist_index(splan->exclRelTlist);
987 
988  splan->onConflictSet =
989  fix_join_expr(root, splan->onConflictSet,
990  NULL, itlist,
992  rtoffset, NUM_EXEC_QUAL(plan));
993 
994  splan->onConflictWhere = (Node *)
995  fix_join_expr(root, (List *) splan->onConflictWhere,
996  NULL, itlist,
998  rtoffset, NUM_EXEC_QUAL(plan));
999 
1000  pfree(itlist);
1001 
1002  splan->exclRelTlist =
1003  fix_scan_list(root, splan->exclRelTlist, rtoffset, 1);
1004  }
1005 
1006  splan->nominalRelation += rtoffset;
1007  if (splan->rootRelation)
1008  splan->rootRelation += rtoffset;
1009  splan->exclRelRTI += rtoffset;
1010 
1011  foreach(l, splan->resultRelations)
1012  {
1013  lfirst_int(l) += rtoffset;
1014  }
1015  foreach(l, splan->rowMarks)
1016  {
1017  PlanRowMark *rc = (PlanRowMark *) lfirst(l);
1018 
1019  rc->rti += rtoffset;
1020  rc->prti += rtoffset;
1021  }
1022 
1023  /*
1024  * Append this ModifyTable node's final result relation RT
1025  * index(es) to the global list for the plan.
1026  */
1027  root->glob->resultRelations =
1029  splan->resultRelations);
1030  if (splan->rootRelation)
1031  {
1032  root->glob->resultRelations =
1034  splan->rootRelation);
1035  }
1036  }
1037  break;
1038  case T_Append:
1039  /* Needs special treatment, see comments below */
1040  return set_append_references(root,
1041  (Append *) plan,
1042  rtoffset);
1043  case T_MergeAppend:
1044  /* Needs special treatment, see comments below */
1045  return set_mergeappend_references(root,
1046  (MergeAppend *) plan,
1047  rtoffset);
1048  case T_RecursiveUnion:
1049  /* This doesn't evaluate targetlist or check quals either */
1050  set_dummy_tlist_references(plan, rtoffset);
1051  Assert(plan->qual == NIL);
1052  break;
1053  case T_BitmapAnd:
1054  {
1055  BitmapAnd *splan = (BitmapAnd *) plan;
1056 
1057  /* BitmapAnd works like Append, but has no tlist */
1058  Assert(splan->plan.targetlist == NIL);
1059  Assert(splan->plan.qual == NIL);
1060  foreach(l, splan->bitmapplans)
1061  {
1062  lfirst(l) = set_plan_refs(root,
1063  (Plan *) lfirst(l),
1064  rtoffset);
1065  }
1066  }
1067  break;
1068  case T_BitmapOr:
1069  {
1070  BitmapOr *splan = (BitmapOr *) plan;
1071 
1072  /* BitmapOr works like Append, but has no tlist */
1073  Assert(splan->plan.targetlist == NIL);
1074  Assert(splan->plan.qual == NIL);
1075  foreach(l, splan->bitmapplans)
1076  {
1077  lfirst(l) = set_plan_refs(root,
1078  (Plan *) lfirst(l),
1079  rtoffset);
1080  }
1081  }
1082  break;
1083  default:
1084  elog(ERROR, "unrecognized node type: %d",
1085  (int) nodeTag(plan));
1086  break;
1087  }
1088 
1089  /*
1090  * Now recurse into child plans, if any
1091  *
1092  * NOTE: it is essential that we recurse into child plans AFTER we set
1093  * subplan references in this plan's tlist and quals. If we did the
1094  * reference-adjustments bottom-up, then we would fail to match this
1095  * plan's var nodes against the already-modified nodes of the children.
1096  */
1097  plan->lefttree = set_plan_refs(root, plan->lefttree, rtoffset);
1098  plan->righttree = set_plan_refs(root, plan->righttree, rtoffset);
1099 
1100  return plan;
1101 }
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:1972
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:1118
List * values_lists
Definition: plannodes.h:548
static void set_param_references(PlannerInfo *root, Plan *plan)
Definition: setrefs.c:2186
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:1444
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:1289
#define linitial_int(l)
Definition: pg_list.h:175
Node * startOffset
Definition: plannodes.h:891
Node * resconstantqual
Definition: plannodes.h:188
List * param_exprs
Definition: plannodes.h:795
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:1371
static Plan * set_subqueryscan_references(PlannerInfo *root, SubqueryScan *plan, int rtoffset)
Definition: setrefs.c:1159
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:1113
List * bitmapplans
Definition: plannodes.h:333
static void set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:2123
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:2241
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:1454
List * rowMarks
Definition: plannodes.h:1010
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:2377
Scan scan
Definition: plannodes.h:491
static int list_length(const List *l)
Definition: pg_list.h:149
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:2902
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:1508
Definition: pg_list.h:50
static Node * fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset, double num_exec)
Definition: setrefs.c:1841
static void set_dummy_tlist_references(Plan *plan, int rtoffset)
Definition: setrefs.c:2310
List * exclRelTlist
Definition: plannodes.h:239
static void set_hash_references(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:1569
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:2623
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 2902 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().

2907 {
2908  indexed_tlist *itlist;
2909 
2910  /*
2911  * We can perform the desired Var fixup by abusing the fix_join_expr
2912  * machinery that formerly handled inner indexscan fixup. We search the
2913  * top plan's targetlist for Vars of non-result relations, and use
2914  * fix_join_expr to convert RETURNING Vars into references to those tlist
2915  * entries, while leaving result-rel Vars as-is.
2916  *
2917  * PlaceHolderVars will also be sought in the targetlist, but no
2918  * more-complex expressions will be. Note that it is not possible for a
2919  * PlaceHolderVar to refer to the result relation, since the result is
2920  * never below an outer join. If that case could happen, we'd have to be
2921  * prepared to pick apart the PlaceHolderVar and evaluate its contained
2922  * expression instead.
2923  */
2924  itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
2925 
2926  rlist = fix_join_expr(root,
2927  rlist,
2928  itlist,
2929  NULL,
2930  resultRelation,
2931  rtoffset,
2932  NUM_EXEC_TLIST(topplan));
2933 
2934  pfree(itlist);
2935 
2936  return rlist;
2937 }
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:2427
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:2623

◆ set_subqueryscan_references()

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

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

1162 {
1163  RelOptInfo *rel;
1164  Plan *result;
1165 
1166  /* Need to look up the subquery's RelOptInfo, since we need its subroot */
1167  rel = find_base_rel(root, plan->scan.scanrelid);
1168 
1169  /* Recursively process the subplan */
1170  plan->subplan = set_plan_references(rel->subroot, plan->subplan);
1171 
1172  if (trivial_subqueryscan(plan))
1173  {
1174  /*
1175  * We can omit the SubqueryScan node and just pull up the subplan.
1176  */
1177  result = clean_up_removed_plan_level((Plan *) plan, plan->subplan);
1178  }
1179  else
1180  {
1181  /*
1182  * Keep the SubqueryScan node. We have to do the processing that
1183  * set_plan_references would otherwise have done on it. Notice we do
1184  * not do set_upper_references() here, because a SubqueryScan will
1185  * always have been created with correct references to its subplan's
1186  * outputs to begin with.
1187  */
1188  plan->scan.scanrelid += rtoffset;
1189  plan->scan.plan.targetlist =
1190  fix_scan_list(root, plan->scan.plan.targetlist,
1191  rtoffset, NUM_EXEC_TLIST((Plan *) plan));
1192  plan->scan.plan.qual =
1193  fix_scan_list(root, plan->scan.plan.qual,
1194  rtoffset, NUM_EXEC_QUAL((Plan *) plan));
1195 
1196  result = (Plan *) plan;
1197  }
1198 
1199  return result;
1200 }
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:1210
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:1268
#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:375
Plan * subplan
Definition: plannodes.h:527

◆ set_upper_references()

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

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

2124 {
2125  Plan *subplan = plan->lefttree;
2126  indexed_tlist *subplan_itlist;
2127  List *output_targetlist;
2128  ListCell *l;
2129 
2130  subplan_itlist = build_tlist_index(subplan->targetlist);
2131 
2132  output_targetlist = NIL;
2133  foreach(l, plan->targetlist)
2134  {
2135  TargetEntry *tle = (TargetEntry *) lfirst(l);
2136  Node *newexpr;
2137 
2138  /* If it's a sort/group item, first try to match by sortref */
2139  if (tle->ressortgroupref != 0)
2140  {
2141  newexpr = (Node *)
2143  tle->ressortgroupref,
2144  subplan_itlist,
2145  OUTER_VAR);
2146  if (!newexpr)
2147  newexpr = fix_upper_expr(root,
2148  (Node *) tle->expr,
2149  subplan_itlist,
2150  OUTER_VAR,
2151  rtoffset,
2152  NUM_EXEC_TLIST(plan));
2153  }
2154  else
2155  newexpr = fix_upper_expr(root,
2156  (Node *) tle->expr,
2157  subplan_itlist,
2158  OUTER_VAR,
2159  rtoffset,
2160  NUM_EXEC_TLIST(plan));
2161  tle = flatCopyTargetEntry(tle);
2162  tle->expr = (Expr *) newexpr;
2163  output_targetlist = lappend(output_targetlist, tle);
2164  }
2165  plan->targetlist = output_targetlist;
2166 
2167  plan->qual = (List *)
2168  fix_upper_expr(root,
2169  (Node *) plan->qual,
2170  subplan_itlist,
2171  OUTER_VAR,
2172  rtoffset,
2173  NUM_EXEC_QUAL(plan));
2174 
2175  pfree(subplan_itlist);
2176 }
#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:2776
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:1454
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:2377
struct Plan * lefttree
Definition: plannodes.h:143
List * targetlist
Definition: plannodes.h:141
Index ressortgroupref
Definition: primnodes.h:1457
static Var * search_indexed_tlist_for_sortgroupref(Expr *node, Index sortgroupref, indexed_tlist *itlist, Index newvarno)
Definition: setrefs.c:2558
Definition: pg_list.h:50
#define OUTER_VAR
Definition: primnodes.h:176

◆ trivial_subqueryscan()

static bool trivial_subqueryscan ( SubqueryScan plan)
static

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

1211 {
1212  int attrno;
1213  ListCell *lp,
1214  *lc;
1215 
1216  if (plan->scan.plan.qual != NIL)
1217  return false;
1218 
1219  if (list_length(plan->scan.plan.targetlist) !=
1220  list_length(plan->subplan->targetlist))
1221  return false; /* tlists not same length */
1222 
1223  attrno = 1;
1224  forboth(lp, plan->scan.plan.targetlist, lc, plan->subplan->targetlist)
1225  {
1226  TargetEntry *ptle = (TargetEntry *) lfirst(lp);
1227  TargetEntry *ctle = (TargetEntry *) lfirst(lc);
1228 
1229  if (ptle->resjunk != ctle->resjunk)
1230  return false; /* tlist doesn't match junk status */
1231 
1232  /*
1233  * We accept either a Var referencing the corresponding element of the
1234  * subplan tlist, or a Const equaling the subplan element. See
1235  * generate_setop_tlist() for motivation.
1236  */
1237  if (ptle->expr && IsA(ptle->expr, Var))
1238  {
1239  Var *var = (Var *) ptle->expr;
1240 
1241  Assert(var->varno == plan->scan.scanrelid);
1242  Assert(var->varlevelsup == 0);
1243  if (var->varattno != attrno)
1244  return false; /* out of order */
1245  }
1246  else if (ptle->expr && IsA(ptle->expr, Const))
1247  {
1248  if (!equal(ptle->expr, ctle->expr))
1249  return false;
1250  }
1251  else
1252  return false;
1253 
1254  attrno++;
1255  }
1256 
1257  return true;
1258 }
#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:3122
AttrNumber varattno
Definition: primnodes.h:191
Definition: primnodes.h:186
bool resjunk
Definition: primnodes.h:1461
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:1454
static int list_length(const List *l)
Definition: pg_list.h:149
List * targetlist
Definition: plannodes.h:141
Plan * subplan
Definition: plannodes.h:527