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

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

Referenced by ExecEvalAlternativeSubPlan().

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

◆ ExecInitAlternativeSubPlan()

AlternativeSubPlanState* ExecInitAlternativeSubPlan ( AlternativeSubPlan asplan,
PlanState parent 
)

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

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

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

◆ ExecReScanSetParamPlan()

void ExecReScanSetParamPlan ( SubPlanState node,
PlanState parent 
)

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

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

◆ ExecSetParamPlan()

void ExecSetParamPlan ( SubPlanState node,
ExprContext econtext 
)

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

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

◆ ExecSetParamPlanMulti()

void ExecSetParamPlanMulti ( const Bitmapset params,
ExprContext econtext 
)

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

1247 {
1248  int paramid;
1249 
1250  paramid = -1;
1251  while ((paramid = bms_next_member(params, paramid)) >= 0)
1252  {
1253  ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1254 
1255  if (prm->execPlan != NULL)
1256  {
1257  /* Parameter not evaluated yet, so go do it */
1258  ExecSetParamPlan(prm->execPlan, econtext);
1259  /* ExecSetParamPlan should have processed this param... */
1260  Assert(prm->execPlan == NULL);
1261  }
1262  }
1263 }
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:1051
ParamExecData * ecxt_param_exec_vals
Definition: execnodes.h:235
#define Assert(condition)
Definition: c.h:732

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

66 {
67  SubPlan *subplan = node->subplan;
68  EState *estate = node->planstate->state;
69  ScanDirection dir = estate->es_direction;
70  Datum retval;
71 
73 
74  /* Set non-null as default */
75  *isNull = false;
76 
77  /* Sanity checks */
78  if (subplan->subLinkType == CTE_SUBLINK)
79  elog(ERROR, "CTE subplans should not be executed via ExecSubPlan");
80  if (subplan->setParam != NIL && subplan->subLinkType != MULTIEXPR_SUBLINK)
81  elog(ERROR, "cannot set parent params from subquery");
82 
83  /* Force forward-scan mode for evaluation */
85 
86  /* Select appropriate evaluation strategy */
87  if (subplan->useHashTable)
88  retval = ExecHashSubPlan(node, econtext, isNull);
89  else
90  retval = ExecScanSubPlan(node, econtext, isNull);
91 
92  /* restore scan direction */
93  estate->es_direction = dir;
94 
95  return retval;
96 }
#define NIL
Definition: pg_list.h:65
SubLinkType subLinkType
Definition: primnodes.h:690
EState * state
Definition: execnodes.h:942
ScanDirection es_direction
Definition: execnodes.h:502
#define ERROR
Definition: elog.h:43
struct PlanState * planstate
Definition: execnodes.h:848
SubPlan * subplan
Definition: execnodes.h:847
ScanDirection
Definition: sdir.h:22
static Datum ExecHashSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:102
uintptr_t Datum
Definition: postgres.h:367
static Datum ExecScanSubPlan(SubPlanState *node, ExprContext *econtext, bool *isNull)
Definition: nodeSubplan.c:224
List * setParam
Definition: primnodes.h:713
#define elog(elevel,...)
Definition: elog.h:226
bool useHashTable
Definition: primnodes.h:704
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99