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