Coverage Report

Created: 2025-06-13 06:06

/src/postgres/src/backend/executor/execParallel.c
Line
Count
Source (jump to first uncovered line)
1
/*-------------------------------------------------------------------------
2
 *
3
 * execParallel.c
4
 *    Support routines for parallel execution.
5
 *
6
 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7
 * Portions Copyright (c) 1994, Regents of the University of California
8
 *
9
 * This file contains routines that are intended to support setting up,
10
 * using, and tearing down a ParallelContext from within the PostgreSQL
11
 * executor.  The ParallelContext machinery will handle starting the
12
 * workers and ensuring that their state generally matches that of the
13
 * leader; see src/backend/access/transam/README.parallel for details.
14
 * However, we must save and restore relevant executor state, such as
15
 * any ParamListInfo associated with the query, buffer/WAL usage info, and
16
 * the actual plan to be passed down to the worker.
17
 *
18
 * IDENTIFICATION
19
 *    src/backend/executor/execParallel.c
20
 *
21
 *-------------------------------------------------------------------------
22
 */
23
24
#include "postgres.h"
25
26
#include "executor/execParallel.h"
27
#include "executor/executor.h"
28
#include "executor/nodeAgg.h"
29
#include "executor/nodeAppend.h"
30
#include "executor/nodeBitmapHeapscan.h"
31
#include "executor/nodeBitmapIndexscan.h"
32
#include "executor/nodeCustom.h"
33
#include "executor/nodeForeignscan.h"
34
#include "executor/nodeHash.h"
35
#include "executor/nodeHashjoin.h"
36
#include "executor/nodeIncrementalSort.h"
37
#include "executor/nodeIndexonlyscan.h"
38
#include "executor/nodeIndexscan.h"
39
#include "executor/nodeMemoize.h"
40
#include "executor/nodeSeqscan.h"
41
#include "executor/nodeSort.h"
42
#include "executor/nodeSubplan.h"
43
#include "executor/tqueue.h"
44
#include "jit/jit.h"
45
#include "nodes/nodeFuncs.h"
46
#include "pgstat.h"
47
#include "tcop/tcopprot.h"
48
#include "utils/datum.h"
49
#include "utils/dsa.h"
50
#include "utils/lsyscache.h"
51
#include "utils/snapmgr.h"
52
53
/*
54
 * Magic numbers for parallel executor communication.  We use constants
55
 * greater than any 32-bit integer here so that values < 2^32 can be used
56
 * by individual parallel nodes to store their own state.
57
 */
58
0
#define PARALLEL_KEY_EXECUTOR_FIXED   UINT64CONST(0xE000000000000001)
59
0
#define PARALLEL_KEY_PLANNEDSTMT    UINT64CONST(0xE000000000000002)
60
0
#define PARALLEL_KEY_PARAMLISTINFO    UINT64CONST(0xE000000000000003)
61
0
#define PARALLEL_KEY_BUFFER_USAGE   UINT64CONST(0xE000000000000004)
62
0
#define PARALLEL_KEY_TUPLE_QUEUE    UINT64CONST(0xE000000000000005)
63
0
#define PARALLEL_KEY_INSTRUMENTATION  UINT64CONST(0xE000000000000006)
64
0
#define PARALLEL_KEY_DSA        UINT64CONST(0xE000000000000007)
65
0
#define PARALLEL_KEY_QUERY_TEXT   UINT64CONST(0xE000000000000008)
66
0
#define PARALLEL_KEY_JIT_INSTRUMENTATION UINT64CONST(0xE000000000000009)
67
0
#define PARALLEL_KEY_WAL_USAGE      UINT64CONST(0xE00000000000000A)
68
69
0
#define PARALLEL_TUPLE_QUEUE_SIZE   65536
70
71
/*
72
 * Fixed-size random stuff that we need to pass to parallel workers.
73
 */
74
typedef struct FixedParallelExecutorState
75
{
76
  int64   tuples_needed;  /* tuple bound, see ExecSetTupleBound */
77
  dsa_pointer param_exec;
78
  int     eflags;
79
  int     jit_flags;
80
} FixedParallelExecutorState;
81
82
/*
83
 * DSM structure for accumulating per-PlanState instrumentation.
84
 *
85
 * instrument_options: Same meaning here as in instrument.c.
86
 *
87
 * instrument_offset: Offset, relative to the start of this structure,
88
 * of the first Instrumentation object.  This will depend on the length of
89
 * the plan_node_id array.
90
 *
91
 * num_workers: Number of workers.
92
 *
93
 * num_plan_nodes: Number of plan nodes.
94
 *
95
 * plan_node_id: Array of plan nodes for which we are gathering instrumentation
96
 * from parallel workers.  The length of this array is given by num_plan_nodes.
97
 */
98
struct SharedExecutorInstrumentation
99
{
100
  int     instrument_options;
101
  int     instrument_offset;
102
  int     num_workers;
103
  int     num_plan_nodes;
104
  int     plan_node_id[FLEXIBLE_ARRAY_MEMBER];
105
  /* array of num_plan_nodes * num_workers Instrumentation objects follows */
106
};
107
#define GetInstrumentationArray(sei) \
108
0
  (AssertVariableIsOfTypeMacro(sei, SharedExecutorInstrumentation *), \
109
0
   (Instrumentation *) (((char *) sei) + sei->instrument_offset))
110
111
/* Context object for ExecParallelEstimate. */
112
typedef struct ExecParallelEstimateContext
113
{
114
  ParallelContext *pcxt;
115
  int     nnodes;
116
} ExecParallelEstimateContext;
117
118
/* Context object for ExecParallelInitializeDSM. */
119
typedef struct ExecParallelInitializeDSMContext
120
{
121
  ParallelContext *pcxt;
122
  SharedExecutorInstrumentation *instrumentation;
123
  int     nnodes;
124
} ExecParallelInitializeDSMContext;
125
126
/* Helper functions that run in the parallel leader. */
127
static char *ExecSerializePlan(Plan *plan, EState *estate);
128
static bool ExecParallelEstimate(PlanState *planstate,
129
                 ExecParallelEstimateContext *e);
130
static bool ExecParallelInitializeDSM(PlanState *planstate,
131
                    ExecParallelInitializeDSMContext *d);
132
static shm_mq_handle **ExecParallelSetupTupleQueues(ParallelContext *pcxt,
133
                          bool reinitialize);
134
static bool ExecParallelReInitializeDSM(PlanState *planstate,
135
                    ParallelContext *pcxt);
136
static bool ExecParallelRetrieveInstrumentation(PlanState *planstate,
137
                        SharedExecutorInstrumentation *instrumentation);
138
139
/* Helper function that runs in the parallel worker. */
140
static DestReceiver *ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc);
141
142
/*
143
 * Create a serialized representation of the plan to be sent to each worker.
144
 */
