PostgreSQL Source Code  git master
prepjointree.c File Reference
Include dependency graph for prepjointree.c:

Go to the source code of this file.

Data Structures

struct  pullup_replace_vars_context
 
struct  reduce_outer_joins_state
 
struct  substitute_multiple_relids_context
 

Typedefs

typedef struct pullup_replace_vars_context pullup_replace_vars_context
 
typedef struct reduce_outer_joins_state reduce_outer_joins_state
 

Functions

static Nodepull_up_sublinks_jointree_recurse (PlannerInfo *root, Node *jtnode, Relids *relids)
 
static Nodepull_up_sublinks_qual_recurse (PlannerInfo *root, Node *node, Node **jtlink1, Relids available_rels1, Node **jtlink2, Relids available_rels2)
 
static Nodepull_up_subqueries_recurse (PlannerInfo *root, Node *jtnode, JoinExpr *lowest_outer_join, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel, bool deletion_ok)
 
static Nodepull_up_simple_subquery (PlannerInfo *root, Node *jtnode, RangeTblEntry *rte, JoinExpr *lowest_outer_join, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel, bool deletion_ok)
 
static Nodepull_up_simple_union_all (PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
 
static void pull_up_union_leaf_queries (Node *setOp, PlannerInfo *root, int parentRTindex, Query *setOpQuery, int childRToffset)
 
static void make_setop_translation_list (Query *query, Index newvarno, List **translated_vars)
 
static bool is_simple_subquery (Query *subquery, RangeTblEntry *rte, JoinExpr *lowest_outer_join, bool deletion_ok)
 
static Nodepull_up_simple_values (PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
 
static bool is_simple_values (PlannerInfo *root, RangeTblEntry *rte, bool deletion_ok)
 
static bool is_simple_union_all (Query *subquery)
 
static bool is_simple_union_all_recurse (Node *setOp, Query *setOpQuery, List *colTypes)
 
static bool is_safe_append_member (Query *subquery)
 
static bool jointree_contains_lateral_outer_refs (Node *jtnode, bool restricted, Relids safe_upper_varnos)
 
static void replace_vars_in_jointree (Node *jtnode, pullup_replace_vars_context *context, JoinExpr *lowest_nulling_outer_join)
 
static Nodepullup_replace_vars (Node *expr, pullup_replace_vars_context *context)
 
static Nodepullup_replace_vars_callback (Var *var, replace_rte_variables_context *context)
 
static Querypullup_replace_vars_subquery (Query *query, pullup_replace_vars_context *context)
 
static Nodepull_up_subqueries_cleanup (Node *jtnode)
 
static reduce_outer_joins_statereduce_outer_joins_pass1 (Node *jtnode)
 
static void reduce_outer_joins_pass2 (Node *jtnode, reduce_outer_joins_state *state, PlannerInfo *root, Relids nonnullable_rels, List *nonnullable_vars, List *forced_null_vars)
 
static void substitute_multiple_relids (Node *node, int varno, Relids subrelids)
 
static void fix_append_rel_relids (List *append_rel_list, int varno, Relids subrelids)
 
static Nodefind_jointree_node_for_rel (Node *jtnode, int relid)
 
void pull_up_sublinks (PlannerInfo *root)
 
void inline_set_returning_functions (PlannerInfo *root)
 
void pull_up_subqueries (PlannerInfo *root)
 
void flatten_simple_union_all (PlannerInfo *root)
 
void reduce_outer_joins (PlannerInfo *root)
 
static bool substitute_multiple_relids_walker (Node *node, substitute_multiple_relids_context *context)
 
Relids get_relids_in_jointree (Node *jtnode, bool include_joins)
 
Relids get_relids_for_join (PlannerInfo *root, int joinrelid)
 

Typedef Documentation

◆ pullup_replace_vars_context

◆ reduce_outer_joins_state

Function Documentation

◆ find_jointree_node_for_rel()

static Node * find_jointree_node_for_rel ( Node jtnode,
int  relid 
)
static

Definition at line 3036 of file prepjointree.c.

References elog, ERROR, FromExpr::fromlist, IsA, JoinExpr::larg, lfirst, nodeTag, JoinExpr::rarg, JoinExpr::rtindex, and pullup_replace_vars_context::varno.

Referenced by get_relids_for_join().

3037 {
3038  if (jtnode == NULL)
3039  return NULL;
3040  if (IsA(jtnode, RangeTblRef))
3041  {
3042  int varno = ((RangeTblRef *) jtnode)->rtindex;
3043 
3044  if (relid == varno)
3045  return jtnode;
3046  }
3047  else if (IsA(jtnode, FromExpr))
3048  {
3049  FromExpr *f = (FromExpr *) jtnode;
3050  ListCell *l;
3051 
3052  foreach(l, f->fromlist)
3053  {
3054  jtnode = find_jointree_node_for_rel(lfirst(l), relid);
3055  if (jtnode)
3056  return jtnode;
3057  }
3058  }
3059  else if (IsA(jtnode, JoinExpr))
3060  {
3061  JoinExpr *j = (JoinExpr *) jtnode;
3062 
3063  if (relid == j->rtindex)
3064  return jtnode;
3065  jtnode = find_jointree_node_for_rel(j->larg, relid);
3066  if (jtnode)
3067  return jtnode;
3068  jtnode = find_jointree_node_for_rel(j->rarg, relid);
3069  if (jtnode)
3070  return jtnode;
3071  }
3072  else
3073  elog(ERROR, "unrecognized node type: %d",
3074  (int) nodeTag(jtnode));
3075  return NULL;
3076 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
List * fromlist
Definition: primnodes.h:1479
Node * larg
Definition: primnodes.h:1459
static Node * find_jointree_node_for_rel(Node *jtnode, int relid)
#define ERROR
Definition: elog.h:43
Node * rarg
Definition: primnodes.h:1460
#define lfirst(lc)
Definition: pg_list.h:106
#define nodeTag(nodeptr)
Definition: nodes.h:522
#define elog
Definition: elog.h:219
int rtindex
Definition: primnodes.h:1464

◆ fix_append_rel_relids()

static void fix_append_rel_relids ( List append_rel_list,
int  varno,
Relids  subrelids 
)
static

Definition at line 2937 of file prepjointree.c.

References Assert, bms_singleton_member(), AppendRelInfo::child_relid, lfirst, AppendRelInfo::parent_relid, substitute_multiple_relids(), and AppendRelInfo::translated_vars.

Referenced by pull_up_simple_subquery().

2938 {
2939  ListCell *l;
2940  int subvarno = -1;
2941 
2942  /*
2943  * We only want to extract the member relid once, but we mustn't fail
2944  * immediately if there are multiple members; it could be that none of the
2945  * AppendRelInfo nodes refer to it. So compute it on first use. Note that
2946  * bms_singleton_member will complain if set is not singleton.
2947  */
2948  foreach(l, append_rel_list)
2949  {
2950  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
2951 
2952  /* The parent_relid shouldn't ever be a pullup target */
2953  Assert(appinfo->parent_relid != varno);
2954 
2955  if (appinfo->child_relid == varno)
2956  {
2957  if (subvarno < 0)
2958  subvarno = bms_singleton_member(subrelids);
2959  appinfo->child_relid = subvarno;
2960  }
2961 
2962  /* Also finish fixups for its translated vars */
2964  varno, subrelids);
2965  }
2966 }
Definition: nodes.h:517
List * translated_vars
Definition: relation.h:2152
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:592
static void substitute_multiple_relids(Node *node, int varno, Relids subrelids)
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
Index child_relid
Definition: relation.h:2125
Index parent_relid
Definition: relation.h:2124

◆ flatten_simple_union_all()

void flatten_simple_union_all ( PlannerInfo root)

Definition at line 2353 of file prepjointree.c.

References Assert, castNode, copyObject, FromExpr::fromlist, PlannerInfo::hasRecursion, RangeTblEntry::inh, is_simple_union_all_recurse(), IsA, Query::jointree, lappend(), list_length(), list_make1, makeNode, NIL, parse(), PlannerInfo::parse, pull_up_union_leaf_queries(), rt_fetch, Query::rtable, RTE_SUBQUERY, RangeTblEntry::rtekind, RangeTblRef::rtindex, and Query::setOperations.

Referenced by subquery_planner().

2354 {
2355  Query *parse = root->parse;
2356  SetOperationStmt *topop;
2357  Node *leftmostjtnode;
2358  int leftmostRTI;
2359  RangeTblEntry *leftmostRTE;
2360  int childRTI;
2361  RangeTblEntry *childRTE;
2362  RangeTblRef *rtr;
2363 
2364  /* Shouldn't be called unless query has setops */
2365  topop = castNode(SetOperationStmt, parse->setOperations);
2366  Assert(topop);
2367 
2368  /* Can't optimize away a recursive UNION */
2369  if (root->hasRecursion)
2370  return;
2371 
2372  /*
2373  * Recursively check the tree of set operations. If not all UNION ALL
2374  * with identical column types, punt.
2375  */
2376  if (!is_simple_union_all_recurse((Node *) topop, parse, topop->colTypes))
2377  return;
2378 
2379  /*
2380  * Locate the leftmost leaf query in the setops tree. The upper query's
2381  * Vars all refer to this RTE (see transformSetOperationStmt).
2382  */
2383  leftmostjtnode = topop->larg;
2384  while (leftmostjtnode && IsA(leftmostjtnode, SetOperationStmt))
2385  leftmostjtnode = ((SetOperationStmt *) leftmostjtnode)->larg;
2386  Assert(leftmostjtnode && IsA(leftmostjtnode, RangeTblRef));
2387  leftmostRTI = ((RangeTblRef *) leftmostjtnode)->rtindex;
2388  leftmostRTE = rt_fetch(leftmostRTI, parse->rtable);
2389  Assert(leftmostRTE->rtekind == RTE_SUBQUERY);
2390 
2391  /*
2392  * Make a copy of the leftmost RTE and add it to the rtable. This copy
2393  * will represent the leftmost leaf query in its capacity as a member of
2394  * the appendrel. The original will represent the appendrel as a whole.
2395  * (We must do things this way because the upper query's Vars have to be
2396  * seen as referring to the whole appendrel.)
2397  */
2398  childRTE = copyObject(leftmostRTE);
2399  parse->rtable = lappend(parse->rtable, childRTE);
2400  childRTI = list_length(parse->rtable);
2401 
2402  /* Modify the setops tree to reference the child copy */
2403  ((RangeTblRef *) leftmostjtnode)->rtindex = childRTI;
2404 
2405  /* Modify the formerly-leftmost RTE to mark it as an appendrel parent */
2406  leftmostRTE->inh = true;
2407 
2408  /*
2409  * Form a RangeTblRef for the appendrel, and insert it into FROM. The top
2410  * Query of a setops tree should have had an empty FromClause initially.
2411  */
2412  rtr = makeNode(RangeTblRef);
2413  rtr->rtindex = leftmostRTI;
2414  Assert(parse->jointree->fromlist == NIL);
2415  parse->jointree->fromlist = list_make1(rtr);
2416 
2417  /*
2418  * Now pretend the query has no setops. We must do this before trying to
2419  * do subquery pullup, because of Assert in pull_up_simple_subquery.
2420  */
2421  parse->setOperations = NULL;
2422 
2423  /*
2424  * Build AppendRelInfo information, and apply pull_up_subqueries to the
2425  * leaf queries of the UNION ALL. (We must do that now because they
2426  * weren't previously referenced by the jointree, and so were missed by
2427  * the main invocation of pull_up_subqueries.)
2428  */
2429  pull_up_union_leaf_queries((Node *) topop, root, leftmostRTI, parse, 0);
2430 }
#define NIL
Definition: pg_list.h:69
static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex, Query *setOpQuery, int childRToffset)
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Query * parse
Definition: relation.h:169
FromExpr * jointree
Definition: parsenodes.h:138
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
Definition: nodes.h:517
static bool is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
List * fromlist
Definition: primnodes.h:1479
bool hasRecursion
Definition: relation.h:321
#define list_make1(x1)
Definition: pg_list.h:139
List * rtable
Definition: parsenodes.h:137
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
List * lappend(List *list, void *datum)
Definition: list.c:128
#define makeNode(_type_)
Definition: nodes.h:565
#define Assert(condition)
Definition: c.h:699
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:962
Node * setOperations
Definition: parsenodes.h:165
#define copyObject(obj)
Definition: nodes.h:630
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:649

◆ get_relids_for_join()

Relids get_relids_for_join ( PlannerInfo root,
int  joinrelid 
)

Definition at line 3019 of file prepjointree.c.

References elog, ERROR, find_jointree_node_for_rel(), get_relids_in_jointree(), Query::jointree, and PlannerInfo::parse.

Referenced by alias_relid_set().

3020 {
3021  Node *jtnode;
3022 
3023  jtnode = find_jointree_node_for_rel((Node *) root->parse->jointree,
3024  joinrelid);
3025  if (!jtnode)
3026  elog(ERROR, "could not find join node %d", joinrelid);
3027  return get_relids_in_jointree(jtnode, false);
3028 }
Query * parse
Definition: relation.h:169
FromExpr * jointree
Definition: parsenodes.h:138
Definition: nodes.h:517
static Node * find_jointree_node_for_rel(Node *jtnode, int relid)
#define ERROR
Definition: elog.h:43
Relids get_relids_in_jointree(Node *jtnode, bool include_joins)
#define elog
Definition: elog.h:219

◆ get_relids_in_jointree()

Relids get_relids_in_jointree ( Node jtnode,
bool  include_joins 
)

Definition at line 2975 of file prepjointree.c.

References bms_add_member(), bms_join(), bms_make_singleton(), elog, ERROR, FromExpr::fromlist, get_relids_in_jointree(), IsA, JoinExpr::larg, lfirst, nodeTag, JoinExpr::rarg, JoinExpr::rtindex, and pullup_replace_vars_context::varno.

Referenced by distribute_qual_to_rels(), get_relids_for_join(), get_relids_in_jointree(), is_simple_subquery(), preprocess_rowmarks(), and pull_up_simple_subquery().

2976 {
2977  Relids result = NULL;
2978 
2979  if (jtnode == NULL)
2980  return result;
2981  if (IsA(jtnode, RangeTblRef))
2982  {
2983  int varno = ((RangeTblRef *) jtnode)->rtindex;
2984 
2985  result = bms_make_singleton(varno);
2986  }
2987  else if (IsA(jtnode, FromExpr))
2988  {
2989  FromExpr *f = (FromExpr *) jtnode;
2990  ListCell *l;
2991 
2992  foreach(l, f->fromlist)
2993  {
2994  result = bms_join(result,
2996  include_joins));
2997  }
2998  }
2999  else if (IsA(jtnode, JoinExpr))
3000  {
3001  JoinExpr *j = (JoinExpr *) jtnode;
3002 
3003  result = get_relids_in_jointree(j->larg, include_joins);
3004  result = bms_join(result,
3005  get_relids_in_jointree(j->rarg, include_joins));
3006  if (include_joins && j->rtindex)
3007  result = bms_add_member(result, j->rtindex);
3008  }
3009  else
3010  elog(ERROR, "unrecognized node type: %d",
3011  (int) nodeTag(jtnode));
3012  return result;
3013 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
List * fromlist
Definition: primnodes.h:1479
Node * larg
Definition: primnodes.h:1459
#define ERROR
Definition: elog.h:43
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:976
Relids get_relids_in_jointree(Node *jtnode, bool include_joins)
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:245
Node * rarg
Definition: primnodes.h:1460
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:764
#define nodeTag(nodeptr)
Definition: nodes.h:522
#define elog
Definition: elog.h:219
int rtindex
Definition: primnodes.h:1464

◆ inline_set_returning_functions()

void inline_set_returning_functions ( PlannerInfo root)

Definition at line 573 of file prepjointree.c.

References RangeTblEntry::functions, inline_set_returning_function(), lfirst, NIL, PlannerInfo::parse, Query::rtable, RTE_FUNCTION, RTE_SUBQUERY, RangeTblEntry::rtekind, and RangeTblEntry::subquery.

Referenced by pull_up_simple_subquery(), and subquery_planner().

574 {
575  ListCell *rt;
576 
577  foreach(rt, root->parse->rtable)
578  {
579  RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
580 
581  if (rte->rtekind == RTE_FUNCTION)
582  {
583  Query *funcquery;
584 
585  /* Check safety of expansion, and expand if possible */
586  funcquery = inline_set_returning_function(root, rte);
587  if (funcquery)
588  {
589  /* Successful expansion, replace the rtable entry */
590  rte->rtekind = RTE_SUBQUERY;
591  rte->subquery = funcquery;
592  rte->functions = NIL;
593  }
594  }
595  }
596 }
#define NIL
Definition: pg_list.h:69
Query * parse
Definition: relation.h:169
Query * inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
Definition: clauses.c:4932
List * rtable
Definition: parsenodes.h:137
#define lfirst(lc)
Definition: pg_list.h:106
List * functions
Definition: parsenodes.h:1016
RTEKind rtekind
Definition: parsenodes.h:962
Query * subquery
Definition: parsenodes.h:985

◆ is_safe_append_member()

static bool is_safe_append_member ( Query subquery)
static

Definition at line 1843 of file prepjointree.c.

References FromExpr::fromlist, IsA, Query::jointree, linitial, list_length(), and FromExpr::quals.

Referenced by pull_up_simple_subquery(), and pull_up_subqueries_recurse().

1844 {
1845  FromExpr *jtnode;
1846 
1847  /*
1848  * It's only safe to pull up the child if its jointree contains exactly
1849  * one RTE, else the AppendRelInfo data structure breaks. The one base RTE
1850  * could be buried in several levels of FromExpr, however.
1851  *
1852  * Also, the child can't have any WHERE quals because there's no place to
1853  * put them in an appendrel. (This is a bit annoying...) If we didn't
1854  * need to check this, we'd just test whether get_relids_in_jointree()
1855  * yields a singleton set, to be more consistent with the coding of
1856  * fix_append_rel_relids().
1857  */
1858  jtnode = subquery->jointree;
1859  while (IsA(jtnode, FromExpr))
1860  {
1861  if (jtnode->quals != NULL)
1862  return false;
1863  if (list_length(jtnode->fromlist) != 1)
1864  return false;
1865  jtnode = linitial(jtnode->fromlist);
1866  }
1867  if (!IsA(jtnode, RangeTblRef))
1868  return false;
1869 
1870  return true;
1871 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
FromExpr * jointree
Definition: parsenodes.h:138
List * fromlist
Definition: primnodes.h:1479
Node * quals
Definition: primnodes.h:1480
#define linitial(l)
Definition: pg_list.h:111
static int list_length(const List *l)
Definition: pg_list.h:89

◆ is_simple_subquery()

static bool is_simple_subquery ( Query subquery,
RangeTblEntry rte,
JoinExpr lowest_outer_join,
bool  deletion_ok 
)
static

Definition at line 1439 of file prepjointree.c.

References bms_is_subset(), CMD_SELECT, Query::commandType, contain_volatile_functions(), Query::cteList, Query::distinctClause, elog, ERROR, FromExpr::fromlist, get_relids_in_jointree(), Query::groupClause, Query::groupingSets, Query::hasAggs, Query::hasForUpdate, Query::hasTargetSRFs, Query::hasWindowFuncs, Query::havingQual, IsA, Query::jointree, jointree_contains_lateral_outer_refs(), RangeTblEntry::lateral, Query::limitCount, Query::limitOffset, NIL, pull_varnos_of_level(), FromExpr::quals, RangeTblEntry::security_barrier, Query::setOperations, Query::sortClause, and Query::targetList.

Referenced by pull_up_simple_subquery(), and pull_up_subqueries_recurse().

1442 {
1443  /*
1444  * Let's just make sure it's a valid subselect ...
1445  */
1446  if (!IsA(subquery, Query) ||
1447  subquery->commandType != CMD_SELECT)
1448  elog(ERROR, "subquery is bogus");
1449 
1450  /*
1451  * Can't currently pull up a query with setops (unless it's simple UNION
1452  * ALL, which is handled by a different code path). Maybe after querytree
1453  * redesign...
1454  */
1455  if (subquery->setOperations)
1456  return false;
1457 
1458  /*
1459  * Can't pull up a subquery involving grouping, aggregation, SRFs,
1460  * sorting, limiting, or WITH. (XXX WITH could possibly be allowed later)
1461  *
1462  * We also don't pull up a subquery that has explicit FOR UPDATE/SHARE
1463  * clauses, because pullup would cause the locking to occur semantically
1464  * higher than it should. Implicit FOR UPDATE/SHARE is okay because in
1465  * that case the locking was originally declared in the upper query
1466  * anyway.
1467  */
1468  if (subquery->hasAggs ||
1469  subquery->hasWindowFuncs ||
1470  subquery->hasTargetSRFs ||
1471  subquery->groupClause ||
1472  subquery->groupingSets ||
1473  subquery->havingQual ||
1474  subquery->sortClause ||
1475  subquery->distinctClause ||
1476  subquery->limitOffset ||
1477  subquery->limitCount ||
1478  subquery->hasForUpdate ||
1479  subquery->cteList)
1480  return false;
1481 
1482  /*
1483  * Don't pull up if the RTE represents a security-barrier view; we
1484  * couldn't prevent information leakage once the RTE's Vars are scattered
1485  * about in the upper query.
1486  */
1487  if (rte->security_barrier)
1488  return false;
1489 
1490  /*
1491  * Don't pull up a subquery with an empty jointree, unless it has no quals
1492  * and deletion_ok is true and we're not underneath an outer join.
1493  *
1494  * query_planner() will correctly generate a Result plan for a jointree
1495  * that's totally empty, but we can't cope with an empty FromExpr
1496  * appearing lower down in a jointree: we identify join rels via baserelid
1497  * sets, so we couldn't distinguish a join containing such a FromExpr from
1498  * one without it. We can only handle such cases if the place where the
1499  * subquery is linked is a FromExpr or inner JOIN that would still be
1500  * nonempty after removal of the subquery, so that it's still identifiable
1501  * via its contained baserelids. Safe contexts are signaled by
1502  * deletion_ok.
1503  *
1504  * But even in a safe context, we must keep the subquery if it has any
1505  * quals, because it's unclear where to put them in the upper query.
1506  *
1507  * Also, we must forbid pullup if such a subquery is underneath an outer
1508  * join, because then we might need to wrap its output columns with
1509  * PlaceHolderVars, and the PHVs would then have empty relid sets meaning
1510  * we couldn't tell where to evaluate them. (This test is separate from
1511  * the deletion_ok flag for possible future expansion: deletion_ok tells
1512  * whether the immediate parent site in the jointree could cope, not
1513  * whether we'd have PHV issues. It's possible this restriction could be
1514  * fixed by letting the PHVs use the relids of the parent jointree item,
1515  * but that complication is for another day.)
1516  *
1517  * Note that deletion of a subquery is also dependent on the check below
1518  * that its targetlist contains no set-returning functions. Deletion from
1519  * a FROM list or inner JOIN is okay only if the subquery must return
1520  * exactly one row.
1521  */
1522  if (subquery->jointree->fromlist == NIL &&
1523  (subquery->jointree->quals != NULL ||
1524  !deletion_ok ||
1525  lowest_outer_join != NULL))
1526  return false;
1527 
1528  /*
1529  * If the subquery is LATERAL, check for pullup restrictions from that.
1530  */
1531  if (rte->lateral)
1532  {
1533  bool restricted;
1534  Relids safe_upper_varnos;
1535 
1536  /*
1537  * The subquery's WHERE and JOIN/ON quals mustn't contain any lateral
1538  * references to rels outside a higher outer join (including the case
1539  * where the outer join is within the subquery itself). In such a
1540  * case, pulling up would result in a situation where we need to
1541  * postpone quals from below an outer join to above it, which is
1542  * probably completely wrong and in any case is a complication that
1543  * doesn't seem worth addressing at the moment.
1544  */
1545  if (lowest_outer_join != NULL)
1546  {
1547  restricted = true;
1548  safe_upper_varnos = get_relids_in_jointree((Node *) lowest_outer_join,
1549  true);
1550  }
1551  else
1552  {
1553  restricted = false;
1554  safe_upper_varnos = NULL; /* doesn't matter */
1555  }
1556 
1558  restricted, safe_upper_varnos))
1559  return false;
1560 
1561  /*
1562  * If there's an outer join above the LATERAL subquery, also disallow
1563  * pullup if the subquery's targetlist has any references to rels
1564  * outside the outer join, since these might get pulled into quals
1565  * above the subquery (but in or below the outer join) and then lead
1566  * to qual-postponement issues similar to the case checked for above.
1567  * (We wouldn't need to prevent pullup if no such references appear in
1568  * outer-query quals, but we don't have enough info here to check
1569  * that. Also, maybe this restriction could be removed if we forced
1570  * such refs to be wrapped in PlaceHolderVars, even when they're below
1571  * the nearest outer join? But it's a pretty hokey usage, so not
1572  * clear this is worth sweating over.)
1573  */
1574  if (lowest_outer_join != NULL)
1575  {
1576  Relids lvarnos = pull_varnos_of_level((Node *) subquery->targetList, 1);
1577 
1578  if (!bms_is_subset(lvarnos, safe_upper_varnos))
1579  return false;
1580  }
1581  }
1582 
1583  /*
1584  * Don't pull up a subquery that has any volatile functions in its
1585  * targetlist. Otherwise we might introduce multiple evaluations of these
1586  * functions, if they get copied to multiple places in the upper query,
1587  * leading to surprising results. (Note: the PlaceHolderVar mechanism
1588  * doesn't quite guarantee single evaluation; else we could pull up anyway
1589  * and just wrap such items in PlaceHolderVars ...)
1590  */
1591  if (contain_volatile_functions((Node *) subquery->targetList))
1592  return false;
1593 
1594  return true;
1595 }
Node * limitOffset
Definition: parsenodes.h:160
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
List * sortClause
Definition: parsenodes.h:158
FromExpr * jointree
Definition: parsenodes.h:138
bool hasAggs
Definition: parsenodes.h:125
List * groupingSets
Definition: parsenodes.h:150
Definition: nodes.h:517
List * fromlist
Definition: primnodes.h:1479
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:958
Node * quals
Definition: primnodes.h:1480
List * targetList
Definition: parsenodes.h:140
List * distinctClause
Definition: parsenodes.h:156
#define ERROR
Definition: elog.h:43
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:374
Relids get_relids_in_jointree(Node *jtnode, bool include_joins)
Node * limitCount
Definition: parsenodes.h:161
static bool jointree_contains_lateral_outer_refs(Node *jtnode, bool restricted, Relids safe_upper_varnos)
bool security_barrier
Definition: parsenodes.h:986
CmdType commandType
Definition: parsenodes.h:112
bool hasTargetSRFs
Definition: parsenodes.h:127
bool hasWindowFuncs
Definition: parsenodes.h:126
List * cteList
Definition: parsenodes.h:135
Node * setOperations
Definition: parsenodes.h:165
List * groupClause
Definition: parsenodes.h:148
bool hasForUpdate
Definition: parsenodes.h:132
#define elog
Definition: elog.h:219
Node * havingQual
Definition: parsenodes.h:152
Relids pull_varnos_of_level(Node *node, int levelsup)
Definition: var.c:120

