PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
clauses.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * clauses.c
4  * routines to manipulate qualification clauses
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/optimizer/util/clauses.c
12  *
13  * HISTORY
14  * AUTHOR DATE MAJOR EVENT
15  * Andrew Yu Nov 3, 1994 clause.c and clauses.c combined
16  *
17  *-------------------------------------------------------------------------
18  */
19 
20 #include "postgres.h"
21 
22 #include "access/htup_details.h"
23 #include "catalog/pg_aggregate.h"
24 #include "catalog/pg_class.h"
25 #include "catalog/pg_language.h"
26 #include "catalog/pg_operator.h"
27 #include "catalog/pg_proc.h"
28 #include "catalog/pg_type.h"
29 #include "executor/executor.h"
30 #include "executor/functions.h"
31 #include "funcapi.h"
32 #include "miscadmin.h"
33 #include "nodes/makefuncs.h"
34 #include "nodes/nodeFuncs.h"
35 #include "optimizer/clauses.h"
36 #include "optimizer/cost.h"
37 #include "optimizer/planmain.h"
38 #include "optimizer/prep.h"
39 #include "optimizer/var.h"
40 #include "parser/analyze.h"
41 #include "parser/parse_agg.h"
42 #include "parser/parse_coerce.h"
43 #include "parser/parse_func.h"
44 #include "rewrite/rewriteManip.h"
45 #include "tcop/tcopprot.h"
46 #include "utils/acl.h"
47 #include "utils/builtins.h"
48 #include "utils/datum.h"
49 #include "utils/fmgroids.h"
50 #include "utils/lsyscache.h"
51 #include "utils/memutils.h"
52 #include "utils/syscache.h"
53 #include "utils/typcache.h"
54 
55 
56 typedef struct
57 {
62 
63 typedef struct
64 {
69  bool estimate;
71 
72 typedef struct
73 {
74  int nargs;
76  int *usecounts;
78 
79 typedef struct
80 {
81  int nargs;
85 
86 typedef struct
87 {
88  char *proname;
89  char *prosrc;
91 
92 typedef struct
93 {
94  char max_hazard; /* worst proparallel hazard found so far */
95  char max_interesting; /* worst proparallel hazard of interest */
96  List *safe_param_ids; /* PARAM_EXEC Param IDs to treat as safe */
98 
99 static bool contain_agg_clause_walker(Node *node, void *context);
100 static bool get_agg_clause_costs_walker(Node *node,
102 static bool find_window_functions_walker(Node *node, WindowFuncLists *lists);
103 static bool contain_subplans_walker(Node *node, void *context);
104 static bool contain_mutable_functions_walker(Node *node, void *context);
105 static bool contain_volatile_functions_walker(Node *node, void *context);
106 static bool contain_volatile_functions_not_nextval_walker(Node *node, void *context);
107 static bool max_parallel_hazard_walker(Node *node,
108  max_parallel_hazard_context *context);
109 static bool contain_nonstrict_functions_walker(Node *node, void *context);
110 static bool contain_context_dependent_node(Node *clause);
111 static bool contain_context_dependent_node_walker(Node *node, int *flags);
112 static bool contain_leaked_vars_walker(Node *node, void *context);
113 static Relids find_nonnullable_rels_walker(Node *node, bool top_level);
114 static List *find_nonnullable_vars_walker(Node *node, bool top_level);
115 static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK);
120  bool *haveNull, bool *forceTrue);
123  bool *haveNull, bool *forceFalse);
124 static Node *simplify_boolean_equality(Oid opno, List *args);
125 static Expr *simplify_function(Oid funcid,
126  Oid result_type, int32 result_typmod,
127  Oid result_collid, Oid input_collid, List **args_p,
128  bool funcvariadic, bool process_args, bool allow_non_const,
130 static List *expand_function_arguments(List *args, Oid result_type,
131  HeapTuple func_tuple);
132 static List *reorder_function_arguments(List *args, HeapTuple func_tuple);
133 static List *add_function_defaults(List *args, HeapTuple func_tuple);
134 static List *fetch_function_defaults(HeapTuple func_tuple);
135 static void recheck_cast_function_args(List *args, Oid result_type,
136  HeapTuple func_tuple);
137 static Expr *evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
138  Oid result_collid, Oid input_collid, List *args,
139  bool funcvariadic,
140  HeapTuple func_tuple,
142 static Expr *inline_function(Oid funcid, Oid result_type, Oid result_collid,
143  Oid input_collid, List *args,
144  bool funcvariadic,
145  HeapTuple func_tuple,
147 static Node *substitute_actual_parameters(Node *expr, int nargs, List *args,
148  int *usecounts);
151 static void sql_inline_error_callback(void *arg);
152 static Expr *evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod,
153  Oid result_collation);
155  int nargs, List *args);
158 static bool tlist_matches_coltypelist(List *tlist, List *coltypelist);
159 
160 
161 /*****************************************************************************
162  * OPERATOR clause functions
163  *****************************************************************************/
164 
165 /*
166  * make_opclause
167  * Creates an operator clause given its operator info, left operand
168  * and right operand (pass NULL to create single-operand clause),
169  * and collation info.
170  */
171 Expr *
172 make_opclause(Oid opno, Oid opresulttype, bool opretset,
173  Expr *leftop, Expr *rightop,
174  Oid opcollid, Oid inputcollid)
175 {
176  OpExpr *expr = makeNode(OpExpr);
177 
178  expr->opno = opno;
179  expr->opfuncid = InvalidOid;
180  expr->opresulttype = opresulttype;
181  expr->opretset = opretset;
182  expr->opcollid = opcollid;
183  expr->inputcollid = inputcollid;
184  if (rightop)
185  expr->args = list_make2(leftop, rightop);
186  else
187  expr->args = list_make1(leftop);
188  expr->location = -1;
189  return (Expr *) expr;
190 }
191 
192 /*
193  * get_leftop
194  *
195  * Returns the left operand of a clause of the form (op expr expr)
196  * or (op expr)
197  */
198 Node *
199 get_leftop(const Expr *clause)
200 {
201  const OpExpr *expr = (const OpExpr *) clause;
202 
203  if (expr->args != NIL)
204  return linitial(expr->args);
205  else
206  return NULL;
207 }
208 
209 /*
210  * get_rightop
211  *
212  * Returns the right operand in a clause of the form (op expr expr).
213  * NB: result will be NULL if applied to a unary op clause.
214  */
215 Node *
216 get_rightop(const Expr *clause)
217 {
218  const OpExpr *expr = (const OpExpr *) clause;
219 
220  if (list_length(expr->args) >= 2)
221  return lsecond(expr->args);
222  else
223  return NULL;
224 }
225 
226 /*****************************************************************************
227  * NOT clause functions
228  *****************************************************************************/
229 
230 /*
231  * not_clause
232  *
233  * Returns t iff this is a 'not' clause: (NOT expr).
234  */
235 bool
236 not_clause(Node *clause)
237 {
238  return (clause != NULL &&
239  IsA(clause, BoolExpr) &&
240  ((BoolExpr *) clause)->boolop == NOT_EXPR);
241 }
242 
243 /*
244  * make_notclause
245  *
246  * Create a 'not' clause given the expression to be negated.
247  */
248 Expr *
249 make_notclause(Expr *notclause)
250 {
251  BoolExpr *expr = makeNode(BoolExpr);
252 
253  expr->boolop = NOT_EXPR;
254  expr->args = list_make1(notclause);
255  expr->location = -1;
256  return (Expr *) expr;
257 }
258 
259 /*
260  * get_notclausearg
261  *
262  * Retrieve the clause within a 'not' clause
263  */
264 Expr *
266 {
267  return linitial(((BoolExpr *) notclause)->args);
268 }
269 
270 /*****************************************************************************
271  * OR clause functions
272  *****************************************************************************/
273 
274 /*
275  * or_clause
276  *
277  * Returns t iff the clause is an 'or' clause: (OR { expr }).
278  */
279 bool
280 or_clause(Node *clause)
281 {
282  return (clause != NULL &&
283  IsA(clause, BoolExpr) &&
284  ((BoolExpr *) clause)->boolop == OR_EXPR);
285 }
286 
287 /*
288  * make_orclause
289  *
290  * Creates an 'or' clause given a list of its subclauses.
291  */
292 Expr *
293 make_orclause(List *orclauses)
294 {
295  BoolExpr *expr = makeNode(BoolExpr);
296 
297  expr->boolop = OR_EXPR;
298  expr->args = orclauses;
299  expr->location = -1;
300  return (Expr *) expr;
301 }
302 
303 /*****************************************************************************
304  * AND clause functions
305  *****************************************************************************/
306 
307 
308 /*
309  * and_clause
310  *
311  * Returns t iff its argument is an 'and' clause: (AND { expr }).
312  */
313 bool
314 and_clause(Node *clause)
315 {
316  return (clause != NULL &&
317  IsA(clause, BoolExpr) &&
318  ((BoolExpr *) clause)->boolop == AND_EXPR);
319 }
320 
321 /*
322  * make_andclause
323  *
324  * Creates an 'and' clause given a list of its subclauses.
325  */
326 Expr *
327 make_andclause(List *andclauses)
328 {
329  BoolExpr *expr = makeNode(BoolExpr);
330 
331  expr->boolop = AND_EXPR;
332  expr->args = andclauses;
333  expr->location = -1;
334  return (Expr *) expr;
335 }
336 
337 /*
338  * make_and_qual
339  *
340  * Variant of make_andclause for ANDing two qual conditions together.
341  * Qual conditions have the property that a NULL nodetree is interpreted
342  * as 'true'.
343  *
344  * NB: this makes no attempt to preserve AND/OR flatness; so it should not
345  * be used on a qual that has already been run through prepqual.c.
346  */
347 Node *
348 make_and_qual(Node *qual1, Node *qual2)
349 {
350  if (qual1 == NULL)
351  return qual2;
352  if (qual2 == NULL)
353  return qual1;
354  return (Node *) make_andclause(list_make2(qual1, qual2));
355 }
356 
357 /*
358  * The planner frequently prefers to represent qualification expressions
359  * as lists of boolean expressions with implicit AND semantics.
360  *
361  * These functions convert between an AND-semantics expression list and the
362  * ordinary representation of a boolean expression.
363  *
364  * Note that an empty list is considered equivalent to TRUE.
365  */
366 Expr *
368 {
369  if (andclauses == NIL)
370  return (Expr *) makeBoolConst(true, false);
371  else if (list_length(andclauses) == 1)
372  return (Expr *) linitial(andclauses);
373  else
374  return make_andclause(andclauses);
375 }
376 
377 List *
379 {
380  /*
381  * NB: because the parser sets the qual field to NULL in a query that has
382  * no WHERE clause, we must consider a NULL input clause as TRUE, even
383  * though one might more reasonably think it FALSE. Grumble. If this
384  * causes trouble, consider changing the parser's behavior.
385  */
386  if (clause == NULL)
387  return NIL; /* NULL -> NIL list == TRUE */
388  else if (and_clause((Node *) clause))
389  return ((BoolExpr *) clause)->args;
390  else if (IsA(clause, Const) &&
391  !((Const *) clause)->constisnull &&
392  DatumGetBool(((Const *) clause)->constvalue))
393  return NIL; /* constant TRUE input -> NIL list */
394  else
395  return list_make1(clause);
396 }
397 
398 
399 /*****************************************************************************
400  * Aggregate-function clause manipulation
401  *****************************************************************************/
402 
403 /*
404  * contain_agg_clause
405  * Recursively search for Aggref/GroupingFunc nodes within a clause.
406  *
407  * Returns true if any aggregate found.
408  *
409  * This does not descend into subqueries, and so should be used only after
410  * reduction of sublinks to subplans, or in contexts where it's known there
411  * are no subqueries. There mustn't be outer-aggregate references either.
412  *
413  * (If you want something like this but able to deal with subqueries,
414  * see rewriteManip.c's contain_aggs_of_level().)
415  */
416 bool
418 {
419  return contain_agg_clause_walker(clause, NULL);
420 }
421 
422 static bool
423 contain_agg_clause_walker(Node *node, void *context)
424 {
425  if (node == NULL)
426  return false;
427  if (IsA(node, Aggref))
428  {
429  Assert(((Aggref *) node)->agglevelsup == 0);
430  return true; /* abort the tree traversal and return true */
431  }
432  if (IsA(node, GroupingFunc))
433  {
434  Assert(((GroupingFunc *) node)->agglevelsup == 0);
435  return true; /* abort the tree traversal and return true */
436  }
437  Assert(!IsA(node, SubLink));
438  return expression_tree_walker(node, contain_agg_clause_walker, context);
439 }
440 
441 /*
442  * get_agg_clause_costs
443  * Recursively find the Aggref nodes in an expression tree, and
444  * accumulate cost information about them.
445  *
446  * 'aggsplit' tells us the expected partial-aggregation mode, which affects
447  * the cost estimates.
448  *
449  * NOTE that the counts/costs are ADDED to those already in *costs ... so
450  * the caller is responsible for zeroing the struct initially.
451  *
452  * We count the nodes, estimate their execution costs, and estimate the total
453  * space needed for their transition state values if all are evaluated in
454  * parallel (as would be done in a HashAgg plan). Also, we check whether
455  * partial aggregation is feasible. See AggClauseCosts for the exact set
456  * of statistics collected.
457  *
458  * In addition, we mark Aggref nodes with the correct aggtranstype, so
459  * that that doesn't need to be done repeatedly. (That makes this function's
460  * name a bit of a misnomer.)
461  *
462  * This does not descend into subqueries, and so should be used only after
463  * reduction of sublinks to subplans, or in contexts where it's known there
464  * are no subqueries. There mustn't be outer-aggregate references either.
465  */
466 void
468  AggClauseCosts *costs)
469 {
471 
472  context.root = root;
473  context.aggsplit = aggsplit;
474  context.costs = costs;
475  (void) get_agg_clause_costs_walker(clause, &context);
476 }
477 
478 static bool
480 {
481  if (node == NULL)
482  return false;
483  if (IsA(node, Aggref))
484  {
485  Aggref *aggref = (Aggref *) node;
486  AggClauseCosts *costs = context->costs;
487  HeapTuple aggTuple;
488  Form_pg_aggregate aggform;
489  Oid aggtransfn;
490  Oid aggfinalfn;
491  Oid aggcombinefn;
492  Oid aggserialfn;
493  Oid aggdeserialfn;
494  Oid aggtranstype;
495  int32 aggtransspace;
496  QualCost argcosts;
497 
498  Assert(aggref->agglevelsup == 0);
499 
500  /*
501  * Fetch info about aggregate from pg_aggregate. Note it's correct to
502  * ignore the moving-aggregate variant, since what we're concerned
503  * with here is aggregates not window functions.
504  */
505  aggTuple = SearchSysCache1(AGGFNOID,
506  ObjectIdGetDatum(aggref->aggfnoid));
507  if (!HeapTupleIsValid(aggTuple))
508  elog(ERROR, "cache lookup failed for aggregate %u",
509  aggref->aggfnoid);
510  aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
511  aggtransfn = aggform->aggtransfn;
512  aggfinalfn = aggform->aggfinalfn;
513  aggcombinefn = aggform->aggcombinefn;
514  aggserialfn = aggform->aggserialfn;
515  aggdeserialfn = aggform->aggdeserialfn;
516  aggtranstype = aggform->aggtranstype;
517  aggtransspace = aggform->aggtransspace;
518  ReleaseSysCache(aggTuple);
519 
520  /*
521  * Resolve the possibly-polymorphic aggregate transition type, unless
522  * already done in a previous pass over the expression.
523  */
524  if (OidIsValid(aggref->aggtranstype))
525  aggtranstype = aggref->aggtranstype;
526  else
527  {
528  Oid inputTypes[FUNC_MAX_ARGS];
529  int numArguments;
530 
531  /* extract argument types (ignoring any ORDER BY expressions) */
532  numArguments = get_aggregate_argtypes(aggref, inputTypes);
533 
534  /* resolve actual type of transition state, if polymorphic */
535  aggtranstype = resolve_aggregate_transtype(aggref->aggfnoid,
536  aggtranstype,
537  inputTypes,
538  numArguments);
539  aggref->aggtranstype = aggtranstype;
540  }
541 
542  /*
543  * Count it, and check for cases requiring ordered input. Note that
544  * ordered-set aggs always have nonempty aggorder. Any ordered-input
545  * case also defeats partial aggregation.
546  */
547  costs->numAggs++;
548  if (aggref->aggorder != NIL || aggref->aggdistinct != NIL)
549  {
550  costs->numOrderedAggs++;
551  costs->hasNonPartial = true;
552  }
553 
554  /*
555  * Check whether partial aggregation is feasible, unless we already
556  * found out that we can't do it.
557  */
558  if (!costs->hasNonPartial)
559  {
560  /*
561  * If there is no combine function, then partial aggregation is
562  * not possible.
563  */
564  if (!OidIsValid(aggcombinefn))
565  costs->hasNonPartial = true;
566 
567  /*
568  * If we have any aggs with transtype INTERNAL then we must check
569  * whether they have serialization/deserialization functions; if
570  * not, we can't serialize partial-aggregation results.
571  */
572  else if (aggtranstype == INTERNALOID &&
573  (!OidIsValid(aggserialfn) || !OidIsValid(aggdeserialfn)))
574  costs->hasNonSerial = true;
575  }
576 
577  /*
578  * Add the appropriate component function execution costs to
579  * appropriate totals.
580  */
581  if (DO_AGGSPLIT_COMBINE(context->aggsplit))
582  {
583  /* charge for combining previously aggregated states */
584  costs->transCost.per_tuple += get_func_cost(aggcombinefn) * cpu_operator_cost;
585  }
586  else
587  costs->transCost.per_tuple += get_func_cost(aggtransfn) * cpu_operator_cost;
588  if (DO_AGGSPLIT_DESERIALIZE(context->aggsplit) &&
589  OidIsValid(aggdeserialfn))
590  costs->transCost.per_tuple += get_func_cost(aggdeserialfn) * cpu_operator_cost;
591  if (DO_AGGSPLIT_SERIALIZE(context->aggsplit) &&
592  OidIsValid(aggserialfn))
593  costs->finalCost += get_func_cost(aggserialfn) * cpu_operator_cost;
594  if (!DO_AGGSPLIT_SKIPFINAL(context->aggsplit) &&
595  OidIsValid(aggfinalfn))
596  costs->finalCost += get_func_cost(aggfinalfn) * cpu_operator_cost;
597 
598  /*
599  * These costs are incurred only by the initial aggregate node, so we
600  * mustn't include them again at upper levels.
601  */
602  if (!DO_AGGSPLIT_COMBINE(context->aggsplit))
603  {
604  /* add the input expressions' cost to per-input-row costs */
605  cost_qual_eval_node(&argcosts, (Node *) aggref->args, context->root);
606  costs->transCost.startup += argcosts.startup;
607  costs->transCost.per_tuple += argcosts.per_tuple;
608 
609  /*
610  * Add any filter's cost to per-input-row costs.
611  *
612  * XXX Ideally we should reduce input expression costs according
613  * to filter selectivity, but it's not clear it's worth the
614  * trouble.
615  */
616  if (aggref->aggfilter)
617  {
618  cost_qual_eval_node(&argcosts, (Node *) aggref->aggfilter,
619  context->root);
620  costs->transCost.startup += argcosts.startup;
621  costs->transCost.per_tuple += argcosts.per_tuple;
622  }
623  }
624 
625  /*
626  * If there are direct arguments, treat their evaluation cost like the
627  * cost of the finalfn.
628  */
629  if (aggref->aggdirectargs)
630  {
631  cost_qual_eval_node(&argcosts, (Node *) aggref->aggdirectargs,
632  context->root);
633  costs->transCost.startup += argcosts.startup;
634  costs->finalCost += argcosts.per_tuple;
635  }
636 
637  /*
638  * If the transition type is pass-by-value then it doesn't add
639  * anything to the required size of the hashtable. If it is
640  * pass-by-reference then we have to add the estimated size of the
641  * value itself, plus palloc overhead.
642  */
643  if (!get_typbyval(aggtranstype))
644  {
645  int32 avgwidth;
646 
647  /* Use average width if aggregate definition gave one */
648  if (aggtransspace > 0)
649  avgwidth = aggtransspace;
650  else if (aggtransfn == F_ARRAY_APPEND)
651  {
652  /*
653  * If the transition function is array_append(), it'll use an
654  * expanded array as transvalue, which will occupy at least
655  * ALLOCSET_SMALL_INITSIZE and possibly more. Use that as the
656  * estimate for lack of a better idea.
657  */
658  avgwidth = ALLOCSET_SMALL_INITSIZE;
659  }
660  else
661  {
662  /*
663  * If transition state is of same type as first aggregated
664  * input, assume it's the same typmod (same width) as well.
665  * This works for cases like MAX/MIN and is probably somewhat
666  * reasonable otherwise.
667  */
668  int32 aggtranstypmod = -1;
669 
670  if (aggref->args)
671  {
672  TargetEntry *tle = (TargetEntry *) linitial(aggref->args);
673 
674  if (aggtranstype == exprType((Node *) tle->expr))
675  aggtranstypmod = exprTypmod((Node *) tle->expr);
676  }
677 
678  avgwidth = get_typavgwidth(aggtranstype, aggtranstypmod);
679  }
680 
681  avgwidth = MAXALIGN(avgwidth);
682  costs->transitionSpace += avgwidth + 2 * sizeof(void *);
683  }
684  else if (aggtranstype == INTERNALOID)
685  {
686  /*
687  * INTERNAL transition type is a special case: although INTERNAL
688  * is pass-by-value, it's almost certainly being used as a pointer
689  * to some large data structure. The aggregate definition can
690  * provide an estimate of the size. If it doesn't, then we assume
691  * ALLOCSET_DEFAULT_INITSIZE, which is a good guess if the data is
692  * being kept in a private memory context, as is done by
693  * array_agg() for instance.
694  */
695  if (aggtransspace > 0)
696  costs->transitionSpace += aggtransspace;
697  else
699  }
700 
701  /*
702  * We assume that the parser checked that there are no aggregates (of
703  * this level anyway) in the aggregated arguments, direct arguments,
704  * or filter clause. Hence, we need not recurse into any of them.
705  */
706  return false;
707  }
708  Assert(!IsA(node, SubLink));
710  (void *) context);
711 }
712 
713 
714 /*****************************************************************************
715  * Window-function clause manipulation
716  *****************************************************************************/
717 
718 /*
719  * contain_window_function
720  * Recursively search for WindowFunc nodes within a clause.
721  *
722  * Since window functions don't have level fields, but are hard-wired to
723  * be associated with the current query level, this is just the same as
724  * rewriteManip.c's function.
725  */
726 bool
728 {
729  return contain_windowfuncs(clause);
730 }
731 
732 /*
733  * find_window_functions
734  * Locate all the WindowFunc nodes in an expression tree, and organize
735  * them by winref ID number.
736  *
737  * Caller must provide an upper bound on the winref IDs expected in the tree.
738  */
740 find_window_functions(Node *clause, Index maxWinRef)
741 {
742  WindowFuncLists *lists = palloc(sizeof(WindowFuncLists));
743 
744  lists->numWindowFuncs = 0;
745  lists->maxWinRef = maxWinRef;
746  lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
747  (void) find_window_functions_walker(clause, lists);
748  return lists;
749 }
750 
751 static bool
753 {
754  if (node == NULL)
755  return false;
756  if (IsA(node, WindowFunc))
757  {
758  WindowFunc *wfunc = (WindowFunc *) node;
759 
760  /* winref is unsigned, so one-sided test is OK */
761  if (wfunc->winref > lists->maxWinRef)
762  elog(ERROR, "WindowFunc contains out-of-range winref %u",
763  wfunc->winref);
764  /* eliminate duplicates, so that we avoid repeated computation */
765  if (!list_member(lists->windowFuncs[wfunc->winref], wfunc))
766  {
767  lists->windowFuncs[wfunc->winref] =
768  lappend(lists->windowFuncs[wfunc->winref], wfunc);
769  lists->numWindowFuncs++;
770  }
771 
772  /*
773  * We assume that the parser checked that there are no window
774  * functions in the arguments or filter clause. Hence, we need not
775  * recurse into them. (If either the parser or the planner screws up
776  * on this point, the executor will still catch it; see ExecInitExpr.)
777  */
778  return false;
779  }
780  Assert(!IsA(node, SubLink));
782  (void *) lists);
783 }
784 
785 
786 /*****************************************************************************
787  * Support for expressions returning sets
788  *****************************************************************************/
789 
790 /*
791  * expression_returns_set_rows
792  * Estimate the number of rows returned by a set-returning expression.
793  * The result is 1 if it's not a set-returning expression.
794  *
795  * We should only examine the top-level function or operator; it used to be
796  * appropriate to recurse, but not anymore. (Even if there are more SRFs in
797  * the function's inputs, their multipliers are accounted for separately.)
798  *
799  * Note: keep this in sync with expression_returns_set() in nodes/nodeFuncs.c.
800  */
801 double
803 {
804  if (clause == NULL)
805  return 1.0;
806  if (IsA(clause, FuncExpr))
807  {
808  FuncExpr *expr = (FuncExpr *) clause;
809 
810  if (expr->funcretset)
811  return clamp_row_est(get_func_rows(expr->funcid));
812  }
813  if (IsA(clause, OpExpr))
814  {
815  OpExpr *expr = (OpExpr *) clause;
816 
817  if (expr->opretset)
818  {
819  set_opfuncid(expr);
820  return clamp_row_est(get_func_rows(expr->opfuncid));
821  }
822  }
823  return 1.0;
824 }
825 
826 
827 /*****************************************************************************
828  * Subplan clause manipulation
829  *****************************************************************************/
830 
831 /*
832  * contain_subplans
833  * Recursively search for subplan nodes within a clause.
834  *
835  * If we see a SubLink node, we will return TRUE. This is only possible if
836  * the expression tree hasn't yet been transformed by subselect.c. We do not
837  * know whether the node will produce a true subplan or just an initplan,
838  * but we make the conservative assumption that it will be a subplan.
839  *
840  * Returns true if any subplan found.
841  */
842 bool
844 {
845  return contain_subplans_walker(clause, NULL);
846 }
847 
848 static bool
849 contain_subplans_walker(Node *node, void *context)
850 {
851  if (node == NULL)
852  return false;
853  if (IsA(node, SubPlan) ||
854  IsA(node, AlternativeSubPlan) ||
855  IsA(node, SubLink))
856  return true; /* abort the tree traversal and return true */
857  return expression_tree_walker(node, contain_subplans_walker, context);
858 }
859 
860 
861 /*****************************************************************************
862  * Check clauses for mutable functions
863  *****************************************************************************/
864 
865 /*
866  * contain_mutable_functions
867  * Recursively search for mutable functions within a clause.
868  *
869  * Returns true if any mutable function (or operator implemented by a
870  * mutable function) is found. This test is needed so that we don't
871  * mistakenly think that something like "WHERE random() < 0.5" can be treated
872  * as a constant qualification.
873  *
874  * We will recursively look into Query nodes (i.e., SubLink sub-selects)
875  * but not into SubPlans. See comments for contain_volatile_functions().
876  */
877 bool
879 {
880  return contain_mutable_functions_walker(clause, NULL);
881 }
882 
883 static bool
884 contain_mutable_functions_checker(Oid func_id, void *context)
885 {
886  return (func_volatile(func_id) != PROVOLATILE_IMMUTABLE);
887 }
888 
889 static bool
891 {
892  if (node == NULL)
893  return false;
894  /* Check for mutable functions in node itself */
896  context))
897  return true;
898 
899  if (IsA(node, SQLValueFunction))
900  {
901  /* all variants of SQLValueFunction are stable */
902  return true;
903  }
904 
905  if (IsA(node, NextValueExpr))
906  {
907  /* NextValueExpr is volatile */
908  return true;
909  }
910 
911  /*
912  * It should be safe to treat MinMaxExpr as immutable, because it will
913  * depend on a non-cross-type btree comparison function, and those should
914  * always be immutable. Treating XmlExpr as immutable is more dubious,
915  * and treating CoerceToDomain as immutable is outright dangerous. But we
916  * have done so historically, and changing this would probably cause more
917  * problems than it would fix. In practice, if you have a non-immutable
918  * domain constraint you are in for pain anyhow.
919  */
920 
921  /* Recurse to check arguments */
922  if (IsA(node, Query))
923  {
924  /* Recurse into subselects */
925  return query_tree_walker((Query *) node,
927  context, 0);
928  }
930  context);
931 }
932 
933 
934 /*****************************************************************************
935  * Check clauses for volatile functions
936  *****************************************************************************/
937 
938 /*
939  * contain_volatile_functions
940  * Recursively search for volatile functions within a clause.
941  *
942  * Returns true if any volatile function (or operator implemented by a
943  * volatile function) is found. This test prevents, for example,
944  * invalid conversions of volatile expressions into indexscan quals.
945  *
946  * We will recursively look into Query nodes (i.e., SubLink sub-selects)
947  * but not into SubPlans. This is a bit odd, but intentional. If we are
948  * looking at a SubLink, we are probably deciding whether a query tree
949  * transformation is safe, and a contained sub-select should affect that;
950  * for example, duplicating a sub-select containing a volatile function
951  * would be bad. However, once we've got to the stage of having SubPlans,
952  * subsequent planning need not consider volatility within those, since
953  * the executor won't change its evaluation rules for a SubPlan based on
954  * volatility.
955  */
956 bool
958 {
959  return contain_volatile_functions_walker(clause, NULL);
960 }
961 
962 static bool
964 {
965  return (func_volatile(func_id) == PROVOLATILE_VOLATILE);
966 }
967 
968 static bool
970 {
971  if (node == NULL)
972  return false;
973  /* Check for volatile functions in node itself */
975  context))
976  return true;
977 
978  if (IsA(node, NextValueExpr))
979  {
980  /* NextValueExpr is volatile */
981  return true;
982  }
983 
984  /*
985  * See notes in contain_mutable_functions_walker about why we treat
986  * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
987  * SQLValueFunction is stable. Hence, none of them are of interest here.
988  */
989 
990  /* Recurse to check arguments */
991  if (IsA(node, Query))
992  {
993  /* Recurse into subselects */
994  return query_tree_walker((Query *) node,
996  context, 0);
997  }
999  context);
1000 }
1001 
1002 /*
1003  * Special purpose version of contain_volatile_functions() for use in COPY:
1004  * ignore nextval(), but treat all other functions normally.
1005  */
1006 bool
1008 {
1009  return contain_volatile_functions_not_nextval_walker(clause, NULL);
1010 }
1011 
1012 static bool
1014 {
1015  return (func_id != F_NEXTVAL_OID &&
1016  func_volatile(func_id) == PROVOLATILE_VOLATILE);
1017 }
1018 
1019 static bool
1021 {
1022  if (node == NULL)
1023  return false;
1024  /* Check for volatile functions in node itself */
1025  if (check_functions_in_node(node,
1027  context))
1028  return true;
1029 
1030  /*
1031  * See notes in contain_mutable_functions_walker about why we treat
1032  * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
1033  * SQLValueFunction is stable. Hence, none of them are of interest here.
1034  * Also, since we're intentionally ignoring nextval(), presumably we
1035  * should ignore NextValueExpr.
1036  */
1037 
1038  /* Recurse to check arguments */
1039  if (IsA(node, Query))
1040  {
1041  /* Recurse into subselects */
1042  return query_tree_walker((Query *) node,
1044  context, 0);
1045  }
1046  return expression_tree_walker(node,
1048  context);
1049 }
1050 
1051 
1052 /*****************************************************************************
1053  * Check queries for parallel unsafe and/or restricted constructs
1054  *****************************************************************************/
1055 
1056 /*
1057  * max_parallel_hazard
1058  * Find the worst parallel-hazard level in the given query
1059  *
1060  * Returns the worst function hazard property (the earliest in this list:
1061  * PROPARALLEL_UNSAFE, PROPARALLEL_RESTRICTED, PROPARALLEL_SAFE) that can
1062  * be found in the given parsetree. We use this to find out whether the query
1063  * can be parallelized at all. The caller will also save the result in
1064  * PlannerGlobal so as to short-circuit checks of portions of the querytree
1065  * later, in the common case where everything is SAFE.
1066  */
1067 char
1069 {
1071 
1072  context.max_hazard = PROPARALLEL_SAFE;
1074  context.safe_param_ids = NIL;
1075  (void) max_parallel_hazard_walker((Node *) parse, &context);
1076  return context.max_hazard;
1077 }
1078 
1079 /*
1080  * is_parallel_safe
1081  * Detect whether the given expr contains only parallel-safe functions
1082  *
1083  * root->glob->maxParallelHazard must previously have been set to the
1084  * result of max_parallel_hazard() on the whole query.
1085  */
1086 bool
1088 {
1090 
1091  /*
1092  * Even if the original querytree contained nothing unsafe, we need to
1093  * search the expression if we have generated any PARAM_EXEC Params while
1094  * planning, because those are parallel-restricted and there might be one
1095  * in this expression. But otherwise we don't need to look.
1096  */
1097  if (root->glob->maxParallelHazard == PROPARALLEL_SAFE &&
1098  root->glob->nParamExec == 0)
1099  return true;
1100  /* Else use max_parallel_hazard's search logic, but stop on RESTRICTED */
1101  context.max_hazard = PROPARALLEL_SAFE;
1103  context.safe_param_ids = NIL;
1104  return !max_parallel_hazard_walker(node, &context);
1105 }
1106 
1107 /* core logic for all parallel-hazard checks */
1108 static bool
1110 {
1111  switch (proparallel)
1112  {
1113  case PROPARALLEL_SAFE:
1114  /* nothing to see here, move along */
1115  break;
1117  /* increase max_hazard to RESTRICTED */
1118  Assert(context->max_hazard != PROPARALLEL_UNSAFE);
1119  context->max_hazard = proparallel;
1120  /* done if we are not expecting any unsafe functions */
1121  if (context->max_interesting == proparallel)
1122  return true;
1123  break;
1124  case PROPARALLEL_UNSAFE:
1125  context->max_hazard = proparallel;
1126  /* we're always done at the first unsafe construct */
1127  return true;
1128  default:
1129  elog(ERROR, "unrecognized proparallel value \"%c\"", proparallel);
1130  break;
1131  }
1132  return false;
1133 }
1134 
1135 /* check_functions_in_node callback */
1136 static bool
1137 max_parallel_hazard_checker(Oid func_id, void *context)
1138 {
1139  return max_parallel_hazard_test(func_parallel(func_id),
1140  (max_parallel_hazard_context *) context);
1141 }
1142 
1143 static bool
1145 {
1146  if (node == NULL)
1147  return false;
1148 
1149  /* Check for hazardous functions in node itself */
1151  context))
1152  return true;
1153 
1154  /*
1155  * It should be OK to treat MinMaxExpr as parallel-safe, since btree
1156  * opclass support functions are generally parallel-safe. XmlExpr is a
1157  * bit more dubious but we can probably get away with it. We err on the
1158  * side of caution by treating CoerceToDomain as parallel-restricted.
1159  * (Note: in principle that's wrong because a domain constraint could
1160  * contain a parallel-unsafe function; but useful constraints probably
1161  * never would have such, and assuming they do would cripple use of
1162  * parallel query in the presence of domain types.) SQLValueFunction
1163  * should be safe in all cases. NextValueExpr is parallel-unsafe.
1164  */
1165  if (IsA(node, CoerceToDomain))
1166  {
1168  return true;
1169  }
1170 
1171  if (IsA(node, NextValueExpr))
1172  {
1174  return true;
1175  }
1176 
1177  /*
1178  * As a notational convenience for callers, look through RestrictInfo.
1179  */
1180  else if (IsA(node, RestrictInfo))
1181  {
1182  RestrictInfo *rinfo = (RestrictInfo *) node;
1183 
1184  return max_parallel_hazard_walker((Node *) rinfo->clause, context);
1185  }
1186 
1187  /*
1188  * Really we should not see SubLink during a max_interesting == restricted
1189  * scan, but if we do, return true.
1190  */
1191  else if (IsA(node, SubLink))
1192  {
1194  return true;
1195  }
1196 
1197  /*
1198  * Only parallel-safe SubPlans can be sent to workers. Within the
1199  * testexpr of the SubPlan, Params representing the output columns of the
1200  * subplan can be treated as parallel-safe, so temporarily add their IDs
1201  * to the safe_param_ids list while examining the testexpr.
1202  */
1203  else if (IsA(node, SubPlan))
1204  {
1205  SubPlan *subplan = (SubPlan *) node;
1206  List *save_safe_param_ids;
1207 
1208  if (!subplan->parallel_safe &&
1210  return true;
1211  save_safe_param_ids = context->safe_param_ids;
1212  context->safe_param_ids = list_concat(list_copy(subplan->paramIds),
1213  context->safe_param_ids);
1214  if (max_parallel_hazard_walker(subplan->testexpr, context))
1215  return true; /* no need to restore safe_param_ids */
1216  context->safe_param_ids = save_safe_param_ids;
1217  /* we must also check args, but no special Param treatment there */
1218  if (max_parallel_hazard_walker((Node *) subplan->args, context))
1219  return true;
1220  /* don't want to recurse normally, so we're done */
1221  return false;
1222  }
1223 
1224  /*
1225  * We can't pass Params to workers at the moment either, so they are also
1226  * parallel-restricted, unless they are PARAM_EXEC Params listed in
1227  * safe_param_ids, meaning they could be generated within the worker.
1228  */
1229  else if (IsA(node, Param))
1230  {
1231  Param *param = (Param *) node;
1232 
1233  if (param->paramkind != PARAM_EXEC ||
1234  !list_member_int(context->safe_param_ids, param->paramid))
1235  {
1237  return true;
1238  }
1239  return false; /* nothing to recurse to */
1240  }
1241 
1242  /*
1243  * When we're first invoked on a completely unplanned tree, we must
1244  * recurse into subqueries so to as to locate parallel-unsafe constructs
1245  * anywhere in the tree.
1246  */
1247  else if (IsA(node, Query))
1248  {
1249  Query *query = (Query *) node;
1250 
1251  /* SELECT FOR UPDATE/SHARE must be treated as unsafe */
1252  if (query->rowMarks != NULL)
1253  {
1254  context->max_hazard = PROPARALLEL_UNSAFE;
1255  return true;
1256  }
1257 
1258  /* Recurse into subselects */
1259  return query_tree_walker(query,
1261  context, 0);
1262  }
1263 
1264  /* Recurse to check arguments */
1265  return expression_tree_walker(node,
1267  context);
1268 }
1269 
1270 
1271 /*****************************************************************************
1272  * Check clauses for nonstrict functions
1273  *****************************************************************************/
1274 
1275 /*
1276  * contain_nonstrict_functions
1277  * Recursively search for nonstrict functions within a clause.
1278  *
1279  * Returns true if any nonstrict construct is found --- ie, anything that
1280  * could produce non-NULL output with a NULL input.
1281  *
1282  * The idea here is that the caller has verified that the expression contains
1283  * one or more Var or Param nodes (as appropriate for the caller's need), and
1284  * now wishes to prove that the expression result will be NULL if any of these
1285  * inputs is NULL. If we return false, then the proof succeeded.
1286  */
1287 bool
1289 {
1290  return contain_nonstrict_functions_walker(clause, NULL);
1291 }
1292 
1293 static bool
1295 {
1296  return !func_strict(func_id);
1297 }
1298 
1299 static bool
1301 {
1302  if (node == NULL)
1303  return false;
1304  if (IsA(node, Aggref))
1305  {
1306  /* an aggregate could return non-null with null input */
1307  return true;
1308  }
1309  if (IsA(node, GroupingFunc))
1310  {
1311  /*
1312  * A GroupingFunc doesn't evaluate its arguments, and therefore must
1313  * be treated as nonstrict.
1314  */
1315  return true;
1316  }
1317  if (IsA(node, WindowFunc))
1318  {
1319  /* a window function could return non-null with null input */
1320  return true;
1321  }
1322  if (IsA(node, ArrayRef))
1323  {
1324  /* array assignment is nonstrict, but subscripting is strict */
1325  if (((ArrayRef *) node)->refassgnexpr != NULL)
1326  return true;
1327  /* else fall through to check args */
1328  }
1329  if (IsA(node, DistinctExpr))
1330  {
1331  /* IS DISTINCT FROM is inherently non-strict */
1332  return true;
1333  }
1334  if (IsA(node, NullIfExpr))
1335  {
1336  /* NULLIF is inherently non-strict */
1337  return true;
1338  }
1339  if (IsA(node, BoolExpr))
1340  {
1341  BoolExpr *expr = (BoolExpr *) node;
1342 
1343  switch (expr->boolop)
1344  {
1345  case AND_EXPR:
1346  case OR_EXPR:
1347  /* AND, OR are inherently non-strict */
1348  return true;
1349  default:
1350  break;
1351  }
1352  }
1353  if (IsA(node, SubLink))
1354  {
1355  /* In some cases a sublink might be strict, but in general not */
1356  return true;
1357  }
1358  if (IsA(node, SubPlan))
1359  return true;
1360  if (IsA(node, AlternativeSubPlan))
1361  return true;
1362  if (IsA(node, FieldStore))
1363  return true;
1364  if (IsA(node, CaseExpr))
1365  return true;
1366  if (IsA(node, ArrayExpr))
1367  return true;
1368  if (IsA(node, RowExpr))
1369  return true;
1370  if (IsA(node, RowCompareExpr))
1371  return true;
1372  if (IsA(node, CoalesceExpr))
1373  return true;
1374  if (IsA(node, MinMaxExpr))
1375  return true;
1376  if (IsA(node, XmlExpr))
1377  return true;
1378  if (IsA(node, NullTest))
1379  return true;
1380  if (IsA(node, BooleanTest))
1381  return true;
1382 
1383  /*
1384  * Check other function-containing nodes; but ArrayCoerceExpr is strict at
1385  * the array level, regardless of elemfunc.
1386  */
1387  if (!IsA(node, ArrayCoerceExpr) &&
1389  context))
1390  return true;
1392  context);
1393 }
1394 
1395 /*****************************************************************************
1396  * Check clauses for context-dependent nodes
1397  *****************************************************************************/
1398 
1399 /*
1400  * contain_context_dependent_node
1401  * Recursively search for context-dependent nodes within a clause.
1402  *
1403  * CaseTestExpr nodes must appear directly within the corresponding CaseExpr,
1404  * not nested within another one, or they'll see the wrong test value. If one
1405  * appears "bare" in the arguments of a SQL function, then we can't inline the
1406  * SQL function for fear of creating such a situation.
1407  *
1408  * CoerceToDomainValue would have the same issue if domain CHECK expressions
1409  * could get inlined into larger expressions, but presently that's impossible.
1410  * Still, it might be allowed in future, or other node types with similar
1411  * issues might get invented. So give this function a generic name, and set
1412  * up the recursion state to allow multiple flag bits.
1413  */
1414 static bool
1416 {
1417  int flags = 0;
1418 
1419  return contain_context_dependent_node_walker(clause, &flags);
1420 }
1421 
1422 #define CCDN_IN_CASEEXPR 0x0001 /* CaseTestExpr okay here? */
1423 
1424 static bool
1426 {
1427  if (node == NULL)
1428  return false;
1429  if (IsA(node, CaseTestExpr))
1430  return !(*flags & CCDN_IN_CASEEXPR);
1431  if (IsA(node, CaseExpr))
1432  {
1433  CaseExpr *caseexpr = (CaseExpr *) node;
1434 
1435  /*
1436  * If this CASE doesn't have a test expression, then it doesn't create
1437  * a context in which CaseTestExprs should appear, so just fall
1438  * through and treat it as a generic expression node.
1439  */
1440  if (caseexpr->arg)
1441  {
1442  int save_flags = *flags;
1443  bool res;
1444 
1445  /*
1446  * Note: in principle, we could distinguish the various sub-parts
1447  * of a CASE construct and set the flag bit only for some of them,
1448  * since we are only expecting CaseTestExprs to appear in the
1449  * "expr" subtree of the CaseWhen nodes. But it doesn't really
1450  * seem worth any extra code. If there are any bare CaseTestExprs
1451  * elsewhere in the CASE, something's wrong already.
1452  */
1453  *flags |= CCDN_IN_CASEEXPR;
1454  res = expression_tree_walker(node,
1456  (void *) flags);
1457  *flags = save_flags;
1458  return res;
1459  }
1460  }
1462  (void *) flags);
1463 }
1464 
1465 /*****************************************************************************
1466  * Check clauses for Vars passed to non-leakproof functions
1467  *****************************************************************************/
1468 
1469 /*
1470  * contain_leaked_vars
1471  * Recursively scan a clause to discover whether it contains any Var
1472  * nodes (of the current query level) that are passed as arguments to
1473  * leaky functions.
1474  *
1475  * Returns true if the clause contains any non-leakproof functions that are
1476  * passed Var nodes of the current query level, and which might therefore leak
1477  * data. Such clauses must be applied after any lower-level security barrier
1478  * clauses.
1479  */
1480 bool
1482 {
1483  return contain_leaked_vars_walker(clause, NULL);
1484 }
1485 
1486 static bool
1487 contain_leaked_vars_checker(Oid func_id, void *context)
1488 {
1489  return !get_func_leakproof(func_id);
1490 }
1491 
1492 static bool
1493 contain_leaked_vars_walker(Node *node, void *context)
1494 {
1495  if (node == NULL)
1496  return false;
1497 
1498  switch (nodeTag(node))
1499  {
1500  case T_Var:
1501  case T_Const:
1502  case T_Param:
1503  case T_ArrayRef:
1504  case T_ArrayExpr:
1505  case T_FieldSelect:
1506  case T_FieldStore:
1507  case T_NamedArgExpr:
1508  case T_BoolExpr:
1509  case T_RelabelType:
1510  case T_CollateExpr:
1511  case T_CaseExpr:
1512  case T_CaseTestExpr:
1513  case T_RowExpr:
1514  case T_MinMaxExpr:
1515  case T_SQLValueFunction:
1516  case T_NullTest:
1517  case T_BooleanTest:
1518  case T_NextValueExpr:
1519  case T_List:
1520 
1521  /*
1522  * We know these node types don't contain function calls; but
1523  * something further down in the node tree might.
1524  */
1525  break;
1526 
1527  case T_FuncExpr:
1528  case T_OpExpr:
1529  case T_DistinctExpr:
1530  case T_NullIfExpr:
1531  case T_ScalarArrayOpExpr:
1532  case T_CoerceViaIO:
1533  case T_ArrayCoerceExpr:
1534 
1535  /*
1536  * If node contains a leaky function call, and there's any Var
1537  * underneath it, reject.
1538  */
1540  context) &&
1541  contain_var_clause(node))
1542  return true;
1543  break;
1544 
1545  case T_RowCompareExpr:
1546  {
1547  /*
1548  * It's worth special-casing this because a leaky comparison
1549  * function only compromises one pair of row elements, which
1550  * might not contain Vars while others do.
1551  */
1552  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1553  ListCell *opid;
1554  ListCell *larg;
1555  ListCell *rarg;
1556 
1557  forthree(opid, rcexpr->opnos,
1558  larg, rcexpr->largs,
1559  rarg, rcexpr->rargs)
1560  {
1561  Oid funcid = get_opcode(lfirst_oid(opid));
1562 
1563  if (!get_func_leakproof(funcid) &&
1564  (contain_var_clause((Node *) lfirst(larg)) ||
1565  contain_var_clause((Node *) lfirst(rarg))))
1566  return true;
1567  }
1568  }
1569  break;
1570 
1571  case T_CurrentOfExpr:
1572 
1573  /*
1574  * WHERE CURRENT OF doesn't contain leaky function calls.
1575  * Moreover, it is essential that this is considered non-leaky,
1576  * since the planner must always generate a TID scan when CURRENT
1577  * OF is present -- c.f. cost_tidscan.
1578  */
1579  return false;
1580 
1581  default:
1582 
1583  /*
1584  * If we don't recognize the node tag, assume it might be leaky.
1585  * This prevents an unexpected security hole if someone adds a new
1586  * node type that can call a function.
1587  */
1588  return true;
1589  }
1591  context);
1592 }
1593 
1594 /*
1595  * find_nonnullable_rels
1596  * Determine which base rels are forced nonnullable by given clause.
1597  *
1598  * Returns the set of all Relids that are referenced in the clause in such
1599  * a way that the clause cannot possibly return TRUE if any of these Relids
1600  * is an all-NULL row. (It is OK to err on the side of conservatism; hence
1601  * the analysis here is simplistic.)
1602  *
1603  * The semantics here are subtly different from contain_nonstrict_functions:
1604  * that function is concerned with NULL results from arbitrary expressions,
1605  * but here we assume that the input is a Boolean expression, and wish to
1606  * see if NULL inputs will provably cause a FALSE-or-NULL result. We expect
1607  * the expression to have been AND/OR flattened and converted to implicit-AND
1608  * format.
1609  *
1610  * Note: this function is largely duplicative of find_nonnullable_vars().
1611  * The reason not to simplify this function into a thin wrapper around
1612  * find_nonnullable_vars() is that the tested conditions really are different:
1613  * a clause like "t1.v1 IS NOT NULL OR t1.v2 IS NOT NULL" does not prove
1614  * that either v1 or v2 can't be NULL, but it does prove that the t1 row
1615  * as a whole can't be all-NULL.
1616  *
1617  * top_level is TRUE while scanning top-level AND/OR structure; here, showing
1618  * the result is either FALSE or NULL is good enough. top_level is FALSE when
1619  * we have descended below a NOT or a strict function: now we must be able to
1620  * prove that the subexpression goes to NULL.
1621  *
1622  * We don't use expression_tree_walker here because we don't want to descend
1623  * through very many kinds of nodes; only the ones we can be sure are strict.
1624  */
1625 Relids
1627 {
1628  return find_nonnullable_rels_walker(clause, true);
1629 }
1630 
1631 static Relids
1632 find_nonnullable_rels_walker(Node *node, bool top_level)
1633 {
1634  Relids result = NULL;
1635  ListCell *l;
1636 
1637  if (node == NULL)
1638  return NULL;
1639  if (IsA(node, Var))
1640  {
1641  Var *var = (Var *) node;
1642 
1643  if (var->varlevelsup == 0)
1644  result = bms_make_singleton(var->varno);
1645  }
1646  else if (IsA(node, List))
1647  {
1648  /*
1649  * At top level, we are examining an implicit-AND list: if any of the
1650  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1651  * not at top level, we are examining the arguments of a strict
1652  * function: if any of them produce NULL then the result of the
1653  * function must be NULL. So in both cases, the set of nonnullable
1654  * rels is the union of those found in the arms, and we pass down the
1655  * top_level flag unmodified.
1656  */
1657  foreach(l, (List *) node)
1658  {
1659  result = bms_join(result,
1661  top_level));
1662  }
1663  }
1664  else if (IsA(node, FuncExpr))
1665  {
1666  FuncExpr *expr = (FuncExpr *) node;
1667 
1668  if (func_strict(expr->funcid))
1669  result = find_nonnullable_rels_walker((Node *) expr->args, false);
1670  }
1671  else if (IsA(node, OpExpr))
1672  {
1673  OpExpr *expr = (OpExpr *) node;
1674 
1675  set_opfuncid(expr);
1676  if (func_strict(expr->opfuncid))
1677  result = find_nonnullable_rels_walker((Node *) expr->args, false);
1678  }
1679  else if (IsA(node, ScalarArrayOpExpr))
1680  {
1681  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1682 
1683  if (is_strict_saop(expr, true))
1684  result = find_nonnullable_rels_walker((Node *) expr->args, false);
1685  }
1686  else if (IsA(node, BoolExpr))
1687  {
1688  BoolExpr *expr = (BoolExpr *) node;
1689 
1690  switch (expr->boolop)
1691  {
1692  case AND_EXPR:
1693  /* At top level we can just recurse (to the List case) */
1694  if (top_level)
1695  {
1696  result = find_nonnullable_rels_walker((Node *) expr->args,
1697  top_level);
1698  break;
1699  }
1700 
1701  /*
1702  * Below top level, even if one arm produces NULL, the result
1703  * could be FALSE (hence not NULL). However, if *all* the
1704  * arms produce NULL then the result is NULL, so we can take
1705  * the intersection of the sets of nonnullable rels, just as
1706  * for OR. Fall through to share code.
1707  */
1708  /* FALL THRU */
1709  case OR_EXPR:
1710 
1711  /*
1712  * OR is strict if all of its arms are, so we can take the
1713  * intersection of the sets of nonnullable rels for each arm.
1714  * This works for both values of top_level.
1715  */
1716  foreach(l, expr->args)
1717  {
1718  Relids subresult;
1719 
1720  subresult = find_nonnullable_rels_walker(lfirst(l),
1721  top_level);
1722  if (result == NULL) /* first subresult? */
1723  result = subresult;
1724  else
1725  result = bms_int_members(result, subresult);
1726 
1727  /*
1728  * If the intersection is empty, we can stop looking. This
1729  * also justifies the test for first-subresult above.
1730  */
1731  if (bms_is_empty(result))
1732  break;
1733  }
1734  break;
1735  case NOT_EXPR:
1736  /* NOT will return null if its arg is null */
1737  result = find_nonnullable_rels_walker((Node *) expr->args,
1738  false);
1739  break;
1740  default:
1741  elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1742  break;
1743  }
1744  }
1745  else if (IsA(node, RelabelType))
1746  {
1747  RelabelType *expr = (RelabelType *) node;
1748 
1749  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1750  }
1751  else if (IsA(node, CoerceViaIO))
1752  {
1753  /* not clear this is useful, but it can't hurt */
1754  CoerceViaIO *expr = (CoerceViaIO *) node;
1755 
1756  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1757  }
1758  else if (IsA(node, ArrayCoerceExpr))
1759  {
1760  /* ArrayCoerceExpr is strict at the array level */
1761  ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1762 
1763  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1764  }
1765  else if (IsA(node, ConvertRowtypeExpr))
1766  {
1767  /* not clear this is useful, but it can't hurt */
1768  ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1769 
1770  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1771  }
1772  else if (IsA(node, CollateExpr))
1773  {
1774  CollateExpr *expr = (CollateExpr *) node;
1775 
1776  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1777  }
1778  else if (IsA(node, NullTest))
1779  {
1780  /* IS NOT NULL can be considered strict, but only at top level */
1781  NullTest *expr = (NullTest *) node;
1782 
1783  if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
1784  result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1785  }
1786  else if (IsA(node, BooleanTest))
1787  {
1788  /* Boolean tests that reject NULL are strict at top level */
1789  BooleanTest *expr = (BooleanTest *) node;
1790 
1791  if (top_level &&
1792  (expr->booltesttype == IS_TRUE ||
1793  expr->booltesttype == IS_FALSE ||
1794  expr->booltesttype == IS_NOT_UNKNOWN))
1795  result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1796  }
1797  else if (IsA(node, PlaceHolderVar))
1798  {
1799  PlaceHolderVar *phv = (PlaceHolderVar *) node;
1800 
1801  result = find_nonnullable_rels_walker((Node *) phv->phexpr, top_level);
1802  }
1803  return result;
1804 }
1805 
1806 /*
1807  * find_nonnullable_vars
1808  * Determine which Vars are forced nonnullable by given clause.
1809  *
1810  * Returns a list of all level-zero Vars that are referenced in the clause in
1811  * such a way that the clause cannot possibly return TRUE if any of these Vars
1812  * is NULL. (It is OK to err on the side of conservatism; hence the analysis
1813  * here is simplistic.)
1814  *
1815  * The semantics here are subtly different from contain_nonstrict_functions:
1816  * that function is concerned with NULL results from arbitrary expressions,
1817  * but here we assume that the input is a Boolean expression, and wish to
1818  * see if NULL inputs will provably cause a FALSE-or-NULL result. We expect
1819  * the expression to have been AND/OR flattened and converted to implicit-AND
1820  * format.
1821  *
1822  * The result is a palloc'd List, but we have not copied the member Var nodes.
1823  * Also, we don't bother trying to eliminate duplicate entries.
1824  *
1825  * top_level is TRUE while scanning top-level AND/OR structure; here, showing
1826  * the result is either FALSE or NULL is good enough. top_level is FALSE when
1827  * we have descended below a NOT or a strict function: now we must be able to
1828  * prove that the subexpression goes to NULL.
1829  *
1830  * We don't use expression_tree_walker here because we don't want to descend
1831  * through very many kinds of nodes; only the ones we can be sure are strict.
1832  */
1833 List *
1835 {
1836  return find_nonnullable_vars_walker(clause, true);
1837 }
1838 
1839 static List *
1840 find_nonnullable_vars_walker(Node *node, bool top_level)
1841 {
1842  List *result = NIL;
1843  ListCell *l;
1844 
1845  if (node == NULL)
1846  return NIL;
1847  if (IsA(node, Var))
1848  {
1849  Var *var = (Var *) node;
1850 
1851  if (var->varlevelsup == 0)
1852  result = list_make1(var);
1853  }
1854  else if (IsA(node, List))
1855  {
1856  /*
1857  * At top level, we are examining an implicit-AND list: if any of the
1858  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1859  * not at top level, we are examining the arguments of a strict
1860  * function: if any of them produce NULL then the result of the
1861  * function must be NULL. So in both cases, the set of nonnullable
1862  * vars is the union of those found in the arms, and we pass down the
1863  * top_level flag unmodified.
1864  */
1865  foreach(l, (List *) node)
1866  {
1867  result = list_concat(result,
1869  top_level));
1870  }
1871  }
1872  else if (IsA(node, FuncExpr))
1873  {
1874  FuncExpr *expr = (FuncExpr *) node;
1875 
1876  if (func_strict(expr->funcid))
1877  result = find_nonnullable_vars_walker((Node *) expr->args, false);
1878  }
1879  else if (IsA(node, OpExpr))
1880  {
1881  OpExpr *expr = (OpExpr *) node;
1882 
1883  set_opfuncid(expr);
1884  if (func_strict(expr->opfuncid))
1885  result = find_nonnullable_vars_walker((Node *) expr->args, false);
1886  }
1887  else if (IsA(node, ScalarArrayOpExpr))
1888  {
1889  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1890 
1891  if (is_strict_saop(expr, true))
1892  result = find_nonnullable_vars_walker((Node *) expr->args, false);
1893  }
1894  else if (IsA(node, BoolExpr))
1895  {
1896  BoolExpr *expr = (BoolExpr *) node;
1897 
1898  switch (expr->boolop)
1899  {
1900  case AND_EXPR:
1901  /* At top level we can just recurse (to the List case) */
1902  if (top_level)
1903  {
1904  result = find_nonnullable_vars_walker((Node *) expr->args,
1905  top_level);
1906  break;
1907  }
1908 
1909  /*
1910  * Below top level, even if one arm produces NULL, the result
1911  * could be FALSE (hence not NULL). However, if *all* the
1912  * arms produce NULL then the result is NULL, so we can take
1913  * the intersection of the sets of nonnullable vars, just as
1914  * for OR. Fall through to share code.
1915  */
1916  /* FALL THRU */
1917  case OR_EXPR:
1918 
1919  /*
1920  * OR is strict if all of its arms are, so we can take the
1921  * intersection of the sets of nonnullable vars for each arm.
1922  * This works for both values of top_level.
1923  */
1924  foreach(l, expr->args)
1925  {
1926  List *subresult;
1927 
1928  subresult = find_nonnullable_vars_walker(lfirst(l),
1929  top_level);
1930  if (result == NIL) /* first subresult? */
1931  result = subresult;
1932  else
1933  result = list_intersection(result, subresult);
1934 
1935  /*
1936  * If the intersection is empty, we can stop looking. This
1937  * also justifies the test for first-subresult above.
1938  */
1939  if (result == NIL)
1940  break;
1941  }
1942  break;
1943  case NOT_EXPR:
1944  /* NOT will return null if its arg is null */
1945  result = find_nonnullable_vars_walker((Node *) expr->args,
1946  false);
1947  break;
1948  default:
1949  elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1950  break;
1951  }
1952  }
1953  else if (IsA(node, RelabelType))
1954  {
1955  RelabelType *expr = (RelabelType *) node;
1956 
1957  result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1958  }
1959  else if (IsA(node, CoerceViaIO))
1960  {
1961  /* not clear this is useful, but it can't hurt */
1962  CoerceViaIO *expr = (CoerceViaIO *) node;
1963 
1964  result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1965  }
1966  else if (IsA(node, ArrayCoerceExpr))
1967  {
1968  /* ArrayCoerceExpr is strict at the array level */
1969  ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1970 
1971  result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1972  }
1973  else if (IsA(node, ConvertRowtypeExpr))
1974  {
1975  /* not clear this is useful, but it can't hurt */
1976  ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1977 
1978  result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1979  }
1980  else if (IsA(node, CollateExpr))
1981  {
1982  CollateExpr *expr = (CollateExpr *) node;
1983 
1984  result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1985  }
1986  else if (IsA(node, NullTest))
1987  {
1988  /* IS NOT NULL can be considered strict, but only at top level */
1989  NullTest *expr = (NullTest *) node;
1990 
1991  if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
1992  result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1993  }
1994  else if (IsA(node, BooleanTest))
1995  {
1996  /* Boolean tests that reject NULL are strict at top level */
1997  BooleanTest *expr = (BooleanTest *) node;
1998 
1999  if (top_level &&
2000  (expr->booltesttype == IS_TRUE ||
2001  expr->booltesttype == IS_FALSE ||
2002  expr->booltesttype == IS_NOT_UNKNOWN))
2003  result = find_nonnullable_vars_walker((Node *) expr->arg, false);
2004  }
2005  else if (IsA(node, PlaceHolderVar))
2006  {
2007  PlaceHolderVar *phv = (PlaceHolderVar *) node;
2008 
2009  result = find_nonnullable_vars_walker((Node *) phv->phexpr, top_level);
2010  }
2011  return result;
2012 }
2013 
2014 /*
2015  * find_forced_null_vars
2016  * Determine which Vars must be NULL for the given clause to return TRUE.
2017  *
2018  * This is the complement of find_nonnullable_vars: find the level-zero Vars
2019  * that must be NULL for the clause to return TRUE. (It is OK to err on the
2020  * side of conservatism; hence the analysis here is simplistic. In fact,
2021  * we only detect simple "var IS NULL" tests at the top level.)
2022  *
2023  * The result is a palloc'd List, but we have not copied the member Var nodes.
2024  * Also, we don't bother trying to eliminate duplicate entries.
2025  */
2026 List *
2028 {
2029  List *result = NIL;
2030  Var *var;
2031  ListCell *l;
2032 
2033  if (node == NULL)
2034  return NIL;
2035  /* Check single-clause cases using subroutine */
2036  var = find_forced_null_var(node);
2037  if (var)
2038  {
2039  result = list_make1(var);
2040  }
2041  /* Otherwise, handle AND-conditions */
2042  else if (IsA(node, List))
2043  {
2044  /*
2045  * At top level, we are examining an implicit-AND list: if any of the
2046  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
2047  */
2048  foreach(l, (List *) node)
2049  {
2050  result = list_concat(result,
2052  }
2053  }
2054  else if (IsA(node, BoolExpr))
2055  {
2056  BoolExpr *expr = (BoolExpr *) node;
2057 
2058  /*
2059  * We don't bother considering the OR case, because it's fairly
2060  * unlikely anyone would write "v1 IS NULL OR v1 IS NULL". Likewise,
2061  * the NOT case isn't worth expending code on.
2062  */
2063  if (expr->boolop == AND_EXPR)
2064  {
2065  /* At top level we can just recurse (to the List case) */
2066  result = find_forced_null_vars((Node *) expr->args);
2067  }
2068  }
2069  return result;
2070 }
2071 
2072 /*
2073  * find_forced_null_var
2074  * Return the Var forced null by the given clause, or NULL if it's
2075  * not an IS NULL-type clause. For success, the clause must enforce
2076  * *only* nullness of the particular Var, not any other conditions.
2077  *
2078  * This is just the single-clause case of find_forced_null_vars(), without
2079  * any allowance for AND conditions. It's used by initsplan.c on individual
2080  * qual clauses. The reason for not just applying find_forced_null_vars()
2081  * is that if an AND of an IS NULL clause with something else were to somehow
2082  * survive AND/OR flattening, initsplan.c might get fooled into discarding
2083  * the whole clause when only the IS NULL part of it had been proved redundant.
2084  */
2085 Var *
2087 {
2088  if (node == NULL)
2089  return NULL;
2090  if (IsA(node, NullTest))
2091  {
2092  /* check for var IS NULL */
2093  NullTest *expr = (NullTest *) node;
2094 
2095  if (expr->nulltesttype == IS_NULL && !expr->argisrow)
2096  {
2097  Var *var = (Var *) expr->arg;
2098 
2099  if (var && IsA(var, Var) &&
2100  var->varlevelsup == 0)
2101  return var;
2102  }
2103  }
2104  else if (IsA(node, BooleanTest))
2105  {
2106  /* var IS UNKNOWN is equivalent to var IS NULL */
2107  BooleanTest *expr = (BooleanTest *) node;
2108 
2109  if (expr->booltesttype == IS_UNKNOWN)
2110  {
2111  Var *var = (Var *) expr->arg;
2112 
2113  if (var && IsA(var, Var) &&
2114  var->varlevelsup == 0)
2115  return var;
2116  }
2117  }
2118  return NULL;
2119 }
2120 
2121 /*
2122  * Can we treat a ScalarArrayOpExpr as strict?
2123  *
2124  * If "falseOK" is true, then a "false" result can be considered strict,
2125  * else we need to guarantee an actual NULL result for NULL input.
2126  *
2127  * "foo op ALL array" is strict if the op is strict *and* we can prove
2128  * that the array input isn't an empty array. We can check that
2129  * for the cases of an array constant and an ARRAY[] construct.
2130  *
2131  * "foo op ANY array" is strict in the falseOK sense if the op is strict.
2132  * If not falseOK, the test is the same as for "foo op ALL array".
2133  */
2134 static bool
2135 is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
2136 {
2137  Node *rightop;
2138 
2139  /* The contained operator must be strict. */
2140  set_sa_opfuncid(expr);
2141  if (!func_strict(expr->opfuncid))
2142  return false;
2143  /* If ANY and falseOK, that's all we need to check. */
2144  if (expr->useOr && falseOK)
2145  return true;
2146  /* Else, we have to see if the array is provably non-empty. */
2147  Assert(list_length(expr->args) == 2);
2148  rightop = (Node *) lsecond(expr->args);
2149  if (rightop && IsA(rightop, Const))
2150  {
2151  Datum arraydatum = ((Const *) rightop)->constvalue;
2152  bool arrayisnull = ((Const *) rightop)->constisnull;
2153  ArrayType *arrayval;
2154  int nitems;
2155 
2156  if (arrayisnull)
2157  return false;
2158  arrayval = DatumGetArrayTypeP(arraydatum);
2159  nitems = ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
2160  if (nitems > 0)
2161  return true;
2162  }
2163  else if (rightop && IsA(rightop, ArrayExpr))
2164  {
2165  ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
2166 
2167  if (arrayexpr->elements != NIL && !arrayexpr->multidims)
2168  return true;
2169  }
2170  return false;
2171 }
2172 
2173 
2174 /*****************************************************************************
2175  * Check for "pseudo-constant" clauses
2176  *****************************************************************************/
2177 
2178 /*
2179  * is_pseudo_constant_clause
2180  * Detect whether an expression is "pseudo constant", ie, it contains no
2181  * variables of the current query level and no uses of volatile functions.
2182  * Such an expr is not necessarily a true constant: it can still contain
2183  * Params and outer-level Vars, not to mention functions whose results
2184  * may vary from one statement to the next. However, the expr's value
2185  * will be constant over any one scan of the current query, so it can be
2186  * used as, eg, an indexscan key.
2187  *
2188  * CAUTION: this function omits to test for one very important class of
2189  * not-constant expressions, namely aggregates (Aggrefs). In current usage
2190  * this is only applied to WHERE clauses and so a check for Aggrefs would be
2191  * a waste of cycles; but be sure to also check contain_agg_clause() if you
2192  * want to know about pseudo-constness in other contexts. The same goes
2193  * for window functions (WindowFuncs).
2194  */
2195 bool
2197 {
2198  /*
2199  * We could implement this check in one recursive scan. But since the
2200  * check for volatile functions is both moderately expensive and unlikely
2201  * to fail, it seems better to look for Vars first and only check for
2202  * volatile functions if we find no Vars.
2203  */
2204  if (!contain_var_clause(clause) &&
2205  !contain_volatile_functions(clause))
2206  return true;
2207  return false;
2208 }
2209 
2210 /*
2211  * is_pseudo_constant_clause_relids
2212  * Same as above, except caller already has available the var membership
2213  * of the expression; this lets us avoid the contain_var_clause() scan.
2214  */
2215 bool
2217 {
2218  if (bms_is_empty(relids) &&
2219  !contain_volatile_functions(clause))
2220  return true;
2221  return false;
2222 }
2223 
2224 
2225 /*****************************************************************************
2226  * *
2227  * General clause-manipulating routines *
2228  * *
2229  *****************************************************************************/
2230 
2231 /*
2232  * NumRelids
2233  * (formerly clause_relids)
2234  *
2235  * Returns the number of different relations referenced in 'clause'.
2236  */
2237 int
2238 NumRelids(Node *clause)
2239 {
2240  Relids varnos = pull_varnos(clause);
2241  int result = bms_num_members(varnos);
2242 
2243  bms_free(varnos);
2244  return result;
2245 }
2246 
2247 /*
2248  * CommuteOpExpr: commute a binary operator clause
2249  *
2250  * XXX the clause is destructively modified!
2251  */
2252 void
2254 {
2255  Oid opoid;
2256  Node *temp;
2257 
2258  /* Sanity checks: caller is at fault if these fail */
2259  if (!is_opclause(clause) ||
2260  list_length(clause->args) != 2)
2261  elog(ERROR, "cannot commute non-binary-operator clause");
2262 
2263  opoid = get_commutator(clause->opno);
2264 
2265  if (!OidIsValid(opoid))
2266  elog(ERROR, "could not find commutator for operator %u",
2267  clause->opno);
2268 
2269  /*
2270  * modify the clause in-place!
2271  */
2272  clause->opno = opoid;
2273  clause->opfuncid = InvalidOid;
2274  /* opresulttype, opretset, opcollid, inputcollid need not change */
2275 
2276  temp = linitial(clause->args);
2277  linitial(clause->args) = lsecond(clause->args);
2278  lsecond(clause->args) = temp;
2279 }
2280 
2281 /*
2282  * CommuteRowCompareExpr: commute a RowCompareExpr clause
2283  *
2284  * XXX the clause is destructively modified!
2285  */
2286 void
2288 {
2289  List *newops;
2290  List *temp;
2291  ListCell *l;
2292 
2293  /* Sanity checks: caller is at fault if these fail */
2294  if (!IsA(clause, RowCompareExpr))
2295  elog(ERROR, "expected a RowCompareExpr");
2296 
2297  /* Build list of commuted operators */
2298  newops = NIL;
2299  foreach(l, clause->opnos)
2300  {
2301  Oid opoid = lfirst_oid(l);
2302 
2303  opoid = get_commutator(opoid);
2304  if (!OidIsValid(opoid))
2305  elog(ERROR, "could not find commutator for operator %u",
2306  lfirst_oid(l));
2307  newops = lappend_oid(newops, opoid);
2308  }
2309 
2310  /*
2311  * modify the clause in-place!
2312  */
2313  switch (clause->rctype)
2314  {
2315  case ROWCOMPARE_LT:
2316  clause->rctype = ROWCOMPARE_GT;
2317  break;
2318  case ROWCOMPARE_LE:
2319  clause->rctype = ROWCOMPARE_GE;
2320  break;
2321  case ROWCOMPARE_GE:
2322  clause->rctype = ROWCOMPARE_LE;
2323  break;
2324  case ROWCOMPARE_GT:
2325  clause->rctype = ROWCOMPARE_LT;
2326  break;
2327  default:
2328  elog(ERROR, "unexpected RowCompare type: %d",
2329  (int) clause->rctype);
2330  break;
2331  }
2332 
2333  clause->opnos = newops;
2334 
2335  /*
2336  * Note: we need not change the opfamilies list; we assume any btree
2337  * opfamily containing an operator will also contain its commutator.
2338  * Collations don't change either.
2339  */
2340 
2341  temp = clause->largs;
2342  clause->largs = clause->rargs;
2343  clause->rargs = temp;
2344 }
2345 
2346 /*
2347  * Helper for eval_const_expressions: check that datatype of an attribute
2348  * is still what it was when the expression was parsed. This is needed to
2349  * guard against improper simplification after ALTER COLUMN TYPE. (XXX we
2350  * may well need to make similar checks elsewhere?)
2351  */
2352 static bool
2353 rowtype_field_matches(Oid rowtypeid, int fieldnum,
2354  Oid expectedtype, int32 expectedtypmod,
2355  Oid expectedcollation)
2356 {
2357  TupleDesc tupdesc;
2358  Form_pg_attribute attr;
2359 
2360  /* No issue for RECORD, since there is no way to ALTER such a type */
2361  if (rowtypeid == RECORDOID)
2362  return true;
2363  tupdesc = lookup_rowtype_tupdesc(rowtypeid, -1);
2364  if (fieldnum <= 0 || fieldnum > tupdesc->natts)
2365  {
2366  ReleaseTupleDesc(tupdesc);
2367  return false;
2368  }
2369  attr = TupleDescAttr(tupdesc, fieldnum - 1);
2370  if (attr->attisdropped ||
2371  attr->atttypid != expectedtype ||
2372  attr->atttypmod != expectedtypmod ||
2373  attr->attcollation != expectedcollation)
2374  {
2375  ReleaseTupleDesc(tupdesc);
2376  return false;
2377  }
2378  ReleaseTupleDesc(tupdesc);
2379  return true;
2380 }
2381 
2382 
2383 /*--------------------
2384  * eval_const_expressions
2385  *
2386  * Reduce any recognizably constant subexpressions of the given
2387  * expression tree, for example "2 + 2" => "4". More interestingly,
2388  * we can reduce certain boolean expressions even when they contain
2389  * non-constant subexpressions: "x OR true" => "true" no matter what
2390  * the subexpression x is. (XXX We assume that no such subexpression
2391  * will have important side-effects, which is not necessarily a good
2392  * assumption in the presence of user-defined functions; do we need a
2393  * pg_proc flag that prevents discarding the execution of a function?)
2394  *
2395  * We do understand that certain functions may deliver non-constant
2396  * results even with constant inputs, "nextval()" being the classic
2397  * example. Functions that are not marked "immutable" in pg_proc
2398  * will not be pre-evaluated here, although we will reduce their
2399  * arguments as far as possible.
2400  *
2401  * Whenever a function is eliminated from the expression by means of
2402  * constant-expression evaluation or inlining, we add the function to
2403  * root->glob->invalItems. This ensures the plan is known to depend on
2404  * such functions, even though they aren't referenced anymore.
2405  *
2406  * We assume that the tree has already been type-checked and contains
2407  * only operators and functions that are reasonable to try to execute.
2408  *
2409  * NOTE: "root" can be passed as NULL if the caller never wants to do any
2410  * Param substitutions nor receive info about inlined functions.
2411  *
2412  * NOTE: the planner assumes that this will always flatten nested AND and
2413  * OR clauses into N-argument form. See comments in prepqual.c.
2414  *
2415  * NOTE: another critical effect is that any function calls that require
2416  * default arguments will be expanded, and named-argument calls will be
2417  * converted to positional notation. The executor won't handle either.
2418  *--------------------
2419  */
2420 Node *
2422 {
2424 
2425  if (root)
2426  context.boundParams = root->glob->boundParams; /* bound Params */
2427  else
2428  context.boundParams = NULL;
2429  context.root = root; /* for inlined-function dependencies */
2430  context.active_fns = NIL; /* nothing being recursively simplified */
2431  context.case_val = NULL; /* no CASE being examined */
2432  context.estimate = false; /* safe transformations only */
2433  return eval_const_expressions_mutator(node, &context);
2434 }
2435 
2436 /*--------------------
2437  * estimate_expression_value
2438  *
2439  * This function attempts to estimate the value of an expression for
2440  * planning purposes. It is in essence a more aggressive version of
2441  * eval_const_expressions(): we will perform constant reductions that are
2442  * not necessarily 100% safe, but are reasonable for estimation purposes.
2443  *
2444  * Currently the extra steps that are taken in this mode are:
2445  * 1. Substitute values for Params, where a bound Param value has been made
2446  * available by the caller of planner(), even if the Param isn't marked
2447  * constant. This effectively means that we plan using the first supplied
2448  * value of the Param.
2449  * 2. Fold stable, as well as immutable, functions to constants.
2450  * 3. Reduce PlaceHolderVar nodes to their contained expressions.
2451  *--------------------
2452  */
2453 Node *
2455 {
2457 
2458  context.boundParams = root->glob->boundParams; /* bound Params */
2459  /* we do not need to mark the plan as depending on inlined functions */
2460  context.root = NULL;
2461  context.active_fns = NIL; /* nothing being recursively simplified */
2462  context.case_val = NULL; /* no CASE being examined */
2463  context.estimate = true; /* unsafe transformations OK */
2464  return eval_const_expressions_mutator(node, &context);
2465 }
2466 
2467 static Node *
2470 {
2471  if (node == NULL)
2472  return NULL;
2473  switch (nodeTag(node))
2474  {
2475  case T_Param:
2476  {
2477  Param *param = (Param *) node;
2478 
2479  /* Look to see if we've been given a value for this Param */
2480  if (param->paramkind == PARAM_EXTERN &&
2481  context->boundParams != NULL &&
2482  param->paramid > 0 &&
2483  param->paramid <= context->boundParams->numParams)
2484  {
2485  ParamExternData *prm = &context->boundParams->params[param->paramid - 1];
2486 
2487  if (OidIsValid(prm->ptype))
2488  {
2489  /* OK to substitute parameter value? */
2490  if (context->estimate ||
2491  (prm->pflags & PARAM_FLAG_CONST))
2492  {
2493  /*
2494  * Return a Const representing the param value.
2495  * Must copy pass-by-ref datatypes, since the
2496  * Param might be in a memory context
2497  * shorter-lived than our output plan should be.
2498  */
2499  int16 typLen;
2500  bool typByVal;
2501  Datum pval;
2502 
2503  Assert(prm->ptype == param->paramtype);
2504  get_typlenbyval(param->paramtype,
2505  &typLen, &typByVal);
2506  if (prm->isnull || typByVal)
2507  pval = prm->value;
2508  else
2509  pval = datumCopy(prm->value, typByVal, typLen);
2510  return (Node *) makeConst(param->paramtype,
2511  param->paramtypmod,
2512  param->paramcollid,
2513  (int) typLen,
2514  pval,
2515  prm->isnull,
2516  typByVal);
2517  }
2518  }
2519  }
2520 
2521  /*
2522  * Not replaceable, so just copy the Param (no need to
2523  * recurse)
2524  */
2525  return (Node *) copyObject(param);
2526  }
2527  case T_WindowFunc:
2528  {
2529  WindowFunc *expr = (WindowFunc *) node;
2530  Oid funcid = expr->winfnoid;
2531  List *args;
2532  Expr *aggfilter;
2533  HeapTuple func_tuple;
2534  WindowFunc *newexpr;
2535 
2536  /*
2537  * We can't really simplify a WindowFunc node, but we mustn't
2538  * just fall through to the default processing, because we
2539  * have to apply expand_function_arguments to its argument
2540  * list. That takes care of inserting default arguments and
2541  * expanding named-argument notation.
2542  */
2543  func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
2544  if (!HeapTupleIsValid(func_tuple))
2545  elog(ERROR, "cache lookup failed for function %u", funcid);
2546 
2547  args = expand_function_arguments(expr->args, expr->wintype,
2548  func_tuple);
2549 
2550  ReleaseSysCache(func_tuple);
2551 
2552  /* Now, recursively simplify the args (which are a List) */
2553  args = (List *)
2556  (void *) context);
2557  /* ... and the filter expression, which isn't */
2558  aggfilter = (Expr *)
2560  context);
2561 
2562  /* And build the replacement WindowFunc node */
2563  newexpr = makeNode(WindowFunc);
2564  newexpr->winfnoid = expr->winfnoid;
2565  newexpr->wintype = expr->wintype;
2566  newexpr->wincollid = expr->wincollid;
2567  newexpr->inputcollid = expr->inputcollid;
2568  newexpr->args = args;
2569  newexpr->aggfilter = aggfilter;
2570  newexpr->winref = expr->winref;
2571  newexpr->winstar = expr->winstar;
2572  newexpr->winagg = expr->winagg;
2573  newexpr->location = expr->location;
2574 
2575  return (Node *) newexpr;
2576  }
2577  case T_FuncExpr:
2578  {
2579  FuncExpr *expr = (FuncExpr *) node;
2580  List *args = expr->args;
2581  Expr *simple;
2582  FuncExpr *newexpr;
2583 
2584  /*
2585  * Code for op/func reduction is pretty bulky, so split it out
2586  * as a separate function. Note: exprTypmod normally returns
2587  * -1 for a FuncExpr, but not when the node is recognizably a
2588  * length coercion; we want to preserve the typmod in the
2589  * eventual Const if so.
2590  */
2591  simple = simplify_function(expr->funcid,
2592  expr->funcresulttype,
2593  exprTypmod(node),
2594  expr->funccollid,
2595  expr->inputcollid,
2596  &args,
2597  expr->funcvariadic,
2598  true,
2599  true,
2600  context);
2601  if (simple) /* successfully simplified it */
2602  return (Node *) simple;
2603 
2604  /*
2605  * The expression cannot be simplified any further, so build
2606  * and return a replacement FuncExpr node using the
2607  * possibly-simplified arguments. Note that we have also
2608  * converted the argument list to positional notation.
2609  */
2610  newexpr = makeNode(FuncExpr);
2611  newexpr->funcid = expr->funcid;
2612  newexpr->funcresulttype = expr->funcresulttype;
2613  newexpr->funcretset = expr->funcretset;
2614  newexpr->funcvariadic = expr->funcvariadic;
2615  newexpr->funcformat = expr->funcformat;
2616  newexpr->funccollid = expr->funccollid;
2617  newexpr->inputcollid = expr->inputcollid;
2618  newexpr->args = args;
2619  newexpr->location = expr->location;
2620  return (Node *) newexpr;
2621  }
2622  case T_OpExpr:
2623  {
2624  OpExpr *expr = (OpExpr *) node;
2625  List *args = expr->args;
2626  Expr *simple;
2627  OpExpr *newexpr;
2628 
2629  /*
2630  * Need to get OID of underlying function. Okay to scribble
2631  * on input to this extent.
2632  */
2633  set_opfuncid(expr);
2634 
2635  /*
2636  * Code for op/func reduction is pretty bulky, so split it out
2637  * as a separate function.
2638  */
2639  simple = simplify_function(expr->opfuncid,
2640  expr->opresulttype, -1,
2641  expr->opcollid,
2642  expr->inputcollid,
2643  &args,
2644  false,
2645  true,
2646  true,
2647  context);
2648  if (simple) /* successfully simplified it */
2649  return (Node *) simple;
2650 
2651  /*
2652  * If the operator is boolean equality or inequality, we know
2653  * how to simplify cases involving one constant and one
2654  * non-constant argument.
2655  */
2656  if (expr->opno == BooleanEqualOperator ||
2657  expr->opno == BooleanNotEqualOperator)
2658  {
2659  simple = (Expr *) simplify_boolean_equality(expr->opno,
2660  args);
2661  if (simple) /* successfully simplified it */
2662  return (Node *) simple;
2663  }
2664 
2665  /*
2666  * The expression cannot be simplified any further, so build
2667  * and return a replacement OpExpr node using the
2668  * possibly-simplified arguments.
2669  */
2670  newexpr = makeNode(OpExpr);
2671  newexpr->opno = expr->opno;
2672  newexpr->opfuncid = expr->opfuncid;
2673  newexpr->opresulttype = expr->opresulttype;
2674  newexpr->opretset = expr->opretset;
2675  newexpr->opcollid = expr->opcollid;
2676  newexpr->inputcollid = expr->inputcollid;
2677  newexpr->args = args;
2678  newexpr->location = expr->location;
2679  return (Node *) newexpr;
2680  }
2681  case T_DistinctExpr:
2682  {
2683  DistinctExpr *expr = (DistinctExpr *) node;
2684  List *args;
2685  ListCell *arg;
2686  bool has_null_input = false;
2687  bool all_null_input = true;
2688  bool has_nonconst_input = false;
2689  Expr *simple;
2690  DistinctExpr *newexpr;
2691 
2692  /*
2693  * Reduce constants in the DistinctExpr's arguments. We know
2694  * args is either NIL or a List node, so we can call
2695  * expression_tree_mutator directly rather than recursing to
2696  * self.
2697  */
2698  args = (List *) expression_tree_mutator((Node *) expr->args,
2700  (void *) context);
2701 
2702  /*
2703  * We must do our own check for NULLs because DistinctExpr has
2704  * different results for NULL input than the underlying
2705  * operator does.
2706  */
2707  foreach(arg, args)
2708  {
2709  if (IsA(lfirst(arg), Const))
2710  {
2711  has_null_input |= ((Const *) lfirst(arg))->constisnull;
2712  all_null_input &= ((Const *) lfirst(arg))->constisnull;
2713  }
2714  else
2715  has_nonconst_input = true;
2716  }
2717 
2718  /* all constants? then can optimize this out */
2719  if (!has_nonconst_input)
2720  {
2721  /* all nulls? then not distinct */
2722  if (all_null_input)
2723  return makeBoolConst(false, false);
2724 
2725  /* one null? then distinct */
2726  if (has_null_input)
2727  return makeBoolConst(true, false);
2728 
2729  /* otherwise try to evaluate the '=' operator */
2730  /* (NOT okay to try to inline it, though!) */
2731 
2732  /*
2733  * Need to get OID of underlying function. Okay to
2734  * scribble on input to this extent.
2735  */
2736  set_opfuncid((OpExpr *) expr); /* rely on struct
2737  * equivalence */
2738 
2739  /*
2740  * Code for op/func reduction is pretty bulky, so split it
2741  * out as a separate function.
2742  */
2743  simple = simplify_function(expr->opfuncid,
2744  expr->opresulttype, -1,
2745  expr->opcollid,
2746  expr->inputcollid,
2747  &args,
2748  false,
2749  false,
2750  false,
2751  context);
2752  if (simple) /* successfully simplified it */
2753  {
2754  /*
2755  * Since the underlying operator is "=", must negate
2756  * its result
2757  */
2758  Const *csimple = castNode(Const, simple);
2759 
2760  csimple->constvalue =
2761  BoolGetDatum(!DatumGetBool(csimple->constvalue));
2762  return (Node *) csimple;
2763  }
2764  }
2765 
2766  /*
2767  * The expression cannot be simplified any further, so build
2768  * and return a replacement DistinctExpr node using the
2769  * possibly-simplified arguments.
2770  */
2771  newexpr = makeNode(DistinctExpr);
2772  newexpr->opno = expr->opno;
2773  newexpr->opfuncid = expr->opfuncid;
2774  newexpr->opresulttype = expr->opresulttype;
2775  newexpr->opretset = expr->opretset;
2776  newexpr->opcollid = expr->opcollid;
2777  newexpr->inputcollid = expr->inputcollid;
2778  newexpr->args = args;
2779  newexpr->location = expr->location;
2780  return (Node *) newexpr;
2781  }
2782  case T_BoolExpr:
2783  {
2784  BoolExpr *expr = (BoolExpr *) node;
2785 
2786  switch (expr->boolop)
2787  {
2788  case OR_EXPR:
2789  {
2790  List *newargs;
2791  bool haveNull = false;
2792  bool forceTrue = false;
2793 
2794  newargs = simplify_or_arguments(expr->args,
2795  context,
2796  &haveNull,
2797  &forceTrue);
2798  if (forceTrue)
2799  return makeBoolConst(true, false);
2800  if (haveNull)
2801  newargs = lappend(newargs,
2802  makeBoolConst(false, true));
2803  /* If all the inputs are FALSE, result is FALSE */
2804  if (newargs == NIL)
2805  return makeBoolConst(false, false);
2806 
2807  /*
2808  * If only one nonconst-or-NULL input, it's the
2809  * result
2810  */
2811  if (list_length(newargs) == 1)
2812  return (Node *) linitial(newargs);
2813  /* Else we still need an OR node */
2814  return (Node *) make_orclause(newargs);
2815  }
2816  case AND_EXPR:
2817  {
2818  List *newargs;
2819  bool haveNull = false;
2820  bool forceFalse = false;
2821 
2822  newargs = simplify_and_arguments(expr->args,
2823  context,
2824  &haveNull,
2825  &forceFalse);
2826  if (forceFalse)
2827  return makeBoolConst(false, false);
2828  if (haveNull)
2829  newargs = lappend(newargs,
2830  makeBoolConst(false, true));
2831  /* If all the inputs are TRUE, result is TRUE */
2832  if (newargs == NIL)
2833  return makeBoolConst(true, false);
2834 
2835  /*
2836  * If only one nonconst-or-NULL input, it's the
2837  * result
2838  */
2839  if (list_length(newargs) == 1)
2840  return (Node *) linitial(newargs);
2841  /* Else we still need an AND node */
2842  return (Node *) make_andclause(newargs);
2843  }
2844  case NOT_EXPR:
2845  {
2846  Node *arg;
2847 
2848  Assert(list_length(expr->args) == 1);
2850  context);
2851 
2852  /*
2853  * Use negate_clause() to see if we can simplify
2854  * away the NOT.
2855  */
2856  return negate_clause(arg);
2857  }
2858  default:
2859  elog(ERROR, "unrecognized boolop: %d",
2860  (int) expr->boolop);
2861  break;
2862  }
2863  break;
2864  }
2865  case T_SubPlan:
2866  case T_AlternativeSubPlan:
2867 
2868  /*
2869  * Return a SubPlan unchanged --- too late to do anything with it.
2870  *
2871  * XXX should we ereport() here instead? Probably this routine
2872  * should never be invoked after SubPlan creation.
2873  */
2874  return node;
2875  case T_RelabelType:
2876  {
2877  /*
2878  * If we can simplify the input to a constant, then we don't
2879  * need the RelabelType node anymore: just change the type
2880  * field of the Const node. Otherwise, must copy the
2881  * RelabelType node.
2882  */
2883  RelabelType *relabel = (RelabelType *) node;
2884  Node *arg;
2885 
2886  arg = eval_const_expressions_mutator((Node *) relabel->arg,
2887  context);
2888 
2889  /*
2890  * If we find stacked RelabelTypes (eg, from foo :: int ::
2891  * oid) we can discard all but the top one.
2892  */
2893  while (arg && IsA(arg, RelabelType))
2894  arg = (Node *) ((RelabelType *) arg)->arg;
2895 
2896  if (arg && IsA(arg, Const))
2897  {
2898  Const *con = (Const *) arg;
2899 
2900  con->consttype = relabel->resulttype;
2901  con->consttypmod = relabel->resulttypmod;
2902  con->constcollid = relabel->resultcollid;
2903  return (Node *) con;
2904  }
2905  else
2906  {
2907  RelabelType *newrelabel = makeNode(RelabelType);
2908 
2909  newrelabel->arg = (Expr *) arg;
2910  newrelabel->resulttype = relabel->resulttype;
2911  newrelabel->resulttypmod = relabel->resulttypmod;
2912  newrelabel->resultcollid = relabel->resultcollid;
2913  newrelabel->relabelformat = relabel->relabelformat;
2914  newrelabel->location = relabel->location;
2915  return (Node *) newrelabel;
2916  }
2917  }
2918  case T_CoerceViaIO:
2919  {
2920  CoerceViaIO *expr = (CoerceViaIO *) node;
2921  List *args;
2922  Oid outfunc;
2923  bool outtypisvarlena;
2924  Oid infunc;
2925  Oid intypioparam;
2926  Expr *simple;
2927  CoerceViaIO *newexpr;
2928 
2929  /* Make a List so we can use simplify_function */
2930  args = list_make1(expr->arg);
2931 
2932  /*
2933  * CoerceViaIO represents calling the source type's output
2934  * function then the result type's input function. So, try to
2935  * simplify it as though it were a stack of two such function
2936  * calls. First we need to know what the functions are.
2937  *
2938  * Note that the coercion functions are assumed not to care
2939  * about input collation, so we just pass InvalidOid for that.
2940  */
2941  getTypeOutputInfo(exprType((Node *) expr->arg),
2942  &outfunc, &outtypisvarlena);
2944  &infunc, &intypioparam);
2945 
2946  simple = simplify_function(outfunc,
2947  CSTRINGOID, -1,
2948  InvalidOid,
2949  InvalidOid,
2950  &args,
2951  false,
2952  true,
2953  true,
2954  context);
2955  if (simple) /* successfully simplified output fn */
2956  {
2957  /*
2958  * Input functions may want 1 to 3 arguments. We always
2959  * supply all three, trusting that nothing downstream will
2960  * complain.
2961  */
2962  args = list_make3(simple,
2963  makeConst(OIDOID,
2964  -1,
2965  InvalidOid,
2966  sizeof(Oid),
2967  ObjectIdGetDatum(intypioparam),
2968  false,
2969  true),
2971  -1,
2972  InvalidOid,
2973  sizeof(int32),
2974  Int32GetDatum(-1),
2975  false,
2976  true));
2977 
2978  simple = simplify_function(infunc,
2979  expr->resulttype, -1,
2980  expr->resultcollid,
2981  InvalidOid,
2982  &args,
2983  false,
2984  false,
2985  true,
2986  context);
2987  if (simple) /* successfully simplified input fn */
2988  return (Node *) simple;
2989  }
2990 
2991  /*
2992  * The expression cannot be simplified any further, so build
2993  * and return a replacement CoerceViaIO node using the
2994  * possibly-simplified argument.
2995  */
2996  newexpr = makeNode(CoerceViaIO);
2997  newexpr->arg = (Expr *) linitial(args);
2998  newexpr->resulttype = expr->resulttype;
2999  newexpr->resultcollid = expr->resultcollid;
3000  newexpr->coerceformat = expr->coerceformat;
3001  newexpr->location = expr->location;
3002  return (Node *) newexpr;
3003  }
3004  case T_ArrayCoerceExpr:
3005  {
3006  ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
3007  Expr *arg;
3008  ArrayCoerceExpr *newexpr;
3009 
3010  /*
3011  * Reduce constants in the ArrayCoerceExpr's argument, then
3012  * build a new ArrayCoerceExpr.
3013  */
3014  arg = (Expr *) eval_const_expressions_mutator((Node *) expr->arg,
3015  context);
3016 
3017  newexpr = makeNode(ArrayCoerceExpr);
3018  newexpr->arg = arg;
3019  newexpr->elemfuncid = expr->elemfuncid;
3020  newexpr->resulttype = expr->resulttype;
3021  newexpr->resulttypmod = expr->resulttypmod;
3022  newexpr->resultcollid = expr->resultcollid;
3023  newexpr->isExplicit = expr->isExplicit;
3024  newexpr->coerceformat = expr->coerceformat;
3025  newexpr->location = expr->location;
3026 
3027  /*
3028  * If constant argument and it's a binary-coercible or
3029  * immutable conversion, we can simplify it to a constant.
3030  */
3031  if (arg && IsA(arg, Const) &&
3032  (!OidIsValid(newexpr->elemfuncid) ||
3034  return (Node *) evaluate_expr((Expr *) newexpr,
3035  newexpr->resulttype,
3036  newexpr->resulttypmod,
3037  newexpr->resultcollid);
3038 
3039  /* Else we must return the partially-simplified node */
3040  return (Node *) newexpr;
3041  }
3042  case T_CollateExpr:
3043  {
3044  /*
3045  * If we can simplify the input to a constant, then we don't
3046  * need the CollateExpr node at all: just change the
3047  * constcollid field of the Const node. Otherwise, replace
3048  * the CollateExpr with a RelabelType. (We do that so as to
3049  * improve uniformity of expression representation and thus
3050  * simplify comparison of expressions.)
3051  */
3052  CollateExpr *collate = (CollateExpr *) node;
3053  Node *arg;
3054 
3055  arg = eval_const_expressions_mutator((Node *) collate->arg,
3056  context);
3057 
3058  if (arg && IsA(arg, Const))
3059  {
3060  Const *con = (Const *) arg;
3061 
3062  con->constcollid = collate->collOid;
3063  return (Node *) con;
3064  }
3065  else if (collate->collOid == exprCollation(arg))
3066  {
3067  /* Don't need a RelabelType either... */
3068  return arg;
3069  }
3070  else
3071  {
3072  RelabelType *relabel = makeNode(RelabelType);
3073 
3074  relabel->resulttype = exprType(arg);
3075  relabel->resulttypmod = exprTypmod(arg);
3076  relabel->resultcollid = collate->collOid;
3078  relabel->location = collate->location;
3079 
3080  /* Don't create stacked RelabelTypes */
3081  while (arg && IsA(arg, RelabelType))
3082  arg = (Node *) ((RelabelType *) arg)->arg;
3083  relabel->arg = (Expr *) arg;
3084 
3085  return (Node *) relabel;
3086  }
3087  }
3088  case T_CaseExpr:
3089  {
3090  /*----------
3091  * CASE expressions can be simplified if there are constant
3092  * condition clauses:
3093  * FALSE (or NULL): drop the alternative
3094  * TRUE: drop all remaining alternatives
3095  * If the first non-FALSE alternative is a constant TRUE,
3096  * we can simplify the entire CASE to that alternative's
3097  * expression. If there are no non-FALSE alternatives,
3098  * we simplify the entire CASE to the default result (ELSE).
3099  *
3100  * If we have a simple-form CASE with constant test
3101  * expression, we substitute the constant value for contained
3102  * CaseTestExpr placeholder nodes, so that we have the
3103  * opportunity to reduce constant test conditions. For
3104  * example this allows
3105  * CASE 0 WHEN 0 THEN 1 ELSE 1/0 END
3106  * to reduce to 1 rather than drawing a divide-by-0 error.
3107  * Note that when the test expression is constant, we don't
3108  * have to include it in the resulting CASE; for example
3109  * CASE 0 WHEN x THEN y ELSE z END
3110  * is transformed by the parser to
3111  * CASE 0 WHEN CaseTestExpr = x THEN y ELSE z END
3112  * which we can simplify to
3113  * CASE WHEN 0 = x THEN y ELSE z END
3114  * It is not necessary for the executor to evaluate the "arg"
3115  * expression when executing the CASE, since any contained
3116  * CaseTestExprs that might have referred to it will have been
3117  * replaced by the constant.
3118  *----------
3119  */
3120  CaseExpr *caseexpr = (CaseExpr *) node;
3121  CaseExpr *newcase;
3122  Node *save_case_val;
3123  Node *newarg;
3124  List *newargs;
3125  bool const_true_cond;
3126  Node *defresult = NULL;
3127  ListCell *arg;
3128 
3129  /* Simplify the test expression, if any */
3130  newarg = eval_const_expressions_mutator((Node *) caseexpr->arg,
3131  context);
3132 
3133  /* Set up for contained CaseTestExpr nodes */
3134  save_case_val = context->case_val;
3135  if (newarg && IsA(newarg, Const))
3136  {
3137  context->case_val = newarg;
3138  newarg = NULL; /* not needed anymore, see above */
3139  }
3140  else
3141  context->case_val = NULL;
3142 
3143  /* Simplify the WHEN clauses */
3144  newargs = NIL;
3145  const_true_cond = false;
3146  foreach(arg, caseexpr->args)
3147  {
3148  CaseWhen *oldcasewhen = lfirst_node(CaseWhen, arg);
3149  Node *casecond;
3150  Node *caseresult;
3151 
3152  /* Simplify this alternative's test condition */
3153  casecond = eval_const_expressions_mutator((Node *) oldcasewhen->expr,
3154  context);
3155 
3156  /*
3157  * If the test condition is constant FALSE (or NULL), then
3158  * drop this WHEN clause completely, without processing
3159  * the result.
3160  */
3161  if (casecond && IsA(casecond, Const))
3162  {
3163  Const *const_input = (Const *) casecond;
3164 
3165  if (const_input->constisnull ||
3166  !DatumGetBool(const_input->constvalue))
3167  continue; /* drop alternative with FALSE cond */
3168  /* Else it's constant TRUE */
3169  const_true_cond = true;
3170  }
3171 
3172  /* Simplify this alternative's result value */
3173  caseresult = eval_const_expressions_mutator((Node *) oldcasewhen->result,
3174  context);
3175 
3176  /* If non-constant test condition, emit a new WHEN node */
3177  if (!const_true_cond)
3178  {
3179  CaseWhen *newcasewhen = makeNode(CaseWhen);
3180 
3181  newcasewhen->expr = (Expr *) casecond;
3182  newcasewhen->result = (Expr *) caseresult;
3183  newcasewhen->location = oldcasewhen->location;
3184  newargs = lappend(newargs, newcasewhen);
3185  continue;
3186  }
3187 
3188  /*
3189  * Found a TRUE condition, so none of the remaining
3190  * alternatives can be reached. We treat the result as
3191  * the default result.
3192  */
3193  defresult = caseresult;
3194  break;
3195  }
3196 
3197  /* Simplify the default result, unless we replaced it above */
3198  if (!const_true_cond)
3199  defresult = eval_const_expressions_mutator((Node *) caseexpr->defresult,
3200  context);
3201 
3202  context->case_val = save_case_val;
3203 
3204  /*
3205  * If no non-FALSE alternatives, CASE reduces to the default
3206  * result
3207  */
3208  if (newargs == NIL)
3209  return defresult;
3210  /* Otherwise we need a new CASE node */
3211  newcase = makeNode(CaseExpr);
3212  newcase->casetype = caseexpr->casetype;
3213  newcase->casecollid = caseexpr->casecollid;
3214  newcase->arg = (Expr *) newarg;
3215  newcase->args = newargs;
3216  newcase->defresult = (Expr *) defresult;
3217  newcase->location = caseexpr->location;
3218  return (Node *) newcase;
3219  }
3220  case T_CaseTestExpr:
3221  {
3222  /*
3223  * If we know a constant test value for the current CASE
3224  * construct, substitute it for the placeholder. Else just
3225  * return the placeholder as-is.
3226  */
3227  if (context->case_val)
3228  return copyObject(context->case_val);
3229  else
3230  return copyObject(node);
3231  }
3232  case T_ArrayExpr:
3233  {
3234  ArrayExpr *arrayexpr = (ArrayExpr *) node;
3235  ArrayExpr *newarray;
3236  bool all_const = true;
3237  List *newelems;
3238  ListCell *element;
3239 
3240  newelems = NIL;
3241  foreach(element, arrayexpr->elements)
3242  {
3243  Node *e;
3244 
3245  e = eval_const_expressions_mutator((Node *) lfirst(element),
3246  context);
3247  if (!IsA(e, Const))
3248  all_const = false;
3249  newelems = lappend(newelems, e);
3250  }
3251 
3252  newarray = makeNode(ArrayExpr);
3253  newarray->array_typeid = arrayexpr->array_typeid;
3254  newarray->array_collid = arrayexpr->array_collid;
3255  newarray->element_typeid = arrayexpr->element_typeid;
3256  newarray->elements = newelems;
3257  newarray->multidims = arrayexpr->multidims;
3258  newarray->location = arrayexpr->location;
3259 
3260  if (all_const)
3261  return (Node *) evaluate_expr((Expr *) newarray,
3262  newarray->array_typeid,
3263  exprTypmod(node),
3264  newarray->array_collid);
3265 
3266  return (Node *) newarray;
3267  }
3268  case T_CoalesceExpr:
3269  {
3270  CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
3271  CoalesceExpr *newcoalesce;
3272  List *newargs;
3273  ListCell *arg;
3274 
3275  newargs = NIL;
3276  foreach(arg, coalesceexpr->args)
3277  {
3278  Node *e;
3279 
3281  context);
3282 
3283  /*
3284  * We can remove null constants from the list. For a
3285  * non-null constant, if it has not been preceded by any
3286  * other non-null-constant expressions then it is the
3287  * result. Otherwise, it's the next argument, but we can
3288  * drop following arguments since they will never be
3289  * reached.
3290  */
3291  if (IsA(e, Const))
3292  {
3293  if (((Const *) e)->constisnull)
3294  continue; /* drop null constant */
3295  if (newargs == NIL)
3296  return e; /* first expr */
3297  newargs = lappend(newargs, e);
3298  break;
3299  }
3300  newargs = lappend(newargs, e);
3301  }
3302 
3303  /*
3304  * If all the arguments were constant null, the result is just
3305  * null
3306  */
3307  if (newargs == NIL)
3308  return (Node *) makeNullConst(coalesceexpr->coalescetype,
3309  -1,
3310  coalesceexpr->coalescecollid);
3311 
3312  newcoalesce = makeNode(CoalesceExpr);
3313  newcoalesce->coalescetype = coalesceexpr->coalescetype;
3314  newcoalesce->coalescecollid = coalesceexpr->coalescecollid;
3315  newcoalesce->args = newargs;
3316  newcoalesce->location = coalesceexpr->location;
3317  return (Node *) newcoalesce;
3318  }
3319  case T_SQLValueFunction:
3320  {
3321  /*
3322  * All variants of SQLValueFunction are stable, so if we are
3323  * estimating the expression's value, we should evaluate the
3324  * current function value. Otherwise just copy.
3325  */
3326  SQLValueFunction *svf = (SQLValueFunction *) node;
3327 
3328  if (context->estimate)
3329  return (Node *) evaluate_expr((Expr *) svf,
3330  svf->type,
3331  svf->typmod,
3332  InvalidOid);
3333  else
3334  return copyObject((Node *) svf);
3335  }
3336  case T_FieldSelect:
3337  {
3338  /*
3339  * We can optimize field selection from a whole-row Var into a
3340  * simple Var. (This case won't be generated directly by the
3341  * parser, because ParseComplexProjection short-circuits it.
3342  * But it can arise while simplifying functions.) Also, we
3343  * can optimize field selection from a RowExpr construct.
3344  *
3345  * However, replacing a whole-row Var in this way has a
3346  * pitfall: if we've already built the rel targetlist for the
3347  * source relation, then the whole-row Var is scheduled to be
3348  * produced by the relation scan, but the simple Var probably
3349  * isn't, which will lead to a failure in setrefs.c. This is
3350  * not a problem when handling simple single-level queries, in
3351  * which expression simplification always happens first. It
3352  * is a risk for lateral references from subqueries, though.
3353  * To avoid such failures, don't optimize uplevel references.
3354  *
3355  * We must also check that the declared type of the field is
3356  * still the same as when the FieldSelect was created --- this
3357  * can change if someone did ALTER COLUMN TYPE on the rowtype.
3358  */
3359  FieldSelect *fselect = (FieldSelect *) node;
3360  FieldSelect *newfselect;
3361  Node *arg;
3362 
3363  arg = eval_const_expressions_mutator((Node *) fselect->arg,
3364  context);
3365  if (arg && IsA(arg, Var) &&
3366  ((Var *) arg)->varattno == InvalidAttrNumber &&
3367  ((Var *) arg)->varlevelsup == 0)
3368  {
3369  if (rowtype_field_matches(((Var *) arg)->vartype,
3370  fselect->fieldnum,
3371  fselect->resulttype,
3372  fselect->resulttypmod,
3373  fselect->resultcollid))
3374  return (Node *) makeVar(((Var *) arg)->varno,
3375  fselect->fieldnum,
3376  fselect->resulttype,
3377  fselect->resulttypmod,
3378  fselect->resultcollid,
3379  ((Var *) arg)->varlevelsup);
3380  }
3381  if (arg && IsA(arg, RowExpr))
3382  {
3383  RowExpr *rowexpr = (RowExpr *) arg;
3384 
3385  if (fselect->fieldnum > 0 &&
3386  fselect->fieldnum <= list_length(rowexpr->args))
3387  {
3388  Node *fld = (Node *) list_nth(rowexpr->args,
3389  fselect->fieldnum - 1);
3390 
3391  if (rowtype_field_matches(rowexpr->row_typeid,
3392  fselect->fieldnum,
3393  fselect->resulttype,
3394  fselect->resulttypmod,
3395  fselect->resultcollid) &&
3396  fselect->resulttype == exprType(fld) &&
3397  fselect->resulttypmod == exprTypmod(fld) &&
3398  fselect->resultcollid == exprCollation(fld))
3399  return fld;
3400  }
3401  }
3402  newfselect = makeNode(FieldSelect);
3403  newfselect->arg = (Expr *) arg;
3404  newfselect->fieldnum = fselect->fieldnum;
3405  newfselect->resulttype = fselect->resulttype;
3406  newfselect->resulttypmod = fselect->resulttypmod;
3407  newfselect->resultcollid = fselect->resultcollid;
3408  return (Node *) newfselect;
3409  }
3410  case T_NullTest:
3411  {
3412  NullTest *ntest = (NullTest *) node;
3413  NullTest *newntest;
3414  Node *arg;
3415 
3416  arg = eval_const_expressions_mutator((Node *) ntest->arg,
3417  context);
3418  if (ntest->argisrow && arg && IsA(arg, RowExpr))
3419  {
3420  /*
3421  * We break ROW(...) IS [NOT] NULL into separate tests on
3422  * its component fields. This form is usually more
3423  * efficient to evaluate, as well as being more amenable
3424  * to optimization.
3425  */
3426  RowExpr *rarg = (RowExpr *) arg;
3427  List *newargs = NIL;
3428  ListCell *l;
3429 
3430  foreach(l, rarg->args)
3431  {
3432  Node *relem = (Node *) lfirst(l);
3433 
3434  /*
3435  * A constant field refutes the whole NullTest if it's
3436  * of the wrong nullness; else we can discard it.
3437  */
3438  if (relem && IsA(relem, Const))
3439  {
3440  Const *carg = (Const *) relem;
3441 
3442  if (carg->constisnull ?
3443  (ntest->nulltesttype == IS_NOT_NULL) :
3444  (ntest->nulltesttype == IS_NULL))
3445  return makeBoolConst(false, false);
3446  continue;
3447  }
3448 
3449  /*
3450  * Else, make a scalar (argisrow == false) NullTest
3451  * for this field. Scalar semantics are required
3452  * because IS [NOT] NULL doesn't recurse; see comments
3453  * in ExecEvalRowNullInt().
3454  */
3455  newntest = makeNode(NullTest);
3456  newntest->arg = (Expr *) relem;
3457  newntest->nulltesttype = ntest->nulltesttype;
3458  newntest->argisrow = false;
3459  newntest->location = ntest->location;
3460  newargs = lappend(newargs, newntest);
3461  }
3462  /* If all the inputs were constants, result is TRUE */
3463  if (newargs == NIL)
3464  return makeBoolConst(true, false);
3465  /* If only one nonconst input, it's the result */
3466  if (list_length(newargs) == 1)
3467  return (Node *) linitial(newargs);
3468  /* Else we need an AND node */
3469  return (Node *) make_andclause(newargs);
3470  }
3471  if (!ntest->argisrow && arg && IsA(arg, Const))
3472  {
3473  Const *carg = (Const *) arg;
3474  bool result;
3475 
3476  switch (ntest->nulltesttype)
3477  {
3478  case IS_NULL:
3479  result = carg->constisnull;
3480  break;
3481  case IS_NOT_NULL:
3482  result = !carg->constisnull;
3483  break;
3484  default:
3485  elog(ERROR, "unrecognized nulltesttype: %d",
3486  (int) ntest->nulltesttype);
3487  result = false; /* keep compiler quiet */
3488  break;
3489  }
3490 
3491  return makeBoolConst(result, false);
3492  }
3493 
3494  newntest = makeNode(NullTest);
3495  newntest->arg = (Expr *) arg;
3496  newntest->nulltesttype = ntest->nulltesttype;
3497  newntest->argisrow = ntest->argisrow;
3498  newntest->location = ntest->location;
3499  return (Node *) newntest;
3500  }
3501  case T_BooleanTest:
3502  {
3503  BooleanTest *btest = (BooleanTest *) node;
3504  BooleanTest *newbtest;
3505  Node *arg;
3506 
3507  arg = eval_const_expressions_mutator((Node *) btest->arg,
3508  context);
3509  if (arg && IsA(arg, Const))
3510  {
3511  Const *carg = (Const *) arg;
3512  bool result;
3513 
3514  switch (btest->booltesttype)
3515  {
3516  case IS_TRUE:
3517  result = (!carg->constisnull &&
3518  DatumGetBool(carg->constvalue));
3519  break;
3520  case IS_NOT_TRUE:
3521  result = (carg->constisnull ||
3522  !DatumGetBool(carg->constvalue));
3523  break;
3524  case IS_FALSE:
3525  result = (!carg->constisnull &&
3526  !DatumGetBool(carg->constvalue));
3527  break;
3528  case IS_NOT_FALSE:
3529  result = (carg->constisnull ||
3530  DatumGetBool(carg->constvalue));
3531  break;
3532  case IS_UNKNOWN:
3533  result = carg->constisnull;
3534  break;
3535  case IS_NOT_UNKNOWN:
3536  result = !carg->constisnull;
3537  break;
3538  default:
3539  elog(ERROR, "unrecognized booltesttype: %d",
3540  (int) btest->booltesttype);
3541  result = false; /* keep compiler quiet */
3542  break;
3543  }
3544 
3545  return makeBoolConst(result, false);
3546  }
3547 
3548  newbtest = makeNode(BooleanTest);
3549  newbtest->arg = (Expr *) arg;
3550  newbtest->booltesttype = btest->booltesttype;
3551  newbtest->location = btest->location;
3552  return (Node *) newbtest;
3553  }
3554  case T_PlaceHolderVar:
3555 
3556  /*
3557  * In estimation mode, just strip the PlaceHolderVar node
3558  * altogether; this amounts to estimating that the contained value
3559  * won't be forced to null by an outer join. In regular mode we
3560  * just use the default behavior (ie, simplify the expression but
3561  * leave the PlaceHolderVar node intact).
3562  */
3563  if (context->estimate)
3564  {
3565  PlaceHolderVar *phv = (PlaceHolderVar *) node;
3566 
3567  return eval_const_expressions_mutator((Node *) phv->phexpr,
3568  context);
3569  }
3570  break;
3571  default:
3572  break;
3573  }
3574 
3575  /*
3576  * For any node type not handled above, we recurse using
3577  * expression_tree_mutator, which will copy the node unchanged but try to
3578  * simplify its arguments (if any) using this routine. For example: we
3579  * cannot eliminate an ArrayRef node, but we might be able to simplify
3580  * constant expressions in its subscripts.
3581  */
3583  (void *) context);
3584 }
3585 
3586 /*
3587  * Subroutine for eval_const_expressions: process arguments of an OR clause
3588  *
3589  * This includes flattening of nested ORs as well as recursion to
3590  * eval_const_expressions to simplify the OR arguments.
3591  *
3592  * After simplification, OR arguments are handled as follows:
3593  * non constant: keep
3594  * FALSE: drop (does not affect result)
3595  * TRUE: force result to TRUE
3596  * NULL: keep only one
3597  * We must keep one NULL input because OR expressions evaluate to NULL when no
3598  * input is TRUE and at least one is NULL. We don't actually include the NULL
3599  * here, that's supposed to be done by the caller.
3600  *
3601  * The output arguments *haveNull and *forceTrue must be initialized FALSE
3602  * by the caller. They will be set TRUE if a null constant or true constant,
3603  * respectively, is detected anywhere in the argument list.
3604  */
3605 static List *
3608  bool *haveNull, bool *forceTrue)
3609 {
3610  List *newargs = NIL;
3611  List *unprocessed_args;
3612 
3613  /*
3614  * We want to ensure that any OR immediately beneath another OR gets
3615  * flattened into a single OR-list, so as to simplify later reasoning.
3616  *
3617  * To avoid stack overflow from recursion of eval_const_expressions, we
3618  * resort to some tenseness here: we keep a list of not-yet-processed
3619  * inputs, and handle flattening of nested ORs by prepending to the to-do
3620  * list instead of recursing. Now that the parser generates N-argument
3621  * ORs from simple lists, this complexity is probably less necessary than
3622  * it once was, but we might as well keep the logic.
3623  */
3624  unprocessed_args = list_copy(args);
3625  while (unprocessed_args)
3626  {
3627  Node *arg = (Node *) linitial(unprocessed_args);
3628 
3629  unprocessed_args = list_delete_first(unprocessed_args);
3630 
3631  /* flatten nested ORs as per above comment */
3632  if (or_clause(arg))
3633  {
3634  List *subargs = list_copy(((BoolExpr *) arg)->args);
3635 
3636  /* overly tense code to avoid leaking unused list header */
3637  if (!unprocessed_args)
3638  unprocessed_args = subargs;
3639  else
3640  {
3641  List *oldhdr = unprocessed_args;
3642 
3643  unprocessed_args = list_concat(subargs, unprocessed_args);
3644  pfree(oldhdr);
3645  }
3646  continue;
3647  }
3648 
3649  /* If it's not an OR, simplify it */
3650  arg = eval_const_expressions_mutator(arg, context);
3651 
3652  /*
3653  * It is unlikely but not impossible for simplification of a non-OR
3654  * clause to produce an OR. Recheck, but don't be too tense about it
3655  * since it's not a mainstream case. In particular we don't worry
3656  * about const-simplifying the input twice.
3657  */
3658  if (or_clause(arg))
3659  {
3660  List *subargs = list_copy(((BoolExpr *) arg)->args);
3661 
3662  unprocessed_args = list_concat(subargs, unprocessed_args);
3663  continue;
3664  }
3665 
3666  /*
3667  * OK, we have a const-simplified non-OR argument. Process it per
3668  * comments above.
3669  */
3670  if (IsA(arg, Const))
3671  {
3672  Const *const_input = (Const *) arg;
3673 
3674  if (const_input->constisnull)
3675  *haveNull = true;
3676  else if (DatumGetBool(const_input->constvalue))
3677  {
3678  *forceTrue = true;
3679 
3680  /*
3681  * Once we detect a TRUE result we can just exit the loop
3682  * immediately. However, if we ever add a notion of
3683  * non-removable functions, we'd need to keep scanning.
3684  */
3685  return NIL;
3686  }
3687  /* otherwise, we can drop the constant-false input */
3688  continue;
3689  }
3690 
3691  /* else emit the simplified arg into the result list */
3692  newargs = lappend(newargs, arg);
3693  }
3694 
3695  return newargs;
3696 }
3697 
3698 /*
3699  * Subroutine for eval_const_expressions: process arguments of an AND clause
3700  *
3701  * This includes flattening of nested ANDs as well as recursion to
3702  * eval_const_expressions to simplify the AND arguments.
3703  *
3704  * After simplification, AND arguments are handled as follows:
3705  * non constant: keep
3706  * TRUE: drop (does not affect result)
3707  * FALSE: force result to FALSE
3708  * NULL: keep only one
3709  * We must keep one NULL input because AND expressions evaluate to NULL when
3710  * no input is FALSE and at least one is NULL. We don't actually include the
3711  * NULL here, that's supposed to be done by the caller.
3712  *
3713  * The output arguments *haveNull and *forceFalse must be initialized FALSE
3714  * by the caller. They will be set TRUE if a null constant or false constant,
3715  * respectively, is detected anywhere in the argument list.
3716  */
3717 static List *
3720  bool *haveNull, bool *forceFalse)
3721 {
3722  List *newargs = NIL;
3723  List *unprocessed_args;
3724 
3725  /* See comments in simplify_or_arguments */
3726  unprocessed_args = list_copy(args);
3727  while (unprocessed_args)
3728  {
3729  Node *arg = (Node *) linitial(unprocessed_args);
3730 
3731  unprocessed_args = list_delete_first(unprocessed_args);
3732 
3733  /* flatten nested ANDs as per above comment */
3734  if (and_clause(arg))
3735  {
3736  List *subargs = list_copy(((BoolExpr *) arg)->args);
3737 
3738  /* overly tense code to avoid leaking unused list header */
3739  if (!unprocessed_args)
3740  unprocessed_args = subargs;
3741  else
3742  {
3743  List *oldhdr = unprocessed_args;
3744 
3745  unprocessed_args = list_concat(subargs, unprocessed_args);
3746  pfree(oldhdr);
3747  }
3748  continue;
3749  }
3750 
3751  /* If it's not an AND, simplify it */
3752  arg = eval_const_expressions_mutator(arg, context);
3753 
3754  /*
3755  * It is unlikely but not impossible for simplification of a non-AND
3756  * clause to produce an AND. Recheck, but don't be too tense about it
3757  * since it's not a mainstream case. In particular we don't worry
3758  * about const-simplifying the input twice.
3759  */
3760  if (and_clause(arg))
3761  {
3762  List *subargs = list_copy(((BoolExpr *) arg)->args);
3763 
3764  unprocessed_args = list_concat(subargs, unprocessed_args);
3765  continue;
3766  }
3767 
3768  /*
3769  * OK, we have a const-simplified non-AND argument. Process it per
3770  * comments above.
3771  */
3772  if (IsA(arg, Const))
3773  {
3774  Const *const_input = (Const *) arg;
3775 
3776  if (const_input->constisnull)
3777  *haveNull = true;
3778  else if (!DatumGetBool(const_input->constvalue))
3779  {
3780  *forceFalse = true;
3781 
3782  /*
3783  * Once we detect a FALSE result we can just exit the loop
3784  * immediately. However, if we ever add a notion of
3785  * non-removable functions, we'd need to keep scanning.
3786  */
3787  return NIL;
3788  }
3789  /* otherwise, we can drop the constant-true input */
3790  continue;
3791  }
3792 
3793  /* else emit the simplified arg into the result list */
3794  newargs = lappend(newargs, arg);
3795  }
3796 
3797  return newargs;
3798 }
3799 
3800 /*
3801  * Subroutine for eval_const_expressions: try to simplify boolean equality
3802  * or inequality condition
3803  *
3804  * Inputs are the operator OID and the simplified arguments to the operator.
3805  * Returns a simplified expression if successful, or NULL if cannot
3806  * simplify the expression.
3807  *
3808  * The idea here is to reduce "x = true" to "x" and "x = false" to "NOT x",
3809  * or similarly "x <> true" to "NOT x" and "x <> false" to "x".
3810  * This is only marginally useful in itself, but doing it in constant folding
3811  * ensures that we will recognize these forms as being equivalent in, for
3812  * example, partial index matching.
3813  *
3814  * We come here only if simplify_function has failed; therefore we cannot
3815  * see two constant inputs, nor a constant-NULL input.
3816  */
3817 static Node *
3819 {
3820  Node *leftop;
3821  Node *rightop;
3822 
3823  Assert(list_length(args) == 2);
3824  leftop = linitial(args);
3825  rightop = lsecond(args);
3826  if (leftop && IsA(leftop, Const))
3827  {
3828  Assert(!((Const *) leftop)->constisnull);
3829  if (opno == BooleanEqualOperator)
3830  {
3831  if (DatumGetBool(((Const *) leftop)->constvalue))
3832  return rightop; /* true = foo */
3833  else
3834  return negate_clause(rightop); /* false = foo */
3835  }
3836  else
3837  {
3838  if (DatumGetBool(((Const *) leftop)->constvalue))
3839  return negate_clause(rightop); /* true <> foo */
3840  else
3841  return rightop; /* false <> foo */
3842  }
3843  }
3844  if (rightop && IsA(rightop, Const))
3845  {
3846  Assert(!((Const *) rightop)->constisnull);
3847  if (opno == BooleanEqualOperator)
3848  {
3849  if (DatumGetBool(((Const *) rightop)->constvalue))
3850  return leftop; /* foo = true */
3851  else
3852  return negate_clause(leftop); /* foo = false */
3853  }
3854  else
3855  {
3856  if (DatumGetBool(((Const *) rightop)->constvalue))
3857  return negate_clause(leftop); /* foo <> true */
3858  else
3859  return leftop; /* foo <> false */
3860  }
3861  }
3862  return NULL;
3863 }
3864 
3865 /*
3866  * Subroutine for eval_const_expressions: try to simplify a function call
3867  * (which might originally have been an operator; we don't care)
3868  *
3869  * Inputs are the function OID, actual result type OID (which is needed for
3870  * polymorphic functions), result typmod, result collation, the input
3871  * collation to use for the function, the original argument list (not
3872  * const-simplified yet, unless process_args is false), and some flags;
3873  * also the context data for eval_const_expressions.
3874  *
3875  * Returns a simplified expression if successful, or NULL if cannot
3876  * simplify the function call.
3877  *
3878  * This function is also responsible for converting named-notation argument
3879  * lists into positional notation and/or adding any needed default argument
3880  * expressions; which is a bit grotty, but it avoids extra fetches of the
3881  * function's pg_proc tuple. For this reason, the args list is
3882  * pass-by-reference. Conversion and const-simplification of the args list
3883  * will be done even if simplification of the function call itself is not
3884  * possible.
3885  */
3886 static Expr *
3887 simplify_function(Oid funcid, Oid result_type, int32 result_typmod,
3888  Oid result_collid, Oid input_collid, List **args_p,
3889  bool funcvariadic, bool process_args, bool allow_non_const,
3891 {
3892  List *args = *args_p;
3893  HeapTuple func_tuple;
3894  Form_pg_proc func_form;
3895  Expr *newexpr;
3896 
3897  /*
3898  * We have three strategies for simplification: execute the function to
3899  * deliver a constant result, use a transform function to generate a
3900  * substitute node tree, or expand in-line the body of the function
3901  * definition (which only works for simple SQL-language functions, but
3902  * that is a common case). Each case needs access to the function's
3903  * pg_proc tuple, so fetch it just once.
3904  *
3905  * Note: the allow_non_const flag suppresses both the second and third
3906  * strategies; so if !allow_non_const, simplify_function can only return a
3907  * Const or NULL. Argument-list rewriting happens anyway, though.
3908  */
3909  func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
3910  if (!HeapTupleIsValid(func_tuple))
3911  elog(ERROR, "cache lookup failed for function %u", funcid);
3912  func_form = (Form_pg_proc) GETSTRUCT(func_tuple);
3913 
3914  /*
3915  * Process the function arguments, unless the caller did it already.
3916  *
3917  * Here we must deal with named or defaulted arguments, and then
3918  * recursively apply eval_const_expressions to the whole argument list.
3919  */
3920  if (process_args)
3921  {
3922  args = expand_function_arguments(args, result_type, func_tuple);
3923  args = (List *) expression_tree_mutator((Node *) args,
3925  (void *) context);
3926  /* Argument processing done, give it back to the caller */
3927  *args_p = args;
3928  }
3929 
3930  /* Now attempt simplification of the function call proper. */
3931 
3932  newexpr = evaluate_function(funcid, result_type, result_typmod,
3933  result_collid, input_collid,
3934  args, funcvariadic,
3935  func_tuple, context);
3936 
3937  if (!newexpr && allow_non_const && OidIsValid(func_form->protransform))
3938  {
3939  /*
3940  * Build a dummy FuncExpr node containing the simplified arg list. We
3941  * use this approach to present a uniform interface to the transform
3942  * function regardless of how the function is actually being invoked.
3943  */
3944  FuncExpr fexpr;
3945 
3946  fexpr.xpr.type = T_FuncExpr;
3947  fexpr.funcid = funcid;
3948  fexpr.funcresulttype = result_type;
3949  fexpr.funcretset = func_form->proretset;
3950  fexpr.funcvariadic = funcvariadic;
3952  fexpr.funccollid = result_collid;
3953  fexpr.inputcollid = input_collid;
3954  fexpr.args = args;
3955  fexpr.location = -1;
3956 
3957  newexpr = (Expr *)
3958  DatumGetPointer(OidFunctionCall1(func_form->protransform,
3959  PointerGetDatum(&fexpr)));
3960  }
3961 
3962  if (!newexpr && allow_non_const)
3963  newexpr = inline_function(funcid, result_type, result_collid,
3964  input_collid, args, funcvariadic,
3965  func_tuple, context);
3966 
3967  ReleaseSysCache(func_tuple);
3968 
3969  return newexpr;
3970 }
3971 
3972 /*
3973  * expand_function_arguments: convert named-notation args to positional args
3974  * and/or insert default args, as needed
3975  *
3976  * If we need to change anything, the input argument list is copied, not
3977  * modified.
3978  *
3979  * Note: this gets applied to operator argument lists too, even though the
3980  * cases it handles should never occur there. This should be OK since it
3981  * will fall through very quickly if there's nothing to do.
3982  */
3983 static List *
3985 {
3986  Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
3987  bool has_named_args = false;
3988  ListCell *lc;
3989 
3990  /* Do we have any named arguments? */
3991  foreach(lc, args)
3992  {
3993  Node *arg = (Node *) lfirst(lc);
3994 
3995  if (IsA(arg, NamedArgExpr))
3996  {
3997  has_named_args = true;
3998  break;
3999  }
4000  }
4001 
4002  /* If so, we must apply reorder_function_arguments */
4003  if (has_named_args)
4004  {
4005  args = reorder_function_arguments(args, func_tuple);
4006  /* Recheck argument types and add casts if needed */
4007  recheck_cast_function_args(args, result_type, func_tuple);
4008  }
4009  else if (list_length(args) < funcform->pronargs)
4010  {
4011  /* No named args, but we seem to be short some defaults */
4012  args = add_function_defaults(args, func_tuple);
4013  /* Recheck argument types and add casts if needed */
4014  recheck_cast_function_args(args, result_type, func_tuple);
4015  }
4016 
4017  return args;
4018 }
4019 
4020 /*
4021  * reorder_function_arguments: convert named-notation args to positional args
4022  *
4023  * This function also inserts default argument values as needed, since it's
4024  * impossible to form a truly valid positional call without that.
4025  */
4026 static List *
4028 {
4029  Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4030  int pronargs = funcform->pronargs;
4031  int nargsprovided = list_length(args);
4032  Node *argarray[FUNC_MAX_ARGS];
4033  ListCell *lc;
4034  int i;
4035 
4036  Assert(nargsprovided <= pronargs);
4037  if (pronargs > FUNC_MAX_ARGS)
4038  elog(ERROR, "too many function arguments");
4039  MemSet(argarray, 0, pronargs * sizeof(Node *));
4040 
4041  /* Deconstruct the argument list into an array indexed by argnumber */
4042  i = 0;
4043  foreach(lc, args)
4044  {
4045  Node *arg = (Node *) lfirst(lc);
4046 
4047  if (!IsA(arg, NamedArgExpr))
4048  {
4049  /* positional argument, assumed to precede all named args */
4050  Assert(argarray[i] == NULL);
4051  argarray[i++] = arg;
4052  }
4053  else
4054  {
4055  NamedArgExpr *na = (NamedArgExpr *) arg;
4056 
4057  Assert(argarray[na->argnumber] == NULL);
4058  argarray[na->argnumber] = (Node *) na->arg;
4059  }
4060  }
4061 
4062  /*
4063  * Fetch default expressions, if needed, and insert into array at proper
4064  * locations (they aren't necessarily consecutive or all used)
4065  */
4066  if (nargsprovided < pronargs)
4067  {
4068  List *defaults = fetch_function_defaults(func_tuple);
4069 
4070  i = pronargs - funcform->pronargdefaults;
4071  foreach(lc, defaults)
4072  {
4073  if (argarray[i] == NULL)
4074  argarray[i] = (Node *) lfirst(lc);
4075  i++;
4076  }
4077  }
4078 
4079  /* Now reconstruct the args list in proper order */
4080  args = NIL;
4081  for (i = 0; i < pronargs; i++)
4082  {
4083  Assert(argarray[i] != NULL);
4084  args = lappend(args, argarray[i]);
4085  }
4086 
4087  return args;
4088 }
4089 
4090 /*
4091  * add_function_defaults: add missing function arguments from its defaults
4092  *
4093  * This is used only when the argument list was positional to begin with,
4094  * and so we know we just need to add defaults at the end.
4095  */
4096 static List *
4098 {
4099  Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4100  int nargsprovided = list_length(args);
4101  List *defaults;
4102  int ndelete;
4103 
4104  /* Get all the default expressions from the pg_proc tuple */
4105  defaults = fetch_function_defaults(func_tuple);
4106 
4107  /* Delete any unused defaults from the list */
4108  ndelete = nargsprovided + list_length(defaults) - funcform->pronargs;
4109  if (ndelete < 0)
4110  elog(ERROR, "not enough default arguments");
4111  while (ndelete-- > 0)
4112  defaults = list_delete_first(defaults);
4113 
4114  /* And form the combined argument list, not modifying the input list */
4115  return list_concat(list_copy(args), defaults);
4116 }
4117 
4118 /*
4119  * fetch_function_defaults: get function's default arguments as expression list
4120  */
4121 static List *
4123 {
4124  List *defaults;
4125  Datum proargdefaults;
4126  bool isnull;
4127  char *str;
4128 
4129  /* The error cases here shouldn't happen, but check anyway */
4130  proargdefaults = SysCacheGetAttr(PROCOID, func_tuple,
4132  &isnull);
4133  if (isnull)
4134  elog(ERROR, "not enough default arguments");
4135  str = TextDatumGetCString(proargdefaults);
4136  defaults = castNode(List, stringToNode(str));
4137  pfree(str);
4138  return defaults;
4139 }
4140 
4141 /*
4142  * recheck_cast_function_args: recheck function args and typecast as needed
4143  * after adding defaults.
4144  *
4145  * It is possible for some of the defaulted arguments to be polymorphic;
4146  * therefore we can't assume that the default expressions have the correct
4147  * data types already. We have to re-resolve polymorphics and do coercion
4148  * just like the parser did.
4149  *
4150  * This should be a no-op if there are no polymorphic arguments,
4151  * but we do it anyway to be sure.
4152  *
4153  * Note: if any casts are needed, the args list is modified in-place;
4154  * caller should have already copied the list structure.
4155  */
4156 static void
4158 {
4159  Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4160  int nargs;
4161  Oid actual_arg_types[FUNC_MAX_ARGS];
4162  Oid declared_arg_types[FUNC_MAX_ARGS];
4163  Oid rettype;
4164  ListCell *lc;
4165 
4166  if (list_length(args) > FUNC_MAX_ARGS)
4167  elog(ERROR, "too many function arguments");
4168  nargs = 0;
4169  foreach(lc, args)
4170  {
4171  actual_arg_types[nargs++] = exprType((Node *) lfirst(lc));
4172  }
4173  Assert(nargs == funcform->pronargs);
4174  memcpy(declared_arg_types, funcform->proargtypes.values,
4175  funcform->pronargs * sizeof(Oid));
4176  rettype = enforce_generic_type_consistency(actual_arg_types,
4177  declared_arg_types,
4178  nargs,
4179  funcform->prorettype,
4180  false);
4181  /* let's just check we got the same answer as the parser did ... */
4182  if (rettype != result_type)
4183  elog(ERROR, "function's resolved result type changed during planning");
4184 
4185  /* perform any necessary typecasting of arguments */
4186  make_fn_arguments(NULL, args, actual_arg_types, declared_arg_types);
4187 }
4188 
4189 /*
4190  * evaluate_function: try to pre-evaluate a function call
4191  *
4192  * We can do this if the function is strict and has any constant-null inputs
4193  * (just return a null constant), or if the function is immutable and has all
4194  * constant inputs (call it and return the result as a Const node). In
4195  * estimation mode we are willing to pre-evaluate stable functions too.
4196  *
4197  * Returns a simplified expression if successful, or NULL if cannot
4198  * simplify the function.
4199  */
4200 static Expr *
4201 evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
4202  Oid result_collid, Oid input_collid, List *args,
4203  bool funcvariadic,
4204  HeapTuple func_tuple,
4206 {
4207  Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4208  bool has_nonconst_input = false;
4209  bool has_null_input = false;
4210  ListCell *arg;
4211  FuncExpr *newexpr;
4212 
4213  /*
4214  * Can't simplify if it returns a set.
4215  */
4216  if (funcform->proretset)
4217  return NULL;
4218 
4219  /*
4220  * Can't simplify if it returns RECORD. The immediate problem is that it
4221  * will be needing an expected tupdesc which we can't supply here.
4222  *
4223  * In the case where it has OUT parameters, it could get by without an
4224  * expected tupdesc, but we still have issues: get_expr_result_type()
4225  * doesn't know how to extract type info from a RECORD constant, and in
4226  * the case of a NULL function result there doesn't seem to be any clean
4227  * way to fix that. In view of the likelihood of there being still other
4228  * gotchas, seems best to leave the function call unreduced.
4229  */
4230  if (funcform->prorettype == RECORDOID)
4231  return NULL;
4232 
4233  /*
4234  * Check for constant inputs and especially constant-NULL inputs.
4235  */
4236  foreach(arg, args)
4237  {
4238  if (IsA(lfirst(arg), Const))
4239  has_null_input |= ((Const *) lfirst(arg))->constisnull;
4240  else
4241  has_nonconst_input = true;
4242  }
4243 
4244  /*
4245  * If the function is strict and has a constant-NULL input, it will never
4246  * be called at all, so we can replace the call by a NULL constant, even
4247  * if there are other inputs that aren't constant, and even if the
4248  * function is not otherwise immutable.
4249  */
4250  if (funcform->proisstrict && has_null_input)
4251  return (Expr *) makeNullConst(result_type, result_typmod,
4252  result_collid);
4253 
4254  /*
4255  * Otherwise, can simplify only if all inputs are constants. (For a
4256  * non-strict function, constant NULL inputs are treated the same as
4257  * constant non-NULL inputs.)
4258  */
4259  if (has_nonconst_input)
4260  return NULL;
4261 
4262  /*
4263  * Ordinarily we are only allowed to simplify immutable functions. But for
4264  * purposes of estimation, we consider it okay to simplify functions that
4265  * are merely stable; the risk that the result might change from planning
4266  * time to execution time is worth taking in preference to not being able
4267  * to estimate the value at all.
4268  */
4269  if (funcform->provolatile == PROVOLATILE_IMMUTABLE)
4270  /* okay */ ;
4271  else if (context->estimate && funcform->provolatile == PROVOLATILE_STABLE)
4272  /* okay */ ;
4273  else
4274  return NULL;
4275 
4276  /*
4277  * OK, looks like we can simplify this operator/function.
4278  *
4279  * Build a new FuncExpr node containing the already-simplified arguments.
4280  */
4281  newexpr = makeNode(FuncExpr);
4282  newexpr->funcid = funcid;
4283  newexpr->funcresulttype = result_type;
4284  newexpr->funcretset = false;
4285  newexpr->funcvariadic = funcvariadic;
4286  newexpr->funcformat = COERCE_EXPLICIT_CALL; /* doesn't matter */
4287  newexpr->funccollid = result_collid; /* doesn't matter */
4288  newexpr->inputcollid = input_collid;
4289  newexpr->args = args;
4290  newexpr->location = -1;
4291 
4292  return evaluate_expr((Expr *) newexpr, result_type, result_typmod,
4293  result_collid);
4294 }
4295 
4296 /*
4297  * inline_function: try to expand a function call inline
4298  *
4299  * If the function is a sufficiently simple SQL-language function
4300  * (just "SELECT expression"), then we can inline it and avoid the rather
4301  * high per-call overhead of SQL functions. Furthermore, this can expose
4302  * opportunities for constant-folding within the function expression.
4303  *
4304  * We have to beware of some special cases however. A directly or
4305  * indirectly recursive function would cause us to recurse forever,
4306  * so we keep track of which functions we are already expanding and
4307  * do not re-expand them. Also, if a parameter is used more than once
4308  * in the SQL-function body, we require it not to contain any volatile
4309  * functions (volatiles might deliver inconsistent answers) nor to be
4310  * unreasonably expensive to evaluate. The expensiveness check not only
4311  * prevents us from doing multiple evaluations of an expensive parameter
4312  * at runtime, but is a safety value to limit growth of an expression due
4313  * to repeated inlining.
4314  *
4315  * We must also beware of changing the volatility or strictness status of
4316  * functions by inlining them.
4317  *
4318  * Also, at the moment we can't inline functions returning RECORD. This
4319  * doesn't work in the general case because it discards information such
4320  * as OUT-parameter declarations.
4321  *
4322  * Also, context-dependent expression nodes in the argument list are trouble.
4323  *
4324  * Returns a simplified expression if successful, or NULL if cannot
4325  * simplify the function.
4326  */
4327 static Expr *
4328 inline_function(Oid funcid, Oid result_type, Oid result_collid,
4329  Oid input_collid, List *args,
4330  bool funcvariadic,
4331  HeapTuple func_tuple,
4333 {
4334  Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4335  char *src;
4336  Datum tmp;
4337  bool isNull;
4338  bool modifyTargetList;
4339  MemoryContext oldcxt;
4340  MemoryContext mycxt;
4341  inline_error_callback_arg callback_arg;
4342  ErrorContextCallback sqlerrcontext;
4343  FuncExpr *fexpr;
4345  ParseState *pstate;
4346  List *raw_parsetree_list;
4347  Query *querytree;
4348  Node *newexpr;
4349  int *usecounts;
4350  ListCell *arg;
4351  int i;
4352 
4353  /*
4354  * Forget it if the function is not SQL-language or has other showstopper
4355  * properties. (The nargs check is just paranoia.)
4356  */
4357  if (funcform->prolang != SQLlanguageId ||
4358  funcform->prosecdef ||
4359  funcform->proretset ||
4360  funcform->prorettype == RECORDOID ||
4361  !heap_attisnull(func_tuple, Anum_pg_proc_proconfig) ||
4362  funcform->pronargs != list_length(args))
4363  return NULL;
4364 
4365  /* Check for recursive function, and give up trying to expand if so */
4366  if (list_member_oid(context->active_fns, funcid))
4367  return NULL;
4368 
4369  /* Check permission to call function (fail later, if not) */
4371  return NULL;
4372 
4373  /* Check whether a plugin wants to hook function entry/exit */
4374  if (FmgrHookIsNeeded(funcid))
4375  return NULL;
4376 
4377  /*
4378  * Make a temporary memory context, so that we don't leak all the stuff
4379  * that parsing might create.
4380  */
4382  "inline_function",
4384  oldcxt = MemoryContextSwitchTo(mycxt);
4385 
4386  /* Fetch the function body */
4387  tmp = SysCacheGetAttr(PROCOID,
4388  func_tuple,
4390  &isNull);
4391  if (isNull)
4392  elog(ERROR, "null prosrc for function %u", funcid);
4393  src = TextDatumGetCString(tmp);
4394 
4395  /*
4396  * Setup error traceback support for ereport(). This is so that we can
4397  * finger the function that bad information came from.
4398  */
4399  callback_arg.proname = NameStr(funcform->proname);
4400  callback_arg.prosrc = src;
4401 
4402  sqlerrcontext.callback = sql_inline_error_callback;
4403  sqlerrcontext.arg = (void *) &callback_arg;
4404  sqlerrcontext.previous = error_context_stack;
4405  error_context_stack = &sqlerrcontext;
4406 
4407  /*
4408  * Set up to handle parameters while parsing the function body. We need a
4409  * dummy FuncExpr node containing the already-simplified arguments to pass
4410  * to prepare_sql_fn_parse_info. (It is really only needed if there are
4411  * some polymorphic arguments, but for simplicity we always build it.)
4412  */
4413  fexpr = makeNode(FuncExpr);
4414  fexpr->funcid = funcid;
4415  fexpr->funcresulttype = result_type;
4416  fexpr->funcretset = false;
4417  fexpr->funcvariadic = funcvariadic;
4418  fexpr->funcformat = COERCE_EXPLICIT_CALL; /* doesn't matter */
4419  fexpr->funccollid = result_collid; /* doesn't matter */
4420  fexpr->inputcollid = input_collid;
4421  fexpr->args = args;
4422  fexpr->location = -1;
4423 
4424  pinfo = prepare_sql_fn_parse_info(func_tuple,
4425  (Node *) fexpr,
4426  input_collid);
4427 
4428  /*
4429  * We just do parsing and parse analysis, not rewriting, because rewriting
4430  * will not affect table-free-SELECT-only queries, which is all that we
4431  * care about. Also, we can punt as soon as we detect more than one
4432  * command in the function body.
4433  */
4434  raw_parsetree_list = pg_parse_query(src);
4435  if (list_length(raw_parsetree_list) != 1)
4436  goto fail;
4437 
4438  pstate = make_parsestate(NULL);
4439  pstate->p_sourcetext = src;
4440  sql_fn_parser_setup(pstate, pinfo);
4441 
4442  querytree = transformTopLevelStmt(pstate, linitial(raw_parsetree_list));
4443 
4444  free_parsestate(pstate);
4445 
4446  /*
4447  * The single command must be a simple "SELECT expression".
4448  *
4449  * Note: if you change the tests involved in this, see also plpgsql's
4450  * exec_simple_check_plan(). That generally needs to have the same idea
4451  * of what's a "simple expression", so that inlining a function that
4452  * previously wasn't inlined won't change plpgsql's conclusion.
4453  */
4454  if (!IsA(querytree, Query) ||
4455  querytree->commandType != CMD_SELECT ||
4456  querytree->hasAggs ||
4457  querytree->hasWindowFuncs ||
4458  querytree->hasTargetSRFs ||
4459  querytree->hasSubLinks ||
4460  querytree->cteList ||
4461  querytree->rtable ||
4462  querytree->jointree->fromlist ||
4463  querytree->jointree->quals ||
4464  querytree->groupClause ||
4465  querytree->groupingSets ||
4466  querytree->havingQual ||
4467  querytree->windowClause ||
4468  querytree->distinctClause ||
4469  querytree->sortClause ||
4470  querytree->limitOffset ||
4471  querytree->limitCount ||
4472  querytree->setOperations ||
4473  list_length(querytree->targetList) != 1)
4474  goto fail;
4475 
4476  /*
4477  * Make sure the function (still) returns what it's declared to. This
4478  * will raise an error if wrong, but that's okay since the function would
4479  * fail at runtime anyway. Note that check_sql_fn_retval will also insert
4480  * a RelabelType if needed to make the tlist expression match the declared
4481  * type of the function.
4482  *
4483  * Note: we do not try this until we have verified that no rewriting was
4484  * needed; that's probably not important, but let's be careful.
4485  */
4486  if (check_sql_fn_retval(funcid, result_type, list_make1(querytree),
4487  &modifyTargetList, NULL))
4488  goto fail; /* reject whole-tuple-result cases */
4489 
4490  /* Now we can grab the tlist expression */
4491  newexpr = (Node *) ((TargetEntry *) linitial(querytree->targetList))->expr;
4492 
4493  /* Assert that check_sql_fn_retval did the right thing */
4494  Assert(exprType(newexpr) == result_type);
4495  /* It couldn't have made any dangerous tlist changes, either */
4496  Assert(!modifyTargetList);
4497 
4498  /*
4499  * Additional validity checks on the expression. It mustn't be more
4500  * volatile than the surrounding function (this is to avoid breaking hacks
4501  * that involve pretending a function is immutable when it really ain't).
4502  * If the surrounding function is declared strict, then the expression
4503  * must contain only strict constructs and must use all of the function
4504  * parameters (this is overkill, but an exact analysis is hard).
4505  */
4506  if (funcform->provolatile == PROVOLATILE_IMMUTABLE &&
4507  contain_mutable_functions(newexpr))
4508  goto fail;
4509  else if (funcform->provolatile == PROVOLATILE_STABLE &&
4510  contain_volatile_functions(newexpr))
4511  goto fail;
4512 
4513  if (funcform->proisstrict &&
4514  contain_nonstrict_functions(newexpr))
4515  goto fail;
4516 
4517  /*
4518  * If any parameter expression contains a context-dependent node, we can't
4519  * inline, for fear of putting such a node into the wrong context.
4520  */
4521  if (contain_context_dependent_node((Node *) args))
4522  goto fail;
4523 
4524  /*
4525  * We may be able to do it; there are still checks on parameter usage to
4526  * make, but those are most easily done in combination with the actual
4527  * substitution of the inputs. So start building expression with inputs
4528  * substituted.
4529  */
4530  usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
4531  newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
4532  args, usecounts);
4533 
4534  /* Now check for parameter usage */
4535  i = 0;
4536  foreach(arg, args)
4537  {
4538  Node *param = lfirst(arg);
4539 
4540  if (usecounts[i] == 0)
4541  {
4542  /* Param not used at all: uncool if func is strict */
4543  if (funcform->proisstrict)
4544  goto fail;
4545  }
4546  else if (usecounts[i] != 1)
4547  {
4548  /* Param used multiple times: uncool if expensive or volatile */
4549  QualCost eval_cost;
4550 
4551  /*
4552  * We define "expensive" as "contains any subplan or more than 10
4553  * operators". Note that the subplan search has to be done
4554  * explicitly, since cost_qual_eval() will barf on unplanned
4555  * subselects.
4556  */
4557  if (contain_subplans(param))
4558  goto fail;
4559  cost_qual_eval(&eval_cost, list_make1(param), NULL);
4560  if (eval_cost.startup + eval_cost.per_tuple >
4561  10 * cpu_operator_cost)
4562  goto fail;
4563 
4564  /*
4565  * Check volatility last since this is more expensive than the
4566  * above tests
4567  */
4568  if (contain_volatile_functions(param))
4569  goto fail;
4570  }
4571  i++;
4572  }
4573 
4574  /*
4575  * Whew --- we can make the substitution. Copy the modified expression
4576  * out of the temporary memory context, and clean up.
4577  */
4578  MemoryContextSwitchTo(oldcxt);
4579 
4580  newexpr = copyObject(newexpr);
4581 
4582  MemoryContextDelete(mycxt);
4583 
4584  /*
4585  * If the result is of a collatable type, force the result to expose the
4586  * correct collation. In most cases this does not matter, but it's
4587  * possible that the function result is used directly as a sort key or in
4588  * other places where we expect exprCollation() to tell the truth.
4589  */
4590  if (OidIsValid(result_collid))
4591  {
4592  Oid exprcoll = exprCollation(newexpr);
4593 
4594  if (OidIsValid(exprcoll) && exprcoll != result_collid)
4595  {
4596  CollateExpr *newnode = makeNode(CollateExpr);
4597 
4598  newnode->arg = (Expr *) newexpr;
4599  newnode->collOid = result_collid;
4600  newnode->location = -1;
4601 
4602  newexpr = (Node *) newnode;
4603  }
4604  }
4605 
4606  /*
4607  * Since there is now no trace of the function in the plan tree, we must
4608  * explicitly record the plan's dependency on the function.
4609  */
4610  if (context->root)
4611  record_plan_function_dependency(context->root, funcid);
4612 
4613  /*
4614  * Recursively try to simplify the modified expression. Here we must add
4615  * the current function to the context list of active functions.
4616  */
4617  context->active_fns = lcons_oid(funcid, context->active_fns);
4618  newexpr = eval_const_expressions_mutator(newexpr, context);
4619  context->active_fns = list_delete_first(context->active_fns);
4620 
4621  error_context_stack = sqlerrcontext.previous;
4622 
4623  return (Expr *) newexpr;
4624 
4625  /* Here if func is not inlinable: release temp memory and return NULL */
4626 fail:
4627  MemoryContextSwitchTo(oldcxt);
4628  MemoryContextDelete(mycxt);
4629  error_context_stack = sqlerrcontext.previous;
4630 
4631  return NULL;
4632 }
4633 
4634 /*
4635  * Replace Param nodes by appropriate actual parameters
4636  */
4637 static Node *
4639  int *usecounts)
4640 {
4642 
4643  context.nargs = nargs;
4644  context.args = args;
4645  context.usecounts = usecounts;
4646 
4647  return substitute_actual_parameters_mutator(expr, &context);
4648 }
4649 
4650 static Node *
4653 {
4654  if (node == NULL)
4655  return NULL;
4656  if (IsA(node, Param))
4657  {
4658  Param *param = (Param *) node;
4659 
4660  if (param->paramkind != PARAM_EXTERN)
4661  elog(ERROR, "unexpected paramkind: %d", (int) param->paramkind);
4662  if (param->paramid <= 0 || param->paramid > context->nargs)
4663  elog(ERROR, "invalid paramid: %d", param->paramid);
4664 
4665  /* Count usage of parameter */
4666  context->usecounts[param->paramid - 1]++;
4667 
4668  /* Select the appropriate actual arg and replace the Param with it */
4669  /* We don't need to copy at this time (it'll get done later) */
4670  return list_nth(context->args, param->paramid - 1);
4671  }
4673  (void *) context);
4674 }
4675 
4676 /*
4677  * error context callback to let us supply a call-stack traceback
4678  */
4679 static void
4681 {
4682  inline_error_callback_arg *callback_arg = (inline_error_callback_arg *) arg;
4683  int syntaxerrposition;
4684 
4685  /* If it's a syntax error, convert to internal syntax error report */
4686  syntaxerrposition = geterrposition();
4687  if (syntaxerrposition > 0)
4688  {
4689  errposition(0);
4690  internalerrposition(syntaxerrposition);
4691  internalerrquery(callback_arg->prosrc);
4692  }
4693 
4694  errcontext("SQL function \"%s\" during inlining", callback_arg->proname);
4695 }
4696 
4697 /*
4698  * evaluate_expr: pre-evaluate a constant expression
4699  *
4700  * We use the executor's routine ExecEvalExpr() to avoid duplication of
4701  * code and ensure we get the same result as the executor would get.
4702  */
4703 static Expr *
4704 evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod,
4705  Oid result_collation)
4706 {
4707  EState *estate;
4708  ExprState *exprstate;
4709  MemoryContext oldcontext;
4710  Datum const_val;
4711  bool const_is_null;
4712  int16 resultTypLen;
4713  bool resultTypByVal;
4714 
4715  /*
4716  * To use the executor, we need an EState.
4717  */
4718  estate = CreateExecutorState();
4719 
4720  /* We can use the estate's working context to avoid memory leaks. */
4721  oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
4722 
4723  /* Make sure any opfuncids are filled in. */
4724  fix_opfuncids((Node *) expr);
4725 
4726  /*
4727  * Prepare expr for execution. (Note: we can't use ExecPrepareExpr
4728  * because it'd result in recursively invoking eval_const_expressions.)
4729  */
4730  exprstate = ExecInitExpr(expr, NULL);
4731 
4732  /*
4733  * And evaluate it.
4734  *
4735  * It is OK to use a default econtext because none of the ExecEvalExpr()
4736  * code used in this situation will use econtext. That might seem
4737  * fortuitous, but it's not so unreasonable --- a constant expression does
4738  * not depend on context, by definition, n'est ce pas?
4739  */
4740  const_val = ExecEvalExprSwitchContext(exprstate,
4741  GetPerTupleExprContext(estate),
4742  &const_is_null);
4743 
4744  /* Get info needed about result datatype */
4745  get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
4746 
4747  /* Get back to outer memory context */
4748  MemoryContextSwitchTo(oldcontext);
4749 
4750  /*
4751  * Must copy result out of sub-context used by expression eval.
4752  *
4753  * Also, if it's varlena, forcibly detoast it. This protects us against
4754  * storing TOAST pointers into plans that might outlive the referenced
4755  * data. (makeConst would handle detoasting anyway, but it's worth a few
4756  * extra lines here so that we can do the copy and detoast in one step.)
4757  */
4758  if (!const_is_null)
4759  {
4760  if (resultTypLen == -1)
4761  const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
4762  else
4763  const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
4764  }
4765 
4766  /* Release all the junk we just created */
4767  FreeExecutorState(estate);
4768 
4769  /*
4770  * Make the constant result node.
4771  */
4772  return (Expr *) makeConst(result_type, result_typmod, result_collation,
4773  resultTypLen,
4774  const_val, const_is_null,
4775  resultTypByVal);
4776 }
4777 
4778 
4779 /*
4780  * inline_set_returning_function
4781  * Attempt to "inline" a set-returning function in the FROM clause.
4782  *
4783  * "rte" is an RTE_FUNCTION rangetable entry. If it represents a call of a
4784  * set-returning SQL function that can safely be inlined, expand the function
4785  * and return the substitute Query structure. Otherwise, return NULL.
4786  *
4787  * This has a good deal of similarity to inline_function(), but that's
4788  * for the non-set-returning case, and there are enough differences to
4789  * justify separate functions.
4790  */
4791 Query *
4793 {
4794  RangeTblFunction *rtfunc;
4795  FuncExpr *fexpr;
4796  Oid func_oid;
4797  HeapTuple func_tuple;
4798  Form_pg_proc funcform;
4799  char *src;
4800  Datum tmp;
4801  bool isNull;
4802  bool modifyTargetList;
4803  MemoryContext oldcxt;
4804  MemoryContext mycxt;
4805  List *saveInvalItems;
4806  inline_error_callback_arg callback_arg;
4807  ErrorContextCallback sqlerrcontext;
4809  List *raw_parsetree_list;
4810  List *querytree_list;
4811  Query *querytree;
4812 
4813  Assert(rte->rtekind == RTE_FUNCTION);
4814 
4815  /*
4816  * It doesn't make a lot of sense for a SQL SRF to refer to itself in its
4817  * own FROM clause, since that must cause infinite recursion at runtime.
4818  * It will cause this code to recurse too, so check for stack overflow.
4819  * (There's no need to do more.)
4820  */
4822 
4823  /* Fail if the RTE has ORDINALITY - we don't implement that here. */
4824  if (rte->funcordinality)
4825  return NULL;
4826 
4827  /* Fail if RTE isn't a single, simple FuncExpr */
4828  if (list_length(rte->functions) != 1)
4829  return NULL;
4830  rtfunc = (RangeTblFunction *) linitial(rte->functions);
4831 
4832  if (!IsA(rtfunc->funcexpr, FuncExpr))
4833  return NULL;
4834  fexpr = (FuncExpr *) rtfunc->funcexpr;
4835 
4836  func_oid = fexpr->funcid;
4837 
4838  /*
4839  * The function must be declared to return a set, else inlining would
4840  * change the results if the contained SELECT didn't return exactly one
4841  * row.
4842  */
4843  if (!fexpr->funcretset)
4844  return NULL;
4845 
4846  /*
4847  * Refuse to inline if the arguments contain any volatile functions or
4848  * sub-selects. Volatile functions are rejected because inlining may
4849  * result in the arguments being evaluated multiple times, risking a
4850  * change in behavior. Sub-selects are rejected partly for implementation
4851  * reasons (pushing them down another level might change their behavior)
4852  * and partly because they're likely to be expensive and so multiple
4853  * evaluation would be bad.
4854  */
4855  if (contain_volatile_functions((Node *) fexpr->args) ||
4856  contain_subplans((Node *) fexpr->args))
4857  return NULL;
4858 
4859  /* Check permission to call function (fail later, if not) */
4860  if (pg_proc_aclcheck(func_oid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
4861  return NULL;
4862 
4863  /* Check whether a plugin wants to hook function entry/exit */
4864  if (FmgrHookIsNeeded(func_oid))
4865  return NULL;
4866 
4867  /*
4868  * OK, let's take a look at the function's pg_proc entry.
4869  */
4870  func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_oid));
4871  if (!HeapTupleIsValid(func_tuple))
4872  elog(ERROR, "cache lookup failed for function %u", func_oid);
4873  funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4874 
4875  /*
4876  * Forget it if the function is not SQL-language or has other showstopper
4877  * properties. In particular it mustn't be declared STRICT, since we
4878  * couldn't enforce that. It also mustn't be VOLATILE, because that is
4879  * supposed to cause it to be executed with its own snapshot, rather than
4880  * sharing the snapshot of the calling query. (Rechecking proretset is
4881  * just paranoia.)
4882  */
4883  if (funcform->prolang != SQLlanguageId ||
4884  funcform->proisstrict ||
4885  funcform->provolatile == PROVOLATILE_VOLATILE ||
4886  funcform->prosecdef ||
4887  !funcform->proretset ||
4888  !heap_attisnull(func_tuple, Anum_pg_proc_proconfig))
4889  {
4890  ReleaseSysCache(func_tuple);
4891  return NULL;
4892  }
4893 
4894  /*
4895  * Make a temporary memory context, so that we don't leak all the stuff
4896  * that parsing might create.
4897  */
4899  "inline_set_returning_function",
4901  oldcxt = MemoryContextSwitchTo(mycxt);
4902 
4903  /*
4904  * When we call eval_const_expressions below, it might try to add items to
4905  * root->glob->invalItems. Since it is running in the temp context, those
4906  * items will be in that context, and will need to be copied out if we're
4907  * successful. Temporarily reset the list so that we can keep those items
4908  * separate from the pre-existing list contents.
4909  */
4910  saveInvalItems = root->glob->invalItems;
4911  root->glob->invalItems = NIL;
4912 
4913  /* Fetch the function body */
4914  tmp = SysCacheGetAttr(PROCOID,
4915  func_tuple,
4917  &isNull);
4918  if (isNull)
4919  elog(ERROR, "null prosrc for function %u", func_oid);
4920  src = TextDatumGetCString(tmp);
4921 
4922  /*
4923  * Setup error traceback support for ereport(). This is so that we can
4924  * finger the function that bad information came from.
4925  */
4926  callback_arg.proname = NameStr(funcform->proname);
4927  callback_arg.prosrc = src;
4928 
4929  sqlerrcontext.callback = sql_inline_error_callback;
4930  sqlerrcontext.arg = (void *) &callback_arg;
4931  sqlerrcontext.previous = error_context_stack;
4932  error_context_stack = &sqlerrcontext;
4933 
4934  /*
4935  * Run eval_const_expressions on the function call. This is necessary to
4936  * ensure that named-argument notation is converted to positional notation
4937  * and any default arguments are inserted. It's a bit of overkill for the
4938  * arguments, since they'll get processed again later, but no harm will be
4939  * done.
4940  */
4941  fexpr = (FuncExpr *) eval_const_expressions(root, (Node *) fexpr);
4942 
4943  /* It should still be a call of the same function, but let's check */
4944  if (!IsA(fexpr, FuncExpr) ||
4945  fexpr->funcid != func_oid)
4946  goto fail;
4947 
4948  /* Arg list length should now match the function */
4949  if (list_length(fexpr->args) != funcform->pronargs)
4950  goto fail;
4951 
4952  /*
4953  * Set up to handle parameters while parsing the function body. We can
4954  * use the FuncExpr just created as the input for
4955  * prepare_sql_fn_parse_info.
4956  */
4957  pinfo = prepare_sql_fn_parse_info(func_tuple,
4958  (Node *) fexpr,
4959  fexpr->inputcollid);
4960 
4961  /*
4962  * Parse, analyze, and rewrite (unlike inline_function(), we can't skip
4963  * rewriting here). We can fail as soon as we find more than one query,
4964  * though.
4965  */
4966  raw_parsetree_list = pg_parse_query(src);
4967  if (list_length(raw_parsetree_list) != 1)
4968  goto fail;
4969 
4970  querytree_list = pg_analyze_and_rewrite_params(linitial(raw_parsetree_list),
4971  src,
4973  pinfo, NULL);
4974  if (list_length(querytree_list) != 1)
4975  goto fail;
4976  querytree = linitial(querytree_list);
4977 
4978  /*
4979  * The single command must be a plain SELECT.
4980  */
4981  if (!IsA(querytree, Query) ||
4982  querytree->commandType != CMD_SELECT)
4983  goto fail;
4984 
4985  /*
4986  * Make sure the function (still) returns what it's declared to. This
4987  * will raise an error if wrong, but that's okay since the function would
4988  * fail at runtime anyway. Note that check_sql_fn_retval will also insert
4989  * RelabelType(s) and/or NULL columns if needed to make the tlist
4990  * expression(s) match the declared type of the function.
4991  *
4992  * If the function returns a composite type, don't inline unless the check
4993  * shows it's returning a whole tuple result; otherwise what it's
4994  * returning is a single composite column which is not what we need.
4995  */
4996  if (!check_sql_fn_retval(func_oid, fexpr->funcresulttype,
4997  querytree_list,
4998  &modifyTargetList, NULL) &&
5000  fexpr->funcresulttype == RECORDOID))
5001  goto fail; /* reject not-whole-tuple-result cases */
5002 
5003  /*
5004  * If we had to modify the tlist to make it match, and the statement is
5005  * one in which changing the tlist contents could change semantics, we
5006  * have to punt and not inline.
5007  */
5008  if (modifyTargetList)
5009  goto fail;
5010 
5011  /*
5012  * If it returns RECORD, we have to check against the column type list
5013  * provided in the RTE; check_sql_fn_retval can't do that. (If no match,
5014  * we just fail to inline, rather than complaining; see notes for
5015  * tlist_matches_coltypelist.) We don't have to do this for functions
5016  * with declared OUT parameters, even though their funcresulttype is
5017  * RECORDOID, so check get_func_result_type too.
5018  */
5019  if (fexpr->funcresulttype == RECORDOID &&
5020  get_func_result_type(func_oid, NULL, NULL) == TYPEFUNC_RECORD &&
5022  rtfunc->funccoltypes))
5023  goto fail;
5024 
5025  /*
5026  * Looks good --- substitute parameters into the query.
5027  */
5028  querytree = substitute_actual_srf_parameters(querytree,
5029  funcform->pronargs,
5030  fexpr->args);
5031 
5032  /*
5033  * Copy the modified query out of the temporary memory context, and clean
5034  * up.
5035  */
5036  MemoryContextSwitchTo(oldcxt);
5037 
5038  querytree = copyObject(querytree);
5039 
5040  /* copy up any new invalItems, too */
5041  root->glob->invalItems = list_concat(saveInvalItems,
5042  copyObject(root->glob->invalItems));
5043 
5044  MemoryContextDelete(mycxt);
5045  error_context_stack = sqlerrcontext.previous;
5046  ReleaseSysCache(func_tuple);
5047 
5048  /*
5049  * We don't have to fix collations here because the upper query is already
5050  * parsed, ie, the collations in the RTE are what count.
5051  */
5052 
5053  /*
5054  * Since there is now no trace of the function in the plan tree, we must
5055  * explicitly record the plan's dependency on the function.
5056  */
5057  record_plan_function_dependency(root, func_oid);
5058 
5059  return querytree;
5060 
5061  /* Here if func is not inlinable: release temp memory and return NULL */
5062 fail:
5063  MemoryContextSwitchTo(oldcxt);
5064  root->glob->invalItems = saveInvalItems;
5065  MemoryContextDelete(mycxt);
5066  error_context_stack = sqlerrcontext.previous;
5067  ReleaseSysCache(func_tuple);
5068 
5069  return NULL;
5070 }
5071 
5072 /*
5073  * Replace Param nodes by appropriate actual parameters
5074  *
5075  * This is just enough different from substitute_actual_parameters()
5076  * that it needs its own code.
5077  */
5078 static Query *
5080 {
5082 
5083  context.nargs = nargs;
5084  context.args = args;
5085  context.sublevels_up = 1;
5086 
5087  return query_tree_mutator(expr,
5089  &context,
5090  0);
5091 }
5092 
5093 static Node *
5096 {
5097  Node *result;
5098 
5099  if (node == NULL)
5100  return NULL;
5101  if (IsA(node, Query))
5102  {
5103  context->sublevels_up++;
5104  result = (Node *) query_tree_mutator((Query *) node,
5106  (void *) context,
5107  0);
5108  context->sublevels_up--;
5109  return result;
5110  }
5111  if (IsA(node, Param))
5112  {
5113  Param *param = (Param *) node;
5114 
5115  if (param->paramkind == PARAM_EXTERN)
5116  {
5117  if (param->paramid <= 0 || param->paramid > context->nargs)
5118  elog(ERROR, "invalid paramid: %d", param->paramid);
5119 
5120  /*
5121  * Since the parameter is being inserted into a subquery, we must
5122  * adjust levels.
5123  */
5124  result = copyObject(list_nth(context->args, param->paramid - 1));
5125  IncrementVarSublevelsUp(result, context->sublevels_up, 0);
5126  return result;
5127  }
5128  }
5129  return expression_tree_mutator(node,
5131  (void *) context);
5132 }
5133 
5134 /*
5135  * Check whether a SELECT targetlist emits the specified column types,
5136  * to see if it's safe to inline a function returning record.
5137  *
5138  * We insist on exact match here. The executor allows binary-coercible
5139  * cases too, but we don't have a way to preserve the correct column types
5140  * in the correct places if we inline the function in such a case.
5141  *
5142  * Note that we only check type OIDs not typmods; this agrees with what the
5143  * executor would do at runtime, and attributing a specific typmod to a
5144  * function result is largely wishful thinking anyway.
5145  */
5146 static bool
5147 tlist_matches_coltypelist(List *tlist, List *coltypelist)
5148 {
5149  ListCell *tlistitem;
5150  ListCell *clistitem;
5151 
5152  clistitem = list_head(coltypelist);
5153  foreach(tlistitem, tlist)
5154  {
5155  TargetEntry *tle = (TargetEntry *) lfirst(tlistitem);
5156  Oid coltype;
5157 
5158  if (tle->resjunk)
5159  continue; /* ignore junk columns */
5160 
5161  if (clistitem == NULL)
5162  return false; /* too many tlist items */
5163 
5164  coltype = lfirst_oid(clistitem);
5165  clistitem = lnext(clistitem);
5166 
5167  if (exprType((Node *) tle->expr) != coltype)
5168  return false; /* column type mismatch */
5169  }
5170 
5171  if (clistitem != NULL)
5172  return false; /* too few tlist items */
5173 
5174  return true;
5175 }
Datum constvalue
Definition: primnodes.h:196
#define list_make2(x1, x2)
Definition: pg_list.h:140
#define list_make3(x1, x2, x3)
Definition: pg_list.h:141
Expr * get_notclausearg(Expr *notclause)
Definition: clauses.c:265
List * aggdistinct
Definition: primnodes.h:303
signed short int16
Definition: c.h:245
char maxParallelHazard
Definition: relation.h:133
Node * limitOffset
Definition: parsenodes.h:158
Oid funcresulttype
Definition: primnodes.h:450
void cost_qual_eval_node(QualCost *cost, Node *qual, PlannerInfo *root)
Definition: costsize.c:3489
bool multidims
Definition: primnodes.h:956
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:76
Node * make_and_qual(Node *qual1, Node *qual2)
Definition: clauses.c:348
#define NIL
Definition: pg_list.h:69
Datum value
Definition: params.h:56
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2246
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1622
Datum boolop(PG_FUNCTION_ARGS)
Definition: _int_bool.c:420
bool contain_leaked_vars(Node *clause)
Definition: clauses.c:1481
void * stringToNode(char *str)
Definition: read.c:38
List * args
Definition: primnodes.h:986
AggClauseCosts * costs
Definition: clauses.c:60
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
static Expr * simplify_function(Oid funcid, Oid result_type, int32 result_typmod, Oid result_collid, Oid input_collid, List **args_p, bool funcvariadic, bool process_args, bool allow_non_const, eval_const_expressions_context *context)
Definition: clauses.c:3887
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
bool is_pseudo_constant_clause_relids(Node *clause, Relids relids)
Definition: clauses.c:2216
bool contain_volatile_functions_not_nextval(Node *clause)
Definition: clauses.c:1007
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:301
Node * negate_clause(Node *node)
Definition: prepqual.c:73
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1313
Index varlevelsup
Definition: primnodes.h:173
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2410
#define PG_DETOAST_DATUM_COPY(datum)
Definition: fmgr.h:207
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2632
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
static Expr * evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod, Oid result_collation)
Definition: clauses.c:4704
#define PROVOLATILE_IMMUTABLE
Definition: pg_proc.h:5533
List * sortClause
Definition: parsenodes.h:156
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:773
List * args
Definition: primnodes.h:359
List * args
Definition: primnodes.h:457
static bool contain_mutable_functions_walker(Node *node, void *context)
Definition: clauses.c:890
Oid wincollid
Definition: primnodes.h:357
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1486
FromExpr * jointree
Definition: parsenodes.h:136
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2454
Oid GetUserId(void)
Definition: miscinit.c:284
Index maxWinRef
Definition: clauses.h:26
Oid resulttype
Definition: primnodes.h:744
double expression_returns_set_rows(Node *clause)
Definition: clauses.c:802
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
#define TYPTYPE_COMPOSITE
Definition: pg_type.h:721
#define OIDOID
Definition: pg_type.h:328
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:276
static bool contain_volatile_functions_not_nextval_walker(Node *node, void *context)
Definition: clauses.c:1020
float4 get_func_rows(Oid funcid)
Definition: lsyscache.c:1660
#define PointerGetDatum(X)
Definition: postgres.h:562
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
Oid funccollid
Definition: primnodes.h:455
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:203
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1582
void sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
Definition: functions.c:273
Oid resulttype
Definition: primnodes.h:812
RowCompareType rctype
Definition: primnodes.h:1031
void get_agg_clause_costs(PlannerInfo *root, Node *clause, AggSplit aggsplit, AggClauseCosts *costs)
Definition: clauses.c:467
bool hasAggs
Definition: parsenodes.h:123
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:75
int numWindowFuncs
Definition: clauses.h:25
WindowFuncLists * find_window_functions(Node *clause, Index maxWinRef)
Definition: clauses.c:740
Oid casecollid
Definition: primnodes.h:907
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define Anum_pg_proc_prosrc
Definition: pg_proc.h:115
Expr * arg
Definition: primnodes.h:791
#define INT4OID
Definition: pg_type.h:316
List * groupingSets
Definition: parsenodes.h:148
ParamKind paramkind
Definition: primnodes.h:244
List * list_copy(const List *oldlist)
Definition: list.c:1160
Definition: nodes.h:509
static bool contain_agg_clause_walker(Node *node, void *context)
Definition: clauses.c:423
Query * inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
Definition: clauses.c:4792
bool check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList, bool *modifyTargetList, JunkFilter **junkFilter)
Definition: functions.c:1532
List * args
Definition: primnodes.h:301
char get_typtype(Oid typid)
Definition: lsyscache.c:2379
void CommuteOpExpr(OpExpr *clause)
Definition: clauses.c:2253
#define MemSet(start, val, len)
Definition: c.h:846
Oid array_typeid
Definition: primnodes.h:952
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2421
Expr * arg
Definition: primnodes.h:742
List * list_concat(List *list1, List *list2)
Definition: list.c:321
static bool contain_mutable_functions_checker(Oid func_id, void *context)
Definition: clauses.c:884
#define PROPARALLEL_RESTRICTED
Definition: pg_proc.h:5543
List * paramIds
Definition: primnodes.h:687
bool funcretset
Definition: primnodes.h:451
bool hasNonSerial
Definition: relation.h:61
static bool contain_nonstrict_functions_walker(Node *node, void *context)
Definition: clauses.c:1300
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: clauses.c:172
List * lcons_oid(Oid datum, List *list)
Definition: list.c:295
List * fromlist
Definition: primnodes.h:1471
bool contain_var_clause(Node *node)
Definition: var.c:331
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:957
void make_fn_arguments(ParseState *pstate, List *fargs, Oid *actual_arg_types, Oid *declared_arg_types)
Definition: parse_func.c:1711
bool hasNonPartial
Definition: relation.h:60
bool funcordinality
Definition: parsenodes.h:1000
QualCost transCost
Definition: relation.h:62
float4 get_func_cost(Oid funcid)
Definition: lsyscache.c:1641
Oid casetype
Definition: primnodes.h:906
unsigned int Oid
Definition: postgres_ext.h:31
List * rowMarks
Definition: parsenodes.h:161
Index winref
Definition: primnodes.h:361
Definition: primnodes.h:163
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:296
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
struct ErrorContextCallback * previous
Definition: elog.h:238
#define OidIsValid(objectId)
Definition: c.h:532
#define FmgrHookIsNeeded(fn_oid)
Definition: fmgr.h:729
#define BooleanEqualOperator
Definition: pg_operator.h:114
#define DO_AGGSPLIT_COMBINE(as)
Definition: nodes.h:768
int natts
Definition: tupdesc.h:73
Node * quals
Definition: primnodes.h:1472
#define lsecond(l)
Definition: pg_list.h:116
int location
Definition: primnodes.h:564
Cost startup
Definition: relation.h:45
int location
Definition: primnodes.h:922
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:159
#define Anum_pg_proc_proconfig
Definition: pg_proc.h:117
static bool get_agg_clause_costs_walker(Node *node, get_agg_clause_costs_context *context)
Definition: clauses.c:479
signed int int32
Definition: c.h:246
char max_parallel_hazard(Query *parse)
Definition: clauses.c:1068
List * windowClause
Definition: parsenodes.h:152
List * targetList
Definition: parsenodes.h:138
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:334
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
List * find_forced_null_vars(Node *node)
Definition: clauses.c:2027
ErrorContextCallback * error_context_stack
Definition: elog.c:88
Expr * make_ands_explicit(List *andclauses)
Definition: clauses.c:367
#define FUNC_MAX_ARGS
bool check_functions_in_node(Node *node, check_function_callback checker, void *context)
Definition: nodeFuncs.c:1651
#define list_make1(x1)
Definition: pg_list.h:139
static Node * substitute_actual_srf_parameters_mutator(Node *node, substitute_actual_srf_parameters_context *context)
Definition: clauses.c:5094
bool get_typbyval(Oid typid)
Definition: lsyscache.c:1972
bool contain_subplans(Node *clause)
Definition: clauses.c:843
Oid consttype
Definition: primnodes.h:192
void FreeExecutorState(EState *estate)
Definition: execUtils.c:183
#define GetPerTupleExprContext(estate)
Definition: executor.h:476
bool is_parallel_safe(PlannerInfo *root, Node *node)
Definition: clauses.c:1087
CoercionForm funcformat
Definition: primnodes.h:454
static void recheck_cast_function_args(List *args, Oid result_type, HeapTuple func_tuple)
Definition: clauses.c:4157
Cost per_tuple
Definition: relation.h:46
static bool contain_volatile_functions_walker(Node *node, void *context)
Definition: clauses.c:969
#define DO_AGGSPLIT_SERIALIZE(as)
Definition: nodes.h:770
static Node * eval_const_expressions_mutator(Node *node, eval_const_expressions_context *context)
Definition: clauses.c:2468
Oid opresulttype
Definition: primnodes.h:498
void pfree(void *pointer)
Definition: mcxt.c:949
MemoryContext es_query_cxt
Definition: execnodes.h:471
ParamListInfo boundParams
Definition: clauses.c:65
bool resjunk
Definition: primnodes.h:1375
#define linitial(l)
Definition: pg_list.h:111
List * rtable
Definition: parsenodes.h:135
List * make_ands_implicit(Expr *clause)
Definition: clauses.c:378
List * distinctClause
Definition: parsenodes.h:154
Oid funcid
Definition: primnodes.h:449
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define CCDN_IN_CASEEXPR
Definition: clauses.c:1422
Expr * phexpr
Definition: relation.h:1906
bool list_member(const List *list, const void *datum)
Definition: list.c:444
static bool rowtype_field_matches(Oid rowtypeid, int fieldnum, Oid expectedtype, int32 expectedtypmod, Oid expectedcollation)
Definition: clauses.c:2353
static Node * substitute_actual_parameters_mutator(Node *node, substitute_actual_parameters_context *context)
Definition: clauses.c:4651
#define is_opclause(clause)
Definition: clauses.h:20
Oid paramcollid
Definition: primnodes.h:248
#define SQLlanguageId
Definition: pg_language.h:80
#define PROVOLATILE_STABLE
Definition: pg_proc.h:5534
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:3463
List * args
Definition: primnodes.h:1047
Node * get_leftop(const Expr *clause)
Definition: clauses.c:199
#define ARR_DIMS(a)
Definition: array.h:275
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:838
List * pg_parse_query(const char *query_string)
Definition: postgres.c:603
Oid enforce_generic_type_consistency(Oid *actual_arg_types, Oid *declared_arg_types, int nargs, Oid rettype, bool allow_poly)
BoolExprType boolop
Definition: primnodes.h:562
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:354
Expr * arg
Definition: primnodes.h:1180
static bool max_parallel_hazard_test(char proparallel, max_parallel_hazard_context *context)
Definition: clauses.c:1109
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:623
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
Oid constcollid
Definition: primnodes.h:194
Oid resultcollid
Definition: primnodes.h:747
#define lfirst_node(type, lc)
Definition: pg_list.h:109
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:605
bool list_member_int(const List *list, int datum)
Definition: list.c:485
bool and_clause(Node *clause)
Definition: clauses.c:314
Node * limitCount
Definition: parsenodes.h:159
Relids find_nonnullable_rels(Node *clause)
Definition: clauses.c:1626
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:179
struct Const Const