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 887 of file var.c.

888 {
889  Relids result = NULL;
890  int rtindex;
891 
892  rtindex = -1;
893  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
894  {
895  RangeTblEntry *rte = rt_fetch(rtindex, query->rtable);
896 
897  if (rte->rtekind == RTE_JOIN)
898  result = bms_join(result, get_relids_for_join(query, rtindex));
899  else
900  result = bms_add_member(result, rtindex);
901  }
902  return result;
903 }
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:951
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1045
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:738
@ RTE_JOIN
Definition: parsenodes.h:1000
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Relids get_relids_for_join(Query *query, int joinrelid)
List * rtable
Definition: parsenodes.h:148
RTEKind rtekind
Definition: parsenodes.h:1015

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

◆ 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 406 of file var.c.

407 {
408  if (node == NULL)
409  return false;
410  if (IsA(node, Var))
411  {
412  if (((Var *) node)->varlevelsup == 0)
413  return true; /* abort the tree traversal and return true */
414  return false;
415  }
416  if (IsA(node, CurrentOfExpr))
417  return true;
418  if (IsA(node, PlaceHolderVar))
419  {
420  if (((PlaceHolderVar *) node)->phlevelsup == 0)
421  return true; /* abort the tree traversal and return true */
422  /* else fall through to check the contained expr */
423  }
424  return expression_tree_walker(node, contain_var_clause_walker, context);
425 }
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:2015
#define IsA(nodeptr, _type_)
Definition: nodes.h:624
Definition: primnodes.h:196

References expression_tree_walker(), and IsA.

Referenced by contain_var_clause().

◆ contain_vars_of_level()

bool contain_vars_of_level ( Node node,
int  levelsup 
)

Definition at line 438 of file var.c.

439 {
440  int sublevels_up = levelsup;
441 
444  (void *) &sublevels_up,
445  0);
446 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3810
static bool contain_vars_of_level_walker(Node *node, int *sublevels_up)
Definition: var.c:449

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(), transformJsonTable(), transformRangeFunction(), transformRangeTableFunc(), transformSetOperationTree(), and transformValuesClause().

◆ contain_vars_of_level_walker()

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

Definition at line 449 of file var.c.

