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