PostgreSQL Source Code  git master
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)
 
Datum ExecSubPlan (SubPlanState *node, ExprContext *econtext, bool *isNull)
 
void ExecReScanSetParamPlan (SubPlanState *node, PlanState *parent)
 
void ExecSetParamPlan (SubPlanState *node, ExprContext *econtext)
 
void ExecSetParamPlanMulti (const Bitmapset *params, ExprContext *econtext)
 

Function Documentation

◆ ExecInitSubPlan()

SubPlanState* ExecInitSubPlan ( SubPlan subplan,
PlanState parent 
)

Definition at line 823 of file nodeSubplan.c.

824 {
826  EState *estate = parent->state;
827 
828  sstate->subplan = subplan;
829 
830  /* Link the SubPlanState to already-initialized subplan */
831  sstate->planstate = (PlanState *) list_nth(estate->es_subplanstates,
832  subplan->plan_id - 1);
833 
834  /*
835  * This check can fail if the planner mistakenly puts a parallel-unsafe
836  * subplan into a parallelized subquery; see ExecSerializePlan.
837  */
838  if (sstate->planstate == NULL)
839  elog(ERROR, "subplan \"%s\" was not initialized",
840  subplan->plan_name);
841 
842  /* Link to parent's state, too */
843  sstate->parent = parent;
844 
845  /* Initialize subexpressions */
846  sstate->testexpr = ExecInitExpr((Expr *) subplan->testexpr, parent);
847  sstate->args = ExecInitExprList(subplan->args, parent);
848 
849  /*
850  * initialize my state
851  */
852  sstate->curTuple = NULL;
853  sstate->curArray = PointerGetDatum(NULL);
854  sstate->projLeft = NULL;
855  sstate->projRight = NULL;
856  sstate->hashtable = NULL;
857  sstate->hashnulls = NULL;
858  sstate->hashtablecxt = NULL;
859  sstate->hashtempcxt = NULL;
860  sstate->innerecontext = NULL;
861  sstate->keyColIdx = NULL;
862  sstate->tab_eq_funcoids = NULL;
863  sstate->tab_hash_funcs = NULL;
864  sstate->tab_eq_funcs = NULL;
865  sstate->tab_collations = NULL;
866  sstate->lhs_hash_funcs = NULL;
867  sstate->cur_eq_funcs = NULL;
868 
869  /*
870  * If this is an initplan, it has output parameters that the parent plan
871  * will use, so mark those parameters as needing evaluation. We don't
872  * actually run the subplan until we first need one of its outputs.
873  *
874  * A CTE subplan's output parameter is never to be evaluated in the normal
875  * way, so skip this in that case.
876  *
877  * Note that we don't set parent->chgParam here: the parent plan hasn't
878  * been run yet, so no need to force it to re-run.
879  */
880  if (subplan->setParam != NIL && subplan->parParam == NIL &&
881  subplan->subLinkType != CTE_SUBLINK)
882  {
883  ListCell *lst;
884 
885  foreach(lst, subplan->setParam)
886  {
887  int paramid = lfirst_int(lst);
888  ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
889 
890  prm->execPlan = sstate;
891  }
892  }
893 
894  /*
895  * If we are going to hash the subquery output, initialize relevant stuff.
896  * (We don't create the hashtable until needed, though.)
897  */
898  if (subplan->useHashTable)
899  {
900  int ncols,
901  i;
902  TupleDesc tupDescLeft;
903  TupleDesc tupDescRight;
904  Oid *cross_eq_funcoids;
905  TupleTableSlot *slot;
906  List *oplist,
907  *lefttlist,
908  *righttlist;
909  ListCell *l;
910 
911  /* We need a memory context to hold the hash table(s) */
912  sstate->hashtablecxt =
914  "Subplan HashTable Context",
916  /* and a small one for the hash tables to use as temp storage */
917  sstate->hashtempcxt =
919  "Subplan HashTable Temp Context",
921  /* and a short-lived exprcontext for function evaluation */
922  sstate->innerecontext = CreateExprContext(estate);
923 
924  /*
925  * We use ExecProject to evaluate the lefthand and righthand
926  * expression lists and form tuples. (You might think that we could
927  * use the sub-select's output tuples directly, but that is not the
928  * case if we had to insert any run-time coercions of the sub-select's
929  * output datatypes; anyway this avoids storing any resjunk columns
930  * that might be in the sub-select's output.) Run through the
931  * combining expressions to build tlists for the lefthand and
932  * righthand sides.
933  *
934  * We also extract the combining operators themselves to initialize
935  * the equality and hashing functions for the hash tables.
936  */
937  if (IsA(subplan->testexpr, OpExpr))
938  {
939  /* single combining operator */
940  oplist = list_make1(subplan->testexpr);
941  }
942  else if (is_andclause(subplan->testexpr))
943  {
944  /* multiple combining operators */
945  oplist = castNode(BoolExpr, subplan->testexpr)->args;
946  }
947  else
948  {
949  /* shouldn't see anything else in a hashable subplan */
950  elog(ERROR, "unrecognized testexpr type: %d",
951  (int) nodeTag(subplan->testexpr));
952  oplist = NIL; /* keep compiler quiet */
953  }
954  ncols = list_length(oplist);
955 
956  lefttlist = righttlist = NIL;
957  sstate->numCols = ncols;
958  sstate->keyColIdx = (AttrNumber *) palloc(ncols * sizeof(AttrNumber));
959  sstate->tab_eq_funcoids = (Oid *) palloc(ncols * sizeof(Oid));
960  sstate->tab_collations = (Oid *) palloc(ncols * sizeof(Oid));
961  sstate->tab_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
962  sstate->tab_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
963  sstate->lhs_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
964  sstate->cur_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
965  /* we'll need the cross-type equality fns below, but not in sstate */
966  cross_eq_funcoids = (Oid *) palloc(ncols * sizeof(Oid));
967 
968  i = 1;
969  foreach(l, oplist)
970  {
971  OpExpr *opexpr = lfirst_node(OpExpr, l);
972  Expr *expr;
973  TargetEntry *tle;
974  Oid rhs_eq_oper;
975  Oid left_hashfn;
976  Oid right_hashfn;
977 
978  Assert(list_length(opexpr->args) == 2);
979 
980  /* Process lefthand argument */
981  expr = (Expr *) linitial(opexpr->args);
982  tle = makeTargetEntry(expr,
983  i,
984  NULL,
985  false);
986  lefttlist = lappend(lefttlist, tle);
987 
988  /* Process righthand argument */
989  expr = (Expr *) lsecond(opexpr->args);
990  tle = makeTargetEntry(expr,
991  i,
992  NULL,
993  false);
994  righttlist = lappend(righttlist, tle);
995 
996  /* Lookup the equality function (potentially cross-type) */
997  cross_eq_funcoids[i - 1] = opexpr->opfuncid;
998  fmgr_info(opexpr->opfuncid, &sstate->cur_eq_funcs[i - 1]);
999  fmgr_info_set_expr((Node *) opexpr, &sstate->cur_eq_funcs[i - 1]);
1000 
1001  /* Look up the equality function for the RHS type */
1002  if (!get_compatible_hash_operators(opexpr->opno,
1003  NULL, &rhs_eq_oper))
1004  elog(ERROR, "could not find compatible hash operator for operator %u",
1005  opexpr->opno);
1006  sstate->tab_eq_funcoids[i - 1] = get_opcode(rhs_eq_oper);
1007  fmgr_info(sstate->tab_eq_funcoids[i - 1],
1008  &sstate->tab_eq_funcs[i - 1]);
1009 
1010  /* Lookup the associated hash functions */
1011  if (!get_op_hash_functions(opexpr->opno,
1012  &left_hashfn, &right_hashfn))
1013  elog(ERROR, "could not find hash function for hash operator %u",
1014  opexpr->opno);
1015  fmgr_info(left_hashfn, &sstate->lhs_hash_funcs[i - 1]);
1016  fmgr_info(right_hashfn, &sstate->tab_hash_funcs[i - 1]);
1017 
1018  /* Set collation */
1019  sstate->tab_collations[i - 1] = opexpr->inputcollid;
1020 
1021  /* keyColIdx is just column numbers 1..n */
1022  sstate->keyColIdx[i - 1] = i;
1023 
1024  i++;
1025  }
1026 
1027  /*
1028  * Construct tupdescs, slots and projection nodes for left and right
1029  * sides. The lefthand expressions will be evaluated in the parent
1030  * plan node's exprcontext, which we don't have access to here.
1031  * Fortunately we can just pass NULL for now and fill it in later
1032  * (hack alert!). The righthand expressions will be evaluated in our
1033  * own innerecontext.
1034  */
1035  tupDescLeft = ExecTypeFromTL(lefttlist);
1036  slot = ExecInitExtraTupleSlot(estate, tupDescLeft, &TTSOpsVirtual);
1037  sstate->projLeft = ExecBuildProjectionInfo(lefttlist,
1038  NULL,
1039  slot,
1040  parent,
1041  NULL);
1042 
1043  sstate->descRight = tupDescRight = ExecTypeFromTL(righttlist);
1044  slot = ExecInitExtraTupleSlot(estate, tupDescRight, &TTSOpsVirtual);
1045  sstate->projRight = ExecBuildProjectionInfo(righttlist,
1046  sstate->innerecontext,
1047  slot,
1048  sstate->planstate,
1049  NULL);
1050 
1051  /*
1052  * Create comparator for lookups of rows in the table (potentially
1053  * cross-type comparisons).
1054  */
1055  sstate->cur_eq_comp = ExecBuildGroupingEqual(tupDescLeft, tupDescRight,
1057  ncols,
1058  sstate->keyColIdx,
1059  cross_eq_funcoids,
1060  sstate->tab_collations,
1061  parent);
1062  }
1063 
1064  return sstate;
1065 }
int16 AttrNumber
Definition: attnum.h:21
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
List * ExecInitExprList(List *nodes, PlanState *parent)
Definition: execExpr.c:319
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:127
ProjectionInfo * ExecBuildProjectionInfo(List *targetList, ExprContext *econtext, TupleTableSlot *slot, PlanState *parent, TupleDesc inputDesc)
Definition: execExpr.c:354
ExprState * ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc, const TupleTableSlotOps *lops, const TupleTableSlotOps *rops, int numCols, const AttrNumber *keyColIdx, const Oid *eqfunctions, const Oid *collations, PlanState *parent)
Definition: execExpr.c:3922
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1830
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:1937
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:304
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
#define fmgr_info_set_expr(expr, finfo)
Definition: fmgr.h:135
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:339
bool get_compatible_hash_operators(Oid opno, Oid *lhs_opno, Oid *rhs_opno)
Definition: lsyscache.c:410
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1263
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:510
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:240
MemoryContext CurrentMemoryContext
Definition: mcxt.c:131
void * palloc(Size size)
Definition: mcxt.c:1304
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:163
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:105
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define nodeTag(nodeptr)
Definition: nodes.h:133
#define makeNode(_type_)
Definition: nodes.h:155
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define lfirst_int(lc)
Definition: pg_list.h:173
#define list_make1(x1)
Definition: pg_list.h:212
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
unsigned int Oid
Definition: postgres_ext.h:31
@ CTE_SUBLINK
Definition: primnodes.h:960
ParamExecData * es_param_exec_vals
Definition: execnodes.h:660
List * es_subplanstates
Definition: execnodes.h:680
Definition: fmgr.h:57
Definition: pg_list.h:54
Definition: nodes.h:129
Oid opno
Definition: primnodes.h:774
List * args
Definition: primnodes.h:792
void * execPlan
Definition: params.h:148
EState * state
Definition: execnodes.h:1045
TupleHashTable hashtable
Definition: execnodes.h:969
ExprState * cur_eq_comp
Definition: execnodes.h:986
FmgrInfo * tab_eq_funcs
Definition: execnodes.h:983
MemoryContext hashtablecxt
Definition: execnodes.h:973
Oid * tab_eq_funcoids
Definition: execnodes.h:979
ExprContext * innerecontext
Definition: execnodes.h:975
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:982
FmgrInfo * cur_eq_funcs
Definition: execnodes.h:985
List * args
Definition: execnodes.h:962
MemoryContext hashtempcxt
Definition: execnodes.h:974
HeapTuple curTuple
Definition: execnodes.h:963
FmgrInfo * lhs_hash_funcs
Definition: execnodes.h:984
AttrNumber * keyColIdx
Definition: execnodes.h:978
struct PlanState * planstate
Definition: execnodes.h:959
TupleDesc descRight
Definition: execnodes.h:966
SubPlan * subplan
Definition: execnodes.h:958
ProjectionInfo * projLeft
Definition: execnodes.h:967
ProjectionInfo * projRight
Definition: execnodes.h:968
ExprState * testexpr
Definition: execnodes.h:961
struct PlanState * parent
Definition: execnodes.h:960
Oid * tab_collations
Definition: execnodes.h:981
TupleHashTable hashnulls
Definition: execnodes.h:970
Datum curArray
Definition: execnodes.h:964
int plan_id
Definition: primnodes.h:1026
char * plan_name
Definition: primnodes.h:1028
List * args
Definition: primnodes.h:1047
bool useHashTable
Definition: primnodes.h:1035
Node * testexpr
Definition: primnodes.h:1023
List * parParam
Definition: primnodes.h:1046
List * setParam
Definition: primnodes.h:1044
SubLinkType subLinkType
Definition: primnodes.h:1021

