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 *node)
 
Varfind_forced_null_var (Node *node)
 
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)
 
Bitmapsetpull_paramids (Expr *expr)
 

Function Documentation

◆ CommuteOpExpr()

void CommuteOpExpr ( OpExpr clause)

Definition at line 2147 of file clauses.c.

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

References OpExpr::args, elog, ERROR, get_commutator(), InvalidOid, is_opclause(), linitial, list_length(), lsecond, OidIsValid, and OpExpr::opno.

Referenced by get_switched_clauses().

◆ contain_agg_clause()

bool contain_agg_clause ( Node clause)

Definition at line 177 of file clauses.c.

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

References contain_agg_clause_walker().

Referenced by get_eclass_for_sort_expr(), mark_nullable_by_grouping(), and subquery_planner().

◆ contain_exec_param()

bool contain_exec_param ( Node clause,
List param_ids 
)

Definition at line 1137 of file clauses.c.

1138 {
1139  return contain_exec_param_walker(clause, param_ids);
1140 }
static bool contain_exec_param_walker(Node *node, List *param_ids)
Definition: clauses.c:1143

References contain_exec_param_walker().

Referenced by test_opexpr_is_hashable().

◆ contain_leaked_vars()

bool contain_leaked_vars ( Node clause)

Definition at line 1263 of file clauses.c.

1264 {
1265  return contain_leaked_vars_walker(clause, NULL);
1266 }
static bool contain_leaked_vars_walker(Node *node, void *context)
Definition: clauses.c:1275

References contain_leaked_vars_walker().

Referenced by make_restrictinfo_internal(), and qual_is_pushdown_safe().

◆ contain_nonstrict_functions()

bool contain_nonstrict_functions ( Node clause)

Definition at line 993 of file clauses.c.

994 {
995  return contain_nonstrict_functions_walker(clause, NULL);
996 }
static bool contain_nonstrict_functions_walker(Node *node, void *context)
Definition: clauses.c:1005

References contain_nonstrict_functions_walker().

Referenced by inline_function(), and pullup_replace_vars_callback().

◆ contain_subplans()

bool contain_subplans ( Node clause)

◆ contain_window_function()

bool contain_window_function ( Node clause)

Definition at line 214 of file clauses.c.

215 {
216  return contain_windowfuncs(clause);
217 }
bool contain_windowfuncs(Node *node)
Definition: rewriteManip.c:215

References contain_windowfuncs().

Referenced by get_eclass_for_sort_expr(), and mark_nullable_by_grouping().

◆ expression_returns_set_rows()

double expression_returns_set_rows ( PlannerInfo root,
Node clause 
)

Definition at line 289 of file clauses.c.

290 {
291  if (clause == NULL)
292  return 1.0;
293  if (IsA(clause, FuncExpr))
294  {
295  FuncExpr *expr = (FuncExpr *) clause;
296 
297  if (expr->funcretset)
298  return clamp_row_est(get_function_rows(root, expr->funcid, clause));
299  }
300  if (IsA(clause, OpExpr))
301  {
302  OpExpr *expr = (OpExpr *) clause;
303 
304  if (expr->opretset)
305  {
306  set_opfuncid(expr);
307  return clamp_row_est(get_function_rows(root, expr->opfuncid, clause));
308  }
309  }
310  return 1.0;
311 }
double clamp_row_est(double nrows)
Definition: costsize.c:213
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1862
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
double get_function_rows(PlannerInfo *root, Oid funcid, Node *node)
Definition: plancat.c:2156
tree ctl root
Definition: radixtree.h:1886
Oid funcid
Definition: primnodes.h:750

References clamp_row_est(), FuncExpr::funcid, get_function_rows(), IsA, root, and set_opfuncid().

Referenced by create_set_projection_path(), estimate_num_groups(), and set_function_size_estimates().

◆ find_forced_null_var()

Var* find_forced_null_var ( Node node)

