PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeSort.c File Reference
#include "postgres.h"
#include "executor/execdebug.h"
#include "executor/nodeSort.h"
#include "miscadmin.h"
#include "utils/tuplesort.h"
Include dependency graph for nodeSort.c:

Go to the source code of this file.

Functions

TupleTableSlotExecSort (SortState *node)
 
SortStateExecInitSort (Sort *node, EState *estate, int eflags)
 
void ExecEndSort (SortState *node)
 
void ExecSortMarkPos (SortState *node)
 
void ExecSortRestrPos (SortState *node)
 
void ExecReScanSort (SortState *node)
 

Function Documentation

void ExecEndSort ( SortState node)

Definition at line 223 of file nodeSort.c.

References ExecClearTuple(), ExecEndNode(), NULL, outerPlanState, ScanState::ps, PlanState::ps_ResultTupleSlot, SO1_printf, SortState::ss, ScanState::ss_ScanTupleSlot, tuplesort_end(), and SortState::tuplesortstate.

Referenced by ExecEndNode().

224 {
225  SO1_printf("ExecEndSort: %s\n",
226  "shutting down sort node");
227 
228  /*
229  * clean out the tuple table
230  */
232  /* must drop pointer to sort result tuple */
234 
235  /*
236  * Release tuplesort resources
237  */
238  if (node->tuplesortstate != NULL)
240  node->tuplesortstate = NULL;
241 
242  /*
243  * shut down the subplan
244  */
246 
247  SO1_printf("ExecEndSort: %s\n",
248  "sort node shutdown");
249 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:624
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:442
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1297
#define SO1_printf(s, p)
Definition: execdebug.h:113
PlanState ps
Definition: execnodes.h:1294
void * tuplesortstate
Definition: execnodes.h:1835
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1081
#define outerPlanState(node)
Definition: execnodes.h:1096
ScanState ss
Definition: execnodes.h:1828
#define NULL
Definition: c.h:226
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1157
SortState* ExecInitSort ( Sort node,
EState estate,
int  eflags 
)

Definition at line 152 of file nodeSort.c.

References SortState::bounded, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, EXEC_FLAG_REWIND, ExecAssignResultTypeFromTL(), ExecAssignScanTypeFromOuterPlan(), ExecInitNode(), ExecInitResultTupleSlot(), ExecInitScanTupleSlot(), makeNode, NULL, outerPlan, outerPlanState, PlanState::plan, ScanState::ps, PlanState::ps_ProjInfo, SortState::randomAccess, SO1_printf, SortState::sort_Done, SortState::ss, PlanState::state, and SortState::tuplesortstate.

Referenced by ExecInitNode().

153 {
154  SortState *sortstate;
155 
156  SO1_printf("ExecInitSort: %s\n",
157  "initializing sort node");
158 
159  /*
160  * create state structure
161  */
162  sortstate = makeNode(SortState);
163  sortstate->ss.ps.plan = (Plan *) node;
164  sortstate->ss.ps.state = estate;
165 
166  /*
167  * We must have random access to the sort output to do backward scan or
168  * mark/restore. We also prefer to materialize the sort output if we
169  * might be called on to rewind and replay it many times.
170  */
171  sortstate->randomAccess = (eflags & (EXEC_FLAG_REWIND |
173  EXEC_FLAG_MARK)) != 0;
174 
175  sortstate->bounded = false;
176  sortstate->sort_Done = false;
177  sortstate->tuplesortstate = NULL;
178 
179  /*
180  * Miscellaneous initialization
181  *
182  * Sort nodes don't initialize their ExprContexts because they never call
183  * ExecQual or ExecProject.
184  */
185 
186  /*
187  * tuple table initialization
188  *
189  * sort nodes only return scan tuples from their sorted relation.
190  */
191  ExecInitResultTupleSlot(estate, &sortstate->ss.ps);
192  ExecInitScanTupleSlot(estate, &sortstate->ss);
193 
194  /*
195  * initialize child nodes
196  *
197  * We shield the child node from the need to support REWIND, BACKWARD, or
198  * MARK/RESTORE.
199  */
201 
202  outerPlanState(sortstate) = ExecInitNode(outerPlan(node), estate, eflags);
203 
204  /*
205  * initialize tuple type. no need to initialize projection info because
206  * this node doesn't do projections.
207  */
208  ExecAssignResultTypeFromTL(&sortstate->ss.ps);
209  ExecAssignScanTypeFromOuterPlan(&sortstate->ss);
210  sortstate->ss.ps.ps_ProjInfo = NULL;
211 
212  SO1_printf("ExecInitSort: %s\n",
213  "sort node initialized");
214 
215  return sortstate;
216 }
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:845
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1083
bool bounded
Definition: execnodes.h:1830
EState * state
Definition: execnodes.h:1053
#define SO1_printf(s, p)
Definition: execdebug.h:113
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:429
PlanState ps
Definition: execnodes.h:1294
void * tuplesortstate
Definition: execnodes.h:1835
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:835
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:1096
ScanState ss
Definition: execnodes.h:1828
#define EXEC_FLAG_REWIND
Definition: executor.h:59
#define outerPlan(node)
Definition: plannodes.h:159
Plan * plan
Definition: execnodes.h:1051
#define makeNode(_type_)
Definition: nodes.h:551
#define NULL
Definition: c.h:226
#define EXEC_FLAG_MARK
Definition: executor.h:61
bool randomAccess
Definition: execnodes.h:1829
bool sort_Done
Definition: execnodes.h:1832
void ExecAssignScanTypeFromOuterPlan(ScanState *scanstate)
Definition: execUtils.c:726
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:137
void ExecReScanSort ( SortState node)

Definition at line 291 of file nodeSort.c.

References SortState::bound, SortState::bound_Done, SortState::bounded, SortState::bounded_Done, PlanState::chgParam, ExecClearTuple(), ExecReScan(), NULL, outerPlan, outerPlanState, ScanState::ps, PlanState::ps_ResultTupleSlot, SortState::randomAccess, SortState::sort_Done, SortState::ss, tuplesort_end(), tuplesort_rescan(), and SortState::tuplesortstate.

Referenced by ExecReScan().

292 {
294 
295  /*
296  * If we haven't sorted yet, just return. If outerplan's chgParam is not
297  * NULL then it will be re-scanned by ExecProcNode, else no reason to
298  * re-scan it at all.
299  */
300  if (!node->sort_Done)
301  return;
302 
303  /* must drop pointer to sort result tuple */
305 
306  /*
307  * If subnode is to be rescanned then we forget previous sort results; we
308  * have to re-read the subplan and re-sort. Also must re-sort if the
309  * bounded-sort parameters changed or we didn't select randomAccess.
310  *
311  * Otherwise we can just rewind and rescan the sorted output.
312  */
313  if (outerPlan->chgParam != NULL ||
314  node->bounded != node->bounded_Done ||
315  node->bound != node->bound_Done ||
316  !node->randomAccess)
317  {
318  node->sort_Done = false;
320  node->tuplesortstate = NULL;
321 
322  /*
323  * if chgParam of subnode is not null then plan will be re-scanned by
324  * first ExecProcNode.
325  */
326  if (outerPlan->chgParam == NULL)
327  ExecReScan(outerPlan);
328  }
329  else
331 }
bool bounded_Done
Definition: execnodes.h:1833
bool bounded
Definition: execnodes.h:1830
int64 bound
Definition: execnodes.h:1831
void ExecReScan(PlanState *node)
Definition: execAmi.c:74
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:442
PlanState ps
Definition: execnodes.h:1294
void * tuplesortstate
Definition: execnodes.h:1835
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1081
void tuplesort_rescan(Tuplesortstate *state)
Definition: tuplesort.c:3115
#define outerPlanState(node)
Definition: execnodes.h:1096
int64 bound_Done
Definition: execnodes.h:1834
ScanState ss
Definition: execnodes.h:1828
Bitmapset * chgParam
Definition: execnodes.h:1076
#define outerPlan(node)
Definition: plannodes.h:159
#define NULL
Definition: c.h:226
bool randomAccess
Definition: execnodes.h:1829
bool sort_Done
Definition: execnodes.h:1832
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1157
TupleTableSlot* ExecSort ( SortState node)

Definition at line 39 of file nodeSort.c.

References SortState::bound, SortState::bound_Done, SortState::bounded, SortState::bounded_Done, Sort::collations, EState::es_direction, ExecGetResultType(), ExecProcNode(), ForwardScanDirection, NULL, Sort::nullsFirst, Sort::numCols, outerPlanState, PlanState::plan, ScanState::ps, PlanState::ps_ResultTupleSlot, SortState::randomAccess, ScanDirectionIsForward, SO1_printf, SortState::sort_Done, Sort::sortColIdx, Sort::sortOperators, SortState::ss, PlanState::state, TupIsNull, tuplesort_begin_heap(), tuplesort_gettupleslot(), tuplesort_performsort(), tuplesort_puttupleslot(), tuplesort_set_bound(), SortState::tuplesortstate, and work_mem.

Referenced by ExecProcNode().

40 {
41  EState *estate;
42  ScanDirection dir;
43  Tuplesortstate *tuplesortstate;
44  TupleTableSlot *slot;
45 
46  /*
47  * get state info from node
48  */
49  SO1_printf("ExecSort: %s\n",
50  "entering routine");
51 
52  estate = node->ss.ps.state;
53  dir = estate->es_direction;
54  tuplesortstate = (Tuplesortstate *) node->tuplesortstate;
55 
56  /*
57  * If first time through, read all tuples from outer plan and pass them to
58  * tuplesort.c. Subsequent calls just fetch tuples from tuplesort.
59  */
60 
61  if (!node->sort_Done)
62  {
63  Sort *plannode = (Sort *) node->ss.ps.plan;
64  PlanState *outerNode;
65  TupleDesc tupDesc;
66 
67  SO1_printf("ExecSort: %s\n",
68  "sorting subplan");
69 
70  /*
71  * Want to scan subplan in the forward direction while creating the
72  * sorted data.
73  */
75 
76  /*
77  * Initialize tuplesort module.
78  */
79  SO1_printf("ExecSort: %s\n",
80  "calling tuplesort_begin");
81 
82  outerNode = outerPlanState(node);
83  tupDesc = ExecGetResultType(outerNode);
84 
85  tuplesortstate = tuplesort_begin_heap(tupDesc,
86  plannode->numCols,
87  plannode->sortColIdx,
88  plannode->sortOperators,
89  plannode->collations,
90  plannode->nullsFirst,
91  work_mem,
92  node->randomAccess);
93  if (node->bounded)
94  tuplesort_set_bound(tuplesortstate, node->bound);
95  node->tuplesortstate = (void *) tuplesortstate;
96 
97  /*
98  * Scan the subplan and feed all the tuples to tuplesort.
99  */
100 
101  for (;;)
102  {
103  slot = ExecProcNode(outerNode);
104 
105  if (TupIsNull(slot))
106  break;
107 
108  tuplesort_puttupleslot(tuplesortstate, slot);
109  }
110 
111  /*
112  * Complete the sort.
113  */
114  tuplesort_performsort(tuplesortstate);
115 
116  /*
117  * restore to user specified direction
118  */
119  estate->es_direction = dir;
120 
121  /*
122  * finally set the sorted flag to true
123  */
124  node->sort_Done = true;
125  node->bounded_Done = node->bounded;
126  node->bound_Done = node->bound;
127  SO1_printf("ExecSort: %s\n", "sorting done");
128  }
129 
130  SO1_printf("ExecSort: %s\n",
131  "retrieving tuple from tuplesort");
132 
133  /*
134  * Get the first or next tuple from tuplesort. Returns NULL if no more
135  * tuples.
136  */
137  slot = node->ss.ps.ps_ResultTupleSlot;
138  (void) tuplesort_gettupleslot(tuplesortstate,
140  slot, NULL);
141  return slot;
142 }
TupleTableSlot * ExecProcNode(PlanState *node)
Definition: execProcnode.c:380
void tuplesort_performsort(Tuplesortstate *state)
Definition: tuplesort.c:1763
bool bounded_Done
Definition: execnodes.h:1833
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
bool bounded
Definition: execnodes.h:1830
int64 bound
Definition: execnodes.h:1831
bool * nullsFirst
Definition: plannodes.h:699
EState * state
Definition: execnodes.h:1053
#define SO1_printf(s, p)
Definition: execdebug.h:113
Oid * sortOperators
Definition: plannodes.h:697
ScanDirection es_direction
Definition: execnodes.h:366
PlanState ps
Definition: execnodes.h:1294
void * tuplesortstate
Definition: execnodes.h:1835
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1081
#define outerPlanState(node)
Definition: execnodes.h:1096
void tuplesort_set_bound(Tuplesortstate *state, int64 bound)
Definition: tuplesort.c:1113
ScanDirection
Definition: sdir.h:22
#define TupIsNull(slot)
Definition: tuptable.h:138
int64 bound_Done
Definition: execnodes.h:1834
ScanState ss
Definition: execnodes.h:1828
int numCols
Definition: plannodes.h:695
int work_mem
Definition: globals.c:112
Plan * plan
Definition: execnodes.h:1051
#define NULL
Definition: c.h:226
bool randomAccess
Definition: execnodes.h:1829
bool sort_Done
Definition: execnodes.h:1832
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:458
AttrNumber * sortColIdx
Definition: plannodes.h:696
Tuplesortstate * tuplesort_begin_heap(TupleDesc tupDesc, int nkeys, AttrNumber *attNums, Oid *sortOperators, Oid *sortCollations, bool *nullsFirstFlags, int workMem, bool randomAccess)
Definition: tuplesort.c:753
Oid * collations
Definition: plannodes.h:698
void tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
Definition: tuplesort.c:1354
bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward, TupleTableSlot *slot, Datum *abbrev)
Definition: tuplesort.c:2099
void ExecSortMarkPos ( SortState node)

Definition at line 258 of file nodeSort.c.

References SortState::sort_Done, tuplesort_markpos(), and SortState::tuplesortstate.

Referenced by ExecMarkPos().

259 {
260  /*
261  * if we haven't sorted yet, just return
262  */
263  if (!node->sort_Done)
264  return;
265 
267 }
void * tuplesortstate
Definition: execnodes.h:1835
bool sort_Done
Definition: execnodes.h:1832
void tuplesort_markpos(Tuplesortstate *state)
Definition: tuplesort.c:3150
void ExecSortRestrPos ( SortState node)

Definition at line 276 of file nodeSort.c.

References SortState::sort_Done, tuplesort_restorepos(), and SortState::tuplesortstate.

Referenced by ExecRestrPos().

277 {
278  /*
279  * if we haven't sorted yet, just return.
280  */
281  if (!node->sort_Done)
282  return;
283 
284  /*
285  * restore the scan to the previously marked position
286  */
288 }
void tuplesort_restorepos(Tuplesortstate *state)
Definition: tuplesort.c:3182
void * tuplesortstate
Definition: execnodes.h:1835
bool sort_Done
Definition: execnodes.h:1832