PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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/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 ISREGCLASSCONST(con)
 
#define fix_scan_list(root, lst, rtoffset)   ((List *) fix_scan_expr(root, (Node *) (lst), rtoffset))
 

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 void set_foreignscan_references (PlannerInfo *root, ForeignScan *fscan, int rtoffset)
 
static void set_customscan_references (PlannerInfo *root, CustomScan *cscan, int rtoffset)
 
static Nodefix_scan_expr (PlannerInfo *root, Node *node, int rtoffset)
 
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 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)
 
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)
 
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)
 
static bool extract_query_dependencies_walker (Node *node, PlannerInfo *context)
 
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 indexed_tlistbuild_tlist_index_other_vars (List *tlist, Index ignore_rel)
 
void record_plan_function_dependency (PlannerInfo *root, Oid funcid)
 
void extract_query_dependencies (Node *query, List **relationOids, List **invalItems, bool *hasRowSecurity)
 

Macro Definition Documentation

#define fix_scan_list (   root,
  lst,
  rtoffset 
)    ((List *) fix_scan_expr(root, (Node *) (lst), rtoffset))
#define ISREGCLASSCONST (   con)
Value:
(((con)->consttype == REGCLASSOID || (con)->consttype == OIDOID) && \
!(con)->constisnull)
#define REGCLASSOID
Definition: pg_type.h:577
#define OIDOID
Definition: pg_type.h:328

Definition at line 77 of file setrefs.c.

Referenced by fix_expr_common().

Function Documentation

static void add_rte_to_flat_rtable ( PlannerGlobal glob,
RangeTblEntry rte 
)
static

Definition at line 383 of file setrefs.c.

References RangeTblEntry::colcollations, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, ereport, errcode(), errmsg(), ERROR, PlannerGlobal::finalrtable, RangeTblEntry::functions, IS_SPECIAL_VARNO, RangeTblEntry::joinaliasvars, lappend(), lappend_oid(), list_length(), NIL, NULL, 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().

384 {
385  RangeTblEntry *newrte;
386 
387  /* flat copy to duplicate all the scalar fields */
388  newrte = (RangeTblEntry *) palloc(sizeof(RangeTblEntry));
389  memcpy(newrte, rte, sizeof(RangeTblEntry));
390 
391  /* zap unneeded sub-structure */
392  newrte->tablesample = NULL;
393  newrte->subquery = NULL;
394  newrte->joinaliasvars = NIL;
395  newrte->functions = NIL;
396  newrte->tablefunc = NULL;
397  newrte->values_lists = NIL;
398  newrte->coltypes = NIL;
399  newrte->coltypmods = NIL;
400  newrte->colcollations = NIL;
401  newrte->securityQuals = NIL;
402 
403  glob->finalrtable = lappend(glob->finalrtable, newrte);
404 
405  /*
406  * Check for RT index overflow; it's very unlikely, but if it did happen,
407  * the executor would get confused by varnos that match the special varno
408  * values.
409  */
411  ereport(ERROR,
412  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
413  errmsg("too many range table entries")));
414 
415  /*
416  * If it's a plain relation RTE, add the table to relationOids.
417  *
418  * We do this even though the RTE might be unreferenced in the plan tree;
419  * this would correspond to cases such as views that were expanded, child
420  * tables that were eliminated by constraint exclusion, etc. Schema
421  * invalidation on such a rel must still force rebuilding of the plan.
422  *
423  * Note we don't bother to avoid making duplicate list entries. We could,
424  * but it would probably cost more cycles than it would save.
425  */
426  if (newrte->rtekind == RTE_RELATION)
427  glob->relationOids = lappend_oid(glob->relationOids, newrte->relid);
428 }
#define NIL
Definition: pg_list.h:69
List * joinaliasvars
Definition: parsenodes.h:988
List * securityQuals
Definition: parsenodes.h:1052
List * coltypmods
Definition: parsenodes.h:1030
int errcode(int sqlerrcode)
Definition: elog.c:575
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
List * colcollations
Definition: parsenodes.h:1031
List * values_lists
Definition: parsenodes.h:1009
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:157
#define ERROR
Definition: elog.h:43
TableFunc * tablefunc
Definition: parsenodes.h:1004
#define ereport(elevel, rest)
Definition: elog.h:122
List * lappend(List *list, void *datum)
Definition: list.c:128
#define NULL
Definition: c.h:229
List * functions
Definition: parsenodes.h:998
static int list_length(const List *l)
Definition: pg_list.h:89
List * relationOids
Definition: relation.h:113
RTEKind rtekind
Definition: parsenodes.h:944
List * finalrtable
Definition: relation.h:104
Query * subquery
Definition: parsenodes.h:967
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
List * coltypes
Definition: parsenodes.h:1029
struct TableSampleClause * tablesample
Definition: parsenodes.h:962
static void add_rtes_to_flat_rtable ( PlannerInfo root,
bool  recursing 
)
static

Definition at line 251 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, NULL, 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().

252 {
253  PlannerGlobal *glob = root->glob;
254  Index rti;
255  ListCell *lc;
256 
257  /*
258  * Add the query's own RTEs to the flattened rangetable.
259  *
260  * At top level, we must add all RTEs so that their indexes in the
261  * flattened rangetable match up with their original indexes. When
262  * recursing, we only care about extracting relation RTEs.
263  */
264  foreach(lc, root->parse->rtable)
265  {
266  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
267 
268  if (!recursing || rte->rtekind == RTE_RELATION)
269  add_rte_to_flat_rtable(glob, rte);
270  }
271 
272  /*
273  * If there are any dead subqueries, they are not referenced in the Plan
274  * tree, so we must add RTEs contained in them to the flattened rtable
275  * separately. (If we failed to do this, the executor would not perform
276  * expected permission checks for tables mentioned in such subqueries.)
277  *
278  * Note: this pass over the rangetable can't be combined with the previous
279  * one, because that would mess up the numbering of the live RTEs in the
280  * flattened rangetable.
281  */
282  rti = 1;
283  foreach(lc, root->parse->rtable)
284  {
285  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
286 
287  /*
288  * We should ignore inheritance-parent RTEs: their contents have been
289  * pulled up into our rangetable already. Also ignore any subquery
290  * RTEs without matching RelOptInfos, as they likewise have been
291  * pulled up.
292  */
293  if (rte->rtekind == RTE_SUBQUERY && !rte->inh &&
294  rti < root->simple_rel_array_size)
295  {
296  RelOptInfo *rel = root->simple_rel_array[rti];
297 
298  if (rel != NULL)
299  {
300  Assert(rel->relid == rti); /* sanity check on array */
301 
302  /*
303  * The subquery might never have been planned at all, if it
304  * was excluded on the basis of self-contradictory constraints
305  * in our query level. In this case apply
306  * flatten_unplanned_rtes.
307  *
308  * If it was planned but the result rel is dummy, we assume
309  * that it has been omitted from our plan tree (see
310  * set_subquery_pathlist), and recurse to pull up its RTEs.
311  *
312  * Otherwise, it should be represented by a SubqueryScan node
313  * somewhere in our plan tree, and we'll pull up its RTEs when
314  * we process that plan node.
315  *
316  * However, if we're recursing, then we should pull up RTEs
317  * whether the subquery is dummy or not, because we've found
318  * that some upper query level is treating this one as dummy,
319  * and so we won't scan this level's plan tree at all.
320  */
321  if (rel->subroot == NULL)
322  flatten_unplanned_rtes(glob, rte);
323  else if (recursing ||
325  UPPERREL_FINAL, NULL)))
326  add_rtes_to_flat_rtable(rel->subroot, true);
327  }
328  }
329  rti++;
330  }
331 }
Query * parse
Definition: relation.h:155
static void flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte)
Definition: setrefs.c:337
static void add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte)
Definition: setrefs.c:383
struct RelOptInfo ** simple_rel_array
Definition: relation.h:179
PlannerInfo * subroot
Definition: relation.h:567
List * rtable
Definition: parsenodes.h:135
#define IS_DUMMY_REL(r)
Definition: relation.h:1187
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:919
PlannerGlobal * glob
Definition: relation.h:157
Index relid
Definition: relation.h:553
unsigned int Index
Definition: c.h:365
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
static void add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing)
Definition: setrefs.c:251
RTEKind rtekind
Definition: parsenodes.h:944
static indexed_tlist * build_tlist_index ( List tlist)
static

Definition at line 1939 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_indexonlyscan_references(), set_join_references(), set_plan_refs(), and set_upper_references().

1940 {
1941  indexed_tlist *itlist;
1942  tlist_vinfo *vinfo;
1943  ListCell *l;
1944 
1945  /* Create data structure with enough slots for all tlist entries */
1946  itlist = (indexed_tlist *)
1948  list_length(tlist) * sizeof(tlist_vinfo));
1949 
1950  itlist->tlist = tlist;
1951  itlist->has_ph_vars = false;
1952  itlist->has_non_vars = false;
1953 
1954  /* Find the Vars and fill in the index array */
1955  vinfo = itlist->vars;
1956  foreach(l, tlist)
1957  {
1958  TargetEntry *tle = (TargetEntry *) lfirst(l);
1959 
1960  if (tle->expr && IsA(tle->expr, Var))
1961  {
1962  Var *var = (Var *) tle->expr;
1963 
1964  vinfo->varno = var->varno;
1965  vinfo->varattno = var->varattno;
1966  vinfo->resno = tle->resno;
1967  vinfo++;
1968  }
1969  else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
1970  itlist->has_ph_vars = true;
1971  else
1972  itlist->has_non_vars = true;
1973  }
1974 
1975  itlist->num_vars = (vinfo - itlist->vars);
1976 
1977  return itlist;
1978 }
Index varno
Definition: setrefs.c:33
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
tlist_vinfo vars[FLEXIBLE_ARRAY_MEMBER]
Definition: setrefs.c:44
AttrNumber resno
Definition: setrefs.c:35
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
AttrNumber varattno
Definition: setrefs.c:34
bool has_ph_vars
Definition: setrefs.c:42
int num_vars
Definition: setrefs.c:41
AttrNumber resno
Definition: primnodes.h:1369
Index varno
Definition: primnodes.h:166
bool has_non_vars
Definition: setrefs.c:43
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1368
static int list_length(const List *l)
Definition: pg_list.h:89
void * palloc(Size size)
Definition: mcxt.c:849
Definition: regcomp.c:224
#define offsetof(type, field)
Definition: c.h:555
List * tlist
Definition: setrefs.c:40
static indexed_tlist* build_tlist_index_other_vars ( List tlist,
Index  ignore_rel 
)
static

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

1990 {
1991  indexed_tlist *itlist;
1992  tlist_vinfo *vinfo;
1993  ListCell *l;
1994 
1995  /* Create data structure with enough slots for all tlist entries */
1996  itlist = (indexed_tlist *)
1998  list_length(tlist) * sizeof(tlist_vinfo));
1999 
2000  itlist->tlist = tlist;
2001  itlist->has_ph_vars = false;
2002  itlist->has_non_vars = false;
2003 
2004  /* Find the desired Vars and fill in the index array */
2005  vinfo = itlist->vars;
2006  foreach(l, tlist)
2007  {
2008  TargetEntry *tle = (TargetEntry *) lfirst(l);
2009 
2010  if (tle->expr && IsA(tle->expr, Var))
2011  {
2012  Var *var = (Var *) tle->expr;
2013 
2014  if (var->varno != ignore_rel)
2015  {
2016  vinfo->varno = var->varno;
2017  vinfo->varattno = var->varattno;
2018  vinfo->resno = tle->resno;
2019  vinfo++;
2020  }
2021  }
2022  else if (tle->expr && IsA(tle->expr, PlaceHolderVar))
2023  itlist->has_ph_vars = true;
2024  }
2025 
2026  itlist->num_vars = (vinfo - itlist->vars);
2027 
2028  return itlist;
2029 }
Index varno
Definition: setrefs.c:33
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
tlist_vinfo vars[FLEXIBLE_ARRAY_MEMBER]
Definition: setrefs.c:44
AttrNumber resno
Definition: setrefs.c:35
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
AttrNumber varattno
Definition: setrefs.c:34
bool has_ph_vars
Definition: setrefs.c:42
int num_vars
Definition: setrefs.c:41
AttrNumber resno
Definition: primnodes.h:1369
Index varno
Definition: primnodes.h:166
bool has_non_vars
Definition: setrefs.c:43
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1368
static int list_length(const List *l)
Definition: pg_list.h:89
void * palloc(Size size)
Definition: mcxt.c:849
Definition: regcomp.c:224
#define offsetof(type, field)
Definition: c.h:555
List * tlist
Definition: setrefs.c:40
static Node * convert_combining_aggrefs ( Node node,
void *  context 
)
static

Definition at line 1804 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(), NIL, and NULL.

Referenced by set_plan_refs().

