PostgreSQL Source Code  git master
prepjointree.c File Reference
#include "postgres.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/optimizer.h"
#include "optimizer/placeholder.h"
#include "optimizer/prep.h"
#include "optimizer/subselect.h"
#include "optimizer/tlist.h"
#include "parser/parse_relation.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteManip.h"
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  find_dependent_phvs_context
 
struct  substitute_phv_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)
 
static Nodepull_up_simple_subquery (PlannerInfo *root, Node *jtnode, RangeTblEntry *rte, JoinExpr *lowest_outer_join, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
 
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, int newvarno, AppendRelInfo *appinfo)
 
static bool is_simple_subquery (PlannerInfo *root, Query *subquery, RangeTblEntry *rte, JoinExpr *lowest_outer_join)
 
static Nodepull_up_simple_values (PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
 
static bool is_simple_values (PlannerInfo *root, RangeTblEntry *rte)
 
static Nodepull_up_constant_function (PlannerInfo *root, Node *jtnode, RangeTblEntry *rte, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
 
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 (PlannerInfo *root, Node *jtnode, bool restricted, Relids safe_upper_varnos)
 
static void perform_pullup_replace_vars (PlannerInfo *root, pullup_replace_vars_context *rvcontext, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
 
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 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 Noderemove_useless_results_recurse (PlannerInfo *root, Node *jtnode)
 
static int get_result_relid (PlannerInfo *root, Node *jtnode)
 
static void remove_result_refs (PlannerInfo *root, int varno, Node *newjtloc)
 
static bool find_dependent_phvs (PlannerInfo *root, int varno)
 
static bool find_dependent_phvs_in_jointree (PlannerInfo *root, Node *node, int varno)
 
static void substitute_phv_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 replace_empty_jointree (Query *parse)
 
void pull_up_sublinks (PlannerInfo *root)
 
void preprocess_function_rtes (PlannerInfo *root)
 
void pull_up_subqueries (PlannerInfo *root)
 
void flatten_simple_union_all (PlannerInfo *root)
 
void reduce_outer_joins (PlannerInfo *root)
 
void remove_useless_result_rtes (PlannerInfo *root)
 
static bool find_dependent_phvs_walker (Node *node, find_dependent_phvs_context *context)
 
static bool substitute_phv_relids_walker (Node *node, substitute_phv_relids_context *context)
 
Relids get_relids_in_jointree (Node *jtnode, bool include_joins)
 
Relids get_relids_for_join (Query *query, int joinrelid)
 

Typedef Documentation

◆ pullup_replace_vars_context

◆ reduce_outer_joins_state

Function Documentation

◆ find_dependent_phvs()

static bool find_dependent_phvs ( PlannerInfo root,
int  varno 
)
static

Definition at line 3347 of file prepjointree.c.

References bms_make_singleton(), find_dependent_phvs_walker(), PlannerInfo::glob, PlannerGlobal::lastPHId, PlannerInfo::parse, query_tree_walker(), find_dependent_phvs_context::relids, and find_dependent_phvs_context::sublevels_up.

Referenced by remove_useless_results_recurse().

3348 {
3350 
3351  /* If there are no PHVs anywhere, we needn't work hard */
3352  if (root->glob->lastPHId == 0)
3353  return false;
3354 
3355  context.relids = bms_make_singleton(varno);
3356  context.sublevels_up = 0;
3357 
3358  return query_tree_walker(root->parse,
3360  (void *) &context,
3361  0);
3362 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355
Query * parse
Definition: pathnodes.h:162
static bool find_dependent_phvs_walker(Node *node, find_dependent_phvs_context *context)
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
PlannerGlobal * glob
Definition: pathnodes.h:164
Index lastPHId
Definition: pathnodes.h:116

◆ find_dependent_phvs_in_jointree()

static bool find_dependent_phvs_in_jointree ( PlannerInfo root,
Node node,
int  varno 
)
static

Definition at line 3365 of file prepjointree.c.

References bms_make_singleton(), bms_next_member(), find_dependent_phvs_walker(), get_relids_in_jointree(), PlannerInfo::glob, PlannerGlobal::lastPHId, RangeTblEntry::lateral, PlannerInfo::parse, range_table_entry_walker(), find_dependent_phvs_context::relids, rt_fetch, Query::rtable, and find_dependent_phvs_context::sublevels_up.

Referenced by remove_useless_results_recurse().

3366 {
3368  Relids subrelids;
3369  int relid;
3370 
3371  /* If there are no PHVs anywhere, we needn't work hard */
3372  if (root->glob->lastPHId == 0)
3373  return false;
3374 
3375  context.relids = bms_make_singleton(varno);
3376  context.sublevels_up = 0;
3377 
3378  /*
3379  * See if the jointree fragment itself contains references (in join quals)
3380  */
3381  if (find_dependent_phvs_walker(node, &context))
3382  return true;
3383 
3384  /*
3385  * Otherwise, identify the set of referenced RTEs (we can ignore joins,
3386  * since they should be flattened already, so their join alias lists no
3387  * longer matter), and tediously check each RTE. We can ignore RTEs that
3388  * are not marked LATERAL, though, since they couldn't possibly contain
3389  * any cross-references to other RTEs.
3390  */
3391  subrelids = get_relids_in_jointree(node, false);
3392  relid = -1;
3393  while ((relid = bms_next_member(subrelids, relid)) >= 0)
3394  {
3395  RangeTblEntry *rte = rt_fetch(relid, root->parse->rtable);
3396 
3397  if (rte->lateral &&
3400  (void *) &context,
3401  0))
3402  return true;
3403  }
3404 
3405  return false;
3406 }
Query * parse
Definition: pathnodes.h:162
bool range_table_entry_walker(RangeTblEntry *rte, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2475
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
static bool find_dependent_phvs_walker(Node *node, find_dependent_phvs_context *context)
List * rtable
Definition: parsenodes.h:147
Relids get_relids_in_jointree(Node *jtnode, bool include_joins)
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
PlannerGlobal * glob
Definition: pathnodes.h:164
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Index lastPHId
Definition: pathnodes.h:116

◆ find_dependent_phvs_walker()

static bool find_dependent_phvs_walker ( Node node,
find_dependent_phvs_context context 
)
static

Definition at line 3310 of file prepjointree.c.

References Assert, bms_equal(), expression_tree_walker(), IsA, PlaceHolderVar::phlevelsup, PlaceHolderVar::phrels, query_tree_walker(), find_dependent_phvs_context::relids, and find_dependent_phvs_context::sublevels_up.

Referenced by find_dependent_phvs(), and find_dependent_phvs_in_jointree().

3312 {
3313  if (node == NULL)
3314  return false;
3315  if (IsA(node, PlaceHolderVar))
3316  {
3317  PlaceHolderVar *phv = (PlaceHolderVar *) node;
3318 
3319  if (phv->phlevelsup == context->sublevels_up &&
3320  bms_equal(context->relids, phv->phrels))
3321  return true;
3322  /* fall through to examine children */
3323  }
3324  if (IsA(node, Query))
3325  {
3326  /* Recurse into subselects */
3327  bool result;
3328 
3329  context->sublevels_up++;
3330  result = query_tree_walker((Query *) node,
3332  (void *) context, 0);
3333  context->sublevels_up--;
3334  return result;
3335  }
3336  /* Shouldn't need to handle planner auxiliary nodes here */
3337  Assert(!IsA(node, SpecialJoinInfo));
3338  Assert(!IsA(node, AppendRelInfo));
3339  Assert(!IsA(node, PlaceHolderInfo));
3340  Assert(!IsA(node, MinMaxAggInfo));
3341 
3343  (void *) context);
3344 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
static bool find_dependent_phvs_walker(Node *node, find_dependent_phvs_context *context)
#define Assert(condition)
Definition: c.h:804
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
Index phlevelsup
Definition: pathnodes.h:2184
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94

◆ find_jointree_node_for_rel()

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

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

3601 {
3602  if (jtnode == NULL)
3603  return NULL;
3604  if (IsA(jtnode, RangeTblRef))
3605  {
3606  int varno = ((RangeTblRef *) jtnode)->rtindex;
3607 
3608  if (relid == varno)
3609  return jtnode;
3610  }
3611  else if (IsA(jtnode, FromExpr))
3612  {
3613  FromExpr *f = (FromExpr *) jtnode;
3614  ListCell *l;
3615 
3616  foreach(l, f->fromlist)
3617  {
3618  jtnode = find_jointree_node_for_rel(lfirst(l), relid);
3619  if (jtnode)
3620  return jtnode;
3621  }
3622  }
3623  else if (IsA(jtnode, JoinExpr))
3624  {
3625  JoinExpr *j = (JoinExpr *) jtnode;
3626 
3627  if (relid == j->rtindex)
3628  return jtnode;
3629  jtnode = find_jointree_node_for_rel(j->larg, relid);
3630  if (jtnode)
3631  return jtnode;
3632  jtnode = find_jointree_node_for_rel(j->rarg, relid);
3633  if (jtnode)
3634  return jtnode;
3635  }
3636  else
3637  elog(ERROR, "unrecognized node type: %d",
3638  (int) nodeTag(jtnode));
3639  return NULL;
3640 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
List * fromlist
Definition: primnodes.h:1564
Node * larg
Definition: primnodes.h:1543
static Node * find_jointree_node_for_rel(Node *jtnode, int relid)
#define ERROR
Definition: elog.h:46
Node * rarg
Definition: primnodes.h:1544
#define lfirst(lc)
Definition: pg_list.h:169
#define nodeTag(nodeptr)
Definition: nodes.h:541
#define elog(elevel,...)
Definition: elog.h:232
int rtindex
Definition: primnodes.h:1549

◆ fix_append_rel_relids()

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

Definition at line 3501 of file prepjointree.c.

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

Referenced by pull_up_simple_subquery(), and remove_result_refs().

3502 {
3503  ListCell *l;
3504  int subvarno = -1;
3505 
3506  /*
3507  * We only want to extract the member relid once, but we mustn't fail
3508  * immediately if there are multiple members; it could be that none of the
3509  * AppendRelInfo nodes refer to it. So compute it on first use. Note that
3510  * bms_singleton_member will complain if set is not singleton.
3511  */
3512  foreach(l, append_rel_list)
3513  {
3514  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
3515 
3516  /* The parent_relid shouldn't ever be a pullup target */
3517  Assert(appinfo->parent_relid != varno);
3518 
3519  if (appinfo->child_relid == varno)
3520  {
3521  if (subvarno < 0)
3522  subvarno = bms_singleton_member(subrelids);
3523  appinfo->child_relid = subvarno;
3524  }
3525 
3526  /* Also fix up any PHVs in its translated vars */
3528  varno, subrelids);
3529  }
3530 }
static void substitute_phv_relids(Node *node, int varno, Relids subrelids)
Definition: nodes.h:536
List * translated_vars
Definition: pathnodes.h:2334
int bms_singleton_member(const Bitmapset *a)
Definition: bitmapset.c:577
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Index child_relid
Definition: pathnodes.h:2307
Index parent_relid
Definition: pathnodes.h:2306

◆ flatten_simple_union_all()

void flatten_simple_union_all ( PlannerInfo root)

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

2467 {
2468  Query *parse = root->parse;
2469  SetOperationStmt *topop;
2470  Node *leftmostjtnode;
2471  int leftmostRTI;
2472  RangeTblEntry *leftmostRTE;
2473  int childRTI;
2474  RangeTblEntry *childRTE;
2475  RangeTblRef *rtr;
2476 
2477  /* Shouldn't be called unless query has setops */
2478  topop = castNode(SetOperationStmt, parse->setOperations);
2479  Assert(topop);
2480 
2481  /* Can't optimize away a recursive UNION */
2482  if (root->hasRecursion)
2483  return;
2484 
2485  /*
2486  * Recursively check the tree of set operations. If not all UNION ALL
2487  * with identical column types, punt.
2488  */
2489  if (!is_simple_union_all_recurse((Node *) topop, parse, topop->colTypes))
2490  return;
2491 
2492  /*
2493  * Locate the leftmost leaf query in the setops tree. The upper query's
2494  * Vars all refer to this RTE (see transformSetOperationStmt).
2495  */
2496  leftmostjtnode = topop->larg;
2497  while (leftmostjtnode && IsA(leftmostjtnode, SetOperationStmt))
2498  leftmostjtnode = ((SetOperationStmt *) leftmostjtnode)->larg;
2499  Assert(leftmostjtnode && IsA(leftmostjtnode, RangeTblRef));
2500  leftmostRTI = ((RangeTblRef *) leftmostjtnode)->rtindex;
2501  leftmostRTE = rt_fetch(leftmostRTI, parse->rtable);
2502  Assert(leftmostRTE->rtekind == RTE_SUBQUERY);
2503 
2504  /*
2505  * Make a copy of the leftmost RTE and add it to the rtable. This copy
2506  * will represent the leftmost leaf query in its capacity as a member of
2507  * the appendrel. The original will represent the appendrel as a whole.
2508  * (We must do things this way because the upper query's Vars have to be
2509  * seen as referring to the whole appendrel.)
2510  */
2511  childRTE = copyObject(leftmostRTE);
2512  parse->rtable = lappend(parse->rtable, childRTE);
2513  childRTI = list_length(parse->rtable);
2514 
2515  /* Modify the setops tree to reference the child copy */
2516  ((RangeTblRef *) leftmostjtnode)->rtindex = childRTI;
2517 
2518  /* Modify the formerly-leftmost RTE to mark it as an appendrel parent */
2519  leftmostRTE->inh = true;
2520 
2521  /*
2522  * Form a RangeTblRef for the appendrel, and insert it into FROM. The top
2523  * Query of a setops tree should have had an empty FromClause initially.
2524  */
2525  rtr = makeNode(RangeTblRef);
2526  rtr->rtindex = leftmostRTI;
2527  Assert(parse->jointree->fromlist == NIL);
2528  parse->jointree->fromlist = list_make1(rtr);
2529 
2530  /*
2531  * Now pretend the query has no setops. We must do this before trying to
2532  * do subquery pullup, because of Assert in pull_up_simple_subquery.
2533  */
2534  parse->setOperations = NULL;
2535 
2536  /*
2537  * Build AppendRelInfo information, and apply pull_up_subqueries to the
2538  * leaf queries of the UNION ALL. (We must do that now because they
2539  * weren't previously referenced by the jointree, and so were missed by
2540  * the main invocation of pull_up_subqueries.)
2541  */
2542  pull_up_union_leaf_queries((Node *) topop, root, leftmostRTI, parse, 0);
2543 }
#define NIL
Definition: pg_list.h:65
static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex, Query *setOpQuery, int childRToffset)
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Query * parse
Definition: pathnodes.h:162
FromExpr * jointree
Definition: parsenodes.h:148
#define castNode(_type_, nodeptr)
Definition: nodes.h:605
Definition: nodes.h:536
static bool is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
List * fromlist
Definition: primnodes.h:1564
bool hasRecursion
Definition: pathnodes.h:352
#define list_make1(x1)
Definition: pg_list.h:206
List * rtable
Definition: parsenodes.h:147
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
List * lappend(List *list, void *datum)
Definition: list.c:336
#define makeNode(_type_)
Definition: nodes.h:584
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:1007
Node * setOperations
Definition: parsenodes.h:177
#define copyObject(obj)
Definition: nodes.h:652
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:673

◆ get_relids_for_join()

Relids get_relids_for_join ( Query query,
int  joinrelid 
)

Definition at line 3583 of file prepjointree.c.

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

Referenced by alias_relid_set().

3584 {
3585  Node *jtnode;
3586 
3587  jtnode = find_jointree_node_for_rel((Node *) query->jointree,
3588  joinrelid);
3589  if (!jtnode)
3590  elog(ERROR, "could not find join node %d", joinrelid);
3591  return get_relids_in_jointree(jtnode, false);
3592 }
FromExpr * jointree
Definition: parsenodes.h:148
Definition: nodes.h:536
static Node * find_jointree_node_for_rel(Node *jtnode, int relid)
#define ERROR
Definition: elog.h:46
Relids get_relids_in_jointree(Node *jtnode, bool include_joins)
#define elog(elevel,...)
Definition: elog.h:232

◆ get_relids_in_jointree()

Relids get_relids_in_jointree ( Node jtnode,
bool  include_joins 
)

Definition at line 3539 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(), find_dependent_phvs_in_jointree(), get_relids_for_join(), get_relids_in_jointree(), is_simple_subquery(), preprocess_rowmarks(), process_implied_equality(), pull_up_simple_subquery(), and remove_result_refs().

3540 {
3541  Relids result = NULL;
3542 
3543  if (jtnode == NULL)
3544  return result;
3545  if (IsA(jtnode, RangeTblRef))
3546  {
3547  int varno = ((RangeTblRef *) jtnode)->rtindex;
3548 
3549  result = bms_make_singleton(varno);
3550  }
3551  else if (IsA(jtnode, FromExpr))
3552  {
3553  FromExpr *f = (FromExpr *) jtnode;
3554  ListCell *l;
3555 
3556  foreach(l, f->fromlist)
3557  {
3558  result = bms_join(result,
3560  include_joins));
3561  }
3562  }
3563  else if (IsA(jtnode, JoinExpr))
3564  {
3565  JoinExpr *j = (JoinExpr *) jtnode;
3566 
3567  result = get_relids_in_jointree(j->larg, include_joins);
3568  result = bms_join(result,
3569  get_relids_in_jointree(j->rarg, include_joins));
3570  if (include_joins && j->rtindex)
3571  result = bms_add_member(result, j->rtindex);
3572  }
3573  else
3574  elog(ERROR, "unrecognized node type: %d",
3575  (int) nodeTag(jtnode));
3576  return result;
3577 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
List * fromlist
Definition: primnodes.h:1564
Node * larg
Definition: primnodes.h:1543
#define ERROR
Definition: elog.h:46
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:949
Relids get_relids_in_jointree(Node *jtnode, bool include_joins)
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
Node * rarg
Definition: primnodes.h:1544
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define nodeTag(nodeptr)
Definition: nodes.h:541
#define elog(elevel,...)
Definition: elog.h:232
int rtindex
Definition: primnodes.h:1549

◆ get_result_relid()

static int get_result_relid ( PlannerInfo root,
Node jtnode 
)
static

Definition at line 3241 of file prepjointree.c.

References IsA, PlannerInfo::parse, rt_fetch, Query::rtable, RTE_RESULT, and pullup_replace_vars_context::varno.

Referenced by remove_useless_results_recurse().

3242 {
3243  int varno;
3244 
3245  if (!IsA(jtnode, RangeTblRef))
3246  return 0;
3247  varno = ((RangeTblRef *) jtnode)->rtindex;
3248  if (rt_fetch(varno, root->parse->rtable)->rtekind != RTE_RESULT)
3249  return 0;
3250  return varno;
3251 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Query * parse
Definition: pathnodes.h:162
List * rtable
Definition: parsenodes.h:147
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31

◆ is_safe_append_member()

static bool is_safe_append_member ( Query subquery)
static

Definition at line 1902 of file prepjointree.c.

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

Referenced by pull_up_simple_subquery(), and pull_up_subqueries_recurse().

1903 {
1904  FromExpr *jtnode;
1905 
1906  /*
1907  * It's only safe to pull up the child if its jointree contains exactly
1908  * one RTE, else the AppendRelInfo data structure breaks. The one base RTE
1909  * could be buried in several levels of FromExpr, however. Also, if the
1910  * child's jointree is completely empty, we can pull up because
1911  * pull_up_simple_subquery will insert a single RTE_RESULT RTE instead.
1912  *
1913  * Also, the child can't have any WHERE quals because there's no place to
1914  * put them in an appendrel. (This is a bit annoying...) If we didn't
1915  * need to check this, we'd just test whether get_relids_in_jointree()
1916  * yields a singleton set, to be more consistent with the coding of
1917  * fix_append_rel_relids().
1918  */
1919  jtnode = subquery->jointree;
1920  Assert(IsA(jtnode, FromExpr));
1921  /* Check the completely-empty case */
1922  if (jtnode->fromlist == NIL && jtnode->quals == NULL)
1923  return true;
1924  /* Check the more general case */
1925  while (IsA(jtnode, FromExpr))
1926  {
1927  if (jtnode->quals != NULL)
1928  return false;
1929  if (list_length(jtnode->fromlist) != 1)
1930  return false;
1931  jtnode = linitial(jtnode->fromlist);
1932  }
1933  if (!IsA(jtnode, RangeTblRef))
1934  return false;
1935 
1936  return true;
1937 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
FromExpr * jointree
Definition: parsenodes.h:148
List * fromlist
Definition: primnodes.h:1564
Node * quals
Definition: primnodes.h:1565
#define linitial(l)
Definition: pg_list.h:174
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149

◆ is_simple_subquery()

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

Definition at line 1413 of file prepjointree.c.

References bms_is_subset(), CMD_SELECT, Query::commandType, contain_volatile_functions(), Query::cteList, Query::distinctClause, elog, ERROR, 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, pull_varnos_of_level(), RangeTblEntry::security_barrier, Query::setOperations, Query::sortClause, and Query::targetList.

Referenced by pull_up_simple_subquery(), and pull_up_subqueries_recurse().

1415 {
1416  /*
1417  * Let's just make sure it's a valid subselect ...
1418  */
1419  if (!IsA(subquery, Query) ||
1420  subquery->commandType != CMD_SELECT)
1421  elog(ERROR, "subquery is bogus");
1422 
1423  /*
1424  * Can't currently pull up a query with setops (unless it's simple UNION
1425  * ALL, which is handled by a different code path). Maybe after querytree
1426  * redesign...
1427  */
1428  if (subquery->setOperations)
1429  return false;
1430 
1431  /*
1432  * Can't pull up a subquery involving grouping, aggregation, SRFs,
1433  * sorting, limiting, or WITH. (XXX WITH could possibly be allowed later)
1434  *
1435  * We also don't pull up a subquery that has explicit FOR UPDATE/SHARE
1436  * clauses, because pullup would cause the locking to occur semantically
1437  * higher than it should. Implicit FOR UPDATE/SHARE is okay because in
1438  * that case the locking was originally declared in the upper query
1439  * anyway.
1440  */
1441  if (subquery->hasAggs ||
1442  subquery->hasWindowFuncs ||
1443  subquery->hasTargetSRFs ||
1444  subquery->groupClause ||
1445  subquery->groupingSets ||
1446  subquery->havingQual ||
1447  subquery->sortClause ||
1448  subquery->distinctClause ||
1449  subquery->limitOffset ||
1450  subquery->limitCount ||
1451  subquery->hasForUpdate ||
1452  subquery->cteList)
1453  return false;
1454 
1455  /*
1456  * Don't pull up if the RTE represents a security-barrier view; we
1457  * couldn't prevent information leakage once the RTE's Vars are scattered
1458  * about in the upper query.
1459  */
1460  if (rte->security_barrier)
1461  return false;
1462 
1463  /*
1464  * If the subquery is LATERAL, check for pullup restrictions from that.
1465  */
1466  if (rte->lateral)
1467  {
1468  bool restricted;
1469  Relids safe_upper_varnos;
1470 
1471  /*
1472  * The subquery's WHERE and JOIN/ON quals mustn't contain any lateral
1473  * references to rels outside a higher outer join (including the case
1474  * where the outer join is within the subquery itself). In such a
1475  * case, pulling up would result in a situation where we need to
1476  * postpone quals from below an outer join to above it, which is
1477  * probably completely wrong and in any case is a complication that
1478  * doesn't seem worth addressing at the moment.
1479  */
1480  if (lowest_outer_join != NULL)
1481  {
1482  restricted = true;
1483  safe_upper_varnos = get_relids_in_jointree((Node *) lowest_outer_join,
1484  true);
1485  }
1486  else
1487  {
1488  restricted = false;
1489  safe_upper_varnos = NULL; /* doesn't matter */
1490  }
1491 
1493  (Node *) subquery->jointree,
1494  restricted, safe_upper_varnos))
1495  return false;
1496 
1497  /*
1498  * If there's an outer join above the LATERAL subquery, also disallow
1499  * pullup if the subquery's targetlist has any references to rels
1500  * outside the outer join, since these might get pulled into quals
1501  * above the subquery (but in or below the outer join) and then lead
1502  * to qual-postponement issues similar to the case checked for above.
1503  * (We wouldn't need to prevent pullup if no such references appear in
1504  * outer-query quals, but we don't have enough info here to check
1505  * that. Also, maybe this restriction could be removed if we forced
1506  * such refs to be wrapped in PlaceHolderVars, even when they're below
1507  * the nearest outer join? But it's a pretty hokey usage, so not
1508  * clear this is worth sweating over.)
1509  */
1510  if (lowest_outer_join != NULL)
1511  {
1512  Relids lvarnos = pull_varnos_of_level(root,
1513  (Node *) subquery->targetList,
1514  1);
1515 
1516  if (!bms_is_subset(lvarnos, safe_upper_varnos))
1517  return false;
1518  }
1519  }
1520 
1521  /*
1522  * Don't pull up a subquery that has any volatile functions in its
1523  * targetlist. Otherwise we might introduce multiple evaluations of these
1524  * functions, if they get copied to multiple places in the upper query,
1525  * leading to surprising results. (Note: the PlaceHolderVar mechanism
1526  * doesn't quite guarantee single evaluation; else we could pull up anyway
1527  * and just wrap such items in PlaceHolderVars ...)
1528  */
1529  if (contain_volatile_functions((Node *) subquery->targetList))
1530  return false;
1531 
1532  return true;
1533 }
Node * limitOffset
Definition: parsenodes.h:171
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
List * sortClause
Definition: parsenodes.h:169
FromExpr * jointree
Definition: parsenodes.h:148
bool hasAggs
Definition: parsenodes.h:133
List * groupingSets
Definition: parsenodes.h:161
Definition: nodes.h:536
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:452
List * targetList
Definition: parsenodes.h:150
List * distinctClause
Definition: parsenodes.h:167
#define ERROR
Definition: elog.h:46
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Relids get_relids_in_jointree(Node *jtnode, bool include_joins)
Node * limitCount
Definition: parsenodes.h:172
bool security_barrier
Definition: parsenodes.h:1043
CmdType commandType
Definition: parsenodes.h:120
bool hasTargetSRFs
Definition: parsenodes.h:135
bool hasWindowFuncs
Definition: parsenodes.h:134
List * cteList
Definition: parsenodes.h:145
Node * setOperations
Definition: parsenodes.h:177
List * groupClause
Definition: parsenodes.h:158
#define elog(elevel,...)
Definition: elog.h:232
bool hasForUpdate
Definition: parsenodes.h:140
static bool jointree_contains_lateral_outer_refs(PlannerInfo *root, Node *jtnode, bool restricted, Relids safe_upper_varnos)
Node * havingQual
Definition: parsenodes.h:163
Relids pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
Definition: var.c:123

◆ is_simple_union_all()

static bool is_simple_union_all ( Query subquery)
static

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

1835 {
1836  SetOperationStmt *topop;
1837 
1838  /* Let's just make sure it's a valid subselect ... */
1839  if (!IsA(subquery, Query) ||
1840  subquery->commandType != CMD_SELECT)
1841  elog(ERROR, "subquery is bogus");
1842 
1843  /* Is it a set-operation query at all? */
1844  topop = castNode(SetOperationStmt, subquery->setOperations);
1845  if (!topop)
1846  return false;
1847 
1848  /* Can't handle ORDER BY, LIMIT/OFFSET, locking, or WITH */
1849  if (subquery->sortClause ||
1850  subquery->limitOffset ||
1851  subquery->limitCount ||
1852  subquery->rowMarks ||
1853  subquery->cteList)
1854  return false;
1855 
1856  /* Recursively check the tree of set operations */
1857  return is_simple_union_all_recurse((Node *) topop, subquery,
1858  topop->colTypes);
1859 }
Node * limitOffset
Definition: parsenodes.h:171
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
List * sortClause
Definition: parsenodes.h:169
#define castNode(_type_, nodeptr)
Definition: nodes.h:605
Definition: nodes.h:536
static bool is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
List * rowMarks
Definition: parsenodes.h:175
#define ERROR
Definition: elog.h:46
Node * limitCount
Definition: parsenodes.h:172
CmdType commandType
Definition: parsenodes.h:120
List * cteList
Definition: parsenodes.h:145
Node * setOperations
Definition: parsenodes.h:177
#define elog(elevel,...)
Definition: elog.h:232

◆ is_simple_union_all_recurse()

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

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

1863 {
1864  if (IsA(setOp, RangeTblRef))
1865  {
1866  RangeTblRef *rtr = (RangeTblRef *) setOp;
1867  RangeTblEntry *rte = rt_fetch(rtr->rtindex, setOpQuery->rtable);
1868  Query *subquery = rte->subquery;
1869 
1870  Assert(subquery != NULL);
1871 
1872  /* Leaf nodes are OK if they match the toplevel column types */
1873  /* We don't have to compare typmods or collations here */
1874  return tlist_same_datatypes(subquery->targetList, colTypes, true);
1875  }
1876  else if (IsA(setOp, SetOperationStmt))
1877  {
1878  SetOperationStmt *op = (SetOperationStmt *) setOp;
1879 
1880  /* Must be UNION ALL */
1881  if (op->op != SETOP_UNION || !op->all)
1882  return false;
1883 
1884  /* Recurse to check inputs */
1885  return is_simple_union_all_recurse(op->larg, setOpQuery, colTypes) &&
1886  is_simple_union_all_recurse(op->rarg, setOpQuery, colTypes);
1887  }
1888  else
1889  {
1890  elog(ERROR, "unrecognized node type: %d",
1891  (int) nodeTag(setOp));
1892  return false; /* keep compiler quiet */
1893  }
1894 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
bool tlist_same_datatypes(List *tlist, List *colTypes, bool junkOK)
Definition: tlist.c:237
static bool is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
List * targetList
Definition: parsenodes.h:150
List * rtable
Definition: parsenodes.h:147
#define ERROR
Definition: elog.h:46
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define Assert(condition)
Definition: c.h:804
SetOperation op
Definition: parsenodes.h:1723
#define nodeTag(nodeptr)
Definition: nodes.h:541
Query * subquery
Definition: parsenodes.h:1042
#define elog(elevel,...)
Definition: elog.h:232

◆ is_simple_values()

static bool is_simple_values ( PlannerInfo root,
RangeTblEntry rte 
)
static

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

1647 {
1648  Assert(rte->rtekind == RTE_VALUES);
1649 
1650  /*
1651  * There must be exactly one VALUES list, else it's not semantically
1652  * correct to replace the VALUES RTE with a RESULT RTE, nor would we have
1653  * a unique set of expressions to substitute into the parent query.
1654  */
1655  if (list_length(rte->values_lists) != 1)
1656  return false;
1657 
1658  /*
1659  * Because VALUES can't appear under an outer join (or at least, we won't
1660  * try to pull it up if it does), we need not worry about LATERAL, nor
1661  * about validity of PHVs for the VALUES' outputs.
1662  */
1663 
1664  /*
1665  * Don't pull up a VALUES that contains any set-returning or volatile
1666  * functions. The considerations here are basically identical to the
1667  * restrictions on a pull-able subquery's targetlist.
1668  */
1669  if (expression_returns_set((Node *) rte->values_lists) ||
1671  return false;
1672 
1673  /*
1674  * Do not pull up a VALUES that's not the only RTE in its parent query.
1675  * This is actually the only case that the parser will generate at the
1676  * moment, and assuming this is true greatly simplifies
1677  * pull_up_simple_values().
1678  */
1679  if (list_length(root->parse->rtable) != 1 ||
1680  rte != (RangeTblEntry *) linitial(root->parse->rtable))
1681  return false;
1682 
1683  return true;
1684 }
Query * parse
Definition: pathnodes.h:162
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:709
Definition: nodes.h:536
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:452
List * values_lists
Definition: parsenodes.h:1112
#define linitial(l)
Definition: pg_list.h:174
List * rtable
Definition: parsenodes.h:147
#define Assert(condition)
Definition: c.h:804
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:1007

◆ jointree_contains_lateral_outer_refs()

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

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

1953 {
1954  if (jtnode == NULL)
1955  return false;
1956  if (IsA(jtnode, RangeTblRef))
1957  return false;
1958  else if (IsA(jtnode, FromExpr))
1959  {
1960  FromExpr *f = (FromExpr *) jtnode;
1961  ListCell *l;
1962 
1963  /* First, recurse to check child joins */
1964  foreach(l, f->fromlist)
1965  {
1967  lfirst(l),
1968  restricted,
1969  safe_upper_varnos))
1970  return true;
1971  }
1972 
1973  /* Then check the top-level quals */
1974  if (restricted &&
1976  safe_upper_varnos))
1977  return true;
1978  }
1979  else if (IsA(jtnode, JoinExpr))
1980  {
1981  JoinExpr *j = (JoinExpr *) jtnode;
1982 
1983  /*
1984  * If this is an outer join, we mustn't allow any upper lateral
1985  * references in or below it.
1986  */
1987  if (j->jointype != JOIN_INNER)
1988  {
1989  restricted = true;
1990  safe_upper_varnos = NULL;
1991  }
1992 
1993  /* Check the child joins */
1995  j->larg,
1996  restricted,
1997  safe_upper_varnos))
1998  return true;
2000  j->rarg,
2001  restricted,
2002  safe_upper_varnos))
2003  return true;
2004 
2005  /* Check the JOIN's qual clauses */
2006  if (restricted &&
2008  safe_upper_varnos))
2009  return true;
2010  }
2011  else
2012  elog(ERROR, "unrecognized node type: %d",
2013  (int) nodeTag(jtnode));
2014  return false;
2015 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
List * fromlist
Definition: primnodes.h:1564
Node * quals
Definition: primnodes.h:1565
Node * larg
Definition: primnodes.h:1543
#define ERROR
Definition: elog.h:46
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Node * quals
Definition: primnodes.h:1547
Node * rarg
Definition: primnodes.h:1544
JoinType jointype
Definition: primnodes.h:1541
#define lfirst(lc)
Definition: pg_list.h:169
#define nodeTag(nodeptr)
Definition: nodes.h:541
#define elog(elevel,...)
Definition: elog.h:232
static bool jointree_contains_lateral_outer_refs(PlannerInfo *root, Node *jtnode, bool restricted, Relids safe_upper_varnos)
Relids pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
Definition: var.c:123

