PostgreSQL Source Code  git master
clauses.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_class.h"
#include "catalog/pg_language.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/executor.h"
#include "executor/functions.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/multibitmapset.h"
#include "nodes/nodeFuncs.h"
#include "nodes/subscripting.h"
#include "nodes/supportnodes.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "optimizer/optimizer.h"
#include "optimizer/plancat.h"
#include "optimizer/planmain.h"
#include "parser/analyze.h"
#include "parser/parse_agg.h"
#include "parser/parse_coerce.h"
#include "parser/parse_func.h"
#include "rewrite/rewriteHandler.h"
#include "rewrite/rewriteManip.h"
#include "tcop/tcopprot.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for clauses.c:

Go to the source code of this file.

Data Structures

struct  eval_const_expressions_context
 
struct  substitute_actual_parameters_context
 
struct  substitute_actual_srf_parameters_context
 
struct  inline_error_callback_arg
 
struct  max_parallel_hazard_context
 

Macros

#define CCDN_CASETESTEXPR_OK   0x0001 /* CaseTestExpr okay here? */
 
#define MIN_ARRAY_SIZE_FOR_HASHED_SAOP   9
 
#define ece_generic_processing(node)
 
#define ece_all_arguments_const(node)    (!expression_tree_walker((Node *) (node), contain_non_const_walker, NULL))
 
#define ece_evaluate_expr(node)
 

Functions

static bool contain_agg_clause_walker (Node *node, void *context)
 
static bool find_window_functions_walker (Node *node, WindowFuncLists *lists)
 
static bool contain_subplans_walker (Node *node, void *context)
 
static bool contain_mutable_functions_walker (Node *node, void *context)
 
static bool contain_volatile_functions_walker (Node *node, void *context)
 
static bool contain_volatile_functions_not_nextval_walker (Node *node, void *context)
 
static bool max_parallel_hazard_walker (Node *node, max_parallel_hazard_context *context)
 
static bool contain_nonstrict_functions_walker (Node *node, void *context)
 
static bool contain_exec_param_walker (Node *node, List *param_ids)
 
static bool contain_context_dependent_node (Node *clause)
 
static bool contain_context_dependent_node_walker (Node *node, int *flags)
 
static bool contain_leaked_vars_walker (Node *node, void *context)
 
static Relids find_nonnullable_rels_walker (Node *node, bool top_level)
 
static Listfind_nonnullable_vars_walker (Node *node, bool top_level)
 
static bool is_strict_saop (ScalarArrayOpExpr *expr, bool falseOK)
 
static bool convert_saop_to_hashed_saop_walker (Node *node, void *context)
 
static Nodeeval_const_expressions_mutator (Node *node, eval_const_expressions_context *context)
 
static bool contain_non_const_walker (Node *node, void *context)
 
static bool ece_function_is_safe (Oid funcid, eval_const_expressions_context *context)
 
static Listsimplify_or_arguments (List *args, eval_const_expressions_context *context, bool *haveNull, bool *forceTrue)
 
static Listsimplify_and_arguments (List *args, eval_const_expressions_context *context, bool *haveNull, bool *forceFalse)
 
static Nodesimplify_boolean_equality (Oid opno, List *args)
 
static Exprsimplify_function (Oid funcid, Oid result_type, int32 result_typmod, Oid result_collid, Oid input_collid, List **args_p, bool funcvariadic, bool process_args, bool allow_non_const, eval_const_expressions_context *context)
 
static Listreorder_function_arguments (List *args, int pronargs, HeapTuple func_tuple)
 
static Listadd_function_defaults (List *args, int pronargs, HeapTuple func_tuple)
 
static Listfetch_function_defaults (HeapTuple func_tuple)
 
static void recheck_cast_function_args (List *args, Oid result_type, Oid *proargtypes, int pronargs, HeapTuple func_tuple)
 
static Exprevaluate_function (Oid funcid, Oid result_type, int32 result_typmod, Oid result_collid, Oid input_collid, List *args, bool funcvariadic, HeapTuple func_tuple, eval_const_expressions_context *context)
 
static Exprinline_function (Oid funcid, Oid result_type, Oid result_collid, Oid input_collid, List *args, bool funcvariadic, HeapTuple func_tuple, eval_const_expressions_context *context)
 
static Nodesubstitute_actual_parameters (Node *expr, int nargs, List *args, int *usecounts)
 
static Nodesubstitute_actual_parameters_mutator (Node *node, substitute_actual_parameters_context *context)
 
static void sql_inline_error_callback (void *arg)
 
static Querysubstitute_actual_srf_parameters (Query *expr, int nargs, List *args)
 
static Nodesubstitute_actual_srf_parameters_mutator (Node *node, substitute_actual_srf_parameters_context *context)
 
static bool pull_paramids_walker (Node *node, Bitmapset **context)
 
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)
 
bool contain_mutable_functions (Node *clause)
 
static bool contain_mutable_functions_checker (Oid func_id, void *context)
 
bool contain_volatile_functions (Node *clause)
 
static bool contain_volatile_functions_checker (Oid func_id, void *context)
 
bool contain_volatile_functions_not_nextval (Node *clause)
 
static bool contain_volatile_functions_not_nextval_checker (Oid func_id, void *context)
 
char max_parallel_hazard (Query *parse)
 
bool is_parallel_safe (PlannerInfo *root, Node *node)
 
static bool max_parallel_hazard_test (char proparallel, max_parallel_hazard_context *context)
 
static bool max_parallel_hazard_checker (Oid func_id, void *context)
 
bool contain_nonstrict_functions (Node *clause)
 
static bool contain_nonstrict_functions_checker (Oid func_id, void *context)
 
bool contain_exec_param (Node *clause, List *param_ids)
 
bool contain_leaked_vars (Node *clause)
 
static bool contain_leaked_vars_checker (Oid func_id, void *context)
 
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)
 
static bool rowtype_field_matches (Oid rowtypeid, int fieldnum, Oid expectedtype, int32 expectedtypmod, Oid expectedcollation)
 
Nodeeval_const_expressions (PlannerInfo *root, Node *node)
 
void convert_saop_to_hashed_saop (Node *node)
 
Nodeestimate_expression_value (PlannerInfo *root, Node *node)
 
Listexpand_function_arguments (List *args, bool include_out_arguments, Oid result_type, HeapTuple func_tuple)
 
Exprevaluate_expr (Expr *expr, Oid result_type, int32 result_typmod, Oid result_collation)
 
Queryinline_set_returning_function (PlannerInfo *root, RangeTblEntry *rte)
 
Bitmapsetpull_paramids (Expr *expr)
 

Macro Definition Documentation

◆ CCDN_CASETESTEXPR_OK

#define CCDN_CASETESTEXPR_OK   0x0001 /* CaseTestExpr okay here? */

Definition at line 1067 of file clauses.c.

◆ ece_all_arguments_const

#define ece_all_arguments_const (   node)     (!expression_tree_walker((Node *) (node), contain_non_const_walker, NULL))

Definition at line 2304 of file clauses.c.

◆ ece_evaluate_expr

#define ece_evaluate_expr (   node)
Value:
((Node *) evaluate_expr((Expr *) (node), \
exprType((Node *) (node)), \
exprTypmod((Node *) (node)), \
exprCollation((Node *) (node))))
Expr * evaluate_expr(Expr *expr, Oid result_type, int32 result_typmod, Oid result_collation)
Definition: clauses.c:4807
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:43
Definition: nodes.h:118

Definition at line 2308 of file clauses.c.

◆ ece_generic_processing

#define ece_generic_processing (   node)
Value:
(void *) context)
static Node * eval_const_expressions_mutator(Node *node, eval_const_expressions_context *context)
Definition: clauses.c:2318
#define expression_tree_mutator(n, m, c)
Definition: nodeFuncs.h:153

Definition at line 2295 of file clauses.c.

◆ MIN_ARRAY_SIZE_FOR_HASHED_SAOP

#define MIN_ARRAY_SIZE_FOR_HASHED_SAOP   9

Definition at line 2147 of file clauses.c.

Function Documentation

◆ add_function_defaults()

static List * add_function_defaults ( List args,
int  pronargs,
HeapTuple  func_tuple 
)
static

Definition at line 4150 of file clauses.c.

4151 {
4152  int nargsprovided = list_length(args);
4153  List *defaults;
4154  int ndelete;
4155 
4156  /* Get all the default expressions from the pg_proc tuple */
4157  defaults = fetch_function_defaults(func_tuple);
4158 
4159  /* Delete any unused defaults from the list */
4160  ndelete = nargsprovided + list_length(defaults) - pronargs;
4161  if (ndelete < 0)
4162  elog(ERROR, "not enough default arguments");
4163  if (ndelete > 0)
4164  defaults = list_delete_first_n(defaults, ndelete);
4165 
4166  /* And form the combined argument list, not modifying the input list */
4167  return list_concat_copy(args, defaults);
4168 }
static List * fetch_function_defaults(HeapTuple func_tuple)
Definition: clauses.c:4174
#define ERROR
Definition: elog.h:35
List * list_delete_first_n(List *list, int n)
Definition: list.c:982
List * list_concat_copy(const List *list1, const List *list2)
Definition: list.c:597
static int list_length(const List *l)
Definition: pg_list.h:150
int16 pronargs
Definition: pg_proc.h:81
Definition: pg_list.h:52

References generate_unaccent_rules::args, elog(), ERROR, fetch_function_defaults(), list_concat_copy(), list_delete_first_n(), list_length(), and pronargs.

Referenced by expand_function_arguments().

◆ CommuteOpExpr()

void CommuteOpExpr ( OpExpr clause)

Definition at line 2025 of file clauses.c.

2026 {
2027  Oid opoid;
2028  Node *temp;
2029 
2030  /* Sanity checks: caller is at fault if these fail */
2031  if (!is_opclause(clause) ||
2032  list_length(clause->args) != 2)
2033  elog(ERROR, "cannot commute non-binary-operator clause");
2034 
2035  opoid = get_commutator(clause->opno);
2036 
2037  if (!OidIsValid(opoid))
2038  elog(ERROR, "could not find commutator for operator %u",
2039  clause->opno);
2040 
2041  /*
2042  * modify the clause in-place!
2043  */
2044  clause->opno = opoid;
2045  clause->opfuncid = InvalidOid;
2046  /* opresulttype, opretset, opcollid, inputcollid need not change */
2047 
2048  temp = linitial(clause->args);
2049  linitial(clause->args) = lsecond(clause->args);
2050  lsecond(clause->args) = temp;
2051 }
#define OidIsValid(objectId)
Definition: c.h:711
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1491
static bool is_opclause(const void *clause)
Definition: nodeFuncs.h:74
#define linitial(l)
Definition: pg_list.h:176
#define lsecond(l)
Definition: pg_list.h:181
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
Oid opno
Definition: primnodes.h:648
List * args
Definition: primnodes.h:666

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

◆ contain_agg_clause_walker()

static bool contain_agg_clause_walker ( Node node,
void *  context 
)
static

Definition at line 183 of file clauses.c.

184 {
185  if (node == NULL)
186  return false;
187  if (IsA(node, Aggref))
188  {
189  Assert(((Aggref *) node)->agglevelsup == 0);
190  return true; /* abort the tree traversal and return true */
191  }
192  if (IsA(node, GroupingFunc))
193  {
194  Assert(((GroupingFunc *) node)->agglevelsup == 0);
195  return true; /* abort the tree traversal and return true */
196  }
197  Assert(!IsA(node, SubLink));
198  return expression_tree_walker(node, contain_agg_clause_walker, context);
199 }
Assert(fmt[strlen(fmt) - 1] !='\n')
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
#define IsA(nodeptr, _type_)
Definition: nodes.h:168

References Assert(), expression_tree_walker, and IsA.

Referenced by contain_agg_clause().

◆ contain_context_dependent_node()

static bool contain_context_dependent_node ( Node clause)
static

Definition at line 1060 of file clauses.c.

1061 {
1062  int flags = 0;
1063 
1064  return contain_context_dependent_node_walker(clause, &flags);
1065 }
static bool contain_context_dependent_node_walker(Node *node, int *flags)
Definition: clauses.c:1070

References contain_context_dependent_node_walker().

Referenced by inline_function().

◆ contain_context_dependent_node_walker()

static bool contain_context_dependent_node_walker ( Node node,
int *  flags 
)
static

Definition at line 1070 of file clauses.c.

1071 {
1072  if (node == NULL)
1073  return false;
1074  if (IsA(node, CaseTestExpr))
1075  return !(*flags & CCDN_CASETESTEXPR_OK);
1076  else if (IsA(node, CaseExpr))
1077  {
1078  CaseExpr *caseexpr = (CaseExpr *) node;
1079 
1080  /*
1081  * If this CASE doesn't have a test expression, then it doesn't create
1082  * a context in which CaseTestExprs should appear, so just fall
1083  * through and treat it as a generic expression node.
1084  */
1085  if (caseexpr->arg)
1086  {
1087  int save_flags = *flags;
1088  bool res;
1089 
1090  /*
1091  * Note: in principle, we could distinguish the various sub-parts
1092  * of a CASE construct and set the flag bit only for some of them,
1093  * since we are only expecting CaseTestExprs to appear in the
1094  * "expr" subtree of the CaseWhen nodes. But it doesn't really
1095  * seem worth any extra code. If there are any bare CaseTestExprs
1096  * elsewhere in the CASE, something's wrong already.
1097  */
1098  *flags |= CCDN_CASETESTEXPR_OK;
1099  res = expression_tree_walker(node,
1101  (void *) flags);
1102  *flags = save_flags;
1103  return res;
1104  }
1105  }
1106  else if (IsA(node, ArrayCoerceExpr))
1107  {
1108  ArrayCoerceExpr *ac = (ArrayCoerceExpr *) node;
1109  int save_flags;
1110  bool res;
1111 
1112  /* Check the array expression */
1113  if (contain_context_dependent_node_walker((Node *) ac->arg, flags))
1114  return true;
1115 
1116  /* Check the elemexpr, which is allowed to contain CaseTestExpr */
1117  save_flags = *flags;
1118  *flags |= CCDN_CASETESTEXPR_OK;
1120  flags);
1121  *flags = save_flags;
1122  return res;
1123  }
1125  (void *) flags);
1126 }
#define CCDN_CASETESTEXPR_OK
Definition: clauses.c:1067
Expr * arg
Definition: primnodes.h:1119

References ArrayCoerceExpr::arg, CaseExpr::arg, CCDN_CASETESTEXPR_OK, ArrayCoerceExpr::elemexpr, expression_tree_walker, IsA, and res.

Referenced by contain_context_dependent_node().

◆ contain_exec_param()

bool contain_exec_param ( Node clause,
List param_ids 
)

Definition at line 1018 of file clauses.c.

1019 {
1020  return contain_exec_param_walker(clause, param_ids);
1021 }
static bool contain_exec_param_walker(Node *node, List *param_ids)
Definition: clauses.c:1024

References contain_exec_param_walker().

Referenced by test_opexpr_is_hashable().

◆ contain_exec_param_walker()

static bool contain_exec_param_walker ( Node node,
List param_ids 
)
static

Definition at line 1024 of file clauses.c.

1025 {
1026  if (node == NULL)
1027  return false;
1028  if (IsA(node, Param))
1029  {
1030  Param *p = (Param *) node;
1031 
1032  if (p->paramkind == PARAM_EXEC &&
1033  list_member_int(param_ids, p->paramid))
1034  return true;
1035  }
1036  return expression_tree_walker(node, contain_exec_param_walker, param_ids);
1037 }
bool list_member_int(const List *list, int datum)
Definition: list.c:701
@ PARAM_EXEC
Definition: primnodes.h:303
int paramid
Definition: primnodes.h:312
ParamKind paramkind
Definition: primnodes.h:311

References expression_tree_walker, IsA, list_member_int(), PARAM_EXEC, Param::paramid, and Param::paramkind.

Referenced by contain_exec_param().

◆ contain_leaked_vars()

bool contain_leaked_vars ( Node clause)

Definition at line 1144 of file clauses.c.

1145 {
1146  return contain_leaked_vars_walker(clause, NULL);
1147 }
static bool contain_leaked_vars_walker(Node *node, void *context)
Definition: clauses.c:1156

References contain_leaked_vars_walker().

Referenced by make_restrictinfo_internal(), and qual_is_pushdown_safe().

◆ contain_leaked_vars_checker()

static bool contain_leaked_vars_checker ( Oid  func_id,
void *  context 
)
static

Definition at line 1150 of file clauses.c.

1151 {
1152  return !get_func_leakproof(func_id);
1153 }
bool get_func_leakproof(Oid funcid)
Definition: lsyscache.c:1819

References get_func_leakproof().

Referenced by contain_leaked_vars_walker().

◆ contain_leaked_vars_walker()

static bool contain_leaked_vars_walker ( Node node,
void *  context 
)
static

Definition at line 1156 of file clauses.c.

1157 {
1158  if (node == NULL)
1159  return false;
1160 
1161  switch (nodeTag(node))
1162  {
1163  case T_Var:
1164  case T_Const:
1165  case T_Param:
1166  case T_ArrayExpr:
1167  case T_FieldSelect:
1168  case T_FieldStore:
1169  case T_NamedArgExpr:
1170  case T_BoolExpr:
1171  case T_RelabelType:
1172  case T_CollateExpr:
1173  case T_CaseExpr:
1174  case T_CaseTestExpr:
1175  case T_RowExpr:
1176  case T_NullTest:
1177  case T_BooleanTest:
1178  case T_NextValueExpr:
1179  case T_List:
1180 
1181  /*
1182  * We know these node types don't contain function calls; but
1183  * something further down in the node tree might.
1184  */
1185  break;
1186 
1187  case T_FuncExpr:
1188  case T_OpExpr:
1189  case T_DistinctExpr:
1190  case T_NullIfExpr:
1191  case T_ScalarArrayOpExpr:
1192  case T_CoerceViaIO:
1193  case T_ArrayCoerceExpr:
1194 
1195  /*
1196  * If node contains a leaky function call, and there's any Var
1197  * underneath it, reject.
1198  */
1200  context) &&
1201  contain_var_clause(node))
1202  return true;
1203  break;
1204 
1205  case T_SubscriptingRef:
1206  {
1207  SubscriptingRef *sbsref = (SubscriptingRef *) node;
1208  const SubscriptRoutines *sbsroutines;
1209 
1210  /* Consult the subscripting support method info */
1211  sbsroutines = getSubscriptingRoutines(sbsref->refcontainertype,
1212  NULL);
1213  if (!sbsroutines ||
1214  !(sbsref->refassgnexpr != NULL ?
1215  sbsroutines->store_leakproof :
1216  sbsroutines->fetch_leakproof))
1217  {
1218  /* Node is leaky, so reject if it contains Vars */
1219  if (contain_var_clause(node))
1220  return true;
1221  }
1222  }
1223  break;
1224 
1225  case T_RowCompareExpr:
1226  {
1227  /*
1228  * It's worth special-casing this because a leaky comparison
1229  * function only compromises one pair of row elements, which
1230  * might not contain Vars while others do.
1231  */
1232  RowCompareExpr *rcexpr = (RowCompareExpr *) node;
1233  ListCell *opid;
1234  ListCell *larg;
1235  ListCell *rarg;
1236 
1237  forthree(opid, rcexpr->opnos,
1238  larg, rcexpr->largs,
1239  rarg, rcexpr->rargs)
1240  {
1241  Oid funcid = get_opcode(lfirst_oid(opid));
1242 
1243  if (!get_func_leakproof(funcid) &&
1244  (contain_var_clause((Node *) lfirst(larg)) ||
1245  contain_var_clause((Node *) lfirst(rarg))))
1246  return true;
1247  }
1248  }
1249  break;
1250 
1251  case T_MinMaxExpr:
1252  {
1253  /*
1254  * MinMaxExpr is leakproof if the comparison function it calls
1255  * is leakproof.
1256  */
1257  MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
1258  TypeCacheEntry *typentry;
1259  bool leakproof;
1260 
1261  /* Look up the btree comparison function for the datatype */
1262  typentry = lookup_type_cache(minmaxexpr->minmaxtype,
1264  if (OidIsValid(typentry->cmp_proc))
1265  leakproof = get_func_leakproof(typentry->cmp_proc);
1266  else
1267  {
1268  /*
1269  * The executor will throw an error, but here we just
1270  * treat the missing function as leaky.
1271  */
1272  leakproof = false;
1273  }
1274 
1275  if (!leakproof &&
1276  contain_var_clause((Node *) minmaxexpr->args))
1277  return true;
1278  }
1279  break;
1280 
1281  case T_CurrentOfExpr:
1282 
1283  /*
1284  * WHERE CURRENT OF doesn't contain leaky function calls.
1285  * Moreover, it is essential that this is considered non-leaky,
1286  * since the planner must always generate a TID scan when CURRENT
1287  * OF is present -- cf. cost_tidscan.
1288  */
1289  return false;
1290 
1291  default:
1292 
1293  /*
1294  * If we don't recognize the node tag, assume it might be leaky.
1295  * This prevents an unexpected security hole if someone adds a new
1296  * node type that can call a function.
1297  */
1298  return true;
1299  }
1301  context);
1302 }
static bool contain_leaked_vars_checker(Oid func_id, void *context)
Definition: clauses.c:1150
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1267
const struct SubscriptRoutines * getSubscriptingRoutines(Oid typid, Oid *typelemp)
Definition: lsyscache.c:3088
bool check_functions_in_node(Node *node, check_function_callback checker, void *context)
Definition: nodeFuncs.c:1710
#define nodeTag(nodeptr)
Definition: nodes.h:122
#define lfirst(lc)
Definition: pg_list.h:170
#define forthree(cell1, list1, cell2, list2, cell3, list3)
Definition: pg_list.h:510
#define lfirst_oid(lc)
Definition: pg_list.h:172
List * args
Definition: primnodes.h:1291
Oid minmaxtype
Definition: primnodes.h:1287
Expr * refassgnexpr
Definition: primnodes.h:554
Oid refcontainertype
Definition: primnodes.h:542
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:339
#define TYPECACHE_CMP_PROC
Definition: typcache.h:139
bool contain_var_clause(Node *node)
Definition: var.c:393

