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 
1082  /* clean out the scan tuple */
1084  /* must drop pointer to sort result tuple */
1086  /* must drop standalone tuple slots from outer node */
1089 
1090  /*
1091  * Release tuplesort resources.
1092  */
1093  if (node->fullsort_state != NULL)
1094  {
1096  node->fullsort_state = NULL;
1097  }
1098  if (node->prefixsort_state != NULL)
1099  {
1101  node->prefixsort_state = NULL;
1102  }
1103 
1104  /*
1105  * Shut down the subplan.
1106  */
1107  ExecEndNode(outerPlanState(node));
1108 
1109  SO_printf("ExecEndIncrementalSort: sort node shutdown\n");
1110 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:557
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
#define SO_printf(s)
Definition: execdebug.h:92
#define outerPlanState(node)
Definition: execnodes.h:1126
Tuplesortstate * prefixsort_state
Definition: execnodes.h:2293
TupleTableSlot * group_pivot
Definition: execnodes.h:2300
TupleTableSlot * transfer_tuple
Definition: execnodes.h:2301
Tuplesortstate * fullsort_state
Definition: execnodes.h:2292
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:1068
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1458
PlanState ps
Definition: execnodes.h:1455
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:972
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:433

References ExecClearTuple(), ExecDropSingleTupleTableSlot(), ExecEndNode(), IncrementalSortState::fullsort_state, IncrementalSortState::group_pivot, outerPlanState, IncrementalSortState::prefixsort_state, ScanState::ps, PlanState::ps_ResultTupleSlot, SO_printf, IncrementalSortState::ss, ScanState::ss_ScanTupleSlot, IncrementalSortState::transfer_tuple, and tuplesort_end().

Referenced by ExecEndNode().

◆ ExecIncrementalSortEstimate()

void ExecIncrementalSortEstimate ( IncrementalSortState node,
ParallelContext pcxt 
)

Definition at line 1186 of file nodeIncrementalSort.c.

1187 {
1188  Size size;
1189 
1190  /* don't need this if not instrumenting or no workers */
1191  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
1192  return;
1193 
1194  size = mul_size(pcxt->nworkers, sizeof(IncrementalSortInfo));
1195  size = add_size(size, offsetof(SharedIncrementalSortInfo, sinfo));
1196  shm_toc_estimate_chunk(&pcxt->estimator, size);
1197  shm_toc_estimate_keys(&pcxt->estimator, 1);
1198 }
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:1040

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 1207 of file nodeIncrementalSort.c.

1208 {
1209  Size size;
1210 
1211  /* don't need this if not instrumenting or no workers */
1212  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
1213  return;
1214 
1215  size = offsetof(SharedIncrementalSortInfo, sinfo)
1216  + pcxt->nworkers * sizeof(IncrementalSortInfo);
1217  node->shared_info = shm_toc_allocate(pcxt->toc, size);
1218  /* ensure any unfilled slots will contain zeroes */
1219  memset(node->shared_info, 0, size);
1220  node->shared_info->num_workers = pcxt->nworkers;
1221  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
1222  node->shared_info);
1223 }
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:2303
shm_toc * toc
Definition: parallel.h:45
Plan * plan
Definition: execnodes.h:1030
int plan_node_id
Definition: plannodes.h:155

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 1232 of file nodeIncrementalSort.c.

1233 {
1234  node->shared_info =
1235  shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
1236  node->am_worker = true;
1237 }
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 1246 of file nodeIncrementalSort.c.

1247 {
1248  Size size;
1250 
1251  if (node->shared_info == NULL)
1252  return;
1253 
1254  size = offsetof(SharedIncrementalSortInfo, sinfo)
1255  + node->shared_info->num_workers * sizeof(IncrementalSortInfo);
1256  si = palloc(size);
1257  memcpy(si, node->shared_info, size);
1258  node->shared_info = si;
1259 }
void * palloc(Size size)
Definition: mcxt.c:1199

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:1799
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1238
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:494
void ExecCreateScanSlotFromOuterPlan(EState *estate, ScanState *scanstate, const TupleTableSlotOps *tts_ops)
Definition: execUtils.c:686
@ INCSORT_LOADFULLSORT
Definition: execnodes.h:2277
#define EXEC_FLAG_BACKWARD
Definition: executor.h:58
#define EXEC_FLAG_MARK
Definition: executor.h:59
Assert(fmt[strlen(fmt) - 1] !='\n')
static TupleTableSlot * ExecIncrementalSort(PlanState *pstate)
#define makeNode(_type_)
Definition: nodes.h:165
#define outerPlan(node)
Definition: plannodes.h:186
IncrementalSortGroupInfo prefixsortGroupInfo
Definition: execnodes.h:2258
IncrementalSortGroupInfo fullsortGroupInfo
Definition: execnodes.h:2257
IncrementalSortExecutionStatus execution_status
Definition: execnodes.h:2290
PresortedKeyData * presorted_keys
Definition: execnodes.h:2295
IncrementalSortInfo incsort_info
Definition: execnodes.h:2297
EState * state
Definition: execnodes.h:1032
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1070
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1036

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 1113 of file nodeIncrementalSort.c.

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

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, IncrementalSortState::presorted_keys, ScanState::ps, PlanState::ps_ResultTupleSlot, IncrementalSortState::ss, IncrementalSortState::transfer_tuple, and tuplesort_reset().

Referenced by ExecReScan().