PostgreSQL Source Code  git master
nodeSort.c File Reference
#include "postgres.h"
#include "access/parallel.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)
 
void ExecSortEstimate (SortState *node, ParallelContext *pcxt)
 
void ExecSortInitializeDSM (SortState *node, ParallelContext *pcxt)
 
void ExecSortInitializeWorker (SortState *node, ParallelWorkerContext *pwcxt)
 
void ExecSortRetrieveInstrumentation (SortState *node)
 

Function Documentation

◆ ExecEndSort()

void ExecEndSort ( SortState node)

Definition at line 235 of file nodeSort.c.

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

Referenced by ExecEndNode().

236 {
237  SO1_printf("ExecEndSort: %s\n",
238  "shutting down sort node");
239 
240  /*
241  * clean out the tuple table
242  */
244  /* must drop pointer to sort result tuple */
246 
247  /*
248  * Release tuplesort resources
249  */
250  if (node->tuplesortstate != NULL)
252  node->tuplesortstate = NULL;
253 
254  /*
255  * shut down the subplan
256  */
258 
259  SO1_printf("ExecEndSort: %s\n",
260  "sort node shutdown");
261 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:543
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1334
#define SO1_printf(s, p)
Definition: execdebug.h:93
PlanState ps
Definition: execnodes.h:1331
void * tuplesortstate
Definition: execnodes.h:2022
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:982
#define outerPlanState(node)
Definition: execnodes.h:1038
ScanState ss
Definition: execnodes.h:2015
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1445

◆ ExecInitSort()

SortState* ExecInitSort ( Sort node,
EState estate,
int  eflags 
)

Definition at line 167 of file nodeSort.c.

References SortState::bounded, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, EXEC_FLAG_REWIND, ExecCreateScanSlotFromOuterPlan(), ExecInitNode(), ExecInitResultTupleSlotTL(), PlanState::ExecProcNode, ExecSort(), makeNode, outerPlan, outerPlanState, PlanState::plan, ScanState::ps, PlanState::ps_ProjInfo, SortState::randomAccess, SO1_printf, SortState::sort_Done, SortState::ss, PlanState::state, TTSOpsMinimalTuple, TTSOpsVirtual, and SortState::tuplesortstate.

Referenced by ExecInitNode().

168 {
169  SortState *sortstate;
170 
171  SO1_printf("ExecInitSort: %s\n",
172  "initializing sort node");
173 
174  /*
175  * create state structure
176  */
177  sortstate = makeNode(SortState);
178  sortstate->ss.ps.plan = (Plan *) node;
179  sortstate->ss.ps.state = estate;
180  sortstate->ss.ps.ExecProcNode = ExecSort;
181 
182  /*
183  * We must have random access to the sort output to do backward scan or
184  * mark/restore. We also prefer to materialize the sort output if we
185  * might be called on to rewind and replay it many times.
186  */
187  sortstate->randomAccess = (eflags & (EXEC_FLAG_REWIND |
189  EXEC_FLAG_MARK)) != 0;
190 
191  sortstate->bounded = false;
192  sortstate->sort_Done = false;
193  sortstate->tuplesortstate = NULL;
194 
195  /*
196  * Miscellaneous initialization
197  *
198  * Sort nodes don't initialize their ExprContexts because they never call
199  * ExecQual or ExecProject.
200  */
201 
202  /*
203  * initialize child nodes
204  *
205  * We shield the child node from the need to support REWIND, BACKWARD, or
206  * MARK/RESTORE.
207  */
209 
210  outerPlanState(sortstate) = ExecInitNode(outerPlan(node), estate, eflags);
211 
212  /*
213  * Initialize scan slot and type.
214  */
215  ExecCreateScanSlotFromOuterPlan(estate, &sortstate->ss, &TTSOpsVirtual);
216 
217  /*
218  * Initialize return slot and type. No need to initialize projection info
219  * because this node doesn't do projections.
220  */
222  sortstate->ss.ps.ps_ProjInfo = NULL;
223 
224  SO1_printf("ExecInitSort: %s\n",
225  "sort node initialized");
226 
227  return sortstate;
228 }
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:984
bool bounded
Definition: execnodes.h:2017
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
EState * state
Definition: execnodes.h:946
#define SO1_printf(s, p)
Definition: execdebug.h:93
PlanState ps
Definition: execnodes.h:1331
void * tuplesortstate
Definition: execnodes.h:2022
static TupleTableSlot * ExecSort(PlanState *pstate)
Definition: nodeSort.c:40
#define EXEC_FLAG_BACKWARD
Definition: executor.h:58
#define outerPlanState(node)
Definition: execnodes.h:1038
ScanState ss
Definition: execnodes.h:2015
#define EXEC_FLAG_REWIND
Definition: executor.h:57
#define outerPlan(node)
Definition: plannodes.h:172
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:950
Plan * plan
Definition: execnodes.h:944
#define makeNode(_type_)
Definition: nodes.h:577
#define EXEC_FLAG_MARK
Definition: executor.h:59
bool randomAccess
Definition: execnodes.h:2016
bool sort_Done
Definition: execnodes.h:2019
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1769
void ExecCreateScanSlotFromOuterPlan(EState *estate, ScanState *scanstate, const TupleTableSlotOps *tts_ops)
Definition: execUtils.c:687
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85

◆ ExecReScanSort()

void ExecReScanSort ( SortState node)

Definition at line 303 of file nodeSort.c.

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

Referenced by ExecReScan().

304 {
306 
307  /*
308  * If we haven't sorted yet, just return. If outerplan's chgParam is not
309  * NULL then it will be re-scanned by ExecProcNode, else no reason to
310  * re-scan it at all.
311  */
312  if (!node->sort_Done)
313  return;
314 
315  /* must drop pointer to sort result tuple */
317 
318  /*
319  * If subnode is to be rescanned then we forget previous sort results; we
320  * have to re-read the subplan and re-sort. Also must re-sort if the
321  * bounded-sort parameters changed or we didn't select randomAccess.
322  *
323  * Otherwise we can just rewind and rescan the sorted output.
324  */
325  if (outerPlan->chgParam != NULL ||
326  node->bounded != node->bounded_Done ||
327  node->bound != node->bound_Done ||
328  !node->randomAccess)
329  {
330  node->sort_Done = false;
332  node->tuplesortstate = NULL;
333 
334  /*
335  * if chgParam of subnode is not null then plan will be re-scanned by
336  * first ExecProcNode.
337  */
338  if (outerPlan->chgParam == NULL)
339  ExecReScan(outerPlan);
340  }
341  else
343 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
bool bounded_Done
Definition: execnodes.h:2020
bool bounded
Definition: execnodes.h:2017
int64 bound
Definition: execnodes.h:2018
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
PlanState ps
Definition: execnodes.h:1331
void * tuplesortstate
Definition: execnodes.h:2022
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:982
void tuplesort_rescan(Tuplesortstate *state)
Definition: tuplesort.c:3258
#define outerPlanState(node)
Definition: execnodes.h:1038
int64 bound_Done
Definition: execnodes.h:2021
ScanState ss
Definition: execnodes.h:2015
Bitmapset * chgParam
Definition: execnodes.h:976
#define outerPlan(node)
Definition: plannodes.h:172
bool randomAccess
Definition: execnodes.h:2016
bool sort_Done
Definition: execnodes.h:2019
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1445

◆ ExecSort()

static TupleTableSlot* ExecSort ( PlanState pstate)
static

Definition at line 40 of file nodeSort.c.

References SortState::am_worker, Assert, SortState::bound, SortState::bound_Done, SortState::bounded, SortState::bounded_Done, castNode, CHECK_FOR_INTERRUPTS, Sort::collations, EState::es_direction, ExecGetResultType(), ExecProcNode(), ForwardScanDirection, IsParallelWorker, Sort::nullsFirst, Sort::numCols, outerPlanState, ParallelWorkerNumber, PlanState::plan, ScanState::ps, PlanState::ps_ResultTupleSlot, SortState::randomAccess, ScanDirectionIsForward, SortState::shared_info, SharedSortInfo::sinstrument, SO1_printf, SortState::sort_Done, Sort::sortColIdx, Sort::sortOperators, SortState::ss, PlanState::state, TupIsNull, tuplesort_begin_heap(), tuplesort_get_stats(), tuplesort_gettupleslot(), tuplesort_performsort(), tuplesort_puttupleslot(), tuplesort_set_bound(), SortState::tuplesortstate, and work_mem.

Referenced by ExecInitSort().

41 {
42  SortState *node = castNode(SortState, pstate);
43  EState *estate;
44  ScanDirection dir;
45  Tuplesortstate *tuplesortstate;
46  TupleTableSlot *slot;
47 
49 
50  /*
51  * get state info from node
52  */
53  SO1_printf("ExecSort: %s\n",
54  "entering routine");
55 
56  estate = node->ss.ps.state;
57  dir = estate->es_direction;
58  tuplesortstate = (Tuplesortstate *) node->tuplesortstate;
59 
60  /*
61  * If first time through, read all tuples from outer plan and pass them to
62  * tuplesort.c. Subsequent calls just fetch tuples from tuplesort.
63  */
64 
65  if (!node->sort_Done)
66  {
67  Sort *plannode = (Sort *) node->ss.ps.plan;
68  PlanState *outerNode;
69  TupleDesc tupDesc;
70 
71  SO1_printf("ExecSort: %s\n",
72  "sorting subplan");
73 
74  /*
75  * Want to scan subplan in the forward direction while creating the
76  * sorted data.
77  */
79 
80  /*
81  * Initialize tuplesort module.
82  */
83  SO1_printf("ExecSort: %s\n",
84  "calling tuplesort_begin");
85 
86  outerNode = outerPlanState(node);
87  tupDesc = ExecGetResultType(outerNode);
88 
89  tuplesortstate = tuplesort_begin_heap(tupDesc,
90  plannode->numCols,
91  plannode->sortColIdx,
92  plannode->sortOperators,
93  plannode->collations,
94  plannode->nullsFirst,
95  work_mem,
96  NULL,
97  node->randomAccess);
98  if (node->bounded)
99  tuplesort_set_bound(tuplesortstate, node->bound);
100  node->tuplesortstate = (void *) tuplesortstate;
101 
102  /*
103  * Scan the subplan and feed all the tuples to tuplesort.
104  */
105 
106  for (;;)
107  {
108  slot = ExecProcNode(outerNode);
109 
110  if (TupIsNull(slot))
111  break;
112 
113  tuplesort_puttupleslot(tuplesortstate, slot);
114  }
115 
116  /*
117  * Complete the sort.
118  */
119  tuplesort_performsort(tuplesortstate);
120 
121  /*
122  * restore to user specified direction
123  */
124  estate->es_direction = dir;
125 
126  /*
127  * finally set the sorted flag to true
128  */
129  node->sort_Done = true;
130  node->bounded_Done = node->bounded;
131  node->bound_Done = node->bound;
132  if (node->shared_info && node->am_worker)
133  {
135 
137  Assert(ParallelWorkerNumber <= node->shared_info->num_workers);
139  tuplesort_get_stats(tuplesortstate, si);
140  }
141  SO1_printf("ExecSort: %s\n", "sorting done");
142  }
143 
144  SO1_printf("ExecSort: %s\n",
145  "retrieving tuple from tuplesort");
146 
147  /*
148  * Get the first or next tuple from tuplesort. Returns NULL if no more
149  * tuples. Note that we only rely on slot tuple remaining valid until the
150  * next fetch from the tuplesort.
151  */
152  slot = node->ss.ps.ps_ResultTupleSlot;
153  (void) tuplesort_gettupleslot(tuplesortstate,
155  false, slot, NULL);
156  return slot;
157 }
void tuplesort_performsort(Tuplesortstate *state)
Definition: tuplesort.c:2021
bool bounded_Done
Definition: execnodes.h:2020
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
#define castNode(_type_, nodeptr)
Definition: nodes.h:598
bool bounded
Definition: execnodes.h:2017
int64 bound
Definition: execnodes.h:2018
SharedSortInfo * shared_info
Definition: execnodes.h:2024
bool * nullsFirst
Definition: plannodes.h:774
bool am_worker
Definition: execnodes.h:2023
EState * state
Definition: execnodes.h:946
#define SO1_printf(s, p)
Definition: execdebug.h:93
void tuplesort_get_stats(Tuplesortstate *state, TuplesortInstrumentation *stats)
Definition: tuplesort.c:3359
Oid * sortOperators
Definition: plannodes.h:772
ScanDirection es_direction
Definition: execnodes.h:504
Tuplesortstate * tuplesort_begin_heap(TupleDesc tupDesc, int nkeys, AttrNumber *attNums, Oid *sortOperators, Oid *sortCollations, bool *nullsFirstFlags, int workMem, SortCoordinate coordinate, bool randomAccess)
Definition: tuplesort.c:878
PlanState ps
Definition: execnodes.h:1331
void * tuplesortstate
Definition: execnodes.h:2022
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:982
#define outerPlanState(node)
Definition: execnodes.h:1038
bool tuplesort_gettupleslot(Tuplesortstate *state, bool forward, bool copy, TupleTableSlot *slot, Datum *abbrev)
Definition: tuplesort.c:2389
void tuplesort_set_bound(Tuplesortstate *state, int64 bound)
Definition: tuplesort.c:1315
ScanDirection
Definition: sdir.h:22
int ParallelWorkerNumber
Definition: parallel.c:112
#define TupIsNull(slot)
Definition: tuptable.h:292
int64 bound_Done
Definition: execnodes.h:2021
ScanState ss
Definition: execnodes.h:2015
#define IsParallelWorker()
Definition: parallel.h:61
int numCols
Definition: plannodes.h:770
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:240
int work_mem
Definition: globals.c:121
Plan * plan
Definition: execnodes.h:944
#define Assert(condition)
Definition: c.h:745
bool randomAccess
Definition: execnodes.h:2016
bool sort_Done
Definition: execnodes.h:2019
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:495
AttrNumber * sortColIdx
Definition: plannodes.h:771
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
Oid * collations
Definition: plannodes.h:773
TuplesortInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER]
Definition: execnodes.h:2006
void tuplesort_puttupleslot(Tuplesortstate *state, TupleTableSlot *slot)
Definition: tuplesort.c:1665

◆ ExecSortEstimate()

void ExecSortEstimate ( SortState node,
ParallelContext pcxt 
)

Definition at line 357 of file nodeSort.c.

References add_size(), ParallelContext::estimator, PlanState::instrument, mul_size(), ParallelContext::nworkers, offsetof, ScanState::ps, shm_toc_estimate_chunk, shm_toc_estimate_keys, and SortState::ss.

Referenced by ExecParallelEstimate().

358 {
359  Size size;
360 
361  /* don't need this if not instrumenting or no workers */
362  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
363  return;
364 
365  size = mul_size(pcxt->nworkers, sizeof(TuplesortInstrumentation));
366  size = add_size(size, offsetof(SharedSortInfo, sinstrument));
367  shm_toc_estimate_chunk(&pcxt->estimator, size);
368  shm_toc_estimate_keys(&pcxt->estimator, 1);
369 }
Instrumentation * instrument
Definition: execnodes.h:954
shm_toc_estimator estimator
Definition: parallel.h:42
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
PlanState ps
Definition: execnodes.h:1331
ScanState ss
Definition: execnodes.h:2015
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
size_t Size
Definition: c.h:473
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define offsetof(type, field)
Definition: c.h:668

◆ ExecSortInitializeDSM()

void ExecSortInitializeDSM ( SortState node,
ParallelContext pcxt 
)

Definition at line 378 of file nodeSort.c.

References PlanState::instrument, SharedSortInfo::num_workers, ParallelContext::nworkers, offsetof, PlanState::plan, Plan::plan_node_id, ScanState::ps, SortState::shared_info, shm_toc_allocate(), shm_toc_insert(), SortState::ss, and ParallelContext::toc.

Referenced by ExecParallelInitializeDSM().

379 {
380  Size size;
381 
382  /* don't need this if not instrumenting or no workers */
383  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
384  return;
385 
386  size = offsetof(SharedSortInfo, sinstrument)
387  + pcxt->nworkers * sizeof(TuplesortInstrumentation);
388  node->shared_info = shm_toc_allocate(pcxt->toc, size);
389  /* ensure any unfilled slots will contain zeroes */
390  memset(node->shared_info, 0, size);
391  node->shared_info->num_workers = pcxt->nworkers;
392  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
393  node->shared_info);
394 }
struct TuplesortInstrumentation TuplesortInstrumentation
Instrumentation * instrument
Definition: execnodes.h:954
int plan_node_id
Definition: plannodes.h:141
SharedSortInfo * shared_info
Definition: execnodes.h:2024
PlanState ps
Definition: execnodes.h:1331
ScanState ss
Definition: execnodes.h:2015
Plan * plan
Definition: execnodes.h:944
size_t Size
Definition: c.h:473
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
#define offsetof(type, field)
Definition: c.h:668
shm_toc * toc
Definition: parallel.h:45

