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

1078 {
1079  SO_printf("ExecEndIncrementalSort: shutting down sort node\n");
1080 
1083 
1084  /*
1085  * Release tuplesort resources.
1086  */
1087  if (node->fullsort_state != NULL)
1088  {
1090  node->fullsort_state = NULL;
1091  }
1092  if (node->prefixsort_state != NULL)
1093  {
1095  node->prefixsort_state = NULL;
1096  }
1097 
1098  /*
1099  * Shut down the subplan.
1100  */
1101  ExecEndNode(outerPlanState(node));
1102 
1103  SO_printf("ExecEndIncrementalSort: sort node shutdown\n");
1104 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:557
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1341
#define SO_printf(s)
Definition: execdebug.h:92
#define outerPlanState(node)
Definition: execnodes.h:1213
Tuplesortstate * prefixsort_state
Definition: execnodes.h:2393
TupleTableSlot * group_pivot
Definition: execnodes.h:2400
TupleTableSlot * transfer_tuple
Definition: execnodes.h:2401
Tuplesortstate * fullsort_state
Definition: execnodes.h:2392
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:971

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

1174 {
1175  Size size;
1176 
1177  /* don't need this if not instrumenting or no workers */
1178  if (!node->ss.ps.instrument || pcxt->nworkers == 0)
1179  return;
1180 
1181  size = mul_size(pcxt->nworkers, sizeof(IncrementalSortInfo));
1182  size = add_size(size, offsetof(SharedIncrementalSortInfo, sinfo));
1184  shm_toc_estimate_keys(&pcxt->estimator, 1);
1185 }
size_t Size
Definition: c.h:605
#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:493
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510
static pg_noinline void Size size
Definition: slab.c:607
shm_toc_estimator estimator
Definition: parallel.h:41
Instrumentation * instrument
Definition: execnodes.h:1127
PlanState ps
Definition: execnodes.h:1564

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

Referenced by ExecParallelEstimate().

◆ ExecIncrementalSortInitializeDSM()

void ExecIncrementalSortInitializeDSM ( IncrementalSortState node,
ParallelContext pcxt 
)

Definition at line 1194 of file nodeIncrementalSort.c.

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

Referenced by ExecParallelInitializeDSM().

◆ ExecIncrementalSortInitializeWorker()

void ExecIncrementalSortInitializeWorker ( IncrementalSortState node,
ParallelWorkerContext pwcxt 
)

Definition at line 1219 of file nodeIncrementalSort.c.

1220 {
1221  node->shared_info =
1222  shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, true);
1223  node->am_worker = true;
1224 }
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 1233 of file nodeIncrementalSort.c.

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

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

Referenced by ExecParallelRetrieveInstrumentation().

◆ ExecInitIncrementalSort()

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

Definition at line 976 of file nodeIncrementalSort.c.

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

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

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

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