PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeSubplan.h File Reference
#include "nodes/execnodes.h"
Include dependency graph for nodeSubplan.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

SubPlanStateExecInitSubPlan (SubPlan *subplan, PlanState *parent)
 
AlternativeSubPlanStateExecInitAlternativeSubPlan (AlternativeSubPlan *asplan, PlanState *parent)
 
Datum ExecSubPlan (SubPlanState *node, ExprContext *econtext, bool *isNull)
 
Datum ExecAlternativeSubPlan (AlternativeSubPlanState *node, ExprContext *econtext, bool *isNull)
 
void ExecReScanSetParamPlan (SubPlanState *node, PlanState *parent)
 
void ExecSetParamPlan (SubPlanState *node, ExprContext *econtext)
 

Function Documentation

Datum ExecAlternativeSubPlan ( AlternativeSubPlanState node,
ExprContext econtext,
bool isNull 
)

Definition at line 1200 of file nodeSubplan.c.

References AlternativeSubPlanState::active, ExecSubPlan(), list_nth_node, and AlternativeSubPlanState::subplans.

Referenced by ExecEvalAlternativeSubPlan().

1203 {
1204  /* Just pass control to the active subplan */
1206  node->subplans, node->active);
1207 
1208  return ExecSubPlan(activesp, econtext, isNull);
1209 }
#define list_nth_node(type, list, n)
Definition: pg_list.h:227
Datum ExecSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:63
AlternativeSubPlanState* ExecInitAlternativeSubPlan ( AlternativeSubPlan asplan,
PlanState parent 
)

Definition at line 1138 of file nodeSubplan.c.

References AlternativeSubPlanState::active, Assert, ExecInitSubPlan(), lappend(), lfirst_node, linitial, list_length(), lsecond, makeNode, SubPlan::per_call_cost, PlanState::plan, Plan::plan_rows, SubPlan::startup_cost, AlternativeSubPlanState::subplan, PlanState::subPlan, AlternativeSubPlan::subplans, and AlternativeSubPlanState::subplans.

Referenced by ExecInitExprRec().

1139 {
1141  double num_calls;
1142  SubPlan *subplan1;
1143  SubPlan *subplan2;
1144  Cost cost1;
1145  Cost cost2;
1146  ListCell *lc;
1147 
1148  asstate->subplan = asplan;
1149 
1150  /*
1151  * Initialize subplans. (Can we get away with only initializing the one
1152  * we're going to use?)
1153  */
1154  foreach(lc, asplan->subplans)
1155  {
1156  SubPlan *sp = lfirst_node(SubPlan, lc);
1157  SubPlanState *sps = ExecInitSubPlan(sp, parent);
1158 
1159  asstate->subplans = lappend(asstate->subplans, sps);
1160  parent->subPlan = lappend(parent->subPlan, sps);
1161  }
1162 
1163  /*
1164  * Select the one to be used. For this, we need an estimate of the number
1165  * of executions of the subplan. We use the number of output rows
1166  * expected from the parent plan node. This is a good estimate if we are
1167  * in the parent's targetlist, and an underestimate (but probably not by
1168  * more than a factor of 2) if we are in the qual.
1169  */
1170  num_calls = parent->plan->plan_rows;
1171 
1172  /*
1173  * The planner saved enough info so that we don't have to work very hard
1174  * to estimate the total cost, given the number-of-calls estimate.
1175  */
1176  Assert(list_length(asplan->subplans) == 2);
1177  subplan1 = (SubPlan *) linitial(asplan->subplans);
1178  subplan2 = (SubPlan *) lsecond(asplan->subplans);
1179 
1180  cost1 = subplan1->startup_cost + num_calls * subplan1->per_call_cost;
1181  cost2 = subplan2->startup_cost + num_calls * subplan2->per_call_cost;
1182 
1183  if (cost1 < cost2)
1184  asstate->active = 0;
1185  else
1186  asstate->active = 1;
1187 
1188  return asstate;
1189 }
double plan_rows
Definition: plannodes.h:131
List * subPlan
Definition: execnodes.h:870
#define lsecond(l)
Definition: pg_list.h:116
SubPlanState * ExecInitSubPlan(SubPlan *subplan, PlanState *parent)
Definition: nodeSubplan.c:691
#define linitial(l)
Definition: pg_list.h:111
AlternativeSubPlan * subplan
Definition: execnodes.h:788
#define lfirst_node(type, lc)
Definition: pg_list.h:109
List * lappend(List *list, void *datum)
Definition: list.c:128
Plan * plan
Definition: execnodes.h:847
Cost per_call_cost
Definition: primnodes.h:713
#define makeNode(_type_)
Definition: nodes.h:557
#define Assert(condition)
Definition: c.h:664
static int list_length(const List *l)
Definition: pg_list.h:89
Cost startup_cost
Definition: primnodes.h:712
double Cost
Definition: nodes.h:640
SubPlanState* ExecInitSubPlan ( SubPlan subplan,
PlanState parent 
)

