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

static TupleTableSlotExecSort (PlanState *pstate)
 
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 228 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().

229 {
230  SO1_printf("ExecEndSort: %s\n",
231  "shutting down sort node");
232 
233  /*
234  * clean out the tuple table
235  */
237  /* must drop pointer to sort result tuple */
239 
240  /*
241  * Release tuplesort resources
242  */
243  if (node->tuplesortstate != NULL)
245  node->tuplesortstate = NULL;
246 
247  /*
248  * shut down the subplan
249  */
251 
252  SO1_printf("ExecEndSort: %s\n",
253  "sort node shutdown");
254 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:523
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1101
#define SO1_printf(s, p)
Definition: execdebug.h:92
PlanState ps
Definition: execnodes.h:1098
void * tuplesortstate
Definition: execnodes.h:1746
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
#define outerPlanState(node)
Definition: execnodes.h:893
ScanState ss
Definition: execnodes.h:1739
#define NULL
Definition: c.h:229
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1167
SortState* ExecInitSort ( Sort node,
EState estate,
int  eflags 
)

Definition at line 156 of file nodeSort.c.

References SortState::bounded, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, EXEC_FLAG_REWIND, ExecAssignResultTypeFromTL(), ExecAssignScanTypeFromOuterPlan(), ExecInitNode(), ExecInitResultTupleSlot(), ExecInitScanTupleSlot(), PlanState::ExecProcNode, ExecSort(), 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().

157 {
158  SortState *sortstate;
159 
160  SO1_printf("ExecInitSort: %s\n",
161  "initializing sort node");
162 
163  /*
164  * create state structure
165  */
166  sortstate = makeNode(SortState);
167  sortstate->ss.ps.plan = (Plan *) node;
168  sortstate->ss.ps.state = estate;
169  sortstate->ss.ps.ExecProcNode = ExecSort;
170 
171  /*
172  * We must have random access to the sort output to do backward scan or
173  * mark/restore. We also prefer to materialize the sort output if we
174  * might be called on to rewind and replay it many times.
175  */
176  sortstate->randomAccess = (eflags & (EXEC_FLAG_REWIND |
178  EXEC_FLAG_MARK)) != 0;
179 
180  sortstate->bounded = false;
181  sortstate->sort_Done = false;
182  sortstate->tuplesortstate = NULL;
183 
184  /*
185  * Miscellaneous initialization
186  *
187  * Sort nodes don't initialize their ExprContexts because they never call
188  * ExecQual or ExecProject.
189  */
190 
191  /*
192  * tuple table initialization
193  *
194  * sort nodes only return scan tuples from their sorted relation.
195  */
196  ExecInitResultTupleSlot(estate, &sortstate->ss.ps);
197  ExecInitScanTupleSlot(estate, &sortstate->ss);
198 
199  /*
200  * initialize child nodes
201  *
202  * We shield the child node from the need to support REWIND, BACKWARD, or
203  * MARK/RESTORE.
204  */
206 
207  outerPlanState(sortstate) = ExecInitNode(outerPlan(node), estate, eflags);
208 
209  /*
210  * initialize tuple type. no need to initialize projection info because
211  * this node doesn't do projections.
212  */
213  ExecAssignResultTypeFromTL(&sortstate->ss.ps);
214  ExecAssignScanTypeFromOuterPlan(&sortstate->ss);
215  sortstate->ss.ps.ps_ProjInfo = NULL;
216 
217  SO1_printf("ExecInitSort: %s\n",
218  "sort node initialized");
219 
220  return sortstate;
221 }
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:882
bool bounded
Definition: execnodes.h:1741
EState * state
Definition: execnodes.h:849
#define SO1_printf(s, p)
Definition: execdebug.h:92
void ExecAssignResultTypeFromTL(PlanState *planstate)
Definition: execUtils.c:445
PlanState ps
Definition: execnodes.h:1098
void * tuplesortstate
Definition: execnodes.h:1746
static TupleTableSlot * ExecSort(PlanState *pstate)
Definition: nodeSort.c:39
void ExecInitResultTupleSlot(EState *estate, PlanState *planstate)
Definition: execTuples.c:832
#define EXEC_FLAG_BACKWARD
Definition: executor.h:60
#define outerPlanState(node)
Definition: execnodes.h:893
ScanState ss
Definition: execnodes.h:1739
#define EXEC_FLAG_REWIND
Definition: executor.h:59
#define outerPlan(node)
Definition: plannodes.h:174
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:853
Plan * plan
Definition: execnodes.h:847
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
#define EXEC_FLAG_MARK
Definition: executor.h:61
bool randomAccess
Definition: execnodes.h:1740
bool sort_Done
Definition: execnodes.h:1743
void ExecAssignScanTypeFromOuterPlan(ScanState *scanstate)
Definition: execUtils.c:557
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
void ExecReScanSort ( SortState node)

Definition at line 296 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().

297 {
299 
300  /*
301  * If we haven't sorted yet, just return. If outerplan's chgParam is not
302  * NULL then it will be re-scanned by ExecProcNode, else no reason to
303  * re-scan it at all.
304  */
305  if (!node->sort_Done)
306  return;
307 
308  /* must drop pointer to sort result tuple */
310 
311  /*
312  * If subnode is to be rescanned then we forget previous sort results; we
313  * have to re-read the subplan and re-sort. Also must re-sort if the
314  * bounded-sort parameters changed or we didn't select randomAccess.
315  *
316  * Otherwise we can just rewind and rescan the sorted output.
317  */
318  if (outerPlan->chgParam != NULL ||
319  node->bounded != node->bounded_Done ||
320  node->bound != node->bound_Done ||
321  !node->randomAccess)
322  {
323  node->sort_Done = false;
325  node->tuplesortstate = NULL;
326 
327  /*
328  * if chgParam of subnode is not null then plan will be re-scanned by
329  * first ExecProcNode.
330  */
331  if (outerPlan->chgParam == NULL)
332  ExecReScan(outerPlan);
333  }
334  else
336 }
bool bounded_Done
Definition: execnodes.h:1744
bool bounded
Definition: execnodes.h:1741
int64 bound
Definition: execnodes.h:1742
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
PlanState ps
Definition: execnodes.h:1098
void * tuplesortstate
Definition: execnodes.h:1746
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
void tuplesort_rescan(Tuplesortstate *state)
Definition: tuplesort.c:3131
#define outerPlanState(node)
Definition: execnodes.h:893
int64 bound_Done
Definition: execnodes.h:1745
ScanState ss
Definition: execnodes.h:1739
Bitmapset * chgParam
Definition: execnodes.h:875
#define outerPlan(node)
Definition: plannodes.h:174
#define NULL
Definition: c.h:229
bool randomAccess
Definition: execnodes.h:1740
bool sort_Done
Definition: execnodes.h:1743
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1167
static TupleTableSlot* ExecSort ( PlanState pstate)
static

Definition at line 39 of file nodeSort.c.

References SortState::bound, SortState::bound_Done, SortState::bounded, SortState::bounded_Done, castNode, CHECK_FOR_INTERRUPTS, 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 ExecInitSort().

40 {
41  SortState *node = castNode(SortState, pstate);
42  EState *estate;
43  ScanDirection dir;
44  Tuplesortstate *tuplesortstate;
45  TupleTableSlot *slot;
46 
48 
49  /*
50  * get state info from node
51  */
52  SO1_printf("ExecSort: %s\n",
53  "entering routine");
54 
55  estate = node->ss.ps.state;
56  dir = estate->es_direction;
57  tuplesortstate = (Tuplesortstate *) node->tuplesortstate;
58 
59  /*
60  * If first time through, read all tuples from outer plan and pass them to
61  * tuplesort.c. Subsequent calls just fetch tuples from tuplesort.
62  */
63 
64  if (!node->sort_Done)
65  {
66  Sort *plannode = (Sort *) node->ss.ps.plan;
67  PlanState *outerNode;
68  TupleDesc tupDesc;
69 
70  SO1_printf("ExecSort: %s\n",
71  "sorting subplan");
72 
73  /*
74  * Want to scan subplan in the forward direction while creating the
75  * sorted data.
76  */
78 
79  /*
80  * Initialize tuplesort module.
81  */
82  SO1_printf("ExecSort: %s\n",
83  "calling tuplesort_begin");
84 
85  outerNode = outerPlanState(node);
86  tupDesc = ExecGetResultType(outerNode);
87 
88  tuplesortstate = tuplesort_begin_heap(tupDesc,
89  plannode->numCols,
90  plannode->sortColIdx,
91  plannode->sortOperators,
92  plannode->collations,
93  plannode->nullsFirst,
94  work_mem,
95  node->randomAccess);
96  if (node->bounded)
97  tuplesort_set_bound(tuplesortstate, node->bound);
98  node->tuplesortstate = (void *) tuplesortstate;
99 
100  /*
101  * Scan the subplan and feed all the tuples to tuplesort.
102  */
103 
104  for (;;)
105  {
106  slot = ExecProcNode(outerNode);
107 
108  if (TupIsNull(slot))
109  break;
110 
111  tuplesort_puttupleslot(tuplesortstate, slot);
112  }
113 
114  /*
115  * Complete the sort.
116  */
117  tuplesort_performsort(tuplesortstate);
118 
119  /*
120  * restore to user specified direction
121  */
122  estate->es_direction = dir;
123 
124  /*
125  * finally set the sorted flag to true
126  */
127  node->sort_Done = true;
128  node->bounded_Done = node->bounded;
129  node->bound_Done = node->bound;
130  SO1_printf("ExecSort: %s\n", "sorting done");
131  }
132 
133  SO1_printf("ExecSort: %s\n",
134  "retrieving tuple from tuplesort");
135 
136  /*
137  * Get the first or next tuple from tuplesort. Returns NULL if no more
138  * tuples. Note that we only rely on slot tuple remaining valid until the
139  * next fetch from the tuplesort.
140  */
141  slot = node->ss.ps.ps_ResultTupleSlot;
142  (void) tuplesort_gettupleslot(tuplesortstate,
144  false, slot, NULL);
145  return slot;
146 }
void tuplesort_performsort(Tuplesortstate *state)
Definition: tuplesort.c:1773
bool bounded_Done
Definition: execnodes.h:1744
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
#define castNode(_type_, nodeptr)
Definition: nodes.h:578
bool bounded
Definition: execnodes.h:1741
int64 bound
Definition: execnodes.h:1742
bool * nullsFirst
Definition: plannodes.h:749
EState * state
Definition: execnodes.h:849
#define SO1_printf(s, p)
Definition: execdebug.h:92
Oid * sortOperators
Definition: plannodes.h:747
ScanDirection es_direction
Definition: execnodes.h:428
PlanState ps
Definition: execnodes.h:1098
void * tuplesortstate
Definition: execnodes.h:1746
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
#define outerPlanState(node)
Definition: execnodes.h:893
bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward, bool copy, TupleTableSlot *slot, Datum *abbrev)
Definition: tuplesort.c:2113
void tuplesort_set_bound(Tuplesortstate *state, int64 bound)
Definition: tuplesort.c:1123
ScanDirection
Definition: sdir.h:22
#define TupIsNull(slot)
Definition: tuptable.h:138
int64 bound_Done
Definition: execnodes.h:1745
ScanState ss
Definition: execnodes.h:1739
int numCols
Definition: plannodes.h:745
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:245
int work_mem
Definition: globals.c:113
Plan * plan
Definition: execnodes.h:847
#define NULL
Definition: c.h:229
bool randomAccess
Definition: execnodes.h:1740
bool sort_Done
Definition: execnodes.h:1743
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:474
AttrNumber * sortColIdx
Definition: plannodes.h:746
Tuplesortstate * tuplesort_begin_heap(TupleDesc tupDesc, int nkeys, AttrNumber *attNums, Oid *sortOperators, Oid *sortCollations, bool *nullsFirstFlags, int workMem, bool randomAccess)
Definition: tuplesort.c:756
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
Oid * collations
Definition: plannodes.h:748
void tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
Definition: tuplesort.c:1364
void ExecSortMarkPos ( SortState node)

Definition at line 263 of file nodeSort.c.

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

Referenced by ExecMarkPos().

264 {
265  /*
266  * if we haven't sorted yet, just return
267  */
268  if (!node->sort_Done)
269  return;
270 
272 }
void * tuplesortstate
Definition: execnodes.h:1746
bool sort_Done
Definition: execnodes.h:1743
void tuplesort_markpos(Tuplesortstate *state)
Definition: tuplesort.c:3166
void ExecSortRestrPos ( SortState node)

Definition at line 281 of file nodeSort.c.

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

Referenced by ExecRestrPos().

282 {
283  /*
284  * if we haven't sorted yet, just return.
285  */
286  if (!node->sort_Done)
287  return;
288 
289  /*
290  * restore the scan to the previously marked position
291  */
293 }
void tuplesort_restorepos(Tuplesortstate *state)
Definition: tuplesort.c:3198
void * tuplesortstate
Definition: execnodes.h:1746
bool sort_Done
Definition: execnodes.h:1743