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