1805 {
1806  if (node == NULL)
1807  return NULL;
1808  if (IsA(node, Aggref))
1809  {
1810  Aggref *orig_agg = (Aggref *) node;
1811  Aggref *child_agg;
1812  Aggref *parent_agg;
1813 
1814  /* Assert we've not chosen to partial-ize any unsupported cases */
1815  Assert(orig_agg->aggorder == NIL);
1816  Assert(orig_agg->aggdistinct == NIL);
1817 
1818  /*
1819  * Since aggregate calls can't be nested, we needn't recurse into the
1820  * arguments. But for safety, flat-copy the Aggref node itself rather
1821  * than modifying it in-place.
1822  */
1823  child_agg = makeNode(Aggref);
1824  memcpy(child_agg, orig_agg, sizeof(Aggref));
1825 
1826  /*
1827  * For the parent Aggref, we want to copy all the fields of the
1828  * original aggregate *except* the args list, which we'll replace
1829  * below, and the aggfilter expression, which should be applied only
1830  * by the child not the parent. Rather than explicitly knowing about
1831  * all the other fields here, we can momentarily modify child_agg to
1832  * provide a suitable source for copyObject.
1833  */
1834  child_agg->args = NIL;
1835  child_agg->aggfilter = NULL;
1836  parent_agg = copyObject(child_agg);
1837  child_agg->args = orig_agg->args;
1838  child_agg->aggfilter = orig_agg->aggfilter;
1839 
1840  /*
1841  * Now, set up child_agg to represent the first phase of partial
1842  * aggregation. For now, assume serialization is required.
1843  */
1845 
1846  /*
1847  * And set up parent_agg to represent the second phase.
1848  */
1849  parent_agg->args = list_make1(makeTargetEntry((Expr *) child_agg,
1850  1, NULL, false));
1852 
1853  return (Node *) parent_agg;
1854  }
1856  (void *) context);
1857 }
List * aggdistinct
Definition: primnodes.h:303
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2410
void mark_partial_aggref(Aggref *agg, AggSplit aggsplit)
Definition: planner.c:5227
Definition: nodes.h:509
List * args
Definition: primnodes.h:301
#define list_make1(x1)
Definition: pg_list.h:139
List * aggorder
Definition: primnodes.h:302
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:235
static Node * convert_combining_aggrefs(Node *node, void *context)
Definition: setrefs.c:1804
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
Expr * aggfilter
Definition: primnodes.h:304
#define copyObject(obj)
Definition: nodes.h:622
static Var* copyVar ( Var var)
inlinestatic

Definition at line 1334 of file setrefs.c.

References palloc().

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

1335 {
1336  Var *newvar = (Var *) palloc(sizeof(Var));
1337 
1338  *newvar = *var;
1339  return newvar;
1340 }
Definition: primnodes.h:163
void * palloc(Size size)
Definition: mcxt.c:849
void extract_query_dependencies ( Node query,
List **  relationOids,
List **  invalItems,
bool hasRowSecurity 
)

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

2538 {
2539  PlannerGlobal glob;
2540  PlannerInfo root;
2541 
2542  /* Make up dummy planner state so we can use this module's machinery */
2543  MemSet(&glob, 0, sizeof(glob));
2544  glob.type = T_PlannerGlobal;
2545  glob.relationOids = NIL;
2546  glob.invalItems = NIL;
2547  /* Hack: we use glob.dependsOnRole to collect hasRowSecurity flags */
2548  glob.dependsOnRole = false;
2549 
2550  MemSet(&root, 0, sizeof(root));
2551  root.type = T_PlannerInfo;
2552  root.glob = &glob;
2553 
2554  (void) extract_query_dependencies_walker(query, &root);
2555 
2556  *relationOids = glob.relationOids;
2557  *invalItems = glob.invalItems;
2558  *hasRowSecurity = glob.dependsOnRole;
2559 }
#define NIL
Definition: pg_list.h:69
#define MemSet(start, val, len)
Definition: c.h:857
bool dependsOnRole
Definition: relation.h:127
PlannerGlobal * glob
Definition: relation.h:157
List * invalItems
Definition: relation.h:115
NodeTag type
Definition: relation.h:153
static bool extract_query_dependencies_walker(Node *node, PlannerInfo *context)
Definition: setrefs.c:2562
NodeTag type
Definition: relation.h:94
List * relationOids
Definition: relation.h:113
static bool extract_query_dependencies_walker ( Node node,
PlannerInfo context 
)
static

Definition at line 2562 of file setrefs.c.

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

Referenced by extract_query_dependencies().

2563 {
2564  if (node == NULL)
2565  return false;
2566  Assert(!IsA(node, PlaceHolderVar));
2567  /* Extract function dependencies and check for regclass Consts */
2568  fix_expr_common(context, node);
2569  if (IsA(node, Query))
2570  {
2571  Query *query = (Query *) node;
2572  ListCell *lc;
2573 
2574  if (query->commandType == CMD_UTILITY)
2575  {
2576  /*
2577  * Ignore utility statements, except those (such as EXPLAIN) that
2578  * contain a parsed-but-not-planned query.
2579  */
2580  query = UtilityContainsQuery(query->utilityStmt);
2581  if (query == NULL)
2582  return false;
2583  }
2584 
2585  /* Remember if any Query has RLS quals applied by rewriter */
2586  if (query->hasRowSecurity)
2587  context->glob->dependsOnRole = true;
2588 
2589  /* Collect relation OIDs in this Query's rtable */
2590  foreach(lc, query->rtable)
2591  {
2592  RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
2593 
2594  if (rte->rtekind == RTE_RELATION)
2595  context->glob->relationOids =
2596  lappend_oid(context->glob->relationOids, rte->relid);
2597  else if (rte->rtekind == RTE_NAMEDTUPLESTORE &&
2598  OidIsValid(rte->relid))
2599  context->glob->relationOids =
2600  lappend_oid(context->glob->relationOids,
2601  rte->relid);
2602  }
2603 
2604  /* And recurse into the query's subexpressions */
2606  (void *) context, 0);
2607  }
2609  (void *) context);
2610 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2246
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Node * utilityStmt
Definition: parsenodes.h:118
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
#define OidIsValid(objectId)
Definition: c.h:538
Query * UtilityContainsQuery(Node *parsetree)
Definition: utility.c:1856
bool dependsOnRole
Definition: relation.h:127
List * rtable
Definition: parsenodes.h:135
PlannerGlobal * glob
Definition: relation.h:157
static bool extract_query_dependencies_walker(Node *node, PlannerInfo *context)
Definition: setrefs.c:2562
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1356
CmdType commandType
Definition: parsenodes.h:110
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1843
List * relationOids
Definition: relation.h:113
RTEKind rtekind
Definition: parsenodes.h:944
bool hasRowSecurity
Definition: parsenodes.h:131
static void fix_expr_common ( PlannerInfo root,
Node node 
)
static

Definition at line 1356 of file setrefs.c.

References Assert, GroupingFunc::cols, Const::constvalue, DatumGetObjectId, equal(), PlannerInfo::glob, PlannerInfo::grouping_map, IsA, ISREGCLASSCONST, lappend_int(), lappend_oid(), lfirst_int, NIL, OidIsValid, 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().

1357 {
1358  /* We assume callers won't call us on a NULL pointer */
1359  if (IsA(node, Aggref))
1360  {
1362  ((Aggref *) node)->aggfnoid);
1363  }
1364  else if (IsA(node, WindowFunc))
1365  {
1367  ((WindowFunc *) node)->winfnoid);
1368  }
1369  else if (IsA(node, FuncExpr))
1370  {
1372  ((FuncExpr *) node)->funcid);
1373  }
1374  else if (IsA(node, OpExpr))
1375  {
1376  set_opfuncid((OpExpr *) node);
1378  ((OpExpr *) node)->opfuncid);
1379  }
1380  else if (IsA(node, DistinctExpr))
1381  {
1382  set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1384  ((DistinctExpr *) node)->opfuncid);
1385  }
1386  else if (IsA(node, NullIfExpr))
1387  {
1388  set_opfuncid((OpExpr *) node); /* rely on struct equivalence */
1390  ((NullIfExpr *) node)->opfuncid);
1391  }
1392  else if (IsA(node, ScalarArrayOpExpr))
1393  {
1396  ((ScalarArrayOpExpr *) node)->opfuncid);
1397  }
1398  else if (IsA(node, ArrayCoerceExpr))
1399  {
1400  if (OidIsValid(((ArrayCoerceExpr *) node)->elemfuncid))
1402  ((ArrayCoerceExpr *) node)->elemfuncid);
1403  }
1404  else if (IsA(node, Const))
1405  {
1406  Const *con = (Const *) node;
1407 
1408  /* Check for regclass reference */
1409  if (ISREGCLASSCONST(con))
1410  root->glob->relationOids =
1411  lappend_oid(root->glob->relationOids,
1413  }
1414  else if (IsA(node, GroupingFunc))
1415  {
1416  GroupingFunc *g = (GroupingFunc *) node;
1417  AttrNumber *grouping_map = root->grouping_map;
1418 
1419  /* If there are no grouping sets, we don't need this. */
1420 
1421  Assert(grouping_map || g->cols == NIL);
1422 
1423  if (grouping_map)
1424  {
1425  ListCell *lc;
1426  List *cols = NIL;
1427 
1428  foreach(lc, g->refs)
1429  {
1430  cols = lappend_int(cols, grouping_map[lfirst_int(lc)]);
1431  }
1432 
1433  Assert(!g->cols || equal(cols, g->cols));
1434 
1435  if (!g->cols)
1436  g->cols = cols;
1437  }
1438  }
1439 }
Datum constvalue
Definition: primnodes.h:196
#define NIL
Definition: pg_list.h:69
#define ISREGCLASSCONST(con)
Definition: setrefs.c:77
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
List * refs
Definition: primnodes.h:343
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2962
#define DatumGetObjectId(X)
Definition: postgres.h:506
AttrNumber * grouping_map
Definition: relation.h:284
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
#define OidIsValid(objectId)
Definition: c.h:538
#define lfirst_int(lc)
Definition: pg_list.h:107
PlannerGlobal * glob
Definition: relation.h:157
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * cols
Definition: primnodes.h:344
void record_plan_function_dependency(PlannerInfo *root, Oid funcid)
Definition: setrefs.c:2496
#define Assert(condition)
Definition: c.h:675
List * relationOids
Definition: relation.h:113
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1613
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
void set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
Definition: nodeFuncs.c:1624
static List * fix_join_expr ( PlannerInfo root,
List clauses,
indexed_tlist outer_itlist,
indexed_tlist inner_itlist,
Index  acceptable_rel,
int  rtoffset 
)
static

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

2191 {
2192  fix_join_expr_context context;
2193 
2194  context.root = root;
2195  context.outer_itlist = outer_itlist;
2196  context.inner_itlist = inner_itlist;
2197  context.acceptable_rel = acceptable_rel;
2198  context.rtoffset = rtoffset;
2199  return (List *) fix_join_expr_mutator((Node *) clauses, &context);
2200 }
indexed_tlist * inner_itlist
Definition: setrefs.c:57
Definition: nodes.h:509
static Node * fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
Definition: setrefs.c:2203
PlannerInfo * root
Definition: setrefs.c:55
indexed_tlist * outer_itlist
Definition: setrefs.c:56
Definition: pg_list.h:45
static Node * fix_join_expr_mutator ( Node node,
fix_join_expr_context context 
)
static

Definition at line 2203 of file setrefs.c.

References fix_join_expr_context::acceptable_rel, copyVar(), elog, ERROR, expression_tree_mutator(), 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, NULL, 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::varnoold.

Referenced by fix_join_expr().

2204 {
2205  Var *newvar;
2206 
2207  if (node == NULL)
2208  return NULL;
2209  if (IsA(node, Var))
2210  {
2211  Var *var = (Var *) node;
2212 
2213  /* Look for the var in the input tlists, first in the outer */
2214  if (context->outer_itlist)
2215  {
2216  newvar = search_indexed_tlist_for_var(var,
2217  context->outer_itlist,
2218  OUTER_VAR,
2219  context->rtoffset);
2220  if (newvar)
2221  return (Node *) newvar;
2222  }
2223 
2224  /* then in the inner. */
2225  if (context->inner_itlist)
2226  {
2227  newvar = search_indexed_tlist_for_var(var,
2228  context->inner_itlist,
2229  INNER_VAR,
2230  context->rtoffset);
2231  if (newvar)
2232  return (Node *) newvar;
2233  }
2234 
2235  /* If it's for acceptable_rel, adjust and return it */
2236  if (var->varno == context->acceptable_rel)
2237  {
2238  var = copyVar(var);
2239  var->varno += context->rtoffset;
2240  if (var->varnoold > 0)
2241  var->varnoold += context->rtoffset;
2242  return (Node *) var;
2243  }
2244 
2245  /* No referent found for Var */
2246  elog(ERROR, "variable not found in subplan target lists");
2247  }
2248  if (IsA(node, PlaceHolderVar))
2249  {
2250  PlaceHolderVar *phv = (PlaceHolderVar *) node;
2251 
2252  /* See if the PlaceHolderVar has bubbled up from a lower plan node */
2253  if (context->outer_itlist && context->outer_itlist->has_ph_vars)
2254  {
2255  newvar = search_indexed_tlist_for_non_var((Expr *) phv,
2256  context->outer_itlist,
2257  OUTER_VAR);
2258  if (newvar)
2259  return (Node *) newvar;
2260  }
2261  if (context->inner_itlist && context->inner_itlist->has_ph_vars)
2262  {
2263  newvar = search_indexed_tlist_for_non_var((Expr *) phv,
2264  context->inner_itlist,
2265  INNER_VAR);
2266  if (newvar)
2267  return (Node *) newvar;
2268  }
2269 
2270  /* If not supplied by input plans, evaluate the contained expr */
2271  return fix_join_expr_mutator((Node *) phv->phexpr, context);
2272  }
2273  if (IsA(node, Param))
2274  return fix_param_node(context->root, (Param *) node);
2275  /* Try matching more complex expressions too, if tlists have any */
2276  if (context->outer_itlist && context->outer_itlist->has_non_vars)
2277  {
2278  newvar = search_indexed_tlist_for_non_var((Expr *) node,
2279  context->outer_itlist,
2280  OUTER_VAR);
2281  if (newvar)
2282  return (Node *) newvar;
2283  }
2284  if (context->inner_itlist && context->inner_itlist->has_non_vars)
2285  {
2286  newvar = search_indexed_tlist_for_non_var((Expr *) node,
2287  context->inner_itlist,
2288  INNER_VAR);
2289  if (newvar)
2290  return (Node *) newvar;
2291  }
2292  fix_expr_common(context->root, node);
2293  return expression_tree_mutator(node,
2295  (void *) context);
2296 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
indexed_tlist * inner_itlist
Definition: setrefs.c:57
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2410
static Node * fix_param_node(PlannerInfo *root, Param *p)
Definition: setrefs.c:1450
Definition: nodes.h:509
Definition: primnodes.h:163
static Node * fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
Definition: setrefs.c:2203
Index varnoold
Definition: primnodes.h:176
bool has_ph_vars
Definition: setrefs.c:42
#define ERROR
Definition: elog.h:43
Expr * phexpr
Definition: relation.h:1850
static Var * copyVar(Var *var)
Definition: setrefs.c:1334
PlannerInfo * root
Definition: setrefs.c:55
Index varno
Definition: primnodes.h:166
bool has_non_vars
Definition: setrefs.c:43
static Var * search_indexed_tlist_for_non_var(Expr *node, indexed_tlist *itlist, Index newvarno)
Definition: setrefs.c:2080
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1356
#define INNER_VAR
Definition: primnodes.h:153
#define NULL
Definition: c.h:229
static Var * search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist, Index newvarno, int rtoffset)
Definition: setrefs.c:2040
indexed_tlist * outer_itlist
Definition: setrefs.c:56
#define elog
Definition: elog.h:219
#define OUTER_VAR
Definition: primnodes.h:154
static Node* fix_param_node ( PlannerInfo root,
Param p 
)
static

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

