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

Go to the source code of this file.

Data Structures

struct  WindowFuncLists
 

Functions

bool contain_agg_clause (Node *clause)
 
bool contain_window_function (Node *clause)
 
WindowFuncListsfind_window_functions (Node *clause, Index maxWinRef)
 
double expression_returns_set_rows (PlannerInfo *root, Node *clause)
 
bool contain_subplans (Node *clause)
 
char max_parallel_hazard (Query *parse)
 
bool is_parallel_safe (PlannerInfo *root, Node *node)
 
bool contain_nonstrict_functions (Node *clause)
 
bool contain_exec_param (Node *clause, List *param_ids)
 
bool contain_leaked_vars (Node *clause)
 
Relids find_nonnullable_rels (Node *clause)
 
Listfind_nonnullable_vars (Node *clause)
 
Listfind_forced_null_vars (Node *clause)
 
Varfind_forced_null_var (Node *clause)
 
bool is_pseudo_constant_clause (Node *clause)
 
bool is_pseudo_constant_clause_relids (Node *clause, Relids relids)
 
int NumRelids (PlannerInfo *root, Node *clause)
 
void CommuteOpExpr (OpExpr *clause)
 
Queryinline_set_returning_function (PlannerInfo *root, RangeTblEntry *rte)
 
Bitmapsetpull_paramids (Expr *expr)
 

Function Documentation

◆ CommuteOpExpr()

void CommuteOpExpr ( OpExpr clause)

Definition at line 2043 of file clauses.c.

2044 {
2045  Oid opoid;
2046  Node *temp;
2047 
2048  /* Sanity checks: caller is at fault if these fail */
2049  if (!is_opclause(clause) ||
2050  list_length(clause->args) != 2)
2051  elog(ERROR, "cannot commute non-binary-operator clause");
2052 
2053  opoid = get_commutator(clause->opno);
2054 
2055  if (!OidIsValid(opoid))
2056  elog(ERROR, "could not find commutator for operator %u",
2057  clause->opno);
2058 
2059  /*
2060  * modify the clause in-place!
2061  */
2062  clause->opno = opoid;
2063  clause->opfuncid = InvalidOid;
2064  /* opresulttype, opretset, opcollid, inputcollid need not change */
2065 
2066  temp = linitial(clause->args);
2067  linitial(clause->args) = lsecond(clause->args);
2068  lsecond(clause->args) = temp;
2069 }
#define OidIsValid(objectId)
Definition: c.h:710
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1490
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:64
static int list_length(const List *l)
Definition: pg_list.h:149
#define linitial(l)
Definition: pg_list.h:174
#define lsecond(l)
Definition: pg_list.h:179
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
Definition: nodes.h:574
Oid opno
Definition: primnodes.h:551
List * args
Definition: primnodes.h:557
Oid opfuncid
Definition: primnodes.h:552

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

◆ contain_agg_clause()

bool contain_agg_clause ( Node clause)

Definition at line 180 of file clauses.c.

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

References contain_agg_clause_walker().

Referenced by get_eclass_for_sort_expr(), and subquery_planner().

◆ contain_exec_param()

bool contain_exec_param ( Node clause,
List param_ids 
)

Definition at line 1079 of file clauses.c.

1080 {
1081  return contain_exec_param_walker(clause, param_ids);
1082 }
static bool contain_exec_param_walker(Node *node, List *param_ids)
Definition: clauses.c:1085

References contain_exec_param_walker().

Referenced by test_opexpr_is_hashable().

◆ contain_leaked_vars()

bool contain_leaked_vars ( Node clause)

Definition at line 1205 of file clauses.c.

1206 {
1207  return contain_leaked_vars_walker(clause, NULL);
1208 }
static bool contain_leaked_vars_walker(Node *node, void *context)
Definition: clauses.c:1217

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 935 of file clauses.c.

936 {
937  return contain_nonstrict_functions_walker(clause, NULL);
938 }
static bool contain_nonstrict_functions_walker(Node *node, void *context)
Definition: clauses.c:947

References contain_nonstrict_functions_walker().

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

◆ contain_subplans()

bool contain_subplans ( Node clause)

Definition at line 333 of file clauses.c.

334 {
335  return contain_subplans_walker(clause, NULL);
336 }
static bool contain_subplans_walker(Node *node, void *context)
Definition: clauses.c:339

References contain_subplans_walker().

