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

References ALLOCSET_DEFAULT_SIZES, ALLOCSET_SMALL_SIZES, AllocSetContextCreate, OpExpr::args, SubPlan::args, SubPlanState::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::opfuncid, palloc(), SubPlanState::parent, 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, SubPlan::testexpr, SubPlanState::testexpr, TTSOpsMinimalTuple, TTSOpsVirtual, and SubPlan::useHashTable.

Referenced by ExecInitExprRec(), and ExecInitNode().

790 {
792  EState *estate = parent->state;
793 
794  sstate->subplan = subplan;
795 
796  /* Link the SubPlanState to already-initialized subplan */
797  sstate->planstate = (PlanState *) list_nth(estate->es_subplanstates,
798  subplan->plan_id - 1);
799 
800  /*
801  * This check can fail if the planner mistakenly puts a parallel-unsafe
802  * subplan into a parallelized subquery; see ExecSerializePlan.
803  */
804  if (sstate->planstate == NULL)
805  elog(ERROR, "subplan \"%s\" was not initialized",
806  subplan->plan_name);
807 
808  /* Link to parent's state, too */
809  sstate->parent = parent;
810 
811  /* Initialize subexpressions */
812  sstate->testexpr = ExecInitExpr((Expr *) subplan->testexpr, parent);
813  sstate->args = ExecInitExprList(subplan->args, parent);
814 
815  /*
816  * initialize my state
817  */
818  sstate->curTuple = NULL;
819  sstate->curArray = PointerGetDatum(NULL);
820  sstate->projLeft = NULL;
821  sstate->projRight = NULL;
822  sstate->hashtable = NULL;
823  sstate->hashnulls = NULL;
824  sstate->hashtablecxt = NULL;
825  sstate->hashtempcxt = NULL;
826  sstate->innerecontext = NULL;
827  sstate->keyColIdx = NULL;
828  sstate->tab_eq_funcoids = NULL;
829  sstate->tab_hash_funcs = NULL;
830  sstate->tab_eq_funcs = NULL;
831  sstate->tab_collations = NULL;
832  sstate->lhs_hash_funcs = NULL;
833  sstate->cur_eq_funcs = NULL;
834 
835  /*
836  * If this is an initplan or MULTIEXPR subplan, it has output parameters
837  * that the parent plan will use, so mark those parameters as needing
838  * evaluation. We don't actually run the subplan until we first need one
839  * of its outputs.
840  *
841  * A CTE subplan's output parameter is never to be evaluated in the normal
842  * way, so skip this in that case.
843  *
844  * Note that we don't set parent->chgParam here: the parent plan hasn't
845  * been run yet, so no need to force it to re-run.
846  */
847  if (subplan->setParam != NIL && subplan->subLinkType != CTE_SUBLINK)
848  {
849  ListCell *lst;
850 
851  foreach(lst, subplan->setParam)
852  {
853  int paramid = lfirst_int(lst);
854  ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
855 
856  prm->execPlan = sstate;
857  }
858  }
859 
860  /*
861  * If we are going to hash the subquery output, initialize relevant stuff.
862  * (We don't create the hashtable until needed, though.)
863  */
864  if (subplan->useHashTable)
865  {
866  int ncols,
867  i;
868  TupleDesc tupDescLeft;
869  TupleDesc tupDescRight;
870  Oid *cross_eq_funcoids;
871  TupleTableSlot *slot;
872  List *oplist,
873  *lefttlist,
874  *righttlist;
875  ListCell *l;
876 
877  /* We need a memory context to hold the hash table(s) */
878  sstate->hashtablecxt =
880  "Subplan HashTable Context",
882  /* and a small one for the hash tables to use as temp storage */
883  sstate->hashtempcxt =
885  "Subplan HashTable Temp Context",
887  /* and a short-lived exprcontext for function evaluation */
888  sstate->innerecontext = CreateExprContext(estate);
889 
890  /*
891  * We use ExecProject to evaluate the lefthand and righthand
892  * expression lists and form tuples. (You might think that we could
893  * use the sub-select's output tuples directly, but that is not the
894  * case if we had to insert any run-time coercions of the sub-select's
895  * output datatypes; anyway this avoids storing any resjunk columns
896  * that might be in the sub-select's output.) Run through the
897  * combining expressions to build tlists for the lefthand and
898  * righthand sides.
899  *
900  * We also extract the combining operators themselves to initialize
901  * the equality and hashing functions for the hash tables.
902  */
903  if (IsA(subplan->testexpr, OpExpr))
904  {
905  /* single combining operator */
906  oplist = list_make1(subplan->testexpr);
907  }
908  else if (is_andclause(subplan->testexpr))
909  {
910  /* multiple combining operators */
911  oplist = castNode(BoolExpr, subplan->testexpr)->args;
912  }
913  else
914  {
915  /* shouldn't see anything else in a hashable subplan */
916  elog(ERROR, "unrecognized testexpr type: %d",
917  (int) nodeTag(subplan->testexpr));
918  oplist = NIL; /* keep compiler quiet */
919  }
920  ncols = list_length(oplist);
921 
922  lefttlist = righttlist = NIL;
923  sstate->numCols = ncols;
924  sstate->keyColIdx = (AttrNumber *) palloc(ncols * sizeof(AttrNumber));
925  sstate->tab_eq_funcoids = (Oid *) palloc(ncols * sizeof(Oid));
926  sstate->tab_collations = (Oid *) palloc(ncols * sizeof(Oid));
927  sstate->tab_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
928  sstate->tab_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
929  sstate->lhs_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
930  sstate->cur_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
931  /* we'll need the cross-type equality fns below, but not in sstate */
932  cross_eq_funcoids = (Oid *) palloc(ncols * sizeof(Oid));
933 
934  i = 1;
935  foreach(l, oplist)
936  {
937  OpExpr *opexpr = lfirst_node(OpExpr, l);
938  Expr *expr;
939  TargetEntry *tle;
940  Oid rhs_eq_oper;
941  Oid left_hashfn;
942  Oid right_hashfn;
943 
944  Assert(list_length(opexpr->args) == 2);
945 
946  /* Process lefthand argument */
947  expr = (Expr *) linitial(opexpr->args);
948  tle = makeTargetEntry(expr,
949  i,
950  NULL,
951  false);
952  lefttlist = lappend(lefttlist, tle);
953 
954  /* Process righthand argument */
955  expr = (Expr *) lsecond(opexpr->args);
956  tle = makeTargetEntry(expr,
957  i,
958  NULL,
959  false);
960  righttlist = lappend(righttlist, tle);
961 
962  /* Lookup the equality function (potentially cross-type) */
963  cross_eq_funcoids[i - 1] = opexpr->opfuncid;
964  fmgr_info(opexpr->opfuncid, &sstate->cur_eq_funcs[i - 1]);
965  fmgr_info_set_expr((Node *) opexpr, &sstate->cur_eq_funcs[i - 1]);
966 
967  /* Look up the equality function for the RHS type */
968  if (!get_compatible_hash_operators(opexpr->opno,
969  NULL, &rhs_eq_oper))
970  elog(ERROR, "could not find compatible hash operator for operator %u",
971  opexpr->opno);
972  sstate->tab_eq_funcoids[i - 1] = get_opcode(rhs_eq_oper);
973  fmgr_info(sstate->tab_eq_funcoids[i - 1],
974  &sstate->tab_eq_funcs[i - 1]);
975 
976  /* Lookup the associated hash functions */
977  if (!get_op_hash_functions(opexpr->opno,
978  &left_hashfn, &right_hashfn))
979  elog(ERROR, "could not find hash function for hash operator %u",
980  opexpr->opno);
981  fmgr_info(left_hashfn, &sstate->lhs_hash_funcs[i - 1]);
982  fmgr_info(right_hashfn, &sstate->tab_hash_funcs[i - 1]);
983 
984  /* Set collation */
985  sstate->tab_collations[i - 1] = opexpr->inputcollid;
986 
987  /* keyColIdx is just column numbers 1..n */
988  sstate->keyColIdx[i - 1] = i;
989 
990  i++;
991  }
992 
993  /*
994  * Construct tupdescs, slots and projection nodes for left and right
995  * sides. The lefthand expressions will be evaluated in the parent
996  * plan node's exprcontext, which we don't have access to here.
997  * Fortunately we can just pass NULL for now and fill it in later
998  * (hack alert!). The righthand expressions will be evaluated in our
999  * own innerecontext.
1000  */
1001  tupDescLeft = ExecTypeFromTL(lefttlist);
1002  slot = ExecInitExtraTupleSlot(estate, tupDescLeft, &TTSOpsVirtual);
1003  sstate->projLeft = ExecBuildProjectionInfo(lefttlist,
1004  NULL,
1005  slot,
1006  parent,
1007  NULL);
1008 
1009  sstate->descRight = tupDescRight = ExecTypeFromTL(righttlist);
1010  slot = ExecInitExtraTupleSlot(estate, tupDescRight, &TTSOpsVirtual);
1011  sstate->projRight = ExecBuildProjectionInfo(righttlist,
1012  sstate->innerecontext,
1013  slot,
1014  sstate->planstate,
1015  NULL);
1016 
1017  /*
1018  * Create comparator for lookups of rows in the table (potentially
1019  * cross-type comparisons).
1020  */
1021  sstate->cur_eq_comp = ExecBuildGroupingEqual(tupDescLeft, tupDescRight,
1023  ncols,
1024  sstate->keyColIdx,
1025  cross_eq_funcoids,
1026  sstate->tab_collations,
1027  parent);
1028  }
1029 
1030  return sstate;
1031 }
#define NIL
Definition: pg_list.h:65
Definition: fmgr.h:56
bool get_compatible_hash_operators(Oid opno, Oid *lhs_opno, Oid *rhs_opno)
Definition: lsyscache.c:408
void * execPlan
Definition: params.h:148
#define IsA(nodeptr, _type_)
Definition: nodes.h:581
ProjectionInfo * projLeft
Definition: execnodes.h:867
#define AllocSetContextCreate
Definition: memutils.h:170
HeapTuple curTuple
Definition: execnodes.h:863
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:508
TupleHashTable hashtable
Definition: execnodes.h:869
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1801
int plan_id
Definition: primnodes.h:730
ExprContext * innerecontext
Definition: execnodes.h:875
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:3359
#define castNode(_type_, nodeptr)
Definition: nodes.h:599
#define PointerGetDatum(X)
Definition: postgres.h:556
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:202
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:97
SubLinkType subLinkType
Definition: primnodes.h:725
Definition: nodes.h:530
Oid * tab_eq_funcoids
Definition: execnodes.h:879
ExprState * testexpr
Definition: execnodes.h:861
EState * state
Definition: execnodes.h:943
unsigned int Oid
Definition: postgres_ext.h:31
#define lsecond(l)
Definition: pg_list.h:179
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:882
#define list_make1(x1)
Definition: pg_list.h:206
ParamExecData * es_param_exec_vals
Definition: execnodes.h:567
ExprState * cur_eq_comp
Definition: execnodes.h:886
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:45
struct PlanState * planstate
Definition: execnodes.h:859
List * args
Definition: execnodes.h:862
SubPlan * subplan
Definition: execnodes.h:858
#define lfirst_int(lc)
Definition: pg_list.h:170
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:126
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
AttrNumber * keyColIdx
Definition: execnodes.h:878
#define lfirst_node(type, lc)
Definition: pg_list.h:172
#define fmgr_info_set_expr(expr, finfo)
Definition: fmgr.h:135
List * ExecInitExprList(List *nodes, PlanState *parent)
Definition: execExpr.c:318
struct PlanState * parent
Definition: execnodes.h:860
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:238
List * lappend(List *list, void *datum)
Definition: list.c:336
Datum curArray
Definition: execnodes.h:864
Oid opfuncid
Definition: primnodes.h:538
Node * testexpr
Definition: primnodes.h:727
MemoryContext hashtempcxt
Definition: execnodes.h:874
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1256
MemoryContext hashtablecxt
Definition: execnodes.h:873
#define makeNode(_type_)
Definition: nodes.h:578
char * plan_name
Definition: primnodes.h:732
#define Assert(condition)
Definition: c.h:804
FmgrInfo * tab_eq_funcs
Definition: execnodes.h:883
List * setParam
Definition: primnodes.h:748
Oid * tab_collations
Definition: execnodes.h:881
static int list_length(const List *l)
Definition: pg_list.h:149
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:1908
Oid inputcollid
Definition: primnodes.h:542
List * es_subplanstates
Definition: execnodes.h:584
FmgrInfo * lhs_hash_funcs
Definition: execnodes.h:884
#define nodeTag(nodeptr)
Definition: nodes.h:535
FmgrInfo * cur_eq_funcs
Definition: execnodes.h:885
void * palloc(Size size)
Definition: mcxt.c:950
ProjectionInfo * ExecBuildProjectionInfo(List *targetList, ExprContext *econtext, TupleTableSlot *slot, PlanState *parent, TupleDesc inputDesc)
Definition: execExpr.c:353
TupleDesc descRight
Definition: execnodes.h:866
#define elog(elevel,...)
Definition: elog.h:228
int i
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:301
bool useHashTable
Definition: primnodes.h:739
ProjectionInfo * projRight
Definition: execnodes.h:868
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:123
Oid opno
Definition: primnodes.h:537
List * args
Definition: primnodes.h:543
Definition: pg_list.h:50
int16 AttrNumber
Definition: attnum.h:21
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
TupleHashTable hashnulls
Definition: execnodes.h:870
List * args
Definition: primnodes.h:751

