Commit 04e96786 authored by Tom Lane's avatar Tom Lane

Code review for nodeGatherMerge.c.

Comment the fields of GatherMergeState, and organize them a bit more
sensibly.  Comment GMReaderTupleBuffer more usefully too.  Improve
assorted other comments that were obsolete or just not very good English.

Get rid of the use of a GMReaderTupleBuffer for the leader process;
that was confusing, since only the "done" field was used, and that
in a way redundant with need_to_scan_locally.

In gather_merge_init, avoid calling load_tuple_array for
already-known-exhausted workers.  I'm not sure if there's a live bug there,
but the case is unlikely to be well tested due to timing considerations.

Remove some useless code, such as duplicating the tts_isempty test done by
TupIsNull.

Remove useless initialization of ps.qual, replacing that with an assertion
that we have no qual to check.  (If we did, the code would fail to check
it.)

Avoid applying heap_copytuple to a null tuple.  While that fails to crash,
it's confusing and it makes the code less legible not more so IMO.

Propagate a couple of these changes into nodeGather.c, as well.

Back-patch to v10, partly because of the possibility that the
gather_merge_init change is fixing a live bug, but mostly to keep
the branches in sync to ease future bug fixes.
parent 41b0dd98
...@@ -71,6 +71,8 @@ ExecInitGather(Gather *node, EState *estate, int eflags) ...@@ -71,6 +71,8 @@ ExecInitGather(Gather *node, EState *estate, int eflags)
gatherstate->ps.plan = (Plan *) node; gatherstate->ps.plan = (Plan *) node;
gatherstate->ps.state = estate; gatherstate->ps.state = estate;
gatherstate->ps.ExecProcNode = ExecGather; gatherstate->ps.ExecProcNode = ExecGather;
gatherstate->initialized = false;
gatherstate->need_to_scan_locally = !node->single_copy; gatherstate->need_to_scan_locally = !node->single_copy;
gatherstate->tuples_needed = -1; gatherstate->tuples_needed = -1;
...@@ -82,10 +84,10 @@ ExecInitGather(Gather *node, EState *estate, int eflags) ...@@ -82,10 +84,10 @@ ExecInitGather(Gather *node, EState *estate, int eflags)
ExecAssignExprContext(estate, &gatherstate->ps); ExecAssignExprContext(estate, &gatherstate->ps);
/* /*
* initialize child expressions * Gather doesn't support checking a qual (it's always more efficient to
* do it in the child node).
*/ */
gatherstate->ps.qual = Assert(!node->plan.qual);
ExecInitQual(node->plan.qual, (PlanState *) gatherstate);
/* /*
* tuple table initialization * tuple table initialization
...@@ -169,15 +171,16 @@ ExecGather(PlanState *pstate) ...@@ -169,15 +171,16 @@ ExecGather(PlanState *pstate)
*/ */
pcxt = node->pei->pcxt; pcxt = node->pei->pcxt;
LaunchParallelWorkers(pcxt); LaunchParallelWorkers(pcxt);
/* We save # workers launched for the benefit of EXPLAIN */
node->nworkers_launched = pcxt->nworkers_launched; node->nworkers_launched = pcxt->nworkers_launched;
node->nreaders = 0;
node->nextreader = 0;
/* Set up tuple queue readers to read the results. */ /* Set up tuple queue readers to read the results. */
if (pcxt->nworkers_launched > 0) if (pcxt->nworkers_launched > 0)
{ {
node->nreaders = 0; node->reader = palloc(pcxt->nworkers_launched *
node->nextreader = 0; sizeof(TupleQueueReader *));
node->reader =
palloc(pcxt->nworkers_launched * sizeof(TupleQueueReader *));
for (i = 0; i < pcxt->nworkers_launched; ++i) for (i = 0; i < pcxt->nworkers_launched; ++i)
{ {
...@@ -316,8 +319,8 @@ gather_readnext(GatherState *gatherstate) ...@@ -316,8 +319,8 @@ gather_readnext(GatherState *gatherstate)
tup = TupleQueueReaderNext(reader, true, &readerdone); tup = TupleQueueReaderNext(reader, true, &readerdone);
/* /*
* If this reader is done, remove it. If all readers are done, clean * If this reader is done, remove it, and collapse the array. If all
* up remaining worker state. * readers are done, clean up remaining worker state.
*/ */
if (readerdone) if (readerdone)
{ {
......
This diff is collapsed.
...@@ -1923,15 +1923,17 @@ typedef struct UniqueState ...@@ -1923,15 +1923,17 @@ typedef struct UniqueState
typedef struct GatherState typedef struct GatherState
{ {
PlanState ps; /* its first field is NodeTag */ PlanState ps; /* its first field is NodeTag */
bool initialized; bool initialized; /* workers launched? */
struct ParallelExecutorInfo *pei; bool need_to_scan_locally; /* need to read from local plan? */
int nreaders;
int nextreader;
int nworkers_launched;
struct TupleQueueReader **reader;
TupleTableSlot *funnel_slot;
bool need_to_scan_locally;
int64 tuples_needed; /* tuple bound, see ExecSetTupleBound */ int64 tuples_needed; /* tuple bound, see ExecSetTupleBound */
/* these fields are set up once: */
TupleTableSlot *funnel_slot;
struct ParallelExecutorInfo *pei;
/* all remaining fields are reinitialized during a rescan: */
int nworkers_launched; /* original number of workers */
int nreaders; /* number of still-active workers */
int nextreader; /* next one to try to read from */
struct TupleQueueReader **reader; /* array with nreaders active entries */
} GatherState; } GatherState;
/* ---------------- /* ----------------
...@@ -1942,25 +1944,27 @@ typedef struct GatherState ...@@ -1942,25 +1944,27 @@ typedef struct GatherState
* merge the results into a single sorted stream. * merge the results into a single sorted stream.
* ---------------- * ----------------
*/ */
struct GMReaderTuple; struct GMReaderTupleBuffer; /* private in nodeGatherMerge.c */
typedef struct GatherMergeState typedef struct GatherMergeState
{ {
PlanState ps; /* its first field is NodeTag */ PlanState ps; /* its first field is NodeTag */
bool initialized; bool initialized; /* workers launched? */
bool gm_initialized; /* gather_merge_init() done? */
bool need_to_scan_locally; /* need to read from local plan? */
int64 tuples_needed; /* tuple bound, see ExecSetTupleBound */
/* these fields are set up once: */
TupleDesc tupDesc; /* descriptor for subplan result tuples */
int gm_nkeys; /* number of sort columns */
SortSupport gm_sortkeys; /* array of length gm_nkeys */
struct ParallelExecutorInfo *pei; struct ParallelExecutorInfo *pei;
int nreaders; /* all remaining fields are reinitialized during a rescan: */
int nworkers_launched; int nworkers_launched; /* original number of workers */
struct TupleQueueReader **reader; int nreaders; /* number of active workers */
TupleDesc tupDesc; TupleTableSlot **gm_slots; /* array with nreaders+1 entries */
TupleTableSlot **gm_slots; struct TupleQueueReader **reader; /* array with nreaders active entries */
struct GMReaderTupleBuffer *gm_tuple_buffers; /* nreaders tuple buffers */
struct binaryheap *gm_heap; /* binary heap of slot indices */ struct binaryheap *gm_heap; /* binary heap of slot indices */
bool gm_initialized; /* gather merge initilized ? */
bool need_to_scan_locally;
int64 tuples_needed; /* tuple bound, see ExecSetTupleBound */
int gm_nkeys;
SortSupport gm_sortkeys; /* array of length ms_nkeys */
struct GMReaderTupleBuffer *gm_tuple_buffers; /* tuple buffer per reader */
} GatherMergeState; } GatherMergeState;
/* ---------------- /* ----------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment