PostgreSQL Source Code  git master
nodeGatherMerge.c File Reference
#include "postgres.h"
#include "executor/executor.h"
#include "executor/execParallel.h"
#include "executor/nodeGatherMerge.h"
#include "executor/tqueue.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "optimizer/optimizer.h"
Include dependency graph for nodeGatherMerge.c:

Go to the source code of this file.

Data Structures

struct  GMReaderTupleBuffer
 

Macros

#define MAX_TUPLE_STORE   10
 

Typedefs

typedef struct GMReaderTupleBuffer GMReaderTupleBuffer
 
typedef int32 SlotNumber
 

Functions

static TupleTableSlotExecGatherMerge (PlanState *pstate)
 
static int32 heap_compare_slots (Datum a, Datum b, void *arg)
 
static TupleTableSlotgather_merge_getnext (GatherMergeState *gm_state)
 
static MinimalTuple gm_readnext_tuple (GatherMergeState *gm_state, int nreader, bool nowait, bool *done)
 
static void ExecShutdownGatherMergeWorkers (GatherMergeState *node)
 
static void gather_merge_setup (GatherMergeState *gm_state)
 
static void gather_merge_init (GatherMergeState *gm_state)
 
static void gather_merge_clear_tuples (GatherMergeState *gm_state)
 
static bool gather_merge_readnext (GatherMergeState *gm_state, int reader, bool nowait)
 
static void load_tuple_array (GatherMergeState *gm_state, int reader)
 
GatherMergeStateExecInitGatherMerge (GatherMerge *node, EState *estate, int eflags)
 
void ExecEndGatherMerge (GatherMergeState *node)
 
void ExecShutdownGatherMerge (GatherMergeState *node)
 
void ExecReScanGatherMerge (GatherMergeState *node)
 

Macro Definition Documentation

◆ MAX_TUPLE_STORE

#define MAX_TUPLE_STORE   10

Definition at line 31 of file nodeGatherMerge.c.

Typedef Documentation

◆ GMReaderTupleBuffer

◆ SlotNumber

typedef int32 SlotNumber

Definition at line 739 of file nodeGatherMerge.c.

Function Documentation

◆ ExecEndGatherMerge()

void ExecEndGatherMerge ( GatherMergeState node)

Definition at line 284 of file nodeGatherMerge.c.

285 {
286  ExecEndNode(outerPlanState(node)); /* let children clean up first */
288 }
void ExecEndNode(PlanState *node)
Definition: execProcnode.c:557
#define outerPlanState(node)
Definition: execnodes.h:1213
void ExecShutdownGatherMerge(GatherMergeState *node)

References ExecEndNode(), ExecShutdownGatherMerge(), and outerPlanState.

Referenced by ExecEndNode().

◆ ExecGatherMerge()

static TupleTableSlot * ExecGatherMerge ( PlanState pstate)
static

Definition at line 183 of file nodeGatherMerge.c.