References MinMaxExpr::args, check_functions_in_node(), TypeCacheEntry::cmp_proc, contain_leaked_vars_checker(), contain_var_clause(), expression_tree_walker, SubscriptRoutines::fetch_leakproof, forthree, get_func_leakproof(), get_opcode(), getSubscriptingRoutines(), RowCompareExpr::largs, lfirst, lfirst_oid, lookup_type_cache(), MinMaxExpr::minmaxtype, nodeTag, OidIsValid, RowCompareExpr::opnos, RowCompareExpr::rargs, SubscriptingRef::refassgnexpr, SubscriptingRef::refcontainertype, SubscriptRoutines::store_leakproof, and TYPECACHE_CMP_PROC.

Referenced by contain_leaked_vars().

◆ contain_mutable_functions()

◆ contain_mutable_functions_checker()

static bool contain_mutable_functions_checker ( Oid  func_id,
void *  context 
)
static

Definition at line 371 of file clauses.c.

372 {
373  return (func_volatile(func_id) != PROVOLATILE_IMMUTABLE);
374 }
char func_volatile(Oid funcid)
Definition: lsyscache.c:1762

References func_volatile().

Referenced by contain_mutable_functions_walker().

◆ contain_mutable_functions_walker()

static bool contain_mutable_functions_walker ( Node node,
void *  context 
)
static

Definition at line 377 of file clauses.c.

378 {
379  if (node == NULL)
380  return false;
381  /* Check for mutable functions in node itself */
383  context))
384  return true;
385 
386  if (IsA(node, NextValueExpr))
387  {
388  /* NextValueExpr is volatile */
389  return true;
390  }
391 
392  /*
393  * It should be safe to treat MinMaxExpr as immutable, because it will
394  * depend on a non-cross-type btree comparison function, and those should
395  * always be immutable. Treating XmlExpr as immutable is more dubious,
396  * and treating CoerceToDomain as immutable is outright dangerous. But we
397  * have done so historically, and changing this would probably cause more
398  * problems than it would fix. In practice, if you have a non-immutable
399  * domain constraint you are in for pain anyhow.
400  */
401 
402  /* Recurse to check arguments */
403  if (IsA(node, Query))
404  {
405  /* Recurse into subselects */
406  return query_tree_walker((Query *) node,
408  context, 0);
409  }
411  context);
412 }
static bool contain_mutable_functions_checker(Oid func_id, void *context)
Definition: clauses.c:371
#define query_tree_walker(q, w, c, f)
Definition: nodeFuncs.h:156

References check_functions_in_node(), contain_mutable_functions_checker(), expression_tree_walker, IsA, and query_tree_walker.

Referenced by contain_mutable_functions().

◆ contain_non_const_walker()

static bool contain_non_const_walker ( Node node,
void *  context 
)
static

Definition at line 3560 of file clauses.c.

3561 {
3562  if (node == NULL)
3563  return false;
3564  if (IsA(node, Const))
3565  return false;
3566  if (IsA(node, List))
3567  return expression_tree_walker(node, contain_non_const_walker, context);
3568  /* Otherwise, abort the tree traversal and return true */
3569  return true;
3570 }
static bool contain_non_const_walker(Node *node, void *context)
Definition: clauses.c:3560

References expression_tree_walker, and IsA.

◆ contain_nonstrict_functions()

bool contain_nonstrict_functions ( Node clause)

Definition at line 874 of file clauses.c.

875 {
876  return contain_nonstrict_functions_walker(clause, NULL);
877 }
static bool contain_nonstrict_functions_walker(Node *node, void *context)
Definition: clauses.c:886

References contain_nonstrict_functions_walker().

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

◆ contain_nonstrict_functions_checker()

static bool contain_nonstrict_functions_checker ( Oid  func_id,
void *  context 
)
static

Definition at line 880 of file clauses.c.

881 {
882  return !func_strict(func_id);
883 }
bool func_strict(Oid funcid)
Definition: lsyscache.c:1743

References func_strict().

Referenced by contain_nonstrict_functions_walker().

◆ contain_nonstrict_functions_walker()

static bool contain_nonstrict_functions_walker ( Node node,
void *  context 
)
static

Definition at line 886 of file clauses.c.

887 {
888  if (node == NULL)
889  return false;
890  if (IsA(node, Aggref))
891  {
892  /* an aggregate could return non-null with null input */
893  return true;
894  }
895  if (IsA(node, GroupingFunc))
896  {
897  /*
898  * A GroupingFunc doesn't evaluate its arguments, and therefore must
899  * be treated as nonstrict.
900  */
901  return true;
902  }
903  if (IsA(node, WindowFunc))
904  {
905  /* a window function could return non-null with null input */
906  return true;
907  }
908  if (IsA(node, SubscriptingRef))
909  {
910  SubscriptingRef *sbsref = (SubscriptingRef *) node;
911  const SubscriptRoutines *sbsroutines;
912 
913  /* Subscripting assignment is always presumed nonstrict */
914  if (sbsref->refassgnexpr != NULL)
915  return true;
916  /* Otherwise we must look up the subscripting support methods */
917  sbsroutines = getSubscriptingRoutines(sbsref->refcontainertype, NULL);
918  if (!(sbsroutines && sbsroutines->fetch_strict))
919  return true;
920  /* else fall through to check args */
921  }
922  if (IsA(node, DistinctExpr))
923  {
924  /* IS DISTINCT FROM is inherently non-strict */
925  return true;
926  }
927  if (IsA(node, NullIfExpr))
928  {
929  /* NULLIF is inherently non-strict */
930  return true;
931  }
932  if (IsA(node, BoolExpr))
933  {
934  BoolExpr *expr = (BoolExpr *) node;
935 
936  switch (expr->boolop)
937  {
938  case AND_EXPR:
939  case OR_EXPR:
940  /* AND, OR are inherently non-strict */
941  return true;
942  default:
943  break;
944  }
945  }
946  if (IsA(node, SubLink))
947  {
948  /* In some cases a sublink might be strict, but in general not */
949  return true;
950  }
951  if (IsA(node, SubPlan))
952  return true;
953  if (IsA(node, AlternativeSubPlan))
954  return true;
955  if (IsA(node, FieldStore))
956  return true;
957  if (IsA(node, CoerceViaIO))
958  {
959  /*
960  * CoerceViaIO is strict regardless of whether the I/O functions are,
961  * so just go look at its argument; asking check_functions_in_node is
962  * useless expense and could deliver the wrong answer.
963  */
965  context);
966  }
967  if (IsA(node, ArrayCoerceExpr))
968  {
969  /*
970  * ArrayCoerceExpr is strict at the array level, regardless of what
971  * the per-element expression is; so we should ignore elemexpr and
972  * recurse only into the arg.
973  */
975  context);
976  }
977  if (IsA(node, CaseExpr))
978  return true;
979  if (IsA(node, ArrayExpr))
980  return true;
981  if (IsA(node, RowExpr))
982  return true;
983  if (IsA(node, RowCompareExpr))
984  return true;
985  if (IsA(node, CoalesceExpr))
986  return true;
987  if (IsA(node, MinMaxExpr))
988  return true;
989  if (IsA(node, XmlExpr))
990  return true;
991  if (IsA(node, NullTest))
992  return true;
993  if (IsA(node, BooleanTest))
994  return true;
995 
996  /* Check other function-containing nodes */
998  context))
999  return true;
1000 
1002  context);
1003 }
static bool contain_nonstrict_functions_checker(Oid func_id, void *context)
Definition: clauses.c:880
void * arg
@ AND_EXPR
Definition: primnodes.h:758
@ OR_EXPR
Definition: primnodes.h:758
BoolExprType boolop
Definition: primnodes.h:766

References AND_EXPR, arg, BoolExpr::boolop, check_functions_in_node(), contain_nonstrict_functions_checker(), expression_tree_walker, SubscriptRoutines::fetch_strict, getSubscriptingRoutines(), IsA, OR_EXPR, SubscriptingRef::refassgnexpr, and SubscriptingRef::refcontainertype.

Referenced by contain_nonstrict_functions().

◆ contain_subplans()

bool contain_subplans ( Node clause)

Definition at line 330 of file clauses.c.

331 {
332  return contain_subplans_walker(clause, NULL);
333 }
static bool contain_subplans_walker(Node *node, void *context)
Definition: clauses.c:336

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

static bool contain_subplans_walker ( Node node,
void *  context 
)
static

Definition at line 336 of file clauses.c.

337 {
338  if (node == NULL)
339  return false;
340  if (IsA(node, SubPlan) ||
341  IsA(node, AlternativeSubPlan) ||
342  IsA(node, SubLink))
343  return true; /* abort the tree traversal and return true */
344  return expression_tree_walker(node, contain_subplans_walker, context);
345 }

References expression_tree_walker, and IsA.

Referenced by contain_subplans().

◆ contain_volatile_functions()

◆ contain_volatile_functions_checker()

static bool contain_volatile_functions_checker ( Oid  func_id,
void *  context 
)
static

Definition at line 454 of file clauses.c.

455 {
456  return (func_volatile(func_id) == PROVOLATILE_VOLATILE);
457 }

References func_volatile().

Referenced by contain_volatile_functions_walker().

◆ contain_volatile_functions_not_nextval()

bool contain_volatile_functions_not_nextval ( Node clause)

Definition at line 555 of file clauses.c.

556 {
558 }
static bool contain_volatile_functions_not_nextval_walker(Node *node, void *context)
Definition: clauses.c:568

References contain_volatile_functions_not_nextval_walker().

Referenced by BeginCopyFrom().

◆ contain_volatile_functions_not_nextval_checker()

static bool contain_volatile_functions_not_nextval_checker ( Oid  func_id,
void *  context 
)
static

Definition at line 561 of file clauses.c.

562 {
563  return (func_id != F_NEXTVAL &&
564  func_volatile(func_id) == PROVOLATILE_VOLATILE);
565 }

References func_volatile().

Referenced by contain_volatile_functions_not_nextval_walker().

◆ contain_volatile_functions_not_nextval_walker()

static bool contain_volatile_functions_not_nextval_walker ( Node node,
void *  context 
)
static

Definition at line 568 of file clauses.c.

569 {
570  if (node == NULL)
571  return false;
572  /* Check for volatile functions in node itself */
573  if (check_functions_in_node(node,
575  context))
576  return true;
577 
578  /*
579  * See notes in contain_mutable_functions_walker about why we treat
580  * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable. Hence, none of
581  * them are of interest here. Also, since we're intentionally ignoring
582  * nextval(), presumably we should ignore NextValueExpr.
583  */
584 
585  /* Recurse to check arguments */
586  if (IsA(node, Query))
587  {
588  /* Recurse into subselects */
589  return query_tree_walker((Query *) node,
591  context, 0);
592  }
593  return expression_tree_walker(node,
595  context);
596 }
static bool contain_volatile_functions_not_nextval_checker(Oid func_id, void *context)
Definition: clauses.c:561

References check_functions_in_node(), contain_volatile_functions_not_nextval_checker(), expression_tree_walker, IsA, and query_tree_walker.

Referenced by contain_volatile_functions_not_nextval().

◆ contain_volatile_functions_walker()

static bool contain_volatile_functions_walker ( Node node,
void *  context 
)
static

Definition at line 460 of file clauses.c.

461 {
462  if (node == NULL)
463  return false;
464  /* Check for volatile functions in node itself */
466  context))
467  return true;
468 
469  if (IsA(node, NextValueExpr))
470  {
471  /* NextValueExpr is volatile */
472  return true;
473  }
474 
475  if (IsA(node, RestrictInfo))
476  {
477  RestrictInfo *rinfo = (RestrictInfo *) node;
478 
479  /*
480  * For RestrictInfo, check if we've checked the volatility of it
481  * before. If so, we can just use the cached value and not bother
482  * checking it again. Otherwise, check it and cache if whether we
483  * found any volatile functions.
484  */
485  if (rinfo->has_volatile == VOLATILITY_NOVOLATILE)
486  return false;
487  else if (rinfo->has_volatile == VOLATILITY_VOLATILE)
488  return true;
489  else
490  {
491  bool hasvolatile;
492 
493  hasvolatile = contain_volatile_functions_walker((Node *) rinfo->clause,
494  context);
495  if (hasvolatile)
496  rinfo->has_volatile = VOLATILITY_VOLATILE;
497  else
498  rinfo->has_volatile = VOLATILITY_NOVOLATILE;
499 
500  return hasvolatile;
501  }
502  }
503 
504  if (IsA(node, PathTarget))
505  {
506  PathTarget *target = (PathTarget *) node;
507 
508  /*
509  * We also do caching for PathTarget the same as we do above for
510  * RestrictInfos.
511  */
513  return false;
514  else if (target->has_volatile_expr == VOLATILITY_VOLATILE)
515  return true;
516  else
517  {
518  bool hasvolatile;
519 
520  hasvolatile = contain_volatile_functions_walker((Node *) target->exprs,
521  context);
522 
523  if (hasvolatile)
525  else
527 
528  return hasvolatile;
529  }
530  }
531 
532  /*
533  * See notes in contain_mutable_functions_walker about why we treat
534  * MinMaxExpr, XmlExpr, and CoerceToDomain as immutable. Hence, none of
535  * them are of interest here.
536  */
537 
538  /* Recurse to check arguments */
539  if (IsA(node, Query))
540  {
541  /* Recurse into subselects */
542  return query_tree_walker((Query *) node,
544  context, 0);
545  }
547  context);
548 }
static bool contain_volatile_functions_checker(Oid func_id, void *context)
Definition: clauses.c:454
@ VOLATILITY_NOVOLATILE
Definition: pathnodes.h:1394
@ VOLATILITY_VOLATILE
Definition: pathnodes.h:1393
VolatileFunctionStatus has_volatile_expr
Definition: pathnodes.h:1439
List * exprs
Definition: pathnodes.h:1427
Expr * clause
Definition: pathnodes.h:2435

References check_functions_in_node(), RestrictInfo::clause, contain_volatile_functions_checker(), expression_tree_walker, PathTarget::exprs, PathTarget::has_volatile_expr, IsA, query_tree_walker, VOLATILITY_NOVOLATILE, and VOLATILITY_VOLATILE.

Referenced by contain_volatile_functions().

◆ 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:197

References contain_windowfuncs().

Referenced by get_eclass_for_sort_expr(), and qual_is_pushdown_safe().

◆ convert_saop_to_hashed_saop()

void convert_saop_to_hashed_saop ( Node node)

Definition at line 2165 of file clauses.c.

2166 {
2167  (void) convert_saop_to_hashed_saop_walker(node, NULL);
2168 }
static bool convert_saop_to_hashed_saop_walker(Node *node, void *context)
Definition: clauses.c:2171

References convert_saop_to_hashed_saop_walker().

Referenced by preprocess_expression().

◆ convert_saop_to_hashed_saop_walker()

static bool convert_saop_to_hashed_saop_walker ( Node node,
void *  context 
)
static

Definition at line 2171 of file clauses.c.

2172 {
2173  if (node == NULL)
2174  return false;
2175 
2176  if (IsA(node, ScalarArrayOpExpr))
2177  {
2178  ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) node;
2179  Expr *arrayarg = (Expr *) lsecond(saop->args);
2180  Oid lefthashfunc;
2181  Oid righthashfunc;
2182 
2183  if (arrayarg && IsA(arrayarg, Const) &&
2184  !((Const *) arrayarg)->constisnull)
2185  {
2186  if (saop->useOr)
2187  {
2188  if (get_op_hash_functions(saop->opno, &lefthashfunc, &righthashfunc) &&
2189  lefthashfunc == righthashfunc)
2190  {
2191  Datum arrdatum = ((Const *) arrayarg)->constvalue;
2192  ArrayType *arr = (ArrayType *) DatumGetPointer(arrdatum);
2193  int nitems;
2194 
2195  /*
2196  * Only fill in the hash functions if the array looks
2197  * large enough for it to be worth hashing instead of
2198  * doing a linear search.
2199  */
2200  nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
2201 
2202  if (nitems >= MIN_ARRAY_SIZE_FOR_HASHED_SAOP)
2203  {
2204  /* Looks good. Fill in the hash functions */
2205  saop->hashfuncid = lefthashfunc;
2206  }
2207  return true;
2208  }
2209  }
2210  else /* !saop->useOr */
2211  {
2212  Oid negator = get_negator(saop->opno);
2213 
2214  /*
2215  * Check if this is a NOT IN using an operator whose negator
2216  * is hashable. If so we can still build a hash table and
2217  * just ensure the lookup items are not in the hash table.
2218  */
2219  if (OidIsValid(negator) &&
2220  get_op_hash_functions(negator, &lefthashfunc, &righthashfunc) &&
2221  lefthashfunc == righthashfunc)
2222  {
2223  Datum arrdatum = ((Const *) arrayarg)->constvalue;
2224  ArrayType *arr = (ArrayType *) DatumGetPointer(arrdatum);
2225  int nitems;
2226 
2227  /*
2228  * Only fill in the hash functions if the array looks
2229  * large enough for it to be worth hashing instead of
2230  * doing a linear search.
2231  */
2232  nitems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));
2233 
2234  if (nitems >= MIN_ARRAY_SIZE_FOR_HASHED_SAOP)
2235  {
2236  /* Looks good. Fill in the hash functions */
2237  saop->hashfuncid = lefthashfunc;
2238 
2239  /*
2240  * Also set the negfuncid. The executor will need
2241  * that to perform hashtable lookups.
2242  */
2243  saop->negfuncid = get_opcode(negator);
2244  }
2245  return true;
2246  }
2247  }
2248  }
2249  }
2250 
2252 }
#define ARR_NDIM(a)
Definition: array.h:283
#define ARR_DIMS(a)
Definition: array.h:287
int ArrayGetNItems(int ndim, const int *dims)
Definition: arrayutils.c:76
#define MIN_ARRAY_SIZE_FOR_HASHED_SAOP
Definition: clauses.c:2147
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:509
Oid get_negator(Oid opno)
Definition: lsyscache.c:1515
uintptr_t Datum
Definition: postgres.h:412
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:660