1451 {
1452  if (p->paramkind == PARAM_MULTIEXPR)
1453  {
1454  int subqueryid = p->paramid >> 16;
1455  int colno = p->paramid & 0xFFFF;
1456  List *params;
1457 
1458  if (subqueryid <= 0 ||
1459  subqueryid > list_length(root->multiexpr_params))
1460  elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
1461  params = (List *) list_nth(root->multiexpr_params, subqueryid - 1);
1462  if (colno <= 0 || colno > list_length(params))
1463  elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
1464  return copyObject(list_nth(params, colno - 1));
1465  }
1466  return (Node *) copyObject(p);
1467 }
ParamKind paramkind
Definition: primnodes.h:244
Definition: nodes.h:509
List * multiexpr_params
Definition: relation.h:232
#define ERROR
Definition: elog.h:43
void * list_nth(const List *list, int n)
Definition: list.c:410
int paramid
Definition: primnodes.h:245
static int list_length(const List *l)
Definition: pg_list.h:89
#define elog
Definition: elog.h:219
#define copyObject(obj)
Definition: nodes.h:622
Definition: pg_list.h:45
static Node * fix_scan_expr ( PlannerInfo root,
Node node,
int  rtoffset 
)
static

Definition at line 1480 of file setrefs.c.

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

Referenced by set_plan_refs().

1481 {
1482  fix_scan_expr_context context;
1483 
1484  context.root = root;
1485  context.rtoffset = rtoffset;
1486 
1487  if (rtoffset != 0 ||
1488  root->multiexpr_params != NIL ||
1489  root->glob->lastPHId != 0 ||
1490  root->minmax_aggs != NIL)
1491  {
1492  return fix_scan_expr_mutator(node, &context);
1493  }
1494  else
1495  {
1496  /*
1497  * If rtoffset == 0, we don't need to change any Vars, and if there
1498  * are no MULTIEXPR subqueries then we don't need to replace
1499  * PARAM_MULTIEXPR Params, and if there are no placeholders anywhere
1500  * we won't need to remove them, and if there are no minmax Aggrefs we
1501  * won't need to replace them. Then it's OK to just scribble on the
1502  * input node tree instead of copying (since the only change, filling
1503  * in any unset opfuncid fields, is harmless). This saves just enough
1504  * cycles to be noticeable on trivial queries.
1505  */
1506  (void) fix_scan_expr_walker(node, &context);
1507  return node;
1508  }
1509 }
#define NIL
Definition: pg_list.h:69
PlannerInfo * root
Definition: setrefs.c:49
List * minmax_aggs
Definition: relation.h:285
List * multiexpr_params
Definition: relation.h:232
PlannerGlobal * glob
Definition: relation.h:157
static bool fix_scan_expr_walker(Node *node, fix_scan_expr_context *context)
Definition: setrefs.c:1581
static Node * fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
Definition: setrefs.c:1512
Index lastPHId
Definition: relation.h:119
static Node * fix_scan_expr_mutator ( Node node,
fix_scan_expr_context context 
)
static

Definition at line 1512 of file setrefs.c.

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

Referenced by fix_scan_expr().

1513 {
1514  if (node == NULL)
1515  return NULL;
1516  if (IsA(node, Var))
1517  {
1518  Var *var = copyVar((Var *) node);
1519 
1520  Assert(var->varlevelsup == 0);
1521 
1522  /*
1523  * We should not see any Vars marked INNER_VAR or OUTER_VAR. But an
1524  * indexqual expression could contain INDEX_VAR Vars.
1525  */
1526  Assert(var->varno != INNER_VAR);
1527  Assert(var->varno != OUTER_VAR);
1528  if (!IS_SPECIAL_VARNO(var->varno))
1529  var->varno += context->rtoffset;
1530  if (var->varnoold > 0)
1531  var->varnoold += context->rtoffset;
1532  return (Node *) var;
1533  }
1534  if (IsA(node, Param))
1535  return fix_param_node(context->root, (Param *) node);
1536  if (IsA(node, Aggref))
1537  {
1538  Aggref *aggref = (Aggref *) node;
1539 
1540  /* See if the Aggref should be replaced by a Param */
1541  if (context->root->minmax_aggs != NIL &&
1542  list_length(aggref->args) == 1)
1543  {
1544  TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
1545  ListCell *lc;
1546 
1547  foreach(lc, context->root->minmax_aggs)
1548  {
1549  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
1550 
1551  if (mminfo->aggfnoid == aggref->aggfnoid &&
1552  equal(mminfo->target, curTarget->expr))
1553  return (Node *) copyObject(mminfo->param);
1554  }
1555  }
1556  /* If no match, just fall through to process it normally */
1557  }
1558  if (IsA(node, CurrentOfExpr))
1559  {
1560  CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
1561 
1562  Assert(cexpr->cvarno != INNER_VAR);
1563  Assert(cexpr->cvarno != OUTER_VAR);
1564  if (!IS_SPECIAL_VARNO(cexpr->cvarno))
1565  cexpr->cvarno += context->rtoffset;
1566  return (Node *) cexpr;
1567  }
1568  if (IsA(node, PlaceHolderVar))
1569  {
1570  /* At scan level, we should always just evaluate the contained expr */
1571  PlaceHolderVar *phv = (PlaceHolderVar *) node;
1572 
1573  return fix_scan_expr_mutator((Node *) phv->phexpr, context);
1574  }
1575  fix_expr_common(context->root, node);
1577  (void *) context);
1578 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Index varlevelsup
Definition: primnodes.h:173
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2410
static Node * fix_param_node(PlannerInfo *root, Param *p)
Definition: setrefs.c:1450
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2962
Param * param
Definition: relation.h:2085
PlannerInfo * root
Definition: setrefs.c:49
Definition: nodes.h:509
List * args
Definition: primnodes.h:301
List * minmax_aggs
Definition: relation.h:285
Definition: primnodes.h:163
Index varnoold
Definition: primnodes.h:176
#define IS_SPECIAL_VARNO(varno)
Definition: primnodes.h:157
#define linitial(l)
Definition: pg_list.h:111
Expr * phexpr
Definition: relation.h:1850
static Var * copyVar(Var *var)
Definition: setrefs.c:1334
Index varno
Definition: primnodes.h:166
static Node * fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
Definition: setrefs.c:1512
Oid aggfnoid
Definition: primnodes.h:294
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1356
#define INNER_VAR
Definition: primnodes.h:153
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Expr * target
Definition: relation.h:2081
Expr * expr
Definition: primnodes.h:1368
static int list_length(const List *l)
Definition: pg_list.h:89
#define copyObject(obj)
Definition: nodes.h:622
#define OUTER_VAR
Definition: primnodes.h:154
static bool fix_scan_expr_walker ( Node node,
fix_scan_expr_context context 
)
static

Definition at line 1581 of file setrefs.c.

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

Referenced by fix_scan_expr().

1582 {
1583  if (node == NULL)
1584  return false;
1585  Assert(!IsA(node, PlaceHolderVar));
1586  fix_expr_common(context->root, node);
1588  (void *) context);
1589 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
PlannerInfo * root
Definition: setrefs.c:49
static bool fix_scan_expr_walker(Node *node, fix_scan_expr_context *context)
Definition: setrefs.c:1581
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1356
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1843
static Node * fix_upper_expr ( PlannerInfo root,
Node node,
indexed_tlist subplan_itlist,
Index  newvarno,
int  rtoffset 
)
static

Definition at line 2329 of file setrefs.c.

References fix_upper_expr_mutator(), fix_upper_expr_context::newvarno, 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_indexonlyscan_references(), set_join_references(), and set_upper_references().

2334 {
2335  fix_upper_expr_context context;
2336 
2337  context.root = root;
2338  context.subplan_itlist = subplan_itlist;
2339  context.newvarno = newvarno;
2340  context.rtoffset = rtoffset;
2341  return fix_upper_expr_mutator(node, &context);
2342 }
static Node * fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
Definition: setrefs.c:2345
PlannerInfo * root
Definition: setrefs.c:64
indexed_tlist * subplan_itlist
Definition: setrefs.c:65
static Node * fix_upper_expr_mutator ( Node node,
fix_upper_expr_context context 
)
static

Definition at line 2345 of file setrefs.c.

References Aggref::aggfnoid, MinMaxAggInfo::aggfnoid, Aggref::args, copyObject, elog, equal(), ERROR, TargetEntry::expr, expression_tree_mutator(), 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, NULL, 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().

2346 {
2347  Var *newvar;
2348 
2349  if (node == NULL)
2350  return NULL;
2351  if (IsA(node, Var))
2352  {
2353  Var *var = (Var *) node;
2354 
2355  newvar = search_indexed_tlist_for_var(var,
2356  context->subplan_itlist,
2357  context->newvarno,
2358  context->rtoffset);
2359  if (!newvar)
2360  elog(ERROR, "variable not found in subplan target list");
2361  return (Node *) newvar;
2362  }
2363  if (IsA(node, PlaceHolderVar))
2364  {
2365  PlaceHolderVar *phv = (PlaceHolderVar *) node;
2366 
2367  /* See if the PlaceHolderVar has bubbled up from a lower plan node */
2368  if (context->subplan_itlist->has_ph_vars)
2369  {
2370  newvar = search_indexed_tlist_for_non_var((Expr *) phv,
2371  context->subplan_itlist,
2372  context->newvarno);
2373  if (newvar)
2374  return (Node *) newvar;
2375  }
2376  /* If not supplied by input plan, evaluate the contained expr */
2377  return fix_upper_expr_mutator((Node *) phv->phexpr, context);
2378  }
2379  if (IsA(node, Param))
2380  return fix_param_node(context->root, (Param *) node);
2381  if (IsA(node, Aggref))
2382  {
2383  Aggref *aggref = (Aggref *) node;
2384 
2385  /* See if the Aggref should be replaced by a Param */
2386  if (context->root->minmax_aggs != NIL &&
2387  list_length(aggref->args) == 1)
2388  {
2389  TargetEntry *curTarget = (TargetEntry *) linitial(aggref->args);
2390  ListCell *lc;
2391 
2392  foreach(lc, context->root->minmax_aggs)
2393  {
2394  MinMaxAggInfo *mminfo = (MinMaxAggInfo *) lfirst(lc);
2395 
2396  if (mminfo->aggfnoid == aggref->aggfnoid &&
2397  equal(mminfo->target, curTarget->expr))
2398  return (Node *) copyObject(mminfo->param);
2399  }
2400  }
2401  /* If no match, just fall through to process it normally */
2402  }
2403  /* Try matching more complex expressions too, if tlist has any */
2404  if (context->subplan_itlist->has_non_vars)
2405  {
2406  newvar = search_indexed_tlist_for_non_var((Expr *) node,
2407  context->subplan_itlist,
2408  context->newvarno);
2409  if (newvar)
2410  return (Node *) newvar;
2411  }
2412  fix_expr_common(context->root, node);
2413  return expression_tree_mutator(node,
2415  (void *) context);
2416 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2410
static Node * fix_param_node(PlannerInfo *root, Param *p)
Definition: setrefs.c:1450
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2962
Param * param
Definition: relation.h:2085
Definition: nodes.h:509
List * args
Definition: primnodes.h:301
List * minmax_aggs
Definition: relation.h:285
Definition: primnodes.h:163
bool has_ph_vars
Definition: setrefs.c:42
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
Expr * phexpr
Definition: relation.h:1850
static Node * fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
Definition: setrefs.c:2345
bool has_non_vars
Definition: setrefs.c:43
static Var * search_indexed_tlist_for_non_var(Expr *node, indexed_tlist *itlist, Index newvarno)
Definition: setrefs.c:2080
Oid aggfnoid
Definition: primnodes.h:294
static void fix_expr_common(PlannerInfo *root, Node *node)
Definition: setrefs.c:1356
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Expr * target
Definition: relation.h:2081
static Var * search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist, Index newvarno, int rtoffset)
Definition: setrefs.c:2040
Expr * expr
Definition: primnodes.h:1368
static int list_length(const List *l)
Definition: pg_list.h:89
PlannerInfo * root
Definition: setrefs.c:64
#define elog
Definition: elog.h:219
#define copyObject(obj)
Definition: nodes.h:622
indexed_tlist * subplan_itlist
Definition: setrefs.c:65
static bool flatten_rtes_walker ( Node node,
PlannerGlobal glob 
)
static