◆ ExecReScanSetParamPlan()

void ExecReScanSetParamPlan ( SubPlanState node,
PlanState parent 
)

Definition at line 1276 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().

1277 {
1278  PlanState *planstate = node->planstate;
1279  SubPlan *subplan = node->subplan;
1280  EState *estate = parent->state;
1281  ListCell *l;
1282 
1283  /* sanity checks */
1284  if (subplan->parParam != NIL)
1285  elog(ERROR, "direct correlated subquery unsupported as initplan");
1286  if (subplan->setParam == NIL)
1287  elog(ERROR, "setParam list of initplan is empty");
1288  if (bms_is_empty(planstate->plan->extParam))
1289  elog(ERROR, "extParam set of initplan is empty");
1290 
1291  /*
1292  * Don't actually re-scan: it'll happen inside ExecSetParamPlan if needed.
1293  */
1294 
1295  /*
1296  * Mark this subplan's output parameters as needing recalculation.
1297  *
1298  * CTE subplans are never executed via parameter recalculation; instead
1299  * they get run when called by nodeCtescan.c. So don't mark the output
1300  * parameter of a CTE subplan as dirty, but do set the chgParam bit for it
1301  * so that dependent plan nodes will get told to rescan.
1302  */
1303  foreach(l, subplan->setParam)
1304  {
1305  int paramid = lfirst_int(l);
1306  ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
1307 
1308  if (subplan->subLinkType != CTE_SUBLINK)
1309  prm->execPlan = node;
1310 
1311  parent->chgParam = bms_add_member(parent->chgParam, paramid);
1312  }
1313 }
#define NIL
Definition: pg_list.h:65
void * execPlan
Definition: params.h:148
SubLinkType subLinkType
Definition: primnodes.h:725
EState * state
Definition: execnodes.h:943
ParamExecData * es_param_exec_vals
Definition: execnodes.h:567
#define ERROR
Definition: elog.h:45
struct PlanState * planstate
Definition: execnodes.h:859
SubPlan * subplan
Definition: execnodes.h:858
#define lfirst_int(lc)
Definition: pg_list.h:170
Bitmapset * chgParam
Definition: execnodes.h:973
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
Plan * plan
Definition: execnodes.h:941
List * setParam
Definition: primnodes.h:748
Bitmapset * extParam
Definition: plannodes.h:154
List * parParam
Definition: primnodes.h:750
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
#define elog(elevel,...)
Definition: elog.h:228