145
static char *
146
ExecSerializePlan(Plan *plan, EState *estate)
147
0
{
148
0
  PlannedStmt *pstmt;
149
0
  ListCell   *lc;
150
151
  /* We can't scribble on the original plan, so make a copy. */
152
0
  plan = copyObject(plan);
153
154
  /*
155
   * The worker will start its own copy of the executor, and that copy will
156
   * insert a junk filter if the toplevel node has any resjunk entries. We
157
   * don't want that to happen, because while resjunk columns shouldn't be
158
   * sent back to the user, here the tuples are coming back to another
159
   * backend which may very well need them.  So mutate the target list
160
   * accordingly.  This is sort of a hack; there might be better ways to do
161
   * this...
162
   */
163
0
  foreach(lc, plan->targetlist)
164
0
  {
165
0
    TargetEntry *tle = lfirst_node(TargetEntry, lc);
166
167
0
    tle->resjunk = false;
168
0
  }
169
170
  /*
171
   * Create a dummy PlannedStmt.  Most of the fields don't need to be valid
172
   * for our purposes, but the worker will need at least a minimal
173
   * PlannedStmt to start the executor.
174
   */
175
0
  pstmt = makeNode(PlannedStmt);
176
0
  pstmt->commandType = CMD_SELECT;
177
0
  pstmt->queryId = pgstat_get_my_query_id();
178
0
  pstmt->planId = pgstat_get_my_plan_id();
179
0
  pstmt->hasReturning = false;
180
0
  pstmt->hasModifyingCTE = false;
181
0
  pstmt->canSetTag = true;
182
0
  pstmt->transientPlan = false;
183
0
  pstmt->dependsOnRole = false;
184
0
  pstmt->parallelModeNeeded = false;
185
0
  pstmt->planTree = plan;
186
0
  pstmt->partPruneInfos = estate->es_part_prune_infos;
187
0
  pstmt->rtable = estate->es_range_table;
188
0
  pstmt->unprunableRelids = estate->es_unpruned_relids;
189
0
  pstmt->permInfos = estate->es_rteperminfos;
190
0
  pstmt->resultRelations = NIL;
191
0
  pstmt->appendRelations = NIL;
192
193
  /*
194
   * Transfer only parallel-safe subplans, leaving a NULL "hole" in the list
195
   * for unsafe ones (so that the list indexes of the safe ones are
196
   * preserved).  This positively ensures that the worker won't try to run,
197
   * or even do ExecInitNode on, an unsafe subplan.  That's important to
198
   * protect, eg, non-parallel-aware FDWs from getting into trouble.
199
   */
200
0
  pstmt->subplans = NIL;
201
0
  foreach(lc, estate->es_plannedstmt->subplans)
202
0
  {
203
0
    Plan     *subplan = (Plan *) lfirst(lc);
204
205
0
    if (subplan && !subplan->parallel_safe)
206
0
      subplan = NULL;
207
0
    pstmt->subplans = lappend(pstmt->subplans, subplan);
208
0
  }
209
210
0
  pstmt->rewindPlanIDs = NULL;
211
0
  pstmt->rowMarks = NIL;
212
0
  pstmt->relationOids = NIL;
213
0
  pstmt->invalItems = NIL; /* workers can't replan anyway... */
214
0
  pstmt->paramExecTypes = estate->es_plannedstmt->paramExecTypes;
215
0
  pstmt->utilityStmt = NULL;
216
0
  pstmt->stmt_location = -1;
217
0
  pstmt->stmt_len = -1;
218
219
  /* Return serialized copy of our dummy PlannedStmt. */
220
0
  return nodeToString(pstmt);
221
0
}
222
223
/*
224
 * Parallel-aware plan nodes (and occasionally others) may need some state
225
 * which is shared across all parallel workers.  Before we size the DSM, give
226
 * them a chance to call shm_toc_estimate_chunk or shm_toc_estimate_keys on
227
 * &pcxt->estimator.
228
 *
229
 * While we're at it, count the number of PlanState nodes in the tree, so
230
 * we know how many Instrumentation structures we need.
231
 */
232
static bool
233
ExecParallelEstimate(PlanState *planstate, ExecParallelEstimateContext *e)
234
0
{
235
0
  if (planstate == NULL)
236
0
    return false;
237
238
  /* Count this node. */
239
0
  e->nnodes++;
240
241
0
  switch (nodeTag(planstate))
242
0
  {
243
0
    case T_SeqScanState:
244
0
      if (planstate->plan->parallel_aware)
245
0
        ExecSeqScanEstimate((SeqScanState *) planstate,
246
0
                  e->pcxt);
247
0
      break;
248
0
    case T_IndexScanState:
249
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
250
0
      ExecIndexScanEstimate((IndexScanState *) planstate,
251
0
                  e->pcxt);
252
0
      break;
253
0
    case T_IndexOnlyScanState:
254
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
255
0
      ExecIndexOnlyScanEstimate((IndexOnlyScanState *) planstate,
256
0
                    e->pcxt);
257
0
      break;
258
0
    case T_BitmapIndexScanState:
259
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
260
0
      ExecBitmapIndexScanEstimate((BitmapIndexScanState *) planstate,
261
0
                    e->pcxt);
262
0
      break;
263
0
    case T_ForeignScanState:
264
0
      if (planstate->plan->parallel_aware)
265
0
        ExecForeignScanEstimate((ForeignScanState *) planstate,
266
0
                    e->pcxt);
267
0
      break;
268
0
    case T_AppendState:
269
0
      if (planstate->plan->parallel_aware)
270
0
        ExecAppendEstimate((AppendState *) planstate,
271
0
                   e->pcxt);
272
0
      break;
273
0
    case T_CustomScanState:
274
0
      if (planstate->plan->parallel_aware)
275
0
        ExecCustomScanEstimate((CustomScanState *) planstate,
276
0
                     e->pcxt);
277
0
      break;
278
0
    case T_BitmapHeapScanState:
279
0
      if (planstate->plan->parallel_aware)
280
0
        ExecBitmapHeapEstimate((BitmapHeapScanState *) planstate,
281
0
                     e->pcxt);
282
0
      break;
283
0
    case T_HashJoinState:
284
0
      if (planstate->plan->parallel_aware)
285
0
        ExecHashJoinEstimate((HashJoinState *) planstate,
286
0
                   e->pcxt);
287
0
      break;
288
0
    case T_HashState:
289
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
290
0
      ExecHashEstimate((HashState *) planstate, e->pcxt);
291
0
      break;
292
0
    case T_SortState:
293
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
294
0
      ExecSortEstimate((SortState *) planstate, e->pcxt);
295
0
      break;
296
0
    case T_IncrementalSortState:
297
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
298
0
      ExecIncrementalSortEstimate((IncrementalSortState *) planstate, e->pcxt);
299
0
      break;
300
0
    case T_AggState:
301
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
302
0
      ExecAggEstimate((AggState *) planstate, e->pcxt);
303
0
      break;
304
0
    case T_MemoizeState:
305
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
306
0
      ExecMemoizeEstimate((MemoizeState *) planstate, e->pcxt);
307
0
      break;
308
0
    default:
309
0
      break;
310
0
  }
311
312
0
  return planstate_tree_walker(planstate, ExecParallelEstimate, e);
313
0
}
314
315
/*
316
 * Estimate the amount of space required to serialize the indicated parameters.
317
 */
318
static Size
319
EstimateParamExecSpace(EState *estate, Bitmapset *params)
320
0
{
321
0
  int     paramid;
322
0
  Size    sz = sizeof(int);
323
324
0
  paramid = -1;
325
0
  while ((paramid = bms_next_member(params, paramid)) >= 0)
326
0
  {
327
0
    Oid     typeOid;
328
0
    int16   typLen;
329
0
    bool    typByVal;
330
0
    ParamExecData *prm;
331
332
0
    prm = &(estate->es_param_exec_vals[paramid]);
333
0
    typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
334
0
                 paramid);
335
336
0
    sz = add_size(sz, sizeof(int)); /* space for paramid */
337
338
    /* space for datum/isnull */
339
0
    if (OidIsValid(typeOid))
340
0
      get_typlenbyval(typeOid, &typLen, &typByVal);
341
0
    else
342
0
    {
343
      /* If no type OID, assume by-value, like copyParamList does. */
344
0
      typLen = sizeof(Datum);
345
0
      typByVal = true;
346
0
    }
347
0
    sz = add_size(sz,
348
0
            datumEstimateSpace(prm->value, prm->isnull,
349
0
                     typByVal, typLen));
350
0
  }
351
0
  return sz;
352
0
}
353
354
/*
355
 * Serialize specified PARAM_EXEC parameters.
356
 *
357
 * We write the number of parameters first, as a 4-byte integer, and then
358
 * write details for each parameter in turn.  The details for each parameter
359
 * consist of a 4-byte paramid (location of param in execution time internal
360
 * parameter array) and then the datum as serialized by datumSerialize().
361
 */
362
static dsa_pointer
363
SerializeParamExecParams(EState *estate, Bitmapset *params, dsa_area *area)
364
0
{
365
0
  Size    size;
366
0
  int     nparams;
367
0
  int     paramid;
368
0
  ParamExecData *prm;
369
0
  dsa_pointer handle;
370
0
  char     *start_address;
371
372
  /* Allocate enough space for the current parameter values. */
373
0
  size = EstimateParamExecSpace(estate, params);
374
0
  handle = dsa_allocate(area, size);
375
0
  start_address = dsa_get_address(area, handle);
376
377
  /* First write the number of parameters as a 4-byte integer. */
378
0
  nparams = bms_num_members(params);
379
0
  memcpy(start_address, &nparams, sizeof(int));
380
0
  start_address += sizeof(int);
381
382
  /* Write details for each parameter in turn. */
383
0
  paramid = -1;
384
0
  while ((paramid = bms_next_member(params, paramid)) >= 0)
385
0
  {
386
0
    Oid     typeOid;
387
0
    int16   typLen;
388
0
    bool    typByVal;
389
390
0
    prm = &(estate->es_param_exec_vals[paramid]);
391
0
    typeOid = list_nth_oid(estate->es_plannedstmt->paramExecTypes,
392
0
                 paramid);
393
394
    /* Write paramid. */
395
0
    memcpy(start_address, &paramid, sizeof(int));
396
0
    start_address += sizeof(int);
397
398
    /* Write datum/isnull */
399
0
    if (OidIsValid(typeOid))
400
0
      get_typlenbyval(typeOid, &typLen, &typByVal);
401
0
    else
402
0
    {
403
      /* If no type OID, assume by-value, like copyParamList does. */
404
0
      typLen = sizeof(Datum);
405
0
      typByVal = true;
406
0
    }
407
0
    datumSerialize(prm->value, prm->isnull, typByVal, typLen,
408
0
             &start_address);
409
0
  }
410
411
0
  return handle;
412
0
}
413
414
/*
415
 * Restore specified PARAM_EXEC parameters.
416
 */