Definition at line 347 of file setrefs.c.

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

Referenced by flatten_unplanned_rtes().

348 {
349  if (node == NULL)
350  return false;
351  if (IsA(node, RangeTblEntry))
352  {
353  RangeTblEntry *rte = (RangeTblEntry *) node;
354 
355  /* As above, we need only save relation RTEs */
356  if (rte->rtekind == RTE_RELATION)
357  add_rte_to_flat_rtable(glob, rte);
358  return false;
359  }
360  if (IsA(node, Query))
361  {
362  /* Recurse into subselects */
363  return query_tree_walker((Query *) node,
365  (void *) glob,
367  }
369  (void *) glob);
370 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2246
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
static void add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte)
Definition: setrefs.c:383
#define QTW_EXAMINE_RTES
Definition: nodeFuncs.h:25
static bool flatten_rtes_walker(Node *node, PlannerGlobal *glob)
Definition: setrefs.c:347
#define NULL
Definition: c.h:229
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1843
RTEKind rtekind
Definition: parsenodes.h:944
static void flatten_unplanned_rtes ( PlannerGlobal glob,
RangeTblEntry rte 
)
static

Definition at line 337 of file setrefs.c.

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

Referenced by add_rtes_to_flat_rtable().

338 {
339  /* Use query_tree_walker to find all RTEs in the parse tree */
340  (void) query_tree_walker(rte->subquery,
342  (void *) glob,
344 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2246
#define QTW_EXAMINE_RTES
Definition: nodeFuncs.h:25
static bool flatten_rtes_walker(Node *node, PlannerGlobal *glob)
Definition: setrefs.c:347
Query * subquery
Definition: parsenodes.h:967
void record_plan_function_dependency ( PlannerInfo root,
Oid  funcid 
)

Definition at line 2496 of file setrefs.c.

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

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

2497 {
2498  /*
2499  * For performance reasons, we don't bother to track built-in functions;
2500  * we just assume they'll never change (or at least not in ways that'd
2501  * invalidate plans using them). For this purpose we can consider a
2502  * built-in function to be one with OID less than FirstBootstrapObjectId.
2503  * Note that the OID generator guarantees never to generate such an OID
2504  * after startup, even at OID wraparound.
2505  */
2506  if (funcid >= (Oid) FirstBootstrapObjectId)
2507  {
2508  PlanInvalItem *inval_item = makeNode(PlanInvalItem);
2509 
2510  /*
2511  * It would work to use any syscache on pg_proc, but the easiest is
2512  * PROCOID since we already have the function's OID at hand. Note
2513  * that plancache.c knows we use PROCOID.
2514  */
2515  inval_item->cacheId = PROCOID;
2516  inval_item->hashValue = GetSysCacheHashValue1(PROCOID,
2517  ObjectIdGetDatum(funcid));
2518 
2519  root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
2520  }
2521 }
#define GetSysCacheHashValue1(cacheId, key1)
Definition: syscache.h:192
unsigned int Oid
Definition: postgres_ext.h:31
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
PlannerGlobal * glob
Definition: relation.h:157
#define FirstBootstrapObjectId
Definition: transam.h:93
List * lappend(List *list, void *datum)
Definition: list.c:128
List * invalItems
Definition: relation.h:115
uint32 hashValue
Definition: plannodes.h:1028
#define makeNode(_type_)
Definition: nodes.h:557
static Var * search_indexed_tlist_for_non_var ( Expr node,
indexed_tlist itlist,
Index  newvarno 
)
static

Definition at line 2080 of file setrefs.c.

References IsA, makeVarFromTargetEntry(), NULL, indexed_tlist::tlist, tlist_member(), Var::varnoold, and Var::varoattno.

Referenced by fix_join_expr_mutator(), and fix_upper_expr_mutator().

2082 {
2083  TargetEntry *tle;
2084 
2085  /*
2086  * If it's a simple Const, replacing it with a Var is silly, even if there
2087  * happens to be an identical Const below; a Var is more expensive to
2088  * execute than a Const. What's more, replacing it could confuse some
2089  * places in the executor that expect to see simple Consts for, eg,
2090  * dropped columns.
2091  */
2092  if (IsA(node, Const))
2093  return NULL;
2094 
2095  tle = tlist_member(node, itlist->tlist);
2096  if (tle)
2097  {
2098  /* Found a matching subplan output expression */
2099  Var *newvar;
2100 
2101  newvar = makeVarFromTargetEntry(newvarno, tle);
2102  newvar->varnoold = 0; /* wasn't ever a plain Var */
2103  newvar->varoattno = 0;
2104  return newvar;
2105  }
2106  return NULL; /* no match */
2107 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Var * makeVarFromTargetEntry(Index varno, TargetEntry *tle)
Definition: makefuncs.c:104
Definition: primnodes.h:163
Index varnoold
Definition: primnodes.h:176
TargetEntry * tlist_member(Expr *node, List *targetlist)
Definition: tlist.c:54
#define NULL
Definition: c.h:229
AttrNumber varoattno
Definition: primnodes.h:177
List * tlist
Definition: setrefs.c:40
static Var * search_indexed_tlist_for_sortgroupref ( Expr node,
Index  sortgroupref,
indexed_tlist itlist,
Index  newvarno 
)
static

Definition at line 2121 of file setrefs.c.

References equal(), TargetEntry::expr, lfirst, makeVarFromTargetEntry(), NULL, TargetEntry::ressortgroupref, indexed_tlist::tlist, Var::varnoold, and Var::varoattno.

Referenced by set_upper_references().

2125 {
2126  ListCell *lc;
2127 
2128  foreach(lc, itlist->tlist)
2129  {
2130  TargetEntry *tle = (TargetEntry *) lfirst(lc);
2131 
2132  /* The equal() check should be redundant, but let's be paranoid */
2133  if (tle->ressortgroupref == sortgroupref &&
2134  equal(node, tle->expr))
2135  {
2136  /* Found a matching subplan output expression */
2137  Var *newvar;
2138 
2139  newvar = makeVarFromTargetEntry(newvarno, tle);
2140  newvar->varnoold = 0; /* wasn't ever a plain Var */
2141  newvar->varoattno = 0;
2142  return newvar;
2143  }
2144  }
2145  return NULL; /* no match */
2146 }
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2962
Var * makeVarFromTargetEntry(Index varno, TargetEntry *tle)
Definition: makefuncs.c:104
Definition: primnodes.h:163
Index varnoold
Definition: primnodes.h:176
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1368
AttrNumber varoattno
Definition: primnodes.h:177
Index ressortgroupref
Definition: primnodes.h:1371
List * tlist
Definition: setrefs.c:40
static Var * search_indexed_tlist_for_var ( Var var,
indexed_tlist itlist,
Index  newvarno,
int  rtoffset 
)
static

Definition at line 2040 of file setrefs.c.

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

Referenced by fix_join_expr_mutator(), and fix_upper_expr_mutator().

2042 {
2043  Index varno = var->varno;
2044  AttrNumber varattno = var->varattno;
2045  tlist_vinfo *vinfo;
2046  int i;
2047 
2048  vinfo = itlist->vars;
2049  i = itlist->num_vars;
2050  while (i-- > 0)
2051  {
2052  if (vinfo->varno == varno && vinfo->varattno == varattno)
2053  {
2054  /* Found a match */
2055  Var *newvar = copyVar(var);
2056 
2057  newvar->varno = newvarno;
2058  newvar->varattno = vinfo->resno;
2059  if (newvar->varnoold > 0)
2060  newvar->varnoold += rtoffset;
2061  return newvar;
2062  }
2063  vinfo++;
2064  }
2065  return NULL; /* no match */
2066 }
Index varno
Definition: setrefs.c:33
tlist_vinfo vars[FLEXIBLE_ARRAY_MEMBER]
Definition: setrefs.c:44
AttrNumber resno
Definition: setrefs.c:35
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
AttrNumber varattno
Definition: setrefs.c:34
Index varnoold
Definition: primnodes.h:176
int num_vars
Definition: setrefs.c:41
static Var * copyVar(Var *var)
Definition: setrefs.c:1334
Index varno
Definition: primnodes.h:166
unsigned int Index
Definition: c.h:365
#define NULL
Definition: c.h:229
int i
int16 AttrNumber
Definition: attnum.h:21
static void set_customscan_references ( PlannerInfo root,
CustomScan cscan,
int  rtoffset 
)
static

Definition at line 1259 of file setrefs.c.

References bms_add_member(), bms_next_member(), build_tlist_index(), CustomScan::custom_exprs, CustomScan::custom_plans, CustomScan::custom_relids, CustomScan::custom_scan_tlist, fix_scan_list, fix_upper_expr(), INDEX_VAR, lfirst, NIL, NULL, pfree(), Scan::plan, Plan::qual, CustomScan::scan, Scan::scanrelid, set_plan_refs(), and Plan::targetlist.

Referenced by set_plan_refs().

1262 {
1263  ListCell *lc;
1264 
1265  /* Adjust scanrelid if it's valid */
1266  if (cscan->scan.scanrelid > 0)
1267  cscan->scan.scanrelid += rtoffset;
1268 
1269  if (cscan->custom_scan_tlist != NIL || cscan->scan.scanrelid == 0)
1270  {
1271  /* Adjust tlist, qual, custom_exprs to reference custom scan tuple */
1273 
1274  cscan->scan.plan.targetlist = (List *)
1275  fix_upper_expr(root,
1276  (Node *) cscan->scan.plan.targetlist,
1277  itlist,
1278  INDEX_VAR,
1279  rtoffset);
1280  cscan->scan.plan.qual = (List *)
1281  fix_upper_expr(root,
1282  (Node *) cscan->scan.plan.qual,
1283  itlist,
1284  INDEX_VAR,
1285  rtoffset);
1286  cscan->custom_exprs = (List *)
1287  fix_upper_expr(root,
1288  (Node *) cscan->custom_exprs,
1289  itlist,
1290  INDEX_VAR,
1291  rtoffset);
1292  pfree(itlist);
1293  /* custom_scan_tlist itself just needs fix_scan_list() adjustments */
1294  cscan->custom_scan_tlist =
1295  fix_scan_list(root, cscan->custom_scan_tlist, rtoffset);
1296  }
1297  else
1298  {
1299  /* Adjust tlist, qual, custom_exprs in the standard way */
1300  cscan->scan.plan.targetlist =
1301  fix_scan_list(root, cscan->scan.plan.targetlist, rtoffset);
1302  cscan->scan.plan.qual =
1303  fix_scan_list(root, cscan->scan.plan.qual, rtoffset);
1304  cscan->custom_exprs =
1305  fix_scan_list(root, cscan->custom_exprs, rtoffset);
1306  }
1307 
1308  /* Adjust child plan-nodes recursively, if needed */
1309  foreach(lc, cscan->custom_plans)
1310  {
1311  lfirst(lc) = set_plan_refs(root, (Plan *) lfirst(lc), rtoffset);
1312  }
1313 
1314  /* Adjust custom_relids if needed */
1315  if (rtoffset > 0)
1316  {
1317  Bitmapset *tempset = NULL;
1318  int x = -1;
1319 
1320  while ((x = bms_next_member(cscan->custom_relids, x)) >= 0)
1321  tempset = bms_add_member(tempset, x + rtoffset);
1322  cscan->custom_relids = tempset;
1323  }
1324 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:145
Plan plan
Definition: plannodes.h:328
Index scanrelid
Definition: plannodes.h:329
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:937
Definition: nodes.h:509
List * custom_exprs
Definition: plannodes.h:629
List * custom_plans
Definition: plannodes.h:628
static Plan * set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:434
void pfree(void *pointer)
Definition: mcxt.c:950
static Node * fix_upper_expr(PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, Index newvarno, int rtoffset)
Definition: setrefs.c:2329
Scan scan
Definition: plannodes.h:625
#define NULL
Definition: c.h:229
#define lfirst(lc)
Definition: pg_list.h:106
List * custom_scan_tlist
Definition: plannodes.h:631
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:1939
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
Bitmapset * custom_relids
Definition: plannodes.h:632
List * targetlist
Definition: plannodes.h:144
#define INDEX_VAR
Definition: primnodes.h:155
Definition: pg_list.h:45
#define fix_scan_list(root, lst, rtoffset)
Definition: setrefs.c:81
static void set_dummy_tlist_references ( Plan plan,
int  rtoffset 
)
static

Definition at line 1873 of file setrefs.c.

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

Referenced by set_plan_refs().

1874 {
1875  List *output_targetlist;
1876  ListCell *l;
1877 
1878  output_targetlist = NIL;
1879  foreach(l, plan->targetlist)
1880  {
1881  TargetEntry *tle = (TargetEntry *) lfirst(l);
1882  Var *oldvar = (Var *) tle->expr;
1883  Var *newvar;
1884 
1885  /*
1886  * As in search_indexed_tlist_for_non_var(), we prefer to keep Consts
1887  * as Consts, not Vars referencing Consts. Here, there's no speed
1888  * advantage to be had, but it makes EXPLAIN output look cleaner, and
1889  * again it avoids confusing the executor.
1890  */
1891  if (IsA(oldvar, Const))
1892  {
1893  /* just reuse the existing TLE node */
1894  output_targetlist = lappend(output_targetlist, tle);
1895  continue;
1896  }
1897 
1898  newvar = makeVar(OUTER_VAR,
1899  tle->resno,
1900  exprType((Node *) oldvar),
1901  exprTypmod((Node *) oldvar),
1902  exprCollation((Node *) oldvar),
1903  0);
1904  if (IsA(oldvar, Var))
1905  {
1906  newvar->varnoold = oldvar->varno + rtoffset;
1907  newvar->varoattno = oldvar->varattno;
1908  }
1909  else
1910  {
1911  newvar->varnoold = 0; /* wasn't ever a plain Var */
1912  newvar->varoattno = 0;
1913  }
1914 
1915  tle = flatCopyTargetEntry(tle);
1916  tle->expr = (Expr *) newvar;
1917  output_targetlist = lappend(output_targetlist, tle);
1918  }
1919  plan->targetlist = output_targetlist;
1920 
1921  /* We don't touch plan->qual here */
1922 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
Definition: nodes.h:509
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
AttrNumber resno
Definition: primnodes.h:1369
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
Definition: makefuncs.c:268
Var * makeVar(Index varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:67
List * lappend(List *list, void *datum)
Definition: list.c:128
Index varno
Definition: primnodes.h:166
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1368
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:720
List * targetlist
Definition: plannodes.h:144
Definition: pg_list.h:45
#define OUTER_VAR
Definition: primnodes.h:154
static void set_foreignscan_references ( PlannerInfo root,
ForeignScan fscan,
int  rtoffset 
)
static

Definition at line 1181 of file setrefs.c.

References bms_add_member(), bms_next_member(), build_tlist_index(), ForeignScan::fdw_exprs, ForeignScan::fdw_recheck_quals, ForeignScan::fdw_scan_tlist, fix_scan_list, fix_upper_expr(), ForeignScan::fs_relids, INDEX_VAR, NIL, NULL, pfree(), Scan::plan, Plan::qual, ForeignScan::scan, Scan::scanrelid, and Plan::targetlist.

Referenced by set_plan_refs().

1184 {
1185  /* Adjust scanrelid if it's valid */
1186  if (fscan->scan.scanrelid > 0)
1187  fscan->scan.scanrelid += rtoffset;
1188 
1189  if (fscan->fdw_scan_tlist != NIL || fscan->scan.scanrelid == 0)
1190  {
1191  /*
1192  * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals to reference
1193  * foreign scan tuple
1194  */
1196 
1197  fscan->scan.plan.targetlist = (List *)
1198  fix_upper_expr(root,
1199  (Node *) fscan->scan.plan.targetlist,
1200  itlist,
1201  INDEX_VAR,
1202  rtoffset);
1203  fscan->scan.plan.qual = (List *)
1204  fix_upper_expr(root,
1205  (Node *) fscan->scan.plan.qual,
1206  itlist,
1207  INDEX_VAR,
1208  rtoffset);
1209  fscan->fdw_exprs = (List *)
1210  fix_upper_expr(root,
1211  (Node *) fscan->fdw_exprs,
1212  itlist,
1213  INDEX_VAR,
1214  rtoffset);
1215  fscan->fdw_recheck_quals = (List *)
1216  fix_upper_expr(root,
1217  (Node *) fscan->fdw_recheck_quals,
1218  itlist,
1219  INDEX_VAR,
1220  rtoffset);
1221  pfree(itlist);
1222  /* fdw_scan_tlist itself just needs fix_scan_list() adjustments */
1223  fscan->fdw_scan_tlist =
1224  fix_scan_list(root, fscan->fdw_scan_tlist, rtoffset);
1225  }
1226  else
1227  {
1228  /*
1229  * Adjust tlist, qual, fdw_exprs, fdw_recheck_quals in the standard
1230  * way
1231  */
1232  fscan->scan.plan.targetlist =
1233  fix_scan_list(root, fscan->scan.plan.targetlist, rtoffset);
1234  fscan->scan.plan.qual =
1235  fix_scan_list(root, fscan->scan.plan.qual, rtoffset);
1236  fscan->fdw_exprs =
1237  fix_scan_list(root, fscan->fdw_exprs, rtoffset);
1238  fscan->fdw_recheck_quals =
1239  fix_scan_list(root, fscan->fdw_recheck_quals, rtoffset);
1240  }
1241 
1242  /* Adjust fs_relids if needed */
1243  if (rtoffset > 0)
1244  {
1245  Bitmapset *tempset = NULL;
1246  int x = -1;
1247 
1248  while ((x = bms_next_member(fscan->fs_relids, x)) >= 0)
1249  tempset = bms_add_member(tempset, x + rtoffset);
1250  fscan->fs_relids = tempset;
1251  }
1252 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:145
Plan plan
Definition: plannodes.h:328
Index scanrelid
Definition: plannodes.h:329
List * fdw_exprs
Definition: plannodes.h:600
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:937
Definition: nodes.h:509
List * fdw_scan_tlist
Definition: plannodes.h:602
void pfree(void *pointer)
Definition: mcxt.c:950
List * fdw_recheck_quals
Definition: plannodes.h:603
static Node * fix_upper_expr(PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, Index newvarno, int rtoffset)
Definition: setrefs.c:2329
#define NULL
Definition: c.h:229
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:1939
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
List * targetlist
Definition: plannodes.h:144
#define INDEX_VAR
Definition: primnodes.h:155
Definition: pg_list.h:45
#define fix_scan_list(root, lst, rtoffset)
Definition: setrefs.c:81
Bitmapset * fs_relids
Definition: plannodes.h:604
static Plan * set_indexonlyscan_references ( PlannerInfo root,
IndexOnlyScan plan,
int  rtoffset 
)
static

Definition at line 1013 of file setrefs.c.

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

Referenced by set_plan_refs().

1016 {
1017  indexed_tlist *index_itlist;
1018 
1019  index_itlist = build_tlist_index(plan->indextlist);
1020 
1021  plan->scan.scanrelid += rtoffset;
1022  plan->scan.plan.targetlist = (List *)
1023  fix_upper_expr(root,
1024  (Node *) plan->scan.plan.targetlist,
1025  index_itlist,
1026  INDEX_VAR,
1027  rtoffset);
1028  plan->scan.plan.qual = (List *)
1029  fix_upper_expr(root,
1030  (Node *) plan->scan.plan.qual,
1031  index_itlist,
1032  INDEX_VAR,
1033  rtoffset);
1034  /* indexqual is already transformed to reference index columns */
1035  plan->indexqual = fix_scan_list(root, plan->indexqual, rtoffset);
1036  /* indexorderby is already transformed to reference index columns */
1037  plan->indexorderby = fix_scan_list(root, plan->indexorderby, rtoffset);
1038  /* indextlist must NOT be transformed to reference index columns */
1039  plan->indextlist = fix_scan_list(root, plan->indextlist, rtoffset);
1040 
1041  pfree(index_itlist);
1042 
1043  return (Plan *) plan;
1044 }
List * qual
Definition: plannodes.h:145
Plan plan
Definition: plannodes.h:328
Index scanrelid
Definition: plannodes.h:329
Definition: nodes.h:509
void pfree(void *pointer)
Definition: mcxt.c:950
static Node * fix_upper_expr(PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, Index newvarno, int rtoffset)
Definition: setrefs.c:2329
List * indextlist
Definition: plannodes.h:421
List * indexorderby
Definition: plannodes.h:420
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:1939
List * indexqual
Definition: plannodes.h:419
List * targetlist
Definition: plannodes.h:144
#define INDEX_VAR
Definition: primnodes.h:155
Definition: pg_list.h:45
#define fix_scan_list(root, lst, rtoffset)
Definition: setrefs.c:81
static void set_join_references ( PlannerInfo root,
Join join,
int  rtoffset 
)
static

Definition at line 1600 of file setrefs.c.

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

Referenced by set_plan_refs().

1601 {
1602  Plan *outer_plan = join->plan.lefttree;
1603  Plan *inner_plan = join->plan.righttree;
1604  indexed_tlist *outer_itlist;
1605  indexed_tlist *inner_itlist;
1606 
1607  outer_itlist = build_tlist_index(outer_plan->targetlist);
1608  inner_itlist = build_tlist_index(inner_plan->targetlist);
1609 
1610  /*
1611  * First process the joinquals (including merge or hash clauses). These
1612  * are logically below the join so they can always use all values
1613  * available from the input tlists. It's okay to also handle
1614  * NestLoopParams now, because those couldn't refer to nullable
1615  * subexpressions.
1616  */
1617  join->joinqual = fix_join_expr(root,
1618  join->joinqual,
1619  outer_itlist,
1620  inner_itlist,
1621  (Index) 0,
1622  rtoffset);
1623 
1624  /* Now do join-type-specific stuff */
1625  if (IsA(join, NestLoop))
1626  {
1627  NestLoop *nl = (NestLoop *) join;
1628  ListCell *lc;
1629 
1630  foreach(lc, nl->nestParams)
1631  {
1632  NestLoopParam *nlp = (NestLoopParam *) lfirst(lc);
1633 
1634  nlp->paramval = (Var *) fix_upper_expr(root,
1635  (Node *) nlp->paramval,
1636  outer_itlist,
1637  OUTER_VAR,
1638  rtoffset);
1639  /* Check we replaced any PlaceHolderVar with simple Var */
1640  if (!(IsA(nlp->paramval, Var) &&
1641  nlp->paramval->varno == OUTER_VAR))
1642  elog(ERROR, "NestLoopParam was not reduced to a simple Var");
1643  }
1644  }
1645  else if (IsA(join, MergeJoin))
1646  {
1647  MergeJoin *mj = (MergeJoin *) join;
1648 
1649  mj->mergeclauses = fix_join_expr(root,
1650  mj->mergeclauses,
1651  outer_itlist,
1652  inner_itlist,
1653  (Index) 0,
1654  rtoffset);
1655  }
1656  else if (IsA(join, HashJoin))
1657  {
1658  HashJoin *hj = (HashJoin *) join;
1659 
1660  hj->hashclauses = fix_join_expr(root,
1661  hj->hashclauses,
1662  outer_itlist,
1663  inner_itlist,
1664  (Index) 0,
1665  rtoffset);
1666  }
1667 
1668  /*
1669  * Now we need to fix up the targetlist and qpqual, which are logically
1670  * above the join. This means they should not re-use any input expression
1671  * that was computed in the nullable side of an outer join. Vars and
1672  * PlaceHolderVars are fine, so we can implement this restriction just by
1673  * clearing has_non_vars in the indexed_tlist structs.
1674  *
1675  * XXX This is a grotty workaround for the fact that we don't clearly
1676  * distinguish between a Var appearing below an outer join and the "same"
1677  * Var appearing above it. If we did, we'd not need to hack the matching
1678  * rules this way.
1679  */
1680  switch (join->jointype)
1681  {
1682  case JOIN_LEFT:
1683  case JOIN_SEMI:
1684  case JOIN_ANTI:
1685  inner_itlist->has_non_vars = false;
1686  break;
1687  case JOIN_RIGHT:
1688  outer_itlist->has_non_vars = false;
1689  break;
1690  case JOIN_FULL:
1691  outer_itlist->has_non_vars = false;
1692  inner_itlist->has_non_vars = false;
1693  break;
1694  default:
1695  break;
1696  }
1697 
1698  join->plan.targetlist = fix_join_expr(root,
1699  join->plan.targetlist,
1700  outer_itlist,
1701  inner_itlist,
1702  (Index) 0,
1703  rtoffset);
1704  join->plan.qual = fix_join_expr(root,
1705  join->plan.qual,
1706  outer_itlist,
1707  inner_itlist,
1708  (Index) 0,
1709  rtoffset);
1710 
1711  pfree(outer_itlist);
1712  pfree(inner_itlist);
1713 }
List * qual
Definition: plannodes.h:145
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
List * nestParams
Definition: plannodes.h:686
List * hashclauses
Definition: plannodes.h:726
Definition: nodes.h:509
Definition: primnodes.h:163
List * mergeclauses
Definition: plannodes.h:711
static List * fix_join_expr(PlannerInfo *root, List *clauses, indexed_tlist *outer_itlist, indexed_tlist *inner_itlist, Index acceptable_rel, int rtoffset)
Definition: setrefs.c:2185
JoinType jointype
Definition: plannodes.h:667
struct Plan * righttree
Definition: plannodes.h:147
void pfree(void *pointer)
Definition: mcxt.c:950
Var * paramval
Definition: plannodes.h:693
#define ERROR
Definition: elog.h:43
static Node * fix_upper_expr(PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, Index newvarno, int rtoffset)
Definition: setrefs.c:2329
Index varno
Definition: primnodes.h:166
bool has_non_vars
Definition: setrefs.c:43
unsigned int Index
Definition: c.h:365
#define lfirst(lc)
Definition: pg_list.h:106
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:1939
struct Plan * lefttree
Definition: plannodes.h:146
List * targetlist
Definition: plannodes.h:144
#define elog
Definition: elog.h:219
#define OUTER_VAR
Definition: primnodes.h:154
List * joinqual
Definition: plannodes.h:669
Plan plan
Definition: plannodes.h:666
Plan* set_plan_references ( PlannerInfo root,
Plan plan 
)

Definition at line 209 of file setrefs.c.

References add_rtes_to_flat_rtable(), PlannerGlobal::finalrowmarks, PlannerGlobal::finalrtable, PlannerInfo::glob, lappend(), lfirst_node, list_length(), palloc(), PlanRowMark::prti, PlannerInfo::rowMarks, PlanRowMark::rti, and set_plan_refs().

Referenced by set_subqueryscan_references(), and standard_planner().

210 {
211  PlannerGlobal *glob = root->glob;
212  int rtoffset = list_length(glob->finalrtable);
213  ListCell *lc;
214 
215  /*
216  * Add all the query's RTEs to the flattened rangetable. The live ones
217  * will have their rangetable indexes increased by rtoffset. (Additional
218  * RTEs, not referenced by the Plan tree, might get added after those.)
219  */
220  add_rtes_to_flat_rtable(root, false);
221 
222  /*
223  * Adjust RT indexes of PlanRowMarks and add to final rowmarks list
224  */
225  foreach(lc, root->rowMarks)
226  {
228  PlanRowMark *newrc;
229 
230  /* flat copy is enough since all fields are scalars */
231  newrc = (PlanRowMark *) palloc(sizeof(PlanRowMark));
232  memcpy(newrc, rc, sizeof(PlanRowMark));
233 
234  /* adjust indexes ... but *not* the rowmarkId */
235  newrc->rti += rtoffset;
236  newrc->prti += rtoffset;
237 
238  glob->finalrowmarks = lappend(glob->finalrowmarks, newrc);
239  }
240 
241  /* Now fix the Plan tree */
242  return set_plan_refs(root, plan, rtoffset);
243 }
List * rowMarks
Definition: relation.h:256
Index prti
Definition: plannodes.h:1005
static Plan * set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:434
#define lfirst_node(type, lc)
Definition: pg_list.h:109
PlannerGlobal * glob
Definition: relation.h:157
List * lappend(List *list, void *datum)
Definition: list.c:128
static void add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing)
Definition: setrefs.c:251
static int list_length(const List *l)
Definition: pg_list.h:89
List * finalrtable
Definition: relation.h:104
void * palloc(Size size)
Definition: mcxt.c:849
List * finalrowmarks
Definition: relation.h:106
static Plan * set_plan_refs ( PlannerInfo root,
Plan plan,
int  rtoffset 
)
static

Definition at line 434 of file setrefs.c.

References Agg::aggsplit, Append::appendplans, 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, fix_join_expr(), fix_scan_expr(), fix_scan_list, forthree, FunctionScan::functions, PlannerInfo::glob, IndexScan::indexorderby, IndexScan::indexorderbyorig, IndexScan::indexqual, BitmapIndexScan::indexqual, IndexScan::indexqualorig, BitmapIndexScan::indexqualorig, lappend(), lappend_int(), PlannerGlobal::lastPlanNodeId, Plan::lefttree, lfirst, lfirst_int, Limit::limitCount, Limit::limitOffset, linitial, linitial_int, list_concat(), list_copy(), list_length(), MergeAppend::mergeplans, NIL, nodeTag, ModifyTable::nominalRelation, PlannerGlobal::nonleafResultRelations, NULL, ModifyTable::onConflictSet, ModifyTable::onConflictWhere, ModifyTable::partitioned_rels, Append::partitioned_rels, MergeAppend::partitioned_rels, pfree(), Result::plan, ModifyTable::plan, Append::plan, MergeAppend::plan, BitmapAnd::plan, BitmapOr::plan, Scan::plan, LockRows::plan, Limit::plan, Plan::plan_node_id, ModifyTable::plans, PlanRowMark::prti, Plan::qual, Result::resconstantqual, PlannerGlobal::resultRelations, ModifyTable::resultRelations, ModifyTable::resultRelIndex, ModifyTable::returningLists, Plan::righttree, PlannerGlobal::rootResultRelations, ModifyTable::rootResultRelIndex, ModifyTable::rowMarks, LockRows::rowMarks, PlanRowMark::rti, SampleScan::scan, IndexScan::scan, BitmapIndexScan::scan, BitmapHeapScan::scan, TidScan::scan, FunctionScan::scan, ValuesScan::scan, TableFuncScan::scan, CteScan::scan, NamedTuplestoreScan::scan, WorkTableScan::scan, Scan::scanrelid, set_customscan_references(), set_dummy_tlist_references(), set_foreignscan_references(), set_indexonlyscan_references(), set_join_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_IndexOnlyScan, T_IndexScan, T_Limit, T_LockRows, T_Material, T_MergeAppend, T_MergeJoin, T_ModifyTable, T_NamedTuplestoreScan, T_NestLoop, T_ProjectSet, T_RecursiveUnion, T_Result, T_SampleScan, T_SeqScan, T_SetOp, T_Sort, T_SubqueryScan, T_TableFuncScan, T_TidScan, T_Unique, T_ValuesScan, T_WindowAgg, T_WorkTableScan, TableFuncScan::tablefunc, SampleScan::tablesample, Plan::targetlist, TidScan::tidquals, ValuesScan::values_lists, and ModifyTable::withCheckOptionLists.

Referenced by set_customscan_references(), and set_plan_references().

435 {
436  ListCell *l;
437 
438  if (plan == NULL)
439  return NULL;
440 
441  /* Assign this node a unique ID. */
442  plan->plan_node_id = root->glob->lastPlanNodeId++;
443 
444  /*
445  * Plan-type-specific fixes
446  */
447  switch (nodeTag(plan))
448  {
449  case T_SeqScan:
450  {
451  SeqScan *splan = (SeqScan *) plan;
452 
453  splan->scanrelid += rtoffset;
454  splan->plan.targetlist =
455  fix_scan_list(root, splan->plan.targetlist, rtoffset);
456  splan->plan.qual =
457  fix_scan_list(root, splan->plan.qual, rtoffset);
458  }
459  break;
460  case T_SampleScan:
461  {
462  SampleScan *splan = (SampleScan *) plan;
463 
464  splan->scan.scanrelid += rtoffset;
465  splan->scan.plan.targetlist =
466  fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
467  splan->scan.plan.qual =
468  fix_scan_list(root, splan->scan.plan.qual, rtoffset);
469  splan->tablesample = (TableSampleClause *)
470  fix_scan_expr(root, (Node *) splan->tablesample, rtoffset);
471  }
472  break;
473  case T_IndexScan:
474  {
475  IndexScan *splan = (IndexScan *) plan;
476 
477  splan->scan.scanrelid += rtoffset;
478  splan->scan.plan.targetlist =
479  fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
480  splan->scan.plan.qual =
481  fix_scan_list(root, splan->scan.plan.qual, rtoffset);
482  splan->indexqual =
483  fix_scan_list(root, splan->indexqual, rtoffset);
484  splan->indexqualorig =
485  fix_scan_list(root, splan->indexqualorig, rtoffset);
486  splan->indexorderby =
487  fix_scan_list(root, splan->indexorderby, rtoffset);
488  splan->indexorderbyorig =
489  fix_scan_list(root, splan->indexorderbyorig, rtoffset);
490  }
491  break;
492  case T_IndexOnlyScan:
493  {
494  IndexOnlyScan *splan = (IndexOnlyScan *) plan;
495 
496  return set_indexonlyscan_references(root, splan, rtoffset);
497  }
498  break;
499  case T_BitmapIndexScan:
500  {
501  BitmapIndexScan *splan = (BitmapIndexScan *) plan;
502 
503  splan->scan.scanrelid += rtoffset;
504  /* no need to fix targetlist and qual */
505  Assert(splan->scan.plan.targetlist == NIL);
506  Assert(splan->scan.plan.qual == NIL);
507  splan->indexqual =
508  fix_scan_list(root, splan->indexqual, rtoffset);
509  splan->indexqualorig =
510  fix_scan_list(root, splan->indexqualorig, rtoffset);
511  }
512  break;
513  case T_BitmapHeapScan:
514  {
515  BitmapHeapScan *splan = (BitmapHeapScan *) plan;
516 
517  splan->scan.scanrelid += rtoffset;
518  splan->scan.plan.targetlist =
519  fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
520  splan->scan.plan.qual =
521  fix_scan_list(root, splan->scan.plan.qual, rtoffset);
522  splan->bitmapqualorig =
523  fix_scan_list(root, splan->bitmapqualorig, rtoffset);
524  }
525  break;
526  case T_TidScan:
527  {
528  TidScan *splan = (TidScan *) plan;
529 
530  splan->scan.scanrelid += rtoffset;
531  splan->scan.plan.targetlist =
532  fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
533  splan->scan.plan.qual =
534  fix_scan_list(root, splan->scan.plan.qual, rtoffset);
535  splan->tidquals =
536  fix_scan_list(root, splan->tidquals, rtoffset);
537  }
538  break;
539  case T_SubqueryScan:
540  /* Needs special treatment, see comments below */
541  return set_subqueryscan_references(root,
542  (SubqueryScan *) plan,
543  rtoffset);
544  case T_FunctionScan:
545  {
546  FunctionScan *splan = (FunctionScan *) plan;
547 
548  splan->scan.scanrelid += rtoffset;
549  splan->scan.plan.targetlist =
550  fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
551  splan->scan.plan.qual =
552  fix_scan_list(root, splan->scan.plan.qual, rtoffset);
553  splan->functions =
554  fix_scan_list(root, splan->functions, rtoffset);
555  }
556  break;
557  case T_TableFuncScan:
558  {
559  TableFuncScan *splan = (TableFuncScan *) plan;
560 
561  splan->scan.scanrelid += rtoffset;
562  splan->scan.plan.targetlist =
563  fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
564  splan->scan.plan.qual =
565  fix_scan_list(root, splan->scan.plan.qual, rtoffset);
566  splan->tablefunc = (TableFunc *)
567  fix_scan_expr(root, (Node *) splan->tablefunc, rtoffset);
568  }
569  break;
570  case T_ValuesScan:
571  {
572  ValuesScan *splan = (ValuesScan *) plan;
573 
574  splan->scan.scanrelid += rtoffset;
575  splan->scan.plan.targetlist =
576  fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
577  splan->scan.plan.qual =
578  fix_scan_list(root, splan->scan.plan.qual, rtoffset);
579  splan->values_lists =
580  fix_scan_list(root, splan->values_lists, rtoffset);
581  }
582  break;
583  case T_CteScan:
584  {
585  CteScan *splan = (CteScan *) plan;
586 
587  splan->scan.scanrelid += rtoffset;
588  splan->scan.plan.targetlist =
589  fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
590  splan->scan.plan.qual =
591  fix_scan_list(root, splan->scan.plan.qual, rtoffset);
592  }
593  break;
595  {
596  NamedTuplestoreScan *splan = (NamedTuplestoreScan *) plan;
597 
598  splan->scan.scanrelid += rtoffset;
599  splan->scan.plan.targetlist =
600  fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
601  splan->scan.plan.qual =
602  fix_scan_list(root, splan->scan.plan.qual, rtoffset);
603  }
604  break;
605  case T_WorkTableScan:
606  {
607  WorkTableScan *splan = (WorkTableScan *) plan;
608 
609  splan->scan.scanrelid += rtoffset;
610  splan->scan.plan.targetlist =
611  fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
612  splan->scan.plan.qual =
613  fix_scan_list(root, splan->scan.plan.qual, rtoffset);
614  }
615  break;
616  case T_ForeignScan:
617  set_foreignscan_references(root, (ForeignScan *) plan, rtoffset);
618  break;
619  case T_CustomScan:
620  set_customscan_references(root, (CustomScan *) plan, rtoffset);
621  break;
622 
623  case T_NestLoop:
624  case T_MergeJoin:
625  case T_HashJoin:
626  set_join_references(root, (Join *) plan, rtoffset);
627  break;
628 
629  case T_Gather:
630  case T_GatherMerge:
631  set_upper_references(root, plan, rtoffset);
632  break;
633 
634  case T_Hash:
635  case T_Material:
636  case T_Sort:
637  case T_Unique:
638  case T_SetOp:
639 
640  /*
641  * These plan types don't actually bother to evaluate their
642  * targetlists, because they just return their unmodified input
643  * tuples. Even though the targetlist won't be used by the
644  * executor, we fix it up for possible use by EXPLAIN (not to
645  * mention ease of debugging --- wrong varnos are very confusing).
646  */
647  set_dummy_tlist_references(plan, rtoffset);
648 
649  /*
650  * Since these plan types don't check quals either, we should not
651  * find any qual expression attached to them.
652  */
653  Assert(plan->qual == NIL);
654  break;
655  case T_LockRows:
656  {
657  LockRows *splan = (LockRows *) plan;
658 
659  /*
660  * Like the plan types above, LockRows doesn't evaluate its
661  * tlist or quals. But we have to fix up the RT indexes in
662  * its rowmarks.
663  */
664  set_dummy_tlist_references(plan, rtoffset);
665  Assert(splan->plan.qual == NIL);
666 
667  foreach(l, splan->rowMarks)
668  {
669  PlanRowMark *rc = (PlanRowMark *) lfirst(l);
670 
671  rc->rti += rtoffset;
672  rc->prti += rtoffset;
673  }
674  }
675  break;
676  case T_Limit:
677  {
678  Limit *splan = (Limit *) plan;
679 
680  /*
681  * Like the plan types above, Limit doesn't evaluate its tlist
682  * or quals. It does have live expressions for limit/offset,
683  * however; and those cannot contain subplan variable refs, so
684  * fix_scan_expr works for them.
685  */
686  set_dummy_tlist_references(plan, rtoffset);
687  Assert(splan->plan.qual == NIL);
688 
689  splan->limitOffset =
690  fix_scan_expr(root, splan->limitOffset, rtoffset);
691  splan->limitCount =
692  fix_scan_expr(root, splan->limitCount, rtoffset);
693  }
694  break;
695  case T_Agg:
696  {
697  Agg *agg = (Agg *) plan;
698 
699  /*
700  * If this node is combining partial-aggregation results, we
701  * must convert its Aggrefs to contain references to the
702  * partial-aggregate subexpressions that will be available
703  * from the child plan node.
704  */
705  if (DO_AGGSPLIT_COMBINE(agg->aggsplit))
706  {
707  plan->targetlist = (List *)
709  NULL);
710  plan->qual = (List *)
712  NULL);
713  }
714 
715  set_upper_references(root, plan, rtoffset);
716  }
717  break;
718  case T_Group:
719  set_upper_references(root, plan, rtoffset);
720  break;
721  case T_WindowAgg:
722  {
723  WindowAgg *wplan = (WindowAgg *) plan;
724 
725  set_upper_references(root, plan, rtoffset);
726 
727  /*
728  * Like Limit node limit/offset expressions, WindowAgg has
729  * frame offset expressions, which cannot contain subplan
730  * variable refs, so fix_scan_expr works for them.
731  */
732  wplan->startOffset =
733  fix_scan_expr(root, wplan->startOffset, rtoffset);
734  wplan->endOffset =
735  fix_scan_expr(root, wplan->endOffset, rtoffset);
736  }
737  break;
738  case T_Result:
739  {
740  Result *splan = (Result *) plan;
741 
742  /*
743  * Result may or may not have a subplan; if not, it's more
744  * like a scan node than an upper node.
745  */
746  if (splan->plan.lefttree != NULL)
747  set_upper_references(root, plan, rtoffset);
748  else
749  {
750  splan->plan.targetlist =
751  fix_scan_list(root, splan->plan.targetlist, rtoffset);
752  splan->plan.qual =
753  fix_scan_list(root, splan->plan.qual, rtoffset);
754  }
755  /* resconstantqual can't contain any subplan variable refs */
756  splan->resconstantqual =
757  fix_scan_expr(root, splan->resconstantqual, rtoffset);
758  }
759  break;
760  case T_ProjectSet:
761  set_upper_references(root, plan, rtoffset);
762  break;
763  case T_ModifyTable:
764  {
765  ModifyTable *splan = (ModifyTable *) plan;
766 
767  Assert(splan->plan.targetlist == NIL);
768  Assert(splan->plan.qual == NIL);
769 
770  splan->withCheckOptionLists =
771  fix_scan_list(root, splan->withCheckOptionLists, rtoffset);
772 
773  if (splan->returningLists)
774  {
775  List *newRL = NIL;
776  ListCell *lcrl,
777  *lcrr,
778  *lcp;
779 
780  /*
781  * Pass each per-subplan returningList through
782  * set_returning_clause_references().
783  */
785  Assert(list_length(splan->returningLists) == list_length(splan->plans));
786  forthree(lcrl, splan->returningLists,
787  lcrr, splan->resultRelations,
788  lcp, splan->plans)
789  {
790  List *rlist = (List *) lfirst(lcrl);
791  Index resultrel = lfirst_int(lcrr);
792  Plan *subplan = (Plan *) lfirst(lcp);
793 
794  rlist = set_returning_clause_references(root,
795  rlist,
796  subplan,
797  resultrel,
798  rtoffset);
799  newRL = lappend(newRL, rlist);
800  }
801  splan->returningLists = newRL;
802 
803  /*
804  * Set up the visible plan targetlist as being the same as
805  * the first RETURNING list. This is for the use of
806  * EXPLAIN; the executor won't pay any attention to the
807  * targetlist. We postpone this step until here so that
808  * we don't have to do set_returning_clause_references()
809  * twice on identical targetlists.
810  */
811  splan->plan.targetlist = copyObject(linitial(newRL));
812  }
813 
814  /*
815  * We treat ModifyTable with ON CONFLICT as a form of 'pseudo
816  * join', where the inner side is the EXCLUDED tuple.
817  * Therefore use fix_join_expr to setup the relevant variables
818  * to INNER_VAR. We explicitly don't create any OUTER_VARs as
819  * those are already used by RETURNING and it seems better to
820  * be non-conflicting.
821  */
822  if (splan->onConflictSet)
823  {
824  indexed_tlist *itlist;
825 
826  itlist = build_tlist_index(splan->exclRelTlist);
827 
828  splan->onConflictSet =
829  fix_join_expr(root, splan->onConflictSet,
830  NULL, itlist,
832  rtoffset);
833 
834  splan->onConflictWhere = (Node *)
835  fix_join_expr(root, (List *) splan->onConflictWhere,
836  NULL, itlist,
838  rtoffset);
839 
840  pfree(itlist);
841 
842  splan->exclRelTlist =
843  fix_scan_list(root, splan->exclRelTlist, rtoffset);
844  }
845 
846  splan->nominalRelation += rtoffset;
847  splan->exclRelRTI += rtoffset;
848 
849  foreach(l, splan->partitioned_rels)
850  {
851  lfirst_int(l) += rtoffset;
852  }
853  foreach(l, splan->resultRelations)
854  {
855  lfirst_int(l) += rtoffset;
856  }
857  foreach(l, splan->rowMarks)
858  {
859  PlanRowMark *rc = (PlanRowMark *) lfirst(l);
860 
861  rc->rti += rtoffset;
862  rc->prti += rtoffset;
863  }
864  foreach(l, splan->plans)
865  {
866  lfirst(l) = set_plan_refs(root,
867  (Plan *) lfirst(l),
868  rtoffset);
869  }
870 
871  /*
872  * Append this ModifyTable node's final result relation RT
873  * index(es) to the global list for the plan, and set its
874  * resultRelIndex to reflect their starting position in the
875  * global list.
876  */
878  root->glob->resultRelations =
880  list_copy(splan->resultRelations));
881 
882  /*
883  * If the main target relation is a partitioned table, the
884  * following list contains the RT indexes of partitioned child
885  * relations including the root, which are not included in the
886  * above list. We also keep RT indexes of the roots
887  * separately to be identitied as such during the executor
888  * initialization.
889  */
890  if (splan->partitioned_rels != NIL)
891  {
894  list_copy(splan->partitioned_rels));
895  /* Remember where this root will be in the global list. */
896  splan->rootResultRelIndex =
898  root->glob->rootResultRelations =
901  }
902  }
903  break;
904  case T_Append:
905  {
906  Append *splan = (Append *) plan;
907 
908  /*
909  * Append, like Sort et al, doesn't actually evaluate its
910  * targetlist or check quals.
911  */
912  set_dummy_tlist_references(plan, rtoffset);
913  Assert(splan->plan.qual == NIL);
914  foreach(l, splan->partitioned_rels)
915  {
916  lfirst_int(l) += rtoffset;
917  }
918  foreach(l, splan->appendplans)
919  {
920  lfirst(l) = set_plan_refs(root,
921  (Plan *) lfirst(l),
922  rtoffset);
923  }
924  }
925  break;
926  case T_MergeAppend:
927  {
928  MergeAppend *splan = (MergeAppend *) plan;
929 
930  /*
931  * MergeAppend, like Sort et al, doesn't actually evaluate its
932  * targetlist or check quals.
933  */
934  set_dummy_tlist_references(plan, rtoffset);
935  Assert(splan->plan.qual == NIL);
936  foreach(l, splan->partitioned_rels)
937  {
938  lfirst_int(l) += rtoffset;
939  }
940  foreach(l, splan->mergeplans)
941  {
942  lfirst(l) = set_plan_refs(root,
943  (Plan *) lfirst(l),
944  rtoffset);
945  }
946  }
947  break;
948  case T_RecursiveUnion:
949  /* This doesn't evaluate targetlist or check quals either */
950  set_dummy_tlist_references(plan, rtoffset);
951  Assert(plan->qual == NIL);
952  break;
953  case T_BitmapAnd:
954  {
955  BitmapAnd *splan = (BitmapAnd *) plan;
956 
957  /* BitmapAnd works like Append, but has no tlist */
958  Assert(splan->plan.targetlist == NIL);
959  Assert(splan->plan.qual == NIL);
960  foreach(l, splan->bitmapplans)
961  {
962  lfirst(l) = set_plan_refs(root,
963  (Plan *) lfirst(l),
964  rtoffset);
965  }
966  }
967  break;
968  case T_BitmapOr:
969  {
970  BitmapOr *splan = (BitmapOr *) plan;
971 
972  /* BitmapOr works like Append, but has no tlist */
973  Assert(splan->plan.targetlist == NIL);
974  Assert(splan->plan.qual == NIL);
975  foreach(l, splan->bitmapplans)
976  {
977  lfirst(l) = set_plan_refs(root,
978  (Plan *) lfirst(l),
979  rtoffset);
980  }
981  }
982  break;
983  default:
984  elog(ERROR, "unrecognized node type: %d",
985  (int) nodeTag(plan));
986  break;
987  }
988 
989  /*
990  * Now recurse into child plans, if any
991  *
992  * NOTE: it is essential that we recurse into child plans AFTER we set
993  * subplan references in this plan's tlist and quals. If we did the
994  * reference-adjustments bottom-up, then we would fail to match this
995  * plan's var nodes against the already-modified nodes of the children.
996  */
997  plan->lefttree = set_plan_refs(root, plan->lefttree, rtoffset);
998  plan->righttree = set_plan_refs(root, plan->righttree, rtoffset);
999 
1000  return plan;
1001 }
List * bitmapplans
Definition: plannodes.h:303
#define NIL
Definition: pg_list.h:69
Plan plan
Definition: plannodes.h:190
List * qual
Definition: plannodes.h:145
Definition: nodes.h:77
Plan plan
Definition: plannodes.h:328
Plan plan
Definition: plannodes.h:914
Index nominalRelation
Definition: plannodes.h:219
Definition: nodes.h:79
Index scanrelid
Definition: plannodes.h:329
Plan plan
Definition: plannodes.h:900
List * withCheckOptionLists
Definition: plannodes.h:226
List * functions
Definition: plannodes.h:508
static void set_join_references(PlannerInfo *root, Join *join, int rtoffset)
Definition: setrefs.c:1600
int resultRelIndex
Definition: plannodes.h:223
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:203
int lastPlanNodeId
Definition: relation.h:123
List * indexqual
Definition: plannodes.h:447
List * tidquals
Definition: plannodes.h:476
TableFunc * tablefunc
Definition: plannodes.h:529
int plan_node_id
Definition: plannodes.h:143
List * resultRelations
Definition: relation.h:108
List * indexqualorig
Definition: plannodes.h:391
struct TableSampleClause * tablesample
Definition: plannodes.h:346
List * list_copy(const List *oldlist)
Definition: list.c:1160
Definition: nodes.h:509
Definition: nodes.h:48
Index prti
Definition: plannodes.h:1005
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * values_lists
Definition: plannodes.h:519
Definition: nodes.h:75
Scan scan
Definition: plannodes.h:518
Node * limitOffset
Definition: plannodes.h:915
#define DO_AGGSPLIT_COMBINE(as)
Definition: nodes.h:768
List * plans
Definition: plannodes.h:225
static Plan * set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:434
List * onConflictSet
Definition: plannodes.h:234
int rootResultRelIndex
Definition: plannodes.h:224
List * resultRelations
Definition: plannodes.h:222
static void set_foreignscan_references(PlannerInfo *root, ForeignScan *fscan, int rtoffset)
Definition: setrefs.c:1181
static List * fix_join_expr(PlannerInfo *root, List *clauses, indexed_tlist *outer_itlist, indexed_tlist *inner_itlist, Index acceptable_rel, int rtoffset)
Definition: setrefs.c:2185
List * rootResultRelations
Definition: relation.h:111
List * appendplans
Definition: plannodes.h:250
#define linitial_int(l)
Definition: pg_list.h:112
Node * startOffset
Definition: plannodes.h:810
Node * resconstantqual
Definition: plannodes.h:191
List * rowMarks
Definition: plannodes.h:230
struct Plan * righttree
Definition: plannodes.h:147
void pfree(void *pointer)
Definition: mcxt.c:950
#define linitial(l)
Definition: pg_list.h:111
Definition: nodes.h:45
#define ERROR
Definition: elog.h:43
List * indexorderbyorig
Definition: plannodes.h:393
#define lfirst_int(lc)
Definition: pg_list.h:107
Scan scan
Definition: plannodes.h:344
Scan scan
Definition: plannodes.h:538
Node * limitCount
Definition: plannodes.h:916
Scan scan
Definition: plannodes.h:388
Definition: nodes.h:76
List * partitioned_rels
Definition: plannodes.h:262
PlannerGlobal * glob
Definition: relation.h:157
Node * endOffset
Definition: plannodes.h:811
static SPIPlanPtr splan
Definition: regress.c:451
static void set_customscan_references(PlannerInfo *root, CustomScan *cscan, int rtoffset)
Definition: setrefs.c:1259
List * partitioned_rels
Definition: plannodes.h:221
List * partitioned_rels
Definition: plannodes.h:249
static Plan * set_subqueryscan_references(PlannerInfo *root, SubqueryScan *plan, int rtoffset)
Definition: setrefs.c:1054
List * indexqual
Definition: plannodes.h:390
List * bitmapqualorig
Definition: plannodes.h:463
List * lappend_int(List *list, int datum)
Definition: list.c:146
List * lappend(List *list, void *datum)
Definition: list.c:128
Plan plan
Definition: plannodes.h:316
static Plan * set_indexonlyscan_references(PlannerInfo *root, IndexOnlyScan *plan, int rtoffset)
Definition: setrefs.c:1013
List * bitmapplans
Definition: plannodes.h:318
static void set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset)
Definition: setrefs.c:1734
unsigned int Index
Definition: c.h:365
Definition: nodes.h:82
static Node * convert_combining_aggrefs(Node *node, void *context)
Definition: setrefs.c:1804
List * indexorderby
Definition: plannodes.h:392
List * nonleafResultRelations
Definition: relation.h:110
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
AggSplit aggsplit
Definition: plannodes.h:784
List * rowMarks
Definition: plannodes.h:901
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:1939
Scan scan
Definition: plannodes.h:475
static int list_length(const List *l)
Definition: pg_list.h:89
struct Plan * lefttree
Definition: plannodes.h:146
List * indexqualorig
Definition: plannodes.h:448
#define nodeTag(nodeptr)
Definition: nodes.h:514
List * targetlist
Definition: plannodes.h:144
Definition: nodes.h:83
static List * set_returning_clause_references(PlannerInfo *root, List *rlist, Plan *topplan, Index resultRelation, int rtoffset)
Definition: setrefs.c:2447
List * mergeplans
Definition: plannodes.h:263
Definition: nodes.h:80
static Node * fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset)
Definition: setrefs.c:1480
List * returningLists
Definition: plannodes.h:227
Plan plan
Definition: plannodes.h:302
Definition: plannodes.h:780
#define elog
Definition: elog.h:219
#define copyObject(obj)
Definition: nodes.h:622
Index exclRelRTI
Definition: plannodes.h:236
Definition: pg_list.h:45
#define fix_scan_list(root, lst, rtoffset)
Definition: setrefs.c:81
static void set_dummy_tlist_references(Plan *plan, int rtoffset)
Definition: setrefs.c:1873
List * exclRelTlist
Definition: plannodes.h:237
Plan plan
Definition: plannodes.h:247
Node * onConflictWhere
Definition: plannodes.h:235
Definition: nodes.h:85
static List * set_returning_clause_references ( PlannerInfo root,
List rlist,
Plan topplan,
Index  resultRelation,
int  rtoffset 
)
static

