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