PostgreSQL Source Code  git master
var.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * var.c
4  * Var node manipulation routines
5  *
6  * Note: for most purposes, PlaceHolderVar is considered a Var too,
7  * even if its contained expression is variable-free. Also, CurrentOfExpr
8  * is treated as a Var for purposes of determining whether an expression
9  * contains variables.
10  *
11  *
12  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  *
16  * IDENTIFICATION
17  * src/backend/optimizer/util/var.c
18  *
19  *-------------------------------------------------------------------------
20  */
21 #include "postgres.h"
22 
23 #include "access/sysattr.h"
24 #include "nodes/nodeFuncs.h"
25 #include "optimizer/optimizer.h"
26 #include "optimizer/placeholder.h"
27 #include "optimizer/prep.h"
28 #include "parser/parsetree.h"
29 #include "rewrite/rewriteManip.h"
30 
31 
32 typedef struct
33 {
38 
39 typedef struct
40 {
44 
45 typedef struct
46 {
50 
51 typedef struct
52 {
56 
57 typedef struct
58 {
60  int flags;
62 
63 typedef struct
64 {
65  Query *query; /* outer Query */
67  bool possible_sublink; /* could aliases include a SubLink? */
68  bool inserted_sublink; /* have we inserted a SubLink? */
70 
71 static bool pull_varnos_walker(Node *node,
72  pull_varnos_context *context);
73 static bool pull_varattnos_walker(Node *node, pull_varattnos_context *context);
74 static bool pull_vars_walker(Node *node, pull_vars_context *context);
75 static bool contain_var_clause_walker(Node *node, void *context);
76 static bool contain_vars_of_level_walker(Node *node, int *sublevels_up);
77 static bool locate_var_of_level_walker(Node *node,
79 static bool pull_var_clause_walker(Node *node,
80  pull_var_clause_context *context);
83 static Relids alias_relid_set(Query *query, Relids relids);
84 
85 
86 /*
87  * pull_varnos
88  * Create a set of all the distinct varnos present in a parsetree.
89  * Only varnos that reference level-zero rtable entries are considered.
90  *
91  * NOTE: this is used on not-yet-planned expressions. It may therefore find
92  * bare SubLinks, and if so it needs to recurse into them to look for uplevel
93  * references to the desired rtable level! But when we find a completed
94  * SubPlan, we only need to look at the parameters passed to the subplan.
95  */
96 Relids
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 }
116 
117 /*
118  * pull_varnos_of_level
119  * Create a set of all the distinct varnos present in a parsetree.
120  * Only Vars of the specified level are considered.
121  */
122 Relids
123 pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
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 }
142 
143 static bool
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 }
229 
230 
231 /*
232  * pull_varattnos
233  * Find all the distinct attribute numbers present in an expression tree,
234  * and add them to the initial contents of *varattnos.
235  * Only Vars of the given varno and rtable level zero are considered.
236  *
237  * Attribute numbers are offset by FirstLowInvalidHeapAttributeNumber so that
238  * we can include system attributes (e.g., OID) in the bitmap representation.
239  *
240  * Currently, this does not support unplanned subqueries; that is not needed
241  * for current uses. It will handle already-planned SubPlan nodes, though,
242  * looking into only the "testexpr" and the "args" list. (The subplan cannot
243  * contain any other references to Vars of the current level.)
244  */
245 void
246 pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
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 }
257 
258 static bool
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 }
280 
281 
282 /*
283  * pull_vars_of_level
284  * Create a list of all Vars (and PlaceHolderVars) referencing the
285  * specified query level in the given parsetree.
286  *
287  * Caution: the Vars are not copied, only linked into the list.
288  */
289 List *
290 pull_vars_of_level(Node *node, int levelsup)
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 }
308 
309 static bool
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 }
345 
346 
347 /*
348  * contain_var_clause
349  * Recursively scan a clause to discover whether it contains any Var nodes
350  * (of the current query level).
351  *
352  * Returns true if any varnode found.
353  *
354  * Does not examine subqueries, therefore must only be used after reduction
355  * of sublinks to subplans!
356  */
357 bool
359 {
360  return contain_var_clause_walker(node, NULL);
361 }
362 
363 static bool
364 contain_var_clause_walker(Node *node, void *context)
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 }
384 
385 
386 /*
387  * contain_vars_of_level
388  * Recursively scan a clause to discover whether it contains any Var nodes
389  * of the specified query level.
390  *
391  * Returns true if any such Var found.
392  *
393  * Will recurse into sublinks. Also, may be invoked directly on a Query.
394  */
395 bool
396 contain_vars_of_level(Node *node, int levelsup)
397 {
398  int sublevels_up = levelsup;
399 
402  (void *) &sublevels_up,
403  0);
404 }
405 
406 static bool
407 contain_vars_of_level_walker(Node *node, int *sublevels_up)
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 }
446 
447 
448 /*
449  * locate_var_of_level
450  * Find the parse location of any Var of the specified query level.
451  *
452  * Returns -1 if no such Var is in the querytree, or if they all have
453  * unknown parse location. (The former case is probably caller error,
454  * but we don't bother to distinguish it from the latter case.)
455  *
456  * Will recurse into sublinks. Also, may be invoked directly on a Query.
457  *
458  * Note: it might seem appropriate to merge this functionality into
459  * contain_vars_of_level, but that would complicate that function's API.
460  * Currently, the only uses of this function are for error reporting,
461  * and so shaving cycles probably isn't very important.
462  */
463 int
464 locate_var_of_level(Node *node, int levelsup)
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 }
478 
479 static bool
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 }
520 
521 
522 /*
523  * pull_var_clause
524  * Recursively pulls all Var nodes from an expression clause.
525  *
526  * Aggrefs are handled according to these bits in 'flags':
527  * PVC_INCLUDE_AGGREGATES include Aggrefs in output list
528  * PVC_RECURSE_AGGREGATES recurse into Aggref arguments
529  * neither flag throw error if Aggref found
530  * Vars within an Aggref's expression are included in the result only
531  * when PVC_RECURSE_AGGREGATES is specified.
532  *
533  * WindowFuncs are handled according to these bits in 'flags':
534  * PVC_INCLUDE_WINDOWFUNCS include WindowFuncs in output list
535  * PVC_RECURSE_WINDOWFUNCS recurse into WindowFunc arguments
536  * neither flag throw error if WindowFunc found
537  * Vars within a WindowFunc's expression are included in the result only
538  * when PVC_RECURSE_WINDOWFUNCS is specified.
539  *
540  * PlaceHolderVars are handled according to these bits in 'flags':
541  * PVC_INCLUDE_PLACEHOLDERS include PlaceHolderVars in output list
542  * PVC_RECURSE_PLACEHOLDERS recurse into PlaceHolderVar arguments
543  * neither flag throw error if PlaceHolderVar found
544  * Vars within a PHV's expression are included in the result only
545  * when PVC_RECURSE_PLACEHOLDERS is specified.
546  *
547  * GroupingFuncs are treated mostly like Aggrefs, and so do not need
548  * their own flag bits.
549  *
550  * CurrentOfExpr nodes are ignored in all cases.
551  *
552  * Upper-level vars (with varlevelsup > 0) should not be seen here,
553  * likewise for upper-level Aggrefs and PlaceHolderVars.
554  *
555  * Returns list of nodes found. Note the nodes themselves are not
556  * copied, only referenced.
557  *
558  * Does not examine subqueries, therefore must only be used after reduction
559  * of sublinks to subplans!
560  */
561 List *
562 pull_var_clause(Node *node, int flags)
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 }
580 
581 static bool
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 }
669 
670 
671 /*
672  * flatten_join_alias_vars
673  * Replace Vars that reference JOIN outputs with references to the original
674  * relation variables instead. This allows quals involving such vars to be
675  * pushed down. Whole-row Vars that reference JOIN relations are expanded
676  * into RowExpr constructs that name the individual output Vars. This
677  * is necessary since we will not scan the JOIN as a base relation, which
678  * is the only way that the executor can directly handle whole-row Vars.
679  *
680  * This also adjusts relid sets found in some expression node types to
681  * substitute the contained base rels for any join relid.
682  *
683  * If a JOIN contains sub-selects that have been flattened, its join alias
684  * entries might now be arbitrary expressions, not just Vars. This affects
685  * this function in one important way: we might find ourselves inserting
686  * SubLink expressions into subqueries, and we must make sure that their
687  * Query.hasSubLinks fields get set to true if so. If there are any
688  * SubLinks in the join alias lists, the outer Query should already have
689  * hasSubLinks = true, so this is only relevant to un-flattened subqueries.
690  *
691  * NOTE: this is used on not-yet-planned expressions. We do not expect it
692  * to be applied directly to the whole Query, so if we see a Query to start
693  * with, we do want to increment sublevels_up (this occurs for LATERAL
694  * subqueries).
695  */
696 Node *
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 }
710 
711 static Node *
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 }
847 
848 /*
849  * alias_relid_set: in a set of RT indexes, replace joins by their
850  * underlying base relids
851  */
852 static Relids
853 alias_relid_set(Query *query, Relids relids)
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 }
List * pull_vars_of_level(Node *node, int levelsup)
Definition: var.c:290
int sublevels_up
Definition: var.c:48
#define NIL
Definition: pg_list.h:65
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:97
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2336
#define PVC_RECURSE_PLACEHOLDERS
Definition: optimizer.h:189
List * args
Definition: primnodes.h:1046
#define IsA(nodeptr, _type_)
Definition: nodes.h:584
List * joinaliasvars
Definition: parsenodes.h:1056
Index varlevelsup
Definition: primnodes.h:191
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2579
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
Relids ph_eval_at
Definition: pathnodes.h:2329
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
Bitmapset * varattnos
Definition: var.c:41
static bool pull_vars_walker(Node *node, pull_vars_context *context)
Definition: var.c:310
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
Definition: nodes.h:533
AttrNumber varattno
Definition: primnodes.h:186
static bool pull_varnos_walker(Node *node, pull_varnos_context *context)
Definition: var.c:144
int sublevels_up
Definition: var.c:36
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
List * pull_var_clause(Node *node, int flags)
Definition: var.c:562
bool contain_var_clause(Node *node)
Definition: var.c:358
Definition: primnodes.h:181
List * vars
Definition: var.c:47
#define PVC_INCLUDE_AGGREGATES
Definition: optimizer.h:183
void pull_varattnos(Node *node, Index varno, Bitmapset **varattnos)
Definition: var.c:246
#define PVC_INCLUDE_WINDOWFUNCS
Definition: optimizer.h:185
int locate_var_of_level(Node *node, int levelsup)
Definition: var.c:464
List * rtable
Definition: parsenodes.h:137
static bool pull_varattnos_walker(Node *node, pull_varattnos_context *context)
Definition: var.c:259
#define ERROR
Definition: elog.h:45
List * colnames
Definition: primnodes.h:1062
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
Oid vartype
Definition: primnodes.h:188
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:949
int location
Definition: primnodes.h:196
int location
Definition: primnodes.h:1063
Relids varnos
Definition: var.c:34
PlannerInfo * root
Definition: var.c:35
Relids get_relids_for_join(Query *query, int joinrelid)
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
List * lappend(List *list, void *datum)
Definition: list.c:336
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3442
Index varno
Definition: primnodes.h:184
static bool contain_var_clause_walker(Node *node, void *context)
Definition: var.c:364
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:187
static bool locate_var_of_level_walker(Node *node, locate_var_of_level_context *context)
Definition: var.c:480
unsigned int Index
Definition: c.h:549
Node * flatten_join_alias_vars(Query *query, Node *node)
Definition: var.c:697
int16 attnum
Definition: pg_attribute.h:83
#define makeNode(_type_)
Definition: nodes.h:581
#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:396
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1885
Oid row_typeid
Definition: primnodes.h:1047
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 InvalidAttrNumber
Definition: attnum.h:23
static bool contain_vars_of_level_walker(Node *node, int *sublevels_up)
Definition: var.c:407
RTEKind rtekind
Definition: parsenodes.h:981
static bool pull_var_clause_walker(Node *node, pull_var_clause_context *context)
Definition: var.c:582
Index phlevelsup
Definition: pathnodes.h:2128
bool hasSubLinks
Definition: parsenodes.h:128
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:186
#define elog(elevel,...)
Definition: elog.h:227
List * placeholder_list
Definition: pathnodes.h:288
static Node * flatten_join_alias_vars_mutator(Node *node, flatten_join_alias_vars_context *context)
Definition: var.c:712
Alias * eref
Definition: parsenodes.h:1120
#define copyObject(obj)
Definition: nodes.h:649
CoercionForm row_format
Definition: primnodes.h:1061
Relids pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
Definition: var.c:123
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:184
Query * query_tree_mutator(Query *query, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3281
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23