PostgreSQL Source Code  git master
execProcnode.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
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.
9  *
10  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  *
14  * IDENTIFICATION
15  * src/backend/executor/execProcnode.c
16  *
17  *-------------------------------------------------------------------------
18  */
19 /*
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.
24  *
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:
28  *
29  * select DEPT.no_emps, EMP.age
30  * from DEPT, EMP
31  * where EMP.name = DEPT.mgr and
32  * DEPT.name = "shoe"
33  *
34  * Suppose the planner gives us the following plan:
35  *
36  * Nest Loop (DEPT.mgr = EMP.name)
37  * / \
38  * / \
39  * Seq Scan Seq Scan
40  * DEPT EMP
41  * (name = "shoe")
42  *
43  * ExecutorStart() is called first.
44  * It calls InitPlan() which calls ExecInitNode() on
45  * the root of the plan -- the nest loop node.
46  *
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.
53  *
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.
62  *
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().
67  *
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.
72  */
73 #include "postgres.h"
74 
75 #include "executor/executor.h"
76 #include "executor/nodeAgg.h"
77 #include "executor/nodeAppend.h"
78 #include "executor/nodeBitmapAnd.h"
81 #include "executor/nodeBitmapOr.h"
82 #include "executor/nodeCtescan.h"
83 #include "executor/nodeCustom.h"
86 #include "executor/nodeGather.h"
88 #include "executor/nodeGroup.h"
89 #include "executor/nodeHash.h"
90 #include "executor/nodeHashjoin.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"
99 #include "executor/nodeMergejoin.h"
102 #include "executor/nodeNestloop.h"
103 #include "executor/nodeProjectSet.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"
114 #include "executor/nodeTidscan.h"
115 #include "executor/nodeUnique.h"
116 #include "executor/nodeValuesscan.h"
117 #include "executor/nodeWindowAgg.h"
119 #include "miscadmin.h"
120 #include "nodes/nodeFuncs.h"
121 
124 static bool ExecShutdownNode_walker(PlanState *node, void *context);
125 
126 
127 /* ------------------------------------------------------------------------
128  * ExecInitNode
129  *
130  * Recursively initializes all the nodes in the plan tree rooted
131  * at 'node'.
132  *
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
137  *
138  * Returns a PlanState node corresponding to the given Plan node.
139  * ------------------------------------------------------------------------
140  */
141 PlanState *
142 ExecInitNode(Plan *node, EState *estate, int eflags)
143 {
144  PlanState *result;
145  List *subps;
146  ListCell *l;
147 
148  /*
149  * do nothing when we get to the end of a leaf on tree.
150  */
151  if (node == NULL)
152  return NULL;
153 
154  /*
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.
158  */
160 
161  switch (nodeTag(node))
162  {
163  /*
164  * control nodes
165  */
166  case T_Result:
167  result = (PlanState *) ExecInitResult((Result *) node,
168  estate, eflags);
169  break;
170 
171  case T_ProjectSet:
172  result = (PlanState *) ExecInitProjectSet((ProjectSet *) node,
173  estate, eflags);
174  break;
175 
176  case T_ModifyTable:
177  result = (PlanState *) ExecInitModifyTable((ModifyTable *) node,
178  estate, eflags);
179  break;
180 
181  case T_Append:
182  result = (PlanState *) ExecInitAppend((Append *) node,
183  estate, eflags);
184  break;
185 
186  case T_MergeAppend:
187  result = (PlanState *) ExecInitMergeAppend((MergeAppend *) node,
188  estate, eflags);
189  break;
190 
191  case T_RecursiveUnion:
192  result = (PlanState *) ExecInitRecursiveUnion((RecursiveUnion *) node,
193  estate, eflags);
194  break;
195 
196  case T_BitmapAnd:
197  result = (PlanState *) ExecInitBitmapAnd((BitmapAnd *) node,
198  estate, eflags);
199  break;
200 
201  case T_BitmapOr:
202  result = (PlanState *) ExecInitBitmapOr((BitmapOr *) node,
203  estate, eflags);
204  break;
205 
206  /*
207  * scan nodes
208  */
209  case T_SeqScan:
210  result = (PlanState *) ExecInitSeqScan((SeqScan *) node,
211  estate, eflags);
212  break;
213 
214  case T_SampleScan:
215  result = (PlanState *) ExecInitSampleScan((SampleScan *) node,
216  estate, eflags);
217  break;
218 
219  case T_IndexScan:
220  result = (PlanState *) ExecInitIndexScan((IndexScan *) node,
221  estate, eflags);
222  break;
223 
224  case T_IndexOnlyScan:
225  result = (PlanState *) ExecInitIndexOnlyScan((IndexOnlyScan *) node,
226  estate, eflags);
227  break;
228 
229  case T_BitmapIndexScan:
230  result = (PlanState *) ExecInitBitmapIndexScan((BitmapIndexScan *) node,
231  estate, eflags);
232  break;
233 
234  case T_BitmapHeapScan:
235  result = (PlanState *) ExecInitBitmapHeapScan((BitmapHeapScan *) node,
236  estate, eflags);
237  break;
238 
239  case T_TidScan:
240  result = (PlanState *) ExecInitTidScan((TidScan *) node,
241  estate, eflags);
242  break;
243 
244  case T_TidRangeScan:
245  result = (PlanState *) ExecInitTidRangeScan((TidRangeScan *) node,
246  estate, eflags);
247  break;
248 
249  case T_SubqueryScan:
250  result = (PlanState *) ExecInitSubqueryScan((SubqueryScan *) node,
251  estate, eflags);
252  break;
253 
254  case T_FunctionScan:
255  result = (PlanState *) ExecInitFunctionScan((FunctionScan *) node,
256  estate, eflags);
257  break;
258 
259  case T_TableFuncScan:
260  result = (PlanState *) ExecInitTableFuncScan((TableFuncScan *) node,
261  estate, eflags);
262  break;
263 
264  case T_ValuesScan:
265  result = (PlanState *) ExecInitValuesScan((ValuesScan *) node,
266  estate, eflags);
267  break;
268 
269  case T_CteScan:
270  result = (PlanState *) ExecInitCteScan((CteScan *) node,
271  estate, eflags);
272  break;
273 
274  case T_NamedTuplestoreScan:
276  estate, eflags);
277  break;
278 
279  case T_WorkTableScan:
280  result = (PlanState *) ExecInitWorkTableScan((WorkTableScan *) node,
281  estate, eflags);
282  break;
283 
284  case T_ForeignScan:
285  result = (PlanState *) ExecInitForeignScan((ForeignScan *) node,
286  estate, eflags);
287  break;
288 
289  case T_CustomScan:
290  result = (PlanState *) ExecInitCustomScan((CustomScan *) node,
291  estate, eflags);
292  break;
293 
294  /*
295  * join nodes
296  */
297  case T_NestLoop:
298  result = (PlanState *) ExecInitNestLoop((NestLoop *) node,
299  estate, eflags);
300  break;
301 
302  case T_MergeJoin:
303  result = (PlanState *) ExecInitMergeJoin((MergeJoin *) node,
304  estate, eflags);
305  break;
306 
307  case T_HashJoin:
308  result = (PlanState *) ExecInitHashJoin((HashJoin *) node,
309  estate, eflags);
310  break;
311 
312  /*
313  * materialization nodes
314  */
315  case T_Material:
316  result = (PlanState *) ExecInitMaterial((Material *) node,
317  estate, eflags);
318  break;
319 
320  case T_Sort:
321  result = (PlanState *) ExecInitSort((Sort *) node,
322  estate, eflags);
323  break;
324 
325  case T_IncrementalSort:
326  result = (PlanState *) ExecInitIncrementalSort((IncrementalSort *) node,
327  estate, eflags);
328  break;
329 
330  case T_Memoize:
331  result = (PlanState *) ExecInitMemoize((Memoize *) node, estate,
332  eflags);
333  break;
334 
335  case T_Group:
336  result = (PlanState *) ExecInitGroup((Group *) node,
337  estate, eflags);
338  break;
339 
340  case T_Agg:
341  result = (PlanState *) ExecInitAgg((Agg *) node,
342  estate, eflags);
343  break;
344 
345  case T_WindowAgg:
346  result = (PlanState *) ExecInitWindowAgg((WindowAgg *) node,
347  estate, eflags);
348  break;
349 
350  case T_Unique:
351  result = (PlanState *) ExecInitUnique((Unique *) node,
352  estate, eflags);
353  break;
354 
355  case T_Gather:
356  result = (PlanState *) ExecInitGather((Gather *) node,
357  estate, eflags);
358  break;
359 
360  case T_GatherMerge:
361  result = (PlanState *) ExecInitGatherMerge((GatherMerge *) node,
362  estate, eflags);
363  break;
364 
365  case T_Hash:
366  result = (PlanState *) ExecInitHash((Hash *) node,
367  estate, eflags);
368  break;
369 
370  case T_SetOp:
371  result = (PlanState *) ExecInitSetOp((SetOp *) node,
372  estate, eflags);
373  break;
374 
375  case T_LockRows:
376  result = (PlanState *) ExecInitLockRows((LockRows *) node,
377  estate, eflags);
378  break;
379 
380  case T_Limit:
381  result = (PlanState *) ExecInitLimit((Limit *) node,
382  estate, eflags);
383  break;
384 
385  default:
386  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
387  result = NULL; /* keep compiler quiet */
388  break;
389  }
390 
391  ExecSetExecProcNode(result, result->ExecProcNode);
392 
393  /*
394  * Initialize any initPlans present in this node. The planner put them in
395  * a separate list for us.
396  */
397  subps = NIL;
398  foreach(l, node->initPlan)
399  {
400  SubPlan *subplan = (SubPlan *) lfirst(l);
401  SubPlanState *sstate;
402 
403  Assert(IsA(subplan, SubPlan));
404  sstate = ExecInitSubPlan(subplan, result);
405  subps = lappend(subps, sstate);
406  }
407  result->initPlan = subps;
408 
409  /* Set up instrumentation for this node if requested */
410  if (estate->es_instrument)
411  result->instrument = InstrAlloc(1, estate->es_instrument,
412  result->async_capable);
413 
414  return result;
415 }
416 
417 
418 /*
419  * If a node wants to change its ExecProcNode function after ExecInitNode()
420  * has finished, it should do so with this function. That way any wrapper
421  * functions can be reinstalled, without the node having to know how that
422  * works.
423  */
424 void
426 {
427  /*
428  * Add a wrapper around the ExecProcNode callback that checks stack depth
429  * during the first execution and maybe adds an instrumentation wrapper.
430  * When the callback is changed after execution has already begun that
431  * means we'll superfluously execute ExecProcNodeFirst, but that seems ok.
432  */
433  node->ExecProcNodeReal = function;
435 }
436 
437 
438 /*
439  * ExecProcNode wrapper that performs some one-time checks, before calling
440  * the relevant node method (possibly via an instrumentation wrapper).
441  */
442 static TupleTableSlot *
444 {
445  /*
446  * Perform stack depth check during the first execution of the node. We
447  * only do so the first time round because it turns out to not be cheap on
448  * some common architectures (eg. x86). This relies on the assumption
449  * that ExecProcNode calls for a given plan node will always be made at
450  * roughly the same stack depth.
451  */
453 
454  /*
455  * If instrumentation is required, change the wrapper to one that just
456  * does instrumentation. Otherwise we can dispense with all wrappers and
457  * have ExecProcNode() directly call the relevant function from now on.
458  */
459  if (node->instrument)
461  else
462  node->ExecProcNode = node->ExecProcNodeReal;
463 
464  return node->ExecProcNode(node);
465 }
466 
467 
468 /*
469  * ExecProcNode wrapper that performs instrumentation calls. By keeping
470  * this a separate function, we avoid overhead in the normal case where
471  * no instrumentation is wanted.
472  */
473 static TupleTableSlot *
475 {
476  TupleTableSlot *result;
477 
478  InstrStartNode(node->instrument);
479 
480  result = node->ExecProcNodeReal(node);
481 
482  InstrStopNode(node->instrument, TupIsNull(result) ? 0.0 : 1.0);
483 
484  return result;
485 }
486 
487 
488 /* ----------------------------------------------------------------
489  * MultiExecProcNode
490  *
491  * Execute a node that doesn't return individual tuples
492  * (it might return a hashtable, bitmap, etc). Caller should
493  * check it got back the expected kind of Node.
494  *
495  * This has essentially the same responsibilities as ExecProcNode,
496  * but it does not do InstrStartNode/InstrStopNode (mainly because
497  * it can't tell how many returned tuples to count). Each per-node
498  * function must provide its own instrumentation support.
499  * ----------------------------------------------------------------
500  */
501 Node *
503 {
504  Node *result;
505 
507 
509 
510  if (node->chgParam != NULL) /* something changed */
511  ExecReScan(node); /* let ReScan handle this */
512 
513  switch (nodeTag(node))
514  {
515  /*
516  * Only node types that actually support multiexec will be listed
517  */
518 
519  case T_HashState:
520  result = MultiExecHash((HashState *) node);
521  break;
522 
523  case T_BitmapIndexScanState:
525  break;
526 
527  case T_BitmapAndState:
528  result = MultiExecBitmapAnd((BitmapAndState *) node);
529  break;
530 
531  case T_BitmapOrState:
532  result = MultiExecBitmapOr((BitmapOrState *) node);
533  break;
534 
535  default:
536  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
537  result = NULL;
538  break;
539  }
540 
541  return result;
542 }
543 
544 
545 /* ----------------------------------------------------------------
546  * ExecEndNode
547  *
548  * Recursively cleans up all the nodes in the plan rooted
549  * at 'node'.
550  *
551  * After this operation, the query plan will not be able to be
552  * processed any further. This should be called only after
553  * the query plan has been fully executed.
554  * ----------------------------------------------------------------
555  */
556 void
558 {
559  /*
560  * do nothing when we get to the end of a leaf on tree.
561  */
562  if (node == NULL)
563  return;
564 
565  /*
566  * Make sure there's enough stack available. Need to check here, in
567  * addition to ExecProcNode() (via ExecProcNodeFirst()), because it's not
568  * guaranteed that ExecProcNode() is reached for all nodes.
569  */
571 
572  if (node->chgParam != NULL)
573  {
574  bms_free(node->chgParam);
575  node->chgParam = NULL;
576  }
577 
578  switch (nodeTag(node))
579  {
580  /*
581  * control nodes
582  */
583  case T_ResultState:
584  ExecEndResult((ResultState *) node);
585  break;
586 
587  case T_ProjectSetState:
589  break;
590 
591  case T_ModifyTableState:
593  break;
594 
595  case T_AppendState:
596  ExecEndAppend((AppendState *) node);
597  break;
598 
599  case T_MergeAppendState:
601  break;
602 
603  case T_RecursiveUnionState:
605  break;
606 
607  case T_BitmapAndState:
609  break;
610 
611  case T_BitmapOrState:
612  ExecEndBitmapOr((BitmapOrState *) node);
613  break;
614 
615  /*
616  * scan nodes
617  */
618  case T_SeqScanState:
619  ExecEndSeqScan((SeqScanState *) node);
620  break;
621 
622  case T_SampleScanState:
624  break;
625 
626  case T_GatherState:
627  ExecEndGather((GatherState *) node);
628  break;
629 
630  case T_GatherMergeState:
632  break;
633 
634  case T_IndexScanState:
636  break;
637 
638  case T_IndexOnlyScanState:
640  break;
641 
642  case T_BitmapIndexScanState:
644  break;
645 
646  case T_BitmapHeapScanState:
648  break;
649 
650  case T_TidScanState:
651  ExecEndTidScan((TidScanState *) node);
652  break;
653 
654  case T_TidRangeScanState:
656  break;
657 
658  case T_SubqueryScanState:
660  break;
661 
662  case T_FunctionScanState:
664  break;
665 
666  case T_TableFuncScanState:
668  break;
669 
670  case T_ValuesScanState:
672  break;
673 
674  case T_CteScanState:
675  ExecEndCteScan((CteScanState *) node);
676  break;
677 
678  case T_NamedTuplestoreScanState:
680  break;
681 
682  case T_WorkTableScanState:
684  break;
685 
686  case T_ForeignScanState:
688  break;
689 
690  case T_CustomScanState:
692  break;
693 
694  /*
695  * join nodes
696  */
697  case T_NestLoopState:
698  ExecEndNestLoop((NestLoopState *) node);
699  break;
700 
701  case T_MergeJoinState:
703  break;
704 
705  case T_HashJoinState:
706  ExecEndHashJoin((HashJoinState *) node);
707  break;
708 
709  /*
710  * materialization nodes
711  */
712  case T_MaterialState:
713  ExecEndMaterial((MaterialState *) node);
714  break;
715 
716  case T_SortState:
717  ExecEndSort((SortState *) node);
718  break;
719 
720  case T_IncrementalSortState:
722  break;
723 
724  case T_MemoizeState:
725  ExecEndMemoize((MemoizeState *) node);
726  break;
727 
728  case T_GroupState:
729  ExecEndGroup((GroupState *) node);
730  break;
731 
732  case T_AggState:
733  ExecEndAgg((AggState *) node);
734  break;
735 
736  case T_WindowAggState:
738  break;
739 
740  case T_UniqueState:
741  ExecEndUnique((UniqueState *) node);
742  break;
743 
744  case T_HashState:
745  ExecEndHash((HashState *) node);
746  break;
747 
748  case T_SetOpState:
749  ExecEndSetOp((SetOpState *) node);
750  break;
751 
752  case T_LockRowsState:
753  ExecEndLockRows((LockRowsState *) node);
754  break;
755 
756  case T_LimitState:
757  ExecEndLimit((LimitState *) node);
758  break;
759 
760  default:
761  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
762  break;
763  }
764 }
765 
766 /*
767  * ExecShutdownNode
768  *
769  * Give execution nodes a chance to stop asynchronous resource consumption
770  * and release any resources still held.
771  */
772 void
774 {
775  (void) ExecShutdownNode_walker(node, NULL);
776 }
777 
778 static bool
779 ExecShutdownNode_walker(PlanState *node, void *context)
780 {
781  if (node == NULL)
782  return false;
783 
785 
786  /*
787  * Treat the node as running while we shut it down, but only if it's run
788  * at least once already. We don't expect much CPU consumption during
789  * node shutdown, but in the case of Gather or Gather Merge, we may shut
790  * down workers at this stage. If so, their buffer usage will get
791  * propagated into pgBufferUsage at this point, and we want to make sure
792  * that it gets associated with the Gather node. We skip this if the node
793  * has never been executed, so as to avoid incorrectly making it appear
794  * that it has.
795  */
796  if (node->instrument && node->instrument->running)
797  InstrStartNode(node->instrument);
798 
800 
801  switch (nodeTag(node))
802  {
803  case T_GatherState:
805  break;
806  case T_ForeignScanState:
808  break;
809  case T_CustomScanState:
811  break;
812  case T_GatherMergeState:
814  break;
815  case T_HashState:
816  ExecShutdownHash((HashState *) node);
817  break;
818  case T_HashJoinState:
820  break;
821  default:
822  break;
823  }
824 
825  /* Stop the node if we started it above, reporting 0 tuples. */
826  if (node->instrument && node->instrument->running)
827  InstrStopNode(node->instrument, 0);
828 
829  return false;
830 }
831 
832 /*
833  * ExecSetTupleBound
834  *
835  * Set a tuple bound for a planstate node. This lets child plan nodes
836  * optimize based on the knowledge that the maximum number of tuples that
837  * their parent will demand is limited. The tuple bound for a node may
838  * only be changed between scans (i.e., after node initialization or just
839  * before an ExecReScan call).
840  *
841  * Any negative tuples_needed value means "no limit", which should be the
842  * default assumption when this is not called at all for a particular node.
843  *
844  * Note: if this is called repeatedly on a plan tree, the exact same set
845  * of nodes must be updated with the new limit each time; be careful that
846  * only unchanging conditions are tested here.
847  */
848 void
849 ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
850 {
851  /*
852  * Since this function recurses, in principle we should check stack depth
853  * here. In practice, it's probably pointless since the earlier node
854  * initialization tree traversal would surely have consumed more stack.
855  */
856 
857  if (IsA(child_node, SortState))
858  {
859  /*
860  * If it is a Sort node, notify it that it can use bounded sort.
861  *
862  * Note: it is the responsibility of nodeSort.c to react properly to
863  * changes of these parameters. If we ever redesign this, it'd be a
864  * good idea to integrate this signaling with the parameter-change
865  * mechanism.
866  */
867  SortState *sortState = (SortState *) child_node;
868 
869  if (tuples_needed < 0)
870  {
871  /* make sure flag gets reset if needed upon rescan */
872  sortState->bounded = false;
873  }
874  else
875  {
876  sortState->bounded = true;
877  sortState->bound = tuples_needed;
878  }
879  }
880  else if (IsA(child_node, IncrementalSortState))
881  {
882  /*
883  * If it is an IncrementalSort node, notify it that it can use bounded
884  * sort.
885  *
886  * Note: it is the responsibility of nodeIncrementalSort.c to react
887  * properly to changes of these parameters. If we ever redesign this,
888  * it'd be a good idea to integrate this signaling with the
889  * parameter-change mechanism.
890  */
891  IncrementalSortState *sortState = (IncrementalSortState *) child_node;
892 
893  if (tuples_needed < 0)
894  {
895  /* make sure flag gets reset if needed upon rescan */
896  sortState->bounded = false;
897  }
898  else
899  {
900  sortState->bounded = true;
901  sortState->bound = tuples_needed;
902  }
903  }
904  else if (IsA(child_node, AppendState))
905  {
906  /*
907  * If it is an Append, we can apply the bound to any nodes that are
908  * children of the Append, since the Append surely need read no more
909  * than that many tuples from any one input.
910  */
911  AppendState *aState = (AppendState *) child_node;
912  int i;
913 
914  for (i = 0; i < aState->as_nplans; i++)
915  ExecSetTupleBound(tuples_needed, aState->appendplans[i]);
916  }
917  else if (IsA(child_node, MergeAppendState))
918  {
919  /*
920  * If it is a MergeAppend, we can apply the bound to any nodes that
921  * are children of the MergeAppend, since the MergeAppend surely need
922  * read no more than that many tuples from any one input.
923  */
924  MergeAppendState *maState = (MergeAppendState *) child_node;
925  int i;
926 
927  for (i = 0; i < maState->ms_nplans; i++)
928  ExecSetTupleBound(tuples_needed, maState->mergeplans[i]);
929  }
930  else if (IsA(child_node, ResultState))
931  {
932  /*
933  * Similarly, for a projecting Result, we can apply the bound to its
934  * child node.
935  *
936  * If Result supported qual checking, we'd have to punt on seeing a
937  * qual. Note that having a resconstantqual is not a showstopper: if
938  * that condition succeeds it affects nothing, while if it fails, no
939  * rows will be demanded from the Result child anyway.
940  */
941  if (outerPlanState(child_node))
942  ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
943  }
944  else if (IsA(child_node, SubqueryScanState))
945  {
946  /*
947  * We can also descend through SubqueryScan, but only if it has no
948  * qual (otherwise it might discard rows).
949  */
950  SubqueryScanState *subqueryState = (SubqueryScanState *) child_node;
951 
952  if (subqueryState->ss.ps.qual == NULL)
953  ExecSetTupleBound(tuples_needed, subqueryState->subplan);
954  }
955  else if (IsA(child_node, GatherState))
956  {
957  /*
958  * A Gather node can propagate the bound to its workers. As with
959  * MergeAppend, no one worker could possibly need to return more
960  * tuples than the Gather itself needs to.
961  *
962  * Note: As with Sort, the Gather node is responsible for reacting
963  * properly to changes to this parameter.
964  */
965  GatherState *gstate = (GatherState *) child_node;
966 
967  gstate->tuples_needed = tuples_needed;
968 
969  /* Also pass down the bound to our own copy of the child plan */
970  ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
971  }
972  else if (IsA(child_node, GatherMergeState))
973  {
974  /* Same comments as for Gather */
975  GatherMergeState *gstate = (GatherMergeState *) child_node;
976 
977  gstate->tuples_needed = tuples_needed;
978 
979  ExecSetTupleBound(tuples_needed, outerPlanState(child_node));
980  }
981 
982  /*
983  * In principle we could descend through any plan node type that is
984  * certain not to discard or combine input rows; but on seeing a node that
985  * can do that, we can't propagate the bound any further. For the moment
986  * it's unclear that any other cases are worth checking here.
987  */
988 }
void bms_free(Bitmapset *a)
Definition: bitmapset.c:209
#define ERROR
Definition: elog.h:35
void ExecReScan(PlanState *node)
Definition: execAmi.c:78
static bool ExecShutdownNode_walker(PlanState *node, void *context)
Definition: execProcnode.c:779
void ExecSetTupleBound(int64 tuples_needed, PlanState *child_node)
Definition: execProcnode.c:849
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:557
void ExecShutdownNode(PlanState *node)
Definition: execProcnode.c:773
static TupleTableSlot * ExecProcNodeInstr(PlanState *node)
Definition: execProcnode.c:474
Node * MultiExecProcNode(PlanState *node)
Definition: execProcnode.c:502
static TupleTableSlot * ExecProcNodeFirst(PlanState *node)
Definition: execProcnode.c:443
void ExecSetExecProcNode(PlanState *node, ExecProcNodeMtd function)
Definition: execProcnode.c:425
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:142
#define outerPlanState(node)
Definition: execnodes.h:1125
TupleTableSlot *(* ExecProcNodeMtd)(struct PlanState *pstate)
Definition: execnodes.h:1014
Instrumentation * InstrAlloc(int n, int instrument_options, bool async_mode)
Definition: instrument.c:31
void InstrStartNode(Instrumentation *instr)
Definition: instrument.c:68
void InstrStopNode(Instrumentation *instr, double nTuples)
Definition: instrument.c:84
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:338
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
AggState * ExecInitAgg(Agg *node, EState *estate, int eflags)
Definition: nodeAgg.c:3165
void ExecEndAgg(AggState *node)
Definition: nodeAgg.c:4292
void ExecEndAppend(AppendState *node)
Definition: nodeAppend.c:383
AppendState * ExecInitAppend(Append *node, EState *estate, int eflags)
Definition: nodeAppend.c:109
Node * MultiExecBitmapAnd(BitmapAndState *node)
void ExecEndBitmapAnd(BitmapAndState *node)
BitmapAndState * ExecInitBitmapAnd(BitmapAnd *node, EState *estate, int eflags)
Definition: nodeBitmapAnd.c:55
void ExecEndBitmapHeapScan(BitmapHeapScanState *node)
BitmapHeapScanState * ExecInitBitmapHeapScan(BitmapHeapScan *node, EState *estate, int eflags)
BitmapIndexScanState * ExecInitBitmapIndexScan(BitmapIndexScan *node, EState *estate, int eflags)
void ExecEndBitmapIndexScan(BitmapIndexScanState *node)
Node * MultiExecBitmapIndexScan(BitmapIndexScanState *node)
void ExecEndBitmapOr(BitmapOrState *node)
Definition: nodeBitmapOr.c:196
BitmapOrState * ExecInitBitmapOr(BitmapOr *node, EState *estate, int eflags)
Definition: nodeBitmapOr.c:56
Node * MultiExecBitmapOr(BitmapOrState *node)
Definition: nodeBitmapOr.c:111
CteScanState * ExecInitCteScan(CteScan *node, EState *estate, int eflags)
Definition: nodeCtescan.c:175
void ExecEndCteScan(CteScanState *node)
Definition: nodeCtescan.c:288
void ExecShutdownCustomScan(CustomScanState *node)
Definition: nodeCustom.c:231
CustomScanState * ExecInitCustomScan(CustomScan *cscan, EState *estate, int eflags)
Definition: nodeCustom.c:29
void ExecEndCustomScan(CustomScanState *node)
Definition: nodeCustom.c:128
void ExecShutdownForeignScan(ForeignScanState *node)
ForeignScanState * ExecInitForeignScan(ForeignScan *node, EState *estate, int eflags)
void ExecEndForeignScan(ForeignScanState *node)
#define planstate_tree_walker(ps, w, c)
Definition: nodeFuncs.h:177
FunctionScanState * ExecInitFunctionScan(FunctionScan *node, EState *estate, int eflags)
void ExecEndFunctionScan(FunctionScanState *node)
GatherMergeState * ExecInitGatherMerge(GatherMerge *node, EState *estate, int eflags)
void ExecShutdownGatherMerge(GatherMergeState *node)
void ExecEndGatherMerge(GatherMergeState *node)
void ExecEndGather(GatherState *node)
Definition: nodeGather.c:249
GatherState * ExecInitGather(Gather *node, EState *estate, int eflags)
Definition: nodeGather.c:58
void ExecShutdownGather(GatherState *node)
Definition: nodeGather.c:419
GroupState * ExecInitGroup(Group *node, EState *estate, int eflags)
Definition: nodeGroup.c:162
void ExecEndGroup(GroupState *node)
Definition: nodeGroup.c:227
HashState * ExecInitHash(Hash *node, EState *estate, int eflags)
Definition: nodeHash.c:354
void ExecEndHash(HashState *node)
Definition: nodeHash.c:407
void ExecShutdownHash(HashState *node)
Definition: nodeHash.c:2651
Node * MultiExecHash(HashState *node)
Definition: nodeHash.c:106
HashJoinState * ExecInitHashJoin(HashJoin *node, EState *estate, int eflags)
Definition: nodeHashjoin.c:617
void ExecEndHashJoin(HashJoinState *node)
Definition: nodeHashjoin.c:765
void ExecShutdownHashJoin(HashJoinState *node)
IncrementalSortState * ExecInitIncrementalSort(IncrementalSort *node, EState *estate, int eflags)
void ExecEndIncrementalSort(IncrementalSortState *node)
void ExecEndIndexOnlyScan(IndexOnlyScanState *node)
IndexOnlyScanState * ExecInitIndexOnlyScan(IndexOnlyScan *node, EState *estate, int eflags)
IndexScanState * ExecInitIndexScan(IndexScan *node, EState *estate, int eflags)
void ExecEndIndexScan(IndexScanState *node)
void ExecEndLimit(LimitState *node)
Definition: nodeLimit.c:535
LimitState * ExecInitLimit(Limit *node, EState *estate, int eflags)
Definition: nodeLimit.c:448
void ExecEndLockRows(LockRowsState *node)
Definition: nodeLockRows.c:386
LockRowsState * ExecInitLockRows(LockRows *node, EState *estate, int eflags)
Definition: nodeLockRows.c:292
MaterialState * ExecInitMaterial(Material *node, EState *estate, int eflags)
Definition: nodeMaterial.c:164
void ExecEndMaterial(MaterialState *node)
Definition: nodeMaterial.c:240
MemoizeState * ExecInitMemoize(Memoize *node, EState *estate, int eflags)
Definition: nodeMemoize.c:909
void ExecEndMemoize(MemoizeState *node)
Definition: nodeMemoize.c:1034
MergeAppendState * ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
void ExecEndMergeAppend(MergeAppendState *node)
MergeJoinState * ExecInitMergeJoin(MergeJoin *node, EState *estate, int eflags)
void ExecEndMergeJoin(MergeJoinState *node)
void ExecEndModifyTable(ModifyTableState *node)
ModifyTableState * ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
NamedTuplestoreScanState * ExecInitNamedTuplestoreScan(NamedTuplestoreScan *node, EState *estate, int eflags)
void ExecEndNamedTuplestoreScan(NamedTuplestoreScanState *node)
void ExecEndNestLoop(NestLoopState *node)
Definition: nodeNestloop.c:362
NestLoopState * ExecInitNestLoop(NestLoop *node, EState *estate, int eflags)
Definition: nodeNestloop.c:263
ProjectSetState * ExecInitProjectSet(ProjectSet *node, EState *estate, int eflags)
void ExecEndProjectSet(ProjectSetState *node)
void ExecEndRecursiveUnion(RecursiveUnionState *node)
RecursiveUnionState * ExecInitRecursiveUnion(RecursiveUnion *node, EState *estate, int eflags)
ResultState * ExecInitResult(Result *node, EState *estate, int eflags)
Definition: nodeResult.c:181
void ExecEndResult(ResultState *node)
Definition: nodeResult.c:241
SampleScanState * ExecInitSampleScan(SampleScan *node, EState *estate, int eflags)
void ExecEndSampleScan(SampleScanState *node)
void ExecEndSeqScan(SeqScanState *node)
Definition: nodeSeqscan.c:184
SeqScanState * ExecInitSeqScan(SeqScan *node, EState *estate, int eflags)
Definition: nodeSeqscan.c:123
void ExecEndSetOp(SetOpState *node)
Definition: nodeSetOp.c:583
SetOpState * ExecInitSetOp(SetOp *node, EState *estate, int eflags)
Definition: nodeSetOp.c:481
SortState * ExecInitSort(Sort *node, EState *estate, int eflags)
Definition: nodeSort.c:221
void ExecEndSort(SortState *node)
Definition: nodeSort.c:301
SubPlanState * ExecInitSubPlan(SubPlan *subplan, PlanState *parent)
Definition: nodeSubplan.c:804
void ExecEndSubqueryScan(SubqueryScanState *node)
SubqueryScanState * ExecInitSubqueryScan(SubqueryScan *node, EState *estate, int eflags)
void ExecEndTableFuncScan(TableFuncScanState *node)
TableFuncScanState * ExecInitTableFuncScan(TableFuncScan *node, EState *estate, int eflags)
TidRangeScanState * ExecInitTidRangeScan(TidRangeScan *node, EState *estate, int eflags)
void ExecEndTidRangeScan(TidRangeScanState *node)
void ExecEndTidScan(TidScanState *node)
Definition: nodeTidscan.c:471
TidScanState * ExecInitTidScan(TidScan *node, EState *estate, int eflags)
Definition: nodeTidscan.c:501
void ExecEndUnique(UniqueState *node)
Definition: nodeUnique.c:169
UniqueState * ExecInitUnique(Unique *node, EState *estate, int eflags)
Definition: nodeUnique.c:115
ValuesScanState * ExecInitValuesScan(ValuesScan *node, EState *estate, int eflags)
void ExecEndValuesScan(ValuesScanState *node)
WindowAggState * ExecInitWindowAgg(WindowAgg *node, EState *estate, int eflags)
void ExecEndWindowAgg(WindowAggState *node)
WorkTableScanState * ExecInitWorkTableScan(WorkTableScan *node, EState *estate, int eflags)
void ExecEndWorkTableScan(WorkTableScanState *node)
#define IsA(nodeptr, _type_)
Definition: nodes.h:168
#define nodeTag(nodeptr)
Definition: nodes.h:122
#define lfirst(lc)
Definition: pg_list.h:170
#define NIL
Definition: pg_list.h:66
void check_stack_depth(void)
Definition: postgres.c:3440
Definition: plannodes.h:993
PlanState ** appendplans
Definition: execnodes.h:1331
int es_instrument
Definition: execnodes.h:662
int64 tuples_needed
Definition: execnodes.h:2568
Definition: pg_list.h:52
PlanState ** mergeplans
Definition: execnodes.h:1374
Definition: nodes.h:118
Instrumentation * instrument
Definition: execnodes.h:1039
ExecProcNodeMtd ExecProcNodeReal
Definition: execnodes.h:1036
ExprState * qual
Definition: execnodes.h:1050
List * initPlan
Definition: execnodes.h:1054
Bitmapset * chgParam
Definition: execnodes.h:1061
bool async_capable
Definition: execnodes.h:1071
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1035
List * initPlan
Definition: plannodes.h:157
PlanState ps
Definition: execnodes.h:1454
bool bounded
Definition: execnodes.h:2229
int64 bound
Definition: execnodes.h:2230
PlanState * subplan
Definition: execnodes.h:1779
#define TupIsNull(slot)
Definition: tuptable.h:300