◆ is_simple_union_all()

static bool is_simple_union_all ( Query subquery)
static

Definition at line 1775 of file prepjointree.c.

References castNode, CMD_SELECT, Query::commandType, Query::cteList, elog, ERROR, is_simple_union_all_recurse(), IsA, Query::limitCount, Query::limitOffset, Query::rowMarks, Query::setOperations, and Query::sortClause.

Referenced by pull_up_subqueries_recurse().

1776 {
1777  SetOperationStmt *topop;
1778 
1779  /* Let's just make sure it's a valid subselect ... */
1780  if (!IsA(subquery, Query) ||
1781  subquery->commandType != CMD_SELECT)
1782  elog(ERROR, "subquery is bogus");
1783 
1784  /* Is it a set-operation query at all? */
1785  topop = castNode(SetOperationStmt, subquery->setOperations);
1786  if (!topop)
1787  return false;
1788 
1789  /* Can't handle ORDER BY, LIMIT/OFFSET, locking, or WITH */
1790  if (subquery->sortClause ||
1791  subquery->limitOffset ||
1792  subquery->limitCount ||
1793  subquery->rowMarks ||
1794  subquery->cteList)
1795  return false;
1796 
1797  /* Recursively check the tree of set operations */
1798  return is_simple_union_all_recurse((Node *) topop, subquery,
1799  topop->colTypes);
1800 }
Node * limitOffset
Definition: parsenodes.h:160
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
List * sortClause
Definition: parsenodes.h:158
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
Definition: nodes.h:517
static bool is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
List * rowMarks
Definition: parsenodes.h:163
#define ERROR
Definition: elog.h:43
Node * limitCount
Definition: parsenodes.h:161
CmdType commandType
Definition: parsenodes.h:112
List * cteList
Definition: parsenodes.h:135
Node * setOperations
Definition: parsenodes.h:165
#define elog
Definition: elog.h:219

◆ is_simple_union_all_recurse()

static bool is_simple_union_all_recurse ( Node setOp,
Query setOpQuery,
List colTypes 
)
static

Definition at line 1803 of file prepjointree.c.

References SetOperationStmt::all, Assert, elog, ERROR, IsA, SetOperationStmt::larg, nodeTag, SetOperationStmt::op, SetOperationStmt::rarg, rt_fetch, Query::rtable, RangeTblRef::rtindex, SETOP_UNION, RangeTblEntry::subquery, Query::targetList, and tlist_same_datatypes().

Referenced by flatten_simple_union_all(), and is_simple_union_all().

1804 {
1805  if (IsA(setOp, RangeTblRef))
1806  {
1807  RangeTblRef *rtr = (RangeTblRef *) setOp;
1808  RangeTblEntry *rte = rt_fetch(rtr->rtindex, setOpQuery->rtable);
1809  Query *subquery = rte->subquery;
1810 
1811  Assert(subquery != NULL);
1812 
1813  /* Leaf nodes are OK if they match the toplevel column types */
1814  /* We don't have to compare typmods or collations here */
1815  return tlist_same_datatypes(subquery->targetList, colTypes, true);
1816  }
1817  else if (IsA(setOp, SetOperationStmt))
1818  {
1819  SetOperationStmt *op = (SetOperationStmt *) setOp;
1820 
1821  /* Must be UNION ALL */
1822  if (op->op != SETOP_UNION || !op->all)
1823  return false;
1824 
1825  /* Recurse to check inputs */
1826  return is_simple_union_all_recurse(op->larg, setOpQuery, colTypes) &&
1827  is_simple_union_all_recurse(op->rarg, setOpQuery, colTypes);
1828  }
1829  else
1830  {
1831  elog(ERROR, "unrecognized node type: %d",
1832  (int) nodeTag(setOp));
1833  return false; /* keep compiler quiet */
1834  }
1835 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
bool tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK)
Definition: tlist.c:251
static bool is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
List * targetList
Definition: parsenodes.h:140
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:43
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define Assert(condition)
Definition: c.h:699
SetOperation op
Definition: parsenodes.h:1599
#define nodeTag(nodeptr)
Definition: nodes.h:522
Query * subquery
Definition: parsenodes.h:985
#define elog
Definition: elog.h:219

◆ is_simple_values()

static bool is_simple_values ( PlannerInfo root,
RangeTblEntry rte,
bool  deletion_ok 
)
static

Definition at line 1719 of file prepjointree.c.

References Assert, contain_volatile_functions(), expression_returns_set(), linitial, list_length(), PlannerInfo::parse, Query::rtable, RTE_VALUES, RangeTblEntry::rtekind, and RangeTblEntry::values_lists.

Referenced by pull_up_subqueries_recurse().

1720 {
1721  Assert(rte->rtekind == RTE_VALUES);
1722 
1723  /*
1724  * We can only pull up a VALUES RTE if deletion_ok is true. It's
1725  * basically the same case as a sub-select with empty FROM list; see
1726  * comments in is_simple_subquery().
1727  */
1728  if (!deletion_ok)
1729  return false;
1730 
1731  /*
1732  * Also, there must be exactly one VALUES list, else it's not semantically
1733  * correct to delete the VALUES RTE.
1734  */
1735  if (list_length(rte->values_lists) != 1)
1736  return false;
1737 
1738  /*
1739  * Because VALUES can't appear under an outer join (or at least, we won't
1740  * try to pull it up if it does), we need not worry about LATERAL, nor
1741  * about validity of PHVs for the VALUES' outputs.
1742  */
1743 
1744  /*
1745  * Don't pull up a VALUES that contains any set-returning or volatile
1746  * functions. Again, the considerations here are basically identical to
1747  * restrictions on a subquery's targetlist.
1748  */
1749  if (expression_returns_set((Node *) rte->values_lists) ||
1751  return false;
1752 
1753  /*
1754  * Do not pull up a VALUES that's not the only RTE in its parent query.
1755  * This is actually the only case that the parser will generate at the
1756  * moment, and assuming this is true greatly simplifies
1757  * pull_up_simple_values().
1758  */
1759  if (list_length(root->parse->rtable) != 1 ||
1760  rte != (RangeTblEntry *) linitial(root->parse->rtable))
1761  return false;
1762 
1763  return true;
1764 }
Query * parse
Definition: relation.h:169
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:670
Definition: nodes.h:517
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:958
List * values_lists
Definition: parsenodes.h:1027
#define linitial(l)
Definition: pg_list.h:111
List * rtable
Definition: parsenodes.h:137
#define Assert(condition)
Definition: c.h:699
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:962

◆ jointree_contains_lateral_outer_refs()

static bool jointree_contains_lateral_outer_refs ( Node jtnode,
bool  restricted,
Relids  safe_upper_varnos 
)
static

Definition at line 1884 of file prepjointree.c.

References bms_is_subset(), elog, ERROR, FromExpr::fromlist, IsA, JOIN_INNER, JoinExpr::jointype, JoinExpr::larg, lfirst, nodeTag, pull_varnos_of_level(), JoinExpr::quals, FromExpr::quals, and JoinExpr::rarg.

Referenced by is_simple_subquery().

1886 {
1887  if (jtnode == NULL)
1888  return false;
1889  if (IsA(jtnode, RangeTblRef))
1890  return false;
1891  else if (IsA(jtnode, FromExpr))
1892  {
1893  FromExpr *f = (FromExpr *) jtnode;
1894  ListCell *l;
1895 
1896  /* First, recurse to check child joins */
1897  foreach(l, f->fromlist)
1898  {
1900  restricted,
1901  safe_upper_varnos))
1902  return true;
1903  }
1904 
1905  /* Then check the top-level quals */
1906  if (restricted &&
1908  safe_upper_varnos))
1909  return true;
1910  }
1911  else if (IsA(jtnode, JoinExpr))
1912  {
1913  JoinExpr *j = (JoinExpr *) jtnode;
1914 
1915  /*
1916  * If this is an outer join, we mustn't allow any upper lateral
1917  * references in or below it.
1918  */
1919  if (j->jointype != JOIN_INNER)
1920  {
1921  restricted = true;
1922  safe_upper_varnos = NULL;
1923  }
1924 
1925  /* Check the child joins */
1927  restricted,
1928  safe_upper_varnos))
1929  return true;
1931  restricted,
1932  safe_upper_varnos))
1933  return true;
1934 
1935  /* Check the JOIN's qual clauses */
1936  if (restricted &&
1938  safe_upper_varnos))
1939  return true;
1940  }
1941  else
1942  elog(ERROR, "unrecognized node type: %d",
1943  (int) nodeTag(jtnode));
1944  return false;
1945 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
List * fromlist
Definition: primnodes.h:1479
Node * quals
Definition: primnodes.h:1480
Node * larg
Definition: primnodes.h:1459
#define ERROR
Definition: elog.h:43
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:374
Node * quals
Definition: primnodes.h:1462
static bool jointree_contains_lateral_outer_refs(Node *jtnode, bool restricted, Relids safe_upper_varnos)
Node * rarg
Definition: primnodes.h:1460
JoinType jointype
Definition: primnodes.h:1457
#define lfirst(lc)
Definition: pg_list.h:106
#define nodeTag(nodeptr)
Definition: nodes.h:522
#define elog
Definition: elog.h:219
Relids pull_varnos_of_level(Node *node, int levelsup)
Definition: var.c:120

