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