Definition at line 1977 of file clauses.c.

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 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
@ IS_UNKNOWN
Definition: primnodes.h:1972
@ IS_NULL
Definition: primnodes.h:1948
BoolTestType booltesttype
Definition: primnodes.h:1979
Expr * arg
Definition: primnodes.h:1978
NullTestType nulltesttype
Definition: primnodes.h:1955
Expr * arg
Definition: primnodes.h:1954
Definition: primnodes.h:248
Index varlevelsup
Definition: primnodes.h:280

References NullTest::arg, BooleanTest::arg, BooleanTest::booltesttype, if(), IS_NULL, IS_UNKNOWN, IsA, NullTest::nulltesttype, and Var::varlevelsup.

Referenced by check_redundant_nullability_qual(), and find_forced_null_vars().

◆ find_forced_null_vars()

List* find_forced_null_vars ( Node node)

Definition at line 1916 of file clauses.c.

1917 {
1918  List *result = NIL;
1919  Var *var;
1920  ListCell *l;
1921 
1922  if (node == NULL)
1923  return NIL;
1924  /* Check single-clause cases using subroutine */
1925  var = find_forced_null_var(node);
1926  if (var)
1927  {
1928  result = mbms_add_member(result,
1929  var->varno,
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 = mbms_add_members(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 }
Var * find_forced_null_var(Node *node)
Definition: clauses.c:1977
List * find_forced_null_vars(Node *node)
Definition: clauses.c:1916
List * mbms_add_member(List *a, int listidx, int bitidx)
List * mbms_add_members(List *a, const List *b)
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
@ AND_EXPR
Definition: primnodes.h:931
BoolExprType boolop
Definition: primnodes.h:939
List * args
Definition: primnodes.h:940
Definition: pg_list.h:54
AttrNumber varattno
Definition: primnodes.h:260
int varno
Definition: primnodes.h:255
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

References AND_EXPR, BoolExpr::args, BoolExpr::boolop, find_forced_null_var(), FirstLowInvalidHeapAttributeNumber, IsA, lfirst, mbms_add_member(), mbms_add_members(), NIL, Var::varattno, and Var::varno.

Referenced by reduce_outer_joins_pass2().

◆ find_nonnullable_rels()

Relids find_nonnullable_rels ( Node clause)

Definition at line 1456 of file clauses.c.

1457 {
1458  return find_nonnullable_rels_walker(clause, true);
1459 }
static Relids find_nonnullable_rels_walker(Node *node, bool top_level)
Definition: clauses.c:1462

References find_nonnullable_rels_walker().

Referenced by make_outerjoininfo(), and reduce_outer_joins_pass2().

◆ find_nonnullable_vars()

List* find_nonnullable_vars ( Node clause)

Definition at line 1707 of file clauses.c.

1708 {
1709  return find_nonnullable_vars_walker(clause, true);
1710 }
static List * find_nonnullable_vars_walker(Node *node, bool top_level)
Definition: clauses.c:1713

References find_nonnullable_vars_walker().

Referenced by reduce_outer_joins_pass2().

◆ find_window_functions()

WindowFuncLists* find_window_functions ( Node clause,
Index  maxWinRef 
)

Definition at line 227 of file clauses.c.

228 {
229  WindowFuncLists *lists = palloc(sizeof(WindowFuncLists));
230 
231  lists->numWindowFuncs = 0;
232  lists->maxWinRef = maxWinRef;
233  lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
234  (void) find_window_functions_walker(clause, lists);
235  return lists;
236 }
static bool find_window_functions_walker(Node *node, WindowFuncLists *lists)
Definition: clauses.c:239
void * palloc0(Size size)
Definition: mcxt.c:1347
void * palloc(Size size)
Definition: mcxt.c:1317
List ** windowFuncs
Definition: clauses.h:23
Index maxWinRef
Definition: clauses.h:22
int numWindowFuncs
Definition: clauses.h:21

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

Referenced by grouping_planner().

◆ inline_set_returning_function()

Query* inline_set_returning_function ( PlannerInfo root,
RangeTblEntry rte 
)

Definition at line 5053 of file clauses.c.

5054 {
5055  RangeTblFunction *rtfunc;
5056  FuncExpr *fexpr;
5057  Oid func_oid;
5058  HeapTuple func_tuple;
5059  Form_pg_proc funcform;
5060  char *src;
5061  Datum tmp;
5062  bool isNull;
5063  MemoryContext oldcxt;
5064  MemoryContext mycxt;
5065  inline_error_callback_arg callback_arg;
5066  ErrorContextCallback sqlerrcontext;
5068  TypeFuncClass functypclass;
5069  TupleDesc rettupdesc;
5070  List *raw_parsetree_list;
5071  List *querytree_list;
5072  Query *querytree;
5073 
5074  Assert(rte->rtekind == RTE_FUNCTION);
5075 
5076  /*
5077  * It doesn't make a lot of sense for a SQL SRF to refer to itself in its
5078  * own FROM clause, since that must cause infinite recursion at runtime.
5079  * It will cause this code to recurse too, so check for stack overflow.
5080  * (There's no need to do more.)
5081  */
5083 
5084  /* Fail if the RTE has ORDINALITY - we don't implement that here. */
5085  if (rte->funcordinality)
5086  return NULL;
5087 
5088  /* Fail if RTE isn't a single, simple FuncExpr */
5089  if (list_length(rte->functions) != 1)
5090  return NULL;
5091  rtfunc = (RangeTblFunction *) linitial(rte->functions);
5092 
5093  if (!IsA(rtfunc->funcexpr, FuncExpr))
5094  return NULL;
5095  fexpr = (FuncExpr *) rtfunc->funcexpr;
5096 
5097  func_oid = fexpr->funcid;
5098 
5099  /*
5100  * The function must be declared to return a set, else inlining would
5101  * change the results if the contained SELECT didn't return exactly one
5102  * row.
5103  */
5104  if (!fexpr->funcretset)
5105  return NULL;
5106 
5107  /*
5108  * Refuse to inline if the arguments contain any volatile functions or
5109  * sub-selects. Volatile functions are rejected because inlining may
5110  * result in the arguments being evaluated multiple times, risking a
5111  * change in behavior. Sub-selects are rejected partly for implementation
5112  * reasons (pushing them down another level might change their behavior)
5113  * and partly because they're likely to be expensive and so multiple
5114  * evaluation would be bad.
5115  */
5116  if (contain_volatile_functions((Node *) fexpr->args) ||
5117  contain_subplans((Node *) fexpr->args))
5118  return NULL;
5119 
5120  /* Check permission to call function (fail later, if not) */
5121  if (object_aclcheck(ProcedureRelationId, func_oid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
5122  return NULL;
5123 
5124  /* Check whether a plugin wants to hook function entry/exit */
5125  if (FmgrHookIsNeeded(func_oid))
5126  return NULL;
5127 
5128  /*
5129  * OK, let's take a look at the function's pg_proc entry.
5130  */
5131  func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_oid));
5132  if (!HeapTupleIsValid(func_tuple))
5133  elog(ERROR, "cache lookup failed for function %u", func_oid);
5134  funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
5135 
5136  /*
5137  * Forget it if the function is not SQL-language or has other showstopper
5138  * properties. In particular it mustn't be declared STRICT, since we
5139  * couldn't enforce that. It also mustn't be VOLATILE, because that is
5140  * supposed to cause it to be executed with its own snapshot, rather than
5141  * sharing the snapshot of the calling query. We also disallow returning
5142  * SETOF VOID, because inlining would result in exposing the actual result
5143  * of the function's last SELECT, which should not happen in that case.
5144  * (Rechecking prokind, proretset, and pronargs is just paranoia.)
5145  */
5146  if (funcform->prolang != SQLlanguageId ||
5147  funcform->prokind != PROKIND_FUNCTION ||
5148  funcform->proisstrict ||
5149  funcform->provolatile == PROVOLATILE_VOLATILE ||
5150  funcform->prorettype == VOIDOID ||
5151  funcform->prosecdef ||
5152  !funcform->proretset ||
5153  list_length(fexpr->args) != funcform->pronargs ||
5154  !heap_attisnull(func_tuple, Anum_pg_proc_proconfig, NULL))
5155  {
5156  ReleaseSysCache(func_tuple);
5157  return NULL;
5158  }
5159 
5160  /*
5161  * Make a temporary memory context, so that we don't leak all the stuff
5162  * that parsing might create.
5163  */
5165  "inline_set_returning_function",
5167  oldcxt = MemoryContextSwitchTo(mycxt);
5168 
5169  /* Fetch the function body */
5170  tmp = SysCacheGetAttrNotNull(PROCOID, func_tuple, Anum_pg_proc_prosrc);
5171  src = TextDatumGetCString(tmp);
5172 
5173  /*
5174  * Setup error traceback support for ereport(). This is so that we can
5175  * finger the function that bad information came from.
5176  */
5177  callback_arg.proname = NameStr(funcform->proname);
5178  callback_arg.prosrc = src;
5179 
5180  sqlerrcontext.callback = sql_inline_error_callback;
5181  sqlerrcontext.arg = (void *) &callback_arg;
5182  sqlerrcontext.previous = error_context_stack;
5183  error_context_stack = &sqlerrcontext;
5184 
5185  /* If we have prosqlbody, pay attention to that not prosrc */
5186  tmp = SysCacheGetAttr(PROCOID,
5187  func_tuple,
5188  Anum_pg_proc_prosqlbody,
5189  &isNull);
5190  if (!isNull)
5191  {
5192  Node *n;
5193 
5195  if (IsA(n, List))
5196  querytree_list = linitial_node(List, castNode(List, n));
5197  else
5198  querytree_list = list_make1(n);
5199  if (list_length(querytree_list) != 1)
5200  goto fail;
5201  querytree = linitial(querytree_list);
5202 
5203  /* Acquire necessary locks, then apply rewriter. */
5204  AcquireRewriteLocks(querytree, true, false);
5205  querytree_list = pg_rewrite_query(querytree);
5206  if (list_length(querytree_list) != 1)
5207  goto fail;
5208  querytree = linitial(querytree_list);
5209  }
5210  else
5211  {
5212  /*
5213  * Set up to handle parameters while parsing the function body. We
5214  * can use the FuncExpr just created as the input for
5215  * prepare_sql_fn_parse_info.
5216  */
5217  pinfo = prepare_sql_fn_parse_info(func_tuple,
5218  (Node *) fexpr,
5219  fexpr->inputcollid);
5220 
5221  /*
5222  * Parse, analyze, and rewrite (unlike inline_function(), we can't
5223  * skip rewriting here). We can fail as soon as we find more than one
5224  * query, though.
5225  */
5226  raw_parsetree_list = pg_parse_query(src);
5227  if (list_length(raw_parsetree_list) != 1)
5228  goto fail;
5229 
5230  querytree_list = pg_analyze_and_rewrite_withcb(linitial(raw_parsetree_list),
5231  src,
5233  pinfo, NULL);
5234  if (list_length(querytree_list) != 1)
5235  goto fail;
5236  querytree = linitial(querytree_list);
5237  }
5238 
5239  /*
5240  * Also resolve the actual function result tupdesc, if composite. If we
5241  * have a coldeflist, believe that; otherwise use get_expr_result_type.
5242  * (This logic should match ExecInitFunctionScan.)
5243  */
5244  if (rtfunc->funccolnames != NIL)
5245  {
5246  functypclass = TYPEFUNC_RECORD;
5247  rettupdesc = BuildDescFromLists(rtfunc->funccolnames,
5248  rtfunc->funccoltypes,
5249  rtfunc->funccoltypmods,
5250  rtfunc->funccolcollations);
5251  }
5252  else
5253  functypclass = get_expr_result_type((Node *) fexpr, NULL, &rettupdesc);
5254 
5255  /*
5256  * The single command must be a plain SELECT.
5257  */
5258  if (!IsA(querytree, Query) ||
5259  querytree->commandType != CMD_SELECT)
5260  goto fail;
5261 
5262  /*
5263  * Make sure the function (still) returns what it's declared to. This
5264  * will raise an error if wrong, but that's okay since the function would
5265  * fail at runtime anyway. Note that check_sql_fn_retval will also insert
5266  * coercions if needed to make the tlist expression(s) match the declared
5267  * type of the function. We also ask it to insert dummy NULL columns for
5268  * any dropped columns in rettupdesc, so that the elements of the modified
5269  * tlist match up to the attribute numbers.
5270  *
5271  * If the function returns a composite type, don't inline unless the check
5272  * shows it's returning a whole tuple result; otherwise what it's
5273  * returning is a single composite column which is not what we need.
5274  */
5275  if (!check_sql_fn_retval(list_make1(querytree_list),
5276  fexpr->funcresulttype, rettupdesc,
5277  funcform->prokind,
5278  true, NULL) &&
5279  (functypclass == TYPEFUNC_COMPOSITE ||
5280  functypclass == TYPEFUNC_COMPOSITE_DOMAIN ||
5281  functypclass == TYPEFUNC_RECORD))
5282  goto fail; /* reject not-whole-tuple-result cases */
5283 
5284  /*
5285  * check_sql_fn_retval might've inserted a projection step, but that's
5286  * fine; just make sure we use the upper Query.
5287  */
5288  querytree = linitial_node(Query, querytree_list);
5289 
5290  /*
5291  * Looks good --- substitute parameters into the query.
5292  */
5294  funcform->pronargs,
5295  fexpr->args);
5296 
5297  /*
5298  * Copy the modified query out of the temporary memory context, and clean
5299  * up.
5300  */
5301  MemoryContextSwitchTo(oldcxt);
5302 
5304 
5305  MemoryContextDelete(mycxt);
5306  error_context_stack = sqlerrcontext.previous;
5307  ReleaseSysCache(func_tuple);
5308 
5309  /*
5310  * We don't have to fix collations here because the upper query is already
5311  * parsed, ie, the collations in the RTE are what count.
5312  */
5313 
5314  /*
5315  * Since there is now no trace of the function in the plan tree, we must
5316  * explicitly record the plan's dependency on the function.
5317  */
5319 
5320  /*
5321  * We must also notice if the inserted query adds a dependency on the
5322  * calling role due to RLS quals.
5323  */
5324  if (querytree->hasRowSecurity)
5325  root->glob->dependsOnRole = true;
5326 
5327  return querytree;
5328 
5329  /* Here if func is not inlinable: release temp memory and return NULL */
5330 fail:
5331  MemoryContextSwitchTo(oldcxt);
5332  MemoryContextDelete(mycxt);
5333  error_context_stack = sqlerrcontext.previous;
5334  ReleaseSysCache(func_tuple);
5335 
5336  return NULL;
5337 }
Datum querytree(PG_FUNCTION_ARGS)
Definition: _int_bool.c:666
@ ACLCHECK_OK
Definition: acl.h:183
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:3891
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define NameStr(name)
Definition: c.h:749
#define Assert(condition)
Definition: c.h:861
static Query * substitute_actual_srf_parameters(Query *expr, int nargs, List *args)
Definition: clauses.c:5346
static void sql_inline_error_callback(void *arg)
Definition: clauses.c:4937
bool contain_subplans(Node *clause)
Definition: clauses.c:330
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:538
ErrorContextCallback * error_context_stack
Definition: elog.c:94
#define FmgrHookIsNeeded(fn_oid)
Definition: fmgr.h:796
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:299
TypeFuncClass
Definition: funcapi.h:147
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
@ TYPEFUNC_RECORD
Definition: funcapi.h:151
@ TYPEFUNC_COMPOSITE_DOMAIN
Definition: funcapi.h:150
void sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
Definition: functions.c:265
bool check_sql_fn_retval(List *queryTreeLists, Oid rettype, TupleDesc rettupdesc, char prokind, bool insertDroppedCols, List **resultTargetList)
Definition: functions.c:1609
SQLFunctionParseInfoPtr prepare_sql_fn_parse_info(HeapTuple procedureTuple, Node *call_expr, Oid inputCollation)
Definition: functions.c:176
bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
Definition: heaptuple.c:455
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
Oid GetUserId(void)
Definition: miscinit.c:514
#define copyObject(obj)
Definition: nodes.h:224
@ CMD_SELECT
Definition: nodes.h:265
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
Definition: params.h:108
@ RTE_FUNCTION
Definition: parsenodes.h:1020
#define ACL_EXECUTE
Definition: parsenodes.h:83
#define linitial_node(type, l)
Definition: pg_list.h:181
#define list_make1(x1)
Definition: pg_list.h:212
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
List * pg_parse_query(const char *query_string)
Definition: postgres.c:615
List * pg_analyze_and_rewrite_withcb(RawStmt *parsetree, const char *query_string, ParserSetupHook parserSetup, void *parserSetupArg, QueryEnvironment *queryEnv)
Definition: postgres.c:770
List * pg_rewrite_query(Query *query)
Definition: postgres.c:810
void check_stack_depth(void)
Definition: postgres.c:3564
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
MemoryContextSwitchTo(old_ctx)
void * stringToNode(const char *str)
Definition: read.c:90
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
void record_plan_function_dependency(PlannerInfo *root, Oid funcid)
Definition: setrefs.c:3484
struct ErrorContextCallback * previous
Definition: elog.h:296
void(* callback)(void *arg)
Definition: elog.h:297
List * args
Definition: primnodes.h:768
bool funcordinality
Definition: parsenodes.h:1179
List * functions
Definition: parsenodes.h:1177
RTEKind rtekind
Definition: parsenodes.h:1047
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:596
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:627
TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)
Definition: tupdesc.c:858

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, contain_subplans(), contain_volatile_functions(), copyObject, CurrentMemoryContext, elog, ERROR, error_context_stack, FmgrHookIsNeeded, RangeTblFunction::funcexpr, FuncExpr::funcid, RangeTblEntry::funcordinality, RangeTblEntry::functions, get_expr_result_type(), GETSTRUCT, GetUserId(), heap_attisnull(), HeapTupleIsValid, if(), IsA, linitial, linitial_node, list_length(), list_make1, MemoryContextDelete(), MemoryContextSwitchTo(), NameStr, NIL, object_aclcheck(), ObjectIdGetDatum(), pg_analyze_and_rewrite_withcb(), pg_parse_query(), pg_rewrite_query(), prepare_sql_fn_parse_info(), ErrorContextCallback::previous, inline_error_callback_arg::proname, inline_error_callback_arg::prosrc, querytree(), record_plan_function_dependency(), ReleaseSysCache(), root, RTE_FUNCTION, RangeTblEntry::rtekind, SearchSysCache1(), sql_fn_parser_setup(), sql_inline_error_callback(), stringToNode(), substitute_actual_srf_parameters(), SysCacheGetAttr(), SysCacheGetAttrNotNull(), TextDatumGetCString, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, and TYPEFUNC_RECORD.

Referenced by preprocess_function_rtes().

◆ is_parallel_safe()

bool is_parallel_safe ( PlannerInfo root,
Node node 
)

Definition at line 753 of file clauses.c.

754 {
756  PlannerInfo *proot;
757  ListCell *l;
758 
759  /*
760  * Even if the original querytree contained nothing unsafe, we need to
761  * search the expression if we have generated any PARAM_EXEC Params while
762  * planning, because those are parallel-restricted and there might be one
763  * in this expression. But otherwise we don't need to look.
764  */
765  if (root->glob->maxParallelHazard == PROPARALLEL_SAFE &&
766  root->glob->paramExecTypes == NIL)
767  return true;
768  /* Else use max_parallel_hazard's search logic, but stop on RESTRICTED */
769  context.max_hazard = PROPARALLEL_SAFE;
770  context.max_interesting = PROPARALLEL_RESTRICTED;
771  context.safe_param_ids = NIL;
772 
773  /*
774  * The params that refer to the same or parent query level are considered
775  * parallel-safe. The idea is that we compute such params at Gather or
776  * Gather Merge node and pass their value to workers.
777  */
778  for (proot = root; proot != NULL; proot = proot->parent_root)
779  {
780  foreach(l, proot->init_plans)
781  {
782  SubPlan *initsubplan = (SubPlan *) lfirst(l);
783 
784  context.safe_param_ids = list_concat(context.safe_param_ids,
785  initsubplan->setParam);
786  }
787  }
788 
789  return !max_parallel_hazard_walker(node, &context);
790 }
static bool max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context)
Definition: clauses.c:829
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
tree context
Definition: radixtree.h:1835
List * init_plans
Definition: pathnodes.h:299
List * setParam
Definition: primnodes.h:1088