◆ make_setop_translation_list()

static void make_setop_translation_list ( Query query,
int  newvarno,
AppendRelInfo appinfo 
)
static

Definition at line 1375 of file prepjointree.c.

References lappend(), lfirst, list_length(), makeVarFromTargetEntry(), NIL, AppendRelInfo::num_child_cols, palloc0(), AppendRelInfo::parent_colnos, TargetEntry::resjunk, TargetEntry::resno, Query::targetList, and AppendRelInfo::translated_vars.

Referenced by pull_up_union_leaf_queries().

1377 {
1378  List *vars = NIL;
1379  AttrNumber *pcolnos;
1380  ListCell *l;
1381 
1382  /* Initialize reverse-translation array with all entries zero */
1383  /* (entries for resjunk columns will stay that way) */
1384  appinfo->num_child_cols = list_length(query->targetList);
1385  appinfo->parent_colnos = pcolnos =
1386  (AttrNumber *) palloc0(appinfo->num_child_cols * sizeof(AttrNumber));
1387 
1388  foreach(l, query->targetList)
1389  {
1390  TargetEntry *tle = (TargetEntry *) lfirst(l);
1391 
1392  if (tle->resjunk)
1393  continue;
1394 
1395  vars = lappend(vars, makeVarFromTargetEntry(newvarno, tle));
1396  pcolnos[tle->resno - 1] = tle->resno;
1397  }
1398 
1399  appinfo->translated_vars = vars;
1400 }
#define NIL
Definition: pg_list.h:65
int num_child_cols
Definition: pathnodes.h:2342
AttrNumber * parent_colnos
Definition: pathnodes.h:2343
List * translated_vars
Definition: pathnodes.h:2334
List * targetList
Definition: parsenodes.h:150
bool resjunk
Definition: primnodes.h:1462
AttrNumber resno
Definition: primnodes.h:1456
Var * makeVarFromTargetEntry(int varno, TargetEntry *tle)
Definition: makefuncs.c:103
List * lappend(List *list, void *datum)
Definition: list.c:336
void * palloc0(Size size)
Definition: mcxt.c:1093
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
Definition: regcomp.c:237
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21

◆ perform_pullup_replace_vars()

static void perform_pullup_replace_vars ( PlannerInfo root,
pullup_replace_vars_context rvcontext,
JoinExpr lowest_nulling_outer_join,
AppendRelInfo containing_appendrel 
)
static

Definition at line 2026 of file prepjointree.c.

References PlannerInfo::append_rel_list, Assert, Query::havingQual, RangeTblEntry::joinaliasvars, Query::jointree, lfirst, pullup_replace_vars_context::need_phvs, Query::onConflict, OnConflictExpr::onConflictSet, OnConflictExpr::onConflictWhere, parse(), PlannerInfo::parse, pullup_replace_vars(), replace_vars_in_jointree(), Query::returningList, Query::rtable, RTE_JOIN, RangeTblEntry::rtekind, Query::setOperations, Query::targetList, and AppendRelInfo::translated_vars.

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