◆ make_setop_translation_list()

static void make_setop_translation_list ( Query query,
Index  newvarno,
List **  translated_vars 
)
static

Definition at line 1408 of file prepjointree.c.

References lappend(), lfirst, makeVarFromTargetEntry(), NIL, TargetEntry::resjunk, and Query::targetList.

Referenced by pull_up_union_leaf_queries().

1410 {
1411  List *vars = NIL;
1412  ListCell *l;
1413 
1414  foreach(l, query->targetList)
1415  {
1416  TargetEntry *tle = (TargetEntry *) lfirst(l);
1417 
1418  if (tle->resjunk)
1419  continue;
1420 
1421  vars = lappend(vars, makeVarFromTargetEntry(newvarno, tle));
1422  }
1423 
1424  *translated_vars = vars;
1425 }
#define NIL
Definition: pg_list.h:69
Var * makeVarFromTargetEntry(Index varno, TargetEntry *tle)
Definition: makefuncs.c:104
List * targetList
Definition: parsenodes.h:140
bool resjunk
Definition: primnodes.h:1383
List * lappend(List *list, void *datum)
Definition: list.c:128
#define lfirst(lc)
Definition: pg_list.h:106
Definition: regcomp.c:224
Definition: pg_list.h:45

◆ pull_up_simple_subquery()

static Node * pull_up_simple_subquery ( PlannerInfo root,
Node jtnode,
RangeTblEntry rte,
JoinExpr lowest_outer_join,
JoinExpr lowest_nulling_outer_join,
AppendRelInfo containing_appendrel,
bool  deletion_ok 
)
static

Definition at line 868 of file prepjointree.c.

References PlannerInfo::append_rel_list, Assert, copyObject, PlannerInfo::cte_plan_ids, Query::cteList, CurrentMemoryContext, PlannerInfo::eq_classes, fix_append_rel_relids(), flatten_join_alias_vars(), get_relids_in_jointree(), PlannerInfo::glob, PlannerInfo::grouping_map, Query::groupingSets, PlannerInfo::hasDeletedRTEs, PlannerInfo::hasRecursion, Query::hasRowSecurity, Query::hasSubLinks, Query::havingQual, IncrementVarSublevelsUp(), INHKIND_NONE, PlannerInfo::inhTargetKind, PlannerInfo::init_plans, inline_set_returning_functions(), is_safe_append_member(), is_simple_subquery(), PlannerInfo::join_info_list, RangeTblEntry::joinaliasvars, Query::jointree, PlannerGlobal::lastPHId, RangeTblEntry::lateral, lfirst, list_concat(), list_length(), makeNode, PlannerInfo::minmax_aggs, PlannerInfo::multiexpr_params, pullup_replace_vars_context::need_phvs, NIL, PlannerInfo::non_recursive_path, OffsetVarNodes(), Query::onConflict, OnConflictExpr::onConflictSet, OnConflictExpr::onConflictWhere, pullup_replace_vars_context::outer_hasSubLinks, PlannerInfo::outer_params, palloc0(), PlannerInfo::parent_root, parse(), PlannerInfo::parse, PlannerInfo::placeholder_list, PlannerInfo::plan_params, PlannerInfo::planner_cxt, PlannerInfo::processed_tlist, pull_up_sublinks(), pull_up_subqueries(), pullup_replace_vars(), PlannerInfo::qual_security_level, PlannerInfo::query_level, pullup_replace_vars_context::relids, replace_vars_in_jointree(), Query::returningList, pullup_replace_vars_context::root, Query::rowMarks, PlannerInfo::rowMarks, Query::rtable, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, pullup_replace_vars_context::rv_cache, Query::setOperations, RangeTblEntry::subquery, substitute_multiple_relids(), RangeTblEntry::tablesample, pullup_replace_vars_context::target_rte, pullup_replace_vars_context::targetlist, Query::targetList, AppendRelInfo::translated_vars, PlannerInfo::upper_rels, PlannerInfo::upper_targets, pullup_replace_vars_context::varno, pullup_replace_vars_context::wrap_non_vars, and PlannerInfo::wt_param_id.

Referenced by pull_up_subqueries_recurse().

873 {
874  Query *parse = root->parse;
875  int varno = ((RangeTblRef *) jtnode)->rtindex;
876  Query *subquery;
877  PlannerInfo *subroot;
878  int rtoffset;
879  pullup_replace_vars_context rvcontext;
880  ListCell *lc;
881 
882  /*
883  * Need a modifiable copy of the subquery to hack on. Even if we didn't
884  * sometimes choose not to pull up below, we must do this to avoid
885  * problems if the same subquery is referenced from multiple jointree
886  * items (which can't happen normally, but might after rule rewriting).
887  */
888  subquery = copyObject(rte->subquery);
889 
890  /*
891  * Create a PlannerInfo data structure for this subquery.
892  *
893  * NOTE: the next few steps should match the first processing in
894  * subquery_planner(). Can we refactor to avoid code duplication, or
895  * would that just make things uglier?
896  */
897  subroot = makeNode(PlannerInfo);
898  subroot->parse = subquery;
899  subroot->glob = root->glob;
900  subroot->query_level = root->query_level;
901  subroot->parent_root = root->parent_root;
902  subroot->plan_params = NIL;
903  subroot->outer_params = NULL;
905  subroot->init_plans = NIL;
906  subroot->cte_plan_ids = NIL;
907  subroot->multiexpr_params = NIL;
908  subroot->eq_classes = NIL;
909  subroot->append_rel_list = NIL;
910  subroot->rowMarks = NIL;
911  memset(subroot->upper_rels, 0, sizeof(subroot->upper_rels));
912  memset(subroot->upper_targets, 0, sizeof(subroot->upper_targets));
913  subroot->processed_tlist = NIL;
914  subroot->grouping_map = NULL;
915  subroot->minmax_aggs = NIL;
916  subroot->qual_security_level = 0;
917  subroot->inhTargetKind = INHKIND_NONE;
918  subroot->hasRecursion = false;
919  subroot->wt_param_id = -1;
920  subroot->non_recursive_path = NULL;
921 
922  /* No CTEs to worry about */
923  Assert(subquery->cteList == NIL);
924 
925  /*
926  * Pull up any SubLinks within the subquery's quals, so that we don't
927  * leave unoptimized SubLinks behind.
928  */
929  if (subquery->hasSubLinks)
930  pull_up_sublinks(subroot);
931 
932  /*
933  * Similarly, inline any set-returning functions in its rangetable.
934  */
936 
937  /*
938  * Recursively pull up the subquery's subqueries, so that
939  * pull_up_subqueries' processing is complete for its jointree and
940  * rangetable.
941  *
942  * Note: it's okay that the subquery's recursion starts with NULL for
943  * containing-join info, even if we are within an outer join in the upper
944  * query; the lower query starts with a clean slate for outer-join
945  * semantics. Likewise, we needn't pass down appendrel state.
946  */
947  pull_up_subqueries(subroot);
948 
949  /*
950  * Now we must recheck whether the subquery is still simple enough to pull
951  * up. If not, abandon processing it.
952  *
953  * We don't really need to recheck all the conditions involved, but it's
954  * easier just to keep this "if" looking the same as the one in
955  * pull_up_subqueries_recurse.
956  */
957  if (is_simple_subquery(subquery, rte,
958  lowest_outer_join, deletion_ok) &&
959  (containing_appendrel == NULL || is_safe_append_member(subquery)))
960  {
961  /* good to go */
962  }
963  else
964  {
965  /*
966  * Give up, return unmodified RangeTblRef.
967  *
968  * Note: The work we just did will be redone when the subquery gets
969  * planned on its own. Perhaps we could avoid that by storing the
970  * modified subquery back into the rangetable, but I'm not gonna risk
971  * it now.
972  */
973  return jtnode;
974  }
975 
976  /*
977  * We must flatten any join alias Vars in the subquery's targetlist,
978  * because pulling up the subquery's subqueries might have changed their
979  * expansions into arbitrary expressions, which could affect
980  * pullup_replace_vars' decisions about whether PlaceHolderVar wrappers
981  * are needed for tlist entries. (Likely it'd be better to do
982  * flatten_join_alias_vars on the whole query tree at some earlier stage,
983  * maybe even in the rewriter; but for now let's just fix this case here.)
984  */
985  subquery->targetList = (List *)
986  flatten_join_alias_vars(subroot, (Node *) subquery->targetList);
987 
988  /*
989  * Adjust level-0 varnos in subquery so that we can append its rangetable
990  * to upper query's. We have to fix the subquery's append_rel_list as
991  * well.
992  */
993  rtoffset = list_length(parse->rtable);
994  OffsetVarNodes((Node *) subquery, rtoffset, 0);
995  OffsetVarNodes((Node *) subroot->append_rel_list, rtoffset, 0);
996 
997  /*
998  * Upper-level vars in subquery are now one level closer to their parent
999  * than before.
1000  */
1001  IncrementVarSublevelsUp((Node *) subquery, -1, 1);
1002  IncrementVarSublevelsUp((Node *) subroot->append_rel_list, -1, 1);
1003 
1004  /*
1005  * The subquery's targetlist items are now in the appropriate form to
1006  * insert into the top query, except that we may need to wrap them in
1007  * PlaceHolderVars. Set up required context data for pullup_replace_vars.
1008  */
1009  rvcontext.root = root;
1010  rvcontext.targetlist = subquery->targetList;
1011  rvcontext.target_rte = rte;
1012  if (rte->lateral)
1013  rvcontext.relids = get_relids_in_jointree((Node *) subquery->jointree,
1014  true);
1015  else /* won't need relids */
1016  rvcontext.relids = NULL;
1017  rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
1018  rvcontext.varno = varno;
1019  /* these flags will be set below, if needed */
1020  rvcontext.need_phvs = false;
1021  rvcontext.wrap_non_vars = false;
1022  /* initialize cache array with indexes 0 .. length(tlist) */
1023  rvcontext.rv_cache = palloc0((list_length(subquery->targetList) + 1) *
1024  sizeof(Node *));
1025 
1026  /*
1027  * If we are under an outer join then non-nullable items and lateral
1028  * references may have to be turned into PlaceHolderVars.
1029  */
1030  if (lowest_nulling_outer_join != NULL)
1031  rvcontext.need_phvs = true;
1032 
1033  /*
1034  * If we are dealing with an appendrel member then anything that's not a
1035  * simple Var has to be turned into a PlaceHolderVar. We force this to
1036  * ensure that what we pull up doesn't get merged into a surrounding
1037  * expression during later processing and then fail to match the
1038  * expression actually available from the appendrel.
1039  */
1040  if (containing_appendrel != NULL)
1041  {
1042  rvcontext.need_phvs = true;
1043  rvcontext.wrap_non_vars = true;
1044  }
1045 
1046  /*
1047  * If the parent query uses grouping sets, we need a PlaceHolderVar for
1048  * anything that's not a simple Var. Again, this ensures that expressions
1049  * retain their separate identity so that they will match grouping set
1050  * columns when appropriate. (It'd be sufficient to wrap values used in
1051  * grouping set columns, and do so only in non-aggregated portions of the
1052  * tlist and havingQual, but that would require a lot of infrastructure
1053  * that pullup_replace_vars hasn't currently got.)
1054  */
1055  if (parse->groupingSets)
1056  {
1057  rvcontext.need_phvs = true;
1058  rvcontext.wrap_non_vars = true;
1059  }
1060 
1061  /*
1062  * Replace all of the top query's references to the subquery's outputs
1063  * with copies of the adjusted subtlist items, being careful not to
1064  * replace any of the jointree structure. (This'd be a lot cleaner if we
1065  * could use query_tree_mutator.) We have to use PHVs in the targetList,
1066  * returningList, and havingQual, since those are certainly above any
1067  * outer join. replace_vars_in_jointree tracks its location in the
1068  * jointree and uses PHVs or not appropriately.
1069  */
1070  parse->targetList = (List *)
1071  pullup_replace_vars((Node *) parse->targetList, &rvcontext);
1072  parse->returningList = (List *)
1073  pullup_replace_vars((Node *) parse->returningList, &rvcontext);
1074  if (parse->onConflict)
1075  {
1076  parse->onConflict->onConflictSet = (List *)
1078  &rvcontext);
1079  parse->onConflict->onConflictWhere =
1081  &rvcontext);
1082 
1083  /*
1084  * We assume ON CONFLICT's arbiterElems, arbiterWhere, exclRelTlist
1085  * can't contain any references to a subquery
1086  */
1087  }
1088  replace_vars_in_jointree((Node *) parse->jointree, &rvcontext,
1089  lowest_nulling_outer_join);
1090  Assert(parse->setOperations == NULL);
1091  parse->havingQual = pullup_replace_vars(parse->havingQual, &rvcontext);
1092 
1093  /*
1094  * Replace references in the translated_vars lists of appendrels. When
1095  * pulling up an appendrel member, we do not need PHVs in the list of the
1096  * parent appendrel --- there isn't any outer join between. Elsewhere, use
1097  * PHVs for safety. (This analysis could be made tighter but it seems
1098  * unlikely to be worth much trouble.)
1099  */
1100  foreach(lc, root->append_rel_list)
1101  {
1102  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(lc);
1103  bool save_need_phvs = rvcontext.need_phvs;
1104 
1105  if (appinfo == containing_appendrel)
1106  rvcontext.need_phvs = false;
1107  appinfo->translated_vars = (List *)
1108  pullup_replace_vars((Node *) appinfo->translated_vars, &rvcontext);
1109  rvcontext.need_phvs = save_need_phvs;
1110  }
1111 
1112  /*
1113  * Replace references in the joinaliasvars lists of join RTEs.
1114  *
1115  * You might think that we could avoid using PHVs for alias vars of joins
1116  * below lowest_nulling_outer_join, but that doesn't work because the
1117  * alias vars could be referenced above that join; we need the PHVs to be
1118  * present in such references after the alias vars get flattened. (It
1119  * might be worth trying to be smarter here, someday.)
1120  */
1121  foreach(lc, parse->rtable)
1122  {
1123  RangeTblEntry *otherrte = (RangeTblEntry *) lfirst(lc);
1124 
1125  if (otherrte->rtekind == RTE_JOIN)
1126  otherrte->joinaliasvars = (List *)
1127  pullup_replace_vars((Node *) otherrte->joinaliasvars,
1128  &rvcontext);
1129  }
1130 
1131  /*
1132  * If the subquery had a LATERAL marker, propagate that to any of its
1133  * child RTEs that could possibly now contain lateral cross-references.
1134  * The children might or might not contain any actual lateral
1135  * cross-references, but we have to mark the pulled-up child RTEs so that
1136  * later planner stages will check for such.
1137  */
1138  if (rte->lateral)
1139  {
1140  foreach(lc, subquery->rtable)
1141  {
1142  RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(lc);
1143 
1144  switch (child_rte->rtekind)
1145  {
1146  case RTE_RELATION:
1147  if (child_rte->tablesample)
1148  child_rte->lateral = true;
1149  break;
1150  case RTE_SUBQUERY:
1151  case RTE_FUNCTION:
1152  case RTE_VALUES:
1153  case RTE_TABLEFUNC:
1154  child_rte->lateral = true;
1155  break;
1156  case RTE_JOIN:
1157  case RTE_CTE:
1158  case RTE_NAMEDTUPLESTORE:
1159  /* these can't contain any lateral references */
1160  break;
1161  }
1162  }
1163  }
1164 
1165  /*
1166  * Now append the adjusted rtable entries to upper query. (We hold off
1167  * until after fixing the upper rtable entries; no point in running that
1168  * code on the subquery ones too.)
1169  */
1170  parse->rtable = list_concat(parse->rtable, subquery->rtable);
1171 
1172  /*
1173  * Pull up any FOR UPDATE/SHARE markers, too. (OffsetVarNodes already
1174  * adjusted the marker rtindexes, so just concat the lists.)
1175  */
1176  parse->rowMarks = list_concat(parse->rowMarks, subquery->rowMarks);
1177 
1178  /*
1179  * We also have to fix the relid sets of any PlaceHolderVar nodes in the
1180  * parent query. (This could perhaps be done by pullup_replace_vars(),
1181  * but it seems cleaner to use two passes.) Note in particular that any
1182  * PlaceHolderVar nodes just created by pullup_replace_vars() will be
1183  * adjusted, so having created them with the subquery's varno is correct.
1184  *
1185  * Likewise, relids appearing in AppendRelInfo nodes have to be fixed. We
1186  * already checked that this won't require introducing multiple subrelids
1187  * into the single-slot AppendRelInfo structs.
1188  */
1189  if (parse->hasSubLinks || root->glob->lastPHId != 0 ||
1190  root->append_rel_list)
1191  {
1192  Relids subrelids;
1193 
1194  subrelids = get_relids_in_jointree((Node *) subquery->jointree, false);
1195  substitute_multiple_relids((Node *) parse, varno, subrelids);
1196  fix_append_rel_relids(root->append_rel_list, varno, subrelids);
1197  }
1198 
1199  /*
1200  * And now add subquery's AppendRelInfos to our list.
1201  */
1203  subroot->append_rel_list);
1204 
1205  /*
1206  * We don't have to do the equivalent bookkeeping for outer-join info,
1207  * because that hasn't been set up yet. placeholder_list likewise.
1208  */
1209  Assert(root->join_info_list == NIL);
1210  Assert(subroot->join_info_list == NIL);
1211  Assert(root->placeholder_list == NIL);
1212  Assert(subroot->placeholder_list == NIL);
1213 
1214  /*
1215  * Miscellaneous housekeeping.
1216  *
1217  * Although replace_rte_variables() faithfully updated parse->hasSubLinks
1218  * if it copied any SubLinks out of the subquery's targetlist, we still
1219  * could have SubLinks added to the query in the expressions of FUNCTION
1220  * and VALUES RTEs copied up from the subquery. So it's necessary to copy
1221  * subquery->hasSubLinks anyway. Perhaps this can be improved someday.
1222  */
1223  parse->hasSubLinks |= subquery->hasSubLinks;
1224 
1225  /* If subquery had any RLS conditions, now main query does too */
1226  parse->hasRowSecurity |= subquery->hasRowSecurity;
1227 
1228  /*
1229  * subquery won't be pulled up if it hasAggs, hasWindowFuncs, or
1230  * hasTargetSRFs, so no work needed on those flags
1231  */
1232 
1233  /*
1234  * Return the adjusted subquery jointree to replace the RangeTblRef entry
1235  * in parent's jointree; or, if we're flattening a subquery with empty
1236  * FROM list, return NULL to signal deletion of the subquery from the
1237  * parent jointree (and set hasDeletedRTEs to ensure cleanup later).
1238  */
1239  if (subquery->jointree->fromlist == NIL)
1240  {
1241  Assert(deletion_ok);
1242  Assert(subquery->jointree->quals == NULL);
1243  root->hasDeletedRTEs = true;
1244  return NULL;
1245  }
1246 
1247  return (Node *) subquery->jointree;
1248 }
#define NIL
Definition: pg_list.h:69
List * rowMarks
Definition: relation.h:268
Query * parse
Definition: relation.h:169
void OffsetVarNodes(Node *node, int offset, int sublevels_up)
Definition: rewriteManip.c:424
List * joinaliasvars
Definition: parsenodes.h:1006
List * plan_params
Definition: relation.h:183
List * join_info_list
Definition: relation.h:264
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:773
FromExpr * jointree
Definition: parsenodes.h:138
OnConflictExpr * onConflict
Definition: parsenodes.h:144
List * groupingSets
Definition: parsenodes.h:150
Definition: nodes.h:517
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * minmax_aggs
Definition: relation.h:300
List * fromlist
Definition: primnodes.h:1479
List * rowMarks
Definition: parsenodes.h:163
AttrNumber * grouping_map
Definition: relation.h:299
bool hasRecursion
Definition: relation.h:321
void pull_up_subqueries(PlannerInfo *root)
Definition: prepjointree.c:607
List * translated_vars
Definition: relation.h:2152
Node * quals
Definition: primnodes.h:1480
List * targetList
Definition: parsenodes.h:140
List * multiexpr_params
Definition: relation.h:246
int wt_param_id
Definition: relation.h:324
List * rtable
Definition: parsenodes.h:137
RangeTblEntry * target_rte
Definition: prepjointree.c:44
bool hasDeletedRTEs
Definition: relation.h:317
void inline_set_returning_functions(PlannerInfo *root)
Definition: prepjointree.c:573
Relids get_relids_in_jointree(Node *jtnode, bool include_joins)
PlannerGlobal * glob
Definition: relation.h:171
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
List * returningList
Definition: parsenodes.h:146
struct PlannerInfo * parent_root
Definition: relation.h:175
void * palloc0(Size size)
Definition: mcxt.c:955
Node * flatten_join_alias_vars(PlannerInfo *root, Node *node)
Definition: var.c:670
List * append_rel_list
Definition: relation.h:266
Index lastPHId
Definition: relation.h:133
List * cte_plan_ids
Definition: relation.h:244
static void substitute_multiple_relids(Node *node, int varno, Relids subrelids)
List * init_plans
Definition: relation.h:242
static bool is_safe_append_member(Query *subquery)
#define makeNode(_type_)
Definition: nodes.h:565
static void fix_append_rel_relids(List *append_rel_list, int varno, Relids subrelids)
static bool is_simple_subquery(Query *subquery, RangeTblEntry *rte, JoinExpr *lowest_outer_join, bool deletion_ok)
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
List * eq_classes
Definition: relation.h:249
InheritanceKind inhTargetKind
Definition: relation.h:312
Bitmapset * outer_params
Definition: relation.h:184
struct Path * non_recursive_path
Definition: relation.h:325
static int list_length(const List *l)
Definition: pg_list.h:89
Index qual_security_level
Definition: relation.h:309
Index query_level
Definition: relation.h:173
void pull_up_sublinks(PlannerInfo *root)
Definition: prepjointree.c:150
RTEKind rtekind
Definition: parsenodes.h:962
List * cteList
Definition: parsenodes.h:135
Node * setOperations
Definition: parsenodes.h:165
static void replace_vars_in_jointree(Node *jtnode, pullup_replace_vars_context *context, JoinExpr *lowest_nulling_outer_join)
Query * subquery
Definition: parsenodes.h:985
bool hasSubLinks
Definition: parsenodes.h:128
List * placeholder_list
Definition: relation.h:270
List * onConflictSet
Definition: primnodes.h:1504
MemoryContext planner_cxt
Definition: relation.h:302
#define copyObject(obj)
Definition: nodes.h:630
Node * havingQual
Definition: parsenodes.h:152
List * processed_tlist
Definition: relation.h:296
Node * onConflictWhere
Definition: primnodes.h:1505
static Node * pullup_replace_vars(Node *expr, pullup_replace_vars_context *context)
Definition: pg_list.h:45
struct TableSampleClause * tablesample
Definition: parsenodes.h:980
bool hasRowSecurity
Definition: parsenodes.h:133
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:649
struct PathTarget * upper_targets[UPPERREL_FINAL+1]
Definition: relation.h:290
List * upper_rels[UPPERREL_FINAL+1]
Definition: relation.h:287

