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)
 
void ExecSetParamPlanMulti (const Bitmapset *params, ExprContext *econtext)
 

Function Documentation

◆ ExecAlternativeSubPlan()

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

Definition at line 1376 of file nodeSubplan.c.

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

Referenced by ExecEvalAlternativeSubPlan().

1379 {
1380  /* Just pass control to the active subplan */
1382  node->subplans, node->active);
1383 
1384  return ExecSubPlan(activesp, econtext, isNull);
1385 }
#define list_nth_node(type, list, n)
Definition: pg_list.h:305
Datum ExecSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:62

◆ ExecInitAlternativeSubPlan()

AlternativeSubPlanState* ExecInitAlternativeSubPlan ( AlternativeSubPlan asplan,
PlanState parent 
)

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

1315 {
1317  double num_calls;
1318  SubPlan *subplan1;
1319  SubPlan *subplan2;
1320  Cost cost1;
1321  Cost cost2;
1322  ListCell *lc;
1323 
1324  asstate->subplan = asplan;
1325 
1326  /*
1327  * Initialize subplans. (Can we get away with only initializing the one
1328  * we're going to use?)
1329  */
1330  foreach(lc, asplan->subplans)
1331  {
1332  SubPlan *sp = lfirst_node(SubPlan, lc);
1333  SubPlanState *sps = ExecInitSubPlan(sp, parent);
1334 
1335  asstate->subplans = lappend(asstate->subplans, sps);
1336  parent->subPlan = lappend(parent->subPlan, sps);
1337  }
1338 
1339  /*
1340  * Select the one to be used. For this, we need an estimate of the number
1341  * of executions of the subplan. We use the number of output rows
1342  * expected from the parent plan node. This is a good estimate if we are
1343  * in the parent's targetlist, and an underestimate (but probably not by
1344  * more than a factor of 2) if we are in the qual.
1345  */
1346  num_calls = parent->plan->plan_rows;
1347 
1348  /*
1349  * The planner saved enough info so that we don't have to work very hard
1350  * to estimate the total cost, given the number-of-calls estimate.
1351  */
1352  Assert(list_length(asplan->subplans) == 2);
1353  subplan1 = (SubPlan *) linitial(asplan->subplans);
1354  subplan2 = (SubPlan *) lsecond(asplan->subplans);
1355 
1356  cost1 = subplan1->startup_cost + num_calls * subplan1->per_call_cost;
1357  cost2 = subplan2->startup_cost + num_calls * subplan2->per_call_cost;
1358 
1359  if (cost1 < cost2)
1360  asstate->active = 0;
1361  else
1362  asstate->active = 1;
1363 
1364  return asstate;
1365 }
double plan_rows
Definition: plannodes.h:127
List * subPlan
Definition: execnodes.h:966
#define lsecond(l)
Definition: pg_list.h:200
SubPlanState * ExecInitSubPlan(SubPlan *subplan, PlanState *parent)
Definition: nodeSubplan.c:789
#define linitial(l)
Definition: pg_list.h:195
AlternativeSubPlan * subplan
Definition: execnodes.h:882
#define lfirst_node(type, lc)
Definition: pg_list.h:193
List * lappend(List *list, void *datum)
Definition: list.c:322
Plan * plan
Definition: execnodes.h:939
Cost per_call_cost
Definition: primnodes.h:719
#define makeNode(_type_)
Definition: nodes.h:573
#define Assert(condition)
Definition: c.h:739
static int list_length(const List *l)
Definition: pg_list.h:169
Cost startup_cost
Definition: primnodes.h:718
double Cost
Definition: nodes.h:659

◆ 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, 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_collations, SubPlanState::tab_eq_funcoids, SubPlanState::tab_eq_funcs, SubPlanState::tab_hash_funcs, SubPlan::testexpr, SubPlanState::testexpr, TTSOpsMinimalTuple, TTSOpsVirtual, and SubPlan::useHashTable.

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

◆ ExecReScanSetParamPlan()

void ExecReScanSetParamPlan ( SubPlanState node,
PlanState parent 
)

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

1269 {
1270  PlanState *planstate = node->planstate;
1271  SubPlan *subplan = node->subplan;
1272  EState *estate = parent->state;
1273  ListCell *l;
1274 
1275  /* sanity checks */
1276  if (subplan->parParam != NIL)
1277  elog(ERROR, "direct correlated subquery unsupported as initplan");
1278  if (subplan->setParam == NIL)
1279  elog(ERROR, "setParam list of initplan is empty");
1280  if (bms_is_empty(planstate->plan->extParam))
1281  elog(ERROR, "extParam set of initplan is empty");
1282 
1283  /*
1284  * Don't actually re-scan: it'll happen inside ExecSetParamPlan if needed.
1285  */
1286 
1287  /*
1288  * Mark this subplan's output parameters as needing recalculation.
1289  *
1290  * CTE subplans are never executed via parameter recalculation; instead
1291  * they get run when called by nodeCtescan.c. So don't mark the output
1292  * parameter of a CTE subplan as dirty, but do set the chgParam bit for it
1293  * so that dependent plan nodes will get told to rescan.
1294  */
1295  foreach(l, subplan->setParam)
1296  {
1297  int paramid = lfirst_int(l);
1298  ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
1299 
1300  if (subplan->subLinkType != CTE_SUBLINK)
1301  prm->execPlan = node;
1302 
1303  parent->chgParam = bms_add_member(parent->chgParam, paramid);
1304  }
1305 }
#define NIL
Definition: pg_list.h:65
void * execPlan
Definition: params.h:147
SubLinkType subLinkType
Definition: primnodes.h:690
EState * state
Definition: execnodes.h:941
ParamExecData * es_param_exec_vals
Definition: execnodes.h:544
#define ERROR
Definition: elog.h:43
struct PlanState * planstate
Definition: execnodes.h:847
SubPlan * subplan
Definition: execnodes.h:846
#define lfirst_int(lc)
Definition: pg_list.h:191
Bitmapset * chgParam
Definition: execnodes.h:971
bool bms_is_empty(const Bitmapset *a)
Definition: bitmapset.c:701
Plan * plan
Definition: execnodes.h:939
List * setParam
Definition: primnodes.h:713
Bitmapset * extParam
Definition: plannodes.h:158
List * parParam
Definition: primnodes.h:715
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 1050 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, 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().

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

◆ ExecSetParamPlanMulti()

void ExecSetParamPlanMulti ( const Bitmapset params,
ExprContext econtext 
)

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

1246 {
1247  int paramid;
1248 
1249  paramid = -1;
1250  while ((paramid = bms_next_member(params, paramid)) >= 0)
1251  {
1252  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1253 
1254  if (prm->execPlan != NULL)
1255  {
1256  /* Parameter not evaluated yet, so go do it */
1257  ExecSetParamPlan(prm->execPlan, econtext);
1258  /* ExecSetParamPlan should have processed this param... */
1259  Assert(prm->execPlan == NULL);
1260  }
1261  }
1262 }
void * execPlan
Definition: params.h:147
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1043
void ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
Definition: nodeSubplan.c:1050
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:234
#define Assert(condition)
Definition: c.h:739

◆ 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 ExecAlternativeSubPlan(), and 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:690
EState * state
Definition: execnodes.h:941
ScanDirection es_direction
Definition: execnodes.h:501
#define ERROR
Definition: elog.h:43
struct PlanState * planstate
Definition: execnodes.h:847
SubPlan * subplan
Definition: execnodes.h:846
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:713
#define elog(elevel,...)
Definition: elog.h:228
bool useHashTable
Definition: primnodes.h:704
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99