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