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