1 /*-------------------------------------------------------------------------
4 * routines to support subselects
6 * Portions Copyright (c) 1996-2006, 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.76 2006/06/28 17:05:49 tgl Exp $
12 *-------------------------------------------------------------------------
16 * ExecSubPlan - process a subselect
17 * ExecInitSubPlan - initialize a subselect
18 * ExecEndSubPlan - shut down a subselect
22 #include "access/heapam.h"
23 #include "executor/executor.h"
24 #include "executor/nodeSubplan.h"
25 #include "nodes/makefuncs.h"
26 #include "optimizer/clauses.h"
27 #include "parser/parse_expr.h"
28 #include "utils/array.h"
29 #include "utils/datum.h"
30 #include "utils/lsyscache.h"
31 #include "utils/memutils.h"
34 static Datum ExecHashSubPlan(SubPlanState *node,
35 ExprContext *econtext,
37 static Datum ExecScanSubPlan(SubPlanState *node,
38 ExprContext *econtext,
40 static void buildSubPlanHash(SubPlanState *node);
41 static bool findPartialMatch(TupleHashTable hashtable, TupleTableSlot *slot);
42 static bool slotAllNulls(TupleTableSlot *slot);
43 static bool slotNoNulls(TupleTableSlot *slot);
46 /* ----------------------------------------------------------------
48 * ----------------------------------------------------------------
51 ExecSubPlan(SubPlanState *node,
52 ExprContext *econtext,
56 SubPlan *subplan = (SubPlan *) node->xprstate.expr;
58 /* Set default values for result flags: non-null, not a set result */
61 *isDone = ExprSingleResult;
63 if (subplan->setParam != NIL)
64 elog(ERROR, "cannot set parent params from subquery");
66 if (subplan->useHashTable)
67 return ExecHashSubPlan(node, econtext, isNull);
69 return ExecScanSubPlan(node, econtext, isNull);
73 * ExecHashSubPlan: store subselect result in an in-memory hash table
76 ExecHashSubPlan(SubPlanState *node,
77 ExprContext *econtext,
80 SubPlan *subplan = (SubPlan *) node->xprstate.expr;
81 PlanState *planstate = node->planstate;
82 ExprContext *innerecontext = node->innerecontext;
85 /* Shouldn't have any direct correlation Vars */
86 if (subplan->parParam != NIL || node->args != NIL)
87 elog(ERROR, "hashed subplan with direct correlation not supported");
90 * If first time through or we need to rescan the subplan, build the hash
93 if (node->hashtable == NULL || planstate->chgParam != NULL)
94 buildSubPlanHash(node);
97 * The result for an empty subplan is always FALSE; no need to evaluate
101 if (!node->havehashrows && !node->havenullrows)
102 return BoolGetDatum(false);
105 * Evaluate lefthand expressions and form a projection tuple. First we
106 * have to set the econtext to use (hack alert!).
108 node->projLeft->pi_exprContext = econtext;
109 slot = ExecProject(node->projLeft, NULL);
112 * Note: because we are typically called in a per-tuple context, we have
113 * to explicitly clear the projected tuple before returning. Otherwise,
114 * we'll have a double-free situation: the per-tuple context will probably
115 * be reset before we're called again, and then the tuple slot will think
116 * it still needs to free the tuple.
120 * Since the hashtable routines will use innerecontext's per-tuple memory
121 * as working memory, be sure to reset it for each tuple.
123 ResetExprContext(innerecontext);
126 * If the LHS is all non-null, probe for an exact match in the main hash
127 * table. If we find one, the result is TRUE. Otherwise, scan the
128 * partly-null table to see if there are any rows that aren't provably
129 * unequal to the LHS; if so, the result is UNKNOWN. (We skip that part
130 * if we don't care about UNKNOWN.) Otherwise, the result is FALSE.
132 * Note: the reason we can avoid a full scan of the main hash table is
133 * that the combining operators are assumed never to yield NULL when both
134 * inputs are non-null. If they were to do so, we might need to produce
135 * UNKNOWN instead of FALSE because of an UNKNOWN result in comparing the
136 * LHS to some main-table entry --- which is a comparison we will not even
137 * make, unless there's a chance match of hash keys.
139 if (slotNoNulls(slot))
141 if (node->havehashrows &&
142 LookupTupleHashEntry(node->hashtable, slot, NULL) != NULL)
144 ExecClearTuple(slot);
145 return BoolGetDatum(true);
147 if (node->havenullrows &&
148 findPartialMatch(node->hashnulls, slot))
150 ExecClearTuple(slot);
152 return BoolGetDatum(false);
154 ExecClearTuple(slot);
155 return BoolGetDatum(false);
159 * When the LHS is partly or wholly NULL, we can never return TRUE. If we
160 * don't care about UNKNOWN, just return FALSE. Otherwise, if the LHS is
161 * wholly NULL, immediately return UNKNOWN. (Since the combining
162 * operators are strict, the result could only be FALSE if the sub-select
163 * were empty, but we already handled that case.) Otherwise, we must scan
164 * both the main and partly-null tables to see if there are any rows that
165 * aren't provably unequal to the LHS; if so, the result is UNKNOWN.
166 * Otherwise, the result is FALSE.
168 if (node->hashnulls == NULL)
170 ExecClearTuple(slot);
171 return BoolGetDatum(false);
173 if (slotAllNulls(slot))
175 ExecClearTuple(slot);
177 return BoolGetDatum(false);
179 /* Scan partly-null table first, since more likely to get a match */
180 if (node->havenullrows &&
181 findPartialMatch(node->hashnulls, slot))
183 ExecClearTuple(slot);
185 return BoolGetDatum(false);
187 if (node->havehashrows &&
188 findPartialMatch(node->hashtable, slot))
190 ExecClearTuple(slot);
192 return BoolGetDatum(false);
194 ExecClearTuple(slot);
195 return BoolGetDatum(false);
199 * ExecScanSubPlan: default case where we have to rescan subplan each time
202 ExecScanSubPlan(SubPlanState *node,
203 ExprContext *econtext,
206 SubPlan *subplan = (SubPlan *) node->xprstate.expr;
207 PlanState *planstate = node->planstate;
208 SubLinkType subLinkType = subplan->subLinkType;
209 MemoryContext oldcontext;
210 TupleTableSlot *slot;
212 bool found = false; /* TRUE if got at least one subplan tuple */
215 ArrayBuildState *astate = NULL;
218 * We are probably in a short-lived expression-evaluation context. Switch
219 * to the child plan's per-query context for manipulating its chgParam,
220 * calling ExecProcNode on it, etc.
222 oldcontext = MemoryContextSwitchTo(node->sub_estate->es_query_cxt);
225 * Set Params of this plan from parent plan correlation values. (Any
226 * calculation we have to do is done in the parent econtext, since the
227 * Param values don't need to have per-query lifetime.)
229 Assert(list_length(subplan->parParam) == list_length(node->args));
231 forboth(l, subplan->parParam, pvar, node->args)
233 int paramid = lfirst_int(l);
234 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
236 prm->value = ExecEvalExprSwitchContext((ExprState *) lfirst(pvar),
240 planstate->chgParam = bms_add_member(planstate->chgParam, paramid);
243 ExecReScan(planstate, NULL);
246 * For all sublink types except EXPR_SUBLINK and ARRAY_SUBLINK, the result
247 * is boolean as are the results of the combining operators. We combine
248 * results across tuples (if the subplan produces more than one) using OR
249 * semantics for ANY_SUBLINK or AND semantics for ALL_SUBLINK.
250 * (ROWCOMPARE_SUBLINK doesn't allow multiple tuples from the subplan.)
251 * NULL results from the combining operators are handled according to the
252 * usual SQL semantics for OR and AND. The result for no input tuples is
253 * FALSE for ANY_SUBLINK, TRUE for ALL_SUBLINK, NULL for
254 * ROWCOMPARE_SUBLINK.
256 * For EXPR_SUBLINK we require the subplan to produce no more than one
257 * tuple, else an error is raised. For ARRAY_SUBLINK we allow the subplan
258 * to produce more than one tuple. In either case, if zero tuples are
259 * produced, we return NULL. Assuming we get a tuple, we just use its
260 * first column (there can be only one non-junk column in this case).
262 result = BoolGetDatum(subLinkType == ALL_SUBLINK);
265 for (slot = ExecProcNode(planstate);
267 slot = ExecProcNode(planstate))
269 TupleDesc tdesc = slot->tts_tupleDescriptor;
275 if (subLinkType == EXISTS_SUBLINK)
278 result = BoolGetDatum(true);
282 if (subLinkType == EXPR_SUBLINK)
284 /* cannot allow multiple input tuples for EXPR sublink */
287 (errcode(ERRCODE_CARDINALITY_VIOLATION),
288 errmsg("more than one row returned by a subquery used as an expression")));
292 * We need to copy the subplan's tuple in case the result is of
293 * pass-by-ref type --- our return value will point into this
294 * copied tuple! Can't use the subplan's instance of the tuple
295 * since it won't still be valid after next ExecProcNode() call.
296 * node->curTuple keeps track of the copied tuple for eventual
299 MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
301 heap_freetuple(node->curTuple);
302 node->curTuple = ExecCopySlotTuple(slot);
303 MemoryContextSwitchTo(node->sub_estate->es_query_cxt);
305 result = heap_getattr(node->curTuple, 1, tdesc, isNull);
306 /* keep scanning subplan to make sure there's only one tuple */
310 if (subLinkType == ARRAY_SUBLINK)
316 /* stash away current value */
317 dvalue = slot_getattr(slot, 1, &disnull);
318 astate = accumArrayResult(astate, dvalue, disnull,
319 tdesc->attrs[0]->atttypid,
321 /* keep scanning subplan to collect all values */
325 /* cannot allow multiple input tuples for ROWCOMPARE sublink either */
326 if (subLinkType == ROWCOMPARE_SUBLINK && found)
328 (errcode(ERRCODE_CARDINALITY_VIOLATION),
329 errmsg("more than one row returned by a subquery used as an expression")));
334 * For ALL, ANY, and ROWCOMPARE sublinks, load up the Params
335 * representing the columns of the sub-select, and then evaluate
336 * the combining expression.
339 foreach(plst, subplan->paramIds)
341 int paramid = lfirst_int(plst);
342 ParamExecData *prmdata;
344 prmdata = &(econtext->ecxt_param_exec_vals[paramid]);
345 Assert(prmdata->execPlan == NULL);
346 prmdata->value = slot_getattr(slot, col, &(prmdata->isnull));
350 rowresult = ExecEvalExprSwitchContext(node->testexpr, econtext,
353 if (subLinkType == ANY_SUBLINK)
355 /* combine across rows per OR semantics */
358 else if (DatumGetBool(rowresult))
360 result = BoolGetDatum(true);
362 break; /* needn't look at any more rows */
365 else if (subLinkType == ALL_SUBLINK)
367 /* combine across rows per AND semantics */
370 else if (!DatumGetBool(rowresult))
372 result = BoolGetDatum(false);
374 break; /* needn't look at any more rows */
379 /* must be ROWCOMPARE_SUBLINK */
388 * deal with empty subplan result. result/isNull were previously
389 * initialized correctly for all sublink types except EXPR, ARRAY, and
390 * ROWCOMPARE; for those, return NULL.
392 if (subLinkType == EXPR_SUBLINK ||
393 subLinkType == ARRAY_SUBLINK ||
394 subLinkType == ROWCOMPARE_SUBLINK)
400 else if (subLinkType == ARRAY_SUBLINK)
402 Assert(astate != NULL);
403 /* We return the result in the caller's context */
404 result = makeArrayResult(astate, oldcontext);
407 MemoryContextSwitchTo(oldcontext);
413 * buildSubPlanHash: load hash table by scanning subplan output.
416 buildSubPlanHash(SubPlanState *node)
418 SubPlan *subplan = (SubPlan *) node->xprstate.expr;
419 PlanState *planstate = node->planstate;
420 int ncols = list_length(subplan->paramIds);
421 ExprContext *innerecontext = node->innerecontext;
422 MemoryContext tempcxt = innerecontext->ecxt_per_tuple_memory;
423 MemoryContext oldcontext;
425 TupleTableSlot *slot;
427 Assert(subplan->subLinkType == ANY_SUBLINK);
430 * If we already had any hash tables, destroy 'em; then create empty hash
433 * If we need to distinguish accurately between FALSE and UNKNOWN (i.e.,
434 * NULL) results of the IN operation, then we have to store subplan output
435 * rows that are partly or wholly NULL. We store such rows in a separate
436 * hash table that we expect will be much smaller than the main table. (We
437 * can use hashing to eliminate partly-null rows that are not distinct.
438 * We keep them separate to minimize the cost of the inevitable full-table
439 * searches; see findPartialMatch.)
441 * If it's not necessary to distinguish FALSE and UNKNOWN, then we don't
442 * need to store subplan output rows that contain NULL.
444 MemoryContextReset(node->tablecxt);
445 node->hashtable = NULL;
446 node->hashnulls = NULL;
447 node->havehashrows = false;
448 node->havenullrows = false;
450 nbuckets = (int) ceil(planstate->plan->plan_rows);
454 node->hashtable = BuildTupleHashTable(ncols,
459 sizeof(TupleHashEntryData),
463 if (!subplan->unknownEqFalse)
466 nbuckets = 1; /* there can only be one entry */
473 node->hashnulls = BuildTupleHashTable(ncols,
478 sizeof(TupleHashEntryData),
484 * We are probably in a short-lived expression-evaluation context. Switch
485 * to the child plan's per-query context for calling ExecProcNode.
487 oldcontext = MemoryContextSwitchTo(node->sub_estate->es_query_cxt);
490 * Reset subplan to start.
492 ExecReScan(planstate, NULL);
495 * Scan the subplan and load the hash table(s). Note that when there are
496 * duplicate rows coming out of the sub-select, only one copy is stored.
498 for (slot = ExecProcNode(planstate);
500 slot = ExecProcNode(planstate))
507 * Load up the Params representing the raw sub-select outputs, then
508 * form the projection tuple to store in the hashtable.
510 foreach(plst, subplan->paramIds)
512 int paramid = lfirst_int(plst);
513 ParamExecData *prmdata;
515 prmdata = &(innerecontext->ecxt_param_exec_vals[paramid]);
516 Assert(prmdata->execPlan == NULL);
517 prmdata->value = slot_getattr(slot, col,
521 slot = ExecProject(node->projRight, NULL);
524 * If result contains any nulls, store separately or not at all.
526 if (slotNoNulls(slot))
528 (void) LookupTupleHashEntry(node->hashtable, slot, &isnew);
529 node->havehashrows = true;
531 else if (node->hashnulls)
533 (void) LookupTupleHashEntry(node->hashnulls, slot, &isnew);
534 node->havenullrows = true;
538 * Reset innerecontext after each inner tuple to free any memory used
539 * in hash computation or comparison routines.
541 ResetExprContext(innerecontext);
545 * Since the projected tuples are in the sub-query's context and not the
546 * main context, we'd better clear the tuple slot before there's any
547 * chance of a reset of the sub-query's context. Else we will have the
548 * potential for a double free attempt. (XXX possibly no longer needed,
551 ExecClearTuple(node->projRight->pi_slot);
553 MemoryContextSwitchTo(oldcontext);
557 * findPartialMatch: does the hashtable contain an entry that is not
558 * provably distinct from the tuple?
560 * We have to scan the whole hashtable; we can't usefully use hashkeys
561 * to guide probing, since we might get partial matches on tuples with
562 * hashkeys quite unrelated to what we'd get from the given tuple.
565 findPartialMatch(TupleHashTable hashtable, TupleTableSlot *slot)
567 int numCols = hashtable->numCols;
568 AttrNumber *keyColIdx = hashtable->keyColIdx;
569 TupleHashIterator hashiter;
570 TupleHashEntry entry;
572 ResetTupleHashIterator(hashtable, &hashiter);
573 while ((entry = ScanTupleHashTable(&hashiter)) != NULL)
575 ExecStoreMinimalTuple(entry->firstTuple, hashtable->tableslot, false);
576 if (!execTuplesUnequal(hashtable->tableslot, slot,
578 hashtable->eqfunctions,
586 * slotAllNulls: is the slot completely NULL?
588 * This does not test for dropped columns, which is OK because we only
589 * use it on projected tuples.
592 slotAllNulls(TupleTableSlot *slot)
594 int ncols = slot->tts_tupleDescriptor->natts;
597 for (i = 1; i <= ncols; i++)
599 if (!slot_attisnull(slot, i))
606 * slotNoNulls: is the slot entirely not NULL?
608 * This does not test for dropped columns, which is OK because we only
609 * use it on projected tuples.
612 slotNoNulls(TupleTableSlot *slot)
614 int ncols = slot->tts_tupleDescriptor->natts;
617 for (i = 1; i <= ncols; i++)
619 if (slot_attisnull(slot, i))
625 /* ----------------------------------------------------------------
628 * Note: the eflags are those passed to the parent plan node of this
629 * subplan; they don't directly describe the execution conditions the
631 * ----------------------------------------------------------------
634 ExecInitSubPlan(SubPlanState *node, EState *estate, int eflags)
636 SubPlan *subplan = (SubPlan *) node->xprstate.expr;
638 MemoryContext oldcontext;
641 * Do access checking on the rangetable entries in the subquery.
643 ExecCheckRTPerms(subplan->rtable);
646 * initialize my state
648 node->needShutdown = false;
649 node->curTuple = NULL;
650 node->projLeft = NULL;
651 node->projRight = NULL;
652 node->hashtable = NULL;
653 node->hashnulls = NULL;
654 node->tablecxt = NULL;
655 node->innerecontext = NULL;
656 node->keyColIdx = NULL;
657 node->eqfunctions = NULL;
658 node->hashfunctions = NULL;
661 * create an EState for the subplan
663 * The subquery needs its own EState because it has its own rangetable. It
664 * shares our Param ID space, however. XXX if rangetable access were done
665 * differently, the subquery could share our EState, which would eliminate
666 * some thrashing about in this module...
668 sp_estate = CreateExecutorState();
669 node->sub_estate = sp_estate;
671 oldcontext = MemoryContextSwitchTo(sp_estate->es_query_cxt);
673 sp_estate->es_range_table = subplan->rtable;
674 sp_estate->es_param_list_info = estate->es_param_list_info;
675 sp_estate->es_param_exec_vals = estate->es_param_exec_vals;
676 sp_estate->es_tupleTable =
677 ExecCreateTupleTable(ExecCountSlotsNode(subplan->plan) + 10);
678 sp_estate->es_snapshot = estate->es_snapshot;
679 sp_estate->es_crosscheck_snapshot = estate->es_crosscheck_snapshot;
680 sp_estate->es_instrument = estate->es_instrument;
683 * Start up the subplan (this is a very cut-down form of InitPlan())
685 * The subplan will never need to do BACKWARD scan or MARK/RESTORE.
686 * If it is a parameterless subplan (not initplan), we suggest that it
687 * be prepared to handle REWIND efficiently; otherwise there is no need.
689 eflags &= EXEC_FLAG_EXPLAIN_ONLY;
690 if (subplan->parParam == NIL && subplan->setParam == NIL)
691 eflags |= EXEC_FLAG_REWIND;
693 node->planstate = ExecInitNode(subplan->plan, sp_estate, eflags);
695 node->needShutdown = true; /* now we need to shutdown the subplan */
697 MemoryContextSwitchTo(oldcontext);
700 * If this plan is un-correlated or undirect correlated one and want to
701 * set params for parent plan then mark parameters as needing evaluation.
703 * Note that in the case of un-correlated subqueries we don't care about
704 * setting parent->chgParam here: indices take care about it, for others -
705 * it doesn't matter...
707 if (subplan->setParam != NIL)
711 foreach(lst, subplan->setParam)
713 int paramid = lfirst_int(lst);
714 ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
716 prm->execPlan = node;
721 * If we are going to hash the subquery output, initialize relevant stuff.
722 * (We don't create the hashtable until needed, though.)
724 if (subplan->useHashTable)
730 TupleTableSlot *slot;
738 /* We need a memory context to hold the hash table(s) */
740 AllocSetContextCreate(CurrentMemoryContext,
741 "Subplan HashTable Context",
742 ALLOCSET_DEFAULT_MINSIZE,
743 ALLOCSET_DEFAULT_INITSIZE,
744 ALLOCSET_DEFAULT_MAXSIZE);
745 /* and a short-lived exprcontext for function evaluation */
746 node->innerecontext = CreateExprContext(estate);
747 /* Silly little array of column numbers 1..n */
748 ncols = list_length(subplan->paramIds);
749 node->keyColIdx = (AttrNumber *) palloc(ncols * sizeof(AttrNumber));
750 for (i = 0; i < ncols; i++)
751 node->keyColIdx[i] = i + 1;
754 * We use ExecProject to evaluate the lefthand and righthand
755 * expression lists and form tuples. (You might think that we could
756 * use the sub-select's output tuples directly, but that is not the
757 * case if we had to insert any run-time coercions of the sub-select's
758 * output datatypes; anyway this avoids storing any resjunk columns
759 * that might be in the sub-select's output.) Run through the
760 * combining expressions to build tlists for the lefthand and
761 * righthand sides. We need both the ExprState list (for ExecProject)
762 * and the underlying parse Exprs (for ExecTypeFromTL).
764 * We also extract the combining operators themselves to initialize
765 * the equality and hashing functions for the hash tables.
767 if (IsA(node->testexpr->expr, OpExpr))
769 /* single combining operator */
770 oplist = list_make1(node->testexpr);
772 else if (and_clause((Node *) node->testexpr->expr))
774 /* multiple combining operators */
775 Assert(IsA(node->testexpr, BoolExprState));
776 oplist = ((BoolExprState *) node->testexpr)->args;
780 /* shouldn't see anything else in a hashable subplan */
781 elog(ERROR, "unrecognized testexpr type: %d",
782 (int) nodeTag(node->testexpr->expr));
783 oplist = NIL; /* keep compiler quiet */
785 Assert(list_length(oplist) == ncols);
787 lefttlist = righttlist = NIL;
788 leftptlist = rightptlist = NIL;
789 node->eqfunctions = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
790 node->hashfunctions = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
794 FuncExprState *fstate = (FuncExprState *) lfirst(l);
795 OpExpr *opexpr = (OpExpr *) fstate->xprstate.expr;
799 GenericExprState *tlestate;
802 Assert(IsA(fstate, FuncExprState));
803 Assert(IsA(opexpr, OpExpr));
804 Assert(list_length(fstate->args) == 2);
806 /* Process lefthand argument */
807 exstate = (ExprState *) linitial(fstate->args);
808 expr = exstate->expr;
809 tle = makeTargetEntry(expr,
813 tlestate = makeNode(GenericExprState);
814 tlestate->xprstate.expr = (Expr *) tle;
815 tlestate->xprstate.evalfunc = NULL;
816 tlestate->arg = exstate;
817 lefttlist = lappend(lefttlist, tlestate);
818 leftptlist = lappend(leftptlist, tle);
820 /* Process righthand argument */
821 exstate = (ExprState *) lsecond(fstate->args);
822 expr = exstate->expr;
823 tle = makeTargetEntry(expr,
827 tlestate = makeNode(GenericExprState);
828 tlestate->xprstate.expr = (Expr *) tle;
829 tlestate->xprstate.evalfunc = NULL;
830 tlestate->arg = exstate;
831 righttlist = lappend(righttlist, tlestate);
832 rightptlist = lappend(rightptlist, tle);
834 /* Lookup the combining function */
835 fmgr_info(opexpr->opfuncid, &node->eqfunctions[i - 1]);
836 node->eqfunctions[i - 1].fn_expr = (Node *) opexpr;
838 /* Lookup the associated hash function */
839 hashfn = get_op_hash_function(opexpr->opno);
840 if (!OidIsValid(hashfn))
841 elog(ERROR, "could not find hash function for hash operator %u",
843 fmgr_info(hashfn, &node->hashfunctions[i - 1]);
849 * Create a tupletable to hold these tuples. (Note: we never bother
850 * to free the tupletable explicitly; that's okay because it will
851 * never store raw disk tuples that might have associated buffer pins.
852 * The only resource involved is memory, which will be cleaned up by
853 * freeing the query context.)
855 tupTable = ExecCreateTupleTable(2);
858 * Construct tupdescs, slots and projection nodes for left and right
859 * sides. The lefthand expressions will be evaluated in the parent
860 * plan node's exprcontext, which we don't have access to here.
861 * Fortunately we can just pass NULL for now and fill it in later
862 * (hack alert!). The righthand expressions will be evaluated in our
865 tupDesc = ExecTypeFromTL(leftptlist, false);
866 slot = ExecAllocTableSlot(tupTable);
867 ExecSetSlotDescriptor(slot, tupDesc);
868 node->projLeft = ExecBuildProjectionInfo(lefttlist,
872 tupDesc = ExecTypeFromTL(rightptlist, false);
873 slot = ExecAllocTableSlot(tupTable);
874 ExecSetSlotDescriptor(slot, tupDesc);
875 node->projRight = ExecBuildProjectionInfo(righttlist,
881 /* ----------------------------------------------------------------
884 * Executes an InitPlan subplan and sets its output parameters.
886 * This is called from ExecEvalParam() when the value of a PARAM_EXEC
887 * parameter is requested and the param's execPlan field is set (indicating
888 * that the param has not yet been evaluated). This allows lazy evaluation
889 * of initplans: we don't run the subplan until/unless we need its output.
890 * Note that this routine MUST clear the execPlan fields of the plan's
891 * output parameters after evaluating them!
892 * ----------------------------------------------------------------
895 ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
897 SubPlan *subplan = (SubPlan *) node->xprstate.expr;
898 PlanState *planstate = node->planstate;
899 SubLinkType subLinkType = subplan->subLinkType;
900 MemoryContext oldcontext;
901 TupleTableSlot *slot;
904 ArrayBuildState *astate = NULL;
907 * Must switch to child query's per-query memory context.
909 oldcontext = MemoryContextSwitchTo(node->sub_estate->es_query_cxt);
911 if (subLinkType == ANY_SUBLINK ||
912 subLinkType == ALL_SUBLINK)
913 elog(ERROR, "ANY/ALL subselect unsupported as initplan");
915 if (planstate->chgParam != NULL)
916 ExecReScan(planstate, NULL);
918 for (slot = ExecProcNode(planstate);
920 slot = ExecProcNode(planstate))
922 TupleDesc tdesc = slot->tts_tupleDescriptor;
925 if (subLinkType == EXISTS_SUBLINK)
927 /* There can be only one param... */
928 int paramid = linitial_int(subplan->setParam);
929 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
931 prm->execPlan = NULL;
932 prm->value = BoolGetDatum(true);
938 if (subLinkType == ARRAY_SUBLINK)
944 /* stash away current value */
945 dvalue = slot_getattr(slot, 1, &disnull);
946 astate = accumArrayResult(astate, dvalue, disnull,
947 tdesc->attrs[0]->atttypid,
949 /* keep scanning subplan to collect all values */
954 (subLinkType == EXPR_SUBLINK ||
955 subLinkType == ROWCOMPARE_SUBLINK))
957 (errcode(ERRCODE_CARDINALITY_VIOLATION),
958 errmsg("more than one row returned by a subquery used as an expression")));
963 * We need to copy the subplan's tuple into our own context, in case
964 * any of the params are pass-by-ref type --- the pointers stored in
965 * the param structs will point at this copied tuple! node->curTuple
966 * keeps track of the copied tuple for eventual freeing.
968 MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
970 heap_freetuple(node->curTuple);
971 node->curTuple = ExecCopySlotTuple(slot);
972 MemoryContextSwitchTo(node->sub_estate->es_query_cxt);
975 * Now set all the setParam params from the columns of the tuple
977 foreach(l, subplan->setParam)
979 int paramid = lfirst_int(l);
980 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
982 prm->execPlan = NULL;
983 prm->value = heap_getattr(node->curTuple, i, tdesc,
991 if (subLinkType == EXISTS_SUBLINK)
993 /* There can be only one param... */
994 int paramid = linitial_int(subplan->setParam);
995 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
997 prm->execPlan = NULL;
998 prm->value = BoolGetDatum(false);
1003 foreach(l, subplan->setParam)
1005 int paramid = lfirst_int(l);
1006 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1008 prm->execPlan = NULL;
1009 prm->value = (Datum) 0;
1014 else if (subLinkType == ARRAY_SUBLINK)
1016 /* There can be only one param... */
1017 int paramid = linitial_int(subplan->setParam);
1018 ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1020 Assert(astate != NULL);
1021 prm->execPlan = NULL;
1022 /* We build the result in query context so it won't disappear */
1023 prm->value = makeArrayResult(astate, econtext->ecxt_per_query_memory);
1024 prm->isnull = false;
1027 MemoryContextSwitchTo(oldcontext);
1030 /* ----------------------------------------------------------------
1032 * ----------------------------------------------------------------
1035 ExecEndSubPlan(SubPlanState *node)
1037 if (node->needShutdown)
1039 MemoryContext oldcontext;
1041 oldcontext = MemoryContextSwitchTo(node->sub_estate->es_query_cxt);
1042 ExecEndPlan(node->planstate, node->sub_estate);
1043 MemoryContextSwitchTo(oldcontext);
1044 FreeExecutorState(node->sub_estate);
1045 node->sub_estate = NULL;
1046 node->planstate = NULL;
1047 node->needShutdown = false;
1052 * Mark an initplan as needing recalculation
1055 ExecReScanSetParamPlan(SubPlanState *node, PlanState *parent)
1057 PlanState *planstate = node->planstate;
1058 SubPlan *subplan = (SubPlan *) node->xprstate.expr;
1059 EState *estate = parent->state;
1063 if (subplan->parParam != NIL)
1064 elog(ERROR, "direct correlated subquery unsupported as initplan");
1065 if (subplan->setParam == NIL)
1066 elog(ERROR, "setParam list of initplan is empty");
1067 if (bms_is_empty(planstate->plan->extParam))
1068 elog(ERROR, "extParam set of initplan is empty");
1071 * Don't actually re-scan: ExecSetParamPlan does it if needed.
1075 * Mark this subplan's output parameters as needing recalculation
1077 foreach(l, subplan->setParam)
1079 int paramid = lfirst_int(l);
1080 ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
1082 prm->execPlan = node;
1083 parent->chgParam = bms_add_member(parent->chgParam, paramid);