PostgreSQL Source Code  git master
subselect.c File Reference
Include dependency graph for subselect.c:

Go to the source code of this file.

Data Structures

struct  convert_testexpr_context
 
struct  process_sublinks_context
 
struct  finalize_primnode_context
 
struct  inline_cte_walker_context
 

Typedefs

typedef struct convert_testexpr_context convert_testexpr_context
 
typedef struct process_sublinks_context process_sublinks_context
 
typedef struct finalize_primnode_context finalize_primnode_context
 
typedef struct inline_cte_walker_context inline_cte_walker_context
 

Functions

static Nodebuild_subplan (PlannerInfo *root, Plan *plan, PlannerInfo *subroot, List *plan_params, SubLinkType subLinkType, int subLinkId, Node *testexpr, List *testexpr_paramids, bool unknownEqFalse)
 
static Listgenerate_subquery_params (PlannerInfo *root, List *tlist, List **paramIds)
 
static Listgenerate_subquery_vars (PlannerInfo *root, List *tlist, Index varno)
 
static Nodeconvert_testexpr (PlannerInfo *root, Node *testexpr, List *subst_nodes)
 
static Nodeconvert_testexpr_mutator (Node *node, convert_testexpr_context *context)
 
static bool subplan_is_hashable (Plan *plan)
 
static bool subpath_is_hashable (Path *path)
 
static bool testexpr_is_hashable (Node *testexpr, List *param_ids)
 
static bool test_opexpr_is_hashable (OpExpr *testexpr, List *param_ids)
 
static bool hash_ok_operator (OpExpr *expr)
 
static bool contain_dml (Node *node)
 
static bool contain_dml_walker (Node *node, void *context)
 
static bool contain_outer_selfref (Node *node)
 
static bool contain_outer_selfref_walker (Node *node, Index *depth)
 
static void inline_cte (PlannerInfo *root, CommonTableExpr *cte)
 
static bool inline_cte_walker (Node *node, inline_cte_walker_context *context)
 
static bool simplify_EXISTS_query (PlannerInfo *root, Query *query)
 
static Queryconvert_EXISTS_to_ANY (PlannerInfo *root, Query *subselect, Node **testexpr, List **paramIds)
 
static Nodereplace_correlation_vars_mutator (Node *node, PlannerInfo *root)
 
static Nodeprocess_sublinks_mutator (Node *node, process_sublinks_context *context)
 
static Bitmapsetfinalize_plan (PlannerInfo *root, Plan *plan, int gather_param, Bitmapset *valid_params, Bitmapset *scan_params)
 
static bool finalize_primnode (Node *node, finalize_primnode_context *context)
 
static bool finalize_agg_primnode (Node *node, finalize_primnode_context *context)
 
static void get_first_col_type (Plan *plan, Oid *coltype, int32 *coltypmod, Oid *colcollation)
 
static Nodemake_subplan (PlannerInfo *root, Query *orig_subquery, SubLinkType subLinkType, int subLinkId, Node *testexpr, bool isTopQual)
 
void SS_process_ctes (PlannerInfo *root)
 
JoinExprconvert_ANY_sublink_to_join (PlannerInfo *root, SubLink *sublink, Relids available_rels)
 
JoinExprconvert_EXISTS_sublink_to_join (PlannerInfo *root, SubLink *sublink, bool under_not, Relids available_rels)
 
NodeSS_replace_correlation_vars (PlannerInfo *root, Node *expr)
 
NodeSS_process_sublinks (PlannerInfo *root, Node *expr, bool isQual)
 
void SS_identify_outer_params (PlannerInfo *root)
 
void SS_charge_for_initplans (PlannerInfo *root, RelOptInfo *final_rel)
 
void SS_attach_initplans (PlannerInfo *root, Plan *plan)
 
void SS_finalize_plan (PlannerInfo *root, Plan *plan)
 
ParamSS_make_initplan_output_param (PlannerInfo *root, Oid resulttype, int32 resulttypmod, Oid resultcollation)
 
void SS_make_initplan_from_plan (PlannerInfo *root, PlannerInfo *subroot, Plan *plan, Param *prm)
 

Typedef Documentation

◆ convert_testexpr_context

◆ finalize_primnode_context

◆ inline_cte_walker_context

◆ process_sublinks_context

Function Documentation

◆ build_subplan()

static Node * build_subplan ( PlannerInfo root,
Plan plan,
PlannerInfo subroot,
List plan_params,
SubLinkType  subLinkType,
int  subLinkId,
Node testexpr,
List testexpr_paramids,
bool  unknownEqFalse 
)
static

Definition at line 321 of file subselect.c.

References ANY_SUBLINK, arg, SubPlan::args, ARRAY_SUBLINK, Assert, bms_add_member(), convert_testexpr(), cost_subplan(), elog, enable_material, ERROR, ExecMaterializesOutput(), EXISTS_SUBLINK, TargetEntry::expr, EXPR_SUBLINK, exprCollation(), exprType(), exprTypmod(), SubPlan::firstColCollation, SubPlan::firstColType, SubPlan::firstColTypmod, format_type_be(), generate_new_exec_param(), generate_subquery_params(), get_first_col_type(), get_promoted_array_type(), PlannerInfo::glob, PlannerInfo::init_plans, InvalidOid, IsA, PlannerParamItem::item, lappend(), lappend_int(), lfirst, lfirst_int, linitial, list_copy(), list_length(), list_make1_int, list_nth_cell(), lnext(), makeNode, makeNullConst(), materialize_finished_plan(), PlannerInfo::multiexpr_params, MULTIEXPR_SUBLINK, NIL, nodeTag, OidIsValid, palloc(), Plan::parallel_safe, SubPlan::parallel_safe, Param::paramid, PlannerParamItem::paramId, SubPlan::paramIds, SubPlan::parParam, SubPlan::plan_id, SubPlan::plan_name, TargetEntry::resjunk, PlannerGlobal::rewindPlanIDs, ROWCOMPARE_SUBLINK, SubPlan::setParam, splan, sprintf, SS_process_sublinks(), SubPlan::subLinkType, subplan_is_hashable(), PlannerGlobal::subplans, PlannerGlobal::subroots, Plan::targetlist, SubPlan::testexpr, testexpr_is_hashable(), SubPlan::unknownEqFalse, and SubPlan::useHashTable.

Referenced by make_subplan().

326 {
327  Node *result;
328  SubPlan *splan;
329  bool isInitPlan;
330  ListCell *lc;
331 
332  /*
333  * Initialize the SubPlan node. Note plan_id, plan_name, and cost fields
334  * are set further down.
335  */
336  splan = makeNode(SubPlan);
337  splan->subLinkType = subLinkType;
338  splan->testexpr = NULL;
339  splan->paramIds = NIL;
340  get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
341  &splan->firstColCollation);
342  splan->useHashTable = false;
343  splan->unknownEqFalse = unknownEqFalse;
344  splan->parallel_safe = plan->parallel_safe;
345  splan->setParam = NIL;
346  splan->parParam = NIL;
347  splan->args = NIL;
348 
349  /*
350  * Make parParam and args lists of param IDs and expressions that current
351  * query level will pass to this child plan.
352  */
353  foreach(lc, plan_params)
354  {
355  PlannerParamItem *pitem = (PlannerParamItem *) lfirst(lc);
356  Node *arg = pitem->item;
357 
358  /*
359  * The Var, PlaceHolderVar, or Aggref has already been adjusted to
360  * have the correct varlevelsup, phlevelsup, or agglevelsup.
361  *
362  * If it's a PlaceHolderVar or Aggref, its arguments might contain
363  * SubLinks, which have not yet been processed (see the comments for
364  * SS_replace_correlation_vars). Do that now.
365  */
366  if (IsA(arg, PlaceHolderVar) ||
367  IsA(arg, Aggref))
368  arg = SS_process_sublinks(root, arg, false);
369 
370  splan->parParam = lappend_int(splan->parParam, pitem->paramId);
371  splan->args = lappend(splan->args, arg);
372  }
373 
374  /*
375  * Un-correlated or undirect correlated plans of EXISTS, EXPR, ARRAY,
376  * ROWCOMPARE, or MULTIEXPR types can be used as initPlans. For EXISTS,
377  * EXPR, or ARRAY, we return a Param referring to the result of evaluating
378  * the initPlan. For ROWCOMPARE, we must modify the testexpr tree to
379  * contain PARAM_EXEC Params instead of the PARAM_SUBLINK Params emitted
380  * by the parser, and then return that tree. For MULTIEXPR, we return a
381  * null constant: the resjunk targetlist item containing the SubLink does
382  * not need to return anything useful, since the referencing Params are
383  * elsewhere.
384  */
385  if (splan->parParam == NIL && subLinkType == EXISTS_SUBLINK)
386  {
387  Param *prm;
388 
389  Assert(testexpr == NULL);
390  prm = generate_new_exec_param(root, BOOLOID, -1, InvalidOid);
391  splan->setParam = list_make1_int(prm->paramid);
392  isInitPlan = true;
393  result = (Node *) prm;
394  }
395  else if (splan->parParam == NIL && subLinkType == EXPR_SUBLINK)
396  {
397  TargetEntry *te = linitial(plan->targetlist);
398  Param *prm;
399 
400  Assert(!te->resjunk);
401  Assert(testexpr == NULL);
402  prm = generate_new_exec_param(root,
403  exprType((Node *) te->expr),
404  exprTypmod((Node *) te->expr),
405  exprCollation((Node *) te->expr));
406  splan->setParam = list_make1_int(prm->paramid);
407  isInitPlan = true;
408  result = (Node *) prm;
409  }
410  else if (splan->parParam == NIL && subLinkType == ARRAY_SUBLINK)
411  {
412  TargetEntry *te = linitial(plan->targetlist);
413  Oid arraytype;
414  Param *prm;
415 
416  Assert(!te->resjunk);
417  Assert(testexpr == NULL);
418  arraytype = get_promoted_array_type(exprType((Node *) te->expr));
419  if (!OidIsValid(arraytype))
420  elog(ERROR, "could not find array type for datatype %s",
421  format_type_be(exprType((Node *) te->expr)));
422  prm = generate_new_exec_param(root,
423  arraytype,
424  exprTypmod((Node *) te->expr),
425  exprCollation((Node *) te->expr));
426  splan->setParam = list_make1_int(prm->paramid);
427  isInitPlan = true;
428  result = (Node *) prm;
429  }
430  else if (splan->parParam == NIL && subLinkType == ROWCOMPARE_SUBLINK)
431  {
432  /* Adjust the Params */
433  List *params;
434 
435  Assert(testexpr != NULL);
436  params = generate_subquery_params(root,
437  plan->targetlist,
438  &splan->paramIds);
439  result = convert_testexpr(root,
440  testexpr,
441  params);
442  splan->setParam = list_copy(splan->paramIds);
443  isInitPlan = true;
444 
445  /*
446  * The executable expression is returned to become part of the outer
447  * plan's expression tree; it is not kept in the initplan node.
448  */
449  }
450  else if (subLinkType == MULTIEXPR_SUBLINK)
451  {
452  /*
453  * Whether it's an initplan or not, it needs to set a PARAM_EXEC Param
454  * for each output column.
455  */
456  List *params;
457 
458  Assert(testexpr == NULL);
459  params = generate_subquery_params(root,
460  plan->targetlist,
461  &splan->setParam);
462 
463  /*
464  * Save the list of replacement Params in the n'th cell of
465  * root->multiexpr_params; setrefs.c will use it to replace
466  * PARAM_MULTIEXPR Params.
467  */
468  while (list_length(root->multiexpr_params) < subLinkId)
470  lc = list_nth_cell(root->multiexpr_params, subLinkId - 1);
471  Assert(lfirst(lc) == NIL);
472  lfirst(lc) = params;
473 
474  /* It can be an initplan if there are no parParams. */
475  if (splan->parParam == NIL)
476  {
477  isInitPlan = true;
478  result = (Node *) makeNullConst(RECORDOID, -1, InvalidOid);
479  }
480  else
481  {
482  isInitPlan = false;
483  result = (Node *) splan;
484  }
485  }
486  else
487  {
488  /*
489  * Adjust the Params in the testexpr, unless caller already took care
490  * of it (as indicated by passing a list of Param IDs).
491  */
492  if (testexpr && testexpr_paramids == NIL)
493  {
494  List *params;
495 
496  params = generate_subquery_params(root,
497  plan->targetlist,
498  &splan->paramIds);
499  splan->testexpr = convert_testexpr(root,
500  testexpr,
501  params);
502  }
503  else
504  {
505  splan->testexpr = testexpr;
506  splan->paramIds = testexpr_paramids;
507  }
508 
509  /*
510  * We can't convert subplans of ALL_SUBLINK or ANY_SUBLINK types to
511  * initPlans, even when they are uncorrelated or undirect correlated,
512  * because we need to scan the output of the subplan for each outer
513  * tuple. But if it's a not-direct-correlated IN (= ANY) test, we
514  * might be able to use a hashtable to avoid comparing all the tuples.
515  */
516  if (subLinkType == ANY_SUBLINK &&
517  splan->parParam == NIL &&
518  subplan_is_hashable(plan) &&
519  testexpr_is_hashable(splan->testexpr, splan->paramIds))
520  splan->useHashTable = true;
521 
522  /*
523  * Otherwise, we have the option to tack a Material node onto the top
524  * of the subplan, to reduce the cost of reading it repeatedly. This
525  * is pointless for a direct-correlated subplan, since we'd have to
526  * recompute its results each time anyway. For uncorrelated/undirect
527  * correlated subplans, we add Material unless the subplan's top plan
528  * node would materialize its output anyway. Also, if enable_material
529  * is false, then the user does not want us to materialize anything
530  * unnecessarily, so we don't.
531  */
532  else if (splan->parParam == NIL && enable_material &&
534  plan = materialize_finished_plan(plan);
535 
536  result = (Node *) splan;
537  isInitPlan = false;
538  }
539 
540  /*
541  * Add the subplan and its PlannerInfo to the global lists.
542  */
543  root->glob->subplans = lappend(root->glob->subplans, plan);
544  root->glob->subroots = lappend(root->glob->subroots, subroot);
545  splan->plan_id = list_length(root->glob->subplans);
546 
547  if (isInitPlan)
548  root->init_plans = lappend(root->init_plans, splan);
549 
550  /*
551  * A parameterless subplan (not initplan) should be prepared to handle
552  * REWIND efficiently. If it has direct parameters then there's no point
553  * since it'll be reset on each scan anyway; and if it's an initplan then
554  * there's no point since it won't get re-run without parameter changes
555  * anyway. The input of a hashed subplan doesn't need REWIND either.
556  */
557  if (splan->parParam == NIL && !isInitPlan && !splan->useHashTable)
559  splan->plan_id);
560 
561  /* Label the subplan for EXPLAIN purposes */
562  splan->plan_name = palloc(32 + 12 * list_length(splan->setParam));
563  sprintf(splan->plan_name, "%s %d",
564  isInitPlan ? "InitPlan" : "SubPlan",
565  splan->plan_id);
566  if (splan->setParam)
567  {
568  char *ptr = splan->plan_name + strlen(splan->plan_name);
569 
570  ptr += sprintf(ptr, " (returns ");
571  foreach(lc, splan->setParam)
572  {
573  ptr += sprintf(ptr, "$%d%s",
574  lfirst_int(lc),
575  lnext(splan->setParam, lc) ? "," : ")");
576  }
577  }
578 
579  /* Lastly, fill in the cost estimates for use later */
580  cost_subplan(root, splan, plan);
581 
582  return result;
583 }
static Node * convert_testexpr(PlannerInfo *root, Node *testexpr, List *subst_nodes)
Definition: subselect.c:654
#define NIL
Definition: pg_list.h:65
Oid firstColType
Definition: primnodes.h:756
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
int plan_id
Definition: primnodes.h:752
Param * generate_new_exec_param(PlannerInfo *root, Oid paramtype, int32 paramtypmod, Oid paramcollation)
Definition: paramassign.c:557
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
static ListCell * list_nth_cell(const List *list, int n)
Definition: pg_list.h:256
static void get_first_col_type(Plan *plan, Oid *coltype, int32 *coltypmod, Oid *colcollation)
Definition: subselect.c:119
static bool subplan_is_hashable(Plan *plan)
Definition: subselect.c:724
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
SubLinkType subLinkType
Definition: primnodes.h:747
static List * generate_subquery_params(PlannerInfo *root, List *tlist, List **paramIds)
Definition: subselect.c:592
List * list_copy(const List *oldlist)
Definition: list.c:1418
Definition: nodes.h:539
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
List * paramIds
Definition: primnodes.h:750
Node * SS_process_sublinks(PlannerInfo *root, Node *expr, bool isQual)
Definition: subselect.c:1918
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Definition: makefuncs.c:337
List * multiexpr_params
Definition: pathnodes.h:245
#define sprintf
Definition: port.h:218
bool resjunk
Definition: primnodes.h:1461
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
#define lfirst_int(lc)
Definition: pg_list.h:170
Bitmapset * rewindPlanIDs
Definition: pathnodes.h:99
List * subplans
Definition: pathnodes.h:95
PlannerGlobal * glob
Definition: pathnodes.h:163
Plan * materialize_finished_plan(Plan *subplan)
Definition: createplan.c:6384
static SPIPlanPtr splan
Definition: regress.c:263
#define list_make1_int(x1)
Definition: pg_list.h:221
List * lappend_int(List *list, int datum)
Definition: list.c:354
List * lappend(List *list, void *datum)
Definition: list.c:336
Oid get_promoted_array_type(Oid typid)
Definition: lsyscache.c:2758
Node * testexpr
Definition: primnodes.h:749
List * init_plans
Definition: pathnodes.h:241
#define InvalidOid
Definition: postgres_ext.h:36
int32 firstColTypmod
Definition: primnodes.h:757
#define makeNode(_type_)
Definition: nodes.h:587
char * plan_name
Definition: primnodes.h:754
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List * setParam
Definition: primnodes.h:770
Expr * expr
Definition: primnodes.h:1454
int paramid
Definition: primnodes.h:268
void cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan)
Definition: costsize.c:4103
bool unknownEqFalse
Definition: primnodes.h:763
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
List * subroots
Definition: pathnodes.h:97
List * parParam
Definition: primnodes.h:772
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
bool parallel_safe
Definition: primnodes.h:766
#define nodeTag(nodeptr)
Definition: nodes.h:544
List * targetlist
Definition: plannodes.h:141
void * palloc(Size size)
Definition: mcxt.c:1062
static bool testexpr_is_hashable(Node *testexpr, List *param_ids)
Definition: subselect.c:773
#define elog(elevel,...)
Definition: elog.h:232
void * arg
bool ExecMaterializesOutput(NodeTag plantype)
Definition: execAmi.c:637
bool useHashTable
Definition: primnodes.h:761
bool parallel_safe
Definition: plannodes.h:130
Oid firstColCollation
Definition: primnodes.h:758
Definition: pg_list.h:50
bool enable_material
Definition: costsize.c:142
List * args
Definition: primnodes.h:773

