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