417
static void
418
RestoreParamExecParams(char *start_address, EState *estate)
419
0
{
420
0
  int     nparams;
421
0
  int     i;
422
0
  int     paramid;
423
424
0
  memcpy(&nparams, start_address, sizeof(int));
425
0
  start_address += sizeof(int);
426
427
0
  for (i = 0; i < nparams; i++)
428
0
  {
429
0
    ParamExecData *prm;
430
431
    /* Read paramid */
432
0
    memcpy(&paramid, start_address, sizeof(int));
433
0
    start_address += sizeof(int);
434
0
    prm = &(estate->es_param_exec_vals[paramid]);
435
436
    /* Read datum/isnull. */
437
0
    prm->value = datumRestore(&start_address, &prm->isnull);
438
0
    prm->execPlan = NULL;
439
0
  }
440
0
}
441
442
/*
443
 * Initialize the dynamic shared memory segment that will be used to control
444
 * parallel execution.
445
 */
446
static bool
447
ExecParallelInitializeDSM(PlanState *planstate,
448
              ExecParallelInitializeDSMContext *d)
449
0
{
450
0
  if (planstate == NULL)
451
0
    return false;
452
453
  /* If instrumentation is enabled, initialize slot for this node. */
454
0
  if (d->instrumentation != NULL)
455
0
    d->instrumentation->plan_node_id[d->nnodes] =
456
0
      planstate->plan->plan_node_id;
457
458
  /* Count this node. */
459
0
  d->nnodes++;
460
461
  /*
462
   * Call initializers for DSM-using plan nodes.
463
   *
464
   * Most plan nodes won't do anything here, but plan nodes that allocated
465
   * DSM may need to initialize shared state in the DSM before parallel
466
   * workers are launched.  They can allocate the space they previously
467
   * estimated using shm_toc_allocate, and add the keys they previously
468
   * estimated using shm_toc_insert, in each case targeting pcxt->toc.
469
   */
470
0
  switch (nodeTag(planstate))
471
0
  {
472
0
    case T_SeqScanState:
473
0
      if (planstate->plan->parallel_aware)
474
0
        ExecSeqScanInitializeDSM((SeqScanState *) planstate,
475
0
                     d->pcxt);
476
0
      break;
477
0
    case T_IndexScanState:
478
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
479
0
      ExecIndexScanInitializeDSM((IndexScanState *) planstate, d->pcxt);
480
0
      break;
481
0
    case T_IndexOnlyScanState:
482
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
483
0
      ExecIndexOnlyScanInitializeDSM((IndexOnlyScanState *) planstate,
484
0
                       d->pcxt);
485
0
      break;
486
0
    case T_BitmapIndexScanState:
487
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
488
0
      ExecBitmapIndexScanInitializeDSM((BitmapIndexScanState *) planstate, d->pcxt);
489
0
      break;
490
0
    case T_ForeignScanState:
491
0
      if (planstate->plan->parallel_aware)
492
0
        ExecForeignScanInitializeDSM((ForeignScanState *) planstate,
493
0
                       d->pcxt);
494
0
      break;
495
0
    case T_AppendState:
496
0
      if (planstate->plan->parallel_aware)
497
0
        ExecAppendInitializeDSM((AppendState *) planstate,
498
0
                    d->pcxt);
499
0
      break;
500
0
    case T_CustomScanState:
501
0
      if (planstate->plan->parallel_aware)
502
0
        ExecCustomScanInitializeDSM((CustomScanState *) planstate,
503
0
                      d->pcxt);
504
0
      break;
505
0
    case T_BitmapHeapScanState:
506
0
      if (planstate->plan->parallel_aware)
507
0
        ExecBitmapHeapInitializeDSM((BitmapHeapScanState *) planstate,
508
0
                      d->pcxt);
509
0
      break;
510
0
    case T_HashJoinState:
511
0
      if (planstate->plan->parallel_aware)
512
0
        ExecHashJoinInitializeDSM((HashJoinState *) planstate,
513
0
                      d->pcxt);
514
0
      break;
515
0
    case T_HashState:
516
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
517
0
      ExecHashInitializeDSM((HashState *) planstate, d->pcxt);
518
0
      break;
519
0
    case T_SortState:
520
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
521
0
      ExecSortInitializeDSM((SortState *) planstate, d->pcxt);
522
0
      break;
523
0
    case T_IncrementalSortState:
524
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
525
0
      ExecIncrementalSortInitializeDSM((IncrementalSortState *) planstate, d->pcxt);
526
0
      break;
527
0
    case T_AggState:
528
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
529
0
      ExecAggInitializeDSM((AggState *) planstate, d->pcxt);
530
0
      break;
531
0
    case T_MemoizeState:
532
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
533
0
      ExecMemoizeInitializeDSM((MemoizeState *) planstate, d->pcxt);
534
0
      break;
535
0
    default:
536
0
      break;
537
0
  }
538
539
0
  return planstate_tree_walker(planstate, ExecParallelInitializeDSM, d);
540
0
}
541
542
/*
543
 * It sets up the response queues for backend workers to return tuples
544
 * to the main backend and start the workers.
545
 */
546
static shm_mq_handle **
547
ExecParallelSetupTupleQueues(ParallelContext *pcxt, bool reinitialize)
548
0
{
549
0
  shm_mq_handle **responseq;
550
0
  char     *tqueuespace;
551
0
  int     i;
552
553
  /* Skip this if no workers. */
554
0
  if (pcxt->nworkers == 0)
555
0
    return NULL;
556
557
  /* Allocate memory for shared memory queue handles. */
558
0
  responseq = (shm_mq_handle **)
559
0
    palloc(pcxt->nworkers * sizeof(shm_mq_handle *));
560
561
  /*
562
   * If not reinitializing, allocate space from the DSM for the queues;
563
   * otherwise, find the already allocated space.
564
   */
565
0
  if (!reinitialize)
566
0
    tqueuespace =
567
0
      shm_toc_allocate(pcxt->toc,
568
0
               mul_size(PARALLEL_TUPLE_QUEUE_SIZE,
569
0
                    pcxt->nworkers));
570
0
  else
571
0
    tqueuespace = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, false);
572
573
  /* Create the queues, and become the receiver for each. */
574
0
  for (i = 0; i < pcxt->nworkers; ++i)
575
0
  {
576
0
    shm_mq     *mq;
577
578
0
    mq = shm_mq_create(tqueuespace +
579
0
               ((Size) i) * PARALLEL_TUPLE_QUEUE_SIZE,
580
0
               (Size) PARALLEL_TUPLE_QUEUE_SIZE);
581
582
0
    shm_mq_set_receiver(mq, MyProc);
583
0
    responseq[i] = shm_mq_attach(mq, pcxt->seg, NULL);
584
0
  }
585
586
  /* Add array of queues to shm_toc, so others can find it. */
587
0
  if (!reinitialize)
588
0
    shm_toc_insert(pcxt->toc, PARALLEL_KEY_TUPLE_QUEUE, tqueuespace);
589
590
  /* Return array of handles. */
591
0
  return responseq;
592
0
}
593
594
/*
595
 * Sets up the required infrastructure for backend workers to perform
596
 * execution and return results to the main backend.
597
 */
598
ParallelExecutorInfo *
599
ExecInitParallelPlan(PlanState *planstate, EState *estate,
600
           Bitmapset *sendParams, int nworkers,
601
           int64 tuples_needed)