2030 {
2031  Query *parse = root->parse;
2032  ListCell *lc;
2033 
2034  /*
2035  * Replace all of the top query's references to the subquery's outputs
2036  * with copies of the adjusted subtlist items, being careful not to
2037  * replace any of the jointree structure. (This'd be a lot cleaner if we
2038  * could use query_tree_mutator.) We have to use PHVs in the targetList,
2039  * returningList, and havingQual, since those are certainly above any
2040  * outer join. replace_vars_in_jointree tracks its location in the
2041  * jointree and uses PHVs or not appropriately.
2042  */
2043  parse->targetList = (List *)
2044  pullup_replace_vars((Node *) parse->targetList, rvcontext);
2045  parse->returningList = (List *)
2046  pullup_replace_vars((Node *) parse->returningList, rvcontext);
2047  if (parse->onConflict)
2048  {
2049  parse->onConflict->onConflictSet = (List *)
2051  rvcontext);
2052  parse->onConflict->onConflictWhere =
2054  rvcontext);
2055 
2056  /*
2057  * We assume ON CONFLICT's arbiterElems, arbiterWhere, exclRelTlist
2058  * can't contain any references to a subquery.
2059  */
2060  }
2061  replace_vars_in_jointree((Node *) parse->jointree, rvcontext,
2062  lowest_nulling_outer_join);
2063  Assert(parse->setOperations == NULL);
2064  parse->havingQual = pullup_replace_vars(parse->havingQual, rvcontext);
2065 
2066  /*
2067  * Replace references in the translated_vars lists of appendrels. When
2068  * pulling up an appendrel member, we do not need PHVs in the list of the
2069  * parent appendrel --- there isn't any outer join between. Elsewhere,
2070  * use PHVs for safety. (This analysis could be made tighter but it seems
2071  * unlikely to be worth much trouble.)
2072  */
2073  foreach(lc, root->append_rel_list)
2074  {
2075  AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(lc);
2076  bool save_need_phvs = rvcontext->need_phvs;
2077 
2078  if (appinfo == containing_appendrel)
2079  rvcontext->need_phvs = false;
2080  appinfo->translated_vars = (List *)
2081  pullup_replace_vars((Node *) appinfo->translated_vars, rvcontext);
2082  rvcontext->need_phvs = save_need_phvs;
2083  }
2084 
2085  /*
2086  * Replace references in the joinaliasvars lists of join RTEs.
2087  *
2088  * You might think that we could avoid using PHVs for alias vars of joins
2089  * below lowest_nulling_outer_join, but that doesn't work because the
2090  * alias vars could be referenced above that join; we need the PHVs to be
2091  * present in such references after the alias vars get flattened. (It
2092  * might be worth trying to be smarter here, someday.)
2093  */
2094  foreach(lc, parse->rtable)
2095  {
2096  RangeTblEntry *otherrte = (RangeTblEntry *) lfirst(lc);
2097 
2098  if (otherrte->rtekind == RTE_JOIN)
2099  otherrte->joinaliasvars = (List *)
2100  pullup_replace_vars((Node *) otherrte->joinaliasvars,
2101  rvcontext);
2102  }
2103 }
Query * parse
Definition: pathnodes.h:162
List * joinaliasvars
Definition: parsenodes.h:1082
FromExpr * jointree
Definition: parsenodes.h:148
OnConflictExpr * onConflict
Definition: parsenodes.h:154
Definition: nodes.h:536
List * translated_vars
Definition: pathnodes.h:2334
List * targetList
Definition: parsenodes.h:150
List * rtable
Definition: parsenodes.h:147
List * returningList
Definition: parsenodes.h:156
List * append_rel_list
Definition: pathnodes.h:284
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
RTEKind rtekind
Definition: parsenodes.h:1007
Node * setOperations
Definition: parsenodes.h:177
static void replace_vars_in_jointree(Node *jtnode, pullup_replace_vars_context *context, JoinExpr *lowest_nulling_outer_join)
List * onConflictSet
Definition: primnodes.h:1589
Node * havingQual
Definition: parsenodes.h:163
Node * onConflictWhere
Definition: primnodes.h:1590
static Node * pullup_replace_vars(Node *expr, pullup_replace_vars_context *context)
Definition: pg_list.h:50
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:673

◆ preprocess_function_rtes()

void preprocess_function_rtes ( PlannerInfo root)

Definition at line 639 of file prepjointree.c.

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

Referenced by pull_up_simple_subquery(), and subquery_planner().

640 {
641  ListCell *rt;
642 
643  foreach(rt, root->parse->rtable)
644  {
645  RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
646 
647  if (rte->rtekind == RTE_FUNCTION)
648  {
649  Query *funcquery;
650 
651  /* Apply const-simplification */
652  rte->functions = (List *)
653  eval_const_expressions(root, (Node *) rte->functions);
654 
655  /* Check safety of expansion, and expand if possible */
656  funcquery = inline_set_returning_function(root, rte);
657  if (funcquery)
658  {
659  /* Successful expansion, convert the RTE to a subquery */
660  rte->rtekind = RTE_SUBQUERY;
661  rte->subquery = funcquery;
662  rte->security_barrier = false;
663  /* Clear fields that should not be set in a subquery RTE */
664  rte->functions = NIL;
665  rte->funcordinality = false;
666  }
667  }
668  }
669 }
#define NIL
Definition: pg_list.h:65
Query * parse
Definition: pathnodes.h:162
Definition: nodes.h:536
Query * inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
Definition: clauses.c:4877
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2094
bool funcordinality
Definition: parsenodes.h:1102
List * rtable
Definition: parsenodes.h:147
bool security_barrier
Definition: parsenodes.h:1043
#define lfirst(lc)
Definition: pg_list.h:169
List * functions
Definition: parsenodes.h:1101
RTEKind rtekind
Definition: parsenodes.h:1007
Query * subquery
Definition: parsenodes.h:1042
Definition: pg_list.h:50

◆ pull_up_constant_function()

static Node * pull_up_constant_function ( PlannerInfo root,
Node jtnode,
RangeTblEntry rte,
JoinExpr lowest_nulling_outer_join,
AppendRelInfo containing_appendrel 
)
static

Definition at line 1705 of file prepjointree.c.

References RangeTblFunction::funccolcount, RangeTblFunction::funcexpr, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_type(), Query::groupingSets, Query::hasSubLinks, IsA, RangeTblEntry::lateral, linitial_node, list_length(), list_make1, makeTargetEntry(), pullup_replace_vars_context::need_phvs, NIL, pullup_replace_vars_context::outer_hasSubLinks, palloc0(), parse(), PlannerInfo::parse, perform_pullup_replace_vars(), pullup_replace_vars_context::relids, pullup_replace_vars_context::root, RTE_RESULT, RangeTblEntry::rtekind, pullup_replace_vars_context::rv_cache, pullup_replace_vars_context::target_rte, pullup_replace_vars_context::targetlist, TYPEFUNC_SCALAR, pullup_replace_vars_context::varno, and pullup_replace_vars_context::wrap_non_vars.

Referenced by pull_up_subqueries_recurse().