◆ contain_dml()

static bool contain_dml ( Node node)
static

Definition at line 1069 of file subselect.c.

References contain_dml_walker().

Referenced by SS_process_ctes().

1070 {
1071  return contain_dml_walker(node, NULL);
1072 }
static bool contain_dml_walker(Node *node, void *context)
Definition: subselect.c:1075

◆ contain_dml_walker()

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

Definition at line 1075 of file subselect.c.

References CMD_SELECT, Query::commandType, expression_tree_walker(), IsA, NIL, query_tree_walker(), and Query::rowMarks.

Referenced by contain_dml().

1076 {
1077  if (node == NULL)
1078  return false;
1079  if (IsA(node, Query))
1080  {
1081  Query *query = (Query *) node;
1082 
1083  if (query->commandType != CMD_SELECT ||
1084  query->rowMarks != NIL)
1085  return true;
1086 
1087  return query_tree_walker(query, contain_dml_walker, context, 0);
1088  }
1089  return expression_tree_walker(node, contain_dml_walker, context);
1090 }
#define NIL
Definition: pg_list.h:65
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
static bool contain_dml_walker(Node *node, void *context)
Definition: subselect.c:1075
List * rowMarks
Definition: parsenodes.h:175
CmdType commandType
Definition: parsenodes.h:120
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904

◆ contain_outer_selfref()

static bool contain_outer_selfref ( Node node)
static

Definition at line 1096 of file subselect.c.

References Assert, contain_outer_selfref_walker(), and IsA.

Referenced by SS_process_ctes().

1097 {
1098  Index depth = 0;
1099 
1100  /*
1101  * We should be starting with a Query, so that depth will be 1 while
1102  * examining its immediate contents.
1103  */
1104  Assert(IsA(node, Query));
1105 
1106  return contain_outer_selfref_walker(node, &depth);
1107 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
unsigned int Index
Definition: c.h:549
#define Assert(condition)
Definition: c.h:804
static bool contain_outer_selfref_walker(Node *node, Index *depth)
Definition: subselect.c:1110

◆ contain_outer_selfref_walker()

static bool contain_outer_selfref_walker ( Node node,
Index depth 
)
static

Definition at line 1110 of file subselect.c.

References RangeTblEntry::ctelevelsup, expression_tree_walker(), IsA, QTW_EXAMINE_RTES_BEFORE, query_tree_walker(), RTE_CTE, RangeTblEntry::rtekind, and RangeTblEntry::self_reference.

Referenced by contain_outer_selfref().

1111 {
1112  if (node == NULL)
1113  return false;
1114  if (IsA(node, RangeTblEntry))
1115  {
1116  RangeTblEntry *rte = (RangeTblEntry *) node;
1117 
1118  /*
1119  * Check for a self-reference to a CTE that's above the Query that our
1120  * search started at.
1121  */
1122  if (rte->rtekind == RTE_CTE &&
1123  rte->self_reference &&
1124  rte->ctelevelsup >= *depth)
1125  return true;
1126  return false; /* allow range_table_walker to continue */
1127  }
1128  if (IsA(node, Query))
1129  {
1130  /* Recurse into subquery, tracking nesting depth properly */
1131  Query *query = (Query *) node;
1132  bool result;
1133 
1134  (*depth)++;
1135 
1137  (void *) depth, QTW_EXAMINE_RTES_BEFORE);
1138 
1139  (*depth)--;
1140 
1141  return result;
1142  }
1144  (void *) depth);
1145 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
#define QTW_EXAMINE_RTES_BEFORE
Definition: nodeFuncs.h:25
bool self_reference
Definition: parsenodes.h:1107
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
Index ctelevelsup
Definition: parsenodes.h:1106
RTEKind rtekind
Definition: parsenodes.h:995
static bool contain_outer_selfref_walker(Node *node, Index *depth)
Definition: subselect.c:1110

◆ convert_ANY_sublink_to_join()

JoinExpr* convert_ANY_sublink_to_join ( PlannerInfo root,
SubLink sublink,
Relids  available_rels 
)

Definition at line 1274 of file subselect.c.

References addRangeTableEntryForSubquery(), JoinExpr::alias, ANY_SUBLINK, Assert, bms_is_empty(), bms_is_subset(), contain_vars_of_level(), contain_volatile_functions(), convert_testexpr(), generate_subquery_vars(), JoinExpr::isNatural, JOIN_SEMI, JoinExpr::join_using_alias, JoinExpr::jointype, lappend(), JoinExpr::larg, list_length(), make_parsestate(), makeAlias(), makeNode, NIL, parse(), PlannerInfo::parse, pull_varnos(), JoinExpr::quals, JoinExpr::rarg, Query::rtable, JoinExpr::rtindex, SubLink::subLinkType, SubLink::subselect, SubLink::testexpr, and JoinExpr::usingClause.

Referenced by pull_up_sublinks_qual_recurse().

1276 {
1277  JoinExpr *result;
1278  Query *parse = root->parse;
1279  Query *subselect = (Query *) sublink->subselect;
1280  Relids upper_varnos;
1281  int rtindex;
1282  ParseNamespaceItem *nsitem;
1283  RangeTblEntry *rte;
1284  RangeTblRef *rtr;
1285  List *subquery_vars;
1286  Node *quals;
1287  ParseState *pstate;
1288 
1289  Assert(sublink->subLinkType == ANY_SUBLINK);
1290 
1291  /*
1292  * The sub-select must not refer to any Vars of the parent query. (Vars of
1293  * higher levels should be okay, though.)
1294  */
1295  if (contain_vars_of_level((Node *) subselect, 1))
1296  return NULL;
1297 
1298  /*
1299  * The test expression must contain some Vars of the parent query, else
1300  * it's not gonna be a join. (Note that it won't have Vars referring to
1301  * the subquery, rather Params.)
1302  */
1303  upper_varnos = pull_varnos(root, sublink->testexpr);
1304  if (bms_is_empty(upper_varnos))
1305  return NULL;
1306 
1307  /*
1308  * However, it can't refer to anything outside available_rels.
1309  */
1310  if (!bms_is_subset(upper_varnos, available_rels))
1311  return NULL;
1312 
1313  /*
1314  * The combining operators and left-hand expressions mustn't be volatile.
1315  */
1316  if (contain_volatile_functions(sublink->testexpr))
1317  return NULL;
1318 
1319  /* Create a dummy ParseState for addRangeTableEntryForSubquery */
1320  pstate = make_parsestate(NULL);
1321 
1322  /*
1323  * Okay, pull up the sub-select into upper range table.
1324  *
1325  * We rely here on the assumption that the outer query has no references
1326  * to the inner (necessarily true, other than the Vars that we build
1327  * below). Therefore this is a lot easier than what pull_up_subqueries has
1328  * to go through.
1329  */
1330  nsitem = addRangeTableEntryForSubquery(pstate,
1331  subselect,
1332  makeAlias("ANY_subquery", NIL),
1333  false,
1334  false);
1335  rte = nsitem->p_rte;
1336  parse->rtable = lappend(parse->rtable, rte);
1337  rtindex = list_length(parse->rtable);
1338 
1339  /*
1340  * Form a RangeTblRef for the pulled-up sub-select.
1341  */
1342  rtr = makeNode(RangeTblRef);
1343  rtr->rtindex = rtindex;
1344 
1345  /*
1346  * Build a list of Vars representing the subselect outputs.
1347  */
1348  subquery_vars = generate_subquery_vars(root,
1349  subselect->targetList,
1350  rtindex);
1351 
1352  /*
1353  * Build the new join's qual expression, replacing Params with these Vars.
1354  */
1355  quals = convert_testexpr(root, sublink->testexpr, subquery_vars);
1356 
1357  /*
1358  * And finally, build the JoinExpr node.
1359  */
1360  result = makeNode(JoinExpr);
1361  result->jointype = JOIN_SEMI;
1362  result->isNatural = false;
1363  result->larg = NULL; /* caller must fill this in */
1364  result->rarg = (Node *) rtr;
1365  result->usingClause = NIL;
1366  result->join_using_alias = NULL;
1367  result->quals = quals;
1368  result->alias = NULL;
1369  result->rtindex = 0; /* we don't need an RTE for it */
1370 
1371  return result;
1372 }
static Node * convert_testexpr(PlannerInfo *root, Node *testexpr, List *subst_nodes)
Definition: subselect.c:654
#define NIL
Definition: pg_list.h:65
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:97
Query * parse
Definition: pathnodes.h:161
Definition: nodes.h:539
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:451
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
Node * larg
Definition: primnodes.h:1542
Alias * makeAlias(const char *aliasname, List *colnames)
Definition: makefuncs.c:387
List * rtable
Definition: parsenodes.h:147
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Alias * join_using_alias
Definition: primnodes.h:1545
static List * generate_subquery_vars(PlannerInfo *root, List *tlist, Index varno)
Definition: subselect.c:625
List * lappend(List *list, void *datum)
Definition: list.c:336
bool isNatural
Definition: primnodes.h:1541
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
List * usingClause
Definition: primnodes.h:1544
Node * quals
Definition: primnodes.h:1546
#define makeNode(_type_)
Definition: nodes.h:587
Node * rarg
Definition: primnodes.h:1543
Alias * alias
Definition: primnodes.h:1547
JoinType jointype
Definition: primnodes.h:1540
#define Assert(condition)
Definition: c.h:804
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:396
static int list_length(const List *l)
Definition: pg_list.h:149
ParseNamespaceItem * addRangeTableEntryForSubquery(ParseState *pstate, Query *subquery, Alias *alias, bool lateral, bool inFromCl)
int rtindex
Definition: primnodes.h:1548
Definition: pg_list.h:50
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:665

◆ convert_EXISTS_sublink_to_join()

JoinExpr* convert_EXISTS_sublink_to_join ( PlannerInfo root,
SubLink sublink,
bool  under_not,
Relids  available_rels 
)

Definition at line 1382 of file subselect.c.

References JoinExpr::alias, Assert, bms_add_member(), bms_first_member(), bms_free(), bms_is_empty(), bms_is_subset(), contain_vars_of_level(), contain_volatile_functions(), copyObject, Query::cteList, EXISTS_SUBLINK, IncrementVarSublevelsUp(), JoinExpr::isNatural, JOIN_ANTI, JOIN_SEMI, JoinExpr::join_using_alias, Query::jointree, JoinExpr::jointype, JoinExpr::larg, linitial, list_concat(), list_length(), makeNode, NIL, OffsetVarNodes(), parse(), PlannerInfo::parse, pull_varnos(), JoinExpr::quals, FromExpr::quals, JoinExpr::rarg, replace_empty_jointree(), Query::rtable, JoinExpr::rtindex, simplify_EXISTS_query(), SubLink::subLinkType, SubLink::subselect, and JoinExpr::usingClause.

Referenced by pull_up_sublinks_qual_recurse().

1384 {
1385  JoinExpr *result;
1386  Query *parse = root->parse;
1387  Query *subselect = (Query *) sublink->subselect;
1388  Node *whereClause;
1389  int rtoffset;
1390  int varno;
1391  Relids clause_varnos;
1392  Relids upper_varnos;
1393 
1394  Assert(sublink->subLinkType == EXISTS_SUBLINK);
1395 
1396  /*
1397  * Can't flatten if it contains WITH. (We could arrange to pull up the
1398  * WITH into the parent query's cteList, but that risks changing the
1399  * semantics, since a WITH ought to be executed once per associated query
1400  * call.) Note that convert_ANY_sublink_to_join doesn't have to reject
1401  * this case, since it just produces a subquery RTE that doesn't have to
1402  * get flattened into the parent query.
1403  */
1404  if (subselect->cteList)
1405  return NULL;
1406 
1407  /*
1408  * Copy the subquery so we can modify it safely (see comments in
1409  * make_subplan).
1410  */
1411  subselect = copyObject(subselect);
1412 
1413  /*
1414  * See if the subquery can be simplified based on the knowledge that it's
1415  * being used in EXISTS(). If we aren't able to get rid of its
1416  * targetlist, we have to fail, because the pullup operation leaves us
1417  * with noplace to evaluate the targetlist.
1418  */
1419  if (!simplify_EXISTS_query(root, subselect))
1420  return NULL;
1421 
1422  /*
1423  * Separate out the WHERE clause. (We could theoretically also remove
1424  * top-level plain JOIN/ON clauses, but it's probably not worth the
1425  * trouble.)
1426  */
1427  whereClause = subselect->jointree->quals;
1428  subselect->jointree->quals = NULL;
1429 
1430  /*
1431  * The rest of the sub-select must not refer to any Vars of the parent
1432  * query. (Vars of higher levels should be okay, though.)
1433  */
1434  if (contain_vars_of_level((Node *) subselect, 1))
1435  return NULL;
1436 
1437  /*
1438  * On the other hand, the WHERE clause must contain some Vars of the
1439  * parent query, else it's not gonna be a join.
1440  */
1441  if (!contain_vars_of_level(whereClause, 1))
1442  return NULL;
1443 
1444  /*
1445  * We don't risk optimizing if the WHERE clause is volatile, either.
1446  */
1447  if (contain_volatile_functions(whereClause))
1448  return NULL;
1449 
1450  /*
1451  * The subquery must have a nonempty jointree, but we can make it so.
1452  */
1453  replace_empty_jointree(subselect);
1454 
1455  /*
1456  * Prepare to pull up the sub-select into top range table.
1457  *
1458  * We rely here on the assumption that the outer query has no references
1459  * to the inner (necessarily true). Therefore this is a lot easier than
1460  * what pull_up_subqueries has to go through.
1461  *
1462  * In fact, it's even easier than what convert_ANY_sublink_to_join has to
1463  * do. The machinations of simplify_EXISTS_query ensured that there is
1464  * nothing interesting in the subquery except an rtable and jointree, and
1465  * even the jointree FromExpr no longer has quals. So we can just append
1466  * the rtable to our own and use the FromExpr in our jointree. But first,
1467  * adjust all level-zero varnos in the subquery to account for the rtable
1468  * merger.
1469  */
1470  rtoffset = list_length(parse->rtable);
1471  OffsetVarNodes((Node *) subselect, rtoffset, 0);
1472  OffsetVarNodes(whereClause, rtoffset, 0);
1473 
1474  /*
1475  * Upper-level vars in subquery will now be one level closer to their
1476  * parent than before; in particular, anything that had been level 1
1477  * becomes level zero.
1478  */
1479  IncrementVarSublevelsUp((Node *) subselect, -1, 1);
1480  IncrementVarSublevelsUp(whereClause, -1, 1);
1481 
1482  /*
1483  * Now that the WHERE clause is adjusted to match the parent query
1484  * environment, we can easily identify all the level-zero rels it uses.
1485  * The ones <= rtoffset belong to the upper query; the ones > rtoffset do
1486  * not.
1487  */
1488  clause_varnos = pull_varnos(root, whereClause);
1489  upper_varnos = NULL;
1490  while ((varno = bms_first_member(clause_varnos)) >= 0)
1491  {
1492  if (varno <= rtoffset)
1493  upper_varnos = bms_add_member(upper_varnos, varno);
1494  }
1495  bms_free(clause_varnos);
1496  Assert(!bms_is_empty(upper_varnos));
1497 
1498  /*
1499  * Now that we've got the set of upper-level varnos, we can make the last
1500  * check: only available_rels can be referenced.
1501  */
1502  if (!bms_is_subset(upper_varnos, available_rels))
1503  return NULL;
1504 
1505  /* Now we can attach the modified subquery rtable to the parent */
1506  parse->rtable = list_concat(parse->rtable, subselect->rtable);
1507 
1508  /*
1509  * And finally, build the JoinExpr node.
1510  */
1511  result = makeNode(JoinExpr);
1512  result->jointype = under_not ? JOIN_ANTI : JOIN_SEMI;
1513  result->isNatural = false;
1514  result->larg = NULL; /* caller must fill this in */
1515  /* flatten out the FromExpr node if it's useless */
1516  if (list_length(subselect->jointree->fromlist) == 1)
1517  result->rarg = (Node *) linitial(subselect->jointree->fromlist);
1518  else
1519  result->rarg = (Node *) subselect->jointree;
1520  result->usingClause = NIL;
1521  result->join_using_alias = NULL;
1522  result->quals = whereClause;
1523  result->alias = NULL;
1524  result->rtindex = 0; /* we don't need an RTE for it */
1525 
1526  return result;
1527 }
#define NIL
Definition: pg_list.h:65
Relids pull_varnos(PlannerInfo *root, Node *node)
Definition: var.c:97
int bms_first_member(Bitmapset *a)
Definition: bitmapset.c:996
Query * parse
Definition: pathnodes.h:161
void OffsetVarNodes(Node *node, int offset, int sublevels_up)
Definition: rewriteManip.c:425
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:776
FromExpr * jointree
Definition: parsenodes.h:148
Definition: nodes.h:539
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:451
static bool simplify_EXISTS_query(PlannerInfo *root, Query *query)
Definition: subselect.c:1546
void replace_empty_jointree(Query *parse)
Definition: prepjointree.c:150
Node * quals
Definition: primnodes.h:1564
Node * larg
Definition: primnodes.h:1542
#define linitial(l)
Definition: pg_list.h:174
List * rtable
Definition: parsenodes.h:147
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Alias * join_using_alias
Definition: primnodes.h:1545
bool isNatural
Definition: primnodes.h:1541
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
List * usingClause
Definition: primnodes.h:1544
Node * quals
Definition: primnodes.h:1546
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define makeNode(_type_)
Definition: nodes.h:587
Node * rarg
Definition: primnodes.h:1543
Alias * alias
Definition: primnodes.h:1547
JoinType jointype
Definition: primnodes.h:1540
#define Assert(condition)
Definition: c.h:804
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:396
static int list_length(const List *l)
Definition: pg_list.h:149
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
List * cteList
Definition: parsenodes.h:145
#define copyObject(obj)
Definition: nodes.h:655
int rtindex
Definition: primnodes.h:1548
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:665