602
0
{
603
0
  ParallelExecutorInfo *pei;
604
0
  ParallelContext *pcxt;
605
0
  ExecParallelEstimateContext e;
606
0
  ExecParallelInitializeDSMContext d;
607
0
  FixedParallelExecutorState *fpes;
608
0
  char     *pstmt_data;
609
0
  char     *pstmt_space;
610
0
  char     *paramlistinfo_space;
611
0
  BufferUsage *bufusage_space;
612
0
  WalUsage   *walusage_space;
613
0
  SharedExecutorInstrumentation *instrumentation = NULL;
614
0
  SharedJitInstrumentation *jit_instrumentation = NULL;
615
0
  int     pstmt_len;
616
0
  int     paramlistinfo_len;
617
0
  int     instrumentation_len = 0;
618
0
  int     jit_instrumentation_len = 0;
619
0
  int     instrument_offset = 0;
620
0
  Size    dsa_minsize = dsa_minimum_size();
621
0
  char     *query_string;
622
0
  int     query_len;
623
624
  /*
625
   * Force any initplan outputs that we're going to pass to workers to be
626
   * evaluated, if they weren't already.
627
   *
628
   * For simplicity, we use the EState's per-output-tuple ExprContext here.
629
   * That risks intra-query memory leakage, since we might pass through here
630
   * many times before that ExprContext gets reset; but ExecSetParamPlan
631
   * doesn't normally leak any memory in the context (see its comments), so
632
   * it doesn't seem worth complicating this function's API to pass it a
633
   * shorter-lived ExprContext.  This might need to change someday.
634
   */
635
0
  ExecSetParamPlanMulti(sendParams, GetPerTupleExprContext(estate));
636
637
  /* Allocate object for return value. */
638
0
  pei = palloc0(sizeof(ParallelExecutorInfo));
639
0
  pei->finished = false;
640
0
  pei->planstate = planstate;
641
642
  /* Fix up and serialize plan to be sent to workers. */
643
0
  pstmt_data = ExecSerializePlan(planstate->plan, estate);
644
645
  /* Create a parallel context. */
646
0
  pcxt = CreateParallelContext("postgres", "ParallelQueryMain", nworkers);
647
0
  pei->pcxt = pcxt;
648
649
  /*
650
   * Before telling the parallel context to create a dynamic shared memory
651
   * segment, we need to figure out how big it should be.  Estimate space
652
   * for the various things we need to store.
653
   */
654
655
  /* Estimate space for fixed-size state. */
656
0
  shm_toc_estimate_chunk(&pcxt->estimator,
657
0
               sizeof(FixedParallelExecutorState));
658
0
  shm_toc_estimate_keys(&pcxt->estimator, 1);
659
660
  /* Estimate space for query text. */
661
0
  query_len = strlen(estate->es_sourceText);
662
0
  shm_toc_estimate_chunk(&pcxt->estimator, query_len + 1);
663
0
  shm_toc_estimate_keys(&pcxt->estimator, 1);
664
665
  /* Estimate space for serialized PlannedStmt. */
666
0
  pstmt_len = strlen(pstmt_data) + 1;
667
0
  shm_toc_estimate_chunk(&pcxt->estimator, pstmt_len);
668
0
  shm_toc_estimate_keys(&pcxt->estimator, 1);
669
670
  /* Estimate space for serialized ParamListInfo. */
671
0
  paramlistinfo_len = EstimateParamListSpace(estate->es_param_list_info);
672
0
  shm_toc_estimate_chunk(&pcxt->estimator, paramlistinfo_len);
673
0
  shm_toc_estimate_keys(&pcxt->estimator, 1);
674
675
  /*
676
   * Estimate space for BufferUsage.
677
   *
678
   * If EXPLAIN is not in use and there are no extensions loaded that care,
679
   * we could skip this.  But we have no way of knowing whether anyone's
680
   * looking at pgBufferUsage, so do it unconditionally.
681
   */
682
0
  shm_toc_estimate_chunk(&pcxt->estimator,
683
0
               mul_size(sizeof(BufferUsage), pcxt->nworkers));
684
0
  shm_toc_estimate_keys(&pcxt->estimator, 1);
685
686
  /*
687
   * Same thing for WalUsage.
688
   */
689
0
  shm_toc_estimate_chunk(&pcxt->estimator,
690
0
               mul_size(sizeof(WalUsage), pcxt->nworkers));
691
0
  shm_toc_estimate_keys(&pcxt->estimator, 1);
692
693
  /* Estimate space for tuple queues. */
694
0
  shm_toc_estimate_chunk(&pcxt->estimator,
695
0
               mul_size(PARALLEL_TUPLE_QUEUE_SIZE, pcxt->nworkers));
696
0
  shm_toc_estimate_keys(&pcxt->estimator, 1);
697
698
  /*
699
   * Give parallel-aware nodes a chance to add to the estimates, and get a
700
   * count of how many PlanState nodes there are.
701
   */
702
0
  e.pcxt = pcxt;
703
0
  e.nnodes = 0;
704
0
  ExecParallelEstimate(planstate, &e);
705
706
  /* Estimate space for instrumentation, if required. */
707
0
  if (estate->es_instrument)
708
0
  {
709
0
    instrumentation_len =
710
0
      offsetof(SharedExecutorInstrumentation, plan_node_id) +
711
0
      sizeof(int) * e.nnodes;
712
0
    instrumentation_len = MAXALIGN(instrumentation_len);
713
0
    instrument_offset = instrumentation_len;
714
0
    instrumentation_len +=
715
0
      mul_size(sizeof(Instrumentation),
716
0
           mul_size(e.nnodes, nworkers));
717
0
    shm_toc_estimate_chunk(&pcxt->estimator, instrumentation_len);
718
0
    shm_toc_estimate_keys(&pcxt->estimator, 1);
719
720
    /* Estimate space for JIT instrumentation, if required. */
721
0
    if (estate->es_jit_flags != PGJIT_NONE)
722
0
    {
723
0
      jit_instrumentation_len =
724
0
        offsetof(SharedJitInstrumentation, jit_instr) +
725
0
        sizeof(JitInstrumentation) * nworkers;
726
0
      shm_toc_estimate_chunk(&pcxt->estimator, jit_instrumentation_len);
727
0
      shm_toc_estimate_keys(&pcxt->estimator, 1);
728
0
    }
729
0
  }
730
731
  /* Estimate space for DSA area. */
732
0
  shm_toc_estimate_chunk(&pcxt->estimator, dsa_minsize);
733
0
  shm_toc_estimate_keys(&pcxt->estimator, 1);
734
735
  /*
736
   * InitializeParallelDSM() passes the active snapshot to the parallel
737
   * worker, which uses it to set es_snapshot.  Make sure we don't set
738
   * es_snapshot differently in the child.
739
   */
740
0
  Assert(GetActiveSnapshot() == estate->es_snapshot);
741
742
  /* Everyone's had a chance to ask for space, so now create the DSM. */
743
0
  InitializeParallelDSM(pcxt);
744
745
  /*
746
   * OK, now we have a dynamic shared memory segment, and it should be big
747
   * enough to store all of the data we estimated we would want to put into
748
   * it, plus whatever general stuff (not specifically executor-related) the
749
   * ParallelContext itself needs to store there.  None of the space we
750
   * asked for has been allocated or initialized yet, though, so do that.
751
   */
752
753
  /* Store fixed-size state. */
754
0
  fpes = shm_toc_allocate(pcxt->toc, sizeof(FixedParallelExecutorState));
755
0
  fpes->tuples_needed = tuples_needed;
756
0
  fpes->param_exec = InvalidDsaPointer;
757
0
  fpes->eflags = estate->es_top_eflags;
758
0
  fpes->jit_flags = estate->es_jit_flags;
759
0
  shm_toc_insert(pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, fpes);
760
761
  /* Store query string */
762
0
  query_string = shm_toc_allocate(pcxt->toc, query_len + 1);
763
0
  memcpy(query_string, estate->es_sourceText, query_len + 1);
764
0
  shm_toc_insert(pcxt->toc, PARALLEL_KEY_QUERY_TEXT, query_string);
765
766
  /* Store serialized PlannedStmt. */
767
0
  pstmt_space = shm_toc_allocate(pcxt->toc, pstmt_len);
768
0
  memcpy(pstmt_space, pstmt_data, pstmt_len);
769
0
  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PLANNEDSTMT, pstmt_space);
770
771
  /* Store serialized ParamListInfo. */
772
0
  paramlistinfo_space = shm_toc_allocate(pcxt->toc, paramlistinfo_len);
773
0
  shm_toc_insert(pcxt->toc, PARALLEL_KEY_PARAMLISTINFO, paramlistinfo_space);
774
0
  SerializeParamList(estate->es_param_list_info, &paramlistinfo_space);
