1 /*-------------------------------------------------------------------------
4 * routines to support subselects
6 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/backend/executor/nodeSubplan.c,v 1.90 2007/08/26 21:44:25 tgl Exp $
12 *-------------------------------------------------------------------------
16 * ExecSubPlan - process a subselect
17 * ExecInitSubPlan - initialize a subselect
23 #include "access/heapam.h"
24 #include "executor/executor.h"
25 #include "executor/nodeSubplan.h"
26 #include "nodes/makefuncs.h"
27 #include "optimizer/clauses.h"
28 #include "utils/array.h"
29 #include "utils/lsyscache.h"
30 #include "utils/memutils.h"
33 static Datum ExecHashSubPlan(SubPlanState *node,
34 ExprContext *econtext,
36 static Datum ExecScanSubPlan(SubPlanState *node,
37 ExprContext *econtext,
39 static void buildSubPlanHash(SubPlanState *node, ExprContext *econtext);
40 static bool findPartialMatch(TupleHashTable hashtable, TupleTableSlot *slot);
41 static bool slotAllNulls(TupleTableSlot *slot);
42 static bool slotNoNulls(TupleTableSlot *slot);
45 /* ----------------------------------------------------------------
47 * ----------------------------------------------------------------
50 ExecSubPlan(SubPlanState *node,
51 ExprContext *econtext,
55 SubPlan *subplan = (SubPlan *) node->xprstate.expr;
57 /* Set default values for result flags: non-null, not a set result */
60 *isDone = ExprSingleResult;
62 if (subplan->setParam != NIL)
63 elog(ERROR, "cannot set parent params from subquery");
65 if (subplan->useHashTable)
66 return ExecHashSubPlan(node, econtext, isNull);
68 return ExecScanSubPlan(node, econtext, isNull);
72 * ExecHashSubPlan: store subselect result in an in-memory hash table
75 ExecHashSubPlan(SubPlanState *node,
76 ExprContext *econtext,
79 SubPlan *subplan = (SubPlan *) node->xprstate.expr;
80 PlanState *planstate = node->planstate;
81 ExprContext *innerecontext = node->innerecontext;
84 /* Shouldn't have any direct correlation Vars */
85 if (subplan->parParam != NIL || node->args != NIL)
86 elog(ERROR, "hashed subplan with direct correlation not supported");
89 * If first time through or we need to rescan the subplan, build the hash
92 if (node->hashtable == NULL || planstate->chgParam != NULL)
93 buildSubPlanHash(node, econtext);
96 * The result for an empty subplan is always FALSE; no need to evaluate
100 if (!node->havehashrows && !node->havenullrows)
101 return BoolGetDatum(false);
104 * Evaluate lefthand expressions and form a projection tuple. First we
105 * have to set the econtext to use (hack alert!).
107 node->projLeft->pi_exprContext = econtext;
108 slot = ExecProject(node->projLeft, NULL);
111 * Note: because we are typically called in a per-tuple context, we have
112 * to explicitly clear the projected tuple before returning. Otherwise,
113 * we'll have a double-free situation: the per-tuple context will probably
114 * be reset before we're called again, and then the tuple slot will think
115 * it still needs to free the tuple.
119 * Since the hashtable routines will use innerecontext's per-tuple memory
120 * as working memory, be sure to reset it for each tuple.
122 ResetExprContext(innerecontext);
125 * If the LHS is all non-null, probe for an exact match in the main hash
126 * table. If we find one, the result is TRUE. Otherwise, scan the
127 * partly-null table to see if there are any rows that aren't provably
128 * unequal to the LHS; if so, the result is UNKNOWN. (We skip that part
129 * if we don't care about UNKNOWN.) Otherwise, the result is FALSE.
131 * Note: the reason we can avoid a full scan of the main hash table is
132 * that the combining operators are assumed never to yield NULL when both
133 * inputs are non-null. If they were to do so, we might need to produce
134 * UNKNOWN instead of FALSE because of an UNKNOWN result in comparing the
135 * LHS to some main-table entry --- which is a comparison we will not even
136 * make, unless there's a chance match of hash keys.
138 if (slotNoNulls(slot))
140 if (node->havehashrows &&
141 FindTupleHashEntry(node->hashtable,
144 node->lhs_hash_funcs) != NULL)
146 ExecClearTuple(slot);
147 return BoolGetDatum(true);
149 if (node->havenullrows &&
150 findPartialMatch(node->hashnulls, slot))
152 ExecClearTuple(slot);
154 return BoolGetDatum(false);
156 ExecClearTuple(slot);
157 return BoolGetDatum(false);
161 * When the LHS is partly or wholly NULL, we can never return TRUE. If we
162 * don't care about UNKNOWN, just return FALSE. Otherwise, if the LHS is
163 * wholly NULL, immediately return UNKNOWN. (Since the combining
164 * operators are strict, the result could only be FALSE if the sub-select
165 * were empty, but we already handled that case.) Otherwise, we must scan
166 * both the main and partly-null tables to see if there are any rows that
167 * aren't provably unequal to the LHS; if so, the result is UNKNOWN.
168 * Otherwise, the result is FALSE.
170 if (node->hashnulls == NULL)
172 ExecClearTuple(slot);
173 return BoolGetDatum(false);
175 if (slotAllNulls(slot))
177 ExecClearTuple(slot);
179 return BoolGetDatum(false);
181 /* Scan partly-null table first, since more likely to get a match */
182 if (node->havenullrows &&
183 findPartialMatch(node->hashnulls, slot))
185 ExecClearTuple(slot);
187 return BoolGetDatum(false);
189 if (node->havehashrows &&
190 findPartialMatch(node->hashtable, slot))
192 ExecClearTuple(slot);
194 return BoolGetDatum(false);
196 ExecClearTuple(slot);
197 return BoolGetDatum(false);
201 * ExecScanSubPlan: default case where we have to rescan subplan each time
204 ExecScanSubPlan(SubPlanState *node,
205 ExprContext *econtext,
208 SubPlan *subplan = (SubPlan *) node->xprstate.expr;
209 PlanState *planstate = node->planstate;
210 SubLinkType subLinkType = subplan->subLinkType;
211 MemoryContext oldcontext;
212 TupleTableSlot *slot;
214 bool found = false; /* TRUE if got at least one subplan tuple */
217 ArrayBuildState *astate = NULL;
220 * We are probably in a short-lived expression-evaluation context. Switch
221 * to the per-query context for manipulating the child plan's chgParam,
222 * calling ExecProcNode on it, etc.
224 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
227 * Set Params of this plan from parent plan correlation values. (Any
228 * calculation we have to do is done in the parent econtext, since the
229 * Param values don't need to have per-query lifetime.)
231 Assert(list_length(subplan->parParam) == list_length(node->args));
233 forboth(l, subplan->parParam, pvar, node->args)
235 int paramid = lfirst_int(l);
236 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
238 prm->value = ExecEvalExprSwitchContext((ExprState *) lfirst(pvar),
242 planstate->chgParam = bms_add_member(planstate->chgParam, paramid);
246 * Now that we've set up its parameters, we can reset the subplan.
248 ExecReScan(planstate, NULL);
251 * For all sublink types except EXPR_SUBLINK and ARRAY_SUBLINK, the result
252 * is boolean as are the results of the combining operators. We combine
253 * results across tuples (if the subplan produces more than one) using OR
254 * semantics for ANY_SUBLINK or AND semantics for ALL_SUBLINK.
255 * (ROWCOMPARE_SUBLINK doesn't allow multiple tuples from the subplan.)
256 * NULL results from the combining operators are handled according to the
257 * usual SQL semantics for OR and AND. The result for no input tuples is
258 * FALSE for ANY_SUBLINK, TRUE for ALL_SUBLINK, NULL for
259 * ROWCOMPARE_SUBLINK.
261 * For EXPR_SUBLINK we require the subplan to produce no more than one
262 * tuple, else an error is raised. If zero tuples are produced, we return
263 * NULL. Assuming we get a tuple, we just use its first column (there can
264 * be only one non-junk column in this case).
266 * For ARRAY_SUBLINK we allow the subplan to produce any number of tuples,
267 * and form an array of the first column's values. Note in particular
268 * that we produce a zero-element array if no tuples are produced (this
269 * is a change from pre-8.3 behavior of returning NULL).
271 result = BoolGetDatum(subLinkType == ALL_SUBLINK);
274 for (slot = ExecProcNode(planstate);
276 slot = ExecProcNode(planstate))
278 TupleDesc tdesc = slot->tts_tupleDescriptor;
284 if (subLinkType == EXISTS_SUBLINK)
287 result = BoolGetDatum(true);
291 if (subLinkType == EXPR_SUBLINK)
293 /* cannot allow multiple input tuples for EXPR sublink */
296 (errcode(ERRCODE_CARDINALITY_VIOLATION),
297 errmsg("more than one row returned by a subquery used as an expression")));
301 * We need to copy the subplan's tuple in case the result is of
302 * pass-by-ref type --- our return value will point into this
303 * copied tuple! Can't use the subplan's instance of the tuple
304 * since it won't still be valid after next ExecProcNode() call.
305 * node->curTuple keeps track of the copied tuple for eventual
309 heap_freetuple(node->curTuple);
310 node->curTuple = ExecCopySlotTuple(slot);
312 result = heap_getattr(node->curTuple, 1, tdesc, isNull);
313 /* keep scanning subplan to make sure there's only one tuple */
317 if (subLinkType == ARRAY_SUBLINK)
323 /* stash away current value */
324 Assert(subplan->firstColType == tdesc->attrs[0]->atttypid);
325 dvalue = slot_getattr(slot, 1, &disnull);
326 astate = accumArrayResult(astate, dvalue, disnull,
327 subplan->firstColType, oldcontext);
328 /* keep scanning subplan to collect all values */
332 /* cannot allow multiple input tuples for ROWCOMPARE sublink either */
333 if (subLinkType == ROWCOMPARE_SUBLINK && found)
335 (errcode(ERRCODE_CARDINALITY_VIOLATION),
336 errmsg("more than one row returned by a subquery used as an expression")));
341 * For ALL, ANY, and ROWCOMPARE sublinks, load up the Params
342 * representing the columns of the sub-select, and then evaluate the
343 * combining expression.
346 foreach(plst, subplan->paramIds)
348 int paramid = lfirst_int(plst);
349 ParamExecData *prmdata;
351 prmdata = &(econtext->ecxt_param_exec_vals[paramid]);
352 Assert(prmdata->execPlan == NULL);
353 prmdata->value = slot_getattr(slot, col, &(prmdata->isnull));
357 rowresult = ExecEvalExprSwitchContext(node->testexpr, econtext,
360 if (subLinkType == ANY_SUBLINK)
362 /* combine across rows per OR semantics */
365 else if (DatumGetBool(rowresult))
367 result = BoolGetDatum(true);
369 break; /* needn't look at any more rows */
372 else if (subLinkType == ALL_SUBLINK)
374 /* combine across rows per AND semantics */
377 else if (!DatumGetBool(rowresult))
379 result = BoolGetDatum(false);
381 break; /* needn't look at any more rows */
386 /* must be ROWCOMPARE_SUBLINK */
392 MemoryContextSwitchTo(oldcontext);
394 if (subLinkType == ARRAY_SUBLINK)
396 /* We return the result in the caller's context */
398 result = makeArrayResult(astate, oldcontext);
400 result = PointerGetDatum(construct_empty_array(subplan->firstColType));
405 * deal with empty subplan result. result/isNull were previously
406 * initialized correctly for all sublink types except EXPR and
407 * ROWCOMPARE; for those, return NULL.
409 if (subLinkType == EXPR_SUBLINK ||
410 subLinkType == ROWCOMPARE_SUBLINK)
421 * buildSubPlanHash: load hash table by scanning subplan output.
424 buildSubPlanHash(SubPlanState *node, ExprContext *econtext)
426 SubPlan *subplan = (SubPlan *) node->xprstate.expr;
427 PlanState *planstate = node->planstate;
428 int ncols = list_length(subplan->paramIds);
429 ExprContext *innerecontext = node->innerecontext;
430 MemoryContext tempcxt = innerecontext->ecxt_per_tuple_memory;
431 MemoryContext oldcontext;
433 TupleTableSlot *slot;
435 Assert(subplan->subLinkType == ANY_SUBLINK);
438 * If we already had any hash tables, destroy 'em; then create empty hash
441 * If we need to distinguish accurately between FALSE and UNKNOWN (i.e.,
442 * NULL) results of the IN operation, then we have to store subplan output
443 * rows that are partly or wholly NULL. We store such rows in a separate
444 * hash table that we expect will be much smaller than the main table. (We
445 * can use hashing to eliminate partly-null rows that are not distinct. We
446 * keep them separate to minimize the cost of the inevitable full-table
447 * searches; see findPartialMatch.)
449 * If it's not necessary to distinguish FALSE and UNKNOWN, then we don't
450 * need to store subplan output rows that contain NULL.
452 MemoryContextReset(node->tablecxt);
453 node->hashtable = NULL;
454 node->hashnulls = NULL;
455 node->havehashrows = false;
456 node->havenullrows = false;
458 nbuckets = (int) ceil(planstate->plan->plan_rows);
462 node->hashtable = BuildTupleHashTable(ncols,
465 node->tab_hash_funcs,
467 sizeof(TupleHashEntryData),
471 if (!subplan->unknownEqFalse)
474 nbuckets = 1; /* there can only be one entry */
481 node->hashnulls = BuildTupleHashTable(ncols,
484 node->tab_hash_funcs,
486 sizeof(TupleHashEntryData),
492 * We are probably in a short-lived expression-evaluation context. Switch
493 * to the per-query context for manipulating the child plan.
495 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
498 * Reset subplan to start.
500 ExecReScan(planstate, NULL);
503 * Scan the subplan and load the hash table(s). Note that when there are
504 * duplicate rows coming out of the sub-select, only one copy is stored.
506 for (slot = ExecProcNode(planstate);
508 slot = ExecProcNode(planstate))
515 * Load up the Params representing the raw sub-select outputs, then
516 * form the projection tuple to store in the hashtable.
518 foreach(plst, subplan->paramIds)
520 int paramid = lfirst_int(plst);
521 ParamExecData *prmdata;
523 prmdata = &(innerecontext->ecxt_param_exec_vals[paramid]);
524 Assert(prmdata->execPlan == NULL);
525 prmdata->value = slot_getattr(slot, col,
529 slot = ExecProject(node->projRight, NULL);
532 * If result contains any nulls, store separately or not at all.
534 if (slotNoNulls(slot))
536 (void) LookupTupleHashEntry(node->hashtable, slot, &isnew);
537 node->havehashrows = true;
539 else if (node->hashnulls)
541 (void) LookupTupleHashEntry(node->hashnulls, slot, &isnew);
542 node->havenullrows = true;
546 * Reset innerecontext after each inner tuple to free any memory used
547 * in hash computation or comparison routines.
549 ResetExprContext(innerecontext);
553 * Since the projected tuples are in the sub-query's context and not the
554 * main context, we'd better clear the tuple slot before there's any
555 * chance of a reset of the sub-query's context. Else we will have the
556 * potential for a double free attempt. (XXX possibly no longer needed,
559 ExecClearTuple(node->projRight->pi_slot);
561 MemoryContextSwitchTo(oldcontext);
565 * findPartialMatch: does the hashtable contain an entry that is not
566 * provably distinct from the tuple?
568 * We have to scan the whole hashtable; we can't usefully use hashkeys
569 * to guide probing, since we might get partial matches on tuples with
570 * hashkeys quite unrelated to what we'd get from the given tuple.
573 findPartialMatch(TupleHashTable hashtable, TupleTableSlot *slot)
575 int numCols = hashtable->numCols;
576 AttrNumber *keyColIdx = hashtable->keyColIdx;
577 TupleHashIterator hashiter;
578 TupleHashEntry entry;
580 InitTupleHashIterator(hashtable, &hashiter);
581 while ((entry = ScanTupleHashTable(&hashiter)) != NULL)
583 ExecStoreMinimalTuple(entry->firstTuple, hashtable->tableslot, false);
584 if (!execTuplesUnequal(slot, hashtable->tableslot,
586 hashtable->cur_eq_funcs,
589 TermTupleHashIterator(&hashiter);
593 /* No TermTupleHashIterator call needed here */
598 * slotAllNulls: is the slot completely NULL?
600 * This does not test for dropped columns, which is OK because we only
601 * use it on projected tuples.
604 slotAllNulls(TupleTableSlot *slot)
606 int ncols = slot->tts_tupleDescriptor->natts;
609 for (i = 1; i <= ncols; i++)
611 if (!slot_attisnull(slot, i))
618 * slotNoNulls: is the slot entirely not NULL?
620 * This does not test for dropped columns, which is OK because we only
621 * use it on projected tuples.
624 slotNoNulls(TupleTableSlot *slot)
626 int ncols = slot->tts_tupleDescriptor->natts;
629 for (i = 1; i <= ncols; i++)
631 if (slot_attisnull(slot, i))
637 /* ----------------------------------------------------------------
640 * Create a SubPlanState for a SubPlan; this is the SubPlan-specific part
641 * of ExecInitExpr(). We split it out so that it can be used for InitPlans
642 * as well as regular SubPlans. Note that we don't link the SubPlan into
643 * the parent's subPlan list, because that shouldn't happen for InitPlans.
644 * Instead, ExecInitExpr() does that one part.
645 * ----------------------------------------------------------------
648 ExecInitSubPlan(SubPlan *subplan, PlanState *parent)
650 SubPlanState *sstate = makeNode(SubPlanState);
651 EState *estate = parent->state;
653 sstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecSubPlan;
654 sstate->xprstate.expr = (Expr *) subplan;
656 /* Link the SubPlanState to already-initialized subplan */
657 sstate->planstate = (PlanState *) list_nth(estate->es_subplanstates,
658 subplan->plan_id - 1);
660 /* Initialize subexpressions */
661 sstate->testexpr = ExecInitExpr((Expr *) subplan->testexpr, parent);
662 sstate->args = (List *) ExecInitExpr((Expr *) subplan->args, parent);
665 * initialize my state
667 sstate->curTuple = NULL;
668 sstate->projLeft = NULL;
669 sstate->projRight = NULL;
670 sstate->hashtable = NULL;
671 sstate->hashnulls = NULL;
672 sstate->tablecxt = NULL;
673 sstate->innerecontext = NULL;
674 sstate->keyColIdx = NULL;
675 sstate->tab_hash_funcs = NULL;
676 sstate->tab_eq_funcs = NULL;
677 sstate->lhs_hash_funcs = NULL;
678 sstate->cur_eq_funcs = NULL;
681 * If this plan is un-correlated or undirect correlated one and want to
682 * set params for parent plan then mark parameters as needing evaluation.
684 * Note that in the case of un-correlated subqueries we don't care about
685 * setting parent->chgParam here: indices take care about it, for others -
686 * it doesn't matter...
688 if (subplan->setParam != NIL)
692 foreach(lst, subplan->setParam)
694 int paramid = lfirst_int(lst);
695 ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
697 prm->execPlan = sstate;
702 * If we are going to hash the subquery output, initialize relevant stuff.
703 * (We don't create the hashtable until needed, though.)
705 if (subplan->useHashTable)
711 TupleTableSlot *slot;
719 /* We need a memory context to hold the hash table(s) */
721 AllocSetContextCreate(CurrentMemoryContext,
722 "Subplan HashTable Context",
723 ALLOCSET_DEFAULT_MINSIZE,
724 ALLOCSET_DEFAULT_INITSIZE,
725 ALLOCSET_DEFAULT_MAXSIZE);
726 /* and a short-lived exprcontext for function evaluation */
727 sstate->innerecontext = CreateExprContext(estate);
728 /* Silly little array of column numbers 1..n */
729 ncols = list_length(subplan->paramIds);
730 sstate->keyColIdx = (AttrNumber *) palloc(ncols * sizeof(AttrNumber));
731 for (i = 0; i < ncols; i++)
732 sstate->keyColIdx[i] = i + 1;
735 * We use ExecProject to evaluate the lefthand and righthand
736 * expression lists and form tuples. (You might think that we could
737 * use the sub-select's output tuples directly, but that is not the
738 * case if we had to insert any run-time coercions of the sub-select's
739 * output datatypes; anyway this avoids storing any resjunk columns
740 * that might be in the sub-select's output.) Run through the
741 * combining expressions to build tlists for the lefthand and
742 * righthand sides. We need both the ExprState list (for ExecProject)
743 * and the underlying parse Exprs (for ExecTypeFromTL).
745 * We also extract the combining operators themselves to initialize
746 * the equality and hashing functions for the hash tables.
748 if (IsA(sstate->testexpr->expr, OpExpr))
750 /* single combining operator */
751 oplist = list_make1(sstate->testexpr);
753 else if (and_clause((Node *) sstate->testexpr->expr))
755 /* multiple combining operators */
756 Assert(IsA(sstate->testexpr, BoolExprState));
757 oplist = ((BoolExprState *) sstate->testexpr)->args;
761 /* shouldn't see anything else in a hashable subplan */
762 elog(ERROR, "unrecognized testexpr type: %d",
763 (int) nodeTag(sstate->testexpr->expr));
764 oplist = NIL; /* keep compiler quiet */
766 Assert(list_length(oplist) == ncols);
768 lefttlist = righttlist = NIL;
769 leftptlist = rightptlist = NIL;
770 sstate->tab_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
771 sstate->tab_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
772 sstate->lhs_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
773 sstate->cur_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
777 FuncExprState *fstate = (FuncExprState *) lfirst(l);
778 OpExpr *opexpr = (OpExpr *) fstate->xprstate.expr;
782 GenericExprState *tlestate;
787 Assert(IsA(fstate, FuncExprState));
788 Assert(IsA(opexpr, OpExpr));
789 Assert(list_length(fstate->args) == 2);
791 /* Process lefthand argument */
792 exstate = (ExprState *) linitial(fstate->args);
793 expr = exstate->expr;
794 tle = makeTargetEntry(expr,
798 tlestate = makeNode(GenericExprState);
799 tlestate->xprstate.expr = (Expr *) tle;
800 tlestate->xprstate.evalfunc = NULL;
801 tlestate->arg = exstate;
802 lefttlist = lappend(lefttlist, tlestate);
803 leftptlist = lappend(leftptlist, tle);
805 /* Process righthand argument */
806 exstate = (ExprState *) lsecond(fstate->args);
807 expr = exstate->expr;
808 tle = makeTargetEntry(expr,
812 tlestate = makeNode(GenericExprState);
813 tlestate->xprstate.expr = (Expr *) tle;
814 tlestate->xprstate.evalfunc = NULL;
815 tlestate->arg = exstate;
816 righttlist = lappend(righttlist, tlestate);
817 rightptlist = lappend(rightptlist, tle);
819 /* Lookup the equality function (potentially cross-type) */
820 fmgr_info(opexpr->opfuncid, &sstate->cur_eq_funcs[i - 1]);
821 sstate->cur_eq_funcs[i - 1].fn_expr = (Node *) opexpr;
823 /* Look up the equality function for the RHS type */
824 if (!get_compatible_hash_operators(opexpr->opno,
826 elog(ERROR, "could not find compatible hash operator for operator %u",
828 fmgr_info(get_opcode(rhs_eq_oper), &sstate->tab_eq_funcs[i - 1]);
830 /* Lookup the associated hash functions */
831 if (!get_op_hash_functions(opexpr->opno,
832 &left_hashfn, &right_hashfn))
833 elog(ERROR, "could not find hash function for hash operator %u",
835 fmgr_info(left_hashfn, &sstate->lhs_hash_funcs[i - 1]);
836 fmgr_info(right_hashfn, &sstate->tab_hash_funcs[i - 1]);
842 * Create a tupletable to hold these tuples. (Note: we never bother
843 * to free the tupletable explicitly; that's okay because it will
844 * never store raw disk tuples that might have associated buffer pins.
845 * The only resource involved is memory, which will be cleaned up by
846 * freeing the query context.)
848 tupTable = ExecCreateTupleTable(2);
851 * Construct tupdescs, slots and projection nodes for left and right
852 * sides. The lefthand expressions will be evaluated in the parent
853 * plan node's exprcontext, which we don't have access to here.
854 * Fortunately we can just pass NULL for now and fill it in later
855 * (hack alert!). The righthand expressions will be evaluated in our
858 tupDesc = ExecTypeFromTL(leftptlist, false);
859 slot = ExecAllocTableSlot(tupTable);
860 ExecSetSlotDescriptor(slot, tupDesc);
861 sstate->projLeft = ExecBuildProjectionInfo(lefttlist,
866 tupDesc = ExecTypeFromTL(rightptlist, false);
867 slot = ExecAllocTableSlot(tupTable);
868 ExecSetSlotDescriptor(slot, tupDesc);
869 sstate->projRight = ExecBuildProjectionInfo(righttlist,
870 sstate->innerecontext,
878 /* ----------------------------------------------------------------
881 * Executes an InitPlan subplan and sets its output parameters.
883 * This is called from ExecEvalParam() when the value of a PARAM_EXEC
884 * parameter is requested and the param's execPlan field is set (indicating
885 * that the param has not yet been evaluated). This allows lazy evaluation
886 * of initplans: we don't run the subplan until/unless we need its output.
887 * Note that this routine MUST clear the execPlan fields of the plan's
888 * output parameters after evaluating them!
889 * ----------------------------------------------------------------
892 ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
894 SubPlan *subplan = (SubPlan *) node->xprstate.expr;
895 PlanState *planstate = node->planstate;
896 SubLinkType subLinkType = subplan->subLinkType;
897 MemoryContext oldcontext;
898 TupleTableSlot *slot;
901 ArrayBuildState *astate = NULL;
904 * Must switch to per-query memory context.
906 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
908 if (subLinkType == ANY_SUBLINK ||
909 subLinkType == ALL_SUBLINK)
910 elog(ERROR, "ANY/ALL subselect unsupported as initplan");
913 * By definition, an initplan has no parameters from our query level,
914 * but it could have some from an outer level. Rescan it if needed.
916 if (planstate->chgParam != NULL)
917 ExecReScan(planstate, NULL);
919 for (slot = ExecProcNode(planstate);
921 slot = ExecProcNode(planstate))
923 TupleDesc tdesc = slot->tts_tupleDescriptor;
926 if (subLinkType == EXISTS_SUBLINK)
928 /* There can be only one param... */
929 int paramid = linitial_int(subplan->setParam);
930 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
932 prm->execPlan = NULL;
933 prm->value = BoolGetDatum(true);
939 if (subLinkType == ARRAY_SUBLINK)
945 /* stash away current value */
946 Assert(subplan->firstColType == tdesc->attrs[0]->atttypid);
947 dvalue = slot_getattr(slot, 1, &disnull);
948 astate = accumArrayResult(astate, dvalue, disnull,
949 subplan->firstColType, oldcontext);
950 /* keep scanning subplan to collect all values */
955 (subLinkType == EXPR_SUBLINK ||
956 subLinkType == ROWCOMPARE_SUBLINK))
958 (errcode(ERRCODE_CARDINALITY_VIOLATION),
959 errmsg("more than one row returned by a subquery used as an expression")));
964 * We need to copy the subplan's tuple into our own context, in case
965 * any of the params are pass-by-ref type --- the pointers stored in
966 * the param structs will point at this copied tuple! node->curTuple
967 * keeps track of the copied tuple for eventual freeing.
970 heap_freetuple(node->curTuple);
971 node->curTuple = ExecCopySlotTuple(slot);
974 * Now set all the setParam params from the columns of the tuple
976 foreach(l, subplan->setParam)
978 int paramid = lfirst_int(l);
979 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
981 prm->execPlan = NULL;
982 prm->value = heap_getattr(node->curTuple, i, tdesc,
988 if (subLinkType == ARRAY_SUBLINK)
990 /* There can be only one param... */
991 int paramid = linitial_int(subplan->setParam);
992 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
994 prm->execPlan = NULL;
995 /* We build the result in query context so it won't disappear */
997 prm->value = makeArrayResult(astate,
998 econtext->ecxt_per_query_memory);
1001 MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
1002 prm->value = PointerGetDatum(construct_empty_array(subplan->firstColType));
1004 prm->isnull = false;
1008 if (subLinkType == EXISTS_SUBLINK)
1010 /* There can be only one param... */
1011 int paramid = linitial_int(subplan->setParam);
1012 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1014 prm->execPlan = NULL;
1015 prm->value = BoolGetDatum(false);
1016 prm->isnull = false;
1020 foreach(l, subplan->setParam)
1022 int paramid = lfirst_int(l);
1023 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1025 prm->execPlan = NULL;
1026 prm->value = (Datum) 0;
1032 MemoryContextSwitchTo(oldcontext);
1036 * Mark an initplan as needing recalculation
1039 ExecReScanSetParamPlan(SubPlanState *node, PlanState *parent)
1041 PlanState *planstate = node->planstate;
1042 SubPlan *subplan = (SubPlan *) node->xprstate.expr;
1043 EState *estate = parent->state;
1047 if (subplan->parParam != NIL)
1048 elog(ERROR, "direct correlated subquery unsupported as initplan");
1049 if (subplan->setParam == NIL)
1050 elog(ERROR, "setParam list of initplan is empty");
1051 if (bms_is_empty(planstate->plan->extParam))
1052 elog(ERROR, "extParam set of initplan is empty");
1055 * Don't actually re-scan: ExecSetParamPlan does it if needed.
1059 * Mark this subplan's output parameters as needing recalculation
1061 foreach(l, subplan->setParam)
1063 int paramid = lfirst_int(l);
1064 ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
1066 prm->execPlan = node;
1067 parent->chgParam = bms_add_member(parent->chgParam, paramid);