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 1986 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().

1987 {
1988  Oid opoid;
1989  Node *temp;
1990 
1991  /* Sanity checks: caller is at fault if these fail */
1992  if (!is_opclause(clause) ||
1993  list_length(clause->args) != 2)
1994  elog(ERROR, "cannot commute non-binary-operator clause");
1995 
1996  opoid = get_commutator(clause->opno);
1997 
1998  if (!OidIsValid(opoid))
1999  elog(ERROR, "could not find commutator for operator %u",
2000  clause->opno);
2001 
2002  /*
2003  * modify the clause in-place!
2004  */
2005  clause->opno = opoid;
2006  clause->opfuncid = InvalidOid;
2007  /* opresulttype, opretset, opcollid, inputcollid need not change */
2008 
2009  temp = linitial(clause->args);
2010  linitial(clause->args) = lsecond(clause->args);
2011  lsecond(clause->args) = temp;
2012 }
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 174 of file clauses.c.

References contain_agg_clause_walker().

Referenced by get_eclass_for_sort_expr(), and subquery_planner().

175 {
176  return contain_agg_clause_walker(clause, NULL);
177 }
static bool contain_agg_clause_walker(Node *node, void *context)
Definition: clauses.c:180

◆ contain_exec_param()

bool contain_exec_param ( Node clause,
List param_ids 
)

Definition at line 1022 of file clauses.c.

References contain_exec_param_walker().

Referenced by test_opexpr_is_hashable().

1023 {
1024  return contain_exec_param_walker(clause, param_ids);
1025 }
static bool contain_exec_param_walker(Node *node, List *param_ids)
Definition: clauses.c:1028

◆ contain_leaked_vars()

bool contain_leaked_vars ( Node clause)

Definition at line 1148 of file clauses.c.

References contain_leaked_vars_walker().

Referenced by make_restrictinfo_internal(), and qual_is_pushdown_safe().

1149 {
1150  return contain_leaked_vars_walker(clause, NULL);
1151 }
static bool contain_leaked_vars_walker(Node *node, void *context)
Definition: clauses.c:1160

◆ contain_nonstrict_functions()

bool contain_nonstrict_functions ( Node clause)

Definition at line 878 of file clauses.c.

References contain_nonstrict_functions_walker().

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

879 {
880  return contain_nonstrict_functions_walker(clause, NULL);
881 }
static bool contain_nonstrict_functions_walker(Node *node, void *context)
Definition: clauses.c:890

◆ contain_subplans()

bool contain_subplans ( Node clause)

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

328 {
329  return contain_subplans_walker(clause, NULL);
330 }
static bool contain_subplans_walker(Node *node, void *context)
Definition: clauses.c:333

◆ contain_window_function()

bool contain_window_function ( Node clause)

Definition at line 211 of file clauses.c.

References contain_windowfuncs().

Referenced by get_eclass_for_sort_expr(), and qual_is_pushdown_safe().

212 {
213  return contain_windowfuncs(clause);
214 }
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 286 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().

287 {
288  if (clause == NULL)
289  return 1.0;
290  if (IsA(clause, FuncExpr))
291  {
292  FuncExpr *expr = (FuncExpr *) clause;
293 
294  if (expr->funcretset)
295  return clamp_row_est(get_function_rows(root, expr->funcid, clause));
296  }
297  if (IsA(clause, OpExpr))
298  {
299  OpExpr *expr = (OpExpr *) clause;
300 
301  if (expr->opretset)
302  {
303  set_opfuncid(expr);
304  return clamp_row_est(get_function_rows(root, expr->opfuncid, clause));
305  }
306  }
307  return 1.0;
308 }
#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 1818 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().

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

◆ find_forced_null_vars()

List* find_forced_null_vars ( Node clause)

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

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

◆ find_nonnullable_rels()

Relids find_nonnullable_rels ( Node clause)

Definition at line 1341 of file clauses.c.

References find_nonnullable_rels_walker().

Referenced by make_outerjoininfo(), and reduce_outer_joins_pass2().

1342 {
1343  return find_nonnullable_rels_walker(clause, true);
1344 }
static Relids find_nonnullable_rels_walker(Node *node, bool top_level)
Definition: clauses.c:1347

◆ find_nonnullable_vars()

List* find_nonnullable_vars ( Node clause)

Definition at line 1566 of file clauses.c.

References find_nonnullable_vars_walker().

Referenced by reduce_outer_joins_pass2().

1567 {
1568  return find_nonnullable_vars_walker(clause, true);
1569 }
static List * find_nonnullable_vars_walker(Node *node, bool top_level)
Definition: clauses.c:1572

◆ find_window_functions()

WindowFuncLists* find_window_functions ( Node clause,
Index  maxWinRef 
)