Definition at line 691 of file nodeSubplan.c.

References ALLOCSET_DEFAULT_SIZES, ALLOCSET_SMALL_SIZES, AllocSetContextCreate(), and_clause(), OpExpr::args, SubPlan::args, SubPlanState::args, Assert, castNode, CreateExprContext(), CTE_SUBLINK, SubPlanState::cur_eq_funcs, SubPlanState::curArray, CurrentMemoryContext, SubPlanState::curTuple, elog, ERROR, EState::es_param_exec_vals, EState::es_subplanstates, ExecBuildProjectionInfo(), ExecInitExpr(), ExecInitExprList(), ExecInitExtraTupleSlot(), ParamExecData::execPlan, ExecSetSlotDescriptor(), ExecTypeFromTL(), fmgr_info(), fmgr_info_set_expr, get_compatible_hash_operators(), get_op_hash_functions(), get_opcode(), SubPlanState::hashnulls, SubPlanState::hashtable, SubPlanState::hashtablecxt, SubPlanState::hashtempcxt, i, SubPlanState::innerecontext, IsA, SubPlanState::keyColIdx, lappend(), lfirst_int, lfirst_node, SubPlanState::lhs_hash_funcs, linitial, list_length(), list_make1, list_nth(), lsecond, makeNode, makeTargetEntry(), NIL, nodeTag, OpExpr::opfuncid, OpExpr::opno, palloc(), SubPlan::paramIds, SubPlanState::parent, SubPlan::plan_id, SubPlanState::planstate, PointerGetDatum, SubPlanState::projLeft, SubPlanState::projRight, SubPlan::setParam, PlanState::state, SubPlan::subLinkType, SubPlanState::subplan, SubPlanState::tab_eq_funcs, SubPlanState::tab_hash_funcs, SubPlan::testexpr, SubPlanState::testexpr, and SubPlan::useHashTable.

Referenced by ExecInitAlternativeSubPlan(), ExecInitExprRec(), and ExecInitNode().