450 {
451  if (node == NULL)
452  return false;
453  if (IsA(node, Var))
454  {
455  if (((Var *) node)->varlevelsup == *sublevels_up)
456  return true; /* abort tree traversal and return true */
457  return false;
458  }
459  if (IsA(node, CurrentOfExpr))
460  {
461  if (*sublevels_up == 0)
462  return true;
463  return false;
464  }
465  if (IsA(node, PlaceHolderVar))
466  {
467  if (((PlaceHolderVar *) node)->phlevelsup == *sublevels_up)
468  return true; /* abort the tree traversal and return true */
469  /* else fall through to check the contained expr */
470  }
471  if (IsA(node, Query))
472  {
473  /* Recurse into subselects */
474  bool result;
475 
476  (*sublevels_up)++;
477  result = query_tree_walker((Query *) node,
479  (void *) sublevels_up,
480  0);
481  (*sublevels_up)--;
482  return result;
483  }
484  return expression_tree_walker(node,
486  (void *) sublevels_up);
487 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2570

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

Referenced by contain_vars_of_level().

◆ flatten_join_alias_vars()

Node* flatten_join_alias_vars ( Query query,
Node node 
)

Definition at line 733 of file var.c.

734 {
736 
737  context.query = query;
738  context.sublevels_up = 0;
739  /* flag whether join aliases could possibly contain SubLinks */
740  context.possible_sublink = query->hasSubLinks;
741  /* if hasSubLinks is already true, no need to work hard */
742  context.inserted_sublink = query->hasSubLinks;
743 
744  return flatten_join_alias_vars_mutator(node, &context);
745 }
bool hasSubLinks
Definition: parsenodes.h:137
static Node * flatten_join_alias_vars_mutator(Node *node, flatten_join_alias_vars_context *context)
Definition: var.c:748

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

◆ flatten_join_alias_vars_mutator()

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

Definition at line 748 of file var.c.

750 {
751  if (node == NULL)
752  return NULL;
753  if (IsA(node, Var))
754  {
755  Var *var = (Var *) node;
756  RangeTblEntry *rte;
757  Node *newvar;
758 
759  /* No change unless Var belongs to a JOIN of the target level */
760  if (var->varlevelsup != context->sublevels_up)
761  return node; /* no need to copy, really */
762  rte = rt_fetch(var->varno, context->query->rtable);
763  if (rte->rtekind != RTE_JOIN)
764  return node;
765  if (var->varattno == InvalidAttrNumber)
766  {
767  /* Must expand whole-row reference */
768  RowExpr *rowexpr;
769  List *fields = NIL;
770  List *colnames = NIL;
771  ListCell *lv;
772  ListCell *ln;
773 
775  forboth(lv, rte->joinaliasvars, ln, rte->eref->colnames)
776  {
777  newvar = (Node *) lfirst(lv);
778  /* Ignore dropped columns */
779  if (newvar == NULL)
780  continue;
781  newvar = copyObject(newvar);
782 
783  /*
784  * If we are expanding an alias carried down from an upper
785  * query, must adjust its varlevelsup fields.
786  */
787  if (context->sublevels_up != 0)
788  IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
789  /* Preserve original Var's location, if possible */
790  if (IsA(newvar, Var))
791  ((Var *) newvar)->location = var->location;
792  /* Recurse in case join input is itself a join */
793  /* (also takes care of setting inserted_sublink if needed) */
794  newvar = flatten_join_alias_vars_mutator(newvar, context);
795  fields = lappend(fields, newvar);
796  /* We need the names of non-dropped columns, too */
797  colnames = lappend(colnames, copyObject((Node *) lfirst(ln)));
798  }
799  rowexpr = makeNode(RowExpr);
800  rowexpr->args = fields;
801  rowexpr->row_typeid = var->vartype;
802  rowexpr->row_format = COERCE_IMPLICIT_CAST;
803  /* vartype will always be RECORDOID, so we always need colnames */
804  rowexpr->colnames = colnames;
805  rowexpr->location = var->location;
806 
807  return (Node *) rowexpr;
808  }
809 
810  /* Expand join alias reference */
811  Assert(var->varattno > 0);
812  newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
813  Assert(newvar != NULL);
814  newvar = copyObject(newvar);
815 
816  /*
817  * If we are expanding an alias carried down from an upper query, must
818  * adjust its varlevelsup fields.
819  */
820  if (context->sublevels_up != 0)
821  IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
822 
823  /* Preserve original Var's location, if possible */
824  if (IsA(newvar, Var))
825  ((Var *) newvar)->location = var->location;
826 
827  /* Recurse in case join input is itself a join */
828  newvar = flatten_join_alias_vars_mutator(newvar, context);
829 
830  /* Detect if we are adding a sublink to query */
831  if (context->possible_sublink && !context->inserted_sublink)
832  context->inserted_sublink = checkExprHasSubLink(newvar);
833 
834  return newvar;
835  }
836  if (IsA(node, PlaceHolderVar))
837  {
838  /* Copy the PlaceHolderVar node with correct mutation of subnodes */
839  PlaceHolderVar *phv;
840 
843  (void *) context);
844  /* now fix PlaceHolderVar's relid sets */
845  if (phv->phlevelsup == context->sublevels_up)
846  {
847  phv->phrels = alias_relid_set(context->query,
848  phv->phrels);
849  }
850  return (Node *) phv;
851  }
852 
853  if (IsA(node, Query))
854  {
855  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
856  Query *newnode;
857  bool save_inserted_sublink;
858 
859  context->sublevels_up++;
860  save_inserted_sublink = context->inserted_sublink;
861  context->inserted_sublink = ((Query *) node)->hasSubLinks;
862  newnode = query_tree_mutator((Query *) node,
864  (void *) context,
866  newnode->hasSubLinks |= context->inserted_sublink;
867  context->inserted_sublink = save_inserted_sublink;
868  context->sublevels_up--;
869  return (Node *) newnode;
870  }
871  /* Already-planned tree not supported */
872  Assert(!IsA(node, SubPlan));
873  /* Shouldn't need to handle these planner auxiliary nodes here */
874  Assert(!IsA(node, SpecialJoinInfo));
875  Assert(!IsA(node, PlaceHolderInfo));
876  Assert(!IsA(node, MinMaxAggInfo));
877 
879  (void *) context);
880 }
#define InvalidAttrNumber
Definition: attnum.h:23
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:336
Query * query_tree_mutator(Query *query, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3648
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2815
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23
#define copyObject(obj)
Definition: nodes.h:689
#define makeNode(_type_)
Definition: nodes.h:621
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
#define NIL
Definition: pg_list.h:65
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:494
bool checkExprHasSubLink(Node *node)
Definition: rewriteManip.c:277
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:776
List * colnames
Definition: primnodes.h:43
Definition: pg_list.h:51
Definition: nodes.h:574
Index phlevelsup
Definition: pathnodes.h:2204
Alias * eref
Definition: parsenodes.h:1161
List * joinaliasvars
Definition: parsenodes.h:1090
Oid row_typeid
Definition: primnodes.h:1076
int location
Definition: primnodes.h:1092
List * args
Definition: primnodes.h:1075
CoercionForm row_format
Definition: primnodes.h:1090
List * colnames
Definition: primnodes.h:1091
Oid vartype
Definition: primnodes.h:202
AttrNumber varattno
Definition: primnodes.h:200
int varno
Definition: primnodes.h:198
Index varlevelsup
Definition: primnodes.h:205
int location
Definition: primnodes.h:210
static Relids alias_relid_set(Query *query, Relids relids)
Definition: var.c:887

References alias_relid_set(), RowExpr::args, Assert(), 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().

◆ locate_var_of_level()

int locate_var_of_level ( Node node,
int  levelsup 
)

Definition at line 506 of file var.c.

507 {
509 
510  context.var_location = -1; /* in case we find nothing */
511  context.sublevels_up = levelsup;
512 
515  (void *) &context,
516  0);
517 
518  return context.var_location;
519 }
static bool locate_var_of_level_walker(Node *node, locate_var_of_level_context *context)
Definition: var.c:522

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

