PostgreSQL Source Code  git master
nodeIncrementalSort.h File Reference
#include "access/parallel.h"
#include "nodes/execnodes.h"
Include dependency graph for nodeIncrementalSort.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

IncrementalSortStateExecInitIncrementalSort (IncrementalSort *node, EState *estate, int eflags)
 
void ExecEndIncrementalSort (IncrementalSortState *node)
 
void ExecReScanIncrementalSort (IncrementalSortState *node)
 
void ExecIncrementalSortEstimate (IncrementalSortState *node, ParallelContext *pcxt)
 
void ExecIncrementalSortInitializeDSM (IncrementalSortState *node, ParallelContext *pcxt)
 
void ExecIncrementalSortInitializeWorker (IncrementalSortState *node, ParallelWorkerContext *pwcxt)
 
void ExecIncrementalSortRetrieveInstrumentation (IncrementalSortState *node)
 

Function Documentation

◆ ExecEndIncrementalSort()

void ExecEndIncrementalSort ( IncrementalSortState node)

Definition at line 1078 of file nodeIncrementalSort.c.

1079 {
1080  SO_printf("ExecEndIncrementalSort: shutting down sort node\n");
1081 
1084 
1085  /*
1086  * Release tuplesort resources.
1087  */
1088  if (node->fullsort_state != NULL)
1089  {
1091  node->fullsort_state = NULL;
1092  }
1093  if (node->prefixsort_state != NULL)
1094  {
1096  node->prefixsort_state = NULL;
1097  }
1098 
1099  /*
1100  * Shut down the subplan.
1101  */
1102  ExecEndNode(outerPlanState(node));
1103 
1104  SO_printf("ExecEndIncrementalSort: sort node shutdown\n");
1105 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:557
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1253
#define SO_printf(s)
Definition: execdebug.h:92
#define outerPlanState(node)
Definition: execnodes.h:1132
Tuplesortstate * prefixsort_state
Definition: execnodes.h:2312
TupleTableSlot * group_pivot
Definition: execnodes.h:2319
TupleTableSlot * transfer_tuple
Definition: execnodes.h:2320
Tuplesortstate * fullsort_state
Definition: execnodes.h:2311
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:969

References ExecDropSingleTupleTableSlot(), ExecEndNode(), IncrementalSortState::fullsort_state, IncrementalSortState::group_pivot, outerPlanState, IncrementalSortState::prefixsort_state, SO_printf, IncrementalSortState::transfer_tuple, and tuplesort_end().

Referenced by ExecEndNode().

◆ ExecIncrementalSortEstimate()

void ExecIncrementalSortEstimate ( IncrementalSortState node,
ParallelContext pcxt 
)

Definition at line 1174 of file nodeIncrementalSort.c.

1175 {
1176  Size size;
1177 
1178  /* don't need this if not instrumenting or no workers */
1179  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
1180  return;
1181 
1182  size = mul_size(pcxt->nworkers, sizeof(IncrementalSortInfo));
1183  size = add_size(size, offsetof(SharedIncrementalSortInfo, sinfo));
1184  shm_toc_estimate_chunk(&pcxt->estimator, size);
1185  shm_toc_estimate_keys(&pcxt->estimator, 1);
1186 }
size_t Size
Definition: c.h:594
#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:494
Size mul_size(Size s1, Size s2)
Definition: shmem.c:511
shm_toc_estimator estimator
Definition: parallel.h:42
Instrumentation * instrument
Definition: execnodes.h:1046
PlanState ps
Definition: execnodes.h:1473

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

Referenced by ExecParallelEstimate().

◆ ExecIncrementalSortInitializeDSM()

void ExecIncrementalSortInitializeDSM ( IncrementalSortState node,
ParallelContext pcxt 
)

Definition at line 1195 of file nodeIncrementalSort.c.

1196 {
1197  Size size;
1198 
1199  /* don't need this if not instrumenting or no workers */
1200  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
1201  return;
1202 
1203  size = offsetof(SharedIncrementalSortInfo, sinfo)
1204  + pcxt->nworkers * sizeof(IncrementalSortInfo);
1205  node->shared_info = shm_toc_allocate(pcxt->toc, size);
1206  /* ensure any unfilled slots will contain zeroes */
1207  memset(node->shared_info, 0, size);
1208  node->shared_info->num_workers = pcxt->nworkers;
1209  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
1210  node->shared_info);
1211 }
struct IncrementalSortInfo IncrementalSortInfo
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
SharedIncrementalSortInfo * shared_info
Definition: execnodes.h:2322
shm_toc * toc
Definition: parallel.h:45
Plan * plan
Definition: execnodes.h:1036
int plan_node_id
Definition: plannodes.h:151