References ScalarArrayOpExpr::args, ARR_DIMS, ARR_NDIM, ArrayGetNItems(), DatumGetPointer(), expression_tree_walker, get_negator(), get_op_hash_functions(), get_opcode(), IsA, lsecond, MIN_ARRAY_SIZE_FOR_HASHED_SAOP, OidIsValid, ScalarArrayOpExpr::opno, and ScalarArrayOpExpr::useOr.

Referenced by convert_saop_to_hashed_saop().

◆ ece_function_is_safe()

static bool ece_function_is_safe ( Oid  funcid,
eval_const_expressions_context context 
)
static

Definition at line 3576 of file clauses.c.

3577 {
3578  char provolatile = func_volatile(funcid);
3579 
3580  /*
3581  * Ordinarily we are only allowed to simplify immutable functions. But for
3582  * purposes of estimation, we consider it okay to simplify functions that
3583  * are merely stable; the risk that the result might change from planning
3584  * time to execution time is worth taking in preference to not being able
3585  * to estimate the value at all.
3586  */
3587  if (provolatile == PROVOLATILE_IMMUTABLE)
3588  return true;
3589  if (context->estimate && provolatile == PROVOLATILE_STABLE)
3590  return true;
3591  return false;
3592 }

References eval_const_expressions_context::estimate, and func_volatile().

Referenced by eval_const_expressions_mutator().

◆ estimate_expression_value()

Node* estimate_expression_value ( PlannerInfo root,
Node node 
)

Definition at line 2273 of file clauses.c.

2274 {
2276 
2277  context.boundParams = root->glob->boundParams; /* bound Params */
2278  /* we do not need to mark the plan as depending on inlined functions */
2279  context.root = NULL;
2280  context.active_fns = NIL; /* nothing being recursively simplified */
2281  context.case_val = NULL; /* no CASE being examined */
2282  context.estimate = true; /* unsafe transformations OK */
2283  return eval_const_expressions_mutator(node, &context);
2284 }
#define NIL
Definition: pg_list.h:66
PlannerGlobal * glob
Definition: pathnodes.h:205
ParamListInfo boundParams
Definition: clauses.c:61

References eval_const_expressions_context::active_fns, eval_const_expressions_context::boundParams, eval_const_expressions_context::case_val, eval_const_expressions_context::estimate, eval_const_expressions_mutator(), PlannerInfo::glob, NIL, and eval_const_expressions_context::root.

Referenced by array_unnest_support(), bernoulli_samplescangetsamplesize(), clause_selectivity_ext(), generate_series_int4_support(), generate_series_int8_support(), get_restriction_variable(), gincost_opexpr(), gincost_scalararrayopexpr(), preprocess_limit(), scalararraysel(), system_rows_samplescangetsamplesize(), system_samplescangetsamplesize(), and system_time_samplescangetsamplesize().

◆ eval_const_expressions()

◆ eval_const_expressions_mutator()

static Node * eval_const_expressions_mutator ( Node node,
eval_const_expressions_context context 
)
static

Definition at line 2318 of file clauses.c.