◆ pull_up_simple_union_all()

static Node * pull_up_simple_union_all ( PlannerInfo root,
Node jtnode,
RangeTblEntry rte 
)
static

Definition at line 1260 of file prepjointree.c.

References Assert, copyObject, IncrementVarSublevelsUp_rtable(), RangeTblEntry::inh, RangeTblEntry::lateral, lfirst, list_concat(), list_length(), PlannerInfo::parse, pull_up_union_leaf_queries(), Query::rtable, RTE_SUBQUERY, RangeTblEntry::rtekind, Query::setOperations, RangeTblEntry::subquery, and pullup_replace_vars_context::varno.

Referenced by pull_up_subqueries_recurse().

1261 {
1262  int varno = ((RangeTblRef *) jtnode)->rtindex;
1263  Query *subquery = rte->subquery;
1264  int rtoffset = list_length(root->parse->rtable);
1265  List *rtable;
1266 
1267  /*
1268  * Make a modifiable copy of the subquery's rtable, so we can adjust
1269  * upper-level Vars in it. There are no such Vars in the setOperations
1270  * tree proper, so fixing the rtable should be sufficient.
1271  */
1272  rtable = copyObject(subquery->rtable);
1273 
1274  /*
1275  * Upper-level vars in subquery are now one level closer to their parent
1276  * than before. We don't have to worry about offsetting varnos, though,
1277  * because the UNION leaf queries can't cross-reference each other.
1278  */
1279  IncrementVarSublevelsUp_rtable(rtable, -1, 1);
1280 
1281  /*
1282  * If the UNION ALL subquery had a LATERAL marker, propagate that to all
1283  * its children. The individual children might or might not contain any
1284  * actual lateral cross-references, but we have to mark the pulled-up
1285  * child RTEs so that later planner stages will check for such.
1286  */
1287  if (rte->lateral)
1288  {
1289  ListCell *rt;
1290 
1291  foreach(rt, rtable)
1292  {
1293  RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(rt);
1294 
1295  Assert(child_rte->rtekind == RTE_SUBQUERY);
1296  child_rte->lateral = true;
1297  }
1298  }
1299 
1300  /*
1301  * Append child RTEs to parent rtable.
1302  */
1303  root->parse->rtable = list_concat(root->parse->rtable, rtable);
1304 
1305  /*
1306  * Recursively scan the subquery's setOperations tree and add
1307  * AppendRelInfo nodes for leaf subqueries to the parent's
1308  * append_rel_list. Also apply pull_up_subqueries to the leaf subqueries.
1309  */
1310  Assert(subquery->setOperations);
1311  pull_up_union_leaf_queries(subquery->setOperations, root, varno, subquery,
1312  rtoffset);
1313 
1314  /*
1315  * Mark the parent as an append relation.
1316  */
1317  rte->inh = true;
1318 
1319  return jtnode;
1320 }
static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex, Query *setOpQuery, int childRToffset)
Query * parse
Definition: relation.h:169
List * list_concat(List *list1, List *list2)
Definition: list.c:321
void IncrementVarSublevelsUp_rtable(List *rtable, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:796
List * rtable
Definition: parsenodes.h:137
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:962
Node * setOperations
Definition: parsenodes.h:165
Query * subquery
Definition: parsenodes.h:985
#define copyObject(obj)
Definition: nodes.h:630
Definition: pg_list.h:45

◆ pull_up_simple_values()

static Node * pull_up_simple_values ( PlannerInfo root,
Node jtnode,
RangeTblEntry rte 
)
static

Definition at line 1611 of file prepjointree.c.

References PlannerInfo::append_rel_list, Assert, contain_vars_of_level(), copyObject, PlannerInfo::hasDeletedRTEs, Query::hasSubLinks, Query::havingQual, PlannerInfo::join_info_list, Query::jointree, lappend(), lfirst, linitial, list_length(), makeTargetEntry(), pullup_replace_vars_context::need_phvs, NIL, Query::onConflict, OnConflictExpr::onConflictSet, OnConflictExpr::onConflictWhere, pullup_replace_vars_context::outer_hasSubLinks, palloc0(), parse(), PlannerInfo::parse, PlannerInfo::placeholder_list, pullup_replace_vars(), pullup_replace_vars_context::relids, replace_vars_in_jointree(), Query::returningList, pullup_replace_vars_context::root, Query::rtable, RTE_VALUES, RangeTblEntry::rtekind, pullup_replace_vars_context::rv_cache, Query::setOperations, pullup_replace_vars_context::target_rte, pullup_replace_vars_context::targetlist, Query::targetList, RangeTblEntry::values_lists, pullup_replace_vars_context::varno, and pullup_replace_vars_context::wrap_non_vars.

Referenced by pull_up_subqueries_recurse().

1612 {
1613  Query *parse = root->parse;
1614  int varno = ((RangeTblRef *) jtnode)->rtindex;
1615  List *values_list;
1616  List *tlist;
1617  AttrNumber attrno;
1618  pullup_replace_vars_context rvcontext;
1619  ListCell *lc;
1620 
1621  Assert(rte->rtekind == RTE_VALUES);
1622  Assert(list_length(rte->values_lists) == 1);
1623 
1624  /*
1625  * Need a modifiable copy of the VALUES list to hack on, just in case it's
1626  * multiply referenced.
1627  */
1628  values_list = copyObject(linitial(rte->values_lists));
1629 
1630  /*
1631  * The VALUES RTE can't contain any Vars of level zero, let alone any that
1632  * are join aliases, so no need to flatten join alias Vars.
1633  */
1634  Assert(!contain_vars_of_level((Node *) values_list, 0));
1635 
1636  /*
1637  * Set up required context data for pullup_replace_vars. In particular,
1638  * we have to make the VALUES list look like a subquery targetlist.
1639  */
1640  tlist = NIL;
1641  attrno = 1;
1642  foreach(lc, values_list)
1643  {
1644  tlist = lappend(tlist,
1645  makeTargetEntry((Expr *) lfirst(lc),
1646  attrno,
1647  NULL,
1648  false));
1649  attrno++;
1650  }
1651  rvcontext.root = root;
1652  rvcontext.targetlist = tlist;
1653  rvcontext.target_rte = rte;
1654  rvcontext.relids = NULL;
1655  rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
1656  rvcontext.varno = varno;
1657  rvcontext.need_phvs = false;
1658  rvcontext.wrap_non_vars = false;
1659  /* initialize cache array with indexes 0 .. length(tlist) */
1660  rvcontext.rv_cache = palloc0((list_length(tlist) + 1) *
1661  sizeof(Node *));
1662 
1663  /*
1664  * Replace all of the top query's references to the RTE's outputs with
1665  * copies of the adjusted VALUES expressions, being careful not to replace
1666  * any of the jointree structure. (This'd be a lot cleaner if we could use
1667  * query_tree_mutator.) Much of this should be no-ops in the dummy Query
1668  * that surrounds a VALUES RTE, but it's not enough code to be worth
1669  * removing.
1670  */
1671  parse->targetList = (List *)
1672  pullup_replace_vars((Node *) parse->targetList, &rvcontext);
1673  parse->returningList = (List *)
1674  pullup_replace_vars((Node *) parse->returningList, &rvcontext);
1675  if (parse->onConflict)
1676  {
1677  parse->onConflict->onConflictSet = (List *)
1679  &rvcontext);
1680  parse->onConflict->onConflictWhere =
1682  &rvcontext);
1683 
1684  /*
1685  * We assume ON CONFLICT's arbiterElems, arbiterWhere, exclRelTlist
1686  * can't contain any references to a subquery
1687  */
1688  }
1689  replace_vars_in_jointree((Node *) parse->jointree, &rvcontext, NULL);
1690  Assert(parse->setOperations == NULL);
1691  parse->havingQual = pullup_replace_vars(parse->havingQual, &rvcontext);
1692 
1693  /*
1694  * There should be no appendrels to fix, nor any join alias Vars, nor any
1695  * outer joins and hence no PlaceHolderVars.
1696  */
1697  Assert(root->append_rel_list == NIL);
1698  Assert(list_length(parse->rtable) == 1);
1699  Assert(root->join_info_list == NIL);
1700  Assert(root->placeholder_list == NIL);
1701 
1702  /*
1703  * Return NULL to signal deletion of the VALUES RTE from the parent
1704  * jointree (and set hasDeletedRTEs to ensure cleanup later).
1705  */
1706  root->hasDeletedRTEs = true;
1707  return NULL;
1708 }
#define NIL
Definition: pg_list.h:69
Query * parse
Definition: relation.h:169
List * join_info_list
Definition: relation.h:264
FromExpr * jointree
Definition: parsenodes.h:138
OnConflictExpr * onConflict
Definition: parsenodes.h:144
Definition: nodes.h:517
List * values_lists
Definition: parsenodes.h:1027
List * targetList
Definition: parsenodes.h:140
#define linitial(l)
Definition: pg_list.h:111
List * rtable
Definition: parsenodes.h:137
RangeTblEntry * target_rte
Definition: prepjointree.c:44
bool hasDeletedRTEs
Definition: relation.h:317
List * returningList
Definition: parsenodes.h:146
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:237
List * lappend(List *list, void *datum)
Definition: list.c:128
void * palloc0(Size size)
Definition: mcxt.c:955
List * append_rel_list
Definition: relation.h:266
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
static int list_length(const List *l)
Definition: pg_list.h:89
RTEKind rtekind
Definition: parsenodes.h:962
Node * setOperations
Definition: parsenodes.h:165
static void replace_vars_in_jointree(Node *jtnode, pullup_replace_vars_context *context, JoinExpr *lowest_nulling_outer_join)
bool hasSubLinks
Definition: parsenodes.h:128
List * placeholder_list
Definition: relation.h:270
List * onConflictSet
Definition: primnodes.h:1504
#define copyObject(obj)
Definition: nodes.h:630
Node * havingQual
Definition: parsenodes.h:152
Node * onConflictWhere
Definition: primnodes.h:1505
static Node * pullup_replace_vars(Node *expr, pullup_replace_vars_context *context)
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:649

◆ pull_up_sublinks()

void pull_up_sublinks ( PlannerInfo root)

Definition at line 150 of file prepjointree.c.

References IsA, Query::jointree, list_make1, makeFromExpr(), PlannerInfo::parse, pull_up_sublinks_jointree_recurse(), and pullup_replace_vars_context::relids.

Referenced by pull_up_simple_subquery(), and subquery_planner().