◆ convert_EXISTS_to_ANY()

static Query * convert_EXISTS_to_ANY ( PlannerInfo root,
Query subselect,
Node **  testexpr,
List **  paramIds 
)
static

Definition at line 1634 of file subselect.c.

References OpExpr::args, Assert, canonicalize_qual(), contain_aggs_of_level(), contain_subplans(), contain_vars_of_level(), contain_volatile_functions(), eval_const_expressions(), exprCollation(), exprType(), exprTypmod(), forfour, generate_new_exec_param(), get_commutator(), Query::hasAggs, hash_ok_operator(), IncrementVarSublevelsUp(), OpExpr::inputcollid, InvalidOid, IsA, Query::jointree, lappend(), lappend_int(), lappend_oid(), lfirst, lfirst_oid, linitial, lsecond, make_ands_explicit(), make_ands_implicit(), make_opclause(), makeTargetEntry(), NIL, OidIsValid, OpExpr::opno, PlannerInfo::parse, FromExpr::quals, and Query::targetList.

Referenced by make_subplan().

1636 {
1637  Node *whereClause;
1638  List *leftargs,
1639  *rightargs,
1640  *opids,
1641  *opcollations,
1642  *newWhere,
1643  *tlist,
1644  *testlist,
1645  *paramids;
1646  ListCell *lc,
1647  *rc,
1648  *oc,
1649  *cc;
1650  AttrNumber resno;
1651 
1652  /*
1653  * Query must not require a targetlist, since we have to insert a new one.
1654  * Caller should have dealt with the case already.
1655  */
1656  Assert(subselect->targetList == NIL);
1657 
1658  /*
1659  * Separate out the WHERE clause. (We could theoretically also remove
1660  * top-level plain JOIN/ON clauses, but it's probably not worth the
1661  * trouble.)
1662  */
1663  whereClause = subselect->jointree->quals;
1664  subselect->jointree->quals = NULL;
1665 
1666  /*
1667  * The rest of the sub-select must not refer to any Vars of the parent
1668  * query. (Vars of higher levels should be okay, though.)
1669  *
1670  * Note: we need not check for Aggrefs separately because we know the
1671  * sub-select is as yet unoptimized; any uplevel Aggref must therefore
1672  * contain an uplevel Var reference. This is not the case below ...
1673  */
1674  if (contain_vars_of_level((Node *) subselect, 1))
1675  return NULL;
1676 
1677  /*
1678  * We don't risk optimizing if the WHERE clause is volatile, either.
1679  */
1680  if (contain_volatile_functions(whereClause))
1681  return NULL;
1682 
1683  /*
1684  * Clean up the WHERE clause by doing const-simplification etc on it.
1685  * Aside from simplifying the processing we're about to do, this is
1686  * important for being able to pull chunks of the WHERE clause up into the
1687  * parent query. Since we are invoked partway through the parent's
1688  * preprocess_expression() work, earlier steps of preprocess_expression()
1689  * wouldn't get applied to the pulled-up stuff unless we do them here. For
1690  * the parts of the WHERE clause that get put back into the child query,
1691  * this work is partially duplicative, but it shouldn't hurt.
1692  *
1693  * Note: we do not run flatten_join_alias_vars. This is OK because any
1694  * parent aliases were flattened already, and we're not going to pull any
1695  * child Vars (of any description) into the parent.
1696  *
1697  * Note: passing the parent's root to eval_const_expressions is
1698  * technically wrong, but we can get away with it since only the
1699  * boundParams (if any) are used, and those would be the same in a
1700  * subroot.
1701  */
1702  whereClause = eval_const_expressions(root, whereClause);
1703  whereClause = (Node *) canonicalize_qual((Expr *) whereClause, false);
1704  whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
1705 
1706  /*
1707  * We now have a flattened implicit-AND list of clauses, which we try to
1708  * break apart into "outervar = innervar" hash clauses. Anything that
1709  * can't be broken apart just goes back into the newWhere list. Note that
1710  * we aren't trying hard yet to ensure that we have only outer or only
1711  * inner on each side; we'll check that if we get to the end.
1712  */
1713  leftargs = rightargs = opids = opcollations = newWhere = NIL;
1714  foreach(lc, (List *) whereClause)
1715  {
1716  OpExpr *expr = (OpExpr *) lfirst(lc);
1717 
1718  if (IsA(expr, OpExpr) &&
1719  hash_ok_operator(expr))
1720  {
1721  Node *leftarg = (Node *) linitial(expr->args);
1722  Node *rightarg = (Node *) lsecond(expr->args);
1723 
1724  if (contain_vars_of_level(leftarg, 1))
1725  {
1726  leftargs = lappend(leftargs, leftarg);
1727  rightargs = lappend(rightargs, rightarg);
1728  opids = lappend_oid(opids, expr->opno);
1729  opcollations = lappend_oid(opcollations, expr->inputcollid);
1730  continue;
1731  }
1732  if (contain_vars_of_level(rightarg, 1))
1733  {
1734  /*
1735  * We must commute the clause to put the outer var on the
1736  * left, because the hashing code in nodeSubplan.c expects
1737  * that. This probably shouldn't ever fail, since hashable
1738  * operators ought to have commutators, but be paranoid.
1739  */
1740  expr->opno = get_commutator(expr->opno);
1741  if (OidIsValid(expr->opno) && hash_ok_operator(expr))
1742  {
1743  leftargs = lappend(leftargs, rightarg);
1744  rightargs = lappend(rightargs, leftarg);
1745  opids = lappend_oid(opids, expr->opno);
1746  opcollations = lappend_oid(opcollations, expr->inputcollid);
1747  continue;
1748  }
1749  /* If no commutator, no chance to optimize the WHERE clause */
1750  return NULL;
1751  }
1752  }
1753  /* Couldn't handle it as a hash clause */
1754  newWhere = lappend(newWhere, expr);
1755  }
1756 
1757  /*
1758  * If we didn't find anything we could convert, fail.
1759  */
1760  if (leftargs == NIL)
1761  return NULL;
1762 
1763  /*
1764  * There mustn't be any parent Vars or Aggs in the stuff that we intend to
1765  * put back into the child query. Note: you might think we don't need to
1766  * check for Aggs separately, because an uplevel Agg must contain an
1767  * uplevel Var in its argument. But it is possible that the uplevel Var
1768  * got optimized away by eval_const_expressions. Consider
1769  *
1770  * SUM(CASE WHEN false THEN uplevelvar ELSE 0 END)
1771  */
1772  if (contain_vars_of_level((Node *) newWhere, 1) ||
1773  contain_vars_of_level((Node *) rightargs, 1))
1774  return NULL;
1775  if (root->parse->hasAggs &&
1776  (contain_aggs_of_level((Node *) newWhere, 1) ||
1777  contain_aggs_of_level((Node *) rightargs, 1)))
1778  return NULL;
1779 
1780  /*
1781  * And there can't be any child Vars in the stuff we intend to pull up.
1782  * (Note: we'd need to check for child Aggs too, except we know the child
1783  * has no aggs at all because of simplify_EXISTS_query's check. The same
1784  * goes for window functions.)
1785  */
1786  if (contain_vars_of_level((Node *) leftargs, 0))
1787  return NULL;
1788 
1789  /*
1790  * Also reject sublinks in the stuff we intend to pull up. (It might be
1791  * possible to support this, but doesn't seem worth the complication.)
1792  */
1793  if (contain_subplans((Node *) leftargs))
1794  return NULL;
1795 
1796  /*
1797  * Okay, adjust the sublevelsup in the stuff we're pulling up.
1798  */
1799  IncrementVarSublevelsUp((Node *) leftargs, -1, 1);
1800 
1801  /*
1802  * Put back any child-level-only WHERE clauses.
1803  */
1804  if (newWhere)
1805  subselect->jointree->quals = (Node *) make_ands_explicit(newWhere);
1806 
1807  /*
1808  * Build a new targetlist for the child that emits the expressions we
1809  * need. Concurrently, build a testexpr for the parent using Params to
1810  * reference the child outputs. (Since we generate Params directly here,
1811  * there will be no need to convert the testexpr in build_subplan.)
1812  */
1813  tlist = testlist = paramids = NIL;
1814  resno = 1;
1815  forfour(lc, leftargs, rc, rightargs, oc, opids, cc, opcollations)
1816  {
1817  Node *leftarg = (Node *) lfirst(lc);
1818  Node *rightarg = (Node *) lfirst(rc);
1819  Oid opid = lfirst_oid(oc);
1820  Oid opcollation = lfirst_oid(cc);
1821  Param *param;
1822 
1823  param = generate_new_exec_param(root,
1824  exprType(rightarg),
1825  exprTypmod(rightarg),
1826  exprCollation(rightarg));
1827  tlist = lappend(tlist,
1828  makeTargetEntry((Expr *) rightarg,
1829  resno++,
1830  NULL,
1831  false));
1832  testlist = lappend(testlist,
1833  make_opclause(opid, BOOLOID, false,
1834  (Expr *) leftarg, (Expr *) param,
1835  InvalidOid, opcollation));
1836  paramids = lappend_int(paramids, param->paramid);
1837  }
1838 
1839  /* Put everything where it should go, and we're done */
1840  subselect->targetList = tlist;
1841  *testexpr = (Node *) make_ands_explicit(testlist);
1842  *paramIds = paramids;
1843 
1844  return subselect;
1845 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Query * parse
Definition: pathnodes.h:161
Oid get_commutator(Oid opno)
Definition: lsyscache.c:1480
Param * generate_new_exec_param(PlannerInfo *root, Oid paramtype, int32 paramtypmod, Oid paramcollation)
Definition: paramassign.c:557
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:776
FromExpr * jointree
Definition: parsenodes.h:148
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
bool hasAggs
Definition: parsenodes.h:133
Definition: nodes.h:539
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2093
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:451
static bool hash_ok_operator(OpExpr *expr)
Definition: subselect.c:844
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define OidIsValid(objectId)
Definition: c.h:710
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Definition: makefuncs.c:610
Node * quals
Definition: primnodes.h:1564
#define lsecond(l)
Definition: pg_list.h:179
List * targetList
Definition: parsenodes.h:150
bool contain_subplans(Node *clause)
Definition: clauses.c:327
#define linitial(l)
Definition: pg_list.h:174
Expr * canonicalize_qual(Expr *qual, bool is_check)
Definition: prepqual.c:294
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
List * lappend_int(List *list, int datum)
Definition: list.c:354
List * lappend(List *list, void *datum)
Definition: list.c:336
Expr * make_ands_explicit(List *andclauses)
Definition: makefuncs.c:708
List * make_ands_implicit(Expr *clause)
Definition: makefuncs.c:719
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:396
int paramid
Definition: primnodes.h:268
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
Oid inputcollid
Definition: primnodes.h:547
bool contain_aggs_of_level(Node *node, int levelsup)
Definition: rewriteManip.c:67
#define forfour(cell1, list1, cell2, list2, cell3, list3, cell4, list4)
Definition: pg_list.h:503
Oid opno
Definition: primnodes.h:542
List * args
Definition: primnodes.h:548
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ convert_testexpr()

static Node * convert_testexpr ( PlannerInfo root,
Node testexpr,
List subst_nodes 
)
static

Definition at line 654 of file subselect.c.

References convert_testexpr_mutator(), convert_testexpr_context::root, and convert_testexpr_context::subst_nodes.

Referenced by build_subplan(), and convert_ANY_sublink_to_join().

657 {
658  convert_testexpr_context context;
659 
660  context.root = root;
661  context.subst_nodes = subst_nodes;
662  return convert_testexpr_mutator(testexpr, &context);
663 }
static Node * convert_testexpr_mutator(Node *node, convert_testexpr_context *context)
Definition: subselect.c:666
PlannerInfo * root
Definition: subselect.c:44

◆ convert_testexpr_mutator()

static Node * convert_testexpr_mutator ( Node node,
convert_testexpr_context context 
)
static

Definition at line 666 of file subselect.c.

References copyObject, elog, ERROR, expression_tree_mutator(), IsA, list_length(), list_nth(), PARAM_SUBLINK, Param::paramid, Param::paramkind, and convert_testexpr_context::subst_nodes.

Referenced by convert_testexpr().

668 {
669  if (node == NULL)
670  return NULL;
671  if (IsA(node, Param))
672  {
673  Param *param = (Param *) node;
674 
675  if (param->paramkind == PARAM_SUBLINK)
676  {
677  if (param->paramid <= 0 ||
678  param->paramid > list_length(context->subst_nodes))
679  elog(ERROR, "unexpected PARAM_SUBLINK ID: %d", param->paramid);
680 
681  /*
682  * We copy the list item to avoid having doubly-linked
683  * substructure in the modified parse tree. This is probably
684  * unnecessary when it's a Param, but be safe.
685  */
686  return (Node *) copyObject(list_nth(context->subst_nodes,
687  param->paramid - 1));
688  }
689  }
690  if (IsA(node, SubLink))
691  {
692  /*
693  * If we come across a nested SubLink, it is neither necessary nor
694  * correct to recurse into it: any PARAM_SUBLINKs we might find inside
695  * belong to the inner SubLink not the outer. So just return it as-is.
696  *
697  * This reasoning depends on the assumption that nothing will pull
698  * subexpressions into or out of the testexpr field of a SubLink, at
699  * least not without replacing PARAM_SUBLINKs first. If we did want
700  * to do that we'd need to rethink the parser-output representation
701  * altogether, since currently PARAM_SUBLINKs are only unique per
702  * SubLink not globally across the query. The whole point of
703  * replacing them with Vars or PARAM_EXEC nodes is to make them
704  * globally unique before they escape from the SubLink's testexpr.
705  *
706  * Note: this can't happen when called during SS_process_sublinks,
707  * because that recursively processes inner SubLinks first. It can
708  * happen when called from convert_ANY_sublink_to_join, though.
709  */
710  return node;
711  }
712  return expression_tree_mutator(node,
714  (void *) context);
715 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
static Node * convert_testexpr_mutator(Node *node, convert_testexpr_context *context)
Definition: subselect.c:666
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2598
ParamKind paramkind
Definition: primnodes.h:267
Definition: nodes.h:539
#define ERROR
Definition: elog.h:46
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
int paramid
Definition: primnodes.h:268
static int list_length(const List *l)
Definition: pg_list.h:149
#define elog(elevel,...)
Definition: elog.h:232
#define copyObject(obj)
Definition: nodes.h:655

◆ finalize_agg_primnode()

static bool finalize_agg_primnode ( Node node,
finalize_primnode_context context 
)
static

Definition at line 2913 of file subselect.c.

References Aggref::aggfilter, Aggref::args, expression_tree_walker(), finalize_primnode(), and IsA.

Referenced by finalize_plan().

2914 {
2915  if (node == NULL)
2916  return false;
2917  if (IsA(node, Aggref))
2918  {
2919  Aggref *agg = (Aggref *) node;
2920 
2921  /* we should not consider the direct arguments, if any */
2922  finalize_primnode((Node *) agg->args, context);
2923  finalize_primnode((Node *) agg->aggfilter, context);
2924  return false; /* there can't be any Aggrefs below here */
2925  }
2927  (void *) context);
2928 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Definition: nodes.h:539
List * args
Definition: primnodes.h:330
static bool finalize_primnode(Node *node, finalize_primnode_context *context)
Definition: subselect.c:2848
static bool finalize_agg_primnode(Node *node, finalize_primnode_context *context)
Definition: subselect.c:2913
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
Expr * aggfilter
Definition: primnodes.h:333

◆ finalize_plan()

static Bitmapset * finalize_plan ( PlannerInfo root,
Plan plan,
int  gather_param,
Bitmapset valid_params,
Bitmapset scan_params 
)
static

Definition at line 2237 of file subselect.c.

References AGG_HASHED, Agg::aggParams, Agg::aggstrategy, Plan::allParam, Assert, bms_add_member(), bms_add_members(), bms_copy(), bms_del_member(), bms_del_members(), bms_difference(), bms_free(), bms_is_empty(), bms_is_subset(), bms_union(), CustomScan::custom_exprs, CustomScan::custom_plans, elog, ModifyTable::epqParam, ERROR, Plan::extParam, ForeignScan::fdw_exprs, ForeignScan::fdw_recheck_quals, finalize_agg_primnode(), finalize_primnode(), find_base_rel(), RangeTblFunction::funcexpr, RangeTblFunction::funcparams, FunctionScan::functions, PlannerInfo::glob, Plan::initPlan, Plan::lefttree, lfirst, lfirst_int, list_length(), list_nth(), nodeTag, ModifyTable::onConflictSet, ModifyTable::onConflictWhere, PlannerInfo::outer_params, Plan::parallel_aware, finalize_primnode_context::paramids, NestLoopParam::paramno, Agg::plan, planner_subplan_get_plan, Plan::qual, ModifyTable::returningLists, Plan::righttree, convert_testexpr_context::root, finalize_primnode_context::root, SubqueryScan::scan, Scan::scanrelid, SubPlan::setParam, SubqueryScan::subplan, PlannerGlobal::subplans, RelOptInfo::subroot, T_Agg, T_Append, T_BitmapAnd, T_BitmapHeapScan, T_BitmapIndexScan, T_BitmapOr, T_CteScan, T_CustomScan, T_ForeignScan, T_FunctionScan, T_Gather, T_GatherMerge, T_Group, T_Hash, T_HashJoin, T_IncrementalSort, T_IndexOnlyScan, T_IndexScan, T_Limit, T_LockRows, T_Material, T_Memoize, T_MergeAppend, T_MergeJoin, T_ModifyTable, T_NamedTuplestoreScan, T_NestLoop, T_ProjectSet, T_RecursiveUnion, T_Result, T_SampleScan, T_SeqScan, T_SetOp, T_Sort, T_SubqueryScan, T_TableFuncScan, T_TidRangeScan, T_TidScan, T_Unique, T_ValuesScan, T_WindowAgg, T_WorkTableScan, and Plan::targetlist.

Referenced by SS_finalize_plan().

2241 {
2242  finalize_primnode_context context;
2243  int locally_added_param;
2244  Bitmapset *nestloop_params;
2245  Bitmapset *initExtParam;
2246  Bitmapset *initSetParam;
2247  Bitmapset *child_params;
2248  ListCell *l;
2249 
2250  if (plan == NULL)
2251  return NULL;
2252 
2253  context.root = root;
2254  context.paramids = NULL; /* initialize set to empty */
2255  locally_added_param = -1; /* there isn't one */
2256  nestloop_params = NULL; /* there aren't any */
2257 
2258  /*
2259  * Examine any initPlans to determine the set of external params they
2260  * reference and the set of output params they supply. (We assume
2261  * SS_finalize_plan was run on them already.)
2262  */
2263  initExtParam = initSetParam = NULL;
2264  foreach(l, plan->initPlan)
2265  {
2266  SubPlan *initsubplan = (SubPlan *) lfirst(l);
2267  Plan *initplan = planner_subplan_get_plan(root, initsubplan);
2268  ListCell *l2;
2269 
2270  initExtParam = bms_add_members(initExtParam, initplan->extParam);
2271  foreach(l2, initsubplan->setParam)
2272  {
2273  initSetParam = bms_add_member(initSetParam, lfirst_int(l2));
2274  }
2275  }
2276 
2277  /* Any setParams are validly referenceable in this node and children */
2278  if (initSetParam)
2279  valid_params = bms_union(valid_params, initSetParam);
2280 
2281  /*
2282  * When we call finalize_primnode, context.paramids sets are automatically
2283  * merged together. But when recursing to self, we have to do it the hard
2284  * way. We want the paramids set to include params in subplans as well as
2285  * at this level.
2286  */
2287 
2288  /* Find params in targetlist and qual */
2289  finalize_primnode((Node *) plan->targetlist, &context);
2290  finalize_primnode((Node *) plan->qual, &context);
2291 
2292  /*
2293  * If it's a parallel-aware scan node, mark it as dependent on the parent
2294  * Gather/GatherMerge's rescan Param.
2295  */
2296  if (plan->parallel_aware)
2297  {
2298  if (gather_param < 0)
2299  elog(ERROR, "parallel-aware plan node is not below a Gather");
2300  context.paramids =
2301  bms_add_member(context.paramids, gather_param);
2302  }
2303 
2304  /* Check additional node-type-specific fields */
2305  switch (nodeTag(plan))
2306  {
2307  case T_Result:
2308  finalize_primnode(((Result *) plan)->resconstantqual,
2309  &context);
2310  break;
2311 
2312  case T_SeqScan:
2313  context.paramids = bms_add_members(context.paramids, scan_params);
2314  break;
2315 
2316  case T_SampleScan:
2317  finalize_primnode((Node *) ((SampleScan *) plan)->tablesample,
2318  &context);
2319  context.paramids = bms_add_members(context.paramids, scan_params);
2320  break;
2321 
2322  case T_IndexScan:
2323  finalize_primnode((Node *) ((IndexScan *) plan)->indexqual,
2324  &context);
2325  finalize_primnode((Node *) ((IndexScan *) plan)->indexorderby,
2326  &context);
2327 
2328  /*
2329  * we need not look at indexqualorig, since it will have the same
2330  * param references as indexqual. Likewise, we can ignore
2331  * indexorderbyorig.
2332  */
2333  context.paramids = bms_add_members(context.paramids, scan_params);
2334  break;
2335 
2336  case T_IndexOnlyScan:
2337  finalize_primnode((Node *) ((IndexOnlyScan *) plan)->indexqual,
2338  &context);
2339  finalize_primnode((Node *) ((IndexOnlyScan *) plan)->indexorderby,
2340  &context);
2341 
2342  /*
2343  * we need not look at indextlist, since it cannot contain Params.
2344  */
2345  context.paramids = bms_add_members(context.paramids, scan_params);
2346  break;
2347 
2348  case T_BitmapIndexScan:
2349  finalize_primnode((Node *) ((BitmapIndexScan *) plan)->indexqual,
2350  &context);
2351 
2352  /*
2353  * we need not look at indexqualorig, since it will have the same
2354  * param references as indexqual.
2355  */
2356  break;
2357 
2358  case T_BitmapHeapScan:
2359  finalize_primnode((Node *) ((BitmapHeapScan *) plan)->bitmapqualorig,
2360  &context);
2361  context.paramids = bms_add_members(context.paramids, scan_params);
2362  break;
2363 
2364  case T_TidScan:
2365  finalize_primnode((Node *) ((TidScan *) plan)->tidquals,
2366  &context);
2367  context.paramids = bms_add_members(context.paramids, scan_params);
2368  break;
2369 
2370  case T_TidRangeScan:
2371  finalize_primnode((Node *) ((TidRangeScan *) plan)->tidrangequals,
2372  &context);
2373  context.paramids = bms_add_members(context.paramids, scan_params);
2374  break;
2375 
2376  case T_SubqueryScan:
2377  {
2378  SubqueryScan *sscan = (SubqueryScan *) plan;
2379  RelOptInfo *rel;
2380  Bitmapset *subquery_params;
2381 
2382  /* We must run finalize_plan on the subquery */
2383  rel = find_base_rel(root, sscan->scan.scanrelid);
2384  subquery_params = rel->subroot->outer_params;
2385  if (gather_param >= 0)
2386  subquery_params = bms_add_member(bms_copy(subquery_params),
2387  gather_param);
2388  finalize_plan(rel->subroot, sscan->subplan, gather_param,
2389  subquery_params, NULL);
2390 
2391  /* Now we can add its extParams to the parent's params */
2392  context.paramids = bms_add_members(context.paramids,
2393  sscan->subplan->extParam);
2394  /* We need scan_params too, though */
2395  context.paramids = bms_add_members(context.paramids,
2396  scan_params);
2397  }
2398  break;
2399 
2400  case T_FunctionScan:
2401  {
2402  FunctionScan *fscan = (FunctionScan *) plan;
2403  ListCell *lc;
2404 
2405  /*
2406  * Call finalize_primnode independently on each function
2407  * expression, so that we can record which params are
2408  * referenced in each, in order to decide which need
2409  * re-evaluating during rescan.
2410  */
2411  foreach(lc, fscan->functions)
2412  {
2413  RangeTblFunction *rtfunc = (RangeTblFunction *) lfirst(lc);
2414  finalize_primnode_context funccontext;
2415 
2416  funccontext = context;
2417  funccontext.paramids = NULL;
2418 
2419  finalize_primnode(rtfunc->funcexpr, &funccontext);
2420 
2421  /* remember results for execution */
2422  rtfunc->funcparams = funccontext.paramids;
2423 
2424  /* add the function's params to the overall set */
2425  context.paramids = bms_add_members(context.paramids,
2426  funccontext.paramids);
2427  }
2428 
2429  context.paramids = bms_add_members(context.paramids,
2430  scan_params);
2431  }
2432  break;
2433 
2434  case T_TableFuncScan:
2435  finalize_primnode((Node *) ((TableFuncScan *) plan)->tablefunc,
2436  &context);
2437  context.paramids = bms_add_members(context.paramids, scan_params);
2438  break;
2439 
2440  case T_ValuesScan:
2441  finalize_primnode((Node *) ((ValuesScan *) plan)->values_lists,
2442  &context);
2443  context.paramids = bms_add_members(context.paramids, scan_params);
2444  break;
2445 
2446  case T_CteScan:
2447  {
2448  /*
2449  * You might think we should add the node's cteParam to
2450  * paramids, but we shouldn't because that param is just a
2451  * linkage mechanism for multiple CteScan nodes for the same
2452  * CTE; it is never used for changed-param signaling. What we
2453  * have to do instead is to find the referenced CTE plan and
2454  * incorporate its external paramids, so that the correct
2455  * things will happen if the CTE references outer-level
2456  * variables. See test cases for bug #4902. (We assume
2457  * SS_finalize_plan was run on the CTE plan already.)
2458  */
2459  int plan_id = ((CteScan *) plan)->ctePlanId;
2460  Plan *cteplan;
2461 
2462  /* so, do this ... */
2463  if (plan_id < 1 || plan_id > list_length(root->glob->subplans))
2464  elog(ERROR, "could not find plan for CteScan referencing plan ID %d",
2465  plan_id);
2466  cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
2467  context.paramids =
2468  bms_add_members(context.paramids, cteplan->extParam);
2469 
2470 #ifdef NOT_USED
2471  /* ... but not this */
2472  context.paramids =
2473  bms_add_member(context.paramids,
2474  ((CteScan *) plan)->cteParam);
2475 #endif
2476 
2477  context.paramids = bms_add_members(context.paramids,
2478  scan_params);
2479  }
2480  break;
2481 
2482  case T_WorkTableScan:
2483  context.paramids =
2484  bms_add_member(context.paramids,
2485  ((WorkTableScan *) plan)->wtParam);
2486  context.paramids = bms_add_members(context.paramids, scan_params);
2487  break;
2488 
2489  case T_NamedTuplestoreScan:
2490  context.paramids = bms_add_members(context.paramids, scan_params);
2491  break;
2492 
2493  case T_ForeignScan:
2494  {
2495  ForeignScan *fscan = (ForeignScan *) plan;
2496 
2497  finalize_primnode((Node *) fscan->fdw_exprs,
2498  &context);
2500  &context);
2501 
2502  /* We assume fdw_scan_tlist cannot contain Params */
2503  context.paramids = bms_add_members(context.paramids,
2504  scan_params);
2505  }
2506  break;
2507 
2508  case T_CustomScan:
2509  {
2510  CustomScan *cscan = (CustomScan *) plan;
2511  ListCell *lc;
2512 
2513  finalize_primnode((Node *) cscan->custom_exprs,
2514  &context);
2515  /* We assume custom_scan_tlist cannot contain Params */
2516  context.paramids =
2517  bms_add_members(context.paramids, scan_params);
2518 
2519  /* child nodes if any */
2520  foreach(lc, cscan->custom_plans)
2521  {
2522  context.paramids =
2523  bms_add_members(context.paramids,
2524  finalize_plan(root,
2525  (Plan *) lfirst(lc),
2526  gather_param,
2527  valid_params,
2528  scan_params));
2529  }
2530  }
2531  break;
2532 
2533  case T_ModifyTable:
2534  {
2535  ModifyTable *mtplan = (ModifyTable *) plan;
2536 
2537  /* Force descendant scan nodes to reference epqParam */
2538  locally_added_param = mtplan->epqParam;
2539  valid_params = bms_add_member(bms_copy(valid_params),
2540  locally_added_param);
2541  scan_params = bms_add_member(bms_copy(scan_params),
2542  locally_added_param);
2544  &context);
2545  finalize_primnode((Node *) mtplan->onConflictSet,
2546  &context);
2548  &context);
2549  /* exclRelTlist contains only Vars, doesn't need examination */
2550  }
2551  break;
2552 
2553  case T_Append:
2554  {
2555  ListCell *l;
2556 
2557  foreach(l, ((Append *) plan)->appendplans)
2558  {
2559  context.paramids =
2560  bms_add_members(context.paramids,
2561  finalize_plan(root,
2562  (Plan *) lfirst(l),
2563  gather_param,
2564  valid_params,
2565  scan_params));
2566  }
2567  }
2568  break;
2569 
2570  case T_MergeAppend:
2571  {
2572  ListCell *l;
2573 
2574  foreach(l, ((MergeAppend *) plan)->mergeplans)
2575  {
2576  context.paramids =
2577  bms_add_members(context.paramids,
2578  finalize_plan(root,
2579  (Plan *) lfirst(l),
2580  gather_param,
2581  valid_params,
2582  scan_params));
2583  }
2584  }
2585  break;
2586 
2587  case T_BitmapAnd:
2588  {
2589  ListCell *l;
2590 
2591  foreach(l, ((BitmapAnd *) plan)->bitmapplans)
2592  {
2593  context.paramids =
2594  bms_add_members(context.paramids,
2595  finalize_plan(root,
2596  (Plan *) lfirst(l),
2597  gather_param,
2598  valid_params,
2599  scan_params));
2600  }
2601  }
2602  break;
2603 
2604  case T_BitmapOr:
2605  {
2606  ListCell *l;
2607 
2608  foreach(l, ((BitmapOr *) plan)->bitmapplans)
2609  {
2610  context.paramids =
2611  bms_add_members(context.paramids,
2612  finalize_plan(root,
2613  (Plan *) lfirst(l),
2614  gather_param,
2615  valid_params,
2616  scan_params));
2617  }
2618  }
2619  break;
2620 
2621  case T_NestLoop:
2622  {
2623  ListCell *l;
2624 
2625  finalize_primnode((Node *) ((Join *) plan)->joinqual,
2626  &context);
2627  /* collect set of params that will be passed to right child */
2628  foreach(l, ((NestLoop *) plan)->nestParams)
2629  {
2630  NestLoopParam *nlp = (NestLoopParam *) lfirst(l);
2631 
2632  nestloop_params = bms_add_member(nestloop_params,
2633  nlp->paramno);
2634  }
2635  }
2636  break;
2637 
2638  case T_MergeJoin:
2639  finalize_primnode((Node *) ((Join *) plan)->joinqual,
2640  &context);
2641  finalize_primnode((Node *) ((MergeJoin *) plan)->mergeclauses,
2642  &context);
2643  break;
2644 
2645  case T_HashJoin:
2646  finalize_primnode((Node *) ((Join *) plan)->joinqual,
2647  &context);
2648  finalize_primnode((Node *) ((HashJoin *) plan)->hashclauses,
2649  &context);
2650  break;
2651 
2652  case T_Limit:
2653  finalize_primnode(((Limit *) plan)->limitOffset,
2654  &context);
2655  finalize_primnode(((Limit *) plan)->limitCount,
2656  &context);
2657  break;
2658 
2659  case T_RecursiveUnion:
2660  /* child nodes are allowed to reference wtParam */
2661  locally_added_param = ((RecursiveUnion *) plan)->wtParam;
2662  valid_params = bms_add_member(bms_copy(valid_params),
2663  locally_added_param);
2664  /* wtParam does *not* get added to scan_params */
2665  break;
2666 
2667  case T_LockRows:
2668  /* Force descendant scan nodes to reference epqParam */
2669  locally_added_param = ((LockRows *) plan)->epqParam;
2670  valid_params = bms_add_member(bms_copy(valid_params),
2671  locally_added_param);
2672  scan_params = bms_add_member(bms_copy(scan_params),
2673  locally_added_param);
2674  break;
2675 
2676  case T_Agg:
2677  {
2678  Agg *agg = (Agg *) plan;
2679 
2680  /*
2681  * AGG_HASHED plans need to know which Params are referenced
2682  * in aggregate calls. Do a separate scan to identify them.
2683  */
2684  if (agg->aggstrategy == AGG_HASHED)
2685  {
2686  finalize_primnode_context aggcontext;
2687 
2688  aggcontext.root = root;
2689  aggcontext.paramids = NULL;
2691  &aggcontext);
2693  &aggcontext);
2694  agg->aggParams = aggcontext.paramids;
2695  }
2696  }
2697  break;
2698 
2699  case T_WindowAgg:
2700  finalize_primnode(((WindowAgg *) plan)->startOffset,
2701  &context);
2702  finalize_primnode(((WindowAgg *) plan)->endOffset,
2703  &context);
2704  break;
2705 
2706  case T_Gather:
2707  /* child nodes are allowed to reference rescan_param, if any */
2708  locally_added_param = ((Gather *) plan)->rescan_param;
2709  if (locally_added_param >= 0)
2710  {
2711  valid_params = bms_add_member(bms_copy(valid_params),
2712  locally_added_param);
2713 
2714  /*
2715  * We currently don't support nested Gathers. The issue so
2716  * far as this function is concerned would be how to identify
2717  * which child nodes depend on which Gather.
2718  */
2719  Assert(gather_param < 0);
2720  /* Pass down rescan_param to child parallel-aware nodes */
2721  gather_param = locally_added_param;
2722  }
2723  /* rescan_param does *not* get added to scan_params */
2724  break;
2725 
2726  case T_GatherMerge:
2727  /* child nodes are allowed to reference rescan_param, if any */
2728  locally_added_param = ((GatherMerge *) plan)->rescan_param;
2729  if (locally_added_param >= 0)
2730  {
2731  valid_params = bms_add_member(bms_copy(valid_params),
2732  locally_added_param);
2733 
2734  /*
2735  * We currently don't support nested Gathers. The issue so
2736  * far as this function is concerned would be how to identify
2737  * which child nodes depend on which Gather.
2738  */
2739  Assert(gather_param < 0);
2740  /* Pass down rescan_param to child parallel-aware nodes */
2741  gather_param = locally_added_param;
2742  }
2743  /* rescan_param does *not* get added to scan_params */
2744  break;
2745 
2746  case T_Memoize:
2747  finalize_primnode((Node *) ((Memoize *) plan)->param_exprs,
2748  &context);
2749  break;
2750 
2751  case T_ProjectSet:
2752  case T_Hash:
2753  case T_Material:
2754  case T_Sort:
2755  case T_IncrementalSort:
2756  case T_Unique:
2757  case T_SetOp:
2758  case T_Group:
2759  /* no node-type-specific fields need fixing */
2760  break;
2761 
2762  default:
2763  elog(ERROR, "unrecognized node type: %d",
2764  (int) nodeTag(plan));
2765  }
2766 
2767  /* Process left and right child plans, if any */
2768  child_params = finalize_plan(root,
2769  plan->lefttree,
2770  gather_param,
2771  valid_params,
2772  scan_params);
2773  context.paramids = bms_add_members(context.paramids, child_params);
2774 
2775  if (nestloop_params)
2776  {
2777  /* right child can reference nestloop_params as well as valid_params */
2778  child_params = finalize_plan(root,
2779  plan->righttree,
2780  gather_param,
2781  bms_union(nestloop_params, valid_params),
2782  scan_params);
2783  /* ... and they don't count as parameters used at my level */
2784  child_params = bms_difference(child_params, nestloop_params);
2785  bms_free(nestloop_params);
2786  }
2787  else
2788  {
2789  /* easy case */
2790  child_params = finalize_plan(root,
2791  plan->righttree,
2792  gather_param,
2793  valid_params,
2794  scan_params);
2795  }
2796  context.paramids = bms_add_members(context.paramids, child_params);
2797 
2798  /*
2799  * Any locally generated parameter doesn't count towards its generating
2800  * plan node's external dependencies. (Note: if we changed valid_params
2801  * and/or scan_params, we leak those bitmapsets; not worth the notational
2802  * trouble to clean them up.)
2803  */
2804  if (locally_added_param >= 0)
2805  {
2806  context.paramids = bms_del_member(context.paramids,
2807  locally_added_param);
2808  }
2809 
2810  /* Now we have all the paramids referenced in this node and children */
2811 
2812  if (!bms_is_subset(context.paramids, valid_params))
2813  elog(ERROR, "plan should not reference subplan's variable");
2814 
2815  /*
2816  * The plan node's allParam and extParam fields should include all its
2817  * referenced paramids, plus contributions from any child initPlans.
2818  * However, any setParams of the initPlans should not be present in the
2819  * parent node's extParams, only in its allParams. (It's possible that
2820  * some initPlans have extParams that are setParams of other initPlans.)
2821  */
2822 
2823  /* allParam must include initplans' extParams and setParams */
2824  plan->allParam = bms_union(context.paramids, initExtParam);
2825  plan->allParam = bms_add_members(plan->allParam, initSetParam);
2826  /* extParam must include any initplan extParams */
2827  plan->extParam = bms_union(context.paramids, initExtParam);
2828  /* but not any initplan setParams */
2829  plan->extParam = bms_del_members(plan->extParam, initSetParam);
2830 
2831  /*
2832  * For speed at execution time, make sure extParam/allParam are actually
2833  * NULL if they are empty sets.
2834  */
2835  if (bms_is_empty(plan->extParam))
2836  plan->extParam = NULL;
2837  if (bms_is_empty(plan->allParam))
2838  plan->allParam = NULL;
2839 
2840  return plan->allParam;
2841 }
List * qual
Definition: plannodes.h:142
Definition: nodes.h:81
PlannerInfo * root
Definition: subselect.c:56
#define planner_subplan_get_plan(root, subplan)
Definition: pathnodes.h:135
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
Definition: nodes.h:83
Index scanrelid
Definition: plannodes.h:344
List * functions
Definition: plannodes.h:537
List * fdw_exprs
Definition: plannodes.h:637
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:291
Definition: nodes.h:539
List * custom_exprs
Definition: plannodes.h:666
Definition: nodes.h:49
Definition: nodes.h:78
List * custom_plans
Definition: plannodes.h:665
List * onConflictSet
Definition: plannodes.h:235
PlannerInfo * subroot
Definition: pathnodes.h:720
AggStrategy aggstrategy
Definition: plannodes.h:860
Definition: nodes.h:46
#define ERROR
Definition: elog.h:46
#define lfirst_int(lc)
Definition: pg_list.h:170
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
static bool finalize_primnode(Node *node, finalize_primnode_context *context)
Definition: subselect.c:2848
bool bms_is_subset(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:315
Definition: nodes.h:80
List * subplans
Definition: pathnodes.h:95
Bitmapset * aggParams
Definition: plannodes.h:868
PlannerGlobal * glob
Definition: pathnodes.h:163
bool parallel_aware
Definition: plannodes.h:129
List * fdw_recheck_quals
Definition: plannodes.h:640
static Bitmapset * finalize_plan(PlannerInfo *root, Plan *plan, int gather_param, Bitmapset *valid_params, Bitmapset *scan_params)
Definition: subselect.c:2237
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
Plan plan
Definition: plannodes.h:859
static bool finalize_agg_primnode(Node *node, finalize_primnode_context *context)
Definition: subselect.c:2913
Definition: nodes.h:86
void bms_free(Bitmapset *a)
Definition: bitmapset.c:208
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List * setParam
Definition: primnodes.h:770
Bitmapset * outer_params
Definition: pathnodes.h:176
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:225
static int list_length(const List *l)
Definition: pg_list.h:149
Bitmapset * extParam
Definition: plannodes.h:159
Bitmapset * funcparams
Definition: parsenodes.h:1182
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define nodeTag(nodeptr)
Definition: nodes.h:544
List * targetlist
Definition: plannodes.h:141
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:928
Definition: nodes.h:87
List * initPlan
Definition: plannodes.h:145
Definition: nodes.h:84
#define elog(elevel,...)
Definition: elog.h:232
List * returningLists
Definition: plannodes.h:228
Definition: plannodes.h:857
RelOptInfo * find_base_rel(PlannerInfo *root, int relid)
Definition: relnode.c:374
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:773
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793
Plan * subplan
Definition: plannodes.h:527
int epqParam
Definition: plannodes.h:232
Node * onConflictWhere
Definition: plannodes.h:237
Definition: nodes.h:89

◆ finalize_primnode()

static bool finalize_primnode ( Node node,
finalize_primnode_context context 
)
static

Definition at line 2848 of file subselect.c.

References SubPlan::args, bms_add_member(), bms_copy(), bms_del_member(), bms_join(), expression_tree_walker(), Plan::extParam, IsA, lfirst_int, PARAM_EXEC, finalize_primnode_context::paramids, SubPlan::paramIds, SubPlan::parParam, planner_subplan_get_plan, finalize_primnode_context::root, and SubPlan::testexpr.

Referenced by finalize_agg_primnode(), and finalize_plan().

2849 {
2850  if (node == NULL)
2851  return false;
2852  if (IsA(node, Param))
2853  {
2854  if (((Param *) node)->paramkind == PARAM_EXEC)
2855  {
2856  int paramid = ((Param *) node)->paramid;
2857 
2858  context->paramids = bms_add_member(context->paramids, paramid);
2859  }
2860  return false; /* no more to do here */
2861  }
2862  if (IsA(node, SubPlan))
2863  {
2864  SubPlan *subplan = (SubPlan *) node;
2865  Plan *plan = planner_subplan_get_plan(context->root, subplan);
2866  ListCell *lc;
2867  Bitmapset *subparamids;
2868 
2869  /* Recurse into the testexpr, but not into the Plan */
2870  finalize_primnode(subplan->testexpr, context);
2871 
2872  /*
2873  * Remove any param IDs of output parameters of the subplan that were
2874  * referenced in the testexpr. These are not interesting for
2875  * parameter change signaling since we always re-evaluate the subplan.
2876  * Note that this wouldn't work too well if there might be uses of the
2877  * same param IDs elsewhere in the plan, but that can't happen because
2878  * generate_new_exec_param never tries to merge params.
2879  */
2880  foreach(lc, subplan->paramIds)
2881  {
2882  context->paramids = bms_del_member(context->paramids,
2883  lfirst_int(lc));
2884  }
2885 
2886  /* Also examine args list */
2887  finalize_primnode((Node *) subplan->args, context);
2888 
2889  /*
2890  * Add params needed by the subplan to paramids, but excluding those
2891  * we will pass down to it. (We assume SS_finalize_plan was run on
2892  * the subplan already.)
2893  */
2894  subparamids = bms_copy(plan->extParam);
2895  foreach(lc, subplan->parParam)
2896  {
2897  subparamids = bms_del_member(subparamids, lfirst_int(lc));
2898  }
2899  context->paramids = bms_join(context->paramids, subparamids);
2900 
2901  return false; /* no more to do here */
2902  }
2904  (void *) context);
2905 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
PlannerInfo * root
Definition: subselect.c:56
#define planner_subplan_get_plan(root, subplan)
Definition: pathnodes.h:135
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
Definition: nodes.h:539
List * paramIds
Definition: primnodes.h:750
#define lfirst_int(lc)
Definition: pg_list.h:170
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:949
static bool finalize_primnode(Node *node, finalize_primnode_context *context)
Definition: subselect.c:2848
Node * testexpr
Definition: primnodes.h:749
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
Bitmapset * extParam
Definition: plannodes.h:159
List * parParam
Definition: primnodes.h:772
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:773
List * args
Definition: primnodes.h:773

◆ generate_subquery_params()

static List * generate_subquery_params ( PlannerInfo root,
List tlist,
List **  paramIds 
)
static

Definition at line 592 of file subselect.c.

References TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), generate_new_exec_param(), lappend(), lappend_int(), lfirst, NIL, Param::paramid, and TargetEntry::resjunk.