184 {
185  GatherMergeState *node = castNode(GatherMergeState, pstate);
186  TupleTableSlot *slot;
187  ExprContext *econtext;
188 
190 
191  /*
192  * As with Gather, we don't launch workers until this node is actually
193  * executed.
194  */
195  if (!node->initialized)
196  {
197  EState *estate = node->ps.state;
198  GatherMerge *gm = castNode(GatherMerge, node->ps.plan);
199 
200  /*
201  * Sometimes we might have to run without parallelism; but if parallel
202  * mode is active then we can try to fire up some workers.
203  */
204  if (gm->num_workers > 0 && estate->es_use_parallel_mode)
205  {
206  ParallelContext *pcxt;
207 
208  /* Initialize, or re-initialize, shared state needed by workers. */
209  if (!node->pei)
211  estate,
212  gm->initParam,
213  gm->num_workers,
214  node->tuples_needed);
215  else
217  node->pei,
218  gm->initParam);
219 
220  /* Try to launch workers. */
221  pcxt = node->pei->pcxt;
222  LaunchParallelWorkers(pcxt);
223  /* We save # workers launched for the benefit of EXPLAIN */
224  node->nworkers_launched = pcxt->nworkers_launched;
225 
226  /* Set up tuple queue readers to read the results. */
227  if (pcxt->nworkers_launched > 0)
228  {
230  /* Make a working array showing the active readers */
231  node->nreaders = pcxt->nworkers_launched;
232  node->reader = (TupleQueueReader **)
233  palloc(node->nreaders * sizeof(TupleQueueReader *));
234  memcpy(node->reader, node->pei->reader,
235  node->nreaders * sizeof(TupleQueueReader *));
236  }
237  else
238  {
239  /* No workers? Then never mind. */
240  node->nreaders = 0;
241  node->reader = NULL;
242  }
243  }
244 
245  /* allow leader to participate if enabled or no choice */
246  if (parallel_leader_participation || node->nreaders == 0)
247  node->need_to_scan_locally = true;
248  node->initialized = true;
249  }
250 
251  /*
252  * Reset per-tuple memory context to free any expression evaluation
253  * storage allocated in the previous tuple cycle.
254  */
255  econtext = node->ps.ps_ExprContext;
256  ResetExprContext(econtext);
257 
258  /*
259  * Get next tuple, either from one of our workers, or by running the plan
260  * ourselves.
261  */
262  slot = gather_merge_getnext(node);
263  if (TupIsNull(slot))
264  return NULL;
265 
266  /* If no projection is required, we're done. */
267  if (node->ps.ps_ProjInfo == NULL)
268  return slot;
269 
270  /*
271  * Form the result tuple using ExecProject(), and return it.
272  */
273  econtext->ecxt_outertuple = slot;
274  return ExecProject(node->ps.ps_ProjInfo);
275 }
void LaunchParallelWorkers(ParallelContext *pcxt)
Definition: parallel.c:552
ParallelExecutorInfo * ExecInitParallelPlan(PlanState *planstate, EState *estate, Bitmapset *sendParams, int nworkers, int64 tuples_needed)
Definition: execParallel.c:587
void ExecParallelReinitialize(PlanState *planstate, ParallelExecutorInfo *pei, Bitmapset *sendParams)
Definition: execParallel.c:904
void ExecParallelCreateReaders(ParallelExecutorInfo *pei)
Definition: execParallel.c:878
static TupleTableSlot * ExecProject(ProjectionInfo *projInfo)
Definition: executor.h:376
#define ResetExprContext(econtext)
Definition: executor.h:544
void * palloc(Size size)
Definition: mcxt.c:1316
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
static TupleTableSlot * gather_merge_getnext(GatherMergeState *gm_state)
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
bool parallel_leader_participation
Definition: planner.c:68
bool es_use_parallel_mode
Definition: execnodes.h:701
TupleTableSlot * ecxt_outertuple
Definition: execnodes.h:259
struct ParallelExecutorInfo * pei
Definition: execnodes.h:2700
struct TupleQueueReader ** reader
Definition: execnodes.h:2706
bool need_to_scan_locally
Definition: execnodes.h:2694
PlanState ps
Definition: execnodes.h:2691
Bitmapset * initParam
Definition: plannodes.h:1186
int num_workers
Definition: plannodes.h:1160
int nworkers_launched
Definition: parallel.h:37
ParallelContext * pcxt
Definition: execParallel.h:27
struct TupleQueueReader ** reader
Definition: execParallel.h:37
Plan * plan
Definition: execnodes.h:1117
EState * state
Definition: execnodes.h:1119
ExprContext * ps_ExprContext
Definition: execnodes.h:1156
ProjectionInfo * ps_ProjInfo
Definition: execnodes.h:1157
#define TupIsNull(slot)
Definition: tuptable.h:306

References castNode, CHECK_FOR_INTERRUPTS, ExprContext::ecxt_outertuple, EState::es_use_parallel_mode, ExecInitParallelPlan(), ExecParallelCreateReaders(), ExecParallelReinitialize(), ExecProject(), gather_merge_getnext(), GatherMergeState::initialized, GatherMerge::initParam, LaunchParallelWorkers(), GatherMergeState::need_to_scan_locally, GatherMergeState::nreaders, GatherMerge::num_workers, ParallelContext::nworkers_launched, GatherMergeState::nworkers_launched, outerPlanState, palloc(), parallel_leader_participation, ParallelExecutorInfo::pcxt, GatherMergeState::pei, PlanState::plan, GatherMergeState::ps, PlanState::ps_ExprContext, PlanState::ps_ProjInfo, ParallelExecutorInfo::reader, GatherMergeState::reader, ResetExprContext, PlanState::state, TupIsNull, and GatherMergeState::tuples_needed.

Referenced by ExecInitGatherMerge().

◆ ExecInitGatherMerge()

GatherMergeState* ExecInitGatherMerge ( GatherMerge node,
EState estate,
int  eflags 
)

Definition at line 67 of file nodeGatherMerge.c.

