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

235 {
236  SO1_printf("ExecEndSort: %s\n",
237  "shutting down sort node");
238 
239  /*
240  * clean out the tuple table
241  */
243  /* must drop pointer to sort result tuple */
245 
246  /*
247  * Release tuplesort resources
248  */
249  if (node->tuplesortstate != NULL)
251  node->tuplesortstate = NULL;
252 
253  /*
254  * shut down the subplan
255  */
257 
258  SO1_printf("ExecEndSort: %s\n",
259  "sort node shutdown");
260 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:539
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:475
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1127
#define SO1_printf(s, p)
Definition: execdebug.h:92
PlanState ps
Definition: execnodes.h:1124
void * tuplesortstate
Definition: execnodes.h:1790
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:901
#define outerPlanState(node)
Definition: execnodes.h:914
ScanState ss
Definition: execnodes.h:1783
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1236

◆ ExecInitSort()

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, 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, 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  * initialize child nodes
203  *
204  * We shield the child node from the need to support REWIND, BACKWARD, or
205  * MARK/RESTORE.
206  */
208 
209  outerPlanState(sortstate) = ExecInitNode(outerPlan(node), estate, eflags);
210 
211  /*
212  * Initialize scan slot and type.
213  */
214  ExecCreateScanSlotFromOuterPlan(estate, &sortstate->ss);
215 
216  /*
217  * Initialize return slot and type. No need to initialize projection info because
218  * this node doesn't do projections.
219  */
220  ExecInitResultTupleSlotTL(estate, &sortstate->ss.ps);
221  sortstate->ss.ps.ps_ProjInfo = NULL;
222 
223  SO1_printf("ExecInitSort: %s\n",
224  "sort node initialized");
225 
226  return sortstate;
227 }
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:903
bool bounded
Definition: execnodes.h:1785
EState * state
Definition: execnodes.h:870
#define SO1_printf(s, p)
Definition: execdebug.h:92
PlanState ps
Definition: execnodes.h:1124
void * tuplesortstate
Definition: execnodes.h:1790
static TupleTableSlot * ExecSort(PlanState *pstate)
Definition: nodeSort.c:40
void ExecCreateScanSlotFromOuterPlan(EState *estate, ScanState *scanstate)
Definition: execUtils.c:593
#define EXEC_FLAG_BACKWARD
Definition: executor.h:61
#define outerPlanState(node)
Definition: execnodes.h:914
ScanState ss
Definition: execnodes.h:1783
#define EXEC_FLAG_REWIND
Definition: executor.h:60
#define outerPlan(node)
Definition: plannodes.h:174
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:874
void ExecInitResultTupleSlotTL(EState *estate, PlanState *planstate)
Definition: execTuples.c:870
Plan * plan
Definition: execnodes.h:868
#define makeNode(_type_)
Definition: nodes.h:561
#define EXEC_FLAG_MARK
Definition: executor.h:62
bool randomAccess
Definition: execnodes.h:1784
bool sort_Done
Definition: execnodes.h:1787
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:139

◆ ExecReScanSort()

void ExecReScanSort ( SortState node)

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

303 {
305 
306  /*
307  * If we haven't sorted yet, just return. If outerplan's chgParam is not
308  * NULL then it will be re-scanned by ExecProcNode, else no reason to
309  * re-scan it at all.
310  */
311  if (!node->sort_Done)
312  return;
313 
314  /* must drop pointer to sort result tuple */
316 
317  /*
318  * If subnode is to be rescanned then we forget previous sort results; we
319  * have to re-read the subplan and re-sort. Also must re-sort if the
320  * bounded-sort parameters changed or we didn't select randomAccess.
321  *
322  * Otherwise we can just rewind and rescan the sorted output.
323  */
324  if (outerPlan->chgParam != NULL ||
325  node->bounded != node->bounded_Done ||
326  node->bound != node->bound_Done ||
327  !node->randomAccess)
328  {
329  node->sort_Done = false;
331  node->tuplesortstate = NULL;
332 
333  /*
334  * if chgParam of subnode is not null then plan will be re-scanned by
335  * first ExecProcNode.
336  */
337  if (outerPlan->chgParam == NULL)
338  ExecReScan(outerPlan);
339  }
340  else
342 }
bool bounded_Done
Definition: execnodes.h:1788
bool bounded
Definition: execnodes.h:1785
int64 bound
Definition: execnodes.h:1786
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:475
PlanState ps
Definition: execnodes.h:1124
void * tuplesortstate
Definition: execnodes.h:1790
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:901
void tuplesort_rescan(Tuplesortstate *state)
Definition: tuplesort.c:3027
#define outerPlanState(node)
Definition: execnodes.h:914
int64 bound_Done
Definition: execnodes.h:1789
ScanState ss
Definition: execnodes.h:1783
Bitmapset * chgParam
Definition: execnodes.h:896
#define outerPlan(node)
Definition: plannodes.h:174
bool randomAccess
Definition: execnodes.h:1784
bool sort_Done
Definition: execnodes.h:1787
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1236