775
776
  /* Allocate space for each worker's BufferUsage; no need to initialize. */
777
0
  bufusage_space = shm_toc_allocate(pcxt->toc,
778
0
                    mul_size(sizeof(BufferUsage), pcxt->nworkers));
779
0
  shm_toc_insert(pcxt->toc, PARALLEL_KEY_BUFFER_USAGE, bufusage_space);
780
0
  pei->buffer_usage = bufusage_space;
781
782
  /* Same for WalUsage. */
783
0
  walusage_space = shm_toc_allocate(pcxt->toc,
784
0
                    mul_size(sizeof(WalUsage), pcxt->nworkers));
785
0
  shm_toc_insert(pcxt->toc, PARALLEL_KEY_WAL_USAGE, walusage_space);
786
0
  pei->wal_usage = walusage_space;
787
788
  /* Set up the tuple queues that the workers will write into. */
789
0
  pei->tqueue = ExecParallelSetupTupleQueues(pcxt, false);
790
791
  /* We don't need the TupleQueueReaders yet, though. */
792
0
  pei->reader = NULL;
793
794
  /*
795
   * If instrumentation options were supplied, allocate space for the data.
796
   * It only gets partially initialized here; the rest happens during
797
   * ExecParallelInitializeDSM.
798
   */
799
0
  if (estate->es_instrument)
800
0
  {
801
0
    Instrumentation *instrument;
802
0
    int     i;
803
804
0
    instrumentation = shm_toc_allocate(pcxt->toc, instrumentation_len);
805
0
    instrumentation->instrument_options = estate->es_instrument;
806
0
    instrumentation->instrument_offset = instrument_offset;
807
0
    instrumentation->num_workers = nworkers;
808
0
    instrumentation->num_plan_nodes = e.nnodes;
809
0
    instrument = GetInstrumentationArray(instrumentation);
810
0
    for (i = 0; i < nworkers * e.nnodes; ++i)
811
0
      InstrInit(&instrument[i], estate->es_instrument);
812
0
    shm_toc_insert(pcxt->toc, PARALLEL_KEY_INSTRUMENTATION,
813
0
             instrumentation);
814
0
    pei->instrumentation = instrumentation;
815
816
0
    if (estate->es_jit_flags != PGJIT_NONE)
817
0
    {
818
0
      jit_instrumentation = shm_toc_allocate(pcxt->toc,
819
0
                           jit_instrumentation_len);
820
0
      jit_instrumentation->num_workers = nworkers;
821
0
      memset(jit_instrumentation->jit_instr, 0,
822
0
           sizeof(JitInstrumentation) * nworkers);
823
0
      shm_toc_insert(pcxt->toc, PARALLEL_KEY_JIT_INSTRUMENTATION,
824
0
               jit_instrumentation);
825
0
      pei->jit_instrumentation = jit_instrumentation;
826
0
    }
827
0
  }
828
829
  /*
830
   * Create a DSA area that can be used by the leader and all workers.
831
   * (However, if we failed to create a DSM and are using private memory
832
   * instead, then skip this.)
833
   */
834
0
  if (pcxt->seg != NULL)
835
0
  {
836
0
    char     *area_space;
837
838
0
    area_space = shm_toc_allocate(pcxt->toc, dsa_minsize);
839
0
    shm_toc_insert(pcxt->toc, PARALLEL_KEY_DSA, area_space);
840
0
    pei->area = dsa_create_in_place(area_space, dsa_minsize,
841
0
                    LWTRANCHE_PARALLEL_QUERY_DSA,
842
0
                    pcxt->seg);
843
844
    /*
845
     * Serialize parameters, if any, using DSA storage.  We don't dare use
846
     * the main parallel query DSM for this because we might relaunch
847
     * workers after the values have changed (and thus the amount of
848
     * storage required has changed).
849
     */
850
0
    if (!bms_is_empty(sendParams))
851
0
    {
852
0
      pei->param_exec = SerializeParamExecParams(estate, sendParams,
853
0
                             pei->area);
854
0
      fpes->param_exec = pei->param_exec;
855
0
    }
856
0
  }
857
858
  /*
859
   * Give parallel-aware nodes a chance to initialize their shared data.
860
   * This also initializes the elements of instrumentation->ps_instrument,
861
   * if it exists.
862
   */
863
0
  d.pcxt = pcxt;
864
0
  d.instrumentation = instrumentation;
865
0
  d.nnodes = 0;
866
867
  /* Install our DSA area while initializing the plan. */
868
0
  estate->es_query_dsa = pei->area;
869
0
  ExecParallelInitializeDSM(planstate, &d);
870
0
  estate->es_query_dsa = NULL;
871
872
  /*
873
   * Make sure that the world hasn't shifted under our feet.  This could
874
   * probably just be an Assert(), but let's be conservative for now.
875
   */
876
0
  if (e.nnodes != d.nnodes)
877
0
    elog(ERROR, "inconsistent count of PlanState nodes");
878
879
  /* OK, we're ready to rock and roll. */
880
0
  return pei;
881
0
}
882
883
/*
884
 * Set up tuple queue readers to read the results of a parallel subplan.
885
 *
886
 * This is separate from ExecInitParallelPlan() because we can launch the
887
 * worker processes and let them start doing something before we do this.
888
 */
889
void
890
ExecParallelCreateReaders(ParallelExecutorInfo *pei)
891
0
{
892
0
  int     nworkers = pei->pcxt->nworkers_launched;
893
0
  int     i;
894
895
0
  Assert(pei->reader == NULL);
896
897
0
  if (nworkers > 0)
898
0
  {
899
0
    pei->reader = (TupleQueueReader **)
900
0
      palloc(nworkers * sizeof(TupleQueueReader *));
901
902
0
    for (i = 0; i < nworkers; i++)
903
0
    {
904
0
      shm_mq_set_handle(pei->tqueue[i],
905
0
                pei->pcxt->worker[i].bgwhandle);
906
0
      pei->reader[i] = CreateTupleQueueReader(pei->tqueue[i]);
907
0
    }
908
0
  }
909
0
}
910
911
/*
912
 * Re-initialize the parallel executor shared memory state before launching
913
 * a fresh batch of workers.
914
 */
915
void
916
ExecParallelReinitialize(PlanState *planstate,
917
             ParallelExecutorInfo *pei,
918
             Bitmapset *sendParams)
919
0
{
920
0
  EState     *estate = planstate->state;
921
0
  FixedParallelExecutorState *fpes;
922
923
  /* Old workers must already be shut down */
924
0
  Assert(pei->finished);
925
926
  /*
927
   * Force any initplan outputs that we're going to pass to workers to be
928
   * evaluated, if they weren't already (see comments in
929
   * ExecInitParallelPlan).
930
   */
931
0
  ExecSetParamPlanMulti(sendParams, GetPerTupleExprContext(estate));
932
933
0
  ReinitializeParallelDSM(pei->pcxt);
934
0
  pei->tqueue = ExecParallelSetupTupleQueues(pei->pcxt, true);
935
0
  pei->reader = NULL;
936
0
  pei->finished = false;
937
938
0
  fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
939
940
  /* Free any serialized parameters from the last round. */
941
0
  if (DsaPointerIsValid(fpes->param_exec))
942
0
  {
943
0
    dsa_free(pei->area, fpes->param_exec);
944
0
    fpes->param_exec = InvalidDsaPointer;
945
0
  }
946
947
  /* Serialize current parameter values if required. */
948
0
  if (!bms_is_empty(sendParams))
949
0
  {
950
0
    pei->param_exec = SerializeParamExecParams(estate, sendParams,
951
0
                           pei->area);
952
0
    fpes->param_exec = pei->param_exec;
953
0
  }
954
955
  /* Traverse plan tree and let each child node reset associated state. */
956
0
  estate->es_query_dsa = pei->area;
957
0
  ExecParallelReInitializeDSM(planstate, pei->pcxt);
958
0
  estate->es_query_dsa = NULL;
959
0
}
960
961
/*
962
 * Traverse plan tree to reinitialize per-node dynamic shared memory state
963
 */
964
static bool
965
ExecParallelReInitializeDSM(PlanState *planstate,
966
              ParallelContext *pcxt)