1709 {
1710  Query *parse = root->parse;
1711  RangeTblFunction *rtf;
1712  TypeFuncClass functypclass;
1713  Oid funcrettype;
1714  TupleDesc tupdesc;
1715  pullup_replace_vars_context rvcontext;
1716 
1717  /* Fail if the RTE has ORDINALITY - we don't implement that here. */
1718  if (rte->funcordinality)
1719  return jtnode;
1720 
1721  /* Fail if RTE isn't a single, simple Const expr */
1722  if (list_length(rte->functions) != 1)
1723  return jtnode;
1725  if (!IsA(rtf->funcexpr, Const))
1726  return jtnode;
1727 
1728  /*
1729  * If the function's result is not a scalar, we punt. In principle we
1730  * could break the composite constant value apart into per-column
1731  * constants, but for now it seems not worth the work.
1732  */
1733  if (rtf->funccolcount != 1)
1734  return jtnode; /* definitely composite */
1735 
1736  functypclass = get_expr_result_type(rtf->funcexpr,
1737  &funcrettype,
1738  &tupdesc);
1739  if (functypclass != TYPEFUNC_SCALAR)
1740  return jtnode; /* must be a one-column composite type */
1741 
1742  /* Create context for applying pullup_replace_vars */
1743  rvcontext.root = root;
1744  rvcontext.targetlist = list_make1(makeTargetEntry((Expr *) rtf->funcexpr,
1745  1, /* resno */
1746  NULL, /* resname */
1747  false)); /* resjunk */
1748  rvcontext.target_rte = rte;
1749 
1750  /*
1751  * Since this function was reduced to a Const, it doesn't contain any
1752  * lateral references, even if it's marked as LATERAL. This means we
1753  * don't need to fill relids.
1754  */
1755  rvcontext.relids = NULL;
1756 
1757  rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
1758  rvcontext.varno = ((RangeTblRef *) jtnode)->rtindex;
1759  /* these flags will be set below, if needed */
1760  rvcontext.need_phvs = false;
1761  rvcontext.wrap_non_vars = false;
1762  /* initialize cache array with indexes 0 .. length(tlist) */
1763  rvcontext.rv_cache = palloc0((list_length(rvcontext.targetlist) + 1) *
1764  sizeof(Node *));
1765 
1766  /*
1767  * If we are under an outer join then non-nullable items and lateral
1768  * references may have to be turned into PlaceHolderVars.
1769  */
1770  if (lowest_nulling_outer_join != NULL)
1771  rvcontext.need_phvs = true;
1772 
1773  /*
1774  * If we are dealing with an appendrel member then anything that's not a
1775  * simple Var has to be turned into a PlaceHolderVar. (See comments in
1776  * pull_up_simple_subquery().)
1777  */
1778  if (containing_appendrel != NULL)
1779  {
1780  rvcontext.need_phvs = true;
1781  rvcontext.wrap_non_vars = true;
1782  }
1783 
1784  /*
1785  * If the parent query uses grouping sets, we need a PlaceHolderVar for
1786  * anything that's not a simple Var.
1787  */
1788  if (parse->groupingSets)
1789  {
1790  rvcontext.need_phvs = true;
1791  rvcontext.wrap_non_vars = true;
1792  }
1793 
1794  /*
1795  * Replace all of the top query's references to the RTE's output with
1796  * copies of the funcexpr, being careful not to replace any of the
1797  * jointree structure.
1798  */
1799  perform_pullup_replace_vars(root, &rvcontext,
1800  lowest_nulling_outer_join,
1801  containing_appendrel);
1802 
1803  /*
1804  * We don't need to bother with changing PlaceHolderVars in the parent
1805  * query. Their references to the RT index are still good for now, and
1806  * will get removed later if we're able to drop the RTE_RESULT.
1807  */
1808 
1809  /*
1810  * Convert the RTE to be RTE_RESULT type, signifying that we don't need to
1811  * scan it anymore, and zero out RTE_FUNCTION-specific fields. Also make
1812  * sure the RTE is not marked LATERAL, since elsewhere we don't expect
1813  * RTE_RESULTs to be LATERAL.
1814  */
1815  rte->rtekind = RTE_RESULT;
1816  rte->functions = NIL;
1817  rte->lateral = false;
1818 
1819  /*
1820  * We can reuse the RangeTblRef node.
1821  */
1822  return jtnode;
1823 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Query * parse
Definition: pathnodes.h:162
List * groupingSets
Definition: parsenodes.h:161
Definition: nodes.h:536
bool funcordinality
Definition: parsenodes.h:1102
unsigned int Oid
Definition: postgres_ext.h:31
#define linitial_node(type, l)
Definition: pg_list.h:177
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:223
#define list_make1(x1)
Definition: pg_list.h:206
RangeTblEntry * target_rte
Definition: prepjointree.c:47
TypeFuncClass
Definition: funcapi.h:146
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
void * palloc0(Size size)
Definition: mcxt.c:1093
static void perform_pullup_replace_vars(PlannerInfo *root, pullup_replace_vars_context *rvcontext, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
List * functions
Definition: parsenodes.h:1101
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:1007
bool hasSubLinks
Definition: parsenodes.h:136
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:673

◆ 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 
)
static

Definition at line 882 of file prepjointree.c.

References PlannerInfo::all_result_relids, PlannerInfo::append_rel_list, Assert, copyObject, PlannerInfo::cte_plan_ids, Query::cteList, CurrentMemoryContext, PlannerInfo::ec_merging_done, PlannerInfo::eq_classes, fix_append_rel_relids(), flatten_join_alias_vars(), get_relids_in_jointree(), PlannerInfo::glob, PlannerInfo::grouping_map, Query::groupingSets, PlannerInfo::hasRecursion, Query::hasRowSecurity, Query::hasSubLinks, IncrementVarSublevelsUp(), PlannerInfo::init_plans, is_safe_append_member(), is_simple_subquery(), IsA, PlannerInfo::join_info_list, PlannerGlobal::lastPHId, RangeTblEntry::lateral, PlannerInfo::leaf_result_relids, lfirst, linitial, list_concat(), list_length(), makeNode, PlannerInfo::minmax_aggs, PlannerInfo::multiexpr_params, pullup_replace_vars_context::need_phvs, NIL, PlannerInfo::non_recursive_path, OffsetVarNodes(), pullup_replace_vars_context::outer_hasSubLinks, PlannerInfo::outer_params, palloc0(), PlannerInfo::parent_root, parse(), PlannerInfo::parse, perform_pullup_replace_vars(), PlannerInfo::placeholder_list, PlannerInfo::plan_params, PlannerInfo::planner_cxt, preprocess_function_rtes(), PlannerInfo::processed_tlist, pull_up_sublinks(), pull_up_subqueries(), PlannerInfo::qual_security_level, PlannerInfo::query_level, pullup_replace_vars_context::relids, replace_empty_jointree(), pullup_replace_vars_context::root, PlannerInfo::row_identity_vars, Query::rowMarks, PlannerInfo::rowMarks, Query::rtable, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, RangeTblEntry::rtekind, pullup_replace_vars_context::rv_cache, RangeTblEntry::subquery, substitute_phv_relids(), RangeTblEntry::tablesample, pullup_replace_vars_context::target_rte, pullup_replace_vars_context::targetlist, Query::targetList, PlannerInfo::update_colnos, 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().

886 {
887  Query *parse = root->parse;
888  int varno = ((RangeTblRef *) jtnode)->rtindex;
889  Query *subquery;
890  PlannerInfo *subroot;
891  int rtoffset;
892  pullup_replace_vars_context rvcontext;
893  ListCell *lc;
894 
895  /*
896  * Make a modifiable copy of the subquery to hack on, so that the RTE will
897  * be left unchanged in case we decide below that we can't pull it up
898  * after all.
899  */
900  subquery = copyObject(rte->subquery);
901 
902  /*
903  * Create a PlannerInfo data structure for this subquery.
904  *
905  * NOTE: the next few steps should match the first processing in
906  * subquery_planner(). Can we refactor to avoid code duplication, or
907  * would that just make things uglier?
908  */
909  subroot = makeNode(PlannerInfo);
910  subroot->parse = subquery;
911  subroot->glob = root->glob;
912  subroot->query_level = root->query_level;
913  subroot->parent_root = root->parent_root;
914  subroot->plan_params = NIL;
915  subroot->outer_params = NULL;
917  subroot->init_plans = NIL;
918  subroot->cte_plan_ids = NIL;
919  subroot->multiexpr_params = NIL;
920  subroot->eq_classes = NIL;
921  subroot->ec_merging_done = false;
922  subroot->all_result_relids = NULL;
923  subroot->leaf_result_relids = NULL;
924  subroot->append_rel_list = NIL;
925  subroot->row_identity_vars = NIL;
926  subroot->rowMarks = NIL;
927  memset(subroot->upper_rels, 0, sizeof(subroot->upper_rels));
928  memset(subroot->upper_targets, 0, sizeof(subroot->upper_targets));
929  subroot->processed_tlist = NIL;
930  subroot->update_colnos = NIL;
931  subroot->grouping_map = NULL;
932  subroot->minmax_aggs = NIL;
933  subroot->qual_security_level = 0;
934  subroot->hasRecursion = false;
935  subroot->wt_param_id = -1;
936  subroot->non_recursive_path = NULL;
937 
938  /* No CTEs to worry about */
939  Assert(subquery->cteList == NIL);
940 
941  /*
942  * If the FROM clause is empty, replace it with a dummy RTE_RESULT RTE, so
943  * that we don't need so many special cases to deal with that situation.
944  */
945  replace_empty_jointree(subquery);
946 
947  /*
948  * Pull up any SubLinks within the subquery's quals, so that we don't
949  * leave unoptimized SubLinks behind.
950  */
951  if (subquery->hasSubLinks)
952  pull_up_sublinks(subroot);
953 
954  /*
955  * Similarly, preprocess its function RTEs to inline any set-returning
956  * functions in its rangetable.
957  */
958  preprocess_function_rtes(subroot);
959 
960  /*
961  * Recursively pull up the subquery's subqueries, so that
962  * pull_up_subqueries' processing is complete for its jointree and
963  * rangetable.
964  *
965  * Note: it's okay that the subquery's recursion starts with NULL for
966  * containing-join info, even if we are within an outer join in the upper
967  * query; the lower query starts with a clean slate for outer-join
968  * semantics. Likewise, we needn't pass down appendrel state.
969  */
970  pull_up_subqueries(subroot);
971 
972  /*
973  * Now we must recheck whether the subquery is still simple enough to pull
974  * up. If not, abandon processing it.
975  *
976  * We don't really need to recheck all the conditions involved, but it's
977  * easier just to keep this "if" looking the same as the one in
978  * pull_up_subqueries_recurse.
979  */
980  if (is_simple_subquery(root, subquery, rte, lowest_outer_join) &&
981  (containing_appendrel == NULL || is_safe_append_member(subquery)))
982  {
983  /* good to go */
984  }
985  else
986  {
987  /*
988  * Give up, return unmodified RangeTblRef.
989  *
990  * Note: The work we just did will be redone when the subquery gets
991  * planned on its own. Perhaps we could avoid that by storing the
992  * modified subquery back into the rangetable, but I'm not gonna risk
993  * it now.
994  */
995  return jtnode;
996  }
997 
998  /*
999  * We must flatten any join alias Vars in the subquery's targetlist,
1000  * because pulling up the subquery's subqueries might have changed their
1001  * expansions into arbitrary expressions, which could affect
1002  * pullup_replace_vars' decisions about whether PlaceHolderVar wrappers
1003  * are needed for tlist entries. (Likely it'd be better to do
1004  * flatten_join_alias_vars on the whole query tree at some earlier stage,
1005  * maybe even in the rewriter; but for now let's just fix this case here.)
1006  */
1007  subquery->targetList = (List *)
1008  flatten_join_alias_vars(subroot->parse, (Node *) subquery->targetList);
1009 
1010  /*
1011  * Adjust level-0 varnos in subquery so that we can append its rangetable
1012  * to upper query's. We have to fix the subquery's append_rel_list as
1013  * well.
1014  */
1015  rtoffset = list_length(parse->rtable);
1016  OffsetVarNodes((Node *) subquery, rtoffset, 0);
1017  OffsetVarNodes((Node *) subroot->append_rel_list, rtoffset, 0);
1018 
1019  /*
1020  * Upper-level vars in subquery are now one level closer to their parent
1021  * than before.
1022  */
1023  IncrementVarSublevelsUp((Node *) subquery, -1, 1);
1024  IncrementVarSublevelsUp((Node *) subroot->append_rel_list, -1, 1);
1025 
1026  /*
1027  * The subquery's targetlist items are now in the appropriate form to
1028  * insert into the top query, except that we may need to wrap them in
1029  * PlaceHolderVars. Set up required context data for pullup_replace_vars.
1030  */
1031  rvcontext.root = root;
1032  rvcontext.targetlist = subquery->targetList;
1033  rvcontext.target_rte = rte;
1034  if (rte->lateral)
1035  rvcontext.relids = get_relids_in_jointree((Node *) subquery->jointree,
1036  true);
1037  else /* won't need relids */
1038  rvcontext.relids = NULL;
1039  rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
1040  rvcontext.varno = varno;
1041  /* these flags will be set below, if needed */
1042  rvcontext.need_phvs = false;
1043  rvcontext.wrap_non_vars = false;
1044  /* initialize cache array with indexes 0 .. length(tlist) */
1045  rvcontext.rv_cache = palloc0((list_length(subquery->targetList) + 1) *
1046  sizeof(Node *));
1047 
1048  /*
1049  * If we are under an outer join then non-nullable items and lateral
1050  * references may have to be turned into PlaceHolderVars.
1051  */
1052  if (lowest_nulling_outer_join != NULL)
1053  rvcontext.need_phvs = true;
1054 
1055  /*
1056  * If we are dealing with an appendrel member then anything that's not a
1057  * simple Var has to be turned into a PlaceHolderVar. We force this to
1058  * ensure that what we pull up doesn't get merged into a surrounding
1059  * expression during later processing and then fail to match the
1060  * expression actually available from the appendrel.
1061  */
1062  if (containing_appendrel != NULL)
1063  {
1064  rvcontext.need_phvs = true;
1065  rvcontext.wrap_non_vars = true;
1066  }
1067 
1068  /*
1069  * If the parent query uses grouping sets, we need a PlaceHolderVar for
1070  * anything that's not a simple Var. Again, this ensures that expressions
1071  * retain their separate identity so that they will match grouping set
1072  * columns when appropriate. (It'd be sufficient to wrap values used in
1073  * grouping set columns, and do so only in non-aggregated portions of the
1074  * tlist and havingQual, but that would require a lot of infrastructure
1075  * that pullup_replace_vars hasn't currently got.)
1076  */
1077  if (parse->groupingSets)
1078  {
1079  rvcontext.need_phvs = true;
1080  rvcontext.wrap_non_vars = true;
1081  }
1082 
1083  /*
1084  * Replace all of the top query's references to the subquery's outputs
1085  * with copies of the adjusted subtlist items, being careful not to
1086  * replace any of the jointree structure.
1087  */
1088  perform_pullup_replace_vars(root, &rvcontext,
1089  lowest_nulling_outer_join,
1090  containing_appendrel);
1091 
1092  /*
1093  * If the subquery had a LATERAL marker, propagate that to any of its
1094  * child RTEs that could possibly now contain lateral cross-references.
1095  * The children might or might not contain any actual lateral
1096  * cross-references, but we have to mark the pulled-up child RTEs so that
1097  * later planner stages will check for such.
1098  */
1099  if (rte->lateral)
1100  {
1101  foreach(lc, subquery->rtable)
1102  {
1103  RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(lc);
1104 
1105  switch (child_rte->rtekind)
1106  {
1107  case RTE_RELATION:
1108  if (child_rte->tablesample)
1109  child_rte->lateral = true;
1110  break;
1111  case RTE_SUBQUERY:
1112  case RTE_FUNCTION:
1113  case RTE_VALUES:
1114  case RTE_TABLEFUNC:
1115  child_rte->lateral = true;
1116  break;
1117  case RTE_JOIN:
1118  case RTE_CTE:
1119  case RTE_NAMEDTUPLESTORE:
1120  case RTE_RESULT:
1121  /* these can't contain any lateral references */
1122  break;
1123  }
1124  }
1125  }
1126 
1127  /*
1128  * Now append the adjusted rtable entries to upper query. (We hold off
1129  * until after fixing the upper rtable entries; no point in running that
1130  * code on the subquery ones too.)
1131  */
1132  parse->rtable = list_concat(parse->rtable, subquery->rtable);
1133 
1134  /*
1135  * Pull up any FOR UPDATE/SHARE markers, too. (OffsetVarNodes already
1136  * adjusted the marker rtindexes, so just concat the lists.)
1137  */
1138  parse->rowMarks = list_concat(parse->rowMarks, subquery->rowMarks);
1139 
1140  /*
1141  * We also have to fix the relid sets of any PlaceHolderVar nodes in the
1142  * parent query. (This could perhaps be done by pullup_replace_vars(),
1143  * but it seems cleaner to use two passes.) Note in particular that any
1144  * PlaceHolderVar nodes just created by pullup_replace_vars() will be
1145  * adjusted, so having created them with the subquery's varno is correct.
1146  *
1147  * Likewise, relids appearing in AppendRelInfo nodes have to be fixed. We
1148  * already checked that this won't require introducing multiple subrelids
1149  * into the single-slot AppendRelInfo structs.
1150  */
1151  if (parse->hasSubLinks || root->glob->lastPHId != 0 ||
1152  root->append_rel_list)
1153  {
1154  Relids subrelids;
1155 
1156  subrelids = get_relids_in_jointree((Node *) subquery->jointree, false);
1157  substitute_phv_relids((Node *) parse, varno, subrelids);
1158  fix_append_rel_relids(root->append_rel_list, varno, subrelids);
1159  }
1160 
1161  /*
1162  * And now add subquery's AppendRelInfos to our list.
1163  */
1165  subroot->append_rel_list);
1166 
1167  /*
1168  * We don't have to do the equivalent bookkeeping for outer-join info,
1169  * because that hasn't been set up yet. placeholder_list likewise.
1170  */
1171  Assert(root->join_info_list == NIL);
1172  Assert(subroot->join_info_list == NIL);
1173  Assert(root->placeholder_list == NIL);
1174  Assert(subroot->placeholder_list == NIL);
1175 
1176  /*
1177  * We no longer need the RTE's copy of the subquery's query tree. Getting
1178  * rid of it saves nothing in particular so far as this level of query is
1179  * concerned; but if this query level is in turn pulled up into a parent,
1180  * we'd waste cycles copying the now-unused query tree.
1181  */
1182  rte->subquery = NULL;
1183 
1184  /*
1185  * Miscellaneous housekeeping.
1186  *
1187  * Although replace_rte_variables() faithfully updated parse->hasSubLinks
1188  * if it copied any SubLinks out of the subquery's targetlist, we still
1189  * could have SubLinks added to the query in the expressions of FUNCTION
1190  * and VALUES RTEs copied up from the subquery. So it's necessary to copy
1191  * subquery->hasSubLinks anyway. Perhaps this can be improved someday.
1192  */
1193  parse->hasSubLinks |= subquery->hasSubLinks;
1194 
1195  /* If subquery had any RLS conditions, now main query does too */
1196  parse->hasRowSecurity |= subquery->hasRowSecurity;
1197 
1198  /*
1199  * subquery won't be pulled up if it hasAggs, hasWindowFuncs, or
1200  * hasTargetSRFs, so no work needed on those flags
1201  */
1202 
1203  /*
1204  * Return the adjusted subquery jointree to replace the RangeTblRef entry
1205  * in parent's jointree; or, if the FromExpr is degenerate, just return
1206  * its single member.
1207  */
1208  Assert(IsA(subquery->jointree, FromExpr));
1209  Assert(subquery->jointree->fromlist != NIL);
1210  if (subquery->jointree->quals == NULL &&
1211  list_length(subquery->jointree->fromlist) == 1)
1212  return (Node *) linitial(subquery->jointree->fromlist);
1213 
1214  return (Node *) subquery->jointree;
1215 }
#define NIL
Definition: pg_list.h:65
static void substitute_phv_relids(Node *node, int varno, Relids subrelids)
List * rowMarks
Definition: pathnodes.h:288
Relids all_result_relids
Definition: pathnodes.h:276
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Query * parse
Definition: pathnodes.h:162
void OffsetVarNodes(Node *node, int offset, int sublevels_up)
Definition: rewriteManip.c:425
List * plan_params
Definition: pathnodes.h:176
bool ec_merging_done
Definition: pathnodes.h:251
List * join_info_list
Definition: pathnodes.h:266
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:776
FromExpr * jointree
Definition: parsenodes.h:148
PlannerInfo * parent_root
Definition: pathnodes.h:168
List * groupingSets
Definition: parsenodes.h:161
Definition: nodes.h:536
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
List * minmax_aggs
Definition: pathnodes.h:333
List * fromlist
Definition: primnodes.h:1564
List * rowMarks
Definition: parsenodes.h:175
AttrNumber * grouping_map
Definition: pathnodes.h:332
bool hasRecursion
Definition: pathnodes.h:352
void pull_up_subqueries(PlannerInfo *root)
Definition: prepjointree.c:680
void replace_empty_jointree(Query *parse)
Definition: prepjointree.c:150
Node * quals
Definition: primnodes.h:1565
List * targetList
Definition: parsenodes.h:150
List * multiexpr_params
Definition: pathnodes.h:246
void preprocess_function_rtes(PlannerInfo *root)
Definition: prepjointree.c:639
int wt_param_id
Definition: pathnodes.h:364
#define linitial(l)
Definition: pg_list.h:174
List * rtable
Definition: parsenodes.h:147
RangeTblEntry * target_rte
Definition: prepjointree.c:47
List * row_identity_vars
Definition: pathnodes.h:286
Relids get_relids_in_jointree(Node *jtnode, bool include_joins)
PlannerGlobal * glob
Definition: pathnodes.h:164
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void * palloc0(Size size)
Definition: mcxt.c:1093
List * append_rel_list
Definition: pathnodes.h:284
Index lastPHId
Definition: pathnodes.h:116
List * cte_plan_ids
Definition: pathnodes.h:244
List * init_plans
Definition: pathnodes.h:242
Node * flatten_join_alias_vars(Query *query, Node *node)
Definition: var.c:732
static bool is_safe_append_member(Query *subquery)
#define makeNode(_type_)
Definition: nodes.h:584
static void perform_pullup_replace_vars(PlannerInfo *root, pullup_replace_vars_context *rvcontext, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
static void fix_append_rel_relids(List *append_rel_list, int varno, Relids subrelids)
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List * eq_classes
Definition: pathnodes.h:249
Bitmapset * outer_params
Definition: pathnodes.h:177
struct Path * non_recursive_path
Definition: pathnodes.h:365
static int list_length(const List *l)
Definition: pg_list.h:149
Index qual_security_level
Definition: pathnodes.h:343
Index query_level
Definition: pathnodes.h:166
void pull_up_sublinks(PlannerInfo *root)
Definition: prepjointree.c:208
RTEKind rtekind
Definition: parsenodes.h:1007
static bool is_simple_subquery(PlannerInfo *root, Query *subquery, RangeTblEntry *rte, JoinExpr *lowest_outer_join)
List * cteList
Definition: parsenodes.h:145
Query * subquery
Definition: parsenodes.h:1042
bool hasSubLinks
Definition: parsenodes.h:136
Relids leaf_result_relids
Definition: pathnodes.h:277
List * placeholder_list
Definition: pathnodes.h:290
MemoryContext planner_cxt
Definition: pathnodes.h:335
#define copyObject(obj)
Definition: nodes.h:652
List * processed_tlist
Definition: pathnodes.h:321
Definition: pg_list.h:50
struct TableSampleClause * tablesample
Definition: parsenodes.h:1037
List * update_colnos
Definition: pathnodes.h:329
bool hasRowSecurity
Definition: parsenodes.h:141
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:673
struct PathTarget * upper_targets[UPPERREL_FINAL+1]
Definition: pathnodes.h:310
List * upper_rels[UPPERREL_FINAL+1]
Definition: pathnodes.h:307

◆ pull_up_simple_union_all()

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

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

1228 {
1229  int varno = ((RangeTblRef *) jtnode)->rtindex;
1230  Query *subquery = rte->subquery;
1231  int rtoffset = list_length(root->parse->rtable);
1232  List *rtable;
1233 
1234  /*
1235  * Make a modifiable copy of the subquery's rtable, so we can adjust
1236  * upper-level Vars in it. There are no such Vars in the setOperations
1237  * tree proper, so fixing the rtable should be sufficient.
1238  */
1239  rtable = copyObject(subquery->rtable);
1240 
1241  /*
1242  * Upper-level vars in subquery are now one level closer to their parent
1243  * than before. We don't have to worry about offsetting varnos, though,
1244  * because the UNION leaf queries can't cross-reference each other.
1245  */
1246  IncrementVarSublevelsUp_rtable(rtable, -1, 1);
1247 
1248  /*
1249  * If the UNION ALL subquery had a LATERAL marker, propagate that to all
1250  * its children. The individual children might or might not contain any
1251  * actual lateral cross-references, but we have to mark the pulled-up
1252  * child RTEs so that later planner stages will check for such.
1253  */
1254  if (rte->lateral)
1255  {
1256  ListCell *rt;
1257 
1258  foreach(rt, rtable)
1259  {
1260  RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(rt);
1261 
1262  Assert(child_rte->rtekind == RTE_SUBQUERY);
1263  child_rte->lateral = true;
1264  }
1265  }
1266 
1267  /*
1268  * Append child RTEs to parent rtable.
1269  */
1270  root->parse->rtable = list_concat(root->parse->rtable, rtable);
1271 
1272  /*
1273  * Recursively scan the subquery's setOperations tree and add
1274  * AppendRelInfo nodes for leaf subqueries to the parent's
1275  * append_rel_list. Also apply pull_up_subqueries to the leaf subqueries.
1276  */
1277  Assert(subquery->setOperations);
1278  pull_up_union_leaf_queries(subquery->setOperations, root, varno, subquery,
1279  rtoffset);
1280 
1281  /*
1282  * Mark the parent as an append relation.
1283  */
1284  rte->inh = true;
1285 
1286  return jtnode;
1287 }
static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex, Query *setOpQuery, int childRToffset)
Query * parse
Definition: pathnodes.h:162
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
void IncrementVarSublevelsUp_rtable(List *rtable, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:799
List * rtable
Definition: parsenodes.h:147
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:1007
Node * setOperations
Definition: parsenodes.h:177
Query * subquery
Definition: parsenodes.h:1042
#define copyObject(obj)
Definition: nodes.h:652
Definition: pg_list.h:50

◆ pull_up_simple_values()

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

Definition at line 1550 of file prepjointree.c.

References PlannerInfo::append_rel_list, Assert, contain_vars_of_level(), copyObject, RangeTblEntry::eref, Query::hasSubLinks, PlannerInfo::join_info_list, lappend(), lfirst, linitial, list_length(), list_make1, makeAlias(), makeNode, makeTargetEntry(), pullup_replace_vars_context::need_phvs, NIL, pullup_replace_vars_context::outer_hasSubLinks, palloc0(), parse(), PlannerInfo::parse, perform_pullup_replace_vars(), PlannerInfo::placeholder_list, pullup_replace_vars_context::relids, pullup_replace_vars_context::root, Query::rtable, RTE_RESULT, RTE_VALUES, RangeTblEntry::rtekind, pullup_replace_vars_context::rv_cache, pullup_replace_vars_context::target_rte, pullup_replace_vars_context::targetlist, RangeTblEntry::values_lists, pullup_replace_vars_context::varno, and pullup_replace_vars_context::wrap_non_vars.

Referenced by pull_up_subqueries_recurse().

1551 {
1552  Query *parse = root->parse;
1553  int varno = ((RangeTblRef *) jtnode)->rtindex;
1554  List *values_list;
1555  List *tlist;
1556  AttrNumber attrno;
1557  pullup_replace_vars_context rvcontext;
1558  ListCell *lc;
1559 
1560  Assert(rte->rtekind == RTE_VALUES);
1561  Assert(list_length(rte->values_lists) == 1);
1562 
1563  /*
1564  * Need a modifiable copy of the VALUES list to hack on, just in case it's
1565  * multiply referenced.
1566  */
1567  values_list = copyObject(linitial(rte->values_lists));
1568 
1569  /*
1570  * The VALUES RTE can't contain any Vars of level zero, let alone any that
1571  * are join aliases, so no need to flatten join alias Vars.
1572  */
1573  Assert(!contain_vars_of_level((Node *) values_list, 0));
1574 
1575  /*
1576  * Set up required context data for pullup_replace_vars. In particular,
1577  * we have to make the VALUES list look like a subquery targetlist.
1578  */
1579  tlist = NIL;
1580  attrno = 1;
1581  foreach(lc, values_list)
1582  {
1583  tlist = lappend(tlist,
1584  makeTargetEntry((Expr *) lfirst(lc),
1585  attrno,
1586  NULL,
1587  false));
1588  attrno++;
1589  }
1590  rvcontext.root = root;
1591  rvcontext.targetlist = tlist;
1592  rvcontext.target_rte = rte;
1593  rvcontext.relids = NULL;
1594  rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
1595  rvcontext.varno = varno;
1596  rvcontext.need_phvs = false;
1597  rvcontext.wrap_non_vars = false;
1598  /* initialize cache array with indexes 0 .. length(tlist) */
1599  rvcontext.rv_cache = palloc0((list_length(tlist) + 1) *
1600  sizeof(Node *));
1601 
1602  /*
1603  * Replace all of the top query's references to the RTE's outputs with
1604  * copies of the adjusted VALUES expressions, being careful not to replace
1605  * any of the jointree structure. We can assume there's no outer joins or
1606  * appendrels in the dummy Query that surrounds a VALUES RTE.
1607  */
1608  perform_pullup_replace_vars(root, &rvcontext, NULL, NULL);
1609 
1610  /*
1611  * There should be no appendrels to fix, nor any outer joins and hence no
1612  * PlaceHolderVars.
1613  */
1614  Assert(root->append_rel_list == NIL);
1615  Assert(root->join_info_list == NIL);
1616  Assert(root->placeholder_list == NIL);
1617 
1618  /*
1619  * Replace the VALUES RTE with a RESULT RTE. The VALUES RTE is the only
1620  * rtable entry in the current query level, so this is easy.
1621  */
1622  Assert(list_length(parse->rtable) == 1);
1623 
1624  /* Create suitable RTE */
1625  rte = makeNode(RangeTblEntry);
1626  rte->rtekind = RTE_RESULT;
1627  rte->eref = makeAlias("*RESULT*", NIL);
1628 
1629  /* Replace rangetable */
1630  parse->rtable = list_make1(rte);
1631 
1632  /* We could manufacture a new RangeTblRef, but the one we have is fine */
1633  Assert(varno == 1);
1634 
1635  return jtnode;
1636 }
#define NIL
Definition: pg_list.h:65
Query * parse
Definition: pathnodes.h:162
List * join_info_list
Definition: pathnodes.h:266
Definition: nodes.h:536
List * values_lists
Definition: parsenodes.h:1112
#define list_make1(x1)
Definition: pg_list.h:206
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
#define linitial(l)
Definition: pg_list.h:174
List * rtable
Definition: parsenodes.h:147
RangeTblEntry * target_rte
Definition: prepjointree.c:47
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
List * lappend(List *list, void *datum)
Definition: list.c:336
void * palloc0(Size size)
Definition: mcxt.c:1093
List * append_rel_list
Definition: pathnodes.h:284
#define makeNode(_type_)
Definition: nodes.h:584
static void perform_pullup_replace_vars(PlannerInfo *root, pullup_replace_vars_context *rvcontext, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:431
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:1007
bool hasSubLinks
Definition: parsenodes.h:136
List * placeholder_list
Definition: pathnodes.h:290
Alias * eref
Definition: parsenodes.h:1153
#define copyObject(obj)
Definition: nodes.h:652
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:673

◆ pull_up_sublinks()

void pull_up_sublinks ( PlannerInfo root)

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

209 {
210  Node *jtnode;
211  Relids relids;
212 
213  /* Begin recursion through the jointree */
214  jtnode = pull_up_sublinks_jointree_recurse(root,
215  (Node *) root->parse->jointree,
216  &relids);
217 
218  /*
219  * root->parse->jointree must always be a FromExpr, so insert a dummy one
220  * if we got a bare RangeTblRef or JoinExpr out of the recursion.
221  */
222  if (IsA(jtnode, FromExpr))
223  root->parse->jointree = (FromExpr *) jtnode;
224  else
225  root->parse->jointree = makeFromExpr(list_make1(jtnode), NULL);
226 }
FromExpr * makeFromExpr(List *fromlist, Node *quals)
Definition: makefuncs.c:285
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Query * parse
Definition: pathnodes.h:162
FromExpr * jointree
Definition: parsenodes.h:148
Definition: nodes.h:536
#define list_make1(x1)
Definition: pg_list.h:206
static Node * pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode, Relids *relids)
Definition: prepjointree.c:235

◆ pull_up_sublinks_jointree_recurse()

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

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

237 {
238  if (jtnode == NULL)
239  {
240  *relids = NULL;
241  }
242  else if (IsA(jtnode, RangeTblRef))
243  {
244  int varno = ((RangeTblRef *) jtnode)->rtindex;
245 
246  *relids = bms_make_singleton(varno);
247  /* jtnode is returned unmodified */
248  }
249  else if (IsA(jtnode, FromExpr))
250  {
251  FromExpr *f = (FromExpr *) jtnode;
252  List *newfromlist = NIL;
253  Relids frelids = NULL;
254  FromExpr *newf;
255  Node *jtlink;
256  ListCell *l;
257 
258  /* First, recurse to process children and collect their relids */
259  foreach(l, f->fromlist)
260  {
261  Node *newchild;
262  Relids childrelids;
263 
264  newchild = pull_up_sublinks_jointree_recurse(root,
265  lfirst(l),
266  &childrelids);
267  newfromlist = lappend(newfromlist, newchild);
268  frelids = bms_join(frelids, childrelids);
269  }
270  /* Build the replacement FromExpr; no quals yet */
271  newf = makeFromExpr(newfromlist, NULL);
272  /* Set up a link representing the rebuilt jointree */
273  jtlink = (Node *) newf;
274  /* Now process qual --- all children are available for use */
275  newf->quals = pull_up_sublinks_qual_recurse(root, f->quals,
276  &jtlink, frelids,
277  NULL, NULL);
278 
279  /*
280  * Note that the result will be either newf, or a stack of JoinExprs
281  * with newf at the base. We rely on subsequent optimization steps to
282  * flatten this and rearrange the joins as needed.
283  *
284  * Although we could include the pulled-up subqueries in the returned
285  * relids, there's no need since upper quals couldn't refer to their
286  * outputs anyway.
287  */
288  *relids = frelids;
289  jtnode = jtlink;
290  }
291  else if (IsA(jtnode, JoinExpr))
292  {
293  JoinExpr *j;
294  Relids leftrelids;
295  Relids rightrelids;
296  Node *jtlink;
297 
298  /*
299  * Make a modifiable copy of join node, but don't bother copying its
300  * subnodes (yet).
301  */
302  j = (JoinExpr *) palloc(sizeof(JoinExpr));
303  memcpy(j, jtnode, sizeof(JoinExpr));
304  jtlink = (Node *) j;
305 
306  /* Recurse to process children and collect their relids */
308  &leftrelids);
310  &rightrelids);
311 
312  /*
313  * Now process qual, showing appropriate child relids as available,
314  * and attach any pulled-up jointree items at the right place. In the
315  * inner-join case we put new JoinExprs above the existing one (much
316  * as for a FromExpr-style join). In outer-join cases the new
317  * JoinExprs must go into the nullable side of the outer join. The
318  * point of the available_rels machinations is to ensure that we only
319  * pull up quals for which that's okay.
320  *
321  * We don't expect to see any pre-existing JOIN_SEMI or JOIN_ANTI
322  * nodes here.
323  */
324  switch (j->jointype)
325  {
326  case JOIN_INNER:
328  &jtlink,
329  bms_union(leftrelids,
330  rightrelids),
331  NULL, NULL);
332  break;
333  case JOIN_LEFT:
335  &j->rarg,
336  rightrelids,
337  NULL, NULL);
338  break;
339  case JOIN_FULL:
340  /* can't do anything with full-join quals */
341  break;
342  case JOIN_RIGHT:
344  &j->larg,
345  leftrelids,
346  NULL, NULL);
347  break;
348  default:
349  elog(ERROR, "unrecognized join type: %d",
350  (int) j->jointype);
351  break;
352  }
353 
354  /*
355  * Although we could include the pulled-up subqueries in the returned
356  * relids, there's no need since upper quals couldn't refer to their
357  * outputs anyway. But we *do* need to include the join's own rtindex
358  * because we haven't yet collapsed join alias variables, so upper
359  * levels would mistakenly think they couldn't use references to this
360  * join.
361  */
362  *relids = bms_join(leftrelids, rightrelids);
363  if (j->rtindex)
364  *relids = bms_add_member(*relids, j->rtindex);
365  jtnode = jtlink;
366  }
367  else
368  elog(ERROR, "unrecognized node type: %d",
369  (int) nodeTag(jtnode));
370  return jtnode;
371 }
#define NIL
Definition: pg_list.h:65
static Node * pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node, Node **jtlink1, Relids available_rels1, Node **jtlink2, Relids available_rels2)
Definition: prepjointree.c:389
FromExpr * makeFromExpr(List *fromlist, Node *quals)
Definition: makefuncs.c:285
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Definition: nodes.h:536
List * fromlist
Definition: primnodes.h:1564
Node * quals
Definition: primnodes.h:1565
Node * larg
Definition: primnodes.h:1543
#define ERROR
Definition: elog.h:46
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:949
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
static Node * pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode, Relids *relids)
Definition: prepjointree.c:235
List * lappend(List *list, void *datum)
Definition: list.c:336
Node * quals
Definition: primnodes.h:1547
Node * rarg
Definition: primnodes.h:1544
JoinType jointype
Definition: primnodes.h:1541
#define lfirst(lc)
Definition: pg_list.h:169
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define nodeTag(nodeptr)
Definition: nodes.h:541
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
int rtindex
Definition: primnodes.h:1549
Definition: pg_list.h:50