692 {
694  EState *estate = parent->state;
695 
696  sstate->subplan = subplan;
697 
698  /* Link the SubPlanState to already-initialized subplan */
699  sstate->planstate = (PlanState *) list_nth(estate->es_subplanstates,
700  subplan->plan_id - 1);
701 
702  /* ... and to its parent's state */
703  sstate->parent = parent;
704 
705  /* Initialize subexpressions */
706  sstate->testexpr = ExecInitExpr((Expr *) subplan->testexpr, parent);
707  sstate->args = ExecInitExprList(subplan->args, parent);
708 
709  /*
710  * initialize my state
711  */
712  sstate->curTuple = NULL;
713  sstate->curArray = PointerGetDatum(NULL);
714  sstate->projLeft = NULL;
715  sstate->projRight = NULL;
716  sstate->hashtable = NULL;
717  sstate->hashnulls = NULL;
718  sstate->hashtablecxt = NULL;
719  sstate->hashtempcxt = NULL;
720  sstate->innerecontext = NULL;
721  sstate->keyColIdx = NULL;
722  sstate->tab_hash_funcs = NULL;
723  sstate->tab_eq_funcs = NULL;
724  sstate->lhs_hash_funcs = NULL;
725  sstate->cur_eq_funcs = NULL;
726 
727  /*
728  * If this is an initplan or MULTIEXPR subplan, it has output parameters
729  * that the parent plan will use, so mark those parameters as needing
730  * evaluation. We don't actually run the subplan until we first need one
731  * of its outputs.
732  *
733  * A CTE subplan's output parameter is never to be evaluated in the normal
734  * way, so skip this in that case.
735  *
736  * Note that we don't set parent->chgParam here: the parent plan hasn't
737  * been run yet, so no need to force it to re-run.
738  */
739  if (subplan->setParam != NIL && subplan->subLinkType != CTE_SUBLINK)
740  {
741  ListCell *lst;
742 
743  foreach(lst, subplan->setParam)
744  {
745  int paramid = lfirst_int(lst);
746  ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
747 
748  prm->execPlan = sstate;
749  }
750  }
751 
752  /*
753  * If we are going to hash the subquery output, initialize relevant stuff.
754  * (We don't create the hashtable until needed, though.)
755  */
756  if (subplan->useHashTable)
757  {
758  int ncols,
759  i;
760  TupleDesc tupDesc;
761  TupleTableSlot *slot;
762  List *oplist,
763  *lefttlist,
764  *righttlist;
765  ListCell *l;
766 
767  /* We need a memory context to hold the hash table(s) */
768  sstate->hashtablecxt =
770  "Subplan HashTable Context",
772  /* and a small one for the hash tables to use as temp storage */
773  sstate->hashtempcxt =
775  "Subplan HashTable Temp Context",
777  /* and a short-lived exprcontext for function evaluation */
778  sstate->innerecontext = CreateExprContext(estate);
779  /* Silly little array of column numbers 1..n */
780  ncols = list_length(subplan->paramIds);
781  sstate->keyColIdx = (AttrNumber *) palloc(ncols * sizeof(AttrNumber));
782  for (i = 0; i < ncols; i++)
783  sstate->keyColIdx[i] = i + 1;
784 
785  /*
786  * We use ExecProject to evaluate the lefthand and righthand
787  * expression lists and form tuples. (You might think that we could
788  * use the sub-select's output tuples directly, but that is not the
789  * case if we had to insert any run-time coercions of the sub-select's
790  * output datatypes; anyway this avoids storing any resjunk columns
791  * that might be in the sub-select's output.) Run through the
792  * combining expressions to build tlists for the lefthand and
793  * righthand sides.
794  *
795  * We also extract the combining operators themselves to initialize
796  * the equality and hashing functions for the hash tables.
797  */
798  if (IsA(subplan->testexpr, OpExpr))
799  {
800  /* single combining operator */
801  oplist = list_make1(subplan->testexpr);
802  }
803  else if (and_clause((Node *) subplan->testexpr))
804  {
805  /* multiple combining operators */
806  oplist = castNode(BoolExpr, subplan->testexpr)->args;
807  }
808  else
809  {
810  /* shouldn't see anything else in a hashable subplan */
811  elog(ERROR, "unrecognized testexpr type: %d",
812  (int) nodeTag(subplan->testexpr));
813  oplist = NIL; /* keep compiler quiet */
814  }
815  Assert(list_length(oplist) == ncols);
816 
817  lefttlist = righttlist = NIL;
818  sstate->tab_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
819  sstate->tab_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
820  sstate->lhs_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
821  sstate->cur_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
822  i = 1;
823  foreach(l, oplist)
824  {
825  OpExpr *opexpr = lfirst_node(OpExpr, l);
826  Expr *expr;
827  TargetEntry *tle;
828  Oid rhs_eq_oper;
829  Oid left_hashfn;
830  Oid right_hashfn;
831 
832  Assert(list_length(opexpr->args) == 2);
833 
834  /* Process lefthand argument */
835  expr = (Expr *) linitial(opexpr->args);
836  tle = makeTargetEntry(expr,
837  i,
838  NULL,
839  false);
840  lefttlist = lappend(lefttlist, tle);
841 
842  /* Process righthand argument */
843  expr = (Expr *) lsecond(opexpr->args);
844  tle = makeTargetEntry(expr,
845  i,
846  NULL,
847  false);
848  righttlist = lappend(righttlist, tle);
849 
850  /* Lookup the equality function (potentially cross-type) */
851  fmgr_info(opexpr->opfuncid, &sstate->cur_eq_funcs[i - 1]);
852  fmgr_info_set_expr((Node *) opexpr, &sstate->cur_eq_funcs[i - 1]);
853 
854  /* Look up the equality function for the RHS type */
855  if (!get_compatible_hash_operators(opexpr->opno,
856  NULL, &rhs_eq_oper))
857  elog(ERROR, "could not find compatible hash operator for operator %u",
858  opexpr->opno);
859  fmgr_info(get_opcode(rhs_eq_oper), &sstate->tab_eq_funcs[i - 1]);
860 
861  /* Lookup the associated hash functions */
862  if (!get_op_hash_functions(opexpr->opno,
863  &left_hashfn, &right_hashfn))
864  elog(ERROR, "could not find hash function for hash operator %u",
865  opexpr->opno);
866  fmgr_info(left_hashfn, &sstate->lhs_hash_funcs[i - 1]);
867  fmgr_info(right_hashfn, &sstate->tab_hash_funcs[i - 1]);
868 
869  i++;
870  }
871 
872  /*
873  * Construct tupdescs, slots and projection nodes for left and right
874  * sides. The lefthand expressions will be evaluated in the parent
875  * plan node's exprcontext, which we don't have access to here.
876  * Fortunately we can just pass NULL for now and fill it in later
877  * (hack alert!). The righthand expressions will be evaluated in our
878  * own innerecontext.
879  */
880  tupDesc = ExecTypeFromTL(lefttlist, false);
881  slot = ExecInitExtraTupleSlot(estate);
882  ExecSetSlotDescriptor(slot, tupDesc);
883  sstate->projLeft = ExecBuildProjectionInfo(lefttlist,
884  NULL,
885  slot,
886  parent,
887  NULL);
888 
889  tupDesc = ExecTypeFromTL(righttlist, false);
890  slot = ExecInitExtraTupleSlot(estate);
891  ExecSetSlotDescriptor(slot, tupDesc);
892  sstate->projRight = ExecBuildProjectionInfo(righttlist,
893  sstate->innerecontext,
894  slot,
895  sstate->planstate,
896  NULL);
897  }
898 
899  return sstate;
900 }
#define NIL
Definition: pg_list.h:69
Definition: fmgr.h:56
bool get_compatible_hash_operators(Oid opno, Oid *lhs_opno, Oid *rhs_opno)
Definition: lsyscache.c:407
void * execPlan
Definition: params.h:99
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
ProjectionInfo * projLeft
Definition: execnodes.h:765
HeapTuple curTuple
Definition: execnodes.h:762
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate)
Definition: execTuples.c:852
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:507
TupleHashTable hashtable
Definition: execnodes.h:767
int plan_id
Definition: primnodes.h:689
ExprContext * innerecontext
Definition: execnodes.h:773
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
#define PointerGetDatum(X)
Definition: postgres.h:562
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:175
SubLinkType subLinkType
Definition: primnodes.h:684
Definition: nodes.h:509
List * paramIds
Definition: primnodes.h:687
ExprState * testexpr
Definition: execnodes.h:760
EState * state
Definition: execnodes.h:849
unsigned int Oid
Definition: postgres_ext.h:31
#define lsecond(l)
Definition: pg_list.h:116
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:775
#define list_make1(x1)
Definition: pg_list.h:139
ParamExecData * es_param_exec_vals
Definition: execnodes.h:466
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
struct PlanState * planstate
Definition: execnodes.h:758
List * args
Definition: execnodes.h:761
SubPlan * subplan
Definition: execnodes.h:757
#define lfirst_int(lc)
Definition: pg_list.h:107
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
AttrNumber * keyColIdx
Definition: execnodes.h:774
#define lfirst_node(type, lc)
Definition: pg_list.h:109
bool and_clause(Node *clause)
Definition: clauses.c:314
void * list_nth(const List *list, int n)
Definition: list.c:410
#define fmgr_info_set_expr(expr, finfo)
Definition: fmgr.h:104
List * ExecInitExprList(List *nodes, PlanState *parent)
Definition: execExpr.c:266
struct PlanState * parent
Definition: execnodes.h:759
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:235
TupleDesc ExecTypeFromTL(List *targetList, bool hasoid)
Definition: execTuples.c:888
List * lappend(List *list, void *datum)
Definition: list.c:128
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
Datum curArray
Definition: execnodes.h:763
void ExecSetSlotDescriptor(TupleTableSlot *slot, TupleDesc tupdesc)
Definition: execTuples.c:247
Oid opfuncid
Definition: primnodes.h:497
Node * testexpr
Definition: primnodes.h:686
MemoryContext hashtempcxt
Definition: execnodes.h:772
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1094
MemoryContext hashtablecxt
Definition: execnodes.h:771
#define makeNode(_type_)
Definition: nodes.h:557
#define Assert(condition)
Definition: c.h:664
FmgrInfo * tab_eq_funcs
Definition: execnodes.h:776
List * setParam
Definition: primnodes.h:707
static int list_length(const List *l)
Definition: pg_list.h:89
List * es_subplanstates
Definition: execnodes.h:486
FmgrInfo * lhs_hash_funcs
Definition: execnodes.h:777
#define nodeTag(nodeptr)
Definition: nodes.h:514
FmgrInfo * cur_eq_funcs
Definition: execnodes.h:778
void * palloc(Size size)
Definition: mcxt.c:848
ProjectionInfo * ExecBuildProjectionInfo(List *targetList, ExprContext *econtext, TupleTableSlot *slot, PlanState *parent, TupleDesc inputDesc)
Definition: execExpr.c:301
int i
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:223
bool useHashTable
Definition: primnodes.h:698
ProjectionInfo * projRight
Definition: execnodes.h:766
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:113
Oid opno
Definition: primnodes.h:496
#define elog
Definition: elog.h:219
List * args
Definition: primnodes.h:502
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
TupleHashTable hashnulls
Definition: execnodes.h:768
List * args
Definition: primnodes.h:710
void ExecReScanSetParamPlan ( SubPlanState node,
PlanState parent 
)

