PostgreSQL Source Code  git master
clauses.h File Reference
#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)
 
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 (PlannerInfo *root, Node *clause)
 
void CommuteOpExpr (OpExpr *clause)
 
Queryinline_set_returning_function (PlannerInfo *root, RangeTblEntry *rte)
 

Function Documentation

◆ CommuteOpExpr()

void CommuteOpExpr ( OpExpr clause)

Definition at line 1983 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().

1984 {
1985  Oid opoid;
1986  Node *temp;
1987 
1988  /* Sanity checks: caller is at fault if these fail */
1989  if (!is_opclause(clause) ||
1990  list_length(clause->args) != 2)
1991  elog(ERROR, "cannot commute non-binary-operator clause");
1992 
1993  opoid = get_commutator(clause->opno);
1994 
1995  if (!OidIsValid(opoid))
1996  elog(ERROR, "could not find commutator for operator %u",
1997  clause->opno);
1998 
1999  /*
2000  * modify the clause in-place!
2001  */
2002  clause->opno = opoid;
2003  clause->opfuncid = InvalidOid;
2004  /* opresulttype, opretset, opcollid, inputcollid need not change */
2005 
2006  temp = linitial(clause->args);
2007  linitial(clause->args) = lsecond(clause->args);
2008  lsecond(clause->args) = temp;
2009 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1480
Definition: nodes.h:539
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define lsecond(l)
Definition: pg_list.h:179
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
Oid opfuncid
Definition: primnodes.h:543
#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:232
Oid opno
Definition: primnodes.h:542
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:66
List * args
Definition: primnodes.h:548

◆ contain_agg_clause()

bool contain_agg_clause ( Node clause)

Definition at line 171 of file clauses.c.

References contain_agg_clause_walker().

Referenced by get_eclass_for_sort_expr(), and subquery_planner().

172 {
173  return contain_agg_clause_walker(clause, NULL);
174 }
static bool contain_agg_clause_walker(Node *node, void *context)
Definition: clauses.c:177

◆ contain_exec_param()

bool contain_exec_param ( Node clause,
List param_ids 
)

Definition at line 1019 of file clauses.c.

References contain_exec_param_walker().

Referenced by test_opexpr_is_hashable().

1020 {
1021  return contain_exec_param_walker(clause, param_ids);
1022 }
static bool contain_exec_param_walker(Node *node, List *param_ids)
Definition: clauses.c:1025

◆ contain_leaked_vars()

bool contain_leaked_vars ( Node clause)

Definition at line 1145 of file clauses.c.

References contain_leaked_vars_walker().

Referenced by make_restrictinfo_internal(), and qual_is_pushdown_safe().

1146 {
1147  return contain_leaked_vars_walker(clause, NULL);
1148 }
static bool contain_leaked_vars_walker(Node *node, void *context)
Definition: clauses.c:1157

◆ contain_nonstrict_functions()

bool contain_nonstrict_functions ( Node clause)

Definition at line 875 of file clauses.c.

References contain_nonstrict_functions_walker().

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

876 {
877  return contain_nonstrict_functions_walker(clause, NULL);
878 }
static bool contain_nonstrict_functions_walker(Node *node, void *context)
Definition: clauses.c:887

◆ contain_subplans()

bool contain_subplans ( Node clause)

Definition at line 324 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().

325 {
326  return contain_subplans_walker(clause, NULL);
327 }
static bool contain_subplans_walker(Node *node, void *context)
Definition: clauses.c:330

◆ contain_window_function()

bool contain_window_function ( Node clause)

Definition at line 208 of file clauses.c.

References contain_windowfuncs().

Referenced by get_eclass_for_sort_expr(), and qual_is_pushdown_safe().

209 {
210  return contain_windowfuncs(clause);
211 }
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 283 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().

284 {
285  if (clause == NULL)
286  return 1.0;
287  if (IsA(clause, FuncExpr))
288  {
289  FuncExpr *expr = (FuncExpr *) clause;
290 
291  if (expr->funcretset)
292  return clamp_row_est(get_function_rows(root, expr->funcid, clause));
293  }
294  if (IsA(clause, OpExpr))
295  {
296  OpExpr *expr = (OpExpr *) clause;
297 
298  if (expr->opretset)
299  {
300  set_opfuncid(expr);
301  return clamp_row_est(get_function_rows(root, expr->opfuncid, clause));
302  }
303  }
304  return 1.0;
305 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
bool funcretset
Definition: primnodes.h:497
Oid funcid
Definition: primnodes.h:495
Oid opfuncid
Definition: primnodes.h:543
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1683
double clamp_row_est(double nrows)
Definition: costsize.c:199
bool opretset
Definition: primnodes.h:545
double get_function_rows(PlannerInfo *root, Oid funcid, Node *node)
Definition: plancat.c:2028

◆ find_forced_null_var()

Var* find_forced_null_var ( Node clause)

Definition at line 1815 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().

1816 {
1817  if (node == NULL)
1818  return NULL;
1819  if (IsA(node, NullTest))
1820  {
1821  /* check for var IS NULL */
1822  NullTest *expr = (NullTest *) node;
1823 
1824  if (expr->nulltesttype == IS_NULL && !expr->argisrow)
1825  {
1826  Var *var = (Var *) expr->arg;
1827 
1828  if (var && IsA(var, Var) &&
1829  var->varlevelsup == 0)
1830  return var;
1831  }
1832  }
1833  else if (IsA(node, BooleanTest))
1834  {
1835  /* var IS UNKNOWN is equivalent to var IS NULL */
1836  BooleanTest *expr = (BooleanTest *) node;
1837 
1838  if (expr->booltesttype == IS_UNKNOWN)
1839  {
1840  Var *var = (Var *) expr->arg;
1841 
1842  if (var && IsA(var, Var) &&
1843  var->varlevelsup == 0)
1844  return var;
1845  }
1846  }
1847  return NULL;
1848 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Index varlevelsup
Definition: primnodes.h:196
Definition: primnodes.h:186
Expr * arg
Definition: primnodes.h:1255
Expr * arg
Definition: primnodes.h:1278
BoolTestType booltesttype
Definition: primnodes.h:1279
NullTestType nulltesttype
Definition: primnodes.h:1256
bool argisrow
Definition: primnodes.h:1257

◆ find_forced_null_vars()

List* find_forced_null_vars ( Node clause)

Definition at line 1756 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().

1757 {
1758  List *result = NIL;
1759  Var *var;
1760  ListCell *l;
1761 
1762  if (node == NULL)
1763  return NIL;
1764  /* Check single-clause cases using subroutine */
1765  var = find_forced_null_var(node);
1766  if (var)
1767  {
1768  result = list_make1(var);
1769  }
1770  /* Otherwise, handle AND-conditions */
1771  else if (IsA(node, List))
1772  {
1773  /*
1774  * At top level, we are examining an implicit-AND list: if any of the
1775  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
1776  */
1777  foreach(l, (List *) node)
1778  {
1779  result = list_concat(result,
1781  }
1782  }
1783  else if (IsA(node, BoolExpr))
1784  {
1785  BoolExpr *expr = (BoolExpr *) node;
1786 
1787  /*
1788  * We don't bother considering the OR case, because it's fairly
1789  * unlikely anyone would write "v1 IS NULL OR v1 IS NULL". Likewise,
1790  * the NOT case isn't worth expending code on.
1791  */
1792  if (expr->boolop == AND_EXPR)
1793  {
1794  /* At top level we can just recurse (to the List case) */
1795  result = find_forced_null_vars((Node *) expr->args);
1796  }
1797  }
1798  return result;
1799 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Definition: nodes.h:539
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
Definition: primnodes.h:186
List * find_forced_null_vars(Node *node)
Definition: clauses.c:1756
#define list_make1(x1)
Definition: pg_list.h:206
BoolExprType boolop
Definition: primnodes.h:615
#define lfirst(lc)
Definition: pg_list.h:169
List * args
Definition: primnodes.h:616
Definition: pg_list.h:50
Var * find_forced_null_var(Node *node)
Definition: clauses.c:1815

◆ find_nonnullable_rels()

Relids find_nonnullable_rels ( Node clause)

Definition at line 1338 of file clauses.c.

References find_nonnullable_rels_walker().

Referenced by make_outerjoininfo(), and reduce_outer_joins_pass2().

1339 {
1340  return find_nonnullable_rels_walker(clause, true);
1341 }
static Relids find_nonnullable_rels_walker(Node *node, bool top_level)
Definition: clauses.c:1344

◆ find_nonnullable_vars()

List* find_nonnullable_vars ( Node clause)

Definition at line 1563 of file clauses.c.

References find_nonnullable_vars_walker().

Referenced by reduce_outer_joins_pass2().

1564 {
1565  return find_nonnullable_vars_walker(clause, true);
1566 }
static List * find_nonnullable_vars_walker(Node *node, bool top_level)
Definition: clauses.c:1569

◆ find_window_functions()

WindowFuncLists* find_window_functions ( Node clause,
Index  maxWinRef 
)

Definition at line 221 of file clauses.c.

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

Referenced by grouping_planner().

222 {
223  WindowFuncLists *lists = palloc(sizeof(WindowFuncLists));
224 
225  lists->numWindowFuncs = 0;
226  lists->maxWinRef = maxWinRef;
227  lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
228  (void) find_window_functions_walker(clause, lists);
229  return lists;
230 }
Index maxWinRef
Definition: clauses.h:22
int numWindowFuncs
Definition: clauses.h:21
void * palloc0(Size size)
Definition: mcxt.c:1093
void * palloc(Size size)
Definition: mcxt.c:1062
static bool find_window_functions_walker(Node *node, WindowFuncLists *lists)
Definition: clauses.c:233
Definition: pg_list.h:50
List ** windowFuncs
Definition: clauses.h:23

◆ inline_set_returning_function()

Query* inline_set_returning_function ( PlannerInfo root,
RangeTblEntry rte 
)

Definition at line 4772 of file clauses.c.

References ACL_EXECUTE, ACLCHECK_OK, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ErrorContextCallback::arg, FuncExpr::args, Assert, BuildDescFromLists(), ErrorContextCallback::callback, castNode, 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(), pg_rewrite_query(), 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(), stringToNode(), substitute_actual_srf_parameters(), SysCacheGetAttr(), TextDatumGetCString, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, and TYPEFUNC_RECORD.

Referenced by preprocess_function_rtes().

4773 {
4774  RangeTblFunction *rtfunc;
4775  FuncExpr *fexpr;
4776  Oid func_oid;
4777  HeapTuple func_tuple;
4778  Form_pg_proc funcform;
4779  char *src;
4780  Datum tmp;
4781  bool isNull;
4782  MemoryContext oldcxt;
4783  MemoryContext mycxt;
4784  inline_error_callback_arg callback_arg;
4785  ErrorContextCallback sqlerrcontext;
4787  TypeFuncClass functypclass;
4788  TupleDesc rettupdesc;
4789  List *raw_parsetree_list;
4790  List *querytree_list;
4791  Query *querytree;
4792 
4793  Assert(rte->rtekind == RTE_FUNCTION);
4794 
4795  /*
4796  * It doesn't make a lot of sense for a SQL SRF to refer to itself in its
4797  * own FROM clause, since that must cause infinite recursion at runtime.
4798  * It will cause this code to recurse too, so check for stack overflow.
4799  * (There's no need to do more.)
4800  */
4802 
4803  /* Fail if the RTE has ORDINALITY - we don't implement that here. */
4804  if (rte->funcordinality)
4805  return NULL;
4806 
4807  /* Fail if RTE isn't a single, simple FuncExpr */
4808  if (list_length(rte->functions) != 1)
4809  return NULL;
4810  rtfunc = (RangeTblFunction *) linitial(rte->functions);
4811 
4812  if (!IsA(rtfunc->funcexpr, FuncExpr))
4813  return NULL;
4814  fexpr = (FuncExpr *) rtfunc->funcexpr;
4815 
4816  func_oid = fexpr->funcid;
4817 
4818  /*
4819  * The function must be declared to return a set, else inlining would
4820  * change the results if the contained SELECT didn't return exactly one
4821  * row.
4822  */
4823  if (!fexpr->funcretset)
4824  return NULL;
4825 
4826  /*
4827  * Refuse to inline if the arguments contain any volatile functions or
4828  * sub-selects. Volatile functions are rejected because inlining may
4829  * result in the arguments being evaluated multiple times, risking a
4830  * change in behavior. Sub-selects are rejected partly for implementation
4831  * reasons (pushing them down another level might change their behavior)
4832  * and partly because they're likely to be expensive and so multiple
4833  * evaluation would be bad.
4834  */
4835  if (contain_volatile_functions((Node *) fexpr->args) ||
4836  contain_subplans((Node *) fexpr->args))
4837  return NULL;
4838 
4839  /* Check permission to call function (fail later, if not) */
4840  if (pg_proc_aclcheck(func_oid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
4841  return NULL;
4842 
4843  /* Check whether a plugin wants to hook function entry/exit */
4844  if (FmgrHookIsNeeded(func_oid))
4845  return NULL;
4846 
4847  /*
4848  * OK, let's take a look at the function's pg_proc entry.
4849  */
4850  func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_oid));
4851  if (!HeapTupleIsValid(func_tuple))
4852  elog(ERROR, "cache lookup failed for function %u", func_oid);
4853  funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4854 
4855  /*
4856  * Forget it if the function is not SQL-language or has other showstopper
4857  * properties. In particular it mustn't be declared STRICT, since we
4858  * couldn't enforce that. It also mustn't be VOLATILE, because that is
4859  * supposed to cause it to be executed with its own snapshot, rather than
4860  * sharing the snapshot of the calling query. We also disallow returning
4861  * SETOF VOID, because inlining would result in exposing the actual result
4862  * of the function's last SELECT, which should not happen in that case.
4863  * (Rechecking prokind, proretset, and pronargs is just paranoia.)
4864  */
4865  if (funcform->prolang != SQLlanguageId ||
4866  funcform->prokind != PROKIND_FUNCTION ||
4867  funcform->proisstrict ||
4868  funcform->provolatile == PROVOLATILE_VOLATILE ||
4869  funcform->prorettype == VOIDOID ||
4870  funcform->prosecdef ||
4871  !funcform->proretset ||
4872  list_length(fexpr->args) != funcform->pronargs ||
4873  !heap_attisnull(func_tuple, Anum_pg_proc_proconfig, NULL))
4874  {
4875  ReleaseSysCache(func_tuple);
4876  return NULL;
4877  }
4878 
4879  /*
4880  * Make a temporary memory context, so that we don't leak all the stuff
4881  * that parsing might create.
4882  */
4884  "inline_set_returning_function",
4886  oldcxt = MemoryContextSwitchTo(mycxt);
4887 
4888  /* Fetch the function body */
4889  tmp = SysCacheGetAttr(PROCOID,
4890  func_tuple,
4891  Anum_pg_proc_prosrc,
4892  &isNull);
4893  if (isNull)
4894  elog(ERROR, "null prosrc for function %u", func_oid);
4895  src = TextDatumGetCString(tmp);
4896 
4897  /*
4898  * Setup error traceback support for ereport(). This is so that we can
4899  * finger the function that bad information came from.
4900  */
4901  callback_arg.proname = NameStr(funcform->proname);
4902  callback_arg.prosrc = src;
4903 
4904  sqlerrcontext.callback = sql_inline_error_callback;
4905  sqlerrcontext.arg = (void *) &callback_arg;
4906  sqlerrcontext.previous = error_context_stack;
4907  error_context_stack = &sqlerrcontext;
4908 
4909  /* If we have prosqlbody, pay attention to that not prosrc */
4910  tmp = SysCacheGetAttr(PROCOID,
4911  func_tuple,
4912  Anum_pg_proc_prosqlbody,
4913  &isNull);
4914  if (!isNull)
4915  {
4916  Node *n;
4917 
4919  if (IsA(n, List))
4920  querytree_list = linitial_node(List, castNode(List, n));
4921  else
4922  querytree_list = list_make1(n);
4923  if (list_length(querytree_list) != 1)
4924  goto fail;
4925  querytree = linitial(querytree_list);
4926 
4927  querytree_list = pg_rewrite_query(querytree);
4928  if (list_length(querytree_list) != 1)
4929  goto fail;
4930  querytree = linitial(querytree_list);
4931  }
4932  else
4933  {
4934  /*
4935  * Set up to handle parameters while parsing the function body. We
4936  * can use the FuncExpr just created as the input for
4937  * prepare_sql_fn_parse_info.
4938  */
4939  pinfo = prepare_sql_fn_parse_info(func_tuple,
4940  (Node *) fexpr,
4941  fexpr->inputcollid);
4942 
4943  /*
4944  * Parse, analyze, and rewrite (unlike inline_function(), we can't
4945  * skip rewriting here). We can fail as soon as we find more than one
4946  * query, though.
4947  */
4948  raw_parsetree_list = pg_parse_query(src);
4949  if (list_length(raw_parsetree_list) != 1)
4950  goto fail;
4951 
4952  querytree_list = pg_analyze_and_rewrite_params(linitial(raw_parsetree_list),
4953  src,
4955  pinfo, NULL);
4956  if (list_length(querytree_list) != 1)
4957  goto fail;
4958  querytree = linitial(querytree_list);
4959  }
4960 
4961  /*
4962  * Also resolve the actual function result tupdesc, if composite. If the
4963  * function is just declared to return RECORD, dig the info out of the AS
4964  * clause.
4965  */
4966  functypclass = get_expr_result_type((Node *) fexpr, NULL, &rettupdesc);
4967  if (functypclass == TYPEFUNC_RECORD)
4968  rettupdesc = BuildDescFromLists(rtfunc->funccolnames,
4969  rtfunc->funccoltypes,
4970  rtfunc->funccoltypmods,
4971  rtfunc->funccolcollations);
4972 
4973  /*
4974  * The single command must be a plain SELECT.
4975  */
4976  if (!IsA(querytree, Query) ||
4977  querytree->commandType != CMD_SELECT)
4978  goto fail;
4979 
4980  /*
4981  * Make sure the function (still) returns what it's declared to. This
4982  * will raise an error if wrong, but that's okay since the function would
4983  * fail at runtime anyway. Note that check_sql_fn_retval will also insert
4984  * coercions if needed to make the tlist expression(s) match the declared
4985  * type of the function. We also ask it to insert dummy NULL columns for
4986  * any dropped columns in rettupdesc, so that the elements of the modified
4987  * tlist match up to the attribute numbers.
4988  *
4989  * If the function returns a composite type, don't inline unless the check
4990  * shows it's returning a whole tuple result; otherwise what it's
4991  * returning is a single composite column which is not what we need.
4992  */
4993  if (!check_sql_fn_retval(list_make1(querytree_list),
4994  fexpr->funcresulttype, rettupdesc,
4995  true, NULL) &&
4996  (functypclass == TYPEFUNC_COMPOSITE ||
4997  functypclass == TYPEFUNC_COMPOSITE_DOMAIN ||
4998  functypclass == TYPEFUNC_RECORD))
4999  goto fail; /* reject not-whole-tuple-result cases */
5000 
5001  /*
5002  * check_sql_fn_retval might've inserted a projection step, but that's
5003  * fine; just make sure we use the upper Query.
5004  */
5005  querytree = linitial_node(Query, querytree_list);
5006 
5007  /*
5008  * Looks good --- substitute parameters into the query.
5009  */
5010  querytree = substitute_actual_srf_parameters(querytree,
5011  funcform->pronargs,
5012  fexpr->args);
5013 
5014  /*
5015  * Copy the modified query out of the temporary memory context, and clean
5016  * up.
5017  */
5018  MemoryContextSwitchTo(oldcxt);
5019 
5020  querytree = copyObject(querytree);
5021 
5022  MemoryContextDelete(mycxt);
5023  error_context_stack = sqlerrcontext.previous;
5024  ReleaseSysCache(func_tuple);
5025 
5026  /*
5027  * We don't have to fix collations here because the upper query is already
5028  * parsed, ie, the collations in the RTE are what count.
5029  */
5030 
5031  /*
5032  * Since there is now no trace of the function in the plan tree, we must
5033  * explicitly record the plan's dependency on the function.
5034  */
5035  record_plan_function_dependency(root, func_oid);
5036 
5037  return querytree;
5038 
5039  /* Here if func is not inlinable: release temp memory and return NULL */
5040 fail:
5041  MemoryContextSwitchTo(oldcxt);
5042  MemoryContextDelete(mycxt);
5043  error_context_stack = sqlerrcontext.previous;
5044  ReleaseSysCache(func_tuple);
5045 
5046  return NULL;
5047 }
Oid funcresulttype
Definition: primnodes.h:496
bool check_sql_fn_retval(List *queryTreeLists, Oid rettype, TupleDesc rettupdesc, bool insertDroppedCols, List **resultTargetList)
Definition: functions.c:1618
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
Definition: tupdesc.c:874
List * args
Definition: primnodes.h:503
Oid GetUserId(void)
Definition: miscinit.c:478
#define castNode(_type_, nodeptr)
Definition: nodes.h:608
void sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
Definition: functions.c:266
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Definition: nodes.h:539
void * stringToNode(const char *str)
Definition: read.c:89
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:359
bool funcretset
Definition: primnodes.h:497
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:448
bool funcordinality
Definition: parsenodes.h:1090
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:247
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:223
struct ErrorContextCallback * previous
Definition: elog.h:246
#define FmgrHookIsNeeded(fn_oid)
Definition: fmgr.h:774
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define list_make1(x1)
Definition: pg_list.h:206
bool contain_subplans(Node *clause)
Definition: clauses.c:324
#define linitial(l)
Definition: pg_list.h:174
Oid funcid
Definition: primnodes.h:495
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
List * pg_parse_query(const char *query_string)
Definition: postgres.c:594
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
Definition: params.h:108
void check_stack_depth(void)
Definition: postgres.c:3469
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
TypeFuncClass
Definition: funcapi.h:146
static void sql_inline_error_callback(void *arg)
Definition: clauses.c:4656
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
#define TextDatumGetCString(d)
Definition: builtins.h:83
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
void record_plan_function_dependency(PlannerInfo *root, Oid funcid)
Definition: setrefs.c:2943
static Query * substitute_actual_srf_parameters(Query *expr, int nargs, List *args)
Definition: clauses.c:5056
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
List * funccoltypmods
Definition: parsenodes.h:1179
CmdType commandType
Definition: parsenodes.h:120
List * funccolcollations
Definition: parsenodes.h:1180
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
List * functions
Definition: parsenodes.h:1089
List * pg_rewrite_query(Query *query)
Definition: postgres.c:737
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:679
Datum querytree(PG_FUNCTION_ARGS)
Definition: _int_bool.c:664
RTEKind rtekind
Definition: parsenodes.h:995
#define ACL_EXECUTE
Definition: parsenodes.h:89
#define elog(elevel,...)
Definition: elog.h:232
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4723
#define NameStr(name)
Definition: c.h:681
SQLFunctionParseInfoPtr prepare_sql_fn_parse_info(HeapTuple procedureTuple, Node *call_expr, Oid inputCollation)
Definition: functions.c:176
#define copyObject(obj)
Definition: nodes.h:655
Definition: pg_list.h:50

◆ is_parallel_safe()

bool is_parallel_safe ( PlannerInfo root,
Node node 
)

Definition at line 635 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(), find_computable_ec_member(), grouping_planner(), make_grouping_rel(), plan_create_index_workers(), query_planner(), relation_can_be_sorted_early(), and set_rel_consider_parallel().

636 {
638  PlannerInfo *proot;
639  ListCell *l;
640 
641  /*
642  * Even if the original querytree contained nothing unsafe, we need to
643  * search the expression if we have generated any PARAM_EXEC Params while
644  * planning, because those are parallel-restricted and there might be one
645  * in this expression. But otherwise we don't need to look.
646  */
647  if (root->glob->maxParallelHazard == PROPARALLEL_SAFE &&
648  root->glob->paramExecTypes == NIL)
649  return true;
650  /* Else use max_parallel_hazard's search logic, but stop on RESTRICTED */
651  context.max_hazard = PROPARALLEL_SAFE;
652  context.max_interesting = PROPARALLEL_RESTRICTED;
653  context.safe_param_ids = NIL;
654 
655  /*
656  * The params that refer to the same or parent query level are considered
657  * parallel-safe. The idea is that we compute such params at Gather or
658  * Gather Merge node and pass their value to workers.
659  */
660  for (proot = root; proot != NULL; proot = proot->parent_root)
661  {
662  foreach(l, proot->init_plans)
663  {
664  SubPlan *initsubplan = (SubPlan *) lfirst(l);
665 
666  context.safe_param_ids = list_concat(context.safe_param_ids,
667  initsubplan->setParam);
668  }
669  }
670 
671  return !max_parallel_hazard_walker(node, &context);
672 }
char maxParallelHazard
Definition: pathnodes.h:129
#define NIL
Definition: pg_list.h:65
PlannerInfo * parent_root
Definition: pathnodes.h:167
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
List * paramExecTypes
Definition: pathnodes.h:113
static bool max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context)
Definition: clauses.c:711
PlannerGlobal * glob
Definition: pathnodes.h:163
List * init_plans
Definition: pathnodes.h:241
#define lfirst(lc)
Definition: pg_list.h:169
List * setParam
Definition: primnodes.h:760

◆ is_pseudo_constant_clause()

bool is_pseudo_constant_clause ( Node clause)

Definition at line 1926 of file clauses.c.

References contain_var_clause(), and contain_volatile_functions().

Referenced by clauselist_selectivity_ext(), dependency_is_compatible_clause(), and dependency_is_compatible_expression().

1927 {
1928  /*
1929  * We could implement this check in one recursive scan. But since the
1930  * check for volatile functions is both moderately expensive and unlikely
1931  * to fail, it seems better to look for Vars first and only check for
1932  * volatile functions if we find no Vars.
1933  */
1934  if (!contain_var_clause(clause) &&
1935  !contain_volatile_functions(clause))
1936  return true;
1937  return false;
1938 }
bool contain_var_clause(Node *node)
Definition: var.c:358
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:448

◆ is_pseudo_constant_clause_relids()

bool is_pseudo_constant_clause_relids ( Node clause,
Relids  relids 
)

Definition at line 1946 of file clauses.c.

References bms_is_empty(), and contain_volatile_functions().

Referenced by clauselist_selectivity_ext().

1947 {
1948  if (bms_is_empty(relids) &&
1949  !contain_volatile_functions(clause))
1950  return true;
1951  return false;
1952 }
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:448
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701

◆ max_parallel_hazard()

char max_parallel_hazard ( Query parse)

Definition at line 616 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().

617 {
619 
620  context.max_hazard = PROPARALLEL_SAFE;
621  context.max_interesting = PROPARALLEL_UNSAFE;
622  context.safe_param_ids = NIL;
623  (void) max_parallel_hazard_walker((Node *) parse, &context);
624  return context.max_hazard;
625 }
#define NIL
Definition: pg_list.h:65
Definition: nodes.h:539
static bool max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context)
Definition: clauses.c:711
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:665

◆ NumRelids()

int NumRelids ( PlannerInfo root,
Node clause 
)

Definition at line 1968 of file clauses.c.

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

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

1969 {
1970  Relids varnos = pull_varnos(root, clause);
1971  int result = bms_num_members(varnos);
1972 
1973  bms_free(varnos);
1974  return result;
1975 }
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:97
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208