68 {
69  GatherMergeState *gm_state;
70  Plan *outerNode;
71  TupleDesc tupDesc;
72 
73  /* Gather merge node doesn't have innerPlan node. */
74  Assert(innerPlan(node) == NULL);
75 
76  /*
77  * create state structure
78  */
79  gm_state = makeNode(GatherMergeState);
80  gm_state->ps.plan = (Plan *) node;
81  gm_state->ps.state = estate;
82  gm_state->ps.ExecProcNode = ExecGatherMerge;
83 
84  gm_state->initialized = false;
85  gm_state->gm_initialized = false;
86  gm_state->tuples_needed = -1;
87 
88  /*
89  * Miscellaneous initialization
90  *
91  * create expression context for node
92  */
93  ExecAssignExprContext(estate, &gm_state->ps);
94 
95  /*
96  * GatherMerge doesn't support checking a qual (it's always more efficient
97  * to do it in the child node).
98  */
99  Assert(!node->plan.qual);
100 
101  /*
102  * now initialize outer plan
103  */
104  outerNode = outerPlan(node);
105  outerPlanState(gm_state) = ExecInitNode(outerNode, estate, eflags);
106 
107  /*
108  * Leader may access ExecProcNode result directly (if
109  * need_to_scan_locally), or from workers via tuple queue. So we can't
110  * trivially rely on the slot type being fixed for expressions evaluated
111  * within this node.
112  */
113  gm_state->ps.outeropsset = true;
114  gm_state->ps.outeropsfixed = false;
115 
116  /*
117  * Store the tuple descriptor into gather merge state, so we can use it
118  * while initializing the gather merge slots.
119  */
120  tupDesc = ExecGetResultType(outerPlanState(gm_state));
121  gm_state->tupDesc = tupDesc;
122 
123  /*
124  * Initialize result type and projection.
125  */
126  ExecInitResultTypeTL(&gm_state->ps);
127  ExecConditionalAssignProjectionInfo(&gm_state->ps, tupDesc, OUTER_VAR);
128 
129  /*
130  * Without projections result slot type is not trivially known, see
131  * comment above.
132  */
133  if (gm_state->ps.ps_ProjInfo == NULL)
134  {
135  gm_state->ps.resultopsset = true;
136  gm_state->ps.resultopsfixed = false;
137  }
138 
139  /*
140  * initialize sort-key information
141  */
142  if (node->numCols)
143  {
144  int i;
145 
146  gm_state->gm_nkeys = node->numCols;
147  gm_state->gm_sortkeys =
148  palloc0(sizeof(SortSupportData) * node->numCols);
149 
150  for (i = 0; i < node->numCols; i++)
151  {
152  SortSupport sortKey = gm_state->gm_sortkeys + i;
153 
154  sortKey->ssup_cxt = CurrentMemoryContext;
155  sortKey->ssup_collation = node->collations[i];
156  sortKey->ssup_nulls_first = node->nullsFirst[i];
157  sortKey->ssup_attno = node->sortColIdx[i];
158 
159  /*
160  * We don't perform abbreviated key conversion here, for the same
161  * reasons that it isn't used in MergeAppend
162  */
163  sortKey->abbreviate = false;
164 
165  PrepareSortSupportFromOrderingOp(node->sortOperators[i], sortKey);
166  }
167  }
168 
169  /* Now allocate the workspace for gather merge */
170  gather_merge_setup(gm_state);
171 
172  return gm_state;
173 }
#define Assert(condition)
Definition: c.h:858
PlanState * ExecInitNode(Plan *node, EState *estate, int eflags)
Definition: execProcnode.c:142
void ExecInitResultTypeTL(PlanState *planstate)
Definition: execTuples.c:1842
TupleDesc ExecGetResultType(PlanState *planstate)
Definition: execUtils.c:493
void ExecAssignExprContext(EState *estate, PlanState *planstate)
Definition: execUtils.c:483
void ExecConditionalAssignProjectionInfo(PlanState *planstate, TupleDesc inputDesc, int varno)
Definition: execUtils.c:558
int i
Definition: isn.c:73
void * palloc0(Size size)
Definition: mcxt.c:1346
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
static void gather_merge_setup(GatherMergeState *gm_state)
static TupleTableSlot * ExecGatherMerge(PlanState *pstate)
#define makeNode(_type_)
Definition: nodes.h:155
#define innerPlan(node)
Definition: plannodes.h:181
#define outerPlan(node)
Definition: plannodes.h:182
#define OUTER_VAR
Definition: primnodes.h:237
void PrepareSortSupportFromOrderingOp(Oid orderingOp, SortSupport ssup)
Definition: sortsupport.c:134
TupleDesc tupDesc
Definition: execnodes.h:2697
SortSupport gm_sortkeys
Definition: execnodes.h:2699
bool outeropsset
Definition: execnodes.h:1200
bool resultopsset
Definition: execnodes.h:1202
bool outeropsfixed
Definition: execnodes.h:1196
bool resultopsfixed
Definition: execnodes.h:1198
ExecProcNodeMtd ExecProcNode
Definition: execnodes.h:1123
List * qual
Definition: plannodes.h:153
AttrNumber ssup_attno
Definition: sortsupport.h:81
bool ssup_nulls_first
Definition: sortsupport.h:75
MemoryContext ssup_cxt
Definition: sortsupport.h:66

