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)
 
AlternativeSubPlanStateExecInitAlternativeSubPlan (AlternativeSubPlan *asplan, PlanState *parent)
 
Datum ExecSubPlan (SubPlanState *node, ExprContext *econtext, bool *isNull)
 
Datum ExecAlternativeSubPlan (AlternativeSubPlanState *node, ExprContext *econtext, bool *isNull)
 
void ExecReScanSetParamPlan (SubPlanState *node, PlanState *parent)
 
void ExecSetParamPlan (SubPlanState *node, ExprContext *econtext)
 

Function Documentation

◆ ExecAlternativeSubPlan()

Datum ExecAlternativeSubPlan ( AlternativeSubPlanState node,
ExprContext econtext,
bool isNull 
)

Definition at line 1288 of file nodeSubplan.c.

References AlternativeSubPlanState::active, ExecSubPlan(), list_nth_node, and AlternativeSubPlanState::subplans.

Referenced by ExecEvalAlternativeSubPlan().

1291 {
1292  /* Just pass control to the active subplan */
1294  node->subplans, node->active);
1295 
1296  return ExecSubPlan(activesp, econtext, isNull);
1297 }
#define list_nth_node(type, list, n)
Definition: pg_list.h:227
Datum ExecSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:63

◆ ExecInitAlternativeSubPlan()

AlternativeSubPlanState* ExecInitAlternativeSubPlan ( AlternativeSubPlan asplan,
PlanState parent 
)

Definition at line 1226 of file nodeSubplan.c.

References AlternativeSubPlanState::active, Assert, ExecInitSubPlan(), lappend(), lfirst_node, linitial, list_length(), lsecond, makeNode, SubPlan::per_call_cost, PlanState::plan, Plan::plan_rows, SubPlan::startup_cost, AlternativeSubPlanState::subplan, PlanState::subPlan, AlternativeSubPlan::subplans, and AlternativeSubPlanState::subplans.

Referenced by ExecInitExprRec().

1227 {
1229  double num_calls;
1230  SubPlan *subplan1;
1231  SubPlan *subplan2;
1232  Cost cost1;
1233  Cost cost2;
1234  ListCell *lc;
1235 
1236  asstate->subplan = asplan;
1237 
1238  /*
1239  * Initialize subplans. (Can we get away with only initializing the one
1240  * we're going to use?)
1241  */
1242  foreach(lc, asplan->subplans)
1243  {
1244  SubPlan *sp = lfirst_node(SubPlan, lc);
1245  SubPlanState *sps = ExecInitSubPlan(sp, parent);
1246 
1247  asstate->subplans = lappend(asstate->subplans, sps);
1248  parent->subPlan = lappend(parent->subPlan, sps);
1249  }
1250 
1251  /*
1252  * Select the one to be used. For this, we need an estimate of the number
1253  * of executions of the subplan. We use the number of output rows
1254  * expected from the parent plan node. This is a good estimate if we are
1255  * in the parent's targetlist, and an underestimate (but probably not by
1256  * more than a factor of 2) if we are in the qual.
1257  */
1258  num_calls = parent->plan->plan_rows;
1259 
1260  /*
1261  * The planner saved enough info so that we don't have to work very hard
1262  * to estimate the total cost, given the number-of-calls estimate.
1263  */
1264  Assert(list_length(asplan->subplans) == 2);
1265  subplan1 = (SubPlan *) linitial(asplan->subplans);
1266  subplan2 = (SubPlan *) lsecond(asplan->subplans);
1267 
1268  cost1 = subplan1->startup_cost + num_calls * subplan1->per_call_cost;
1269  cost2 = subplan2->startup_cost + num_calls * subplan2->per_call_cost;
1270 
1271  if (cost1 < cost2)
1272  asstate->active = 0;
1273  else
1274  asstate->active = 1;
1275 
1276  return asstate;
1277 }
double plan_rows
Definition: plannodes.h:133
List * subPlan
Definition: execnodes.h:936
#define lsecond(l)
Definition: pg_list.h:116
SubPlanState * ExecInitSubPlan(SubPlan *subplan, PlanState *parent)
Definition: nodeSubplan.c:766
#define linitial(l)
Definition: pg_list.h:111
AlternativeSubPlan * subplan
Definition: execnodes.h:855
#define lfirst_node(type, lc)
Definition: pg_list.h:109
List * lappend(List *list, void *datum)
Definition: list.c:128
Plan * plan
Definition: execnodes.h:912
Cost per_call_cost
Definition: primnodes.h:714
#define makeNode(_type_)
Definition: nodes.h:565
#define Assert(condition)
Definition: c.h:699
static int list_length(const List *l)
Definition: pg_list.h:89
Cost startup_cost
Definition: primnodes.h:713
double Cost
Definition: nodes.h:648