References PlanState::instrument, SharedIncrementalSortInfo::num_workers, ParallelContext::nworkers, PlanState::plan, Plan::plan_node_id, ScanState::ps, IncrementalSortState::shared_info, shm_toc_allocate(), shm_toc_insert(), IncrementalSortState::ss, and ParallelContext::toc.

Referenced by ExecParallelInitializeDSM().

◆ ExecIncrementalSortInitializeWorker()

void ExecIncrementalSortInitializeWorker ( IncrementalSortState node,
ParallelWorkerContext pwcxt 
)

Definition at line 1220 of file nodeIncrementalSort.c.

1221 {
1222  node->shared_info =
1223  shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
1224  node->am_worker = true;
1225 }
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

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

Referenced by ExecParallelInitializeWorker().

◆ ExecIncrementalSortRetrieveInstrumentation()

void ExecIncrementalSortRetrieveInstrumentation ( IncrementalSortState node)

Definition at line 1234 of file nodeIncrementalSort.c.

1235 {
1236  Size size;
1238 
1239  if (node->shared_info == NULL)
1240  return;
1241 
1242  size = offsetof(SharedIncrementalSortInfo, sinfo)
1243  + node->shared_info->num_workers * sizeof(IncrementalSortInfo);
1244  si = palloc(size);
1245  memcpy(si, node->shared_info, size);
1246  node->shared_info = si;
1247 }
void * palloc(Size size)
Definition: mcxt.c:1226

References SharedIncrementalSortInfo::num_workers, palloc(), and IncrementalSortState::shared_info.

Referenced by ExecParallelRetrieveInstrumentation().

◆ ExecInitIncrementalSort()

IncrementalSortState* ExecInitIncrementalSort ( IncrementalSort node,
EState estate,
int  eflags 
)

Definition at line 977 of file nodeIncrementalSort.c.

