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