Referenced by convert_EXISTS_to_ANY(), ExecInitValuesScan(), inline_function(), inline_set_returning_function(), qual_is_pushdown_safe(), and subquery_planner().

◆ contain_window_function()

bool contain_window_function ( Node clause)

Definition at line 217 of file clauses.c.

218 {
219  return contain_windowfuncs(clause);
220 }
bool contain_windowfuncs(Node *node)
Definition: rewriteManip.c:197

References contain_windowfuncs().

Referenced by get_eclass_for_sort_expr(), and qual_is_pushdown_safe().

◆ expression_returns_set_rows()

double expression_returns_set_rows ( PlannerInfo root,
Node clause 
)

Definition at line 292 of file clauses.c.

293 {
294  if (clause == NULL)
295  return 1.0;
296  if (IsA(clause, FuncExpr))
297  {
298  FuncExpr *expr = (FuncExpr *) clause;
299 
300  if (expr->funcretset)
301  return clamp_row_est(get_function_rows(root, expr->funcid, clause));
302  }
303  if (IsA(clause, OpExpr))
304  {
305  OpExpr *expr = (OpExpr *) clause;
306 
307  if (expr->opretset)
308  {
309  set_opfuncid(expr);
310  return clamp_row_est(get_function_rows(root, expr->opfuncid, clause));
311  }
312  }
313  return 1.0;
314 }
double clamp_row_est(double nrows)
Definition: costsize.c:201
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1794
#define IsA(nodeptr, _type_)
Definition: nodes.h:624
double get_function_rows(PlannerInfo *root, Oid funcid, Node *node)
Definition: plancat.c:2049
bool funcretset
Definition: primnodes.h:506
Oid funcid
Definition: primnodes.h:504
bool opretset
Definition: primnodes.h:554

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_incremental(), and set_function_size_estimates().

◆ find_forced_null_var()

Var* find_forced_null_var ( Node clause)

Definition at line 1875 of file clauses.c.

1876 {
1877  if (node == NULL)
1878  return NULL;
1879  if (IsA(node, NullTest))
1880  {
1881  /* check for var IS NULL */
1882  NullTest *expr = (NullTest *) node;
1883 
1884  if (expr->nulltesttype == IS_NULL && !expr->argisrow)
1885  {
1886  Var *var = (Var *) expr->arg;
1887 
1888  if (var && IsA(var, Var) &&
1889  var->varlevelsup == 0)
1890  return var;
1891  }
1892  }
1893  else if (IsA(node, BooleanTest))
1894  {
1895  /* var IS UNKNOWN is equivalent to var IS NULL */
1896  BooleanTest *expr = (BooleanTest *) node;
1897 
1898  if (expr->booltesttype == IS_UNKNOWN)
1899  {
1900  Var *var = (Var *) expr->arg;
1901 
1902  if (var && IsA(var, Var) &&
1903  var->varlevelsup == 0)
1904  return var;
1905  }
1906  }
1907  return NULL;
1908 }
@ IS_UNKNOWN
Definition: primnodes.h:1543
@ IS_NULL
Definition: primnodes.h:1520
BoolTestType booltesttype
Definition: primnodes.h:1550
Expr * arg
Definition: primnodes.h:1549
NullTestType nulltesttype
Definition: primnodes.h:1527
bool argisrow
Definition: primnodes.h:1528
Expr * arg
Definition: primnodes.h:1526
Definition: primnodes.h:196
Index varlevelsup
Definition: primnodes.h:205

References NullTest::arg, BooleanTest::arg, NullTest::argisrow, 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 clause)

Definition at line 1816 of file clauses.c.

