1 /*-------------------------------------------------------------------------
2  *
3  * nodeSubplan.c
4  *	  routines to support sub-selects appearing in expressions
5  *
6  * This module is concerned with executing SubPlan expression nodes, which
7  * should not be confused with sub-SELECTs appearing in FROM.  SubPlans are
8  * divided into "initplans", which are those that need only one evaluation per
9  * query (among other restrictions, this requires that they don't use any
10  * direct correlation variables from the parent plan level), and "regular"
11  * subplans, which are re-evaluated every time their result is required.
12  *
13  *
14  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
15  * Portions Copyright (c) 1994, Regents of the University of California
16  *
17  * IDENTIFICATION
18  *	  src/backend/executor/nodeSubplan.c
19  *
20  *-------------------------------------------------------------------------
21  */
22 /*
23  *	 INTERFACE ROUTINES
24  *		ExecSubPlan  - process a subselect
25  *		ExecInitSubPlan - initialize a subselect
26  */
27 #include "postgres.h"
28 
29 #include <limits.h>
30 #include <math.h>
31 
32 #include "access/htup_details.h"
33 #include "executor/executor.h"
34 #include "executor/nodeSubplan.h"
35 #include "nodes/makefuncs.h"
36 #include "optimizer/clauses.h"
37 #include "utils/array.h"
38 #include "utils/lsyscache.h"
39 #include "utils/memutils.h"
40 
41 
42 static Datum ExecSubPlan(SubPlanState *node,
43 			ExprContext *econtext,
44 			bool *isNull,
45 			ExprDoneCond *isDone);
46 static Datum ExecAlternativeSubPlan(AlternativeSubPlanState *node,
47 					   ExprContext *econtext,
48 					   bool *isNull,
49 					   ExprDoneCond *isDone);
50 static Datum ExecHashSubPlan(SubPlanState *node,
51 				ExprContext *econtext,
52 				bool *isNull);
53 static Datum ExecScanSubPlan(SubPlanState *node,
54 				ExprContext *econtext,
55 				bool *isNull);
56 static void buildSubPlanHash(SubPlanState *node, ExprContext *econtext);
57 static bool findPartialMatch(TupleHashTable hashtable, TupleTableSlot *slot,
58 				 FmgrInfo *eqfunctions);
59 static bool slotAllNulls(TupleTableSlot *slot);
60 static bool slotNoNulls(TupleTableSlot *slot);
61 
62 
63 /* ----------------------------------------------------------------
64  *		ExecSubPlan
65  *
66  * This is the main entry point for execution of a regular SubPlan.
67  * ----------------------------------------------------------------
68  */
69 static Datum
ExecSubPlan(SubPlanState * node,ExprContext * econtext,bool * isNull,ExprDoneCond * isDone)70 ExecSubPlan(SubPlanState *node,
71 			ExprContext *econtext,
72 			bool *isNull,
73 			ExprDoneCond *isDone)
74 {
75 	SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
76 	EState	   *estate = node->planstate->state;
77 	ScanDirection dir = estate->es_direction;
78 	Datum		retval;
79 
80 	/* Set default values for result flags: non-null, not a set result */
81 	*isNull = false;
82 	if (isDone)
83 		*isDone = ExprSingleResult;
84 
85 	/* Sanity checks */
86 	if (subplan->subLinkType == CTE_SUBLINK)
87 		elog(ERROR, "CTE subplans should not be executed via ExecSubPlan");
88 	if (subplan->setParam != NIL && subplan->subLinkType != MULTIEXPR_SUBLINK)
89 		elog(ERROR, "cannot set parent params from subquery");
90 
91 	/* Force forward-scan mode for evaluation */
92 	estate->es_direction = ForwardScanDirection;
93 
94 	/* Select appropriate evaluation strategy */
95 	if (subplan->useHashTable)
96 		retval = ExecHashSubPlan(node, econtext, isNull);
97 	else
98 		retval = ExecScanSubPlan(node, econtext, isNull);
99 
100 	/* restore scan direction */
101 	estate->es_direction = dir;
102 
103 	return retval;
104 }
105 
106 /*
107  * ExecHashSubPlan: store subselect result in an in-memory hash table
108  */
109 static Datum
ExecHashSubPlan(SubPlanState * node,ExprContext * econtext,bool * isNull)110 ExecHashSubPlan(SubPlanState *node,
111 				ExprContext *econtext,
112 				bool *isNull)
113 {
114 	SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
115 	PlanState  *planstate = node->planstate;
116 	TupleTableSlot *slot;
117 
118 	/* Shouldn't have any direct correlation Vars */
119 	if (subplan->parParam != NIL || node->args != NIL)
120 		elog(ERROR, "hashed subplan with direct correlation not supported");
121 
122 	/*
123 	 * If first time through or we need to rescan the subplan, build the hash
124 	 * table.
125 	 */
126 	if (node->hashtable == NULL || planstate->chgParam != NULL)
127 		buildSubPlanHash(node, econtext);
128 
129 	/*
130 	 * The result for an empty subplan is always FALSE; no need to evaluate
131 	 * lefthand side.
132 	 */
133 	*isNull = false;
134 	if (!node->havehashrows && !node->havenullrows)
135 		return BoolGetDatum(false);
136 
137 	/*
138 	 * Evaluate lefthand expressions and form a projection tuple. First we
139 	 * have to set the econtext to use (hack alert!).
140 	 */
141 	node->projLeft->pi_exprContext = econtext;
142 	slot = ExecProject(node->projLeft, NULL);
143 
144 	/*
145 	 * Note: because we are typically called in a per-tuple context, we have
146 	 * to explicitly clear the projected tuple before returning. Otherwise,
147 	 * we'll have a double-free situation: the per-tuple context will probably
148 	 * be reset before we're called again, and then the tuple slot will think
149 	 * it still needs to free the tuple.
150 	 */
151 
152 	/*
153 	 * If the LHS is all non-null, probe for an exact match in the main hash
154 	 * table.  If we find one, the result is TRUE. Otherwise, scan the
155 	 * partly-null table to see if there are any rows that aren't provably
156 	 * unequal to the LHS; if so, the result is UNKNOWN.  (We skip that part
157 	 * if we don't care about UNKNOWN.) Otherwise, the result is FALSE.
158 	 *
159 	 * Note: the reason we can avoid a full scan of the main hash table is
160 	 * that the combining operators are assumed never to yield NULL when both
161 	 * inputs are non-null.  If they were to do so, we might need to produce
162 	 * UNKNOWN instead of FALSE because of an UNKNOWN result in comparing the
163 	 * LHS to some main-table entry --- which is a comparison we will not even
164 	 * make, unless there's a chance match of hash keys.
165 	 */
166 	if (slotNoNulls(slot))
167 	{
168 		if (node->havehashrows &&
169 			FindTupleHashEntry(node->hashtable,
170 							   slot,
171 							   node->cur_eq_funcs,
172 							   node->lhs_hash_funcs) != NULL)
173 		{
174 			ExecClearTuple(slot);
175 			return BoolGetDatum(true);
176 		}
177 		if (node->havenullrows &&
178 			findPartialMatch(node->hashnulls, slot, node->cur_eq_funcs))
179 		{
180 			ExecClearTuple(slot);
181 			*isNull = true;
182 			return BoolGetDatum(false);
183 		}
184 		ExecClearTuple(slot);
185 		return BoolGetDatum(false);
186 	}
187 
188 	/*
189 	 * When the LHS is partly or wholly NULL, we can never return TRUE. If we
190 	 * don't care about UNKNOWN, just return FALSE.  Otherwise, if the LHS is
191 	 * wholly NULL, immediately return UNKNOWN.  (Since the combining
192 	 * operators are strict, the result could only be FALSE if the sub-select
193 	 * were empty, but we already handled that case.) Otherwise, we must scan
194 	 * both the main and partly-null tables to see if there are any rows that
195 	 * aren't provably unequal to the LHS; if so, the result is UNKNOWN.
196 	 * Otherwise, the result is FALSE.
197 	 */
198 	if (node->hashnulls == NULL)
199 	{
200 		ExecClearTuple(slot);
201 		return BoolGetDatum(false);
202 	}
203 	if (slotAllNulls(slot))
204 	{
205 		ExecClearTuple(slot);
206 		*isNull = true;
207 		return BoolGetDatum(false);
208 	}
209 	/* Scan partly-null table first, since more likely to get a match */
210 	if (node->havenullrows &&
211 		findPartialMatch(node->hashnulls, slot, node->cur_eq_funcs))
212 	{
213 		ExecClearTuple(slot);
214 		*isNull = true;
215 		return BoolGetDatum(false);
216 	}
217 	if (node->havehashrows &&
218 		findPartialMatch(node->hashtable, slot, node->cur_eq_funcs))
219 	{
220 		ExecClearTuple(slot);
221 		*isNull = true;
222 		return BoolGetDatum(false);
223 	}
224 	ExecClearTuple(slot);
225 	return BoolGetDatum(false);
226 }
227 
228 /*
229  * ExecScanSubPlan: default case where we have to rescan subplan each time
230  */
231 static Datum
ExecScanSubPlan(SubPlanState * node,ExprContext * econtext,bool * isNull)232 ExecScanSubPlan(SubPlanState *node,
233 				ExprContext *econtext,
234 				bool *isNull)
235 {
236 	SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
237 	PlanState  *planstate = node->planstate;
238 	SubLinkType subLinkType = subplan->subLinkType;
239 	MemoryContext oldcontext;
240 	TupleTableSlot *slot;
241 	Datum		result;
242 	bool		found = false;	/* TRUE if got at least one subplan tuple */
243 	ListCell   *pvar;
244 	ListCell   *l;
245 	ArrayBuildStateAny *astate = NULL;
246 
247 	/*
248 	 * MULTIEXPR subplans, when "executed", just return NULL; but first we
249 	 * mark the subplan's output parameters as needing recalculation.  (This
250 	 * is a bit of a hack: it relies on the subplan appearing later in its
251 	 * targetlist than any of the referencing Params, so that all the Params
252 	 * have been evaluated before we re-mark them for the next evaluation
253 	 * cycle.  But in general resjunk tlist items appear after non-resjunk
254 	 * ones, so this should be safe.)  Unlike ExecReScanSetParamPlan, we do
255 	 * *not* set bits in the parent plan node's chgParam, because we don't
256 	 * want to cause a rescan of the parent.
257 	 */
258 	if (subLinkType == MULTIEXPR_SUBLINK)
259 	{
260 		EState	   *estate = node->parent->state;
261 
262 		foreach(l, subplan->setParam)
263 		{
264 			int			paramid = lfirst_int(l);
265 			ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
266 
267 			prm->execPlan = node;
268 		}
269 		*isNull = true;
270 		return (Datum) 0;
271 	}
272 
273 	/* Initialize ArrayBuildStateAny in caller's context, if needed */
274 	if (subLinkType == ARRAY_SUBLINK)
275 		astate = initArrayResultAny(subplan->firstColType,
276 									CurrentMemoryContext, true);
277 
278 	/*
279 	 * We are probably in a short-lived expression-evaluation context. Switch
280 	 * to the per-query context for manipulating the child plan's chgParam,
281 	 * calling ExecProcNode on it, etc.
282 	 */
283 	oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
284 
285 	/*
286 	 * Set Params of this plan from parent plan correlation values. (Any
287 	 * calculation we have to do is done in the parent econtext, since the
288 	 * Param values don't need to have per-query lifetime.)
289 	 */
290 	Assert(list_length(subplan->parParam) == list_length(node->args));
291 
292 	forboth(l, subplan->parParam, pvar, node->args)
293 	{
294 		int			paramid = lfirst_int(l);
295 		ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
296 
297 		prm->value = ExecEvalExprSwitchContext((ExprState *) lfirst(pvar),
298 											   econtext,
299 											   &(prm->isnull),
300 											   NULL);
301 		planstate->chgParam = bms_add_member(planstate->chgParam, paramid);
302 	}
303 
304 	/*
305 	 * Now that we've set up its parameters, we can reset the subplan.
306 	 */
307 	ExecReScan(planstate);
308 
309 	/*
310 	 * For all sublink types except EXPR_SUBLINK and ARRAY_SUBLINK, the result
311 	 * is boolean as are the results of the combining operators. We combine
312 	 * results across tuples (if the subplan produces more than one) using OR
313 	 * semantics for ANY_SUBLINK or AND semantics for ALL_SUBLINK.
314 	 * (ROWCOMPARE_SUBLINK doesn't allow multiple tuples from the subplan.)
315 	 * NULL results from the combining operators are handled according to the
316 	 * usual SQL semantics for OR and AND.  The result for no input tuples is
317 	 * FALSE for ANY_SUBLINK, TRUE for ALL_SUBLINK, NULL for
318 	 * ROWCOMPARE_SUBLINK.
319 	 *
320 	 * For EXPR_SUBLINK we require the subplan to produce no more than one
321 	 * tuple, else an error is raised.  If zero tuples are produced, we return
322 	 * NULL.  Assuming we get a tuple, we just use its first column (there can
323 	 * be only one non-junk column in this case).
324 	 *
325 	 * For ARRAY_SUBLINK we allow the subplan to produce any number of tuples,
326 	 * and form an array of the first column's values.  Note in particular
327 	 * that we produce a zero-element array if no tuples are produced (this is
328 	 * a change from pre-8.3 behavior of returning NULL).
329 	 */
330 	result = BoolGetDatum(subLinkType == ALL_SUBLINK);
331 	*isNull = false;
332 
333 	for (slot = ExecProcNode(planstate);
334 		 !TupIsNull(slot);
335 		 slot = ExecProcNode(planstate))
336 	{
337 		TupleDesc	tdesc = slot->tts_tupleDescriptor;
338 		Datum		rowresult;
339 		bool		rownull;
340 		int			col;
341 		ListCell   *plst;
342 
343 		if (subLinkType == EXISTS_SUBLINK)
344 		{
345 			found = true;
346 			result = BoolGetDatum(true);
347 			break;
348 		}
349 
350 		if (subLinkType == EXPR_SUBLINK)
351 		{
352 			/* cannot allow multiple input tuples for EXPR sublink */
353 			if (found)
354 				ereport(ERROR,
355 						(errcode(ERRCODE_CARDINALITY_VIOLATION),
356 						 errmsg("more than one row returned by a subquery used as an expression")));
357 			found = true;
358 
359 			/*
360 			 * We need to copy the subplan's tuple in case the result is of
361 			 * pass-by-ref type --- our return value will point into this
362 			 * copied tuple!  Can't use the subplan's instance of the tuple
363 			 * since it won't still be valid after next ExecProcNode() call.
364 			 * node->curTuple keeps track of the copied tuple for eventual
365 			 * freeing.
366 			 */
367 			if (node->curTuple)
368 				heap_freetuple(node->curTuple);
369 			node->curTuple = ExecCopySlotTuple(slot);
370 
371 			result = heap_getattr(node->curTuple, 1, tdesc, isNull);
372 			/* keep scanning subplan to make sure there's only one tuple */
373 			continue;
374 		}
375 
376 		if (subLinkType == ARRAY_SUBLINK)
377 		{
378 			Datum		dvalue;
379 			bool		disnull;
380 
381 			found = true;
382 			/* stash away current value */
383 			Assert(subplan->firstColType == tdesc->attrs[0]->atttypid);
384 			dvalue = slot_getattr(slot, 1, &disnull);
385 			astate = accumArrayResultAny(astate, dvalue, disnull,
386 										 subplan->firstColType, oldcontext);
387 			/* keep scanning subplan to collect all values */
388 			continue;
389 		}
390 
391 		/* cannot allow multiple input tuples for ROWCOMPARE sublink either */
392 		if (subLinkType == ROWCOMPARE_SUBLINK && found)
393 			ereport(ERROR,
394 					(errcode(ERRCODE_CARDINALITY_VIOLATION),
395 					 errmsg("more than one row returned by a subquery used as an expression")));
396 
397 		found = true;
398 
399 		/*
400 		 * For ALL, ANY, and ROWCOMPARE sublinks, load up the Params
401 		 * representing the columns of the sub-select, and then evaluate the
402 		 * combining expression.
403 		 */
404 		col = 1;
405 		foreach(plst, subplan->paramIds)
406 		{
407 			int			paramid = lfirst_int(plst);
408 			ParamExecData *prmdata;
409 
410 			prmdata = &(econtext->ecxt_param_exec_vals[paramid]);
411 			Assert(prmdata->execPlan == NULL);
412 			prmdata->value = slot_getattr(slot, col, &(prmdata->isnull));
413 			col++;
414 		}
415 
416 		rowresult = ExecEvalExprSwitchContext(node->testexpr, econtext,
417 											  &rownull, NULL);
418 
419 		if (subLinkType == ANY_SUBLINK)
420 		{
421 			/* combine across rows per OR semantics */
422 			if (rownull)
423 				*isNull = true;
424 			else if (DatumGetBool(rowresult))
425 			{
426 				result = BoolGetDatum(true);
427 				*isNull = false;
428 				break;			/* needn't look at any more rows */
429 			}
430 		}
431 		else if (subLinkType == ALL_SUBLINK)
432 		{
433 			/* combine across rows per AND semantics */
434 			if (rownull)
435 				*isNull = true;
436 			else if (!DatumGetBool(rowresult))
437 			{
438 				result = BoolGetDatum(false);
439 				*isNull = false;
440 				break;			/* needn't look at any more rows */
441 			}
442 		}
443 		else
444 		{
445 			/* must be ROWCOMPARE_SUBLINK */
446 			result = rowresult;
447 			*isNull = rownull;
448 		}
449 	}
450 
451 	MemoryContextSwitchTo(oldcontext);
452 
453 	if (subLinkType == ARRAY_SUBLINK)
454 	{
455 		/* We return the result in the caller's context */
456 		result = makeArrayResultAny(astate, oldcontext, true);
457 	}
458 	else if (!found)
459 	{
460 		/*
461 		 * deal with empty subplan result.  result/isNull were previously
462 		 * initialized correctly for all sublink types except EXPR and
463 		 * ROWCOMPARE; for those, return NULL.
464 		 */
465 		if (subLinkType == EXPR_SUBLINK ||
466 			subLinkType == ROWCOMPARE_SUBLINK)
467 		{
468 			result = (Datum) 0;
469 			*isNull = true;
470 		}
471 	}
472 
473 	return result;
474 }
475 
476 /*
477  * buildSubPlanHash: load hash table by scanning subplan output.
478  */
479 static void
buildSubPlanHash(SubPlanState * node,ExprContext * econtext)480 buildSubPlanHash(SubPlanState *node, ExprContext *econtext)
481 {
482 	SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
483 	PlanState  *planstate = node->planstate;
484 	int			ncols = node->numCols;
485 	ExprContext *innerecontext = node->innerecontext;
486 	MemoryContext oldcontext;
487 	long		nbuckets;
488 	TupleTableSlot *slot;
489 
490 	Assert(subplan->subLinkType == ANY_SUBLINK);
491 
492 	/*
493 	 * If we already had any hash tables, destroy 'em; then create empty hash
494 	 * table(s).
495 	 *
496 	 * If we need to distinguish accurately between FALSE and UNKNOWN (i.e.,
497 	 * NULL) results of the IN operation, then we have to store subplan output
498 	 * rows that are partly or wholly NULL.  We store such rows in a separate
499 	 * hash table that we expect will be much smaller than the main table. (We
500 	 * can use hashing to eliminate partly-null rows that are not distinct. We
501 	 * keep them separate to minimize the cost of the inevitable full-table
502 	 * searches; see findPartialMatch.)
503 	 *
504 	 * If it's not necessary to distinguish FALSE and UNKNOWN, then we don't
505 	 * need to store subplan output rows that contain NULL.
506 	 */
507 	MemoryContextReset(node->hashtablecxt);
508 	node->hashtable = NULL;
509 	node->hashnulls = NULL;
510 	node->havehashrows = false;
511 	node->havenullrows = false;
512 
513 	nbuckets = (long) Min(planstate->plan->plan_rows, (double) LONG_MAX);
514 	if (nbuckets < 1)
515 		nbuckets = 1;
516 
517 	node->hashtable = BuildTupleHashTable(ncols,
518 										  node->keyColIdx,
519 										  node->tab_eq_funcs,
520 										  node->tab_hash_funcs,
521 										  nbuckets,
522 										  sizeof(TupleHashEntryData),
523 										  node->hashtablecxt,
524 										  node->hashtempcxt);
525 
526 	if (!subplan->unknownEqFalse)
527 	{
528 		if (ncols == 1)
529 			nbuckets = 1;		/* there can only be one entry */
530 		else
531 		{
532 			nbuckets /= 16;
533 			if (nbuckets < 1)
534 				nbuckets = 1;
535 		}
536 		node->hashnulls = BuildTupleHashTable(ncols,
537 											  node->keyColIdx,
538 											  node->tab_eq_funcs,
539 											  node->tab_hash_funcs,
540 											  nbuckets,
541 											  sizeof(TupleHashEntryData),
542 											  node->hashtablecxt,
543 											  node->hashtempcxt);
544 	}
545 
546 	/*
547 	 * We are probably in a short-lived expression-evaluation context. Switch
548 	 * to the per-query context for manipulating the child plan.
549 	 */
550 	oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
551 
552 	/*
553 	 * Reset subplan to start.
554 	 */
555 	ExecReScan(planstate);
556 
557 	/*
558 	 * Scan the subplan and load the hash table(s).  Note that when there are
559 	 * duplicate rows coming out of the sub-select, only one copy is stored.
560 	 */
561 	for (slot = ExecProcNode(planstate);
562 		 !TupIsNull(slot);
563 		 slot = ExecProcNode(planstate))
564 	{
565 		int			col = 1;
566 		ListCell   *plst;
567 		bool		isnew;
568 
569 		/*
570 		 * Load up the Params representing the raw sub-select outputs, then
571 		 * form the projection tuple to store in the hashtable.
572 		 */
573 		foreach(plst, subplan->paramIds)
574 		{
575 			int			paramid = lfirst_int(plst);
576 			ParamExecData *prmdata;
577 
578 			prmdata = &(innerecontext->ecxt_param_exec_vals[paramid]);
579 			Assert(prmdata->execPlan == NULL);
580 			prmdata->value = slot_getattr(slot, col,
581 										  &(prmdata->isnull));
582 			col++;
583 		}
584 		slot = ExecProject(node->projRight, NULL);
585 
586 		/*
587 		 * If result contains any nulls, store separately or not at all.
588 		 */
589 		if (slotNoNulls(slot))
590 		{
591 			(void) LookupTupleHashEntry(node->hashtable, slot, &isnew);
592 			node->havehashrows = true;
593 		}
594 		else if (node->hashnulls)
595 		{
596 			(void) LookupTupleHashEntry(node->hashnulls, slot, &isnew);
597 			node->havenullrows = true;
598 		}
599 
600 		/*
601 		 * Reset innerecontext after each inner tuple to free any memory used
602 		 * during ExecProject.
603 		 */
604 		ResetExprContext(innerecontext);
605 	}
606 
607 	/*
608 	 * Since the projected tuples are in the sub-query's context and not the
609 	 * main context, we'd better clear the tuple slot before there's any
610 	 * chance of a reset of the sub-query's context.  Else we will have the
611 	 * potential for a double free attempt.  (XXX possibly no longer needed,
612 	 * but can't hurt.)
613 	 */
614 	ExecClearTuple(node->projRight->pi_slot);
615 
616 	MemoryContextSwitchTo(oldcontext);
617 }
618 
619 /*
620  * findPartialMatch: does the hashtable contain an entry that is not
621  * provably distinct from the tuple?
622  *
623  * We have to scan the whole hashtable; we can't usefully use hashkeys
624  * to guide probing, since we might get partial matches on tuples with
625  * hashkeys quite unrelated to what we'd get from the given tuple.
626  *
627  * Caller must provide the equality functions to use, since in cross-type
628  * cases these are different from the hashtable's internal functions.
629  */
630 static bool
findPartialMatch(TupleHashTable hashtable,TupleTableSlot * slot,FmgrInfo * eqfunctions)631 findPartialMatch(TupleHashTable hashtable, TupleTableSlot *slot,
632 				 FmgrInfo *eqfunctions)
633 {
634 	int			numCols = hashtable->numCols;
635 	AttrNumber *keyColIdx = hashtable->keyColIdx;
636 	TupleHashIterator hashiter;
637 	TupleHashEntry entry;
638 
639 	InitTupleHashIterator(hashtable, &hashiter);
640 	while ((entry = ScanTupleHashTable(&hashiter)) != NULL)
641 	{
642 		ExecStoreMinimalTuple(entry->firstTuple, hashtable->tableslot, false);
643 		if (!execTuplesUnequal(slot, hashtable->tableslot,
644 							   numCols, keyColIdx,
645 							   eqfunctions,
646 							   hashtable->tempcxt))
647 		{
648 			TermTupleHashIterator(&hashiter);
649 			return true;
650 		}
651 	}
652 	/* No TermTupleHashIterator call needed here */
653 	return false;
654 }
655 
656 /*
657  * slotAllNulls: is the slot completely NULL?
658  *
659  * This does not test for dropped columns, which is OK because we only
660  * use it on projected tuples.
661  */
662 static bool
slotAllNulls(TupleTableSlot * slot)663 slotAllNulls(TupleTableSlot *slot)
664 {
665 	int			ncols = slot->tts_tupleDescriptor->natts;
666 	int			i;
667 
668 	for (i = 1; i <= ncols; i++)
669 	{
670 		if (!slot_attisnull(slot, i))
671 			return false;
672 	}
673 	return true;
674 }
675 
676 /*
677  * slotNoNulls: is the slot entirely not NULL?
678  *
679  * This does not test for dropped columns, which is OK because we only
680  * use it on projected tuples.
681  */
682 static bool
slotNoNulls(TupleTableSlot * slot)683 slotNoNulls(TupleTableSlot *slot)
684 {
685 	int			ncols = slot->tts_tupleDescriptor->natts;
686 	int			i;
687 
688 	for (i = 1; i <= ncols; i++)
689 	{
690 		if (slot_attisnull(slot, i))
691 			return false;
692 	}
693 	return true;
694 }
695 
696 /* ----------------------------------------------------------------
697  *		ExecInitSubPlan
698  *
699  * Create a SubPlanState for a SubPlan; this is the SubPlan-specific part
700  * of ExecInitExpr().  We split it out so that it can be used for InitPlans
701  * as well as regular SubPlans.  Note that we don't link the SubPlan into
702  * the parent's subPlan list, because that shouldn't happen for InitPlans.
703  * Instead, ExecInitExpr() does that one part.
704  * ----------------------------------------------------------------
705  */
706 SubPlanState *
ExecInitSubPlan(SubPlan * subplan,PlanState * parent)707 ExecInitSubPlan(SubPlan *subplan, PlanState *parent)
708 {
709 	SubPlanState *sstate = makeNode(SubPlanState);
710 	EState	   *estate = parent->state;
711 
712 	sstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecSubPlan;
713 	sstate->xprstate.expr = (Expr *) subplan;
714 
715 	/* Link the SubPlanState to already-initialized subplan */
716 	sstate->planstate = (PlanState *) list_nth(estate->es_subplanstates,
717 											   subplan->plan_id - 1);
718 
719 	/* ... and to its parent's state */
720 	sstate->parent = parent;
721 
722 	/* Initialize subexpressions */
723 	sstate->testexpr = ExecInitExpr((Expr *) subplan->testexpr, parent);
724 	sstate->args = (List *) ExecInitExpr((Expr *) subplan->args, parent);
725 
726 	/*
727 	 * initialize my state
728 	 */
729 	sstate->curTuple = NULL;
730 	sstate->curArray = PointerGetDatum(NULL);
731 	sstate->projLeft = NULL;
732 	sstate->projRight = NULL;
733 	sstate->hashtable = NULL;
734 	sstate->hashnulls = NULL;
735 	sstate->hashtablecxt = NULL;
736 	sstate->hashtempcxt = NULL;
737 	sstate->innerecontext = NULL;
738 	sstate->keyColIdx = NULL;
739 	sstate->tab_hash_funcs = NULL;
740 	sstate->tab_eq_funcs = NULL;
741 	sstate->lhs_hash_funcs = NULL;
742 	sstate->cur_eq_funcs = NULL;
743 
744 	/*
745 	 * If this is an initplan or MULTIEXPR subplan, it has output parameters
746 	 * that the parent plan will use, so mark those parameters as needing
747 	 * evaluation.  We don't actually run the subplan until we first need one
748 	 * of its outputs.
749 	 *
750 	 * A CTE subplan's output parameter is never to be evaluated in the normal
751 	 * way, so skip this in that case.
752 	 *
753 	 * Note that we don't set parent->chgParam here: the parent plan hasn't
754 	 * been run yet, so no need to force it to re-run.
755 	 */
756 	if (subplan->setParam != NIL && subplan->subLinkType != CTE_SUBLINK)
757 	{
758 		ListCell   *lst;
759 
760 		foreach(lst, subplan->setParam)
761 		{
762 			int			paramid = lfirst_int(lst);
763 			ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
764 
765 			prm->execPlan = sstate;
766 		}
767 	}
768 
769 	/*
770 	 * If we are going to hash the subquery output, initialize relevant stuff.
771 	 * (We don't create the hashtable until needed, though.)
772 	 */
773 	if (subplan->useHashTable)
774 	{
775 		int			ncols,
776 					i;
777 		TupleDesc	tupDesc;
778 		TupleTableSlot *slot;
779 		List	   *oplist,
780 				   *lefttlist,
781 				   *righttlist,
782 				   *leftptlist,
783 				   *rightptlist;
784 		ListCell   *l;
785 
786 		/* We need a memory context to hold the hash table(s) */
787 		sstate->hashtablecxt =
788 			AllocSetContextCreate(CurrentMemoryContext,
789 								  "Subplan HashTable Context",
790 								  ALLOCSET_DEFAULT_SIZES);
791 		/* and a small one for the hash tables to use as temp storage */
792 		sstate->hashtempcxt =
793 			AllocSetContextCreate(CurrentMemoryContext,
794 								  "Subplan HashTable Temp Context",
795 								  ALLOCSET_SMALL_SIZES);
796 		/* and a short-lived exprcontext for function evaluation */
797 		sstate->innerecontext = CreateExprContext(estate);
798 
799 		/*
800 		 * We use ExecProject to evaluate the lefthand and righthand
801 		 * expression lists and form tuples.  (You might think that we could
802 		 * use the sub-select's output tuples directly, but that is not the
803 		 * case if we had to insert any run-time coercions of the sub-select's
804 		 * output datatypes; anyway this avoids storing any resjunk columns
805 		 * that might be in the sub-select's output.) Run through the
806 		 * combining expressions to build tlists for the lefthand and
807 		 * righthand sides.  We need both the ExprState list (for ExecProject)
808 		 * and the underlying parse Exprs (for ExecTypeFromTL).
809 		 *
810 		 * We also extract the combining operators themselves to initialize
811 		 * the equality and hashing functions for the hash tables.
812 		 */
813 		if (IsA(sstate->testexpr->expr, OpExpr))
814 		{
815 			/* single combining operator */
816 			oplist = list_make1(sstate->testexpr);
817 		}
818 		else if (and_clause((Node *) sstate->testexpr->expr))
819 		{
820 			/* multiple combining operators */
821 			Assert(IsA(sstate->testexpr, BoolExprState));
822 			oplist = ((BoolExprState *) sstate->testexpr)->args;
823 		}
824 		else
825 		{
826 			/* shouldn't see anything else in a hashable subplan */
827 			elog(ERROR, "unrecognized testexpr type: %d",
828 				 (int) nodeTag(sstate->testexpr->expr));
829 			oplist = NIL;		/* keep compiler quiet */
830 		}
831 		ncols = list_length(oplist);
832 
833 		lefttlist = righttlist = NIL;
834 		leftptlist = rightptlist = NIL;
835 		sstate->numCols = ncols;
836 		sstate->keyColIdx = (AttrNumber *) palloc(ncols * sizeof(AttrNumber));
837 		sstate->tab_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
838 		sstate->tab_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
839 		sstate->lhs_hash_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
840 		sstate->cur_eq_funcs = (FmgrInfo *) palloc(ncols * sizeof(FmgrInfo));
841 		i = 1;
842 		foreach(l, oplist)
843 		{
844 			FuncExprState *fstate = (FuncExprState *) lfirst(l);
845 			OpExpr	   *opexpr = (OpExpr *) fstate->xprstate.expr;
846 			ExprState  *exstate;
847 			Expr	   *expr;
848 			TargetEntry *tle;
849 			GenericExprState *tlestate;
850 			Oid			rhs_eq_oper;
851 			Oid			left_hashfn;
852 			Oid			right_hashfn;
853 
854 			Assert(IsA(fstate, FuncExprState));
855 			Assert(IsA(opexpr, OpExpr));
856 			Assert(list_length(fstate->args) == 2);
857 
858 			/* Process lefthand argument */
859 			exstate = (ExprState *) linitial(fstate->args);
860 			expr = exstate->expr;
861 			tle = makeTargetEntry(expr,
862 								  i,
863 								  NULL,
864 								  false);
865 			tlestate = makeNode(GenericExprState);
866 			tlestate->xprstate.expr = (Expr *) tle;
867 			tlestate->xprstate.evalfunc = NULL;
868 			tlestate->arg = exstate;
869 			lefttlist = lappend(lefttlist, tlestate);
870 			leftptlist = lappend(leftptlist, tle);
871 
872 			/* Process righthand argument */
873 			exstate = (ExprState *) lsecond(fstate->args);
874 			expr = exstate->expr;
875 			tle = makeTargetEntry(expr,
876 								  i,
877 								  NULL,
878 								  false);
879 			tlestate = makeNode(GenericExprState);
880 			tlestate->xprstate.expr = (Expr *) tle;
881 			tlestate->xprstate.evalfunc = NULL;
882 			tlestate->arg = exstate;
883 			righttlist = lappend(righttlist, tlestate);
884 			rightptlist = lappend(rightptlist, tle);
885 
886 			/* Lookup the equality function (potentially cross-type) */
887 			fmgr_info(opexpr->opfuncid, &sstate->cur_eq_funcs[i - 1]);
888 			fmgr_info_set_expr((Node *) opexpr, &sstate->cur_eq_funcs[i - 1]);
889 
890 			/* Look up the equality function for the RHS type */
891 			if (!get_compatible_hash_operators(opexpr->opno,
892 											   NULL, &rhs_eq_oper))
893 				elog(ERROR, "could not find compatible hash operator for operator %u",
894 					 opexpr->opno);
895 			fmgr_info(get_opcode(rhs_eq_oper), &sstate->tab_eq_funcs[i - 1]);
896 
897 			/* Lookup the associated hash functions */
898 			if (!get_op_hash_functions(opexpr->opno,
899 									   &left_hashfn, &right_hashfn))
900 				elog(ERROR, "could not find hash function for hash operator %u",
901 					 opexpr->opno);
902 			fmgr_info(left_hashfn, &sstate->lhs_hash_funcs[i - 1]);
903 			fmgr_info(right_hashfn, &sstate->tab_hash_funcs[i - 1]);
904 
905 			/* keyColIdx is just column numbers 1..n */
906 			sstate->keyColIdx[i - 1] = i;
907 
908 			i++;
909 		}
910 
911 		/*
912 		 * Construct tupdescs, slots and projection nodes for left and right
913 		 * sides.  The lefthand expressions will be evaluated in the parent
914 		 * plan node's exprcontext, which we don't have access to here.
915 		 * Fortunately we can just pass NULL for now and fill it in later
916 		 * (hack alert!).  The righthand expressions will be evaluated in our
917 		 * own innerecontext.
918 		 */
919 		tupDesc = ExecTypeFromTL(leftptlist, false);
920 		slot = ExecInitExtraTupleSlot(estate);
921 		ExecSetSlotDescriptor(slot, tupDesc);
922 		sstate->projLeft = ExecBuildProjectionInfo(lefttlist,
923 												   NULL,
924 												   slot,
925 												   NULL);
926 
927 		tupDesc = ExecTypeFromTL(rightptlist, false);
928 		slot = ExecInitExtraTupleSlot(estate);
929 		ExecSetSlotDescriptor(slot, tupDesc);
930 		sstate->projRight = ExecBuildProjectionInfo(righttlist,
931 													sstate->innerecontext,
932 													slot,
933 													NULL);
934 	}
935 
936 	return sstate;
937 }
938 
939 /* ----------------------------------------------------------------
940  *		ExecSetParamPlan
941  *
942  *		Executes a subplan and sets its output parameters.
943  *
944  * This is called from ExecEvalParamExec() when the value of a PARAM_EXEC
945  * parameter is requested and the param's execPlan field is set (indicating
946  * that the param has not yet been evaluated).  This allows lazy evaluation
947  * of initplans: we don't run the subplan until/unless we need its output.
948  * Note that this routine MUST clear the execPlan fields of the plan's
949  * output parameters after evaluating them!
950  *
951  * The results of this function are stored in the EState associated with the
952  * ExprContext (particularly, its ecxt_param_exec_vals); any pass-by-ref
953  * result Datums are allocated in the EState's per-query memory.  The passed
954  * econtext can be any ExprContext belonging to that EState; which one is
955  * important only to the extent that the ExprContext's per-tuple memory
956  * context is used to evaluate any parameters passed down to the subplan.
957  * (Thus in principle, the shorter-lived the ExprContext the better, since
958  * that data isn't needed after we return.  In practice, because initplan
959  * parameters are never more complex than Vars, Aggrefs, etc, evaluating them
960  * currently never leaks any memory anyway.)
961  * ----------------------------------------------------------------
962  */
963 void
ExecSetParamPlan(SubPlanState * node,ExprContext * econtext)964 ExecSetParamPlan(SubPlanState *node, ExprContext *econtext)
965 {
966 	SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
967 	PlanState  *planstate = node->planstate;
968 	SubLinkType subLinkType = subplan->subLinkType;
969 	EState	   *estate = planstate->state;
970 	ScanDirection dir = estate->es_direction;
971 	MemoryContext oldcontext;
972 	TupleTableSlot *slot;
973 	ListCell   *pvar;
974 	ListCell   *l;
975 	bool		found = false;
976 	ArrayBuildStateAny *astate = NULL;
977 
978 	if (subLinkType == ANY_SUBLINK ||
979 		subLinkType == ALL_SUBLINK)
980 		elog(ERROR, "ANY/ALL subselect unsupported as initplan");
981 	if (subLinkType == CTE_SUBLINK)
982 		elog(ERROR, "CTE subplans should not be executed via ExecSetParamPlan");
983 
984 	/*
985 	 * Enforce forward scan direction regardless of caller. It's hard but not
986 	 * impossible to get here in backward scan, so make it work anyway.
987 	 */
988 	estate->es_direction = ForwardScanDirection;
989 
990 	/* Initialize ArrayBuildStateAny in caller's context, if needed */
991 	if (subLinkType == ARRAY_SUBLINK)
992 		astate = initArrayResultAny(subplan->firstColType,
993 									CurrentMemoryContext, true);
994 
995 	/*
996 	 * Must switch to per-query memory context.
997 	 */
998 	oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory);
999 
1000 	/*
1001 	 * Set Params of this plan from parent plan correlation values. (Any
1002 	 * calculation we have to do is done in the parent econtext, since the
1003 	 * Param values don't need to have per-query lifetime.)  Currently, we
1004 	 * expect only MULTIEXPR_SUBLINK plans to have any correlation values.
1005 	 */
1006 	Assert(subplan->parParam == NIL || subLinkType == MULTIEXPR_SUBLINK);
1007 	Assert(list_length(subplan->parParam) == list_length(node->args));
1008 
1009 	forboth(l, subplan->parParam, pvar, node->args)
1010 	{
1011 		int			paramid = lfirst_int(l);
1012 		ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1013 
1014 		prm->value = ExecEvalExprSwitchContext((ExprState *) lfirst(pvar),
1015 											   econtext,
1016 											   &(prm->isnull),
1017 											   NULL);
1018 		planstate->chgParam = bms_add_member(planstate->chgParam, paramid);
1019 	}
1020 
1021 	/*
1022 	 * Run the plan.  (If it needs to be rescanned, the first ExecProcNode
1023 	 * call will take care of that.)
1024 	 */
1025 	for (slot = ExecProcNode(planstate);
1026 		 !TupIsNull(slot);
1027 		 slot = ExecProcNode(planstate))
1028 	{
1029 		TupleDesc	tdesc = slot->tts_tupleDescriptor;
1030 		int			i = 1;
1031 
1032 		if (subLinkType == EXISTS_SUBLINK)
1033 		{
1034 			/* There can be only one setParam... */
1035 			int			paramid = linitial_int(subplan->setParam);
1036 			ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1037 
1038 			prm->execPlan = NULL;
1039 			prm->value = BoolGetDatum(true);
1040 			prm->isnull = false;
1041 			found = true;
1042 			break;
1043 		}
1044 
1045 		if (subLinkType == ARRAY_SUBLINK)
1046 		{
1047 			Datum		dvalue;
1048 			bool		disnull;
1049 
1050 			found = true;
1051 			/* stash away current value */
1052 			Assert(subplan->firstColType == tdesc->attrs[0]->atttypid);
1053 			dvalue = slot_getattr(slot, 1, &disnull);
1054 			astate = accumArrayResultAny(astate, dvalue, disnull,
1055 										 subplan->firstColType, oldcontext);
1056 			/* keep scanning subplan to collect all values */
1057 			continue;
1058 		}
1059 
1060 		if (found &&
1061 			(subLinkType == EXPR_SUBLINK ||
1062 			 subLinkType == MULTIEXPR_SUBLINK ||
1063 			 subLinkType == ROWCOMPARE_SUBLINK))
1064 			ereport(ERROR,
1065 					(errcode(ERRCODE_CARDINALITY_VIOLATION),
1066 					 errmsg("more than one row returned by a subquery used as an expression")));
1067 
1068 		found = true;
1069 
1070 		/*
1071 		 * We need to copy the subplan's tuple into our own context, in case
1072 		 * any of the params are pass-by-ref type --- the pointers stored in
1073 		 * the param structs will point at this copied tuple! node->curTuple
1074 		 * keeps track of the copied tuple for eventual freeing.
1075 		 */
1076 		if (node->curTuple)
1077 			heap_freetuple(node->curTuple);
1078 		node->curTuple = ExecCopySlotTuple(slot);
1079 
1080 		/*
1081 		 * Now set all the setParam params from the columns of the tuple
1082 		 */
1083 		foreach(l, subplan->setParam)
1084 		{
1085 			int			paramid = lfirst_int(l);
1086 			ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1087 
1088 			prm->execPlan = NULL;
1089 			prm->value = heap_getattr(node->curTuple, i, tdesc,
1090 									  &(prm->isnull));
1091 			i++;
1092 		}
1093 	}
1094 
1095 	if (subLinkType == ARRAY_SUBLINK)
1096 	{
1097 		/* There can be only one setParam... */
1098 		int			paramid = linitial_int(subplan->setParam);
1099 		ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1100 
1101 		/*
1102 		 * We build the result array in query context so it won't disappear;
1103 		 * to avoid leaking memory across repeated calls, we have to remember
1104 		 * the latest value, much as for curTuple above.
1105 		 */
1106 		if (node->curArray != PointerGetDatum(NULL))
1107 			pfree(DatumGetPointer(node->curArray));
1108 		node->curArray = makeArrayResultAny(astate,
1109 											econtext->ecxt_per_query_memory,
1110 											true);
1111 		prm->execPlan = NULL;
1112 		prm->value = node->curArray;
1113 		prm->isnull = false;
1114 	}
1115 	else if (!found)
1116 	{
1117 		if (subLinkType == EXISTS_SUBLINK)
1118 		{
1119 			/* There can be only one setParam... */
1120 			int			paramid = linitial_int(subplan->setParam);
1121 			ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1122 
1123 			prm->execPlan = NULL;
1124 			prm->value = BoolGetDatum(false);
1125 			prm->isnull = false;
1126 		}
1127 		else
1128 		{
1129 			/* For other sublink types, set all the output params to NULL */
1130 			foreach(l, subplan->setParam)
1131 			{
1132 				int			paramid = lfirst_int(l);
1133 				ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1134 
1135 				prm->execPlan = NULL;
1136 				prm->value = (Datum) 0;
1137 				prm->isnull = true;
1138 			}
1139 		}
1140 	}
1141 
1142 	MemoryContextSwitchTo(oldcontext);
1143 
1144 	/* restore scan direction */
1145 	estate->es_direction = dir;
1146 }
1147 
1148 /*
1149  * ExecSetParamPlanMulti
1150  *
1151  * Apply ExecSetParamPlan to evaluate any not-yet-evaluated initplan output
1152  * parameters whose ParamIDs are listed in "params".  Any listed params that
1153  * are not initplan outputs are ignored.
1154  *
1155  * As with ExecSetParamPlan, any ExprContext belonging to the current EState
1156  * can be used, but in principle a shorter-lived ExprContext is better than a
1157  * longer-lived one.
1158  */
1159 void
ExecSetParamPlanMulti(const Bitmapset * params,ExprContext * econtext)1160 ExecSetParamPlanMulti(const Bitmapset *params, ExprContext *econtext)
1161 {
1162 	int			paramid;
1163 
1164 	paramid = -1;
1165 	while ((paramid = bms_next_member(params, paramid)) >= 0)
1166 	{
1167 		ParamExecData *prm = &(econtext->ecxt_param_exec_vals[paramid]);
1168 
1169 		if (prm->execPlan != NULL)
1170 		{
1171 			/* Parameter not evaluated yet, so go do it */
1172 			ExecSetParamPlan(prm->execPlan, econtext);
1173 			/* ExecSetParamPlan should have processed this param... */
1174 			Assert(prm->execPlan == NULL);
1175 		}
1176 	}
1177 }
1178 
1179 /*
1180  * Mark an initplan as needing recalculation
1181  */
1182 void
ExecReScanSetParamPlan(SubPlanState * node,PlanState * parent)1183 ExecReScanSetParamPlan(SubPlanState *node, PlanState *parent)
1184 {
1185 	PlanState  *planstate = node->planstate;
1186 	SubPlan    *subplan = (SubPlan *) node->xprstate.expr;
1187 	EState	   *estate = parent->state;
1188 	ListCell   *l;
1189 
1190 	/* sanity checks */
1191 	if (subplan->parParam != NIL)
1192 		elog(ERROR, "direct correlated subquery unsupported as initplan");
1193 	if (subplan->setParam == NIL)
1194 		elog(ERROR, "setParam list of initplan is empty");
1195 	if (bms_is_empty(planstate->plan->extParam))
1196 		elog(ERROR, "extParam set of initplan is empty");
1197 
1198 	/*
1199 	 * Don't actually re-scan: it'll happen inside ExecSetParamPlan if needed.
1200 	 */
1201 
1202 	/*
1203 	 * Mark this subplan's output parameters as needing recalculation.
1204 	 *
1205 	 * CTE subplans are never executed via parameter recalculation; instead
1206 	 * they get run when called by nodeCtescan.c.  So don't mark the output
1207 	 * parameter of a CTE subplan as dirty, but do set the chgParam bit for it
1208 	 * so that dependent plan nodes will get told to rescan.
1209 	 */
1210 	foreach(l, subplan->setParam)
1211 	{
1212 		int			paramid = lfirst_int(l);
1213 		ParamExecData *prm = &(estate->es_param_exec_vals[paramid]);
1214 
1215 		if (subplan->subLinkType != CTE_SUBLINK)
1216 			prm->execPlan = node;
1217 
1218 		parent->chgParam = bms_add_member(parent->chgParam, paramid);
1219 	}
1220 }
1221 
1222 
1223 /*
1224  * ExecInitAlternativeSubPlan
1225  *
1226  * Initialize for execution of one of a set of alternative subplans.
1227  */
1228 AlternativeSubPlanState *
ExecInitAlternativeSubPlan(AlternativeSubPlan * asplan,PlanState * parent)1229 ExecInitAlternativeSubPlan(AlternativeSubPlan *asplan, PlanState *parent)
1230 {
1231 	AlternativeSubPlanState *asstate = makeNode(AlternativeSubPlanState);
1232 	double		num_calls;
1233 	SubPlan    *subplan1;
1234 	SubPlan    *subplan2;
1235 	Cost		cost1;
1236 	Cost		cost2;
1237 
1238 	asstate->xprstate.evalfunc = (ExprStateEvalFunc) ExecAlternativeSubPlan;
1239 	asstate->xprstate.expr = (Expr *) asplan;
1240 
1241 	/*
1242 	 * Initialize subplans.  (Can we get away with only initializing the one
1243 	 * we're going to use?)
1244 	 */
1245 	asstate->subplans = (List *) ExecInitExpr((Expr *) asplan->subplans,
1246 											  parent);
1247 
1248 	/*
1249 	 * Select the one to be used.  For this, we need an estimate of the number
1250 	 * of executions of the subplan.  We use the number of output rows
1251 	 * expected from the parent plan node.  This is a good estimate if we are
1252 	 * in the parent's targetlist, and an underestimate (but probably not by
1253 	 * more than a factor of 2) if we are in the qual.
1254 	 */
1255 	num_calls = parent->plan->plan_rows;
1256 
1257 	/*
1258 	 * The planner saved enough info so that we don't have to work very hard
1259 	 * to estimate the total cost, given the number-of-calls estimate.
1260 	 */
1261 	Assert(list_length(asplan->subplans) == 2);
1262 	subplan1 = (SubPlan *) linitial(asplan->subplans);
1263 	subplan2 = (SubPlan *) lsecond(asplan->subplans);
1264 
1265 	cost1 = subplan1->startup_cost + num_calls * subplan1->per_call_cost;
1266 	cost2 = subplan2->startup_cost + num_calls * subplan2->per_call_cost;
1267 
1268 	if (cost1 < cost2)
1269 		asstate->active = 0;
1270 	else
1271 		asstate->active = 1;
1272 
1273 	return asstate;
1274 }
1275 
1276 /*
1277  * ExecAlternativeSubPlan
1278  *
1279  * Execute one of a set of alternative subplans.
1280  *
1281  * Note: in future we might consider changing to different subplans on the
1282  * fly, in case the original rowcount estimate turns out to be way off.
1283  */
1284 static Datum
ExecAlternativeSubPlan(AlternativeSubPlanState * node,ExprContext * econtext,bool * isNull,ExprDoneCond * isDone)1285 ExecAlternativeSubPlan(AlternativeSubPlanState *node,
1286 					   ExprContext *econtext,
1287 					   bool *isNull,
1288 					   ExprDoneCond *isDone)
1289 {
1290 	/* Just pass control to the active subplan */
1291 	SubPlanState *activesp = (SubPlanState *) list_nth(node->subplans,
1292 													   node->active);
1293 
1294 	Assert(IsA(activesp, SubPlanState));
1295 
1296 	return ExecSubPlan(activesp,
1297 					   econtext,
1298 					   isNull,
1299 					   isDone);
1300 }
1301