References SortSupportData::abbreviate, Assert, CurrentMemoryContext, ExecAssignExprContext(), ExecConditionalAssignProjectionInfo(), ExecGatherMerge(), ExecGetResultType(), ExecInitNode(), ExecInitResultTypeTL(), PlanState::ExecProcNode, gather_merge_setup(), GatherMergeState::gm_initialized, GatherMergeState::gm_nkeys, GatherMergeState::gm_sortkeys, i, GatherMergeState::initialized, innerPlan, makeNode, GatherMerge::numCols, OUTER_VAR, PlanState::outeropsfixed, PlanState::outeropsset, outerPlan, outerPlanState, palloc0(), PlanState::plan, GatherMerge::plan, PrepareSortSupportFromOrderingOp(), GatherMergeState::ps, PlanState::ps_ProjInfo, Plan::qual, PlanState::resultopsfixed, PlanState::resultopsset, SortSupportData::ssup_attno, SortSupportData::ssup_collation, SortSupportData::ssup_cxt, SortSupportData::ssup_nulls_first, PlanState::state, GatherMergeState::tupDesc, and GatherMergeState::tuples_needed.

Referenced by ExecInitNode().

◆ ExecReScanGatherMerge()

void ExecReScanGatherMerge ( GatherMergeState node)

Definition at line 334 of file nodeGatherMerge.c.

335 {
336  GatherMerge *gm = (GatherMerge *) node->ps.plan;
338 
339  /* Make sure any existing workers are gracefully shut down */
341 
342  /* Free any unused tuples, so we don't leak memory across rescans */
344 
345  /* Mark node so that shared state will be rebuilt at next call */
346  node->initialized = false;
347  node->gm_initialized = false;
348 
349  /*
350  * Set child node's chgParam to tell it that the next scan might deliver a
351  * different set of rows within the leader process. (The overall rowset
352  * shouldn't change, but the leader process's subset might; hence nodes
353  * between here and the parallel table scan node mustn't optimize on the
354  * assumption of an unchanging rowset.)
355  */
356  if (gm->rescan_param >= 0)
357  outerPlan->chgParam = bms_add_member(outerPlan->chgParam,
358  gm->rescan_param);
359 
360  /*
361  * If chgParam of subnode is not null then plan will be re-scanned by
362  * first ExecProcNode. Note: because this does nothing if we have a
363  * rescan_param, it's currently guaranteed that parallel-aware child nodes
364  * will not see a ReScan call until after they get a ReInitializeDSM call.
365  * That ordering might not be something to rely on, though. A good rule
366  * of thumb is that ReInitializeDSM should reset only shared state, ReScan
367  * should reset only local state, and anything that depends on both of
368  * those steps being finished must wait until the first ExecProcNode call.
369  */
370  if (outerPlan->chgParam == NULL)
372 }
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
void ExecReScan(PlanState *node)
Definition: execAmi.c:76
static void gather_merge_clear_tuples(GatherMergeState *gm_state)
static void ExecShutdownGatherMergeWorkers(GatherMergeState *node)
int rescan_param
Definition: plannodes.h:1163

References bms_add_member(), ExecReScan(), ExecShutdownGatherMergeWorkers(), gather_merge_clear_tuples(), GatherMergeState::gm_initialized, GatherMergeState::initialized, outerPlan, outerPlanState, PlanState::plan, GatherMergeState::ps, and GatherMerge::rescan_param.

Referenced by ExecReScan().

◆ ExecShutdownGatherMerge()

void ExecShutdownGatherMerge ( GatherMergeState node)

Definition at line 297 of file nodeGatherMerge.c.

298 {
300 
301  /* Now destroy the parallel context. */
302  if (node->pei != NULL)
303  {
304  ExecParallelCleanup(node->pei);
305  node->pei = NULL;
306  }
307 }
void ExecParallelCleanup(ParallelExecutorInfo *pei)

References ExecParallelCleanup(), ExecShutdownGatherMergeWorkers(), and GatherMergeState::pei.

Referenced by ExecEndGatherMerge(), and ExecShutdownNode_walker().

◆ ExecShutdownGatherMergeWorkers()

static void ExecShutdownGatherMergeWorkers ( GatherMergeState node)
static

Definition at line 316 of file nodeGatherMerge.c.

317 {
318  if (node->pei != NULL)
319  ExecParallelFinish(node->pei);
320 
321  /* Flush local copy of reader array */
322  if (node->reader)
323  pfree(node->reader);
324  node->reader = NULL;
325 }
void ExecParallelFinish(ParallelExecutorInfo *pei)
void pfree(void *pointer)
Definition: mcxt.c:1520

References ExecParallelFinish(), GatherMergeState::pei, pfree(), and GatherMergeState::reader.