967
0
{
968
0
  if (planstate == NULL)
969
0
    return false;
970
971
  /*
972
   * Call reinitializers for DSM-using plan nodes.
973
   */
974
0
  switch (nodeTag(planstate))
975
0
  {
976
0
    case T_SeqScanState:
977
0
      if (planstate->plan->parallel_aware)
978
0
        ExecSeqScanReInitializeDSM((SeqScanState *) planstate,
979
0
                       pcxt);
980
0
      break;
981
0
    case T_IndexScanState:
982
0
      if (planstate->plan->parallel_aware)
983
0
        ExecIndexScanReInitializeDSM((IndexScanState *) planstate,
984
0
                       pcxt);
985
0
      break;
986
0
    case T_IndexOnlyScanState:
987
0
      if (planstate->plan->parallel_aware)
988
0
        ExecIndexOnlyScanReInitializeDSM((IndexOnlyScanState *) planstate,
989
0
                         pcxt);
990
0
      break;
991
0
    case T_ForeignScanState:
992
0
      if (planstate->plan->parallel_aware)
993
0
        ExecForeignScanReInitializeDSM((ForeignScanState *) planstate,
994
0
                         pcxt);
995
0
      break;
996
0
    case T_AppendState:
997
0
      if (planstate->plan->parallel_aware)
998
0
        ExecAppendReInitializeDSM((AppendState *) planstate, pcxt);
999
0
      break;
1000
0
    case T_CustomScanState:
1001
0
      if (planstate->plan->parallel_aware)
1002
0
        ExecCustomScanReInitializeDSM((CustomScanState *) planstate,
1003
0
                        pcxt);
1004
0
      break;
1005
0
    case T_BitmapHeapScanState:
1006
0
      if (planstate->plan->parallel_aware)
1007
0
        ExecBitmapHeapReInitializeDSM((BitmapHeapScanState *) planstate,
1008
0
                        pcxt);
1009
0
      break;
1010
0
    case T_HashJoinState:
1011
0
      if (planstate->plan->parallel_aware)
1012
0
        ExecHashJoinReInitializeDSM((HashJoinState *) planstate,
1013
0
                      pcxt);
1014
0
      break;
1015
0
    case T_BitmapIndexScanState:
1016
0
    case T_HashState:
1017
0
    case T_SortState:
1018
0
    case T_IncrementalSortState:
1019
0
    case T_MemoizeState:
1020
      /* these nodes have DSM state, but no reinitialization is required */
1021
0
      break;
1022
1023
0
    default:
1024
0
      break;
1025
0
  }
1026
1027
0
  return planstate_tree_walker(planstate, ExecParallelReInitializeDSM, pcxt);
1028
0
}
1029
1030
/*
1031
 * Copy instrumentation information about this node and its descendants from
1032
 * dynamic shared memory.
1033
 */
1034
static bool
1035
ExecParallelRetrieveInstrumentation(PlanState *planstate,
1036
                  SharedExecutorInstrumentation *instrumentation)
1037
0
{
1038
0
  Instrumentation *instrument;
1039
0
  int     i;
1040
0
  int     n;
1041
0
  int     ibytes;
1042
0
  int     plan_node_id = planstate->plan->plan_node_id;
1043
0
  MemoryContext oldcontext;
1044
1045
  /* Find the instrumentation for this node. */
1046
0
  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1047
0
    if (instrumentation->plan_node_id[i] == plan_node_id)
1048
0
      break;
1049
0
  if (i >= instrumentation->num_plan_nodes)
1050
0
    elog(ERROR, "plan node %d not found", plan_node_id);
1051
1052
  /* Accumulate the statistics from all workers. */
1053
0
  instrument = GetInstrumentationArray(instrumentation);
1054
0
  instrument += i * instrumentation->num_workers;
1055
0
  for (n = 0; n < instrumentation->num_workers; ++n)
1056
0
    InstrAggNode(planstate->instrument, &instrument[n]);
1057
1058
  /*
1059
   * Also store the per-worker detail.
1060
   *
1061
   * Worker instrumentation should be allocated in the same context as the
1062
   * regular instrumentation information, which is the per-query context.
1063
   * Switch into per-query memory context.
1064
   */
1065
0
  oldcontext = MemoryContextSwitchTo(planstate->state->es_query_cxt);
1066
0
  ibytes = mul_size(instrumentation->num_workers, sizeof(Instrumentation));
1067
0
  planstate->worker_instrument =
1068
0
    palloc(ibytes + offsetof(WorkerInstrumentation, instrument));
1069
0
  MemoryContextSwitchTo(oldcontext);
1070
1071
0
  planstate->worker_instrument->num_workers = instrumentation->num_workers;
1072
0
  memcpy(&planstate->worker_instrument->instrument, instrument, ibytes);
1073
1074
  /* Perform any node-type-specific work that needs to be done. */
1075
0
  switch (nodeTag(planstate))
1076
0
  {
1077
0
    case T_IndexScanState:
1078
0
      ExecIndexScanRetrieveInstrumentation((IndexScanState *) planstate);
1079
0
      break;
1080
0
    case T_IndexOnlyScanState:
1081
0
      ExecIndexOnlyScanRetrieveInstrumentation((IndexOnlyScanState *) planstate);
1082
0
      break;
1083
0
    case T_BitmapIndexScanState:
1084
0
      ExecBitmapIndexScanRetrieveInstrumentation((BitmapIndexScanState *) planstate);
1085
0
      break;
1086
0
    case T_SortState:
1087
0
      ExecSortRetrieveInstrumentation((SortState *) planstate);
1088
0
      break;
1089
0
    case T_IncrementalSortState:
1090
0
      ExecIncrementalSortRetrieveInstrumentation((IncrementalSortState *) planstate);
1091
0
      break;
1092
0
    case T_HashState:
1093
0
      ExecHashRetrieveInstrumentation((HashState *) planstate);
1094
0
      break;
1095
0
    case T_AggState:
1096
0
      ExecAggRetrieveInstrumentation((AggState *) planstate);
1097
0
      break;
1098
0
    case T_MemoizeState:
1099
0
      ExecMemoizeRetrieveInstrumentation((MemoizeState *) planstate);
1100
0
      break;
1101
0
    case T_BitmapHeapScanState:
1102
0
      ExecBitmapHeapRetrieveInstrumentation((BitmapHeapScanState *) planstate);
1103
0
      break;
1104
0
    default:
1105
0
      break;
1106
0
  }
1107
1108
0
  return planstate_tree_walker(planstate, ExecParallelRetrieveInstrumentation,
1109
0
                 instrumentation);
1110
0
}
1111
1112
/*
1113
 * Add up the workers' JIT instrumentation from dynamic shared memory.
1114
 */
1115
static void
1116
ExecParallelRetrieveJitInstrumentation(PlanState *planstate,
1117
                     SharedJitInstrumentation *shared_jit)
1118
0
{
1119
0
  JitInstrumentation *combined;
1120
0
  int     ibytes;
1121
1122
0
  int     n;
1123
1124
  /*
1125
   * Accumulate worker JIT instrumentation into the combined JIT
1126
   * instrumentation, allocating it if required.
1127
   */
1128
0
  if (!planstate->state->es_jit_worker_instr)
1129
0
    planstate->state->es_jit_worker_instr =
1130
0
      MemoryContextAllocZero(planstate->state->es_query_cxt, sizeof(JitInstrumentation));
1131
0
  combined = planstate->state->es_jit_worker_instr;
1132
1133
  /* Accumulate all the workers' instrumentations. */
1134
0
  for (n = 0; n < shared_jit->num_workers; ++n)
1135
0
    InstrJitAgg(combined, &shared_jit->jit_instr[n]);
1136
1137
  /*
1138
   * Store the per-worker detail.
1139
   *
1140
   * Similar to ExecParallelRetrieveInstrumentation(), allocate the
1141
   * instrumentation in per-query context.
1142
   */
1143
0
  ibytes = offsetof(SharedJitInstrumentation, jit_instr)
1144
0
    + mul_size(shared_jit->num_workers, sizeof(JitInstrumentation));
1145
0
  planstate->worker_jit_instrument =
1146
0
    MemoryContextAlloc(planstate->state->es_query_cxt, ibytes);
1147
1148
0
  memcpy(planstate->worker_jit_instrument, shared_jit, ibytes);
1149
0
}
1150
1151
/*
1152
 * Finish parallel execution.  We wait for parallel workers to finish, and
1153
 * accumulate their buffer/WAL usage.
1154
 */