◆ ExecSortInitializeWorker()

void ExecSortInitializeWorker ( SortState node,
ParallelWorkerContext pwcxt 
)

Definition at line 403 of file nodeSort.c.

References SortState::am_worker, PlanState::plan, Plan::plan_node_id, ScanState::ps, SortState::shared_info, shm_toc_lookup(), SortState::ss, and ParallelWorkerContext::toc.

Referenced by ExecParallelInitializeWorker().

404 {
405  node->shared_info =
406  shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
407  node->am_worker = true;
408 }
int plan_node_id
Definition: plannodes.h:141
SharedSortInfo * shared_info
Definition: execnodes.h:2024
bool am_worker
Definition: execnodes.h:2023
PlanState ps
Definition: execnodes.h:1331
ScanState ss
Definition: execnodes.h:2015
Plan * plan
Definition: execnodes.h:944
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

◆ ExecSortMarkPos()

void ExecSortMarkPos ( SortState node)

Definition at line 270 of file nodeSort.c.

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

Referenced by ExecMarkPos().

271 {
272  /*
273  * if we haven't sorted yet, just return
274  */
275  if (!node->sort_Done)
276  return;
277 
279 }
void * tuplesortstate
Definition: execnodes.h:2022
bool sort_Done
Definition: execnodes.h:2019
void tuplesort_markpos(Tuplesortstate *state)
Definition: tuplesort.c:3293