Definition at line 2447 of file setrefs.c.

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

Referenced by set_plan_refs().

2452 {
2453  indexed_tlist *itlist;
2454 
2455  /*
2456  * We can perform the desired Var fixup by abusing the fix_join_expr
2457  * machinery that formerly handled inner indexscan fixup. We search the
2458  * top plan's targetlist for Vars of non-result relations, and use
2459  * fix_join_expr to convert RETURNING Vars into references to those tlist
2460  * entries, while leaving result-rel Vars as-is.
2461  *
2462  * PlaceHolderVars will also be sought in the targetlist, but no
2463  * more-complex expressions will be. Note that it is not possible for a
2464  * PlaceHolderVar to refer to the result relation, since the result is
2465  * never below an outer join. If that case could happen, we'd have to be
2466  * prepared to pick apart the PlaceHolderVar and evaluate its contained
2467  * expression instead.
2468  */
2469  itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
2470 
2471  rlist = fix_join_expr(root,
2472  rlist,
2473  itlist,
2474  NULL,
2475  resultRelation,
2476  rtoffset);
2477 
2478  pfree(itlist);
2479 
2480  return rlist;
2481 }
static List * fix_join_expr(PlannerInfo *root, List *clauses, indexed_tlist *outer_itlist, indexed_tlist *inner_itlist, Index acceptable_rel, int rtoffset)
Definition: setrefs.c:2185
void pfree(void *pointer)
Definition: mcxt.c:950
#define NULL
Definition: c.h:229
static indexed_tlist * build_tlist_index_other_vars(List *tlist, Index ignore_rel)
Definition: setrefs.c:1989
List * targetlist
Definition: plannodes.h:144
static Plan * set_subqueryscan_references ( PlannerInfo root,
SubqueryScan plan,
int  rtoffset 
)
static

Definition at line 1054 of file setrefs.c.

References find_base_rel(), fix_scan_list, forboth, Plan::initPlan, lfirst, list_concat(), Scan::plan, Plan::qual, TargetEntry::resname, TargetEntry::resorigcol, TargetEntry::resorigtbl, result, SubqueryScan::scan, Scan::scanrelid, set_plan_references(), SubqueryScan::subplan, RelOptInfo::subroot, Plan::targetlist, and trivial_subqueryscan().

Referenced by set_plan_refs().

1057 {
1058  RelOptInfo *rel;
1059  Plan *result;
1060 
1061  /* Need to look up the subquery's RelOptInfo, since we need its subroot */
1062  rel = find_base_rel(root, plan->scan.scanrelid);
1063 
1064  /* Recursively process the subplan */
1065  plan->subplan = set_plan_references(rel->subroot, plan->subplan);
1066 
1067  if (trivial_subqueryscan(plan))
1068  {
1069  /*
1070  * We can omit the SubqueryScan node and just pull up the subplan.
1071  */
1072  ListCell *lp,
1073  *lc;
1074 
1075  result = plan->subplan;
1076 
1077  /* We have to be sure we don't lose any initplans */
1078  result->initPlan = list_concat(plan->scan.plan.initPlan,
1079  result->initPlan);
1080 
1081  /*
1082  * We also have to transfer the SubqueryScan's result-column names
1083  * into the subplan, else columns sent to client will be improperly
1084  * labeled if this is the topmost plan level. Copy the "source
1085  * column" information too.
1086  */
1087  forboth(lp, plan->scan.plan.targetlist, lc, result->targetlist)
1088  {
1089  TargetEntry *ptle = (TargetEntry *) lfirst(lp);
1090  TargetEntry *ctle = (TargetEntry *) lfirst(lc);
1091 
1092  ctle->resname = ptle->resname;
1093  ctle->resorigtbl = ptle->resorigtbl;
1094  ctle->resorigcol = ptle->resorigcol;
1095  }
1096  }
1097  else
1098  {
1099  /*
1100  * Keep the SubqueryScan node. We have to do the processing that
1101  * set_plan_references would otherwise have done on it. Notice we do
1102  * not do set_upper_references() here, because a SubqueryScan will
1103  * always have been created with correct references to its subplan's
1104  * outputs to begin with.
1105  */
1106  plan->scan.scanrelid += rtoffset;
1107  plan->scan.plan.targetlist =
1108  fix_scan_list(root, plan->scan.plan.targetlist, rtoffset);
1109  plan->scan.plan.qual =
1110  fix_scan_list(root, plan->scan.plan.qual, rtoffset);
1111 
1112  result = (Plan *) plan;
1113  }
1114 
1115  return result;
1116 }
List * qual
Definition: plannodes.h:145
Plan plan
Definition: plannodes.h:328
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
Index scanrelid
Definition: plannodes.h:329
Oid resorigtbl
Definition: primnodes.h:1373
List * list_concat(List *list1, List *list2)
Definition: list.c:321
return result
Definition: formatting.c:1633
char * resname
Definition: primnodes.h:1370
static bool trivial_subqueryscan(SubqueryScan *plan)
Definition: setrefs.c:1126
PlannerInfo * subroot
Definition: relation.h:567
#define lfirst(lc)
Definition: pg_list.h:106
List * targetlist
Definition: plannodes.h:144
AttrNumber resorigcol
Definition: primnodes.h:1374
List * initPlan
Definition: plannodes.h:148
Plan * set_plan_references(PlannerInfo *root, Plan *plan)
Definition: setrefs.c:209
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:243
#define fix_scan_list(root, lst, rtoffset)
Definition: setrefs.c:81
Plan * subplan
Definition: plannodes.h:498
static void set_upper_references ( PlannerInfo root,
Plan plan,
int  rtoffset 
)
static

