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