2320 {
2321  if (node == NULL)
2322  return NULL;
2323  switch (nodeTag(node))
2324  {
2325  case T_Param:
2326  {
2327  Param *param = (Param *) node;
2328  ParamListInfo paramLI = context->boundParams;
2329 
2330  /* Look to see if we've been given a value for this Param */
2331  if (param->paramkind == PARAM_EXTERN &&
2332  paramLI != NULL &&
2333  param->paramid > 0 &&
2334  param->paramid <= paramLI->numParams)
2335  {
2336  ParamExternData *prm;
2337  ParamExternData prmdata;
2338 
2339  /*
2340  * Give hook a chance in case parameter is dynamic. Tell
2341  * it that this fetch is speculative, so it should avoid
2342  * erroring out if parameter is unavailable.
2343  */
2344  if (paramLI->paramFetch != NULL)
2345  prm = paramLI->paramFetch(paramLI, param->paramid,
2346  true, &prmdata);
2347  else
2348  prm = &paramLI->params[param->paramid - 1];
2349 
2350  /*
2351  * We don't just check OidIsValid, but insist that the
2352  * fetched type match the Param, just in case the hook did
2353  * something unexpected. No need to throw an error here
2354  * though; leave that for runtime.
2355  */
2356  if (OidIsValid(prm->ptype) &&
2357  prm->ptype == param->paramtype)
2358  {
2359  /* OK to substitute parameter value? */
2360  if (context->estimate ||
2361  (prm->pflags & PARAM_FLAG_CONST))
2362  {
2363  /*
2364  * Return a Const representing the param value.
2365  * Must copy pass-by-ref datatypes, since the
2366  * Param might be in a memory context
2367  * shorter-lived than our output plan should be.
2368  */
2369  int16 typLen;
2370  bool typByVal;
2371  Datum pval;
2372  Const *con;
2373 
2374  get_typlenbyval(param->paramtype,
2375  &typLen, &typByVal);
2376  if (prm->isnull || typByVal)
2377  pval = prm->value;
2378  else
2379  pval = datumCopy(prm->value, typByVal, typLen);
2380  con = makeConst(param->paramtype,
2381  param->paramtypmod,
2382  param->paramcollid,
2383  (int) typLen,
2384  pval,
2385  prm->isnull,
2386  typByVal);
2387  con->location = param->location;
2388  return (Node *) con;
2389  }
2390  }
2391  }
2392 
2393  /*
2394  * Not replaceable, so just copy the Param (no need to
2395  * recurse)
2396  */
2397  return (Node *) copyObject(param);
2398  }
2399  case T_WindowFunc:
2400  {
2401  WindowFunc *expr = (WindowFunc *) node;
2402  Oid funcid = expr->winfnoid;
2403  List *args;
2404  Expr *aggfilter;
2405  HeapTuple func_tuple;
2406  WindowFunc *newexpr;
2407 
2408  /*
2409  * We can't really simplify a WindowFunc node, but we mustn't
2410  * just fall through to the default processing, because we
2411  * have to apply expand_function_arguments to its argument
2412  * list. That takes care of inserting default arguments and
2413  * expanding named-argument notation.
2414  */
2415  func_tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
2416  if (!HeapTupleIsValid(func_tuple))
2417  elog(ERROR, "cache lookup failed for function %u", funcid);
2418 
2420  false, expr->wintype,
2421  func_tuple);
2422 
2423  ReleaseSysCache(func_tuple);
2424 
2425  /* Now, recursively simplify the args (which are a List) */
2426  args = (List *)
2429  (void *) context);
2430  /* ... and the filter expression, which isn't */
2431  aggfilter = (Expr *)
2433  context);
2434 
2435  /* And build the replacement WindowFunc node */
2436  newexpr = makeNode(WindowFunc);
2437  newexpr->winfnoid = expr->winfnoid;
2438  newexpr->wintype = expr->wintype;
2439  newexpr->wincollid = expr->wincollid;
2440  newexpr->inputcollid = expr->inputcollid;
2441  newexpr->args = args;
2442  newexpr->aggfilter = aggfilter;
2443  newexpr->winref = expr->winref;
2444  newexpr->winstar = expr->winstar;
2445  newexpr->winagg = expr->winagg;
2446  newexpr->location = expr->location;
2447 
2448  return (Node *) newexpr;
2449  }
2450  case T_FuncExpr:
2451  {
2452  FuncExpr *expr = (FuncExpr *) node;
2453  List *args = expr->args;
2454  Expr *simple;
2455  FuncExpr *newexpr;
2456 
2457  /*
2458  * Code for op/func reduction is pretty bulky, so split it out
2459  * as a separate function. Note: exprTypmod normally returns
2460  * -1 for a FuncExpr, but not when the node is recognizably a
2461  * length coercion; we want to preserve the typmod in the
2462  * eventual Const if so.
2463  */
2464  simple = simplify_function(expr->funcid,
2465  expr->funcresulttype,
2466  exprTypmod(node),
2467  expr->funccollid,
2468  expr->inputcollid,
2469  &args,
2470  expr->funcvariadic,
2471  true,
2472  true,
2473  context);
2474  if (simple) /* successfully simplified it */
2475  return (Node *) simple;
2476 
2477  /*
2478  * The expression cannot be simplified any further, so build
2479  * and return a replacement FuncExpr node using the
2480  * possibly-simplified arguments. Note that we have also
2481  * converted the argument list to positional notation.
2482  */
2483  newexpr = makeNode(FuncExpr);
2484  newexpr->funcid = expr->funcid;
2485  newexpr->funcresulttype = expr->funcresulttype;
2486  newexpr->funcretset = expr->funcretset;
2487  newexpr->funcvariadic = expr->funcvariadic;
2488  newexpr->funcformat = expr->funcformat;
2489  newexpr->funccollid = expr->funccollid;
2490  newexpr->inputcollid = expr->inputcollid;
2491  newexpr->args = args;
2492  newexpr->location = expr->location;
2493  return (Node *) newexpr;
2494  }
2495  case T_OpExpr:
2496  {
2497  OpExpr *expr = (OpExpr *) node;
2498  List *args = expr->args;
2499  Expr *simple;
2500  OpExpr *newexpr;
2501 
2502  /*
2503  * Need to get OID of underlying function. Okay to scribble
2504  * on input to this extent.
2505  */
2506  set_opfuncid(expr);
2507 
2508  /*
2509  * Code for op/func reduction is pretty bulky, so split it out
2510  * as a separate function.
2511  */
2512  simple = simplify_function(expr->opfuncid,
2513  expr->opresulttype, -1,
2514  expr->opcollid,
2515  expr->inputcollid,
2516  &args,
2517  false,
2518  true,
2519  true,
2520  context);
2521  if (simple) /* successfully simplified it */
2522  return (Node *) simple;
2523 
2524  /*
2525  * If the operator is boolean equality or inequality, we know
2526  * how to simplify cases involving one constant and one
2527  * non-constant argument.
2528  */
2529  if (expr->opno == BooleanEqualOperator ||
2530  expr->opno == BooleanNotEqualOperator)
2531  {
2532  simple = (Expr *) simplify_boolean_equality(expr->opno,
2533  args);
2534  if (simple) /* successfully simplified it */
2535  return (Node *) simple;
2536  }
2537 
2538  /*
2539  * The expression cannot be simplified any further, so build
2540  * and return a replacement OpExpr node using the
2541  * possibly-simplified arguments.
2542  */
2543  newexpr = makeNode(OpExpr);
2544  newexpr->opno = expr->opno;
2545  newexpr->opfuncid = expr->opfuncid;
2546  newexpr->opresulttype = expr->opresulttype;
2547  newexpr->opretset = expr->opretset;
2548  newexpr->opcollid = expr->opcollid;
2549  newexpr->inputcollid = expr->inputcollid;
2550  newexpr->args = args;
2551  newexpr->location = expr->location;
2552  return (Node *) newexpr;
2553  }
2554  case T_DistinctExpr:
2555  {
2556  DistinctExpr *expr = (DistinctExpr *) node;
2557  List *args;
2558  ListCell *arg;
2559  bool has_null_input = false;
2560  bool all_null_input = true;
2561  bool has_nonconst_input = false;
2562  Expr *simple;
2563  DistinctExpr *newexpr;
2564 
2565  /*
2566  * Reduce constants in the DistinctExpr's arguments. We know
2567  * args is either NIL or a List node, so we can call
2568  * expression_tree_mutator directly rather than recursing to
2569  * self.
2570  */
2571  args = (List *) expression_tree_mutator((Node *) expr->args,
2573  (void *) context);
2574 
2575  /*
2576  * We must do our own check for NULLs because DistinctExpr has
2577  * different results for NULL input than the underlying
2578  * operator does.
2579  */
2580  foreach(arg, args)
2581  {
2582  if (IsA(lfirst(arg), Const))
2583  {
2584  has_null_input |= ((Const *) lfirst(arg))->constisnull;
2585  all_null_input &= ((Const *) lfirst(arg))->constisnull;
2586  }
2587  else
2588  has_nonconst_input = true;
2589  }
2590 
2591  /* all constants? then can optimize this out */
2592  if (!has_nonconst_input)
2593  {
2594  /* all nulls? then not distinct */
2595  if (all_null_input)
2596  return makeBoolConst(false, false);
2597 
2598  /* one null? then distinct */
2599  if (has_null_input)
2600  return makeBoolConst(true, false);
2601 
2602  /* otherwise try to evaluate the '=' operator */
2603  /* (NOT okay to try to inline it, though!) */
2604 
2605  /*
2606  * Need to get OID of underlying function. Okay to
2607  * scribble on input to this extent.
2608  */
2609  set_opfuncid((OpExpr *) expr); /* rely on struct
2610  * equivalence */
2611 
2612  /*
2613  * Code for op/func reduction is pretty bulky, so split it
2614  * out as a separate function.
2615  */
2616  simple = simplify_function(expr->opfuncid,
2617  expr->opresulttype, -1,
2618  expr->opcollid,
2619  expr->inputcollid,
2620  &args,
2621  false,
2622  false,
2623  false,
2624  context);
2625  if (simple) /* successfully simplified it */
2626  {
2627  /*
2628  * Since the underlying operator is "=", must negate
2629  * its result
2630  */
2631  Const *csimple = castNode(Const, simple);
2632 
2633  csimple->constvalue =
2634  BoolGetDatum(!DatumGetBool(csimple->constvalue));
2635  return (Node *) csimple;
2636  }
2637  }
2638 
2639  /*
2640  * The expression cannot be simplified any further, so build
2641  * and return a replacement DistinctExpr node using the
2642  * possibly-simplified arguments.
2643  */
2644  newexpr = makeNode(DistinctExpr);
2645  newexpr->opno = expr->opno;
2646  newexpr->opfuncid = expr->opfuncid;
2647  newexpr->opresulttype = expr->opresulttype;
2648  newexpr->opretset = expr->opretset;
2649  newexpr->opcollid = expr->opcollid;
2650  newexpr->inputcollid = expr->inputcollid;
2651  newexpr->args = args;
2652  newexpr->location = expr->location;
2653  return (Node *) newexpr;
2654  }
2655  case T_NullIfExpr:
2656  {
2657  NullIfExpr *expr;
2658  ListCell *arg;
2659  bool has_nonconst_input = false;
2660 
2661  /* Copy the node and const-simplify its arguments */
2662  expr = (NullIfExpr *) ece_generic_processing(node);
2663 
2664  /* If either argument is NULL they can't be equal */
2665  foreach(arg, expr->args)
2666  {
2667  if (!IsA(lfirst(arg), Const))
2668  has_nonconst_input = true;
2669  else if (((Const *) lfirst(arg))->constisnull)
2670  return (Node *) linitial(expr->args);
2671  }
2672 
2673  /*
2674  * Need to get OID of underlying function before checking if
2675  * the function is OK to evaluate.
2676  */
2677  set_opfuncid((OpExpr *) expr);
2678 
2679  if (!has_nonconst_input &&
2680  ece_function_is_safe(expr->opfuncid, context))
2681  return ece_evaluate_expr(expr);
2682 
2683  return (Node *) expr;
2684  }
2685  case T_ScalarArrayOpExpr:
2686  {
2687  ScalarArrayOpExpr *saop;
2688 
2689  /* Copy the node and const-simplify its arguments */
2690  saop = (ScalarArrayOpExpr *) ece_generic_processing(node);
2691 
2692  /* Make sure we know underlying function */
2693  set_sa_opfuncid(saop);
2694 
2695  /*
2696  * If all arguments are Consts, and it's a safe function, we
2697  * can fold to a constant
2698  */
2699  if (ece_all_arguments_const(saop) &&
2700  ece_function_is_safe(saop->opfuncid, context))
2701  return ece_evaluate_expr(saop);
2702  return (Node *) saop;
2703  }
2704  case T_BoolExpr:
2705  {
2706  BoolExpr *expr = (BoolExpr *) node;
2707 
2708  switch (expr->boolop)
2709  {
2710  case OR_EXPR:
2711  {
2712  List *newargs;
2713  bool haveNull = false;
2714  bool forceTrue = false;
2715 
2716  newargs = simplify_or_arguments(expr->args,
2717  context,
2718  &haveNull,
2719  &forceTrue);
2720  if (forceTrue)
2721  return makeBoolConst(true, false);
2722  if (haveNull)
2723  newargs = lappend(newargs,
2724  makeBoolConst(false, true));
2725  /* If all the inputs are FALSE, result is FALSE */
2726  if (newargs == NIL)
2727  return makeBoolConst(false, false);
2728 
2729  /*
2730  * If only one nonconst-or-NULL input, it's the
2731  * result
2732  */
2733  if (list_length(newargs) == 1)
2734  return (Node *) linitial(newargs);
2735  /* Else we still need an OR node */
2736  return (Node *) make_orclause(newargs);
2737  }
2738  case AND_EXPR:
2739  {
2740  List *newargs;
2741  bool haveNull = false;
2742  bool forceFalse = false;
2743 
2744  newargs = simplify_and_arguments(expr->args,
2745  context,
2746  &haveNull,
2747  &forceFalse);
2748  if (forceFalse)
2749  return makeBoolConst(false, false);
2750  if (haveNull)
2751  newargs = lappend(newargs,
2752  makeBoolConst(false, true));
2753  /* If all the inputs are TRUE, result is TRUE */
2754  if (newargs == NIL)
2755  return makeBoolConst(true, false);
2756 
2757  /*
2758  * If only one nonconst-or-NULL input, it's the
2759  * result
2760  */
2761  if (list_length(newargs) == 1)
2762  return (Node *) linitial(newargs);
2763  /* Else we still need an AND node */
2764  return (Node *) make_andclause(newargs);
2765  }
2766  case NOT_EXPR:
2767  {
2768  Node *arg;
2769 
2770  Assert(list_length(expr->args) == 1);
2772  context);
2773 
2774  /*
2775  * Use negate_clause() to see if we can simplify
2776  * away the NOT.
2777  */
2778  return negate_clause(arg);
2779  }
2780  default:
2781  elog(ERROR, "unrecognized boolop: %d",
2782  (int) expr->boolop);
2783  break;
2784  }
2785  break;
2786  }
2787  case T_SubPlan:
2788  case T_AlternativeSubPlan:
2789 
2790  /*
2791  * Return a SubPlan unchanged --- too late to do anything with it.
2792  *
2793  * XXX should we ereport() here instead? Probably this routine
2794  * should never be invoked after SubPlan creation.
2795  */
2796  return node;
2797  case T_RelabelType:
2798  {
2799  RelabelType *relabel = (RelabelType *) node;
2800  Node *arg;
2801 
2802  /* Simplify the input ... */
2803  arg = eval_const_expressions_mutator((Node *) relabel->arg,
2804  context);
2805  /* ... and attach a new RelabelType node, if needed */
2806  return applyRelabelType(arg,
2807  relabel->resulttype,
2808  relabel->resulttypmod,
2809  relabel->resultcollid,
2810  relabel->relabelformat,
2811  relabel->location,
2812  true);
2813  }
2814  case T_CoerceViaIO:
2815  {
2816  CoerceViaIO *expr = (CoerceViaIO *) node;
2817  List *args;
2818  Oid outfunc;
2819  bool outtypisvarlena;
2820  Oid infunc;
2821  Oid intypioparam;
2822  Expr *simple;
2823  CoerceViaIO *newexpr;
2824 
2825  /* Make a List so we can use simplify_function */
2826  args = list_make1(expr->arg);
2827 
2828  /*
2829  * CoerceViaIO represents calling the source type's output
2830  * function then the result type's input function. So, try to
2831  * simplify it as though it were a stack of two such function
2832  * calls. First we need to know what the functions are.
2833  *
2834  * Note that the coercion functions are assumed not to care
2835  * about input collation, so we just pass InvalidOid for that.
2836  */
2837  getTypeOutputInfo(exprType((Node *) expr->arg),
2838  &outfunc, &outtypisvarlena);
2840  &infunc, &intypioparam);
2841 
2842  simple = simplify_function(outfunc,
2843  CSTRINGOID, -1,
2844  InvalidOid,
2845  InvalidOid,
2846  &args,
2847  false,
2848  true,
2849  true,
2850  context);
2851  if (simple) /* successfully simplified output fn */
2852  {
2853  /*
2854  * Input functions may want 1 to 3 arguments. We always
2855  * supply all three, trusting that nothing downstream will
2856  * complain.
2857  */
2858  args = list_make3(simple,
2859  makeConst(OIDOID,
2860  -1,
2861  InvalidOid,
2862  sizeof(Oid),
2863  ObjectIdGetDatum(intypioparam),
2864  false,
2865  true),
2866  makeConst(INT4OID,
2867  -1,
2868  InvalidOid,
2869  sizeof(int32),
2870  Int32GetDatum(-1),
2871  false,
2872  true));
2873 
2874  simple = simplify_function(infunc,
2875  expr->resulttype, -1,
2876  expr->resultcollid,
2877  InvalidOid,
2878  &args,
2879  false,
2880  false,
2881  true,
2882  context);
2883  if (simple) /* successfully simplified input fn */
2884  return (Node *) simple;
2885  }
2886 
2887  /*
2888  * The expression cannot be simplified any further, so build
2889  * and return a replacement CoerceViaIO node using the
2890  * possibly-simplified argument.
2891  */
2892  newexpr = makeNode(CoerceViaIO);
2893  newexpr->arg = (Expr *) linitial(args);
2894  newexpr->resulttype = expr->resulttype;
2895  newexpr->resultcollid = expr->resultcollid;
2896  newexpr->coerceformat = expr->coerceformat;
2897  newexpr->location = expr->location;
2898  return (Node *) newexpr;
2899  }
2900  case T_ArrayCoerceExpr:
2901  {
2903  Node *save_case_val;
2904 
2905  /*
2906  * Copy the node and const-simplify its arguments. We can't
2907  * use ece_generic_processing() here because we need to mess
2908  * with case_val only while processing the elemexpr.
2909  */
2910  memcpy(ac, node, sizeof(ArrayCoerceExpr));
2911  ac->arg = (Expr *)
2913  context);
2914 
2915  /*
2916  * Set up for the CaseTestExpr node contained in the elemexpr.
2917  * We must prevent it from absorbing any outer CASE value.
2918  */
2919  save_case_val = context->case_val;
2920  context->case_val = NULL;
2921 
2922  ac->elemexpr = (Expr *)
2924  context);
2925 
2926  context->case_val = save_case_val;
2927 
2928  /*
2929  * If constant argument and the per-element expression is
2930  * immutable, we can simplify the whole thing to a constant.
2931  * Exception: although contain_mutable_functions considers
2932  * CoerceToDomain immutable for historical reasons, let's not
2933  * do so here; this ensures coercion to an array-over-domain
2934  * does not apply the domain's constraints until runtime.
2935  */
2936  if (ac->arg && IsA(ac->arg, Const) &&
2937  ac->elemexpr && !IsA(ac->elemexpr, CoerceToDomain) &&
2939  return ece_evaluate_expr(ac);
2940 
2941  return (Node *) ac;
2942  }
2943  case T_CollateExpr:
2944  {
2945  /*
2946  * We replace CollateExpr with RelabelType, so as to improve
2947  * uniformity of expression representation and thus simplify
2948  * comparison of expressions. Hence this looks very nearly
2949  * the same as the RelabelType case, and we can apply the same
2950  * optimizations to avoid unnecessary RelabelTypes.
2951  */
2952  CollateExpr *collate = (CollateExpr *) node;
2953  Node *arg;
2954 
2955  /* Simplify the input ... */
2956  arg = eval_const_expressions_mutator((Node *) collate->arg,
2957  context);
2958  /* ... and attach a new RelabelType node, if needed */
2959  return applyRelabelType(arg,
2960  exprType(arg),
2961  exprTypmod(arg),
2962  collate->collOid,
2964  collate->location,
2965  true);
2966  }
2967  case T_CaseExpr:
2968  {
2969  /*----------
2970  * CASE expressions can be simplified if there are constant
2971  * condition clauses:
2972  * FALSE (or NULL): drop the alternative
2973  * TRUE: drop all remaining alternatives
2974  * If the first non-FALSE alternative is a constant TRUE,
2975  * we can simplify the entire CASE to that alternative's
2976  * expression. If there are no non-FALSE alternatives,
2977  * we simplify the entire CASE to the default result (ELSE).
2978  *
2979  * If we have a simple-form CASE with constant test
2980  * expression, we substitute the constant value for contained
2981  * CaseTestExpr placeholder nodes, so that we have the
2982  * opportunity to reduce constant test conditions. For
2983  * example this allows
2984  * CASE 0 WHEN 0 THEN 1 ELSE 1/0 END
2985  * to reduce to 1 rather than drawing a divide-by-0 error.
2986  * Note that when the test expression is constant, we don't
2987  * have to include it in the resulting CASE; for example
2988  * CASE 0 WHEN x THEN y ELSE z END
2989  * is transformed by the parser to
2990  * CASE 0 WHEN CaseTestExpr = x THEN y ELSE z END
2991  * which we can simplify to
2992  * CASE WHEN 0 = x THEN y ELSE z END
2993  * It is not necessary for the executor to evaluate the "arg"
2994  * expression when executing the CASE, since any contained
2995  * CaseTestExprs that might have referred to it will have been
2996  * replaced by the constant.
2997  *----------
2998  */
2999  CaseExpr *caseexpr = (CaseExpr *) node;
3000  CaseExpr *newcase;
3001  Node *save_case_val;
3002  Node *newarg;
3003  List *newargs;
3004  bool const_true_cond;
3005  Node *defresult = NULL;
3006  ListCell *arg;
3007 
3008  /* Simplify the test expression, if any */
3009  newarg = eval_const_expressions_mutator((Node *) caseexpr->arg,
3010  context);
3011 
3012  /* Set up for contained CaseTestExpr nodes */
3013  save_case_val = context->case_val;
3014  if (newarg && IsA(newarg, Const))
3015  {
3016  context->case_val = newarg;
3017  newarg = NULL; /* not needed anymore, see above */
3018  }
3019  else
3020  context->case_val = NULL;
3021 
3022  /* Simplify the WHEN clauses */
3023  newargs = NIL;
3024  const_true_cond = false;
3025  foreach(arg, caseexpr->args)
3026  {
3027  CaseWhen *oldcasewhen = lfirst_node(CaseWhen, arg);
3028  Node *casecond;
3029  Node *caseresult;
3030 
3031  /* Simplify this alternative's test condition */
3032  casecond = eval_const_expressions_mutator((Node *) oldcasewhen->expr,
3033  context);
3034 
3035  /*
3036  * If the test condition is constant FALSE (or NULL), then
3037  * drop this WHEN clause completely, without processing
3038  * the result.
3039  */
3040  if (casecond && IsA(casecond, Const))
3041  {
3042  Const *const_input = (Const *) casecond;
3043 
3044  if (const_input->constisnull ||
3045  !DatumGetBool(const_input->constvalue))
3046  continue; /* drop alternative with FALSE cond */
3047  /* Else it's constant TRUE */
3048  const_true_cond = true;
3049  }
3050 
3051  /* Simplify this alternative's result value */
3052  caseresult = eval_const_expressions_mutator((Node *) oldcasewhen->result,
3053  context);
3054 
3055  /* If non-constant test condition, emit a new WHEN node */
3056  if (!const_true_cond)
3057  {
3058  CaseWhen *newcasewhen = makeNode(CaseWhen);
3059 
3060  newcasewhen->expr = (Expr *) casecond;
3061  newcasewhen->result = (Expr *) caseresult;
3062  newcasewhen->location = oldcasewhen->location;
3063  newargs = lappend(newargs, newcasewhen);
3064  continue;
3065  }
3066 
3067  /*
3068  * Found a TRUE condition, so none of the remaining
3069  * alternatives can be reached. We treat the result as
3070  * the default result.
3071  */
3072  defresult = caseresult;
3073  break;
3074  }
3075 
3076  /* Simplify the default result, unless we replaced it above */
3077  if (!const_true_cond)
3078  defresult = eval_const_expressions_mutator((Node *) caseexpr->defresult,
3079  context);
3080 
3081  context->case_val = save_case_val;
3082 
3083  /*
3084  * If no non-FALSE alternatives, CASE reduces to the default
3085  * result
3086  */
3087  if (newargs == NIL)
3088  return defresult;
3089  /* Otherwise we need a new CASE node */
3090  newcase = makeNode(CaseExpr);
3091  newcase->casetype = caseexpr->casetype;
3092  newcase->casecollid = caseexpr->casecollid;
3093  newcase->arg = (Expr *) newarg;
3094  newcase->args = newargs;
3095  newcase->defresult = (Expr *) defresult;
3096  newcase->location = caseexpr->location;
3097  return (Node *) newcase;
3098  }
3099  case T_CaseTestExpr:
3100  {
3101  /*
3102  * If we know a constant test value for the current CASE
3103  * construct, substitute it for the placeholder. Else just
3104  * return the placeholder as-is.
3105  */
3106  if (context->case_val)
3107  return copyObject(context->case_val);
3108  else
3109  return copyObject(node);
3110  }
3111  case T_SubscriptingRef:
3112  case T_ArrayExpr:
3113  case T_RowExpr:
3114  case T_MinMaxExpr:
3115  {
3116  /*
3117  * Generic handling for node types whose own processing is
3118  * known to be immutable, and for which we need no smarts
3119  * beyond "simplify if all inputs are constants".
3120  *
3121  * Treating SubscriptingRef this way assumes that subscripting
3122  * fetch and assignment are both immutable. This constrains
3123  * type-specific subscripting implementations; maybe we should
3124  * relax it someday.
3125  *
3126  * Treating MinMaxExpr this way amounts to assuming that the
3127  * btree comparison function it calls is immutable; see the
3128  * reasoning in contain_mutable_functions_walker.
3129  */
3130 
3131  /* Copy the node and const-simplify its arguments */
3132  node = ece_generic_processing(node);
3133  /* If all arguments are Consts, we can fold to a constant */
3134  if (ece_all_arguments_const(node))
3135  return ece_evaluate_expr(node);
3136  return node;
3137  }
3138  case T_CoalesceExpr:
3139  {
3140  CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
3141  CoalesceExpr *newcoalesce;
3142  List *newargs;
3143  ListCell *arg;
3144 
3145  newargs = NIL;
3146  foreach(arg, coalesceexpr->args)
3147  {
3148  Node *e;
3149 
3151  context);
3152 
3153  /*
3154  * We can remove null constants from the list. For a
3155  * non-null constant, if it has not been preceded by any
3156  * other non-null-constant expressions then it is the
3157  * result. Otherwise, it's the next argument, but we can
3158  * drop following arguments since they will never be
3159  * reached.
3160  */
3161  if (IsA(e, Const))
3162  {
3163  if (((Const *) e)->constisnull)
3164  continue; /* drop null constant */
3165  if (newargs == NIL)
3166  return e; /* first expr */
3167  newargs = lappend(newargs, e);
3168  break;
3169  }
3170  newargs = lappend(newargs, e);
3171  }
3172 
3173  /*
3174  * If all the arguments were constant null, the result is just
3175  * null
3176  */
3177  if (newargs == NIL)
3178  return (Node *) makeNullConst(coalesceexpr->coalescetype,
3179  -1,
3180  coalesceexpr->coalescecollid);
3181 
3182  newcoalesce = makeNode(CoalesceExpr);
3183  newcoalesce->coalescetype = coalesceexpr->coalescetype;
3184  newcoalesce->coalescecollid = coalesceexpr->coalescecollid;
3185  newcoalesce->args = newargs;
3186  newcoalesce->location = coalesceexpr->location;
3187  return (Node *) newcoalesce;
3188  }
3189  case T_FieldSelect:
3190  {
3191  /*
3192  * We can optimize field selection from a whole-row Var into a
3193  * simple Var. (This case won't be generated directly by the
3194  * parser, because ParseComplexProjection short-circuits it.
3195  * But it can arise while simplifying functions.) Also, we
3196  * can optimize field selection from a RowExpr construct, or
3197  * of course from a constant.
3198  *
3199  * However, replacing a whole-row Var in this way has a
3200  * pitfall: if we've already built the rel targetlist for the
3201  * source relation, then the whole-row Var is scheduled to be
3202  * produced by the relation scan, but the simple Var probably
3203  * isn't, which will lead to a failure in setrefs.c. This is
3204  * not a problem when handling simple single-level queries, in
3205  * which expression simplification always happens first. It
3206  * is a risk for lateral references from subqueries, though.
3207  * To avoid such failures, don't optimize uplevel references.
3208  *
3209  * We must also check that the declared type of the field is
3210  * still the same as when the FieldSelect was created --- this
3211  * can change if someone did ALTER COLUMN TYPE on the rowtype.
3212  * If it isn't, we skip the optimization; the case will
3213  * probably fail at runtime, but that's not our problem here.
3214  */
3215  FieldSelect *fselect = (FieldSelect *) node;
3216  FieldSelect *newfselect;
3217  Node *arg;
3218 
3219  arg = eval_const_expressions_mutator((Node *) fselect->arg,
3220  context);
3221  if (arg && IsA(arg, Var) &&
3222  ((Var *) arg)->varattno == InvalidAttrNumber &&
3223  ((Var *) arg)->varlevelsup == 0)
3224  {
3225  if (rowtype_field_matches(((Var *) arg)->vartype,
3226  fselect->fieldnum,
3227  fselect->resulttype,
3228  fselect->resulttypmod,
3229  fselect->resultcollid))
3230  return (Node *) makeVar(((Var *) arg)->varno,
3231  fselect->fieldnum,
3232  fselect->resulttype,
3233  fselect->resulttypmod,
3234  fselect->resultcollid,
3235  ((Var *) arg)->varlevelsup);
3236  }
3237  if (arg && IsA(arg, RowExpr))
3238  {
3239  RowExpr *rowexpr = (RowExpr *) arg;
3240 
3241  if (fselect->fieldnum > 0 &&
3242  fselect->fieldnum <= list_length(rowexpr->args))
3243  {
3244  Node *fld = (Node *) list_nth(rowexpr->args,
3245  fselect->fieldnum - 1);
3246 
3247  if (rowtype_field_matches(rowexpr->row_typeid,
3248  fselect->fieldnum,
3249  fselect->resulttype,
3250  fselect->resulttypmod,
3251  fselect->resultcollid) &&
3252  fselect->resulttype == exprType(fld) &&
3253  fselect->resulttypmod == exprTypmod(fld) &&
3254  fselect->resultcollid == exprCollation(fld))
3255  return fld;
3256  }
3257  }
3258  newfselect = makeNode(FieldSelect);
3259  newfselect->arg = (Expr *) arg;
3260  newfselect->fieldnum = fselect->fieldnum;
3261  newfselect->resulttype = fselect->resulttype;
3262  newfselect->resulttypmod = fselect->resulttypmod;
3263  newfselect->resultcollid = fselect->resultcollid;
3264  if (arg && IsA(arg, Const))
3265  {
3266  Const *con = (Const *) arg;
3267 
3269  newfselect->fieldnum,
3270  newfselect->resulttype,
3271  newfselect->resulttypmod,
3272  newfselect->resultcollid))
3273  return ece_evaluate_expr(newfselect);
3274  }
3275  return (Node *) newfselect;
3276  }
3277  case T_NullTest:
3278  {
3279  NullTest *ntest = (NullTest *) node;
3280  NullTest *newntest;
3281  Node *arg;
3282 
3284  context);
3285  if (ntest->argisrow && arg && IsA(arg, RowExpr))
3286  {
3287  /*
3288  * We break ROW(...) IS [NOT] NULL into separate tests on
3289  * its component fields. This form is usually more
3290  * efficient to evaluate, as well as being more amenable
3291  * to optimization.
3292  */
3293  RowExpr *rarg = (RowExpr *) arg;
3294  List *newargs = NIL;
3295  ListCell *l;
3296 
3297  foreach(l, rarg->args)
3298  {
3299  Node *relem = (Node *) lfirst(l);
3300 
3301  /*
3302  * A constant field refutes the whole NullTest if it's
3303  * of the wrong nullness; else we can discard it.
3304  */
3305  if (relem && IsA(relem, Const))
3306  {
3307  Const *carg = (Const *) relem;
3308 
3309  if (carg->constisnull ?
3310  (ntest->nulltesttype == IS_NOT_NULL) :
3311  (ntest->nulltesttype == IS_NULL))
3312  return makeBoolConst(false, false);
3313  continue;
3314  }
3315 
3316  /*
3317  * Else, make a scalar (argisrow == false) NullTest
3318  * for this field. Scalar semantics are required
3319  * because IS [NOT] NULL doesn't recurse; see comments
3320  * in ExecEvalRowNullInt().
3321  */
3322  newntest = makeNode(NullTest);
3323  newntest->arg = (Expr *) relem;
3324  newntest->nulltesttype = ntest->nulltesttype;
3325  newntest->argisrow = false;
3326  newntest->location = ntest->location;
3327  newargs = lappend(newargs, newntest);
3328  }
3329  /* If all the inputs were constants, result is TRUE */
3330  if (newargs == NIL)
3331  return makeBoolConst(true, false);
3332  /* If only one nonconst input, it's the result */
3333  if (list_length(newargs) == 1)
3334  return (Node *) linitial(newargs);
3335  /* Else we need an AND node */
3336  return (Node *) make_andclause(newargs);
3337  }
3338  if (!ntest->argisrow && arg && IsA(arg, Const))
3339  {
3340  Const *carg = (Const *) arg;
3341  bool result;
3342 
3343  switch (ntest->nulltesttype)
3344  {
3345  case IS_NULL:
3346  result = carg->constisnull;
3347  break;
3348  case IS_NOT_NULL:
3349  result = !carg->constisnull;
3350  break;
3351  default:
3352  elog(ERROR, "unrecognized nulltesttype: %d",
3353  (int) ntest->nulltesttype);
3354  result = false; /* keep compiler quiet */
3355  break;
3356  }
3357 
3358  return makeBoolConst(result, false);
3359  }
3360 
3361  newntest = makeNode(NullTest);
3362  newntest->arg = (Expr *) arg;
3363  newntest->nulltesttype = ntest->nulltesttype;
3364  newntest->argisrow = ntest->argisrow;
3365  newntest->location = ntest->location;
3366  return (Node *) newntest;
3367  }
3368  case T_BooleanTest:
3369  {
3370  /*
3371  * This case could be folded into the generic handling used
3372  * for ArrayExpr etc. But because the simplification logic is
3373  * so trivial, applying evaluate_expr() to perform it would be
3374  * a heavy overhead. BooleanTest is probably common enough to
3375  * justify keeping this bespoke implementation.
3376  */
3377  BooleanTest *btest = (BooleanTest *) node;
3378  BooleanTest *newbtest;
3379  Node *arg;
3380 
3382  context);
3383  if (arg && IsA(arg, Const))
3384  {
3385  Const *carg = (Const *) arg;
3386  bool result;
3387 
3388  switch (btest->booltesttype)
3389  {
3390  case IS_TRUE:
3391  result = (!carg->constisnull &&
3392  DatumGetBool(carg->constvalue));
3393  break;
3394  case IS_NOT_TRUE:
3395  result = (carg->constisnull ||
3396  !DatumGetBool(carg->constvalue));
3397  break;
3398  case IS_FALSE:
3399  result = (!carg->constisnull &&
3400  !DatumGetBool(carg->constvalue));
3401  break;
3402  case IS_NOT_FALSE:
3403  result = (carg->constisnull ||
3404  DatumGetBool(carg->constvalue));
3405  break;
3406  case IS_UNKNOWN:
3407  result = carg->constisnull;
3408  break;
3409  case IS_NOT_UNKNOWN:
3410  result = !carg->constisnull;
3411  break;
3412  default:
3413  elog(ERROR, "unrecognized booltesttype: %d",
3414  (int) btest->booltesttype);
3415  result = false; /* keep compiler quiet */
3416  break;
3417  }
3418 
3419  return makeBoolConst(result, false);
3420  }
3421 
3422  newbtest = makeNode(BooleanTest);
3423  newbtest->arg = (Expr *) arg;
3424  newbtest->booltesttype = btest->booltesttype;
3425  newbtest->location = btest->location;
3426  return (Node *) newbtest;
3427  }
3428  case T_CoerceToDomain:
3429  {
3430  /*
3431  * If the domain currently has no constraints, we replace the
3432  * CoerceToDomain node with a simple RelabelType, which is
3433  * both far faster to execute and more amenable to later
3434  * optimization. We must then mark the plan as needing to be
3435  * rebuilt if the domain's constraints change.
3436  *
3437  * Also, in estimation mode, always replace CoerceToDomain
3438  * nodes, effectively assuming that the coercion will succeed.
3439  */
3440  CoerceToDomain *cdomain = (CoerceToDomain *) node;
3441  CoerceToDomain *newcdomain;
3442  Node *arg;
3443 
3444  arg = eval_const_expressions_mutator((Node *) cdomain->arg,
3445  context);
3446  if (context->estimate ||
3447  !DomainHasConstraints(cdomain->resulttype))
3448  {
3449  /* Record dependency, if this isn't estimation mode */
3450  if (context->root && !context->estimate)
3451  record_plan_type_dependency(context->root,
3452  cdomain->resulttype);
3453 
3454  /* Generate RelabelType to substitute for CoerceToDomain */
3455  return applyRelabelType(arg,
3456  cdomain->resulttype,
3457  cdomain->resulttypmod,
3458  cdomain->resultcollid,
3459  cdomain->coercionformat,
3460  cdomain->location,
3461  true);
3462  }
3463 
3464  newcdomain = makeNode(CoerceToDomain);
3465  newcdomain->arg = (Expr *) arg;
3466  newcdomain->resulttype = cdomain->resulttype;
3467  newcdomain->resulttypmod = cdomain->resulttypmod;
3468  newcdomain->resultcollid = cdomain->resultcollid;
3469  newcdomain->coercionformat = cdomain->coercionformat;
3470  newcdomain->location = cdomain->location;
3471  return (Node *) newcdomain;
3472  }
3473  case T_PlaceHolderVar:
3474 
3475  /*
3476  * In estimation mode, just strip the PlaceHolderVar node
3477  * altogether; this amounts to estimating that the contained value
3478  * won't be forced to null by an outer join. In regular mode we
3479  * just use the default behavior (ie, simplify the expression but
3480  * leave the PlaceHolderVar node intact).
3481  */
3482  if (context->estimate)
3483  {
3484  PlaceHolderVar *phv = (PlaceHolderVar *) node;
3485 
3486  return eval_const_expressions_mutator((Node *) phv->phexpr,
3487  context);
3488  }
3489  break;
3490  case T_ConvertRowtypeExpr:
3491  {
3493  Node *arg;
3494  ConvertRowtypeExpr *newcre;
3495 
3497  context);
3498 
3499  newcre = makeNode(ConvertRowtypeExpr);
3500  newcre->resulttype = cre->resulttype;
3501  newcre->convertformat = cre->convertformat;
3502  newcre->location = cre->location;
3503 
3504  /*
3505  * In case of a nested ConvertRowtypeExpr, we can convert the
3506  * leaf row directly to the topmost row format without any
3507  * intermediate conversions. (This works because
3508  * ConvertRowtypeExpr is used only for child->parent
3509  * conversion in inheritance trees, which works by exact match
3510  * of column name, and a column absent in an intermediate
3511  * result can't be present in the final result.)
3512  *
3513  * No need to check more than one level deep, because the
3514  * above recursion will have flattened anything else.
3515  */
3516  if (arg != NULL && IsA(arg, ConvertRowtypeExpr))
3517  {
3519 
3520  arg = (Node *) argcre->arg;
3521 
3522  /*
3523  * Make sure an outer implicit conversion can't hide an
3524  * inner explicit one.
3525  */
3527  newcre->convertformat = argcre->convertformat;
3528  }
3529 
3530  newcre->arg = (Expr *) arg;
3531 
3532  if (arg != NULL && IsA(arg, Const))
3533  return ece_evaluate_expr((Node *) newcre);
3534  return (Node *) newcre;
3535  }
3536  default:
3537  break;
3538  }
3539 
3540  /*
3541  * For any node type not handled above, copy the node unchanged but
3542  * const-simplify its subexpressions. This is the correct thing for node
3543  * types whose behavior might change between planning and execution, such
3544  * as CurrentOfExpr. It's also a safe default for new node types not
3545  * known to this routine.
3546  */
3547  return ece_generic_processing(node);
3548 }
#define InvalidAttrNumber
Definition: attnum.h:23
signed short int16
Definition: c.h:429
signed int int32
Definition: c.h:430
static List * simplify_or_arguments(List *args, eval_const_expressions_context *context, bool *haveNull, bool *forceTrue)
Definition: clauses.c:3614
static bool rowtype_field_matches(Oid rowtypeid, int fieldnum, Oid expectedtype, int32 expectedtypmod, Oid expectedcollation)
Definition: clauses.c:2064
#define ece_all_arguments_const(node)
Definition: clauses.c:2304
#define ece_evaluate_expr(node)
Definition: clauses.c:2308
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:365
#define ece_generic_processing(node)
Definition: clauses.c:2295
List * expand_function_arguments(List *args, bool include_out_arguments, Oid result_type, HeapTuple func_tuple)
Definition: clauses.c:3999
static bool ece_function_is_safe(Oid funcid, eval_const_expressions_context *context)
Definition: clauses.c:3576
static List * simplify_and_arguments(List *args, eval_const_expressions_context *context, bool *haveNull, bool *forceFalse)
Definition: clauses.c:3720
static Expr * simplify_function(Oid funcid, Oid result_type, int32 result_typmod, Oid result_collid, Oid input_collid, List **args_p, bool funcvariadic, bool process_args, bool allow_non_const, eval_const_expressions_context *context)
Definition: clauses.c:3883
static Node * simplify_boolean_equality(Oid opno, List *args)
Definition: clauses.c:3814
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
List * lappend(List *list, void *datum)
Definition: list.c:338
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2865
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2209
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2832
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:337
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:299
Expr * make_andclause(List *andclauses)
Definition: makefuncs.c:636
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Definition: makefuncs.c:66
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:357
Expr * make_orclause(List *orclauses)
Definition: makefuncs.c:652
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:266
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:764
Node * applyRelabelType(Node *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat, int rlocation, bool overwrite_ok)
Definition: nodeFuncs.c:579
void set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
Definition: nodeFuncs.c:1683
void set_opfuncid(OpExpr *opexpr)
Definition: nodeFuncs.c:1672
#define copyObject(obj)
Definition: nodes.h:233
#define makeNode(_type_)
Definition: nodes.h:165
#define castNode(_type_, nodeptr)
Definition: nodes.h:186
#define PARAM_FLAG_CONST
Definition: params.h:88
#define lfirst_node(type, lc)
Definition: pg_list.h:174
#define list_make1(x1)
Definition: pg_list.h:210
#define list_make3(x1, x2, x3)
Definition: pg_list.h:214
static void * list_nth(const List *list, int n)
Definition: pg_list.h:297
static bool DatumGetBool(Datum X)
Definition: postgres.h:438
static Datum BoolGetDatum(bool X)
Definition: postgres.h:450
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:600
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:560
Node * negate_clause(Node *node)
Definition: prepqual.c:74
e
Definition: preproc-init.c:82
@ IS_NOT_TRUE
Definition: primnodes.h:1382
@ IS_NOT_FALSE
Definition: primnodes.h:1382
@ IS_NOT_UNKNOWN
Definition: primnodes.h:1382
@ IS_TRUE
Definition: primnodes.h:1382
@ IS_UNKNOWN
Definition: primnodes.h:1382
@ IS_FALSE
Definition: primnodes.h:1382
@ NOT_EXPR
Definition: primnodes.h:758
@ PARAM_EXTERN
Definition: primnodes.h:302
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:588
@ IS_NULL
Definition: primnodes.h:1359
@ IS_NOT_NULL
Definition: primnodes.h:1359
struct Const Const
void record_plan_type_dependency(PlannerInfo *root, Oid typid)
Definition: setrefs.c:3299
List * args
Definition: primnodes.h:767
BoolTestType booltesttype
Definition: primnodes.h:1389
Expr * arg
Definition: primnodes.h:1388
int location
Definition: primnodes.h:1122
Oid casecollid
Definition: primnodes.h:1118
Oid casetype
Definition: primnodes.h:1117
Expr * defresult
Definition: primnodes.h:1121
List * args
Definition: primnodes.h:1120
Expr * result
Definition: primnodes.h:1132
Expr * expr
Definition: primnodes.h:1131
int location
Definition: primnodes.h:1133
List * args
Definition: primnodes.h:1271
Oid coalescetype
Definition: primnodes.h:1269
Oid coalescecollid
Definition: primnodes.h:1270
CoercionForm coercionformat
Definition: primnodes.h:1409
int32 resulttypmod
Definition: primnodes.h:1407
Expr * arg
Definition: primnodes.h:1021
CoercionForm coerceformat
Definition: primnodes.h:1025
Oid resultcollid
Definition: primnodes.h:1024
Oid resulttype
Definition: primnodes.h:1022
Expr * arg
Definition: primnodes.h:1087
Oid consttype
Definition: primnodes.h:259
Datum constvalue
Definition: primnodes.h:263
bool constisnull
Definition: primnodes.h:264
int location
Definition: primnodes.h:270
CoercionForm convertformat
Definition: primnodes.h:1073
AttrNumber fieldnum
Definition: primnodes.h:950
Oid resulttype
Definition: primnodes.h:951
Oid resultcollid
Definition: primnodes.h:954
Expr * arg
Definition: primnodes.h:949
int32 resulttypmod
Definition: primnodes.h:953
Oid funccollid
Definition: primnodes.h:604
bool funcretset
Definition: primnodes.h:600
Oid funcid
Definition: primnodes.h:598
bool funcvariadic
Definition: primnodes.h:601
List * args
Definition: primnodes.h:606
CoercionForm funcformat
Definition: primnodes.h:603
Oid inputcollid
Definition: primnodes.h:605
int location
Definition: primnodes.h:607
Oid funcresulttype
Definition: primnodes.h:599
NullTestType nulltesttype
Definition: primnodes.h:1366
int location
Definition: primnodes.h:1368
bool argisrow
Definition: primnodes.h:1367
Expr * arg
Definition: primnodes.h:1365
bool opretset
Definition: primnodes.h:657
int location
Definition: primnodes.h:669
Oid opcollid
Definition: primnodes.h:660
Oid inputcollid
Definition: primnodes.h:663
Oid opresulttype
Definition: primnodes.h:654
bool isnull
Definition: params.h:93
uint16 pflags
Definition: params.h:94
Datum value
Definition: params.h:92
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:125
ParamFetchHook paramFetch
Definition: params.h:112
int32 paramtypmod
Definition: primnodes.h:314
Oid paramtype
Definition: primnodes.h:313
int location
Definition: primnodes.h:316
Oid paramcollid
Definition: primnodes.h:315
Oid resultcollid
Definition: primnodes.h:1004
int32 resulttypmod
Definition: primnodes.h:1003
CoercionForm relabelformat
Definition: primnodes.h:1005
Oid resulttype
Definition: primnodes.h:1002
Expr * arg
Definition: primnodes.h:1001
Oid row_typeid
Definition: primnodes.h:1208
List * args
Definition: primnodes.h:1207
Definition: primnodes.h:205
bool winagg
Definition: primnodes.h:497
Oid inputcollid
Definition: primnodes.h:492
List * args
Definition: primnodes.h:493
Index winref
Definition: primnodes.h:495
Expr * aggfilter
Definition: primnodes.h:494
int location
Definition: primnodes.h:498
Oid wincollid
Definition: primnodes.h:491
Oid winfnoid
Definition: primnodes.h:489
bool winstar
Definition: primnodes.h:496
Oid wintype
Definition: primnodes.h:490
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1221
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1173
@ PROCOID
Definition: syscache.h:79
bool DomainHasConstraints(Oid type_id)
Definition: typcache.c:1392