◆ 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 389 of file prepjointree.c.

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

Referenced by pull_up_sublinks_jointree_recurse().

392 {
393  if (node == NULL)
394  return NULL;
395  if (IsA(node, SubLink))
396  {
397  SubLink *sublink = (SubLink *) node;
398  JoinExpr *j;
399  Relids child_rels;
400 
401  /* Is it a convertible ANY or EXISTS clause? */
402  if (sublink->subLinkType == ANY_SUBLINK)
403  {
404  if ((j = convert_ANY_sublink_to_join(root, sublink,
405  available_rels1)) != NULL)
406  {
407  /* Yes; insert the new join node into the join tree */
408  j->larg = *jtlink1;
409  *jtlink1 = (Node *) j;
410  /* Recursively process pulled-up jointree nodes */
412  j->rarg,
413  &child_rels);
414 
415  /*
416  * Now recursively process the pulled-up quals. Any inserted
417  * joins can get stacked onto either j->larg or j->rarg,
418  * depending on which rels they reference.
419  */
421  j->quals,
422  &j->larg,
423  available_rels1,
424  &j->rarg,
425  child_rels);
426  /* Return NULL representing constant TRUE */
427  return NULL;
428  }
429  if (available_rels2 != NULL &&
430  (j = convert_ANY_sublink_to_join(root, sublink,
431  available_rels2)) != NULL)
432  {
433  /* Yes; insert the new join node into the join tree */
434  j->larg = *jtlink2;
435  *jtlink2 = (Node *) j;
436  /* Recursively process pulled-up jointree nodes */
438  j->rarg,
439  &child_rels);
440 
441  /*
442  * Now recursively process the pulled-up quals. Any inserted
443  * joins can get stacked onto either j->larg or j->rarg,
444  * depending on which rels they reference.
445  */
447  j->quals,
448  &j->larg,
449  available_rels2,
450  &j->rarg,
451  child_rels);
452  /* Return NULL representing constant TRUE */
453  return NULL;
454  }
455  }
456  else if (sublink->subLinkType == EXISTS_SUBLINK)
457  {
458  if ((j = convert_EXISTS_sublink_to_join(root, sublink, false,
459  available_rels1)) != NULL)
460  {
461  /* Yes; insert the new join node into the join tree */
462  j->larg = *jtlink1;
463  *jtlink1 = (Node *) j;
464  /* Recursively process pulled-up jointree nodes */
466  j->rarg,
467  &child_rels);
468 
469  /*
470  * Now recursively process the pulled-up quals. Any inserted
471  * joins can get stacked onto either j->larg or j->rarg,
472  * depending on which rels they reference.
473  */
475  j->quals,
476  &j->larg,
477  available_rels1,
478  &j->rarg,
479  child_rels);
480  /* Return NULL representing constant TRUE */
481  return NULL;
482  }
483  if (available_rels2 != NULL &&
484  (j = convert_EXISTS_sublink_to_join(root, sublink, false,
485  available_rels2)) != NULL)
486  {
487  /* Yes; insert the new join node into the join tree */
488  j->larg = *jtlink2;
489  *jtlink2 = (Node *) j;
490  /* Recursively process pulled-up jointree nodes */
492  j->rarg,
493  &child_rels);
494 
495  /*
496  * Now recursively process the pulled-up quals. Any inserted
497  * joins can get stacked onto either j->larg or j->rarg,
498  * depending on which rels they reference.
499  */
501  j->quals,
502  &j->larg,
503  available_rels2,
504  &j->rarg,
505  child_rels);
506  /* Return NULL representing constant TRUE */
507  return NULL;
508  }
509  }
510  /* Else return it unmodified */
511  return node;
512  }
513  if (is_notclause(node))
514  {
515  /* If the immediate argument of NOT is EXISTS, try to convert */
516  SubLink *sublink = (SubLink *) get_notclausearg((Expr *) node);
517  JoinExpr *j;
518  Relids child_rels;
519 
520  if (sublink && IsA(sublink, SubLink))
521  {
522  if (sublink->subLinkType == EXISTS_SUBLINK)
523  {
524  if ((j = convert_EXISTS_sublink_to_join(root, sublink, true,
525  available_rels1)) != NULL)
526  {
527  /* Yes; insert the new join node into the join tree */
528  j->larg = *jtlink1;
529  *jtlink1 = (Node *) j;
530  /* Recursively process pulled-up jointree nodes */
532  j->rarg,
533  &child_rels);
534 
535  /*
536  * Now recursively process the pulled-up quals. Because
537  * we are underneath a NOT, we can't pull up sublinks that
538  * reference the left-hand stuff, but it's still okay to
539  * pull up sublinks referencing j->rarg.
540  */
542  j->quals,
543  &j->rarg,
544  child_rels,
545  NULL, NULL);
546  /* Return NULL representing constant TRUE */
547  return NULL;
548  }
549  if (available_rels2 != NULL &&
550  (j = convert_EXISTS_sublink_to_join(root, sublink, true,
551  available_rels2)) != NULL)
552  {
553  /* Yes; insert the new join node into the join tree */
554  j->larg = *jtlink2;
555  *jtlink2 = (Node *) j;
556  /* Recursively process pulled-up jointree nodes */
558  j->rarg,
559  &child_rels);
560 
561  /*
562  * Now recursively process the pulled-up quals. Because
563  * we are underneath a NOT, we can't pull up sublinks that
564  * reference the left-hand stuff, but it's still okay to
565  * pull up sublinks referencing j->rarg.
566  */
568  j->quals,
569  &j->rarg,
570  child_rels,
571  NULL, NULL);
572  /* Return NULL representing constant TRUE */
573  return NULL;
574  }
575  }
576  }
577  /* Else return it unmodified */
578  return node;
579  }
580  if (is_andclause(node))
581  {
582  /* Recurse into AND clause */
583  List *newclauses = NIL;
584  ListCell *l;
585 
586  foreach(l, ((BoolExpr *) node)->args)
587  {
588  Node *oldclause = (Node *) lfirst(l);
589  Node *newclause;
590 
591  newclause = pull_up_sublinks_qual_recurse(root,
592  oldclause,
593  jtlink1,
594  available_rels1,
595  jtlink2,
596  available_rels2);
597  if (newclause)
598  newclauses = lappend(newclauses, newclause);
599  }
600  /* We might have got back fewer clauses than we started with */
601  if (newclauses == NIL)
602  return NULL;
603  else if (list_length(newclauses) == 1)
604  return (Node *) linitial(newclauses);
605  else
606  return (Node *) make_andclause(newclauses);
607  }
608  /* Stop if not an AND */
609  return node;
610 }
#define NIL
Definition: pg_list.h:65
static Node * pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node, Node **jtlink1, Relids available_rels1, Node **jtlink2, Relids available_rels2)
Definition: prepjointree.c:389
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:97
Definition: nodes.h:536
JoinExpr * convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink, Relids available_rels)
Definition: subselect.c:1274
JoinExpr * convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink, bool under_not, Relids available_rels)
Definition: subselect.c:1382
Node * larg
Definition: primnodes.h:1543
#define linitial(l)
Definition: pg_list.h:174
Expr * make_andclause(List *andclauses)
Definition: makefuncs.c:636
static Node * pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode, Relids *relids)
Definition: prepjointree.c:235
List * lappend(List *list, void *datum)
Definition: list.c:336
static bool is_notclause(const void *clause)
Definition: nodeFuncs.h:115
Node * quals
Definition: primnodes.h:1547
static Expr * get_notclausearg(const void *notclause)
Definition: nodeFuncs.h:124
Node * rarg
Definition: primnodes.h:1544
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
Definition: pg_list.h:50

◆ pull_up_subqueries()

void pull_up_subqueries ( PlannerInfo root)

Definition at line 680 of file prepjointree.c.

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

Referenced by pull_up_simple_subquery(), and subquery_planner().

681 {
682  /* Top level of jointree must always be a FromExpr */
683  Assert(IsA(root->parse->jointree, FromExpr));
684  /* Recursion starts with no containing join nor appendrel */
685  root->parse->jointree = (FromExpr *)
687  NULL, NULL, NULL);
688  /* We should still have a FromExpr */
689  Assert(IsA(root->parse->jointree, FromExpr));
690 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Query * parse
Definition: pathnodes.h:162
FromExpr * jointree
Definition: parsenodes.h:148
static Node * pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode, JoinExpr *lowest_outer_join, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
Definition: prepjointree.c:730
Definition: nodes.h:536
#define Assert(condition)
Definition: c.h:804

◆ 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 
)
static

Definition at line 730 of file prepjointree.c.

References Assert, elog, ERROR, FromExpr::fromlist, 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, JoinExpr::jointype, JoinExpr::larg, lfirst, nodeTag, PlannerInfo::parse, pull_up_constant_function(), pull_up_simple_subquery(), pull_up_simple_union_all(), pull_up_simple_values(), JoinExpr::rarg, rt_fetch, Query::rtable, RTE_FUNCTION, 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().