◆ ExecSortRestrPos()

void ExecSortRestrPos ( SortState node)

Definition at line 288 of file nodeSort.c.

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

Referenced by ExecRestrPos().

289 {
290  /*
291  * if we haven't sorted yet, just return.
292  */
293  if (!node->sort_Done)
294  return;
295 
296  /*
297  * restore the scan to the previously marked position
298  */
300 }
void tuplesort_restorepos(Tuplesortstate *state)
Definition: tuplesort.c:3325
void * tuplesortstate
Definition: execnodes.h:2022
bool sort_Done
Definition: execnodes.h:2019

◆ ExecSortRetrieveInstrumentation()

void ExecSortRetrieveInstrumentation ( SortState node)

Definition at line 417 of file nodeSort.c.

References SharedSortInfo::num_workers, offsetof, palloc(), and SortState::shared_info.

Referenced by ExecParallelRetrieveInstrumentation().

418 {
419  Size size;
420  SharedSortInfo *si;
421 
422  if (node->shared_info == NULL)
423  return;
424 
425  size = offsetof(SharedSortInfo, sinstrument)
427  si = palloc(size);
428  memcpy(si, node->shared_info, size);
429  node->shared_info = si;
430 }
struct TuplesortInstrumentation TuplesortInstrumentation
SharedSortInfo * shared_info
Definition: execnodes.h:2024
size_t Size
Definition: c.h:473
void * palloc(Size size)
Definition: mcxt.c:950
#define offsetof(type, field)
Definition: c.h:668