References ALLOCSET_DEFAULT_SIZES, ALLOCSET_SMALL_SIZES, AllocSetContextCreate, SubPlanState::args, OpExpr::args, SubPlan::args, Assert(), castNode, CreateExprContext(), CTE_SUBLINK, SubPlanState::cur_eq_comp, SubPlanState::cur_eq_funcs, SubPlanState::curArray, CurrentMemoryContext, SubPlanState::curTuple, SubPlanState::descRight, elog, ERROR, EState::es_param_exec_vals, EState::es_subplanstates, ExecBuildGroupingEqual(), ExecBuildProjectionInfo(), ExecInitExpr(), ExecInitExprList(), ExecInitExtraTupleSlot(), ParamExecData::execPlan, 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, is_andclause(), IsA, SubPlanState::keyColIdx, lappend(), lfirst_int, lfirst_node, SubPlanState::lhs_hash_funcs, linitial, list_length(), list_make1, list_nth(), lsecond, makeNode, makeTargetEntry(), NIL, nodeTag, SubPlanState::numCols, OpExpr::opno, palloc(), SubPlanState::parent, SubPlan::parParam, SubPlan::plan_id, SubPlan::plan_name, SubPlanState::planstate, PointerGetDatum(), SubPlanState::projLeft, SubPlanState::projRight, SubPlan::setParam, PlanState::state, SubPlan::subLinkType, SubPlanState::subplan, SubPlanState::tab_collations, SubPlanState::tab_eq_funcoids, SubPlanState::tab_eq_funcs, SubPlanState::tab_hash_funcs, SubPlanState::testexpr, SubPlan::testexpr, TTSOpsMinimalTuple, TTSOpsVirtual, and SubPlan::useHashTable.

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

