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

2149 {
2150  Oid opoid;
2151  Node *temp;
2152 
2153  /* Sanity checks: caller is at fault if these fail */
2154  if (!is_opclause(clause) ||
2155  list_length(clause->args) != 2)
2156  elog(ERROR, "cannot commute non-binary-operator clause");
2157 
2158  opoid = get_commutator(clause->opno);
2159 
2160  if (!OidIsValid(opoid))
2161  elog(ERROR, "could not find commutator for operator %u",
2162  clause->opno);
2163 
2164  /*
2165  * modify the clause in-place!
2166  */
2167  clause->opno = opoid;
2168  clause->opfuncid = InvalidOid;
2169  /* opresulttype, opretset, opcollid, inputcollid need not change */
2170 
2171  temp = linitial(clause->args);
2172  linitial(clause->args) = lsecond(clause->args);
2173  lsecond(clause->args) = temp;
2174 }
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1421
Definition: nodes.h:529
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:644
#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:214
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 181 of file clauses.c.

References contain_agg_clause_walker().

Referenced by get_eclass_for_sort_expr(), and subquery_planner().

182 {
183  return contain_agg_clause_walker(clause, NULL);
184 }
static bool contain_agg_clause_walker(Node *node, void *context)
Definition: clauses.c:187

◆ contain_leaked_vars()

bool contain_leaked_vars ( Node clause)

Definition at line 1329 of file clauses.c.

References contain_leaked_vars_walker().

Referenced by make_restrictinfo_internal(), and qual_is_pushdown_safe().

1330 {
1331  return contain_leaked_vars_walker(clause, NULL);
1332 }
static bool contain_leaked_vars_walker(Node *node, void *context)
Definition: clauses.c:1341

◆ contain_nonstrict_functions()

bool contain_nonstrict_functions ( Node clause)

Definition at line 1096 of file clauses.c.

References contain_nonstrict_functions_walker().

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

1097 {
1098  return contain_nonstrict_functions_walker(clause, NULL);
1099 }
static bool contain_nonstrict_functions_walker(Node *node, void *context)
Definition: clauses.c:1108

◆ contain_subplans()

bool contain_subplans ( Node clause)

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

613 {
614  return contain_subplans_walker(clause, NULL);
615 }
static bool contain_subplans_walker(Node *node, void *context)
Definition: clauses.c:618

◆ contain_window_function()

bool contain_window_function ( Node clause)

Definition at line 496 of file clauses.c.

References contain_windowfuncs().

Referenced by get_eclass_for_sort_expr(), and qual_is_pushdown_safe().

497 {
498  return contain_windowfuncs(clause);
499 }
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 571 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().

572 {
573  if (clause == NULL)
574  return 1.0;
575  if (IsA(clause, FuncExpr))
576  {
577  FuncExpr *expr = (FuncExpr *) clause;
578 
579  if (expr->funcretset)
580  return clamp_row_est(get_function_rows(root, expr->funcid, clause));
581  }
582  if (IsA(clause, OpExpr))
583  {
584  OpExpr *expr = (OpExpr *) clause;
585 
586  if (expr->opretset)
587  {
588  set_opfuncid(expr);
589  return clamp_row_est(get_function_rows(root, expr->opfuncid, clause));
590  }
591  }
592  return 1.0;
593 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
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:191
bool opretset
Definition: primnodes.h:519
double get_function_rows(PlannerInfo *root, Oid funcid, Node *node)
Definition: plancat.c:1969

◆ find_forced_null_var()

Var* find_forced_null_var ( Node clause)

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

1981 {
1982  if (node == NULL)
1983  return NULL;
1984  if (IsA(node, NullTest))
1985  {
1986  /* check for var IS NULL */
1987  NullTest *expr = (NullTest *) node;
1988 
1989  if (expr->nulltesttype == IS_NULL && !expr->argisrow)
1990  {
1991  Var *var = (Var *) expr->arg;
1992 
1993  if (var && IsA(var, Var) &&
1994  var->varlevelsup == 0)
1995  return var;
1996  }
1997  }
1998  else if (IsA(node, BooleanTest))
1999  {
2000  /* var IS UNKNOWN is equivalent to var IS NULL */
2001  BooleanTest *expr = (BooleanTest *) node;
2002 
2003  if (expr->booltesttype == IS_UNKNOWN)
2004  {
2005  Var *var = (Var *) expr->arg;
2006 
2007  if (var && IsA(var, Var) &&
2008  var->varlevelsup == 0)
2009  return var;
2010  }
2011  }
2012  return NULL;
2013 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:580
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 1921 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().

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

◆ find_nonnullable_rels()

Relids find_nonnullable_rels ( Node clause)

Definition at line 1503 of file clauses.c.

References find_nonnullable_rels_walker().

Referenced by make_outerjoininfo(), and reduce_outer_joins_pass2().

1504 {
1505  return find_nonnullable_rels_walker(clause, true);
1506 }
static Relids find_nonnullable_rels_walker(Node *node, bool top_level)
Definition: clauses.c:1509

◆ find_nonnullable_vars()

List* find_nonnullable_vars ( Node clause)

Definition at line 1728 of file clauses.c.

References find_nonnullable_vars_walker().

Referenced by reduce_outer_joins_pass2().

1729 {
1730  return find_nonnullable_vars_walker(clause, true);
1731 }
static List * find_nonnullable_vars_walker(Node *node, bool top_level)
Definition: clauses.c:1734

◆ find_window_functions()

WindowFuncLists* find_window_functions ( Node clause,
Index  maxWinRef 
)

Definition at line 509 of file clauses.c.

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

Referenced by grouping_planner().

510 {
511  WindowFuncLists *lists = palloc(sizeof(WindowFuncLists));
512 
513  lists->numWindowFuncs = 0;
514  lists->maxWinRef = maxWinRef;
515  lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
516  (void) find_window_functions_walker(clause, lists);
517  return lists;
518 }
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:521
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 231 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().

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

◆ inline_set_returning_function()

Query* inline_set_returning_function ( PlannerInfo root,
RangeTblEntry rte 
)

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

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

◆ is_parallel_safe()

bool is_parallel_safe ( PlannerInfo root,
Node node 
)

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

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

References contain_var_clause(), and contain_volatile_functions().

Referenced by clauselist_selectivity_simple(), and dependency_is_compatible_clause().

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

◆ is_pseudo_constant_clause_relids()

bool is_pseudo_constant_clause_relids ( Node clause,
Relids  relids 
)

Definition at line 2111 of file clauses.c.

References bms_is_empty(), and contain_volatile_functions().

Referenced by clauselist_selectivity_simple().

2112 {
2113  if (bms_is_empty(relids) &&
2114  !contain_volatile_functions(clause))
2115  return true;
2116  return false;
2117 }
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:726
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701

◆ max_parallel_hazard()

char max_parallel_hazard ( Query parse)

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

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

◆ NumRelids()

int NumRelids ( Node clause)

Definition at line 2133 of file clauses.c.

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

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

2134 {
2135  Relids varnos = pull_varnos(clause);
2136  int result = bms_num_members(varnos);
2137 
2138  bms_free(varnos);
2139  return result;
2140 }
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