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