References context, PlannerInfo::init_plans, lfirst, list_concat(), max_parallel_hazard_walker(), NIL, root, and SubPlan::setParam.

Referenced by apply_projection_to_path(), build_join_rel(), create_minmaxagg_path(), 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().

◆ is_pseudo_constant_clause()

bool is_pseudo_constant_clause ( Node clause)

Definition at line 2088 of file clauses.c.

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:408

References contain_var_clause(), and contain_volatile_functions().

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

◆ is_pseudo_constant_clause_relids()

bool is_pseudo_constant_clause_relids ( Node clause,
Relids  relids 
)

Definition at line 2108 of file clauses.c.

2109 {
2110  if (bms_is_empty(relids) &&
2111  !contain_volatile_functions(clause))
2112  return true;
2113  return false;
2114 }
#define bms_is_empty(a)
Definition: bitmapset.h:118

References bms_is_empty, and contain_volatile_functions().

Referenced by clauselist_selectivity_ext().

◆ max_parallel_hazard()

char max_parallel_hazard ( Query parse)

Definition at line 734 of file clauses.c.

735 {
737 
738  context.max_hazard = PROPARALLEL_SAFE;
739  context.max_interesting = PROPARALLEL_UNSAFE;
740  context.safe_param_ids = NIL;
742  return context.max_hazard;
743 }
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:715

References context, max_parallel_hazard_walker(), NIL, and parse().

Referenced by standard_planner().

◆ NumRelids()

int NumRelids ( PlannerInfo root,
Node clause 
)

Definition at line 2130 of file clauses.c.

2131 {
2132  int result;
2133  Relids varnos = pull_varnos(root, clause);
2134 
2135  varnos = bms_del_members(varnos, root->outer_join_rels);
2136  result = bms_num_members(varnos);
2137  bms_free(varnos);
2138  return result;
2139 }
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:751
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1161
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:113

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

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

◆ pull_paramids()

Bitmapset* pull_paramids ( Expr expr)

Definition at line 5406 of file clauses.c.

5407 {
5408  Bitmapset *result = NULL;
5409 
5410  (void) pull_paramids_walker((Node *) expr, &result);
5411 
5412  return result;
5413 }
static bool pull_paramids_walker(Node *node, Bitmapset **context)
Definition: clauses.c:5416

References pull_paramids_walker().

Referenced by create_memoize_plan().