1817 {
1818  List *result = NIL;
1819  Var *var;
1820  ListCell *l;
1821 
1822  if (node == NULL)
1823  return NIL;
1824  /* Check single-clause cases using subroutine */
1825  var = find_forced_null_var(node);
1826  if (var)
1827  {
1828  result = list_make1(var);
1829  }
1830  /* Otherwise, handle AND-conditions */
1831  else if (IsA(node, List))
1832  {
1833  /*
1834  * At top level, we are examining an implicit-AND list: if any of the
1835  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
1836  */
1837  foreach(l, (List *) node)
1838  {
1839  result = list_concat(result,
1841  }
1842  }
1843  else if (IsA(node, BoolExpr))
1844  {
1845  BoolExpr *expr = (BoolExpr *) node;
1846 
1847  /*
1848  * We don't bother considering the OR case, because it's fairly
1849  * unlikely anyone would write "v1 IS NULL OR v1 IS NULL". Likewise,
1850  * the NOT case isn't worth expending code on.
1851  */
1852  if (expr->boolop == AND_EXPR)
1853  {
1854  /* At top level we can just recurse (to the List case) */
1855  result = find_forced_null_vars((Node *) expr->args);
1856  }
1857  }
1858  return result;
1859 }
Var * find_forced_null_var(Node *node)
Definition: clauses.c:1875
List * find_forced_null_vars(Node *node)
Definition: clauses.c:1816
List * list_concat(List *list1, const List *list2)
Definition: list.c:540
#define lfirst(lc)
Definition: pg_list.h:169
#define NIL
Definition: pg_list.h:65
#define list_make1(x1)
Definition: pg_list.h:206
@ AND_EXPR
Definition: primnodes.h:628
BoolExprType boolop
Definition: primnodes.h:634
List * args
Definition: primnodes.h:635
Definition: pg_list.h:51

References AND_EXPR, BoolExpr::args, BoolExpr::boolop, find_forced_null_var(), IsA, lfirst, list_concat(), list_make1, and NIL.

Referenced by reduce_outer_joins_pass2().

◆ find_nonnullable_rels()

Relids find_nonnullable_rels ( Node clause)

Definition at line 1398 of file clauses.c.

1399 {
1400  return find_nonnullable_rels_walker(clause, true);
1401 }
static Relids find_nonnullable_rels_walker(Node *node, bool top_level)
Definition: clauses.c:1404

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 1623 of file clauses.c.

1624 {
1625  return find_nonnullable_vars_walker(clause, true);
1626 }
static List * find_nonnullable_vars_walker(Node *node, bool top_level)
Definition: clauses.c:1629

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 230 of file clauses.c.

231 {
232  WindowFuncLists *lists = palloc(sizeof(WindowFuncLists));
233 
234  lists->numWindowFuncs = 0;
235  lists->maxWinRef = maxWinRef;
236  lists->windowFuncs = (List **) palloc0((maxWinRef + 1) * sizeof(List *));
237  (void) find_window_functions_walker(clause, lists);
238  return lists;
239 }
static bool find_window_functions_walker(Node *node, WindowFuncLists *lists)
Definition: clauses.c:242
void * palloc0(Size size)
Definition: mcxt.c:1099
void * palloc(Size size)
Definition: mcxt.c:1068
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 4956 of file clauses.c.

4957 {
4958  RangeTblFunction *rtfunc;
4959  FuncExpr *fexpr;
4960  Oid func_oid;
4961  HeapTuple func_tuple;
4962  Form_pg_proc funcform;
4963  char *src;
4964  Datum tmp;
4965  bool isNull;
4966  MemoryContext oldcxt;
4967  MemoryContext mycxt;
4968  inline_error_callback_arg callback_arg;
4969  ErrorContextCallback sqlerrcontext;
4971  TypeFuncClass functypclass;
4972  TupleDesc rettupdesc;
4973  List *raw_parsetree_list;
4974  List *querytree_list;
4975  Query *querytree;
4976 
4977  Assert(rte->rtekind == RTE_FUNCTION);
4978 
4979  /*
4980  * It doesn't make a lot of sense for a SQL SRF to refer to itself in its
4981  * own FROM clause, since that must cause infinite recursion at runtime.
4982  * It will cause this code to recurse too, so check for stack overflow.
4983  * (There's no need to do more.)
4984  */
4986 
4987  /* Fail if the RTE has ORDINALITY - we don't implement that here. */
4988  if (rte->funcordinality)
4989  return NULL;
4990 
4991  /* Fail if RTE isn't a single, simple FuncExpr */
4992  if (list_length(rte->functions) != 1)
4993  return NULL;
4994  rtfunc = (RangeTblFunction *) linitial(rte->functions);
4995 
4996  if (!IsA(rtfunc->funcexpr, FuncExpr))
4997  return NULL;
4998  fexpr = (FuncExpr *) rtfunc->funcexpr;
4999 
5000  func_oid = fexpr->funcid;
5001 
5002  /*
5003  * The function must be declared to return a set, else inlining would
5004  * change the results if the contained SELECT didn't return exactly one
5005  * row.
5006  */
5007  if (!fexpr->funcretset)
5008  return NULL;
5009 
5010  /*
5011  * Refuse to inline if the arguments contain any volatile functions or
5012  * sub-selects. Volatile functions are rejected because inlining may
5013  * result in the arguments being evaluated multiple times, risking a
5014  * change in behavior. Sub-selects are rejected partly for implementation
5015  * reasons (pushing them down another level might change their behavior)
5016  * and partly because they're likely to be expensive and so multiple
5017  * evaluation would be bad.
5018  */
5019  if (contain_volatile_functions((Node *) fexpr->args) ||
5020  contain_subplans((Node *) fexpr->args))
5021  return NULL;
5022 
5023  /* Check permission to call function (fail later, if not) */
5024  if (pg_proc_aclcheck(func_oid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
5025  return NULL;
5026 
5027  /* Check whether a plugin wants to hook function entry/exit */
5028  if (FmgrHookIsNeeded(func_oid))
5029  return NULL;
5030 
5031  /*
5032  * OK, let's take a look at the function's pg_proc entry.
5033  */
5034  func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(func_oid));
5035  if (!HeapTupleIsValid(func_tuple))
5036  elog(ERROR, "cache lookup failed for function %u", func_oid);
5037  funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
5038 
5039  /*
5040  * Forget it if the function is not SQL-language or has other showstopper
5041  * properties. In particular it mustn't be declared STRICT, since we
5042  * couldn't enforce that. It also mustn't be VOLATILE, because that is
5043  * supposed to cause it to be executed with its own snapshot, rather than
5044  * sharing the snapshot of the calling query. We also disallow returning
5045  * SETOF VOID, because inlining would result in exposing the actual result
5046  * of the function's last SELECT, which should not happen in that case.
5047  * (Rechecking prokind, proretset, and pronargs is just paranoia.)
5048  */
5049  if (funcform->prolang != SQLlanguageId ||
5050  funcform->prokind != PROKIND_FUNCTION ||
5051  funcform->proisstrict ||
5052  funcform->provolatile == PROVOLATILE_VOLATILE ||
5053  funcform->prorettype == VOIDOID ||
5054  funcform->prosecdef ||
5055  !funcform->proretset ||
5056  list_length(fexpr->args) != funcform->pronargs ||
5057  !heap_attisnull(func_tuple, Anum_pg_proc_proconfig, NULL))
5058  {
5059  ReleaseSysCache(func_tuple);
5060  return NULL;
5061  }
5062 
5063  /*
5064  * Make a temporary memory context, so that we don't leak all the stuff
5065  * that parsing might create.
5066  */
5068  "inline_set_returning_function",
5070  oldcxt = MemoryContextSwitchTo(mycxt);
5071 
5072  /* Fetch the function body */
5073  tmp = SysCacheGetAttr(PROCOID,
5074  func_tuple,
5075  Anum_pg_proc_prosrc,
5076  &isNull);
5077  if (isNull)
5078  elog(ERROR, "null prosrc for function %u", func_oid);
5079  src = TextDatumGetCString(tmp);
5080 
5081  /*
5082  * Setup error traceback support for ereport(). This is so that we can
5083  * finger the function that bad information came from.
5084  */
5085  callback_arg.proname = NameStr(funcform->proname);
5086  callback_arg.prosrc = src;
5087 
5088  sqlerrcontext.callback = sql_inline_error_callback;
5089  sqlerrcontext.arg = (void *) &callback_arg;
5090  sqlerrcontext.previous = error_context_stack;
5091  error_context_stack = &sqlerrcontext;
5092 
5093  /* If we have prosqlbody, pay attention to that not prosrc */
5094  tmp = SysCacheGetAttr(PROCOID,
5095  func_tuple,
5096  Anum_pg_proc_prosqlbody,
5097  &isNull);
5098  if (!isNull)
5099  {
5100  Node *n;
5101 
5103  if (IsA(n, List))
5104  querytree_list = linitial_node(List, castNode(List, n));
5105  else
5106  querytree_list = list_make1(n);
5107  if (list_length(querytree_list) != 1)
5108  goto fail;
5109  querytree = linitial(querytree_list);
5110 
5111  /* Acquire necessary locks, then apply rewriter. */
5112  AcquireRewriteLocks(querytree, true, false);
5113  querytree_list = pg_rewrite_query(querytree);
5114  if (list_length(querytree_list) != 1)
5115  goto fail;
5116  querytree = linitial(querytree_list);
5117  }
5118  else
5119  {
5120  /*
5121  * Set up to handle parameters while parsing the function body. We
5122  * can use the FuncExpr just created as the input for
5123  * prepare_sql_fn_parse_info.
5124  */
5125  pinfo = prepare_sql_fn_parse_info(func_tuple,
5126  (Node *) fexpr,
5127  fexpr->inputcollid);
5128 
5129  /*
5130  * Parse, analyze, and rewrite (unlike inline_function(), we can't
5131  * skip rewriting here). We can fail as soon as we find more than one
5132  * query, though.
5133  */
5134  raw_parsetree_list = pg_parse_query(src);
5135  if (list_length(raw_parsetree_list) != 1)
5136  goto fail;
5137 
5138  querytree_list = pg_analyze_and_rewrite_withcb(linitial(raw_parsetree_list),
5139  src,
5141  pinfo, NULL);
5142  if (list_length(querytree_list) != 1)
5143  goto fail;
5144  querytree = linitial(querytree_list);
5145  }
5146 
5147  /*
5148  * Also resolve the actual function result tupdesc, if composite. If the
5149  * function is just declared to return RECORD, dig the info out of the AS
5150  * clause.
5151  */
5152  functypclass = get_expr_result_type((Node *) fexpr, NULL, &rettupdesc);
5153  if (functypclass == TYPEFUNC_RECORD)
5154  rettupdesc = BuildDescFromLists(rtfunc->funccolnames,
5155  rtfunc->funccoltypes,
5156  rtfunc->funccoltypmods,
5157  rtfunc->funccolcollations);
5158 
5159  /*
5160  * The single command must be a plain SELECT.
5161  */
5162  if (!IsA(querytree, Query) ||
5163  querytree->commandType != CMD_SELECT)
5164  goto fail;
5165 
5166  /*
5167  * Make sure the function (still) returns what it's declared to. This
5168  * will raise an error if wrong, but that's okay since the function would
5169  * fail at runtime anyway. Note that check_sql_fn_retval will also insert
5170  * coercions if needed to make the tlist expression(s) match the declared
5171  * type of the function. We also ask it to insert dummy NULL columns for
5172  * any dropped columns in rettupdesc, so that the elements of the modified
5173  * tlist match up to the attribute numbers.
5174  *
5175  * If the function returns a composite type, don't inline unless the check
5176  * shows it's returning a whole tuple result; otherwise what it's
5177  * returning is a single composite column which is not what we need.
5178  */
5179  if (!check_sql_fn_retval(list_make1(querytree_list),
5180  fexpr->funcresulttype, rettupdesc,
5181  true, NULL) &&
5182  (functypclass == TYPEFUNC_COMPOSITE ||
5183  functypclass == TYPEFUNC_COMPOSITE_DOMAIN ||
5184  functypclass == TYPEFUNC_RECORD))
5185  goto fail; /* reject not-whole-tuple-result cases */
5186 
5187  /*
5188  * check_sql_fn_retval might've inserted a projection step, but that's
5189  * fine; just make sure we use the upper Query.
5190  */
5191  querytree = linitial_node(Query, querytree_list);
5192 
5193  /*
5194  * Looks good --- substitute parameters into the query.
5195  */
5197  funcform->pronargs,
5198  fexpr->args);
5199 
5200  /*
5201  * Copy the modified query out of the temporary memory context, and clean
5202  * up.
5203  */
5204  MemoryContextSwitchTo(oldcxt);
5205 
5207 
5208  MemoryContextDelete(mycxt);
5209  error_context_stack = sqlerrcontext.previous;
5210  ReleaseSysCache(func_tuple);
5211 
5212  /*
5213  * We don't have to fix collations here because the upper query is already
5214  * parsed, ie, the collations in the RTE are what count.
5215  */
5216 
5217  /*
5218  * Since there is now no trace of the function in the plan tree, we must
5219  * explicitly record the plan's dependency on the function.
5220  */
5221  record_plan_function_dependency(root, func_oid);
5222 
5223  return querytree;
5224 
5225  /* Here if func is not inlinable: release temp memory and return NULL */
5226 fail:
5227  MemoryContextSwitchTo(oldcxt);
5228  MemoryContextDelete(mycxt);
5229  error_context_stack = sqlerrcontext.previous;
5230  ReleaseSysCache(func_tuple);
5231 
5232  return NULL;
5233 }
Datum querytree(PG_FUNCTION_ARGS)
Definition: _int_bool.c:663
@ ACLCHECK_OK
Definition: acl.h:182
AclResult pg_proc_aclcheck(Oid proc_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:5071
#define TextDatumGetCString(d)
Definition: builtins.h:86
#define NameStr(name)
Definition: c.h:681
static Query * substitute_actual_srf_parameters(Query *expr, int nargs, List *args)
Definition: clauses.c:5242
static void sql_inline_error_callback(void *arg)
Definition: clauses.c:4840
bool contain_subplans(Node *clause)
Definition: clauses.c:333
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:496
ErrorContextCallback * error_context_stack
Definition: elog.c:93
#define FmgrHookIsNeeded(fn_oid)
Definition: fmgr.h:778
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:292
TypeFuncClass
Definition: funcapi.h:147
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
@ TYPEFUNC_RECORD
Definition: funcapi.h:151
@ TYPEFUNC_COMPOSITE_DOMAIN
Definition: funcapi.h:150
bool check_sql_fn_retval(List *queryTreeLists, Oid rettype, TupleDesc rettupdesc, bool insertDroppedCols, List **resultTargetList)
Definition: functions.c:1599
void sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
Definition: functions.c:265
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:359
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
Assert(fmt[strlen(fmt) - 1] !='\n')
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:197
Oid GetUserId(void)
Definition: miscinit.c:492
#define copyObject(obj)
Definition: nodes.h:689
@ CMD_SELECT
Definition: nodes.h:721
#define castNode(_type_, nodeptr)
Definition: nodes.h:642
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
Definition: params.h:108
@ RTE_FUNCTION
Definition: parsenodes.h:1001
#define ACL_EXECUTE
Definition: parsenodes.h:89
#define linitial_node(type, l)
Definition: pg_list.h:177
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
List * pg_parse_query(const char *query_string)
Definition: postgres.c:592
List * pg_analyze_and_rewrite_withcb(RawStmt *parsetree, const char *query_string, ParserSetupHook parserSetup, void *parserSetupArg, QueryEnvironment *queryEnv)
Definition: postgres.c:733
List * pg_rewrite_query(Query *query)
Definition: postgres.c:773
void check_stack_depth(void)
Definition: postgres.c:3500
uintptr_t Datum
Definition: postgres.h:411
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
void * stringToNode(const char *str)
Definition: read.c:89
void AcquireRewriteLocks(Query *parsetree, bool forExecute, bool forUpdatePushedDown)
void record_plan_function_dependency(PlannerInfo *root, Oid funcid)
Definition: setrefs.c:3187
struct ErrorContextCallback * previous
Definition: elog.h:232
void(* callback)(void *arg)
Definition: elog.h:233
List * args
Definition: primnodes.h:512
Oid funcresulttype
Definition: primnodes.h:505
bool funcordinality
Definition: parsenodes.h:1110
List * functions
Definition: parsenodes.h:1109
RTEKind rtekind
Definition: parsenodes.h:1015
List * funccolcollations
Definition: parsenodes.h:1200
List * funccoltypmods
Definition: parsenodes.h:1199
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1173
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1434
@ PROCOID
Definition: syscache.h:79
TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
Definition: tupdesc.c:877

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::funccolcollations, RangeTblFunction::funccolnames, RangeTblFunction::funccoltypes, RangeTblFunction::funccoltypmods, RangeTblFunction::funcexpr, FuncExpr::funcid, RangeTblEntry::funcordinality, FuncExpr::funcresulttype, FuncExpr::funcretset, RangeTblEntry::functions, get_expr_result_type(), GETSTRUCT, GetUserId(), heap_attisnull(), HeapTupleIsValid, if(), IsA, linitial, linitial_node, list_length(), list_make1, MemoryContextDelete(), MemoryContextSwitchTo(), NameStr, ObjectIdGetDatum, pg_analyze_and_rewrite_withcb(), pg_parse_query(), pg_proc_aclcheck(), pg_rewrite_query(), prepare_sql_fn_parse_info(), ErrorContextCallback::previous, PROCOID, inline_error_callback_arg::proname, inline_error_callback_arg::prosrc, querytree(), record_plan_function_dependency(), ReleaseSysCache(), RTE_FUNCTION, RangeTblEntry::rtekind, SearchSysCache1(), sql_fn_parser_setup(), sql_inline_error_callback(), stringToNode(), substitute_actual_srf_parameters(), SysCacheGetAttr(), TextDatumGetCString, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, and TYPEFUNC_RECORD.

Referenced by preprocess_function_rtes().

◆ is_parallel_safe()

bool is_parallel_safe ( PlannerInfo root,
Node node 
)

Definition at line 683 of file clauses.c.

684 {
686  PlannerInfo *proot;
687  ListCell *l;
688 
689  /*
690  * Even if the original querytree contained nothing unsafe, we need to
691  * search the expression if we have generated any PARAM_EXEC Params while
692  * planning, because those are parallel-restricted and there might be one
693  * in this expression. But otherwise we don't need to look.
694  */
695  if (root->glob->maxParallelHazard == PROPARALLEL_SAFE &&
696  root->glob->paramExecTypes == NIL)
697  return true;
698  /* Else use max_parallel_hazard's search logic, but stop on RESTRICTED */
699  context.max_hazard = PROPARALLEL_SAFE;
700  context.max_interesting = PROPARALLEL_RESTRICTED;
701  context.safe_param_ids = NIL;
702 
703  /*
704  * The params that refer to the same or parent query level are considered
705  * parallel-safe. The idea is that we compute such params at Gather or
706  * Gather Merge node and pass their value to workers.
707  */
708  for (proot = root; proot != NULL; proot = proot->parent_root)
709  {
710  foreach(l, proot->init_plans)
711  {
712  SubPlan *initsubplan = (SubPlan *) lfirst(l);
713 
714  context.safe_param_ids = list_concat(context.safe_param_ids,
715  initsubplan->setParam);
716  }
717  }
718 
719  return !max_parallel_hazard_walker(node, &context);
720 }
static bool max_parallel_hazard_walker(Node *node, max_parallel_hazard_context *context)
Definition: clauses.c:759
char maxParallelHazard
Definition: pathnodes.h:130
List * paramExecTypes
Definition: pathnodes.h:114
List * init_plans
Definition: pathnodes.h:242
PlannerGlobal * glob
Definition: pathnodes.h:164
PlannerInfo * parent_root
Definition: pathnodes.h:168
List * setParam
Definition: primnodes.h:779

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

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

◆ is_pseudo_constant_clause()

bool is_pseudo_constant_clause ( Node clause)

Definition at line 1986 of file clauses.c.

1987 {
1988  /*
1989  * We could implement this check in one recursive scan. But since the
1990  * check for volatile functions is both moderately expensive and unlikely
1991  * to fail, it seems better to look for Vars first and only check for
1992  * volatile functions if we find no Vars.
1993  */
1994  if (!contain_var_clause(clause) &&
1995  !contain_volatile_functions(clause))
1996  return true;
1997  return false;
1998 }
bool contain_var_clause(Node *node)
Definition: var.c:400

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 2006 of file clauses.c.

2007 {
2008  if (bms_is_empty(relids) &&
2009  !contain_volatile_functions(clause))
2010  return true;
2011  return false;
2012 }
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:703

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 664 of file clauses.c.

665 {
667 
668  context.max_hazard = PROPARALLEL_SAFE;
669  context.max_interesting = PROPARALLEL_UNSAFE;
670  context.safe_param_ids = NIL;
671  (void) max_parallel_hazard_walker((Node *) parse, &context);
672  return context.max_hazard;
673 }
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:673

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

◆ NumRelids()

int NumRelids ( PlannerInfo root,
Node clause 
)

Definition at line 2028 of file clauses.c.

2029 {
2030  Relids varnos = pull_varnos(root, clause);
2031  int result = bms_num_members(varnos);
2032 
2033  bms_free(varnos);
2034  return result;
2035 }
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
int bms_num_members(const Bitmapset *a)
Definition: bitmapset.c:648
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:100

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

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

◆ pull_paramids()

Bitmapset* pull_paramids ( Expr expr)

Definition at line 5302 of file clauses.c.

5303 {
5304  Bitmapset *result = NULL;
5305 
5306  (void) pull_paramids_walker((Node *) expr, &result);
5307 
5308  return result;
5309 }
static bool pull_paramids_walker(Node *node, Bitmapset **context)
Definition: clauses.c:5312

References pull_paramids_walker().

Referenced by create_memoize_plan().