References WindowFunc::aggfilter, AND_EXPR, applyRelabelType(), arg, FieldSelect::arg, RelabelType::arg, CoerceViaIO::arg, ArrayCoerceExpr::arg, ConvertRowtypeExpr::arg, CollateExpr::arg, CaseExpr::arg, NullTest::arg, BooleanTest::arg, CoerceToDomain::arg, NullTest::argisrow, generate_unaccent_rules::args, WindowFunc::args, FuncExpr::args, OpExpr::args, BoolExpr::args, CaseExpr::args, RowExpr::args, CoalesceExpr::args, Assert(), BoolGetDatum(), BoolExpr::boolop, BooleanTest::booltesttype, eval_const_expressions_context::boundParams, eval_const_expressions_context::case_val, CaseExpr::casecollid, CaseExpr::casetype, castNode, CoalesceExpr::coalescecollid, CoalesceExpr::coalescetype, COERCE_IMPLICIT_CAST, CoerceViaIO::coerceformat, CoerceToDomain::coercionformat, CollateExpr::collOid, Const::constisnull, Const::consttype, Const::constvalue, contain_mutable_functions(), ConvertRowtypeExpr::convertformat, copyObject, datumCopy(), DatumGetBool(), CaseExpr::defresult, DomainHasConstraints(), ece_all_arguments_const, ece_evaluate_expr, ece_function_is_safe(), ece_generic_processing, ArrayCoerceExpr::elemexpr, elog(), ERROR, eval_const_expressions_context::estimate, expand_function_arguments(), CaseWhen::expr, exprCollation(), expression_tree_mutator, exprType(), exprTypmod(), FieldSelect::fieldnum, FuncExpr::funccollid, FuncExpr::funcformat, FuncExpr::funcid, FuncExpr::funcresulttype, FuncExpr::funcretset, FuncExpr::funcvariadic, get_typlenbyval(), getTypeInputInfo(), getTypeOutputInfo(), HeapTupleIsValid, if(), WindowFunc::inputcollid, FuncExpr::inputcollid, OpExpr::inputcollid, Int32GetDatum(), InvalidAttrNumber, InvalidOid, IS_FALSE, IS_NOT_FALSE, IS_NOT_NULL, IS_NOT_TRUE, IS_NOT_UNKNOWN, IS_NULL, IS_TRUE, IS_UNKNOWN, IsA, ParamExternData::isnull, lappend(), lfirst, lfirst_node, linitial, list_length(), list_make1, list_make3, list_nth(), Const::location, Param::location, WindowFunc::location, FuncExpr::location, OpExpr::location, RelabelType::location, CoerceViaIO::location, ConvertRowtypeExpr::location, CollateExpr::location, CaseExpr::location, CaseWhen::location, CoalesceExpr::location, NullTest::location, BooleanTest::location, CoerceToDomain::location, make_andclause(), make_orclause(), makeBoolConst(), makeConst(), makeNode, makeNullConst(), makeVar(), negate_clause(), NIL, nodeTag, NOT_EXPR, NullTest::nulltesttype, ParamListInfoData::numParams, ObjectIdGetDatum(), OidIsValid, OpExpr::opcollid, OpExpr::opno, OpExpr::opresulttype, OpExpr::opretset, OR_EXPR, PARAM_EXTERN, PARAM_FLAG_CONST, Param::paramcollid, ParamListInfoData::paramFetch, Param::paramid, Param::paramkind, ParamListInfoData::params, Param::paramtype, Param::paramtypmod, ParamExternData::pflags, PROCOID, ParamExternData::ptype, record_plan_type_dependency(), RelabelType::relabelformat, ReleaseSysCache(), CaseWhen::result, FieldSelect::resultcollid, RelabelType::resultcollid, CoerceViaIO::resultcollid, CoerceToDomain::resultcollid, FieldSelect::resulttype, RelabelType::resulttype, CoerceViaIO::resulttype, ConvertRowtypeExpr::resulttype, CoerceToDomain::resulttype, FieldSelect::resulttypmod, RelabelType::resulttypmod, CoerceToDomain::resulttypmod, eval_const_expressions_context::root, RowExpr::row_typeid, rowtype_field_matches(), SearchSysCache1(), set_opfuncid(), set_sa_opfuncid(), simplify_and_arguments(), simplify_boolean_equality(), simplify_function(), simplify_or_arguments(), ParamExternData::value, WindowFunc::winagg, WindowFunc::wincollid, WindowFunc::winfnoid, WindowFunc::winref, WindowFunc::winstar, and WindowFunc::wintype.

Referenced by estimate_expression_value(), eval_const_expressions(), inline_function(), simplify_and_arguments(), simplify_function(), and simplify_or_arguments().

◆ evaluate_expr()

Expr* evaluate_expr ( Expr expr,
Oid  result_type,
int32  result_typmod,
Oid  result_collation 
)

Definition at line 4807 of file clauses.c.

4809 {
4810  EState *estate;
4811  ExprState *exprstate;
4812  MemoryContext oldcontext;
4813  Datum const_val;
4814  bool const_is_null;
4815  int16 resultTypLen;
4816  bool resultTypByVal;
4817 
4818  /*
4819  * To use the executor, we need an EState.
4820  */
4821  estate = CreateExecutorState();
4822 
4823  /* We can use the estate's working context to avoid memory leaks. */
4824  oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
4825 
4826  /* Make sure any opfuncids are filled in. */
4827  fix_opfuncids((Node *) expr);
4828 
4829  /*
4830  * Prepare expr for execution. (Note: we can't use ExecPrepareExpr
4831  * because it'd result in recursively invoking eval_const_expressions.)
4832  */
4833  exprstate = ExecInitExpr(expr, NULL);
4834 
4835  /*
4836  * And evaluate it.
4837  *
4838  * It is OK to use a default econtext because none of the ExecEvalExpr()
4839  * code used in this situation will use econtext. That might seem
4840  * fortuitous, but it's not so unreasonable --- a constant expression does
4841  * not depend on context, by definition, n'est ce pas?
4842  */
4843  const_val = ExecEvalExprSwitchContext(exprstate,
4844  GetPerTupleExprContext(estate),
4845  &const_is_null);
4846 
4847  /* Get info needed about result datatype */
4848  get_typlenbyval(result_type, &resultTypLen, &resultTypByVal);
4849 
4850  /* Get back to outer memory context */
4851  MemoryContextSwitchTo(oldcontext);
4852 
4853  /*
4854  * Must copy result out of sub-context used by expression eval.
4855  *
4856  * Also, if it's varlena, forcibly detoast it. This protects us against
4857  * storing TOAST pointers into plans that might outlive the referenced
4858  * data. (makeConst would handle detoasting anyway, but it's worth a few
4859  * extra lines here so that we can do the copy and detoast in one step.)
4860  */
4861  if (!const_is_null)
4862  {
4863  if (resultTypLen == -1)
4864  const_val = PointerGetDatum(PG_DETOAST_DATUM_COPY(const_val));
4865  else
4866  const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
4867  }
4868 
4869  /* Release all the junk we just created */
4870  FreeExecutorState(estate);
4871 
4872  /*
4873  * Make the constant result node.
4874  */
4875  return (Expr *) makeConst(result_type, result_typmod, result_collation,
4876  resultTypLen,
4877  const_val, const_is_null,
4878  resultTypByVal);
4879 }
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:124
EState * CreateExecutorState(void)
Definition: execUtils.c:92
void FreeExecutorState(EState *estate)
Definition: execUtils.c:190
#define GetPerTupleExprContext(estate)
Definition: executor.h:538
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:336
#define PG_DETOAST_DATUM_COPY(datum)
Definition: fmgr.h:242
void fix_opfuncids(Node *node)
Definition: nodeFuncs.c:1641
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:135
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
MemoryContext es_query_cxt
Definition: execnodes.h:656

