PostgreSQL Source Code  git master
var.c File Reference
#include "postgres.h"
#include "access/sysattr.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
#include "optimizer/placeholder.h"
#include "optimizer/prep.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteManip.h"
Include dependency graph for var.c:

Go to the source code of this file.

Data Structures

struct  pull_varnos_context
 
struct  pull_varattnos_context
 
struct  pull_vars_context
 
struct  locate_var_of_level_context
 
struct  pull_var_clause_context
 
struct  flatten_join_alias_vars_context
 

Functions

static bool pull_varnos_walker (Node *node, pull_varnos_context *context)
 
static bool pull_varattnos_walker (Node *node, pull_varattnos_context *context)
 
static bool pull_vars_walker (Node *node, pull_vars_context *context)
 
static bool contain_var_clause_walker (Node *node, void *context)
 
static bool contain_vars_of_level_walker (Node *node, int *sublevels_up)
 
static bool locate_var_of_level_walker (Node *node, locate_var_of_level_context *context)
 
static bool pull_var_clause_walker (Node *node, pull_var_clause_context *context)
 
static Nodeflatten_join_alias_vars_mutator (Node *node, flatten_join_alias_vars_context *context)
 
static Relids alias_relid_set (Query *query, Relids relids)
 
Relids pull_varnos (PlannerInfo *root, Node *node)
 
Relids pull_varnos_of_level (PlannerInfo *root, Node *node, int levelsup)
 
void pull_varattnos (Node *node, Index varno, Bitmapset **varattnos)
 
Listpull_vars_of_level (Node *node, int levelsup)
 
bool contain_var_clause (Node *node)
 
bool contain_vars_of_level (Node *node, int levelsup)
 
int locate_var_of_level (Node *node, int levelsup)
 
Listpull_var_clause (Node *node, int flags)
 
Nodeflatten_join_alias_vars (Query *query, Node *node)
 

Function Documentation

◆ alias_relid_set()

static Relids alias_relid_set ( Query query,
Relids  relids 
)
static

Definition at line 853 of file var.c.

References bms_add_member(), bms_join(), bms_next_member(), get_relids_for_join(), rt_fetch, Query::rtable, RTE_JOIN, and RangeTblEntry::rtekind.

Referenced by flatten_join_alias_vars_mutator().