◆ ExecInitSubPlan()

SubPlanState* ExecInitSubPlan ( SubPlan subplan,
PlanState parent 
)

Definition at line 766 of file nodeSubplan.c.

References ALLOCSET_DEFAULT_SIZES, ALLOCSET_SMALL_SIZES, AllocSetContextCreate, and_clause(), 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, IsA, SubPlanState::keyColIdx, lappend(), lfirst_int, lfirst_node, SubPlanState::lhs_hash_funcs, linitial, list_length(), list_make1, list_nth(), lsecond, makeNode, makeTargetEntry(), NIL, nodeTag, OpExpr::opfuncid, palloc(), SubPlan::paramIds, SubPlanState::parent, SubPlan::plan_id, SubPlanState::planstate, PointerGetDatum, SubPlanState::projLeft, SubPlanState::projRight, SubPlan::setParam, PlanState::state, SubPlan::subLinkType, SubPlanState::subplan, SubPlanState::tab_eq_funcoids, SubPlanState::tab_eq_funcs, SubPlanState::tab_hash_funcs, SubPlan::testexpr, SubPlanState::testexpr, and SubPlan::useHashTable.

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

767 {
769  EState *estate = parent->state;
770 
771  sstate->subplan = subplan;
772 
773  /* Link the SubPlanState to already-initialized subplan */
774  sstate->planstate = (PlanState *) list_nth(estate->es_subplanstates,
775  subplan->plan_id - 1);
776 
777  /* ... and to its parent's state */
778  sstate->parent = parent;
779 
780  /* Initialize subexpressions */
781  sstate->testexpr = ExecInitExpr((Expr *) subplan->testexpr, parent);
782  sstate->args = ExecInitExprList(subplan->args, parent);
783 
784  /*
785  * initialize my state
786  */
787  sstate->curTuple = NULL;
788  sstate->curArray = PointerGetDatum(NULL);
789  sstate->projLeft = NULL;
790  sstate->projRight = NULL;
791  sstate->hashtable = NULL;
792  sstate->hashnulls = NULL;
793  sstate->hashtablecxt = NULL;
794  sstate->hashtempcxt = NULL;
795  sstate->innerecontext = NULL;
796  sstate->keyColIdx = NULL;
797  sstate->tab_eq_funcoids = NULL;
798  sstate->tab_hash_funcs = NULL;
799  sstate->tab_eq_funcs = NULL;
800  sstate->lhs_hash_funcs = NULL;
801  sstate->cur_eq_funcs = NULL;
802 
803  /*
804  * If this is an initplan or MULTIEXPR subplan, it has output parameters
805  * that the parent plan will use, so mark those parameters as needing
806  * evaluation. We don't actually run the subplan until we first need one
807  * of its outputs.
808  *
809  * A CTE subplan's output parameter is never to be evaluated in the normal
810  * way, so skip this in that case.
811  *
812  * Note that we don't set parent->chgParam here: the parent plan hasn't
813  * been run yet, so no need to force it to re-run.
814  */
815  if (subplan->setParam != NIL && subplan->subLinkType != CTE_SUBLINK)
816  {
817  ListCell *lst;
818 
819  foreach(lst, subplan->setParam)
820  {
821  int paramid = lfirst_int(lst);
822  ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
823 
824  prm->execPlan = sstate;
825  }
826  }
827 
828  /*
829  * If we are going to hash the subquery output, initialize relevant stuff.
830  * (We don't create the hashtable until needed, though.)
831  */
832  if (subplan->useHashTable)
833  {
834  int ncols,
835  i;
836  TupleDesc tupDescLeft;
837  TupleDesc tupDescRight;
838  TupleTableSlot *slot;
839  List *oplist,
840  *lefttlist,
841  *righttlist;
842  ListCell *l;
843 
844  /* We need a memory context to hold the hash table(s) */
845  sstate->hashtablecxt =
847  "Subplan HashTable Context",
849  /* and a small one for the hash tables to use as temp storage */
850  sstate->hashtempcxt =
852  "Subplan HashTable Temp Context",
854  /* and a short-lived exprcontext for function evaluation */
855  sstate->innerecontext = CreateExprContext(estate);
856  /* Silly little array of column numbers 1..n */
857  ncols = list_length(subplan->paramIds);
858  sstate->keyColIdx = (AttrNumber *) palloc(ncols * sizeof(AttrNumber));
859  for (i = 0; i < ncols; i++)
860  sstate->keyColIdx[i] = i + 1;
861 
862  /*
863  * We use ExecProject to evaluate the lefthand and righthand
864  * expression lists and form tuples. (You might think that we could
865  * use the sub-select's output tuples directly, but that is not the
866  * case if we had to insert any run-time coercions of the sub-select's
867  * output datatypes; anyway this avoids storing any resjunk columns
868  * that might be in the sub-select's output.) Run through the
869  * combining expressions to build tlists for the lefthand and
870  * righthand sides.
871  *
872  * We also extract the combining operators themselves to initialize
873  * the equality and hashing functions for the hash tables.
874  */
875  if (IsA(subplan->testexpr, OpExpr))
876  {
877  /* single combining operator */
878  oplist = list_make1(subplan->testexpr);
879  }
880  else if (and_clause((Node *) subplan->testexpr))
881  {
882  /* multiple combining operators */
883  oplist = castNode(BoolExpr, subplan->testexpr)->args;
884  }
885  else
886  {
887  /* shouldn't see anything else in a hashable subplan */
888  elog(ERROR, "unrecognized testexpr type: %d",
889  (int) nodeTag(subplan->testexpr));
890  oplist = NIL; /* keep compiler quiet */
891  }
892  Assert(list_length(oplist) == ncols);
893 
894  lefttlist = righttlist = NIL;
895  sstate->tab_eq_funcoids = (Oid *) palloc(ncols * sizeof(Oid));
896  sstate->tab_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
897  sstate->tab_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
898  sstate->lhs_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
899  sstate->cur_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
900  i = 1;
901  foreach(l, oplist)
902  {
903  OpExpr *opexpr = lfirst_node(OpExpr, l);
904  Expr *expr;
905  TargetEntry *tle;
906  Oid rhs_eq_oper;
907  Oid left_hashfn;
908  Oid right_hashfn;
909 
910  Assert(list_length(opexpr->args) == 2);
911 
912  /* Process lefthand argument */
913  expr = (Expr *) linitial(opexpr->args);
914  tle = makeTargetEntry(expr,
915  i,
916  NULL,
917  false);
918  lefttlist = lappend(lefttlist, tle);
919 
920  /* Process righthand argument */
921  expr = (Expr *) lsecond(opexpr->args);
922  tle = makeTargetEntry(expr,
923  i,
924  NULL,
925  false);
926  righttlist = lappend(righttlist, tle);
927 
928  /* Lookup the equality function (potentially cross-type) */
929  sstate->tab_eq_funcoids[i - 1] = opexpr->opfuncid;
930  fmgr_info(opexpr->opfuncid, &sstate->cur_eq_funcs[i - 1]);
931  fmgr_info_set_expr((Node *) opexpr, &sstate->cur_eq_funcs[i - 1]);
932 
933  /* Look up the equality function for the RHS type */
934  if (!get_compatible_hash_operators(opexpr->opno,
935  NULL, &rhs_eq_oper))
936  elog(ERROR, "could not find compatible hash operator for operator %u",
937  opexpr->opno);
938  fmgr_info(get_opcode(rhs_eq_oper), &sstate->tab_eq_funcs[i - 1]);
939 
940  /* Lookup the associated hash functions */
941  if (!get_op_hash_functions(opexpr->opno,
942  &left_hashfn, &right_hashfn))
943  elog(ERROR, "could not find hash function for hash operator %u",
944  opexpr->opno);
945  fmgr_info(left_hashfn, &sstate->lhs_hash_funcs[i - 1]);
946  fmgr_info(right_hashfn, &sstate->tab_hash_funcs[i - 1]);
947 
948  i++;
949  }
950 
951  /*
952  * Construct tupdescs, slots and projection nodes for left and right
953  * sides. The lefthand expressions will be evaluated in the parent
954  * plan node's exprcontext, which we don't have access to here.
955  * Fortunately we can just pass NULL for now and fill it in later
956  * (hack alert!). The righthand expressions will be evaluated in our
957  * own innerecontext.
958  */
959  tupDescLeft = ExecTypeFromTL(lefttlist, false);
960  slot = ExecInitExtraTupleSlot(estate, tupDescLeft);
961  sstate->projLeft = ExecBuildProjectionInfo(lefttlist,
962  NULL,
963  slot,
964  parent,
965  NULL);
966 
967  sstate->descRight = tupDescRight = ExecTypeFromTL(righttlist, false);
968  slot = ExecInitExtraTupleSlot(estate, tupDescRight);
969  sstate->projRight = ExecBuildProjectionInfo(righttlist,
970  sstate->innerecontext,
971  slot,
972  sstate->planstate,
973  NULL);
974 
975  /*
976  * Create comparator for lookups of rows in the table (potentially
977  * across-type comparison).
978  */
979  sstate->cur_eq_comp = ExecBuildGroupingEqual(tupDescLeft, tupDescRight,
980  ncols,
981  sstate->keyColIdx,
982  sstate->tab_eq_funcoids,
983  parent);
984 
985  }
986 
987  return sstate;
988 }
#define NIL
Definition: pg_list.h:69
Definition: fmgr.h:56
bool get_compatible_hash_operators(Oid opno, Oid *lhs_opno, Oid *rhs_opno)
Definition: lsyscache.c:407
void * execPlan
Definition: params.h:147
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
ProjectionInfo * projLeft
Definition: execnodes.h:829
HeapTuple curTuple
Definition: execnodes.h:825
bool get_op_hash_functions(Oid opno, RegProcedure *lhs_procno, RegProcedure *rhs_procno)
Definition: lsyscache.c:507
ExprState * ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc, int numCols, AttrNumber *keyColIdx, Oid *eqfunctions, PlanState *parent)
Definition: execExpr.c:3216
TupleHashTable hashtable
Definition: execnodes.h:831
int plan_id
Definition: primnodes.h:690
ExprContext * innerecontext
Definition: execnodes.h:837
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
#define PointerGetDatum(X)
Definition: postgres.h:541
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:202
SubLinkType subLinkType
Definition: primnodes.h:685
Definition: nodes.h:517
Oid * tab_eq_funcoids
Definition: execnodes.h:839
List * paramIds
Definition: primnodes.h:688
ExprState * testexpr
Definition: execnodes.h:823
EState * state
Definition: execnodes.h:914
unsigned int Oid
Definition: postgres_ext.h:31
#define lsecond(l)
Definition: pg_list.h:116
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc)
Definition: execTuples.c:931
FmgrInfo * tab_hash_funcs
Definition: execnodes.h:841
#define list_make1(x1)
Definition: pg_list.h:139
ParamExecData * es_param_exec_vals
Definition: execnodes.h:518
ExprState * cur_eq_comp
Definition: execnodes.h:845
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
struct PlanState * planstate
Definition: execnodes.h:821
List * args
Definition: execnodes.h:824
SubPlan * subplan
Definition: execnodes.h:820
#define lfirst_int(lc)
Definition: pg_list.h:107
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
AttrNumber * keyColIdx
Definition: execnodes.h:838
#define lfirst_node(type, lc)
Definition: pg_list.h:109
bool and_clause(Node *clause)
Definition: clauses.c:315
void * list_nth(const List *list, int n)
Definition: list.c:410
#define fmgr_info_set_expr(expr, finfo)
Definition: fmgr.h:107
List * ExecInitExprList(List *nodes, PlanState *parent)
Definition: execExpr.c:314
struct PlanState * parent
Definition: execnodes.h:822
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:237
TupleDesc ExecTypeFromTL(List *targetList, bool hasoid)
Definition: execTuples.c:965
List * lappend(List *list, void *datum)
Definition: list.c:128
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
Datum curArray
Definition: execnodes.h:826
Oid opfuncid
Definition: primnodes.h:498
Node * testexpr
Definition: primnodes.h:687
MemoryContext hashtempcxt
Definition: execnodes.h:836
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1079
MemoryContext hashtablecxt
Definition: execnodes.h:835
#define makeNode(_type_)
Definition: nodes.h:565
#define Assert(condition)
Definition: c.h:699
FmgrInfo * tab_eq_funcs
Definition: execnodes.h:842
List * setParam
Definition: primnodes.h:708
static int list_length(const List *l)
Definition: pg_list.h:89
List * es_subplanstates
Definition: execnodes.h:538
FmgrInfo * lhs_hash_funcs
Definition: execnodes.h:843
#define nodeTag(nodeptr)
Definition: nodes.h:522
FmgrInfo * cur_eq_funcs
Definition: execnodes.h:844
void * palloc(Size size)
Definition: mcxt.c:924
ProjectionInfo * ExecBuildProjectionInfo(List *targetList, ExprContext *econtext, TupleTableSlot *slot, PlanState *parent, TupleDesc inputDesc)
Definition: execExpr.c:349
TupleDesc descRight
Definition: execnodes.h:828
int i
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:228
bool useHashTable
Definition: primnodes.h:699
ProjectionInfo * projRight
Definition: execnodes.h:830
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:119
Oid opno
Definition: primnodes.h:497
#define elog
Definition: elog.h:219
List * args
Definition: primnodes.h:503
Definition: pg_list.h:45
int16 AttrNumber
Definition: attnum.h:21
TupleHashTable hashnulls
Definition: execnodes.h:832
List * args
Definition: primnodes.h:711