Referenced by build_subplan().

593 {
594  List *result;
595  List *ids;
596  ListCell *lc;
597 
598  result = ids = NIL;
599  foreach(lc, tlist)
600  {
601  TargetEntry *tent = (TargetEntry *) lfirst(lc);
602  Param *param;
603 
604  if (tent->resjunk)
605  continue;
606 
607  param = generate_new_exec_param(root,
608  exprType((Node *) tent->expr),
609  exprTypmod((Node *) tent->expr),
610  exprCollation((Node *) tent->expr));
611  result = lappend(result, param);
612  ids = lappend_int(ids, param->paramid);
613  }
614 
615  *paramIds = ids;
616  return result;
617 }
#define NIL
Definition: pg_list.h:65
Param * generate_new_exec_param(PlannerInfo *root, Oid paramtype, int32 paramtypmod, Oid paramcollation)
Definition: paramassign.c:557
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
Definition: nodes.h:539
bool resjunk
Definition: primnodes.h:1461
List * lappend_int(List *list, int datum)
Definition: list.c:354
List * lappend(List *list, void *datum)
Definition: list.c:336
#define lfirst(lc)
Definition: pg_list.h:169
Expr * expr
Definition: primnodes.h:1454
int paramid
Definition: primnodes.h:268
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
Definition: pg_list.h:50

