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