734 {
735  Assert(jtnode != NULL);
736  if (IsA(jtnode, RangeTblRef))
737  {
738  int varno = ((RangeTblRef *) jtnode)->rtindex;
739  RangeTblEntry *rte = rt_fetch(varno, root->parse->rtable);
740 
741  /*
742  * Is this a subquery RTE, and if so, is the subquery simple enough to
743  * pull up?
744  *
745  * If we are looking at an append-relation member, we can't pull it up
746  * unless is_safe_append_member says so.
747  */
748  if (rte->rtekind == RTE_SUBQUERY &&
749  is_simple_subquery(root, rte->subquery, rte, lowest_outer_join) &&
750  (containing_appendrel == NULL ||
752  return pull_up_simple_subquery(root, jtnode, rte,
753  lowest_outer_join,
754  lowest_nulling_outer_join,
755  containing_appendrel);
756 
757  /*
758  * Alternatively, is it a simple UNION ALL subquery? If so, flatten
759  * into an "append relation".
760  *
761  * It's safe to do this regardless of whether this query is itself an
762  * appendrel member. (If you're thinking we should try to flatten the
763  * two levels of appendrel together, you're right; but we handle that
764  * in set_append_rel_pathlist, not here.)
765  */
766  if (rte->rtekind == RTE_SUBQUERY &&
768  return pull_up_simple_union_all(root, jtnode, rte);
769 
770  /*
771  * Or perhaps it's a simple VALUES RTE?
772  *
773  * We don't allow VALUES pullup below an outer join nor into an
774  * appendrel (such cases are impossible anyway at the moment).
775  */
776  if (rte->rtekind == RTE_VALUES &&
777  lowest_outer_join == NULL &&
778  containing_appendrel == NULL &&
779  is_simple_values(root, rte))
780  return pull_up_simple_values(root, jtnode, rte);
781 
782  /*
783  * Or perhaps it's a FUNCTION RTE that we could inline?
784  */
785  if (rte->rtekind == RTE_FUNCTION)
786  return pull_up_constant_function(root, jtnode, rte,
787  lowest_nulling_outer_join,
788  containing_appendrel);
789 
790  /* Otherwise, do nothing at this node. */
791  }
792  else if (IsA(jtnode, FromExpr))
793  {
794  FromExpr *f = (FromExpr *) jtnode;
795  ListCell *l;
796 
797  Assert(containing_appendrel == NULL);
798  /* Recursively transform all the child nodes */
799  foreach(l, f->fromlist)
800  {
802  lowest_outer_join,
803  lowest_nulling_outer_join,
804  NULL);
805  }
806  }
807  else if (IsA(jtnode, JoinExpr))
808  {
809  JoinExpr *j = (JoinExpr *) jtnode;
810 
811  Assert(containing_appendrel == NULL);
812  /* Recurse, being careful to tell myself when inside outer join */
813  switch (j->jointype)
814  {
815  case JOIN_INNER:
816  j->larg = pull_up_subqueries_recurse(root, j->larg,
817  lowest_outer_join,
818  lowest_nulling_outer_join,
819  NULL);
820  j->rarg = pull_up_subqueries_recurse(root, j->rarg,
821  lowest_outer_join,
822  lowest_nulling_outer_join,
823  NULL);
824  break;
825  case JOIN_LEFT:
826  case JOIN_SEMI:
827  case JOIN_ANTI:
828  j->larg = pull_up_subqueries_recurse(root, j->larg,
829  j,
830  lowest_nulling_outer_join,
831  NULL);
832  j->rarg = pull_up_subqueries_recurse(root, j->rarg,
833  j,
834  j,
835  NULL);
836  break;
837  case JOIN_FULL:
838  j->larg = pull_up_subqueries_recurse(root, j->larg,
839  j,
840  j,
841  NULL);
842  j->rarg = pull_up_subqueries_recurse(root, j->rarg,
843  j,
844  j,
845  NULL);
846  break;
847  case JOIN_RIGHT:
848  j->larg = pull_up_subqueries_recurse(root, j->larg,
849  j,
850  j,
851  NULL);
852  j->rarg = pull_up_subqueries_recurse(root, j->rarg,
853  j,
854  lowest_nulling_outer_join,
855  NULL);
856  break;
857  default:
858  elog(ERROR, "unrecognized join type: %d",
859  (int) j->jointype);
860  break;
861  }
862  }
863  else
864  elog(ERROR, "unrecognized node type: %d",
865  (int) nodeTag(jtnode));
866  return jtnode;
867 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Query * parse
Definition: pathnodes.h:162
static bool is_simple_values(PlannerInfo *root, RangeTblEntry *rte)
static Node * pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode, JoinExpr *lowest_outer_join, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
Definition: prepjointree.c:730
static Node * pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte, JoinExpr *lowest_outer_join, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
Definition: prepjointree.c:882
List * fromlist
Definition: primnodes.h:1564
static bool is_simple_union_all(Query *subquery)
Node * larg
Definition: primnodes.h:1543
static Node * pull_up_simple_union_all(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
List * rtable
Definition: parsenodes.h:147
#define ERROR
Definition: elog.h:46
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
static bool is_safe_append_member(Query *subquery)
Node * rarg
Definition: primnodes.h:1544
JoinType jointype
Definition: primnodes.h:1541
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
#define nodeTag(nodeptr)
Definition: nodes.h:541
RTEKind rtekind
Definition: parsenodes.h:1007
static bool is_simple_subquery(PlannerInfo *root, Query *subquery, RangeTblEntry *rte, JoinExpr *lowest_outer_join)
static Node * pull_up_constant_function(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
Query * subquery
Definition: parsenodes.h:1042
#define elog(elevel,...)
Definition: elog.h:232
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 1308 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, and RangeTblRef::rtindex.

Referenced by flatten_simple_union_all(), and pull_up_simple_union_all().

1310 {
1311  if (IsA(setOp, RangeTblRef))
1312  {
1313  RangeTblRef *rtr = (RangeTblRef *) setOp;
1314  int childRTindex;
1315  AppendRelInfo *appinfo;
1316 
1317  /*
1318  * Calculate the index in the parent's range table
1319  */
1320  childRTindex = childRToffset + rtr->rtindex;
1321 
1322  /*
1323  * Build a suitable AppendRelInfo, and attach to parent's list.
1324  */
1325  appinfo = makeNode(AppendRelInfo);
1326  appinfo->parent_relid = parentRTindex;
1327  appinfo->child_relid = childRTindex;
1328  appinfo->parent_reltype = InvalidOid;
1329  appinfo->child_reltype = InvalidOid;
1330  make_setop_translation_list(setOpQuery, childRTindex, appinfo);
1331  appinfo->parent_reloid = InvalidOid;
1332  root->append_rel_list = lappend(root->append_rel_list, appinfo);
1333 
1334  /*
1335  * Recursively apply pull_up_subqueries to the new child RTE. (We
1336  * must build the AppendRelInfo first, because this will modify it.)
1337  * Note that we can pass NULL for containing-join info even if we're
1338  * actually under an outer join, because the child's expressions
1339  * aren't going to propagate up to the join. Also, we ignore the
1340  * possibility that pull_up_subqueries_recurse() returns a different
1341  * jointree node than what we pass it; if it does, the important thing
1342  * is that it replaced the child relid in the AppendRelInfo node.
1343  */
1344  rtr = makeNode(RangeTblRef);
1345  rtr->rtindex = childRTindex;
1346  (void) pull_up_subqueries_recurse(root, (Node *) rtr,
1347  NULL, NULL, appinfo);
1348  }
1349  else if (IsA(setOp, SetOperationStmt))
1350  {
1351  SetOperationStmt *op = (SetOperationStmt *) setOp;
1352 
1353  /* Recurse to reach leaf queries */
1354  pull_up_union_leaf_queries(op->larg, root, parentRTindex, setOpQuery,
1355  childRToffset);
1356  pull_up_union_leaf_queries(op->rarg, root, parentRTindex, setOpQuery,
1357  childRToffset);
1358  }
1359  else
1360  {
1361  elog(ERROR, "unrecognized node type: %d",
1362  (int) nodeTag(setOp));
1363  }
1364 }
static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex, Query *setOpQuery, int childRToffset)
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
static Node * pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode, JoinExpr *lowest_outer_join, JoinExpr *lowest_nulling_outer_join, AppendRelInfo *containing_appendrel)
Definition: prepjointree.c:730
Definition: nodes.h:536
static void make_setop_translation_list(Query *query, int newvarno, AppendRelInfo *appinfo)
Oid parent_reltype
Definition: pathnodes.h:2315
#define ERROR
Definition: elog.h:46
List * lappend(List *list, void *datum)
Definition: list.c:336
List * append_rel_list
Definition: pathnodes.h:284
#define InvalidOid
Definition: postgres_ext.h:36
#define makeNode(_type_)
Definition: nodes.h:584
#define nodeTag(nodeptr)
Definition: nodes.h:541
#define elog(elevel,...)
Definition: elog.h:232
Index child_relid
Definition: pathnodes.h:2307
Index parent_relid
Definition: pathnodes.h:2306

◆ pullup_replace_vars()

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

Definition at line 2235 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 perform_pullup_replace_vars(), and replace_vars_in_jointree().

2236 {
2237  return replace_rte_variables(expr,
2238  context->varno, 0,
2240  (void *) context,
2241  context->outer_hasSubLinks);
2242 }
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 2245 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().

2247 {
2249  int varattno = var->varattno;
2250  Node *newnode;
2251 
2252  /*
2253  * If PlaceHolderVars are needed, we cache the modified expressions in
2254  * rcon->rv_cache[]. This is not in hopes of any material speed gain
2255  * within this function, but to avoid generating identical PHVs with
2256  * different IDs. That would result in duplicate evaluations at runtime,
2257  * and possibly prevent optimizations that rely on recognizing different
2258  * references to the same subquery output as being equal(). So it's worth
2259  * a bit of extra effort to avoid it.
2260  */
2261  if (rcon->need_phvs &&
2262  varattno >= InvalidAttrNumber &&
2263  varattno <= list_length(rcon->targetlist) &&
2264  rcon->rv_cache[varattno] != NULL)
2265  {
2266  /* Just copy the entry and fall through to adjust its varlevelsup */
2267  newnode = copyObject(rcon->rv_cache[varattno]);
2268  }
2269  else if (varattno == InvalidAttrNumber)
2270  {
2271  /* Must expand whole-tuple reference into RowExpr */
2272  RowExpr *rowexpr;
2273  List *colnames;
2274  List *fields;
2275  bool save_need_phvs = rcon->need_phvs;
2276  int save_sublevelsup = context->sublevels_up;
2277 
2278  /*
2279  * If generating an expansion for a var of a named rowtype (ie, this
2280  * is a plain relation RTE), then we must include dummy items for
2281  * dropped columns. If the var is RECORD (ie, this is a JOIN), then
2282  * omit dropped columns. Either way, attach column names to the
2283  * RowExpr for use of ruleutils.c.
2284  *
2285  * In order to be able to cache the results, we always generate the
2286  * expansion with varlevelsup = 0, and then adjust if needed.
2287  */
2288  expandRTE(rcon->target_rte,
2289  var->varno, 0 /* not varlevelsup */ , var->location,
2290  (var->vartype != RECORDOID),
2291  &colnames, &fields);
2292  /* Adjust the generated per-field Vars, but don't insert PHVs */
2293  rcon->need_phvs = false;
2294  context->sublevels_up = 0; /* to match the expandRTE output */
2295  fields = (List *) replace_rte_variables_mutator((Node *) fields,
2296  context);
2297  rcon->need_phvs = save_need_phvs;
2298  context->sublevels_up = save_sublevelsup;
2299 
2300  rowexpr = makeNode(RowExpr);
2301  rowexpr->args = fields;
2302  rowexpr->row_typeid = var->vartype;
2303  rowexpr->row_format = COERCE_IMPLICIT_CAST;
2304  rowexpr->colnames = colnames;
2305  rowexpr->location = var->location;
2306  newnode = (Node *) rowexpr;
2307 
2308  /*
2309  * Insert PlaceHolderVar if needed. Notice that we are wrapping one
2310  * PlaceHolderVar around the whole RowExpr, rather than putting one
2311  * around each element of the row. This is because we need the
2312  * expression to yield NULL, not ROW(NULL,NULL,...) when it is forced
2313  * to null by an outer join.
2314  */
2315  if (rcon->need_phvs)
2316  {
2317  /* RowExpr is certainly not strict, so always need PHV */
2318  newnode = (Node *)
2320  (Expr *) newnode,
2321  bms_make_singleton(rcon->varno));
2322  /* cache it with the PHV, and with varlevelsup still zero */
2323  rcon->rv_cache[InvalidAttrNumber] = copyObject(newnode);
2324  }
2325  }
2326  else
2327  {
2328  /* Normal case referencing one targetlist element */
2329  TargetEntry *tle = get_tle_by_resno(rcon->targetlist, varattno);
2330 
2331  if (tle == NULL) /* shouldn't happen */
2332  elog(ERROR, "could not find attribute %d in subquery targetlist",
2333  varattno);
2334 
2335  /* Make a copy of the tlist item to return */
2336  newnode = (Node *) copyObject(tle->expr);
2337 
2338  /* Insert PlaceHolderVar if needed */
2339  if (rcon->need_phvs)
2340  {
2341  bool wrap;
2342 
2343  if (newnode && IsA(newnode, Var) &&
2344  ((Var *) newnode)->varlevelsup == 0)
2345  {
2346  /*
2347  * Simple Vars always escape being wrapped, unless they are
2348  * lateral references to something outside the subquery being
2349  * pulled up. (Even then, we could omit the PlaceHolderVar if
2350  * the referenced rel is under the same lowest outer join, but
2351  * it doesn't seem worth the trouble to check that.)
2352  */
2353  if (rcon->target_rte->lateral &&
2354  !bms_is_member(((Var *) newnode)->varno, rcon->relids))
2355  wrap = true;
2356  else
2357  wrap = false;
2358  }
2359  else if (newnode && IsA(newnode, PlaceHolderVar) &&
2360  ((PlaceHolderVar *) newnode)->phlevelsup == 0)
2361  {
2362  /* No need to wrap a PlaceHolderVar with another one, either */
2363  wrap = false;
2364  }
2365  else if (rcon->wrap_non_vars)
2366  {
2367  /* Wrap all non-Vars in a PlaceHolderVar */
2368  wrap = true;
2369  }
2370  else
2371  {
2372  /*
2373  * If it contains a Var of the subquery being pulled up, and
2374  * does not contain any non-strict constructs, then it's
2375  * certainly nullable so we don't need to insert a
2376  * PlaceHolderVar.
2377  *
2378  * This analysis could be tighter: in particular, a non-strict
2379  * construct hidden within a lower-level PlaceHolderVar is not
2380  * reason to add another PHV. But for now it doesn't seem
2381  * worth the code to be more exact.
2382  *
2383  * Note: in future maybe we should insert a PlaceHolderVar
2384  * anyway, if the tlist item is expensive to evaluate?
2385  *
2386  * For a LATERAL subquery, we have to check the actual var
2387  * membership of the node, but if it's non-lateral then any
2388  * level-zero var must belong to the subquery.
2389  */
2390  if ((rcon->target_rte->lateral ?
2391  bms_overlap(pull_varnos(rcon->root, (Node *) newnode),
2392  rcon->relids) :
2393  contain_vars_of_level((Node *) newnode, 0)) &&
2394  !contain_nonstrict_functions((Node *) newnode))
2395  {
2396  /* No wrap needed */
2397  wrap = false;
2398  }
2399  else
2400  {
2401  /* Else wrap it in a PlaceHolderVar */
2402  wrap = true;
2403  }
2404  }
2405 
2406  if (wrap)
2407  newnode = (Node *)
2409  (Expr *) newnode,
2410  bms_make_singleton(rcon->varno));
2411 
2412  /*
2413  * Cache it if possible (ie, if the attno is in range, which it
2414  * probably always should be). We can cache the value even if we
2415  * decided we didn't need a PHV, since this result will be
2416  * suitable for any request that has need_phvs.
2417  */
2418  if (varattno > InvalidAttrNumber &&
2419  varattno <= list_length(rcon->targetlist))
2420  rcon->rv_cache[varattno] = copyObject(newnode);
2421  }
2422  }
2423 
2424  /* Must adjust varlevelsup if tlist item is from higher query */
2425  if (var->varlevelsup > 0)
2426  IncrementVarSublevelsUp(newnode, var->varlevelsup, 0);
2427 
2428  return newnode;
2429 }
int varno
Definition: primnodes.h:189
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:97
List * args
Definition: primnodes.h:1068
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Index varlevelsup
Definition: primnodes.h:196
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:776
Definition: nodes.h:536
AttrNumber varattno
Definition: primnodes.h:191
Definition: primnodes.h:186
PlaceHolderVar * make_placeholder_expr(PlannerInfo *root, Expr *expr, Relids phrels)
Definition: placeholder.c:39
#define ERROR
Definition: elog.h:46
List * colnames
Definition: primnodes.h:1084
RangeTblEntry * target_rte
Definition: prepjointree.c:47
Node * replace_rte_variables_mutator(Node *node, replace_rte_variables_context *context)
Oid vartype
Definition: primnodes.h:193
int location
Definition: primnodes.h:201
int location
Definition: primnodes.h:1085
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
#define makeNode(_type_)
Definition: nodes.h:584
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:431
Expr * expr
Definition: primnodes.h:1455
Oid row_typeid
Definition: primnodes.h:1069
static int list_length(const List *l)
Definition: pg_list.h:149
#define InvalidAttrNumber
Definition: attnum.h:23
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
#define elog(elevel,...)
Definition: elog.h:232
TargetEntry * get_tle_by_resno(List *tlist, AttrNumber resno)
bool contain_nonstrict_functions(Node *clause)
Definition: clauses.c:879
#define copyObject(obj)
Definition: nodes.h:652
CoercionForm row_format
Definition: primnodes.h:1083
Definition: pg_list.h:50
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427

◆ pullup_replace_vars_subquery()

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

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

2441 {
2442  Assert(IsA(query, Query));
2443  return (Query *) replace_rte_variables((Node *) query,
2444  context->varno, 1,
2446  (void *) context,
2447  NULL);
2448 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
static Node * pullup_replace_vars_callback(Var *var, replace_rte_variables_context *context)
Definition: nodes.h:536
#define Assert(condition)
Definition: c.h:804
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 2584 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().

2585 {
2587 
2588  /*
2589  * To avoid doing strictness checks on more quals than necessary, we want
2590  * to stop descending the jointree as soon as there are no outer joins
2591  * below our current point. This consideration forces a two-pass process.
2592  * The first pass gathers information about which base rels appear below
2593  * each side of each join clause, and about whether there are outer
2594  * join(s) below each side of each join clause. The second pass examines
2595  * qual clauses and changes join types as it descends the tree.
2596  */
2597  state = reduce_outer_joins_pass1((Node *) root->parse->jointree);
2598 
2599  /* planner.c shouldn't have called me if no outer joins */
2600  if (state == NULL || !state->contains_outer)
2601  elog(ERROR, "so where are the outer joins?");
2602 
2604  state, root, NULL, NIL, NIL);
2605 }
#define NIL
Definition: pg_list.h:65
Query * parse
Definition: pathnodes.h:162
FromExpr * jointree
Definition: parsenodes.h:148
Definition: nodes.h:536
#define ERROR
Definition: elog.h:46
static reduce_outer_joins_state * reduce_outer_joins_pass1(Node *jtnode)
Definition: regguts.h:317
#define elog(elevel,...)
Definition: elog.h:232
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 2613 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().

2614 {
2615  reduce_outer_joins_state *result;
2616 
2617  result = (reduce_outer_joins_state *)
2619  result->relids = NULL;
2620  result->contains_outer = false;
2621  result->sub_states = NIL;
2622 
2623  if (jtnode == NULL)
2624  return result;
2625  if (IsA(jtnode, RangeTblRef))
2626  {
2627  int varno = ((RangeTblRef *) jtnode)->rtindex;
2628 
2629  result->relids = bms_make_singleton(varno);
2630  }
2631  else if (IsA(jtnode, FromExpr))
2632  {
2633  FromExpr *f = (FromExpr *) jtnode;
2634  ListCell *l;
2635 
2636  foreach(l, f->fromlist)
2637  {
2638  reduce_outer_joins_state *sub_state;
2639 
2640  sub_state = reduce_outer_joins_pass1(lfirst(l));
2641  result->relids = bms_add_members(result->relids,
2642  sub_state->relids);
2643  result->contains_outer |= sub_state->contains_outer;
2644  result->sub_states = lappend(result->sub_states, sub_state);
2645  }
2646  }
2647  else if (IsA(jtnode, JoinExpr))
2648  {
2649  JoinExpr *j = (JoinExpr *) jtnode;
2650  reduce_outer_joins_state *sub_state;
2651 
2652  /* join's own RT index is not wanted in result->relids */
2653  if (IS_OUTER_JOIN(j->jointype))
2654  result->contains_outer = true;
2655 
2656  sub_state = reduce_outer_joins_pass1(j->larg);
2657  result->relids = bms_add_members(result->relids,
2658  sub_state->relids);
2659  result->contains_outer |= sub_state->contains_outer;
2660  result->sub_states = lappend(result->sub_states, sub_state);
2661 
2662  sub_state = reduce_outer_joins_pass1(j->rarg);
2663  result->relids = bms_add_members(result->relids,
2664  sub_state->relids);
2665  result->contains_outer |= sub_state->contains_outer;
2666  result->sub_states = lappend(result->sub_states, sub_state);
2667  }
2668  else
2669  elog(ERROR, "unrecognized node type: %d",
2670  (int) nodeTag(jtnode));
2671  return result;
2672 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
#define IS_OUTER_JOIN(jointype)
Definition: nodes.h:753
List * fromlist
Definition: primnodes.h:1564
Node * larg
Definition: primnodes.h:1543
#define ERROR
Definition: elog.h:46
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
List * lappend(List *list, void *datum)
Definition: list.c:336
Node * rarg
Definition: primnodes.h:1544
static reduce_outer_joins_state * reduce_outer_joins_pass1(Node *jtnode)
JoinType jointype
Definition: primnodes.h:1541
#define lfirst(lc)
Definition: pg_list.h:169
#define nodeTag(nodeptr)
Definition: nodes.h:541
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793

◆ 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 2685 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().

2691 {
2692  /*
2693  * pass 2 should never descend as far as an empty subnode or base rel,
2694  * because it's only called on subtrees marked as contains_outer.
2695  */
2696  if (jtnode == NULL)
2697  elog(ERROR, "reached empty jointree");
2698  if (IsA(jtnode, RangeTblRef))
2699  elog(ERROR, "reached base rel");
2700  else if (IsA(jtnode, FromExpr))
2701  {
2702  FromExpr *f = (FromExpr *) jtnode;
2703  ListCell *l;
2704  ListCell *s;
2705  Relids pass_nonnullable_rels;
2706  List *pass_nonnullable_vars;
2707  List *pass_forced_null_vars;
2708 
2709  /* Scan quals to see if we can add any constraints */
2710  pass_nonnullable_rels = find_nonnullable_rels(f->quals);
2711  pass_nonnullable_rels = bms_add_members(pass_nonnullable_rels,
2712  nonnullable_rels);
2713  pass_nonnullable_vars = find_nonnullable_vars(f->quals);
2714  pass_nonnullable_vars = list_concat(pass_nonnullable_vars,
2715  nonnullable_vars);
2716  pass_forced_null_vars = find_forced_null_vars(f->quals);
2717  pass_forced_null_vars = list_concat(pass_forced_null_vars,
2718  forced_null_vars);
2719  /* And recurse --- but only into interesting subtrees */
2721  forboth(l, f->fromlist, s, state->sub_states)
2722  {
2723  reduce_outer_joins_state *sub_state = lfirst(s);
2724 
2725  if (sub_state->contains_outer)
2726  reduce_outer_joins_pass2(lfirst(l), sub_state, root,
2727  pass_nonnullable_rels,
2728  pass_nonnullable_vars,
2729  pass_forced_null_vars);
2730  }
2731  bms_free(pass_nonnullable_rels);
2732  /* can't so easily clean up var lists, unfortunately */
2733  }
2734  else if (IsA(jtnode, JoinExpr))
2735  {
2736  JoinExpr *j = (JoinExpr *) jtnode;
2737  int rtindex = j->rtindex;
2738  JoinType jointype = j->jointype;
2739  reduce_outer_joins_state *left_state = linitial(state->sub_states);
2740  reduce_outer_joins_state *right_state = lsecond(state->sub_states);
2741  List *local_nonnullable_vars = NIL;
2742  bool computed_local_nonnullable_vars = false;
2743 
2744  /* Can we simplify this join? */
2745  switch (jointype)
2746  {
2747  case JOIN_INNER:
2748  break;
2749  case JOIN_LEFT:
2750  if (bms_overlap(nonnullable_rels, right_state->relids))
2751  jointype = JOIN_INNER;
2752  break;
2753  case JOIN_RIGHT:
2754  if (bms_overlap(nonnullable_rels, left_state->relids))
2755  jointype = JOIN_INNER;
2756  break;
2757  case JOIN_FULL:
2758  if (bms_overlap(nonnullable_rels, left_state->relids))
2759  {
2760  if (bms_overlap(nonnullable_rels, right_state->relids))
2761  jointype = JOIN_INNER;
2762  else
2763  jointype = JOIN_LEFT;
2764  }
2765  else
2766  {
2767  if (bms_overlap(nonnullable_rels, right_state->relids))
2768  jointype = JOIN_RIGHT;
2769  }
2770  break;
2771  case JOIN_SEMI:
2772  case JOIN_ANTI:
2773 
2774  /*
2775  * These could only have been introduced by pull_up_sublinks,
2776  * so there's no way that upper quals could refer to their
2777  * righthand sides, and no point in checking.
2778  */
2779  break;
2780  default:
2781  elog(ERROR, "unrecognized join type: %d",
2782  (int) jointype);
2783  break;
2784  }
2785 
2786  /*
2787  * Convert JOIN_RIGHT to JOIN_LEFT. Note that in the case where we
2788  * reduced JOIN_FULL to JOIN_RIGHT, this will mean the JoinExpr no
2789  * longer matches the internal ordering of any CoalesceExpr's built to
2790  * represent merged join variables. We don't care about that at
2791  * present, but be wary of it ...
2792  */
2793  if (jointype == JOIN_RIGHT)
2794  {
2795  Node *tmparg;
2796 
2797  tmparg = j->larg;
2798  j->larg = j->rarg;
2799  j->rarg = tmparg;
2800  jointype = JOIN_LEFT;
2801  right_state = linitial(state->sub_states);
2802  left_state = lsecond(state->sub_states);
2803  }
2804 
2805  /*
2806  * See if we can reduce JOIN_LEFT to JOIN_ANTI. This is the case if
2807  * the join's own quals are strict for any var that was forced null by
2808  * higher qual levels. NOTE: there are other ways that we could
2809  * detect an anti-join, in particular if we were to check whether Vars
2810  * coming from the RHS must be non-null because of table constraints.
2811  * That seems complicated and expensive though (in particular, one
2812  * would have to be wary of lower outer joins). For the moment this
2813  * seems sufficient.
2814  */
2815  if (jointype == JOIN_LEFT)
2816  {
2817  List *overlap;
2818 
2819  local_nonnullable_vars = find_nonnullable_vars(j->quals);
2820  computed_local_nonnullable_vars = true;
2821 
2822  /*
2823  * It's not sufficient to check whether local_nonnullable_vars and
2824  * forced_null_vars overlap: we need to know if the overlap
2825  * includes any RHS variables.
2826  */
2827  overlap = list_intersection(local_nonnullable_vars,
2828  forced_null_vars);
2829  if (overlap != NIL &&
2830  bms_overlap(pull_varnos(root, (Node *) overlap),
2831  right_state->relids))
2832  jointype = JOIN_ANTI;
2833  }
2834 
2835  /* Apply the jointype change, if any, to both jointree node and RTE */
2836  if (rtindex && jointype != j->jointype)
2837  {
2838  RangeTblEntry *rte = rt_fetch(rtindex, root->parse->rtable);
2839 
2840  Assert(rte->rtekind == RTE_JOIN);
2841  Assert(rte->jointype == j->jointype);
2842  rte->jointype = jointype;
2843  }
2844  j->jointype = jointype;
2845 
2846  /* Only recurse if there's more to do below here */
2847  if (left_state->contains_outer || right_state->contains_outer)
2848  {
2849  Relids local_nonnullable_rels;
2850  List *local_forced_null_vars;
2851  Relids pass_nonnullable_rels;
2852  List *pass_nonnullable_vars;
2853  List *pass_forced_null_vars;
2854 
2855  /*
2856  * If this join is (now) inner, we can add any constraints its
2857  * quals provide to those we got from above. But if it is outer,
2858  * we can pass down the local constraints only into the nullable
2859  * side, because an outer join never eliminates any rows from its
2860  * non-nullable side. Also, there is no point in passing upper
2861  * constraints into the nullable side, since if there were any
2862  * we'd have been able to reduce the join. (In the case of upper
2863  * forced-null constraints, we *must not* pass them into the
2864  * nullable side --- they either applied here, or not.) The upshot
2865  * is that we pass either the local or the upper constraints,
2866  * never both, to the children of an outer join.
2867  *
2868  * Note that a SEMI join works like an inner join here: it's okay
2869  * to pass down both local and upper constraints. (There can't be
2870  * any upper constraints affecting its inner side, but it's not
2871  * worth having a separate code path to avoid passing them.)
2872  *
2873  * At a FULL join we just punt and pass nothing down --- is it
2874  * possible to be smarter?
2875  */
2876  if (jointype != JOIN_FULL)
2877  {
2878  local_nonnullable_rels = find_nonnullable_rels(j->quals);
2879  if (!computed_local_nonnullable_vars)
2880  local_nonnullable_vars = find_nonnullable_vars(j->quals);
2881  local_forced_null_vars = find_forced_null_vars(j->quals);
2882  if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
2883  {
2884  /* OK to merge upper and local constraints */
2885  local_nonnullable_rels = bms_add_members(local_nonnullable_rels,
2886  nonnullable_rels);
2887  local_nonnullable_vars = list_concat(local_nonnullable_vars,
2888  nonnullable_vars);
2889  local_forced_null_vars = list_concat(local_forced_null_vars,
2890  forced_null_vars);
2891  }
2892  }
2893  else
2894  {
2895  /* no use in calculating these */
2896  local_nonnullable_rels = NULL;
2897  local_forced_null_vars = NIL;
2898  }
2899 
2900  if (left_state->contains_outer)
2901  {
2902  if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
2903  {
2904  /* pass union of local and upper constraints */
2905  pass_nonnullable_rels = local_nonnullable_rels;
2906  pass_nonnullable_vars = local_nonnullable_vars;
2907  pass_forced_null_vars = local_forced_null_vars;
2908  }
2909  else if (jointype != JOIN_FULL) /* ie, LEFT or ANTI */
2910  {
2911  /* can't pass local constraints to non-nullable side */
2912  pass_nonnullable_rels = nonnullable_rels;
2913  pass_nonnullable_vars = nonnullable_vars;
2914  pass_forced_null_vars = forced_null_vars;
2915  }
2916  else
2917  {
2918  /* no constraints pass through JOIN_FULL */
2919  pass_nonnullable_rels = NULL;
2920  pass_nonnullable_vars = NIL;
2921  pass_forced_null_vars = NIL;
2922  }
2923  reduce_outer_joins_pass2(j->larg, left_state, root,
2924  pass_nonnullable_rels,
2925  pass_nonnullable_vars,
2926  pass_forced_null_vars);
2927  }
2928 
2929  if (right_state->contains_outer)
2930  {
2931  if (jointype != JOIN_FULL) /* ie, INNER/LEFT/SEMI/ANTI */
2932  {
2933  /* pass appropriate constraints, per comment above */
2934  pass_nonnullable_rels = local_nonnullable_rels;
2935  pass_nonnullable_vars = local_nonnullable_vars;
2936  pass_forced_null_vars = local_forced_null_vars;
2937  }
2938  else
2939  {
2940  /* no constraints pass through JOIN_FULL */
2941  pass_nonnullable_rels = NULL;
2942  pass_nonnullable_vars = NIL;
2943  pass_forced_null_vars = NIL;
2944  }
2945  reduce_outer_joins_pass2(j->rarg, right_state, root,
2946  pass_nonnullable_rels,
2947  pass_nonnullable_vars,
2948  pass_forced_null_vars);
2949  }
2950  bms_free(local_nonnullable_rels);
2951  }
2952  }
2953  else
2954  elog(ERROR, "unrecognized node type: %d",
2955  (int) nodeTag(jtnode));
2956 }
#define NIL
Definition: pg_list.h:65
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:97
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Query * parse
Definition: pathnodes.h:162
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
Definition: nodes.h:536
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
List * fromlist
Definition: primnodes.h:1564
Node * quals
Definition: primnodes.h:1565
#define lsecond(l)
Definition: pg_list.h:179
JoinType
Definition: nodes.h:704
Node * larg
Definition: primnodes.h:1543
List * find_forced_null_vars(Node *node)
Definition: clauses.c:1760
#define linitial(l)
Definition: pg_list.h:174
List * rtable
Definition: parsenodes.h:147
#define ERROR
Definition: elog.h:46
Relids find_nonnullable_rels(Node *clause)
Definition: clauses.c:1342
JoinType jointype
Definition: parsenodes.h:1080
List * list_intersection(const List *list1, const List *list2)
Definition: list.c:1033
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Node * quals
Definition: primnodes.h:1547
List * find_nonnullable_vars(Node *clause)
Definition: clauses.c:1567
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
Node * rarg
Definition: primnodes.h:1544
JoinType jointype
Definition: primnodes.h:1541
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
#define nodeTag(nodeptr)
Definition: nodes.h:541
RTEKind rtekind
Definition: parsenodes.h:1007
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:494
#define elog(elevel,...)
Definition: elog.h:232
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:1549
Definition: pg_list.h:50
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793

◆ remove_result_refs()

static void remove_result_refs ( PlannerInfo root,
int  varno,
Node newjtloc 
)
static

Definition at line 3271 of file prepjointree.c.

References PlannerInfo::append_rel_list, Assert, bms_is_empty(), fix_append_rel_relids(), get_relids_in_jointree(), PlannerInfo::glob, PlannerGlobal::lastPHId, PlannerInfo::parse, and substitute_phv_relids().

Referenced by remove_useless_results_recurse().

3272 {
3273  /* Fix up PlaceHolderVars as needed */
3274  /* If there are no PHVs anywhere, we can skip this bit */
3275  if (root->glob->lastPHId != 0)
3276  {
3277  Relids subrelids;
3278 
3279  subrelids = get_relids_in_jointree(newjtloc, false);
3280  Assert(!bms_is_empty(subrelids));
3281  substitute_phv_relids((Node *) root->parse, varno, subrelids);
3282  fix_append_rel_relids(root->append_rel_list, varno, subrelids);
3283  }
3284 
3285  /*
3286  * We also need to remove any PlanRowMark referencing the RTE, but we
3287  * postpone that work until we return to remove_useless_result_rtes.
3288  */
3289 }
static void substitute_phv_relids(Node *node, int varno, Relids subrelids)
Query * parse
Definition: pathnodes.h:162
Definition: nodes.h:536
Relids get_relids_in_jointree(Node *jtnode, bool include_joins)
PlannerGlobal * glob
Definition: pathnodes.h:164
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
List * append_rel_list
Definition: pathnodes.h:284
Index lastPHId
Definition: pathnodes.h:116
static void fix_append_rel_relids(List *append_rel_list, int varno, Relids subrelids)
#define Assert(condition)
Definition: c.h:804

◆ remove_useless_result_rtes()

void remove_useless_result_rtes ( PlannerInfo root)

Definition at line 2997 of file prepjointree.c.

References Assert, foreach_delete_current, IsA, Query::jointree, lfirst, PlannerInfo::parse, remove_useless_results_recurse(), PlannerInfo::rowMarks, rt_fetch, Query::rtable, RTE_RESULT, and PlanRowMark::rti.

Referenced by subquery_planner().

2998 {
2999  ListCell *cell;
3000 
3001  /* Top level of jointree must always be a FromExpr */
3002  Assert(IsA(root->parse->jointree, FromExpr));
3003  /* Recurse ... */
3004  root->parse->jointree = (FromExpr *)
3006  /* We should still have a FromExpr */
3007  Assert(IsA(root->parse->jointree, FromExpr));
3008 
3009  /*
3010  * Remove any PlanRowMark referencing an RTE_RESULT RTE. We obviously
3011  * must do that for any RTE_RESULT that we just removed. But one for a
3012  * RTE that we did not remove can be dropped anyway: since the RTE has
3013  * only one possible output row, there is no need for EPQ to mark and
3014  * restore that row.
3015  *
3016  * It's necessary, not optional, to remove the PlanRowMark for a surviving
3017  * RTE_RESULT RTE; otherwise we'll generate a whole-row Var for the
3018  * RTE_RESULT, which the executor has no support for.
3019  */
3020  foreach(cell, root->rowMarks)
3021  {
3022  PlanRowMark *rc = (PlanRowMark *) lfirst(cell);
3023 
3024  if (rt_fetch(rc->rti, root->parse->rtable)->rtekind == RTE_RESULT)
3025  root->rowMarks = foreach_delete_current(root->rowMarks, cell);
3026  }
3027 }
List * rowMarks
Definition: pathnodes.h:288
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Query * parse
Definition: pathnodes.h:162
FromExpr * jointree
Definition: parsenodes.h:148
Definition: nodes.h:536
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:369
List * rtable
Definition: parsenodes.h:147
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
static Node * remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)

◆ remove_useless_results_recurse()

static Node * remove_useless_results_recurse ( PlannerInfo root,
Node jtnode 
)
static

Definition at line 3036 of file prepjointree.c.

References Assert, bms_add_member(), bms_next_member(), elog, ERROR, find_dependent_phvs(), find_dependent_phvs_in_jointree(), foreach_delete_current, FromExpr::fromlist, get_result_relid(), IsA, JOIN_ANTI, JOIN_FULL, JOIN_INNER, JOIN_LEFT, JOIN_RIGHT, JOIN_SEMI, Query::jointree, JoinExpr::jointype, JoinExpr::larg, lfirst, linitial, list_length(), list_make1, makeFromExpr(), nodeTag, PlannerInfo::parse, JoinExpr::quals, FromExpr::quals, JoinExpr::rarg, remove_result_refs(), and pullup_replace_vars_context::varno.

Referenced by remove_useless_result_rtes().

3037 {
3038  Assert(jtnode != NULL);
3039  if (IsA(jtnode, RangeTblRef))
3040  {
3041  /* Can't immediately do anything with a RangeTblRef */
3042  }
3043  else if (IsA(jtnode, FromExpr))
3044  {
3045  FromExpr *f = (FromExpr *) jtnode;
3046  Relids result_relids = NULL;
3047  ListCell *cell;
3048 
3049  /*
3050  * We can drop RTE_RESULT rels from the fromlist so long as at least
3051  * one child remains, since joining to a one-row table changes
3052  * nothing. (But we can't drop a RTE_RESULT that computes PHV(s) that
3053  * are needed by some sibling. The cleanup transformation below would
3054  * reassign the PHVs to be computed at the join, which is too late for
3055  * the sibling's use.) The easiest way to mechanize this rule is to
3056  * modify the list in-place.
3057  */
3058  foreach(cell, f->fromlist)
3059  {
3060  Node *child = (Node *) lfirst(cell);
3061  int varno;
3062 
3063  /* Recursively transform child ... */
3064  child = remove_useless_results_recurse(root, child);
3065  /* ... and stick it back into the tree */
3066  lfirst(cell) = child;
3067 
3068  /*
3069  * If it's an RTE_RESULT with at least one sibling, and no sibling
3070  * references dependent PHVs, we can drop it. We don't yet know
3071  * what the inner join's final relid set will be, so postpone
3072  * cleanup of PHVs etc till after this loop.
3073  */
3074  if (list_length(f->fromlist) > 1 &&
3075  (varno = get_result_relid(root, child)) != 0 &&
3076  !find_dependent_phvs_in_jointree(root, (Node *) f, varno))
3077  {
3078  f->fromlist = foreach_delete_current(f->fromlist, cell);
3079  result_relids = bms_add_member(result_relids, varno);
3080  }
3081  }
3082 
3083  /*
3084  * Clean up if we dropped any RTE_RESULT RTEs. This is a bit
3085  * inefficient if there's more than one, but it seems better to
3086  * optimize the support code for the single-relid case.
3087  */
3088  if (result_relids)
3089  {
3090  int varno = -1;
3091 
3092  while ((varno = bms_next_member(result_relids, varno)) >= 0)
3093  remove_result_refs(root, varno, (Node *) f);
3094  }
3095 
3096  /*
3097  * If we're not at the top of the jointree, it's valid to simplify a
3098  * degenerate FromExpr into its single child. (At the top, we must
3099  * keep the FromExpr since Query.jointree is required to point to a
3100  * FromExpr.)
3101  */
3102  if (f != root->parse->jointree &&
3103  f->quals == NULL &&
3104  list_length(f->fromlist) == 1)
3105  return (Node *) linitial(f->fromlist);
3106  }
3107  else if (IsA(jtnode, JoinExpr))
3108  {
3109  JoinExpr *j = (JoinExpr *) jtnode;
3110  int varno;
3111 
3112  /* First, recurse */
3113  j->larg = remove_useless_results_recurse(root, j->larg);
3114  j->rarg = remove_useless_results_recurse(root, j->rarg);
3115 
3116  /* Apply join-type-specific optimization rules */
3117  switch (j->jointype)
3118  {
3119  case JOIN_INNER:
3120 
3121  /*
3122  * An inner join is equivalent to a FromExpr, so if either
3123  * side was simplified to an RTE_RESULT rel, we can replace
3124  * the join with a FromExpr with just the other side; and if
3125  * the qual is empty (JOIN ON TRUE) then we can omit the
3126  * FromExpr as well.
3127  *
3128  * Just as in the FromExpr case, we can't simplify if the
3129  * other input rel references any PHVs that are marked as to
3130  * be evaluated at the RTE_RESULT rel, because we can't
3131  * postpone their evaluation in that case. But we only have
3132  * to check this in cases where it's syntactically legal for
3133  * the other input to have a LATERAL reference to the
3134  * RTE_RESULT rel. Only RHSes of inner and left joins are
3135  * allowed to have such refs.
3136  */
3137  if ((varno = get_result_relid(root, j->larg)) != 0 &&
3138  !find_dependent_phvs_in_jointree(root, j->rarg, varno))
3139  {
3140  remove_result_refs(root, varno, j->rarg);
3141  if (j->quals)
3142  jtnode = (Node *)
3143  makeFromExpr(list_make1(j->rarg), j->quals);
3144  else
3145  jtnode = j->rarg;
3146  }
3147  else if ((varno = get_result_relid(root, j->rarg)) != 0)
3148  {
3149  remove_result_refs(root, varno, j->larg);
3150  if (j->quals)
3151  jtnode = (Node *)
3152  makeFromExpr(list_make1(j->larg), j->quals);
3153  else
3154  jtnode = j->larg;
3155  }
3156  break;
3157  case JOIN_LEFT:
3158 
3159  /*
3160  * We can simplify this case if the RHS is an RTE_RESULT, with
3161  * two different possibilities:
3162  *
3163  * If the qual is empty (JOIN ON TRUE), then the join can be
3164  * strength-reduced to a plain inner join, since each LHS row
3165  * necessarily has exactly one join partner. So we can always
3166  * discard the RHS, much as in the JOIN_INNER case above.
3167  * (Again, the LHS could not contain a lateral reference to
3168  * the RHS.)
3169  *
3170  * Otherwise, it's still true that each LHS row should be
3171  * returned exactly once, and since the RHS returns no columns
3172  * (unless there are PHVs that have to be evaluated there), we
3173  * don't much care if it's null-extended or not. So in this
3174  * case also, we can just ignore the qual and discard the left
3175  * join.
3176  */
3177  if ((varno = get_result_relid(root, j->rarg)) != 0 &&
3178  (j->quals == NULL ||
3179  !find_dependent_phvs(root, varno)))
3180  {
3181  remove_result_refs(root, varno, j->larg);
3182  jtnode = j->larg;
3183  }
3184  break;
3185  case JOIN_RIGHT:
3186  /* Mirror-image of the JOIN_LEFT case */
3187  if ((varno = get_result_relid(root, j->larg)) != 0 &&
3188  (j->quals == NULL ||
3189  !find_dependent_phvs(root, varno)))
3190  {
3191  remove_result_refs(root, varno, j->rarg);
3192  jtnode = j->rarg;
3193  }
3194  break;
3195  case JOIN_SEMI:
3196 
3197  /*
3198  * We may simplify this case if the RHS is an RTE_RESULT; the
3199  * join qual becomes effectively just a filter qual for the
3200  * LHS, since we should either return the LHS row or not. For
3201  * simplicity we inject the filter qual into a new FromExpr.
3202  *
3203  * Unlike the LEFT/RIGHT cases, we just Assert that there are
3204  * no PHVs that need to be evaluated at the semijoin's RHS,
3205  * since the rest of the query couldn't reference any outputs
3206  * of the semijoin's RHS.
3207  */
3208  if ((varno = get_result_relid(root, j->rarg)) != 0)
3209  {
3210  Assert(!find_dependent_phvs(root, varno));
3211  remove_result_refs(root, varno, j->larg);
3212  if (j->quals)
3213  jtnode = (Node *)
3214  makeFromExpr(list_make1(j->larg), j->quals);
3215  else
3216  jtnode = j->larg;
3217  }
3218  break;
3219  case JOIN_FULL:
3220  case JOIN_ANTI:
3221  /* We have no special smarts for these cases */
3222  break;
3223  default:
3224  elog(ERROR, "unrecognized join type: %d",
3225  (int) j->jointype);
3226  break;
3227  }
3228  }
3229  else
3230  elog(ERROR, "unrecognized node type: %d",
3231  (int) nodeTag(jtnode));
3232  return jtnode;
3233 }
FromExpr * makeFromExpr(List *fromlist, Node *quals)
Definition: makefuncs.c:285
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Query * parse
Definition: pathnodes.h:162
static int get_result_relid(PlannerInfo *root, Node *jtnode)
FromExpr * jointree
Definition: parsenodes.h:148
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Definition: nodes.h:536
List * fromlist
Definition: primnodes.h:1564
static bool find_dependent_phvs(PlannerInfo *root, int varno)
static void remove_result_refs(PlannerInfo *root, int varno, Node *newjtloc)
Node * quals
Definition: primnodes.h:1565
static bool find_dependent_phvs_in_jointree(PlannerInfo *root, Node *node, int varno)
Node * larg
Definition: primnodes.h:1543
#define foreach_delete_current(lst, cell)
Definition: pg_list.h:369
#define list_make1(x1)
Definition: pg_list.h:206
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
Node * quals
Definition: primnodes.h:1547
Node * rarg
Definition: primnodes.h:1544
JoinType jointype
Definition: primnodes.h:1541
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define nodeTag(nodeptr)
Definition: nodes.h:541
#define elog(elevel,...)
Definition: elog.h:232
static Node * remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)