Definition at line 1092 of file nodeSubplan.c.

References bms_add_member(), bms_is_empty(), PlanState::chgParam, CTE_SUBLINK, elog, ERROR, EState::es_param_exec_vals, ParamExecData::execPlan, Plan::extParam, lfirst_int, NIL, SubPlan::parParam, PlanState::plan, SubPlanState::planstate, SubPlan::setParam, PlanState::state, SubPlan::subLinkType, and SubPlanState::subplan.

Referenced by ExecReScan().

1093 {
1094  PlanState *planstate = node->planstate;
1095  SubPlan *subplan = node->subplan;
1096  EState *estate = parent->state;
1097  ListCell *l;
1098 
1099  /* sanity checks */
1100  if (subplan->parParam != NIL)
1101  elog(ERROR, "direct correlated subquery unsupported as initplan");
1102  if (subplan->setParam == NIL)
1103  elog(ERROR, "setParam list of initplan is empty");
1104  if (bms_is_empty(planstate->plan->extParam))
1105  elog(ERROR, "extParam set of initplan is empty");
1106 
1107  /*
1108  * Don't actually re-scan: it'll happen inside ExecSetParamPlan if needed.
1109  */
1110 
1111  /*
1112  * Mark this subplan's output parameters as needing recalculation.
1113  *
1114  * CTE subplans are never executed via parameter recalculation; instead
1115  * they get run when called by nodeCtescan.c. So don't mark the output
1116  * parameter of a CTE subplan as dirty, but do set the chgParam bit for it
1117  * so that dependent plan nodes will get told to rescan.
1118  */
1119  foreach(l, subplan->setParam)
1120  {
1121  int paramid = lfirst_int(l);
1122  ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
1123 
1124  if (subplan->subLinkType != CTE_SUBLINK)
1125  prm->execPlan = node;
1126 
1127  parent->chgParam = bms_add_member(parent->chgParam, paramid);
1128  }
1129 }
#define NIL
Definition: pg_list.h:69
void * execPlan
Definition: params.h:99
SubLinkType subLinkType
Definition: primnodes.h:684
EState * state
Definition: execnodes.h:849
ParamExecData * es_param_exec_vals
Definition: execnodes.h:466
#define ERROR
Definition: elog.h:43
struct PlanState * planstate
Definition: execnodes.h:758
SubPlan * subplan
Definition: execnodes.h:757
#define lfirst_int(lc)
Definition: pg_list.h:107
Bitmapset * chgParam
Definition: execnodes.h:875
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:663
Plan * plan
Definition: execnodes.h:847
List * setParam
Definition: primnodes.h:707
Bitmapset * extParam
Definition: plannodes.h:162
List * parParam
Definition: primnodes.h:709
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
#define elog
Definition: elog.h:219
void ExecSetParamPlan ( SubPlanState node,
ExprContext econtext 
)

