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 819 of file nodeSubplan.c.

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

References ALLOCSET_DEFAULT_SIZES, ALLOCSET_SMALL_SIZES, AllocSetContextCreate, OpExpr::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(), 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 ExecInitNode(), and ExecInitSubPlanExpr().

◆ ExecReScanSetParamPlan()

void ExecReScanSetParamPlan ( SubPlanState node,
PlanState parent 
)

Definition at line 1286 of file nodeSubplan.c.

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

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 1087 of file nodeSubplan.c.

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

References accumArrayResultAny(), ALL_SUBLINK, ANY_SUBLINK, SubPlan::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 1263 of file nodeSubplan.c.

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

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