Definition at line 224 of file clauses.c.

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

Referenced by grouping_planner().

225 {
226  WindowFuncLists *lists = palloc(sizeof(WindowFuncLists));
227 
228  lists->numWindowFuncs = 0;
229  lists->maxWinRef = maxWinRef;
230  lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
231  (void) find_window_functions_walker(clause, lists);
232  return lists;
233 }
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:236
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 4870 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().

4871 {
4872  RangeTblFunction *rtfunc;
4873  FuncExpr *fexpr;
4874  Oid func_oid;
4875  HeapTuple func_tuple;
4876  Form_pg_proc funcform;
4877  char *src;
4878  Datum tmp;
4879  bool isNull;
4880  MemoryContext oldcxt;
4881  MemoryContext mycxt;
4882  inline_error_callback_arg callback_arg;
4883  ErrorContextCallback sqlerrcontext;
4885  TypeFuncClass functypclass;
4886  TupleDesc rettupdesc;
4887  List *raw_parsetree_list;
4888  List *querytree_list;
4889  Query *querytree;
4890 
4891  Assert(rte->rtekind == RTE_FUNCTION);
4892 
4893  /*
4894  * It doesn't make a lot of sense for a SQL SRF to refer to itself in its
4895  * own FROM clause, since that must cause infinite recursion at runtime.
4896  * It will cause this code to recurse too, so check for stack overflow.
4897  * (There's no need to do more.)
4898  */
4900 
4901  /* Fail if the RTE has ORDINALITY - we don't implement that here. */
4902  if (rte->funcordinality)
4903  return NULL;
4904 
4905  /* Fail if RTE isn't a single, simple FuncExpr */
4906  if (list_length(rte->functions) != 1)
4907  return NULL;
4908  rtfunc = (RangeTblFunction *) linitial(rte->functions);
4909 
4910  if (!IsA(rtfunc->funcexpr, FuncExpr))
4911  return NULL;
4912  fexpr = (FuncExpr *) rtfunc->funcexpr;
4913 
4914  func_oid = fexpr->funcid;
4915 
4916  /*
4917  * The function must be declared to return a set, else inlining would
4918  * change the results if the contained SELECT didn't return exactly one
4919  * row.
4920  */
4921  if (!fexpr->funcretset)
4922  return NULL;
4923 
4924  /*
4925  * Refuse to inline if the arguments contain any volatile functions or
4926  * sub-selects. Volatile functions are rejected because inlining may
4927  * result in the arguments being evaluated multiple times, risking a
4928  * change in behavior. Sub-selects are rejected partly for implementation
4929  * reasons (pushing them down another level might change their behavior)
4930  * and partly because they're likely to be expensive and so multiple
4931  * evaluation would be bad.
4932  */
4933  if (contain_volatile_functions((Node *) fexpr->args) ||
4934  contain_subplans((Node *) fexpr->args))
4935  return NULL;
4936 
4937  /* Check permission to call function (fail later, if not) */
4938  if (pg_proc_aclcheck(func_oid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
4939  return NULL;
4940 
4941  /* Check whether a plugin wants to hook function entry/exit */
4942  if (FmgrHookIsNeeded(func_oid))
4943  return NULL;
4944 
4945  /*
4946  * OK, let's take a look at the function's pg_proc entry.
4947  */
4948  func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_oid));
4949  if (!HeapTupleIsValid(func_tuple))
4950  elog(ERROR, "cache lookup failed for function %u", func_oid);
4951  funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4952 
4953  /*
4954  * Forget it if the function is not SQL-language or has other showstopper
4955  * properties. In particular it mustn't be declared STRICT, since we
4956  * couldn't enforce that. It also mustn't be VOLATILE, because that is
4957  * supposed to cause it to be executed with its own snapshot, rather than
4958  * sharing the snapshot of the calling query. We also disallow returning
4959  * SETOF VOID, because inlining would result in exposing the actual result
4960  * of the function's last SELECT, which should not happen in that case.
4961  * (Rechecking prokind, proretset, and pronargs is just paranoia.)
4962  */
4963  if (funcform->prolang != SQLlanguageId ||
4964  funcform->prokind != PROKIND_FUNCTION ||
4965  funcform->proisstrict ||
4966  funcform->provolatile == PROVOLATILE_VOLATILE ||
4967  funcform->prorettype == VOIDOID ||
4968  funcform->prosecdef ||
4969  !funcform->proretset ||
4970  list_length(fexpr->args) != funcform->pronargs ||
4971  !heap_attisnull(func_tuple, Anum_pg_proc_proconfig, NULL))
4972  {
4973  ReleaseSysCache(func_tuple);
4974  return NULL;
4975  }
4976 
4977  /*
4978  * Make a temporary memory context, so that we don't leak all the stuff
4979  * that parsing might create.
4980  */
4982  "inline_set_returning_function",
4984  oldcxt = MemoryContextSwitchTo(mycxt);
4985 
4986  /* Fetch the function body */
4987  tmp = SysCacheGetAttr(PROCOID,
4988  func_tuple,
4989  Anum_pg_proc_prosrc,
4990  &isNull);
4991  if (isNull)
4992  elog(ERROR, "null prosrc for function %u", func_oid);
4993  src = TextDatumGetCString(tmp);
4994 
4995  /*
4996  * Setup error traceback support for ereport(). This is so that we can
4997  * finger the function that bad information came from.
4998  */
4999  callback_arg.proname = NameStr(funcform->proname);
5000  callback_arg.prosrc = src;
5001 
5002  sqlerrcontext.callback = sql_inline_error_callback;
5003  sqlerrcontext.arg = (void *) &callback_arg;
5004  sqlerrcontext.previous = error_context_stack;
5005  error_context_stack = &sqlerrcontext;
5006 
5007  /* If we have prosqlbody, pay attention to that not prosrc */
5008  tmp = SysCacheGetAttr(PROCOID,
5009  func_tuple,
5010  Anum_pg_proc_prosqlbody,
5011  &isNull);
5012  if (!isNull)
5013  {
5014  Node *n;
5015 
5017  if (IsA(n, List))
5018  querytree_list = linitial_node(List, castNode(List, n));
5019  else
5020  querytree_list = list_make1(n);
5021  if (list_length(querytree_list) != 1)
5022  goto fail;
5023  querytree = linitial(querytree_list);
5024 
5025  querytree_list = pg_rewrite_query(querytree);
5026  if (list_length(querytree_list) != 1)
5027  goto fail;
5028  querytree = linitial(querytree_list);
5029  }
5030  else
5031  {
5032  /*
5033  * Set up to handle parameters while parsing the function body. We
5034  * can use the FuncExpr just created as the input for
5035  * prepare_sql_fn_parse_info.
5036  */
5037  pinfo = prepare_sql_fn_parse_info(func_tuple,
5038  (Node *) fexpr,
5039  fexpr->inputcollid);
5040 
5041  /*
5042  * Parse, analyze, and rewrite (unlike inline_function(), we can't
5043  * skip rewriting here). We can fail as soon as we find more than one
5044  * query, though.
5045  */
5046  raw_parsetree_list = pg_parse_query(src);
5047  if (list_length(raw_parsetree_list) != 1)
5048  goto fail;
5049 
5050  querytree_list = pg_analyze_and_rewrite_params(linitial(raw_parsetree_list),
5051  src,
5053  pinfo, NULL);
5054  if (list_length(querytree_list) != 1)
5055  goto fail;
5056  querytree = linitial(querytree_list);
5057  }
5058 
5059  /*
5060  * Also resolve the actual function result tupdesc, if composite. If the
5061  * function is just declared to return RECORD, dig the info out of the AS
5062  * clause.
5063  */
5064  functypclass = get_expr_result_type((Node *) fexpr, NULL, &rettupdesc);
5065  if (functypclass == TYPEFUNC_RECORD)
5066  rettupdesc = BuildDescFromLists(rtfunc->funccolnames,
5067  rtfunc->funccoltypes,
5068  rtfunc->funccoltypmods,
5069  rtfunc->funccolcollations);
5070 
5071  /*
5072  * The single command must be a plain SELECT.
5073  */
5074  if (!IsA(querytree, Query) ||
5075  querytree->commandType != CMD_SELECT)
5076  goto fail;
5077 
5078  /*
5079  * Make sure the function (still) returns what it's declared to. This
5080  * will raise an error if wrong, but that's okay since the function would
5081  * fail at runtime anyway. Note that check_sql_fn_retval will also insert
5082  * coercions if needed to make the tlist expression(s) match the declared
5083  * type of the function. We also ask it to insert dummy NULL columns for
5084  * any dropped columns in rettupdesc, so that the elements of the modified
5085  * tlist match up to the attribute numbers.
5086  *
5087  * If the function returns a composite type, don't inline unless the check
5088  * shows it's returning a whole tuple result; otherwise what it's
5089  * returning is a single composite column which is not what we need.
5090  */
5091  if (!check_sql_fn_retval(list_make1(querytree_list),
5092  fexpr->funcresulttype, rettupdesc,
5093  true, NULL) &&
5094  (functypclass == TYPEFUNC_COMPOSITE ||
5095  functypclass == TYPEFUNC_COMPOSITE_DOMAIN ||
5096  functypclass == TYPEFUNC_RECORD))
5097  goto fail; /* reject not-whole-tuple-result cases */
5098 
5099  /*
5100  * check_sql_fn_retval might've inserted a projection step, but that's
5101  * fine; just make sure we use the upper Query.
5102  */
5103  querytree = linitial_node(Query, querytree_list);
5104 
5105  /*
5106  * Looks good --- substitute parameters into the query.
5107  */
5108  querytree = substitute_actual_srf_parameters(querytree,
5109  funcform->pronargs,
5110  fexpr->args);
5111 
5112  /*
5113  * Copy the modified query out of the temporary memory context, and clean
5114  * up.
5115  */
5116  MemoryContextSwitchTo(oldcxt);
5117 
5118  querytree = copyObject(querytree);
5119 
5120  MemoryContextDelete(mycxt);
5121  error_context_stack = sqlerrcontext.previous;
5122  ReleaseSysCache(func_tuple);
5123 
5124  /*
5125  * We don't have to fix collations here because the upper query is already
5126  * parsed, ie, the collations in the RTE are what count.
5127  */
5128 
5129  /*
5130  * Since there is now no trace of the function in the plan tree, we must
5131  * explicitly record the plan's dependency on the function.
5132  */
5133  record_plan_function_dependency(root, func_oid);
5134 
5135  return querytree;
5136 
5137  /* Here if func is not inlinable: release temp memory and return NULL */
5138 fail:
5139  MemoryContextSwitchTo(oldcxt);
5140  MemoryContextDelete(mycxt);
5141  error_context_stack = sqlerrcontext.previous;
5142  ReleaseSysCache(func_tuple);
5143 
5144  return NULL;
5145 }
Oid funcresulttype
Definition: primnodes.h:496
bool check_sql_fn_retval(List *queryTreeLists, Oid rettype, TupleDesc rettupdesc, bool insertDroppedCols, List **resultTargetList)
Definition: functions.c:1599
#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:877
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:265
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:451
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:327
#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:4754
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:2952
static Query * substitute_actual_srf_parameters(Query *expr, int nargs, List *args)
Definition: clauses.c:5154
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:4718
#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 638 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().