References CreateExecutorState(), datumCopy(), EState::es_query_cxt, ExecEvalExprSwitchContext(), ExecInitExpr(), fix_opfuncids(), FreeExecutorState(), get_typlenbyval(), GetPerTupleExprContext, makeConst(), MemoryContextSwitchTo(), PG_DETOAST_DATUM_COPY, and PointerGetDatum().

Referenced by evaluate_function(), and transformPartitionBoundValue().

◆ evaluate_function()

static Expr * evaluate_function ( Oid  funcid,
Oid  result_type,
int32  result_typmod,
Oid  result_collid,
Oid  input_collid,
List args,
bool  funcvariadic,
HeapTuple  func_tuple,
eval_const_expressions_context context 
)
static

Definition at line 4254 of file clauses.c.

4259 {
4260  Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4261  bool has_nonconst_input = false;
4262  bool has_null_input = false;
4263  ListCell *arg;
4264  FuncExpr *newexpr;
4265 
4266  /*
4267  * Can't simplify if it returns a set.
4268  */
4269  if (funcform->proretset)
4270  return NULL;
4271 
4272  /*
4273  * Can't simplify if it returns RECORD. The immediate problem is that it
4274  * will be needing an expected tupdesc which we can't supply here.
4275  *
4276  * In the case where it has OUT parameters, it could get by without an
4277  * expected tupdesc, but we still have issues: get_expr_result_type()
4278  * doesn't know how to extract type info from a RECORD constant, and in
4279  * the case of a NULL function result there doesn't seem to be any clean
4280  * way to fix that. In view of the likelihood of there being still other
4281  * gotchas, seems best to leave the function call unreduced.
4282  */
4283  if (funcform->prorettype == RECORDOID)
4284  return NULL;
4285 
4286  /*
4287  * Check for constant inputs and especially constant-NULL inputs.
4288  */
4289  foreach(arg, args)
4290  {
4291  if (IsA(lfirst(arg), Const))
4292  has_null_input |= ((Const *) lfirst(arg))->constisnull;
4293  else
4294  has_nonconst_input = true;
4295  }
4296 
4297  /*
4298  * If the function is strict and has a constant-NULL input, it will never
4299  * be called at all, so we can replace the call by a NULL constant, even
4300  * if there are other inputs that aren't constant, and even if the
4301  * function is not otherwise immutable.
4302  */
4303  if (funcform->proisstrict && has_null_input)
4304  return (Expr *) makeNullConst(result_type, result_typmod,
4305  result_collid);
4306 
4307  /*
4308  * Otherwise, can simplify only if all inputs are constants. (For a
4309  * non-strict function, constant NULL inputs are treated the same as
4310  * constant non-NULL inputs.)
4311  */
4312  if (has_nonconst_input)
4313  return NULL;
4314 
4315  /*
4316  * Ordinarily we are only allowed to simplify immutable functions. But for
4317  * purposes of estimation, we consider it okay to simplify functions that
4318  * are merely stable; the risk that the result might change from planning
4319  * time to execution time is worth taking in preference to not being able
4320  * to estimate the value at all.
4321  */
4322  if (funcform->provolatile == PROVOLATILE_IMMUTABLE)
4323  /* okay */ ;
4324  else if (context->estimate && funcform->provolatile == PROVOLATILE_STABLE)
4325  /* okay */ ;
4326  else
4327  return NULL;
4328 
4329  /*
4330  * OK, looks like we can simplify this operator/function.
4331  *
4332  * Build a new FuncExpr node containing the already-simplified arguments.
4333  */
4334  newexpr = makeNode(FuncExpr);
4335  newexpr->funcid = funcid;
4336  newexpr->funcresulttype = result_type;
4337  newexpr->funcretset = false;
4338  newexpr->funcvariadic = funcvariadic;
4339  newexpr->funcformat = COERCE_EXPLICIT_CALL; /* doesn't matter */
4340  newexpr->funccollid = result_collid; /* doesn't matter */
4341  newexpr->inputcollid = input_collid;
4342  newexpr->args = args;
4343  newexpr->location = -1;
4344 
4345  return evaluate_expr((Expr *) newexpr, result_type, result_typmod,
4346  result_collid);
4347 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
@ COERCE_EXPLICIT_CALL
Definition: primnodes.h:586

References arg, generate_unaccent_rules::args, FuncExpr::args, COERCE_EXPLICIT_CALL, eval_const_expressions_context::estimate, evaluate_expr(), FuncExpr::funccollid, FuncExpr::funcformat, FuncExpr::funcid, FuncExpr::funcresulttype, FuncExpr::funcretset, FuncExpr::funcvariadic, GETSTRUCT, FuncExpr::inputcollid, IsA, lfirst, FuncExpr::location, makeNode, and makeNullConst().

Referenced by simplify_function().

◆ expand_function_arguments()

List* expand_function_arguments ( List args,
bool  include_out_arguments,
Oid  result_type,
HeapTuple  func_tuple 
)

Definition at line 3999 of file clauses.c.

4001 {
4002  Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4003  Oid *proargtypes = funcform->proargtypes.values;
4004  int pronargs = funcform->pronargs;
4005  bool has_named_args = false;
4006  ListCell *lc;
4007 
4008  /*
4009  * If we are asked to match to OUT arguments, then use the proallargtypes
4010  * array (which includes those); otherwise use proargtypes (which
4011  * doesn't). Of course, if proallargtypes is null, we always use
4012  * proargtypes. (Fetching proallargtypes is annoyingly expensive
4013  * considering that we may have nothing to do here, but fortunately the
4014  * common case is include_out_arguments == false.)
4015  */
4016  if (include_out_arguments)
4017  {
4018  Datum proallargtypes;
4019  bool isNull;
4020 
4021  proallargtypes = SysCacheGetAttr(PROCOID, func_tuple,
4022  Anum_pg_proc_proallargtypes,
4023  &isNull);
4024  if (!isNull)
4025  {
4026  ArrayType *arr = DatumGetArrayTypeP(proallargtypes);
4027 
4028  pronargs = ARR_DIMS(arr)[0];
4029  if (ARR_NDIM(arr) != 1 ||
4030  pronargs < 0 ||
4031  ARR_HASNULL(arr) ||
4032  ARR_ELEMTYPE(arr) != OIDOID)
4033  elog(ERROR, "proallargtypes is not a 1-D Oid array or it contains nulls");
4034  Assert(pronargs >= funcform->pronargs);
4035  proargtypes = (Oid *) ARR_DATA_PTR(arr);
4036  }
4037  }
4038 
4039  /* Do we have any named arguments? */
4040  foreach(lc, args)
4041  {
4042  Node *arg = (Node *) lfirst(lc);
4043 
4044  if (IsA(arg, NamedArgExpr))
4045  {
4046  has_named_args = true;
4047  break;
4048  }
4049  }
4050 
4051  /* If so, we must apply reorder_function_arguments */
4052  if (has_named_args)
4053  {
4054  args = reorder_function_arguments(args, pronargs, func_tuple);
4055  /* Recheck argument types and add casts if needed */
4056  recheck_cast_function_args(args, result_type,
4057  proargtypes, pronargs,
4058  func_tuple);
4059  }
4060  else if (list_length(args) < pronargs)
4061  {
4062  /* No named args, but we seem to be short some defaults */
4063  args = add_function_defaults(args, pronargs, func_tuple);
4064  /* Recheck argument types and add casts if needed */
4065  recheck_cast_function_args(args, result_type,
4066  proargtypes, pronargs,
4067  func_tuple);
4068  }
4069 
4070  return args;
4071 }
#define ARR_DATA_PTR(a)
Definition: array.h:315
#define DatumGetArrayTypeP(X)
Definition: array.h:254
#define ARR_ELEMTYPE(a)
Definition: array.h:285
#define ARR_HASNULL(a)
Definition: array.h:284
static List * add_function_defaults(List *args, int pronargs, HeapTuple func_tuple)
Definition: clauses.c:4150
static List * reorder_function_arguments(List *args, int pronargs, HeapTuple func_tuple)
Definition: clauses.c:4080
static void recheck_cast_function_args(List *args, Oid result_type, Oid *proargtypes, int pronargs, HeapTuple func_tuple)
Definition: clauses.c:4209
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1434

References add_function_defaults(), arg, generate_unaccent_rules::args, ARR_DATA_PTR, ARR_DIMS, ARR_ELEMTYPE, ARR_HASNULL, ARR_NDIM, Assert(), DatumGetArrayTypeP, elog(), ERROR, GETSTRUCT, IsA, lfirst, list_length(), PROCOID, pronargs, recheck_cast_function_args(), reorder_function_arguments(), and SysCacheGetAttr().

Referenced by eval_const_expressions_mutator(), simplify_function(), and transformCallStmt().

◆ 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:201
double get_function_rows(PlannerInfo *root, Oid funcid, Node *node)
Definition: plancat.c:2068

References clamp_row_est(), FuncExpr::funcid, FuncExpr::funcretset, get_function_rows(), IsA, OpExpr::opretset, and set_opfuncid().

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

◆ fetch_function_defaults()

static List * fetch_function_defaults ( HeapTuple  func_tuple)
static

Definition at line 4174 of file clauses.c.

4175 {
4176  List *defaults;
4177  Datum proargdefaults;
4178  bool isnull;
4179  char *str;
4180 
4181  /* The error cases here shouldn't happen, but check anyway */
4182  proargdefaults = SysCacheGetAttr(PROCOID, func_tuple,
4183  Anum_pg_proc_proargdefaults,
4184  &isnull);
4185  if (isnull)
4186  elog(ERROR, "not enough default arguments");
4187  str = TextDatumGetCString(proargdefaults);
4188  defaults = castNode(List, stringToNode(str));
4189  pfree(str);
4190  return defaults;
4191 }
#define TextDatumGetCString(d)
Definition: builtins.h:86
void pfree(void *pointer)
Definition: mcxt.c:1306
void * stringToNode(const char *str)
Definition: read.c:90

References castNode, elog(), ERROR, pfree(), PROCOID, generate_unaccent_rules::str, stringToNode(), SysCacheGetAttr(), and TextDatumGetCString.

Referenced by add_function_defaults(), and reorder_function_arguments().

◆ find_forced_null_var()

Var* find_forced_null_var ( Node node)

Definition at line 1857 of file clauses.c.

1858 {
1859  if (node == NULL)
1860  return NULL;
1861  if (IsA(node, NullTest))
1862  {
1863  /* check for var IS NULL */
1864  NullTest *expr = (NullTest *) node;
1865 
1866  if (expr->nulltesttype == IS_NULL && !expr->argisrow)
1867  {
1868  Var *var = (Var *) expr->arg;
1869 
1870  if (var && IsA(var, Var) &&
1871  var->varlevelsup == 0)
1872  return var;
1873  }
1874  }
1875  else if (IsA(node, BooleanTest))
1876  {
1877  /* var IS UNKNOWN is equivalent to var IS NULL */
1878  BooleanTest *expr = (BooleanTest *) node;
1879 
1880  if (expr->booltesttype == IS_UNKNOWN)
1881  {
1882  Var *var = (Var *) expr->arg;
1883 
1884  if (var && IsA(var, Var) &&
1885  var->varlevelsup == 0)
1886  return var;
1887  }
1888  }
1889  return NULL;
1890 }
Index varlevelsup
Definition: primnodes.h:230

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 node)

Definition at line 1796 of file clauses.c.

