PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
nodeSort.h File Reference
#include "access/parallel.h"
#include "nodes/execnodes.h"
Include dependency graph for nodeSort.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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 ExecSortReInitializeDSM (SortState *node, ParallelContext *pcxt)
 
void ExecSortInitializeWorker (SortState *node, shm_toc *toc)
 
void ExecSortRetrieveInstrumentation (SortState *node)
 

Function Documentation

void ExecEndSort ( SortState node)

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

239 {
240  SO1_printf("ExecEndSort: %s\n",
241  "shutting down sort node");
242 
243  /*
244  * clean out the tuple table
245  */
247  /* must drop pointer to sort result tuple */
249 
250  /*
251  * Release tuplesort resources
252  */
253  if (node->tuplesortstate != NULL)
255  node->tuplesortstate = NULL;
256 
257  /*
258  * shut down the subplan
259  */
261 
262  SO1_printf("ExecEndSort: %s\n",
263  "sort node shutdown");
264 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:523
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1104
#define SO1_printf(s, p)
Definition: execdebug.h:92
PlanState ps
Definition: execnodes.h:1101
void * tuplesortstate
Definition: execnodes.h:1759
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
#define outerPlanState(node)
Definition: execnodes.h:893
ScanState ss
Definition: execnodes.h:1752
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1104
SortState* ExecInitSort ( Sort node,
EState estate,
int  eflags 
)

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

167 {
168  SortState *sortstate;
169 
170  SO1_printf("ExecInitSort: %s\n",
171  "initializing sort node");
172 
173  /*
174  * create state structure
175  */
176  sortstate = makeNode(SortState);
177  sortstate->ss.ps.plan = (Plan *) node;
178  sortstate->ss.ps.state = estate;
179  sortstate->ss.ps.ExecProcNode = ExecSort;
180 
181  /*
182  * We must have random access to the sort output to do backward scan or
183  * mark/restore. We also prefer to materialize the sort output if we
184  * might be called on to rewind and replay it many times.
185  */
186  sortstate->randomAccess = (eflags & (EXEC_FLAG_REWIND |
188  EXEC_FLAG_MARK)) != 0;
189 
190  sortstate->bounded = false;
191  sortstate->sort_Done = false;
192  sortstate->tuplesortstate = NULL;
193 
194  /*
195  * Miscellaneous initialization
196  *
197  * Sort nodes don't initialize their ExprContexts because they never call
198  * ExecQual or ExecProject.
199  */
200 
201  /*
202  * tuple table initialization
203  *
204  * sort nodes only return scan tuples from their sorted relation.
205  */
206  ExecInitResultTupleSlot(estate, &sortstate->ss.ps);
207  ExecInitScanTupleSlot(estate, &sortstate->ss);
208 
209  /*
210  * initialize child nodes
211  *
212  * We shield the child node from the need to support REWIND, BACKWARD, or
213  * MARK/RESTORE.
214  */
216 
217  outerPlanState(sortstate) = ExecInitNode(outerPlan(node), estate, eflags);
218 
219  /*
220  * initialize tuple type. no need to initialize projection info because
221  * this node doesn't do projections.
222  */
223  ExecAssignResultTypeFromTL(&sortstate->ss.ps);
224  ExecAssignScanTypeFromOuterPlan(&sortstate->ss);
225  sortstate->ss.ps.ps_ProjInfo = NULL;
226 
227  SO1_printf("ExecInitSort: %s\n",
228  "sort node initialized");
229 
230  return sortstate;
231 }
void ExecInitScanTupleSlot(EState *estate, ScanState *scanstate)
Definition: execTuples.c:842
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:882
bool bounded
Definition: execnodes.h:1754
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:1101
void * tuplesortstate
Definition: execnodes.h:1759
static TupleTableSlot * ExecSort(PlanState *pstate)
Definition: nodeSort.c:40
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:1752
#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:558
#define EXEC_FLAG_MARK
Definition: executor.h:61
bool randomAccess
Definition: execnodes.h:1753
bool sort_Done
Definition: execnodes.h:1756
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 306 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().

307 {
309 
310  /*
311  * If we haven't sorted yet, just return. If outerplan's chgParam is not
312  * NULL then it will be re-scanned by ExecProcNode, else no reason to
313  * re-scan it at all.
314  */
315  if (!node->sort_Done)
316  return;
317 
318  /* must drop pointer to sort result tuple */
320 
321  /*
322  * If subnode is to be rescanned then we forget previous sort results; we
323  * have to re-read the subplan and re-sort. Also must re-sort if the
324  * bounded-sort parameters changed or we didn't select randomAccess.
325  *
326  * Otherwise we can just rewind and rescan the sorted output.
327  */
328  if (outerPlan->chgParam != NULL ||
329  node->bounded != node->bounded_Done ||
330  node->bound != node->bound_Done ||
331  !node->randomAccess)
332  {
333  node->sort_Done = false;
335  node->tuplesortstate = NULL;
336 
337  /*
338  * if chgParam of subnode is not null then plan will be re-scanned by
339  * first ExecProcNode.
340  */
341  if (outerPlan->chgParam == NULL)
342  ExecReScan(outerPlan);
343  }
344  else
346 }
bool bounded_Done
Definition: execnodes.h:1757
bool bounded
Definition: execnodes.h:1754
int64 bound
Definition: execnodes.h:1755
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
PlanState ps
Definition: execnodes.h:1101
void * tuplesortstate
Definition: execnodes.h:1759
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:880
void tuplesort_rescan(Tuplesortstate *state)
Definition: tuplesort.c:2840
#define outerPlanState(node)
Definition: execnodes.h:893
int64 bound_Done
Definition: execnodes.h:1758
ScanState ss
Definition: execnodes.h:1752
Bitmapset * chgParam
Definition: execnodes.h:875
#define outerPlan(node)
Definition: plannodes.h:174
bool randomAccess
Definition: execnodes.h:1753
bool sort_Done
Definition: execnodes.h:1756
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1104
void ExecSortEstimate ( SortState node,
ParallelContext pcxt 
)

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

361 {
362  Size size;
363 
364  /* don't need this if not instrumenting or no workers */
365  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
366  return;
367 
368  size = mul_size(pcxt->nworkers, sizeof(TuplesortInstrumentation));
369  size = add_size(size, offsetof(SharedSortInfo, sinstrument));
370  shm_toc_estimate_chunk(&pcxt->estimator, size);
371  shm_toc_estimate_keys(&pcxt->estimator, 1);
372 }
Instrumentation * instrument
Definition: execnodes.h:857
shm_toc_estimator estimator
Definition: parallel.h:41
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
PlanState ps
Definition: execnodes.h:1101
ScanState ss
Definition: execnodes.h:1752
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
size_t Size
Definition: c.h:350
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define offsetof(type, field)
Definition: c.h:549
void ExecSortInitializeDSM ( SortState node,
ParallelContext pcxt 
)

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

382 {
383  Size size;
384 
385  /* don't need this if not instrumenting or no workers */
386  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
387  return;
388 
389  size = offsetof(SharedSortInfo, sinstrument)
390  + pcxt->nworkers * sizeof(TuplesortInstrumentation);
391  node->shared_info = shm_toc_allocate(pcxt->toc, size);
392  /* ensure any unfilled slots will contain zeroes */
393  memset(node->shared_info, 0, size);
394  node->shared_info->num_workers = pcxt->nworkers;
395  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
396  node->shared_info);
397 }
struct TuplesortInstrumentation TuplesortInstrumentation
Instrumentation * instrument
Definition: execnodes.h:857
int plan_node_id
Definition: plannodes.h:143
SharedSortInfo * shared_info
Definition: execnodes.h:1761
PlanState ps
Definition: execnodes.h:1101
ScanState ss
Definition: execnodes.h:1752
Plan * plan
Definition: execnodes.h:847
size_t Size
Definition: c.h:350
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:549
shm_toc * toc
Definition: parallel.h:44
void ExecSortInitializeWorker ( SortState node,
shm_toc toc 
)

