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

827{
829 EState *estate = parent->state;
830
831 sstate->subplan = subplan;
832
833 /* Link the SubPlanState to already-initialized subplan */
834 sstate->planstate = (PlanState *) list_nth(estate->es_subplanstates,
835 subplan->plan_id - 1);
836
837 /*
838 * This check can fail if the planner mistakenly puts a parallel-unsafe
839 * subplan into a parallelized subquery; see ExecSerializePlan.
840 */
841 if (sstate->planstate == NULL)
842 elog(ERROR, "subplan \"%s\" was not initialized",
843 subplan->plan_name);
844
845 /* Link to parent's state, too */
846 sstate->parent = parent;
847
848 /* Initialize subexpressions */
849 sstate->testexpr = ExecInitExpr((Expr *) subplan->testexpr, parent);
850
851 /*
852 * initialize my state
853 */
854 sstate->curTuple = NULL;
855 sstate->curArray = PointerGetDatum(NULL);
856 sstate->projLeft = NULL;
857 sstate->projRight = NULL;
858 sstate->hashtable = NULL;
859 sstate->hashnulls = NULL;
860 sstate->hashtablecxt = NULL;
861 sstate->hashtempcxt = NULL;
862 sstate->innerecontext = NULL;
863 sstate->keyColIdx = NULL;
864 sstate->tab_eq_funcoids = NULL;
865 sstate->tab_hash_funcs = NULL;
866 sstate->tab_collations = 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 FmgrInfo *lhs_hash_funcs;
907 List *oplist,
908 *lefttlist,
909 *righttlist;
910 ListCell *l;
911
912 /* We need a memory context to hold the hash table(s) */
913 sstate->hashtablecxt =
915 "Subplan HashTable Context",
917 /* and a small one for the hash tables to use as temp storage */
918 sstate->hashtempcxt =
920 "Subplan HashTable Temp Context",
922 /* and a short-lived exprcontext for function evaluation */
923 sstate->innerecontext = CreateExprContext(estate);
924
925 /*
926 * We use ExecProject to evaluate the lefthand and righthand
927 * expression lists and form tuples. (You might think that we could
928 * use the sub-select's output tuples directly, but that is not the
929 * case if we had to insert any run-time coercions of the sub-select's
930 * output datatypes; anyway this avoids storing any resjunk columns
931 * that might be in the sub-select's output.) Run through the
932 * combining expressions to build tlists for the lefthand and
933 * righthand sides.
934 *
935 * We also extract the combining operators themselves to initialize
936 * the equality and hashing functions for the hash tables.
937 */
938 if (IsA(subplan->testexpr, OpExpr))
939 {
940 /* single combining operator */
941 oplist = list_make1(subplan->testexpr);
942 }
943 else if (is_andclause(subplan->testexpr))
944 {
945 /* multiple combining operators */
946 oplist = castNode(BoolExpr, subplan->testexpr)->args;
947 }
948 else
949 {
950 /* shouldn't see anything else in a hashable subplan */
951 elog(ERROR, "unrecognized testexpr type: %d",
952 (int) nodeTag(subplan->testexpr));
953 oplist = NIL; /* keep compiler quiet */
954 }
955 ncols = list_length(oplist);
956
957 lefttlist = righttlist = NIL;
958 sstate->numCols = ncols;
959 sstate->keyColIdx = (AttrNumber *) palloc(ncols * sizeof(AttrNumber));
960 sstate->tab_eq_funcoids = (Oid *) palloc(ncols * sizeof(Oid));
961 sstate->tab_collations = (Oid *) palloc(ncols * sizeof(Oid));
962 sstate->tab_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
963 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 */
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
1008 /* Lookup the associated hash functions */
1009 if (!get_op_hash_functions(opexpr->opno,
1010 &left_hashfn, &right_hashfn))
1011 elog(ERROR, "could not find hash function for hash operator %u",
1012 opexpr->opno);
1013 fmgr_info(left_hashfn, &lhs_hash_funcs[i - 1]);
1014 fmgr_info(right_hashfn, &sstate->tab_hash_funcs[i - 1]);
1015
1016 /* Set collation */
1017 sstate->tab_collations[i - 1] = opexpr->inputcollid;
1018
1019 /* keyColIdx is just column numbers 1..n */
1020 sstate->keyColIdx[i - 1] = i;
1021
1022 i++;
1023 }
1024
1025 /*
1026 * Construct tupdescs, slots and projection nodes for left and right
1027 * sides. The lefthand expressions will be evaluated in the parent
1028 * plan node's exprcontext, which we don't have access to here.
1029 * Fortunately we can just pass NULL for now and fill it in later
1030 * (hack alert!). The righthand expressions will be evaluated in our
1031 * own innerecontext.
1032 */
1033 tupDescLeft = ExecTypeFromTL(lefttlist);
1034 slot = ExecInitExtraTupleSlot(estate, tupDescLeft, &TTSOpsVirtual);
1035 sstate->projLeft = ExecBuildProjectionInfo(lefttlist,
1036 NULL,
1037 slot,
1038 parent,
1039 NULL);
1040
1041 sstate->descRight = tupDescRight = ExecTypeFromTL(righttlist);
1042 slot = ExecInitExtraTupleSlot(estate, tupDescRight, &TTSOpsVirtual);
1043 sstate->projRight = ExecBuildProjectionInfo(righttlist,
1044 sstate->innerecontext,
1045 slot,
1046 sstate->planstate,
1047 NULL);
1048
1049 /* Build the ExprState for generating hash values */
1050 sstate->lhs_hash_expr = ExecBuildHash32FromAttrs(tupDescLeft,
1052 lhs_hash_funcs,
1053 sstate->tab_collations,
1054 sstate->numCols,
1055 sstate->keyColIdx,
1056 parent,
1057 0);
1058
1059 /*
1060 * Create comparator for lookups of rows in the table (potentially
1061 * cross-type comparisons).
1062 */
1063 sstate->cur_eq_comp = ExecBuildGroupingEqual(tupDescLeft, tupDescRight,
1065 ncols,
1066 sstate->keyColIdx,
1067 cross_eq_funcoids,
1068 sstate->tab_collations,
1069 parent);
1070 }
1071
1072 return sstate;
1073}
int16 AttrNumber
Definition: attnum.h:21
#define Assert(condition)
Definition: c.h:815
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
ExprState * ExecBuildHash32FromAttrs(TupleDesc desc, const TupleTableSlotOps *ops, FmgrInfo *hashfunctions, Oid *collations, int numCols, AttrNumber *keyColIdx, PlanState *parent, uint32 init_value)
Definition: execExpr.c:4122
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:4446
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:84
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:2018
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:86
TupleDesc ExecTypeFromTL(List *targetList)
Definition: execTuples.c:2125
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:306
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:72
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:242
void * palloc(Size size)
Definition: mcxt.c:1317
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
#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
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:327
unsigned int Oid
Definition: postgres_ext.h:32
@ CTE_SUBLINK
Definition: primnodes.h:1020
ParamExecData * es_param_exec_vals
Definition: execnodes.h:686
List * es_subplanstates
Definition: execnodes.h:706
Definition: fmgr.h:57
Definition: pg_list.h:54
Definition: nodes.h:129
Oid opno
Definition: primnodes.h:834
List * args
Definition: primnodes.h:852
void * execPlan
Definition: params.h:148
EState * state
Definition: execnodes.h:1143
TupleHashTable hashtable
Definition: execnodes.h:997
ExprState * lhs_hash_expr
Definition: execnodes.h:1011
ExprState * cur_eq_comp
Definition: execnodes.h:1013
MemoryContext hashtablecxt
Definition: execnodes.h:1001
Oid * tab_eq_funcoids
Definition: execnodes.h:1007
ExprContext * innerecontext
Definition: execnodes.h:1003
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:1010
FmgrInfo * cur_eq_funcs
Definition: execnodes.h:1012
MemoryContext hashtempcxt
Definition: execnodes.h:1002
HeapTuple curTuple
Definition: execnodes.h:991
AttrNumber * keyColIdx
Definition: execnodes.h:1006
struct PlanState * planstate
Definition: execnodes.h:988
TupleDesc descRight
Definition: execnodes.h:994
SubPlan * subplan
Definition: execnodes.h:987
ProjectionInfo * projLeft
Definition: execnodes.h:995
ProjectionInfo * projRight
Definition: execnodes.h:996
ExprState * testexpr
Definition: execnodes.h:990
struct PlanState * parent
Definition: execnodes.h:989
Oid * tab_collations
Definition: execnodes.h:1009
TupleHashTable hashnulls
Definition: execnodes.h:998
Datum curArray
Definition: execnodes.h:992
int plan_id
Definition: primnodes.h:1086
char * plan_name
Definition: primnodes.h:1088
bool useHashTable
Definition: primnodes.h:1095
Node * testexpr
Definition: primnodes.h:1083
List * parParam
Definition: primnodes.h:1106
List * setParam
Definition: primnodes.h:1104
SubLinkType subLinkType
Definition: primnodes.h:1081

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(), 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, SubPlanState::hashtablecxt, SubPlanState::hashtempcxt, 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, and SubPlan::useHashTable.

Referenced by ExecInitNode(), and ExecInitSubPlanExpr().

◆ ExecReScanSetParamPlan()

void ExecReScanSetParamPlan ( SubPlanState node,
PlanState parent 
)

Definition at line 1299 of file nodeSubplan.c.

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

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

1277{
1278 int paramid;
1279
1280 paramid = -1;
1281 while ((paramid = bms_next_member(params, paramid)) >= 0)
1282 {
1283 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1284
1285 if (prm->execPlan != NULL)
1286 {
1287 /* Parameter not evaluated yet, so go do it */
1288 ExecSetParamPlan(prm->execPlan, econtext);
1289 /* ExecSetParamPlan should have processed this param... */
1290 Assert(prm->execPlan == NULL);
1291 }
1292 }
1293}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
void ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
Definition: nodeSubplan.c:1100

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