1797 {
1798  List *result = NIL;
1799  Var *var;
1800  ListCell *l;
1801 
1802  if (node == NULL)
1803  return NIL;
1804  /* Check single-clause cases using subroutine */
1805  var = find_forced_null_var(node);
1806  if (var)
1807  {
1808  result = mbms_add_member(result,
1809  var->varno,
1811  }
1812  /* Otherwise, handle AND-conditions */
1813  else if (IsA(node, List))
1814  {
1815  /*
1816  * At top level, we are examining an implicit-AND list: if any of the
1817  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL.
1818  */
1819  foreach(l, (List *) node)
1820  {
1821  result = mbms_add_members(result,
1823  }
1824  }
1825  else if (IsA(node, BoolExpr))
1826  {
1827  BoolExpr *expr = (BoolExpr *) node;
1828 
1829  /*
1830  * We don't bother considering the OR case, because it's fairly
1831  * unlikely anyone would write "v1 IS NULL OR v1 IS NULL". Likewise,
1832  * the NOT case isn't worth expending code on.
1833  */
1834  if (expr->boolop == AND_EXPR)
1835  {
1836  /* At top level we can just recurse (to the List case) */
1837  result = find_forced_null_vars((Node *) expr->args);
1838  }
1839  }
1840  return result;
1841 }
Var * find_forced_null_var(Node *node)
Definition: clauses.c:1857
List * find_forced_null_vars(Node *node)
Definition: clauses.c:1796
List * mbms_add_member(List *a, int listidx, int bitidx)
List * mbms_add_members(List *a, const List *b)
AttrNumber varattno
Definition: primnodes.h:217
int varno
Definition: primnodes.h:212
#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 1336 of file clauses.c.

1337 {
1338  return find_nonnullable_rels_walker(clause, true);
1339 }
static Relids find_nonnullable_rels_walker(Node *node, bool top_level)
Definition: clauses.c:1342

References find_nonnullable_rels_walker().

Referenced by make_outerjoininfo(), and reduce_outer_joins_pass2().

◆ find_nonnullable_rels_walker()

static Relids find_nonnullable_rels_walker ( Node node,
bool  top_level 
)
static

Definition at line 1342 of file clauses.c.

1343 {
1344  Relids result = NULL;
1345  ListCell *l;
1346 
1347  if (node == NULL)
1348  return NULL;
1349  if (IsA(node, Var))
1350  {
1351  Var *var = (Var *) node;
1352 
1353  if (var->varlevelsup == 0)
1354  result = bms_make_singleton(var->varno);
1355  }
1356  else if (IsA(node, List))
1357  {
1358  /*
1359  * At top level, we are examining an implicit-AND list: if any of the
1360  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1361  * not at top level, we are examining the arguments of a strict
1362  * function: if any of them produce NULL then the result of the
1363  * function must be NULL. So in both cases, the set of nonnullable
1364  * rels is the union of those found in the arms, and we pass down the
1365  * top_level flag unmodified.
1366  */
1367  foreach(l, (List *) node)
1368  {
1369  result = bms_join(result,
1371  top_level));
1372  }
1373  }
1374  else if (IsA(node, FuncExpr))
1375  {
1376  FuncExpr *expr = (FuncExpr *) node;
1377 
1378  if (func_strict(expr->funcid))
1379  result = find_nonnullable_rels_walker((Node *) expr->args, false);
1380  }
1381  else if (IsA(node, OpExpr))
1382  {
1383  OpExpr *expr = (OpExpr *) node;
1384 
1385  set_opfuncid(expr);
1386  if (func_strict(expr->opfuncid))
1387  result = find_nonnullable_rels_walker((Node *) expr->args, false);
1388  }
1389  else if (IsA(node, ScalarArrayOpExpr))
1390  {
1391  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1392 
1393  if (is_strict_saop(expr, true))
1394  result = find_nonnullable_rels_walker((Node *) expr->args, false);
1395  }
1396  else if (IsA(node, BoolExpr))
1397  {
1398  BoolExpr *expr = (BoolExpr *) node;
1399 
1400  switch (expr->boolop)
1401  {
1402  case AND_EXPR:
1403  /* At top level we can just recurse (to the List case) */
1404  if (top_level)
1405  {
1406  result = find_nonnullable_rels_walker((Node *) expr->args,
1407  top_level);
1408  break;
1409  }
1410 
1411  /*
1412  * Below top level, even if one arm produces NULL, the result
1413  * could be FALSE (hence not NULL). However, if *all* the
1414  * arms produce NULL then the result is NULL, so we can take
1415  * the intersection of the sets of nonnullable rels, just as
1416  * for OR. Fall through to share code.
1417  */
1418  /* FALL THRU */
1419  case OR_EXPR:
1420 
1421  /*
1422  * OR is strict if all of its arms are, so we can take the
1423  * intersection of the sets of nonnullable rels for each arm.
1424  * This works for both values of top_level.
1425  */
1426  foreach(l, expr->args)
1427  {
1428  Relids subresult;
1429 
1430  subresult = find_nonnullable_rels_walker(lfirst(l),
1431  top_level);
1432  if (result == NULL) /* first subresult? */
1433  result = subresult;
1434  else
1435  result = bms_int_members(result, subresult);
1436 
1437  /*
1438  * If the intersection is empty, we can stop looking. This
1439  * also justifies the test for first-subresult above.
1440  */
1441  if (bms_is_empty(result))
1442  break;
1443  }
1444  break;
1445  case NOT_EXPR:
1446  /* NOT will return null if its arg is null */
1447  result = find_nonnullable_rels_walker((Node *) expr->args,
1448  false);
1449  break;
1450  default:
1451  elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1452  break;
1453  }
1454  }
1455  else if (IsA(node, RelabelType))
1456  {
1457  RelabelType *expr = (RelabelType *) node;
1458 
1459  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1460  }
1461  else if (IsA(node, CoerceViaIO))
1462  {
1463  /* not clear this is useful, but it can't hurt */
1464  CoerceViaIO *expr = (CoerceViaIO *) node;
1465 
1466  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1467  }
1468  else if (IsA(node, ArrayCoerceExpr))
1469  {
1470  /* ArrayCoerceExpr is strict at the array level; ignore elemexpr */
1471  ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1472 
1473  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1474  }
1475  else if (IsA(node, ConvertRowtypeExpr))
1476  {
1477  /* not clear this is useful, but it can't hurt */
1478  ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1479 
1480  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1481  }
1482  else if (IsA(node, CollateExpr))
1483  {
1484  CollateExpr *expr = (CollateExpr *) node;
1485 
1486  result = find_nonnullable_rels_walker((Node *) expr->arg, top_level);
1487  }
1488  else if (IsA(node, NullTest))
1489  {
1490  /* IS NOT NULL can be considered strict, but only at top level */
1491  NullTest *expr = (NullTest *) node;
1492 
1493  if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
1494  result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1495  }
1496  else if (IsA(node, BooleanTest))
1497  {
1498  /* Boolean tests that reject NULL are strict at top level */
1499  BooleanTest *expr = (BooleanTest *) node;
1500 
1501  if (top_level &&
1502  (expr->booltesttype == IS_TRUE ||
1503  expr->booltesttype == IS_FALSE ||
1504  expr->booltesttype == IS_NOT_UNKNOWN))
1505  result = find_nonnullable_rels_walker((Node *) expr->arg, false);
1506  }
1507  else if (IsA(node, SubPlan))
1508  {
1509  SubPlan *splan = (SubPlan *) node;
1510 
1511  /*
1512  * For some types of SubPlan, we can infer strictness from Vars in the
1513  * testexpr (the LHS of the original SubLink).
1514  *
1515  * For ANY_SUBLINK, if the subquery produces zero rows, the result is
1516  * always FALSE. If the subquery produces more than one row, the
1517  * per-row results of the testexpr are combined using OR semantics.
1518  * Hence ANY_SUBLINK can be strict only at top level, but there it's
1519  * as strict as the testexpr is.
1520  *
1521  * For ROWCOMPARE_SUBLINK, if the subquery produces zero rows, the
1522  * result is always NULL. Otherwise, the result is as strict as the
1523  * testexpr is. So we can check regardless of top_level.
1524  *
1525  * We can't prove anything for other sublink types (in particular,
1526  * note that ALL_SUBLINK will return TRUE if the subquery is empty).
1527  */
1528  if ((top_level && splan->subLinkType == ANY_SUBLINK) ||
1529  splan->subLinkType == ROWCOMPARE_SUBLINK)
1530  result = find_nonnullable_rels_walker(splan->testexpr, top_level);
1531  }
1532  else if (IsA(node, PlaceHolderVar))
1533  {
1534  PlaceHolderVar *phv = (PlaceHolderVar *) node;
1535 
1536  /*
1537  * If the contained expression forces any rels non-nullable, so does
1538  * the PHV.
1539  */
1540  result = find_nonnullable_rels_walker((Node *) phv->phexpr, top_level);
1541 
1542  /*
1543  * If the PHV's syntactic scope is exactly one rel, it will be forced
1544  * to be evaluated at that rel, and so it will behave like a Var of
1545  * that rel: if the rel's entire output goes to null, so will the PHV.
1546  * (If the syntactic scope is a join, we know that the PHV will go to
1547  * null if the whole join does; but that is AND semantics while we
1548  * need OR semantics for find_nonnullable_rels' result, so we can't do
1549  * anything with the knowledge.)
1550  */
1551  if (phv->phlevelsup == 0 &&
1552  bms_membership(phv->phrels) == BMS_SINGLETON)
1553  result = bms_add_members(result, phv->phrels);
1554  }
1555  return result;
1556 }
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:953
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:796
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:906
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:704
BMS_Membership bms_membership(const Bitmapset *a)
Definition: bitmapset.c:675
@ BMS_SINGLETON
Definition: bitmapset.h:74
static bool is_strict_saop(ScalarArrayOpExpr *expr, bool falseOK)
Definition: clauses.c:1906
@ ANY_SUBLINK
Definition: primnodes.h:826
@ ROWCOMPARE_SUBLINK
Definition: primnodes.h:827
static SPIPlanPtr splan
Definition: regress.c:265
Index phlevelsup
Definition: pathnodes.h:2633

References AND_EXPR, ANY_SUBLINK, RelabelType::arg, CoerceViaIO::arg, ArrayCoerceExpr::arg, ConvertRowtypeExpr::arg, CollateExpr::arg, NullTest::arg, BooleanTest::arg, NullTest::argisrow, FuncExpr::args, OpExpr::args, ScalarArrayOpExpr::args, BoolExpr::args, bms_add_members(), bms_int_members(), bms_is_empty(), bms_join(), bms_make_singleton(), bms_membership(), BMS_SINGLETON, BoolExpr::boolop, BooleanTest::booltesttype, elog(), ERROR, func_strict(), FuncExpr::funcid, IS_FALSE, IS_NOT_NULL, IS_NOT_UNKNOWN, is_strict_saop(), IS_TRUE, IsA, lfirst, NOT_EXPR, NullTest::nulltesttype, OR_EXPR, PlaceHolderVar::phlevelsup, ROWCOMPARE_SUBLINK, set_opfuncid(), splan, Var::varlevelsup, and Var::varno.

Referenced by find_nonnullable_rels().

◆ find_nonnullable_vars()

List* find_nonnullable_vars ( Node clause)

Definition at line 1587 of file clauses.c.

1588 {
1589  return find_nonnullable_vars_walker(clause, true);
1590 }
static List * find_nonnullable_vars_walker(Node *node, bool top_level)
Definition: clauses.c:1593

References find_nonnullable_vars_walker().

Referenced by reduce_outer_joins_pass2().

◆ find_nonnullable_vars_walker()

static List * find_nonnullable_vars_walker ( Node node,
bool  top_level 
)
static

Definition at line 1593 of file clauses.c.

1594 {
1595  List *result = NIL;
1596  ListCell *l;
1597 
1598  if (node == NULL)
1599  return NIL;
1600  if (IsA(node, Var))
1601  {
1602  Var *var = (Var *) node;
1603 
1604  if (var->varlevelsup == 0)
1605  result = mbms_add_member(result,
1606  var->varno,
1608  }
1609  else if (IsA(node, List))
1610  {
1611  /*
1612  * At top level, we are examining an implicit-AND list: if any of the
1613  * arms produces FALSE-or-NULL then the result is FALSE-or-NULL. If
1614  * not at top level, we are examining the arguments of a strict
1615  * function: if any of them produce NULL then the result of the
1616  * function must be NULL. So in both cases, the set of nonnullable
1617  * vars is the union of those found in the arms, and we pass down the
1618  * top_level flag unmodified.
1619  */
1620  foreach(l, (List *) node)
1621  {
1622  result = mbms_add_members(result,
1624  top_level));
1625  }
1626  }
1627  else if (IsA(node, FuncExpr))
1628  {
1629  FuncExpr *expr = (FuncExpr *) node;
1630 
1631  if (func_strict(expr->funcid))
1632  result = find_nonnullable_vars_walker((Node *) expr->args, false);
1633  }
1634  else if (IsA(node, OpExpr))
1635  {
1636  OpExpr *expr = (OpExpr *) node;
1637 
1638  set_opfuncid(expr);
1639  if (func_strict(expr->opfuncid))
1640  result = find_nonnullable_vars_walker((Node *) expr->args, false);
1641  }
1642  else if (IsA(node, ScalarArrayOpExpr))
1643  {
1644  ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
1645 
1646  if (is_strict_saop(expr, true))
1647  result = find_nonnullable_vars_walker((Node *) expr->args, false);
1648  }
1649  else if (IsA(node, BoolExpr))
1650  {
1651  BoolExpr *expr = (BoolExpr *) node;
1652 
1653  switch (expr->boolop)
1654  {
1655  case AND_EXPR:
1656 
1657  /*
1658  * At top level we can just recurse (to the List case), since
1659  * the result should be the union of what we can prove in each
1660  * arm.
1661  */
1662  if (top_level)
1663  {
1664  result = find_nonnullable_vars_walker((Node *) expr->args,
1665  top_level);
1666  break;
1667  }
1668 
1669  /*
1670  * Below top level, even if one arm produces NULL, the result
1671  * could be FALSE (hence not NULL). However, if *all* the
1672  * arms produce NULL then the result is NULL, so we can take
1673  * the intersection of the sets of nonnullable vars, just as
1674  * for OR. Fall through to share code.
1675  */
1676  /* FALL THRU */
1677  case OR_EXPR:
1678 
1679  /*
1680  * OR is strict if all of its arms are, so we can take the
1681  * intersection of the sets of nonnullable vars for each arm.
1682  * This works for both values of top_level.
1683  */
1684  foreach(l, expr->args)
1685  {
1686  List *subresult;
1687 
1688  subresult = find_nonnullable_vars_walker(lfirst(l),
1689  top_level);
1690  if (result == NIL) /* first subresult? */
1691  result = subresult;
1692  else
1693  result = mbms_int_members(result, subresult);
1694 
1695  /*
1696  * If the intersection is empty, we can stop looking. This
1697  * also justifies the test for first-subresult above.
1698  */
1699  if (result == NIL)
1700  break;
1701  }
1702  break;
1703  case NOT_EXPR:
1704  /* NOT will return null if its arg is null */
1705  result = find_nonnullable_vars_walker((Node *) expr->args,
1706  false);
1707  break;
1708  default:
1709  elog(ERROR, "unrecognized boolop: %d", (int) expr->boolop);
1710  break;
1711  }
1712  }
1713  else if (IsA(node, RelabelType))
1714  {
1715  RelabelType *expr = (RelabelType *) node;
1716 
1717  result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1718  }
1719  else if (IsA(node, CoerceViaIO))
1720  {
1721  /* not clear this is useful, but it can't hurt */
1722  CoerceViaIO *expr = (CoerceViaIO *) node;
1723 
1724  result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1725  }
1726  else if (IsA(node, ArrayCoerceExpr))
1727  {
1728  /* ArrayCoerceExpr is strict at the array level; ignore elemexpr */
1729  ArrayCoerceExpr *expr = (ArrayCoerceExpr *) node;
1730 
1731  result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1732  }
1733  else if (IsA(node, ConvertRowtypeExpr))
1734  {
1735  /* not clear this is useful, but it can't hurt */
1736  ConvertRowtypeExpr *expr = (ConvertRowtypeExpr *) node;
1737 
1738  result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1739  }
1740  else if (IsA(node, CollateExpr))
1741  {
1742  CollateExpr *expr = (CollateExpr *) node;
1743 
1744  result = find_nonnullable_vars_walker((Node *) expr->arg, top_level);
1745  }
1746  else if (IsA(node, NullTest))
1747  {
1748  /* IS NOT NULL can be considered strict, but only at top level */
1749  NullTest *expr = (NullTest *) node;
1750 
1751  if (top_level && expr->nulltesttype == IS_NOT_NULL && !expr->argisrow)
1752  result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1753  }
1754  else if (IsA(node, BooleanTest))
1755  {
1756  /* Boolean tests that reject NULL are strict at top level */
1757  BooleanTest *expr = (BooleanTest *) node;
1758 
1759  if (top_level &&
1760  (expr->booltesttype == IS_TRUE ||
1761  expr->booltesttype == IS_FALSE ||
1762  expr->booltesttype == IS_NOT_UNKNOWN))
1763  result = find_nonnullable_vars_walker((Node *) expr->arg, false);
1764  }
1765  else if (IsA(node, SubPlan))
1766  {
1767  SubPlan *splan = (SubPlan *) node;
1768 
1769  /* See analysis in find_nonnullable_rels_walker */
1770  if ((top_level && splan->subLinkType == ANY_SUBLINK) ||
1771  splan->subLinkType == ROWCOMPARE_SUBLINK)
1772  result = find_nonnullable_vars_walker(splan->testexpr, top_level);
1773  }
1774  else if (IsA(node, PlaceHolderVar))
1775  {
1776  PlaceHolderVar *phv = (PlaceHolderVar *) node;
1777 
1778  result = find_nonnullable_vars_walker((Node *) phv->phexpr, top_level);
1779  }
1780  return result;
1781 }
List * mbms_int_members(List *a, const List *b)

References AND_EXPR, ANY_SUBLINK, RelabelType::arg, CoerceViaIO::arg, ArrayCoerceExpr::arg, ConvertRowtypeExpr::arg, CollateExpr::arg, NullTest::arg, BooleanTest::arg, NullTest::argisrow, FuncExpr::args, OpExpr::args, ScalarArrayOpExpr::args, BoolExpr::args, BoolExpr::boolop, BooleanTest::booltesttype, elog(), ERROR, FirstLowInvalidHeapAttributeNumber, func_strict(), FuncExpr::funcid, IS_FALSE, IS_NOT_NULL, IS_NOT_UNKNOWN, is_strict_saop(), IS_TRUE, IsA, lfirst, mbms_add_member(), mbms_add_members(), mbms_int_members(), NIL, NOT_EXPR, NullTest::nulltesttype, OR_EXPR, ROWCOMPARE_SUBLINK, set_opfuncid(), splan, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by find_nonnullable_vars().

◆ 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:1230
void * palloc(Size size)
Definition: mcxt.c:1199
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().

◆ find_window_functions_walker()

static bool find_window_functions_walker ( Node node,
WindowFuncLists lists 
)
static

Definition at line 239 of file clauses.c.

240 {
241  if (node == NULL)
242  return false;
243  if (IsA(node, WindowFunc))
244  {
245  WindowFunc *wfunc = (WindowFunc *) node;
246 
247  /* winref is unsigned, so one-sided test is OK */
248  if (wfunc->winref > lists->maxWinRef)
249  elog(ERROR, "WindowFunc contains out-of-range winref %u",
250  wfunc->winref);
251  /* eliminate duplicates, so that we avoid repeated computation */
252  if (!list_member(lists->windowFuncs[wfunc->winref], wfunc))
253  {
254  lists->windowFuncs[wfunc->winref] =
255  lappend(lists->windowFuncs[wfunc->winref], wfunc);
256  lists->numWindowFuncs++;
257  }
258 
259  /*
260  * We assume that the parser checked that there are no window
261  * functions in the arguments or filter clause. Hence, we need not
262  * recurse into them. (If either the parser or the planner screws up
263  * on this point, the executor will still catch it; see ExecInitExpr.)
264  */
265  return false;
266  }
267  Assert(!IsA(node, SubLink));
269  (void *) lists);
270 }
bool list_member(const List *list, const void *datum)
Definition: list.c:660

References Assert(), elog(), ERROR, expression_tree_walker, IsA, lappend(), list_member(), WindowFuncLists::maxWinRef, WindowFuncLists::numWindowFuncs, WindowFuncLists::windowFuncs, and WindowFunc::winref.

Referenced by find_window_functions().

◆ inline_function()

static Expr * inline_function ( Oid  funcid,
Oid  result_type,
Oid  result_collid,
Oid  input_collid,
List args,
bool  funcvariadic,
HeapTuple  func_tuple,
eval_const_expressions_context context 
)
static

Definition at line 4381 of file clauses.c.

4386 {
4387  Form_pg_proc funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
4388  char *src;
4389  Datum tmp;
4390  bool isNull;
4391  MemoryContext oldcxt;
4392  MemoryContext mycxt;
4393  inline_error_callback_arg callback_arg;
4394  ErrorContextCallback sqlerrcontext;
4395  FuncExpr *fexpr;
4397  TupleDesc rettupdesc;
4398  ParseState *pstate;
4399  List *raw_parsetree_list;
4400  List *querytree_list;
4401  Query *querytree;
4402  Node *newexpr;
4403  int *usecounts;
4404  ListCell *arg;
4405  int i;
4406 
4407  /*
4408  * Forget it if the function is not SQL-language or has other showstopper
4409  * properties. (The prokind and nargs checks are just paranoia.)
4410  */
4411  if (funcform->prolang != SQLlanguageId ||
4412  funcform->prokind != PROKIND_FUNCTION ||
4413  funcform->prosecdef ||
4414  funcform->proretset ||
4415  funcform->prorettype == RECORDOID ||
4416  !heap_attisnull(func_tuple, Anum_pg_proc_proconfig, NULL) ||
4417  funcform->pronargs != list_length(args))
4418  return NULL;
4419 
4420  /* Check for recursive function, and give up trying to expand if so */
4421  if (list_member_oid(context->active_fns, funcid))
4422  return NULL;
4423 
4424  /* Check permission to call function (fail later, if not) */
4425  if (object_aclcheck(ProcedureRelationId, funcid, GetUserId(), ACL_EXECUTE) != ACLCHECK_OK)
4426  return NULL;
4427 
4428  /* Check whether a plugin wants to hook function entry/exit */
4429  if (FmgrHookIsNeeded(funcid))
4430  return NULL;
4431 
4432  /*
4433  * Make a temporary memory context, so that we don't leak all the stuff
4434  * that parsing might create.
4435  */
4437  "inline_function",
4439  oldcxt = MemoryContextSwitchTo(mycxt);
4440 
4441  /*
4442  * We need a dummy FuncExpr node containing the already-simplified
4443  * arguments. (In some cases we don't really need it, but building it is
4444  * cheap enough that it's not worth contortions to avoid.)
4445  */
4446  fexpr = makeNode(FuncExpr);
4447  fexpr->funcid = funcid;
4448  fexpr->funcresulttype = result_type;
4449  fexpr->funcretset = false;
4450  fexpr->funcvariadic = funcvariadic;
4451  fexpr->funcformat = COERCE_EXPLICIT_CALL; /* doesn't matter */
4452  fexpr->funccollid = result_collid; /* doesn't matter */
4453  fexpr->inputcollid = input_collid;
4454  fexpr->args = args;
4455  fexpr->location = -1;
4456 
4457  /* Fetch the function body */
4458  tmp = SysCacheGetAttr(PROCOID,
4459  func_tuple,
4460  Anum_pg_proc_prosrc,
4461  &isNull);
4462  if (isNull)
4463  elog(ERROR, "null prosrc for function %u", funcid);
4464  src = TextDatumGetCString(tmp);
4465 
4466  /*
4467  * Setup error traceback support for ereport(). This is so that we can
4468  * finger the function that bad information came from.
4469  */
4470  callback_arg.proname = NameStr(funcform->proname);
4471  callback_arg.prosrc = src;
4472 
4473  sqlerrcontext.callback = sql_inline_error_callback;
4474  sqlerrcontext.arg = (void *) &callback_arg;
4475  sqlerrcontext.previous = error_context_stack;
4476  error_context_stack = &sqlerrcontext;
4477 
4478  /* If we have prosqlbody, pay attention to that not prosrc */
4479  tmp = SysCacheGetAttr(PROCOID,
4480  func_tuple,
4481  Anum_pg_proc_prosqlbody,
4482  &isNull);
4483  if (!isNull)
4484  {
4485  Node *n;
4486  List *query_list;
4487 
4489  if (IsA(n, List))
4490  query_list = linitial_node(List, castNode(List, n));
4491  else
4492  query_list = list_make1(n);
4493  if (list_length(query_list) != 1)
4494  goto fail;
4495  querytree = linitial(query_list);
4496 
4497  /*
4498  * Because we'll insist below that the querytree have an empty rtable
4499  * and no sublinks, it cannot have any relation references that need
4500  * to be locked or rewritten. So we can omit those steps.
4501  */
4502  }
4503  else
4504  {
4505  /* Set up to handle parameters while parsing the function body. */
4506  pinfo = prepare_sql_fn_parse_info(func_tuple,
4507  (Node *) fexpr,
4508  input_collid);
4509 
4510  /*
4511  * We just do parsing and parse analysis, not rewriting, because
4512  * rewriting will not affect table-free-SELECT-only queries, which is
4513  * all that we care about. Also, we can punt as soon as we detect
4514  * more than one command in the function body.
4515  */
4516  raw_parsetree_list = pg_parse_query(src);
4517  if (list_length(raw_parsetree_list) != 1)
4518  goto fail;
4519 
4520  pstate = make_parsestate(NULL);
4521  pstate->p_sourcetext = src;
4522  sql_fn_parser_setup(pstate, pinfo);
4523 
4524  querytree = transformTopLevelStmt(pstate, linitial(raw_parsetree_list));
4525 
4526  free_parsestate(pstate);
4527  }
4528 
4529  /*
4530  * The single command must be a simple "SELECT expression".
4531  *
4532  * Note: if you change the tests involved in this, see also plpgsql's
4533  * exec_simple_check_plan(). That generally needs to have the same idea
4534  * of what's a "simple expression", so that inlining a function that
4535  * previously wasn't inlined won't change plpgsql's conclusion.
4536  */
4537  if (!IsA(querytree, Query) ||
4538  querytree->commandType != CMD_SELECT ||
4539  querytree->hasAggs ||
4540  querytree->hasWindowFuncs ||
4541  querytree->hasTargetSRFs ||
4542  querytree->hasSubLinks ||
4543  querytree->cteList ||
4544  querytree->rtable ||
4545  querytree->jointree->fromlist ||
4546  querytree->jointree->quals ||
4547  querytree->groupClause ||
4548  querytree->groupingSets ||
4549  querytree->havingQual ||
4550  querytree->windowClause ||
4551  querytree->distinctClause ||
4552  querytree->sortClause ||
4553  querytree->limitOffset ||
4554  querytree->limitCount ||
4555  querytree->setOperations ||
4556  list_length(querytree->targetList) != 1)
4557  goto fail;
4558 
4559  /* If the function result is composite, resolve it */
4560  (void) get_expr_result_type((Node *) fexpr,
4561  NULL,
4562  &rettupdesc);
4563 
4564  /*
4565  * Make sure the function (still) returns what it's declared to. This
4566  * will raise an error if wrong, but that's okay since the function would
4567  * fail at runtime anyway. Note that check_sql_fn_retval will also insert
4568  * a coercion if needed to make the tlist expression match the declared
4569  * type of the function.
4570  *
4571  * Note: we do not try this until we have verified that no rewriting was
4572  * needed; that's probably not important, but let's be careful.
4573  */
4574  querytree_list = list_make1(querytree);
4575  if (check_sql_fn_retval(list_make1(querytree_list),
4576  result_type, rettupdesc,
4577  false, NULL))
4578  goto fail; /* reject whole-tuple-result cases */
4579 
4580  /*
4581  * Given the tests above, check_sql_fn_retval shouldn't have decided to
4582  * inject a projection step, but let's just make sure.
4583  */
4584  if (querytree != linitial(querytree_list))
4585  goto fail;
4586 
4587  /* Now we can grab the tlist expression */
4588  newexpr = (Node *) ((TargetEntry *) linitial(querytree->targetList))->expr;
4589 
4590  /*
4591  * If the SQL function returns VOID, we can only inline it if it is a
4592  * SELECT of an expression returning VOID (ie, it's just a redirection to
4593  * another VOID-returning function). In all non-VOID-returning cases,
4594  * check_sql_fn_retval should ensure that newexpr returns the function's
4595  * declared result type, so this test shouldn't fail otherwise; but we may
4596  * as well cope gracefully if it does.
4597  */
4598  if (exprType(newexpr) != result_type)
4599  goto fail;
4600 
4601  /*
4602  * Additional validity checks on the expression. It mustn't be more
4603  * volatile than the surrounding function (this is to avoid breaking hacks
4604  * that involve pretending a function is immutable when it really ain't).
4605  * If the surrounding function is declared strict, then the expression
4606  * must contain only strict constructs and must use all of the function
4607  * parameters (this is overkill, but an exact analysis is hard).
4608  */
4609  if (funcform->provolatile == PROVOLATILE_IMMUTABLE &&
4610  contain_mutable_functions(newexpr))
4611  goto fail;
4612  else if (funcform->provolatile == PROVOLATILE_STABLE &&
4613  contain_volatile_functions(newexpr))
4614  goto fail;
4615 
4616  if (funcform->proisstrict &&
4617  contain_nonstrict_functions(newexpr))
4618  goto fail;
4619 
4620  /*
4621  * If any parameter expression contains a context-dependent node, we can't
4622  * inline, for fear of putting such a node into the wrong context.
4623  */
4625  goto fail;
4626 
4627  /*
4628  * We may be able to do it; there are still checks on parameter usage to
4629  * make, but those are most easily done in combination with the actual
4630  * substitution of the inputs. So start building expression with inputs
4631  * substituted.
4632  */
4633  usecounts = (int *) palloc0(funcform->pronargs * sizeof(int));
4634  newexpr = substitute_actual_parameters(newexpr, funcform->pronargs,
4635  args, usecounts);
4636 
4637  /* Now check for parameter usage */
4638  i = 0;
4639  foreach(arg, args)
4640  {
4641  Node *param = lfirst(arg);
4642 
4643  if (usecounts[i] == 0)
4644  {
4645  /* Param not used at all: uncool if func is strict */
4646  if (funcform->proisstrict)
4647  goto fail;
4648  }
4649  else if (usecounts[i] != 1)
4650  {
4651  /* Param used multiple times: uncool if expensive or volatile */
4652  QualCost eval_cost;
4653 
4654  /*
4655  * We define "expensive" as "contains any subplan or more than 10
4656  * operators". Note that the subplan search has to be done
4657  * explicitly, since cost_qual_eval() will barf on unplanned
4658  * subselects.
4659  */
4660  if (contain_subplans(param))
4661  goto fail;
4662  cost_qual_eval(&eval_cost, list_make1(param), NULL);
4663  if (eval_cost.startup + eval_cost.per_tuple >
4664  10 * cpu_operator_cost)
4665  goto fail;
4666 
4667  /*
4668  * Check volatility last since this is more expensive than the
4669  * above tests
4670  */
4671  if (contain_volatile_functions(param))
4672  goto fail;
4673  }
4674  i++;
4675  }
4676 
4677  /*
4678  * Whew --- we can make the substitution. Copy the modified expression
4679  * out of the temporary memory context, and clean up.
4680  */
4681  MemoryContextSwitchTo(oldcxt);
4682 
4683  newexpr = copyObject(newexpr);
4684 
4685  MemoryContextDelete(mycxt);
4686 
4687  /*
4688  * If the result is of a collatable type, force the result to expose the
4689  * correct collation. In most cases this does not matter, but it's
4690  * possible that the function result is used directly as a sort key or in
4691  * other places where we expect exprCollation() to tell the truth.
4692  */
4693  if (OidIsValid(result_collid))
4694  {
4695  Oid exprcoll = exprCollation(newexpr);
4696 
4697  if (OidIsValid(exprcoll) && exprcoll != result_collid)
4698  {
4699  CollateExpr *newnode = makeNode(CollateExpr);
4700 
4701  newnode->arg = (Expr *) newexpr;
4702  newnode->collOid = result_collid;
4703  newnode->location = -1;
4704 
4705  newexpr = (Node *) newnode;
4706  }
4707  }
4708 
4709  /*
4710  * Since there is now no trace of the function in the plan tree, we must
4711  * explicitly record the plan's dependency on the function.
4712  */
4713  if (context->root)
4714  record_plan_function_dependency(context->root, funcid);
4715 
4716  /*
4717  * Recursively try to simplify the modified expression. Here we must add
4718  * the current function to the context list of active functions.
4719  */
4720  context->active_fns = lappend_oid(context->active_fns, funcid);
4721  newexpr = eval_const_expressions_mutator(newexpr, context);
4722  context->active_fns = list_delete_last(context->active_fns);
4723 
4724  error_context_stack = sqlerrcontext.previous;
4725 
4726  return (Expr *) newexpr;
4727 
4728  /* Here if func is not inlinable: release temp memory and return NULL */
4729 fail:
4730  MemoryContextSwitchTo(oldcxt);
4731  MemoryContextDelete(mycxt);
4732  error_context_stack = sqlerrcontext.previous;
4733 
4734  return NULL;
4735 }
Datum querytree(PG_FUNCTION_ARGS)
Definition: _int_bool.c:663
@ ACLCHECK_OK
Definition: acl.h:184
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
Definition: aclchk.c:4598
#define NameStr(name)
Definition: c.h:682
static Node * substitute_actual_parameters(Node *expr, int nargs, List *args, int *usecounts)
Definition: clauses.c:4741
static void sql_inline_error_callback(void *arg)
Definition: clauses.c:4783
static bool contain_context_dependent_node(Node *clause)
Definition: clauses.c:1060
bool contain_nonstrict_functions(Node *clause)
Definition: clauses.c:874
bool contain_subplans(Node *clause)
Definition: clauses.c:330
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:448
double cpu_operator_cost
Definition: costsize.c:124
void cost_qual_eval(QualCost *cost, List *quals, PlannerInfo *root)
Definition: costsize.c:4368
ErrorContextCallback * error_context_stack
Definition: elog.c:94
#define FmgrHookIsNeeded(fn_oid)
Definition: fmgr.h:789
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:292
bool check_sql_fn_retval(List *queryTreeLists, Oid rettype, TupleDesc rettupdesc, bool insertDroppedCols, List **resultTargetList)
Definition: functions.c:1612
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
int i
Definition: isn.c:73
List * lappend_oid(List *list, Oid datum)
Definition: list.c:374
bool list_member_oid(const List *list, Oid datum)
Definition: list.c:721
List * list_delete_last(List *list)
Definition: list.c:956
MemoryContext CurrentMemoryContext
Definition: mcxt.c:124
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:376
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
Oid GetUserId(void)
Definition: miscinit.c:497
@ CMD_SELECT
Definition: nodes.h:265
void free_parsestate(ParseState *pstate)
Definition: parse_node.c:76
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:43
#define ACL_EXECUTE
Definition: parsenodes.h:90
Query * transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
Definition: analyze.c:250
#define linitial_node(type, l)
Definition: pg_list.h:179
List * pg_parse_query(const char *query_string)
Definition: postgres.c:578
void record_plan_function_dependency(PlannerInfo *root, Oid funcid)
Definition: setrefs.c:3259
struct ErrorContextCallback * previous
Definition: elog.h:234
void(* callback)(void *arg)
Definition: elog.h:235
const char * p_sourcetext
Definition: parse_node.h:185
Cost per_tuple
Definition: pathnodes.h:48
Cost startup
Definition: pathnodes.h:47