978 {
979  IncrementalSortState *incrsortstate;
980 
981  SO_printf("ExecInitIncrementalSort: initializing sort node\n");
982 
983  /*
984  * Incremental sort can't be used with EXEC_FLAG_BACKWARD or
985  * EXEC_FLAG_MARK, because the current sort state contains only one sort
986  * batch rather than the full result set.
987  */
988  Assert((eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)) == 0);
989 
990  /* Initialize state structure. */
991  incrsortstate = makeNode(IncrementalSortState);
992  incrsortstate->ss.ps.plan = (Plan *) node;
993  incrsortstate->ss.ps.state = estate;
994  incrsortstate->ss.ps.ExecProcNode = ExecIncrementalSort;
995 
996  incrsortstate->execution_status = INCSORT_LOADFULLSORT;
997  incrsortstate->bounded = false;
998  incrsortstate->outerNodeDone = false;
999  incrsortstate->bound_Done = 0;
1000  incrsortstate->fullsort_state = NULL;
1001  incrsortstate->prefixsort_state = NULL;
1002  incrsortstate->group_pivot = NULL;
1003  incrsortstate->transfer_tuple = NULL;
1004  incrsortstate->n_fullsort_remaining = 0;
1005  incrsortstate->presorted_keys = NULL;
1006 
1007  if (incrsortstate->ss.ps.instrument != NULL)
1008  {
1009  IncrementalSortGroupInfo *fullsortGroupInfo =
1010  &incrsortstate->incsort_info.fullsortGroupInfo;
1011  IncrementalSortGroupInfo *prefixsortGroupInfo =
1012  &incrsortstate->incsort_info.prefixsortGroupInfo;
1013 
1014  fullsortGroupInfo->groupCount = 0;
1015  fullsortGroupInfo->maxDiskSpaceUsed = 0;
1016  fullsortGroupInfo->totalDiskSpaceUsed = 0;
1017  fullsortGroupInfo->maxMemorySpaceUsed = 0;
1018  fullsortGroupInfo->totalMemorySpaceUsed = 0;
1019  fullsortGroupInfo->sortMethods = 0;
1020  prefixsortGroupInfo->groupCount = 0;
1021  prefixsortGroupInfo->maxDiskSpaceUsed = 0;
1022  prefixsortGroupInfo->totalDiskSpaceUsed = 0;
1023  prefixsortGroupInfo->maxMemorySpaceUsed = 0;
1024  prefixsortGroupInfo->totalMemorySpaceUsed = 0;
1025  prefixsortGroupInfo->sortMethods = 0;
1026  }
1027 
1028  /*
1029  * Miscellaneous initialization
1030  *
1031  * Sort nodes don't initialize their ExprContexts because they never call
1032  * ExecQual or ExecProject.
1033  */
1034 
1035  /*
1036  * Initialize child nodes.
1037  *
1038  * Incremental sort does not support backwards scans and mark/restore, so
1039  * we don't bother removing the flags from eflags here. We allow passing a
1040  * REWIND flag, because although incremental sort can't use it, the child
1041  * nodes may be able to do something more useful.
1042  */
1043  outerPlanState(incrsortstate) = ExecInitNode(outerPlan(node), estate, eflags);
1044 
1045  /*
1046  * Initialize scan slot and type.
1047  */
1048  ExecCreateScanSlotFromOuterPlan(estate, &incrsortstate->ss, &TTSOpsMinimalTuple);
1049 
1050  /*
1051  * Initialize return slot and type. No need to initialize projection info
1052  * because we don't do any projections.
1053  */
1055  incrsortstate->ss.ps.ps_ProjInfo = NULL;
1056 
1057  /*
1058  * Initialize standalone slots to store a tuple for pivot prefix keys and
1059  * for carrying over a tuple from one batch to the next.
1060  */
1061  incrsortstate->group_pivot =
1064  incrsortstate->transfer_tuple =
1067 
1068  SO_printf("ExecInitIncrementalSort: sort node initialized\n");
1069 
1070  return incrsortstate;
1071 }
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:142
void ExecInitResultTupleSlotTL(PlanState *planstate, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1798
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1237
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:498
void ExecCreateScanSlotFromOuterPlan(EState *estate, ScanState *scanstate, const TupleTableSlotOps *tts_ops)
Definition: execUtils.c:664
@ INCSORT_LOADFULLSORT
Definition: execnodes.h:2296
#define EXEC_FLAG_BACKWARD
Definition: executor.h:68
#define EXEC_FLAG_MARK
Definition: executor.h:69
Assert(fmt[strlen(fmt) - 1] !='\n')
static TupleTableSlot * ExecIncrementalSort(PlanState *pstate)
#define makeNode(_type_)
Definition: nodes.h:176
#define outerPlan(node)
Definition: plannodes.h:182
IncrementalSortGroupInfo prefixsortGroupInfo
Definition: execnodes.h:2277
IncrementalSortGroupInfo fullsortGroupInfo
Definition: execnodes.h:2276
IncrementalSortExecutionStatus execution_status
Definition: execnodes.h:2309
PresortedKeyData * presorted_keys
Definition: execnodes.h:2314
IncrementalSortInfo incsort_info
Definition: execnodes.h:2316
EState * state
Definition: execnodes.h:1038
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1076
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1042

References Assert(), IncrementalSortState::bound_Done, IncrementalSortState::bounded, EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, ExecCreateScanSlotFromOuterPlan(), ExecGetResultType(), ExecIncrementalSort(), ExecInitNode(), ExecInitResultTupleSlotTL(), PlanState::ExecProcNode, IncrementalSortState::execution_status, IncrementalSortState::fullsort_state, IncrementalSortInfo::fullsortGroupInfo, IncrementalSortState::group_pivot, IncrementalSortGroupInfo::groupCount, IncrementalSortState::incsort_info, INCSORT_LOADFULLSORT, PlanState::instrument, makeNode, MakeSingleTupleTableSlot(), IncrementalSortGroupInfo::maxDiskSpaceUsed, IncrementalSortGroupInfo::maxMemorySpaceUsed, IncrementalSortState::n_fullsort_remaining, IncrementalSortState::outerNodeDone, outerPlan, outerPlanState, PlanState::plan, IncrementalSortState::prefixsort_state, IncrementalSortInfo::prefixsortGroupInfo, IncrementalSortState::presorted_keys, ScanState::ps, PlanState::ps_ProjInfo, SO_printf, IncrementalSortGroupInfo::sortMethods, IncrementalSortState::ss, PlanState::state, IncrementalSortGroupInfo::totalDiskSpaceUsed, IncrementalSortGroupInfo::totalMemorySpaceUsed, IncrementalSortState::transfer_tuple, and TTSOpsMinimalTuple.

Referenced by ExecInitNode().

◆ ExecReScanIncrementalSort()

void ExecReScanIncrementalSort ( IncrementalSortState node)

Definition at line 1108 of file nodeIncrementalSort.c.

1109 {
1111 
1112  /*
1113  * Incremental sort doesn't support efficient rescan even when parameters
1114  * haven't changed (e.g., rewind) because unlike regular sort we don't
1115  * store all tuples at once for the full sort.
1116  *
1117  * So even if EXEC_FLAG_REWIND is set we just reset all of our state and
1118  * re-execute the sort along with the child node. Incremental sort itself
1119  * can't do anything smarter, but maybe the child nodes can.
1120  *
1121  * In theory if we've only filled the full sort with one batch (and
1122  * haven't reset it for a new batch yet) then we could efficiently rewind,
1123  * but that seems a narrow enough case that it's not worth handling
1124  * specially at this time.
1125  */
1126 
1127  /* must drop pointer to sort result tuple */
1129 
1130  if (node->group_pivot != NULL)
1131  ExecClearTuple(node->group_pivot);
1132  if (node->transfer_tuple != NULL)
1134 
1135  node->outerNodeDone = false;
1136  node->n_fullsort_remaining = 0;
1137  node->bound_Done = 0;
1138 
1140 
1141  /*
1142  * If we've set up either of the sort states yet, we need to reset them.
1143  * We could end them and null out the pointers, but there's no reason to
1144  * repay the setup cost, and because ExecIncrementalSort guards presorted
1145  * column functions by checking to see if the full sort state has been
1146  * initialized yet, setting the sort states to null here might actually
1147  * cause a leak.
1148  */
1149  if (node->fullsort_state != NULL)
1151  if (node->prefixsort_state != NULL)
1153 
1154  /*
1155  * If chgParam of subnode is not null, then the plan will be re-scanned by
1156  * the first ExecProcNode.
1157  */
1158  if (outerPlan->chgParam == NULL)
1160 }
void ExecReScan(PlanState *node)
Definition: execAmi.c:78
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1074
void tuplesort_reset(Tuplesortstate *state)
Definition: tuplesort.c:1037
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:433

References IncrementalSortState::bound_Done, ExecClearTuple(), ExecReScan(), IncrementalSortState::execution_status, IncrementalSortState::fullsort_state, IncrementalSortState::group_pivot, INCSORT_LOADFULLSORT, IncrementalSortState::n_fullsort_remaining, IncrementalSortState::outerNodeDone, outerPlan, outerPlanState, IncrementalSortState::prefixsort_state, ScanState::ps, PlanState::ps_ResultTupleSlot, IncrementalSortState::ss, IncrementalSortState::transfer_tuple, and tuplesort_reset().

Referenced by ExecReScan().