◆ replace_empty_jointree()

void replace_empty_jointree ( Query parse)

Definition at line 150 of file prepjointree.c.

References RangeTblEntry::eref, FromExpr::fromlist, Query::jointree, lappend(), list_length(), list_make1, makeAlias(), makeNode, NIL, Query::rtable, RTE_RESULT, RangeTblEntry::rtekind, RangeTblRef::rtindex, and Query::setOperations.

Referenced by convert_EXISTS_sublink_to_join(), pull_up_simple_subquery(), and subquery_planner().

151 {
152  RangeTblEntry *rte;
153  Index rti;
154  RangeTblRef *rtr;
155 
156  /* Nothing to do if jointree is already nonempty */
157  if (parse->jointree->fromlist != NIL)
158  return;
159 
160  /* We mustn't change it in the top level of a setop tree, either */
161  if (parse->setOperations)
162  return;
163 
164  /* Create suitable RTE */
165  rte = makeNode(RangeTblEntry);
166  rte->rtekind = RTE_RESULT;
167  rte->eref = makeAlias("*RESULT*", NIL);
168 
169  /* Add it to rangetable */
170  parse->rtable = lappend(parse->rtable, rte);
171  rti = list_length(parse->rtable);
172 
173  /* And jam a reference into the jointree */
174  rtr = makeNode(RangeTblRef);
175  rtr->rtindex = rti;
176  parse->jointree->fromlist = list_make1(rtr);
177 }
#define NIL
Definition: pg_list.h:65
FromExpr * jointree
Definition: parsenodes.h:148
List * fromlist
Definition: primnodes.h:1564
#define list_make1(x1)
Definition: pg_list.h:206
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
List * rtable
Definition: parsenodes.h:147
List * lappend(List *list, void *datum)
Definition: list.c:336
unsigned int Index
Definition: c.h:549
#define makeNode(_type_)
Definition: nodes.h:584
static int list_length(const List *l)
Definition: pg_list.h:149
RTEKind rtekind
Definition: parsenodes.h:1007
Node * setOperations
Definition: parsenodes.h:177
Alias * eref
Definition: parsenodes.h:1153