◆ generate_subquery_vars()

static List * generate_subquery_vars ( PlannerInfo root,
List tlist,
Index  varno 
)
static

Definition at line 625 of file subselect.c.

References lappend(), lfirst, makeVarFromTargetEntry(), NIL, and TargetEntry::resjunk.

Referenced by convert_ANY_sublink_to_join().

626 {
627  List *result;
628  ListCell *lc;
629 
630  result = NIL;
631  foreach(lc, tlist)
632  {
633  TargetEntry *tent = (TargetEntry *) lfirst(lc);
634  Var *var;
635 
636  if (tent->resjunk)
637  continue;
638 
639  var = makeVarFromTargetEntry(varno, tent);
640  result = lappend(result, var);
641  }
642 
643  return result;
644 }
#define NIL
Definition: pg_list.h:65
Var * makeVarFromTargetEntry(Index varno, TargetEntry *tle)
Definition: makefuncs.c:103
Definition: primnodes.h:186
bool resjunk
Definition: primnodes.h:1461
List * lappend(List *list, void *datum)
Definition: list.c:336
#define lfirst(lc)
Definition: pg_list.h:169
Definition: pg_list.h:50

◆ get_first_col_type()

static void get_first_col_type ( Plan plan,
Oid coltype,
int32 coltypmod,
Oid colcollation 
)
static

Definition at line 119 of file subselect.c.

References TargetEntry::expr, exprCollation(), exprType(), exprTypmod(), InvalidOid, linitial_node, TargetEntry::resjunk, and Plan::targetlist.

Referenced by build_subplan(), SS_make_initplan_from_plan(), and SS_process_ctes().

121 {
122  /* In cases such as EXISTS, tlist might be empty; arbitrarily use VOID */
123  if (plan->targetlist)
124  {
126 
127  if (!tent->resjunk)
128  {
129  *coltype = exprType((Node *) tent->expr);
130  *coltypmod = exprTypmod((Node *) tent->expr);
131  *colcollation = exprCollation((Node *) tent->expr);
132  return;
133  }
134  }
135  *coltype = VOIDOID;
136  *coltypmod = -1;
137  *colcollation = InvalidOid;
138 }
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
Definition: nodes.h:539
#define linitial_node(type, l)
Definition: pg_list.h:177
bool resjunk
Definition: primnodes.h:1461
#define InvalidOid
Definition: postgres_ext.h:36
Expr * expr
Definition: primnodes.h:1454
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:759
List * targetlist
Definition: plannodes.h:141

◆ hash_ok_operator()

static bool hash_ok_operator ( OpExpr expr)
static

Definition at line 844 of file subselect.c.

References OpExpr::args, elog, ERROR, exprType(), func_strict(), GETSTRUCT, HeapTupleIsValid, linitial, list_length(), ObjectIdGetDatum, op_hashjoinable(), OPEROID, OpExpr::opno, ReleaseSysCache(), and SearchSysCache1().

Referenced by convert_EXISTS_to_ANY(), and test_opexpr_is_hashable().