854 {
855  Relids result = NULL;
856  int rtindex;
857 
858  rtindex = -1;
859  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
860  {
861  RangeTblEntry *rte = rt_fetch(rtindex, query->rtable);
862 
863  if (rte->rtekind == RTE_JOIN)
864  result = bms_join(result, get_relids_for_join(query, rtindex));
865  else
866  result = bms_add_member(result, rtindex);
867  }
868  return result;
869 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
List * rtable
Definition: parsenodes.h:147
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:949
Relids get_relids_for_join(Query *query, int joinrelid)
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
RTEKind rtekind
Definition: parsenodes.h:995

◆ contain_var_clause()

bool contain_var_clause ( Node node)

◆ contain_var_clause_walker()

static bool contain_var_clause_walker ( Node node,
void *  context 
)
static

Definition at line 364 of file var.c.

References expression_tree_walker(), and IsA.

Referenced by contain_var_clause().

365 {
366  if (node == NULL)
367  return false;
368  if (IsA(node, Var))
369  {
370  if (((Var *) node)->varlevelsup == 0)
371  return true; /* abort the tree traversal and return true */
372  return false;
373  }
374  if (IsA(node, CurrentOfExpr))
375  return true;
376  if (IsA(node, PlaceHolderVar))
377  {
378  if (((PlaceHolderVar *) node)->phlevelsup == 0)
379  return true; /* abort the tree traversal and return true */
380  /* else fall through to check the contained expr */
381  }
382  return expression_tree_walker(node, contain_var_clause_walker, context);
383 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Definition: primnodes.h:186
static bool contain_var_clause_walker(Node *node, void *context)
Definition: var.c:364
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904

◆ contain_vars_of_level()

bool contain_vars_of_level ( Node node,
int  levelsup 
)

Definition at line 396 of file var.c.

References contain_vars_of_level_walker(), and query_or_expression_tree_walker().

Referenced by apply_child_basequals(), checkExprIsVarFree(), convert_ANY_sublink_to_join(), convert_EXISTS_sublink_to_join(), convert_EXISTS_to_ANY(), pull_up_simple_values(), pullup_replace_vars_callback(), transformAExprIn(), transformInsertStmt(), transformRangeFunction(), transformRangeTableFunc(), transformSetOperationTree(), and transformValuesClause().

397 {
398  int sublevels_up = levelsup;
399 
402  (void *) &sublevels_up,
403  0);
404 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3461
static bool contain_vars_of_level_walker(Node *node, int *sublevels_up)
Definition: var.c:407

◆ contain_vars_of_level_walker()

static bool contain_vars_of_level_walker ( Node node,
int *  sublevels_up 
)
static

Definition at line 407 of file var.c.

References expression_tree_walker(), IsA, and query_tree_walker().

Referenced by contain_vars_of_level().

408 {
409  if (node == NULL)
410  return false;
411  if (IsA(node, Var))
412  {
413  if (((Var *) node)->varlevelsup == *sublevels_up)
414  return true; /* abort tree traversal and return true */
415  return false;
416  }
417  if (IsA(node, CurrentOfExpr))
418  {
419  if (*sublevels_up == 0)
420  return true;
421  return false;
422  }
423  if (IsA(node, PlaceHolderVar))
424  {
425  if (((PlaceHolderVar *) node)->phlevelsup == *sublevels_up)
426  return true; /* abort the tree traversal and return true */
427  /* else fall through to check the contained expr */
428  }
429  if (IsA(node, Query))
430  {
431  /* Recurse into subselects */
432  bool result;
433 
434  (*sublevels_up)++;
435  result = query_tree_walker((Query *) node,
437  (void *) sublevels_up,
438  0);
439  (*sublevels_up)--;
440  return result;
441  }
442  return expression_tree_walker(node,
444  (void *) sublevels_up);
445 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Definition: primnodes.h:186
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
static bool contain_vars_of_level_walker(Node *node, int *sublevels_up)
Definition: var.c:407

◆ flatten_join_alias_vars()

Node* flatten_join_alias_vars ( Query query,
Node node 
)

Definition at line 697 of file var.c.

References flatten_join_alias_vars_mutator(), Query::hasSubLinks, flatten_join_alias_vars_context::inserted_sublink, flatten_join_alias_vars_context::possible_sublink, flatten_join_alias_vars_context::query, and flatten_join_alias_vars_context::sublevels_up.

Referenced by finalize_grouping_exprs_walker(), parseCheckAggregates(), preprocess_expression(), pull_up_simple_subquery(), and subquery_planner().

698 {
700 
701  context.query = query;
702  context.sublevels_up = 0;
703  /* flag whether join aliases could possibly contain SubLinks */
704  context.possible_sublink = query->hasSubLinks;
705  /* if hasSubLinks is already true, no need to work hard */
706  context.inserted_sublink = query->hasSubLinks;
707 
708  return flatten_join_alias_vars_mutator(node, &context);
709 }
bool hasSubLinks
Definition: parsenodes.h:136
static Node * flatten_join_alias_vars_mutator(Node *node, flatten_join_alias_vars_context *context)
Definition: var.c:712

◆ flatten_join_alias_vars_mutator()

static Node * flatten_join_alias_vars_mutator ( Node node,
flatten_join_alias_vars_context context 
)
static

Definition at line 712 of file var.c.

References alias_relid_set(), RowExpr::args, Assert, attnum, checkExprHasSubLink(), COERCE_IMPLICIT_CAST, Alias::colnames, RowExpr::colnames, copyObject, RangeTblEntry::eref, expression_tree_mutator(), forboth, Query::hasSubLinks, IncrementVarSublevelsUp(), flatten_join_alias_vars_context::inserted_sublink, InvalidAttrNumber, IsA, RangeTblEntry::joinaliasvars, lappend(), lfirst, list_length(), list_nth(), Var::location, RowExpr::location, makeNode, NIL, PlaceHolderVar::phlevelsup, PlaceHolderVar::phrels, flatten_join_alias_vars_context::possible_sublink, QTW_IGNORE_JOINALIASES, flatten_join_alias_vars_context::query, query_tree_mutator(), RowExpr::row_format, RowExpr::row_typeid, rt_fetch, Query::rtable, RTE_JOIN, RangeTblEntry::rtekind, flatten_join_alias_vars_context::sublevels_up, Var::varattno, Var::varlevelsup, Var::varno, and Var::vartype.

Referenced by flatten_join_alias_vars().

714 {
715  if (node == NULL)
716  return NULL;
717  if (IsA(node, Var))
718  {
719  Var *var = (Var *) node;
720  RangeTblEntry *rte;
721  Node *newvar;
722 
723  /* No change unless Var belongs to a JOIN of the target level */
724  if (var->varlevelsup != context->sublevels_up)
725  return node; /* no need to copy, really */
726  rte = rt_fetch(var->varno, context->query->rtable);
727  if (rte->rtekind != RTE_JOIN)
728  return node;
729  if (var->varattno == InvalidAttrNumber)
730  {
731  /* Must expand whole-row reference */
732  RowExpr *rowexpr;
733  List *fields = NIL;
734  List *colnames = NIL;
736  ListCell *lv;
737  ListCell *ln;
738 
739  attnum = 0;
741  forboth(lv, rte->joinaliasvars, ln, rte->eref->colnames)
742  {
743  newvar = (Node *) lfirst(lv);
744  attnum++;
745  /* Ignore dropped columns */
746  if (newvar == NULL)
747  continue;
748  newvar = copyObject(newvar);
749 
750  /*
751  * If we are expanding an alias carried down from an upper
752  * query, must adjust its varlevelsup fields.
753  */
754  if (context->sublevels_up != 0)
755  IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
756  /* Preserve original Var's location, if possible */
757  if (IsA(newvar, Var))
758  ((Var *) newvar)->location = var->location;
759  /* Recurse in case join input is itself a join */
760  /* (also takes care of setting inserted_sublink if needed) */
761  newvar = flatten_join_alias_vars_mutator(newvar, context);
762  fields = lappend(fields, newvar);
763  /* We need the names of non-dropped columns, too */
764  colnames = lappend(colnames, copyObject((Node *) lfirst(ln)));
765  }
766  rowexpr = makeNode(RowExpr);
767  rowexpr->args = fields;
768  rowexpr->row_typeid = var->vartype;
769  rowexpr->row_format = COERCE_IMPLICIT_CAST;
770  rowexpr->colnames = colnames;
771  rowexpr->location = var->location;
772 
773  return (Node *) rowexpr;
774  }
775 
776  /* Expand join alias reference */
777  Assert(var->varattno > 0);
778  newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
779  Assert(newvar != NULL);
780  newvar = copyObject(newvar);
781 
782  /*
783  * If we are expanding an alias carried down from an upper query, must
784  * adjust its varlevelsup fields.
785  */
786  if (context->sublevels_up != 0)
787  IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
788 
789  /* Preserve original Var's location, if possible */
790  if (IsA(newvar, Var))
791  ((Var *) newvar)->location = var->location;
792 
793  /* Recurse in case join input is itself a join */
794  newvar = flatten_join_alias_vars_mutator(newvar, context);
795 
796  /* Detect if we are adding a sublink to query */
797  if (context->possible_sublink && !context->inserted_sublink)
798  context->inserted_sublink = checkExprHasSubLink(newvar);
799 
800  return newvar;
801  }
802  if (IsA(node, PlaceHolderVar))
803  {
804  /* Copy the PlaceHolderVar node with correct mutation of subnodes */
805  PlaceHolderVar *phv;
806 
809  (void *) context);
810  /* now fix PlaceHolderVar's relid sets */
811  if (phv->phlevelsup == context->sublevels_up)
812  {
813  phv->phrels = alias_relid_set(context->query,
814  phv->phrels);
815  }
816  return (Node *) phv;
817  }
818 
819  if (IsA(node, Query))
820  {
821  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
822  Query *newnode;
823  bool save_inserted_sublink;
824 
825  context->sublevels_up++;
826  save_inserted_sublink = context->inserted_sublink;
827  context->inserted_sublink = ((Query *) node)->hasSubLinks;
828  newnode = query_tree_mutator((Query *) node,
830  (void *) context,
832  newnode->hasSubLinks |= context->inserted_sublink;
833  context->inserted_sublink = save_inserted_sublink;
834  context->sublevels_up--;
835  return (Node *) newnode;
836  }
837  /* Already-planned tree not supported */
838  Assert(!IsA(node, SubPlan));
839  /* Shouldn't need to handle these planner auxiliary nodes here */
840  Assert(!IsA(node, SpecialJoinInfo));
841  Assert(!IsA(node, PlaceHolderInfo));
842  Assert(!IsA(node, MinMaxAggInfo));
843 
845  (void *) context);
846 }
#define NIL
Definition: pg_list.h:65
List * args
Definition: primnodes.h:1058
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
List * joinaliasvars
Definition: parsenodes.h:1070
Index varlevelsup
Definition: primnodes.h:196
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2598
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:776
List * colnames
Definition: primnodes.h:43
bool checkExprHasSubLink(Node *node)
Definition: rewriteManip.c:277
static Relids alias_relid_set(Query *query, Relids relids)
Definition: var.c:853
Definition: nodes.h:539
AttrNumber varattno
Definition: primnodes.h:191
Definition: primnodes.h:186
List * rtable
Definition: parsenodes.h:147
List * colnames
Definition: primnodes.h:1074
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
Oid vartype
Definition: primnodes.h:193
int location
Definition: primnodes.h:201
int location
Definition: primnodes.h:1075
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
List * lappend(List *list, void *datum)
Definition: list.c:336
Index varno
Definition: primnodes.h:189
int16 attnum
Definition: pg_attribute.h:83
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Oid row_typeid
Definition: primnodes.h:1059
static int list_length(const List *l)
Definition: pg_list.h:149
#define InvalidAttrNumber
Definition: attnum.h:23
RTEKind rtekind
Definition: parsenodes.h:995
Index phlevelsup
Definition: pathnodes.h:2173
bool hasSubLinks
Definition: parsenodes.h:136
static Node * flatten_join_alias_vars_mutator(Node *node, flatten_join_alias_vars_context *context)
Definition: var.c:712
Alias * eref
Definition: parsenodes.h:1141
#define copyObject(obj)
Definition: nodes.h:655
CoercionForm row_format
Definition: primnodes.h:1073
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
Query * query_tree_mutator(Query *query, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3300
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23

◆ locate_var_of_level()

int locate_var_of_level ( Node node,
int  levelsup 
)

Definition at line 464 of file var.c.

References locate_var_of_level_walker(), query_or_expression_tree_walker(), locate_var_of_level_context::sublevels_up, and locate_var_of_level_context::var_location.

Referenced by check_agg_arguments(), checkExprIsVarFree(), and transformSetOperationTree().

465 {
467 
468  context.var_location = -1; /* in case we find nothing */
469  context.sublevels_up = levelsup;
470 
473  (void *) &context,
474  0);
475 
476  return context.var_location;
477 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3461
static bool locate_var_of_level_walker(Node *node, locate_var_of_level_context *context)
Definition: var.c:480

◆ locate_var_of_level_walker()

static bool locate_var_of_level_walker ( Node node,
locate_var_of_level_context context 
)
static

Definition at line 480 of file var.c.

References expression_tree_walker(), IsA, Var::location, query_tree_walker(), locate_var_of_level_context::sublevels_up, locate_var_of_level_context::var_location, and Var::varlevelsup.

Referenced by locate_var_of_level().

482 {
483  if (node == NULL)
484  return false;
485  if (IsA(node, Var))
486  {
487  Var *var = (Var *) node;
488 
489  if (var->varlevelsup == context->sublevels_up &&
490  var->location >= 0)
491  {
492  context->var_location = var->location;
493  return true; /* abort tree traversal and return true */
494  }
495  return false;
496  }
497  if (IsA(node, CurrentOfExpr))
498  {
499  /* since CurrentOfExpr doesn't carry location, nothing we can do */
500  return false;
501  }
502  /* No extra code needed for PlaceHolderVar; just look in contained expr */
503  if (IsA(node, Query))
504  {
505  /* Recurse into subselects */
506  bool result;
507 
508  context->sublevels_up++;
509  result = query_tree_walker((Query *) node,
511  (void *) context,
512  0);
513  context->sublevels_up--;
514  return result;
515  }
516  return expression_tree_walker(node,
518  (void *) context);
519 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Index varlevelsup
Definition: primnodes.h:196
Definition: primnodes.h:186
int location
Definition: primnodes.h:201
static bool locate_var_of_level_walker(Node *node, locate_var_of_level_context *context)
Definition: var.c:480
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904

◆ pull_var_clause()

List* pull_var_clause ( Node node,
int  flags 
)

Definition at line 562 of file var.c.

References Assert, pull_var_clause_context::flags, NIL, pull_var_clause_walker(), PVC_INCLUDE_AGGREGATES, PVC_INCLUDE_PLACEHOLDERS, PVC_INCLUDE_WINDOWFUNCS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS, PVC_RECURSE_WINDOWFUNCS, and pull_var_clause_context::varlist.

Referenced by AddRelationNewConstraints(), build_base_rel_tlists(), build_remote_returning(), build_tlist_to_deparse(), CreateTrigger(), dependency_is_compatible_expression(), distribute_qual_to_rels(), estimate_num_groups(), find_computable_ec_member(), find_placeholders_in_expr(), fix_placeholder_input_needed_levels(), foreign_grouping_ok(), generate_base_implied_equalities_no_const(), make_group_input_target(), make_partial_grouping_target(), make_sort_input_target(), make_window_input_target(), preprocess_targetlist(), process_implied_equality(), qual_is_pushdown_safe(), and StoreRelCheck().

563 {
564  pull_var_clause_context context;
565 
566  /* Assert that caller has not specified inconsistent flags */
573 
574  context.varlist = NIL;
575  context.flags = flags;
576 
577  pull_var_clause_walker(node, &context);
578  return context.varlist;
579 }
#define NIL
Definition: pg_list.h:65
#define PVC_RECURSE_PLACEHOLDERS
Definition: optimizer.h:191
#define PVC_INCLUDE_AGGREGATES
Definition: optimizer.h:185
#define PVC_INCLUDE_WINDOWFUNCS
Definition: optimizer.h:187
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:189
#define Assert(condition)
Definition: c.h:804
static bool pull_var_clause_walker(Node *node, pull_var_clause_context *context)
Definition: var.c:582
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:188
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:186

◆ pull_var_clause_walker()

static bool pull_var_clause_walker ( Node node,
pull_var_clause_context context 
)
static

Definition at line 582 of file var.c.

References elog, ERROR, expression_tree_walker(), pull_var_clause_context::flags, IsA, lappend(), PVC_INCLUDE_AGGREGATES, PVC_INCLUDE_PLACEHOLDERS, PVC_INCLUDE_WINDOWFUNCS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS, PVC_RECURSE_WINDOWFUNCS, and pull_var_clause_context::varlist.

Referenced by pull_var_clause().

583 {
584  if (node == NULL)
585  return false;
586  if (IsA(node, Var))
587  {
588  if (((Var *) node)->varlevelsup != 0)
589  elog(ERROR, "Upper-level Var found where not expected");
590  context->varlist = lappend(context->varlist, node);
591  return false;
592  }
593  else if (IsA(node, Aggref))
594  {
595  if (((Aggref *) node)->agglevelsup != 0)
596  elog(ERROR, "Upper-level Aggref found where not expected");
597  if (context->flags & PVC_INCLUDE_AGGREGATES)
598  {
599  context->varlist = lappend(context->varlist, node);
600  /* we do NOT descend into the contained expression */
601  return false;
602  }
603  else if (context->flags & PVC_RECURSE_AGGREGATES)
604  {
605  /* fall through to recurse into the aggregate's arguments */
606  }
607  else
608  elog(ERROR, "Aggref found where not expected");
609  }
610  else if (IsA(node, GroupingFunc))
611  {
612  if (((GroupingFunc *) node)->agglevelsup != 0)
613  elog(ERROR, "Upper-level GROUPING found where not expected");
614  if (context->flags & PVC_INCLUDE_AGGREGATES)
615  {
616  context->varlist = lappend(context->varlist, node);
617  /* we do NOT descend into the contained expression */
618  return false;
619  }
620  else if (context->flags & PVC_RECURSE_AGGREGATES)
621  {
622  /*
623  * We do NOT descend into the contained expression, even if the
624  * caller asked for it, because we never actually evaluate it -
625  * the result is driven entirely off the associated GROUP BY
626  * clause, so we never need to extract the actual Vars here.
627  */
628  return false;
629  }
630  else
631  elog(ERROR, "GROUPING found where not expected");
632  }
633  else if (IsA(node, WindowFunc))
634  {
635  /* WindowFuncs have no levelsup field to check ... */
636  if (context->flags & PVC_INCLUDE_WINDOWFUNCS)
637  {
638  context->varlist = lappend(context->varlist, node);
639  /* we do NOT descend into the contained expressions */
640  return false;
641  }
642  else if (context->flags & PVC_RECURSE_WINDOWFUNCS)
643  {
644  /* fall through to recurse into the windowfunc's arguments */
645  }
646  else
647  elog(ERROR, "WindowFunc found where not expected");
648  }
649  else if (IsA(node, PlaceHolderVar))
650  {
651  if (((PlaceHolderVar *) node)->phlevelsup != 0)
652  elog(ERROR, "Upper-level PlaceHolderVar found where not expected");
653  if (context->flags & PVC_INCLUDE_PLACEHOLDERS)
654  {
655  context->varlist = lappend(context->varlist, node);
656  /* we do NOT descend into the contained expression */
657  return false;
658  }
659  else if (context->flags & PVC_RECURSE_PLACEHOLDERS)
660  {
661  /* fall through to recurse into the placeholder's expression */
662  }
663  else
664  elog(ERROR, "PlaceHolderVar found where not expected");
665  }
667  (void *) context);
668 }
#define PVC_RECURSE_PLACEHOLDERS
Definition: optimizer.h:191
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Definition: primnodes.h:186
#define PVC_INCLUDE_AGGREGATES
Definition: optimizer.h:185
#define PVC_INCLUDE_WINDOWFUNCS
Definition: optimizer.h:187
#define ERROR
Definition: elog.h:46
List * lappend(List *list, void *datum)
Definition: list.c:336
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:189
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
static bool pull_var_clause_walker(Node *node, pull_var_clause_context *context)
Definition: var.c:582
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:188
#define elog(elevel,...)
Definition: elog.h:232
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:186

◆ pull_varattnos()

void pull_varattnos ( Node node,
Index  varno,
Bitmapset **  varattnos 
)

Definition at line 246 of file var.c.

References pull_varattnos_walker(), pull_varattnos_context::varattnos, and pull_varattnos_context::varno.

Referenced by check_index_only(), check_selective_binary_conversion(), ComputePartitionAttrs(), create_foreignscan_plan(), DefineIndex(), deparseReturningList(), fill_extraUpdatedCols(), has_partition_attrs(), postgresGetForeignRelSize(), RelationGetIndexAttrBitmap(), remove_unused_subquery_outputs(), and statext_is_compatible_clause().

247 {
248  pull_varattnos_context context;
249 
250  context.varattnos = *varattnos;
251  context.varno = varno;
252 
253  (void) pull_varattnos_walker(node, &context);
254 
255  *varattnos = context.varattnos;
256 }
Bitmapset * varattnos
Definition: var.c:41
static bool pull_varattnos_walker(Node *node, pull_varattnos_context *context)
Definition: var.c:259

◆ pull_varattnos_walker()

static bool pull_varattnos_walker ( Node node,
pull_varattnos_context context 
)
static

Definition at line 259 of file var.c.

References Assert, bms_add_member(), expression_tree_walker(), FirstLowInvalidHeapAttributeNumber, IsA, Var::varattno, pull_varattnos_context::varattnos, Var::varlevelsup, pull_varattnos_context::varno, and Var::varno.

Referenced by pull_varattnos().

260 {
261  if (node == NULL)
262  return false;
263  if (IsA(node, Var))
264  {
265  Var *var = (Var *) node;
266 
267  if (var->varno == context->varno && var->varlevelsup == 0)
268  context->varattnos =
269  bms_add_member(context->varattnos,
271  return false;
272  }
273 
274  /* Should not find an unplanned subquery */
275  Assert(!IsA(node, Query));
276 
278  (void *) context);
279 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Index varlevelsup
Definition: primnodes.h:196
Bitmapset * varattnos
Definition: var.c:41
AttrNumber varattno
Definition: primnodes.h:191
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
Definition: primnodes.h:186
static bool pull_varattnos_walker(Node *node, pull_varattnos_context *context)
Definition: var.c:259
Index varno
Definition: primnodes.h:189
#define Assert(condition)
Definition: c.h:804
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736

◆ pull_varnos()

Relids pull_varnos ( PlannerInfo root,
Node node 
)

Definition at line 97 of file var.c.

References pull_varnos_walker(), query_or_expression_tree_walker(), pull_varnos_context::root, pull_varnos_context::sublevels_up, and pull_varnos_context::varnos.

Referenced by compute_semijoin_info(), convert_ANY_sublink_to_join(), convert_EXISTS_sublink_to_join(), cost_incremental_sort(), distribute_qual_to_rels(), examine_variable(), expand_indexqual_rowcompare(), find_placeholder_info(), get_eclass_for_sort_expr(), is_pseudo_constant_for_index(), IsTidEqualAnyClause(), join_is_removable(), make_outerjoininfo(), make_restrictinfo_internal(), match_rowcompare_to_indexcol(), match_saopclause_to_indexcol(), NumRelids(), process_implied_equality(), pullup_replace_vars_callback(), and reduce_outer_joins_pass2().

98 {
99  pull_varnos_context context;
100 
101  context.varnos = NULL;
102  context.root = root;
103  context.sublevels_up = 0;
104 
105  /*
106  * Must be prepared to start with a Query or a bare expression tree; if
107  * it's a Query, we don't want to increment sublevels_up.
108  */
111  (void *) &context,
112  0);
113 
114  return context.varnos;
115 }
static bool pull_varnos_walker(Node *node, pull_varnos_context *context)
Definition: var.c:144
int sublevels_up
Definition: var.c:36
Relids varnos
Definition: var.c:34
PlannerInfo * root
Definition: var.c:35
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3461

◆ pull_varnos_of_level()

Relids pull_varnos_of_level ( PlannerInfo root,
Node node,
int  levelsup 
)

Definition at line 123 of file var.c.

References pull_varnos_walker(), query_or_expression_tree_walker(), pull_varnos_context::root, pull_varnos_context::sublevels_up, and pull_varnos_context::varnos.

Referenced by is_simple_subquery(), and jointree_contains_lateral_outer_refs().

124 {
125  pull_varnos_context context;
126 
127  context.varnos = NULL;
128  context.root = root;
129  context.sublevels_up = levelsup;
130 
131  /*
132  * Must be prepared to start with a Query or a bare expression tree; if
133  * it's a Query, we don't want to increment sublevels_up.
134  */
137  (void *) &context,
138  0);
139 
140  return context.varnos;
141 }
static bool pull_varnos_walker(Node *node, pull_varnos_context *context)
Definition: var.c:144
int sublevels_up
Definition: var.c:36
Relids varnos
Definition: var.c:34
PlannerInfo * root
Definition: var.c:35
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3461

◆ pull_varnos_walker()

static bool pull_varnos_walker ( Node node,
pull_varnos_context context 
)
static

Definition at line 144 of file var.c.

References bms_add_member(), bms_add_members(), CurrentOfExpr::cvarno, expression_tree_walker(), IsA, lfirst, PlaceHolderInfo::ph_eval_at, PlaceHolderVar::phid, PlaceHolderInfo::phid, PlaceHolderVar::phlevelsup, PlaceHolderVar::phrels, PlannerInfo::placeholder_list, query_tree_walker(), pull_varnos_context::root, pull_varnos_context::sublevels_up, Var::varlevelsup, Var::varno, and pull_varnos_context::varnos.

Referenced by pull_varnos(), and pull_varnos_of_level().

145 {
146  if (node == NULL)
147  return false;
148  if (IsA(node, Var))
149  {
150  Var *var = (Var *) node;
151 
152  if (var->varlevelsup == context->sublevels_up)
153  context->varnos = bms_add_member(context->varnos, var->varno);
154  return false;
155  }
156  if (IsA(node, CurrentOfExpr))
157  {
158  CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
159 
160  if (context->sublevels_up == 0)
161  context->varnos = bms_add_member(context->varnos, cexpr->cvarno);
162  return false;
163  }
164  if (IsA(node, PlaceHolderVar))
165  {
166  PlaceHolderVar *phv = (PlaceHolderVar *) node;
167 
168  /*
169  * If a PlaceHolderVar is not of the target query level, ignore it,
170  * instead recursing into its expression to see if it contains any
171  * vars that are of the target level.
172  */
173  if (phv->phlevelsup == context->sublevels_up)
174  {
175  /*
176  * Ideally, the PHV's contribution to context->varnos is its
177  * ph_eval_at set. However, this code can be invoked before
178  * that's been computed. If we cannot find a PlaceHolderInfo,
179  * fall back to the conservative assumption that the PHV will be
180  * evaluated at its syntactic level (phv->phrels).
181  *
182  * There is a second hazard: this code is also used to examine
183  * qual clauses during deconstruct_jointree, when we may have a
184  * PlaceHolderInfo but its ph_eval_at value is not yet final, so
185  * that theoretically we could obtain a relid set that's smaller
186  * than we'd see later on. That should never happen though,
187  * because we deconstruct the jointree working upwards. Any outer
188  * join that forces delay of evaluation of a given qual clause
189  * will be processed before we examine that clause here, so the
190  * ph_eval_at value should have been updated to include it.
191  */
192  PlaceHolderInfo *phinfo = NULL;
193 
194  if (phv->phlevelsup == 0)
195  {
196  ListCell *lc;
197 
198  foreach(lc, context->root->placeholder_list)
199  {
200  phinfo = (PlaceHolderInfo *) lfirst(lc);
201  if (phinfo->phid == phv->phid)
202  break;
203  phinfo = NULL;
204  }
205  }
206  if (phinfo != NULL)
207  context->varnos = bms_add_members(context->varnos,
208  phinfo->ph_eval_at);
209  else
210  context->varnos = bms_add_members(context->varnos,
211  phv->phrels);
212  return false; /* don't recurse into expression */
213  }
214  }
215  else if (IsA(node, Query))
216  {
217  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
218  bool result;
219 
220  context->sublevels_up++;
221  result = query_tree_walker((Query *) node, pull_varnos_walker,
222  (void *) context, 0);
223  context->sublevels_up--;
224  return result;
225  }
227  (void *) context);
228 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Index varlevelsup
Definition: primnodes.h:196
Relids ph_eval_at
Definition: pathnodes.h:2402
static bool pull_varnos_walker(Node *node, pull_varnos_context *context)
Definition: var.c:144
int sublevels_up
Definition: var.c:36
Definition: primnodes.h:186
Relids varnos
Definition: var.c:34
PlannerInfo * root
Definition: var.c:35
Index varno
Definition: primnodes.h:189
#define lfirst(lc)
Definition: pg_list.h:169
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
Index phlevelsup
Definition: pathnodes.h:2173
List * placeholder_list
Definition: pathnodes.h:289
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793

◆ pull_vars_of_level()

List* pull_vars_of_level ( Node node,
int  levelsup 
)

Definition at line 290 of file var.c.

References NIL, pull_vars_walker(), query_or_expression_tree_walker(), pull_vars_context::sublevels_up, and pull_vars_context::vars.

Referenced by extract_lateral_references().

291 {
292  pull_vars_context context;
293 
294  context.vars = NIL;
295  context.sublevels_up = levelsup;
296 
297  /*
298  * Must be prepared to start with a Query or a bare expression tree; if
299  * it's a Query, we don't want to increment sublevels_up.
300  */
303  (void *) &context,
304  0);
305 
306  return context.vars;
307 }
int sublevels_up
Definition: var.c:48
#define NIL
Definition: pg_list.h:65
static bool pull_vars_walker(Node *node, pull_vars_context *context)
Definition: var.c:310
List * vars
Definition: var.c:47
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3461

◆ pull_vars_walker()

static bool pull_vars_walker ( Node node,
pull_vars_context context 
)
static

Definition at line 310 of file var.c.

References expression_tree_walker(), IsA, lappend(), PlaceHolderVar::phlevelsup, query_tree_walker(), pull_vars_context::sublevels_up, Var::varlevelsup, and pull_vars_context::vars.

Referenced by pull_vars_of_level().

311 {
312  if (node == NULL)
313  return false;
314  if (IsA(node, Var))
315  {
316  Var *var = (Var *) node;
317 
318  if (var->varlevelsup == context->sublevels_up)
319  context->vars = lappend(context->vars, var);
320  return false;
321  }
322  if (IsA(node, PlaceHolderVar))
323  {
324  PlaceHolderVar *phv = (PlaceHolderVar *) node;
325 
326  if (phv->phlevelsup == context->sublevels_up)
327  context->vars = lappend(context->vars, phv);
328  /* we don't want to look into the contained expression */
329  return false;
330  }
331  if (IsA(node, Query))
332  {
333  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
334  bool result;
335 
336  context->sublevels_up++;
337  result = query_tree_walker((Query *) node, pull_vars_walker,
338  (void *) context, 0);
339  context->sublevels_up--;
340  return result;
341  }
343  (void *) context);
344 }
int sublevels_up
Definition: var.c:48
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Index varlevelsup
Definition: primnodes.h:196
static bool pull_vars_walker(Node *node, pull_vars_context *context)
Definition: var.c:310
Definition: primnodes.h:186
List * vars
Definition: var.c:47
List * lappend(List *list, void *datum)
Definition: list.c:336
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
Index phlevelsup
Definition: pathnodes.h:2173