References ACL_EXECUTE, ACLCHECK_OK, eval_const_expressions_context::active_fns, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, arg, CollateExpr::arg, ErrorContextCallback::arg, generate_unaccent_rules::args, FuncExpr::args, ErrorContextCallback::callback, castNode, check_sql_fn_retval(), CMD_SELECT, COERCE_EXPLICIT_CALL, CollateExpr::collOid, contain_context_dependent_node(), contain_mutable_functions(), contain_nonstrict_functions(), contain_subplans(), contain_volatile_functions(), copyObject, cost_qual_eval(), cpu_operator_cost, CurrentMemoryContext, elog(), ERROR, error_context_stack, eval_const_expressions_mutator(), exprCollation(), exprType(), FmgrHookIsNeeded, free_parsestate(), FuncExpr::funccollid, FuncExpr::funcformat, FuncExpr::funcid, FuncExpr::funcresulttype, FuncExpr::funcretset, FuncExpr::funcvariadic, get_expr_result_type(), GETSTRUCT, GetUserId(), heap_attisnull(), i, FuncExpr::inputcollid, IsA, lappend_oid(), lfirst, linitial, linitial_node, list_delete_last(), list_length(), list_make1, list_member_oid(), FuncExpr::location, CollateExpr::location, make_parsestate(), makeNode, MemoryContextDelete(), MemoryContextSwitchTo(), NameStr, object_aclcheck(), OidIsValid, ParseState::p_sourcetext, palloc0(), QualCost::per_tuple, pg_parse_query(), prepare_sql_fn_parse_info(), ErrorContextCallback::previous, PROCOID, inline_error_callback_arg::proname, inline_error_callback_arg::prosrc, querytree(), record_plan_function_dependency(), eval_const_expressions_context::root, sql_fn_parser_setup(), sql_inline_error_callback(), QualCost::startup, stringToNode(), substitute_actual_parameters(), SysCacheGetAttr(), TextDatumGetCString, and transformTopLevelStmt().

Referenced by simplify_function().

◆ inline_set_returning_function()

Query* inline_set_returning_function ( PlannerInfo root,
RangeTblEntry rte 
)

Definition at line 4899 of file clauses.c.

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

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, object_aclcheck(), ObjectIdGetDatum(), pg_analyze_and_rewrite_withcb(), pg_parse_query(), 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 634 of file clauses.c.

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

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

1969 {
1970  /*
1971  * We could implement this check in one recursive scan. But since the
1972  * check for volatile functions is both moderately expensive and unlikely
1973  * to fail, it seems better to look for Vars first and only check for
1974  * volatile functions if we find no Vars.
1975  */
1976  if (!contain_var_clause(clause) &&
1977  !contain_volatile_functions(clause))
1978  return true;
1979  return false;
1980 }

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

1989 {
1990  if (bms_is_empty(relids) &&
1991  !contain_volatile_functions(clause))
1992  return true;
1993  return false;
1994 }

References bms_is_empty(), and contain_volatile_functions().

Referenced by clauselist_selectivity_ext().

◆ is_strict_saop()

static bool is_strict_saop ( ScalarArrayOpExpr expr,
bool  falseOK 
)
static

Definition at line 1906 of file clauses.c.

1907 {
1908  Node *rightop;
1909 
1910  /* The contained operator must be strict. */
1911  set_sa_opfuncid(expr);
1912  if (!func_strict(expr->opfuncid))
1913  return false;
1914  /* If ANY and falseOK, that's all we need to check. */
1915  if (expr->useOr && falseOK)
1916  return true;
1917  /* Else, we have to see if the array is provably non-empty. */
1918  Assert(list_length(expr->args) == 2);
1919  rightop = (Node *) lsecond(expr->args);
1920  if (rightop && IsA(rightop, Const))
1921  {
1922  Datum arraydatum = ((Const *) rightop)->constvalue;
1923  bool arrayisnull = ((Const *) rightop)->constisnull;
1924  ArrayType *arrayval;
1925  int nitems;
1926 
1927  if (arrayisnull)
1928  return false;
1929  arrayval = DatumGetArrayTypeP(arraydatum);
1930  nitems = ArrayGetNItems(ARR_NDIM(arrayval), ARR_DIMS(arrayval));
1931  if (nitems > 0)
1932  return true;
1933  }
1934  else if (rightop && IsA(rightop, ArrayExpr))
1935  {
1936  ArrayExpr *arrayexpr = (ArrayExpr *) rightop;
1937 
1938  if (arrayexpr->elements != NIL && !arrayexpr->multidims)
1939  return true;
1940  }
1941  return false;
1942 }
bool multidims
Definition: primnodes.h:1179
List * elements
Definition: primnodes.h:1178

References ScalarArrayOpExpr::args, ARR_DIMS, ARR_NDIM, ArrayGetNItems(), Assert(), DatumGetArrayTypeP, ArrayExpr::elements, func_strict(), IsA, list_length(), lsecond, ArrayExpr::multidims, NIL, set_sa_opfuncid(), and ScalarArrayOpExpr::useOr.

Referenced by find_nonnullable_rels_walker(), and find_nonnullable_vars_walker().

◆ max_parallel_hazard()

char max_parallel_hazard ( Query parse)

Definition at line 615 of file clauses.c.

616 {
618 
619  context.max_hazard = PROPARALLEL_SAFE;
620  context.max_interesting = PROPARALLEL_UNSAFE;
621  context.safe_param_ids = NIL;
622  (void) max_parallel_hazard_walker((Node *) parse, &context);
623  return context.max_hazard;
624 }
static struct subre * parse(struct vars *v, int stopper, int type, struct state *init, struct state *final)
Definition: regcomp.c:717

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

◆ max_parallel_hazard_checker()

static bool max_parallel_hazard_checker ( Oid  func_id,
void *  context 
)
static

Definition at line 703 of file clauses.c.

704 {
705  return max_parallel_hazard_test(func_parallel(func_id),
706  (max_parallel_hazard_context *) context);
707 }
static bool max_parallel_hazard_test(char proparallel, max_parallel_hazard_context *context)
Definition: clauses.c:675
char func_parallel(Oid funcid)
Definition: lsyscache.c:1781

References func_parallel(), and max_parallel_hazard_test().

Referenced by max_parallel_hazard_walker().

◆ max_parallel_hazard_test()

static bool max_parallel_hazard_test ( char  proparallel,
max_parallel_hazard_context context 
)
static

Definition at line 675 of file clauses.c.

676 {
677  switch (proparallel)
678  {
679  case PROPARALLEL_SAFE:
680  /* nothing to see here, move along */
681  break;
682  case PROPARALLEL_RESTRICTED:
683  /* increase max_hazard to RESTRICTED */
684  Assert(context->max_hazard != PROPARALLEL_UNSAFE);
685  context->max_hazard = proparallel;
686  /* done if we are not expecting any unsafe functions */
687  if (context->max_interesting == proparallel)
688  return true;
689  break;
690  case PROPARALLEL_UNSAFE:
691  context->max_hazard = proparallel;
692  /* we're always done at the first unsafe construct */
693  return true;
694  default:
695  elog(ERROR, "unrecognized proparallel value \"%c\"", proparallel);
696  break;
697  }
698  return false;
699 }

References Assert(), elog(), ERROR, max_parallel_hazard_context::max_hazard, and max_parallel_hazard_context::max_interesting.

Referenced by max_parallel_hazard_checker(), and max_parallel_hazard_walker().

◆ max_parallel_hazard_walker()

static bool max_parallel_hazard_walker ( Node node,
max_parallel_hazard_context context 
)
static

Definition at line 710 of file clauses.c.

711 {
712  if (node == NULL)
713  return false;
714 
715  /* Check for hazardous functions in node itself */
717  context))
718  return true;
719 
720  /*
721  * It should be OK to treat MinMaxExpr as parallel-safe, since btree
722  * opclass support functions are generally parallel-safe. XmlExpr is a
723  * bit more dubious but we can probably get away with it. We err on the
724  * side of caution by treating CoerceToDomain as parallel-restricted.
725  * (Note: in principle that's wrong because a domain constraint could
726  * contain a parallel-unsafe function; but useful constraints probably
727  * never would have such, and assuming they do would cripple use of
728  * parallel query in the presence of domain types.) NextValueExpr is
729  * parallel-unsafe.
730  */
731  if (IsA(node, CoerceToDomain))
732  {
733  if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
734  return true;
735  }
736 
737  else if (IsA(node, NextValueExpr))
738  {
739  if (max_parallel_hazard_test(PROPARALLEL_UNSAFE, context))
740  return true;
741  }
742 
743  /*
744  * Treat window functions as parallel-restricted because we aren't sure
745  * whether the input row ordering is fully deterministic, and the output
746  * of window functions might vary across workers if not. (In some cases,
747  * like where the window frame orders by a primary key, we could relax
748  * this restriction. But it doesn't currently seem worth expending extra
749  * effort to do so.)
750  */
751  else if (IsA(node, WindowFunc))
752  {
753  if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
754  return true;
755  }
756 
757  /*
758  * As a notational convenience for callers, look through RestrictInfo.
759  */
760  else if (IsA(node, RestrictInfo))
761  {
762  RestrictInfo *rinfo = (RestrictInfo *) node;
763 
764  return max_parallel_hazard_walker((Node *) rinfo->clause, context);
765  }
766 
767  /*
768  * Really we should not see SubLink during a max_interesting == restricted
769  * scan, but if we do, return true.
770  */
771  else if (IsA(node, SubLink))
772  {
773  if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
774  return true;
775  }
776 
777  /*
778  * Only parallel-safe SubPlans can be sent to workers. Within the
779  * testexpr of the SubPlan, Params representing the output columns of the
780  * subplan can be treated as parallel-safe, so temporarily add their IDs
781  * to the safe_param_ids list while examining the testexpr.
782  */
783  else if (IsA(node, SubPlan))
784  {
785  SubPlan *subplan = (SubPlan *) node;
786  List *save_safe_param_ids;
787 
788  if (!subplan->parallel_safe &&
789  max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
790  return true;
791  save_safe_param_ids = context->safe_param_ids;
792  context->safe_param_ids = list_concat_copy(context->safe_param_ids,
793  subplan->paramIds);
794  if (max_parallel_hazard_walker(subplan->testexpr, context))
795  return true; /* no need to restore safe_param_ids */
796  list_free(context->safe_param_ids);
797  context->safe_param_ids = save_safe_param_ids;
798  /* we must also check args, but no special Param treatment there */
799  if (max_parallel_hazard_walker((Node *) subplan->args, context))
800  return true;
801  /* don't want to recurse normally, so we're done */
802  return false;
803  }
804 
805  /*
806  * We can't pass Params to workers at the moment either, so they are also
807  * parallel-restricted, unless they are PARAM_EXTERN Params or are
808  * PARAM_EXEC Params listed in safe_param_ids, meaning they could be
809  * either generated within workers or can be computed by the leader and
810  * then their value can be passed to workers.
811  */
812  else if (IsA(node, Param))
813  {
814  Param *param = (Param *) node;
815 
816  if (param->paramkind == PARAM_EXTERN)
817  return false;
818 
819  if (param->paramkind != PARAM_EXEC ||
820  !list_member_int(context->safe_param_ids, param->paramid))
821  {
822  if (max_parallel_hazard_test(PROPARALLEL_RESTRICTED, context))
823  return true;
824  }
825  return false; /* nothing to recurse to */
826  }
827 
828  /*
829  * When we're first invoked on a completely unplanned tree, we must
830  * recurse into subqueries so to as to locate parallel-unsafe constructs
831  * anywhere in the tree.
832  */
833  else if (IsA(node, Query))
834  {
835  Query *query = (Query *) node;
836 
837  /* SELECT FOR UPDATE/SHARE must be treated as unsafe */
838  if (query->rowMarks != NULL)
839  {
840  context->max_hazard = PROPARALLEL_UNSAFE;
841  return true;
842  }
843 
844  /* Recurse into subselects */
845  return query_tree_walker(query,
847  context, 0);
848  }
849 
850  /* Recurse to check arguments */
851  return expression_tree_walker(node,
853  context);
854 }
static bool max_parallel_hazard_checker(Oid func_id, void *context)
Definition: clauses.c:703
void list_free(List *list)
Definition: list.c:1545
List * rowMarks
Definition: parsenodes.h:189
List * args
Definition: primnodes.h:914
List * paramIds
Definition: primnodes.h:891
Node * testexpr
Definition: primnodes.h:890
bool parallel_safe
Definition: primnodes.h:907

References SubPlan::args, check_functions_in_node(), RestrictInfo::clause, expression_tree_walker, IsA