Referenced by ExecReScanGatherMerge(), and ExecShutdownGatherMerge().

◆ gather_merge_clear_tuples()

static void gather_merge_clear_tuples ( GatherMergeState gm_state)
static

Definition at line 519 of file nodeGatherMerge.c.

520 {
521  int i;
522 
523  for (i = 0; i < gm_state->nreaders; i++)
524  {
525  GMReaderTupleBuffer *tuple_buffer = &gm_state->gm_tuple_buffers[i];
526 
527  while (tuple_buffer->readCounter < tuple_buffer->nTuples)
528  pfree(tuple_buffer->tuple[tuple_buffer->readCounter++]);
529 
530  ExecClearTuple(gm_state->gm_slots[i + 1]);
531  }
532 }
MinimalTuple * tuple
struct GMReaderTupleBuffer * gm_tuple_buffers
Definition: execnodes.h:2707
TupleTableSlot ** gm_slots
Definition: execnodes.h:2705
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:454

References ExecClearTuple(), GatherMergeState::gm_slots, GatherMergeState::gm_tuple_buffers, i, GatherMergeState::nreaders, GMReaderTupleBuffer::nTuples, pfree(), GMReaderTupleBuffer::readCounter, and GMReaderTupleBuffer::tuple.

Referenced by ExecReScanGatherMerge(), and gather_merge_getnext().

◆ gather_merge_getnext()

static TupleTableSlot * gather_merge_getnext ( GatherMergeState gm_state)
static

Definition at line 540 of file nodeGatherMerge.c.

541 {
542  int i;
543 
544  if (!gm_state->gm_initialized)
545  {
546  /*
547  * First time through: pull the first tuple from each participant, and
548  * set up the heap.
549  */
550  gather_merge_init(gm_state);
551  }
552  else
553  {
554  /*
555  * Otherwise, pull the next tuple from whichever participant we
556  * returned from last time, and reinsert that participant's index into
557  * the heap, because it might now compare differently against the
558  * other elements of the heap.
559  */
560  i = DatumGetInt32(binaryheap_first(gm_state->gm_heap));
561 
562  if (gather_merge_readnext(gm_state, i, false))
564  else
565  {
566  /* reader exhausted, remove it from heap */
567  (void) binaryheap_remove_first(gm_state->gm_heap);
568  }
569  }
570 
571  if (binaryheap_empty(gm_state->gm_heap))
572  {
573  /* All the queues are exhausted, and so is the heap */
574  gather_merge_clear_tuples(gm_state);
575  return NULL;
576  }
577  else
578  {
579  /* Return next tuple from whichever participant has the leading one */
580  i = DatumGetInt32(binaryheap_first(gm_state->gm_heap));
581  return gm_state->gm_slots[i];
582  }
583 }
void binaryheap_replace_first(binaryheap *heap, bh_node_type d)
Definition: binaryheap.c:255
bh_node_type binaryheap_first(binaryheap *heap)
Definition: binaryheap.c:177
bh_node_type binaryheap_remove_first(binaryheap *heap)
Definition: binaryheap.c:192
#define binaryheap_empty(h)
Definition: binaryheap.h:65
static void gather_merge_init(GatherMergeState *gm_state)
static bool gather_merge_readnext(GatherMergeState *gm_state, int reader, bool nowait)
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
struct binaryheap * gm_heap
Definition: execnodes.h:2708

References binaryheap_empty, binaryheap_first(), binaryheap_remove_first(), binaryheap_replace_first(), DatumGetInt32(), gather_merge_clear_tuples(), gather_merge_init(), gather_merge_readnext(), GatherMergeState::gm_heap, GatherMergeState::gm_initialized, GatherMergeState::gm_slots, i, and Int32GetDatum().

Referenced by ExecGatherMerge().

◆ gather_merge_init()

static void gather_merge_init ( GatherMergeState gm_state)
static

Definition at line 436 of file nodeGatherMerge.c.

