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

Go to the source code of this file.

Data Structures

struct  WindowFuncLists
 

Functions

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

Function Documentation

◆ CommuteOpExpr()

void CommuteOpExpr ( OpExpr clause)

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

2146 {
2147  Oid opoid;
2148  Node *temp;
2149 
2150  /* Sanity checks: caller is at fault if these fail */
2151  if (!is_opclause(clause) ||
2152  list_length(clause->args) != 2)
2153  elog(ERROR, "cannot commute non-binary-operator clause");
2154 
2155  opoid = get_commutator(clause->opno);
2156 
2157  if (!OidIsValid(opoid))
2158  elog(ERROR, "could not find commutator for operator %u",
2159  clause->opno);
2160 
2161  /*
2162  * modify the clause in-place!
2163  */
2164  clause->opno = opoid;
2165  clause->opfuncid = InvalidOid;
2166  /* opresulttype, opretset, opcollid, inputcollid need not change */
2167 
2168  temp = linitial(clause->args);
2169  linitial(clause->args) = lsecond(clause->args);
2170  lsecond(clause->args) = temp;
2171 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1311
Definition: nodes.h:525
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:645
#define lsecond(l)
Definition: pg_list.h:200
#define linitial(l)
Definition: pg_list.h:195
#define ERROR
Definition: elog.h:43
Oid opfuncid
Definition: primnodes.h:517
#define InvalidOid
Definition: postgres_ext.h:36
static int list_length(const List *l)
Definition: pg_list.h:169
#define elog(elevel,...)
Definition: elog.h:228
Oid opno
Definition: primnodes.h:516
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:63
List * args
Definition: primnodes.h:522

◆ contain_agg_clause()

bool contain_agg_clause ( Node clause)

Definition at line 178 of file clauses.c.

References contain_agg_clause_walker().

Referenced by get_eclass_for_sort_expr(), and subquery_planner().

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

◆ contain_leaked_vars()

bool contain_leaked_vars ( Node clause)

Definition at line 1326 of file clauses.c.

References contain_leaked_vars_walker().

Referenced by make_restrictinfo_internal(), and qual_is_pushdown_safe().

1327 {
1328  return contain_leaked_vars_walker(clause, NULL);
1329 }
static bool contain_leaked_vars_walker(Node *node, void *context)
Definition: clauses.c:1338

◆ contain_nonstrict_functions()

bool contain_nonstrict_functions ( Node clause)

Definition at line 1093 of file clauses.c.

References contain_nonstrict_functions_walker().

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

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

◆ contain_subplans()

bool contain_subplans ( Node clause)

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

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

◆ contain_window_function()

bool contain_window_function ( Node clause)

Definition at line 493 of file clauses.c.

References contain_windowfuncs().

Referenced by get_eclass_for_sort_expr(), and qual_is_pushdown_safe().

494 {
495  return contain_windowfuncs(clause);
496 }
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 568 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().

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

◆ find_forced_null_var()

Var* find_forced_null_var ( Node clause)

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

1978 {
1979  if (node == NULL)
1980  return NULL;
1981  if (IsA(node, NullTest))
1982  {
1983  /* check for var IS NULL */
1984  NullTest *expr = (NullTest *) node;
1985 
1986  if (expr->nulltesttype == IS_NULL && !expr->argisrow)
1987  {
1988  Var *var = (Var *) expr->arg;
1989 
1990  if (var && IsA(var, Var) &&
1991  var->varlevelsup == 0)
1992  return var;
1993  }
1994  }
1995  else if (IsA(node, BooleanTest))
1996  {
1997  /* var IS UNKNOWN is equivalent to var IS NULL */
1998  BooleanTest *expr = (BooleanTest *) node;
1999 
2000  if (expr->booltesttype == IS_UNKNOWN)
2001  {
2002  Var *var = (Var *) expr->arg;
2003 
2004  if (var && IsA(var, Var) &&
2005  var->varlevelsup == 0)
2006  return var;
2007  }
2008  }
2009  return NULL;
2010 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Index varlevelsup
Definition: primnodes.h:191
Definition: primnodes.h:181
Expr * arg
Definition: primnodes.h:1219
Expr * arg
Definition: primnodes.h:1242
BoolTestType booltesttype
Definition: primnodes.h:1243
NullTestType nulltesttype
Definition: primnodes.h:1220
bool argisrow
Definition: primnodes.h:1221

◆ find_forced_null_vars()

List* find_forced_null_vars ( Node clause)

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

1919 {
1920  List *result = NIL;
1921  Var *var;
1922  ListCell *l;
1923 
1924  if (node == NULL)
1925  return NIL;
1926  /* Check single-clause cases using subroutine */
1927  var = find_forced_null_var(node);
1928  if (var)
1929  {
1930  result = list_make1(var);
1931  }
1932  /* Otherwise, handle AND-conditions */
1933  else if (IsA(node, List))
1934  {
1935  /*
1936  * At top level, we are examining an implicit-AND list: if any of the
1937  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
1938  */
1939  foreach(l, (List *) node)
1940  {
1941  result = list_concat(result,
1943  }
1944  }
1945  else if (IsA(node, BoolExpr))
1946  {
1947  BoolExpr *expr = (BoolExpr *) node;
1948 
1949  /*
1950  * We don't bother considering the OR case, because it's fairly
1951  * unlikely anyone would write "v1 IS NULL OR v1 IS NULL". Likewise,
1952  * the NOT case isn't worth expending code on.
1953  */
1954  if (expr->boolop == AND_EXPR)
1955  {
1956  /* At top level we can just recurse (to the List case) */
1957  result = find_forced_null_vars((Node *) expr->args);
1958  }
1959  }
1960  return result;
1961 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Definition: nodes.h:525
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
Definition: primnodes.h:181
List * find_forced_null_vars(Node *node)
Definition: clauses.c:1918
#define list_make1(x1)
Definition: pg_list.h:227
BoolExprType boolop
Definition: primnodes.h:582
#define lfirst(lc)
Definition: pg_list.h:190
List * args
Definition: primnodes.h:583
Definition: pg_list.h:50
Var * find_forced_null_var(Node *node)
Definition: clauses.c:1977

◆ find_nonnullable_rels()

Relids find_nonnullable_rels ( Node clause)

Definition at line 1500 of file clauses.c.

References find_nonnullable_rels_walker().

Referenced by make_outerjoininfo(), and reduce_outer_joins_pass2().

1501 {
1502  return find_nonnullable_rels_walker(clause, true);
1503 }
static Relids find_nonnullable_rels_walker(Node *node, bool top_level)
Definition: clauses.c:1506

◆ find_nonnullable_vars()

List* find_nonnullable_vars ( Node clause)

Definition at line 1725 of file clauses.c.

References find_nonnullable_vars_walker().

Referenced by reduce_outer_joins_pass2().

1726 {
1727  return find_nonnullable_vars_walker(clause, true);
1728 }
static List * find_nonnullable_vars_walker(Node *node, bool top_level)
Definition: clauses.c:1731

◆ find_window_functions()

WindowFuncLists* find_window_functions ( Node clause,
Index  maxWinRef 
)

Definition at line 506 of file clauses.c.

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

Referenced by grouping_planner().

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

◆ get_agg_clause_costs()

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

Definition at line 228 of file clauses.c.

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

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

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

◆ inline_set_returning_function()

Query* inline_set_returning_function ( PlannerInfo root,
RangeTblEntry rte 
)

Definition at line 4880 of file clauses.c.

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

Referenced by preprocess_function_rtes().

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

◆ is_parallel_safe()

bool is_parallel_safe ( PlannerInfo root,
Node node 
)

Definition at line 853 of file clauses.c.

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

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

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

◆ is_pseudo_constant_clause()

bool is_pseudo_constant_clause ( Node clause)

Definition at line 2088 of file clauses.c.

References contain_var_clause(), and contain_volatile_functions().

Referenced by clauselist_selectivity_simple(), and dependency_is_compatible_clause().

2089 {
2090  /*
2091  * We could implement this check in one recursive scan. But since the
2092  * check for volatile functions is both moderately expensive and unlikely
2093  * to fail, it seems better to look for Vars first and only check for
2094  * volatile functions if we find no Vars.
2095  */
2096  if (!contain_var_clause(clause) &&
2097  !contain_volatile_functions(clause))
2098  return true;
2099  return false;
2100 }
bool contain_var_clause(Node *node)
Definition: var.c:331
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:723

◆ is_pseudo_constant_clause_relids()

bool is_pseudo_constant_clause_relids ( Node clause,
Relids  relids 
)

Definition at line 2108 of file clauses.c.

References bms_is_empty(), and contain_volatile_functions().

Referenced by clauselist_selectivity_simple().

2109 {
2110  if (bms_is_empty(relids) &&
2111  !contain_volatile_functions(clause))
2112  return true;
2113  return false;
2114 }
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:723
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701

◆ max_parallel_hazard()

char max_parallel_hazard ( Query parse)

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

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

◆ NumRelids()

int NumRelids ( Node clause)

Definition at line 2130 of file clauses.c.

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

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

2131 {
2132  Relids varnos = pull_varnos(clause);
2133  int result = bms_num_members(varnos);
2134 
2135  bms_free(varnos);
2136  return result;
2137 }
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:646
Relids pull_varnos(Node *node)
Definition: var.c:95
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208