◆ locate_var_of_level_walker()

static bool locate_var_of_level_walker ( Node node,
locate_var_of_level_context context 
)
static

Definition at line 522 of file var.c.

524 {
525  if (node == NULL)
526  return false;
527  if (IsA(node, Var))
528  {
529  Var *var = (Var *) node;
530 
531  if (var->varlevelsup == context->sublevels_up &&
532  var->location >= 0)
533  {
534  context->var_location = var->location;
535  return true; /* abort tree traversal and return true */
536  }
537  return false;
538  }
539  if (IsA(node, CurrentOfExpr))
540  {
541  /* since CurrentOfExpr doesn't carry location, nothing we can do */
542  return false;
543  }
544  /* No extra code needed for PlaceHolderVar; just look in contained expr */
545  if (IsA(node, Query))
546  {
547  /* Recurse into subselects */
548  bool result;
549 
550  context->sublevels_up++;
551  result = query_tree_walker((Query *) node,
553  (void *) context,
554  0);
555  context->sublevels_up--;
556  return result;
557  }
558  return expression_tree_walker(node,
560  (void *) context);
561 }

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

◆ pull_var_clause()

List* pull_var_clause ( Node node,
int  flags 
)

Definition at line 604 of file var.c.

605 {
606  pull_var_clause_context context;
607 
608  /* Assert that caller has not specified inconsistent flags */
615 
616  context.varlist = NIL;
617  context.flags = flags;
618 
619  pull_var_clause_walker(node, &context);
620  return context.varlist;
621 }
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:189
#define PVC_RECURSE_PLACEHOLDERS
Definition: optimizer.h:193
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:191
#define PVC_INCLUDE_WINDOWFUNCS
Definition: optimizer.h:190
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:192
#define PVC_INCLUDE_AGGREGATES
Definition: optimizer.h:188
static bool pull_var_clause_walker(Node *node, pull_var_clause_context *context)
Definition: var.c:624

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(), CreateTriggerFiringOn(), dependency_is_compatible_expression(), distribute_qual_to_rels(), estimate_num_groups_incremental(), 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().

◆ pull_var_clause_walker()