151 {
152  Node *jtnode;
153  Relids relids;
154 
155  /* Begin recursion through the jointree */
156  jtnode = pull_up_sublinks_jointree_recurse(root,
157  (Node *) root->parse->jointree,
158  &relids);
159 
160  /*
161  * root->parse->jointree must always be a FromExpr, so insert a dummy one
162  * if we got a bare RangeTblRef or JoinExpr out of the recursion.
163  */
164  if (IsA(jtnode, FromExpr))
165  root->parse->jointree = (FromExpr *) jtnode;
166  else
167  root->parse->jointree = makeFromExpr(list_make1(jtnode), NULL);
168 }
FromExpr * makeFromExpr(List *fromlist, Node *quals)
Definition: makefuncs.c:284
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Query * parse
Definition: relation.h:169
FromExpr * jointree
Definition: parsenodes.h:138
Definition: nodes.h:517
#define list_make1(x1)
Definition: pg_list.h:139
static Node * pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode, Relids *relids)
Definition: prepjointree.c:177

◆ pull_up_sublinks_jointree_recurse()

static Node * pull_up_sublinks_jointree_recurse ( PlannerInfo root,
Node jtnode,
Relids relids 
)
static

Definition at line 177 of file prepjointree.c.

References bms_add_member(), bms_join(), bms_make_singleton(), bms_union(), elog, ERROR, FromExpr::fromlist, IsA, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JoinExpr::jointype, lappend(), JoinExpr::larg, lfirst, makeFromExpr(), NIL, nodeTag, palloc(), pull_up_sublinks_qual_recurse(), JoinExpr::quals, FromExpr::quals, JoinExpr::rarg, JoinExpr::rtindex, and pullup_replace_vars_context::varno.

Referenced by pull_up_sublinks(), and pull_up_sublinks_qual_recurse().

179 {
180  if (jtnode == NULL)
181  {
182  *relids = NULL;
183  }
184  else if (IsA(jtnode, RangeTblRef))
185  {
186  int varno = ((RangeTblRef *) jtnode)->rtindex;
187 
188  *relids = bms_make_singleton(varno);
189  /* jtnode is returned unmodified */
190  }
191  else if (IsA(jtnode, FromExpr))
192  {
193  FromExpr *f = (FromExpr *) jtnode;
194  List *newfromlist = NIL;
195  Relids frelids = NULL;
196  FromExpr *newf;
197  Node *jtlink;
198  ListCell *l;
199 
200  /* First, recurse to process children and collect their relids */
201  foreach(l, f->fromlist)
202  {
203  Node *newchild;
204  Relids childrelids;
205 
206  newchild = pull_up_sublinks_jointree_recurse(root,
207  lfirst(l),
208  &childrelids);
209  newfromlist = lappend(newfromlist, newchild);
210  frelids = bms_join(frelids, childrelids);
211  }
212  /* Build the replacement FromExpr; no quals yet */
213  newf = makeFromExpr(newfromlist, NULL);
214  /* Set up a link representing the rebuilt jointree */
215  jtlink = (Node *) newf;
216  /* Now process qual --- all children are available for use */
217  newf->quals = pull_up_sublinks_qual_recurse(root, f->quals,
218  &jtlink, frelids,
219  NULL, NULL);
220 
221  /*
222  * Note that the result will be either newf, or a stack of JoinExprs
223  * with newf at the base. We rely on subsequent optimization steps to
224  * flatten this and rearrange the joins as needed.
225  *
226  * Although we could include the pulled-up subqueries in the returned
227  * relids, there's no need since upper quals couldn't refer to their
228  * outputs anyway.
229  */
230  *relids = frelids;
231  jtnode = jtlink;
232  }
233  else if (IsA(jtnode, JoinExpr))
234  {
235  JoinExpr *j;
236  Relids leftrelids;
237  Relids rightrelids;
238  Node *jtlink;
239 
240  /*
241  * Make a modifiable copy of join node, but don't bother copying its
242  * subnodes (yet).
243  */
244  j = (JoinExpr *) palloc(sizeof(JoinExpr));
245  memcpy(j, jtnode, sizeof(JoinExpr));
246  jtlink = (Node *) j;
247 
248  /* Recurse to process children and collect their relids */
250  &leftrelids);
252  &rightrelids);
253 
254  /*
255  * Now process qual, showing appropriate child relids as available,
256  * and attach any pulled-up jointree items at the right place. In the
257  * inner-join case we put new JoinExprs above the existing one (much
258  * as for a FromExpr-style join). In outer-join cases the new
259  * JoinExprs must go into the nullable side of the outer join. The
260  * point of the available_rels machinations is to ensure that we only
261  * pull up quals for which that's okay.
262  *
263  * We don't expect to see any pre-existing JOIN_SEMI or JOIN_ANTI
264  * nodes here.
265  */
266  switch (j->jointype)
267  {
268  case JOIN_INNER:
270  &jtlink,
271  bms_union(leftrelids,
272  rightrelids),
273  NULL, NULL);
274  break;
275  case JOIN_LEFT:
277  &j->rarg,
278  rightrelids,
279  NULL, NULL);
280  break;
281  case JOIN_FULL:
282  /* can't do anything with full-join quals */
283  break;
284  case JOIN_RIGHT:
286  &j->larg,
287  leftrelids,
288  NULL, NULL);
289  break;
290  default:
291  elog(ERROR, "unrecognized join type: %d",
292  (int) j->jointype);
293  break;
294  }
295 
296  /*
297  * Although we could include the pulled-up subqueries in the returned
298  * relids, there's no need since upper quals couldn't refer to their
299  * outputs anyway. But we *do* need to include the join's own rtindex
300  * because we haven't yet collapsed join alias variables, so upper
301  * levels would mistakenly think they couldn't use references to this
302  * join.
303  */
304  *relids = bms_join(leftrelids, rightrelids);
305  if (j->rtindex)
306  *relids = bms_add_member(*relids, j->rtindex);
307  jtnode = jtlink;
308  }
309  else
310  elog(ERROR, "unrecognized node type: %d",
311  (int) nodeTag(jtnode));
312  return jtnode;
313 }
#define NIL
Definition: pg_list.h:69
static Node * pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node, Node **jtlink1, Relids available_rels1, Node **jtlink2, Relids available_rels2)
Definition: prepjointree.c:331
FromExpr * makeFromExpr(List *fromlist, Node *quals)
Definition: makefuncs.c:284
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Definition: nodes.h:517
List * fromlist
Definition: primnodes.h:1479
Node * quals
Definition: primnodes.h:1480
Node * larg
Definition: primnodes.h:1459
#define ERROR
Definition: elog.h:43
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:976
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:245
static Node * pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode, Relids *relids)
Definition: prepjointree.c:177
List * lappend(List *list, void *datum)
Definition: list.c:128
Node * quals
Definition: primnodes.h:1462
Node * rarg
Definition: primnodes.h:1460
JoinType jointype
Definition: primnodes.h:1457
#define lfirst(lc)
Definition: pg_list.h:106
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:764
#define nodeTag(nodeptr)
Definition: nodes.h:522
void * palloc(Size size)
Definition: mcxt.c:924
#define elog
Definition: elog.h:219
int rtindex
Definition: primnodes.h:1464
Definition: pg_list.h:45

◆ pull_up_sublinks_qual_recurse()

static Node * pull_up_sublinks_qual_recurse ( PlannerInfo root,
Node node,
Node **  jtlink1,
Relids  available_rels1,
Node **  jtlink2,
Relids  available_rels2 
)
static

Definition at line 331 of file prepjointree.c.

References and_clause(), ANY_SUBLINK, generate_unaccent_rules::args, convert_ANY_sublink_to_join(), convert_EXISTS_sublink_to_join(), EXISTS_SUBLINK, get_notclausearg(), IsA, lappend(), JoinExpr::larg, lfirst, linitial, list_length(), make_andclause(), NIL, not_clause(), pull_up_sublinks_jointree_recurse(), JoinExpr::quals, JoinExpr::rarg, and SubLink::subLinkType.

Referenced by pull_up_sublinks_jointree_recurse().

334 {
335  if (node == NULL)
336  return NULL;
337  if (IsA(node, SubLink))
338  {
339  SubLink *sublink = (SubLink *) node;
340  JoinExpr *j;
341  Relids child_rels;
342 
343  /* Is it a convertible ANY or EXISTS clause? */
344  if (sublink->subLinkType == ANY_SUBLINK)
345  {
346  if ((j = convert_ANY_sublink_to_join(root, sublink,
347  available_rels1)) != NULL)
348  {
349  /* Yes; insert the new join node into the join tree */
350  j->larg = *jtlink1;
351  *jtlink1 = (Node *) j;
352  /* Recursively process pulled-up jointree nodes */
354  j->rarg,
355  &child_rels);
356 
357  /*
358  * Now recursively process the pulled-up quals. Any inserted
359  * joins can get stacked onto either j->larg or j->rarg,
360  * depending on which rels they reference.
361  */
363  j->quals,
364  &j->larg,
365  available_rels1,
366  &j->rarg,
367  child_rels);
368  /* Return NULL representing constant TRUE */
369  return NULL;
370  }
371  if (available_rels2 != NULL &&
372  (j = convert_ANY_sublink_to_join(root, sublink,
373  available_rels2)) != NULL)
374  {
375  /* Yes; insert the new join node into the join tree */
376  j->larg = *jtlink2;
377  *jtlink2 = (Node *) j;
378  /* Recursively process pulled-up jointree nodes */
380  j->rarg,
381  &child_rels);
382 
383  /*
384  * Now recursively process the pulled-up quals. Any inserted
385  * joins can get stacked onto either j->larg or j->rarg,
386  * depending on which rels they reference.
387  */
389  j->quals,
390  &j->larg,
391  available_rels2,
392  &j->rarg,
393  child_rels);
394  /* Return NULL representing constant TRUE */
395  return NULL;
396  }
397  }
398  else if (sublink->subLinkType == EXISTS_SUBLINK)
399  {
400  if ((j = convert_EXISTS_sublink_to_join(root, sublink, false,
401  available_rels1)) != NULL)
402  {
403  /* Yes; insert the new join node into the join tree */
404  j->larg = *jtlink1;
405  *jtlink1 = (Node *) j;
406  /* Recursively process pulled-up jointree nodes */
408  j->rarg,
409  &child_rels);
410 
411  /*
412  * Now recursively process the pulled-up quals. Any inserted
413  * joins can get stacked onto either j->larg or j->rarg,
414  * depending on which rels they reference.
415  */
417  j->quals,
418  &j->larg,
419  available_rels1,
420  &j->rarg,
421  child_rels);
422  /* Return NULL representing constant TRUE */
423  return NULL;
424  }
425  if (available_rels2 != NULL &&
426  (j = convert_EXISTS_sublink_to_join(root, sublink, false,
427  available_rels2)) != NULL)
428  {
429  /* Yes; insert the new join node into the join tree */
430  j->larg = *jtlink2;
431  *jtlink2 = (Node *) j;
432  /* Recursively process pulled-up jointree nodes */
434  j->rarg,
435  &child_rels);
436 
437  /*
438  * Now recursively process the pulled-up quals. Any inserted
439  * joins can get stacked onto either j->larg or j->rarg,
440  * depending on which rels they reference.
441  */
443  j->quals,
444  &j->larg,
445  available_rels2,
446  &j->rarg,
447  child_rels);
448  /* Return NULL representing constant TRUE */
449  return NULL;
450  }
451  }
452  /* Else return it unmodified */
453  return node;
454  }
455  if (not_clause(node))
456  {
457  /* If the immediate argument of NOT is EXISTS, try to convert */
458  SubLink *sublink = (SubLink *) get_notclausearg((Expr *) node);
459  JoinExpr *j;
460  Relids child_rels;
461 
462  if (sublink && IsA(sublink, SubLink))
463  {
464  if (sublink->subLinkType == EXISTS_SUBLINK)
465  {
466  if ((j = convert_EXISTS_sublink_to_join(root, sublink, true,
467  available_rels1)) != NULL)
468  {
469  /* Yes; insert the new join node into the join tree */
470  j->larg = *jtlink1;
471  *jtlink1 = (Node *) j;
472  /* Recursively process pulled-up jointree nodes */
474  j->rarg,
475  &child_rels);
476 
477  /*
478  * Now recursively process the pulled-up quals. Because
479  * we are underneath a NOT, we can't pull up sublinks that
480  * reference the left-hand stuff, but it's still okay to
481  * pull up sublinks referencing j->rarg.
482  */
484  j->quals,
485  &j->rarg,
486  child_rels,
487  NULL, NULL);
488  /* Return NULL representing constant TRUE */
489  return NULL;
490  }
491  if (available_rels2 != NULL &&
492  (j = convert_EXISTS_sublink_to_join(root, sublink, true,
493  available_rels2)) != NULL)
494  {
495  /* Yes; insert the new join node into the join tree */
496  j->larg = *jtlink2;
497  *jtlink2 = (Node *) j;
498  /* Recursively process pulled-up jointree nodes */
500  j->rarg,
501  &child_rels);
502 
503  /*
504  * Now recursively process the pulled-up quals. Because
505  * we are underneath a NOT, we can't pull up sublinks that
506  * reference the left-hand stuff, but it's still okay to
507  * pull up sublinks referencing j->rarg.
508  */
510  j->quals,
511  &j->rarg,
512  child_rels,
513  NULL, NULL);
514  /* Return NULL representing constant TRUE */
515  return NULL;
516  }
517  }
518  }
519  /* Else return it unmodified */
520  return node;
521  }
522  if (and_clause(node))
523  {
524  /* Recurse into AND clause */
525  List *newclauses = NIL;
526  ListCell *l;
527 
528  foreach(l, ((BoolExpr *) node)->args)
529  {
530  Node *oldclause = (Node *) lfirst(l);
531  Node *newclause;
532 
533  newclause = pull_up_sublinks_qual_recurse(root,
534  oldclause,
535  jtlink1,
536  available_rels1,
537  jtlink2,
538  available_rels2);
539  if (newclause)
540  newclauses = lappend(newclauses, newclause);
541  }
542  /* We might have got back fewer clauses than we started with */
543  if (newclauses == NIL)
544  return NULL;
545  else if (list_length(newclauses) == 1)
546  return (Node *) linitial(newclauses);
547  else
548  return (Node *) make_andclause(newclauses);
549  }
550  /* Stop if not an AND */
551  return node;
552 }
Expr * get_notclausearg(Expr *notclause)
Definition: clauses.c:266
#define NIL
Definition: pg_list.h:69
static Node * pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node, Node **jtlink1, Relids available_rels1, Node **jtlink2, Relids available_rels2)
Definition: prepjointree.c:331
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Definition: nodes.h:517
JoinExpr * convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink, Relids available_rels)
Definition: subselect.c:1317
JoinExpr * convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink, bool under_not, Relids available_rels)
Definition: subselect.c:1422
Node * larg
Definition: primnodes.h:1459
#define linitial(l)
Definition: pg_list.h:111
bool and_clause(Node *clause)
Definition: clauses.c:315
bool not_clause(Node *clause)
Definition: clauses.c:237
static Node * pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode, Relids *relids)
Definition: prepjointree.c:177
List * lappend(List *list, void *datum)
Definition: list.c:128
Node * quals
Definition: primnodes.h:1462
Node * rarg
Definition: primnodes.h:1460
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
Expr * make_andclause(List *andclauses)
Definition: clauses.c:328
Definition: pg_list.h:45

◆ pull_up_subqueries()

void pull_up_subqueries ( PlannerInfo root)

Definition at line 607 of file prepjointree.c.

References Assert, PlannerInfo::hasDeletedRTEs, IsA, Query::jointree, PlannerInfo::parse, pull_up_subqueries_cleanup(), and pull_up_subqueries_recurse().

Referenced by pull_up_simple_subquery(), and subquery_planner().

608 {
609  /* Top level of jointree must always be a FromExpr */
610  Assert(IsA(root->parse->jointree, FromExpr));
611  /* Reset flag saying we need a deletion cleanup pass */
612  root->hasDeletedRTEs = false;
613  /* Recursion starts with no containing join nor appendrel */
614  root->parse->jointree = (FromExpr *)
616  NULL, NULL, NULL, false);
617  /* Apply cleanup phase if necessary */
618  if (root->hasDeletedRTEs)
619  root->parse->jointree = (FromExpr *)
621  Assert(IsA(root->parse->jointree, FromExpr));
622 }
static Node * pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode, JoinExpr *lowest_outer_join, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel, bool deletion_ok)
Definition: prepjointree.c:673
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Query * parse
Definition: relation.h:169
FromExpr * jointree
Definition: parsenodes.h:138
Definition: nodes.h:517
bool hasDeletedRTEs
Definition: relation.h:317
static Node * pull_up_subqueries_cleanup(Node *jtnode)
#define Assert(condition)
Definition: c.h:699

◆ pull_up_subqueries_cleanup()

static Node * pull_up_subqueries_cleanup ( Node jtnode)
static

Definition at line 2287 of file prepjointree.c.

References Assert, elog, ERROR, FromExpr::fromlist, IsA, JOIN_INNER, JoinExpr::jointype, lappend(), JoinExpr::larg, lfirst, list_make1, makeFromExpr(), NIL, nodeTag, JoinExpr::quals, and JoinExpr::rarg.

Referenced by pull_up_subqueries().

2288 {
2289  Assert(jtnode != NULL);
2290  if (IsA(jtnode, RangeTblRef))
2291  {
2292  /* Nothing to do at leaf nodes. */
2293  }
2294  else if (IsA(jtnode, FromExpr))
2295  {
2296  FromExpr *f = (FromExpr *) jtnode;
2297  List *newfrom = NIL;
2298  ListCell *l;
2299 
2300  foreach(l, f->fromlist)
2301  {
2302  Node *child = (Node *) lfirst(l);
2303 
2304  if (child == NULL)
2305  continue;
2306  child = pull_up_subqueries_cleanup(child);
2307  newfrom = lappend(newfrom, child);
2308  }
2309  f->fromlist = newfrom;
2310  }
2311  else if (IsA(jtnode, JoinExpr))
2312  {
2313  JoinExpr *j = (JoinExpr *) jtnode;
2314 
2315  if (j->larg)
2317  if (j->rarg)
2319  if (j->larg == NULL)
2320  {
2321  Assert(j->jointype == JOIN_INNER);
2322  Assert(j->rarg != NULL);
2323  return (Node *) makeFromExpr(list_make1(j->rarg), j->quals);
2324  }
2325  else if (j->rarg == NULL)
2326  {
2327  Assert(j->jointype == JOIN_INNER);
2328  return (Node *) makeFromExpr(list_make1(j->larg), j->quals);
2329  }
2330  }
2331  else
2332  elog(ERROR, "unrecognized node type: %d",
2333  (int) nodeTag(jtnode));
2334  return jtnode;
2335 }
#define NIL
Definition: pg_list.h:69
FromExpr * makeFromExpr(List *fromlist, Node *quals)
Definition: makefuncs.c:284
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Definition: nodes.h:517
List * fromlist
Definition: primnodes.h:1479
Node * larg
Definition: primnodes.h:1459
#define list_make1(x1)
Definition: pg_list.h:139
#define ERROR
Definition: elog.h:43
List * lappend(List *list, void *datum)
Definition: list.c:128
Node * quals
Definition: primnodes.h:1462
static Node * pull_up_subqueries_cleanup(Node *jtnode)
Node * rarg
Definition: primnodes.h:1460
JoinType jointype
Definition: primnodes.h:1457
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
#define nodeTag(nodeptr)
Definition: nodes.h:522
#define elog
Definition: elog.h:219
Definition: pg_list.h:45

◆ pull_up_subqueries_recurse()

static Node * pull_up_subqueries_recurse ( PlannerInfo root,
Node jtnode,
JoinExpr lowest_outer_join,
JoinExpr lowest_nulling_outer_join,
AppendRelInfo containing_appendrel,
bool  deletion_ok 
)
static

Definition at line 673 of file prepjointree.c.

References Assert, elog, ERROR, FromExpr::fromlist, PlannerInfo::hasDeletedRTEs, is_safe_append_member(), is_simple_subquery(), is_simple_union_all(), is_simple_values(), IsA, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_SEMI, Query::jointree, JoinExpr::jointype, JoinExpr::larg, lfirst, lnext, nodeTag, PlannerInfo::parse, pull_up_simple_subquery(), pull_up_simple_union_all(), pull_up_simple_values(), FromExpr::quals, JoinExpr::rarg, rt_fetch, Query::rtable, RTE_SUBQUERY, RTE_VALUES, RangeTblEntry::rtekind, RangeTblEntry::subquery, and pullup_replace_vars_context::varno.

Referenced by pull_up_subqueries(), and pull_up_union_leaf_queries().

678 {
679  Assert(jtnode != NULL);
680  if (IsA(jtnode, RangeTblRef))
681  {
682  int varno = ((RangeTblRef *) jtnode)->rtindex;
683  RangeTblEntry *rte = rt_fetch(varno, root->parse->rtable);
684 
685  /*
686  * Is this a subquery RTE, and if so, is the subquery simple enough to
687  * pull up?
688  *
689  * If we are looking at an append-relation member, we can't pull it up
690  * unless is_safe_append_member says so.
691  */
692  if (rte->rtekind == RTE_SUBQUERY &&
693  is_simple_subquery(rte->subquery, rte,
694  lowest_outer_join, deletion_ok) &&
695  (containing_appendrel == NULL ||
697  return pull_up_simple_subquery(root, jtnode, rte,
698  lowest_outer_join,
699  lowest_nulling_outer_join,
700  containing_appendrel,
701  deletion_ok);
702 
703  /*
704  * Alternatively, is it a simple UNION ALL subquery? If so, flatten
705  * into an "append relation".
706  *
707  * It's safe to do this regardless of whether this query is itself an
708  * appendrel member. (If you're thinking we should try to flatten the
709  * two levels of appendrel together, you're right; but we handle that
710  * in set_append_rel_pathlist, not here.)
711  */
712  if (rte->rtekind == RTE_SUBQUERY &&
714  return pull_up_simple_union_all(root, jtnode, rte);
715 
716  /*
717  * Or perhaps it's a simple VALUES RTE?
718  *
719  * We don't allow VALUES pullup below an outer join nor into an
720  * appendrel (such cases are impossible anyway at the moment).
721  */
722  if (rte->rtekind == RTE_VALUES &&
723  lowest_outer_join == NULL &&
724  containing_appendrel == NULL &&
725  is_simple_values(root, rte, deletion_ok))
726  return pull_up_simple_values(root, jtnode, rte);
727 
728  /* Otherwise, do nothing at this node. */
729  }
730  else if (IsA(jtnode, FromExpr))
731  {
732  FromExpr *f = (FromExpr *) jtnode;
733  bool have_undeleted_child = false;
734  ListCell *l;
735 
736  Assert(containing_appendrel == NULL);
737 
738  /*
739  * If the FromExpr has quals, it's not deletable even if its parent
740  * would allow deletion.
741  */
742  if (f->quals)
743  deletion_ok = false;
744 
745  foreach(l, f->fromlist)
746  {
747  /*
748  * In a non-deletable FromExpr, we can allow deletion of child
749  * nodes so long as at least one child remains; so it's okay
750  * either if any previous child survives, or if there's more to
751  * come. If all children are deletable in themselves, we'll force
752  * the last one to remain unflattened.
753  *
754  * As a separate matter, we can allow deletion of all children of
755  * the top-level FromExpr in a query, since that's a special case
756  * anyway.
757  */
758  bool sub_deletion_ok = (deletion_ok ||
759  have_undeleted_child ||
760  lnext(l) != NULL ||
761  f == root->parse->jointree);
762 
764  lowest_outer_join,
765  lowest_nulling_outer_join,
766  NULL,
767  sub_deletion_ok);
768  if (lfirst(l) != NULL)
769  have_undeleted_child = true;
770  }
771 
772  if (deletion_ok && !have_undeleted_child)
773  {
774  /* OK to delete this FromExpr entirely */
775  root->hasDeletedRTEs = true; /* probably is set already */
776  return NULL;
777  }
778  }
779  else if (IsA(jtnode, JoinExpr))
780  {
781  JoinExpr *j = (JoinExpr *) jtnode;
782 
783  Assert(containing_appendrel == NULL);
784  /* Recurse, being careful to tell myself when inside outer join */
785  switch (j->jointype)
786  {
787  case JOIN_INNER:
788 
789  /*
790  * INNER JOIN can allow deletion of either child node, but not
791  * both. So right child gets permission to delete only if
792  * left child didn't get removed.
793  */
794  j->larg = pull_up_subqueries_recurse(root, j->larg,
795  lowest_outer_join,
796  lowest_nulling_outer_join,
797  NULL,
798  true);
799  j->rarg = pull_up_subqueries_recurse(root, j->rarg,
800  lowest_outer_join,
801  lowest_nulling_outer_join,
802  NULL,
803  j->larg != NULL);
804  break;
805  case JOIN_LEFT:
806  case JOIN_SEMI:
807  case JOIN_ANTI:
808  j->larg = pull_up_subqueries_recurse(root, j->larg,
809  j,
810  lowest_nulling_outer_join,
811  NULL,
812  false);
813  j->rarg = pull_up_subqueries_recurse(root, j->rarg,
814  j,
815  j,
816  NULL,
817  false);
818  break;
819  case JOIN_FULL:
820  j->larg = pull_up_subqueries_recurse(root, j->larg,
821  j,
822  j,
823  NULL,
824  false);
825  j->rarg = pull_up_subqueries_recurse(root, j->rarg,
826  j,
827  j,
828  NULL,
829  false);
830  break;
831  case JOIN_RIGHT:
832  j->larg = pull_up_subqueries_recurse(root, j->larg,
833  j,
834  j,
835  NULL,
836  false);
837  j->rarg = pull_up_subqueries_recurse(root, j->rarg,
838  j,
839  lowest_nulling_outer_join,
840  NULL,
841  false);
842  break;
843  default:
844  elog(ERROR, "unrecognized join type: %d",
845  (int) j->jointype);
846  break;
847  }
848  }
849  else
850  elog(ERROR, "unrecognized node type: %d",
851  (int) nodeTag(jtnode));
852  return jtnode;
853 }
static Node * pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode, JoinExpr *lowest_outer_join, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel, bool deletion_ok)
Definition: prepjointree.c:673
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Query * parse
Definition: relation.h:169
FromExpr * jointree
Definition: parsenodes.h:138
List * fromlist
Definition: primnodes.h:1479
static bool is_simple_union_all(Query *subquery)
static bool is_simple_values(PlannerInfo *root, RangeTblEntry *rte, bool deletion_ok)
Node * quals
Definition: primnodes.h:1480
Node * larg
Definition: primnodes.h:1459
static Node * pull_up_simple_union_all(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:43
bool hasDeletedRTEs
Definition: relation.h:317
#define lnext(lc)
Definition: pg_list.h:105
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
static bool is_safe_append_member(Query *subquery)
Node * rarg
Definition: primnodes.h:1460
static bool is_simple_subquery(Query *subquery, RangeTblEntry *rte, JoinExpr *lowest_outer_join, bool deletion_ok)
JoinType jointype
Definition: primnodes.h:1457
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
#define nodeTag(nodeptr)
Definition: nodes.h:522
static Node * pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte, JoinExpr *lowest_outer_join, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel, bool deletion_ok)
Definition: prepjointree.c:868
RTEKind rtekind
Definition: parsenodes.h:962
Query * subquery
Definition: parsenodes.h:985
#define elog
Definition: elog.h:219
static Node * pull_up_simple_values(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)

◆ pull_up_union_leaf_queries()

static void pull_up_union_leaf_queries ( Node setOp,
PlannerInfo root,
int  parentRTindex,
Query setOpQuery,
int  childRToffset 
)
static

Definition at line 1341 of file prepjointree.c.

References PlannerInfo::append_rel_list, AppendRelInfo::child_relid, AppendRelInfo::child_reltype, elog, ERROR, InvalidOid, IsA, lappend(), SetOperationStmt::larg, make_setop_translation_list(), makeNode, nodeTag, AppendRelInfo::parent_relid, AppendRelInfo::parent_reloid, AppendRelInfo::parent_reltype, pull_up_subqueries_recurse(), SetOperationStmt::rarg, RangeTblRef::rtindex, and AppendRelInfo::translated_vars.

Referenced by flatten_simple_union_all(), and pull_up_simple_union_all().

1343 {
1344  if (IsA(setOp, RangeTblRef))
1345  {
1346  RangeTblRef *rtr = (RangeTblRef *) setOp;
1347  int childRTindex;
1348  AppendRelInfo *appinfo;
1349 
1350  /*
1351  * Calculate the index in the parent's range table
1352  */
1353  childRTindex = childRToffset + rtr->rtindex;
1354 
1355  /*
1356  * Build a suitable AppendRelInfo, and attach to parent's list.
1357  */
1358  appinfo = makeNode(AppendRelInfo);
1359  appinfo->parent_relid = parentRTindex;
1360  appinfo->child_relid = childRTindex;
1361  appinfo->parent_reltype = InvalidOid;
1362  appinfo->child_reltype = InvalidOid;
1363  make_setop_translation_list(setOpQuery, childRTindex,
1364  &appinfo->translated_vars);
1365  appinfo->parent_reloid = InvalidOid;
1366  root->append_rel_list = lappend(root->append_rel_list, appinfo);
1367 
1368  /*
1369  * Recursively apply pull_up_subqueries to the new child RTE. (We
1370  * must build the AppendRelInfo first, because this will modify it.)
1371  * Note that we can pass NULL for containing-join info even if we're
1372  * actually under an outer join, because the child's expressions
1373  * aren't going to propagate up to the join. Also, we ignore the
1374  * possibility that pull_up_subqueries_recurse() returns a different
1375  * jointree node than what we pass it; if it does, the important thing
1376  * is that it replaced the child relid in the AppendRelInfo node.
1377  */
1378  rtr = makeNode(RangeTblRef);
1379  rtr->rtindex = childRTindex;
1380  (void) pull_up_subqueries_recurse(root, (Node *) rtr,
1381  NULL, NULL, appinfo, false);
1382  }
1383  else if (IsA(setOp, SetOperationStmt))
1384  {
1385  SetOperationStmt *op = (SetOperationStmt *) setOp;
1386 
1387  /* Recurse to reach leaf queries */
1388  pull_up_union_leaf_queries(op->larg, root, parentRTindex, setOpQuery,
1389  childRToffset);
1390  pull_up_union_leaf_queries(op->rarg, root, parentRTindex, setOpQuery,
1391  childRToffset);
1392  }
1393  else
1394  {
1395  elog(ERROR, "unrecognized node type: %d",
1396  (int) nodeTag(setOp));
1397  }
1398 }
static Node * pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode, JoinExpr *lowest_outer_join, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel, bool deletion_ok)
Definition: prepjointree.c:673
static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex, Query *setOpQuery, int childRToffset)
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Definition: nodes.h:517
List * translated_vars
Definition: relation.h:2152
Oid parent_reltype
Definition: relation.h:2133
#define ERROR
Definition: elog.h:43
List * lappend(List *list, void *datum)
Definition: list.c:128
List * append_rel_list
Definition: relation.h:266
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:565
#define nodeTag(nodeptr)
Definition: nodes.h:522
static void make_setop_translation_list(Query *query, Index newvarno, List **translated_vars)
Oid child_reltype
Definition: relation.h:2134
#define elog
Definition: elog.h:219
Index child_relid
Definition: relation.h:2125
Oid parent_reloid
Definition: relation.h:2159
Index parent_relid
Definition: relation.h:2124

◆ pullup_replace_vars()

static Node * pullup_replace_vars ( Node expr,
pullup_replace_vars_context context 
)
static

Definition at line 2064 of file prepjointree.c.

References pullup_replace_vars_context::outer_hasSubLinks, pullup_replace_vars_callback(), replace_rte_variables(), and pullup_replace_vars_context::varno.

Referenced by pull_up_simple_subquery(), pull_up_simple_values(), and replace_vars_in_jointree().

2065 {
2066  return replace_rte_variables(expr,
2067  context->varno, 0,
2069  (void *) context,
2070  context->outer_hasSubLinks);
2071 }
static Node * pullup_replace_vars_callback(Var *var, replace_rte_variables_context *context)
Node * replace_rte_variables(Node *node, int target_varno, int sublevels_up, replace_rte_variables_callback callback, void *callback_arg, bool *outer_hasSubLinks)

◆ pullup_replace_vars_callback()

static Node * pullup_replace_vars_callback ( Var var,
replace_rte_variables_context context 
)
static

Definition at line 2074 of file prepjointree.c.

References RowExpr::args, bms_is_member(), bms_make_singleton(), bms_overlap(), replace_rte_variables_context::callback_arg, COERCE_IMPLICIT_CAST, RowExpr::colnames, contain_nonstrict_functions(), contain_vars_of_level(), copyObject, elog, ERROR, expandRTE(), TargetEntry::expr, get_tle_by_resno(), IncrementVarSublevelsUp(), InvalidAttrNumber, IsA, RangeTblEntry::lateral, list_length(), Var::location, RowExpr::location, make_placeholder_expr(), makeNode, pullup_replace_vars_context::need_phvs, pull_varnos(), pullup_replace_vars_context::relids, replace_rte_variables_mutator(), pullup_replace_vars_context::root, RowExpr::row_format, RowExpr::row_typeid, pullup_replace_vars_context::rv_cache, replace_rte_variables_context::sublevels_up, pullup_replace_vars_context::target_rte, pullup_replace_vars_context::targetlist, Var::varattno, Var::varlevelsup, pullup_replace_vars_context::varno, Var::varno, Var::vartype, and pullup_replace_vars_context::wrap_non_vars.

Referenced by pullup_replace_vars(), and pullup_replace_vars_subquery().

