nbtree: fix read page recheck typo.
[pgsql.git] / src / backend / executor / execProcnode.c
blob34f28dfeceae51eeeb8157e96bf0851b4dba43d7
1 /*-------------------------------------------------------------------------
3 * execProcnode.c
4 * contains dispatch functions which call the appropriate "initialize",
5 * "get a tuple", and "cleanup" routines for the given node type.
6 * If the node has children, then it will presumably call ExecInitNode,
7 * ExecProcNode, or ExecEndNode on its subnodes and do the appropriate
8 * processing.
10 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
11 * Portions Copyright (c) 1994, Regents of the University of California
14 * IDENTIFICATION
15 * src/backend/executor/execProcnode.c
17 *-------------------------------------------------------------------------
20 * NOTES
21 * This used to be three files. It is now all combined into
22 * one file so that it is easier to keep the dispatch routines
23 * in sync when new nodes are added.
25 * EXAMPLE
26 * Suppose we want the age of the manager of the shoe department and
27 * the number of employees in that department. So we have the query:
29 * select DEPT.no_emps, EMP.age
30 * from DEPT, EMP
31 * where EMP.name = DEPT.mgr and
32 * DEPT.name = "shoe"
34 * Suppose the planner gives us the following plan:
36 * Nest Loop (DEPT.mgr = EMP.name)
37 * / \
38 * / \
39 * Seq Scan Seq Scan
40 * DEPT EMP
41 * (name = "shoe")
43 * ExecutorStart() is called first.
44 * It calls InitPlan() which calls ExecInitNode() on
45 * the root of the plan -- the nest loop node.
47 * * ExecInitNode() notices that it is looking at a nest loop and
48 * as the code below demonstrates, it calls ExecInitNestLoop().
49 * Eventually this calls ExecInitNode() on the right and left subplans
50 * and so forth until the entire plan is initialized. The result
51 * of ExecInitNode() is a plan state tree built with the same structure
52 * as the underlying plan tree.
54 * * Then when ExecutorRun() is called, it calls ExecutePlan() which calls
55 * ExecProcNode() repeatedly on the top node of the plan state tree.
56 * Each time this happens, ExecProcNode() will end up calling
57 * ExecNestLoop(), which calls ExecProcNode() on its subplans.
58 * Each of these subplans is a sequential scan so ExecSeqScan() is
59 * called. The slots returned by ExecSeqScan() may contain
60 * tuples which contain the attributes ExecNestLoop() uses to
61 * form the tuples it returns.
63 * * Eventually ExecSeqScan() stops returning tuples and the nest
64 * loop join ends. Lastly, ExecutorEnd() calls ExecEndNode() which
65 * calls ExecEndNestLoop() which in turn calls ExecEndNode() on
66 * its subplans which result in ExecEndSeqScan().
68 * This should show how the executor works by having
69 * ExecInitNode(), ExecProcNode() and ExecEndNode() dispatch
70 * their work to the appropriate node support routines which may
71 * in turn call these routines themselves on their subplans.
73 #include "postgres.h"
75 #include "executor/executor.h"
76 #include "executor/nodeAgg.h"
77 #include "executor/nodeAppend.h"
78 #include "executor/nodeBitmapAnd.h"
79 #include "executor/nodeBitmapHeapscan.h"
80 #include "executor/nodeBitmapIndexscan.h"
81 #include "executor/nodeBitmapOr.h"
82 #include "executor/nodeCtescan.h"
83 #include "executor/nodeCustom.h"
84 #include "executor/nodeForeignscan.h"
85 #include "executor/nodeFunctionscan.h"
86 #include "executor/nodeGather.h"
87 #include "executor/nodeGatherMerge.h"
88 #include "executor/nodeGroup.h"
89 #include "executor/nodeHash.h"
90 #include "executor/nodeHashjoin.h"
91 #include "executor/nodeIncrementalSort.h"
92 #include "executor/nodeIndexonlyscan.h"
93 #include "executor/nodeIndexscan.h"
94 #include "executor/nodeLimit.h"
95 #include "executor/nodeLockRows.h"
96 #include "executor/nodeMaterial.h"
97 #include "executor/nodeMemoize.h"
98 #include "executor/nodeMergeAppend.h"
99 #include "executor/nodeMergejoin.h"
100 #include "executor/nodeModifyTable.h"
101 #include "executor/nodeNamedtuplestorescan.h"
102 #include "executor/nodeNestloop.h"
103 #include "executor/nodeProjectSet.h"
104 #include "executor/nodeRecursiveunion.h"
105 #include "executor/nodeResult.h"
106 #include "executor/nodeSamplescan.h"
107 #include "executor/nodeSeqscan.h"
108 #include "executor/nodeSetOp.h"
109 #include "executor/nodeSort.h"
110 #include "executor/nodeSubplan.h"
111 #include "executor/nodeSubqueryscan.h"
112 #include "executor/nodeTableFuncscan.h"
113 #include "executor/nodeTidrangescan.h"
114 #include "executor/nodeTidscan.h"
115 #include "executor/nodeUnique.h"
116 #include "executor/nodeValuesscan.h"
117 #include "executor/nodeWindowAgg.h"
118 #include "executor/nodeWorktablescan.h"
119 #include "miscadmin.h"
120 #include "nodes/nodeFuncs.h"
122 static TupleTableSlot *ExecProcNodeFirst(PlanState *node);
123 static TupleTableSlot *ExecProcNodeInstr(PlanState *node);
124 static bool ExecShutdownNode_walker(PlanState *node, void *context);
127 /* ------------------------------------------------------------------------
128 * ExecInitNode
130 * Recursively initializes all the nodes in the plan tree rooted
131 * at 'node'.
133 * Inputs:
134 * 'node' is the current node of the plan produced by the query planner
135 * 'estate' is the shared execution state for the plan tree
136 * 'eflags' is a bitwise OR of flag bits described in executor.h
138 * Returns a PlanState node corresponding to the given Plan node.
139 * ------------------------------------------------------------------------
141 PlanState *
142 ExecInitNode(Plan *node, EState *estate, int eflags)
144 PlanState *result;
145 List *subps;
146 ListCell *l;
149 * do nothing when we get to the end of a leaf on tree.
151 if (node == NULL)
152 return NULL;
155 * Make sure there's enough stack available. Need to check here, in
156 * addition to ExecProcNode() (via ExecProcNodeFirst()), to ensure the
157 * stack isn't overrun while initializing the node tree.
159 check_stack_depth();
161 switch (nodeTag(node))
164 * control nodes
166 case T_Result:
167 result = (PlanState *) ExecInitResult((Result *) node,
168 estate, eflags);
169 break;
171 case T_ProjectSet:
172 result = (PlanState *) ExecInitProjectSet((ProjectSet *) node,
173 estate, eflags);
174 break;
176 case T_ModifyTable:
177 result = (PlanState *) ExecInitModifyTable((ModifyTable *) node,
178 estate, eflags);
179 break;
181 case T_Append:
182 result = (PlanState *) ExecInitAppend((Append *) node,
183 estate, eflags);
184 break;
186 case T_MergeAppend:
187 result = (PlanState *) ExecInitMergeAppend((MergeAppend *) node,
188 estate, eflags);
189 break;
191 case T_RecursiveUnion:
192 result = (PlanState *) ExecInitRecursiveUnion((RecursiveUnion *) node,
193 estate, eflags);
194 break;
196 case T_BitmapAnd:
197 result = (PlanState *) ExecInitBitmapAnd((BitmapAnd *) node,
198 estate, eflags);
199 break;
201 case T_BitmapOr:
202 result = (PlanState *) ExecInitBitmapOr((BitmapOr *) node,
203 estate, eflags);
204 break;
207 * scan nodes
209 case T_SeqScan:
210 result = (PlanState *) ExecInitSeqScan((SeqScan *) node,
211 estate, eflags);
212 break;
214 case T_SampleScan:
215 result = (PlanState *) ExecInitSampleScan((SampleScan *) node,
216 estate, eflags);
217 break;
219 case T_IndexScan:
220 result = (PlanState *) ExecInitIndexScan((IndexScan *) node,
221 estate, eflags);
222 break;
224 case T_IndexOnlyScan:
225 result = (PlanState *) ExecInitIndexOnlyScan((IndexOnlyScan *) node,
226 estate, eflags);
227 break;
229 case T_BitmapIndexScan:
230 result = (PlanState *) ExecInitBitmapIndexScan((BitmapIndexScan *) node,
231 estate, eflags);
232 break;
234 case T_BitmapHeapScan:
235 result = (PlanState *) ExecInitBitmapHeapScan((BitmapHeapScan *) node,
236 estate, eflags);
237 break;
239 case T_TidScan:
240 result = (PlanState *) ExecInitTidScan((TidScan *) node,
241 estate, eflags);
242 break;
244 case T_TidRangeScan:
245 result = (PlanState *) ExecInitTidRangeScan((TidRangeScan *) node,
246 estate, eflags);
247 break;
249 case T_SubqueryScan:
250 result = (PlanState *) ExecInitSubqueryScan((SubqueryScan *) node,
251 estate, eflags);
252 break;
254 case T_FunctionScan:
255 result = (PlanState *) ExecInitFunctionScan((FunctionScan *) node,
256 estate, eflags);
257 break;
259 case T_TableFuncScan:
260 result = (PlanState *) ExecInitTableFuncScan((TableFuncScan *) node,
261 estate, eflags);
262 break;
264 case T_ValuesScan:
265 result = (PlanState *) ExecInitValuesScan((ValuesScan *) node,
266 estate, eflags);
267 break;
269 case T_CteScan:
270 result = (PlanState *) ExecInitCteScan((CteScan *) node,
271 estate, eflags);
272 break;
274 case T_NamedTuplestoreScan:
275 result = (PlanState *) ExecInitNamedTuplestoreScan((NamedTuplestoreScan *) node,
276 estate, eflags);
277 break;
279 case T_WorkTableScan:
280 result = (PlanState *) ExecInitWorkTableScan((WorkTableScan *) node,
281 estate, eflags);
282 break;
284 case T_ForeignScan:
285 result = (PlanState *) ExecInitForeignScan((ForeignScan *) node,
286 estate, eflags);
287 break;
289 case T_CustomScan:
290 result = (PlanState *) ExecInitCustomScan((CustomScan *) node,
291 estate, eflags);
292 break;
295 * join nodes
297 case T_NestLoop:
298 result = (PlanState *) ExecInitNestLoop((NestLoop *) node,
299 estate, eflags);
300 break;
302 case T_MergeJoin:
303 result = (PlanState *) ExecInitMergeJoin((MergeJoin *) node,
304 estate, eflags);
305 break;
307 case T_HashJoin:
308 result = (PlanState *) ExecInitHashJoin((HashJoin *) node,
309 estate, eflags);
310 break;
313 * materialization nodes
315 case T_Material:
316 result = (PlanState *) ExecInitMaterial((Material *) node,
317 estate, eflags);
318 break;
320 case T_Sort:
321 result = (PlanState *) ExecInitSort((Sort *) node,
322 estate, eflags);
323 break;
325 case T_IncrementalSort:
326 result = (PlanState *) ExecInitIncrementalSort((IncrementalSort *) node,
327 estate, eflags);
328 break;
330 case T_Memoize:
331 result = (PlanState *) ExecInitMemoize((Memoize *) node, estate,
332 eflags);
333 break;
335 case T_Group:
336 result = (PlanState *) ExecInitGroup((Group *) node,
337 estate, eflags);
338 break;
340 case T_Agg:
341 result = (PlanState *) ExecInitAgg((Agg *) node,
342 estate, eflags);
343 break;
345 case T_WindowAgg:
346 result = (PlanState *) ExecInitWindowAgg((WindowAgg *) node,
347 estate, eflags);
348 break;
350 case T_Unique:
351 result = (PlanState *) ExecInitUnique((Unique *) node,
352 estate, eflags);
353 break;
355 case T_Gather:
356 result = (PlanState *) ExecInitGather((Gather *) node,
357 estate, eflags);
358 break;
360 case T_GatherMerge:
361 result = (PlanState *) ExecInitGatherMerge((GatherMerge *) node,
362 estate, eflags);
363 break;
365 case T_Hash:
366 result = (PlanState *) ExecInitHash((Hash *) node,
367 estate, eflags);
368 break;
370 case T_SetOp:
371 result = (PlanState *) ExecInitSetOp((SetOp *) node,
372 estate, eflags);
373 break;
375 case T_LockRows:
376 result = (PlanState *) ExecInitLockRows((LockRows *) node,
377 estate, eflags);
378 break;
380 case T_Limit:
381 result = (PlanState *) ExecInitLimit((Limit *) node,
382 estate, eflags);
383 break;
385 default:
386 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
387 result = NULL; /* keep compiler quiet */
388 break;
391 ExecSetExecProcNode(result, result->ExecProcNode);
394 * Initialize any initPlans present in this node. The planner put them in
395 * a separate list for us.
397 * The defining characteristic of initplans is that they don't have
398 * arguments, so we don't need to evaluate them (in contrast to
399 * ExecInitSubPlanExpr()).
401 subps = NIL;
402 foreach(l, node->initPlan)
404 SubPlan *subplan = (SubPlan *) lfirst(l);
405 SubPlanState *sstate;
407 Assert(IsA(subplan, SubPlan));
408 Assert(subplan->args == NIL);
409 sstate = ExecInitSubPlan(subplan, result);
410 subps = lappend(subps, sstate);
412 result->initPlan = subps;
414 /* Set up instrumentation for this node if requested */
415 if (estate->es_instrument)
416 result->instrument = InstrAlloc(1, estate->es_instrument,
417 result->async_capable);
419 return result;
424 * If a node wants to change its ExecProcNode function after ExecInitNode()
425 * has finished, it should do so with this function. That way any wrapper
426 * functions can be reinstalled, without the node having to know how that
427 * works.
429 void
430 ExecSetExecProcNode(PlanState *node, ExecProcNodeMtd function)
433 * Add a wrapper around the ExecProcNode callback that checks stack depth
434 * during the first execution and maybe adds an instrumentation wrapper.
435 * When the callback is changed after execution has already begun that
436 * means we'll superfluously execute ExecProcNodeFirst, but that seems ok.
438 node->ExecProcNodeReal = function;
439 node->ExecProcNode = ExecProcNodeFirst;
444 * ExecProcNode wrapper that performs some one-time checks, before calling
445 * the relevant node method (possibly via an instrumentation wrapper).
447 static TupleTableSlot *
448 ExecProcNodeFirst(PlanState *node)
451 * Perform stack depth check during the first execution of the node. We
452 * only do so the first time round because it turns out to not be cheap on
453 * some common architectures (eg. x86). This relies on the assumption
454 * that ExecProcNode calls for a given plan node will always be made at
455 * roughly the same stack depth.
457 check_stack_depth();
460 * If instrumentation is required, change the wrapper to one that just
461 * does instrumentation. Otherwise we can dispense with all wrappers and
462 * have ExecProcNode() directly call the relevant function from now on.
464 if (node->instrument)
465 node->ExecProcNode = ExecProcNodeInstr;
466 else
467 node->ExecProcNode = node->ExecProcNodeReal;
469 return node->ExecProcNode(node);
474 * ExecProcNode wrapper that performs instrumentation calls. By keeping
475 * this a separate function, we avoid overhead in the normal case where
476 * no instrumentation is wanted.
478 static TupleTableSlot *
479 ExecProcNodeInstr(PlanState *node)
481 TupleTableSlot *result;
483 InstrStartNode(node->instrument);
485 result = node->ExecProcNodeReal(node);
487 InstrStopNode(node->instrument, TupIsNull(result) ? 0.0 : 1.0);
489 return result;
493 /* ----------------------------------------------------------------
494 * MultiExecProcNode
496 * Execute a node that doesn't return individual tuples
497 * (it might return a hashtable, bitmap, etc). Caller should
498 * check it got back the expected kind of Node.
500 * This has essentially the same responsibilities as ExecProcNode,
501 * but it does not do InstrStartNode/InstrStopNode (mainly because
502 * it can't tell how many returned tuples to count). Each per-node
503 * function must provide its own instrumentation support.
504 * ----------------------------------------------------------------
506 Node *
507 MultiExecProcNode(PlanState *node)
509 Node *result;
511 check_stack_depth();
513 CHECK_FOR_INTERRUPTS();
515 if (node->chgParam != NULL) /* something changed */
516 ExecReScan(node); /* let ReScan handle this */
518 switch (nodeTag(node))
521 * Only node types that actually support multiexec will be listed
524 case T_HashState:
525 result = MultiExecHash((HashState *) node);
526 break;
528 case T_BitmapIndexScanState:
529 result = MultiExecBitmapIndexScan((BitmapIndexScanState *) node);
530 break;
532 case T_BitmapAndState:
533 result = MultiExecBitmapAnd((BitmapAndState *) node);
534 break;
536 case T_BitmapOrState:
537 result = MultiExecBitmapOr((BitmapOrState *) node);
538 break;
540 default:
541 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
542 result = NULL;
543 break;
546 return result;
550 /* ----------------------------------------------------------------
551 * ExecEndNode
553 * Recursively cleans up all the nodes in the plan rooted
554 * at 'node'.
556 * After this operation, the query plan will not be able to be
557 * processed any further. This should be called only after
558 * the query plan has been fully executed.
559 * ----------------------------------------------------------------
561 void
562 ExecEndNode(PlanState *node)
565 * do nothing when we get to the end of a leaf on tree.
567 if (node == NULL)
568 return;
571 * Make sure there's enough stack available. Need to check here, in
572 * addition to ExecProcNode() (via ExecProcNodeFirst()), because it's not
573 * guaranteed that ExecProcNode() is reached for all nodes.
575 check_stack_depth();
577 if (node->chgParam != NULL)
579 bms_free(node->chgParam);
580 node->chgParam = NULL;
583 switch (nodeTag(node))
586 * control nodes
588 case T_ResultState:
589 ExecEndResult((ResultState *) node);
590 break;
592 case T_ProjectSetState:
593 ExecEndProjectSet((ProjectSetState *) node);
594 break;
596 case T_ModifyTableState:
597 ExecEndModifyTable((ModifyTableState *) node);
598 break;
600 case T_AppendState:
601 ExecEndAppend((AppendState *) node);
602 break;
604 case T_MergeAppendState:
605 ExecEndMergeAppend((MergeAppendState *) node);
606 break;
608 case T_RecursiveUnionState:
609 ExecEndRecursiveUnion((RecursiveUnionState *) node);
610 break;
612 case T_BitmapAndState:
613 ExecEndBitmapAnd((BitmapAndState *) node);
614 break;
616 case T_BitmapOrState:
617 ExecEndBitmapOr((BitmapOrState *) node);
618 break;
621 * scan nodes
623 case T_SeqScanState:
624 ExecEndSeqScan((SeqScanState *) node);
625 break;
627 case T_SampleScanState:
628 ExecEndSampleScan((SampleScanState *) node);
629 break;
631 case T_GatherState:
632 ExecEndGather((GatherState *) node);
633 break;
635 case T_GatherMergeState:
636 ExecEndGatherMerge((GatherMergeState *) node);
637 break;
639 case T_IndexScanState:
640 ExecEndIndexScan((IndexScanState *) node);
641 break;
643 case T_IndexOnlyScanState:
644 ExecEndIndexOnlyScan((IndexOnlyScanState *) node);
645 break;
647 case T_BitmapIndexScanState:
648 ExecEndBitmapIndexScan((BitmapIndexScanState *) node);
649 break;
651 case T_BitmapHeapScanState:
652 ExecEndBitmapHeapScan((BitmapHeapScanState *) node);
653 break;
655 case T_TidScanState:
656 ExecEndTidScan((TidScanState *) node);
657 break;
659 case T_TidRangeScanState:
660 ExecEndTidRangeScan((TidRangeScanState *) node);
661 break;
663 case T_SubqueryScanState:
664 ExecEndSubqueryScan((SubqueryScanState *) node);
665 break;
667 case T_FunctionScanState:
668 ExecEndFunctionScan((FunctionScanState *) node);
669 break;
671 case T_TableFuncScanState:
672 ExecEndTableFuncScan((TableFuncScanState *) node);
673 break;
675 case T_CteScanState:
676 ExecEndCteScan((CteScanState *) node);
677 break;
679 case T_ForeignScanState:
680 ExecEndForeignScan((ForeignScanState *) node);
681 break;
683 case T_CustomScanState:
684 ExecEndCustomScan((CustomScanState *) node);
685 break;
688 * join nodes
690 case T_NestLoopState:
691 ExecEndNestLoop((NestLoopState *) node);
692 break;
694 case T_MergeJoinState:
695 ExecEndMergeJoin((MergeJoinState *) node);
696 break;
698 case T_HashJoinState:
699 ExecEndHashJoin((HashJoinState *) node);
700 break;
703 * materialization nodes
705 case T_MaterialState:
706 ExecEndMaterial((MaterialState *) node);
707 break;
709 case T_SortState:
710 ExecEndSort((SortState *) node);
711 break;
713 case T_IncrementalSortState:
714 ExecEndIncrementalSort((IncrementalSortState *) node);
715 break;
717 case T_MemoizeState:
718 ExecEndMemoize((MemoizeState *) node);
719 break;
721 case T_GroupState:
722 ExecEndGroup((GroupState *) node);
723 break;
725 case T_AggState:
726 ExecEndAgg((AggState *) node);
727 break;
729 case T_WindowAggState:
730 ExecEndWindowAgg((WindowAggState *) node);
731 break;
733 case T_UniqueState:
734 ExecEndUnique((UniqueState *) node);
735 break;
737 case T_HashState:
738 ExecEndHash((HashState *) node);
739 break;
741 case T_SetOpState:
742 ExecEndSetOp((SetOpState *) node);
743 break;
745 case T_LockRowsState:
746 ExecEndLockRows((LockRowsState *) node);
747 break;
749 case T_LimitState:
750 ExecEndLimit((LimitState *) node);
751 break;
753 /* No clean up actions for these nodes. */
754 case T_ValuesScanState:
755 case T_NamedTuplestoreScanState:
756 case T_WorkTableScanState:
757 break;
759 default:
760 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
761 break;
766 * ExecShutdownNode
768 * Give execution nodes a chance to stop asynchronous resource consumption
769 * and release any resources still held.
771 void
772 ExecShutdownNode(PlanState *node)
774 (void) ExecShutdownNode_walker(node, NULL);
777 static bool
778 ExecShutdownNode_walker(PlanState *node, void *context)
780 if (node == NULL)
781 return false;
783 check_stack_depth();
786 * Treat the node as running while we shut it down, but only if it's run
787 * at least once already. We don't expect much CPU consumption during
788 * node shutdown, but in the case of Gather or Gather Merge, we may shut
789 * down workers at this stage. If so, their buffer usage will get
790 * propagated into pgBufferUsage at this point, and we want to make sure
791 * that it gets associated with the Gather node. We skip this if the node
792 * has never been executed, so as to avoid incorrectly making it appear
793 * that it has.
795 if (node->instrument && node->instrument->running)
796 InstrStartNode(node->instrument);
798 planstate_tree_walker(node, ExecShutdownNode_walker, context);
800 switch (nodeTag(node))
802 case T_GatherState:
803 ExecShutdownGather((GatherState *) node);
804 break;
805 case T_ForeignScanState:
806 ExecShutdownForeignScan((ForeignScanState *) node);
807 break;
808 case T_CustomScanState:
809 ExecShutdownCustomScan((CustomScanState *) node);
810 break;
811 case T_GatherMergeState:
812 ExecShutdownGatherMerge((GatherMergeState *) node);
813 break;
814 case T_HashState:
815 ExecShutdownHash((HashState *) node);
816 break;
817 case T_HashJoinState:
818 ExecShutdownHashJoin((HashJoinState *) node);
819 break;
820 default:
821 break;
824 /* Stop the node if we started it above, reporting 0 tuples. */
825 if (node->instrument && node->instrument->running)
826 InstrStopNode(node->instrument, 0);
828 return false;
832 * ExecSetTupleBound
834 * Set a tuple bound for a planstate node. This lets child plan nodes
835 * optimize based on the knowledge that the maximum number of tuples that
836 * their parent will demand is limited. The tuple bound for a node may
837 * only be changed between scans (i.e., after node initialization or just
838 * before an ExecReScan call).
840 * Any negative tuples_needed value means "no limit", which should be the
841 * default assumption when this is not called at all for a particular node.
843 * Note: if this is called repeatedly on a plan tree, the exact same set
844 * of nodes must be updated with the new limit each time; be careful that
845 * only unchanging conditions are tested here.
847 void
848 ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
851 * Since this function recurses, in principle we should check stack depth
852 * here. In practice, it's probably pointless since the earlier node
853 * initialization tree traversal would surely have consumed more stack.
856 if (IsA(child_node, SortState))
859 * If it is a Sort node, notify it that it can use bounded sort.
861 * Note: it is the responsibility of nodeSort.c to react properly to
862 * changes of these parameters. If we ever redesign this, it'd be a
863 * good idea to integrate this signaling with the parameter-change
864 * mechanism.
866 SortState *sortState = (SortState *) child_node;
868 if (tuples_needed < 0)
870 /* make sure flag gets reset if needed upon rescan */
871 sortState->bounded = false;
873 else
875 sortState->bounded = true;
876 sortState->bound = tuples_needed;
879 else if (IsA(child_node, IncrementalSortState))
882 * If it is an IncrementalSort node, notify it that it can use bounded
883 * sort.
885 * Note: it is the responsibility of nodeIncrementalSort.c to react
886 * properly to changes of these parameters. If we ever redesign this,
887 * it'd be a good idea to integrate this signaling with the
888 * parameter-change mechanism.
890 IncrementalSortState *sortState = (IncrementalSortState *) child_node;
892 if (tuples_needed < 0)
894 /* make sure flag gets reset if needed upon rescan */
895 sortState->bounded = false;
897 else
899 sortState->bounded = true;
900 sortState->bound = tuples_needed;
903 else if (IsA(child_node, AppendState))
906 * If it is an Append, we can apply the bound to any nodes that are
907 * children of the Append, since the Append surely need read no more
908 * than that many tuples from any one input.
910 AppendState *aState = (AppendState *) child_node;
911 int i;
913 for (i = 0; i < aState->as_nplans; i++)
914 ExecSetTupleBound(tuples_needed, aState->appendplans[i]);
916 else if (IsA(child_node, MergeAppendState))
919 * If it is a MergeAppend, we can apply the bound to any nodes that
920 * are children of the MergeAppend, since the MergeAppend surely need
921 * read no more than that many tuples from any one input.
923 MergeAppendState *maState = (MergeAppendState *) child_node;
924 int i;
926 for (i = 0; i < maState->ms_nplans; i++)
927 ExecSetTupleBound(tuples_needed, maState->mergeplans[i]);
929 else if (IsA(child_node, ResultState))
932 * Similarly, for a projecting Result, we can apply the bound to its
933 * child node.
935 * If Result supported qual checking, we'd have to punt on seeing a
936 * qual. Note that having a resconstantqual is not a showstopper: if
937 * that condition succeeds it affects nothing, while if it fails, no
938 * rows will be demanded from the Result child anyway.
940 if (outerPlanState(child_node))
941 ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
943 else if (IsA(child_node, SubqueryScanState))
946 * We can also descend through SubqueryScan, but only if it has no
947 * qual (otherwise it might discard rows).
949 SubqueryScanState *subqueryState = (SubqueryScanState *) child_node;
951 if (subqueryState->ss.ps.qual == NULL)
952 ExecSetTupleBound(tuples_needed, subqueryState->subplan);
954 else if (IsA(child_node, GatherState))
957 * A Gather node can propagate the bound to its workers. As with
958 * MergeAppend, no one worker could possibly need to return more
959 * tuples than the Gather itself needs to.
961 * Note: As with Sort, the Gather node is responsible for reacting
962 * properly to changes to this parameter.
964 GatherState *gstate = (GatherState *) child_node;
966 gstate->tuples_needed = tuples_needed;
968 /* Also pass down the bound to our own copy of the child plan */
969 ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
971 else if (IsA(child_node, GatherMergeState))
973 /* Same comments as for Gather */
974 GatherMergeState *gstate = (GatherMergeState *) child_node;
976 gstate->tuples_needed = tuples_needed;
978 ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
982 * In principle we could descend through any plan node type that is
983 * certain not to discard or combine input rows; but on seeing a node that
984 * can do that, we can't propagate the bound any further. For the moment
985 * it's unclear that any other cases are worth checking here.