1155
void
1156
ExecParallelFinish(ParallelExecutorInfo *pei)
1157
0
{
1158
0
  int     nworkers = pei->pcxt->nworkers_launched;
1159
0
  int     i;
1160
1161
  /* Make this be a no-op if called twice in a row. */
1162
0
  if (pei->finished)
1163
0
    return;
1164
1165
  /*
1166
   * Detach from tuple queues ASAP, so that any still-active workers will
1167
   * notice that no further results are wanted.
1168
   */
1169
0
  if (pei->tqueue != NULL)
1170
0
  {
1171
0
    for (i = 0; i < nworkers; i++)
1172
0
      shm_mq_detach(pei->tqueue[i]);
1173
0
    pfree(pei->tqueue);
1174
0
    pei->tqueue = NULL;
1175
0
  }
1176
1177
  /*
1178
   * While we're waiting for the workers to finish, let's get rid of the
1179
   * tuple queue readers.  (Any other local cleanup could be done here too.)
1180
   */
1181
0
  if (pei->reader != NULL)
1182
0
  {
1183
0
    for (i = 0; i < nworkers; i++)
1184
0
      DestroyTupleQueueReader(pei->reader[i]);
1185
0
    pfree(pei->reader);
1186
0
    pei->reader = NULL;
1187
0
  }
1188
1189
  /* Now wait for the workers to finish. */
1190
0
  WaitForParallelWorkersToFinish(pei->pcxt);
1191
1192
  /*
1193
   * Next, accumulate buffer/WAL usage.  (This must wait for the workers to
1194
   * finish, or we might get incomplete data.)
1195
   */
1196
0
  for (i = 0; i < nworkers; i++)
1197
0
    InstrAccumParallelQuery(&pei->buffer_usage[i], &pei->wal_usage[i]);
1198
1199
0
  pei->finished = true;
1200
0
}
1201
1202
/*
1203
 * Accumulate instrumentation, and then clean up whatever ParallelExecutorInfo
1204
 * resources still exist after ExecParallelFinish.  We separate these
1205
 * routines because someone might want to examine the contents of the DSM
1206
 * after ExecParallelFinish and before calling this routine.
1207
 */
1208
void
1209
ExecParallelCleanup(ParallelExecutorInfo *pei)
1210
0
{
1211
  /* Accumulate instrumentation, if any. */
1212
0
  if (pei->instrumentation)
1213
0
    ExecParallelRetrieveInstrumentation(pei->planstate,
1214
0
                      pei->instrumentation);
1215
1216
  /* Accumulate JIT instrumentation, if any. */
1217
0
  if (pei->jit_instrumentation)
1218
0
    ExecParallelRetrieveJitInstrumentation(pei->planstate,
1219
0
                         pei->jit_instrumentation);
1220
1221
  /* Free any serialized parameters. */
1222
0
  if (DsaPointerIsValid(pei->param_exec))
1223
0
  {
1224
0
    dsa_free(pei->area, pei->param_exec);
1225
0
    pei->param_exec = InvalidDsaPointer;
1226
0
  }
1227
0
  if (pei->area != NULL)
1228
0
  {
1229
0
    dsa_detach(pei->area);
1230
0
    pei->area = NULL;
1231
0
  }
1232
0
  if (pei->pcxt != NULL)
1233
0
  {
1234
0
    DestroyParallelContext(pei->pcxt);
1235
0
    pei->pcxt = NULL;
1236
0
  }
1237
0
  pfree(pei);
1238
0
}
1239
1240
/*
1241
 * Create a DestReceiver to write tuples we produce to the shm_mq designated
1242
 * for that purpose.
1243
 */
1244
static DestReceiver *
1245
ExecParallelGetReceiver(dsm_segment *seg, shm_toc *toc)
1246
0
{
1247
0
  char     *mqspace;
1248
0
  shm_mq     *mq;
1249
1250
0
  mqspace = shm_toc_lookup(toc, PARALLEL_KEY_TUPLE_QUEUE, false);
1251
0
  mqspace += ParallelWorkerNumber * PARALLEL_TUPLE_QUEUE_SIZE;
1252
0
  mq = (shm_mq *) mqspace;
1253
0
  shm_mq_set_sender(mq, MyProc);
1254
0
  return CreateTupleQueueDestReceiver(shm_mq_attach(mq, seg, NULL));
1255
0
}
1256
1257
/*
1258
 * Create a QueryDesc for the PlannedStmt we are to execute, and return it.
1259
 */
1260
static QueryDesc *
1261
ExecParallelGetQueryDesc(shm_toc *toc, DestReceiver *receiver,
1262
             int instrument_options)
1263
0
{
1264
0
  char     *pstmtspace;
1265
0
  char     *paramspace;
1266
0
  PlannedStmt *pstmt;
1267
0
  ParamListInfo paramLI;
1268
0
  char     *queryString;
1269
1270
  /* Get the query string from shared memory */
1271
0
  queryString = shm_toc_lookup(toc, PARALLEL_KEY_QUERY_TEXT, false);
1272
1273
  /* Reconstruct leader-supplied PlannedStmt. */
1274
0
  pstmtspace = shm_toc_lookup(toc, PARALLEL_KEY_PLANNEDSTMT, false);
1275
0
  pstmt = (PlannedStmt *) stringToNode(pstmtspace);
1276
1277
  /* Reconstruct ParamListInfo. */
1278
0
  paramspace = shm_toc_lookup(toc, PARALLEL_KEY_PARAMLISTINFO, false);
1279
0
  paramLI = RestoreParamList(&paramspace);
1280
1281
  /* Create a QueryDesc for the query. */
1282
0
  return CreateQueryDesc(pstmt,
1283
0
               queryString,
1284
0
               GetActiveSnapshot(), InvalidSnapshot,
1285
0
               receiver, paramLI, NULL, instrument_options);
1286
0
}
1287
1288
/*
1289
 * Copy instrumentation information from this node and its descendants into
1290
 * dynamic shared memory, so that the parallel leader can retrieve it.
1291
 */
1292
static bool
1293
ExecParallelReportInstrumentation(PlanState *planstate,
1294
                  SharedExecutorInstrumentation *instrumentation)
1295
0
{
1296
0
  int     i;
1297
0
  int     plan_node_id = planstate->plan->plan_node_id;
1298
0
  Instrumentation *instrument;
1299
1300
0
  InstrEndLoop(planstate->instrument);
1301
1302
  /*
1303
   * If we shuffled the plan_node_id values in ps_instrument into sorted
1304
   * order, we could use binary search here.  This might matter someday if
1305
   * we're pushing down sufficiently large plan trees.  For now, do it the
1306
   * slow, dumb way.
1307
   */
1308
0
  for (i = 0; i < instrumentation->num_plan_nodes; ++i)
1309
0
    if (instrumentation->plan_node_id[i] == plan_node_id)
1310
0
      break;
1311
0
  if (i >= instrumentation->num_plan_nodes)
1312
0
    elog(ERROR, "plan node %d not found", plan_node_id);
1313
1314
  /*
1315
   * Add our statistics to the per-node, per-worker totals.  It's possible
1316
   * that this could happen more than once if we relaunched workers.
1317
   */
1318
0
  instrument = GetInstrumentationArray(instrumentation);
1319
0
  instrument += i * instrumentation->num_workers;
1320
0
  Assert(IsParallelWorker());
1321
0
  Assert(ParallelWorkerNumber < instrumentation->num_workers);
1322
0
  InstrAggNode(&instrument[ParallelWorkerNumber], planstate->instrument);
1323
1324
0
  return planstate_tree_walker(planstate, ExecParallelReportInstrumentation,
1325
0
                 instrumentation);
1326
0
}
1327
1328
/*
1329
 * Initialize the PlanState and its descendants with the information
1330
 * retrieved from shared memory.  This has to be done once the PlanState
1331
 * is allocated and initialized by executor; that is, after ExecutorStart().
1332
 */
