PostgreSQL Source Code  git master
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 ExecSortInitializeWorker (SortState *node, ParallelWorkerContext *pwcxt)
 
void ExecSortRetrieveInstrumentation (SortState *node)
 

Function Documentation

◆ ExecEndSort()

void ExecEndSort ( SortState node)

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

292 {
293  SO1_printf("ExecEndSort: %s\n",
294  "shutting down sort node");
295 
296  /*
297  * clean out the tuple table
298  */
300  /* must drop pointer to sort result tuple */
302 
303  /*
304  * Release tuplesort resources
305  */
306  if (node->tuplesortstate != NULL)
308  node->tuplesortstate = NULL;
309 
310  /*
311  * shut down the subplan
312  */
314 
315  SO1_printf("ExecEndSort: %s\n",
316  "sort node shutdown");
317 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:556
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1380
#define SO1_printf(s, p)
Definition: execdebug.h:93
PlanState ps
Definition: execnodes.h:1377
void * tuplesortstate
Definition: execnodes.h:2152
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1004
#define outerPlanState(node)
Definition: execnodes.h:1062
ScanState ss
Definition: execnodes.h:2145
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1464

◆ ExecInitSort()

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

Definition at line 214 of file nodeSort.c.

References SortState::bounded, SortState::datumSort, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, EXEC_FLAG_REWIND, ExecCreateScanSlotFromOuterPlan(), ExecGetResultType(), 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().

215 {
216  SortState *sortstate;
217 
218  SO1_printf("ExecInitSort: %s\n",
219  "initializing sort node");
220 
221  /*
222  * create state structure
223  */
224  sortstate = makeNode(SortState);
225  sortstate->ss.ps.plan = (Plan *) node;
226  sortstate->ss.ps.state = estate;
227  sortstate->ss.ps.ExecProcNode = ExecSort;
228 
229  /*
230  * We must have random access to the sort output to do backward scan or
231  * mark/restore. We also prefer to materialize the sort output if we
232  * might be called on to rewind and replay it many times.
233  */
234  sortstate->randomAccess = (eflags & (EXEC_FLAG_REWIND |
236  EXEC_FLAG_MARK)) != 0;
237 
238  sortstate->bounded = false;
239  sortstate->sort_Done = false;
240  sortstate->tuplesortstate = NULL;
241 
242  /*
243  * Miscellaneous initialization
244  *
245  * Sort nodes don't initialize their ExprContexts because they never call
246  * ExecQual or ExecProject.
247  */
248 
249  /*
250  * initialize child nodes
251  *
252  * We shield the child node from the need to support REWIND, BACKWARD, or
253  * MARK/RESTORE.
254  */
256 
257  outerPlanState(sortstate) = ExecInitNode(outerPlan(node), estate, eflags);
258 
259  /*
260  * Initialize scan slot and type.
261  */
262  ExecCreateScanSlotFromOuterPlan(estate, &sortstate->ss, &TTSOpsVirtual);
263 
264  /*
265  * Initialize return slot and type. No need to initialize projection info
266  * because this node doesn't do projections.
267  */
269  sortstate->ss.ps.ps_ProjInfo = NULL;
270 
271  /*
272  * We perform a Datum sort when we're sorting just a single column,
273  * otherwise we perform a tuple sort.
274  */
275  if (ExecGetResultType(outerPlanState(sortstate))->natts == 1)
276  sortstate->datumSort = true;
277  else
278  sortstate->datumSort = false;
279 
280  SO1_printf("ExecInitSort: %s\n",
281  "sort node initialized");
282 
283  return sortstate;
284 }
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1006
bool bounded
Definition: execnodes.h:2147
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
EState * state
Definition: execnodes.h:968
#define SO1_printf(s, p)
Definition: execdebug.h:93
PlanState ps
Definition: execnodes.h:1377
void * tuplesortstate
Definition: execnodes.h:2152
static TupleTableSlot * ExecSort(PlanState *pstate)
Definition: nodeSort.c:50
#define EXEC_FLAG_BACKWARD
Definition: executor.h:58
#define outerPlanState(node)
Definition: execnodes.h:1062
bool datumSort
Definition: execnodes.h:2154
ScanState ss
Definition: execnodes.h:2145
#define EXEC_FLAG_REWIND
Definition: executor.h:57
#define outerPlan(node)
Definition: plannodes.h:171
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:972
Plan * plan
Definition: execnodes.h:966
#define makeNode(_type_)
Definition: nodes.h:584
#define EXEC_FLAG_MARK
Definition: executor.h:59
bool randomAccess
Definition: execnodes.h:2146
bool sort_Done
Definition: execnodes.h:2149
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1799
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:490
void ExecCreateScanSlotFromOuterPlan(EState *estate, ScanState *scanstate, const TupleTableSlotOps *tts_ops)
Definition: execUtils.c:682
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:141
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85

◆ ExecReScanSort()

void ExecReScanSort ( SortState node)

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

360 {
362 
363  /*
364  * If we haven't sorted yet, just return. If outerplan's chgParam is not
365  * NULL then it will be re-scanned by ExecProcNode, else no reason to
366  * re-scan it at all.
367  */
368  if (!node->sort_Done)
369  return;
370 
371  /* must drop pointer to sort result tuple */
373 
374  /*
375  * If subnode is to be rescanned then we forget previous sort results; we
376  * have to re-read the subplan and re-sort. Also must re-sort if the
377  * bounded-sort parameters changed or we didn't select randomAccess.
378  *
379  * Otherwise we can just rewind and rescan the sorted output.
380  */
381  if (outerPlan->chgParam != NULL ||
382  node->bounded != node->bounded_Done ||
383  node->bound != node->bound_Done ||
384  !node->randomAccess)
385  {
386  node->sort_Done = false;
388  node->tuplesortstate = NULL;
389 
390  /*
391  * if chgParam of subnode is not null then plan will be re-scanned by
392  * first ExecProcNode.
393  */
394  if (outerPlan->chgParam == NULL)
395  ExecReScan(outerPlan);
396  }
397  else
399 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
bool bounded_Done
Definition: execnodes.h:2150
bool bounded
Definition: execnodes.h:2147
int64 bound
Definition: execnodes.h:2148
void ExecReScan(PlanState *node)
Definition: execAmi.c:78
PlanState ps
Definition: execnodes.h:1377
void * tuplesortstate
Definition: execnodes.h:2152
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1004
void tuplesort_rescan(Tuplesortstate *state)
Definition: tuplesort.c:3277
#define outerPlanState(node)
Definition: execnodes.h:1062
int64 bound_Done
Definition: execnodes.h:2151
ScanState ss
Definition: execnodes.h:2145
Bitmapset * chgParam
Definition: execnodes.h:998
#define outerPlan(node)
Definition: plannodes.h:171
bool randomAccess
Definition: execnodes.h:2146
bool sort_Done
Definition: execnodes.h:2149
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1464

◆ ExecSortEstimate()

void ExecSortEstimate ( SortState node,
ParallelContext pcxt 
)

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

414 {
415  Size size;
416 
417  /* don't need this if not instrumenting or no workers */
418  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
419  return;
420 
421  size = mul_size(pcxt->nworkers, sizeof(TuplesortInstrumentation));
422  size = add_size(size, offsetof(SharedSortInfo, sinstrument));
423  shm_toc_estimate_chunk(&pcxt->estimator, size);
424  shm_toc_estimate_keys(&pcxt->estimator, 1);
425 }
Instrumentation * instrument
Definition: execnodes.h:976
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:1377
ScanState ss
Definition: execnodes.h:2145
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
size_t Size
Definition: c.h:540
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define offsetof(type, field)
Definition: c.h:727

◆ ExecSortInitializeDSM()

void ExecSortInitializeDSM ( SortState node,
ParallelContext pcxt 
)

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

435 {
436  Size size;
437 
438  /* don't need this if not instrumenting or no workers */
439  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
440  return;
441 
442  size = offsetof(SharedSortInfo, sinstrument)
443  + pcxt->nworkers * sizeof(TuplesortInstrumentation);
444  node->shared_info = shm_toc_allocate(pcxt->toc, size);
445  /* ensure any unfilled slots will contain zeroes */
446  memset(node->shared_info, 0, size);
447  node->shared_info->num_workers = pcxt->nworkers;
448  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
449  node->shared_info);
450 }
struct TuplesortInstrumentation TuplesortInstrumentation
Instrumentation * instrument
Definition: execnodes.h:976
int plan_node_id
Definition: plannodes.h:140
SharedSortInfo * shared_info
Definition: execnodes.h:2155
PlanState ps
Definition: execnodes.h:1377
ScanState ss
Definition: execnodes.h:2145
Plan * plan
Definition: execnodes.h:966
size_t Size
Definition: c.h:540
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:727
shm_toc * toc
Definition: parallel.h:45

◆ ExecSortInitializeWorker()

void ExecSortInitializeWorker ( SortState node,
ParallelWorkerContext pwcxt 
)

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

460 {
461  node->shared_info =
462  shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
463  node->am_worker = true;
464 }
int plan_node_id
Definition: plannodes.h:140
SharedSortInfo * shared_info
Definition: execnodes.h:2155
bool am_worker
Definition: execnodes.h:2153
PlanState ps
Definition: execnodes.h:1377
ScanState ss
Definition: execnodes.h:2145
Plan * plan
Definition: execnodes.h:966
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

◆ ExecSortMarkPos()

void ExecSortMarkPos ( SortState node)

Definition at line 326 of file nodeSort.c.

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

Referenced by ExecMarkPos().

327 {
328  /*
329  * if we haven't sorted yet, just return
330  */
331  if (!node->sort_Done)
332  return;
333 
335 }
void * tuplesortstate
Definition: execnodes.h:2152
bool sort_Done
Definition: execnodes.h:2149
void tuplesort_markpos(Tuplesortstate *state)
Definition: tuplesort.c:3312

◆ ExecSortRestrPos()

void ExecSortRestrPos ( SortState node)

Definition at line 344 of file nodeSort.c.

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

Referenced by ExecRestrPos().

345 {
346  /*
347  * if we haven't sorted yet, just return.
348  */
349  if (!node->sort_Done)
350  return;
351 
352  /*
353  * restore the scan to the previously marked position
354  */
356 }
void tuplesort_restorepos(Tuplesortstate *state)
Definition: tuplesort.c:3344
void * tuplesortstate
Definition: execnodes.h:2152
bool sort_Done
Definition: execnodes.h:2149

◆ ExecSortRetrieveInstrumentation()

void ExecSortRetrieveInstrumentation ( SortState node)

Definition at line 473 of file nodeSort.c.

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

Referenced by ExecParallelRetrieveInstrumentation().

474 {
475  Size size;
476  SharedSortInfo *si;
477 
478  if (node->shared_info == NULL)
479  return;
480 
481  size = offsetof(SharedSortInfo, sinstrument)
483  si = palloc(size);
484  memcpy(si, node->shared_info, size);
485  node->shared_info = si;
486 }
struct TuplesortInstrumentation TuplesortInstrumentation
SharedSortInfo * shared_info
Definition: execnodes.h:2155
size_t Size
Definition: c.h:540
void * palloc(Size size)
Definition: mcxt.c:1062
#define offsetof(type, field)
Definition: c.h:727