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