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

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

References contain_agg_clause_walker().

Referenced by get_eclass_for_sort_expr(), and subquery_planner().

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

◆ contain_exec_param()

bool contain_exec_param ( Node clause,
List param_ids 
)

Definition at line 1023 of file clauses.c.

References contain_exec_param_walker().

Referenced by test_opexpr_is_hashable().

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

◆ contain_leaked_vars()

bool contain_leaked_vars ( Node clause)

Definition at line 1149 of file clauses.c.

References contain_leaked_vars_walker().

Referenced by make_restrictinfo_internal(), and qual_is_pushdown_safe().

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

◆ contain_nonstrict_functions()

bool contain_nonstrict_functions ( Node clause)

Definition at line 879 of file clauses.c.

References contain_nonstrict_functions_walker().

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

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

◆ contain_subplans()

bool contain_subplans ( Node clause)

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

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

◆ contain_window_function()

bool contain_window_function ( Node clause)

Definition at line 212 of file clauses.c.

References contain_windowfuncs().

Referenced by get_eclass_for_sort_expr(), and qual_is_pushdown_safe().

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

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

1820 {
1821  if (node == NULL)
1822  return NULL;
1823  if (IsA(node, NullTest))
1824  {
1825  /* check for var IS NULL */
1826  NullTest *expr = (NullTest *) node;
1827 
1828  if (expr->nulltesttype == IS_NULL && !expr->argisrow)
1829  {
1830  Var *var = (Var *) expr->arg;
1831 
1832  if (var && IsA(var, Var) &&
1833  var->varlevelsup == 0)
1834  return var;
1835  }
1836  }
1837  else if (IsA(node, BooleanTest))
1838  {
1839  /* var IS UNKNOWN is equivalent to var IS NULL */
1840  BooleanTest *expr = (BooleanTest *) node;
1841 
1842  if (expr->booltesttype == IS_UNKNOWN)
1843  {
1844  Var *var = (Var *) expr->arg;
1845 
1846  if (var && IsA(var, Var) &&
1847  var->varlevelsup == 0)
1848  return var;
1849  }
1850  }
1851  return NULL;
1852 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:589
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 1760 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().

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

◆ find_nonnullable_rels()

Relids find_nonnullable_rels ( Node clause)

Definition at line 1342 of file clauses.c.

References find_nonnullable_rels_walker().

Referenced by make_outerjoininfo(), and reduce_outer_joins_pass2().

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

◆ find_nonnullable_vars()

List* find_nonnullable_vars ( Node clause)

Definition at line 1567 of file clauses.c.

References find_nonnullable_vars_walker().

Referenced by reduce_outer_joins_pass2().

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

◆ find_window_functions()

WindowFuncLists* find_window_functions ( Node clause,
Index  maxWinRef 
)

Definition at line 225 of file clauses.c.

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

Referenced by grouping_planner().

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

References ACL_EXECUTE, ACLCHECK_OK, AcquireRewriteLocks(), 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().

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

◆ is_parallel_safe()

bool is_parallel_safe ( PlannerInfo root,
Node node 
)

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

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

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

◆ is_pseudo_constant_clause_relids()

bool is_pseudo_constant_clause_relids ( Node clause,
Relids  relids 
)

Definition at line 1950 of file clauses.c.

References bms_is_empty(), and contain_volatile_functions().

Referenced by clauselist_selectivity_ext().

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

◆ max_parallel_hazard()

char max_parallel_hazard ( Query parse)

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

621 {
623 
624  context.max_hazard = PROPARALLEL_SAFE;
625  context.max_interesting = PROPARALLEL_UNSAFE;
626  context.safe_param_ids = NIL;
627  (void) max_parallel_hazard_walker((Node *) parse, &context);
628  return context.max_hazard;
629 }
#define NIL
Definition: pg_list.h:65
Definition: nodes.h:538
static bool max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context)
Definition: clauses.c:715
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:673

◆ NumRelids()

int NumRelids ( PlannerInfo root,
Node clause 
)

Definition at line 1972 of file clauses.c.

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

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

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