1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
/*-------------------------------------------------------------------------
*
* nodeGather.c
* Support routines for scanning a plan via multiple workers.
*
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/executor/nodeGather.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/relscan.h"
#include "executor/execdebug.h"
#include "executor/execParallel.h"
#include "executor/nodeGather.h"
#include "executor/nodeSubplan.h"
#include "executor/tqueue.h"
#include "utils/rel.h"
static TupleTableSlot *gather_getnext(GatherState *gatherstate);
/* ----------------------------------------------------------------
* ExecInitGather
* ----------------------------------------------------------------
*/
GatherState *
ExecInitGather(Gather *node, EState *estate, int eflags)
{
GatherState *gatherstate;
/* Gather node doesn't have innerPlan node. */
Assert(innerPlan(node) == NULL);
/*
* create state structure
*/
gatherstate = makeNode(GatherState);
gatherstate->ps.plan = (Plan *) node;
gatherstate->ps.state = estate;
gatherstate->need_to_scan_workers = false;
gatherstate->need_to_scan_locally = !node->single_copy;
/*
* Miscellaneous initialization
*
* create expression context for node
*/
ExecAssignExprContext(estate, &gatherstate->ps);
/*
* initialize child expressions
*/
gatherstate->ps.targetlist = (List *)
ExecInitExpr((Expr *) node->plan.targetlist,
(PlanState *) gatherstate);
gatherstate->ps.qual = (List *)
ExecInitExpr((Expr *) node->plan.qual,
(PlanState *) gatherstate);
/*
* tuple table initialization
*/
ExecInitResultTupleSlot(estate, &gatherstate->ps);
/*
* now initialize outer plan
*/
outerPlanState(gatherstate) = ExecInitNode(outerPlan(node), estate, eflags);
gatherstate->ps.ps_TupFromTlist = false;
/*
* Initialize result tuple type and projection info.
*/
ExecAssignResultTypeFromTL(&gatherstate->ps);
ExecAssignProjectionInfo(&gatherstate->ps, NULL);
return gatherstate;
}
/* ----------------------------------------------------------------
* ExecGather(node)
*
* Scans the relation via multiple workers and returns
* the next qualifying tuple.
* ----------------------------------------------------------------
*/
TupleTableSlot *
ExecGather(GatherState *node)
{
int i;
TupleTableSlot *slot;
/*
* Initialize the parallel context and workers on first execution. We do
* this on first execution rather than during node initialization, as it
* needs to allocate large dynamic segement, so it is better to do if it
* is really needed.
*/
if (!node->pei)
{
EState *estate = node->ps.state;
/* Initialize the workers required to execute Gather node. */
node->pei = ExecInitParallelPlan(node->ps.lefttree,
estate,
((Gather *) (node->ps.plan))->num_workers);
/*
* Register backend workers. If the required number of workers are not
* available then we perform the scan with available workers and if
* there are no more workers available, then the Gather node will just
* scan locally.
*/
LaunchParallelWorkers(node->pei->pcxt);
node->funnel = CreateTupleQueueFunnel();
for (i = 0; i < node->pei->pcxt->nworkers; ++i)
{
if (node->pei->pcxt->worker[i].bgwhandle)
{
shm_mq_set_handle(node->pei->tqueue[i],
node->pei->pcxt->worker[i].bgwhandle);
RegisterTupleQueueOnFunnel(node->funnel, node->pei->tqueue[i]);
node->need_to_scan_workers = true;
}
}
/* If no workers are available, we must always scan locally. */
if (!node->need_to_scan_workers)
node->need_to_scan_locally = true;
}
slot = gather_getnext(node);
if (TupIsNull(slot))
{
/*
* Destroy the parallel context once we complete fetching all the
* tuples. Otherwise, the DSM and workers will stick around for the
* lifetime of the entire statement.
*/
ExecShutdownGather(node);
}
return slot;
}
/* ----------------------------------------------------------------
* ExecEndGather
*
* frees any storage allocated through C routines.
* ----------------------------------------------------------------
*/
void
ExecEndGather(GatherState *node)
{
ExecShutdownGather(node);
ExecFreeExprContext(&node->ps);
ExecClearTuple(node->ps.ps_ResultTupleSlot);
ExecEndNode(outerPlanState(node));
}
/*
* gather_getnext
*
* Get the next tuple from shared memory queue. This function
* is reponsible for fetching tuples from all the queues associated
* with worker backends used in Gather node execution and if there is
* no data available from queues or no worker is available, it does
* fetch the data from local node.
*/
static TupleTableSlot *
gather_getnext(GatherState *gatherstate)
{
PlanState *outerPlan;
TupleTableSlot *outerTupleSlot;
TupleTableSlot *slot;
HeapTuple tup;
/*
* We can use projection info of Gather for the tuples received from
* worker backends as currently for all cases worker backends sends the
* projected tuple as required by Gather node.
*/
slot = gatherstate->ps.ps_ProjInfo->pi_slot;
while (gatherstate->need_to_scan_workers ||
gatherstate->need_to_scan_locally)
{
if (gatherstate->need_to_scan_workers)
{
bool done = false;
/* wait only if local scan is done */
tup = TupleQueueFunnelNext(gatherstate->funnel,
gatherstate->need_to_scan_locally,
&done);
if (done)
gatherstate->need_to_scan_workers = false;
if (HeapTupleIsValid(tup))
{
ExecStoreTuple(tup, /* tuple to store */
slot, /* slot to store in */
InvalidBuffer, /* buffer associated with this
* tuple */
true); /* pfree this pointer if not from heap */
return slot;
}
}
if (gatherstate->need_to_scan_locally)
{
outerPlan = outerPlanState(gatherstate);
outerTupleSlot = ExecProcNode(outerPlan);
if (!TupIsNull(outerTupleSlot))
return outerTupleSlot;
gatherstate->need_to_scan_locally = false;
}
}
return ExecClearTuple(slot);
}
/* ----------------------------------------------------------------
* ExecShutdownGather
*
* Destroy the setup for parallel workers. Collect all the
* stats after workers are stopped, else some work done by
* workers won't be accounted.
* ----------------------------------------------------------------
*/
void
ExecShutdownGather(GatherState *node)
{
Gather *gather;
if (node->pei == NULL || node->pei->pcxt == NULL)
return;
/*
* Ensure all workers have finished before destroying the parallel context
* to ensure a clean exit.
*/
if (node->funnel)
{
DestroyTupleQueueFunnel(node->funnel);
node->funnel = NULL;
}
ExecParallelFinish(node->pei);
/* destroy parallel context. */
DestroyParallelContext(node->pei->pcxt);
node->pei->pcxt = NULL;
gather = (Gather *) node->ps.plan;
node->need_to_scan_locally = !gather->single_copy;
node->need_to_scan_workers = false;
}
/* ----------------------------------------------------------------
* Join Support
* ----------------------------------------------------------------
*/
/* ----------------------------------------------------------------
* ExecReScanGather
*
* Re-initialize the workers and rescans a relation via them.
* ----------------------------------------------------------------
*/
void
ExecReScanGather(GatherState *node)
{
/*
* Re-initialize the parallel context and workers to perform rescan of
* relation. We want to gracefully shutdown all the workers so that they
* should be able to propagate any error or other information to master
* backend before dying.
*/
ExecShutdownGather(node);
ExecReScan(node->ps.lefttree);
}
|