2076 {
2078  int varattno = var->varattno;
2079  Node *newnode;
2080 
2081  /*
2082  * If PlaceHolderVars are needed, we cache the modified expressions in
2083  * rcon->rv_cache[]. This is not in hopes of any material speed gain
2084  * within this function, but to avoid generating identical PHVs with
2085  * different IDs. That would result in duplicate evaluations at runtime,
2086  * and possibly prevent optimizations that rely on recognizing different
2087  * references to the same subquery output as being equal(). So it's worth
2088  * a bit of extra effort to avoid it.
2089  */
2090  if (rcon->need_phvs &&
2091  varattno >= InvalidAttrNumber &&
2092  varattno <= list_length(rcon->targetlist) &&
2093  rcon->rv_cache[varattno] != NULL)
2094  {
2095  /* Just copy the entry and fall through to adjust its varlevelsup */
2096  newnode = copyObject(rcon->rv_cache[varattno]);
2097  }
2098  else if (varattno == InvalidAttrNumber)
2099  {
2100  /* Must expand whole-tuple reference into RowExpr */
2101  RowExpr *rowexpr;
2102  List *colnames;
2103  List *fields;
2104  bool save_need_phvs = rcon->need_phvs;
2105  int save_sublevelsup = context->sublevels_up;
2106 
2107  /*
2108  * If generating an expansion for a var of a named rowtype (ie, this
2109  * is a plain relation RTE), then we must include dummy items for
2110  * dropped columns. If the var is RECORD (ie, this is a JOIN), then
2111  * omit dropped columns. Either way, attach column names to the
2112  * RowExpr for use of ruleutils.c.
2113  *
2114  * In order to be able to cache the results, we always generate the
2115  * expansion with varlevelsup = 0, and then adjust if needed.
2116  */
2117  expandRTE(rcon->target_rte,
2118  var->varno, 0 /* not varlevelsup */ , var->location,
2119  (var->vartype != RECORDOID),
2120  &colnames, &fields);
2121  /* Adjust the generated per-field Vars, but don't insert PHVs */
2122  rcon->need_phvs = false;
2123  context->sublevels_up = 0; /* to match the expandRTE output */
2124  fields = (List *) replace_rte_variables_mutator((Node *) fields,
2125  context);
2126  rcon->need_phvs = save_need_phvs;
2127  context->sublevels_up = save_sublevelsup;
2128 
2129  rowexpr = makeNode(RowExpr);
2130  rowexpr->args = fields;
2131  rowexpr->row_typeid = var->vartype;
2132  rowexpr->row_format = COERCE_IMPLICIT_CAST;
2133  rowexpr->colnames = colnames;
2134  rowexpr->location = var->location;
2135  newnode = (Node *) rowexpr;
2136 
2137  /*
2138  * Insert PlaceHolderVar if needed. Notice that we are wrapping one
2139  * PlaceHolderVar around the whole RowExpr, rather than putting one
2140  * around each element of the row. This is because we need the
2141  * expression to yield NULL, not ROW(NULL,NULL,...) when it is forced
2142  * to null by an outer join.
2143  */
2144  if (rcon->need_phvs)
2145  {
2146  /* RowExpr is certainly not strict, so always need PHV */
2147  newnode = (Node *)
2149  (Expr *) newnode,
2150  bms_make_singleton(rcon->varno));
2151  /* cache it with the PHV, and with varlevelsup still zero */
2152  rcon->rv_cache[InvalidAttrNumber] = copyObject(newnode);
2153  }
2154  }
2155  else
2156  {
2157  /* Normal case referencing one targetlist element */
2158  TargetEntry *tle = get_tle_by_resno(rcon->targetlist, varattno);
2159 
2160  if (tle == NULL) /* shouldn't happen */
2161  elog(ERROR, "could not find attribute %d in subquery targetlist",
2162  varattno);
2163 
2164  /* Make a copy of the tlist item to return */
2165  newnode = (Node *) copyObject(tle->expr);
2166 
2167  /* Insert PlaceHolderVar if needed */
2168  if (rcon->need_phvs)
2169  {
2170  bool wrap;
2171 
2172  if (newnode && IsA(newnode, Var) &&
2173  ((Var *) newnode)->varlevelsup == 0)
2174  {
2175  /*
2176  * Simple Vars always escape being wrapped, unless they are
2177  * lateral references to something outside the subquery being
2178  * pulled up. (Even then, we could omit the PlaceHolderVar if
2179  * the referenced rel is under the same lowest outer join, but
2180  * it doesn't seem worth the trouble to check that.)
2181  */
2182  if (rcon->target_rte->lateral &&
2183  !bms_is_member(((Var *) newnode)->varno, rcon->relids))
2184  wrap = true;
2185  else
2186  wrap = false;
2187  }
2188  else if (newnode && IsA(newnode, PlaceHolderVar) &&
2189  ((PlaceHolderVar *) newnode)->phlevelsup == 0)
2190  {
2191  /* No need to wrap a PlaceHolderVar with another one, either */
2192  wrap = false;
2193  }
2194  else if (rcon->wrap_non_vars)
2195  {
2196  /* Wrap all non-Vars in a PlaceHolderVar */
2197  wrap = true;
2198  }
2199  else
2200  {
2201  /*
2202  * If it contains a Var of the subquery being pulled up, and
2203  * does not contain any non-strict constructs, then it's
2204  * certainly nullable so we don't need to insert a
2205  * PlaceHolderVar.
2206  *
2207  * This analysis could be tighter: in particular, a non-strict
2208  * construct hidden within a lower-level PlaceHolderVar is not
2209  * reason to add another PHV. But for now it doesn't seem
2210  * worth the code to be more exact.
2211  *
2212  * Note: in future maybe we should insert a PlaceHolderVar
2213  * anyway, if the tlist item is expensive to evaluate?
2214  *
2215  * For a LATERAL subquery, we have to check the actual var
2216  * membership of the node, but if it's non-lateral then any
2217  * level-zero var must belong to the subquery.
2218  */
2219  if ((rcon->target_rte->lateral ?
2220  bms_overlap(pull_varnos((Node *) newnode), rcon->relids) :
2221  contain_vars_of_level((Node *) newnode, 0)) &&
2222  !contain_nonstrict_functions((Node *) newnode))
2223  {
2224  /* No wrap needed */
2225  wrap = false;
2226  }
2227  else
2228  {
2229  /* Else wrap it in a PlaceHolderVar */
2230  wrap = true;
2231  }
2232  }
2233 
2234  if (wrap)
2235  newnode = (Node *)
2237  (Expr *) newnode,
2238  bms_make_singleton(rcon->varno));
2239 
2240  /*
2241  * Cache it if possible (ie, if the attno is in range, which it
2242  * probably always should be). We can cache the value even if we
2243  * decided we didn't need a PHV, since this result will be
2244  * suitable for any request that has need_phvs.
2245  */
2246  if (varattno > InvalidAttrNumber &&
2247  varattno <= list_length(rcon->targetlist))
2248  rcon->rv_cache[varattno] = copyObject(newnode);
2249  }
2250  }
2251 
2252  /* Must adjust varlevelsup if tlist item is from higher query */
2253  if (var->varlevelsup > 0)
2254  IncrementVarSublevelsUp(newnode, var->varlevelsup, 0);
2255 
2256  return newnode;
2257 }
List * args
Definition: primnodes.h:991
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Index varlevelsup
Definition: primnodes.h:174
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:773
Definition: nodes.h:517
AttrNumber varattno
Definition: primnodes.h:169
Definition: primnodes.h:164
PlaceHolderVar * make_placeholder_expr(PlannerInfo *root, Expr *expr, Relids phrels)
Definition: placeholder.c:40
#define ERROR
Definition: elog.h:43
List * colnames
Definition: primnodes.h:1007
RangeTblEntry * target_rte
Definition: prepjointree.c:44
Node * replace_rte_variables_mutator(Node *node, replace_rte_variables_context *context)
Oid vartype
Definition: primnodes.h:171
int location
Definition: primnodes.h:179
int location
Definition: primnodes.h:1008
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:245
Relids pull_varnos(Node *node)
Definition: var.c:95
Index varno
Definition: primnodes.h:167
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
#define makeNode(_type_)
Definition: nodes.h:565
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:369
Expr * expr
Definition: primnodes.h:1376
Oid row_typeid
Definition: primnodes.h:992
static int list_length(const List *l)
Definition: pg_list.h:89
#define InvalidAttrNumber
Definition: attnum.h:23
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:509
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
#define elog
Definition: elog.h:219
bool contain_nonstrict_functions(Node *clause)
Definition: clauses.c:1315
#define copyObject(obj)
Definition: nodes.h:630
CoercionForm row_format
Definition: primnodes.h:1006
Definition: pg_list.h:45
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:486

◆ pullup_replace_vars_subquery()

static Query * pullup_replace_vars_subquery ( Query query,
pullup_replace_vars_context context 
)
static

Definition at line 2267 of file prepjointree.c.

References Assert, IsA, pullup_replace_vars_callback(), replace_rte_variables(), and pullup_replace_vars_context::varno.

Referenced by replace_vars_in_jointree().

2269 {
2270  Assert(IsA(query, Query));
2271  return (Query *) replace_rte_variables((Node *) query,
2272  context->varno, 1,
2274  (void *) context,
2275  NULL);
2276 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
static Node * pullup_replace_vars_callback(Var *var, replace_rte_variables_context *context)
Definition: nodes.h:517
#define Assert(condition)
Definition: c.h:699
Node * replace_rte_variables(Node *node, int target_varno, int sublevels_up, replace_rte_variables_callback callback, void *callback_arg, bool *outer_hasSubLinks)

◆ reduce_outer_joins()

void reduce_outer_joins ( PlannerInfo root)

Definition at line 2471 of file prepjointree.c.

References reduce_outer_joins_state::contains_outer, elog, ERROR, Query::jointree, NIL, PlannerInfo::parse, reduce_outer_joins_pass1(), and reduce_outer_joins_pass2().

Referenced by subquery_planner().

2472 {
2474 
2475  /*
2476  * To avoid doing strictness checks on more quals than necessary, we want
2477  * to stop descending the jointree as soon as there are no outer joins
2478  * below our current point. This consideration forces a two-pass process.
2479  * The first pass gathers information about which base rels appear below
2480  * each side of each join clause, and about whether there are outer
2481  * join(s) below each side of each join clause. The second pass examines
2482  * qual clauses and changes join types as it descends the tree.
2483  */
2484  state = reduce_outer_joins_pass1((Node *) root->parse->jointree);
2485 
2486  /* planner.c shouldn't have called me if no outer joins */
2487  if (state == NULL || !state->contains_outer)
2488  elog(ERROR, "so where are the outer joins?");
2489 
2491  state, root, NULL, NIL, NIL);
2492 }
#define NIL
Definition: pg_list.h:69
Query * parse
Definition: relation.h:169
FromExpr * jointree
Definition: parsenodes.h:138
Definition: nodes.h:517
#define ERROR
Definition: elog.h:43
static reduce_outer_joins_state * reduce_outer_joins_pass1(Node *jtnode)
Definition: regguts.h:298
#define elog
Definition: elog.h:219
static void reduce_outer_joins_pass2(Node *jtnode, reduce_outer_joins_state *state, PlannerInfo *root, Relids nonnullable_rels, List *nonnullable_vars, List *forced_null_vars)

◆ reduce_outer_joins_pass1()

static reduce_outer_joins_state * reduce_outer_joins_pass1 ( Node jtnode)
static

Definition at line 2500 of file prepjointree.c.

References bms_add_members(), bms_make_singleton(), reduce_outer_joins_state::contains_outer, elog, ERROR, FromExpr::fromlist, IS_OUTER_JOIN, IsA, JoinExpr::jointype, lappend(), JoinExpr::larg, lfirst, NIL, nodeTag, palloc(), JoinExpr::rarg, reduce_outer_joins_state::relids, reduce_outer_joins_state::sub_states, and pullup_replace_vars_context::varno.

Referenced by reduce_outer_joins().

