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 *pcxt)
 
void ExecIncrementalSortRetrieveInstrumentation (IncrementalSortState *node)
 

Function Documentation

◆ ExecEndIncrementalSort()

void ExecEndIncrementalSort ( IncrementalSortState node)

Definition at line 1076 of file nodeIncrementalSort.c.

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

1077 {
1078  SO_printf("ExecEndIncrementalSort: shutting down sort node\n");
1079 
1080  /* clean out the scan tuple */
1082  /* must drop pointer to sort result tuple */
1084  /* must drop standalone tuple slots from outer node */
1087 
1088  /*
1089  * Release tuplesort resources.
1090  */
1091  if (node->fullsort_state != NULL)
1092  {
1094  node->fullsort_state = NULL;
1095  }
1096  if (node->prefixsort_state != NULL)
1097  {
1099  node->prefixsort_state = NULL;
1100  }
1101 
1102  /*
1103  * Shut down the subplan.
1104  */
1105  ExecEndNode(outerPlanState(node));
1106 
1107  SO_printf("ExecEndIncrementalSort: sort node shutdown\n");
1108 }
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:543
TupleTableSlot * ss_ScanTupleSlot
Definition: execnodes.h:1333
PlanState ps
Definition: execnodes.h:1330
#define SO_printf(s)
Definition: execdebug.h:92
TupleTableSlot * ps_ResultTupleSlot
Definition: execnodes.h:979
#define outerPlanState(node)
Definition: execnodes.h:1035
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1224
Tuplesortstate * fullsort_state
Definition: execnodes.h:2078
Tuplesortstate * prefixsort_state
Definition: execnodes.h:2079
TupleTableSlot * group_pivot
Definition: execnodes.h:2086
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1445
TupleTableSlot * transfer_tuple
Definition: execnodes.h:2087

◆ ExecIncrementalSortEstimate()

void ExecIncrementalSortEstimate ( IncrementalSortState node,
ParallelContext pcxt 
)

Definition at line 1184 of file nodeIncrementalSort.c.

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

Referenced by ExecParallelEstimate().

1185 {
1186  Size size;
1187 
1188  /* don't need this if not instrumenting or no workers */
1189  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
1190  return;
1191 
1192  size = mul_size(pcxt->nworkers, sizeof(IncrementalSortInfo));
1193  size = add_size(size, offsetof(SharedIncrementalSortInfo, sinfo));
1194  shm_toc_estimate_chunk(&pcxt->estimator, size);
1195  shm_toc_estimate_keys(&pcxt->estimator, 1);
1196 }
Instrumentation * instrument
Definition: execnodes.h:951
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:1330
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
size_t Size
Definition: c.h:540
#define shm_toc_estimate_keys(e, cnt)
Definition: shm_toc.h:53
#define offsetof(type, field)
Definition: c.h:727

◆ ExecIncrementalSortInitializeDSM()

void ExecIncrementalSortInitializeDSM ( IncrementalSortState node,
ParallelContext pcxt 
)

Definition at line 1205 of file nodeIncrementalSort.c.

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

1206 {
1207  Size size;
1208 
1209  /* don't need this if not instrumenting or no workers */
1210  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
1211  return;
1212 
1213  size = offsetof(SharedIncrementalSortInfo, sinfo)
1214  + pcxt->nworkers * sizeof(IncrementalSortInfo);
1215  node->shared_info = shm_toc_allocate(pcxt->toc, size);
1216  /* ensure any unfilled slots will contain zeroes */
1217  memset(node->shared_info, 0, size);
1218  node->shared_info->num_workers = pcxt->nworkers;
1219  shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id,
1220  node->shared_info);
1221 }
Instrumentation * instrument
Definition: execnodes.h:951
int plan_node_id
Definition: plannodes.h:135
PlanState ps
Definition: execnodes.h:1330
Plan * plan
Definition: execnodes.h:941
struct IncrementalSortInfo IncrementalSortInfo
size_t Size
Definition: c.h:540
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
SharedIncrementalSortInfo * shared_info
Definition: execnodes.h:2089
#define offsetof(type, field)
Definition: c.h:727
shm_toc * toc
Definition: parallel.h:45

◆ ExecIncrementalSortInitializeWorker()

void ExecIncrementalSortInitializeWorker ( IncrementalSortState node,
ParallelWorkerContext pcxt 
)

Definition at line 1230 of file nodeIncrementalSort.c.

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

1231 {
1232  node->shared_info =
1233  shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
1234  node->am_worker = true;
1235 }
int plan_node_id
Definition: plannodes.h:135
PlanState ps
Definition: execnodes.h:1330
Plan * plan
Definition: execnodes.h:941
SharedIncrementalSortInfo * shared_info
Definition: execnodes.h:2089
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232

◆ ExecIncrementalSortRetrieveInstrumentation()

void ExecIncrementalSortRetrieveInstrumentation ( IncrementalSortState node)

Definition at line 1244 of file nodeIncrementalSort.c.

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

Referenced by ExecParallelRetrieveInstrumentation().

1245 {
1246  Size size;
1248 
1249  if (node->shared_info == NULL)
1250  return;
1251 
1252  size = offsetof(SharedIncrementalSortInfo, sinfo)
1253  + node->shared_info->num_workers * sizeof(IncrementalSortInfo);
1254  si = palloc(size);
1255  memcpy(si, node->shared_info, size);
1256  node->shared_info = si;
1257 }
struct IncrementalSortInfo IncrementalSortInfo
size_t Size
Definition: c.h:540
void * palloc(Size size)
Definition: mcxt.c:950
SharedIncrementalSortInfo * shared_info
Definition: execnodes.h:2089
#define offsetof(type, field)
Definition: c.h:727

◆ ExecInitIncrementalSort()

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

Definition at line 975 of file nodeIncrementalSort.c.

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

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

◆ ExecReScanIncrementalSort()

void ExecReScanIncrementalSort ( IncrementalSortState node)

Definition at line 1111 of file nodeIncrementalSort.c.

References IncrementalSortState::bound_Done, PlanState::chgParam, 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().

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