1333
static bool
1334
ExecParallelInitializeWorker(PlanState *planstate, ParallelWorkerContext *pwcxt)
1335
0
{
1336
0
  if (planstate == NULL)
1337
0
    return false;
1338
1339
0
  switch (nodeTag(planstate))
1340
0
  {
1341
0
    case T_SeqScanState:
1342
0
      if (planstate->plan->parallel_aware)
1343
0
        ExecSeqScanInitializeWorker((SeqScanState *) planstate, pwcxt);
1344
0
      break;
1345
0
    case T_IndexScanState:
1346
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
1347
0
      ExecIndexScanInitializeWorker((IndexScanState *) planstate, pwcxt);
1348
0
      break;
1349
0
    case T_IndexOnlyScanState:
1350
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
1351
0
      ExecIndexOnlyScanInitializeWorker((IndexOnlyScanState *) planstate,
1352
0
                        pwcxt);
1353
0
      break;
1354
0
    case T_BitmapIndexScanState:
1355
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
1356
0
      ExecBitmapIndexScanInitializeWorker((BitmapIndexScanState *) planstate,
1357
0
                        pwcxt);
1358
0
      break;
1359
0
    case T_ForeignScanState:
1360
0
      if (planstate->plan->parallel_aware)
1361
0
        ExecForeignScanInitializeWorker((ForeignScanState *) planstate,
1362
0
                        pwcxt);
1363
0
      break;
1364
0
    case T_AppendState:
1365
0
      if (planstate->plan->parallel_aware)
1366
0
        ExecAppendInitializeWorker((AppendState *) planstate, pwcxt);
1367
0
      break;
1368
0
    case T_CustomScanState:
1369
0
      if (planstate->plan->parallel_aware)
1370
0
        ExecCustomScanInitializeWorker((CustomScanState *) planstate,
1371
0
                         pwcxt);
1372
0
      break;
1373
0
    case T_BitmapHeapScanState:
1374
0
      if (planstate->plan->parallel_aware)
1375
0
        ExecBitmapHeapInitializeWorker((BitmapHeapScanState *) planstate,
1376
0
                         pwcxt);
1377
0
      break;
1378
0
    case T_HashJoinState:
1379
0
      if (planstate->plan->parallel_aware)
1380
0
        ExecHashJoinInitializeWorker((HashJoinState *) planstate,
1381
0
                       pwcxt);
1382
0
      break;
1383
0
    case T_HashState:
1384
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
1385
0
      ExecHashInitializeWorker((HashState *) planstate, pwcxt);
1386
0
      break;
1387
0
    case T_SortState:
1388
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
1389
0
      ExecSortInitializeWorker((SortState *) planstate, pwcxt);
1390
0
      break;
1391
0
    case T_IncrementalSortState:
1392
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
1393
0
      ExecIncrementalSortInitializeWorker((IncrementalSortState *) planstate,
1394
0
                        pwcxt);
1395
0
      break;
1396
0
    case T_AggState:
1397
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
1398
0
      ExecAggInitializeWorker((AggState *) planstate, pwcxt);
1399
0
      break;
1400
0
    case T_MemoizeState:
1401
      /* even when not parallel-aware, for EXPLAIN ANALYZE */
1402
0
      ExecMemoizeInitializeWorker((MemoizeState *) planstate, pwcxt);
1403
0
      break;
1404
0
    default:
1405
0
      break;
1406
0
  }
1407
1408
0
  return planstate_tree_walker(planstate, ExecParallelInitializeWorker,
1409
0
                 pwcxt);
1410
0
}
1411
1412
/*
1413
 * Main entrypoint for parallel query worker processes.
1414
 *
1415
 * We reach this function from ParallelWorkerMain, so the setup necessary to
1416
 * create a sensible parallel environment has already been done;
1417
 * ParallelWorkerMain worries about stuff like the transaction state, combo
1418
 * CID mappings, and GUC values, so we don't need to deal with any of that
1419
 * here.
1420
 *
1421
 * Our job is to deal with concerns specific to the executor.  The parallel
1422
 * group leader will have stored a serialized PlannedStmt, and it's our job
1423
 * to execute that plan and write the resulting tuples to the appropriate
1424
 * tuple queue.  Various bits of supporting information that we need in order
1425
 * to do this are also stored in the dsm_segment and can be accessed through
1426
 * the shm_toc.
1427
 */
1428
void
1429
ParallelQueryMain(dsm_segment *seg, shm_toc *toc)
1430
0
{
1431
0
  FixedParallelExecutorState *fpes;
1432
0
  BufferUsage *buffer_usage;
1433
0
  WalUsage   *wal_usage;
1434
0
  DestReceiver *receiver;
1435
0
  QueryDesc  *queryDesc;
1436
0
  SharedExecutorInstrumentation *instrumentation;
1437
0
  SharedJitInstrumentation *jit_instrumentation;
1438
0
  int     instrument_options = 0;
1439
0
  void     *area_space;
1440
0
  dsa_area   *area;
1441
0
  ParallelWorkerContext pwcxt;
1442
1443
  /* Get fixed-size state. */
1444
0
  fpes = shm_toc_lookup(toc, PARALLEL_KEY_EXECUTOR_FIXED, false);
1445
1446
  /* Set up DestReceiver, SharedExecutorInstrumentation, and QueryDesc. */
1447
0
  receiver = ExecParallelGetReceiver(seg, toc);
1448
0
  instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_INSTRUMENTATION, true);
1449
0
  if (instrumentation != NULL)
1450
0
    instrument_options = instrumentation->instrument_options;
1451
0
  jit_instrumentation = shm_toc_lookup(toc, PARALLEL_KEY_JIT_INSTRUMENTATION,
1452
0
                     true);
1453
0
  queryDesc = ExecParallelGetQueryDesc(toc, receiver, instrument_options);
1454
1455
  /* Setting debug_query_string for individual workers */
1456
0
  debug_query_string = queryDesc->sourceText;
1457
1458
  /* Report workers' query for monitoring purposes */
1459
0
  pgstat_report_activity(STATE_RUNNING, debug_query_string);
1460
1461
  /* Attach to the dynamic shared memory area. */
1462
0
  area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
1463
0
  area = dsa_attach_in_place(area_space, seg);
1464
1465
  /* Start up the executor */
1466
0
  queryDesc->plannedstmt->jitFlags = fpes->jit_flags;
1467
0
  ExecutorStart(queryDesc, fpes->eflags);
1468
1469
  /* Special executor initialization steps for parallel workers */
1470
0
  queryDesc->planstate->state->es_query_dsa = area;
1471
0
  if (DsaPointerIsValid(fpes->param_exec))
1472
0
  {
1473
0
    char     *paramexec_space;
1474
1475
0
    paramexec_space = dsa_get_address(area, fpes->param_exec);
1476
0
    RestoreParamExecParams(paramexec_space, queryDesc->estate);
1477
0
  }
1478
0
  pwcxt.toc = toc;
1479
0
  pwcxt.seg = seg;
1480
0
  ExecParallelInitializeWorker(queryDesc->planstate, &pwcxt);
1481
1482
  /* Pass down any tuple bound */
1483
0
  ExecSetTupleBound(fpes->tuples_needed, queryDesc->planstate);
1484
1485
  /*
1486
   * Prepare to track buffer/WAL usage during query execution.
1487
   *
1488
   * We do this after starting up the executor to match what happens in the
1489
   * leader, which also doesn't count buffer accesses and WAL activity that
1490
   * occur during executor startup.
1491
   */
1492
0
  InstrStartParallelQuery();
1493
1494
  /*
1495
   * Run the plan.  If we specified a tuple bound, be careful not to demand
1496
   * more tuples than that.
1497
   */
1498
0
  ExecutorRun(queryDesc,
1499
0
        ForwardScanDirection,
1500
0
        fpes->tuples_needed < 0 ? (int64) 0 : fpes->tuples_needed);
1501
1502
  /* Shut down the executor */
1503
0
  ExecutorFinish(queryDesc);
1504
1505
  /* Report buffer/WAL usage during parallel execution. */
1506
0
  buffer_usage = shm_toc_lookup(toc, PARALLEL_KEY_BUFFER_USAGE, false);
1507
0
  wal_usage = shm_toc_lookup(toc, PARALLEL_KEY_WAL_USAGE, false);
1508
0
  InstrEndParallelQuery(&buffer_usage[ParallelWorkerNumber],
1509
0
              &wal_usage[ParallelWorkerNumber]);
1510
1511
  /* Report instrumentation data if any instrumentation options are set. */
1512
0
  if (instrumentation != NULL)
1513
0
    ExecParallelReportInstrumentation(queryDesc->planstate,
1514
0
                      instrumentation);
1515
1516
  /* Report JIT instrumentation data if any */
1517
0
  if (queryDesc->estate->es_jit && jit_instrumentation != NULL)
1518
0
  {
1519
0
    Assert(ParallelWorkerNumber < jit_instrumentation->num_workers);
1520
0
    jit_instrumentation->jit_instr[ParallelWorkerNumber] =
1521
0
      queryDesc->estate->es_jit->instr;
1522
0
  }
1523
1524
  /* Must do this after capturing instrumentation. */
1525
0
  ExecutorEnd(queryDesc);
1526
1527
  /* Cleanup. */
1528
0
  dsa_detach(area);
1529
0
  FreeQueryDesc(queryDesc);
1530
0
  receiver->rDestroy(receiver);
1531
0
}