Definition at line 1734 of file setrefs.c.

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

Referenced by set_plan_refs().

1735 {
1736  Plan *subplan = plan->lefttree;
1737  indexed_tlist *subplan_itlist;
1738  List *output_targetlist;
1739  ListCell *l;
1740 
1741  subplan_itlist = build_tlist_index(subplan->targetlist);
1742 
1743  output_targetlist = NIL;
1744  foreach(l, plan->targetlist)
1745  {
1746  TargetEntry *tle = (TargetEntry *) lfirst(l);
1747  Node *newexpr;
1748 
1749  /* If it's a non-Var sort/group item, first try to match by sortref */
1750  if (tle->ressortgroupref != 0 && !IsA(tle->expr, Var))
1751  {
1752  newexpr = (Node *)
1754  tle->ressortgroupref,
1755  subplan_itlist,
1756  OUTER_VAR);
1757  if (!newexpr)
1758  newexpr = fix_upper_expr(root,
1759  (Node *) tle->expr,
1760  subplan_itlist,
1761  OUTER_VAR,
1762  rtoffset);
1763  }
1764  else
1765  newexpr = fix_upper_expr(root,
1766  (Node *) tle->expr,
1767  subplan_itlist,
1768  OUTER_VAR,
1769  rtoffset);
1770  tle = flatCopyTargetEntry(tle);
1771  tle->expr = (Expr *) newexpr;
1772  output_targetlist = lappend(output_targetlist, tle);
1773  }
1774  plan->targetlist = output_targetlist;
1775 
1776  plan->qual = (List *)
1777  fix_upper_expr(root,
1778  (Node *) plan->qual,
1779  subplan_itlist,
1780  OUTER_VAR,
1781  rtoffset);
1782 
1783  pfree(subplan_itlist);
1784 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:145
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Definition: nodes.h:509
Definition: primnodes.h:163
void pfree(void *pointer)
Definition: mcxt.c:950
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
Definition: makefuncs.c:268
static Node * fix_upper_expr(PlannerInfo *root, Node *node, indexed_tlist *subplan_itlist, Index newvarno, int rtoffset)
Definition: setrefs.c:2329
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1368
static indexed_tlist * build_tlist_index(List *tlist)
Definition: setrefs.c:1939
struct Plan * lefttree
Definition: plannodes.h:146
List * targetlist
Definition: plannodes.h:144
Index ressortgroupref
Definition: primnodes.h:1371
static Var * search_indexed_tlist_for_sortgroupref(Expr *node, Index sortgroupref, indexed_tlist *itlist, Index newvarno)
Definition: setrefs.c:2121
Definition: pg_list.h:45
#define OUTER_VAR
Definition: primnodes.h:154
static bool trivial_subqueryscan ( SubqueryScan plan)
static

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