◆ 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 2114 of file prepjointree.c.

References Assert, elog, ERROR, FromExpr::fromlist, RangeTblEntry::functions, IsA, JOIN_FULL, JoinExpr::jointype, 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_RESULT, 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 perform_pullup_replace_vars().

2117 {
2118  if (jtnode == NULL)
2119  return;
2120  if (IsA(jtnode, RangeTblRef))
2121  {
2122  /*
2123  * If the RangeTblRef refers to a LATERAL subquery (that isn't the
2124  * same subquery we're pulling up), it might contain references to the
2125  * target subquery, which we must replace. We drive this from the
2126  * jointree scan, rather than a scan of the rtable, for a couple of
2127  * reasons: we can avoid processing no-longer-referenced RTEs, and we
2128  * can use the appropriate setting of need_phvs depending on whether
2129  * the RTE is above possibly-nulling outer joins or not.
2130  */
2131  int varno = ((RangeTblRef *) jtnode)->rtindex;
2132 
2133  if (varno != context->varno) /* ignore target subquery itself */
2134  {
2135  RangeTblEntry *rte = rt_fetch(varno, context->root->parse->rtable);
2136 
2137  Assert(rte != context->target_rte);
2138  if (rte->lateral)
2139  {
2140  switch (rte->rtekind)
2141  {
2142  case RTE_RELATION:
2143  /* shouldn't be marked LATERAL unless tablesample */
2144  Assert(rte->tablesample);
2145  rte->tablesample = (TableSampleClause *)
2147  context);
2148  break;
2149  case RTE_SUBQUERY:
2150  rte->subquery =
2152  context);
2153  break;
2154  case RTE_FUNCTION:
2155  rte->functions = (List *)
2157  context);
2158  break;
2159  case RTE_TABLEFUNC:
2160  rte->tablefunc = (TableFunc *)
2162  context);
2163  break;
2164  case RTE_VALUES:
2165  rte->values_lists = (List *)
2167  context);
2168  break;
2169  case RTE_JOIN:
2170  case RTE_CTE:
2171  case RTE_NAMEDTUPLESTORE:
2172  case RTE_RESULT:
2173  /* these shouldn't be marked LATERAL */
2174  Assert(false);
2175  break;
2176  }
2177  }
2178  }
2179  }
2180  else if (IsA(jtnode, FromExpr))
2181  {
2182  FromExpr *f = (FromExpr *) jtnode;
2183  ListCell *l;
2184 
2185  foreach(l, f->fromlist)
2186  replace_vars_in_jointree(lfirst(l), context,
2187  lowest_nulling_outer_join);
2188  f->quals = pullup_replace_vars(f->quals, context);
2189  }
2190  else if (IsA(jtnode, JoinExpr))
2191  {
2192  JoinExpr *j = (JoinExpr *) jtnode;
2193  bool save_need_phvs = context->need_phvs;
2194 
2195  if (j == lowest_nulling_outer_join)
2196  {
2197  /* no more PHVs in or below this join */
2198  context->need_phvs = false;
2199  lowest_nulling_outer_join = NULL;
2200  }
2201  replace_vars_in_jointree(j->larg, context, lowest_nulling_outer_join);
2202  replace_vars_in_jointree(j->rarg, context, lowest_nulling_outer_join);
2203 
2204  /*
2205  * Use PHVs within the join quals of a full join, even when it's the
2206  * lowest nulling outer join. Otherwise, we cannot identify which
2207  * side of the join a pulled-up var-free expression came from, which
2208  * can lead to failure to make a plan at all because none of the quals
2209  * appear to be mergeable or hashable conditions. For this purpose we
2210  * don't care about the state of wrap_non_vars, so leave it alone.
2211  */
2212  if (j->jointype == JOIN_FULL)
2213  context->need_phvs = true;
2214 
2215  j->quals = pullup_replace_vars(j->quals, context);
2216 
2217  /*
2218  * We don't bother to update the colvars list, since it won't be used
2219  * again ...
2220  */
2221  context->need_phvs = save_need_phvs;
2222  }
2223  else
2224  elog(ERROR, "unrecognized node type: %d",
2225  (int) nodeTag(jtnode));
2226 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
Query * parse
Definition: pathnodes.h:162
Definition: nodes.h:536
List * fromlist
Definition: primnodes.h:1564
List * values_lists
Definition: parsenodes.h:1112
Node * quals
Definition: primnodes.h:1565
Node * larg
Definition: primnodes.h:1543
List * rtable
Definition: parsenodes.h:147
#define ERROR
Definition: elog.h:46
RangeTblEntry * target_rte
Definition: prepjointree.c:47
TableFunc * tablefunc
Definition: parsenodes.h:1107
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Node * quals
Definition: primnodes.h:1547
Node * rarg
Definition: primnodes.h:1544
JoinType jointype
Definition: primnodes.h:1541
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List * functions
Definition: parsenodes.h:1101
#define nodeTag(nodeptr)
Definition: nodes.h:541
RTEKind rtekind
Definition: parsenodes.h:1007
static void replace_vars_in_jointree(Node *jtnode, pullup_replace_vars_context *context, JoinExpr *lowest_nulling_outer_join)
Query * subquery
Definition: parsenodes.h:1042
static Query * pullup_replace_vars_subquery(Query *query, pullup_replace_vars_context *context)
#define elog(elevel,...)
Definition: elog.h:232
static Node * pullup_replace_vars(Node *expr, pullup_replace_vars_context *context)
Definition: pg_list.h:50
struct TableSampleClause * tablesample
Definition: parsenodes.h:1037

◆ substitute_phv_relids()

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

Definition at line 3473 of file prepjointree.c.

References query_or_expression_tree_walker(), substitute_phv_relids_context::sublevels_up, substitute_phv_relids_context::subrelids, substitute_phv_relids_walker(), pullup_replace_vars_context::varno, and substitute_phv_relids_context::varno.

Referenced by fix_append_rel_relids(), pull_up_simple_subquery(), and remove_result_refs().

3474 {
3476 
3477  context.varno = varno;
3478  context.sublevels_up = 0;
3479  context.subrelids = subrelids;
3480 
3481  /*
3482  * Must be prepared to start with a Query or a bare expression tree.
3483  */
3486  (void *) &context,
3487  0);
3488 }
static bool substitute_phv_relids_walker(Node *node, substitute_phv_relids_context *context)
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3461

◆ substitute_phv_relids_walker()

static bool substitute_phv_relids_walker ( Node node,
substitute_phv_relids_context context 
)
static

Definition at line 3429 of file prepjointree.c.

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

Referenced by substitute_phv_relids().

3431 {
3432  if (node == NULL)
3433  return false;
3434  if (IsA(node, PlaceHolderVar))
3435  {
3436  PlaceHolderVar *phv = (PlaceHolderVar *) node;
3437 
3438  if (phv->phlevelsup == context->sublevels_up &&
3439  bms_is_member(context->varno, phv->phrels))
3440  {
3441  phv->phrels = bms_union(phv->phrels,
3442  context->subrelids);
3443  phv->phrels = bms_del_member(phv->phrels,
3444  context->varno);
3445  /* Assert we haven't broken the PHV */
3446  Assert(!bms_is_empty(phv->phrels));
3447  }
3448  /* fall through to examine children */
3449  }
3450  if (IsA(node, Query))
3451  {
3452  /* Recurse into subselects */
3453  bool result;
3454 
3455  context->sublevels_up++;
3456  result = query_tree_walker((Query *) node,
3458  (void *) context, 0);
3459  context->sublevels_up--;
3460  return result;
3461  }
3462  /* Shouldn't need to handle planner auxiliary nodes here */
3463  Assert(!IsA(node, SpecialJoinInfo));
3464  Assert(!IsA(node, AppendRelInfo));
3465  Assert(!IsA(node, PlaceHolderInfo));
3466  Assert(!IsA(node, MinMaxAggInfo));
3467 
3469  (void *) context);
3470 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
static bool substitute_phv_relids_walker(Node *node, substitute_phv_relids_context *context)
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
#define Assert(condition)
Definition: c.h:804
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
Index phlevelsup
Definition: pathnodes.h:2184
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:773
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:427