1 /*-------------------------------------------------------------------------
4 * routines to manipulate qualification clauses
6 * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/optimizer/util/clauses.c
14 * AUTHOR DATE MAJOR EVENT
15 * Andrew Yu Nov 3, 1994 clause.c and clauses.c combined
17 *-------------------------------------------------------------------------
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"
32 #include "miscadmin.h"
33 #include "nodes/makefuncs.h"
34 #include "nodes/nodeFuncs.h"
35 #include "optimizer/clauses.h"
36 #include "optimizer/cost.h"
37 #include "optimizer/planmain.h"
38 #include "optimizer/prep.h"
39 #include "optimizer/var.h"
40 #include "parser/analyze.h"
41 #include "parser/parse_agg.h"
42 #include "parser/parse_coerce.h"
43 #include "parser/parse_func.h"
44 #include "rewrite/rewriteManip.h"
45 #include "tcop/tcopprot.h"
46 #include "utils/acl.h"
47 #include "utils/builtins.h"
48 #include "utils/datum.h"
49 #include "utils/fmgroids.h"
50 #include "utils/lsyscache.h"
51 #include "utils/memutils.h"
52 #include "utils/syscache.h"
53 #include "utils/typcache.h"
60 AggClauseCosts *costs;
61 } get_agg_clause_costs_context;
65 ParamListInfo boundParams;
70 } eval_const_expressions_context;
77 } substitute_actual_parameters_context;
84 } substitute_actual_srf_parameters_context;
90 } inline_error_callback_arg;
94 char max_hazard; /* worst proparallel hazard found so far */
95 char max_interesting; /* worst proparallel hazard of interest */
96 List *safe_param_ids; /* PARAM_EXEC Param IDs to treat as safe */
97 } max_parallel_hazard_context;
99 static bool contain_agg_clause_walker(Node *node, void *context);
100 static bool get_agg_clause_costs_walker(Node *node,
101 get_agg_clause_costs_context *context);
102 static bool find_window_functions_walker(Node *node, WindowFuncLists *lists);
103 static bool contain_subplans_walker(Node *node, void *context);
104 static bool contain_mutable_functions_walker(Node *node, void *context);
105 static bool contain_volatile_functions_walker(Node *node, void *context);
106 static bool contain_volatile_functions_not_nextval_walker(Node *node, void *context);
107 static bool max_parallel_hazard_walker(Node *node,
108 max_parallel_hazard_context *context);
109 static bool contain_nonstrict_functions_walker(Node *node, void *context);
110 static bool contain_context_dependent_node(Node *clause);
111 static bool contain_context_dependent_node_walker(Node *node, int *flags);
112 static bool contain_leaked_vars_walker(Node *node, void *context);
113 static Relids find_nonnullable_rels_walker(Node *node, bool top_level);
114 static List *find_nonnullable_vars_walker(Node *node, bool top_level);
115 static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK);
116 static Node *eval_const_expressions_mutator(Node *node,
117 eval_const_expressions_context *context);
118 static bool contain_non_const_walker(Node *node, void *context);
119 static bool ece_function_is_safe(Oid funcid,
120 eval_const_expressions_context *context);
121 static List *simplify_or_arguments(List *args,
122 eval_const_expressions_context *context,
123 bool *haveNull, bool *forceTrue);
124 static List *simplify_and_arguments(List *args,
125 eval_const_expressions_context *context,
126 bool *haveNull, bool *forceFalse);
127 static Node *simplify_boolean_equality(Oid opno, List *args);
128 static Expr *simplify_function(Oid funcid,
129 Oid result_type, int32 result_typmod,
130 Oid result_collid, Oid input_collid, List **args_p,
131 bool funcvariadic, bool process_args, bool allow_non_const,
132 eval_const_expressions_context *context);
133 static List *reorder_function_arguments(List *args, HeapTuple func_tuple);
134 static List *add_function_defaults(List *args, HeapTuple func_tuple);
135 static List *fetch_function_defaults(HeapTuple func_tuple);
136 static void recheck_cast_function_args(List *args, Oid result_type,
137 HeapTuple func_tuple);
138 static Expr *evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
139 Oid result_collid, Oid input_collid, List *args,
141 HeapTuple func_tuple,
142 eval_const_expressions_context *context);
143 static Expr *inline_function(Oid funcid, Oid result_type, Oid result_collid,
144 Oid input_collid, List *args,
146 HeapTuple func_tuple,
147 eval_const_expressions_context *context);
148 static Node *substitute_actual_parameters(Node *expr, int nargs, List *args,
150 static Node *substitute_actual_parameters_mutator(Node *node,
151 substitute_actual_parameters_context *context);
152 static void sql_inline_error_callback(void *arg);
153 static Expr *evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod,
154 Oid result_collation);
155 static Query *substitute_actual_srf_parameters(Query *expr,
156 int nargs, List *args);
157 static Node *substitute_actual_srf_parameters_mutator(Node *node,
158 substitute_actual_srf_parameters_context *context);
159 static bool tlist_matches_coltypelist(List *tlist, List *coltypelist);
162 /*****************************************************************************
163 * OPERATOR clause functions
164 *****************************************************************************/
168 * Creates an operator clause given its operator info, left operand
169 * and right operand (pass NULL to create single-operand clause),
170 * and collation info.
173 make_opclause(Oid opno, Oid opresulttype, bool opretset,
174 Expr *leftop, Expr *rightop,
175 Oid opcollid, Oid inputcollid)
177 OpExpr *expr = makeNode(OpExpr);
180 expr->opfuncid = InvalidOid;
181 expr->opresulttype = opresulttype;
182 expr->opretset = opretset;
183 expr->opcollid = opcollid;
184 expr->inputcollid = inputcollid;
186 expr->args = list_make2(leftop, rightop);
188 expr->args = list_make1(leftop);
190 return (Expr *) expr;
196 * Returns the left operand of a clause of the form (op expr expr)
200 get_leftop(const Expr *clause)
202 const OpExpr *expr = (const OpExpr *) clause;
204 if (expr->args != NIL)
205 return linitial(expr->args);
213 * Returns the right operand in a clause of the form (op expr expr).
214 * NB: result will be NULL if applied to a unary op clause.
217 get_rightop(const Expr *clause)
219 const OpExpr *expr = (const OpExpr *) clause;
221 if (list_length(expr->args) >= 2)
222 return lsecond(expr->args);
227 /*****************************************************************************
228 * NOT clause functions
229 *****************************************************************************/
234 * Returns t iff this is a 'not' clause: (NOT expr).
237 not_clause(Node *clause)
239 return (clause != NULL &&
240 IsA(clause, BoolExpr) &&
241 ((BoolExpr *) clause)->boolop == NOT_EXPR);
247 * Create a 'not' clause given the expression to be negated.
250 make_notclause(Expr *notclause)
252 BoolExpr *expr = makeNode(BoolExpr);
254 expr->boolop = NOT_EXPR;
255 expr->args = list_make1(notclause);
257 return (Expr *) expr;
263 * Retrieve the clause within a 'not' clause
266 get_notclausearg(Expr *notclause)
268 return linitial(((BoolExpr *) notclause)->args);
271 /*****************************************************************************
272 * OR clause functions
273 *****************************************************************************/
278 * Returns t iff the clause is an 'or' clause: (OR { expr }).
281 or_clause(Node *clause)
283 return (clause != NULL &&
284 IsA(clause, BoolExpr) &&
285 ((BoolExpr *) clause)->boolop == OR_EXPR);
291 * Creates an 'or' clause given a list of its subclauses.
294 make_orclause(List *orclauses)
296 BoolExpr *expr = makeNode(BoolExpr);
298 expr->boolop = OR_EXPR;
299 expr->args = orclauses;
301 return (Expr *) expr;
304 /*****************************************************************************
305 * AND clause functions
306 *****************************************************************************/
312 * Returns t iff its argument is an 'and' clause: (AND { expr }).
315 and_clause(Node *clause)
317 return (clause != NULL &&
318 IsA(clause, BoolExpr) &&
319 ((BoolExpr *) clause)->boolop == AND_EXPR);
325 * Creates an 'and' clause given a list of its subclauses.
328 make_andclause(List *andclauses)
330 BoolExpr *expr = makeNode(BoolExpr);
332 expr->boolop = AND_EXPR;
333 expr->args = andclauses;
335 return (Expr *) expr;
341 * Variant of make_andclause for ANDing two qual conditions together.
342 * Qual conditions have the property that a NULL nodetree is interpreted
345 * NB: this makes no attempt to preserve AND/OR flatness; so it should not
346 * be used on a qual that has already been run through prepqual.c.
349 make_and_qual(Node *qual1, Node *qual2)
355 return (Node *) make_andclause(list_make2(qual1, qual2));
359 * The planner frequently prefers to represent qualification expressions
360 * as lists of boolean expressions with implicit AND semantics.
362 * These functions convert between an AND-semantics expression list and the
363 * ordinary representation of a boolean expression.
365 * Note that an empty list is considered equivalent to TRUE.
368 make_ands_explicit(List *andclauses)
370 if (andclauses == NIL)
371 return (Expr *) makeBoolConst(true, false);
372 else if (list_length(andclauses) == 1)
373 return (Expr *) linitial(andclauses);
375 return make_andclause(andclauses);
379 make_ands_implicit(Expr *clause)
382 * NB: because the parser sets the qual field to NULL in a query that has
383 * no WHERE clause, we must consider a NULL input clause as TRUE, even
384 * though one might more reasonably think it FALSE. Grumble. If this
385 * causes trouble, consider changing the parser's behavior.
388 return NIL; /* NULL -> NIL list == TRUE */
389 else if (and_clause((Node *) clause))
390 return ((BoolExpr *) clause)->args;
391 else if (IsA(clause, Const) &&
392 !((Const *) clause)->constisnull &&
393 DatumGetBool(((Const *) clause)->constvalue))
394 return NIL; /* constant TRUE input -> NIL list */
396 return list_make1(clause);
400 /*****************************************************************************
401 * Aggregate-function clause manipulation
402 *****************************************************************************/
406 * Recursively search for Aggref/GroupingFunc nodes within a clause.
408 * Returns true if any aggregate found.
410 * This does not descend into subqueries, and so should be used only after
411 * reduction of sublinks to subplans, or in contexts where it's known there
412 * are no subqueries. There mustn't be outer-aggregate references either.
414 * (If you want something like this but able to deal with subqueries,
415 * see rewriteManip.c's contain_aggs_of_level().)
418 contain_agg_clause(Node *clause)
420 return contain_agg_clause_walker(clause, NULL);
424 contain_agg_clause_walker(Node *node, void *context)
428 if (IsA(node, Aggref))
430 Assert(((Aggref *) node)->agglevelsup == 0);
431 return true; /* abort the tree traversal and return true */
433 if (IsA(node, GroupingFunc))
435 Assert(((GroupingFunc *) node)->agglevelsup == 0);
436 return true; /* abort the tree traversal and return true */
438 Assert(!IsA(node, SubLink));
439 return expression_tree_walker(node, contain_agg_clause_walker, context);
443 * get_agg_clause_costs
444 * Recursively find the Aggref nodes in an expression tree, and
445 * accumulate cost information about them.
447 * 'aggsplit' tells us the expected partial-aggregation mode, which affects
448 * the cost estimates.
450 * NOTE that the counts/costs are ADDED to those already in *costs ... so
451 * the caller is responsible for zeroing the struct initially.
453 * We count the nodes, estimate their execution costs, and estimate the total
454 * space needed for their transition state values if all are evaluated in
455 * parallel (as would be done in a HashAgg plan). Also, we check whether
456 * partial aggregation is feasible. See AggClauseCosts for the exact set
457 * of statistics collected.
459 * In addition, we mark Aggref nodes with the correct aggtranstype, so
460 * that that doesn't need to be done repeatedly. (That makes this function's
461 * name a bit of a misnomer.)
463 * This does not descend into subqueries, and so should be used only after
464 * reduction of sublinks to subplans, or in contexts where it's known there
465 * are no subqueries. There mustn't be outer-aggregate references either.
468 get_agg_clause_costs(PlannerInfo *root, Node *clause, AggSplit aggsplit,
469 AggClauseCosts *costs)
471 get_agg_clause_costs_context context;
474 context.aggsplit = aggsplit;
475 context.costs = costs;
476 (void) get_agg_clause_costs_walker(clause, &context);
480 get_agg_clause_costs_walker(Node *node, get_agg_clause_costs_context *context)
484 if (IsA(node, Aggref))
486 Aggref *aggref = (Aggref *) node;
487 AggClauseCosts *costs = context->costs;
489 Form_pg_aggregate aggform;
499 Assert(aggref->agglevelsup == 0);
502 * Fetch info about aggregate from pg_aggregate. Note it's correct to
503 * ignore the moving-aggregate variant, since what we're concerned
504 * with here is aggregates not window functions.
506 aggTuple = SearchSysCache1(AGGFNOID,
507 ObjectIdGetDatum(aggref->aggfnoid));
508 if (!HeapTupleIsValid(aggTuple))
509 elog(ERROR, "cache lookup failed for aggregate %u",
511 aggform = (Form_pg_aggregate) GETSTRUCT(aggTuple);
512 aggtransfn = aggform->aggtransfn;
513 aggfinalfn = aggform->aggfinalfn;
514 aggcombinefn = aggform->aggcombinefn;
515 aggserialfn = aggform->aggserialfn;
516 aggdeserialfn = aggform->aggdeserialfn;
517 aggtranstype = aggform->aggtranstype;
518 aggtransspace = aggform->aggtransspace;
519 ReleaseSysCache(aggTuple);
522 * Resolve the possibly-polymorphic aggregate transition type, unless
523 * already done in a previous pass over the expression.
525 if (OidIsValid(aggref->aggtranstype))
526 aggtranstype = aggref->aggtranstype;
529 Oid inputTypes[FUNC_MAX_ARGS];
532 /* extract argument types (ignoring any ORDER BY expressions) */
533 numArguments = get_aggregate_argtypes(aggref, inputTypes);
535 /* resolve actual type of transition state, if polymorphic */
536 aggtranstype = resolve_aggregate_transtype(aggref->aggfnoid,
540 aggref->aggtranstype = aggtranstype;
544 * Count it, and check for cases requiring ordered input. Note that
545 * ordered-set aggs always have nonempty aggorder. Any ordered-input
546 * case also defeats partial aggregation.
549 if (aggref->aggorder != NIL || aggref->aggdistinct != NIL)
551 costs->numOrderedAggs++;
552 costs->hasNonPartial = true;
556 * Check whether partial aggregation is feasible, unless we already
557 * found out that we can't do it.
559 if (!costs->hasNonPartial)
562 * If there is no combine function, then partial aggregation is
565 if (!OidIsValid(aggcombinefn))
566 costs->hasNonPartial = true;
569 * If we have any aggs with transtype INTERNAL then we must check
570 * whether they have serialization/deserialization functions; if
571 * not, we can't serialize partial-aggregation results.
573 else if (aggtranstype == INTERNALOID &&
574 (!OidIsValid(aggserialfn) || !OidIsValid(aggdeserialfn)))
575 costs->hasNonSerial = true;
579 * Add the appropriate component function execution costs to
580 * appropriate totals.
582 if (DO_AGGSPLIT_COMBINE(context->aggsplit))
584 /* charge for combining previously aggregated states */
585 costs->transCost.per_tuple += get_func_cost(aggcombinefn) * cpu_operator_cost;
588 costs->transCost.per_tuple += get_func_cost(aggtransfn) * cpu_operator_cost;
589 if (DO_AGGSPLIT_DESERIALIZE(context->aggsplit) &&
590 OidIsValid(aggdeserialfn))
591 costs->transCost.per_tuple += get_func_cost(aggdeserialfn) * cpu_operator_cost;
592 if (DO_AGGSPLIT_SERIALIZE(context->aggsplit) &&
593 OidIsValid(aggserialfn))
594 costs->finalCost += get_func_cost(aggserialfn) * cpu_operator_cost;
595 if (!DO_AGGSPLIT_SKIPFINAL(context->aggsplit) &&
596 OidIsValid(aggfinalfn))
597 costs->finalCost += get_func_cost(aggfinalfn) * cpu_operator_cost;
600 * These costs are incurred only by the initial aggregate node, so we
601 * mustn't include them again at upper levels.
603 if (!DO_AGGSPLIT_COMBINE(context->aggsplit))
605 /* add the input expressions' cost to per-input-row costs */
606 cost_qual_eval_node(&argcosts, (Node *) aggref->args, context->root);
607 costs->transCost.startup += argcosts.startup;
608 costs->transCost.per_tuple += argcosts.per_tuple;
611 * Add any filter's cost to per-input-row costs.
613 * XXX Ideally we should reduce input expression costs according
614 * to filter selectivity, but it's not clear it's worth the
617 if (aggref->aggfilter)
619 cost_qual_eval_node(&argcosts, (Node *) aggref->aggfilter,
621 costs->transCost.startup += argcosts.startup;
622 costs->transCost.per_tuple += argcosts.per_tuple;
627 * If there are direct arguments, treat their evaluation cost like the
628 * cost of the finalfn.
630 if (aggref->aggdirectargs)
632 cost_qual_eval_node(&argcosts, (Node *) aggref->aggdirectargs,
634 costs->transCost.startup += argcosts.startup;
635 costs->finalCost += argcosts.per_tuple;
639 * If the transition type is pass-by-value then it doesn't add
640 * anything to the required size of the hashtable. If it is
641 * pass-by-reference then we have to add the estimated size of the
642 * value itself, plus palloc overhead.
644 if (!get_typbyval(aggtranstype))
648 /* Use average width if aggregate definition gave one */
649 if (aggtransspace > 0)
650 avgwidth = aggtransspace;
651 else if (aggtransfn == F_ARRAY_APPEND)
654 * If the transition function is array_append(), it'll use an
655 * expanded array as transvalue, which will occupy at least
656 * ALLOCSET_SMALL_INITSIZE and possibly more. Use that as the
657 * estimate for lack of a better idea.
659 avgwidth = ALLOCSET_SMALL_INITSIZE;
664 * If transition state is of same type as first aggregated
665 * input, assume it's the same typmod (same width) as well.
666 * This works for cases like MAX/MIN and is probably somewhat
667 * reasonable otherwise.
669 int32 aggtranstypmod = -1;
673 TargetEntry *tle = (TargetEntry *) linitial(aggref->args);
675 if (aggtranstype == exprType((Node *) tle->expr))
676 aggtranstypmod = exprTypmod((Node *) tle->expr);
679 avgwidth = get_typavgwidth(aggtranstype, aggtranstypmod);
682 avgwidth = MAXALIGN(avgwidth);
683 costs->transitionSpace += avgwidth + 2 * sizeof(void *);
685 else if (aggtranstype == INTERNALOID)
688 * INTERNAL transition type is a special case: although INTERNAL
689 * is pass-by-value, it's almost certainly being used as a pointer
690 * to some large data structure. The aggregate definition can
691 * provide an estimate of the size. If it doesn't, then we assume
692 * ALLOCSET_DEFAULT_INITSIZE, which is a good guess if the data is
693 * being kept in a private memory context, as is done by
694 * array_agg() for instance.
696 if (aggtransspace > 0)
697 costs->transitionSpace += aggtransspace;
699 costs->transitionSpace += ALLOCSET_DEFAULT_INITSIZE;
703 * We assume that the parser checked that there are no aggregates (of
704 * this level anyway) in the aggregated arguments, direct arguments,
705 * or filter clause. Hence, we need not recurse into any of them.
709 Assert(!IsA(node, SubLink));
710 return expression_tree_walker(node, get_agg_clause_costs_walker,
715 /*****************************************************************************
716 * Window-function clause manipulation
717 *****************************************************************************/
720 * contain_window_function
721 * Recursively search for WindowFunc nodes within a clause.
723 * Since window functions don't have level fields, but are hard-wired to
724 * be associated with the current query level, this is just the same as
725 * rewriteManip.c's function.
728 contain_window_function(Node *clause)
730 return contain_windowfuncs(clause);
734 * find_window_functions
735 * Locate all the WindowFunc nodes in an expression tree, and organize
736 * them by winref ID number.
738 * Caller must provide an upper bound on the winref IDs expected in the tree.
741 find_window_functions(Node *clause, Index maxWinRef)
743 WindowFuncLists *lists = palloc(sizeof(WindowFuncLists));
745 lists->numWindowFuncs = 0;
746 lists->maxWinRef = maxWinRef;
747 lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
748 (void) find_window_functions_walker(clause, lists);
753 find_window_functions_walker(Node *node, WindowFuncLists *lists)
757 if (IsA(node, WindowFunc))
759 WindowFunc *wfunc = (WindowFunc *) node;
761 /* winref is unsigned, so one-sided test is OK */
762 if (wfunc->winref > lists->maxWinRef)
763 elog(ERROR, "WindowFunc contains out-of-range winref %u",
765 /* eliminate duplicates, so that we avoid repeated computation */
766 if (!list_member(lists->windowFuncs[wfunc->winref], wfunc))
768 lists->windowFuncs[wfunc->winref] =
769 lappend(lists->windowFuncs[wfunc->winref], wfunc);
770 lists->numWindowFuncs++;
774 * We assume that the parser checked that there are no window
775 * functions in the arguments or filter clause. Hence, we need not
776 * recurse into them. (If either the parser or the planner screws up
777 * on this point, the executor will still catch it; see ExecInitExpr.)
781 Assert(!IsA(node, SubLink));
782 return expression_tree_walker(node, find_window_functions_walker,
787 /*****************************************************************************
788 * Support for expressions returning sets
789 *****************************************************************************/
792 * expression_returns_set_rows
793 * Estimate the number of rows returned by a set-returning expression.
794 * The result is 1 if it's not a set-returning expression.
796 * We should only examine the top-level function or operator; it used to be
797 * appropriate to recurse, but not anymore. (Even if there are more SRFs in
798 * the function's inputs, their multipliers are accounted for separately.)
800 * Note: keep this in sync with expression_returns_set() in nodes/nodeFuncs.c.
803 expression_returns_set_rows(Node *clause)
807 if (IsA(clause, FuncExpr))
809 FuncExpr *expr = (FuncExpr *) clause;
811 if (expr->funcretset)
812 return clamp_row_est(get_func_rows(expr->funcid));
814 if (IsA(clause, OpExpr))
816 OpExpr *expr = (OpExpr *) clause;
821 return clamp_row_est(get_func_rows(expr->opfuncid));
828 /*****************************************************************************
829 * Subplan clause manipulation
830 *****************************************************************************/
834 * Recursively search for subplan nodes within a clause.
836 * If we see a SubLink node, we will return true. This is only possible if
837 * the expression tree hasn't yet been transformed by subselect.c. We do not
838 * know whether the node will produce a true subplan or just an initplan,
839 * but we make the conservative assumption that it will be a subplan.
841 * Returns true if any subplan found.
844 contain_subplans(Node *clause)
846 return contain_subplans_walker(clause, NULL);
850 contain_subplans_walker(Node *node, void *context)
854 if (IsA(node, SubPlan) ||
855 IsA(node, AlternativeSubPlan) ||
857 return true; /* abort the tree traversal and return true */
858 return expression_tree_walker(node, contain_subplans_walker, context);
862 /*****************************************************************************
863 * Check clauses for mutable functions
864 *****************************************************************************/
867 * contain_mutable_functions
868 * Recursively search for mutable functions within a clause.
870 * Returns true if any mutable function (or operator implemented by a
871 * mutable function) is found. This test is needed so that we don't
872 * mistakenly think that something like "WHERE random() < 0.5" can be treated
873 * as a constant qualification.
875 * We will recursively look into Query nodes (i.e., SubLink sub-selects)
876 * but not into SubPlans. See comments for contain_volatile_functions().
879 contain_mutable_functions(Node *clause)
881 return contain_mutable_functions_walker(clause, NULL);
885 contain_mutable_functions_checker(Oid func_id, void *context)
887 return (func_volatile(func_id) != PROVOLATILE_IMMUTABLE);
891 contain_mutable_functions_walker(Node *node, void *context)
895 /* Check for mutable functions in node itself */
896 if (check_functions_in_node(node, contain_mutable_functions_checker,
900 if (IsA(node, SQLValueFunction))
902 /* all variants of SQLValueFunction are stable */
906 if (IsA(node, NextValueExpr))
908 /* NextValueExpr is volatile */
913 * It should be safe to treat MinMaxExpr as immutable, because it will
914 * depend on a non-cross-type btree comparison function, and those should
915 * always be immutable. Treating XmlExpr as immutable is more dubious,
916 * and treating CoerceToDomain as immutable is outright dangerous. But we
917 * have done so historically, and changing this would probably cause more
918 * problems than it would fix. In practice, if you have a non-immutable
919 * domain constraint you are in for pain anyhow.
922 /* Recurse to check arguments */
923 if (IsA(node, Query))
925 /* Recurse into subselects */
926 return query_tree_walker((Query *) node,
927 contain_mutable_functions_walker,
930 return expression_tree_walker(node, contain_mutable_functions_walker,
935 /*****************************************************************************
936 * Check clauses for volatile functions
937 *****************************************************************************/
940 * contain_volatile_functions
941 * Recursively search for volatile functions within a clause.
943 * Returns true if any volatile function (or operator implemented by a
944 * volatile function) is found. This test prevents, for example,
945 * invalid conversions of volatile expressions into indexscan quals.
947 * We will recursively look into Query nodes (i.e., SubLink sub-selects)
948 * but not into SubPlans. This is a bit odd, but intentional. If we are
949 * looking at a SubLink, we are probably deciding whether a query tree
950 * transformation is safe, and a contained sub-select should affect that;
951 * for example, duplicating a sub-select containing a volatile function
952 * would be bad. However, once we've got to the stage of having SubPlans,
953 * subsequent planning need not consider volatility within those, since
954 * the executor won't change its evaluation rules for a SubPlan based on
958 contain_volatile_functions(Node *clause)
960 return contain_volatile_functions_walker(clause, NULL);
964 contain_volatile_functions_checker(Oid func_id, void *context)
966 return (func_volatile(func_id) == PROVOLATILE_VOLATILE);
970 contain_volatile_functions_walker(Node *node, void *context)
974 /* Check for volatile functions in node itself */
975 if (check_functions_in_node(node, contain_volatile_functions_checker,
979 if (IsA(node, NextValueExpr))
981 /* NextValueExpr is volatile */
986 * See notes in contain_mutable_functions_walker about why we treat
987 * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
988 * SQLValueFunction is stable. Hence, none of them are of interest here.
991 /* Recurse to check arguments */
992 if (IsA(node, Query))
994 /* Recurse into subselects */
995 return query_tree_walker((Query *) node,
996 contain_volatile_functions_walker,
999 return expression_tree_walker(node, contain_volatile_functions_walker,
1004 * Special purpose version of contain_volatile_functions() for use in COPY:
1005 * ignore nextval(), but treat all other functions normally.
1008 contain_volatile_functions_not_nextval(Node *clause)
1010 return contain_volatile_functions_not_nextval_walker(clause, NULL);
1014 contain_volatile_functions_not_nextval_checker(Oid func_id, void *context)
1016 return (func_id != F_NEXTVAL_OID &&
1017 func_volatile(func_id) == PROVOLATILE_VOLATILE);
1021 contain_volatile_functions_not_nextval_walker(Node *node, void *context)
1025 /* Check for volatile functions in node itself */
1026 if (check_functions_in_node(node,
1027 contain_volatile_functions_not_nextval_checker,
1032 * See notes in contain_mutable_functions_walker about why we treat
1033 * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable, while
1034 * SQLValueFunction is stable. Hence, none of them are of interest here.
1035 * Also, since we're intentionally ignoring nextval(), presumably we
1036 * should ignore NextValueExpr.
1039 /* Recurse to check arguments */
1040 if (IsA(node, Query))
1042 /* Recurse into subselects */
1043 return query_tree_walker((Query *) node,
1044 contain_volatile_functions_not_nextval_walker,
1047 return expression_tree_walker(node,
1048 contain_volatile_functions_not_nextval_walker,
1053 /*****************************************************************************
1054 * Check queries for parallel unsafe and/or restricted constructs
1055 *****************************************************************************/
1058 * max_parallel_hazard
1059 * Find the worst parallel-hazard level in the given query
1061 * Returns the worst function hazard property (the earliest in this list:
1062 * PROPARALLEL_UNSAFE, PROPARALLEL_RESTRICTED, PROPARALLEL_SAFE) that can
1063 * be found in the given parsetree. We use this to find out whether the query
1064 * can be parallelized at all. The caller will also save the result in
1065 * PlannerGlobal so as to short-circuit checks of portions of the querytree
1066 * later, in the common case where everything is SAFE.
1069 max_parallel_hazard(Query *parse)
1071 max_parallel_hazard_context context;
1073 context.max_hazard = PROPARALLEL_SAFE;
1074 context.max_interesting = PROPARALLEL_UNSAFE;
1075 context.safe_param_ids = NIL;
1076 (void) max_parallel_hazard_walker((Node *) parse, &context);
1077 return context.max_hazard;
1082 * Detect whether the given expr contains only parallel-safe functions
1084 * root->glob->maxParallelHazard must previously have been set to the
1085 * result of max_parallel_hazard() on the whole query.
1088 is_parallel_safe(PlannerInfo *root, Node *node)
1090 max_parallel_hazard_context context;
1095 * Even if the original querytree contained nothing unsafe, we need to
1096 * search the expression if we have generated any PARAM_EXEC Params while
1097 * planning, because those are parallel-restricted and there might be one
1098 * in this expression. But otherwise we don't need to look.
1100 if (root->glob->maxParallelHazard == PROPARALLEL_SAFE &&
1101 root->glob->paramExecTypes == NIL)
1103 /* Else use max_parallel_hazard's search logic, but stop on RESTRICTED */
1104 context.max_hazard = PROPARALLEL_SAFE;
1105 context.max_interesting = PROPARALLEL_RESTRICTED;
1106 context.safe_param_ids = NIL;
1109 * The params that refer to the same or parent query level are considered
1110 * parallel-safe. The idea is that we compute such params at Gather or
1111 * Gather Merge node and pass their value to workers.
1113 for (proot = root; proot != NULL; proot = proot->parent_root)
1115 foreach(l, proot->init_plans)
1117 SubPlan *initsubplan = (SubPlan *) lfirst(l);
1120 foreach(l2, initsubplan->setParam)
1121 context.safe_param_ids = lcons_int(lfirst_int(l2),
1122 context.safe_param_ids);
1126 return !max_parallel_hazard_walker(node, &context);
1129 /* core logic for all parallel-hazard checks */
1131 max_parallel_hazard_test(char proparallel, max_parallel_hazard_context *context)
1133 switch (proparallel)
1135 case PROPARALLEL_SAFE:
1136 /* nothing to see here, move along */
1138 case PROPARALLEL_RESTRICTED:
1139 /* increase max_hazard to RESTRICTED */
1140 Assert(context->max_hazard != PROPARALLEL_UNSAFE);
1141 context->max_hazard = proparallel;
1142 /* done if we are not expecting any unsafe functions */
1143 if (context->max_interesting == proparallel)
1146 case PROPARALLEL_UNSAFE:
1147 context->max_hazard = proparallel;
1148 /* we're always done at the first unsafe construct */
1151 elog(ERROR, "unrecognized proparallel value \"%c\"", proparallel);
1157 /* check_functions_in_node callback */
1159 max_parallel_hazard_checker(Oid func_id, void *context)
1161 return max_parallel_hazard_test(func_parallel(func_id),
1162 (max_parallel_hazard_context *) context);
1166 max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context)
1171 /* Check for hazardous functions in node itself */
1172 if (check_functions_in_node(node, max_parallel_hazard_checker,
1177 * It should be OK to treat MinMaxExpr as parallel-safe, since btree
1178 * opclass support functions are generally parallel-safe. XmlExpr is a
1179 * bit more dubious but we can probably get away with it. We err on the
1180 * side of caution by treating CoerceToDomain as parallel-restricted.
1181 * (Note: in principle that's wrong because a domain constraint could
1182 * contain a parallel-unsafe function; but useful constraints probably
1183 * never would have such, and assuming they do would cripple use of
1184 * parallel query in the presence of domain types.) SQLValueFunction
1185 * should be safe in all cases. NextValueExpr is parallel-unsafe.
1187 if (IsA(node, CoerceToDomain))
1189 if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
1193 if (IsA(node, NextValueExpr))
1195 if (max_parallel_hazard_test(PROPARALLEL_UNSAFE, context))
1200 * As a notational convenience for callers, look through RestrictInfo.
1202 else if (IsA(node, RestrictInfo))
1204 RestrictInfo *rinfo = (RestrictInfo *) node;
1206 return max_parallel_hazard_walker((Node *) rinfo->clause, context);
1210 * Really we should not see SubLink during a max_interesting == restricted
1211 * scan, but if we do, return true.
1213 else if (IsA(node, SubLink))
1215 if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
1220 * Only parallel-safe SubPlans can be sent to workers. Within the
1221 * testexpr of the SubPlan, Params representing the output columns of the
1222 * subplan can be treated as parallel-safe, so temporarily add their IDs
1223 * to the safe_param_ids list while examining the testexpr.
1225 else if (IsA(node, SubPlan))
1227 SubPlan *subplan = (SubPlan *) node;
1228 List *save_safe_param_ids;
1230 if (!subplan->parallel_safe &&
1231 max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
1233 save_safe_param_ids = context->safe_param_ids;
1234 context->safe_param_ids = list_concat(list_copy(subplan->paramIds),
1235 context->safe_param_ids);
1236 if (max_parallel_hazard_walker(subplan->testexpr, context))
1237 return true; /* no need to restore safe_param_ids */
1238 context->safe_param_ids = save_safe_param_ids;
1239 /* we must also check args, but no special Param treatment there */
1240 if (max_parallel_hazard_walker((Node *) subplan->args, context))
1242 /* don't want to recurse normally, so we're done */
1247 * We can't pass Params to workers at the moment either, so they are also
1248 * parallel-restricted, unless they are PARAM_EXTERN Params or are
1249 * PARAM_EXEC Params listed in safe_param_ids, meaning they could be
1250 * either generated within the worker or can be computed in master and
1251 * then their value can be passed to the worker.
1253 else if (IsA(node, Param))
1255 Param *param = (Param *) node;
1257 if (param->paramkind == PARAM_EXTERN)
1260 if (param->paramkind != PARAM_EXEC ||
1261 !list_member_int(context->safe_param_ids, param->paramid))
1263 if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
1266 return false; /* nothing to recurse to */
1270 * When we're first invoked on a completely unplanned tree, we must
1271 * recurse into subqueries so to as to locate parallel-unsafe constructs
1272 * anywhere in the tree.
1274 else if (IsA(node, Query))
1276 Query *query = (Query *) node;
1278 /* SELECT FOR UPDATE/SHARE must be treated as unsafe */
1279 if (query->rowMarks != NULL)
1281 context->max_hazard = PROPARALLEL_UNSAFE;
1285 /* Recurse into subselects */
1286 return query_tree_walker(query,
1287 max_parallel_hazard_walker,
1291 /* Recurse to check arguments */
1292 return expression_tree_walker(node,
1293 max_parallel_hazard_walker,
1298 /*****************************************************************************
1299 * Check clauses for nonstrict functions
1300 *****************************************************************************/
1303 * contain_nonstrict_functions
1304 * Recursively search for nonstrict functions within a clause.
1306 * Returns true if any nonstrict construct is found --- ie, anything that
1307 * could produce non-NULL output with a NULL input.
1309 * The idea here is that the caller has verified that the expression contains
1310 * one or more Var or Param nodes (as appropriate for the caller's need), and
1311 * now wishes to prove that the expression result will be NULL if any of these
1312 * inputs is NULL. If we return false, then the proof succeeded.
1315 contain_nonstrict_functions(Node *clause)
1317 return contain_nonstrict_functions_walker(clause, NULL);
1321 contain_nonstrict_functions_checker(Oid func_id, void *context)
1323 return !func_strict(func_id);
1327 contain_nonstrict_functions_walker(Node *node, void *context)
1331 if (IsA(node, Aggref))
1333 /* an aggregate could return non-null with null input */
1336 if (IsA(node, GroupingFunc))
1339 * A GroupingFunc doesn't evaluate its arguments, and therefore must
1340 * be treated as nonstrict.
1344 if (IsA(node, WindowFunc))
1346 /* a window function could return non-null with null input */
1349 if (IsA(node, ArrayRef))
1351 /* array assignment is nonstrict, but subscripting is strict */
1352 if (((ArrayRef *) node)->refassgnexpr != NULL)
1354 /* else fall through to check args */
1356 if (IsA(node, DistinctExpr))
1358 /* IS DISTINCT FROM is inherently non-strict */
1361 if (IsA(node, NullIfExpr))
1363 /* NULLIF is inherently non-strict */
1366 if (IsA(node, BoolExpr))
1368 BoolExpr *expr = (BoolExpr *) node;
1370 switch (expr->boolop)
1374 /* AND, OR are inherently non-strict */
1380 if (IsA(node, SubLink))
1382 /* In some cases a sublink might be strict, but in general not */
1385 if (IsA(node, SubPlan))
1387 if (IsA(node, AlternativeSubPlan))
1389 if (IsA(node, FieldStore))
1391 if (IsA(node, ArrayCoerceExpr))
1394 * ArrayCoerceExpr is strict at the array level, regardless of what
1395 * the per-element expression is; so we should ignore elemexpr and
1396 * recurse only into the arg.
1398 return expression_tree_walker((Node *) ((ArrayCoerceExpr *) node)->arg,
1399 contain_nonstrict_functions_walker,
1402 if (IsA(node, CaseExpr))
1404 if (IsA(node, ArrayExpr))
1406 if (IsA(node, RowExpr))
1408 if (IsA(node, RowCompareExpr))
1410 if (IsA(node, CoalesceExpr))
1412 if (IsA(node, MinMaxExpr))
1414 if (IsA(node, XmlExpr))
1416 if (IsA(node, NullTest))
1418 if (IsA(node, BooleanTest))
1421 /* Check other function-containing nodes */
1422 if (check_functions_in_node(node, contain_nonstrict_functions_checker,
1426 return expression_tree_walker(node, contain_nonstrict_functions_walker,
1430 /*****************************************************************************
1431 * Check clauses for context-dependent nodes
1432 *****************************************************************************/
1435 * contain_context_dependent_node
1436 * Recursively search for context-dependent nodes within a clause.
1438 * CaseTestExpr nodes must appear directly within the corresponding CaseExpr,
1439 * not nested within another one, or they'll see the wrong test value. If one
1440 * appears "bare" in the arguments of a SQL function, then we can't inline the
1441 * SQL function for fear of creating such a situation.
1443 * CoerceToDomainValue would have the same issue if domain CHECK expressions
1444 * could get inlined into larger expressions, but presently that's impossible.
1445 * Still, it might be allowed in future, or other node types with similar
1446 * issues might get invented. So give this function a generic name, and set
1447 * up the recursion state to allow multiple flag bits.
1450 contain_context_dependent_node(Node *clause)
1454 return contain_context_dependent_node_walker(clause, &flags);
1457 #define CCDN_IN_CASEEXPR 0x0001 /* CaseTestExpr okay here? */
1460 contain_context_dependent_node_walker(Node *node, int *flags)
1464 if (IsA(node, CaseTestExpr))
1465 return !(*flags & CCDN_IN_CASEEXPR);
1466 if (IsA(node, CaseExpr))
1468 CaseExpr *caseexpr = (CaseExpr *) node;
1471 * If this CASE doesn't have a test expression, then it doesn't create
1472 * a context in which CaseTestExprs should appear, so just fall
1473 * through and treat it as a generic expression node.
1477 int save_flags = *flags;
1481 * Note: in principle, we could distinguish the various sub-parts
1482 * of a CASE construct and set the flag bit only for some of them,
1483 * since we are only expecting CaseTestExprs to appear in the
1484 * "expr" subtree of the CaseWhen nodes. But it doesn't really
1485 * seem worth any extra code. If there are any bare CaseTestExprs
1486 * elsewhere in the CASE, something's wrong already.
1488 *flags |= CCDN_IN_CASEEXPR;
1489 res = expression_tree_walker(node,
1490 contain_context_dependent_node_walker,
1492 *flags = save_flags;
1496 return expression_tree_walker(node, contain_context_dependent_node_walker,
1500 /*****************************************************************************
1501 * Check clauses for Vars passed to non-leakproof functions
1502 *****************************************************************************/
1505 * contain_leaked_vars
1506 * Recursively scan a clause to discover whether it contains any Var
1507 * nodes (of the current query level) that are passed as arguments to
1510 * Returns true if the clause contains any non-leakproof functions that are
1511 * passed Var nodes of the current query level, and which might therefore leak
1512 * data. Such clauses must be applied after any lower-level security barrier
1516 contain_leaked_vars(Node *clause)
1518 return contain_leaked_vars_walker(clause, NULL);
1522 contain_leaked_vars_checker(Oid func_id, void *context)
1524 return !get_func_leakproof(func_id);
1528 contain_leaked_vars_walker(Node *node, void *context)
1533 switch (nodeTag(node))
1542 case T_NamedArgExpr:
1547 case T_CaseTestExpr:
1550 case T_SQLValueFunction:
1553 case T_NextValueExpr:
1557 * We know these node types don't contain function calls; but
1558 * something further down in the node tree might.
1564 case T_DistinctExpr:
1566 case T_ScalarArrayOpExpr:
1568 case T_ArrayCoerceExpr:
1571 * If node contains a leaky function call, and there's any Var
1572 * underneath it, reject.
1574 if (check_functions_in_node(node, contain_leaked_vars_checker,
1576 contain_var_clause(node))
1580 case T_RowCompareExpr:
1583 * It's worth special-casing this because a leaky comparison
1584 * function only compromises one pair of row elements, which
1585 * might not contain Vars while others do.
1587 RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1592 forthree(opid, rcexpr->opnos,
1593 larg, rcexpr->largs,
1594 rarg, rcexpr->rargs)
1596 Oid funcid = get_opcode(lfirst_oid(opid));
1598 if (!get_func_leakproof(funcid) &&
1599 (contain_var_clause((Node *) lfirst(larg)) ||
1600 contain_var_clause((Node *) lfirst(rarg))))
1606 case T_CurrentOfExpr:
1609 * WHERE CURRENT OF doesn't contain leaky function calls.
1610 * Moreover, it is essential that this is considered non-leaky,
1611 * since the planner must always generate a TID scan when CURRENT
1612 * OF is present -- cf. cost_tidscan.
1619 * If we don't recognize the node tag, assume it might be leaky.
1620 * This prevents an unexpected security hole if someone adds a new
1621 * node type that can call a function.
1625 return expression_tree_walker(node, contain_leaked_vars_walker,
1630 * find_nonnullable_rels
1631 * Determine which base rels are forced nonnullable by given clause.
1633 * Returns the set of all Relids that are referenced in the clause in such
1634 * a way that the clause cannot possibly return TRUE if any of these Relids
1635 * is an all-NULL row. (It is OK to err on the side of conservatism; hence
1636 * the analysis here is simplistic.)
1638 * The semantics here are subtly different from contain_nonstrict_functions:
1639 * that function is concerned with NULL results from arbitrary expressions,
1640 * but here we assume that the input is a Boolean expression, and wish to
1641 * see if NULL inputs will provably cause a FALSE-or-NULL result. We expect
1642 * the expression to have been AND/OR flattened and converted to implicit-AND
1645 * Note: this function is largely duplicative of find_nonnullable_vars().
1646 * The reason not to simplify this function into a thin wrapper around
1647 * find_nonnullable_vars() is that the tested conditions really are different:
1648 * a clause like "t1.v1 IS NOT NULL OR t1.v2 IS NOT NULL" does not prove
1649 * that either v1 or v2 can't be NULL, but it does prove that the t1 row
1650 * as a whole can't be all-NULL.
1652 * top_level is true while scanning top-level AND/OR structure; here, showing
1653 * the result is either FALSE or NULL is good enough. top_level is false when
1654 * we have descended below a NOT or a strict function: now we must be able to
1655 * prove that the subexpression goes to NULL.
1657 * We don't use expression_tree_walker here because we don't want to descend
1658 * through very many kinds of nodes; only the ones we can be sure are strict.
1661 find_nonnullable_rels(Node *clause)
1663 return find_nonnullable_rels_walker(clause, true);
1667 find_nonnullable_rels_walker(Node *node, bool top_level)
1669 Relids result = NULL;
1676 Var *var = (Var *) node;
1678 if (var->varlevelsup == 0)
1679 result = bms_make_singleton(var->varno);
1681 else if (IsA(node, List))
1684 * At top level, we are examining an implicit-AND list: if any of the
1685 * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1686 * not at top level, we are examining the arguments of a strict
1687 * function: if any of them produce NULL then the result of the
1688 * function must be NULL. So in both cases, the set of nonnullable
1689 * rels is the union of those found in the arms, and we pass down the
1690 * top_level flag unmodified.
1692 foreach(l, (List *) node)
1694 result = bms_join(result,
1695 find_nonnullable_rels_walker(lfirst(l),
1699 else if (IsA(node, FuncExpr))
1701 FuncExpr *expr = (FuncExpr *) node;
1703 if (func_strict(expr->funcid))
1704 result = find_nonnullable_rels_walker((Node *) expr->args, false);
1706 else if (IsA(node, OpExpr))
1708 OpExpr *expr = (OpExpr *) node;
1711 if (func_strict(expr->opfuncid))
1712 result = find_nonnullable_rels_walker((Node *) expr->args, false);
1714 else if (IsA(node, ScalarArrayOpExpr))
1716 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1718 if (is_strict_saop(expr, true))
1719 result = find_nonnullable_rels_walker((Node *) expr->args, false);
1721 else if (IsA(node, BoolExpr))
1723 BoolExpr *expr = (BoolExpr *) node;
1725 switch (expr->boolop)
1728 /* At top level we can just recurse (to the List case) */
1731 result = find_nonnullable_rels_walker((Node *) expr->args,
1737 * Below top level, even if one arm produces NULL, the result
1738 * could be FALSE (hence not NULL). However, if *all* the
1739 * arms produce NULL then the result is NULL, so we can take
1740 * the intersection of the sets of nonnullable rels, just as
1741 * for OR. Fall through to share code.
1747 * OR is strict if all of its arms are, so we can take the
1748 * intersection of the sets of nonnullable rels for each arm.
1749 * This works for both values of top_level.
1751 foreach(l, expr->args)
1755 subresult = find_nonnullable_rels_walker(lfirst(l),
1757 if (result == NULL) /* first subresult? */
1760 result = bms_int_members(result, subresult);
1763 * If the intersection is empty, we can stop looking. This
1764 * also justifies the test for first-subresult above.
1766 if (bms_is_empty(result))
1771 /* NOT will return null if its arg is null */
1772 result = find_nonnullable_rels_walker((Node *) expr->args,
1776 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1780 else if (IsA(node, RelabelType))
1782 RelabelType *expr = (RelabelType *) node;
1784 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1786 else if (IsA(node, CoerceViaIO))
1788 /* not clear this is useful, but it can't hurt */
1789 CoerceViaIO *expr = (CoerceViaIO *) node;
1791 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1793 else if (IsA(node, ArrayCoerceExpr))
1795 /* ArrayCoerceExpr is strict at the array level; ignore elemexpr */
1796 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1798 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1800 else if (IsA(node, ConvertRowtypeExpr))
1802 /* not clear this is useful, but it can't hurt */
1803 ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1805 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1807 else if (IsA(node, CollateExpr))
1809 CollateExpr *expr = (CollateExpr *) node;
1811 result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1813 else if (IsA(node, NullTest))
1815 /* IS NOT NULL can be considered strict, but only at top level */
1816 NullTest *expr = (NullTest *) node;
1818 if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
1819 result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1821 else if (IsA(node, BooleanTest))
1823 /* Boolean tests that reject NULL are strict at top level */
1824 BooleanTest *expr = (BooleanTest *) node;
1827 (expr->booltesttype == IS_TRUE ||
1828 expr->booltesttype == IS_FALSE ||
1829 expr->booltesttype == IS_NOT_UNKNOWN))
1830 result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1832 else if (IsA(node, PlaceHolderVar))
1834 PlaceHolderVar *phv = (PlaceHolderVar *) node;
1836 result = find_nonnullable_rels_walker((Node *) phv->phexpr, top_level);
1842 * find_nonnullable_vars
1843 * Determine which Vars are forced nonnullable by given clause.
1845 * Returns a list of all level-zero Vars that are referenced in the clause in
1846 * such a way that the clause cannot possibly return TRUE if any of these Vars
1847 * is NULL. (It is OK to err on the side of conservatism; hence the analysis
1848 * here is simplistic.)
1850 * The semantics here are subtly different from contain_nonstrict_functions:
1851 * that function is concerned with NULL results from arbitrary expressions,
1852 * but here we assume that the input is a Boolean expression, and wish to
1853 * see if NULL inputs will provably cause a FALSE-or-NULL result. We expect
1854 * the expression to have been AND/OR flattened and converted to implicit-AND
1857 * The result is a palloc'd List, but we have not copied the member Var nodes.
1858 * Also, we don't bother trying to eliminate duplicate entries.
1860 * top_level is true while scanning top-level AND/OR structure; here, showing
1861 * the result is either FALSE or NULL is good enough. top_level is false when
1862 * we have descended below a NOT or a strict function: now we must be able to
1863 * prove that the subexpression goes to NULL.
1865 * We don't use expression_tree_walker here because we don't want to descend
1866 * through very many kinds of nodes; only the ones we can be sure are strict.
1869 find_nonnullable_vars(Node *clause)
1871 return find_nonnullable_vars_walker(clause, true);
1875 find_nonnullable_vars_walker(Node *node, bool top_level)
1884 Var *var = (Var *) node;
1886 if (var->varlevelsup == 0)
1887 result = list_make1(var);
1889 else if (IsA(node, List))
1892 * At top level, we are examining an implicit-AND list: if any of the
1893 * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1894 * not at top level, we are examining the arguments of a strict
1895 * function: if any of them produce NULL then the result of the
1896 * function must be NULL. So in both cases, the set of nonnullable
1897 * vars is the union of those found in the arms, and we pass down the
1898 * top_level flag unmodified.
1900 foreach(l, (List *) node)
1902 result = list_concat(result,
1903 find_nonnullable_vars_walker(lfirst(l),
1907 else if (IsA(node, FuncExpr))
1909 FuncExpr *expr = (FuncExpr *) node;
1911 if (func_strict(expr->funcid))
1912 result = find_nonnullable_vars_walker((Node *) expr->args, false);
1914 else if (IsA(node, OpExpr))
1916 OpExpr *expr = (OpExpr *) node;
1919 if (func_strict(expr->opfuncid))
1920 result = find_nonnullable_vars_walker((Node *) expr->args, false);
1922 else if (IsA(node, ScalarArrayOpExpr))
1924 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1926 if (is_strict_saop(expr, true))
1927 result = find_nonnullable_vars_walker((Node *) expr->args, false);
1929 else if (IsA(node, BoolExpr))
1931 BoolExpr *expr = (BoolExpr *) node;
1933 switch (expr->boolop)
1936 /* At top level we can just recurse (to the List case) */
1939 result = find_nonnullable_vars_walker((Node *) expr->args,
1945 * Below top level, even if one arm produces NULL, the result
1946 * could be FALSE (hence not NULL). However, if *all* the
1947 * arms produce NULL then the result is NULL, so we can take
1948 * the intersection of the sets of nonnullable vars, just as
1949 * for OR. Fall through to share code.
1955 * OR is strict if all of its arms are, so we can take the
1956 * intersection of the sets of nonnullable vars for each arm.
1957 * This works for both values of top_level.
1959 foreach(l, expr->args)
1963 subresult = find_nonnullable_vars_walker(lfirst(l),
1965 if (result == NIL) /* first subresult? */
1968 result = list_intersection(result, subresult);
1971 * If the intersection is empty, we can stop looking. This
1972 * also justifies the test for first-subresult above.
1979 /* NOT will return null if its arg is null */
1980 result = find_nonnullable_vars_walker((Node *) expr->args,
1984 elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1988 else if (IsA(node, RelabelType))
1990 RelabelType *expr = (RelabelType *) node;
1992 result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1994 else if (IsA(node, CoerceViaIO))
1996 /* not clear this is useful, but it can't hurt */
1997 CoerceViaIO *expr = (CoerceViaIO *) node;
1999 result = find_nonnullable_vars_walker((Node *) expr->arg, false);
2001 else if (IsA(node, ArrayCoerceExpr))
2003 /* ArrayCoerceExpr is strict at the array level; ignore elemexpr */
2004 ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
2006 result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
2008 else if (IsA(node, ConvertRowtypeExpr))
2010 /* not clear this is useful, but it can't hurt */
2011 ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
2013 result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
2015 else if (IsA(node, CollateExpr))
2017 CollateExpr *expr = (CollateExpr *) node;
2019 result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
2021 else if (IsA(node, NullTest))
2023 /* IS NOT NULL can be considered strict, but only at top level */
2024 NullTest *expr = (NullTest *) node;
2026 if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
2027 result = find_nonnullable_vars_walker((Node *) expr->arg, false);
2029 else if (IsA(node, BooleanTest))
2031 /* Boolean tests that reject NULL are strict at top level */
2032 BooleanTest *expr = (BooleanTest *) node;
2035 (expr->booltesttype == IS_TRUE ||
2036 expr->booltesttype == IS_FALSE ||
2037 expr->booltesttype == IS_NOT_UNKNOWN))
2038 result = find_nonnullable_vars_walker((Node *) expr->arg, false);
2040 else if (IsA(node, PlaceHolderVar))
2042 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2044 result = find_nonnullable_vars_walker((Node *) phv->phexpr, top_level);
2050 * find_forced_null_vars
2051 * Determine which Vars must be NULL for the given clause to return TRUE.
2053 * This is the complement of find_nonnullable_vars: find the level-zero Vars
2054 * that must be NULL for the clause to return TRUE. (It is OK to err on the
2055 * side of conservatism; hence the analysis here is simplistic. In fact,
2056 * we only detect simple "var IS NULL" tests at the top level.)
2058 * The result is a palloc'd List, but we have not copied the member Var nodes.
2059 * Also, we don't bother trying to eliminate duplicate entries.
2062 find_forced_null_vars(Node *node)
2070 /* Check single-clause cases using subroutine */
2071 var = find_forced_null_var(node);
2074 result = list_make1(var);
2076 /* Otherwise, handle AND-conditions */
2077 else if (IsA(node, List))
2080 * At top level, we are examining an implicit-AND list: if any of the
2081 * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
2083 foreach(l, (List *) node)
2085 result = list_concat(result,
2086 find_forced_null_vars(lfirst(l)));
2089 else if (IsA(node, BoolExpr))
2091 BoolExpr *expr = (BoolExpr *) node;
2094 * We don't bother considering the OR case, because it's fairly
2095 * unlikely anyone would write "v1 IS NULL OR v1 IS NULL". Likewise,
2096 * the NOT case isn't worth expending code on.
2098 if (expr->boolop == AND_EXPR)
2100 /* At top level we can just recurse (to the List case) */
2101 result = find_forced_null_vars((Node *) expr->args);
2108 * find_forced_null_var
2109 * Return the Var forced null by the given clause, or NULL if it's
2110 * not an IS NULL-type clause. For success, the clause must enforce
2111 * *only* nullness of the particular Var, not any other conditions.
2113 * This is just the single-clause case of find_forced_null_vars(), without
2114 * any allowance for AND conditions. It's used by initsplan.c on individual
2115 * qual clauses. The reason for not just applying find_forced_null_vars()
2116 * is that if an AND of an IS NULL clause with something else were to somehow
2117 * survive AND/OR flattening, initsplan.c might get fooled into discarding
2118 * the whole clause when only the IS NULL part of it had been proved redundant.
2121 find_forced_null_var(Node *node)
2125 if (IsA(node, NullTest))
2127 /* check for var IS NULL */
2128 NullTest *expr = (NullTest *) node;
2130 if (expr->nulltesttype == IS_NULL && !expr->argisrow)
2132 Var *var = (Var *) expr->arg;
2134 if (var && IsA(var, Var) &&
2135 var->varlevelsup == 0)
2139 else if (IsA(node, BooleanTest))
2141 /* var IS UNKNOWN is equivalent to var IS NULL */
2142 BooleanTest *expr = (BooleanTest *) node;
2144 if (expr->booltesttype == IS_UNKNOWN)
2146 Var *var = (Var *) expr->arg;
2148 if (var && IsA(var, Var) &&
2149 var->varlevelsup == 0)
2157 * Can we treat a ScalarArrayOpExpr as strict?
2159 * If "falseOK" is true, then a "false" result can be considered strict,
2160 * else we need to guarantee an actual NULL result for NULL input.
2162 * "foo op ALL array" is strict if the op is strict *and* we can prove
2163 * that the array input isn't an empty array. We can check that
2164 * for the cases of an array constant and an ARRAY[] construct.
2166 * "foo op ANY array" is strict in the falseOK sense if the op is strict.
2167 * If not falseOK, the test is the same as for "foo op ALL array".
2170 is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
2174 /* The contained operator must be strict. */
2175 set_sa_opfuncid(expr);
2176 if (!func_strict(expr->opfuncid))
2178 /* If ANY and falseOK, that's all we need to check. */
2179 if (expr->useOr && falseOK)
2181 /* Else, we have to see if the array is provably non-empty. */
2182 Assert(list_length(expr->args) == 2);
2183 rightop = (Node *) lsecond(expr->args);
2184 if (rightop && IsA(rightop, Const))
2186 Datum arraydatum = ((Const *) rightop)->constvalue;
2187 bool arrayisnull = ((Const *) rightop)->constisnull;
2188 ArrayType *arrayval;
2193 arrayval = DatumGetArrayTypeP(arraydatum);
2194 nitems = ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
2198 else if (rightop && IsA(rightop, ArrayExpr))
2200 ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
2202 if (arrayexpr->elements != NIL && !arrayexpr->multidims)
2209 /*****************************************************************************
2210 * Check for "pseudo-constant" clauses
2211 *****************************************************************************/
2214 * is_pseudo_constant_clause
2215 * Detect whether an expression is "pseudo constant", ie, it contains no
2216 * variables of the current query level and no uses of volatile functions.
2217 * Such an expr is not necessarily a true constant: it can still contain
2218 * Params and outer-level Vars, not to mention functions whose results
2219 * may vary from one statement to the next. However, the expr's value
2220 * will be constant over any one scan of the current query, so it can be
2221 * used as, eg, an indexscan key.
2223 * CAUTION: this function omits to test for one very important class of
2224 * not-constant expressions, namely aggregates (Aggrefs). In current usage
2225 * this is only applied to WHERE clauses and so a check for Aggrefs would be
2226 * a waste of cycles; but be sure to also check contain_agg_clause() if you
2227 * want to know about pseudo-constness in other contexts. The same goes
2228 * for window functions (WindowFuncs).
2231 is_pseudo_constant_clause(Node *clause)
2234 * We could implement this check in one recursive scan. But since the
2235 * check for volatile functions is both moderately expensive and unlikely
2236 * to fail, it seems better to look for Vars first and only check for
2237 * volatile functions if we find no Vars.
2239 if (!contain_var_clause(clause) &&
2240 !contain_volatile_functions(clause))
2246 * is_pseudo_constant_clause_relids
2247 * Same as above, except caller already has available the var membership
2248 * of the expression; this lets us avoid the contain_var_clause() scan.
2251 is_pseudo_constant_clause_relids(Node *clause, Relids relids)
2253 if (bms_is_empty(relids) &&
2254 !contain_volatile_functions(clause))
2260 /*****************************************************************************
2262 * General clause-manipulating routines *
2264 *****************************************************************************/
2268 * (formerly clause_relids)
2270 * Returns the number of different relations referenced in 'clause'.
2273 NumRelids(Node *clause)
2275 Relids varnos = pull_varnos(clause);
2276 int result = bms_num_members(varnos);
2283 * CommuteOpExpr: commute a binary operator clause
2285 * XXX the clause is destructively modified!
2288 CommuteOpExpr(OpExpr *clause)
2293 /* Sanity checks: caller is at fault if these fail */
2294 if (!is_opclause(clause) ||
2295 list_length(clause->args) != 2)
2296 elog(ERROR, "cannot commute non-binary-operator clause");
2298 opoid = get_commutator(clause->opno);
2300 if (!OidIsValid(opoid))
2301 elog(ERROR, "could not find commutator for operator %u",
2305 * modify the clause in-place!
2307 clause->opno = opoid;
2308 clause->opfuncid = InvalidOid;
2309 /* opresulttype, opretset, opcollid, inputcollid need not change */
2311 temp = linitial(clause->args);
2312 linitial(clause->args) = lsecond(clause->args);
2313 lsecond(clause->args) = temp;
2317 * CommuteRowCompareExpr: commute a RowCompareExpr clause
2319 * XXX the clause is destructively modified!
2322 CommuteRowCompareExpr(RowCompareExpr *clause)
2328 /* Sanity checks: caller is at fault if these fail */
2329 if (!IsA(clause, RowCompareExpr))
2330 elog(ERROR, "expected a RowCompareExpr");
2332 /* Build list of commuted operators */
2334 foreach(l, clause->opnos)
2336 Oid opoid = lfirst_oid(l);
2338 opoid = get_commutator(opoid);
2339 if (!OidIsValid(opoid))
2340 elog(ERROR, "could not find commutator for operator %u",
2342 newops = lappend_oid(newops, opoid);
2346 * modify the clause in-place!
2348 switch (clause->rctype)
2351 clause->rctype = ROWCOMPARE_GT;
2354 clause->rctype = ROWCOMPARE_GE;
2357 clause->rctype = ROWCOMPARE_LE;
2360 clause->rctype = ROWCOMPARE_LT;
2363 elog(ERROR, "unexpected RowCompare type: %d",
2364 (int) clause->rctype);
2368 clause->opnos = newops;
2371 * Note: we need not change the opfamilies list; we assume any btree
2372 * opfamily containing an operator will also contain its commutator.
2373 * Collations don't change either.
2376 temp = clause->largs;
2377 clause->largs = clause->rargs;
2378 clause->rargs = temp;
2382 * Helper for eval_const_expressions: check that datatype of an attribute
2383 * is still what it was when the expression was parsed. This is needed to
2384 * guard against improper simplification after ALTER COLUMN TYPE. (XXX we
2385 * may well need to make similar checks elsewhere?)
2387 * rowtypeid may come from a whole-row Var, and therefore it can be a domain
2388 * over composite, but for this purpose we only care about checking the type
2389 * of a contained field.
2392 rowtype_field_matches(Oid rowtypeid, int fieldnum,
2393 Oid expectedtype, int32 expectedtypmod,
2394 Oid expectedcollation)
2397 Form_pg_attribute attr;
2399 /* No issue for RECORD, since there is no way to ALTER such a type */
2400 if (rowtypeid == RECORDOID)
2402 tupdesc = lookup_rowtype_tupdesc_domain(rowtypeid, -1, false);
2403 if (fieldnum <= 0 || fieldnum > tupdesc->natts)
2405 ReleaseTupleDesc(tupdesc);
2408 attr = TupleDescAttr(tupdesc, fieldnum - 1);
2409 if (attr->attisdropped ||
2410 attr->atttypid != expectedtype ||
2411 attr->atttypmod != expectedtypmod ||
2412 attr->attcollation != expectedcollation)
2414 ReleaseTupleDesc(tupdesc);
2417 ReleaseTupleDesc(tupdesc);
2422 /*--------------------
2423 * eval_const_expressions
2425 * Reduce any recognizably constant subexpressions of the given
2426 * expression tree, for example "2 + 2" => "4". More interestingly,
2427 * we can reduce certain boolean expressions even when they contain
2428 * non-constant subexpressions: "x OR true" => "true" no matter what
2429 * the subexpression x is. (XXX We assume that no such subexpression
2430 * will have important side-effects, which is not necessarily a good
2431 * assumption in the presence of user-defined functions; do we need a
2432 * pg_proc flag that prevents discarding the execution of a function?)
2434 * We do understand that certain functions may deliver non-constant
2435 * results even with constant inputs, "nextval()" being the classic
2436 * example. Functions that are not marked "immutable" in pg_proc
2437 * will not be pre-evaluated here, although we will reduce their
2438 * arguments as far as possible.
2440 * Whenever a function is eliminated from the expression by means of
2441 * constant-expression evaluation or inlining, we add the function to
2442 * root->glob->invalItems. This ensures the plan is known to depend on
2443 * such functions, even though they aren't referenced anymore.
2445 * We assume that the tree has already been type-checked and contains
2446 * only operators and functions that are reasonable to try to execute.
2448 * NOTE: "root" can be passed as NULL if the caller never wants to do any
2449 * Param substitutions nor receive info about inlined functions.
2451 * NOTE: the planner assumes that this will always flatten nested AND and
2452 * OR clauses into N-argument form. See comments in prepqual.c.
2454 * NOTE: another critical effect is that any function calls that require
2455 * default arguments will be expanded, and named-argument calls will be
2456 * converted to positional notation. The executor won't handle either.
2457 *--------------------
2460 eval_const_expressions(PlannerInfo *root, Node *node)
2462 eval_const_expressions_context context;
2465 context.boundParams = root->glob->boundParams; /* bound Params */
2467 context.boundParams = NULL;
2468 context.root = root; /* for inlined-function dependencies */
2469 context.active_fns = NIL; /* nothing being recursively simplified */
2470 context.case_val = NULL; /* no CASE being examined */
2471 context.estimate = false; /* safe transformations only */
2472 return eval_const_expressions_mutator(node, &context);
2475 /*--------------------
2476 * estimate_expression_value
2478 * This function attempts to estimate the value of an expression for
2479 * planning purposes. It is in essence a more aggressive version of
2480 * eval_const_expressions(): we will perform constant reductions that are
2481 * not necessarily 100% safe, but are reasonable for estimation purposes.
2483 * Currently the extra steps that are taken in this mode are:
2484 * 1. Substitute values for Params, where a bound Param value has been made
2485 * available by the caller of planner(), even if the Param isn't marked
2486 * constant. This effectively means that we plan using the first supplied
2487 * value of the Param.
2488 * 2. Fold stable, as well as immutable, functions to constants.
2489 * 3. Reduce PlaceHolderVar nodes to their contained expressions.
2490 *--------------------
2493 estimate_expression_value(PlannerInfo *root, Node *node)
2495 eval_const_expressions_context context;
2497 context.boundParams = root->glob->boundParams; /* bound Params */
2498 /* we do not need to mark the plan as depending on inlined functions */
2499 context.root = NULL;
2500 context.active_fns = NIL; /* nothing being recursively simplified */
2501 context.case_val = NULL; /* no CASE being examined */
2502 context.estimate = true; /* unsafe transformations OK */
2503 return eval_const_expressions_mutator(node, &context);
2507 * The generic case in eval_const_expressions_mutator is to recurse using
2508 * expression_tree_mutator, which will copy the given node unchanged but
2509 * const-simplify its arguments (if any) as far as possible. If the node
2510 * itself does immutable processing, and each of its arguments were reduced
2511 * to a Const, we can then reduce it to a Const using evaluate_expr. (Some
2512 * node types need more complicated logic; for example, a CASE expression
2513 * might be reducible to a constant even if not all its subtrees are.)
2515 #define ece_generic_processing(node) \
2516 expression_tree_mutator((Node *) (node), eval_const_expressions_mutator, \
2520 * Check whether all arguments of the given node were reduced to Consts.
2521 * By going directly to expression_tree_walker, contain_non_const_walker
2522 * is not applied to the node itself, only to its children.
2524 #define ece_all_arguments_const(node) \
2525 (!expression_tree_walker((Node *) (node), contain_non_const_walker, NULL))
2527 /* Generic macro for applying evaluate_expr */
2528 #define ece_evaluate_expr(node) \
2529 ((Node *) evaluate_expr((Expr *) (node), \
2530 exprType((Node *) (node)), \
2531 exprTypmod((Node *) (node)), \
2532 exprCollation((Node *) (node))))
2535 * Recursive guts of eval_const_expressions/estimate_expression_value
2538 eval_const_expressions_mutator(Node *node,
2539 eval_const_expressions_context *context)
2543 switch (nodeTag(node))
2547 Param *param = (Param *) node;
2548 ParamListInfo paramLI = context->boundParams;
2550 /* Look to see if we've been given a value for this Param */
2551 if (param->paramkind == PARAM_EXTERN &&
2553 param->paramid > 0 &&
2554 param->paramid <= paramLI->numParams)
2556 ParamExternData *prm;
2557 ParamExternData prmdata;
2560 * Give hook a chance in case parameter is dynamic. Tell
2561 * it that this fetch is speculative, so it should avoid
2562 * erroring out if parameter is unavailable.
2564 if (paramLI->paramFetch != NULL)
2565 prm = paramLI->paramFetch(paramLI, param->paramid,
2568 prm = ¶mLI->params[param->paramid - 1];
2570 if (OidIsValid(prm->ptype))
2572 /* OK to substitute parameter value? */
2573 if (context->estimate ||
2574 (prm->pflags & PARAM_FLAG_CONST))
2577 * Return a Const representing the param value.
2578 * Must copy pass-by-ref datatypes, since the
2579 * Param might be in a memory context
2580 * shorter-lived than our output plan should be.
2586 Assert(prm->ptype == param->paramtype);
2587 get_typlenbyval(param->paramtype,
2588 &typLen, &typByVal);
2589 if (prm->isnull || typByVal)
2592 pval = datumCopy(prm->value, typByVal, typLen);
2593 return (Node *) makeConst(param->paramtype,
2605 * Not replaceable, so just copy the Param (no need to
2608 return (Node *) copyObject(param);
2612 WindowFunc *expr = (WindowFunc *) node;
2613 Oid funcid = expr->winfnoid;
2616 HeapTuple func_tuple;
2617 WindowFunc *newexpr;
2620 * We can't really simplify a WindowFunc node, but we mustn't
2621 * just fall through to the default processing, because we
2622 * have to apply expand_function_arguments to its argument
2623 * list. That takes care of inserting default arguments and
2624 * expanding named-argument notation.
2626 func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
2627 if (!HeapTupleIsValid(func_tuple))
2628 elog(ERROR, "cache lookup failed for function %u", funcid);
2630 args = expand_function_arguments(expr->args, expr->wintype,
2633 ReleaseSysCache(func_tuple);
2635 /* Now, recursively simplify the args (which are a List) */
2637 expression_tree_mutator((Node *) args,
2638 eval_const_expressions_mutator,
2640 /* ... and the filter expression, which isn't */
2641 aggfilter = (Expr *)
2642 eval_const_expressions_mutator((Node *) expr->aggfilter,
2645 /* And build the replacement WindowFunc node */
2646 newexpr = makeNode(WindowFunc);
2647 newexpr->winfnoid = expr->winfnoid;
2648 newexpr->wintype = expr->wintype;
2649 newexpr->wincollid = expr->wincollid;
2650 newexpr->inputcollid = expr->inputcollid;
2651 newexpr->args = args;
2652 newexpr->aggfilter = aggfilter;
2653 newexpr->winref = expr->winref;
2654 newexpr->winstar = expr->winstar;
2655 newexpr->winagg = expr->winagg;
2656 newexpr->location = expr->location;
2658 return (Node *) newexpr;
2662 FuncExpr *expr = (FuncExpr *) node;
2663 List *args = expr->args;
2668 * Code for op/func reduction is pretty bulky, so split it out
2669 * as a separate function. Note: exprTypmod normally returns
2670 * -1 for a FuncExpr, but not when the node is recognizably a
2671 * length coercion; we want to preserve the typmod in the
2672 * eventual Const if so.
2674 simple = simplify_function(expr->funcid,
2675 expr->funcresulttype,
2684 if (simple) /* successfully simplified it */
2685 return (Node *) simple;
2688 * The expression cannot be simplified any further, so build
2689 * and return a replacement FuncExpr node using the
2690 * possibly-simplified arguments. Note that we have also
2691 * converted the argument list to positional notation.
2693 newexpr = makeNode(FuncExpr);
2694 newexpr->funcid = expr->funcid;
2695 newexpr->funcresulttype = expr->funcresulttype;
2696 newexpr->funcretset = expr->funcretset;
2697 newexpr->funcvariadic = expr->funcvariadic;
2698 newexpr->funcformat = expr->funcformat;
2699 newexpr->funccollid = expr->funccollid;
2700 newexpr->inputcollid = expr->inputcollid;
2701 newexpr->args = args;
2702 newexpr->location = expr->location;
2703 return (Node *) newexpr;
2707 OpExpr *expr = (OpExpr *) node;
2708 List *args = expr->args;
2713 * Need to get OID of underlying function. Okay to scribble
2714 * on input to this extent.
2719 * Code for op/func reduction is pretty bulky, so split it out
2720 * as a separate function.
2722 simple = simplify_function(expr->opfuncid,
2723 expr->opresulttype, -1,
2731 if (simple) /* successfully simplified it */
2732 return (Node *) simple;
2735 * If the operator is boolean equality or inequality, we know
2736 * how to simplify cases involving one constant and one
2737 * non-constant argument.
2739 if (expr->opno == BooleanEqualOperator ||
2740 expr->opno == BooleanNotEqualOperator)
2742 simple = (Expr *) simplify_boolean_equality(expr->opno,
2744 if (simple) /* successfully simplified it */
2745 return (Node *) simple;
2749 * The expression cannot be simplified any further, so build
2750 * and return a replacement OpExpr node using the
2751 * possibly-simplified arguments.
2753 newexpr = makeNode(OpExpr);
2754 newexpr->opno = expr->opno;
2755 newexpr->opfuncid = expr->opfuncid;
2756 newexpr->opresulttype = expr->opresulttype;
2757 newexpr->opretset = expr->opretset;
2758 newexpr->opcollid = expr->opcollid;
2759 newexpr->inputcollid = expr->inputcollid;
2760 newexpr->args = args;
2761 newexpr->location = expr->location;
2762 return (Node *) newexpr;
2764 case T_DistinctExpr:
2766 DistinctExpr *expr = (DistinctExpr *) node;
2769 bool has_null_input = false;
2770 bool all_null_input = true;
2771 bool has_nonconst_input = false;
2773 DistinctExpr *newexpr;
2776 * Reduce constants in the DistinctExpr's arguments. We know
2777 * args is either NIL or a List node, so we can call
2778 * expression_tree_mutator directly rather than recursing to
2781 args = (List *) expression_tree_mutator((Node *) expr->args,
2782 eval_const_expressions_mutator,
2786 * We must do our own check for NULLs because DistinctExpr has
2787 * different results for NULL input than the underlying
2792 if (IsA(lfirst(arg), Const))
2794 has_null_input |= ((Const *) lfirst(arg))->constisnull;
2795 all_null_input &= ((Const *) lfirst(arg))->constisnull;
2798 has_nonconst_input = true;
2801 /* all constants? then can optimize this out */
2802 if (!has_nonconst_input)
2804 /* all nulls? then not distinct */
2806 return makeBoolConst(false, false);
2808 /* one null? then distinct */
2810 return makeBoolConst(true, false);
2812 /* otherwise try to evaluate the '=' operator */
2813 /* (NOT okay to try to inline it, though!) */
2816 * Need to get OID of underlying function. Okay to
2817 * scribble on input to this extent.
2819 set_opfuncid((OpExpr *) expr); /* rely on struct
2823 * Code for op/func reduction is pretty bulky, so split it
2824 * out as a separate function.
2826 simple = simplify_function(expr->opfuncid,
2827 expr->opresulttype, -1,
2835 if (simple) /* successfully simplified it */
2838 * Since the underlying operator is "=", must negate
2841 Const *csimple = castNode(Const, simple);
2843 csimple->constvalue =
2844 BoolGetDatum(!DatumGetBool(csimple->constvalue));
2845 return (Node *) csimple;
2850 * The expression cannot be simplified any further, so build
2851 * and return a replacement DistinctExpr node using the
2852 * possibly-simplified arguments.
2854 newexpr = makeNode(DistinctExpr);
2855 newexpr->opno = expr->opno;
2856 newexpr->opfuncid = expr->opfuncid;
2857 newexpr->opresulttype = expr->opresulttype;
2858 newexpr->opretset = expr->opretset;
2859 newexpr->opcollid = expr->opcollid;
2860 newexpr->inputcollid = expr->inputcollid;
2861 newexpr->args = args;
2862 newexpr->location = expr->location;
2863 return (Node *) newexpr;
2865 case T_ScalarArrayOpExpr:
2867 ScalarArrayOpExpr *saop;
2869 /* Copy the node and const-simplify its arguments */
2870 saop = (ScalarArrayOpExpr *) ece_generic_processing(node);
2872 /* Make sure we know underlying function */
2873 set_sa_opfuncid(saop);
2876 * If all arguments are Consts, and it's a safe function, we
2877 * can fold to a constant
2879 if (ece_all_arguments_const(saop) &&
2880 ece_function_is_safe(saop->opfuncid, context))
2881 return ece_evaluate_expr(saop);
2882 return (Node *) saop;
2886 BoolExpr *expr = (BoolExpr *) node;
2888 switch (expr->boolop)
2893 bool haveNull = false;
2894 bool forceTrue = false;
2896 newargs = simplify_or_arguments(expr->args,
2901 return makeBoolConst(true, false);
2903 newargs = lappend(newargs,
2904 makeBoolConst(false, true));
2905 /* If all the inputs are FALSE, result is FALSE */
2907 return makeBoolConst(false, false);
2910 * If only one nonconst-or-NULL input, it's the
2913 if (list_length(newargs) == 1)
2914 return (Node *) linitial(newargs);
2915 /* Else we still need an OR node */
2916 return (Node *) make_orclause(newargs);
2921 bool haveNull = false;
2922 bool forceFalse = false;
2924 newargs = simplify_and_arguments(expr->args,
2929 return makeBoolConst(false, false);
2931 newargs = lappend(newargs,
2932 makeBoolConst(false, true));
2933 /* If all the inputs are TRUE, result is TRUE */
2935 return makeBoolConst(true, false);
2938 * If only one nonconst-or-NULL input, it's the
2941 if (list_length(newargs) == 1)
2942 return (Node *) linitial(newargs);
2943 /* Else we still need an AND node */
2944 return (Node *) make_andclause(newargs);
2950 Assert(list_length(expr->args) == 1);
2951 arg = eval_const_expressions_mutator(linitial(expr->args),
2955 * Use negate_clause() to see if we can simplify
2958 return negate_clause(arg);
2961 elog(ERROR, "unrecognized boolop: %d",
2962 (int) expr->boolop);
2968 case T_AlternativeSubPlan:
2971 * Return a SubPlan unchanged --- too late to do anything with it.
2973 * XXX should we ereport() here instead? Probably this routine
2974 * should never be invoked after SubPlan creation.
2980 * If we can simplify the input to a constant, then we don't
2981 * need the RelabelType node anymore: just change the type
2982 * field of the Const node. Otherwise, must copy the
2985 RelabelType *relabel = (RelabelType *) node;
2988 arg = eval_const_expressions_mutator((Node *) relabel->arg,
2992 * If we find stacked RelabelTypes (eg, from foo :: int ::
2993 * oid) we can discard all but the top one.
2995 while (arg && IsA(arg, RelabelType))
2996 arg = (Node *) ((RelabelType *) arg)->arg;
2998 if (arg && IsA(arg, Const))
3000 Const *con = (Const *) arg;
3002 con->consttype = relabel->resulttype;
3003 con->consttypmod = relabel->resulttypmod;
3004 con->constcollid = relabel->resultcollid;
3005 return (Node *) con;
3009 RelabelType *newrelabel = makeNode(RelabelType);
3011 newrelabel->arg = (Expr *) arg;
3012 newrelabel->resulttype = relabel->resulttype;
3013 newrelabel->resulttypmod = relabel->resulttypmod;
3014 newrelabel->resultcollid = relabel->resultcollid;
3015 newrelabel->relabelformat = relabel->relabelformat;
3016 newrelabel->location = relabel->location;
3017 return (Node *) newrelabel;
3022 CoerceViaIO *expr = (CoerceViaIO *) node;
3025 bool outtypisvarlena;
3029 CoerceViaIO *newexpr;
3031 /* Make a List so we can use simplify_function */
3032 args = list_make1(expr->arg);
3035 * CoerceViaIO represents calling the source type's output
3036 * function then the result type's input function. So, try to
3037 * simplify it as though it were a stack of two such function
3038 * calls. First we need to know what the functions are.
3040 * Note that the coercion functions are assumed not to care
3041 * about input collation, so we just pass InvalidOid for that.
3043 getTypeOutputInfo(exprType((Node *) expr->arg),
3044 &outfunc, &outtypisvarlena);
3045 getTypeInputInfo(expr->resulttype,
3046 &infunc, &intypioparam);
3048 simple = simplify_function(outfunc,
3057 if (simple) /* successfully simplified output fn */
3060 * Input functions may want 1 to 3 arguments. We always
3061 * supply all three, trusting that nothing downstream will
3064 args = list_make3(simple,
3069 ObjectIdGetDatum(intypioparam),
3080 simple = simplify_function(infunc,
3081 expr->resulttype, -1,
3089 if (simple) /* successfully simplified input fn */
3090 return (Node *) simple;
3094 * The expression cannot be simplified any further, so build
3095 * and return a replacement CoerceViaIO node using the
3096 * possibly-simplified argument.
3098 newexpr = makeNode(CoerceViaIO);
3099 newexpr->arg = (Expr *) linitial(args);
3100 newexpr->resulttype = expr->resulttype;
3101 newexpr->resultcollid = expr->resultcollid;
3102 newexpr->coerceformat = expr->coerceformat;
3103 newexpr->location = expr->location;
3104 return (Node *) newexpr;
3106 case T_ArrayCoerceExpr:
3108 ArrayCoerceExpr *ac;
3110 /* Copy the node and const-simplify its arguments */
3111 ac = (ArrayCoerceExpr *) ece_generic_processing(node);
3114 * If constant argument and the per-element expression is
3115 * immutable, we can simplify the whole thing to a constant.
3116 * Exception: although contain_mutable_functions considers
3117 * CoerceToDomain immutable for historical reasons, let's not
3118 * do so here; this ensures coercion to an array-over-domain
3119 * does not apply the domain's constraints until runtime.
3121 if (ac->arg && IsA(ac->arg, Const) &&
3122 ac->elemexpr && !IsA(ac->elemexpr, CoerceToDomain) &&
3123 !contain_mutable_functions((Node *) ac->elemexpr))
3124 return ece_evaluate_expr(ac);
3130 * If we can simplify the input to a constant, then we don't
3131 * need the CollateExpr node at all: just change the
3132 * constcollid field of the Const node. Otherwise, replace
3133 * the CollateExpr with a RelabelType. (We do that so as to
3134 * improve uniformity of expression representation and thus
3135 * simplify comparison of expressions.)
3137 CollateExpr *collate = (CollateExpr *) node;
3140 arg = eval_const_expressions_mutator((Node *) collate->arg,
3143 if (arg && IsA(arg, Const))
3145 Const *con = (Const *) arg;
3147 con->constcollid = collate->collOid;
3148 return (Node *) con;
3150 else if (collate->collOid == exprCollation(arg))
3152 /* Don't need a RelabelType either... */
3157 RelabelType *relabel = makeNode(RelabelType);
3159 relabel->resulttype = exprType(arg);
3160 relabel->resulttypmod = exprTypmod(arg);
3161 relabel->resultcollid = collate->collOid;
3162 relabel->relabelformat = COERCE_IMPLICIT_CAST;
3163 relabel->location = collate->location;
3165 /* Don't create stacked RelabelTypes */
3166 while (arg && IsA(arg, RelabelType))
3167 arg = (Node *) ((RelabelType *) arg)->arg;
3168 relabel->arg = (Expr *) arg;
3170 return (Node *) relabel;
3176 * CASE expressions can be simplified if there are constant
3177 * condition clauses:
3178 * FALSE (or NULL): drop the alternative
3179 * TRUE: drop all remaining alternatives
3180 * If the first non-FALSE alternative is a constant TRUE,
3181 * we can simplify the entire CASE to that alternative's
3182 * expression. If there are no non-FALSE alternatives,
3183 * we simplify the entire CASE to the default result (ELSE).
3185 * If we have a simple-form CASE with constant test
3186 * expression, we substitute the constant value for contained
3187 * CaseTestExpr placeholder nodes, so that we have the
3188 * opportunity to reduce constant test conditions. For
3189 * example this allows
3190 * CASE 0 WHEN 0 THEN 1 ELSE 1/0 END
3191 * to reduce to 1 rather than drawing a divide-by-0 error.
3192 * Note that when the test expression is constant, we don't
3193 * have to include it in the resulting CASE; for example
3194 * CASE 0 WHEN x THEN y ELSE z END
3195 * is transformed by the parser to
3196 * CASE 0 WHEN CaseTestExpr = x THEN y ELSE z END
3197 * which we can simplify to
3198 * CASE WHEN 0 = x THEN y ELSE z END
3199 * It is not necessary for the executor to evaluate the "arg"
3200 * expression when executing the CASE, since any contained
3201 * CaseTestExprs that might have referred to it will have been
3202 * replaced by the constant.
3205 CaseExpr *caseexpr = (CaseExpr *) node;
3207 Node *save_case_val;
3210 bool const_true_cond;
3211 Node *defresult = NULL;
3214 /* Simplify the test expression, if any */
3215 newarg = eval_const_expressions_mutator((Node *) caseexpr->arg,
3218 /* Set up for contained CaseTestExpr nodes */
3219 save_case_val = context->case_val;
3220 if (newarg && IsA(newarg, Const))
3222 context->case_val = newarg;
3223 newarg = NULL; /* not needed anymore, see above */
3226 context->case_val = NULL;
3228 /* Simplify the WHEN clauses */
3230 const_true_cond = false;
3231 foreach(arg, caseexpr->args)
3233 CaseWhen *oldcasewhen = lfirst_node(CaseWhen, arg);
3237 /* Simplify this alternative's test condition */
3238 casecond = eval_const_expressions_mutator((Node *) oldcasewhen->expr,
3242 * If the test condition is constant FALSE (or NULL), then
3243 * drop this WHEN clause completely, without processing
3246 if (casecond && IsA(casecond, Const))
3248 Const *const_input = (Const *) casecond;
3250 if (const_input->constisnull ||
3251 !DatumGetBool(const_input->constvalue))
3252 continue; /* drop alternative with FALSE cond */
3253 /* Else it's constant TRUE */
3254 const_true_cond = true;
3257 /* Simplify this alternative's result value */
3258 caseresult = eval_const_expressions_mutator((Node *) oldcasewhen->result,
3261 /* If non-constant test condition, emit a new WHEN node */
3262 if (!const_true_cond)
3264 CaseWhen *newcasewhen = makeNode(CaseWhen);
3266 newcasewhen->expr = (Expr *) casecond;
3267 newcasewhen->result = (Expr *) caseresult;
3268 newcasewhen->location = oldcasewhen->location;
3269 newargs = lappend(newargs, newcasewhen);
3274 * Found a TRUE condition, so none of the remaining
3275 * alternatives can be reached. We treat the result as
3276 * the default result.
3278 defresult = caseresult;
3282 /* Simplify the default result, unless we replaced it above */
3283 if (!const_true_cond)
3284 defresult = eval_const_expressions_mutator((Node *) caseexpr->defresult,
3287 context->case_val = save_case_val;
3290 * If no non-FALSE alternatives, CASE reduces to the default
3295 /* Otherwise we need a new CASE node */
3296 newcase = makeNode(CaseExpr);
3297 newcase->casetype = caseexpr->casetype;
3298 newcase->casecollid = caseexpr->casecollid;
3299 newcase->arg = (Expr *) newarg;
3300 newcase->args = newargs;
3301 newcase->defresult = (Expr *) defresult;
3302 newcase->location = caseexpr->location;
3303 return (Node *) newcase;
3305 case T_CaseTestExpr:
3308 * If we know a constant test value for the current CASE
3309 * construct, substitute it for the placeholder. Else just
3310 * return the placeholder as-is.
3312 if (context->case_val)
3313 return copyObject(context->case_val);
3315 return copyObject(node);
3322 * Generic handling for node types whose own processing is
3323 * known to be immutable, and for which we need no smarts
3324 * beyond "simplify if all inputs are constants".
3327 /* Copy the node and const-simplify its arguments */
3328 node = ece_generic_processing(node);
3329 /* If all arguments are Consts, we can fold to a constant */
3330 if (ece_all_arguments_const(node))
3331 return ece_evaluate_expr(node);
3334 case T_CoalesceExpr:
3336 CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
3337 CoalesceExpr *newcoalesce;
3342 foreach(arg, coalesceexpr->args)
3346 e = eval_const_expressions_mutator((Node *) lfirst(arg),
3350 * We can remove null constants from the list. For a
3351 * non-null constant, if it has not been preceded by any
3352 * other non-null-constant expressions then it is the
3353 * result. Otherwise, it's the next argument, but we can
3354 * drop following arguments since they will never be
3359 if (((Const *) e)->constisnull)
3360 continue; /* drop null constant */
3362 return e; /* first expr */
3363 newargs = lappend(newargs, e);
3366 newargs = lappend(newargs, e);
3370 * If all the arguments were constant null, the result is just
3374 return (Node *) makeNullConst(coalesceexpr->coalescetype,
3376 coalesceexpr->coalescecollid);
3378 newcoalesce = makeNode(CoalesceExpr);
3379 newcoalesce->coalescetype = coalesceexpr->coalescetype;
3380 newcoalesce->coalescecollid = coalesceexpr->coalescecollid;
3381 newcoalesce->args = newargs;
3382 newcoalesce->location = coalesceexpr->location;
3383 return (Node *) newcoalesce;
3385 case T_SQLValueFunction:
3388 * All variants of SQLValueFunction are stable, so if we are
3389 * estimating the expression's value, we should evaluate the
3390 * current function value. Otherwise just copy.
3392 SQLValueFunction *svf = (SQLValueFunction *) node;
3394 if (context->estimate)
3395 return (Node *) evaluate_expr((Expr *) svf,
3400 return copyObject((Node *) svf);
3405 * We can optimize field selection from a whole-row Var into a
3406 * simple Var. (This case won't be generated directly by the
3407 * parser, because ParseComplexProjection short-circuits it.
3408 * But it can arise while simplifying functions.) Also, we
3409 * can optimize field selection from a RowExpr construct, or
3410 * of course from a constant.
3412 * However, replacing a whole-row Var in this way has a
3413 * pitfall: if we've already built the rel targetlist for the
3414 * source relation, then the whole-row Var is scheduled to be
3415 * produced by the relation scan, but the simple Var probably
3416 * isn't, which will lead to a failure in setrefs.c. This is
3417 * not a problem when handling simple single-level queries, in
3418 * which expression simplification always happens first. It
3419 * is a risk for lateral references from subqueries, though.
3420 * To avoid such failures, don't optimize uplevel references.
3422 * We must also check that the declared type of the field is
3423 * still the same as when the FieldSelect was created --- this
3424 * can change if someone did ALTER COLUMN TYPE on the rowtype.
3425 * If it isn't, we skip the optimization; the case will
3426 * probably fail at runtime, but that's not our problem here.
3428 FieldSelect *fselect = (FieldSelect *) node;
3429 FieldSelect *newfselect;
3432 arg = eval_const_expressions_mutator((Node *) fselect->arg,
3434 if (arg && IsA(arg, Var) &&
3435 ((Var *) arg)->varattno == InvalidAttrNumber &&
3436 ((Var *) arg)->varlevelsup == 0)
3438 if (rowtype_field_matches(((Var *) arg)->vartype,
3440 fselect->resulttype,
3441 fselect->resulttypmod,
3442 fselect->resultcollid))
3443 return (Node *) makeVar(((Var *) arg)->varno,
3445 fselect->resulttype,
3446 fselect->resulttypmod,
3447 fselect->resultcollid,
3448 ((Var *) arg)->varlevelsup);
3450 if (arg && IsA(arg, RowExpr))
3452 RowExpr *rowexpr = (RowExpr *) arg;
3454 if (fselect->fieldnum > 0 &&
3455 fselect->fieldnum <= list_length(rowexpr->args))
3457 Node *fld = (Node *) list_nth(rowexpr->args,
3458 fselect->fieldnum - 1);
3460 if (rowtype_field_matches(rowexpr->row_typeid,
3462 fselect->resulttype,
3463 fselect->resulttypmod,
3464 fselect->resultcollid) &&
3465 fselect->resulttype == exprType(fld) &&
3466 fselect->resulttypmod == exprTypmod(fld) &&
3467 fselect->resultcollid == exprCollation(fld))
3471 newfselect = makeNode(FieldSelect);
3472 newfselect->arg = (Expr *) arg;
3473 newfselect->fieldnum = fselect->fieldnum;
3474 newfselect->resulttype = fselect->resulttype;
3475 newfselect->resulttypmod = fselect->resulttypmod;
3476 newfselect->resultcollid = fselect->resultcollid;
3477 if (arg && IsA(arg, Const))
3479 Const *con = (Const *) arg;
3481 if (rowtype_field_matches(con->consttype,
3482 newfselect->fieldnum,
3483 newfselect->resulttype,
3484 newfselect->resulttypmod,
3485 newfselect->resultcollid))
3486 return ece_evaluate_expr(newfselect);
3488 return (Node *) newfselect;
3492 NullTest *ntest = (NullTest *) node;
3496 arg = eval_const_expressions_mutator((Node *) ntest->arg,
3498 if (ntest->argisrow && arg && IsA(arg, RowExpr))
3501 * We break ROW(...) IS [NOT] NULL into separate tests on
3502 * its component fields. This form is usually more
3503 * efficient to evaluate, as well as being more amenable
3506 RowExpr *rarg = (RowExpr *) arg;
3507 List *newargs = NIL;
3510 foreach(l, rarg->args)
3512 Node *relem = (Node *) lfirst(l);
3515 * A constant field refutes the whole NullTest if it's
3516 * of the wrong nullness; else we can discard it.
3518 if (relem && IsA(relem, Const))
3520 Const *carg = (Const *) relem;
3522 if (carg->constisnull ?
3523 (ntest->nulltesttype == IS_NOT_NULL) :
3524 (ntest->nulltesttype == IS_NULL))
3525 return makeBoolConst(false, false);
3530 * Else, make a scalar (argisrow == false) NullTest
3531 * for this field. Scalar semantics are required
3532 * because IS [NOT] NULL doesn't recurse; see comments
3533 * in ExecEvalRowNullInt().
3535 newntest = makeNode(NullTest);
3536 newntest->arg = (Expr *) relem;
3537 newntest->nulltesttype = ntest->nulltesttype;
3538 newntest->argisrow = false;
3539 newntest->location = ntest->location;
3540 newargs = lappend(newargs, newntest);
3542 /* If all the inputs were constants, result is TRUE */
3544 return makeBoolConst(true, false);
3545 /* If only one nonconst input, it's the result */
3546 if (list_length(newargs) == 1)
3547 return (Node *) linitial(newargs);
3548 /* Else we need an AND node */
3549 return (Node *) make_andclause(newargs);
3551 if (!ntest->argisrow && arg && IsA(arg, Const))
3553 Const *carg = (Const *) arg;
3556 switch (ntest->nulltesttype)
3559 result = carg->constisnull;
3562 result = !carg->constisnull;
3565 elog(ERROR, "unrecognized nulltesttype: %d",
3566 (int) ntest->nulltesttype);
3567 result = false; /* keep compiler quiet */
3571 return makeBoolConst(result, false);
3574 newntest = makeNode(NullTest);
3575 newntest->arg = (Expr *) arg;
3576 newntest->nulltesttype = ntest->nulltesttype;
3577 newntest->argisrow = ntest->argisrow;
3578 newntest->location = ntest->location;
3579 return (Node *) newntest;
3584 * This case could be folded into the generic handling used
3585 * for ArrayRef etc. But because the simplification logic is
3586 * so trivial, applying evaluate_expr() to perform it would be
3587 * a heavy overhead. BooleanTest is probably common enough to
3588 * justify keeping this bespoke implementation.
3590 BooleanTest *btest = (BooleanTest *) node;
3591 BooleanTest *newbtest;
3594 arg = eval_const_expressions_mutator((Node *) btest->arg,
3596 if (arg && IsA(arg, Const))
3598 Const *carg = (Const *) arg;
3601 switch (btest->booltesttype)
3604 result = (!carg->constisnull &&
3605 DatumGetBool(carg->constvalue));
3608 result = (carg->constisnull ||
3609 !DatumGetBool(carg->constvalue));
3612 result = (!carg->constisnull &&
3613 !DatumGetBool(carg->constvalue));
3616 result = (carg->constisnull ||
3617 DatumGetBool(carg->constvalue));
3620 result = carg->constisnull;
3622 case IS_NOT_UNKNOWN:
3623 result = !carg->constisnull;
3626 elog(ERROR, "unrecognized booltesttype: %d",
3627 (int) btest->booltesttype);
3628 result = false; /* keep compiler quiet */
3632 return makeBoolConst(result, false);
3635 newbtest = makeNode(BooleanTest);
3636 newbtest->arg = (Expr *) arg;
3637 newbtest->booltesttype = btest->booltesttype;
3638 newbtest->location = btest->location;
3639 return (Node *) newbtest;
3641 case T_PlaceHolderVar:
3644 * In estimation mode, just strip the PlaceHolderVar node
3645 * altogether; this amounts to estimating that the contained value
3646 * won't be forced to null by an outer join. In regular mode we
3647 * just use the default behavior (ie, simplify the expression but
3648 * leave the PlaceHolderVar node intact).
3650 if (context->estimate)
3652 PlaceHolderVar *phv = (PlaceHolderVar *) node;
3654 return eval_const_expressions_mutator((Node *) phv->phexpr,
3663 * For any node type not handled above, copy the node unchanged but
3664 * const-simplify its subexpressions. This is the correct thing for node
3665 * types whose behavior might change between planning and execution, such
3666 * as CoerceToDomain. It's also a safe default for new node types not
3667 * known to this routine.
3669 return ece_generic_processing(node);
3673 * Subroutine for eval_const_expressions: check for non-Const nodes.
3675 * We can abort recursion immediately on finding a non-Const node. This is
3676 * critical for performance, else eval_const_expressions_mutator would take
3677 * O(N^2) time on non-simplifiable trees. However, we do need to descend
3678 * into List nodes since expression_tree_walker sometimes invokes the walker
3679 * function directly on List subtrees.
3682 contain_non_const_walker(Node *node, void *context)
3686 if (IsA(node, Const))
3688 if (IsA(node, List))
3689 return expression_tree_walker(node, contain_non_const_walker, context);
3690 /* Otherwise, abort the tree traversal and return true */
3695 * Subroutine for eval_const_expressions: check if a function is OK to evaluate
3698 ece_function_is_safe(Oid funcid, eval_const_expressions_context *context)
3700 char provolatile = func_volatile(funcid);
3703 * Ordinarily we are only allowed to simplify immutable functions. But for
3704 * purposes of estimation, we consider it okay to simplify functions that
3705 * are merely stable; the risk that the result might change from planning
3706 * time to execution time is worth taking in preference to not being able
3707 * to estimate the value at all.
3709 if (provolatile == PROVOLATILE_IMMUTABLE)
3711 if (context->estimate && provolatile == PROVOLATILE_STABLE)
3717 * Subroutine for eval_const_expressions: process arguments of an OR clause
3719 * This includes flattening of nested ORs as well as recursion to
3720 * eval_const_expressions to simplify the OR arguments.
3722 * After simplification, OR arguments are handled as follows:
3723 * non constant: keep
3724 * FALSE: drop (does not affect result)
3725 * TRUE: force result to TRUE
3726 * NULL: keep only one
3727 * We must keep one NULL input because OR expressions evaluate to NULL when no
3728 * input is TRUE and at least one is NULL. We don't actually include the NULL
3729 * here, that's supposed to be done by the caller.
3731 * The output arguments *haveNull and *forceTrue must be initialized false
3732 * by the caller. They will be set true if a NULL constant or TRUE constant,
3733 * respectively, is detected anywhere in the argument list.
3736 simplify_or_arguments(List *args,
3737 eval_const_expressions_context *context,
3738 bool *haveNull, bool *forceTrue)
3740 List *newargs = NIL;
3741 List *unprocessed_args;
3744 * We want to ensure that any OR immediately beneath another OR gets
3745 * flattened into a single OR-list, so as to simplify later reasoning.
3747 * To avoid stack overflow from recursion of eval_const_expressions, we
3748 * resort to some tenseness here: we keep a list of not-yet-processed
3749 * inputs, and handle flattening of nested ORs by prepending to the to-do
3750 * list instead of recursing. Now that the parser generates N-argument
3751 * ORs from simple lists, this complexity is probably less necessary than
3752 * it once was, but we might as well keep the logic.
3754 unprocessed_args = list_copy(args);
3755 while (unprocessed_args)
3757 Node *arg = (Node *) linitial(unprocessed_args);
3759 unprocessed_args = list_delete_first(unprocessed_args);
3761 /* flatten nested ORs as per above comment */
3764 List *subargs = list_copy(((BoolExpr *) arg)->args);
3766 /* overly tense code to avoid leaking unused list header */
3767 if (!unprocessed_args)
3768 unprocessed_args = subargs;
3771 List *oldhdr = unprocessed_args;
3773 unprocessed_args = list_concat(subargs, unprocessed_args);
3779 /* If it's not an OR, simplify it */
3780 arg = eval_const_expressions_mutator(arg, context);
3783 * It is unlikely but not impossible for simplification of a non-OR
3784 * clause to produce an OR. Recheck, but don't be too tense about it
3785 * since it's not a mainstream case. In particular we don't worry
3786 * about const-simplifying the input twice.
3790 List *subargs = list_copy(((BoolExpr *) arg)->args);
3792 unprocessed_args = list_concat(subargs, unprocessed_args);
3797 * OK, we have a const-simplified non-OR argument. Process it per
3800 if (IsA(arg, Const))
3802 Const *const_input = (Const *) arg;
3804 if (const_input->constisnull)
3806 else if (DatumGetBool(const_input->constvalue))
3811 * Once we detect a TRUE result we can just exit the loop
3812 * immediately. However, if we ever add a notion of
3813 * non-removable functions, we'd need to keep scanning.
3817 /* otherwise, we can drop the constant-false input */
3821 /* else emit the simplified arg into the result list */
3822 newargs = lappend(newargs, arg);
3829 * Subroutine for eval_const_expressions: process arguments of an AND clause
3831 * This includes flattening of nested ANDs as well as recursion to
3832 * eval_const_expressions to simplify the AND arguments.
3834 * After simplification, AND arguments are handled as follows:
3835 * non constant: keep
3836 * TRUE: drop (does not affect result)
3837 * FALSE: force result to FALSE
3838 * NULL: keep only one
3839 * We must keep one NULL input because AND expressions evaluate to NULL when
3840 * no input is FALSE and at least one is NULL. We don't actually include the
3841 * NULL here, that's supposed to be done by the caller.
3843 * The output arguments *haveNull and *forceFalse must be initialized false
3844 * by the caller. They will be set true if a null constant or false constant,
3845 * respectively, is detected anywhere in the argument list.
3848 simplify_and_arguments(List *args,
3849 eval_const_expressions_context *context,
3850 bool *haveNull, bool *forceFalse)
3852 List *newargs = NIL;
3853 List *unprocessed_args;
3855 /* See comments in simplify_or_arguments */
3856 unprocessed_args = list_copy(args);
3857 while (unprocessed_args)
3859 Node *arg = (Node *) linitial(unprocessed_args);
3861 unprocessed_args = list_delete_first(unprocessed_args);
3863 /* flatten nested ANDs as per above comment */
3864 if (and_clause(arg))
3866 List *subargs = list_copy(((BoolExpr *) arg)->args);
3868 /* overly tense code to avoid leaking unused list header */
3869 if (!unprocessed_args)
3870 unprocessed_args = subargs;
3873 List *oldhdr = unprocessed_args;
3875 unprocessed_args = list_concat(subargs, unprocessed_args);
3881 /* If it's not an AND, simplify it */
3882 arg = eval_const_expressions_mutator(arg, context);
3885 * It is unlikely but not impossible for simplification of a non-AND
3886 * clause to produce an AND. Recheck, but don't be too tense about it
3887 * since it's not a mainstream case. In particular we don't worry
3888 * about const-simplifying the input twice.
3890 if (and_clause(arg))
3892 List *subargs = list_copy(((BoolExpr *) arg)->args);
3894 unprocessed_args = list_concat(subargs, unprocessed_args);
3899 * OK, we have a const-simplified non-AND argument. Process it per
3902 if (IsA(arg, Const))
3904 Const *const_input = (Const *) arg;
3906 if (const_input->constisnull)
3908 else if (!DatumGetBool(const_input->constvalue))
3913 * Once we detect a FALSE result we can just exit the loop
3914 * immediately. However, if we ever add a notion of
3915 * non-removable functions, we'd need to keep scanning.
3919 /* otherwise, we can drop the constant-true input */
3923 /* else emit the simplified arg into the result list */
3924 newargs = lappend(newargs, arg);
3931 * Subroutine for eval_const_expressions: try to simplify boolean equality
3932 * or inequality condition
3934 * Inputs are the operator OID and the simplified arguments to the operator.
3935 * Returns a simplified expression if successful, or NULL if cannot
3936 * simplify the expression.
3938 * The idea here is to reduce "x = true" to "x" and "x = false" to "NOT x",
3939 * or similarly "x <> true" to "NOT x" and "x <> false" to "x".
3940 * This is only marginally useful in itself, but doing it in constant folding
3941 * ensures that we will recognize these forms as being equivalent in, for
3942 * example, partial index matching.
3944 * We come here only if simplify_function has failed; therefore we cannot
3945 * see two constant inputs, nor a constant-NULL input.
3948 simplify_boolean_equality(Oid opno, List *args)
3953 Assert(list_length(args) == 2);
3954 leftop = linitial(args);
3955 rightop = lsecond(args);
3956 if (leftop && IsA(leftop, Const))
3958 Assert(!((Const *) leftop)->constisnull);
3959 if (opno == BooleanEqualOperator)
3961 if (DatumGetBool(((Const *) leftop)->constvalue))
3962 return rightop; /* true = foo */
3964 return negate_clause(rightop); /* false = foo */
3968 if (DatumGetBool(((Const *) leftop)->constvalue))
3969 return negate_clause(rightop); /* true <> foo */
3971 return rightop; /* false <> foo */
3974 if (rightop && IsA(rightop, Const))
3976 Assert(!((Const *) rightop)->constisnull);
3977 if (opno == BooleanEqualOperator)
3979 if (DatumGetBool(((Const *) rightop)->constvalue))
3980 return leftop; /* foo = true */
3982 return negate_clause(leftop); /* foo = false */
3986 if (DatumGetBool(((Const *) rightop)->constvalue))
3987 return negate_clause(leftop); /* foo <> true */
3989 return leftop; /* foo <> false */
3996 * Subroutine for eval_const_expressions: try to simplify a function call
3997 * (which might originally have been an operator; we don't care)
3999 * Inputs are the function OID, actual result type OID (which is needed for
4000 * polymorphic functions), result typmod, result collation, the input
4001 * collation to use for the function, the original argument list (not
4002 * const-simplified yet, unless process_args is false), and some flags;
4003 * also the context data for eval_const_expressions.
4005 * Returns a simplified expression if successful, or NULL if cannot
4006 * simplify the function call.
4008 * This function is also responsible for converting named-notation argument
4009 * lists into positional notation and/or adding any needed default argument
4010 * expressions; which is a bit grotty, but it avoids extra fetches of the
4011 * function's pg_proc tuple. For this reason, the args list is
4012 * pass-by-reference. Conversion and const-simplification of the args list
4013 * will be done even if simplification of the function call itself is not
4017 simplify_function(Oid funcid, Oid result_type, int32 result_typmod,
4018 Oid result_collid, Oid input_collid, List **args_p,
4019 bool funcvariadic, bool process_args, bool allow_non_const,
4020 eval_const_expressions_context *context)
4022 List *args = *args_p;
4023 HeapTuple func_tuple;
4024 Form_pg_proc func_form;
4028 * We have three strategies for simplification: execute the function to
4029 * deliver a constant result, use a transform function to generate a
4030 * substitute node tree, or expand in-line the body of the function
4031 * definition (which only works for simple SQL-language functions, but
4032 * that is a common case). Each case needs access to the function's
4033 * pg_proc tuple, so fetch it just once.
4035 * Note: the allow_non_const flag suppresses both the second and third
4036 * strategies; so if !allow_non_const, simplify_function can only return a
4037 * Const or NULL. Argument-list rewriting happens anyway, though.
4039 func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
4040 if (!HeapTupleIsValid(func_tuple))
4041 elog(ERROR, "cache lookup failed for function %u", funcid);
4042 func_form = (Form_pg_proc) GETSTRUCT(func_tuple);
4045 * Process the function arguments, unless the caller did it already.
4047 * Here we must deal with named or defaulted arguments, and then
4048 * recursively apply eval_const_expressions to the whole argument list.
4052 args = expand_function_arguments(args, result_type, func_tuple);
4053 args = (List *) expression_tree_mutator((Node *) args,
4054 eval_const_expressions_mutator,
4056 /* Argument processing done, give it back to the caller */
4060 /* Now attempt simplification of the function call proper. */
4062 newexpr = evaluate_function(funcid, result_type, result_typmod,
4063 result_collid, input_collid,
4065 func_tuple, context);
4067 if (!newexpr && allow_non_const && OidIsValid(func_form->protransform))
4070 * Build a dummy FuncExpr node containing the simplified arg list. We
4071 * use this approach to present a uniform interface to the transform
4072 * function regardless of how the function is actually being invoked.
4076 fexpr.xpr.type = T_FuncExpr;
4077 fexpr.funcid = funcid;
4078 fexpr.funcresulttype = result_type;
4079 fexpr.funcretset = func_form->proretset;
4080 fexpr.funcvariadic = funcvariadic;
4081 fexpr.funcformat = COERCE_EXPLICIT_CALL;
4082 fexpr.funccollid = result_collid;
4083 fexpr.inputcollid = input_collid;
4085 fexpr.location = -1;
4088 DatumGetPointer(OidFunctionCall1(func_form->protransform,
4089 PointerGetDatum(&fexpr)));
4092 if (!newexpr && allow_non_const)
4093 newexpr = inline_function(funcid, result_type, result_collid,
4094 input_collid, args, funcvariadic,
4095 func_tuple, context);
4097 ReleaseSysCache(func_tuple);
4103 * expand_function_arguments: convert named-notation args to positional args
4104 * and/or insert default args, as needed
4106 * If we need to change anything, the input argument list is copied, not
4109 * Note: this gets applied to operator argument lists too, even though the
4110 * cases it handles should never occur there. This should be OK since it
4111 * will fall through very quickly if there's nothing to do.
4114 expand_function_arguments(List *args, Oid result_type, HeapTuple func_tuple)
4116 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4117 bool has_named_args = false;
4120 /* Do we have any named arguments? */
4123 Node *arg = (Node *) lfirst(lc);
4125 if (IsA(arg, NamedArgExpr))
4127 has_named_args = true;
4132 /* If so, we must apply reorder_function_arguments */
4135 args = reorder_function_arguments(args, func_tuple);
4136 /* Recheck argument types and add casts if needed */
4137 recheck_cast_function_args(args, result_type, func_tuple);
4139 else if (list_length(args) < funcform->pronargs)
4141 /* No named args, but we seem to be short some defaults */
4142 args = add_function_defaults(args, func_tuple);
4143 /* Recheck argument types and add casts if needed */
4144 recheck_cast_function_args(args, result_type, func_tuple);
4151 * reorder_function_arguments: convert named-notation args to positional args
4153 * This function also inserts default argument values as needed, since it's
4154 * impossible to form a truly valid positional call without that.
4157 reorder_function_arguments(List *args, HeapTuple func_tuple)
4159 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4160 int pronargs = funcform->pronargs;
4161 int nargsprovided = list_length(args);
4162 Node *argarray[FUNC_MAX_ARGS];
4166 Assert(nargsprovided <= pronargs);
4167 if (pronargs > FUNC_MAX_ARGS)
4168 elog(ERROR, "too many function arguments");
4169 MemSet(argarray, 0, pronargs * sizeof(Node *));
4171 /* Deconstruct the argument list into an array indexed by argnumber */
4175 Node *arg = (Node *) lfirst(lc);
4177 if (!IsA(arg, NamedArgExpr))
4179 /* positional argument, assumed to precede all named args */
4180 Assert(argarray[i] == NULL);
4181 argarray[i++] = arg;
4185 NamedArgExpr *na = (NamedArgExpr *) arg;
4187 Assert(argarray[na->argnumber] == NULL);
4188 argarray[na->argnumber] = (Node *) na->arg;
4193 * Fetch default expressions, if needed, and insert into array at proper
4194 * locations (they aren't necessarily consecutive or all used)
4196 if (nargsprovided < pronargs)
4198 List *defaults = fetch_function_defaults(func_tuple);
4200 i = pronargs - funcform->pronargdefaults;
4201 foreach(lc, defaults)
4203 if (argarray[i] == NULL)
4204 argarray[i] = (Node *) lfirst(lc);
4209 /* Now reconstruct the args list in proper order */
4211 for (i = 0; i < pronargs; i++)
4213 Assert(argarray[i] != NULL);
4214 args = lappend(args, argarray[i]);
4221 * add_function_defaults: add missing function arguments from its defaults
4223 * This is used only when the argument list was positional to begin with,
4224 * and so we know we just need to add defaults at the end.
4227 add_function_defaults(List *args, HeapTuple func_tuple)
4229 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4230 int nargsprovided = list_length(args);
4234 /* Get all the default expressions from the pg_proc tuple */
4235 defaults = fetch_function_defaults(func_tuple);
4237 /* Delete any unused defaults from the list */
4238 ndelete = nargsprovided + list_length(defaults) - funcform->pronargs;
4240 elog(ERROR, "not enough default arguments");
4241 while (ndelete-- > 0)
4242 defaults = list_delete_first(defaults);
4244 /* And form the combined argument list, not modifying the input list */
4245 return list_concat(list_copy(args), defaults);
4249 * fetch_function_defaults: get function's default arguments as expression list
4252 fetch_function_defaults(HeapTuple func_tuple)
4255 Datum proargdefaults;
4259 /* The error cases here shouldn't happen, but check anyway */
4260 proargdefaults = SysCacheGetAttr(PROCOID, func_tuple,
4261 Anum_pg_proc_proargdefaults,
4264 elog(ERROR, "not enough default arguments");
4265 str = TextDatumGetCString(proargdefaults);
4266 defaults = castNode(List, stringToNode(str));
4272 * recheck_cast_function_args: recheck function args and typecast as needed
4273 * after adding defaults.
4275 * It is possible for some of the defaulted arguments to be polymorphic;
4276 * therefore we can't assume that the default expressions have the correct
4277 * data types already. We have to re-resolve polymorphics and do coercion
4278 * just like the parser did.
4280 * This should be a no-op if there are no polymorphic arguments,
4281 * but we do it anyway to be sure.
4283 * Note: if any casts are needed, the args list is modified in-place;
4284 * caller should have already copied the list structure.
4287 recheck_cast_function_args(List *args, Oid result_type, HeapTuple func_tuple)
4289 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4291 Oid actual_arg_types[FUNC_MAX_ARGS];
4292 Oid declared_arg_types[FUNC_MAX_ARGS];
4296 if (list_length(args) > FUNC_MAX_ARGS)
4297 elog(ERROR, "too many function arguments");
4301 actual_arg_types[nargs++] = exprType((Node *) lfirst(lc));
4303 Assert(nargs == funcform->pronargs);
4304 memcpy(declared_arg_types, funcform->proargtypes.values,
4305 funcform->pronargs * sizeof(Oid));
4306 rettype = enforce_generic_type_consistency(actual_arg_types,
4309 funcform->prorettype,
4311 /* let's just check we got the same answer as the parser did ... */
4312 if (rettype != result_type)
4313 elog(ERROR, "function's resolved result type changed during planning");
4315 /* perform any necessary typecasting of arguments */
4316 make_fn_arguments(NULL, args, actual_arg_types, declared_arg_types);
4320 * evaluate_function: try to pre-evaluate a function call
4322 * We can do this if the function is strict and has any constant-null inputs
4323 * (just return a null constant), or if the function is immutable and has all
4324 * constant inputs (call it and return the result as a Const node). In
4325 * estimation mode we are willing to pre-evaluate stable functions too.
4327 * Returns a simplified expression if successful, or NULL if cannot
4328 * simplify the function.
4331 evaluate_function(Oid funcid, Oid result_type, int32 result_typmod,
4332 Oid result_collid, Oid input_collid, List *args,
4334 HeapTuple func_tuple,
4335 eval_const_expressions_context *context)
4337 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4338 bool has_nonconst_input = false;
4339 bool has_null_input = false;
4344 * Can't simplify if it returns a set.
4346 if (funcform->proretset)
4350 * Can't simplify if it returns RECORD. The immediate problem is that it
4351 * will be needing an expected tupdesc which we can't supply here.
4353 * In the case where it has OUT parameters, it could get by without an
4354 * expected tupdesc, but we still have issues: get_expr_result_type()
4355 * doesn't know how to extract type info from a RECORD constant, and in
4356 * the case of a NULL function result there doesn't seem to be any clean
4357 * way to fix that. In view of the likelihood of there being still other
4358 * gotchas, seems best to leave the function call unreduced.
4360 if (funcform->prorettype == RECORDOID)
4364 * Check for constant inputs and especially constant-NULL inputs.
4368 if (IsA(lfirst(arg), Const))
4369 has_null_input |= ((Const *) lfirst(arg))->constisnull;
4371 has_nonconst_input = true;
4375 * If the function is strict and has a constant-NULL input, it will never
4376 * be called at all, so we can replace the call by a NULL constant, even
4377 * if there are other inputs that aren't constant, and even if the
4378 * function is not otherwise immutable.
4380 if (funcform->proisstrict && has_null_input)
4381 return (Expr *) makeNullConst(result_type, result_typmod,
4385 * Otherwise, can simplify only if all inputs are constants. (For a
4386 * non-strict function, constant NULL inputs are treated the same as
4387 * constant non-NULL inputs.)
4389 if (has_nonconst_input)
4393 * Ordinarily we are only allowed to simplify immutable functions. But for
4394 * purposes of estimation, we consider it okay to simplify functions that
4395 * are merely stable; the risk that the result might change from planning
4396 * time to execution time is worth taking in preference to not being able
4397 * to estimate the value at all.
4399 if (funcform->provolatile == PROVOLATILE_IMMUTABLE)
4401 else if (context->estimate && funcform->provolatile == PROVOLATILE_STABLE)
4407 * OK, looks like we can simplify this operator/function.
4409 * Build a new FuncExpr node containing the already-simplified arguments.
4411 newexpr = makeNode(FuncExpr);
4412 newexpr->funcid = funcid;
4413 newexpr->funcresulttype = result_type;
4414 newexpr->funcretset = false;
4415 newexpr->funcvariadic = funcvariadic;
4416 newexpr->funcformat = COERCE_EXPLICIT_CALL; /* doesn't matter */
4417 newexpr->funccollid = result_collid; /* doesn't matter */
4418 newexpr->inputcollid = input_collid;
4419 newexpr->args = args;
4420 newexpr->location = -1;
4422 return evaluate_expr((Expr *) newexpr, result_type, result_typmod,
4427 * inline_function: try to expand a function call inline
4429 * If the function is a sufficiently simple SQL-language function
4430 * (just "SELECT expression"), then we can inline it and avoid the rather
4431 * high per-call overhead of SQL functions. Furthermore, this can expose
4432 * opportunities for constant-folding within the function expression.
4434 * We have to beware of some special cases however. A directly or
4435 * indirectly recursive function would cause us to recurse forever,
4436 * so we keep track of which functions we are already expanding and
4437 * do not re-expand them. Also, if a parameter is used more than once
4438 * in the SQL-function body, we require it not to contain any volatile
4439 * functions (volatiles might deliver inconsistent answers) nor to be
4440 * unreasonably expensive to evaluate. The expensiveness check not only
4441 * prevents us from doing multiple evaluations of an expensive parameter
4442 * at runtime, but is a safety value to limit growth of an expression due
4443 * to repeated inlining.
4445 * We must also beware of changing the volatility or strictness status of
4446 * functions by inlining them.
4448 * Also, at the moment we can't inline functions returning RECORD. This
4449 * doesn't work in the general case because it discards information such
4450 * as OUT-parameter declarations.
4452 * Also, context-dependent expression nodes in the argument list are trouble.
4454 * Returns a simplified expression if successful, or NULL if cannot
4455 * simplify the function.
4458 inline_function(Oid funcid, Oid result_type, Oid result_collid,
4459 Oid input_collid, List *args,
4461 HeapTuple func_tuple,
4462 eval_const_expressions_context *context)
4464 Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4468 bool modifyTargetList;
4469 MemoryContext oldcxt;
4470 MemoryContext mycxt;
4471 inline_error_callback_arg callback_arg;
4472 ErrorContextCallback sqlerrcontext;
4474 SQLFunctionParseInfoPtr pinfo;
4476 List *raw_parsetree_list;
4484 * Forget it if the function is not SQL-language or has other showstopper
4485 * properties. (The prokind and nargs checks are just paranoia.)
4487 if (funcform->prolang != SQLlanguageId ||
4488 funcform->prokind != PROKIND_FUNCTION ||
4489 funcform->prosecdef ||
4490 funcform->proretset ||
4491 funcform->prorettype == RECORDOID ||
4492 !heap_attisnull(func_tuple, Anum_pg_proc_proconfig, NULL) ||
4493 funcform->pronargs != list_length(args))
4496 /* Check for recursive function, and give up trying to expand if so */
4497 if (list_member_oid(context->active_fns, funcid))
4500 /* Check permission to call function (fail later, if not) */
4501 if (pg_proc_aclcheck(funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
4504 /* Check whether a plugin wants to hook function entry/exit */
4505 if (FmgrHookIsNeeded(funcid))
4509 * Make a temporary memory context, so that we don't leak all the stuff
4510 * that parsing might create.
4512 mycxt = AllocSetContextCreate(CurrentMemoryContext,
4514 ALLOCSET_DEFAULT_SIZES);
4515 oldcxt = MemoryContextSwitchTo(mycxt);
4517 /* Fetch the function body */
4518 tmp = SysCacheGetAttr(PROCOID,
4520 Anum_pg_proc_prosrc,
4523 elog(ERROR, "null prosrc for function %u", funcid);
4524 src = TextDatumGetCString(tmp);
4527 * Setup error traceback support for ereport(). This is so that we can
4528 * finger the function that bad information came from.
4530 callback_arg.proname = NameStr(funcform->proname);
4531 callback_arg.prosrc = src;
4533 sqlerrcontext.callback = sql_inline_error_callback;
4534 sqlerrcontext.arg = (void *) &callback_arg;
4535 sqlerrcontext.previous = error_context_stack;
4536 error_context_stack = &sqlerrcontext;
4539 * Set up to handle parameters while parsing the function body. We need a
4540 * dummy FuncExpr node containing the already-simplified arguments to pass
4541 * to prepare_sql_fn_parse_info. (It is really only needed if there are
4542 * some polymorphic arguments, but for simplicity we always build it.)
4544 fexpr = makeNode(FuncExpr);
4545 fexpr->funcid = funcid;
4546 fexpr->funcresulttype = result_type;
4547 fexpr->funcretset = false;
4548 fexpr->funcvariadic = funcvariadic;
4549 fexpr->funcformat = COERCE_EXPLICIT_CALL; /* doesn't matter */
4550 fexpr->funccollid = result_collid; /* doesn't matter */
4551 fexpr->inputcollid = input_collid;
4553 fexpr->location = -1;
4555 pinfo = prepare_sql_fn_parse_info(func_tuple,
4560 * We just do parsing and parse analysis, not rewriting, because rewriting
4561 * will not affect table-free-SELECT-only queries, which is all that we
4562 * care about. Also, we can punt as soon as we detect more than one
4563 * command in the function body.
4565 raw_parsetree_list = pg_parse_query(src);
4566 if (list_length(raw_parsetree_list) != 1)
4569 pstate = make_parsestate(NULL);
4570 pstate->p_sourcetext = src;
4571 sql_fn_parser_setup(pstate, pinfo);
4573 querytree = transformTopLevelStmt(pstate, linitial(raw_parsetree_list));
4575 free_parsestate(pstate);
4578 * The single command must be a simple "SELECT expression".
4580 * Note: if you change the tests involved in this, see also plpgsql's
4581 * exec_simple_check_plan(). That generally needs to have the same idea
4582 * of what's a "simple expression", so that inlining a function that
4583 * previously wasn't inlined won't change plpgsql's conclusion.
4585 if (!IsA(querytree, Query) ||
4586 querytree->commandType != CMD_SELECT ||
4587 querytree->hasAggs ||
4588 querytree->hasWindowFuncs ||
4589 querytree->hasTargetSRFs ||
4590 querytree->hasSubLinks ||
4591 querytree->cteList ||
4592 querytree->rtable ||
4593 querytree->jointree->fromlist ||
4594 querytree->jointree->quals ||
4595 querytree->groupClause ||
4596 querytree->groupingSets ||
4597 querytree->havingQual ||
4598 querytree->windowClause ||
4599 querytree->distinctClause ||
4600 querytree->sortClause ||
4601 querytree->limitOffset ||
4602 querytree->limitCount ||
4603 querytree->setOperations ||
4604 list_length(querytree->targetList) != 1)
4608 * Make sure the function (still) returns what it's declared to. This
4609 * will raise an error if wrong, but that's okay since the function would
4610 * fail at runtime anyway. Note that check_sql_fn_retval will also insert
4611 * a RelabelType if needed to make the tlist expression match the declared
4612 * type of the function.
4614 * Note: we do not try this until we have verified that no rewriting was
4615 * needed; that's probably not important, but let's be careful.
4617 if (check_sql_fn_retval(funcid, result_type, list_make1(querytree),
4618 &modifyTargetList, NULL))
4619 goto fail; /* reject whole-tuple-result cases */
4621 /* Now we can grab the tlist expression */
4622 newexpr = (Node *) ((TargetEntry *) linitial(querytree->targetList))->expr;
4625 * If the SQL function returns VOID, we can only inline it if it is a
4626 * SELECT of an expression returning VOID (ie, it's just a redirection to
4627 * another VOID-returning function). In all non-VOID-returning cases,
4628 * check_sql_fn_retval should ensure that newexpr returns the function's
4629 * declared result type, so this test shouldn't fail otherwise; but we may
4630 * as well cope gracefully if it does.
4632 if (exprType(newexpr) != result_type)
4635 /* check_sql_fn_retval couldn't have made any dangerous tlist changes */
4636 Assert(!modifyTargetList);
4639 * Additional validity checks on the expression. It mustn't be more
4640 * volatile than the surrounding function (this is to avoid breaking hacks
4641 * that involve pretending a function is immutable when it really ain't).
4642 * If the surrounding function is declared strict, then the expression
4643 * must contain only strict constructs and must use all of the function
4644 * parameters (this is overkill, but an exact analysis is hard).
4646 if (funcform->provolatile == PROVOLATILE_IMMUTABLE &&
4647 contain_mutable_functions(newexpr))
4649 else if (funcform->provolatile == PROVOLATILE_STABLE &&
4650 contain_volatile_functions(newexpr))
4653 if (funcform->proisstrict &&
4654 contain_nonstrict_functions(newexpr))
4658 * If any parameter expression contains a context-dependent node, we can't
4659 * inline, for fear of putting such a node into the wrong context.
4661 if (contain_context_dependent_node((Node *) args))
4665 * We may be able to do it; there are still checks on parameter usage to
4666 * make, but those are most easily done in combination with the actual
4667 * substitution of the inputs. So start building expression with inputs
4670 usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
4671 newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
4674 /* Now check for parameter usage */
4678 Node *param = lfirst(arg);
4680 if (usecounts[i] == 0)
4682 /* Param not used at all: uncool if func is strict */
4683 if (funcform->proisstrict)
4686 else if (usecounts[i] != 1)
4688 /* Param used multiple times: uncool if expensive or volatile */
4692 * We define "expensive" as "contains any subplan or more than 10
4693 * operators". Note that the subplan search has to be done
4694 * explicitly, since cost_qual_eval() will barf on unplanned
4697 if (contain_subplans(param))
4699 cost_qual_eval(&eval_cost, list_make1(param), NULL);
4700 if (eval_cost.startup + eval_cost.per_tuple >
4701 10 * cpu_operator_cost)
4705 * Check volatility last since this is more expensive than the
4708 if (contain_volatile_functions(param))
4715 * Whew --- we can make the substitution. Copy the modified expression
4716 * out of the temporary memory context, and clean up.
4718 MemoryContextSwitchTo(oldcxt);
4720 newexpr = copyObject(newexpr);
4722 MemoryContextDelete(mycxt);
4725 * If the result is of a collatable type, force the result to expose the
4726 * correct collation. In most cases this does not matter, but it's
4727 * possible that the function result is used directly as a sort key or in
4728 * other places where we expect exprCollation() to tell the truth.
4730 if (OidIsValid(result_collid))
4732 Oid exprcoll = exprCollation(newexpr);
4734 if (OidIsValid(exprcoll) && exprcoll != result_collid)
4736 CollateExpr *newnode = makeNode(CollateExpr);
4738 newnode->arg = (Expr *) newexpr;
4739 newnode->collOid = result_collid;
4740 newnode->location = -1;
4742 newexpr = (Node *) newnode;
4747 * Since there is now no trace of the function in the plan tree, we must
4748 * explicitly record the plan's dependency on the function.
4751 record_plan_function_dependency(context->root, funcid);
4754 * Recursively try to simplify the modified expression. Here we must add
4755 * the current function to the context list of active functions.
4757 context->active_fns = lcons_oid(funcid, context->active_fns);
4758 newexpr = eval_const_expressions_mutator(newexpr, context);
4759 context->active_fns = list_delete_first(context->active_fns);
4761 error_context_stack = sqlerrcontext.previous;
4763 return (Expr *) newexpr;
4765 /* Here if func is not inlinable: release temp memory and return NULL */
4767 MemoryContextSwitchTo(oldcxt);
4768 MemoryContextDelete(mycxt);
4769 error_context_stack = sqlerrcontext.previous;
4775 * Replace Param nodes by appropriate actual parameters
4778 substitute_actual_parameters(Node *expr, int nargs, List *args,
4781 substitute_actual_parameters_context context;
4783 context.nargs = nargs;
4784 context.args = args;
4785 context.usecounts = usecounts;
4787 return substitute_actual_parameters_mutator(expr, &context);
4791 substitute_actual_parameters_mutator(Node *node,
4792 substitute_actual_parameters_context *context)
4796 if (IsA(node, Param))
4798 Param *param = (Param *) node;
4800 if (param->paramkind != PARAM_EXTERN)
4801 elog(ERROR, "unexpected paramkind: %d", (int) param->paramkind);
4802 if (param->paramid <= 0 || param->paramid > context->nargs)
4803 elog(ERROR, "invalid paramid: %d", param->paramid);
4805 /* Count usage of parameter */
4806 context->usecounts[param->paramid - 1]++;
4808 /* Select the appropriate actual arg and replace the Param with it */
4809 /* We don't need to copy at this time (it'll get done later) */
4810 return list_nth(context->args, param->paramid - 1);
4812 return expression_tree_mutator(node, substitute_actual_parameters_mutator,
4817 * error context callback to let us supply a call-stack traceback
4820 sql_inline_error_callback(void *arg)
4822 inline_error_callback_arg *callback_arg = (inline_error_callback_arg *) arg;
4823 int syntaxerrposition;
4825 /* If it's a syntax error, convert to internal syntax error report */
4826 syntaxerrposition = geterrposition();
4827 if (syntaxerrposition > 0)
4830 internalerrposition(syntaxerrposition);
4831 internalerrquery(callback_arg->prosrc);
4834 errcontext("SQL function \"%s\" during inlining", callback_arg->proname);
4838 * evaluate_expr: pre-evaluate a constant expression
4840 * We use the executor's routine ExecEvalExpr() to avoid duplication of
4841 * code and ensure we get the same result as the executor would get.
4844 evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod,
4845 Oid result_collation)
4848 ExprState *exprstate;
4849 MemoryContext oldcontext;
4853 bool resultTypByVal;
4856 * To use the executor, we need an EState.
4858 estate = CreateExecutorState();
4860 /* We can use the estate's working context to avoid memory leaks. */
4861 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
4863 /* Make sure any opfuncids are filled in. */
4864 fix_opfuncids((Node *) expr);
4867 * Prepare expr for execution. (Note: we can't use ExecPrepareExpr
4868 * because it'd result in recursively invoking eval_const_expressions.)
4870 exprstate = ExecInitExpr(expr, NULL);
4875 * It is OK to use a default econtext because none of the ExecEvalExpr()
4876 * code used in this situation will use econtext. That might seem
4877 * fortuitous, but it's not so unreasonable --- a constant expression does
4878 * not depend on context, by definition, n'est ce pas?
4880 const_val = ExecEvalExprSwitchContext(exprstate,
4881 GetPerTupleExprContext(estate),
4884 /* Get info needed about result datatype */
4885 get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
4887 /* Get back to outer memory context */
4888 MemoryContextSwitchTo(oldcontext);
4891 * Must copy result out of sub-context used by expression eval.
4893 * Also, if it's varlena, forcibly detoast it. This protects us against
4894 * storing TOAST pointers into plans that might outlive the referenced
4895 * data. (makeConst would handle detoasting anyway, but it's worth a few
4896 * extra lines here so that we can do the copy and detoast in one step.)
4900 if (resultTypLen == -1)
4901 const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
4903 const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
4906 /* Release all the junk we just created */
4907 FreeExecutorState(estate);
4910 * Make the constant result node.
4912 return (Expr *) makeConst(result_type, result_typmod, result_collation,
4914 const_val, const_is_null,
4920 * inline_set_returning_function
4921 * Attempt to "inline" a set-returning function in the FROM clause.
4923 * "rte" is an RTE_FUNCTION rangetable entry. If it represents a call of a
4924 * set-returning SQL function that can safely be inlined, expand the function
4925 * and return the substitute Query structure. Otherwise, return NULL.
4927 * This has a good deal of similarity to inline_function(), but that's
4928 * for the non-set-returning case, and there are enough differences to
4929 * justify separate functions.
4932 inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
4934 RangeTblFunction *rtfunc;
4937 HeapTuple func_tuple;
4938 Form_pg_proc funcform;
4942 bool modifyTargetList;
4943 MemoryContext oldcxt;
4944 MemoryContext mycxt;
4945 List *saveInvalItems;
4946 inline_error_callback_arg callback_arg;
4947 ErrorContextCallback sqlerrcontext;
4948 SQLFunctionParseInfoPtr pinfo;
4949 List *raw_parsetree_list;
4950 List *querytree_list;
4953 Assert(rte->rtekind == RTE_FUNCTION);
4956 * It doesn't make a lot of sense for a SQL SRF to refer to itself in its
4957 * own FROM clause, since that must cause infinite recursion at runtime.
4958 * It will cause this code to recurse too, so check for stack overflow.
4959 * (There's no need to do more.)
4961 check_stack_depth();
4963 /* Fail if the RTE has ORDINALITY - we don't implement that here. */
4964 if (rte->funcordinality)
4967 /* Fail if RTE isn't a single, simple FuncExpr */
4968 if (list_length(rte->functions) != 1)
4970 rtfunc = (RangeTblFunction *) linitial(rte->functions);
4972 if (!IsA(rtfunc->funcexpr, FuncExpr))
4974 fexpr = (FuncExpr *) rtfunc->funcexpr;
4976 func_oid = fexpr->funcid;
4979 * The function must be declared to return a set, else inlining would
4980 * change the results if the contained SELECT didn't return exactly one
4983 if (!fexpr->funcretset)
4987 * Refuse to inline if the arguments contain any volatile functions or
4988 * sub-selects. Volatile functions are rejected because inlining may
4989 * result in the arguments being evaluated multiple times, risking a
4990 * change in behavior. Sub-selects are rejected partly for implementation
4991 * reasons (pushing them down another level might change their behavior)
4992 * and partly because they're likely to be expensive and so multiple
4993 * evaluation would be bad.
4995 if (contain_volatile_functions((Node *) fexpr->args) ||
4996 contain_subplans((Node *) fexpr->args))
4999 /* Check permission to call function (fail later, if not) */
5000 if (pg_proc_aclcheck(func_oid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
5003 /* Check whether a plugin wants to hook function entry/exit */
5004 if (FmgrHookIsNeeded(func_oid))
5008 * OK, let's take a look at the function's pg_proc entry.
5010 func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_oid));
5011 if (!HeapTupleIsValid(func_tuple))
5012 elog(ERROR, "cache lookup failed for function %u", func_oid);
5013 funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
5016 * Forget it if the function is not SQL-language or has other showstopper
5017 * properties. In particular it mustn't be declared STRICT, since we
5018 * couldn't enforce that. It also mustn't be VOLATILE, because that is
5019 * supposed to cause it to be executed with its own snapshot, rather than
5020 * sharing the snapshot of the calling query. We also disallow returning
5021 * SETOF VOID, because inlining would result in exposing the actual result
5022 * of the function's last SELECT, which should not happen in that case.
5023 * (Rechecking prokind and proretset is just paranoia.)
5025 if (funcform->prolang != SQLlanguageId ||
5026 funcform->prokind != PROKIND_FUNCTION ||
5027 funcform->proisstrict ||
5028 funcform->provolatile == PROVOLATILE_VOLATILE ||
5029 funcform->prorettype == VOIDOID ||
5030 funcform->prosecdef ||
5031 !funcform->proretset ||
5032 !heap_attisnull(func_tuple, Anum_pg_proc_proconfig, NULL))
5034 ReleaseSysCache(func_tuple);
5039 * Make a temporary memory context, so that we don't leak all the stuff
5040 * that parsing might create.
5042 mycxt = AllocSetContextCreate(CurrentMemoryContext,
5043 "inline_set_returning_function",
5044 ALLOCSET_DEFAULT_SIZES);
5045 oldcxt = MemoryContextSwitchTo(mycxt);
5048 * When we call eval_const_expressions below, it might try to add items to
5049 * root->glob->invalItems. Since it is running in the temp context, those
5050 * items will be in that context, and will need to be copied out if we're
5051 * successful. Temporarily reset the list so that we can keep those items
5052 * separate from the pre-existing list contents.
5054 saveInvalItems = root->glob->invalItems;
5055 root->glob->invalItems = NIL;
5057 /* Fetch the function body */
5058 tmp = SysCacheGetAttr(PROCOID,
5060 Anum_pg_proc_prosrc,
5063 elog(ERROR, "null prosrc for function %u", func_oid);
5064 src = TextDatumGetCString(tmp);
5067 * Setup error traceback support for ereport(). This is so that we can
5068 * finger the function that bad information came from.
5070 callback_arg.proname = NameStr(funcform->proname);
5071 callback_arg.prosrc = src;
5073 sqlerrcontext.callback = sql_inline_error_callback;
5074 sqlerrcontext.arg = (void *) &callback_arg;
5075 sqlerrcontext.previous = error_context_stack;
5076 error_context_stack = &sqlerrcontext;
5079 * Run eval_const_expressions on the function call. This is necessary to
5080 * ensure that named-argument notation is converted to positional notation
5081 * and any default arguments are inserted. It's a bit of overkill for the
5082 * arguments, since they'll get processed again later, but no harm will be
5085 fexpr = (FuncExpr *) eval_const_expressions(root, (Node *) fexpr);
5087 /* It should still be a call of the same function, but let's check */
5088 if (!IsA(fexpr, FuncExpr) ||
5089 fexpr->funcid != func_oid)
5092 /* Arg list length should now match the function */
5093 if (list_length(fexpr->args) != funcform->pronargs)
5097 * Set up to handle parameters while parsing the function body. We can
5098 * use the FuncExpr just created as the input for
5099 * prepare_sql_fn_parse_info.
5101 pinfo = prepare_sql_fn_parse_info(func_tuple,
5103 fexpr->inputcollid);
5106 * Parse, analyze, and rewrite (unlike inline_function(), we can't skip
5107 * rewriting here). We can fail as soon as we find more than one query,
5110 raw_parsetree_list = pg_parse_query(src);
5111 if (list_length(raw_parsetree_list) != 1)
5114 querytree_list = pg_analyze_and_rewrite_params(linitial(raw_parsetree_list),
5116 (ParserSetupHook) sql_fn_parser_setup,
5118 if (list_length(querytree_list) != 1)
5120 querytree = linitial(querytree_list);
5123 * The single command must be a plain SELECT.
5125 if (!IsA(querytree, Query) ||
5126 querytree->commandType != CMD_SELECT)
5130 * Make sure the function (still) returns what it's declared to. This
5131 * will raise an error if wrong, but that's okay since the function would
5132 * fail at runtime anyway. Note that check_sql_fn_retval will also insert
5133 * RelabelType(s) and/or NULL columns if needed to make the tlist
5134 * expression(s) match the declared type of the function.
5136 * If the function returns a composite type, don't inline unless the check
5137 * shows it's returning a whole tuple result; otherwise what it's
5138 * returning is a single composite column which is not what we need. (Like
5139 * check_sql_fn_retval, we deliberately exclude domains over composite
5142 if (!check_sql_fn_retval(func_oid, fexpr->funcresulttype,
5144 &modifyTargetList, NULL) &&
5145 (get_typtype(fexpr->funcresulttype) == TYPTYPE_COMPOSITE ||
5146 fexpr->funcresulttype == RECORDOID))
5147 goto fail; /* reject not-whole-tuple-result cases */
5150 * If we had to modify the tlist to make it match, and the statement is
5151 * one in which changing the tlist contents could change semantics, we
5152 * have to punt and not inline.
5154 if (modifyTargetList)
5158 * If it returns RECORD, we have to check against the column type list
5159 * provided in the RTE; check_sql_fn_retval can't do that. (If no match,
5160 * we just fail to inline, rather than complaining; see notes for
5161 * tlist_matches_coltypelist.) We don't have to do this for functions
5162 * with declared OUT parameters, even though their funcresulttype is
5163 * RECORDOID, so check get_func_result_type too.
5165 if (fexpr->funcresulttype == RECORDOID &&
5166 get_func_result_type(func_oid, NULL, NULL) == TYPEFUNC_RECORD &&
5167 !tlist_matches_coltypelist(querytree->targetList,
5168 rtfunc->funccoltypes))
5172 * Looks good --- substitute parameters into the query.
5174 querytree = substitute_actual_srf_parameters(querytree,
5179 * Copy the modified query out of the temporary memory context, and clean
5182 MemoryContextSwitchTo(oldcxt);
5184 querytree = copyObject(querytree);
5186 /* copy up any new invalItems, too */
5187 root->glob->invalItems = list_concat(saveInvalItems,
5188 copyObject(root->glob->invalItems));
5190 MemoryContextDelete(mycxt);
5191 error_context_stack = sqlerrcontext.previous;
5192 ReleaseSysCache(func_tuple);
5195 * We don't have to fix collations here because the upper query is already
5196 * parsed, ie, the collations in the RTE are what count.
5200 * Since there is now no trace of the function in the plan tree, we must
5201 * explicitly record the plan's dependency on the function.
5203 record_plan_function_dependency(root, func_oid);
5207 /* Here if func is not inlinable: release temp memory and return NULL */
5209 MemoryContextSwitchTo(oldcxt);
5210 root->glob->invalItems = saveInvalItems;
5211 MemoryContextDelete(mycxt);
5212 error_context_stack = sqlerrcontext.previous;
5213 ReleaseSysCache(func_tuple);
5219 * Replace Param nodes by appropriate actual parameters
5221 * This is just enough different from substitute_actual_parameters()
5222 * that it needs its own code.
5225 substitute_actual_srf_parameters(Query *expr, int nargs, List *args)
5227 substitute_actual_srf_parameters_context context;
5229 context.nargs = nargs;
5230 context.args = args;
5231 context.sublevels_up = 1;
5233 return query_tree_mutator(expr,
5234 substitute_actual_srf_parameters_mutator,
5240 substitute_actual_srf_parameters_mutator(Node *node,
5241 substitute_actual_srf_parameters_context *context)
5247 if (IsA(node, Query))
5249 context->sublevels_up++;
5250 result = (Node *) query_tree_mutator((Query *) node,
5251 substitute_actual_srf_parameters_mutator,
5254 context->sublevels_up--;
5257 if (IsA(node, Param))
5259 Param *param = (Param *) node;
5261 if (param->paramkind == PARAM_EXTERN)
5263 if (param->paramid <= 0 || param->paramid > context->nargs)
5264 elog(ERROR, "invalid paramid: %d", param->paramid);
5267 * Since the parameter is being inserted into a subquery, we must
5270 result = copyObject(list_nth(context->args, param->paramid - 1));
5271 IncrementVarSublevelsUp(result, context->sublevels_up, 0);
5275 return expression_tree_mutator(node,
5276 substitute_actual_srf_parameters_mutator,
5281 * Check whether a SELECT targetlist emits the specified column types,
5282 * to see if it's safe to inline a function returning record.
5284 * We insist on exact match here. The executor allows binary-coercible
5285 * cases too, but we don't have a way to preserve the correct column types
5286 * in the correct places if we inline the function in such a case.
5288 * Note that we only check type OIDs not typmods; this agrees with what the
5289 * executor would do at runtime, and attributing a specific typmod to a
5290 * function result is largely wishful thinking anyway.
5293 tlist_matches_coltypelist(List *tlist, List *coltypelist)
5295 ListCell *tlistitem;
5296 ListCell *clistitem;
5298 clistitem = list_head(coltypelist);
5299 foreach(tlistitem, tlist)
5301 TargetEntry *tle = (TargetEntry *) lfirst(tlistitem);
5305 continue; /* ignore junk columns */
5307 if (clistitem == NULL)
5308 return false; /* too many tlist items */
5310 coltype = lfirst_oid(clistitem);
5311 clistitem = lnext(clistitem);
5313 if (exprType((Node *) tle->expr) != coltype)
5314 return false; /* column type mismatch */
5317 if (clistitem != NULL)
5318 return false; /* too few tlist items */