PostgreSQL Source Code  git master
clauses.h File Reference
#include "access/htup.h"
#include "nodes/pathnodes.h"
Include dependency graph for clauses.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  WindowFuncLists
 

Functions

bool contain_agg_clause (Node *clause)
 
void get_agg_clause_costs (PlannerInfo *root, Node *clause, AggSplit aggsplit, AggClauseCosts *costs)
 
bool contain_window_function (Node *clause)
 
WindowFuncListsfind_window_functions (Node *clause, Index maxWinRef)
 
double expression_returns_set_rows (PlannerInfo *root, Node *clause)
 
bool contain_subplans (Node *clause)
 
char max_parallel_hazard (Query *parse)
 
bool is_parallel_safe (PlannerInfo *root, Node *node)
 
bool contain_nonstrict_functions (Node *clause)
 
bool contain_exec_param (Node *clause, List *param_ids)
 
bool contain_leaked_vars (Node *clause)
 
Relids find_nonnullable_rels (Node *clause)
 
Listfind_nonnullable_vars (Node *clause)
 
Listfind_forced_null_vars (Node *clause)
 
Varfind_forced_null_var (Node *clause)
 
bool is_pseudo_constant_clause (Node *clause)
 
bool is_pseudo_constant_clause_relids (Node *clause, Relids relids)
 
int NumRelids (Node *clause)
 
void CommuteOpExpr (OpExpr *clause)
 
Queryinline_set_returning_function (PlannerInfo *root, RangeTblEntry *rte)
 

Function Documentation

◆ CommuteOpExpr()

void CommuteOpExpr ( OpExpr clause)

Definition at line 2180 of file clauses.c.

References OpExpr::args, elog, ERROR, get_commutator(), InvalidOid, is_opclause(), linitial, list_length(), lsecond, OidIsValid, OpExpr::opfuncid, and OpExpr::opno.

Referenced by get_switched_clauses().