static bool pull_var_clause_walker ( Node node,
pull_var_clause_context context 
)
static

Definition at line 624 of file var.c.

625 {
626  if (node == NULL)
627  return false;
628  if (IsA(node, Var))
629  {
630  if (((Var *) node)->varlevelsup != 0)
631  elog(ERROR, "Upper-level Var found where not expected");
632  context->varlist = lappend(context->varlist, node);
633  return false;
634  }
635  else if (IsA(node, Aggref))
636  {
637  if (((Aggref *) node)->agglevelsup != 0)
638  elog(ERROR, "Upper-level Aggref found where not expected");
639  if (context->flags & PVC_INCLUDE_AGGREGATES)
640  {
641  context->varlist = lappend(context->varlist, node);
642  /* we do NOT descend into the contained expression */
643  return false;
644  }
645  else if (context->flags & PVC_RECURSE_AGGREGATES)
646  {
647  /* fall through to recurse into the aggregate's arguments */
648  }
649  else
650  elog(ERROR, "Aggref found where not expected");
651  }
652  else if (IsA(node, GroupingFunc))
653  {
654  if (((GroupingFunc *) node)->agglevelsup != 0)
655  elog(ERROR, "Upper-level GROUPING found where not expected");
656  if (context->flags & PVC_INCLUDE_AGGREGATES)
657  {
658  context->varlist = lappend(context->varlist, node);
659  /* we do NOT descend into the contained expression */
660  return false;
661  }
662  else if (context->flags & PVC_RECURSE_AGGREGATES)
663  {
664  /* fall through to recurse into the GroupingFunc's arguments */
665  }
666  else
667  elog(ERROR, "GROUPING found where not expected");
668  }
669  else if (IsA(node, WindowFunc))
670  {
671  /* WindowFuncs have no levelsup field to check ... */
672  if (context->flags & PVC_INCLUDE_WINDOWFUNCS)
673  {
674  context->varlist = lappend(context->varlist, node);
675  /* we do NOT descend into the contained expressions */
676  return false;
677  }
678  else if (context->flags & PVC_RECURSE_WINDOWFUNCS)
679  {
680  /* fall through to recurse into the windowfunc's arguments */
681  }
682  else
683  elog(ERROR, "WindowFunc found where not expected");
684  }
685  else if (IsA(node, PlaceHolderVar))
686  {
687  if (((PlaceHolderVar *) node)->phlevelsup != 0)
688  elog(ERROR, "Upper-level PlaceHolderVar found where not expected");
689  if (context->flags & PVC_INCLUDE_PLACEHOLDERS)
690  {
691  context->varlist = lappend(context->varlist, node);
692  /* we do NOT descend into the contained expression */
693  return false;
694  }
695  else if (context->flags & PVC_RECURSE_PLACEHOLDERS)
696  {
697  /* fall through to recurse into the placeholder's expression */
698  }
699  else
700  elog(ERROR, "PlaceHolderVar found where not expected");
701  }
703  (void *) context);
704 }
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218

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

◆ pull_varattnos()

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

Definition at line 288 of file var.c.

289 {
290  pull_varattnos_context context;
291 
292  context.varattnos = *varattnos;
293  context.varno = varno;
294 
295  (void) pull_varattnos_walker(node, &context);
296 
297  *varattnos = context.varattnos;
298 }
Bitmapset * varattnos
Definition: var.c:41
static bool pull_varattnos_walker(Node *node, pull_varattnos_context *context)
Definition: var.c:301

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(), CreateStatistics(), DefineIndex(), deparseReturningList(), fill_extraUpdatedCols(), has_partition_attrs(), postgresGetForeignRelSize(), RelationGetIndexAttrBitmap(), remove_unused_subquery_outputs(), and statext_is_compatible_clause().

◆ pull_varattnos_walker()

static bool pull_varattnos_walker ( Node node,
pull_varattnos_context context 
)
static

Definition at line 301 of file var.c.

302 {
303  if (node == NULL)
304  return false;
305  if (IsA(node, Var))
306  {
307  Var *var = (Var *) node;
308 
309  if (var->varno == context->varno && var->varlevelsup == 0)
310  context->varattnos =
311  bms_add_member(context->varattnos,
313  return false;
314  }
315 
316  /* Should not find an unplanned subquery */
317  Assert(!IsA(node, Query));
318 
320  (void *) context);
321 }
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

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