Definition at line 916 of file nodeSubplan.c.

References accumArrayResultAny(), ALL_SUBLINK, ANY_SUBLINK, SubPlanState::args, ARRAY_SUBLINK, Assert, bms_add_member(), BoolGetDatum, PlanState::chgParam, CTE_SUBLINK, SubPlanState::curArray, CurrentMemoryContext, SubPlanState::curTuple, DatumGetPointer, ExprContext::ecxt_param_exec_vals, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, ExecCopySlotTuple(), ExecEvalExprSwitchContext(), ParamExecData::execPlan, ExecProcNode(), EXISTS_SUBLINK, EXPR_SUBLINK, SubPlan::firstColType, forboth, heap_freetuple(), heap_getattr, i, initArrayResultAny(), ParamExecData::isnull, lfirst, lfirst_int, linitial_int, list_length(), makeArrayResultAny(), MemoryContextSwitchTo(), MULTIEXPR_SUBLINK, NIL, SubPlan::parParam, pfree(), SubPlanState::planstate, PointerGetDatum, ROWCOMPARE_SUBLINK, SubPlan::setParam, slot_getattr(), SubPlan::subLinkType, SubPlanState::subplan, TupleTableSlot::tts_tupleDescriptor, TupIsNull, TupleDescAttr, and ParamExecData::value.