◆ ExecReScanSetParamPlan()

void ExecReScanSetParamPlan ( SubPlanState node,
PlanState parent 
)

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

1181 {
1182  PlanState *planstate = node->planstate;
1183  SubPlan *subplan = node->subplan;
1184  EState *estate = parent->state;
1185  ListCell *l;
1186 
1187  /* sanity checks */
1188  if (subplan->parParam != NIL)
1189  elog(ERROR, "direct correlated subquery unsupported as initplan");
1190  if (subplan->setParam == NIL)
1191  elog(ERROR, "setParam list of initplan is empty");
1192  if (bms_is_empty(planstate->plan->extParam))
1193  elog(ERROR, "extParam set of initplan is empty");
1194 
1195  /*
1196  * Don't actually re-scan: it'll happen inside ExecSetParamPlan if needed.
1197  */
1198 
1199  /*
1200  * Mark this subplan's output parameters as needing recalculation.
1201  *
1202  * CTE subplans are never executed via parameter recalculation; instead
1203  * they get run when called by nodeCtescan.c. So don't mark the output
1204  * parameter of a CTE subplan as dirty, but do set the chgParam bit for it
1205  * so that dependent plan nodes will get told to rescan.
1206  */
1207  foreach(l, subplan->setParam)
1208  {
1209  int paramid = lfirst_int(l);
1210  ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
1211 
1212  if (subplan->subLinkType != CTE_SUBLINK)
1213  prm->execPlan = node;
1214 
1215  parent->chgParam = bms_add_member(parent->chgParam, paramid);
1216  }
1217 }
#define NIL
Definition: pg_list.h:69
void * execPlan
Definition: params.h:147
SubLinkType subLinkType
Definition: primnodes.h:685
EState * state
Definition: execnodes.h:914
ParamExecData * es_param_exec_vals
Definition: execnodes.h:518
#define ERROR
Definition: elog.h:43
struct PlanState * planstate
Definition: execnodes.h:821
SubPlan * subplan
Definition: execnodes.h:820
#define lfirst_int(lc)
Definition: pg_list.h:107
Bitmapset * chgParam
Definition: execnodes.h:941
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:729
Plan * plan
Definition: execnodes.h:912
List * setParam
Definition: primnodes.h:708
Bitmapset * extParam
Definition: plannodes.h:164
List * parParam
Definition: primnodes.h:710
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:764
#define elog
Definition: elog.h:219