2501 {
2502  reduce_outer_joins_state *result;
2503 
2504  result = (reduce_outer_joins_state *)
2506  result->relids = NULL;
2507  result->contains_outer = false;
2508  result->sub_states = NIL;
2509 
2510  if (jtnode == NULL)
2511  return result;
2512  if (IsA(jtnode, RangeTblRef))
2513  {
2514  int varno = ((RangeTblRef *) jtnode)->rtindex;
2515 
2516  result->relids = bms_make_singleton(varno);
2517  }
2518  else if (IsA(jtnode, FromExpr))
2519  {
2520  FromExpr *f = (FromExpr *) jtnode;
2521  ListCell *l;
2522 
2523  foreach(l, f->fromlist)
2524  {
2525  reduce_outer_joins_state *sub_state;
2526 
2527  sub_state = reduce_outer_joins_pass1(lfirst(l));
2528  result->relids = bms_add_members(result->relids,
2529  sub_state->relids);
2530  result->contains_outer |= sub_state->contains_outer;
2531  result->sub_states = lappend(result->sub_states, sub_state);
2532  }
2533  }
2534  else if (IsA(jtnode, JoinExpr))
2535  {
2536  JoinExpr *j = (JoinExpr *) jtnode;
2537  reduce_outer_joins_state *sub_state;
2538 
2539  /* join's own RT index is not wanted in result->relids */
2540  if (IS_OUTER_JOIN(j->jointype))
2541  result->contains_outer = true;
2542 
2543  sub_state = reduce_outer_joins_pass1(j->larg);
2544  result->relids = bms_add_members(result->relids,
2545  sub_state->relids);
2546  result->contains_outer |= sub_state->contains_outer;
2547  result->sub_states = lappend(result->sub_states, sub_state);
2548 
2549  sub_state = reduce_outer_joins_pass1(j->rarg);
2550  result->relids = bms_add_members(result->relids,
2551  sub_state->relids);
2552  result->contains_outer |= sub_state->contains_outer;
2553  result->sub_states = lappend(result->sub_states, sub_state);
2554  }
2555  else
2556  elog(ERROR, "unrecognized node type: %d",
2557  (int) nodeTag(jtnode));
2558  return result;
2559 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:730
List * fromlist
Definition: primnodes.h:1479
Node * larg
Definition: primnodes.h:1459
#define ERROR
Definition: elog.h:43
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:245
List * lappend(List *list, void *datum)
Definition: list.c:128
Node * rarg
Definition: primnodes.h:1460
static reduce_outer_joins_state * reduce_outer_joins_pass1(Node *jtnode)
JoinType jointype
Definition: primnodes.h:1457
#define lfirst(lc)
Definition: pg_list.h:106
#define nodeTag(nodeptr)
Definition: nodes.h:522
void * palloc(Size size)
Definition: mcxt.c:924
#define elog
Definition: elog.h:219
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:821

◆ reduce_outer_joins_pass2()

static void reduce_outer_joins_pass2 ( Node jtnode,
reduce_outer_joins_state state,
PlannerInfo root,
Relids  nonnullable_rels,
List nonnullable_vars,
List forced_null_vars 
)
static

Definition at line 2572 of file prepjointree.c.

References Assert, bms_add_members(), bms_free(), bms_overlap(), reduce_outer_joins_state::contains_outer, elog, ERROR, find_forced_null_vars(), find_nonnullable_rels(), find_nonnullable_vars(), forboth, FromExpr::fromlist, IsA, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_SEMI, RangeTblEntry::jointype, JoinExpr::jointype, JoinExpr::larg, lfirst, linitial, list_concat(), list_intersection(), list_length(), lsecond, NIL, nodeTag, PlannerInfo::parse, pull_varnos(), JoinExpr::quals, FromExpr::quals, JoinExpr::rarg, reduce_outer_joins_state::relids, rt_fetch, Query::rtable, RTE_JOIN, RangeTblEntry::rtekind, JoinExpr::rtindex, and reduce_outer_joins_state::sub_states.

Referenced by reduce_outer_joins().

2578 {
2579  /*
2580  * pass 2 should never descend as far as an empty subnode or base rel,
2581  * because it's only called on subtrees marked as contains_outer.
2582  */
2583  if (jtnode == NULL)
2584  elog(ERROR, "reached empty jointree");
2585  if (IsA(jtnode, RangeTblRef))
2586  elog(ERROR, "reached base rel");
2587  else if (IsA(jtnode, FromExpr))
2588  {
2589  FromExpr *f = (FromExpr *) jtnode;
2590  ListCell *l;
2591  ListCell *s;
2592  Relids pass_nonnullable_rels;
2593  List *pass_nonnullable_vars;
2594  List *pass_forced_null_vars;
2595 
2596  /* Scan quals to see if we can add any constraints */
2597  pass_nonnullable_rels = find_nonnullable_rels(f->quals);
2598  pass_nonnullable_rels = bms_add_members(pass_nonnullable_rels,
2599  nonnullable_rels);
2600  /* NB: we rely on list_concat to not damage its second argument */
2601  pass_nonnullable_vars = find_nonnullable_vars(f->quals);
2602  pass_nonnullable_vars = list_concat(pass_nonnullable_vars,
2603  nonnullable_vars);
2604  pass_forced_null_vars = find_forced_null_vars(f->quals);
2605  pass_forced_null_vars = list_concat(pass_forced_null_vars,
2606  forced_null_vars);
2607  /* And recurse --- but only into interesting subtrees */
2609  forboth(l, f->fromlist, s, state->sub_states)
2610  {
2611  reduce_outer_joins_state *sub_state = lfirst(s);
2612 
2613  if (sub_state->contains_outer)
2614  reduce_outer_joins_pass2(lfirst(l), sub_state, root,
2615  pass_nonnullable_rels,
2616  pass_nonnullable_vars,
2617  pass_forced_null_vars);
2618  }
2619  bms_free(pass_nonnullable_rels);
2620  /* can't so easily clean up var lists, unfortunately */
2621  }
2622  else if (IsA(jtnode, JoinExpr))
2623  {
2624  JoinExpr *j = (JoinExpr *) jtnode;
2625  int rtindex = j->rtindex;
2626  JoinType jointype = j->jointype;
2627  reduce_outer_joins_state *left_state = linitial(state->sub_states);
2628  reduce_outer_joins_state *right_state = lsecond(state->sub_states);
2629  List *local_nonnullable_vars = NIL;
2630  bool computed_local_nonnullable_vars = false;
2631 
2632  /* Can we simplify this join? */
2633  switch (jointype)
2634  {
2635  case JOIN_INNER:
2636  break;
2637  case JOIN_LEFT:
2638  if (bms_overlap(nonnullable_rels, right_state->relids))
2639  jointype = JOIN_INNER;
2640  break;
2641  case JOIN_RIGHT:
2642  if (bms_overlap(nonnullable_rels, left_state->relids))
2643  jointype = JOIN_INNER;
2644  break;
2645  case JOIN_FULL:
2646  if (bms_overlap(nonnullable_rels, left_state->relids))
2647  {
2648  if (bms_overlap(nonnullable_rels, right_state->relids))
2649  jointype = JOIN_INNER;
2650  else
2651  jointype = JOIN_LEFT;
2652  }
2653  else
2654  {
2655  if (bms_overlap(nonnullable_rels, right_state->relids))
2656  jointype = JOIN_RIGHT;
2657  }
2658  break;
2659  case JOIN_SEMI:
2660  case JOIN_ANTI:
2661 
2662  /*
2663  * These could only have been introduced by pull_up_sublinks,
2664  * so there's no way that upper quals could refer to their
2665  * righthand sides, and no point in checking.
2666  */
2667  break;
2668  default:
2669  elog(ERROR, "unrecognized join type: %d",
2670  (int) jointype);
2671  break;
2672  }
2673 
2674  /*
2675  * Convert JOIN_RIGHT to JOIN_LEFT. Note that in the case where we
2676  * reduced JOIN_FULL to JOIN_RIGHT, this will mean the JoinExpr no
2677  * longer matches the internal ordering of any CoalesceExpr's built to
2678  * represent merged join variables. We don't care about that at
2679  * present, but be wary of it ...
2680  */
2681  if (jointype == JOIN_RIGHT)
2682  {
2683  Node *tmparg;
2684 
2685  tmparg = j->larg;
2686  j->larg = j->rarg;
2687  j->rarg = tmparg;
2688  jointype = JOIN_LEFT;
2689  right_state = linitial(state->sub_states);
2690  left_state = lsecond(state->sub_states);
2691  }
2692 
2693  /*
2694  * See if we can reduce JOIN_LEFT to JOIN_ANTI. This is the case if
2695  * the join's own quals are strict for any var that was forced null by
2696  * higher qual levels. NOTE: there are other ways that we could
2697  * detect an anti-join, in particular if we were to check whether Vars
2698  * coming from the RHS must be non-null because of table constraints.
2699  * That seems complicated and expensive though (in particular, one
2700  * would have to be wary of lower outer joins). For the moment this
2701  * seems sufficient.
2702  */
2703  if (jointype == JOIN_LEFT)
2704  {
2705  List *overlap;
2706 
2707  local_nonnullable_vars = find_nonnullable_vars(j->quals);
2708  computed_local_nonnullable_vars = true;
2709 
2710  /*
2711  * It's not sufficient to check whether local_nonnullable_vars and
2712  * forced_null_vars overlap: we need to know if the overlap
2713  * includes any RHS variables.
2714  */
2715  overlap = list_intersection(local_nonnullable_vars,
2716  forced_null_vars);
2717  if (overlap != NIL &&
2718  bms_overlap(pull_varnos((Node *) overlap),
2719  right_state->relids))
2720  jointype = JOIN_ANTI;
2721  }
2722 
2723  /* Apply the jointype change, if any, to both jointree node and RTE */
2724  if (rtindex && jointype != j->jointype)
2725  {
2726  RangeTblEntry *rte = rt_fetch(rtindex, root->parse->rtable);
2727 
2728  Assert(rte->rtekind == RTE_JOIN);
2729  Assert(rte->jointype == j->jointype);
2730  rte->jointype = jointype;
2731  }
2732  j->jointype = jointype;
2733 
2734  /* Only recurse if there's more to do below here */
2735  if (left_state->contains_outer || right_state->contains_outer)
2736  {
2737  Relids local_nonnullable_rels;
2738  List *local_forced_null_vars;
2739  Relids pass_nonnullable_rels;
2740  List *pass_nonnullable_vars;
2741  List *pass_forced_null_vars;
2742 
2743  /*
2744  * If this join is (now) inner, we can add any constraints its
2745  * quals provide to those we got from above. But if it is outer,
2746  * we can pass down the local constraints only into the nullable
2747  * side, because an outer join never eliminates any rows from its
2748  * non-nullable side. Also, there is no point in passing upper
2749  * constraints into the nullable side, since if there were any
2750  * we'd have been able to reduce the join. (In the case of upper
2751  * forced-null constraints, we *must not* pass them into the
2752  * nullable side --- they either applied here, or not.) The upshot
2753  * is that we pass either the local or the upper constraints,
2754  * never both, to the children of an outer join.
2755  *
2756  * Note that a SEMI join works like an inner join here: it's okay
2757  * to pass down both local and upper constraints. (There can't be
2758  * any upper constraints affecting its inner side, but it's not
2759  * worth having a separate code path to avoid passing them.)
2760  *
2761  * At a FULL join we just punt and pass nothing down --- is it
2762  * possible to be smarter?
2763  */
2764  if (jointype != JOIN_FULL)
2765  {
2766  local_nonnullable_rels = find_nonnullable_rels(j->quals);
2767  if (!computed_local_nonnullable_vars)
2768  local_nonnullable_vars = find_nonnullable_vars(j->quals);
2769  local_forced_null_vars = find_forced_null_vars(j->quals);
2770  if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
2771  {
2772  /* OK to merge upper and local constraints */
2773  local_nonnullable_rels = bms_add_members(local_nonnullable_rels,
2774  nonnullable_rels);
2775  local_nonnullable_vars = list_concat(local_nonnullable_vars,
2776  nonnullable_vars);
2777  local_forced_null_vars = list_concat(local_forced_null_vars,
2778  forced_null_vars);
2779  }
2780  }
2781  else
2782  {
2783  /* no use in calculating these */
2784  local_nonnullable_rels = NULL;
2785  local_forced_null_vars = NIL;
2786  }
2787 
2788  if (left_state->contains_outer)
2789  {
2790  if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
2791  {
2792  /* pass union of local and upper constraints */
2793  pass_nonnullable_rels = local_nonnullable_rels;
2794  pass_nonnullable_vars = local_nonnullable_vars;
2795  pass_forced_null_vars = local_forced_null_vars;
2796  }
2797  else if (jointype != JOIN_FULL) /* ie, LEFT or ANTI */
2798  {
2799  /* can't pass local constraints to non-nullable side */
2800  pass_nonnullable_rels = nonnullable_rels;
2801  pass_nonnullable_vars = nonnullable_vars;
2802  pass_forced_null_vars = forced_null_vars;
2803  }
2804  else
2805  {
2806  /* no constraints pass through JOIN_FULL */
2807  pass_nonnullable_rels = NULL;
2808  pass_nonnullable_vars = NIL;
2809  pass_forced_null_vars = NIL;
2810  }
2811  reduce_outer_joins_pass2(j->larg, left_state, root,
2812  pass_nonnullable_rels,
2813  pass_nonnullable_vars,
2814  pass_forced_null_vars);
2815  }
2816 
2817  if (right_state->contains_outer)
2818  {
2819  if (jointype != JOIN_FULL) /* ie, INNER/LEFT/SEMI/ANTI */
2820  {
2821  /* pass appropriate constraints, per comment above */
2822  pass_nonnullable_rels = local_nonnullable_rels;
2823  pass_nonnullable_vars = local_nonnullable_vars;
2824  pass_forced_null_vars = local_forced_null_vars;
2825  }
2826  else
2827  {
2828  /* no constraints pass through JOIN_FULL */
2829  pass_nonnullable_rels = NULL;
2830  pass_nonnullable_vars = NIL;
2831  pass_forced_null_vars = NIL;
2832  }
2833  reduce_outer_joins_pass2(j->rarg, right_state, root,
2834  pass_nonnullable_rels,
2835  pass_nonnullable_vars,
2836  pass_forced_null_vars);
2837  }
2838  bms_free(local_nonnullable_rels);
2839  }
2840  }
2841  else
2842  elog(ERROR, "unrecognized node type: %d",
2843  (int) nodeTag(jtnode));
2844 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Query * parse
Definition: relation.h:169
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
Definition: nodes.h:517
List * list_concat(List *list1, List *list2)
Definition: list.c:321
List * fromlist
Definition: primnodes.h:1479
Node * quals
Definition: primnodes.h:1480
#define lsecond(l)
Definition: pg_list.h:116
JoinType
Definition: nodes.h:681
Node * larg
Definition: primnodes.h:1459
List * find_forced_null_vars(Node *node)
Definition: clauses.c:2062
#define linitial(l)
Definition: pg_list.h:111
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:43
Relids find_nonnullable_rels(Node *clause)
Definition: clauses.c:1661
JoinType jointype
Definition: parsenodes.h:1005
List * list_intersection(const List *list1, const List *list2)
Definition: list.c:800
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Relids pull_varnos(Node *node)
Definition: var.c:95
Node * quals
Definition: primnodes.h:1462
List * find_nonnullable_vars(Node *clause)
Definition: clauses.c:1869
void bms_free(Bitmapset *a)
Definition: bitmapset.c:267
Node * rarg
Definition: primnodes.h:1460
JoinType jointype
Definition: primnodes.h:1457
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
static int list_length(const List *l)
Definition: pg_list.h:89
#define nodeTag(nodeptr)
Definition: nodes.h:522
RTEKind rtekind
Definition: parsenodes.h:962
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:509
#define elog
Definition: elog.h:219
static void reduce_outer_joins_pass2(Node *jtnode, reduce_outer_joins_state *state, PlannerInfo *root, Relids nonnullable_rels, List *nonnullable_vars, List *forced_null_vars)
int rtindex
Definition: primnodes.h:1464
Definition: pg_list.h:45
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:821

◆ replace_vars_in_jointree()

static void replace_vars_in_jointree ( Node jtnode,
pullup_replace_vars_context context,
JoinExpr lowest_nulling_outer_join 
)
static

Definition at line 1956 of file prepjointree.c.

References Assert, elog, ERROR, FromExpr::fromlist, RangeTblEntry::functions, IsA, JoinExpr::larg, RangeTblEntry::lateral, lfirst, pullup_replace_vars_context::need_phvs, nodeTag, PlannerInfo::parse, pullup_replace_vars(), pullup_replace_vars_subquery(), JoinExpr::quals, FromExpr::quals, JoinExpr::rarg, pullup_replace_vars_context::root, rt_fetch, Query::rtable, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, RangeTblEntry::subquery, RangeTblEntry::tablefunc, RangeTblEntry::tablesample, pullup_replace_vars_context::target_rte, RangeTblEntry::values_lists, and pullup_replace_vars_context::varno.

Referenced by pull_up_simple_subquery(), and pull_up_simple_values().

1959 {
1960  if (jtnode == NULL)
1961  return;
1962  if (IsA(jtnode, RangeTblRef))
1963  {
1964  /*
1965  * If the RangeTblRef refers to a LATERAL subquery (that isn't the
1966  * same subquery we're pulling up), it might contain references to the
1967  * target subquery, which we must replace. We drive this from the
1968  * jointree scan, rather than a scan of the rtable, for a couple of
1969  * reasons: we can avoid processing no-longer-referenced RTEs, and we
1970  * can use the appropriate setting of need_phvs depending on whether
1971  * the RTE is above possibly-nulling outer joins or not.
1972  */
1973  int varno = ((RangeTblRef *) jtnode)->rtindex;
1974 
1975  if (varno != context->varno) /* ignore target subquery itself */
1976  {
1977  RangeTblEntry *rte = rt_fetch(varno, context->root->parse->rtable);
1978 
1979  Assert(rte != context->target_rte);
1980  if (rte->lateral)
1981  {
1982  switch (rte->rtekind)
1983  {
1984  case RTE_RELATION:
1985  /* shouldn't be marked LATERAL unless tablesample */
1986  Assert(rte->tablesample);
1987  rte->tablesample = (TableSampleClause *)
1989  context);
1990  break;
1991  case RTE_SUBQUERY:
1992  rte->subquery =
1994  context);
1995  break;
1996  case RTE_FUNCTION:
1997  rte->functions = (List *)
1999  context);
2000  break;
2001  case RTE_TABLEFUNC:
2002  rte->tablefunc = (TableFunc *)
2004  context);
2005  break;
2006  case RTE_VALUES:
2007  rte->values_lists = (List *)
2009  context);
2010  break;
2011  case RTE_JOIN:
2012  case RTE_CTE:
2013  case RTE_NAMEDTUPLESTORE:
2014  /* these shouldn't be marked LATERAL */
2015  Assert(false);
2016  break;
2017  }
2018  }
2019  }
2020  }
2021  else if (IsA(jtnode, FromExpr))
2022  {
2023  FromExpr *f = (FromExpr *) jtnode;
2024  ListCell *l;
2025 
2026  foreach(l, f->fromlist)
2027  replace_vars_in_jointree(lfirst(l), context,
2028  lowest_nulling_outer_join);
2029  f->quals = pullup_replace_vars(f->quals, context);
2030  }
2031  else if (IsA(jtnode, JoinExpr))
2032  {
2033  JoinExpr *j = (JoinExpr *) jtnode;
2034  bool save_need_phvs = context->need_phvs;
2035 
2036  if (j == lowest_nulling_outer_join)
2037  {
2038  /* no more PHVs in or below this join */
2039  context->need_phvs = false;
2040  lowest_nulling_outer_join = NULL;
2041  }
2042  replace_vars_in_jointree(j->larg, context, lowest_nulling_outer_join);
2043  replace_vars_in_jointree(j->rarg, context, lowest_nulling_outer_join);
2044  j->quals = pullup_replace_vars(j->quals, context);
2045 
2046  /*
2047  * We don't bother to update the colvars list, since it won't be used
2048  * again ...
2049  */
2050  context->need_phvs = save_need_phvs;
2051  }
2052  else
2053  elog(ERROR, "unrecognized node type: %d",
2054  (int) nodeTag(jtnode));
2055 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
Query * parse
Definition: relation.h:169
Definition: nodes.h:517
List * fromlist
Definition: primnodes.h:1479
List * values_lists
Definition: parsenodes.h:1027
Node * quals
Definition: primnodes.h:1480
Node * larg
Definition: primnodes.h:1459
List * rtable
Definition: parsenodes.h:137
#define ERROR
Definition: elog.h:43
RangeTblEntry * target_rte
Definition: prepjointree.c:44
TableFunc * tablefunc
Definition: parsenodes.h:1022
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Node * quals
Definition: primnodes.h:1462
Node * rarg
Definition: primnodes.h:1460
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
List * functions
Definition: parsenodes.h:1016
#define nodeTag(nodeptr)
Definition: nodes.h:522
RTEKind rtekind
Definition: parsenodes.h:962
static void replace_vars_in_jointree(Node *jtnode, pullup_replace_vars_context *context, JoinExpr *lowest_nulling_outer_join)
Query * subquery
Definition: parsenodes.h:985
static Query * pullup_replace_vars_subquery(Query *query, pullup_replace_vars_context *context)
#define elog
Definition: elog.h:219
static Node * pullup_replace_vars(Node *expr, pullup_replace_vars_context *context)
Definition: pg_list.h:45
struct TableSampleClause * tablesample
Definition: parsenodes.h:980

◆ substitute_multiple_relids()

static void substitute_multiple_relids ( Node node,
int  varno,
Relids  subrelids 
)
static

Definition at line 2909 of file prepjointree.c.

References query_or_expression_tree_walker(), substitute_multiple_relids_context::sublevels_up, substitute_multiple_relids_context::subrelids, substitute_multiple_relids_walker(), pullup_replace_vars_context::varno, and substitute_multiple_relids_context::varno.

Referenced by fix_append_rel_relids(), and pull_up_simple_subquery().

2910 {
2912 
2913  context.varno = varno;
2914  context.sublevels_up = 0;
2915  context.subrelids = subrelids;
2916 
2917  /*
2918  * Must be prepared to start with a Query or a bare expression tree.
2919  */
2922  (void *) &context,
2923  0);
2924 }
static bool substitute_multiple_relids_walker(Node *node, substitute_multiple_relids_context *context)
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3203

◆ substitute_multiple_relids_walker()

static bool substitute_multiple_relids_walker ( Node node,
substitute_multiple_relids_context context 
)
static

Definition at line 2867 of file prepjointree.c.

References Assert, bms_del_member(), bms_is_member(), bms_union(), expression_tree_walker(), IsA, PlaceHolderVar::phlevelsup, PlaceHolderVar::phrels, query_tree_walker(), substitute_multiple_relids_context::sublevels_up, substitute_multiple_relids_context::subrelids, and substitute_multiple_relids_context::varno.

Referenced by substitute_multiple_relids().

2869 {
2870  if (node == NULL)
2871  return false;
2872  if (IsA(node, PlaceHolderVar))
2873  {
2874  PlaceHolderVar *phv = (PlaceHolderVar *) node;
2875 
2876  if (phv->phlevelsup == context->sublevels_up &&
2877  bms_is_member(context->varno, phv->phrels))
2878  {
2879  phv->phrels = bms_union(phv->phrels,
2880  context->subrelids);
2881  phv->phrels = bms_del_member(phv->phrels,
2882  context->varno);
2883  }
2884  /* fall through to examine children */
2885  }
2886  if (IsA(node, Query))
2887  {
2888  /* Recurse into subselects */
2889  bool result;
2890 
2891  context->sublevels_up++;
2892  result = query_tree_walker((Query *) node,
2894  (void *) context, 0);
2895  context->sublevels_up--;
2896  return result;
2897  }
2898  /* Shouldn't need to handle planner auxiliary nodes here */
2899  Assert(!IsA(node, SpecialJoinInfo));
2900  Assert(!IsA(node, AppendRelInfo));
2901  Assert(!IsA(node, PlaceHolderInfo));
2902  Assert(!IsA(node, MinMaxAggInfo));
2903 
2905  (void *) context);
2906 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2256
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
static bool substitute_multiple_relids_walker(Node *node, substitute_multiple_relids_context *context)
Relids phrels
Definition: relation.h:2000
#define Assert(condition)
Definition: c.h:699
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:284
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1834
Index phlevelsup
Definition: relation.h:2002
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:801
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:486