◆ ExecReScanSetParamPlan()

void ExecReScanSetParamPlan ( SubPlanState node,
PlanState parent 
)

Definition at line 1291 of file nodeSubplan.c.

1292 {
1293  PlanState *planstate = node->planstate;
1294  SubPlan *subplan = node->subplan;
1295  EState *estate = parent->state;
1296  ListCell *l;
1297 
1298  /* sanity checks */
1299  if (subplan->parParam != NIL)
1300  elog(ERROR, "direct correlated subquery unsupported as initplan");
1301  if (subplan->setParam == NIL)
1302  elog(ERROR, "setParam list of initplan is empty");
1303  if (bms_is_empty(planstate->plan->extParam))
1304  elog(ERROR, "extParam set of initplan is empty");
1305 
1306  /*
1307  * Don't actually re-scan: it'll happen inside ExecSetParamPlan if needed.
1308  */
1309 
1310  /*
1311  * Mark this subplan's output parameters as needing recalculation.
1312  *
1313  * CTE subplans are never executed via parameter recalculation; instead
1314  * they get run when called by nodeCtescan.c. So don't mark the output
1315  * parameter of a CTE subplan as dirty, but do set the chgParam bit for it
1316  * so that dependent plan nodes will get told to rescan.
1317  */
1318  foreach(l, subplan->setParam)
1319  {
1320  int paramid = lfirst_int(l);
1321  ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
1322 
1323  if (subplan->subLinkType != CTE_SUBLINK)
1324  prm->execPlan = node;
1325 
1326  parent->chgParam = bms_add_member(parent->chgParam, paramid);
1327  }
1328 }
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
#define bms_is_empty(a)
Definition: bitmapset.h:118
Plan * plan
Definition: execnodes.h:1043
Bitmapset * chgParam
Definition: execnodes.h:1075
Bitmapset * extParam
Definition: plannodes.h:170

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

