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 301 of file nodeSort.c.

302 {
303  SO1_printf("ExecEndSort: %s\n",
304  "shutting down sort node");
305 
306  /*
307  * clean out the tuple table
308  */
310  /* must drop pointer to sort result tuple */
312 
313  /*
314  * Release tuplesort resources
315  */
316  if (node->tuplesortstate != NULL)
318  node->tuplesortstate = NULL;
319 
320  /*
321  * shut down the subplan
322  */
324 
325  SO1_printf("ExecEndSort: %s\n",
326  "sort node shutdown");
327 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:557
#define SO1_printf(s, p)
Definition: execdebug.h:93
#define outerPlanState(node)
Definition: execnodes.h:1120
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1062
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1452
PlanState ps
Definition: execnodes.h:1449
void * tuplesortstate
Definition: execnodes.h:2229
ScanState ss
Definition: execnodes.h:2222
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:972
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:433

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

Referenced by ExecEndNode().

◆ ExecInitSort()

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

Definition at line 221 of file nodeSort.c.

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

References SortState::bounded, SortState::datumSort, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, EXEC_FLAG_REWIND, ExecCreateScanSlotFromOuterPlan(), ExecGetResultType(), ExecInitNode(), ExecInitResultTupleSlotTL(), PlanState::ExecProcNode, ExecSort(), makeNode, TupleDescData::natts, 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().

◆ ExecReScanSort()

void ExecReScanSort ( SortState node)

Definition at line 369 of file nodeSort.c.

370 {
372 
373  /*
374  * If we haven't sorted yet, just return. If outerplan's chgParam is not
375  * NULL then it will be re-scanned by ExecProcNode, else no reason to
376  * re-scan it at all.
377  */
378  if (!node->sort_Done)
379  return;
380 
381  /* must drop pointer to sort result tuple */
383 
384  /*
385  * If subnode is to be rescanned then we forget previous sort results; we
386  * have to re-read the subplan and re-sort. Also must re-sort if the
387  * bounded-sort parameters changed or we didn't select randomAccess.
388  *
389  * Otherwise we can just rewind and rescan the sorted output.
390  */
391  if (outerPlan->chgParam != NULL ||
392  node->bounded != node->bounded_Done ||
393  node->bound != node->bound_Done ||
394  !node->randomAccess)
395  {
396  node->sort_Done = false;
398  node->tuplesortstate = NULL;
399 
400  /*
401  * if chgParam of subnode is not null then plan will be re-scanned by
402  * first ExecProcNode.
403  */
404  if (outerPlan->chgParam == NULL)
406  }
407  else
409 }
void ExecReScan(PlanState *node)
Definition: execAmi.c:78
int64 bound_Done
Definition: execnodes.h:2228
bool bounded_Done
Definition: execnodes.h:2227
int64 bound
Definition: execnodes.h:2225
void tuplesort_rescan(Tuplesortstate *state)
Definition: tuplesort.c:2440

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

◆ ExecSortEstimate()

void ExecSortEstimate ( SortState node,
ParallelContext pcxt 
)

Definition at line 423 of file nodeSort.c.

424 {
425  Size size;
426 
427  /* don't need this if not instrumenting or no workers */
428  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
429  return;
430 
431  size = mul_size(pcxt->nworkers, sizeof(TuplesortInstrumentation));
432  size = add_size(size, offsetof(SharedSortInfo, sinstrument));
433  shm_toc_estimate_chunk(&pcxt->estimator, size);
434  shm_toc_estimate_keys(&pcxt->estimator, 1);
435 }
size_t Size
Definition: c.h:541
#define shm_toc_estimate_chunk(e, sz)
Definition: shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
shm_toc_estimator estimator
Definition: parallel.h:42
Instrumentation * instrument
Definition: execnodes.h:1034

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

Referenced by ExecParallelEstimate().

◆ ExecSortInitializeDSM()

void ExecSortInitializeDSM ( SortState node,
ParallelContext pcxt 
)

Definition at line 444 of file nodeSort.c.

445 {
446  Size size;
447 
448  /* don't need this if not instrumenting or no workers */
449  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
450  return;
451 
452  size = offsetof(SharedSortInfo, sinstrument)
453  + pcxt->nworkers * sizeof(TuplesortInstrumentation);
454  node->shared_info = shm_toc_allocate(pcxt->toc, size);
455  /* ensure any unfilled slots will contain zeroes */
456  memset(node->shared_info, 0, size);
457  node->shared_info->num_workers = pcxt->nworkers;
458  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
459  node->shared_info);
460 }
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition: shm_toc.c:171
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition: shm_toc.c:88
shm_toc * toc
Definition: parallel.h:45
int plan_node_id
Definition: plannodes.h:149
SharedSortInfo * shared_info
Definition: execnodes.h:2232
struct TuplesortInstrumentation TuplesortInstrumentation

References PlanState::instrument, SharedSortInfo::num_workers, ParallelContext::nworkers, 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().

◆ ExecSortInitializeWorker()

void ExecSortInitializeWorker ( SortState node,
ParallelWorkerContext pwcxt 
)

Definition at line 469 of file nodeSort.c.

470 {
471  node->shared_info =
472  shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
473  node->am_worker = true;
474 }
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
bool am_worker
Definition: execnodes.h:2230

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().

◆ ExecSortMarkPos()

void ExecSortMarkPos ( SortState node)

Definition at line 336 of file nodeSort.c.

337 {
338  /*
339  * if we haven't sorted yet, just return
340  */
341  if (!node->sort_Done)
342  return;
343 
345 }
void tuplesort_markpos(Tuplesortstate *state)
Definition: tuplesort.c:2473

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

Referenced by ExecMarkPos().

◆ ExecSortRestrPos()

void ExecSortRestrPos ( SortState node)

Definition at line 354 of file nodeSort.c.

355 {
356  /*
357  * if we haven't sorted yet, just return.
358  */
359  if (!node->sort_Done)
360  return;
361 
362  /*
363  * restore the scan to the previously marked position
364  */
366 }
void tuplesort_restorepos(Tuplesortstate *state)
Definition: tuplesort.c:2504

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

Referenced by ExecRestrPos().

◆ ExecSortRetrieveInstrumentation()

void ExecSortRetrieveInstrumentation ( SortState node)

Definition at line 483 of file nodeSort.c.

484 {
485  Size size;
486  SharedSortInfo *si;
487 
488  if (node->shared_info == NULL)
489  return;
490 
491  size = offsetof(SharedSortInfo, sinstrument)
493  si = palloc(size);
494  memcpy(si, node->shared_info, size);
495  node->shared_info = si;
496 }
void * palloc(Size size)
Definition: mcxt.c:1199

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

Referenced by ExecParallelRetrieveInstrumentation().