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 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:1331
#define SO1_printf(s, p)
Definition: execdebug.h:93
PlanState ps
Definition: execnodes.h:1328
void * tuplesortstate
Definition: execnodes.h:2020
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:977
#define outerPlanState(node)
Definition: execnodes.h:1033
ScanState ss
Definition: execnodes.h:2013
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:979
bool bounded
Definition: execnodes.h:2015
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
EState * state
Definition: execnodes.h:941
#define SO1_printf(s, p)
Definition: execdebug.h:93
PlanState ps
Definition: execnodes.h:1328
void * tuplesortstate
Definition: execnodes.h:2020
static TupleTableSlot * ExecSort(PlanState *pstate)
Definition: nodeSort.c:40
#define EXEC_FLAG_BACKWARD
Definition: executor.h:58
#define outerPlanState(node)
Definition: execnodes.h:1033
ScanState ss
Definition: execnodes.h:2013
#define EXEC_FLAG_REWIND
Definition: executor.h:57
#define outerPlan(node)
Definition: plannodes.h:166
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:945
Plan * plan
Definition: execnodes.h:939
#define makeNode(_type_)
Definition: nodes.h:576
#define EXEC_FLAG_MARK
Definition: executor.h:59
bool randomAccess
Definition: execnodes.h:2014
bool sort_Done
Definition: execnodes.h:2017
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:681
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:2018
bool bounded
Definition: execnodes.h:2015
int64 bound
Definition: execnodes.h:2016
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
PlanState ps
Definition: execnodes.h:1328
void * tuplesortstate
Definition: execnodes.h:2020
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:977
void tuplesort_rescan(Tuplesortstate *state)
Definition: tuplesort.c:3258
#define outerPlanState(node)
Definition: execnodes.h:1033
int64 bound_Done
Definition: execnodes.h:2019
ScanState ss
Definition: execnodes.h:2013
Bitmapset * chgParam
Definition: execnodes.h:971
#define outerPlan(node)
Definition: plannodes.h:166
bool randomAccess
Definition: execnodes.h:2014
bool sort_Done
Definition: execnodes.h:2017
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1445

◆ 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:949
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:1328
ScanState ss
Definition: execnodes.h:2013
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:474
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define offsetof(type, field)
Definition: c.h:669

◆ 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:949
int plan_node_id
Definition: plannodes.h:135
SharedSortInfo * shared_info
Definition: execnodes.h:2022
PlanState ps
Definition: execnodes.h:1328
ScanState ss
Definition: execnodes.h:2013
Plan * plan
Definition: execnodes.h:939
size_t Size
Definition: c.h:474
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:669
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:135
SharedSortInfo * shared_info
Definition: execnodes.h:2022
bool am_worker
Definition: execnodes.h:2021
PlanState ps
Definition: execnodes.h:1328
ScanState ss
Definition: execnodes.h:2013
Plan * plan
Definition: execnodes.h:939
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:2020
bool sort_Done
Definition: execnodes.h:2017
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:2020
bool sort_Done
Definition: execnodes.h:2017

◆ 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:2022
size_t Size
Definition: c.h:474
void * palloc(Size size)
Definition: mcxt.c:950
#define offsetof(type, field)
Definition: c.h:669