437 {
438  int nreaders = gm_state->nreaders;
439  bool nowait = true;
440  int i;
441 
442  /* Assert that gather_merge_setup made enough space */
443  Assert(nreaders <= castNode(GatherMerge, gm_state->ps.plan)->num_workers);
444 
445  /* Reset leader's tuple slot to empty */
446  gm_state->gm_slots[0] = NULL;
447 
448  /* Reset the tuple slot and tuple array for each worker */
449  for (i = 0; i < nreaders; i++)
450  {
451  /* Reset tuple array to empty */
452  gm_state->gm_tuple_buffers[i].nTuples = 0;
453  gm_state->gm_tuple_buffers[i].readCounter = 0;
454  /* Reset done flag to not-done */
455  gm_state->gm_tuple_buffers[i].done = false;
456  /* Ensure output slot is empty */
457  ExecClearTuple(gm_state->gm_slots[i + 1]);
458  }
459 
460  /* Reset binary heap to empty */
461  binaryheap_reset(gm_state->gm_heap);
462 
463  /*
464  * First, try to read a tuple from each worker (including leader) in
465  * nowait mode. After this, if not all workers were able to produce a
466  * tuple (or a "done" indication), then re-read from remaining workers,
467  * this time using wait mode. Add all live readers (those producing at
468  * least one tuple) to the heap.
469  */
470 reread:
471  for (i = 0; i <= nreaders; i++)
472  {
474 
475  /* skip this source if already known done */
476  if ((i == 0) ? gm_state->need_to_scan_locally :
477  !gm_state->gm_tuple_buffers[i - 1].done)
478  {
479  if (TupIsNull(gm_state->gm_slots[i]))
480  {
481  /* Don't have a tuple yet, try to get one */
482  if (gather_merge_readnext(gm_state, i, nowait))
484  Int32GetDatum(i));
485  }
486  else
487  {
488  /*
489  * We already got at least one tuple from this worker, but
490  * might as well see if it has any more ready by now.
491  */
492  load_tuple_array(gm_state, i);
493  }
494  }
495  }
496 
497  /* need not recheck leader, since nowait doesn't matter for it */
498  for (i = 1; i <= nreaders; i++)
499  {
500  if (!gm_state->gm_tuple_buffers[i - 1].done &&
501  TupIsNull(gm_state->gm_slots[i]))
502  {
503  nowait = false;
504  goto reread;
505  }
506  }
507 
508  /* Now heapify the heap. */
509  binaryheap_build(gm_state->gm_heap);
510 
511  gm_state->gm_initialized = true;
512 }
void binaryheap_build(binaryheap *heap)
Definition: binaryheap.c:138
void binaryheap_reset(binaryheap *heap)
Definition: binaryheap.c:63
void binaryheap_add_unordered(binaryheap *heap, bh_node_type d)
Definition: binaryheap.c:116
static void load_tuple_array(GatherMergeState *gm_state, int reader)

References Assert, binaryheap_add_unordered(), binaryheap_build(), binaryheap_reset(), castNode, CHECK_FOR_INTERRUPTS, GMReaderTupleBuffer::done, ExecClearTuple(), gather_merge_readnext(), GatherMergeState::gm_heap, GatherMergeState::gm_initialized, GatherMergeState::gm_slots, GatherMergeState::gm_tuple_buffers, i, Int32GetDatum(), load_tuple_array(), GatherMergeState::need_to_scan_locally, GatherMergeState::nreaders, GMReaderTupleBuffer::nTuples, PlanState::plan, GatherMergeState::ps, GMReaderTupleBuffer::readCounter, and TupIsNull.

Referenced by gather_merge_getnext().

◆ gather_merge_readnext()

static bool gather_merge_readnext ( GatherMergeState gm_state,
int  reader,
bool  nowait 
)
static

Definition at line 629 of file nodeGatherMerge.c.

630 {
631  GMReaderTupleBuffer *tuple_buffer;
632  MinimalTuple tup;
633 
634  /*
635  * If we're being asked to generate a tuple from the leader, then we just
636  * call ExecProcNode as normal to produce one.
637  */
638  if (reader == 0)
639  {
640  if (gm_state->need_to_scan_locally)
641  {
642  PlanState *outerPlan = outerPlanState(gm_state);
643  TupleTableSlot *outerTupleSlot;
644  EState *estate = gm_state->ps.state;
645 
646  /* Install our DSA area while executing the plan. */
647  estate->es_query_dsa = gm_state->pei ? gm_state->pei->area : NULL;
648  outerTupleSlot = ExecProcNode(outerPlan);
649  estate->es_query_dsa = NULL;
650 
651  if (!TupIsNull(outerTupleSlot))
652  {
653  gm_state->gm_slots[0] = outerTupleSlot;
654  return true;
655  }
656  /* need_to_scan_locally serves as "done" flag for leader */
657  gm_state->need_to_scan_locally = false;
658  }
659  return false;
660  }
661 
662  /* Otherwise, check the state of the relevant tuple buffer. */
663  tuple_buffer = &gm_state->gm_tuple_buffers[reader - 1];
664 
665  if (tuple_buffer->nTuples > tuple_buffer->readCounter)
666  {
667  /* Return any tuple previously read that is still buffered. */
668  tup = tuple_buffer->tuple[tuple_buffer->readCounter++];
669  }
670  else if (tuple_buffer->done)
671  {
672  /* Reader is known to be exhausted. */
673  return false;
674  }
675  else
676  {
677  /* Read and buffer next tuple. */
678  tup = gm_readnext_tuple(gm_state,
679  reader,
680  nowait,
681  &tuple_buffer->done);
682  if (!tup)
683  return false;
684 
685  /*
686  * Attempt to read more tuples in nowait mode and store them in the
687  * pending-tuple array for the reader.
688  */
689  load_tuple_array(gm_state, reader);
690  }
691 
692  Assert(tup);
693 
694  /* Build the TupleTableSlot for the given tuple */
695  ExecStoreMinimalTuple(tup, /* tuple to store */
696  gm_state->gm_slots[reader], /* slot in which to
697  * store the tuple */
698  true); /* pfree tuple when done with it */
699 
700  return true;
701 }
TupleTableSlot * ExecStoreMinimalTuple(MinimalTuple mtup, TupleTableSlot *slot, bool shouldFree)
Definition: execTuples.c:1533
static TupleTableSlot * ExecProcNode(PlanState *node)
Definition: executor.h:269
static MinimalTuple gm_readnext_tuple(GatherMergeState *gm_state, int nreader, bool nowait, bool *done)
struct dsa_area * es_query_dsa
Definition: execnodes.h:704