◆ ExecSetParamPlan()

void ExecSetParamPlan ( SubPlanState node,
ExprContext econtext 
)

Definition at line 1004 of file nodeSubplan.c.

References accumArrayResultAny(), ALL_SUBLINK, ANY_SUBLINK, SubPlanState::args, ARRAY_SUBLINK, Assert, atttypid, 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, ExecCopySlotTuple(), ExecEvalExprSwitchContext(), ParamExecData::execPlan, ExecProcNode(), EXISTS_SUBLINK, EXPR_SUBLINK, SubPlan::firstColType, forboth, 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(), SubPlan::subLinkType, SubPlanState::subplan, TupleTableSlot::tts_tupleDescriptor, TupIsNull, TupleDescAttr, and ParamExecData::value.

Referenced by ExecEvalParamExec(), and ExecEvalParamExecParams().

1005 {
1006  SubPlan *subplan = node->subplan;
1007  PlanState *planstate = node->planstate;
1008  SubLinkType subLinkType = subplan->subLinkType;
1009  MemoryContext oldcontext;
1010  TupleTableSlot *slot;
1011  ListCell *pvar;
1012  ListCell *l;
1013  bool found = false;
1014  ArrayBuildStateAny *astate = NULL;
1015 
1016  if (subLinkType == ANY_SUBLINK ||
1017  subLinkType == ALL_SUBLINK)
1018  elog(ERROR, "ANY/ALL subselect unsupported as initplan");
1019  if (subLinkType == CTE_SUBLINK)
1020  elog(ERROR, "CTE subplans should not be executed via ExecSetParamPlan");
1021 
1022  /* Initialize ArrayBuildStateAny in caller's context, if needed */
1023  if (subLinkType == ARRAY_SUBLINK)
1024  astate = initArrayResultAny(subplan->firstColType,
1025  CurrentMemoryContext, true);
1026 
1027  /*
1028  * Must switch to per-query memory context.
1029  */
1030  oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1031 
1032  /*
1033  * Set Params of this plan from parent plan correlation values. (Any
1034  * calculation we have to do is done in the parent econtext, since the
1035  * Param values don't need to have per-query lifetime.) Currently, we
1036  * expect only MULTIEXPR_SUBLINK plans to have any correlation values.
1037  */
1038  Assert(subplan->parParam == NIL || subLinkType == MULTIEXPR_SUBLINK);
1039  Assert(list_length(subplan->parParam) == list_length(node->args));
1040 
1041  forboth(l, subplan->parParam, pvar, node->args)
1042  {
1043  int paramid = lfirst_int(l);
1044  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1045 
1047  econtext,
1048  &(prm->isnull));
1049  planstate->chgParam = bms_add_member(planstate->chgParam, paramid);
1050  }
1051 
1052  /*
1053  * Run the plan. (If it needs to be rescanned, the first ExecProcNode
1054  * call will take care of that.)
1055  */
1056  for (slot = ExecProcNode(planstate);
1057  !TupIsNull(slot);
1058  slot = ExecProcNode(planstate))
1059  {
1060  TupleDesc tdesc = slot->tts_tupleDescriptor;
1061  int i = 1;
1062 
1063  if (subLinkType == EXISTS_SUBLINK)
1064  {
1065  /* There can be only one setParam... */
1066  int paramid = linitial_int(subplan->setParam);
1067  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1068 
1069  prm->execPlan = NULL;
1070  prm->value = BoolGetDatum(true);
1071  prm->isnull = false;
1072  found = true;
1073  break;
1074  }
1075 
1076  if (subLinkType == ARRAY_SUBLINK)
1077  {
1078  Datum dvalue;
1079  bool disnull;
1080 
1081  found = true;
1082  /* stash away current value */
1083  Assert(subplan->firstColType == TupleDescAttr(tdesc, 0)->atttypid);
1084  dvalue = slot_getattr(slot, 1, &disnull);
1085  astate = accumArrayResultAny(astate, dvalue, disnull,
1086  subplan->firstColType, oldcontext);
1087  /* keep scanning subplan to collect all values */
1088  continue;
1089  }
1090 
1091  if (found &&
1092  (subLinkType == EXPR_SUBLINK ||
1093  subLinkType == MULTIEXPR_SUBLINK ||
1094  subLinkType == ROWCOMPARE_SUBLINK))
1095  ereport(ERROR,
1096  (errcode(ERRCODE_CARDINALITY_VIOLATION),
1097  errmsg("more than one row returned by a subquery used as an expression")));
1098 
1099  found = true;
1100 
1101  /*
1102  * We need to copy the subplan's tuple into our own context, in case
1103  * any of the params are pass-by-ref type --- the pointers stored in
1104  * the param structs will point at this copied tuple! node->curTuple
1105  * keeps track of the copied tuple for eventual freeing.
1106  */
1107  if (node->curTuple)
1108  heap_freetuple(node->curTuple);
1109  node->curTuple = ExecCopySlotTuple(slot);
1110 
1111  /*
1112  * Now set all the setParam params from the columns of the tuple
1113  */
1114  foreach(l, subplan->setParam)
1115  {
1116  int paramid = lfirst_int(l);
1117  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1118 
1119  prm->execPlan = NULL;
1120  prm->value = heap_getattr(node->curTuple, i, tdesc,
1121  &(prm->isnull));
1122  i++;
1123  }
1124  }
1125 
1126  if (subLinkType == ARRAY_SUBLINK)
1127  {
1128  /* There can be only one setParam... */
1129  int paramid = linitial_int(subplan->setParam);
1130  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1131 
1132  /*
1133  * We build the result array in query context so it won't disappear;
1134  * to avoid leaking memory across repeated calls, we have to remember
1135  * the latest value, much as for curTuple above.
1136  */
1137  if (node->curArray != PointerGetDatum(NULL))
1138  pfree(DatumGetPointer(node->curArray));
1139  node->curArray = makeArrayResultAny(astate,
1140  econtext->ecxt_per_query_memory,
1141  true);
1142  prm->execPlan = NULL;
1143  prm->value = node->curArray;
1144  prm->isnull = false;
1145  }
1146  else if (!found)
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(false);
1156  prm->isnull = false;
1157  }
1158  else
1159  {
1160  /* For other sublink types, set all the output params to NULL */
1161  foreach(l, subplan->setParam)
1162  {
1163  int paramid = lfirst_int(l);
1164  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1165 
1166  prm->execPlan = NULL;
1167  prm->value = (Datum) 0;
1168  prm->isnull = true;
1169  }
1170  }
1171  }
1172 
1173  MemoryContextSwitchTo(oldcontext);
1174 }
#define NIL
Definition: pg_list.h:69
Oid firstColType
Definition: primnodes.h:694
void * execPlan
Definition: params.h:147
HeapTuple curTuple
Definition: execnodes.h:825
static Datum ExecEvalExprSwitchContext(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:296
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:180
ArrayBuildStateAny * initArrayResultAny(Oid input_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5468
#define PointerGetDatum(X)
Definition: postgres.h:541
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:93
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SubLinkType subLinkType
Definition: primnodes.h:685
int errcode(int sqlerrcode)
Definition: elog.c:575
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1773
#define linitial_int(l)
Definition: pg_list.h:112
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
struct PlanState * planstate
Definition: execnodes.h:821
List * args
Definition: execnodes.h:824
SubPlan * subplan
Definition: execnodes.h:820
#define lfirst_int(lc)
Definition: pg_list.h:107
bool isnull
Definition: params.h:149
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:229
#define TupIsNull(slot)
Definition: tuptable.h:146
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
Oid atttypid
Definition: pg_attribute.h:49
#define ereport(elevel, rest)
Definition: elog.h:122
Bitmapset * chgParam
Definition: execnodes.h:941
SubLinkType
Definition: primnodes.h:619
ArrayBuildStateAny * accumArrayResultAny(ArrayBuildStateAny *astate, Datum dvalue, bool disnull, Oid input_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5513
HeapTuple ExecCopySlotTuple(TupleTableSlot *slot)
Definition: execTuples.c:581
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:781
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:124
Datum curArray
Definition: execnodes.h:826
uintptr_t Datum
Definition: postgres.h:367
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:233
#define BoolGetDatum(X)
Definition: postgres.h:387
#define Assert(condition)
Definition: c.h:699
#define lfirst(lc)
Definition: pg_list.h:106
List * setParam
Definition: primnodes.h:708
static int list_length(const List *l)
Definition: pg_list.h:89
List * parParam
Definition: primnodes.h:710
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:764
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:225
#define DatumGetPointer(X)
Definition: postgres.h:534
Datum makeArrayResultAny(ArrayBuildStateAny *astate, MemoryContext rcontext, bool release)
Definition: arrayfuncs.c:5541
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: heaptuple.c:1518
Datum value
Definition: params.h:148
#define elog
Definition: elog.h:219

◆ ExecSubPlan()

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

Definition at line 63 of file nodeSubplan.c.

References CHECK_FOR_INTERRUPTS, CTE_SUBLINK, elog, ERROR, ExecHashSubPlan(), ExecScanSubPlan(), MULTIEXPR_SUBLINK, NIL, SubPlan::setParam, SubPlan::subLinkType, SubPlanState::subplan, and SubPlan::useHashTable.

Referenced by ExecAlternativeSubPlan(), and ExecEvalSubPlan().

66 {
67  SubPlan *subplan = node->subplan;
68 
70 
71  /* Set non-null as default */
72  *isNull = false;
73 
74  /* Sanity checks */
75  if (subplan->subLinkType == CTE_SUBLINK)
76  elog(ERROR, "CTE subplans should not be executed via ExecSubPlan");
77  if (subplan->setParam != NIL && subplan->subLinkType != MULTIEXPR_SUBLINK)
78  elog(ERROR, "cannot set parent params from subquery");
79 
80  /* Select appropriate evaluation strategy */
81  if (subplan->useHashTable)
82  return ExecHashSubPlan(node, econtext, isNull);
83  else
84  return ExecScanSubPlan(node, econtext, isNull);
85 }
#define NIL
Definition: pg_list.h:69
SubLinkType subLinkType
Definition: primnodes.h:685
#define ERROR
Definition: elog.h:43
SubPlan * subplan
Definition: execnodes.h:820
static Datum ExecHashSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:91
static Datum ExecScanSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:213
List * setParam
Definition: primnodes.h:708
bool useHashTable
Definition: primnodes.h:699
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define elog
Definition: elog.h:219