Definition at line 423 of file nodeSort.c.

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

Referenced by ExecParallelInitializeWorker().

424 {
425  node->shared_info =
426  shm_toc_lookup(toc, node->ss.ps.plan->plan_node_id, true);
427  node->am_worker = true;
428 }
int plan_node_id
Definition: plannodes.h:143
SharedSortInfo * shared_info
Definition: execnodes.h:1761
bool am_worker
Definition: execnodes.h:1760
PlanState ps
Definition: execnodes.h:1101
ScanState ss
Definition: execnodes.h:1752
Plan * plan
Definition: execnodes.h:847
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
void ExecSortMarkPos ( SortState node)

Definition at line 273 of file nodeSort.c.

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

Referenced by ExecMarkPos().

274 {
275  /*
276  * if we haven't sorted yet, just return
277  */
278  if (!node->sort_Done)
279  return;
280 
282 }
void * tuplesortstate
Definition: execnodes.h:1759
bool sort_Done
Definition: execnodes.h:1756
void tuplesort_markpos(Tuplesortstate *state)
Definition: tuplesort.c:2875
void ExecSortReInitializeDSM ( SortState node,
ParallelContext pcxt 
)

Definition at line 406 of file nodeSort.c.

References SharedSortInfo::num_workers, SortState::shared_info, and SharedSortInfo::sinstrument.

Referenced by ExecParallelReInitializeDSM().

407 {
408  /* If there's any instrumentation space, clear it for next time */
409  if (node->shared_info != NULL)
410  {
411  memset(node->shared_info->sinstrument, 0,
413  }
414 }
SharedSortInfo * shared_info
Definition: execnodes.h:1761
TuplesortInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER]
Definition: execnodes.h:1743
void ExecSortRestrPos ( SortState node)

Definition at line 291 of file nodeSort.c.

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

Referenced by ExecRestrPos().

292 {
293  /*
294  * if we haven't sorted yet, just return.
295  */
296  if (!node->sort_Done)
297  return;
298 
299  /*
300  * restore the scan to the previously marked position
301  */
303 }
void tuplesort_restorepos(Tuplesortstate *state)
Definition: tuplesort.c:2907
void * tuplesortstate
Definition: execnodes.h:1759
bool sort_Done
Definition: execnodes.h:1756
void ExecSortRetrieveInstrumentation ( SortState node)

Definition at line 437 of file nodeSort.c.

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

Referenced by ExecParallelRetrieveInstrumentation().

438 {
439  Size size;
440  SharedSortInfo *si;
441 
442  if (node->shared_info == NULL)
443  return;
444 
445  size = offsetof(SharedSortInfo, sinstrument)
447  si = palloc(size);
448  memcpy(si, node->shared_info, size);
449  node->shared_info = si;
450 }
struct TuplesortInstrumentation TuplesortInstrumentation
SharedSortInfo * shared_info
Definition: execnodes.h:1761
size_t Size
Definition: c.h:350
void * palloc(Size size)
Definition: mcxt.c:848
#define offsetof(type, field)
Definition: c.h:549