References ParallelExecutorInfo::area, Assert, GMReaderTupleBuffer::done, EState::es_query_dsa, ExecProcNode(), ExecStoreMinimalTuple(), gm_readnext_tuple(), GatherMergeState::gm_slots, GatherMergeState::gm_tuple_buffers, load_tuple_array(), GatherMergeState::need_to_scan_locally, GMReaderTupleBuffer::nTuples, outerPlan, outerPlanState, GatherMergeState::pei, GatherMergeState::ps, GMReaderTupleBuffer::readCounter, PlanState::state, TupIsNull, and GMReaderTupleBuffer::tuple.

Referenced by gather_merge_getnext(), and gather_merge_init().

◆ gather_merge_setup()

static void gather_merge_setup ( GatherMergeState gm_state)
static

Definition at line 388 of file nodeGatherMerge.c.

389 {
390  GatherMerge *gm = castNode(GatherMerge, gm_state->ps.plan);
391  int nreaders = gm->num_workers;
392  int i;
393 
394  /*
395  * Allocate gm_slots for the number of workers + one more slot for leader.
396  * Slot 0 is always for the leader. Leader always calls ExecProcNode() to
397  * read the tuple, and then stores it directly into its gm_slots entry.
398  * For other slots, code below will call ExecInitExtraTupleSlot() to
399  * create a slot for the worker's results. Note that during any single
400  * scan, we might have fewer than num_workers available workers, in which
401  * case the extra array entries go unused.
402  */
403  gm_state->gm_slots = (TupleTableSlot **)
404  palloc0((nreaders + 1) * sizeof(TupleTableSlot *));
405 
406  /* Allocate the tuple slot and tuple array for each worker */
407  gm_state->gm_tuple_buffers = (GMReaderTupleBuffer *)
408  palloc0(nreaders * sizeof(GMReaderTupleBuffer));
409 
410  for (i = 0; i < nreaders; i++)
411  {
412  /* Allocate the tuple array with length MAX_TUPLE_STORE */
413  gm_state->gm_tuple_buffers[i].tuple =
415 
416  /* Initialize tuple slot for worker */
417  gm_state->gm_slots[i + 1] =
418  ExecInitExtraTupleSlot(gm_state->ps.state, gm_state->tupDesc,
420  }
421 
422  /* Allocate the resources for the merge */
423  gm_state->gm_heap = binaryheap_allocate(nreaders + 1,
425  gm_state);
426 }
binaryheap * binaryheap_allocate(int capacity, binaryheap_comparator compare, void *arg)
Definition: binaryheap.c:39
TupleTableSlot * ExecInitExtraTupleSlot(EState *estate, TupleDesc tupledesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1918
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:86
static int32 heap_compare_slots(Datum a, Datum b, void *arg)
#define MAX_TUPLE_STORE

References binaryheap_allocate(), castNode, ExecInitExtraTupleSlot(), GatherMergeState::gm_heap, GatherMergeState::gm_slots, GatherMergeState::gm_tuple_buffers, heap_compare_slots(), i, MAX_TUPLE_STORE, GatherMerge::num_workers, palloc0(), PlanState::plan, GatherMergeState::ps, PlanState::state, TTSOpsMinimalTuple, GatherMergeState::tupDesc, and GMReaderTupleBuffer::tuple.

Referenced by ExecInitGatherMerge().

◆ gm_readnext_tuple()

static MinimalTuple gm_readnext_tuple ( GatherMergeState gm_state,
int  nreader,
bool  nowait,
bool done 
)
static

Definition at line 707 of file nodeGatherMerge.c.

709 {
710  TupleQueueReader *reader;
711  MinimalTuple tup;
712 
713  /* Check for async events, particularly messages from workers. */
715 
716  /*
717  * Attempt to read a tuple.
718  *
719  * Note that TupleQueueReaderNext will just return NULL for a worker which
720  * fails to initialize. We'll treat that worker as having produced no
721  * tuples; WaitForParallelWorkersToFinish will error out when we get
722  * there.
723  */
724  reader = gm_state->reader[nreader - 1];
725  tup = TupleQueueReaderNext(reader, nowait, done);
726 
727  /*
728  * Since we'll be buffering these across multiple calls, we need to make a
729  * copy.
730  */
731  return tup ? heap_copy_minimal_tuple(tup) : NULL;
732 }
MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup)
Definition: heaptuple.c:1535
MinimalTuple TupleQueueReaderNext(TupleQueueReader *reader, bool nowait, bool *done)
Definition: tqueue.c:176

References CHECK_FOR_INTERRUPTS, heap_copy_minimal_tuple(), GatherMergeState::reader, and TupleQueueReaderNext().

Referenced by gather_merge_readnext(), and load_tuple_array().

◆ heap_compare_slots()

static int32 heap_compare_slots ( Datum  a,
Datum  b,
void *  arg 
)
static

Definition at line 745 of file nodeGatherMerge.c.

746 {
748  SlotNumber slot1 = DatumGetInt32(a);
749  SlotNumber slot2 = DatumGetInt32(b);
750 
751  TupleTableSlot *s1 = node->gm_slots[slot1];
752  TupleTableSlot *s2 = node->gm_slots[slot2];
753  int nkey;
754 
755  Assert(!TupIsNull(s1));
756  Assert(!TupIsNull(s2));
757 
758  for (nkey = 0; nkey < node->gm_nkeys; nkey++)
759  {
760  SortSupport sortKey = node->gm_sortkeys + nkey;
761  AttrNumber attno = sortKey->ssup_attno;
762  Datum datum1,
763  datum2;
764  bool isNull1,
765  isNull2;
766  int compare;
767 
768  datum1 = slot_getattr(s1, attno, &isNull1);
769  datum2 = slot_getattr(s2, attno, &isNull2);
770 
771  compare = ApplySortComparator(datum1, isNull1,
772  datum2, isNull2,
773  sortKey);
774  if (compare != 0)
775  {
777  return compare;
778  }
779  }
780  return 0;
781 }
int16 AttrNumber
Definition: attnum.h:21
#define INVERT_COMPARE_RESULT(var)
Definition: c.h:1106
static int compare(const void *arg1, const void *arg2)
Definition: geqo_pool.c:145
int b
Definition: isn.c:70
int a
Definition: isn.c:69
int32 SlotNumber
void * arg
uintptr_t Datum
Definition: postgres.h:64
char * s1
char * s2
static int ApplySortComparator(Datum datum1, bool isNull1, Datum datum2, bool isNull2, SortSupport ssup)
Definition: sortsupport.h:200
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:395

References a, ApplySortComparator(), arg, Assert, b, compare(), DatumGetInt32(), GatherMergeState::gm_nkeys, GatherMergeState::gm_slots, GatherMergeState::gm_sortkeys, INVERT_COMPARE_RESULT, s1, s2, slot_getattr(), SortSupportData::ssup_attno, and TupIsNull.

Referenced by gather_merge_setup().

◆ load_tuple_array()

static void load_tuple_array ( GatherMergeState gm_state,
int  reader 
)
static

Definition at line 590 of file nodeGatherMerge.c.

591 {
592  GMReaderTupleBuffer *tuple_buffer;
593  int i;
594 
595  /* Don't do anything if this is the leader. */
596  if (reader == 0)
597  return;
598 
599  tuple_buffer = &gm_state->gm_tuple_buffers[reader - 1];
600 
601  /* If there's nothing in the array, reset the counters to zero. */
602  if (tuple_buffer->nTuples == tuple_buffer->readCounter)
603  tuple_buffer->nTuples = tuple_buffer->readCounter = 0;
604 
605  /* Try to fill additional slots in the array. */
606  for (i = tuple_buffer->nTuples; i < MAX_TUPLE_STORE; i++)
607  {
608  MinimalTuple tuple;
609 
610  tuple = gm_readnext_tuple(gm_state,
611  reader,
612  true,
613  &tuple_buffer->done);
614  if (!tuple)
615  break;
616  tuple_buffer->tuple[i] = tuple;
617  tuple_buffer->nTuples++;
618  }
619 }

References GMReaderTupleBuffer::done, gm_readnext_tuple(), GatherMergeState::gm_tuple_buffers, i, MAX_TUPLE_STORE, GMReaderTupleBuffer::nTuples, GMReaderTupleBuffer::readCounter, and GMReaderTupleBuffer::tuple.

Referenced by gather_merge_init(), and gather_merge_readnext().