◆ ExecSetParamPlan()

void ExecSetParamPlan ( SubPlanState node,
ExprContext econtext 
)

Definition at line 1058 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, EState::es_direction, ExecCopySlotHeapTuple(), ExecEvalExprSwitchContext(), ParamExecData::execPlan, ExecProcNode(), EXISTS_SUBLINK, EXPR_SUBLINK, SubPlan::firstColType, forboth, ForwardScanDirection, 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(), PlanState::state, SubPlan::subLinkType, SubPlanState::subplan, TupleTableSlot::tts_tupleDescriptor, TupIsNull, TupleDescAttr, and ParamExecData::value.

Referenced by ExecEvalParamExec(), and ExecSetParamPlanMulti().

1059 {
1060  SubPlan *subplan = node->subplan;
1061  PlanState *planstate = node->planstate;
1062  SubLinkType subLinkType = subplan->subLinkType;
1063  EState *estate = planstate->state;
1064  ScanDirection dir = estate->es_direction;
1065  MemoryContext oldcontext;
1066  TupleTableSlot *slot;
1067  ListCell *pvar;
1068  ListCell *l;
1069  bool found = false;
1070  ArrayBuildStateAny *astate = NULL;
1071 
1072  if (subLinkType == ANY_SUBLINK ||
1073  subLinkType == ALL_SUBLINK)
1074  elog(ERROR, "ANY/ALL subselect unsupported as initplan");
1075  if (subLinkType == CTE_SUBLINK)
1076  elog(ERROR, "CTE subplans should not be executed via ExecSetParamPlan");
1077 
1078  /*
1079  * Enforce forward scan direction regardless of caller. It's hard but not
1080  * impossible to get here in backward scan, so make it work anyway.
1081  */
1083 
1084  /* Initialize ArrayBuildStateAny in caller's context, if needed */
1085  if (subLinkType == ARRAY_SUBLINK)
1086  astate = initArrayResultAny(subplan->firstColType,
1087  CurrentMemoryContext, true);
1088 
1089  /*
1090  * Must switch to per-query memory context.
1091  */
1092  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1093 
1094  /*
1095  * Set Params of this plan from parent plan correlation values. (Any
1096  * calculation we have to do is done in the parent econtext, since the
1097  * Param values don't need to have per-query lifetime.) Currently, we
1098  * expect only MULTIEXPR_SUBLINK plans to have any correlation values.
1099  */
1100  Assert(subplan->parParam == NIL || subLinkType == MULTIEXPR_SUBLINK);
1101  Assert(list_length(subplan->parParam) == list_length(node->args));
1102 
1103  forboth(l, subplan->parParam, pvar, node->args)
1104  {
1105  int paramid = lfirst_int(l);
1106  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1107 
1109  econtext,
1110  &(prm->isnull));
1111  planstate->chgParam = bms_add_member(planstate->chgParam, paramid);
1112  }
1113 
1114  /*
1115  * Run the plan. (If it needs to be rescanned, the first ExecProcNode
1116  * call will take care of that.)
1117  */
1118  for (slot = ExecProcNode(planstate);
1119  !TupIsNull(slot);
1120  slot = ExecProcNode(planstate))
1121  {
1122  TupleDesc tdesc = slot->tts_tupleDescriptor;
1123  int i = 1;
1124 
1125  if (subLinkType == EXISTS_SUBLINK)
1126  {
1127  /* There can be only one setParam... */
1128  int paramid = linitial_int(subplan->setParam);
1129  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1130 
1131  prm->execPlan = NULL;
1132  prm->value = BoolGetDatum(true);
1133  prm->isnull = false;
1134  found = true;
1135  break;
1136  }
1137 
1138  if (subLinkType == ARRAY_SUBLINK)
1139  {
1140  Datum dvalue;
1141  bool disnull;
1142 
1143  found = true;
1144  /* stash away current value */
1145  Assert(subplan->firstColType == TupleDescAttr(tdesc, 0)->atttypid);
1146  dvalue = slot_getattr(slot, 1, &disnull);
1147  astate = accumArrayResultAny(astate, dvalue, disnull,
1148  subplan->firstColType, oldcontext);
1149  /* keep scanning subplan to collect all values */
1150  continue;
1151  }
1152 
1153  if (found &&
1154  (subLinkType == EXPR_SUBLINK ||
1155  subLinkType == MULTIEXPR_SUBLINK ||
1156  subLinkType == ROWCOMPARE_SUBLINK))
1157  ereport(ERROR,
1158  (errcode(ERRCODE_CARDINALITY_VIOLATION),
1159  errmsg("more than one row returned by a subquery used as an expression")));
1160 
1161  found = true;
1162 
1163  /*
1164  * We need to copy the subplan's tuple into our own context, in case
1165  * any of the params are pass-by-ref type --- the pointers stored in
1166  * the param structs will point at this copied tuple! node->curTuple
1167  * keeps track of the copied tuple for eventual freeing.
1168  */
1169  if (node->curTuple)
1170  heap_freetuple(node->curTuple);
1171  node->curTuple = ExecCopySlotHeapTuple(slot);
1172 
1173  /*
1174  * Now set all the setParam params from the columns of the tuple
1175  */
1176  foreach(l, subplan->setParam)
1177  {
1178  int paramid = lfirst_int(l);
1179  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1180 
1181  prm->execPlan = NULL;
1182  prm->value = heap_getattr(node->curTuple, i, tdesc,
1183  &(prm->isnull));
1184  i++;
1185  }
1186  }
1187 
1188  if (subLinkType == ARRAY_SUBLINK)
1189  {
1190  /* There can be only one setParam... */
1191  int paramid = linitial_int(subplan->setParam);
1192  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1193 
1194  /*
1195  * We build the result array in query context so it won't disappear;
1196  * to avoid leaking memory across repeated calls, we have to remember
1197  * the latest value, much as for curTuple above.
1198  */
1199  if (node->curArray != PointerGetDatum(NULL))
1200  pfree(DatumGetPointer(node->curArray));
1201  node->curArray = makeArrayResultAny(astate,
1202  econtext->ecxt_per_query_memory,
1203  true);
1204  prm->execPlan = NULL;
1205  prm->value = node->curArray;
1206  prm->isnull = false;
1207  }
1208  else if (!found)
1209  {
1210  if (subLinkType == EXISTS_SUBLINK)
1211  {
1212  /* There can be only one setParam... */
1213  int paramid = linitial_int(subplan->setParam);
1214  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1215 
1216  prm->execPlan = NULL;
1217  prm->value = BoolGetDatum(false);
1218  prm->isnull = false;
1219  }
1220  else
1221  {
1222  /* For other sublink types, set all the output params to NULL */
1223  foreach(l, subplan->setParam)
1224  {
1225  int paramid = lfirst_int(l);
1226  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1227 
1228  prm->execPlan = NULL;
1229  prm->value = (Datum) 0;
1230  prm->isnull = true;
1231  }
1232  }
1233  }
1234 
1235  MemoryContextSwitchTo(oldcontext);
1236 
1237  /* restore scan direction */
1238  estate->es_direction = dir;
1239 }
#define NIL
Definition: pg_list.h:65
Oid firstColType
Definition: primnodes.h:734
void * execPlan
Definition: params.h:148
HeapTuple curTuple
Definition: execnodes.h:863
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:307
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
ArrayBuildStateAny * initArrayResultAny(Oid input_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5507
#define PointerGetDatum(X)
Definition: postgres.h:556
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SubLinkType subLinkType
Definition: primnodes.h:725
int errcode(int sqlerrcode)
Definition: elog.c:704
EState * state
Definition: execnodes.h:943
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
ScanDirection es_direction
Definition: execnodes.h:529
#define linitial_int(l)
Definition: pg_list.h:175
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:45
struct PlanState * planstate
Definition: execnodes.h:859
List * args
Definition: execnodes.h:862
SubPlan * subplan
Definition: execnodes.h:858
#define lfirst_int(lc)
Definition: pg_list.h:170
bool isnull
Definition: params.h:150
ScanDirection
Definition: sdir.h:22
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:236
#define TupIsNull(slot)
Definition: tuptable.h:292
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
Bitmapset * chgParam
Definition: execnodes.h:973
SubLinkType
Definition: primnodes.h:659
ArrayBuildStateAny * accumArrayResultAny(ArrayBuildStateAny *astate, Datum dvalue, bool disnull, Oid input_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5552
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
Datum curArray
Definition: execnodes.h:864
uintptr_t Datum
Definition: postgres.h:367
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:242
#define BoolGetDatum(X)
Definition: postgres.h:402
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:381
#define ereport(elevel,...)
Definition: elog.h:155
static HeapTuple ExecCopySlotHeapTuple(TupleTableSlot *slot)
Definition: tuptable.h:452
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
List * setParam
Definition: primnodes.h:748
static int list_length(const List *l)
Definition: pg_list.h:149
List * parParam
Definition: primnodes.h:750
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:232
#define DatumGetPointer(X)
Definition: postgres.h:549
Datum makeArrayResultAny(ArrayBuildStateAny *astate, MemoryContext rcontext, bool release)
Definition: arrayfuncs.c:5580
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define elog(elevel,...)
Definition: elog.h:228
int i
Datum value
Definition: params.h:149

◆ ExecSetParamPlanMulti()

void ExecSetParamPlanMulti ( const Bitmapset params,
ExprContext econtext 
)

Definition at line 1253 of file nodeSubplan.c.

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

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

1254 {
1255  int paramid;
1256 
1257  paramid = -1;
1258  while ((paramid = bms_next_member(params, paramid)) >= 0)
1259  {
1260  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1261 
1262  if (prm->execPlan != NULL)
1263  {
1264  /* Parameter not evaluated yet, so go do it */
1265  ExecSetParamPlan(prm->execPlan, econtext);
1266  /* ExecSetParamPlan should have processed this param... */
1267  Assert(prm->execPlan == NULL);
1268  }
1269  }
1270 }
void * execPlan
Definition: params.h:148
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
void ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
Definition: nodeSubplan.c:1058
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:236
#define Assert(condition)
Definition: c.h:804

◆ ExecSubPlan()

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

Definition at line 62 of file nodeSubplan.c.

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

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 NIL
Definition: pg_list.h:65
SubLinkType subLinkType
Definition: primnodes.h:725
EState * state
Definition: execnodes.h:943
ScanDirection es_direction
Definition: execnodes.h:529
#define ERROR
Definition: elog.h:45
struct PlanState * planstate
Definition: execnodes.h:859
SubPlan * subplan
Definition: execnodes.h:858
ScanDirection
Definition: sdir.h:22
static Datum ExecHashSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:101
uintptr_t Datum
Definition: postgres.h:367
static Datum ExecScanSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:223
List * setParam
Definition: primnodes.h:748
#define elog(elevel,...)
Definition: elog.h:228
bool useHashTable
Definition: primnodes.h:739
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:100