◆ pull_varnos()

Relids pull_varnos ( PlannerInfo root,
Node node 
)

Definition at line 100 of file var.c.

101 {
102  pull_varnos_context context;
103 
104  context.varnos = NULL;
105  context.root = root;
106  context.sublevels_up = 0;
107 
108  /*
109  * Must be prepared to start with a Query or a bare expression tree; if
110  * it's a Query, we don't want to increment sublevels_up.
111  */
114  (void *) &context,
115  0);
116 
117  return context.varnos;
118 }
PlannerInfo * root
Definition: var.c:35
int sublevels_up
Definition: var.c:36
Relids varnos
Definition: var.c:34
static bool pull_varnos_walker(Node *node, pull_varnos_context *context)
Definition: var.c:147

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(), pg_get_expr_worker(), process_implied_equality(), pullup_replace_vars_callback(), and reduce_outer_joins_pass2().

◆ pull_varnos_of_level()

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

Definition at line 126 of file var.c.

127 {
128  pull_varnos_context context;
129 
130  context.varnos = NULL;
131  context.root = root;
132  context.sublevels_up = levelsup;
133 
134  /*
135  * Must be prepared to start with a Query or a bare expression tree; if
136  * it's a Query, we don't want to increment sublevels_up.
137  */
140  (void *) &context,
141  0);
142 
143  return context.varnos;
144 }

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

◆ pull_varnos_walker()

static bool pull_varnos_walker ( Node node,
pull_varnos_context context 
)
static

Definition at line 147 of file var.c.

148 {
149  if (node == NULL)
150  return false;
151  if (IsA(node, Var))
152  {
153  Var *var = (Var *) node;
154 
155  if (var->varlevelsup == context->sublevels_up)
156  context->varnos = bms_add_member(context->varnos, var->varno);
157  return false;
158  }
159  if (IsA(node, CurrentOfExpr))
160  {
161  CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
162 
163  if (context->sublevels_up == 0)
164  context->varnos = bms_add_member(context->varnos, cexpr->cvarno);
165  return false;
166  }
167  if (IsA(node, PlaceHolderVar))
168  {
169  PlaceHolderVar *phv = (PlaceHolderVar *) node;
170 
171  /*
172  * If a PlaceHolderVar is not of the target query level, ignore it,
173  * instead recursing into its expression to see if it contains any
174  * vars that are of the target level. We'll also do that when the
175  * caller doesn't pass a "root" pointer. (We probably shouldn't see
176  * PlaceHolderVars at all in such cases, but if we do, this is a
177  * reasonable behavior.)
178  */
179  if (phv->phlevelsup == context->sublevels_up &&
180  context->root != NULL)
181  {
182  /*
183  * Ideally, the PHV's contribution to context->varnos is its
184  * ph_eval_at set. However, this code can be invoked before
185  * that's been computed. If we cannot find a PlaceHolderInfo,
186  * fall back to the conservative assumption that the PHV will be
187  * evaluated at its syntactic level (phv->phrels).
188  *
189  * There is a second hazard: this code is also used to examine
190  * qual clauses during deconstruct_jointree, when we may have a
191  * PlaceHolderInfo but its ph_eval_at value is not yet final, so
192  * that theoretically we could obtain a relid set that's smaller
193  * than we'd see later on. That should never happen though,
194  * because we deconstruct the jointree working upwards. Any outer
195  * join that forces delay of evaluation of a given qual clause
196  * will be processed before we examine that clause here, so the
197  * ph_eval_at value should have been updated to include it.
198  *
199  * Another problem is that a PlaceHolderVar can appear in quals or
200  * tlists that have been translated for use in a child appendrel.
201  * Typically such a PHV is a parameter expression sourced by some
202  * other relation, so that the translation from parent appendrel
203  * to child doesn't change its phrels, and we should still take
204  * ph_eval_at at face value. But in corner cases, the PHV's
205  * original phrels can include the parent appendrel itself, in
206  * which case the translated PHV will have the child appendrel in
207  * phrels, and we must translate ph_eval_at to match.
208  */
209  PlaceHolderInfo *phinfo = NULL;
210 
211  if (phv->phlevelsup == 0)
212  {
213  ListCell *lc;
214 
215  foreach(lc, context->root->placeholder_list)
216  {
217  phinfo = (PlaceHolderInfo *) lfirst(lc);
218  if (phinfo->phid == phv->phid)
219  break;
220  phinfo = NULL;
221  }
222  }
223  if (phinfo == NULL)
224  {
225  /* No PlaceHolderInfo yet, use phrels */
226  context->varnos = bms_add_members(context->varnos,
227  phv->phrels);
228  }
229  else if (bms_equal(phv->phrels, phinfo->ph_var->phrels))
230  {
231  /* Normal case: use ph_eval_at */
232  context->varnos = bms_add_members(context->varnos,
233  phinfo->ph_eval_at);
234  }
235  else
236  {
237  /* Translated PlaceHolderVar: translate ph_eval_at to match */
238  Relids newevalat,
239  delta;
240 
241  /* remove what was removed from phv->phrels ... */
242  delta = bms_difference(phinfo->ph_var->phrels, phv->phrels);
243  newevalat = bms_difference(phinfo->ph_eval_at, delta);
244  /* ... then if that was in fact part of ph_eval_at ... */
245  if (!bms_equal(newevalat, phinfo->ph_eval_at))
246  {
247  /* ... add what was added */
248  delta = bms_difference(phv->phrels, phinfo->ph_var->phrels);
249  newevalat = bms_join(newevalat, delta);
250  }
251  context->varnos = bms_join(context->varnos,
252  newevalat);
253  }
254  return false; /* don't recurse into expression */
255  }
256  }
257  else if (IsA(node, Query))
258  {
259  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
260  bool result;
261 
262  context->sublevels_up++;
263  result = query_tree_walker((Query *) node, pull_varnos_walker,
264  (void *) context, 0);
265  context->sublevels_up--;
266  return result;
267  }
269  (void *) context);
270 }
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:94
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:291
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:795
Relids ph_eval_at
Definition: pathnodes.h:2433
PlaceHolderVar * ph_var
Definition: pathnodes.h:2432
List * placeholder_list
Definition: pathnodes.h:291

