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