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)
 
Size EstimateSubplanHashTableSpace (double nentries, Size tupleWidth, bool unknownEqFalse)
 
void ExecReScanSetParamPlan (SubPlanState *node, PlanState *parent)
 
void ExecSetParamPlan (SubPlanState *node, ExprContext *econtext)
 
void ExecSetParamPlanMulti (const Bitmapset *params, ExprContext *econtext)
 

Function Documentation

◆ EstimateSubplanHashTableSpace()

Size EstimateSubplanHashTableSpace ( double  nentries,
Size  tupleWidth,
bool  unknownEqFalse 
)

Definition at line 638 of file nodeSubplan.c.

641{
642 Size tab1space,
643 tab2space;
644
645 /* Estimate size of main hashtable */
646 tab1space = EstimateTupleHashTableSpace(nentries,
647 tupleWidth,
648 0 /* no additional data */ );
649
650 /* Give up if that's already too big */
651 if (tab1space >= SIZE_MAX)
652 return tab1space;
653
654 /* Done if we don't need a hashnulls table */
655 if (unknownEqFalse)
656 return tab1space;
657
658 /*
659 * Adjust the rowcount estimate in the same way that buildSubPlanHash
660 * will, except that we don't bother with the special case for a single
661 * hash column. (We skip that detail because it'd be notationally painful
662 * for our caller to provide the column count, and this table has
663 * relatively little impact on the total estimate anyway.)
664 */
665 nentries /= 16;
666 if (nentries < 1)
667 nentries = 1;
668
669 /*
670 * It might be sane to also reduce the tupleWidth, but on the other hand
671 * we are not accounting for the space taken by the tuples' null bitmaps.
672 * Leave it alone for now.
673 */
674 tab2space = EstimateTupleHashTableSpace(nentries,
675 tupleWidth,
676 0 /* no additional data */ );
677
678 /* Guard against overflow */
679 if (tab2space >= SIZE_MAX - tab1space)
680 return SIZE_MAX;
681
682 return tab1space + tab2space;
683}
size_t Size
Definition: c.h:615
Size EstimateTupleHashTableSpace(double nentries, Size tupleWidth, Size additionalsize)
Definition: execGrouping.c:321

References EstimateTupleHashTableSpace().

Referenced by subpath_is_hashable(), and subplan_is_hashable().

◆ ExecInitSubPlan()

SubPlanState * ExecInitSubPlan ( SubPlan subplan,
PlanState parent 
)

Definition at line 852 of file nodeSubplan.c.