Referenced by ExecEvalParamExec().

917 {
918  SubPlan *subplan = node->subplan;
919  PlanState *planstate = node->planstate;
920  SubLinkType subLinkType = subplan->subLinkType;
921  MemoryContext oldcontext;
922  TupleTableSlot *slot;
923  ListCell *pvar;
924  ListCell *l;
925  bool found = false;
926  ArrayBuildStateAny *astate = NULL;
927 
928  if (subLinkType == ANY_SUBLINK ||
929  subLinkType == ALL_SUBLINK)
930  elog(ERROR, "ANY/ALL subselect unsupported as initplan");
931  if (subLinkType == CTE_SUBLINK)
932  elog(ERROR, "CTE subplans should not be executed via ExecSetParamPlan");
933 
934  /* Initialize ArrayBuildStateAny in caller's context, if needed */
935  if (subLinkType == ARRAY_SUBLINK)
936  astate = initArrayResultAny(subplan->firstColType,
937  CurrentMemoryContext, true);
938 
939  /*
940  * Must switch to per-query memory context.
941  */
942  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
943 
944  /*
945  * Set Params of this plan from parent plan correlation values. (Any
946  * calculation we have to do is done in the parent econtext, since the
947  * Param values don't need to have per-query lifetime.) Currently, we
948  * expect only MULTIEXPR_SUBLINK plans to have any correlation values.
949  */
950  Assert(subplan->parParam == NIL || subLinkType == MULTIEXPR_SUBLINK);
951  Assert(list_length(subplan->parParam) == list_length(node->args));
952 
953  forboth(l, subplan->parParam, pvar, node->args)
954  {
955  int paramid = lfirst_int(l);
956  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
957 
959  econtext,
960  &(prm->isnull));
961  planstate->chgParam = bms_add_member(planstate->chgParam, paramid);
962  }
963 
964  /*
965  * Run the plan. (If it needs to be rescanned, the first ExecProcNode
966  * call will take care of that.)
967  */
968  for (slot = ExecProcNode(planstate);
969  !TupIsNull(slot);
970  slot = ExecProcNode(planstate))
971  {
972  TupleDesc tdesc = slot->tts_tupleDescriptor;
973  int i = 1;
974 
975  if (subLinkType == EXISTS_SUBLINK)
976  {
977  /* There can be only one setParam... */
978  int paramid = linitial_int(subplan->setParam);
979  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
980 
981  prm->execPlan = NULL;
982  prm->value = BoolGetDatum(true);
983  prm->isnull = false;
984  found = true;
985  break;
986  }
987 
988  if (subLinkType == ARRAY_SUBLINK)
989  {
990  Datum dvalue;
991  bool disnull;
992 
993  found = true;
994  /* stash away current value */
995  Assert(subplan->firstColType == TupleDescAttr(tdesc, 0)->atttypid);
996  dvalue = slot_getattr(slot, 1, &disnull);
997  astate = accumArrayResultAny(astate, dvalue, disnull,
998  subplan->firstColType, oldcontext);
999  /* keep scanning subplan to collect all values */
1000  continue;
1001  }
1002 
1003  if (found &&
1004  (subLinkType == EXPR_SUBLINK ||
1005  subLinkType == MULTIEXPR_SUBLINK ||
1006  subLinkType == ROWCOMPARE_SUBLINK))
1007  ereport(ERROR,
1008  (errcode(ERRCODE_CARDINALITY_VIOLATION),
1009  errmsg("more than one row returned by a subquery used as an expression")));
1010 
1011  found = true;
1012 
1013  /*
1014  * We need to copy the subplan's tuple into our own context, in case
1015  * any of the params are pass-by-ref type --- the pointers stored in
1016  * the param structs will point at this copied tuple! node->curTuple
1017  * keeps track of the copied tuple for eventual freeing.
1018  */
1019  if (node->curTuple)
1020  heap_freetuple(node->curTuple);
1021  node->curTuple = ExecCopySlotTuple(slot);
1022 
1023  /*
1024  * Now set all the setParam params from the columns of the tuple
1025  */
1026  foreach(l, subplan->setParam)
1027  {
1028  int paramid = lfirst_int(l);
1029  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1030 
1031  prm->execPlan = NULL;
1032  prm->value = heap_getattr(node->curTuple, i, tdesc,
1033  &(prm->isnull));
1034  i++;
1035  }
1036  }
1037 
1038  if (subLinkType == ARRAY_SUBLINK)
1039  {
1040  /* There can be only one setParam... */
1041  int paramid = linitial_int(subplan->setParam);
1042  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1043 
1044  /*
1045  * We build the result array in query context so it won't disappear;
1046  * to avoid leaking memory across repeated calls, we have to remember
1047  * the latest value, much as for curTuple above.
1048  */
1049  if (node->curArray != PointerGetDatum(NULL))
1050  pfree(DatumGetPointer(node->curArray));
1051  node->curArray = makeArrayResultAny(astate,
1052  econtext->ecxt_per_query_memory,
1053  true);
1054  prm->execPlan = NULL;
1055  prm->value = node->curArray;
1056  prm->isnull = false;
1057  }
1058  else if (!found)
1059  {
1060  if (subLinkType == EXISTS_SUBLINK)
1061  {
1062  /* There can be only one setParam... */
1063  int paramid = linitial_int(subplan->setParam);
1064  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1065 
1066  prm->execPlan = NULL;
1067  prm->value = BoolGetDatum(false);
1068  prm->isnull = false;
1069  }
1070  else
1071  {
1072  /* For other sublink types, set all the output params to NULL */
1073  foreach(l, subplan->setParam)
1074  {
1075  int paramid = lfirst_int(l);
1076  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1077 
1078  prm->execPlan = NULL;
1079  prm->value = (Datum) 0;
1080  prm->isnull = true;
1081  }
1082  }
1083  }
1084 
1085  MemoryContextSwitchTo(oldcontext);
1086 }
#define NIL
Definition: pg_list.h:69
Oid firstColType
Definition: primnodes.h:693
void * execPlan
Definition: params.h:99
HeapTuple curTuple
Definition: execnodes.h:762
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:301
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
ArrayBuildStateAny * initArrayResultAny(Oid input_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5497
#define PointerGetDatum(X)
Definition: postgres.h:562
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SubLinkType subLinkType
Definition: primnodes.h:684
int errcode(int sqlerrcode)
Definition: elog.c:575
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1373
#define linitial_int(l)
Definition: pg_list.h:112
void pfree(void *pointer)
Definition: mcxt.c:949
#define ERROR
Definition: elog.h:43
struct PlanState * planstate
Definition: execnodes.h:758
List * args
Definition: execnodes.h:761
SubPlan * subplan
Definition: execnodes.h:757
#define lfirst_int(lc)
Definition: pg_list.h:107
bool isnull
Definition: params.h:101
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:206
#define TupIsNull(slot)
Definition: tuptable.h:138
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define ereport(elevel, rest)
Definition: elog.h:122
Bitmapset * chgParam
Definition: execnodes.h:875
SubLinkType
Definition: primnodes.h:618
ArrayBuildStateAny * accumArrayResultAny(ArrayBuildStateAny *astate, Datum dvalue, bool disnull, Oid input_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5542
HeapTuple ExecCopySlotTuple(TupleTableSlot *slot)
Definition: execTuples.c:545
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:769
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
Datum curArray
Definition: execnodes.h:763
uintptr_t Datum
Definition: postgres.h:372
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:246
#define BoolGetDatum(X)
Definition: postgres.h:408
#define Assert(condition)
Definition: c.h:664
#define lfirst(lc)
Definition: pg_list.h:106
List * setParam
Definition: primnodes.h:707
static int list_length(const List *l)
Definition: pg_list.h:89
List * parParam
Definition: primnodes.h:709
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:698
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:202
#define DatumGetPointer(X)
Definition: postgres.h:555
Datum makeArrayResultAny(ArrayBuildStateAny *astate, MemoryContext rcontext, bool release)
Definition: arrayfuncs.c:5570
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: heaptuple.c:1142
Datum value
Definition: params.h:100
#define elog
Definition: elog.h:219
Datum ExecSubPlan ( SubPlanState node,
ExprContext econtext,
bool isNull 
)

Definition at line 63 of file nodeSubplan.c.

References CHECK_FOR_INTERRUPTS, CTE_SUBLINK, elog, ERROR, ExecHashSubPlan(), ExecScanSubPlan(), MULTIEXPR_SUBLINK, NIL, SubPlan::setParam, SubPlan::subLinkType, SubPlanState::subplan, and SubPlan::useHashTable.

Referenced by ExecAlternativeSubPlan(), and ExecEvalSubPlan().

66 {
67  SubPlan *subplan = node->subplan;
68 
70 
71  /* Set non-null as default */
72  *isNull = false;
73 
74  /* Sanity checks */
75  if (subplan->subLinkType == CTE_SUBLINK)
76  elog(ERROR, "CTE subplans should not be executed via ExecSubPlan");
77  if (subplan->setParam != NIL && subplan->subLinkType != MULTIEXPR_SUBLINK)
78  elog(ERROR, "cannot set parent params from subquery");
79 
80  /* Select appropriate evaluation strategy */
81  if (subplan->useHashTable)
82  return ExecHashSubPlan(node, econtext, isNull);
83  else
84  return ExecScanSubPlan(node, econtext, isNull);
85 }
#define NIL
Definition: pg_list.h:69
SubLinkType subLinkType
Definition: primnodes.h:684
#define ERROR
Definition: elog.h:43
SubPlan * subplan
Definition: execnodes.h:757
static Datum ExecHashSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:91
static Datum ExecScanSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:213
List * setParam
Definition: primnodes.h:707
bool useHashTable
Definition: primnodes.h:698
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define elog
Definition: elog.h:219