◆ ExecSetParamPlan()

void ExecSetParamPlan ( SubPlanState node,
ExprContext econtext 
)

Definition at line 1092 of file nodeSubplan.c.

1093 {
1094  SubPlan *subplan = node->subplan;
1095  PlanState *planstate = node->planstate;
1096  SubLinkType subLinkType = subplan->subLinkType;
1097  EState *estate = planstate->state;
1098  ScanDirection dir = estate->es_direction;
1099  MemoryContext oldcontext;
1100  TupleTableSlot *slot;
1101  ListCell *l;
1102  bool found = false;
1103  ArrayBuildStateAny *astate = NULL;
1104 
1105  if (subLinkType == ANY_SUBLINK ||
1106  subLinkType == ALL_SUBLINK)
1107  elog(ERROR, "ANY/ALL subselect unsupported as initplan");
1108  if (subLinkType == CTE_SUBLINK)
1109  elog(ERROR, "CTE subplans should not be executed via ExecSetParamPlan");
1110  if (subplan->parParam || node->args)
1111  elog(ERROR, "correlated subplans should not be executed via ExecSetParamPlan");
1112 
1113  /*
1114  * Enforce forward scan direction regardless of caller. It's hard but not
1115  * impossible to get here in backward scan, so make it work anyway.
1116  */
1118 
1119  /* Initialize ArrayBuildStateAny in caller's context, if needed */
1120  if (subLinkType == ARRAY_SUBLINK)
1121  astate = initArrayResultAny(subplan->firstColType,
1122  CurrentMemoryContext, true);
1123 
1124  /*
1125  * Must switch to per-query memory context.
1126  */
1127  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1128 
1129  /*
1130  * Run the plan. (If it needs to be rescanned, the first ExecProcNode
1131  * call will take care of that.)
1132  */
1133  for (slot = ExecProcNode(planstate);
1134  !TupIsNull(slot);
1135  slot = ExecProcNode(planstate))
1136  {
1137  TupleDesc tdesc = slot->tts_tupleDescriptor;
1138  int i = 1;
1139 
1140  if (subLinkType == EXISTS_SUBLINK)
1141  {
1142  /* There can be only one setParam... */
1143  int paramid = linitial_int(subplan->setParam);
1144  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1145 
1146  prm->execPlan = NULL;
1147  prm->value = BoolGetDatum(true);
1148  prm->isnull = false;
1149  found = true;
1150  break;
1151  }
1152 
1153  if (subLinkType == ARRAY_SUBLINK)
1154  {
1155  Datum dvalue;
1156  bool disnull;
1157 
1158  found = true;
1159  /* stash away current value */
1160  Assert(subplan->firstColType == TupleDescAttr(tdesc, 0)->atttypid);
1161  dvalue = slot_getattr(slot, 1, &disnull);
1162  astate = accumArrayResultAny(astate, dvalue, disnull,
1163  subplan->firstColType, oldcontext);
1164  /* keep scanning subplan to collect all values */
1165  continue;
1166  }
1167 
1168  if (found &&
1169  (subLinkType == EXPR_SUBLINK ||
1170  subLinkType == MULTIEXPR_SUBLINK ||
1171  subLinkType == ROWCOMPARE_SUBLINK))
1172  ereport(ERROR,
1173  (errcode(ERRCODE_CARDINALITY_VIOLATION),
1174  errmsg("more than one row returned by a subquery used as an expression")));
1175 
1176  found = true;
1177 
1178  /*
1179  * We need to copy the subplan's tuple into our own context, in case
1180  * any of the params are pass-by-ref type --- the pointers stored in
1181  * the param structs will point at this copied tuple! node->curTuple
1182  * keeps track of the copied tuple for eventual freeing.
1183  */
1184  if (node->curTuple)
1185  heap_freetuple(node->curTuple);
1186  node->curTuple = ExecCopySlotHeapTuple(slot);
1187 
1188  /*
1189  * Now set all the setParam params from the columns of the tuple
1190  */
1191  foreach(l, subplan->setParam)
1192  {
1193  int paramid = lfirst_int(l);
1194  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1195 
1196  prm->execPlan = NULL;
1197  prm->value = heap_getattr(node->curTuple, i, tdesc,
1198  &(prm->isnull));
1199  i++;
1200  }
1201  }
1202 
1203  if (subLinkType == ARRAY_SUBLINK)
1204  {
1205  /* There can be only one setParam... */
1206  int paramid = linitial_int(subplan->setParam);
1207  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1208 
1209  /*
1210  * We build the result array in query context so it won't disappear;
1211  * to avoid leaking memory across repeated calls, we have to remember
1212  * the latest value, much as for curTuple above.
1213  */
1214  if (node->curArray != PointerGetDatum(NULL))
1215  pfree(DatumGetPointer(node->curArray));
1216  node->curArray = makeArrayResultAny(astate,
1217  econtext->ecxt_per_query_memory,
1218  true);
1219  prm->execPlan = NULL;
1220  prm->value = node->curArray;
1221  prm->isnull = false;
1222  }
1223  else if (!found)
1224  {
1225  if (subLinkType == EXISTS_SUBLINK)
1226  {
1227  /* There can be only one setParam... */
1228  int paramid = linitial_int(subplan->setParam);
1229  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1230 
1231  prm->execPlan = NULL;
1232  prm->value = BoolGetDatum(false);
1233  prm->isnull = false;
1234  }
1235  else
1236  {
1237  /* For other sublink types, set all the output params to NULL */
1238  foreach(l, subplan->setParam)
1239  {
1240  int paramid = lfirst_int(l);
1241  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1242 
1243  prm->execPlan = NULL;
1244  prm->value = (Datum) 0;
1245  prm->isnull = true;
1246  }
1247  }
1248  }
1249 
1250  MemoryContextSwitchTo(oldcontext);
1251 
1252  /* restore scan direction */
1253  estate->es_direction = dir;
1254 }
ArrayBuildStateAny * initArrayResultAny(Oid input_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5763
ArrayBuildStateAny * accumArrayResultAny(ArrayBuildStateAny *astate, Datum dvalue, bool disnull, Oid input_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5808
Datum makeArrayResultAny(ArrayBuildStateAny *astate, MemoryContext rcontext, bool release)
Definition: arrayfuncs.c:5836
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ereport(elevel,...)
Definition: elog.h:149
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:269
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1434
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: htup_details.h:792
void pfree(void *pointer)
Definition: mcxt.c:1508
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
#define linitial_int(l)
Definition: pg_list.h:179
uintptr_t Datum
Definition: postgres.h:64
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
SubLinkType
Definition: primnodes.h:952
@ ARRAY_SUBLINK
Definition: primnodes.h:959
@ ANY_SUBLINK
Definition: primnodes.h:955
@ MULTIEXPR_SUBLINK
Definition: primnodes.h:958
@ EXPR_SUBLINK
Definition: primnodes.h:957
@ ROWCOMPARE_SUBLINK
Definition: primnodes.h:956
@ ALL_SUBLINK
Definition: primnodes.h:954
@ EXISTS_SUBLINK
Definition: primnodes.h:953
ScanDirection
Definition: sdir.h:25
@ ForwardScanDirection
Definition: sdir.h:28
ScanDirection es_direction
Definition: execnodes.h:621
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:266
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:262
bool isnull
Definition: params.h:150
Datum value
Definition: params.h:149
Oid firstColType
Definition: primnodes.h:1030
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:123
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static HeapTuple ExecCopySlotHeapTuple(TupleTableSlot *slot)
Definition: tuptable.h:460
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:389
#define TupIsNull(slot)
Definition: tuptable.h:300

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

Referenced by ExecEvalParamExec(), and ExecSetParamPlanMulti().

◆ ExecSetParamPlanMulti()

void ExecSetParamPlanMulti ( const Bitmapset params,
ExprContext econtext 
)

Definition at line 1268 of file nodeSubplan.c.

1269 {
1270  int paramid;
1271 
1272  paramid = -1;
1273  while ((paramid = bms_next_member(params, paramid)) >= 0)
1274  {
1275  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1276 
1277  if (prm->execPlan != NULL)
1278  {
1279  /* Parameter not evaluated yet, so go do it */
1280  ExecSetParamPlan(prm->execPlan, econtext);
1281  /* ExecSetParamPlan should have processed this param... */
1282  Assert(prm->execPlan == NULL);
1283  }
1284  }
1285 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
void ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
Definition: nodeSubplan.c:1092

References Assert(), bms_next_member(), ExprContext::ecxt_param_exec_vals, ParamExecData::execPlan, and ExecSetParamPlan().

Referenced by EvalPlanQualBegin(), EvalPlanQualStart(), ExecInitParallelPlan(), and ExecParallelReinitialize().

◆ ExecSubPlan()

Datum ExecSubPlan ( SubPlanState node,
ExprContext econtext,
bool isNull 
)

Definition at line 62 of file nodeSubplan.c.

65 {
66  SubPlan *subplan = node->subplan;
67  EState *estate = node->planstate->state;
68  ScanDirection dir = estate->es_direction;
69  Datum retval;
70 
72 
73  /* Set non-null as default */
74  *isNull = false;
75 
76  /* Sanity checks */
77  if (subplan->subLinkType == CTE_SUBLINK)
78  elog(ERROR, "CTE subplans should not be executed via ExecSubPlan");
79  if (subplan->setParam != NIL && subplan->subLinkType != MULTIEXPR_SUBLINK)
80  elog(ERROR, "cannot set parent params from subquery");
81 
82  /* Force forward-scan mode for evaluation */
84 
85  /* Select appropriate evaluation strategy */
86  if (subplan->useHashTable)
87  retval = ExecHashSubPlan(node, econtext, isNull);
88  else
89  retval = ExecScanSubPlan(node, econtext, isNull);
90 
91  /* restore scan direction */
92  estate->es_direction = dir;
93 
94  return retval;
95 }
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
static Datum ExecHashSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:101
static Datum ExecScanSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:223

References CHECK_FOR_INTERRUPTS, CTE_SUBLINK, elog, ERROR, EState::es_direction, ExecHashSubPlan(), ExecScanSubPlan(), ForwardScanDirection, MULTIEXPR_SUBLINK, NIL, SubPlanState::planstate, SubPlan::setParam, PlanState::state, SubPlan::subLinkType, SubPlanState::subplan, and SubPlan::useHashTable.

Referenced by ExecEvalSubPlan().