853{
855 EState *estate = parent->state;
856
857 sstate->subplan = subplan;
858
859 /* Link the SubPlanState to already-initialized subplan */
860 sstate->planstate = (PlanState *) list_nth(estate->es_subplanstates,
861 subplan->plan_id - 1);
862
863 /*
864 * This check can fail if the planner mistakenly puts a parallel-unsafe
865 * subplan into a parallelized subquery; see ExecSerializePlan.
866 */
867 if (sstate->planstate == NULL)
868 elog(ERROR, "subplan \"%s\" was not initialized",
869 subplan->plan_name);
870
871 /* Link to parent's state, too */
872 sstate->parent = parent;
873
874 /* Initialize subexpressions */
875 sstate->testexpr = ExecInitExpr((Expr *) subplan->testexpr, parent);
876
877 /*
878 * initialize my state
879 */
880 sstate->curTuple = NULL;
881 sstate->curArray = PointerGetDatum(NULL);
882 sstate->projLeft = NULL;
883 sstate->projRight = NULL;
884 sstate->hashtable = NULL;
885 sstate->hashnulls = NULL;
886 sstate->tuplesContext = NULL;
887 sstate->innerecontext = NULL;
888 sstate->keyColIdx = NULL;
889 sstate->tab_eq_funcoids = NULL;
890 sstate->tab_hash_funcs = NULL;
891 sstate->tab_collations = NULL;
892 sstate->cur_eq_funcs = NULL;
893
894 /*
895 * If this is an initplan, it has output parameters that the parent plan
896 * will use, so mark those parameters as needing evaluation. We don't
897 * actually run the subplan until we first need one of its outputs.
898 *
899 * A CTE subplan's output parameter is never to be evaluated in the normal
900 * way, so skip this in that case.
901 *
902 * Note that we don't set parent->chgParam here: the parent plan hasn't
903 * been run yet, so no need to force it to re-run.
904 */
905 if (subplan->setParam != NIL && subplan->parParam == NIL &&
906 subplan->subLinkType != CTE_SUBLINK)
907 {
908 ListCell *lst;
909
910 foreach(lst, subplan->setParam)
911 {
912 int paramid = lfirst_int(lst);
913 ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
914
915 prm->execPlan = sstate;
916 }
917 }
918
919 /*
920 * If we are going to hash the subquery output, initialize relevant stuff.
921 * (We don't create the hashtable until needed, though.)
922 */
923 if (subplan->useHashTable)
924 {
925 int ncols,
926 i;
927 TupleDesc tupDescLeft;
928 TupleDesc tupDescRight;
929 Oid *cross_eq_funcoids;
930 TupleTableSlot *slot;
931 FmgrInfo *lhs_hash_funcs;
932 List *oplist,
933 *lefttlist,
934 *righttlist;
935 ListCell *l;
936
937 /* We need a memory context to hold the hash table(s)' tuples */
938 sstate->tuplesContext =
940 "SubPlan hashed tuples",
942 /* and a short-lived exprcontext for function evaluation */
943 sstate->innerecontext = CreateExprContext(estate);
944
945 /*
946 * We use ExecProject to evaluate the lefthand and righthand
947 * expression lists and form tuples. (You might think that we could
948 * use the sub-select's output tuples directly, but that is not the
949 * case if we had to insert any run-time coercions of the sub-select's
950 * output datatypes; anyway this avoids storing any resjunk columns
951 * that might be in the sub-select's output.) Run through the
952 * combining expressions to build tlists for the lefthand and
953 * righthand sides.
954 *
955 * We also extract the combining operators themselves to initialize
956 * the equality and hashing functions for the hash tables.
957 */
958 if (IsA(subplan->testexpr, OpExpr))
959 {
960 /* single combining operator */
961 oplist = list_make1(subplan->testexpr);
962 }
963 else if (is_andclause(subplan->testexpr))
964 {
965 /* multiple combining operators */
966 oplist = castNode(BoolExpr, subplan->testexpr)->args;
967 }
968 else
969 {
970 /* shouldn't see anything else in a hashable subplan */
971 elog(ERROR, "unrecognized testexpr type: %d",
972 (int) nodeTag(subplan->testexpr));
973 oplist = NIL; /* keep compiler quiet */
974 }
975 ncols = list_length(oplist);
976
977 lefttlist = righttlist = NIL;
978 sstate->numCols = ncols;
979 sstate->keyColIdx = (AttrNumber *) palloc(ncols * sizeof(AttrNumber));
980 sstate->tab_eq_funcoids = (Oid *) palloc(ncols * sizeof(Oid));
981 sstate->tab_collations = (Oid *) palloc(ncols * sizeof(Oid));
982 sstate->tab_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
983 lhs_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
984 sstate->cur_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
985 /* we'll need the cross-type equality fns below, but not in sstate */
986 cross_eq_funcoids = (Oid *) palloc(ncols * sizeof(Oid));
987
988 i = 1;
989 foreach(l, oplist)
990 {
991 OpExpr *opexpr = lfirst_node(OpExpr, l);
992 Expr *expr;
993 TargetEntry *tle;
994 Oid rhs_eq_oper;
995 Oid left_hashfn;
996 Oid right_hashfn;
997
998 Assert(list_length(opexpr->args) == 2);
999
1000 /* Process lefthand argument */
1001 expr = (Expr *) linitial(opexpr->args);
1002 tle = makeTargetEntry(expr,
1003 i,
1004 NULL,
1005 false);
1006 lefttlist = lappend(lefttlist, tle);
1007
1008 /* Process righthand argument */
1009 expr = (Expr *) lsecond(opexpr->args);
1010 tle = makeTargetEntry(expr,
1011 i,
1012 NULL,
1013 false);
1014 righttlist = lappend(righttlist, tle);
1015
1016 /* Lookup the equality function (potentially cross-type) */
1017 cross_eq_funcoids[i - 1] = opexpr->opfuncid;
1018 fmgr_info(opexpr->opfuncid, &sstate->cur_eq_funcs[i - 1]);
1019 fmgr_info_set_expr((Node *) opexpr, &sstate->cur_eq_funcs[i - 1]);
1020
1021 /* Look up the equality function for the RHS type */
1023 NULL, &rhs_eq_oper))
1024 elog(ERROR, "could not find compatible hash operator for operator %u",
1025 opexpr->opno);
1026 sstate->tab_eq_funcoids[i - 1] = get_opcode(rhs_eq_oper);
1027
1028 /* Lookup the associated hash functions */
1029 if (!get_op_hash_functions(opexpr->opno,
1030 &left_hashfn, &right_hashfn))
1031 elog(ERROR, "could not find hash function for hash operator %u",
1032 opexpr->opno);
1033 fmgr_info(left_hashfn, &lhs_hash_funcs[i - 1]);
1034 fmgr_info(right_hashfn, &sstate->tab_hash_funcs[i - 1]);
1035
1036 /* Set collation */
1037 sstate->tab_collations[i - 1] = opexpr->inputcollid;
1038
1039 /* keyColIdx is just column numbers 1..n */
1040 sstate->keyColIdx[i - 1] = i;
1041
1042 i++;
1043 }
1044
1045 /*
1046 * Construct tupdescs, slots and projection nodes for left and right
1047 * sides. The lefthand expressions will be evaluated in the parent
1048 * plan node's exprcontext, which we don't have access to here.
1049 * Fortunately we can just pass NULL for now and fill it in later
1050 * (hack alert!). The righthand expressions will be evaluated in our
1051 * own innerecontext.
1052 */
1053 tupDescLeft = ExecTypeFromTL(lefttlist);
1054 slot = ExecInitExtraTupleSlot(estate, tupDescLeft, &TTSOpsVirtual);
1055 sstate->projLeft = ExecBuildProjectionInfo(lefttlist,
1056 NULL,
1057 slot,
1058 parent,
1059 NULL);
1060
1061 sstate->descRight = tupDescRight = ExecTypeFromTL(righttlist);
1062 slot = ExecInitExtraTupleSlot(estate, tupDescRight, &TTSOpsVirtual);
1063 sstate->projRight = ExecBuildProjectionInfo(righttlist,
1064 sstate->innerecontext,
1065 slot,
1066 sstate->planstate,
1067 NULL);
1068
1069 /* Build the ExprState for generating hash values */
1070 sstate->lhs_hash_expr = ExecBuildHash32FromAttrs(tupDescLeft,
1072 lhs_hash_funcs,
1073 sstate->tab_collations,
1074 sstate->numCols,
1075 sstate->keyColIdx,
1076 parent,
1077 0);
1078
1079 /*
1080 * Create comparator for lookups of rows in the table (potentially
1081 * cross-type comparisons).
1082 */
1083 sstate->cur_eq_comp = ExecBuildGroupingEqual(tupDescLeft, tupDescRight,
1085 ncols,
1086 sstate->keyColIdx,
1087 cross_eq_funcoids,
1088 sstate->tab_collations,
1089 parent);
1090 }
1091
1092 return sstate;
1093}
int16 AttrNumber
Definition: attnum.h:21
MemoryContext BumpContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: bump.c:133
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
ExprState * ExecBuildHash32FromAttrs(TupleDesc desc, const TupleTableSlotOps *ops, FmgrInfo *hashfunctions, Oid *collations, int numCols, AttrNumber *keyColIdx, PlanState *parent, uint32 init_value)
Definition: execExpr.c:4141
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:143
ProjectionInfo * ExecBuildProjectionInfo(List *targetList, ExprContext *econtext, TupleTableSlot *slot, PlanState *parent, TupleDesc inputDesc)
Definition: execExpr.c:370
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:4465
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:84
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:2020
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:86
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:2127
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:307
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:128
#define fmgr_info_set_expr(expr, finfo)
Definition: fmgr.h:135
Assert(PointerIsAligned(start, uint64))
int i
Definition: isn.c:77
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:482
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1452
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:582
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:289
void * palloc(Size size)
Definition: mcxt.c:1365
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
static bool is_andclause(const void *clause)
Definition: nodeFuncs.h:107
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define nodeTag(nodeptr)
Definition: nodes.h:139
#define makeNode(_type_)
Definition: nodes.h:161
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
#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
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
unsigned int Oid
Definition: postgres_ext.h:32
@ CTE_SUBLINK
Definition: primnodes.h:1036
ParamExecData * es_param_exec_vals
Definition: execnodes.h:705
List * es_subplanstates
Definition: execnodes.h:725
Definition: fmgr.h:57
Definition: pg_list.h:54
Definition: nodes.h:135
Oid opno
Definition: primnodes.h:850
List * args
Definition: primnodes.h:868
void * execPlan
Definition: params.h:147
EState * state
Definition: execnodes.h:1167
TupleHashTable hashtable
Definition: execnodes.h:1022
ExprState * lhs_hash_expr
Definition: execnodes.h:1035
PlanState * parent
Definition: execnodes.h:1014
ExprState * cur_eq_comp
Definition: execnodes.h:1037
Oid * tab_eq_funcoids
Definition: execnodes.h:1031
ExprContext * innerecontext
Definition: execnodes.h:1027
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:1034
FmgrInfo * cur_eq_funcs
Definition: execnodes.h:1036
PlanState * planstate
Definition: execnodes.h:1013
HeapTuple curTuple
Definition: execnodes.h:1016
AttrNumber * keyColIdx
Definition: execnodes.h:1030
TupleDesc descRight
Definition: execnodes.h:1019
SubPlan * subplan
Definition: execnodes.h:1012
ProjectionInfo * projLeft
Definition: execnodes.h:1020
ProjectionInfo * projRight
Definition: execnodes.h:1021
ExprState * testexpr
Definition: execnodes.h:1015
MemoryContext tuplesContext
Definition: execnodes.h:1026
Oid * tab_collations
Definition: execnodes.h:1033
TupleHashTable hashnulls
Definition: execnodes.h:1023
Datum curArray
Definition: execnodes.h:1017
int plan_id
Definition: primnodes.h:1102
char * plan_name
Definition: primnodes.h:1104
bool useHashTable
Definition: primnodes.h:1112
Node * testexpr
Definition: primnodes.h:1099
List * parParam
Definition: primnodes.h:1123
List * setParam
Definition: primnodes.h:1121
SubLinkType subLinkType
Definition: primnodes.h:1097