1127 {
1128  int attrno;
1129  ListCell *lp,
1130  *lc;
1131 
1132  if (plan->scan.plan.qual != NIL)
1133  return false;
1134 
1135  if (list_length(plan->scan.plan.targetlist) !=
1136  list_length(plan->subplan->targetlist))
1137  return false; /* tlists not same length */
1138 
1139  attrno = 1;
1140  forboth(lp, plan->scan.plan.targetlist, lc, plan->subplan->targetlist)
1141  {
1142  TargetEntry *ptle = (TargetEntry *) lfirst(lp);
1143  TargetEntry *ctle = (TargetEntry *) lfirst(lc);
1144 
1145  if (ptle->resjunk != ctle->resjunk)
1146  return false; /* tlist doesn't match junk status */
1147 
1148  /*
1149  * We accept either a Var referencing the corresponding element of the
1150  * subplan tlist, or a Const equaling the subplan element. See
1151  * generate_setop_tlist() for motivation.
1152  */
1153  if (ptle->expr && IsA(ptle->expr, Var))
1154  {
1155  Var *var = (Var *) ptle->expr;
1156 
1157  Assert(var->varno == plan->scan.scanrelid);
1158  Assert(var->varlevelsup == 0);
1159  if (var->varattno != attrno)
1160  return false; /* out of order */
1161  }
1162  else if (ptle->expr && IsA(ptle->expr, Const))
1163  {
1164  if (!equal(ptle->expr, ctle->expr))
1165  return false;
1166  }
1167  else
1168  return false;
1169 
1170  attrno++;
1171  }
1172 
1173  return true;
1174 }
#define NIL
Definition: pg_list.h:69
List * qual
Definition: plannodes.h:145
Plan plan
Definition: plannodes.h:328
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Index varlevelsup
Definition: primnodes.h:173
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
Index scanrelid
Definition: plannodes.h:329
bool equal(const void *a, const void *b)
Definition: equalfuncs.c:2962
AttrNumber varattno
Definition: primnodes.h:168
Definition: primnodes.h:163
bool resjunk
Definition: primnodes.h:1375
Index varno
Definition: primnodes.h:166
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1368
static int list_length(const List *l)
Definition: pg_list.h:89
List * targetlist
Definition: plannodes.h:144
Plan * subplan
Definition: plannodes.h:498