639 {
641  PlannerInfo *proot;
642  ListCell *l;
643 
644  /*
645  * Even if the original querytree contained nothing unsafe, we need to
646  * search the expression if we have generated any PARAM_EXEC Params while
647  * planning, because those are parallel-restricted and there might be one
648  * in this expression. But otherwise we don't need to look.
649  */
650  if (root->glob->maxParallelHazard == PROPARALLEL_SAFE &&
651  root->glob->paramExecTypes == NIL)
652  return true;
653  /* Else use max_parallel_hazard's search logic, but stop on RESTRICTED */
654  context.max_hazard = PROPARALLEL_SAFE;
655  context.max_interesting = PROPARALLEL_RESTRICTED;
656  context.safe_param_ids = NIL;
657 
658  /*
659  * The params that refer to the same or parent query level are considered
660  * parallel-safe. The idea is that we compute such params at Gather or
661  * Gather Merge node and pass their value to workers.
662  */
663  for (proot = root; proot != NULL; proot = proot->parent_root)
664  {
665  foreach(l, proot->init_plans)
666  {
667  SubPlan *initsubplan = (SubPlan *) lfirst(l);
668 
669  context.safe_param_ids = list_concat(context.safe_param_ids,
670  initsubplan->setParam);
671  }
672  }
673 
674  return !max_parallel_hazard_walker(node, &context);
675 }
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:714
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:770

◆ is_pseudo_constant_clause()

bool is_pseudo_constant_clause ( Node clause)

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

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

◆ is_pseudo_constant_clause_relids()

bool is_pseudo_constant_clause_relids ( Node clause,
Relids  relids 
)

Definition at line 1949 of file clauses.c.

References bms_is_empty(), and contain_volatile_functions().

Referenced by clauselist_selectivity_ext().

1950 {
1951  if (bms_is_empty(relids) &&
1952  !contain_volatile_functions(clause))
1953  return true;
1954  return false;
1955 }
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:451
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701

◆ max_parallel_hazard()

char max_parallel_hazard ( Query parse)

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

620 {
622 
623  context.max_hazard = PROPARALLEL_SAFE;
624  context.max_interesting = PROPARALLEL_UNSAFE;
625  context.safe_param_ids = NIL;
626  (void) max_parallel_hazard_walker((Node *) parse, &context);
627  return context.max_hazard;
628 }
#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:714
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 1971 of file clauses.c.

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

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

1972 {
1973  Relids varnos = pull_varnos(root, clause);
1974  int result = bms_num_members(varnos);
1975 
1976  bms_free(varnos);
1977  return result;
1978 }
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