References ALLOCSET_DEFAULT_SIZES, OpExpr::args, Assert(), BumpContextCreate(), 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(), ExecBuildHash32FromAttrs(), 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, i, SubPlanState::innerecontext, is_andclause(), IsA, SubPlanState::keyColIdx, lappend(), lfirst_int, lfirst_node, SubPlanState::lhs_hash_expr, 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_hash_funcs, SubPlanState::testexpr, SubPlan::testexpr, TTSOpsMinimalTuple, TTSOpsVirtual, SubPlanState::tuplesContext, and SubPlan::useHashTable.

Referenced by ExecInitNode(), and ExecInitSubPlanExpr().

◆ ExecReScanSetParamPlan()

void ExecReScanSetParamPlan ( SubPlanState node,
PlanState parent 
)

Definition at line 1319 of file nodeSubplan.c.

1320{
1321 PlanState *planstate = node->planstate;
1322 SubPlan *subplan = node->subplan;
1323 EState *estate = parent->state;
1324 ListCell *l;
1325
1326 /* sanity checks */
1327 if (subplan->parParam != NIL)
1328 elog(ERROR, "direct correlated subquery unsupported as initplan");
1329 if (subplan->setParam == NIL)
1330 elog(ERROR, "setParam list of initplan is empty");
1331 if (bms_is_empty(planstate->plan->extParam))
1332 elog(ERROR, "extParam set of initplan is empty");
1333
1334 /*
1335 * Don't actually re-scan: it'll happen inside ExecSetParamPlan if needed.
1336 */
1337
1338 /*
1339 * Mark this subplan's output parameters as needing recalculation.
1340 *
1341 * CTE subplans are never executed via parameter recalculation; instead
1342 * they get run when called by nodeCtescan.c. So don't mark the output
1343 * parameter of a CTE subplan as dirty, but do set the chgParam bit for it
1344 * so that dependent plan nodes will get told to rescan.
1345 */
1346 foreach(l, subplan->setParam)
1347 {
1348 int paramid = lfirst_int(l);
1349 ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
1350
1351 if (subplan->subLinkType != CTE_SUBLINK)
1352 prm->execPlan = node;
1353
1354 parent->chgParam = bms_add_member(parent->chgParam, paramid);
1355 }
1356}
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:1165
Bitmapset * chgParam
Definition: execnodes.h:1197
Bitmapset * extParam
Definition: plannodes.h:249

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