◆ ExecSortEstimate()

void ExecSortEstimate ( SortState node,
ParallelContext pcxt 
)

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

357 {
358  Size size;
359 
360  /* don't need this if not instrumenting or no workers */
361  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
362  return;
363 
364  size = mul_size(pcxt->nworkers, sizeof(TuplesortInstrumentation));
365  size = add_size(size, offsetof(SharedSortInfo, sinstrument));
366  shm_toc_estimate_chunk(&pcxt->estimator, size);
367  shm_toc_estimate_keys(&pcxt->estimator, 1);
368 }
Instrumentation * instrument
Definition: execnodes.h:878
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:1124
ScanState ss
Definition: execnodes.h:1783
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:422
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define offsetof(type, field)
Definition: c.h:611

◆ ExecSortInitializeDSM()

void ExecSortInitializeDSM ( SortState node,
ParallelContext pcxt 
)

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

378 {
379  Size size;
380 
381  /* don't need this if not instrumenting or no workers */
382  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
383  return;
384 
385  size = offsetof(SharedSortInfo, sinstrument)
386  + pcxt->nworkers * sizeof(TuplesortInstrumentation);
387  node->shared_info = shm_toc_allocate(pcxt->toc, size);
388  /* ensure any unfilled slots will contain zeroes */
389  memset(node->shared_info, 0, size);
390  node->shared_info->num_workers = pcxt->nworkers;
391  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
392  node->shared_info);
393 }
struct TuplesortInstrumentation TuplesortInstrumentation
Instrumentation * instrument
Definition: execnodes.h:878
int plan_node_id
Definition: plannodes.h:143
SharedSortInfo * shared_info
Definition: execnodes.h:1792
PlanState ps
Definition: execnodes.h:1124
ScanState ss
Definition: execnodes.h:1783
Plan * plan
Definition: execnodes.h:868
size_t Size
Definition: c.h:422
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:611
shm_toc * toc
Definition: parallel.h:44

◆ ExecSortInitializeWorker()

void ExecSortInitializeWorker ( SortState node,
ParallelWorkerContext pwcxt 
)

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

403 {
404  node->shared_info =
405  shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
406  node->am_worker = true;
407 }
int plan_node_id
Definition: plannodes.h:143
SharedSortInfo * shared_info
Definition: execnodes.h:1792
bool am_worker
Definition: execnodes.h:1791
PlanState ps
Definition: execnodes.h:1124
ScanState ss
Definition: execnodes.h:1783
Plan * plan
Definition: execnodes.h:868
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

◆ ExecSortMarkPos()

void ExecSortMarkPos ( SortState node)

Definition at line 269 of file nodeSort.c.

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

Referenced by ExecMarkPos().

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

◆ ExecSortRestrPos()

void ExecSortRestrPos ( SortState node)

Definition at line 287 of file nodeSort.c.

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

Referenced by ExecRestrPos().

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

◆ ExecSortRetrieveInstrumentation()

void ExecSortRetrieveInstrumentation ( SortState node)

Definition at line 416 of file nodeSort.c.

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

Referenced by ExecParallelRetrieveInstrumentation().

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