2181 {
2182  Oid opoid;
2183  Node *temp;
2184 
2185  /* Sanity checks: caller is at fault if these fail */
2186  if (!is_opclause(clause) ||
2187  list_length(clause->args) != 2)
2188  elog(ERROR, "cannot commute non-binary-operator clause");
2189 
2190  opoid = get_commutator(clause->opno);
2191 
2192  if (!OidIsValid(opoid))
2193  elog(ERROR, "could not find commutator for operator %u",
2194  clause->opno);
2195 
2196  /*
2197  * modify the clause in-place!
2198  */
2199  clause->opno = opoid;
2200  clause->opfuncid = InvalidOid;
2201  /* opresulttype, opretset, opcollid, inputcollid need not change */
2202 
2203  temp = linitial(clause->args);
2204  linitial(clause->args) = lsecond(clause->args);
2205  lsecond(clause->args) = temp;
2206 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1421
Definition: nodes.h:528
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:652
#define lsecond(l)
Definition: pg_list.h:179
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:43
Oid opfuncid
Definition: primnodes.h:517
#define InvalidOid
Definition: postgres_ext.h:36
static int list_length(const List *l)
Definition: pg_list.h:149
#define elog(elevel,...)
Definition: elog.h:214
Oid opno
Definition: primnodes.h:516
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:66
List * args
Definition: primnodes.h:522

◆ contain_agg_clause()

bool contain_agg_clause ( Node clause)

Definition at line 179 of file clauses.c.

References contain_agg_clause_walker().

Referenced by get_eclass_for_sort_expr(), and subquery_planner().

180 {
181  return contain_agg_clause_walker(clause, NULL);
182 }
static bool contain_agg_clause_walker(Node *node, void *context)
Definition: clauses.c:185

◆ contain_exec_param()

bool contain_exec_param ( Node clause,
List param_ids 
)

Definition at line 1235 of file clauses.c.

References contain_exec_param_walker().

Referenced by test_opexpr_is_hashable().

1236 {
1237  return contain_exec_param_walker(clause, param_ids);
1238 }
static bool contain_exec_param_walker(Node *node, List *param_ids)
Definition: clauses.c:1241

◆ contain_leaked_vars()

bool contain_leaked_vars ( Node clause)

Definition at line 1361 of file clauses.c.

References contain_leaked_vars_walker().

Referenced by make_restrictinfo_internal(), and qual_is_pushdown_safe().

1362 {
1363  return contain_leaked_vars_walker(clause, NULL);
1364 }
static bool contain_leaked_vars_walker(Node *node, void *context)
Definition: clauses.c:1373

◆ contain_nonstrict_functions()

bool contain_nonstrict_functions ( Node clause)

Definition at line 1094 of file clauses.c.

References contain_nonstrict_functions_walker().

Referenced by inline_function(), process_equivalence(), and pullup_replace_vars_callback().

1095 {
1096  return contain_nonstrict_functions_walker(clause, NULL);
1097 }
static bool contain_nonstrict_functions_walker(Node *node, void *context)
Definition: clauses.c:1106

◆ contain_subplans()

bool contain_subplans ( Node clause)

Definition at line 610 of file clauses.c.

References contain_subplans_walker().

Referenced by convert_EXISTS_to_ANY(), ExecInitValuesScan(), inline_function(), inline_set_returning_function(), qual_is_pushdown_safe(), and subquery_planner().

611 {
612  return contain_subplans_walker(clause, NULL);
613 }
static bool contain_subplans_walker(Node *node, void *context)
Definition: clauses.c:616

◆ contain_window_function()

bool contain_window_function ( Node clause)

Definition at line 494 of file clauses.c.

References contain_windowfuncs().

Referenced by get_eclass_for_sort_expr(), and qual_is_pushdown_safe().

495 {
496  return contain_windowfuncs(clause);
497 }
bool contain_windowfuncs(Node *node)
Definition: rewriteManip.c:197

◆ expression_returns_set_rows()

double expression_returns_set_rows ( PlannerInfo root,
Node clause 
)

Definition at line 569 of file clauses.c.

References clamp_row_est(), FuncExpr::funcid, FuncExpr::funcretset, get_function_rows(), IsA, OpExpr::opfuncid, OpExpr::opretset, and set_opfuncid().

Referenced by create_set_projection_path(), estimate_num_groups(), and set_function_size_estimates().

570 {
571  if (clause == NULL)
572  return 1.0;
573  if (IsA(clause, FuncExpr))
574  {
575  FuncExpr *expr = (FuncExpr *) clause;
576 
577  if (expr->funcretset)
578  return clamp_row_est(get_function_rows(root, expr->funcid, clause));
579  }
580  if (IsA(clause, OpExpr))
581  {
582  OpExpr *expr = (OpExpr *) clause;
583 
584  if (expr->opretset)
585  {
586  set_opfuncid(expr);
587  return clamp_row_est(get_function_rows(root, expr->opfuncid, clause));
588  }
589  }
590  return 1.0;
591 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
bool funcretset
Definition: primnodes.h:471
Oid funcid
Definition: primnodes.h:469
Oid opfuncid
Definition: primnodes.h:517
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1667
double clamp_row_est(double nrows)
Definition: costsize.c:196
bool opretset
Definition: primnodes.h:519
double get_function_rows(PlannerInfo *root, Oid funcid, Node *node)
Definition: plancat.c:1968

◆ find_forced_null_var()

Var* find_forced_null_var ( Node clause)

Definition at line 2012 of file clauses.c.

References NullTest::arg, BooleanTest::arg, NullTest::argisrow, BooleanTest::booltesttype, IS_NULL, IS_UNKNOWN, IsA, NullTest::nulltesttype, and Var::varlevelsup.

Referenced by check_redundant_nullability_qual(), and find_forced_null_vars().

2013 {
2014  if (node == NULL)
2015  return NULL;
2016  if (IsA(node, NullTest))
2017  {
2018  /* check for var IS NULL */
2019  NullTest *expr = (NullTest *) node;
2020 
2021  if (expr->nulltesttype == IS_NULL && !expr->argisrow)
2022  {
2023  Var *var = (Var *) expr->arg;
2024 
2025  if (var && IsA(var, Var) &&
2026  var->varlevelsup == 0)
2027  return var;
2028  }
2029  }
2030  else if (IsA(node, BooleanTest))
2031  {
2032  /* var IS UNKNOWN is equivalent to var IS NULL */
2033  BooleanTest *expr = (BooleanTest *) node;
2034 
2035  if (expr->booltesttype == IS_UNKNOWN)
2036  {
2037  Var *var = (Var *) expr->arg;
2038 
2039  if (var && IsA(var, Var) &&
2040  var->varlevelsup == 0)
2041  return var;
2042  }
2043  }
2044  return NULL;
2045 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
Index varlevelsup
Definition: primnodes.h:191
Definition: primnodes.h:181
Expr * arg
Definition: primnodes.h:1222
Expr * arg
Definition: primnodes.h:1245
BoolTestType booltesttype
Definition: primnodes.h:1246
NullTestType nulltesttype
Definition: primnodes.h:1223
bool argisrow
Definition: primnodes.h:1224

◆ find_forced_null_vars()

List* find_forced_null_vars ( Node clause)

Definition at line 1953 of file clauses.c.

References AND_EXPR, BoolExpr::args, BoolExpr::boolop, find_forced_null_var(), find_forced_null_vars(), IsA, lfirst, list_concat(), list_make1, and NIL.

Referenced by find_forced_null_vars(), and reduce_outer_joins_pass2().

1954 {
1955  List *result = NIL;
1956  Var *var;
1957  ListCell *l;
1958 
1959  if (node == NULL)
1960  return NIL;
1961  /* Check single-clause cases using subroutine */
1962  var = find_forced_null_var(node);
1963  if (var)
1964  {
1965  result = list_make1(var);
1966  }
1967  /* Otherwise, handle AND-conditions */
1968  else if (IsA(node, List))
1969  {
1970  /*
1971  * At top level, we are examining an implicit-AND list: if any of the
1972  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
1973  */
1974  foreach(l, (List *) node)
1975  {
1976  result = list_concat(result,
1978  }
1979  }
1980  else if (IsA(node, BoolExpr))
1981  {
1982  BoolExpr *expr = (BoolExpr *) node;
1983 
1984  /*
1985  * We don't bother considering the OR case, because it's fairly
1986  * unlikely anyone would write "v1 IS NULL OR v1 IS NULL". Likewise,
1987  * the NOT case isn't worth expending code on.
1988  */
1989  if (expr->boolop == AND_EXPR)
1990  {
1991  /* At top level we can just recurse (to the List case) */
1992  result = find_forced_null_vars((Node *) expr->args);
1993  }
1994  }
1995  return result;
1996 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
Definition: nodes.h:528
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
Definition: primnodes.h:181
List * find_forced_null_vars(Node *node)
Definition: clauses.c:1953
#define list_make1(x1)
Definition: pg_list.h:206
BoolExprType boolop
Definition: primnodes.h:582
#define lfirst(lc)
Definition: pg_list.h:169
List * args
Definition: primnodes.h:583
Definition: pg_list.h:50
Var * find_forced_null_var(Node *node)
Definition: clauses.c:2012

◆ find_nonnullable_rels()

Relids find_nonnullable_rels ( Node clause)

Definition at line 1535 of file clauses.c.

References find_nonnullable_rels_walker().

Referenced by make_outerjoininfo(), and reduce_outer_joins_pass2().

1536 {
1537  return find_nonnullable_rels_walker(clause, true);
1538 }
static Relids find_nonnullable_rels_walker(Node *node, bool top_level)
Definition: clauses.c:1541

◆ find_nonnullable_vars()

List* find_nonnullable_vars ( Node clause)

Definition at line 1760 of file clauses.c.

References find_nonnullable_vars_walker().

Referenced by reduce_outer_joins_pass2().

1761 {
1762  return find_nonnullable_vars_walker(clause, true);
1763 }
static List * find_nonnullable_vars_walker(Node *node, bool top_level)
Definition: clauses.c:1766

◆ find_window_functions()

WindowFuncLists* find_window_functions ( Node clause,
Index  maxWinRef 
)

Definition at line 507 of file clauses.c.

References find_window_functions_walker(), WindowFuncLists::maxWinRef, WindowFuncLists::numWindowFuncs, palloc(), palloc0(), and WindowFuncLists::windowFuncs.

Referenced by grouping_planner().

508 {
509  WindowFuncLists *lists = palloc(sizeof(WindowFuncLists));
510 
511  lists->numWindowFuncs = 0;
512  lists->maxWinRef = maxWinRef;
513  lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
514  (void) find_window_functions_walker(clause, lists);
515  return lists;
516 }
Index maxWinRef
Definition: clauses.h:23
int numWindowFuncs
Definition: clauses.h:22
void * palloc0(Size size)
Definition: mcxt.c:981
void * palloc(Size size)
Definition: mcxt.c:950
static bool find_window_functions_walker(Node *node, WindowFuncLists *lists)
Definition: clauses.c:519
Definition: pg_list.h:50
List ** windowFuncs
Definition: clauses.h:24

◆ get_agg_clause_costs()

void get_agg_clause_costs ( PlannerInfo root,
Node clause,
AggSplit  aggsplit,
AggClauseCosts costs 
)

Definition at line 229 of file clauses.c.

References get_agg_clause_costs_context::aggsplit, get_agg_clause_costs_context::costs, get_agg_clause_costs_walker(), and get_agg_clause_costs_context::root.

Referenced by create_partial_grouping_paths(), estimate_path_cost_size(), and grouping_planner().

231 {
233 
234  context.root = root;
235  context.aggsplit = aggsplit;
236  context.costs = costs;
237  (void) get_agg_clause_costs_walker(clause, &context);
238 }
AggClauseCosts * costs
Definition: clauses.c:61
static bool get_agg_clause_costs_walker(Node *node, get_agg_clause_costs_context *context)
Definition: clauses.c:241

◆ inline_set_returning_function()

Query* inline_set_returning_function ( PlannerInfo root,
RangeTblEntry rte 
)

Definition at line 4849 of file clauses.c.

References ACL_EXECUTE, ACLCHECK_OK, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ErrorContextCallback::arg, FuncExpr::args, Assert, BuildDescFromLists(), ErrorContextCallback::callback, check_sql_fn_retval(), check_stack_depth(), CMD_SELECT, Query::commandType, contain_subplans(), contain_volatile_functions(), copyObject, CurrentMemoryContext, elog, ERROR, error_context_stack, FmgrHookIsNeeded, RangeTblFunction::funccolcollations, RangeTblFunction::funccolnames, RangeTblFunction::funccoltypes, RangeTblFunction::funccoltypmods, RangeTblFunction::funcexpr, FuncExpr::funcid, RangeTblEntry::funcordinality, FuncExpr::funcretset, RangeTblEntry::functions, get_expr_result_type(), GETSTRUCT, GetUserId(), heap_attisnull(), HeapTupleIsValid, IsA, linitial, linitial_node, list_length(), list_make1, MemoryContextDelete(), MemoryContextSwitchTo(), NameStr, ObjectIdGetDatum, pg_analyze_and_rewrite_params(), pg_parse_query(), pg_proc_aclcheck(), prepare_sql_fn_parse_info(), ErrorContextCallback::previous, PROCOID, inline_error_callback_arg::proname, inline_error_callback_arg::prosrc, querytree(), record_plan_function_dependency(), ReleaseSysCache(), RTE_FUNCTION, RangeTblEntry::rtekind, SearchSysCache1(), sql_fn_parser_setup(), sql_inline_error_callback(), substitute_actual_srf_parameters(), SysCacheGetAttr(), TextDatumGetCString, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, and TYPEFUNC_RECORD.

Referenced by preprocess_function_rtes().

4850 {
4851  RangeTblFunction *rtfunc;
4852  FuncExpr *fexpr;
4853  Oid func_oid;
4854  HeapTuple func_tuple;
4855  Form_pg_proc funcform;
4856  char *src;
4857  Datum tmp;
4858  bool isNull;
4859  MemoryContext oldcxt;
4860  MemoryContext mycxt;
4861  inline_error_callback_arg callback_arg;
4862  ErrorContextCallback sqlerrcontext;
4864  TypeFuncClass functypclass;
4865  TupleDesc rettupdesc;
4866  List *raw_parsetree_list;
4867  List *querytree_list;
4868  Query *querytree;
4869 
4870  Assert(rte->rtekind == RTE_FUNCTION);
4871 
4872  /*
4873  * It doesn't make a lot of sense for a SQL SRF to refer to itself in its
4874  * own FROM clause, since that must cause infinite recursion at runtime.
4875  * It will cause this code to recurse too, so check for stack overflow.
4876  * (There's no need to do more.)
4877  */
4879 
4880  /* Fail if the RTE has ORDINALITY - we don't implement that here. */
4881  if (rte->funcordinality)
4882  return NULL;
4883 
4884  /* Fail if RTE isn't a single, simple FuncExpr */
4885  if (list_length(rte->functions) != 1)
4886  return NULL;
4887  rtfunc = (RangeTblFunction *) linitial(rte->functions);
4888 
4889  if (!IsA(rtfunc->funcexpr, FuncExpr))
4890  return NULL;
4891  fexpr = (FuncExpr *) rtfunc->funcexpr;
4892 
4893  func_oid = fexpr->funcid;
4894 
4895  /*
4896  * The function must be declared to return a set, else inlining would
4897  * change the results if the contained SELECT didn't return exactly one
4898  * row.
4899  */
4900  if (!fexpr->funcretset)
4901  return NULL;
4902 
4903  /*
4904  * Refuse to inline if the arguments contain any volatile functions or
4905  * sub-selects. Volatile functions are rejected because inlining may
4906  * result in the arguments being evaluated multiple times, risking a
4907  * change in behavior. Sub-selects are rejected partly for implementation
4908  * reasons (pushing them down another level might change their behavior)
4909  * and partly because they're likely to be expensive and so multiple
4910  * evaluation would be bad.
4911  */
4912  if (contain_volatile_functions((Node *) fexpr->args) ||
4913  contain_subplans((Node *) fexpr->args))
4914  return NULL;
4915 
4916  /* Check permission to call function (fail later, if not) */
4917  if (pg_proc_aclcheck(func_oid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
4918  return NULL;
4919 
4920  /* Check whether a plugin wants to hook function entry/exit */
4921  if (FmgrHookIsNeeded(func_oid))
4922  return NULL;
4923 
4924  /*
4925  * OK, let's take a look at the function's pg_proc entry.
4926  */
4927  func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_oid));
4928  if (!HeapTupleIsValid(func_tuple))
4929  elog(ERROR, "cache lookup failed for function %u", func_oid);
4930  funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4931 
4932  /*
4933  * Forget it if the function is not SQL-language or has other showstopper
4934  * properties. In particular it mustn't be declared STRICT, since we
4935  * couldn't enforce that. It also mustn't be VOLATILE, because that is
4936  * supposed to cause it to be executed with its own snapshot, rather than
4937  * sharing the snapshot of the calling query. We also disallow returning
4938  * SETOF VOID, because inlining would result in exposing the actual result
4939  * of the function's last SELECT, which should not happen in that case.
4940  * (Rechecking prokind, proretset, and pronargs is just paranoia.)
4941  */
4942  if (funcform->prolang != SQLlanguageId ||
4943  funcform->prokind != PROKIND_FUNCTION ||
4944  funcform->proisstrict ||
4945  funcform->provolatile == PROVOLATILE_VOLATILE ||
4946  funcform->prorettype == VOIDOID ||
4947  funcform->prosecdef ||
4948  !funcform->proretset ||
4949  list_length(fexpr->args) != funcform->pronargs ||
4950  !heap_attisnull(func_tuple, Anum_pg_proc_proconfig, NULL))
4951  {
4952  ReleaseSysCache(func_tuple);
4953  return NULL;
4954  }
4955 
4956  /*
4957  * Make a temporary memory context, so that we don't leak all the stuff
4958  * that parsing might create.
4959  */
4961  "inline_set_returning_function",
4963  oldcxt = MemoryContextSwitchTo(mycxt);
4964 
4965  /* Fetch the function body */
4966  tmp = SysCacheGetAttr(PROCOID,
4967  func_tuple,
4968  Anum_pg_proc_prosrc,
4969  &isNull);
4970  if (isNull)
4971  elog(ERROR, "null prosrc for function %u", func_oid);
4972  src = TextDatumGetCString(tmp);
4973 
4974  /*
4975  * Setup error traceback support for ereport(). This is so that we can
4976  * finger the function that bad information came from.
4977  */
4978  callback_arg.proname = NameStr(funcform->proname);
4979  callback_arg.prosrc = src;
4980 
4981  sqlerrcontext.callback = sql_inline_error_callback;
4982  sqlerrcontext.arg = (void *) &callback_arg;
4983  sqlerrcontext.previous = error_context_stack;
4984  error_context_stack = &sqlerrcontext;
4985 
4986  /*
4987  * Set up to handle parameters while parsing the function body. We can
4988  * use the FuncExpr just created as the input for
4989  * prepare_sql_fn_parse_info.
4990  */
4991  pinfo = prepare_sql_fn_parse_info(func_tuple,
4992  (Node *) fexpr,
4993  fexpr->inputcollid);
4994 
4995  /*
4996  * Also resolve the actual function result tupdesc, if composite. If the
4997  * function is just declared to return RECORD, dig the info out of the AS
4998  * clause.
4999  */
5000  functypclass = get_expr_result_type((Node *) fexpr, NULL, &rettupdesc);
5001  if (functypclass == TYPEFUNC_RECORD)
5002  rettupdesc = BuildDescFromLists(rtfunc->funccolnames,
5003  rtfunc->funccoltypes,
5004  rtfunc->funccoltypmods,
5005  rtfunc->funccolcollations);
5006 
5007  /*
5008  * Parse, analyze, and rewrite (unlike inline_function(), we can't skip
5009  * rewriting here). We can fail as soon as we find more than one query,
5010  * though.
5011  */
5012  raw_parsetree_list = pg_parse_query(src);
5013  if (list_length(raw_parsetree_list) != 1)
5014  goto fail;
5015 
5016  querytree_list = pg_analyze_and_rewrite_params(linitial(raw_parsetree_list),
5017  src,
5019  pinfo, NULL);
5020  if (list_length(querytree_list) != 1)
5021  goto fail;
5022  querytree = linitial(querytree_list);
5023 
5024  /*
5025  * The single command must be a plain SELECT.
5026  */
5027  if (!IsA(querytree, Query) ||
5028  querytree->commandType != CMD_SELECT)
5029  goto fail;
5030 
5031  /*
5032  * Make sure the function (still) returns what it's declared to. This
5033  * will raise an error if wrong, but that's okay since the function would
5034  * fail at runtime anyway. Note that check_sql_fn_retval will also insert
5035  * coercions if needed to make the tlist expression(s) match the declared
5036  * type of the function. We also ask it to insert dummy NULL columns for
5037  * any dropped columns in rettupdesc, so that the elements of the modified
5038  * tlist match up to the attribute numbers.
5039  *
5040  * If the function returns a composite type, don't inline unless the check
5041  * shows it's returning a whole tuple result; otherwise what it's
5042  * returning is a single composite column which is not what we need.
5043  */
5044  if (!check_sql_fn_retval(list_make1(querytree_list),
5045  fexpr->funcresulttype, rettupdesc,
5046  true, NULL) &&
5047  (functypclass == TYPEFUNC_COMPOSITE ||
5048  functypclass == TYPEFUNC_COMPOSITE_DOMAIN ||
5049  functypclass == TYPEFUNC_RECORD))
5050  goto fail; /* reject not-whole-tuple-result cases */
5051 
5052  /*
5053  * check_sql_fn_retval might've inserted a projection step, but that's
5054  * fine; just make sure we use the upper Query.
5055  */
5056  querytree = linitial_node(Query, querytree_list);
5057 
5058  /*
5059  * Looks good --- substitute parameters into the query.
5060  */
5061  querytree = substitute_actual_srf_parameters(querytree,
5062  funcform->pronargs,
5063  fexpr->args);
5064 
5065  /*
5066  * Copy the modified query out of the temporary memory context, and clean
5067  * up.
5068  */
5069  MemoryContextSwitchTo(oldcxt);
5070 
5071  querytree = copyObject(querytree);
5072 
5073  MemoryContextDelete(mycxt);
5074  error_context_stack = sqlerrcontext.previous;
5075  ReleaseSysCache(func_tuple);
5076 
5077  /*
5078  * We don't have to fix collations here because the upper query is already
5079  * parsed, ie, the collations in the RTE are what count.
5080  */
5081 
5082  /*
5083  * Since there is now no trace of the function in the plan tree, we must
5084  * explicitly record the plan's dependency on the function.
5085  */
5086  record_plan_function_dependency(root, func_oid);
5087 
5088  return querytree;
5089 
5090  /* Here if func is not inlinable: release temp memory and return NULL */
5091 fail:
5092  MemoryContextSwitchTo(oldcxt);
5093  MemoryContextDelete(mycxt);
5094  error_context_stack = sqlerrcontext.previous;
5095  ReleaseSysCache(func_tuple);
5096 
5097  return NULL;
5098 }
Oid funcresulttype
Definition: primnodes.h:470
bool check_sql_fn_retval(List *queryTreeLists, Oid rettype, TupleDesc rettupdesc, bool insertDroppedCols, List **resultTargetList)
Definition: functions.c:1601
#define IsA(nodeptr, _type_)
Definition: nodes.h:579
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
#define AllocSetContextCreate
Definition: memutils.h:170
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
Definition: tupdesc.c:892
List * args
Definition: primnodes.h:477
Oid GetUserId(void)
Definition: miscinit.c:476
void sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
Definition: functions.c:280
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:528
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
bool funcretset
Definition: primnodes.h:471
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
bool funcordinality
Definition: parsenodes.h:1069
unsigned int Oid
Definition: postgres_ext.h:31
#define linitial_node(type, l)
Definition: pg_list.h:177
void(* callback)(void *arg)
Definition: elog.h:229
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:221
struct ErrorContextCallback * previous
Definition: elog.h:228
#define FmgrHookIsNeeded(fn_oid)
Definition: fmgr.h:772
ErrorContextCallback * error_context_stack
Definition: elog.c:92
#define list_make1(x1)
Definition: pg_list.h:206
bool contain_subplans(Node *clause)
Definition: clauses.c:610
#define linitial(l)
Definition: pg_list.h:174
Oid funcid
Definition: primnodes.h:469
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
List * pg_parse_query(const char *query_string)
Definition: postgres.c:628
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
Definition: params.h:108
void check_stack_depth(void)
Definition: postgres.c:3312
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
TypeFuncClass
Definition: funcapi.h:146
static void sql_inline_error_callback(void *arg)
Definition: clauses.c:4733
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
#define TextDatumGetCString(d)
Definition: builtins.h:87
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1377
void record_plan_function_dependency(PlannerInfo *root, Oid funcid)
Definition: setrefs.c:2889
static Query * substitute_actual_srf_parameters(Query *expr, int nargs, List *args)
Definition: clauses.c:5107
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:133
List * funccoltypmods
Definition: parsenodes.h:1158
CmdType commandType
Definition: parsenodes.h:112
List * funccolcollations
Definition: parsenodes.h:1159
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:746
List * functions
Definition: parsenodes.h:1068
static int list_length(const List *l)
Definition: pg_list.h:149
List * pg_analyze_and_rewrite_params(RawStmt *parsetree, const char *query_string, ParserSetupHook parserSetup, void *parserSetupArg, QueryEnvironment *queryEnv)
Definition: postgres.c:713
Datum querytree(PG_FUNCTION_ARGS)
Definition: _int_bool.c:664
RTEKind rtekind
Definition: parsenodes.h:981
#define ACL_EXECUTE
Definition: parsenodes.h:81
#define elog(elevel,...)
Definition: elog.h:214
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4587
#define NameStr(name)
Definition: c.h:623
SQLFunctionParseInfoPtr prepare_sql_fn_parse_info(HeapTuple procedureTuple, Node *call_expr, Oid inputCollation)
Definition: functions.c:190
#define copyObject(obj)
Definition: nodes.h:644
Definition: pg_list.h:50

◆ is_parallel_safe()

bool is_parallel_safe ( PlannerInfo root,
Node node 
)

Definition at line 854 of file clauses.c.

References PlannerInfo::glob, PlannerInfo::init_plans, lfirst, list_concat(), max_parallel_hazard_context::max_hazard, max_parallel_hazard_context::max_interesting, max_parallel_hazard_walker(), PlannerGlobal::maxParallelHazard, NIL, PlannerGlobal::paramExecTypes, PlannerInfo::parent_root, max_parallel_hazard_context::safe_param_ids, and SubPlan::setParam.

Referenced by apply_projection_to_path(), build_join_rel(), create_projection_path(), create_set_projection_path(), create_window_paths(), grouping_planner(), make_grouping_rel(), plan_create_index_workers(), query_planner(), and set_rel_consider_parallel().

855 {
857  PlannerInfo *proot;
858  ListCell *l;
859 
860  /*
861  * Even if the original querytree contained nothing unsafe, we need to
862  * search the expression if we have generated any PARAM_EXEC Params while
863  * planning, because those are parallel-restricted and there might be one
864  * in this expression. But otherwise we don't need to look.
865  */
866  if (root->glob->maxParallelHazard == PROPARALLEL_SAFE &&
867  root->glob->paramExecTypes == NIL)
868  return true;
869  /* Else use max_parallel_hazard's search logic, but stop on RESTRICTED */
870  context.max_hazard = PROPARALLEL_SAFE;
871  context.max_interesting = PROPARALLEL_RESTRICTED;
872  context.safe_param_ids = NIL;
873 
874  /*
875  * The params that refer to the same or parent query level are considered
876  * parallel-safe. The idea is that we compute such params at Gather or
877  * Gather Merge node and pass their value to workers.
878  */
879  for (proot = root; proot != NULL; proot = proot->parent_root)
880  {
881  foreach(l, proot->init_plans)
882  {
883  SubPlan *initsubplan = (SubPlan *) lfirst(l);
884 
885  context.safe_param_ids = list_concat(context.safe_param_ids,
886  initsubplan->setParam);
887  }
888  }
889 
890  return !max_parallel_hazard_walker(node, &context);
891 }
char maxParallelHazard
Definition: pathnodes.h:145
#define NIL
Definition: pg_list.h:65
PlannerInfo * parent_root
Definition: pathnodes.h:183
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
List * paramExecTypes
Definition: pathnodes.h:129
static bool max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context)
Definition: clauses.c:930
PlannerGlobal * glob
Definition: pathnodes.h:179
List * init_plans
Definition: pathnodes.h:257
#define lfirst(lc)
Definition: pg_list.h:169
List * setParam
Definition: primnodes.h:727

◆ is_pseudo_constant_clause()

bool is_pseudo_constant_clause ( Node clause)

Definition at line 2123 of file clauses.c.

References contain_var_clause(), and contain_volatile_functions().

Referenced by clauselist_selectivity_simple(), and dependency_is_compatible_clause().

2124 {
2125  /*
2126  * We could implement this check in one recursive scan. But since the
2127  * check for volatile functions is both moderately expensive and unlikely
2128  * to fail, it seems better to look for Vars first and only check for
2129  * volatile functions if we find no Vars.
2130  */
2131  if (!contain_var_clause(clause) &&
2132  !contain_volatile_functions(clause))
2133  return true;
2134  return false;
2135 }
bool contain_var_clause(Node *node)
Definition: var.c:331
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724

◆ is_pseudo_constant_clause_relids()

bool is_pseudo_constant_clause_relids ( Node clause,
Relids  relids 
)

Definition at line 2143 of file clauses.c.

References bms_is_empty(), and contain_volatile_functions().

Referenced by clauselist_selectivity_simple().

2144 {
2145  if (bms_is_empty(relids) &&
2146  !contain_volatile_functions(clause))
2147  return true;
2148  return false;
2149 }
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:724
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701

◆ max_parallel_hazard()

char max_parallel_hazard ( Query parse)

Definition at line 835 of file clauses.c.

References max_parallel_hazard_context::max_hazard, max_parallel_hazard_context::max_interesting, max_parallel_hazard_walker(), NIL, parse(), and max_parallel_hazard_context::safe_param_ids.

Referenced by standard_planner().

836 {
838 
839  context.max_hazard = PROPARALLEL_SAFE;
840  context.max_interesting = PROPARALLEL_UNSAFE;
841  context.safe_param_ids = NIL;
842  (void) max_parallel_hazard_walker((Node *) parse, &context);
843  return context.max_hazard;
844 }
#define NIL
Definition: pg_list.h:65
Definition: nodes.h:528
static bool max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context)
Definition: clauses.c:930
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:648

◆ NumRelids()

int NumRelids ( Node clause)

Definition at line 2165 of file clauses.c.

References bms_free(), bms_num_members(), and pull_varnos().

Referenced by clauselist_selectivity_simple(), rowcomparesel(), and treat_as_join_clause().

2166 {
2167  Relids varnos = pull_varnos(clause);
2168  int result = bms_num_members(varnos);
2169 
2170  bms_free(varnos);
2171  return result;
2172 }
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
Relids pull_varnos(Node *node)
Definition: var.c:95
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208