References bms_add_member(), bms_add_members(), bms_difference(), bms_equal(), bms_join(), CurrentOfExpr::cvarno, expression_tree_walker(), IsA, lfirst, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_var, 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().

◆ pull_vars_of_level()

List* pull_vars_of_level ( Node node,
int  levelsup 
)

Definition at line 332 of file var.c.

333 {
334  pull_vars_context context;
335 
336  context.vars = NIL;
337  context.sublevels_up = levelsup;
338 
339  /*
340  * Must be prepared to start with a Query or a bare expression tree; if
341  * it's a Query, we don't want to increment sublevels_up.
342  */
345  (void *) &context,
346  0);
347 
348  return context.vars;
349 }
int sublevels_up
Definition: var.c:48
List * vars
Definition: var.c:47
static bool pull_vars_walker(Node *node, pull_vars_context *context)
Definition: var.c:352

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

◆ pull_vars_walker()

static bool pull_vars_walker ( Node node,
pull_vars_context context 
)
static

Definition at line 352 of file var.c.

353 {
354  if (node == NULL)
355  return false;
356  if (IsA(node, Var))
357  {
358  Var *var = (Var *) node;
359 
360  if (var->varlevelsup == context->sublevels_up)
361  context->vars = lappend(context->vars, var);
362  return false;
363  }
364  if (IsA(node, PlaceHolderVar))
365  {
366  PlaceHolderVar *phv = (PlaceHolderVar *) node;
367 
368  if (phv->phlevelsup == context->sublevels_up)
369  context->vars = lappend(context->vars, phv);
370  /* we don't want to look into the contained expression */
371  return false;
372  }
373  if (IsA(node, Query))
374  {
375  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
376  bool result;
377 
378  context->sublevels_up++;
379  result = query_tree_walker((Query *) node, pull_vars_walker,
380  (void *) context, 0);
381  context->sublevels_up--;
382  return result;
383  }
385  (void *) context);
386 }

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