845 {
846  Oid opid = expr->opno;
847 
848  /* quick out if not a binary operator */
849  if (list_length(expr->args) != 2)
850  return false;
851  if (opid == ARRAY_EQ_OP)
852  {
853  /* array_eq is strict, but must check input type to ensure hashable */
854  /* XXX record_eq will need same treatment when it becomes hashable */
855  Node *leftarg = linitial(expr->args);
856 
857  return op_hashjoinable(opid, exprType(leftarg));
858  }
859  else
860  {
861  /* else must look up the operator properties */
862  HeapTuple tup;
863  Form_pg_operator optup;
864 
866  if (!HeapTupleIsValid(tup))
867  elog(ERROR, "cache lookup failed for operator %u", opid);
868  optup = (Form_pg_operator) GETSTRUCT(tup);
869  if (!optup->oprcanhash || !func_strict(optup->oprcode))
870  {
871  ReleaseSysCache(tup);
872  return false;
873  }
874  ReleaseSysCache(tup);
875  return true;
876  }
877 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
Definition: nodes.h:539
unsigned int Oid
Definition: postgres_ext.h:31
bool op_hashjoinable(Oid opno, Oid inputtype)
Definition: lsyscache.c:1408
#define linitial(l)
Definition: pg_list.h:174
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:41
static int list_length(const List *l)
Definition: pg_list.h:149
FormData_pg_operator * Form_pg_operator
Definition: pg_operator.h:83
bool func_strict(Oid funcid)
Definition: lsyscache.c:1732
#define elog(elevel,...)
Definition: elog.h:232
Oid opno
Definition: primnodes.h:542
List * args
Definition: primnodes.h:548

◆ inline_cte()

static void inline_cte ( PlannerInfo root,
CommonTableExpr cte 
)
static

Definition at line 1151 of file subselect.c.

References Assert, castNode, inline_cte_walker_context::ctename, CommonTableExpr::ctename, inline_cte_walker_context::ctequery, CommonTableExpr::ctequery, CommonTableExpr::cterefcount, inline_cte_walker(), inline_cte_walker_context::levelsup, PlannerInfo::parse, and inline_cte_walker_context::refcount.

Referenced by SS_process_ctes().

1152 {
1153  struct inline_cte_walker_context context;
1154 
1155  context.ctename = cte->ctename;
1156  /* Start at levelsup = -1 because we'll immediately increment it */
1157  context.levelsup = -1;
1158  context.refcount = cte->cterefcount;
1159  context.ctequery = castNode(Query, cte->ctequery);
1160 
1161  (void) inline_cte_walker((Node *) root->parse, &context);
1162 
1163  /* Assert we replaced all references */
1164  Assert(context.refcount == 0);
1165 }
Query * parse
Definition: pathnodes.h:161
#define castNode(_type_, nodeptr)
Definition: nodes.h:608
Definition: nodes.h:539
#define Assert(condition)
Definition: c.h:804
const char * ctename
Definition: subselect.c:62
static bool inline_cte_walker(Node *node, inline_cte_walker_context *context)
Definition: subselect.c:1168

◆ inline_cte_walker()

static bool inline_cte_walker ( Node node,
inline_cte_walker_context context 
)
static

Definition at line 1168 of file subselect.c.

References RangeTblEntry::colcollations, RangeTblEntry::coltypes, RangeTblEntry::coltypmods, copyObject, RangeTblEntry::ctelevelsup, inline_cte_walker_context::ctename, RangeTblEntry::ctename, inline_cte_walker_context::ctequery, expression_tree_walker(), IncrementVarSublevelsUp(), IsA, inline_cte_walker_context::levelsup, NIL, QTW_EXAMINE_RTES_AFTER, query_tree_walker(), inline_cte_walker_context::refcount, RTE_CTE, RTE_SUBQUERY, RangeTblEntry::rtekind, RangeTblEntry::security_barrier, RangeTblEntry::self_reference, and RangeTblEntry::subquery.

Referenced by inline_cte().

1169 {
1170  if (node == NULL)
1171  return false;
1172  if (IsA(node, Query))
1173  {
1174  Query *query = (Query *) node;
1175 
1176  context->levelsup++;
1177 
1178  /*
1179  * Visit the query's RTE nodes after their contents; otherwise
1180  * query_tree_walker would descend into the newly inlined CTE query,
1181  * which we don't want.
1182  */
1183  (void) query_tree_walker(query, inline_cte_walker, context,
1185 
1186  context->levelsup--;
1187 
1188  return false;
1189  }
1190  else if (IsA(node, RangeTblEntry))
1191  {
1192  RangeTblEntry *rte = (RangeTblEntry *) node;
1193 
1194  if (rte->rtekind == RTE_CTE &&
1195  strcmp(rte->ctename, context->ctename) == 0 &&
1196  rte->ctelevelsup == context->levelsup)
1197  {
1198  /*
1199  * Found a reference to replace. Generate a copy of the CTE query
1200  * with appropriate level adjustment for outer references (e.g.,
1201  * to other CTEs).
1202  */
1203  Query *newquery = copyObject(context->ctequery);
1204 
1205  if (context->levelsup > 0)
1206  IncrementVarSublevelsUp((Node *) newquery, context->levelsup, 1);
1207 
1208  /*
1209  * Convert the RTE_CTE RTE into a RTE_SUBQUERY.
1210  *
1211  * Historically, a FOR UPDATE clause has been treated as extending
1212  * into views and subqueries, but not into CTEs. We preserve this
1213  * distinction by not trying to push rowmarks into the new
1214  * subquery.
1215  */
1216  rte->rtekind = RTE_SUBQUERY;
1217  rte->subquery = newquery;
1218  rte->security_barrier = false;
1219 
1220  /* Zero out CTE-specific fields */
1221  rte->ctename = NULL;
1222  rte->ctelevelsup = 0;
1223  rte->self_reference = false;
1224  rte->coltypes = NIL;
1225  rte->coltypmods = NIL;
1226  rte->colcollations = NIL;
1227 
1228  /* Count the number of replacements we've done */
1229  context->refcount--;
1230  }
1231 
1232  return false;
1233  }
1234 
1235  return expression_tree_walker(node, inline_cte_walker, context);
1236 }
#define NIL
Definition: pg_list.h:65
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
#define QTW_EXAMINE_RTES_AFTER
Definition: nodeFuncs.h:27
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:776
List * coltypmods
Definition: parsenodes.h:1128
Definition: nodes.h:539
List * colcollations
Definition: parsenodes.h:1129
bool self_reference
Definition: parsenodes.h:1107
bool security_barrier
Definition: parsenodes.h:1031
const char * ctename
Definition: subselect.c:62
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
Index ctelevelsup
Definition: parsenodes.h:1106
RTEKind rtekind
Definition: parsenodes.h:995
char * ctename
Definition: parsenodes.h:1105
Query * subquery
Definition: parsenodes.h:1030
#define copyObject(obj)
Definition: nodes.h:655
List * coltypes
Definition: parsenodes.h:1127
static bool inline_cte_walker(Node *node, inline_cte_walker_context *context)
Definition: subselect.c:1168

◆ make_subplan()

static Node* make_subplan ( PlannerInfo root,
Query orig_subquery,
SubLinkType  subLinkType,
int  subLinkId,
Node testexpr,
bool  isTopQual 
)
static

Definition at line 163 of file subselect.c.

References ALL_SUBLINK, ANY_SUBLINK, Assert, build_subplan(), castNode, RelOptInfo::cheapest_total_path, convert_EXISTS_to_ANY(), copyObject, create_plan(), EXISTS_SUBLINK, fetch_upper_rel(), get_cheapest_fractional_path(), PlannerInfo::glob, PlannerInfo::hasAlternativeSubPlans, IsA, list_make2, makeNode, NIL, SubPlan::parParam, PlannerInfo::plan_params, simplify_EXISTS_query(), subpath_is_hashable(), AlternativeSubPlan::subplans, subquery_planner(), UPPERREL_FINAL, and SubPlan::useHashTable.

Referenced by process_sublinks_mutator().

166 {
167  Query *subquery;
168  bool simple_exists = false;
169  double tuple_fraction;
170  PlannerInfo *subroot;
171  RelOptInfo *final_rel;
172  Path *best_path;
173  Plan *plan;
174  List *plan_params;
175  Node *result;
176 
177  /*
178  * Copy the source Query node. This is a quick and dirty kluge to resolve
179  * the fact that the parser can generate trees with multiple links to the
180  * same sub-Query node, but the planner wants to scribble on the Query.
181  * Try to clean this up when we do querytree redesign...
182  */
183  subquery = copyObject(orig_subquery);
184 
185  /*
186  * If it's an EXISTS subplan, we might be able to simplify it.
187  */
188  if (subLinkType == EXISTS_SUBLINK)
189  simple_exists = simplify_EXISTS_query(root, subquery);
190 
191  /*
192  * For an EXISTS subplan, tell lower-level planner to expect that only the
193  * first tuple will be retrieved. For ALL and ANY subplans, we will be
194  * able to stop evaluating if the test condition fails or matches, so very
195  * often not all the tuples will be retrieved; for lack of a better idea,
196  * specify 50% retrieval. For EXPR, MULTIEXPR, and ROWCOMPARE subplans,
197  * use default behavior (we're only expecting one row out, anyway).
198  *
199  * NOTE: if you change these numbers, also change cost_subplan() in
200  * path/costsize.c.
201  *
202  * XXX If an ANY subplan is uncorrelated, build_subplan may decide to hash
203  * its output. In that case it would've been better to specify full
204  * retrieval. At present, however, we can only check hashability after
205  * we've made the subplan :-(. (Determining whether it'll fit in hash_mem
206  * is the really hard part.) Therefore, we don't want to be too
207  * optimistic about the percentage of tuples retrieved, for fear of
208  * selecting a plan that's bad for the materialization case.
209  */
210  if (subLinkType == EXISTS_SUBLINK)
211  tuple_fraction = 1.0; /* just like a LIMIT 1 */
212  else if (subLinkType == ALL_SUBLINK ||
213  subLinkType == ANY_SUBLINK)
214  tuple_fraction = 0.5; /* 50% */
215  else
216  tuple_fraction = 0.0; /* default behavior */
217 
218  /* plan_params should not be in use in current query level */
219  Assert(root->plan_params == NIL);
220 
221  /* Generate Paths for the subquery */
222  subroot = subquery_planner(root->glob, subquery,
223  root,
224  false, tuple_fraction);
225 
226  /* Isolate the params needed by this specific subplan */
227  plan_params = root->plan_params;
228  root->plan_params = NIL;
229 
230  /*
231  * Select best Path and turn it into a Plan. At least for now, there
232  * seems no reason to postpone doing that.
233  */
234  final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
235  best_path = get_cheapest_fractional_path(final_rel, tuple_fraction);
236 
237  plan = create_plan(subroot, best_path);
238 
239  /* And convert to SubPlan or InitPlan format. */
240  result = build_subplan(root, plan, subroot, plan_params,
241  subLinkType, subLinkId,
242  testexpr, NIL, isTopQual);
243 
244  /*
245  * If it's a correlated EXISTS with an unimportant targetlist, we might be
246  * able to transform it to the equivalent of an IN and then implement it
247  * by hashing. We don't have enough information yet to tell which way is
248  * likely to be better (it depends on the expected number of executions of
249  * the EXISTS qual, and we are much too early in planning the outer query
250  * to be able to guess that). So we generate both plans, if possible, and
251  * leave it to setrefs.c to decide which to use.
252  */
253  if (simple_exists && IsA(result, SubPlan))
254  {
255  Node *newtestexpr;
256  List *paramIds;
257 
258  /* Make a second copy of the original subquery */
259  subquery = copyObject(orig_subquery);
260  /* and re-simplify */
261  simple_exists = simplify_EXISTS_query(root, subquery);
262  Assert(simple_exists);
263  /* See if it can be converted to an ANY query */
264  subquery = convert_EXISTS_to_ANY(root, subquery,
265  &newtestexpr, &paramIds);
266  if (subquery)
267  {
268  /* Generate Paths for the ANY subquery; we'll need all rows */
269  subroot = subquery_planner(root->glob, subquery,
270  root,
271  false, 0.0);
272 
273  /* Isolate the params needed by this specific subplan */
274  plan_params = root->plan_params;
275  root->plan_params = NIL;
276 
277  /* Select best Path */
278  final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
279  best_path = final_rel->cheapest_total_path;
280 
281  /* Now we can check if it'll fit in hash_mem */
282  if (subpath_is_hashable(best_path))
283  {
284  SubPlan *hashplan;
285  AlternativeSubPlan *asplan;
286 
287  /* OK, finish planning the ANY subquery */
288  plan = create_plan(subroot, best_path);
289 
290  /* ... and convert to SubPlan format */
291  hashplan = castNode(SubPlan,
292  build_subplan(root, plan, subroot,
293  plan_params,
294  ANY_SUBLINK, 0,
295  newtestexpr,
296  paramIds,
297  true));
298  /* Check we got what we expected */
299  Assert(hashplan->parParam == NIL);
300  Assert(hashplan->useHashTable);
301 
302  /* Leave it to setrefs.c to decide which plan to use */
303  asplan = makeNode(AlternativeSubPlan);
304  asplan->subplans = list_make2(result, hashplan);
305  result = (Node *) asplan;
306  root->hasAlternativeSubPlans = true;
307  }
308  }
309  }
310 
311  return result;
312 }
#define list_make2(x1, x2)
Definition: pg_list.h:208
#define NIL
Definition: pg_list.h:65
static Node * build_subplan(PlannerInfo *root, Plan *plan, PlannerInfo *subroot, List *plan_params, SubLinkType subLinkType, int subLinkId, Node *testexpr, List *testexpr_paramids, bool unknownEqFalse)
Definition: subselect.c:321
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
List * plan_params
Definition: pathnodes.h:175
#define castNode(_type_, nodeptr)
Definition: nodes.h:608
Definition: nodes.h:539
static bool simplify_EXISTS_query(PlannerInfo *root, Query *query)
Definition: subselect.c:1546
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:332
bool hasAlternativeSubPlans
Definition: pathnodes.h:350
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1207
struct Path * cheapest_total_path
Definition: pathnodes.h:694
PlannerGlobal * glob
Definition: pathnodes.h:163
static Query * convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect, Node **testexpr, List **paramIds)
Definition: subselect.c:1634
#define makeNode(_type_)
Definition: nodes.h:587
#define Assert(condition)
Definition: c.h:804
List * parParam
Definition: primnodes.h:772
bool useHashTable
Definition: primnodes.h:761
#define copyObject(obj)
Definition: nodes.h:655
Definition: pg_list.h:50
PlannerInfo * subquery_planner(PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root, bool hasRecursion, double tuple_fraction)
Definition: planner.c:590
static bool subpath_is_hashable(Path *path)
Definition: subselect.c:748
Path * get_cheapest_fractional_path(RelOptInfo *rel, double tuple_fraction)
Definition: planner.c:5494

◆ process_sublinks_mutator()

static Node * process_sublinks_mutator ( Node node,
process_sublinks_context context 
)
static

Definition at line 1928 of file subselect.c.

References generate_unaccent_rules::args, Assert, expression_tree_mutator(), is_andclause(), is_orclause(), IsA, process_sublinks_context::isTopQual, lappend(), lfirst, list_concat(), make_andclause(), make_orclause(), make_subplan(), NIL, process_sublinks_context::root, SubLink::subLinkId, SubLink::subLinkType, SubLink::subselect, and SubLink::testexpr.

Referenced by SS_process_sublinks().

1929 {
1930  process_sublinks_context locContext;
1931 
1932  locContext.root = context->root;
1933 
1934  if (node == NULL)
1935  return NULL;
1936  if (IsA(node, SubLink))
1937  {
1938  SubLink *sublink = (SubLink *) node;
1939  Node *testexpr;
1940 
1941  /*
1942  * First, recursively process the lefthand-side expressions, if any.
1943  * They're not top-level anymore.
1944  */
1945  locContext.isTopQual = false;
1946  testexpr = process_sublinks_mutator(sublink->testexpr, &locContext);
1947 
1948  /*
1949  * Now build the SubPlan node and make the expr to return.
1950  */
1951  return make_subplan(context->root,
1952  (Query *) sublink->subselect,
1953  sublink->subLinkType,
1954  sublink->subLinkId,
1955  testexpr,
1956  context->isTopQual);
1957  }
1958 
1959  /*
1960  * Don't recurse into the arguments of an outer PHV or aggregate here. Any
1961  * SubLinks in the arguments have to be dealt with at the outer query
1962  * level; they'll be handled when build_subplan collects the PHV or Aggref
1963  * into the arguments to be passed down to the current subplan.
1964  */
1965  if (IsA(node, PlaceHolderVar))
1966  {
1967  if (((PlaceHolderVar *) node)->phlevelsup > 0)
1968  return node;
1969  }
1970  else if (IsA(node, Aggref))
1971  {
1972  if (((Aggref *) node)->agglevelsup > 0)
1973  return node;
1974  }
1975 
1976  /*
1977  * We should never see a SubPlan expression in the input (since this is
1978  * the very routine that creates 'em to begin with). We shouldn't find
1979  * ourselves invoked directly on a Query, either.
1980  */
1981  Assert(!IsA(node, SubPlan));
1982  Assert(!IsA(node, AlternativeSubPlan));
1983  Assert(!IsA(node, Query));
1984 
1985  /*
1986  * Because make_subplan() could return an AND or OR clause, we have to
1987  * take steps to preserve AND/OR flatness of a qual. We assume the input
1988  * has been AND/OR flattened and so we need no recursion here.
1989  *
1990  * (Due to the coding here, we will not get called on the List subnodes of
1991  * an AND; and the input is *not* yet in implicit-AND format. So no check
1992  * is needed for a bare List.)
1993  *
1994  * Anywhere within the top-level AND/OR clause structure, we can tell
1995  * make_subplan() that NULL and FALSE are interchangeable. So isTopQual
1996  * propagates down in both cases. (Note that this is unlike the meaning
1997  * of "top level qual" used in most other places in Postgres.)
1998  */
1999  if (is_andclause(node))
2000  {
2001  List *newargs = NIL;
2002  ListCell *l;
2003 
2004  /* Still at qual top-level */
2005  locContext.isTopQual = context->isTopQual;
2006 
2007  foreach(l, ((BoolExpr *) node)->args)
2008  {
2009  Node *newarg;
2010 
2011  newarg = process_sublinks_mutator(lfirst(l), &locContext);
2012  if (is_andclause(newarg))
2013  newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
2014  else
2015  newargs = lappend(newargs, newarg);
2016  }
2017  return (Node *) make_andclause(newargs);
2018  }
2019 
2020  if (is_orclause(node))
2021  {
2022  List *newargs = NIL;
2023  ListCell *l;
2024 
2025  /* Still at qual top-level */
2026  locContext.isTopQual = context->isTopQual;
2027 
2028  foreach(l, ((BoolExpr *) node)->args)
2029  {
2030  Node *newarg;
2031 
2032  newarg = process_sublinks_mutator(lfirst(l), &locContext);
2033  if (is_orclause(newarg))
2034  newargs = list_concat(newargs, ((BoolExpr *) newarg)->args);
2035  else
2036  newargs = lappend(newargs, newarg);
2037  }
2038  return (Node *) make_orclause(newargs);
2039  }
2040 
2041  /*
2042  * If we recurse down through anything other than an AND or OR node, we
2043  * are definitely not at top qual level anymore.
2044  */
2045  locContext.isTopQual = false;
2046 
2047  return expression_tree_mutator(node,
2049  (void *) &locContext);
2050 }
#define NIL
Definition: pg_list.h:65
static Node * make_subplan(PlannerInfo *root, Query *orig_subquery, SubLinkType subLinkType, int subLinkId, Node *testexpr, bool isTopQual)
Definition: subselect.c:163
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2598
static bool is_orclause(const void *clause)
Definition: nodeFuncs.h:106
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:97
Definition: nodes.h:539
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
Expr * make_orclause(List *orclauses)
Definition: makefuncs.c:652
static Node * process_sublinks_mutator(Node *node, process_sublinks_context *context)
Definition: subselect.c:1928
Expr * make_andclause(List *andclauses)
Definition: makefuncs.c:636
List * lappend(List *list, void *datum)
Definition: list.c:336
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
Definition: pg_list.h:50

◆ replace_correlation_vars_mutator()

static Node * replace_correlation_vars_mutator ( Node node,
PlannerInfo root 
)
static

Definition at line 1880 of file subselect.c.

References expression_tree_mutator(), IsA, replace_outer_agg(), replace_outer_grouping(), replace_outer_placeholdervar(), and replace_outer_var().

Referenced by SS_replace_correlation_vars().

1881 {
1882  if (node == NULL)
1883  return NULL;
1884  if (IsA(node, Var))
1885  {
1886  if (((Var *) node)->varlevelsup > 0)
1887  return (Node *) replace_outer_var(root, (Var *) node);
1888  }
1889  if (IsA(node, PlaceHolderVar))
1890  {
1891  if (((PlaceHolderVar *) node)->phlevelsup > 0)
1892  return (Node *) replace_outer_placeholdervar(root,
1893  (PlaceHolderVar *) node);
1894  }
1895  if (IsA(node, Aggref))
1896  {
1897  if (((Aggref *) node)->agglevelsup > 0)
1898  return (Node *) replace_outer_agg(root, (Aggref *) node);
1899  }
1900  if (IsA(node, GroupingFunc))
1901  {
1902  if (((GroupingFunc *) node)->agglevelsup > 0)
1903  return (Node *) replace_outer_grouping(root, (GroupingFunc *) node);
1904  }
1905  return expression_tree_mutator(node,
1907  (void *) root);
1908 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2598
Definition: nodes.h:539
Definition: primnodes.h:186
Param * replace_outer_placeholdervar(PlannerInfo *root, PlaceHolderVar *phv)
Definition: paramassign.c:196
Param * replace_outer_var(PlannerInfo *root, Var *var)
Definition: paramassign.c:119
Param * replace_outer_grouping(PlannerInfo *root, GroupingFunc *grp)
Definition: paramassign.c:269
static Node * replace_correlation_vars_mutator(Node *node, PlannerInfo *root)
Definition: subselect.c:1880
Param * replace_outer_agg(PlannerInfo *root, Aggref *agg)
Definition: paramassign.c:223

◆ simplify_EXISTS_query()

static bool simplify_EXISTS_query ( PlannerInfo root,
Query query 
)
static

Definition at line 1546 of file subselect.c.

References Assert, CMD_SELECT, Query::commandType, Const::constisnull, Const::consttype, Const::constvalue, DatumGetInt64, Query::distinctClause, eval_const_expressions(), Query::groupClause, Query::groupingSets, Query::hasAggs, Query::hasDistinctOn, Query::hasModifyingCTE, Query::hasTargetSRFs, Query::hasWindowFuncs, Query::havingQual, IsA, Query::limitCount, Query::limitOffset, NIL, Query::rowMarks, Query::setOperations, Query::sortClause, Query::targetList, and Query::windowClause.

Referenced by convert_EXISTS_sublink_to_join(), and make_subplan().

1547 {
1548  /*
1549  * We don't try to simplify at all if the query uses set operations,
1550  * aggregates, grouping sets, SRFs, modifying CTEs, HAVING, OFFSET, or FOR
1551  * UPDATE/SHARE; none of these seem likely in normal usage and their
1552  * possible effects are complex. (Note: we could ignore an "OFFSET 0"
1553  * clause, but that traditionally is used as an optimization fence, so we
1554  * don't.)
1555  */
1556  if (query->commandType != CMD_SELECT ||
1557  query->setOperations ||
1558  query->hasAggs ||
1559  query->groupingSets ||
1560  query->hasWindowFuncs ||
1561  query->hasTargetSRFs ||
1562  query->hasModifyingCTE ||
1563  query->havingQual ||
1564  query->limitOffset ||
1565  query->rowMarks)
1566  return false;
1567 
1568  /*
1569  * LIMIT with a constant positive (or NULL) value doesn't affect the
1570  * semantics of EXISTS, so let's ignore such clauses. This is worth doing
1571  * because people accustomed to certain other DBMSes may be in the habit
1572  * of writing EXISTS(SELECT ... LIMIT 1) as an optimization. If there's a
1573  * LIMIT with anything else as argument, though, we can't simplify.
1574  */
1575  if (query->limitCount)
1576  {
1577  /*
1578  * The LIMIT clause has not yet been through eval_const_expressions,
1579  * so we have to apply that here. It might seem like this is a waste
1580  * of cycles, since the only case plausibly worth worrying about is
1581  * "LIMIT 1" ... but what we'll actually see is "LIMIT int8(1::int4)",
1582  * so we have to fold constants or we're not going to recognize it.
1583  */
1584  Node *node = eval_const_expressions(root, query->limitCount);
1585  Const *limit;
1586 
1587  /* Might as well update the query if we simplified the clause. */
1588  query->limitCount = node;
1589 
1590  if (!IsA(node, Const))
1591  return false;
1592 
1593  limit = (Const *) node;
1594  Assert(limit->consttype == INT8OID);
1595  if (!limit->constisnull && DatumGetInt64(limit->constvalue) <= 0)
1596  return false;
1597 
1598  /* Whether or not the targetlist is safe, we can drop the LIMIT. */
1599  query->limitCount = NULL;
1600  }
1601 
1602  /*
1603  * Otherwise, we can throw away the targetlist, as well as any GROUP,
1604  * WINDOW, DISTINCT, and ORDER BY clauses; none of those clauses will
1605  * change a nonzero-rows result to zero rows or vice versa. (Furthermore,
1606  * since our parsetree representation of these clauses depends on the
1607  * targetlist, we'd better throw them away if we drop the targetlist.)
1608  */
1609  query->targetList = NIL;
1610  query->groupClause = NIL;
1611  query->windowClause = NIL;
1612  query->distinctClause = NIL;
1613  query->sortClause = NIL;
1614  query->hasDistinctOn = false;
1615 
1616  return true;
1617 }
Datum constvalue
Definition: primnodes.h:219
Node * limitOffset
Definition: parsenodes.h:171
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
List * sortClause
Definition: parsenodes.h:169
bool hasAggs
Definition: parsenodes.h:133
List * groupingSets
Definition: parsenodes.h:161
Definition: nodes.h:539
Node * eval_const_expressions(PlannerInfo *root, Node *node)
Definition: clauses.c:2093
List * rowMarks
Definition: parsenodes.h:175
bool hasDistinctOn
Definition: parsenodes.h:137
List * windowClause
Definition: parsenodes.h:165
List * targetList
Definition: parsenodes.h:150
Oid consttype
Definition: primnodes.h:215
List * distinctClause
Definition: parsenodes.h:167
#define DatumGetInt64(X)
Definition: postgres.h:651
Node * limitCount
Definition: parsenodes.h:172
CmdType commandType
Definition: parsenodes.h:120
bool hasTargetSRFs
Definition: parsenodes.h:135
#define Assert(condition)
Definition: c.h:804
bool hasWindowFuncs
Definition: parsenodes.h:134
Node * setOperations
Definition: parsenodes.h:177
List * groupClause
Definition: parsenodes.h:158
bool hasModifyingCTE
Definition: parsenodes.h:139
Node * havingQual
Definition: parsenodes.h:163
bool constisnull
Definition: primnodes.h:220

◆ SS_attach_initplans()

void SS_attach_initplans ( PlannerInfo root,
Plan plan 
)

Definition at line 2184 of file subselect.c.

References PlannerInfo::init_plans, and Plan::initPlan.

Referenced by create_plan().

2185 {
2186  plan->initPlan = root->init_plans;
2187 }
List * init_plans
Definition: pathnodes.h:241
List * initPlan
Definition: plannodes.h:145

◆ SS_charge_for_initplans()

void SS_charge_for_initplans ( PlannerInfo root,
RelOptInfo final_rel 
)

Definition at line 2127 of file subselect.c.

References RelOptInfo::consider_parallel, PlannerInfo::init_plans, lfirst, NIL, Path::parallel_safe, RelOptInfo::partial_pathlist, RelOptInfo::pathlist, SubPlan::per_call_cost, SubPlan::startup_cost, Path::startup_cost, and Path::total_cost.

Referenced by build_minmax_path(), and subquery_planner().

2128 {
2129  Cost initplan_cost;
2130  ListCell *lc;
2131 
2132  /* Nothing to do if no initPlans */
2133  if (root->init_plans == NIL)
2134  return;
2135 
2136  /*
2137  * Compute the cost increment just once, since it will be the same for all
2138  * Paths. We assume each initPlan gets run once during top plan startup.
2139  * This is a conservative overestimate, since in fact an initPlan might be
2140  * executed later than plan startup, or even not at all.
2141  */
2142  initplan_cost = 0;
2143  foreach(lc, root->init_plans)
2144  {
2145  SubPlan *initsubplan = (SubPlan *) lfirst(lc);
2146 
2147  initplan_cost += initsubplan->startup_cost + initsubplan->per_call_cost;
2148  }
2149 
2150  /*
2151  * Now adjust the costs and parallel_safe flags.
2152  */
2153  foreach(lc, final_rel->pathlist)
2154  {
2155  Path *path = (Path *) lfirst(lc);
2156 
2157  path->startup_cost += initplan_cost;
2158  path->total_cost += initplan_cost;
2159  path->parallel_safe = false;
2160  }
2161 
2162  /*
2163  * Forget about any partial paths and clear consider_parallel, too;
2164  * they're not usable if we attached an initPlan.
2165  */
2166  final_rel->partial_pathlist = NIL;
2167  final_rel->consider_parallel = false;
2168 
2169  /* We needn't do set_cheapest() here, caller will do it */
2170 }
#define NIL
Definition: pg_list.h:65
List * partial_pathlist
Definition: pathnodes.h:692
Cost startup_cost
Definition: pathnodes.h:1185
List * init_plans
Definition: pathnodes.h:241
Cost per_call_cost
Definition: primnodes.h:776
Cost total_cost
Definition: pathnodes.h:1186
#define lfirst(lc)
Definition: pg_list.h:169
bool parallel_safe
Definition: pathnodes.h:1180
bool consider_parallel
Definition: pathnodes.h:684
Cost startup_cost
Definition: primnodes.h:775
List * pathlist
Definition: pathnodes.h:690
double Cost
Definition: nodes.h:673

◆ SS_finalize_plan()

void SS_finalize_plan ( PlannerInfo root,
Plan plan 
)

Definition at line 2199 of file subselect.c.

References finalize_plan(), and PlannerInfo::outer_params.

Referenced by standard_planner().

2200 {
2201  /* No setup needed, just recurse through plan tree. */
2202  (void) finalize_plan(root, plan, -1, root->outer_params, NULL);
2203 }
static Bitmapset * finalize_plan(PlannerInfo *root, Plan *plan, int gather_param, Bitmapset *valid_params, Bitmapset *scan_params)
Definition: subselect.c:2237
Bitmapset * outer_params
Definition: pathnodes.h:176

◆ SS_identify_outer_params()

void SS_identify_outer_params ( PlannerInfo root)

Definition at line 2065 of file subselect.c.

References bms_add_member(), PlannerInfo::glob, PlannerInfo::init_plans, lfirst, lfirst_int, NIL, PlannerInfo::outer_params, PlannerGlobal::paramExecTypes, PlannerParamItem::paramId, PlannerInfo::parent_root, PlannerInfo::plan_params, SubPlan::setParam, and PlannerInfo::wt_param_id.

Referenced by build_minmax_path(), and subquery_planner().

2066 {
2067  Bitmapset *outer_params;
2068  PlannerInfo *proot;
2069  ListCell *l;
2070 
2071  /*
2072  * If no parameters have been assigned anywhere in the tree, we certainly
2073  * don't need to do anything here.
2074  */
2075  if (root->glob->paramExecTypes == NIL)
2076  return;
2077 
2078  /*
2079  * Scan all query levels above this one to see which parameters are due to
2080  * be available from them, either because lower query levels have
2081  * requested them (via plan_params) or because they will be available from
2082  * initPlans of those levels.
2083  */
2084  outer_params = NULL;
2085  for (proot = root->parent_root; proot != NULL; proot = proot->parent_root)
2086  {
2087  /* Include ordinary Var/PHV/Aggref params */
2088  foreach(l, proot->plan_params)
2089  {
2090  PlannerParamItem *pitem = (PlannerParamItem *) lfirst(l);
2091 
2092  outer_params = bms_add_member(outer_params, pitem->paramId);
2093  }
2094  /* Include any outputs of outer-level initPlans */
2095  foreach(l, proot->init_plans)
2096  {
2097  SubPlan *initsubplan = (SubPlan *) lfirst(l);
2098  ListCell *l2;
2099 
2100  foreach(l2, initsubplan->setParam)
2101  {
2102  outer_params = bms_add_member(outer_params, lfirst_int(l2));
2103  }
2104  }
2105  /* Include worktable ID, if a recursive query is being planned */
2106  if (proot->wt_param_id >= 0)
2107  outer_params = bms_add_member(outer_params, proot->wt_param_id);
2108  }
2109  root->outer_params = outer_params;
2110 }
#define NIL
Definition: pg_list.h:65
List * plan_params
Definition: pathnodes.h:175
PlannerInfo * parent_root
Definition: pathnodes.h:167
int wt_param_id
Definition: pathnodes.h:363
List * paramExecTypes
Definition: pathnodes.h:113
#define lfirst_int(lc)
Definition: pg_list.h:170
PlannerGlobal * glob
Definition: pathnodes.h:163
List * init_plans
Definition: pathnodes.h:241
#define lfirst(lc)
Definition: pg_list.h:169
List * setParam
Definition: primnodes.h:770
Bitmapset * outer_params
Definition: pathnodes.h:176
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736

◆ SS_make_initplan_from_plan()

void SS_make_initplan_from_plan ( PlannerInfo root,
PlannerInfo subroot,
Plan plan,
Param prm 
)

Definition at line 2956 of file subselect.c.

References cost_subplan(), EXPR_SUBLINK, SubPlan::firstColCollation, SubPlan::firstColType, SubPlan::firstColTypmod, get_first_col_type(), PlannerInfo::glob, PlannerInfo::init_plans, lappend(), list_length(), list_make1_int, makeNode, Param::paramid, SubPlan::plan_id, SubPlan::plan_name, psprintf(), SubPlan::setParam, SubPlan::subLinkType, PlannerGlobal::subplans, and PlannerGlobal::subroots.

Referenced by create_minmaxagg_plan().

2959 {
2960  SubPlan *node;
2961 
2962  /*
2963  * Add the subplan and its PlannerInfo to the global lists.
2964  */
2965  root->glob->subplans = lappend(root->glob->subplans, plan);
2966  root->glob->subroots = lappend(root->glob->subroots, subroot);
2967 
2968  /*
2969  * Create a SubPlan node and add it to the outer list of InitPlans. Note
2970  * it has to appear after any other InitPlans it might depend on (see
2971  * comments in ExecReScan).
2972  */
2973  node = makeNode(SubPlan);
2974  node->subLinkType = EXPR_SUBLINK;
2975  node->plan_id = list_length(root->glob->subplans);
2976  node->plan_name = psprintf("InitPlan %d (returns $%d)",
2977  node->plan_id, prm->paramid);
2978  get_first_col_type(plan, &node->firstColType, &node->firstColTypmod,
2979  &node->firstColCollation);
2980  node->setParam = list_make1_int(prm->paramid);
2981 
2982  root->init_plans = lappend(root->init_plans, node);
2983 
2984  /*
2985  * The node can't have any inputs (since it's an initplan), so the
2986  * parParam and args lists remain empty.
2987  */
2988 
2989  /* Set costs of SubPlan using info from the plan tree */
2990  cost_subplan(subroot, node, plan);
2991 }
Oid firstColType
Definition: primnodes.h:756
int plan_id
Definition: primnodes.h:752
static void get_first_col_type(Plan *plan, Oid *coltype, int32 *coltypmod, Oid *colcollation)
Definition: subselect.c:119
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
SubLinkType subLinkType
Definition: primnodes.h:747
List * subplans
Definition: pathnodes.h:95
PlannerGlobal * glob
Definition: pathnodes.h:163
#define list_make1_int(x1)
Definition: pg_list.h:221
List * lappend(List *list, void *datum)
Definition: list.c:336
List * init_plans
Definition: pathnodes.h:241
int32 firstColTypmod
Definition: primnodes.h:757
#define makeNode(_type_)
Definition: nodes.h:587
char * plan_name
Definition: primnodes.h:754
List * setParam
Definition: primnodes.h:770
int paramid
Definition: primnodes.h:268
void cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan)
Definition: costsize.c:4103
static int list_length(const List *l)
Definition: pg_list.h:149
List * subroots
Definition: pathnodes.h:97
Oid firstColCollation
Definition: primnodes.h:758

◆ SS_make_initplan_output_param()

Param* SS_make_initplan_output_param ( PlannerInfo root,
Oid  resulttype,
int32  resulttypmod,
Oid  resultcollation 
)

Definition at line 2940 of file subselect.c.

References generate_new_exec_param().

Referenced by preprocess_minmax_aggregates().

2943 {
2944  return generate_new_exec_param(root, resulttype,
2945  resulttypmod, resultcollation);
2946 }
Param * generate_new_exec_param(PlannerInfo *root, Oid paramtype, int32 paramtypmod, Oid paramcollation)
Definition: paramassign.c:557

◆ SS_process_ctes()

void SS_process_ctes ( PlannerInfo root)

Definition at line 892 of file subselect.c.

References SubPlan::args, Assert, assign_special_exec_param(), RelOptInfo::cheapest_total_path, CMD_SELECT, contain_dml(), contain_outer_selfref(), contain_volatile_functions(), copyObject, cost_subplan(), create_plan(), PlannerInfo::cte_plan_ids, CTE_SUBLINK, Query::cteList, CommonTableExpr::ctematerialized, CTEMaterializeDefault, CTEMaterializeNever, CommonTableExpr::ctename, CommonTableExpr::ctequery, CommonTableExpr::cterecursive, CommonTableExpr::cterefcount, elog, ERROR, fetch_upper_rel(), SubPlan::firstColCollation, SubPlan::firstColType, SubPlan::firstColTypmod, get_first_col_type(), PlannerInfo::glob, PlannerInfo::init_plans, inline_cte(), lappend(), lappend_int(), lfirst, list_length(), list_make1_int, makeNode, NIL, SubPlan::parallel_safe, SubPlan::paramIds, SubPlan::parParam, PlannerInfo::parse, SubPlan::plan_id, SubPlan::plan_name, PlannerInfo::plan_params, psprintf(), SubPlan::setParam, splan, SubPlan::subLinkType, PlannerGlobal::subplans, subquery_planner(), PlannerGlobal::subroots, SubPlan::testexpr, SubPlan::unknownEqFalse, UPPERREL_FINAL, and SubPlan::useHashTable.

Referenced by subquery_planner().

893 {
894  ListCell *lc;
895 
896  Assert(root->cte_plan_ids == NIL);
897 
898  foreach(lc, root->parse->cteList)
899  {
900  CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
901  CmdType cmdType = ((Query *) cte->ctequery)->commandType;
902  Query *subquery;
903  PlannerInfo *subroot;
904  RelOptInfo *final_rel;
905  Path *best_path;
906  Plan *plan;
907  SubPlan *splan;
908  int paramid;
909 
910  /*
911  * Ignore SELECT CTEs that are not actually referenced anywhere.
912  */
913  if (cte->cterefcount == 0 && cmdType == CMD_SELECT)
914  {
915  /* Make a dummy entry in cte_plan_ids */
916  root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
917  continue;
918  }
919 
920  /*
921  * Consider inlining the CTE (creating RTE_SUBQUERY RTE(s)) instead of
922  * implementing it as a separately-planned CTE.
923  *
924  * We cannot inline if any of these conditions hold:
925  *
926  * 1. The user said not to (the CTEMaterializeAlways option).
927  *
928  * 2. The CTE is recursive.
929  *
930  * 3. The CTE has side-effects; this includes either not being a plain
931  * SELECT, or containing volatile functions. Inlining might change
932  * the side-effects, which would be bad.
933  *
934  * 4. The CTE is multiply-referenced and contains a self-reference to
935  * a recursive CTE outside itself. Inlining would result in multiple
936  * recursive self-references, which we don't support.
937  *
938  * Otherwise, we have an option whether to inline or not. That should
939  * always be a win if there's just a single reference, but if the CTE
940  * is multiply-referenced then it's unclear: inlining adds duplicate
941  * computations, but the ability to absorb restrictions from the outer
942  * query level could outweigh that. We do not have nearly enough
943  * information at this point to tell whether that's true, so we let
944  * the user express a preference. Our default behavior is to inline
945  * only singly-referenced CTEs, but a CTE marked CTEMaterializeNever
946  * will be inlined even if multiply referenced.
947  *
948  * Note: we check for volatile functions last, because that's more
949  * expensive than the other tests needed.
950  */
951  if ((cte->ctematerialized == CTEMaterializeNever ||
953  cte->cterefcount == 1)) &&
954  !cte->cterecursive &&
955  cmdType == CMD_SELECT &&
956  !contain_dml(cte->ctequery) &&
957  (cte->cterefcount <= 1 ||
960  {
961  inline_cte(root, cte);
962  /* Make a dummy entry in cte_plan_ids */
963  root->cte_plan_ids = lappend_int(root->cte_plan_ids, -1);
964  continue;
965  }
966 
967  /*
968  * Copy the source Query node. Probably not necessary, but let's keep
969  * this similar to make_subplan.
970  */
971  subquery = (Query *) copyObject(cte->ctequery);
972 
973  /* plan_params should not be in use in current query level */
974  Assert(root->plan_params == NIL);
975 
976  /*
977  * Generate Paths for the CTE query. Always plan for full retrieval
978  * --- we don't have enough info to predict otherwise.
979  */
980  subroot = subquery_planner(root->glob, subquery,
981  root,
982  cte->cterecursive, 0.0);
983 
984  /*
985  * Since the current query level doesn't yet contain any RTEs, it
986  * should not be possible for the CTE to have requested parameters of
987  * this level.
988  */
989  if (root->plan_params)
990  elog(ERROR, "unexpected outer reference in CTE query");
991 
992  /*
993  * Select best Path and turn it into a Plan. At least for now, there
994  * seems no reason to postpone doing that.
995  */
996  final_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
997  best_path = final_rel->cheapest_total_path;
998 
999  plan = create_plan(subroot, best_path);
1000 
1001  /*
1002  * Make a SubPlan node for it. This is just enough unlike
1003  * build_subplan that we can't share code.
1004  *
1005  * Note plan_id, plan_name, and cost fields are set further down.
1006  */
1007  splan = makeNode(SubPlan);
1008  splan->subLinkType = CTE_SUBLINK;
1009  splan->testexpr = NULL;
1010  splan->paramIds = NIL;
1011  get_first_col_type(plan, &splan->firstColType, &splan->firstColTypmod,
1012  &splan->firstColCollation);
1013  splan->useHashTable = false;
1014  splan->unknownEqFalse = false;
1015 
1016  /*
1017  * CTE scans are not considered for parallelism (cf
1018  * set_rel_consider_parallel), and even if they were, initPlans aren't
1019  * parallel-safe.
1020  */
1021  splan->parallel_safe = false;
1022  splan->setParam = NIL;
1023  splan->parParam = NIL;
1024  splan->args = NIL;
1025 
1026  /*
1027  * The node can't have any inputs (since it's an initplan), so the
1028  * parParam and args lists remain empty. (It could contain references
1029  * to earlier CTEs' output param IDs, but CTE outputs are not
1030  * propagated via the args list.)
1031  */
1032 
1033  /*
1034  * Assign a param ID to represent the CTE's output. No ordinary
1035  * "evaluation" of this param slot ever happens, but we use the param
1036  * ID for setParam/chgParam signaling just as if the CTE plan were
1037  * returning a simple scalar output. (Also, the executor abuses the
1038  * ParamExecData slot for this param ID for communication among
1039  * multiple CteScan nodes that might be scanning this CTE.)
1040  */
1041  paramid = assign_special_exec_param(root);
1042  splan->setParam = list_make1_int(paramid);
1043 
1044  /*
1045  * Add the subplan and its PlannerInfo to the global lists.
1046  */
1047  root->glob->subplans = lappend(root->glob->subplans, plan);
1048  root->glob->subroots = lappend(root->glob->subroots, subroot);
1049  splan->plan_id = list_length(root->glob->subplans);
1050 
1051  root->init_plans = lappend(root->init_plans, splan);
1052 
1053  root->cte_plan_ids = lappend_int(root->cte_plan_ids, splan->plan_id);
1054 
1055  /* Label the subplan for EXPLAIN purposes */
1056  splan->plan_name = psprintf("CTE %s", cte->ctename);
1057 
1058  /* Lastly, fill in the cost estimates for use later */
1059  cost_subplan(root, splan, plan);
1060  }
1061 }
#define NIL
Definition: pg_list.h:65
Oid firstColType
Definition: primnodes.h:756
Query * parse
Definition: pathnodes.h:161
int plan_id
Definition: primnodes.h:752
List * plan_params
Definition: pathnodes.h:175
static void get_first_col_type(Plan *plan, Oid *coltype, int32 *coltypmod, Oid *colcollation)
Definition: subselect.c:119
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
SubLinkType subLinkType
Definition: primnodes.h:747
List * paramIds
Definition: primnodes.h:750
bool contain_volatile_functions(Node *clause)
Definition: clauses.c:451
int assign_special_exec_param(PlannerInfo *root)
Definition: paramassign.c:584
Plan * create_plan(PlannerInfo *root, Path *best_path)
Definition: createplan.c:332
#define ERROR
Definition: elog.h:46
static void inline_cte(PlannerInfo *root, CommonTableExpr *cte)
Definition: subselect.c:1151
RelOptInfo * fetch_upper_rel(PlannerInfo *root, UpperRelationKind kind, Relids relids)
Definition: relnode.c:1207
struct Path * cheapest_total_path
Definition: pathnodes.h:694
List * subplans
Definition: pathnodes.h:95
PlannerGlobal * glob
Definition: pathnodes.h:163
static SPIPlanPtr splan
Definition: regress.c:263
#define list_make1_int(x1)
Definition: pg_list.h:221
List * lappend_int(List *list, int datum)
Definition: list.c:354
List * lappend(List *list, void *datum)
Definition: list.c:336
List * cte_plan_ids
Definition: pathnodes.h:243
Node * testexpr
Definition: primnodes.h:749
List * init_plans
Definition: pathnodes.h:241
static bool contain_dml(Node *node)
Definition: subselect.c:1069
static bool contain_outer_selfref(Node *node)
Definition: subselect.c:1096
int32 firstColTypmod
Definition: primnodes.h:757
#define makeNode(_type_)
Definition: nodes.h:587
char * plan_name
Definition: primnodes.h:754
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
CTEMaterialize ctematerialized
Definition: parsenodes.h:1502
List * setParam
Definition: primnodes.h:770
void cost_subplan(PlannerInfo *root, SubPlan *subplan, Plan *plan)
Definition: costsize.c:4103
bool unknownEqFalse
Definition: primnodes.h:763
static int list_length(const List *l)
Definition: pg_list.h:149
List * subroots
Definition: pathnodes.h:97
List * parParam
Definition: primnodes.h:772
bool parallel_safe
Definition: primnodes.h:766
List * cteList
Definition: parsenodes.h:145
#define elog(elevel,...)
Definition: elog.h:232
bool useHashTable
Definition: primnodes.h:761
Oid firstColCollation
Definition: primnodes.h:758
#define copyObject(obj)
Definition: nodes.h:655
PlannerInfo * subquery_planner(PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root, bool hasRecursion, double tuple_fraction)
Definition: planner.c:590
CmdType
Definition: nodes.h:682
List * args
Definition: primnodes.h:773

◆ SS_process_sublinks()

Node* SS_process_sublinks ( PlannerInfo root,
Node expr,
bool  isQual 
)

Definition at line 1918 of file subselect.c.

References process_sublinks_context::isTopQual, process_sublinks_mutator(), convert_testexpr_context::root, and process_sublinks_context::root.

Referenced by build_subplan(), and preprocess_expression().

1919 {
1920  process_sublinks_context context;
1921 
1922  context.root = root;
1923  context.isTopQual = isQual;
1924  return process_sublinks_mutator(expr, &context);
1925 }
static Node * process_sublinks_mutator(Node *node, process_sublinks_context *context)
Definition: subselect.c:1928

◆ SS_replace_correlation_vars()

Node* SS_replace_correlation_vars ( PlannerInfo root,
Node expr 
)

Definition at line 1873 of file subselect.c.

References replace_correlation_vars_mutator().

Referenced by preprocess_expression().

1874 {
1875  /* No setup needed for tree walk, so away we go */
1876  return replace_correlation_vars_mutator(expr, root);
1877 }
static Node * replace_correlation_vars_mutator(Node *node, PlannerInfo *root)
Definition: subselect.c:1880

◆ subpath_is_hashable()

static bool subpath_is_hashable ( Path path)
static

Definition at line 748 of file subselect.c.

References get_hash_memory_limit(), MAXALIGN, Path::pathtarget, Path::rows, SizeofHeapTupleHeader, and PathTarget::width.

Referenced by make_subplan().

749 {
750  double subquery_size;
751 
752  /*
753  * The estimated size of the subquery result must fit in hash_mem. (Note:
754  * we use heap tuple overhead here even though the tuples will actually be
755  * stored as MinimalTuples; this provides some fudge factor for hashtable
756  * overhead.)
757  */
758  subquery_size = path->rows *
760  if (subquery_size > get_hash_memory_limit())
761  return false;
762 
763  return true;
764 }
#define SizeofHeapTupleHeader
Definition: htup_details.h:184
PathTarget * pathtarget
Definition: pathnodes.h:1175
double rows
Definition: pathnodes.h:1184
size_t get_hash_memory_limit(void)
Definition: nodeHash.c:3401
#define MAXALIGN(LEN)
Definition: c.h:757

◆ subplan_is_hashable()

static bool subplan_is_hashable ( Plan plan)
static

Definition at line 724 of file subselect.c.

References get_hash_memory_limit(), MAXALIGN, Plan::plan_rows, Plan::plan_width, and SizeofHeapTupleHeader.

Referenced by build_subplan().

725 {
726  double subquery_size;
727 
728  /*
729  * The estimated size of the subquery result must fit in hash_mem. (Note:
730  * we use heap tuple overhead here even though the tuples will actually be
731  * stored as MinimalTuples; this provides some fudge factor for hashtable
732  * overhead.)
733  */
734  subquery_size = plan->plan_rows *
736  if (subquery_size > get_hash_memory_limit())
737  return false;
738 
739  return true;
740 }
#define SizeofHeapTupleHeader
Definition: htup_details.h:184
double plan_rows
Definition: plannodes.h:123
int plan_width
Definition: plannodes.h:124
size_t get_hash_memory_limit(void)
Definition: nodeHash.c:3401
#define MAXALIGN(LEN)
Definition: c.h:757

◆ test_opexpr_is_hashable()

static bool test_opexpr_is_hashable ( OpExpr testexpr,
List param_ids 
)
static

Definition at line 804 of file subselect.c.

References OpExpr::args, contain_exec_param(), contain_var_clause(), hash_ok_operator(), linitial, list_length(), and lsecond.

Referenced by testexpr_is_hashable().

805 {
806  /*
807  * The combining operator must be hashable and strict. The need for
808  * hashability is obvious, since we want to use hashing. Without
809  * strictness, behavior in the presence of nulls is too unpredictable. We
810  * actually must assume even more than plain strictness: it can't yield
811  * NULL for non-null inputs, either (see nodeSubplan.c). However, hash
812  * indexes and hash joins assume that too.
813  */
814  if (!hash_ok_operator(testexpr))
815  return false;
816 
817  /*
818  * The left and right inputs must belong to the outer and inner queries
819  * respectively; hence Params that will be supplied by the subquery must
820  * not appear in the LHS, and Vars of the outer query must not appear in
821  * the RHS. (Ordinarily, this must be true because of the way that the
822  * parser builds an ANY SubLink's testexpr ... but inlining of functions
823  * could have changed the expression's structure, so we have to check.
824  * Such cases do not occur often enough to be worth trying to optimize, so
825  * we don't worry about trying to commute the clause or anything like
826  * that; we just need to be sure not to build an invalid plan.)
827  */
828  if (list_length(testexpr->args) != 2)
829  return false;
830  if (contain_exec_param((Node *) linitial(testexpr->args), param_ids))
831  return false;
832  if (contain_var_clause((Node *) lsecond(testexpr->args)))
833  return false;
834  return true;
835 }
bool contain_exec_param(Node *clause, List *param_ids)
Definition: clauses.c:1022
Definition: nodes.h:539
bool contain_var_clause(Node *node)
Definition: var.c:358
static bool hash_ok_operator(OpExpr *expr)
Definition: subselect.c:844
#define lsecond(l)
Definition: pg_list.h:179
#define linitial(l)
Definition: pg_list.h:174
static int list_length(const List *l)
Definition: pg_list.h:149
List * args
Definition: primnodes.h:548

◆ testexpr_is_hashable()

static bool testexpr_is_hashable ( Node testexpr,
List param_ids 
)
static

Definition at line 773 of file subselect.c.

References generate_unaccent_rules::args, is_andclause(), IsA, lfirst, and test_opexpr_is_hashable().

Referenced by build_subplan().

774 {
775  /*
776  * The testexpr must be a single OpExpr, or an AND-clause containing only
777  * OpExprs, each of which satisfy test_opexpr_is_hashable().
778  */
779  if (testexpr && IsA(testexpr, OpExpr))
780  {
781  if (test_opexpr_is_hashable((OpExpr *) testexpr, param_ids))
782  return true;
783  }
784  else if (is_andclause(testexpr))
785  {
786  ListCell *l;
787 
788  foreach(l, ((BoolExpr *) testexpr)->args)
789  {
790  Node *andarg = (Node *) lfirst(l);
791 
792  if (!IsA(andarg, OpExpr))
793  return false;
794  if (!test_opexpr_is_hashable((OpExpr *) andarg, param_ids))
795  return false;
796  }
797  return true;
798  }
799 
800  return false;
801 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:97
Definition: nodes.h:539
static bool test_opexpr_is_hashable(OpExpr *testexpr, List *param_ids)
Definition: subselect.c:804
#define lfirst(lc)
Definition: pg_list.h:169