1121{
1122 SubPlan *subplan = node->subplan;
1123 PlanState *planstate = node->planstate;
1124 SubLinkType subLinkType = subplan->subLinkType;
1125 EState *estate = planstate->state;
1126 ScanDirection dir = estate->es_direction;
1127 MemoryContext oldcontext;
1128 TupleTableSlot *slot;
1129 ListCell *l;
1130 bool found = false;
1131 ArrayBuildStateAny *astate = NULL;
1132
1133 if (subLinkType == ANY_SUBLINK ||
1134 subLinkType == ALL_SUBLINK)
1135 elog(ERROR, "ANY/ALL subselect unsupported as initplan");
1136 if (subLinkType == CTE_SUBLINK)
1137 elog(ERROR, "CTE subplans should not be executed via ExecSetParamPlan");
1138 if (subplan->parParam || subplan->args)
1139 elog(ERROR, "correlated subplans should not be executed via ExecSetParamPlan");
1140
1141 /*
1142 * Enforce forward scan direction regardless of caller. It's hard but not
1143 * impossible to get here in backward scan, so make it work anyway.
1144 */
1146
1147 /* Initialize ArrayBuildStateAny in caller's context, if needed */
1148 if (subLinkType == ARRAY_SUBLINK)
1149 astate = initArrayResultAny(subplan->firstColType,
1150 CurrentMemoryContext, true);
1151
1152 /*
1153 * Must switch to per-query memory context.
1154 */
1155 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1156
1157 /*
1158 * Run the plan. (If it needs to be rescanned, the first ExecProcNode
1159 * call will take care of that.)
1160 */
1161 for (slot = ExecProcNode(planstate);
1162 !TupIsNull(slot);
1163 slot = ExecProcNode(planstate))
1164 {
1165 TupleDesc tdesc = slot->tts_tupleDescriptor;
1166 int i = 1;
1167
1168 if (subLinkType == EXISTS_SUBLINK)
1169 {
1170 /* There can be only one setParam... */
1171 int paramid = linitial_int(subplan->setParam);
1172 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1173
1174 prm->execPlan = NULL;
1175 prm->value = BoolGetDatum(true);
1176 prm->isnull = false;
1177 found = true;
1178 break;
1179 }
1180
1181 if (subLinkType == ARRAY_SUBLINK)
1182 {
1183 Datum dvalue;
1184 bool disnull;
1185
1186 found = true;
1187 /* stash away current value */
1188 Assert(subplan->firstColType == TupleDescAttr(tdesc, 0)->atttypid);
1189 dvalue = slot_getattr(slot, 1, &disnull);
1190 astate = accumArrayResultAny(astate, dvalue, disnull,
1191 subplan->firstColType, oldcontext);
1192 /* keep scanning subplan to collect all values */
1193 continue;
1194 }
1195
1196 if (found &&
1197 (subLinkType == EXPR_SUBLINK ||
1198 subLinkType == MULTIEXPR_SUBLINK ||
1199 subLinkType == ROWCOMPARE_SUBLINK))
1200 ereport(ERROR,
1201 (errcode(ERRCODE_CARDINALITY_VIOLATION),
1202 errmsg("more than one row returned by a subquery used as an expression")));
1203
1204 found = true;
1205
1206 /*
1207 * We need to copy the subplan's tuple into our own context, in case
1208 * any of the params are pass-by-ref type --- the pointers stored in
1209 * the param structs will point at this copied tuple! node->curTuple
1210 * keeps track of the copied tuple for eventual freeing.
1211 */
1212 if (node->curTuple)
1213 heap_freetuple(node->curTuple);
1214 node->curTuple = ExecCopySlotHeapTuple(slot);
1215
1216 /*
1217 * Now set all the setParam params from the columns of the tuple
1218 */
1219 foreach(l, subplan->setParam)
1220 {
1221 int paramid = lfirst_int(l);
1222 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1223
1224 prm->execPlan = NULL;
1225 prm->value = heap_getattr(node->curTuple, i, tdesc,
1226 &(prm->isnull));
1227 i++;
1228 }
1229 }
1230
1231 if (subLinkType == ARRAY_SUBLINK)
1232 {
1233 /* There can be only one setParam... */
1234 int paramid = linitial_int(subplan->setParam);
1235 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1236
1237 /*
1238 * We build the result array in query context so it won't disappear;
1239 * to avoid leaking memory across repeated calls, we have to remember
1240 * the latest value, much as for curTuple above.
1241 */
1242 if (node->curArray != PointerGetDatum(NULL))
1244 node->curArray = makeArrayResultAny(astate,
1245 econtext->ecxt_per_query_memory,
1246 true);
1247 prm->execPlan = NULL;
1248 prm->value = node->curArray;
1249 prm->isnull = false;
1250 }
1251 else if (!found)
1252 {
1253 if (subLinkType == EXISTS_SUBLINK)
1254 {
1255 /* There can be only one setParam... */
1256 int paramid = linitial_int(subplan->setParam);
1257 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1258
1259 prm->execPlan = NULL;
1260 prm->value = BoolGetDatum(false);
1261 prm->isnull = false;
1262 }
1263 else
1264 {
1265 /* For other sublink types, set all the output params to NULL */
1266 foreach(l, subplan->setParam)
1267 {
1268 int paramid = lfirst_int(l);
1269 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1270
1271 prm->execPlan = NULL;
1272 prm->value = (Datum) 0;
1273 prm->isnull = true;
1274 }
1275 }
1276 }
1277
1278 MemoryContextSwitchTo(oldcontext);
1279
1280 /* restore scan direction */
1281 estate->es_direction = dir;
1282}
ArrayBuildStateAny * initArrayResultAny(Oid input_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5783
ArrayBuildStateAny * accumArrayResultAny(ArrayBuildStateAny *astate, Datum dvalue, bool disnull, Oid input_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5830
Datum makeArrayResultAny(ArrayBuildStateAny *astate, MemoryContext rcontext, bool release)
Definition: arrayfuncs.c:5858
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ereport(elevel,...)
Definition: elog.h:150
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:314
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: htup_details.h:904
void pfree(void *pointer)
Definition: mcxt.c:1594
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
#define linitial_int(l)
Definition: pg_list.h:179
static Datum BoolGetDatum(bool X)
Definition: postgres.h:112
uint64_t Datum
Definition: postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322
SubLinkType
Definition: primnodes.h:1028
@ ARRAY_SUBLINK
Definition: primnodes.h:1035
@ ANY_SUBLINK
Definition: primnodes.h:1031
@ MULTIEXPR_SUBLINK
Definition: primnodes.h:1034
@ EXPR_SUBLINK
Definition: primnodes.h:1033
@ ROWCOMPARE_SUBLINK
Definition: primnodes.h:1032
@ ALL_SUBLINK
Definition: primnodes.h:1030
@ EXISTS_SUBLINK
Definition: primnodes.h:1029
ScanDirection
Definition: sdir.h:25
@ ForwardScanDirection
Definition: sdir.h:28
ScanDirection es_direction
Definition: execnodes.h:659
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:284
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:280
bool isnull
Definition: params.h:149
Datum value
Definition: params.h:148
List * args
Definition: primnodes.h:1124
Oid firstColType
Definition: primnodes.h:1106
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:122
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160
static HeapTuple ExecCopySlotHeapTuple(TupleTableSlot *slot)
Definition: tuptable.h:484
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:398
#define TupIsNull(slot)
Definition: tuptable.h:309

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

1297{
1298 int paramid;
1299
1300 paramid = -1;
1301 while ((paramid = bms_next_member(params, paramid)) >= 0)
1302 {
1303 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1304
1305 if (prm->execPlan != NULL)
1306 {
1307 /* Parameter not evaluated yet, so go do it */
1308 ExecSetParamPlan(prm->execPlan, econtext);
1309 /* ExecSetParamPlan should have processed this param... */
1310 Assert(prm->execPlan == NULL);
1311 }
1312 }
1313}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
void ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
Definition: nodeSubplan.c:1120

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

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

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