PostgreSQL Source Code git master
Loading...
Searching...
No Matches
parallel.c File Reference
#include "postgres.h"
#include "access/brin.h"
#include "access/gin.h"
#include "access/nbtree.h"
#include "access/parallel.h"
#include "access/session.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "catalog/index.h"
#include "catalog/namespace.h"
#include "catalog/pg_enum.h"
#include "catalog/storage.h"
#include "commands/async.h"
#include "commands/vacuum.h"
#include "executor/execParallel.h"
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "libpq/pqmq.h"
#include "miscadmin.h"
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
#include "storage/spin.h"
#include "tcop/tcopprot.h"
#include "utils/combocid.h"
#include "utils/guc.h"
#include "utils/inval.h"
#include "utils/memutils.h"
#include "utils/relmapper.h"
#include "utils/snapmgr.h"
#include "utils/wait_event.h"
Include dependency graph for parallel.c:

Go to the source code of this file.

Data Structures

struct  FixedParallelState
 

Macros

#define PARALLEL_ERROR_QUEUE_SIZE   16384
 
#define PARALLEL_MAGIC   0x50477c7c
 
#define PARALLEL_KEY_FIXED   UINT64CONST(0xFFFFFFFFFFFF0001)
 
#define PARALLEL_KEY_ERROR_QUEUE   UINT64CONST(0xFFFFFFFFFFFF0002)
 
#define PARALLEL_KEY_LIBRARY   UINT64CONST(0xFFFFFFFFFFFF0003)
 
#define PARALLEL_KEY_GUC   UINT64CONST(0xFFFFFFFFFFFF0004)
 
#define PARALLEL_KEY_COMBO_CID   UINT64CONST(0xFFFFFFFFFFFF0005)
 
#define PARALLEL_KEY_TRANSACTION_SNAPSHOT   UINT64CONST(0xFFFFFFFFFFFF0006)
 
#define PARALLEL_KEY_ACTIVE_SNAPSHOT   UINT64CONST(0xFFFFFFFFFFFF0007)
 
#define PARALLEL_KEY_TRANSACTION_STATE   UINT64CONST(0xFFFFFFFFFFFF0008)
 
#define PARALLEL_KEY_ENTRYPOINT   UINT64CONST(0xFFFFFFFFFFFF0009)
 
#define PARALLEL_KEY_SESSION_DSM   UINT64CONST(0xFFFFFFFFFFFF000A)
 
#define PARALLEL_KEY_PENDING_SYNCS   UINT64CONST(0xFFFFFFFFFFFF000B)
 
#define PARALLEL_KEY_REINDEX_STATE   UINT64CONST(0xFFFFFFFFFFFF000C)
 
#define PARALLEL_KEY_RELMAPPER_STATE   UINT64CONST(0xFFFFFFFFFFFF000D)
 
#define PARALLEL_KEY_UNCOMMITTEDENUMS   UINT64CONST(0xFFFFFFFFFFFF000E)
 
#define PARALLEL_KEY_CLIENTCONNINFO   UINT64CONST(0xFFFFFFFFFFFF000F)
 

Typedefs

typedef struct FixedParallelState FixedParallelState
 

Functions

static void ProcessParallelMessage (ParallelContext *pcxt, int i, StringInfo msg)
 
static void WaitForParallelWorkersToExit (ParallelContext *pcxt)
 
static parallel_worker_main_type LookupParallelWorkerFunction (const char *libraryname, const char *funcname)
 
static void ParallelWorkerShutdown (int code, Datum arg)
 
ParallelContextCreateParallelContext (const char *library_name, const char *function_name, int nworkers)
 
void InitializeParallelDSM (ParallelContext *pcxt)
 
void ReinitializeParallelDSM (ParallelContext *pcxt)
 
void ReinitializeParallelWorkers (ParallelContext *pcxt, int nworkers_to_launch)
 
void LaunchParallelWorkers (ParallelContext *pcxt)
 
void WaitForParallelWorkersToAttach (ParallelContext *pcxt)
 
void WaitForParallelWorkersToFinish (ParallelContext *pcxt)
 
void DestroyParallelContext (ParallelContext *pcxt)
 
bool ParallelContextActive (void)
 
void HandleParallelMessageInterrupt (void)
 
void ProcessParallelMessages (void)
 
void AtEOSubXact_Parallel (bool isCommit, SubTransactionId mySubId)
 
void AtEOXact_Parallel (bool isCommit)
 
void ParallelWorkerMain (Datum main_arg)
 
void ParallelWorkerReportLastRecEnd (XLogRecPtr last_xlog_end)
 

Variables

int ParallelWorkerNumber = -1
 
volatile sig_atomic_t ParallelMessagePending = false
 
bool InitializingParallelWorker = false
 
static FixedParallelStateMyFixedParallelState
 
static dlist_head pcxt_list = DLIST_STATIC_INIT(pcxt_list)
 
static pid_t ParallelLeaderPid
 
struct { 
 
   const char *   fn_name 
 
   parallel_worker_main_type   fn_addr 
 
InternalParallelWorkers [] 
 

Macro Definition Documentation

◆ PARALLEL_ERROR_QUEUE_SIZE

#define PARALLEL_ERROR_QUEUE_SIZE   16384

Definition at line 57 of file parallel.c.

◆ PARALLEL_KEY_ACTIVE_SNAPSHOT

#define PARALLEL_KEY_ACTIVE_SNAPSHOT   UINT64CONST(0xFFFFFFFFFFFF0007)

Definition at line 73 of file parallel.c.

◆ PARALLEL_KEY_CLIENTCONNINFO

#define PARALLEL_KEY_CLIENTCONNINFO   UINT64CONST(0xFFFFFFFFFFFF000F)

Definition at line 81 of file parallel.c.

◆ PARALLEL_KEY_COMBO_CID

#define PARALLEL_KEY_COMBO_CID   UINT64CONST(0xFFFFFFFFFFFF0005)

Definition at line 71 of file parallel.c.

◆ PARALLEL_KEY_ENTRYPOINT

#define PARALLEL_KEY_ENTRYPOINT   UINT64CONST(0xFFFFFFFFFFFF0009)

Definition at line 75 of file parallel.c.

◆ PARALLEL_KEY_ERROR_QUEUE

#define PARALLEL_KEY_ERROR_QUEUE   UINT64CONST(0xFFFFFFFFFFFF0002)

Definition at line 68 of file parallel.c.

◆ PARALLEL_KEY_FIXED

#define PARALLEL_KEY_FIXED   UINT64CONST(0xFFFFFFFFFFFF0001)

Definition at line 67 of file parallel.c.

◆ PARALLEL_KEY_GUC

#define PARALLEL_KEY_GUC   UINT64CONST(0xFFFFFFFFFFFF0004)

Definition at line 70 of file parallel.c.

◆ PARALLEL_KEY_LIBRARY

#define PARALLEL_KEY_LIBRARY   UINT64CONST(0xFFFFFFFFFFFF0003)

Definition at line 69 of file parallel.c.

◆ PARALLEL_KEY_PENDING_SYNCS

#define PARALLEL_KEY_PENDING_SYNCS   UINT64CONST(0xFFFFFFFFFFFF000B)

Definition at line 77 of file parallel.c.

◆ PARALLEL_KEY_REINDEX_STATE

#define PARALLEL_KEY_REINDEX_STATE   UINT64CONST(0xFFFFFFFFFFFF000C)

Definition at line 78 of file parallel.c.

◆ PARALLEL_KEY_RELMAPPER_STATE

#define PARALLEL_KEY_RELMAPPER_STATE   UINT64CONST(0xFFFFFFFFFFFF000D)

Definition at line 79 of file parallel.c.

◆ PARALLEL_KEY_SESSION_DSM

#define PARALLEL_KEY_SESSION_DSM   UINT64CONST(0xFFFFFFFFFFFF000A)

Definition at line 76 of file parallel.c.

◆ PARALLEL_KEY_TRANSACTION_SNAPSHOT

#define PARALLEL_KEY_TRANSACTION_SNAPSHOT   UINT64CONST(0xFFFFFFFFFFFF0006)

Definition at line 72 of file parallel.c.

◆ PARALLEL_KEY_TRANSACTION_STATE

#define PARALLEL_KEY_TRANSACTION_STATE   UINT64CONST(0xFFFFFFFFFFFF0008)

Definition at line 74 of file parallel.c.

◆ PARALLEL_KEY_UNCOMMITTEDENUMS

#define PARALLEL_KEY_UNCOMMITTEDENUMS   UINT64CONST(0xFFFFFFFFFFFF000E)

Definition at line 80 of file parallel.c.

◆ PARALLEL_MAGIC

#define PARALLEL_MAGIC   0x50477c7c

Definition at line 60 of file parallel.c.

Typedef Documentation

◆ FixedParallelState

Function Documentation

◆ AtEOSubXact_Parallel()

void AtEOSubXact_Parallel ( bool  isCommit,
SubTransactionId  mySubId 
)

Definition at line 1263 of file parallel.c.

1264{
1265 while (!dlist_is_empty(&pcxt_list))
1266 {
1267 ParallelContext *pcxt;
1268
1270 if (pcxt->subid != mySubId)
1271 break;
1272 if (isCommit)
1273 elog(WARNING, "leaked parallel context");
1275 }
1276}
void DestroyParallelContext(ParallelContext *pcxt)
Definition parallel.c:959
static dlist_head pcxt_list
Definition parallel.c:129
#define WARNING
Definition elog.h:36
#define elog(elevel,...)
Definition elog.h:226
#define dlist_head_element(type, membername, lhead)
Definition ilist.h:603
static bool dlist_is_empty(const dlist_head *head)
Definition ilist.h:336
static int fb(int x)
SubTransactionId subid
Definition parallel.h:36

References DestroyParallelContext(), dlist_head_element, dlist_is_empty(), elog, fb(), pcxt_list, ParallelContext::subid, and WARNING.

Referenced by AbortSubTransaction(), and CommitSubTransaction().

◆ AtEOXact_Parallel()

void AtEOXact_Parallel ( bool  isCommit)

Definition at line 1284 of file parallel.c.

1285{
1286 while (!dlist_is_empty(&pcxt_list))
1287 {
1288 ParallelContext *pcxt;
1289
1291 if (isCommit)
1292 elog(WARNING, "leaked parallel context");
1294 }
1295}

References DestroyParallelContext(), dlist_head_element, dlist_is_empty(), elog, fb(), pcxt_list, and WARNING.

Referenced by AbortTransaction(), and CommitTransaction().

◆ CreateParallelContext()

ParallelContext * CreateParallelContext ( const char library_name,
const char function_name,
int  nworkers 
)

Definition at line 175 of file parallel.c.

177{
178 MemoryContext oldcontext;
179 ParallelContext *pcxt;
180
181 /* It is unsafe to create a parallel context if not in parallel mode. */
183
184 /* Number of workers should be non-negative. */
185 Assert(nworkers >= 0);
186
187 /* We might be running in a short-lived memory context. */
189
190 /* Initialize a new ParallelContext. */
193 pcxt->nworkers = nworkers;
194 pcxt->nworkers_to_launch = nworkers;
195 pcxt->library_name = pstrdup(library_name);
196 pcxt->function_name = pstrdup(function_name);
200
201 /* Restore previous memory context. */
202 MemoryContextSwitchTo(oldcontext);
203
204 return pcxt;
205}
#define Assert(condition)
Definition c.h:906
ErrorContextCallback * error_context_stack
Definition elog.c:99
#define palloc0_object(type)
Definition fe_memutils.h:75
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition ilist.h:347
MemoryContext TopTransactionContext
Definition mcxt.c:171
char * pstrdup(const char *in)
Definition mcxt.c:1781
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
#define shm_toc_initialize_estimator(e)
Definition shm_toc.h:49
char * library_name
Definition parallel.h:40
ErrorContextCallback * error_context_stack
Definition parallel.h:42
shm_toc_estimator estimator
Definition parallel.h:43
dlist_node node
Definition parallel.h:35
int nworkers_to_launch
Definition parallel.h:38
char * function_name
Definition parallel.h:41
SubTransactionId GetCurrentSubTransactionId(void)
Definition xact.c:793
bool IsInParallelMode(void)
Definition xact.c:1091

References Assert, dlist_push_head(), error_context_stack, ParallelContext::error_context_stack, ParallelContext::estimator, ParallelContext::function_name, GetCurrentSubTransactionId(), IsInParallelMode(), ParallelContext::library_name, MemoryContextSwitchTo(), ParallelContext::node, ParallelContext::nworkers, ParallelContext::nworkers_to_launch, palloc0_object, pcxt_list, pstrdup(), shm_toc_initialize_estimator, ParallelContext::subid, and TopTransactionContext.

Referenced by _brin_begin_parallel(), _bt_begin_parallel(), _gin_begin_parallel(), ExecInitParallelPlan(), and parallel_vacuum_init().

◆ DestroyParallelContext()

void DestroyParallelContext ( ParallelContext pcxt)

Definition at line 959 of file parallel.c.

960{
961 int i;
962
963 /*
964 * Be careful about order of operations here! We remove the parallel
965 * context from the list before we do anything else; otherwise, if an
966 * error occurs during a subsequent step, we might try to nuke it again
967 * from AtEOXact_Parallel or AtEOSubXact_Parallel.
968 */
969 dlist_delete(&pcxt->node);
970
971 /* Kill each worker in turn, and forget their error queues. */
972 if (pcxt->worker != NULL)
973 {
974 for (i = 0; i < pcxt->nworkers_launched; ++i)
975 {
976 if (pcxt->worker[i].error_mqh != NULL)
977 {
979
981 pcxt->worker[i].error_mqh = NULL;
982 }
983 }
984 }
985
986 /*
987 * If we have allocated a shared memory segment, detach it. This will
988 * implicitly detach the error queues, and any other shared memory queues,
989 * stored there.
990 */
991 if (pcxt->seg != NULL)
992 {
993 dsm_detach(pcxt->seg);
994 pcxt->seg = NULL;
995 }
996
997 /*
998 * If this parallel context is actually in backend-private memory rather
999 * than shared memory, free that memory instead.
1000 */
1001 if (pcxt->private_memory != NULL)
1002 {
1003 pfree(pcxt->private_memory);
1004 pcxt->private_memory = NULL;
1005 }
1006
1007 /*
1008 * We can't finish transaction commit or abort until all of the workers
1009 * have exited. This means, in particular, that we can't respond to
1010 * interrupts at this stage.
1011 */
1015
1016 /* Free the worker array itself. */
1017 if (pcxt->worker != NULL)
1018 {
1019 pfree(pcxt->worker);
1020 pcxt->worker = NULL;
1021 }
1022
1023 /* Free memory. */
1024 pfree(pcxt->library_name);
1025 pfree(pcxt->function_name);
1026 pfree(pcxt);
1027}
static void WaitForParallelWorkersToExit(ParallelContext *pcxt)
Definition parallel.c:919
void TerminateBackgroundWorker(BackgroundWorkerHandle *handle)
Definition bgworker.c:1304
void dsm_detach(dsm_segment *seg)
Definition dsm.c:803
static void dlist_delete(dlist_node *node)
Definition ilist.h:405
int i
Definition isn.c:77
void pfree(void *pointer)
Definition mcxt.c:1616
#define RESUME_INTERRUPTS()
Definition miscadmin.h:136
#define HOLD_INTERRUPTS()
Definition miscadmin.h:134
void shm_mq_detach(shm_mq_handle *mqh)
Definition shm_mq.c:845
dsm_segment * seg
Definition parallel.h:44
ParallelWorkerInfo * worker
Definition parallel.h:47
void * private_memory
Definition parallel.h:45
int nworkers_launched
Definition parallel.h:39
BackgroundWorkerHandle * bgwhandle
Definition parallel.h:29
shm_mq_handle * error_mqh
Definition parallel.h:30

References ParallelWorkerInfo::bgwhandle, dlist_delete(), dsm_detach(), ParallelWorkerInfo::error_mqh, fb(), ParallelContext::function_name, HOLD_INTERRUPTS, i, ParallelContext::library_name, ParallelContext::node, ParallelContext::nworkers_launched, pfree(), ParallelContext::private_memory, RESUME_INTERRUPTS, ParallelContext::seg, shm_mq_detach(), TerminateBackgroundWorker(), WaitForParallelWorkersToExit(), and ParallelContext::worker.

Referenced by _brin_begin_parallel(), _brin_end_parallel(), _bt_begin_parallel(), _bt_end_parallel(), _gin_begin_parallel(), _gin_end_parallel(), AtEOSubXact_Parallel(), AtEOXact_Parallel(), ExecParallelCleanup(), and parallel_vacuum_end().

◆ HandleParallelMessageInterrupt()

void HandleParallelMessageInterrupt ( void  )

Definition at line 1046 of file parallel.c.

1047{
1048 InterruptPending = true;
1051}
volatile sig_atomic_t ParallelMessagePending
Definition parallel.c:120
volatile sig_atomic_t InterruptPending
Definition globals.c:32
struct Latch * MyLatch
Definition globals.c:63
void SetLatch(Latch *latch)
Definition latch.c:290

References InterruptPending, MyLatch, ParallelMessagePending, and SetLatch().

Referenced by procsignal_sigusr1_handler().

◆ InitializeParallelDSM()

void InitializeParallelDSM ( ParallelContext pcxt)

Definition at line 213 of file parallel.c.

214{
215 MemoryContext oldcontext;
216 Size library_len = 0;
217 Size guc_len = 0;
218 Size combocidlen = 0;
219 Size tsnaplen = 0;
220 Size asnaplen = 0;
221 Size tstatelen = 0;
223 Size reindexlen = 0;
224 Size relmapperlen = 0;
227 Size segsize = 0;
228 int i;
233
234 /* We might be running in a very short-lived memory context. */
236
237 /* Allow space to store the fixed-size parallel state. */
240
241 /*
242 * If we manage to reach here while non-interruptible, it's unsafe to
243 * launch any workers: we would fail to process interrupts sent by them.
244 * We can deal with that edge case by pretending no workers were
245 * requested.
246 */
248 pcxt->nworkers = 0;
249
250 /*
251 * Normally, the user will have requested at least one worker process, but
252 * if by chance they have not, we can skip a bunch of things here.
253 */
254 if (pcxt->nworkers > 0)
255 {
256 /* Get (or create) the per-session DSM segment's handle. */
258
259 /*
260 * If we weren't able to create a per-session DSM segment, then we can
261 * continue but we can't safely launch any workers because their
262 * record typmods would be incompatible so they couldn't exchange
263 * tuples.
264 */
266 pcxt->nworkers = 0;
267 }
268
269 if (pcxt->nworkers > 0)
270 {
273 "parallel error queue size not buffer-aligned");
274
275 /* Estimate space for various kinds of state sharing. */
283 {
286 }
302 /* If you add more chunks here, you probably need to add keys. */
304
305 /* Estimate space need for error queues. */
308 pcxt->nworkers));
310
311 /* Estimate how much we'll need for the entrypoint info. */
313 strlen(pcxt->function_name) + 2);
315 }
316
317 /*
318 * Create DSM and initialize with new table of contents. But if the user
319 * didn't request any workers, then don't bother creating a dynamic shared
320 * memory segment; instead, just use backend-private memory.
321 *
322 * Also, if we can't create a dynamic shared memory segment because the
323 * maximum number of segments have already been created, then fall back to
324 * backend-private memory, and plan not to use any workers. We hope this
325 * won't happen very often, but it's better to abandon the use of
326 * parallelism than to fail outright.
327 */
329 if (pcxt->nworkers > 0)
331 if (pcxt->seg != NULL)
334 segsize);
335 else
336 {
337 pcxt->nworkers = 0;
340 segsize);
341 }
342
343 /* Initialize fixed-size state in shared memory. */
346 fps->database_id = MyDatabaseId;
347 fps->authenticated_user_id = GetAuthenticatedUserId();
348 fps->session_user_id = GetSessionUserId();
349 fps->outer_user_id = GetCurrentRoleId();
350 GetUserIdAndSecContext(&fps->current_user_id, &fps->sec_context);
351 fps->session_user_is_superuser = GetSessionUserIsSuperuser();
352 fps->role_is_superuser = current_role_is_superuser;
353 GetTempNamespaceState(&fps->temp_namespace_id,
354 &fps->temp_toast_namespace_id);
355 fps->parallel_leader_pgproc = MyProc;
356 fps->parallel_leader_pid = MyProcPid;
357 fps->parallel_leader_proc_number = MyProcNumber;
360 fps->serializable_xact_handle = ShareSerializableXact();
361 SpinLockInit(&fps->mutex);
362 fps->last_xlog_end = InvalidXLogRecPtr;
364
365 /* We can skip the rest of this if we're not budgeting for any workers. */
366 if (pcxt->nworkers > 0)
367 {
368 char *libraryspace;
369 char *gucspace;
370 char *combocidspace;
371 char *tsnapspace;
372 char *asnapspace;
373 char *tstatespace;
374 char *pendingsyncsspace;
375 char *reindexspace;
376 char *relmapperspace;
377 char *error_queue_space;
379 char *entrypointstate;
383
384 /* Serialize shared libraries we have loaded. */
388
389 /* Serialize GUC settings. */
393
394 /* Serialize combo CID state. */
398
399 /*
400 * Serialize the transaction snapshot if the transaction isolation
401 * level uses a transaction snapshot.
402 */
404 {
408 tsnapspace);
409 }
410
411 /* Serialize the active snapshot. */
415
416 /* Provide the handle for per-session segment. */
418 sizeof(dsm_handle));
422
423 /* Serialize transaction state. */
427
428 /* Serialize pending syncs. */
433
434 /* Serialize reindex state. */
438
439 /* Serialize relmapper state. */
444
445 /* Serialize uncommitted enum state. */
451
452 /* Serialize our ClientConnectionInfo. */
457
458 /* Allocate space for worker information. */
460
461 /*
462 * Establish error queues in dynamic shared memory.
463 *
464 * These queues should be used only for transmitting ErrorResponse,
465 * NoticeResponse, and NotifyResponse protocol messages. Tuple data
466 * should be transmitted via separate (possibly larger?) queues.
467 */
469 shm_toc_allocate(pcxt->toc,
471 pcxt->nworkers));
472 for (i = 0; i < pcxt->nworkers; ++i)
473 {
474 char *start;
475 shm_mq *mq;
476
480 pcxt->worker[i].error_mqh = shm_mq_attach(mq, pcxt->seg, NULL);
481 }
483
484 /*
485 * Serialize entrypoint information. It's unsafe to pass function
486 * pointers across processes, as the function pointer may be different
487 * in each process in EXEC_BACKEND builds, so we always pass library
488 * and function name. (We use library name "postgres" for functions
489 * in the core backend.)
490 */
493 strlen(pcxt->function_name) + 2);
497 }
498
499 /* Update nworkers_to_launch, in case we changed nworkers above. */
500 pcxt->nworkers_to_launch = pcxt->nworkers;
501
502 /* Restore previous memory context. */
503 MemoryContextSwitchTo(oldcontext);
504}
#define PARALLEL_KEY_TRANSACTION_STATE
Definition parallel.c:74
#define PARALLEL_KEY_GUC
Definition parallel.c:70
#define PARALLEL_KEY_UNCOMMITTEDENUMS
Definition parallel.c:80
#define PARALLEL_KEY_TRANSACTION_SNAPSHOT
Definition parallel.c:72
#define PARALLEL_KEY_CLIENTCONNINFO
Definition parallel.c:81
#define PARALLEL_KEY_PENDING_SYNCS
Definition parallel.c:77
#define PARALLEL_KEY_ACTIVE_SNAPSHOT
Definition parallel.c:73
#define PARALLEL_KEY_ERROR_QUEUE
Definition parallel.c:68
#define PARALLEL_KEY_SESSION_DSM
Definition parallel.c:76
#define PARALLEL_MAGIC
Definition parallel.c:60
#define PARALLEL_KEY_REINDEX_STATE
Definition parallel.c:78
#define PARALLEL_KEY_LIBRARY
Definition parallel.c:69
#define PARALLEL_KEY_FIXED
Definition parallel.c:67
#define PARALLEL_KEY_ENTRYPOINT
Definition parallel.c:75
#define PARALLEL_KEY_COMBO_CID
Definition parallel.c:71
#define PARALLEL_ERROR_QUEUE_SIZE
Definition parallel.c:57
#define PARALLEL_KEY_RELMAPPER_STATE
Definition parallel.c:79
#define BUFFERALIGN(LEN)
Definition c.h:861
#define StaticAssertDecl(condition, errmessage)
Definition c.h:971
size_t Size
Definition c.h:652
void SerializeComboCIDState(Size maxsize, char *start_address)
Definition combocid.c:316
Size EstimateComboCIDStateSpace(void)
Definition combocid.c:297
void SerializeLibraryState(Size maxsize, char *start_address)
Definition dfmgr.c:719
Size EstimateLibraryStateSpace(void)
Definition dfmgr.c:702
void * dsm_segment_address(dsm_segment *seg)
Definition dsm.c:1095
dsm_segment * dsm_create(Size size, int flags)
Definition dsm.c:516
#define DSM_CREATE_NULL_IF_MAXSEGMENTS
Definition dsm.h:20
uint32 dsm_handle
Definition dsm_impl.h:55
#define DSM_HANDLE_INVALID
Definition dsm_impl.h:58
#define palloc0_array(type, count)
Definition fe_memutils.h:77
int MyProcPid
Definition globals.c:47
ProcNumber MyProcNumber
Definition globals.c:90
Oid MyDatabaseId
Definition globals.c:94
void SerializeGUCState(Size maxsize, char *start_address)
Definition guc.c:5970
Size EstimateGUCStateSpace(void)
Definition guc.c:5817
bool current_role_is_superuser
Definition guc_tables.c:547
return str start
void SerializeReindexState(Size maxsize, char *start_address)
Definition index.c:4251
Size EstimateReindexStateSpace(void)
Definition index.c:4240
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
MemoryContext TopMemoryContext
Definition mcxt.c:166
#define INTERRUPTS_CAN_BE_PROCESSED()
Definition miscadmin.h:130
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition miscinit.c:613
bool GetSessionUserIsSuperuser(void)
Definition miscinit.c:516
Size EstimateClientConnectionInfoSpace(void)
Definition miscinit.c:1036
Oid GetSessionUserId(void)
Definition miscinit.c:509
Oid GetAuthenticatedUserId(void)
Definition miscinit.c:546
Oid GetCurrentRoleId(void)
Definition miscinit.c:936
void SerializeClientConnectionInfo(Size maxsize PG_USED_FOR_ASSERTS_ONLY, char *start_address)
Definition miscinit.c:1052
void GetTempNamespaceState(Oid *tempNamespaceId, Oid *tempToastNamespaceId)
Definition namespace.c:3877
Size EstimateUncommittedEnumsSpace(void)
Definition pg_enum.c:826
void SerializeUncommittedEnums(void *space, Size size)
Definition pg_enum.c:840
SerializableXactHandle ShareSerializableXact(void)
Definition predicate.c:5046
Size EstimateRelationMapSpace(void)
Definition relmapper.c:714
void SerializeRelationMap(Size maxSize, char *startAddress)
Definition relmapper.c:725
dsm_handle GetSessionDsmHandle(void)
Definition session.c:70
shm_mq * shm_mq_create(void *address, Size size)
Definition shm_mq.c:179
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition shm_mq.c:208
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition shm_mq.c:292
void * shm_toc_allocate(shm_toc *toc, Size nbytes)
Definition shm_toc.c:88
Size shm_toc_estimate(shm_toc_estimator *e)
Definition shm_toc.c:263
shm_toc * shm_toc_create(uint64 magic, void *address, Size nbytes)
Definition shm_toc.c:40
void shm_toc_insert(shm_toc *toc, uint64 key, void *address)
Definition shm_toc.c:171
#define shm_toc_estimate_chunk(e, sz)
Definition shm_toc.h:51
#define shm_toc_estimate_keys(e, cnt)
Definition shm_toc.h:53
Size mul_size(Size s1, Size s2)
Definition shmem.c:497
void SerializeSnapshot(Snapshot snapshot, char *start_address)
Definition snapmgr.c:1736
Snapshot GetTransactionSnapshot(void)
Definition snapmgr.c:272
Size EstimateSnapshotSpace(Snapshot snapshot)
Definition snapmgr.c:1712
Snapshot GetActiveSnapshot(void)
Definition snapmgr.c:800
static void SpinLockInit(volatile slock_t *lock)
Definition spin.h:50
PGPROC * MyProc
Definition proc.c:68
void SerializePendingSyncs(Size maxSize, char *startAddress)
Definition storage.c:600
Size EstimatePendingSyncsSpace(void)
Definition storage.c:587
shm_toc * toc
Definition parallel.h:46
void SerializeTransactionState(Size maxsize, char *start_address)
Definition xact.c:5563
Size EstimateTransactionStateSpace(void)
Definition xact.c:5535
TimestampTz GetCurrentStatementStartTimestamp(void)
Definition xact.c:881
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition xact.c:872
#define IsolationUsesXactSnapshot()
Definition xact.h:52
#define InvalidXLogRecPtr
Definition xlogdefs.h:28

References BUFFERALIGN, current_role_is_superuser, dsm_create(), DSM_CREATE_NULL_IF_MAXSEGMENTS, DSM_HANDLE_INVALID, dsm_segment_address(), ParallelWorkerInfo::error_mqh, EstimateClientConnectionInfoSpace(), EstimateComboCIDStateSpace(), EstimateGUCStateSpace(), EstimateLibraryStateSpace(), EstimatePendingSyncsSpace(), EstimateReindexStateSpace(), EstimateRelationMapSpace(), EstimateSnapshotSpace(), EstimateTransactionStateSpace(), EstimateUncommittedEnumsSpace(), ParallelContext::estimator, fb(), ParallelContext::function_name, GetActiveSnapshot(), GetAuthenticatedUserId(), GetCurrentRoleId(), GetCurrentStatementStartTimestamp(), GetCurrentTransactionStartTimestamp(), GetSessionDsmHandle(), GetSessionUserId(), GetSessionUserIsSuperuser(), GetTempNamespaceState(), GetTransactionSnapshot(), GetUserIdAndSecContext(), i, INTERRUPTS_CAN_BE_PROCESSED, InvalidXLogRecPtr, IsolationUsesXactSnapshot, ParallelContext::library_name, MemoryContextAlloc(), MemoryContextSwitchTo(), mul_size(), MyDatabaseId, MyProc, MyProcNumber, MyProcPid, ParallelContext::nworkers, ParallelContext::nworkers_to_launch, palloc0_array, PARALLEL_ERROR_QUEUE_SIZE, PARALLEL_KEY_ACTIVE_SNAPSHOT, PARALLEL_KEY_CLIENTCONNINFO, PARALLEL_KEY_COMBO_CID, PARALLEL_KEY_ENTRYPOINT, PARALLEL_KEY_ERROR_QUEUE, PARALLEL_KEY_FIXED, PARALLEL_KEY_GUC, PARALLEL_KEY_LIBRARY, PARALLEL_KEY_PENDING_SYNCS, PARALLEL_KEY_REINDEX_STATE, PARALLEL_KEY_RELMAPPER_STATE, PARALLEL_KEY_SESSION_DSM, PARALLEL_KEY_TRANSACTION_SNAPSHOT, PARALLEL_KEY_TRANSACTION_STATE, PARALLEL_KEY_UNCOMMITTEDENUMS, PARALLEL_MAGIC, ParallelContext::private_memory, ParallelContext::seg, SerializeClientConnectionInfo(), SerializeComboCIDState(), SerializeGUCState(), SerializeLibraryState(), SerializePendingSyncs(), SerializeReindexState(), SerializeRelationMap(), SerializeSnapshot(), SerializeTransactionState(), SerializeUncommittedEnums(), ShareSerializableXact(), shm_mq_attach(), shm_mq_create(), shm_mq_set_receiver(), shm_toc_allocate(), shm_toc_create(), shm_toc_estimate(), shm_toc_estimate_chunk, shm_toc_estimate_keys, shm_toc_insert(), SpinLockInit(), start, StaticAssertDecl, ParallelContext::toc, TopMemoryContext, TopTransactionContext, and ParallelContext::worker.

Referenced by _brin_begin_parallel(), _bt_begin_parallel(), _gin_begin_parallel(), ExecInitParallelPlan(), and parallel_vacuum_init().

◆ LaunchParallelWorkers()

void LaunchParallelWorkers ( ParallelContext pcxt)

Definition at line 583 of file parallel.c.

584{
585 MemoryContext oldcontext;
586 BackgroundWorker worker;
587 int i;
588 bool any_registrations_failed = false;
589
590 /* Skip this if we have no workers. */
591 if (pcxt->nworkers == 0 || pcxt->nworkers_to_launch == 0)
592 return;
593
594 /* We need to be a lock group leader. */
596
597 /* If we do have workers, we'd better have a DSM segment. */
598 Assert(pcxt->seg != NULL);
599
600 /* We might be running in a short-lived memory context. */
602
603 /* Configure a worker. */
604 memset(&worker, 0, sizeof(worker));
605 snprintf(worker.bgw_name, BGW_MAXLEN, "parallel worker for PID %d",
606 MyProcPid);
607 snprintf(worker.bgw_type, BGW_MAXLEN, "parallel worker");
608 worker.bgw_flags =
613 sprintf(worker.bgw_library_name, "postgres");
614 sprintf(worker.bgw_function_name, "ParallelWorkerMain");
616 worker.bgw_notify_pid = MyProcPid;
617
618 /*
619 * Start workers.
620 *
621 * The caller must be able to tolerate ending up with fewer workers than
622 * expected, so there is no need to throw an error here if registration
623 * fails. It wouldn't help much anyway, because registering the worker in
624 * no way guarantees that it will start up and initialize successfully.
625 */
626 for (i = 0; i < pcxt->nworkers_to_launch; ++i)
627 {
628 memcpy(worker.bgw_extra, &i, sizeof(int));
631 &pcxt->worker[i].bgwhandle))
632 {
634 pcxt->worker[i].bgwhandle);
635 pcxt->nworkers_launched++;
636 }
637 else
638 {
639 /*
640 * If we weren't able to register the worker, then we've bumped up
641 * against the max_worker_processes limit, and future
642 * registrations will probably fail too, so arrange to skip them.
643 * But we still have to execute this code for the remaining slots
644 * to make sure that we forget about the error queues we budgeted
645 * for those workers. Otherwise, we'll wait for them to start,
646 * but they never will.
647 */
649 pcxt->worker[i].bgwhandle = NULL;
651 pcxt->worker[i].error_mqh = NULL;
652 }
653 }
654
655 /*
656 * Now that nworkers_launched has taken its final value, we can initialize
657 * known_attached_workers.
658 */
659 if (pcxt->nworkers_launched > 0)
660 {
662 pcxt->nknown_attached_workers = 0;
663 }
664
665 /* Restore previous memory context. */
666 MemoryContextSwitchTo(oldcontext);
667}
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition bgworker.c:1053
#define BGW_NEVER_RESTART
Definition bgworker.h:92
#define BGWORKER_CLASS_PARALLEL
Definition bgworker.h:75
@ BgWorkerStart_ConsistentState
Definition bgworker.h:87
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition bgworker.h:60
#define BGWORKER_SHMEM_ACCESS
Definition bgworker.h:53
#define BGW_MAXLEN
Definition bgworker.h:93
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition dsm.c:1123
#define sprintf
Definition port.h:262
#define snprintf
Definition port.h:260
static Datum UInt32GetDatum(uint32 X)
Definition postgres.h:242
void shm_mq_set_handle(shm_mq_handle *mqh, BackgroundWorkerHandle *handle)
Definition shm_mq.c:321
void BecomeLockGroupLeader(void)
Definition proc.c:1999
char bgw_function_name[BGW_MAXLEN]
Definition bgworker.h:104
char bgw_name[BGW_MAXLEN]
Definition bgworker.h:98
char bgw_type[BGW_MAXLEN]
Definition bgworker.h:99
BgWorkerStartTime bgw_start_time
Definition bgworker.h:101
char bgw_extra[BGW_EXTRALEN]
Definition bgworker.h:106
pid_t bgw_notify_pid
Definition bgworker.h:107
char bgw_library_name[MAXPGPATH]
Definition bgworker.h:103
bool * known_attached_workers
Definition parallel.h:49
int nknown_attached_workers
Definition parallel.h:48

References Assert, BecomeLockGroupLeader(), BackgroundWorker::bgw_extra, BackgroundWorker::bgw_flags, BackgroundWorker::bgw_function_name, BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_main_arg, BGW_MAXLEN, BackgroundWorker::bgw_name, BGW_NEVER_RESTART, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, BackgroundWorker::bgw_start_time, BackgroundWorker::bgw_type, ParallelWorkerInfo::bgwhandle, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_CLASS_PARALLEL, BGWORKER_SHMEM_ACCESS, BgWorkerStart_ConsistentState, dsm_segment_handle(), ParallelWorkerInfo::error_mqh, fb(), i, ParallelContext::known_attached_workers, MemoryContextSwitchTo(), MyProcPid, ParallelContext::nknown_attached_workers, ParallelContext::nworkers, ParallelContext::nworkers_launched, ParallelContext::nworkers_to_launch, palloc0_array, RegisterDynamicBackgroundWorker(), ParallelContext::seg, shm_mq_detach(), shm_mq_set_handle(), snprintf, sprintf, TopTransactionContext, UInt32GetDatum(), and ParallelContext::worker.

Referenced by _brin_begin_parallel(), _bt_begin_parallel(), _gin_begin_parallel(), ExecGather(), ExecGatherMerge(), and parallel_vacuum_process_all_indexes().

◆ LookupParallelWorkerFunction()

static parallel_worker_main_type LookupParallelWorkerFunction ( const char libraryname,
const char funcname 
)
static

Definition at line 1650 of file parallel.c.

1651{
1652 /*
1653 * If the function is to be loaded from postgres itself, search the
1654 * InternalParallelWorkers array.
1655 */
1656 if (strcmp(libraryname, "postgres") == 0)
1657 {
1658 int i;
1659
1660 for (i = 0; i < lengthof(InternalParallelWorkers); i++)
1661 {
1664 }
1665
1666 /* We can only reach this by programming error. */
1667 elog(ERROR, "internal function \"%s\" not found", funcname);
1668 }
1669
1670 /* Otherwise load from external library. */
1673}
parallel_worker_main_type fn_addr
Definition parallel.c:141
const char * fn_name
Definition parallel.c:140
static const struct @16 InternalParallelWorkers[]
#define lengthof(array)
Definition c.h:836
void * load_external_function(const char *filename, const char *funcname, bool signalNotFound, void **filehandle)
Definition dfmgr.c:95
#define ERROR
Definition elog.h:39
void(* parallel_worker_main_type)(dsm_segment *seg, shm_toc *toc)
Definition parallel.h:25
#define funcname

References elog, ERROR, fb(), fn_addr, fn_name, funcname, i, InternalParallelWorkers, lengthof, and load_external_function().

Referenced by ParallelWorkerMain().

◆ ParallelContextActive()

bool ParallelContextActive ( void  )

Definition at line 1033 of file parallel.c.

1034{
1035 return !dlist_is_empty(&pcxt_list);
1036}

References dlist_is_empty(), and pcxt_list.

Referenced by AtPrepare_PredicateLocks(), ExitParallelMode(), and ReleasePredicateLocks().

◆ ParallelWorkerMain()

void ParallelWorkerMain ( Datum  main_arg)

Definition at line 1301 of file parallel.c.

1302{
1303 dsm_segment *seg;
1304 shm_toc *toc;
1306 char *error_queue_space;
1307 shm_mq *mq;
1309 char *libraryspace;
1310 char *entrypointstate;
1311 char *library_name;
1312 char *function_name;
1314 char *gucspace;
1315 char *combocidspace;
1316 char *tsnapspace;
1317 char *asnapspace;
1318 char *tstatespace;
1319 char *pendingsyncsspace;
1320 char *reindexspace;
1321 char *relmapperspace;
1323 char *clientconninfospace;
1327
1328 /* Set flag to indicate that we're initializing a parallel worker. */
1330
1331 /* Establish signal handlers. */
1333
1334 /* Determine and set our parallel worker number. */
1337
1338 /* Set up a memory context to work in, just for cleanliness. */
1340 "Parallel worker",
1342
1343 /*
1344 * Attach to the dynamic shared memory segment for the parallel query, and
1345 * find its table of contents.
1346 *
1347 * Note: at this point, we have not created any ResourceOwner in this
1348 * process. This will result in our DSM mapping surviving until process
1349 * exit, which is fine. If there were a ResourceOwner, it would acquire
1350 * ownership of the mapping, but we have no need for that.
1351 */
1353 if (seg == NULL)
1354 ereport(ERROR,
1356 errmsg("could not map dynamic shared memory segment")));
1358 if (toc == NULL)
1359 ereport(ERROR,
1361 errmsg("invalid magic number in dynamic shared memory segment")));
1362
1363 /* Look up fixed parallel state. */
1364 fps = shm_toc_lookup(toc, PARALLEL_KEY_FIXED, false);
1366
1367 /* Arrange to signal the leader if we exit. */
1369 ParallelLeaderProcNumber = fps->parallel_leader_proc_number;
1371
1372 /*
1373 * Now we can find and attach to the error queue provided for us. That's
1374 * good, because until we do that, any errors that happen here will not be
1375 * reported back to the process that requested that this worker be
1376 * launched.
1377 */
1379 mq = (shm_mq *) (error_queue_space +
1382 mqh = shm_mq_attach(mq, seg, NULL);
1384 pq_set_parallel_leader(fps->parallel_leader_pid,
1385 fps->parallel_leader_proc_number);
1386
1387 /*
1388 * Hooray! Primary initialization is complete. Now, we need to set up our
1389 * backend-local state to match the original backend.
1390 */
1391
1392 /*
1393 * Join locking group. We must do this before anything that could try to
1394 * acquire a heavyweight lock, because any heavyweight locks acquired to
1395 * this point could block either directly against the parallel group
1396 * leader or against some process which in turn waits for a lock that
1397 * conflicts with the parallel group leader, causing an undetected
1398 * deadlock. (If we can't join the lock group, the leader has gone away,
1399 * so just exit quietly.)
1400 */
1401 if (!BecomeLockGroupMember(fps->parallel_leader_pgproc,
1402 fps->parallel_leader_pid))
1403 return;
1404
1405 /*
1406 * Restore transaction and statement start-time timestamps. This must
1407 * happen before anything that would start a transaction, else asserts in
1408 * xact.c will fire.
1409 */
1410 SetParallelStartTimestamps(fps->xact_ts, fps->stmt_ts);
1411
1412 /*
1413 * Identify the entry point to be called. In theory this could result in
1414 * loading an additional library, though most likely the entry point is in
1415 * the core backend or in a library we just loaded.
1416 */
1418 library_name = entrypointstate;
1419 function_name = entrypointstate + strlen(library_name) + 1;
1420
1421 entrypt = LookupParallelWorkerFunction(library_name, function_name);
1422
1423 /*
1424 * Restore current session authorization and role id. No verification
1425 * happens here, we just blindly adopt the leader's state. Note that this
1426 * has to happen before InitPostgres, since InitializeSessionUserId will
1427 * not set these variables.
1428 */
1429 SetAuthenticatedUserId(fps->authenticated_user_id);
1430 SetSessionAuthorization(fps->session_user_id,
1431 fps->session_user_is_superuser);
1432 SetCurrentRoleId(fps->outer_user_id, fps->role_is_superuser);
1433
1434 /*
1435 * Restore database connection. We skip connection authorization checks,
1436 * reasoning that (a) the leader checked these things when it started, and
1437 * (b) we do not want parallel mode to cause these failures, because that
1438 * would make use of parallel query plans not transparent to applications.
1439 */
1441 fps->authenticated_user_id,
1444
1445 /*
1446 * Set the client encoding to the database encoding, since that is what
1447 * the leader will expect. (We're cheating a bit by not calling
1448 * PrepareClientEncoding first. It's okay because this call will always
1449 * result in installing a no-op conversion. No error should be possible,
1450 * but check anyway.)
1451 */
1453 elog(ERROR, "SetClientEncoding(%d) failed", GetDatabaseEncoding());
1454
1455 /*
1456 * Load libraries that were loaded by original backend. We want to do
1457 * this before restoring GUCs, because the libraries might define custom
1458 * variables.
1459 */
1464
1465 /* Crank up a transaction state appropriate to a parallel worker. */
1468
1469 /*
1470 * Restore state that affects catalog access. Ideally we'd do this even
1471 * before calling InitPostgres, but that has order-of-initialization
1472 * problems, and also the relmapper would get confused during the
1473 * CommitTransactionCommand call above.
1474 */
1476 false);
1484
1485 /* Attach to the per-session DSM segment and contained objects. */
1489
1490 /*
1491 * If the transaction isolation level is REPEATABLE READ or SERIALIZABLE,
1492 * the leader has serialized the transaction snapshot and we must restore
1493 * it. At lower isolation levels, there is no transaction-lifetime
1494 * snapshot, but we need TransactionXmin to get set to a value which is
1495 * less than or equal to the xmin of every snapshot that will be used by
1496 * this worker. The easiest way to accomplish that is to install the
1497 * active snapshot as the transaction snapshot. Code running in this
1498 * parallel worker might take new snapshots via GetTransactionSnapshot()
1499 * or GetLatestSnapshot(), but it shouldn't have any way of acquiring a
1500 * snapshot older than the active snapshot.
1501 */
1507 fps->parallel_leader_pgproc);
1509
1510 /*
1511 * We've changed which tuples we can see, and must therefore invalidate
1512 * system caches.
1513 */
1515
1516 /*
1517 * Restore GUC values from launching backend. We can't do this earlier,
1518 * because GUC check hooks that do catalog lookups need to see the same
1519 * database state as the leader. Also, the check hooks for
1520 * session_authorization and role assume we already set the correct role
1521 * OIDs.
1522 */
1525
1526 /*
1527 * Restore current user ID and security context. No verification happens
1528 * here, we just blindly adopt the leader's state. We can't do this till
1529 * after restoring GUCs, else we'll get complaints about restoring
1530 * session_authorization and role. (In effect, we're assuming that all
1531 * the restored values are okay to set, even if we are now inside a
1532 * restricted context.)
1533 */
1534 SetUserIdAndSecContext(fps->current_user_id, fps->sec_context);
1535
1536 /* Restore temp-namespace state to ensure search path matches leader's. */
1537 SetTempNamespaceState(fps->temp_namespace_id,
1538 fps->temp_toast_namespace_id);
1539
1540 /* Restore uncommitted enums. */
1542 false);
1544
1545 /* Restore the ClientConnectionInfo. */
1547 false);
1549
1550 /*
1551 * Initialize SystemUser now that MyClientConnectionInfo is restored. Also
1552 * ensure that auth_method is actually valid, aka authn_id is not NULL.
1553 */
1557
1558 /* Attach to the leader's serializable transaction, if SERIALIZABLE. */
1559 AttachSerializableXact(fps->serializable_xact_handle);
1560
1561 /*
1562 * We've initialized all of our state now; nothing should change
1563 * hereafter.
1564 */
1567
1568 /*
1569 * Time to do the real work: invoke the caller-supplied code.
1570 */
1571 entrypt(seg, toc);
1572
1573 /* Must exit parallel mode to pop active snapshot. */
1575
1576 /* Must pop active snapshot so snapmgr.c doesn't complain. */
1578
1579 /* Shut down the parallel-worker transaction. */
1581
1582 /* Detach from the per-session DSM segment. */
1583 DetachSession();
1584
1585 /* Report success. */
1587}
static parallel_worker_main_type LookupParallelWorkerFunction(const char *libraryname, const char *funcname)
Definition parallel.c:1650
int ParallelWorkerNumber
Definition parallel.c:117
bool InitializingParallelWorker
Definition parallel.c:123
static FixedParallelState * MyFixedParallelState
Definition parallel.c:126
static pid_t ParallelLeaderPid
Definition parallel.c:132
static void ParallelWorkerShutdown(int code, Datum arg)
Definition parallel.c:1622
void BackgroundWorkerUnblockSignals(void)
Definition bgworker.c:934
void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid, uint32 flags)
Definition bgworker.c:894
#define BGWORKER_BYPASS_ROLELOGINCHECK
Definition bgworker.h:167
#define BGWORKER_BYPASS_ALLOWCONN
Definition bgworker.h:166
void RestoreComboCIDState(char *comboCIDstate)
Definition combocid.c:342
void RestoreLibraryState(char *start_address)
Definition dfmgr.c:741
dsm_segment * dsm_attach(dsm_handle h)
Definition dsm.c:665
int errcode(int sqlerrcode)
Definition elog.c:874
#define ereport(elevel,...)
Definition elog.h:150
ProcNumber ParallelLeaderProcNumber
Definition globals.c:92
void RestoreGUCState(void *gucstate)
Definition guc.c:6062
const char * hba_authname(UserAuth auth_method)
Definition hba.c:3138
void RestoreReindexState(const void *reindexstate)
Definition index.c:4269
void InvalidateSystemCaches(void)
Definition inval.c:916
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:344
#define pq_putmessage(msgtype, s, len)
Definition libpq.h:52
int GetDatabaseEncoding(void)
Definition mbutils.c:1389
int SetClientEncoding(int encoding)
Definition mbutils.c:217
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
void InitializeSystemUser(const char *authn_id, const char *auth_method)
Definition miscinit.c:875
void SetSessionAuthorization(Oid userid, bool is_superuser)
Definition miscinit.c:921
void SetCurrentRoleId(Oid roleid, bool is_superuser)
Definition miscinit.c:957
ClientConnectionInfo MyClientConnectionInfo
Definition miscinit.c:1019
void RestoreClientConnectionInfo(char *conninfo)
Definition miscinit.c:1085
void SetAuthenticatedUserId(Oid userid)
Definition miscinit.c:553
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition miscinit.c:620
void SetTempNamespaceState(Oid tempNamespaceId, Oid tempToastNamespaceId)
Definition namespace.c:3893
static char * errmsg
void RestoreUncommittedEnums(void *space)
Definition pg_enum.c:886
static uint32 DatumGetUInt32(Datum X)
Definition postgres.h:232
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
BackgroundWorker * MyBgworkerEntry
Definition postmaster.c:200
void pq_set_parallel_leader(pid_t pid, ProcNumber procNumber)
Definition pqmq.c:84
void pq_redirect_to_shm_mq(dsm_segment *seg, shm_mq_handle *mqh)
Definition pqmq.c:55
void AttachSerializableXact(SerializableXactHandle handle)
Definition predicate.c:5055
#define PqMsg_Terminate
Definition protocol.h:28
void RestoreRelationMap(char *startAddress)
Definition relmapper.c:742
void DetachSession(void)
Definition session.c:201
void AttachSession(dsm_handle handle)
Definition session.c:155
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition shm_mq.c:226
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition shm_toc.c:232
shm_toc * shm_toc_attach(uint64 magic, void *address)
Definition shm_toc.c:64
void PushActiveSnapshot(Snapshot snapshot)
Definition snapmgr.c:682
Snapshot RestoreSnapshot(char *start_address)
Definition snapmgr.c:1793
void RestoreTransactionSnapshot(Snapshot snapshot, PGPROC *source_pgproc)
Definition snapmgr.c:1853
void PopActiveSnapshot(void)
Definition snapmgr.c:775
bool BecomeLockGroupMember(PGPROC *leader, int pid)
Definition proc.c:2029
void RestorePendingSyncs(char *startAddress)
Definition storage.c:651
const char * authn_id
Definition libpq-be.h:99
pid_t parallel_leader_pid
Definition parallel.c:98
void ExitParallelMode(void)
Definition xact.c:1066
void EnterParallelMode(void)
Definition xact.c:1053
void StartTransactionCommand(void)
Definition xact.c:3081
void StartParallelWorkerTransaction(char *tstatespace)
Definition xact.c:5634
void SetParallelStartTimestamps(TimestampTz xact_ts, TimestampTz stmt_ts)
Definition xact.c:861
void EndParallelWorkerTransaction(void)
Definition xact.c:5659
void CommitTransactionCommand(void)
Definition xact.c:3179

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, AttachSerializableXact(), AttachSession(), ClientConnectionInfo::auth_method, ClientConnectionInfo::authn_id, BackgroundWorkerInitializeConnectionByOid(), BackgroundWorkerUnblockSignals(), BecomeLockGroupMember(), before_shmem_exit(), BackgroundWorker::bgw_extra, BGWORKER_BYPASS_ALLOWCONN, BGWORKER_BYPASS_ROLELOGINCHECK, CommitTransactionCommand(), CurrentMemoryContext, DatumGetUInt32(), DetachSession(), dsm_attach(), dsm_segment_address(), elog, EndParallelWorkerTransaction(), EnterParallelMode(), ereport, errcode(), errmsg, ERROR, ExitParallelMode(), fb(), GetDatabaseEncoding(), hba_authname(), InitializeSystemUser(), InitializingParallelWorker, InvalidateSystemCaches(), LookupParallelWorkerFunction(), MyBgworkerEntry, MyClientConnectionInfo, MyFixedParallelState, MyProc, PARALLEL_ERROR_QUEUE_SIZE, PARALLEL_KEY_ACTIVE_SNAPSHOT, PARALLEL_KEY_CLIENTCONNINFO, PARALLEL_KEY_COMBO_CID, PARALLEL_KEY_ENTRYPOINT, PARALLEL_KEY_ERROR_QUEUE, PARALLEL_KEY_FIXED, PARALLEL_KEY_GUC, PARALLEL_KEY_LIBRARY, PARALLEL_KEY_PENDING_SYNCS, PARALLEL_KEY_REINDEX_STATE, PARALLEL_KEY_RELMAPPER_STATE, PARALLEL_KEY_SESSION_DSM, PARALLEL_KEY_TRANSACTION_SNAPSHOT, PARALLEL_KEY_TRANSACTION_STATE, PARALLEL_KEY_UNCOMMITTEDENUMS, FixedParallelState::parallel_leader_pid, PARALLEL_MAGIC, ParallelLeaderPid, ParallelLeaderProcNumber, ParallelWorkerNumber, ParallelWorkerShutdown(), PointerGetDatum(), PopActiveSnapshot(), pq_putmessage, pq_redirect_to_shm_mq(), pq_set_parallel_leader(), PqMsg_Terminate, PushActiveSnapshot(), RestoreClientConnectionInfo(), RestoreComboCIDState(), RestoreGUCState(), RestoreLibraryState(), RestorePendingSyncs(), RestoreReindexState(), RestoreRelationMap(), RestoreSnapshot(), RestoreTransactionSnapshot(), RestoreUncommittedEnums(), SetAuthenticatedUserId(), SetClientEncoding(), SetCurrentRoleId(), SetParallelStartTimestamps(), SetSessionAuthorization(), SetTempNamespaceState(), SetUserIdAndSecContext(), shm_mq_attach(), shm_mq_set_sender(), shm_toc_attach(), shm_toc_lookup(), StartParallelWorkerTransaction(), StartTransactionCommand(), and TopMemoryContext.

◆ ParallelWorkerReportLastRecEnd()

void ParallelWorkerReportLastRecEnd ( XLogRecPtr  last_xlog_end)

Definition at line 1594 of file parallel.c.

1595{
1597
1598 Assert(fps != NULL);
1599 SpinLockAcquire(&fps->mutex);
1600 if (fps->last_xlog_end < last_xlog_end)
1601 fps->last_xlog_end = last_xlog_end;
1602 SpinLockRelease(&fps->mutex);
1603}
static void SpinLockRelease(volatile slock_t *lock)
Definition spin.h:62
static void SpinLockAcquire(volatile slock_t *lock)
Definition spin.h:56

References Assert, fb(), MyFixedParallelState, SpinLockAcquire(), and SpinLockRelease().

Referenced by CommitTransaction().

◆ ParallelWorkerShutdown()

static void ParallelWorkerShutdown ( int  code,
Datum  arg 
)
static

Definition at line 1622 of file parallel.c.

1623{
1627
1629}
Datum arg
Definition elog.c:1322
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
int SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
Definition procsignal.c:287
@ PROCSIG_PARALLEL_MESSAGE
Definition procsignal.h:34

References arg, DatumGetPointer(), dsm_detach(), ParallelLeaderPid, ParallelLeaderProcNumber, PROCSIG_PARALLEL_MESSAGE, and SendProcSignal().

Referenced by ParallelWorkerMain().

◆ ProcessParallelMessage()

static void ProcessParallelMessage ( ParallelContext pcxt,
int  i,
StringInfo  msg 
)
static

Definition at line 1146 of file parallel.c.

1147{
1148 char msgtype;
1149
1150 if (pcxt->known_attached_workers != NULL &&
1151 !pcxt->known_attached_workers[i])
1152 {
1153 pcxt->known_attached_workers[i] = true;
1155 }
1156
1157 msgtype = pq_getmsgbyte(msg);
1158
1159 switch (msgtype)
1160 {
1163 {
1166
1167 /* Parse ErrorResponse or NoticeResponse. */
1169
1170 /* Death of a worker isn't enough justification for suicide. */
1171 edata.elevel = Min(edata.elevel, ERROR);
1172
1173 /*
1174 * If desired, add a context line to show that this is a
1175 * message propagated from a parallel worker. Otherwise, it
1176 * can sometimes be confusing to understand what actually
1177 * happened. (We don't do this in DEBUG_PARALLEL_REGRESS mode
1178 * because it causes test-result instability depending on
1179 * whether a parallel worker is actually used or not.)
1180 */
1182 {
1183 if (edata.context)
1184 edata.context = psprintf("%s\n%s", edata.context,
1185 _("parallel worker"));
1186 else
1187 edata.context = pstrdup(_("parallel worker"));
1188 }
1189
1190 /*
1191 * Context beyond that should use the error context callbacks
1192 * that were in effect when the ParallelContext was created,
1193 * not the current ones.
1194 */
1197
1198 /* Rethrow error or print notice. */
1200
1201 /* Not an error, so restore previous context stack. */
1203
1204 break;
1205 }
1206
1208 {
1209 /* Propagate NotifyResponse. */
1210 int32 pid;
1211 const char *channel;
1212 const char *payload;
1213
1214 pid = pq_getmsgint(msg, 4);
1215 channel = pq_getmsgrawstring(msg);
1216 payload = pq_getmsgrawstring(msg);
1217 pq_endmessage(msg);
1218
1219 NotifyMyFrontEnd(channel, payload, pid);
1220
1221 break;
1222 }
1223
1224 case PqMsg_Progress:
1225 {
1226 /*
1227 * Only incremental progress reporting is currently supported.
1228 * However, it's possible to add more fields to the message to
1229 * allow for handling of other backend progress APIs.
1230 */
1231 int index = pq_getmsgint(msg, 4);
1232 int64 incr = pq_getmsgint64(msg);
1233
1234 pq_getmsgend(msg);
1235
1237
1238 break;
1239 }
1240
1241 case PqMsg_Terminate:
1242 {
1244 pcxt->worker[i].error_mqh = NULL;
1245 break;
1246 }
1247
1248 default:
1249 {
1250 elog(ERROR, "unrecognized message type received from parallel worker: %c (message length %d bytes)",
1251 msgtype, msg->len);
1252 }
1253 }
1254}
void NotifyMyFrontEnd(const char *channel, const char *payload, int32 srcPid)
Definition async.c:3093
void pgstat_progress_incr_param(int index, int64 incr)
#define Min(x, y)
Definition c.h:1054
int64_t int64
Definition c.h:576
int32_t int32
Definition c.h:575
void ThrowErrorData(ErrorData *edata)
Definition elog.c:2090
#define _(x)
Definition elog.c:95
@ DEBUG_PARALLEL_REGRESS
Definition optimizer.h:98
int debug_parallel_query
Definition planner.c:69
unsigned int pq_getmsgint(StringInfo msg, int b)
Definition pqformat.c:414
void pq_getmsgend(StringInfo msg)
Definition pqformat.c:634
void pq_endmessage(StringInfo buf)
Definition pqformat.c:296
int pq_getmsgbyte(StringInfo msg)
Definition pqformat.c:398
const char * pq_getmsgrawstring(StringInfo msg)
Definition pqformat.c:607
int64 pq_getmsgint64(StringInfo msg)
Definition pqformat.c:452
void pq_parse_errornotice(StringInfo msg, ErrorData *edata)
Definition pqmq.c:224
#define PqMsg_NotificationResponse
Definition protocol.h:41
#define PqMsg_Progress
Definition protocol.h:70
#define PqMsg_ErrorResponse
Definition protocol.h:44
#define PqMsg_NoticeResponse
Definition protocol.h:49
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
Definition type.h:96

References _, debug_parallel_query, DEBUG_PARALLEL_REGRESS, elog, ERROR, error_context_stack, ParallelContext::error_context_stack, ParallelWorkerInfo::error_mqh, fb(), i, ParallelContext::known_attached_workers, StringInfoData::len, Min, ParallelContext::nknown_attached_workers, NotifyMyFrontEnd(), pgstat_progress_incr_param(), pq_endmessage(), pq_getmsgbyte(), pq_getmsgend(), pq_getmsgint(), pq_getmsgint64(), pq_getmsgrawstring(), pq_parse_errornotice(), PqMsg_ErrorResponse, PqMsg_NoticeResponse, PqMsg_NotificationResponse, PqMsg_Progress, PqMsg_Terminate, psprintf(), pstrdup(), shm_mq_detach(), ThrowErrorData(), and ParallelContext::worker.

Referenced by ProcessParallelMessages().

◆ ProcessParallelMessages()

void ProcessParallelMessages ( void  )

Definition at line 1057 of file parallel.c.

1058{
1059 dlist_iter iter;
1060 MemoryContext oldcontext;
1061
1063
1064 /*
1065 * This is invoked from ProcessInterrupts(), and since some of the
1066 * functions it calls contain CHECK_FOR_INTERRUPTS(), there is a potential
1067 * for recursive calls if more signals are received while this runs. It's
1068 * unclear that recursive entry would be safe, and it doesn't seem useful
1069 * even if it is safe, so let's block interrupts until done.
1070 */
1072
1073 /*
1074 * Moreover, CurrentMemoryContext might be pointing almost anywhere. We
1075 * don't want to risk leaking data into long-lived contexts, so let's do
1076 * our work here in a private context that we can reset on each use.
1077 */
1078 if (hpm_context == NULL) /* first time through? */
1080 "ProcessParallelMessages",
1082 else
1084
1085 oldcontext = MemoryContextSwitchTo(hpm_context);
1086
1087 /* OK to process messages. Reset the flag saying there are more to do. */
1088 ParallelMessagePending = false;
1089
1090 dlist_foreach(iter, &pcxt_list)
1091 {
1092 ParallelContext *pcxt;
1093 int i;
1094
1095 pcxt = dlist_container(ParallelContext, node, iter.cur);
1096 if (pcxt->worker == NULL)
1097 continue;
1098
1099 for (i = 0; i < pcxt->nworkers_launched; ++i)
1100 {
1101 /*
1102 * Read as many messages as we can from each worker, but stop when
1103 * either (1) the worker's error queue goes away, which can happen
1104 * if we receive a Terminate message from the worker; or (2) no
1105 * more messages can be read from the worker without blocking.
1106 */
1107 while (pcxt->worker[i].error_mqh != NULL)
1108 {
1109 shm_mq_result res;
1110 Size nbytes;
1111 void *data;
1112
1113 res = shm_mq_receive(pcxt->worker[i].error_mqh, &nbytes,
1114 &data, true);
1115 if (res == SHM_MQ_WOULD_BLOCK)
1116 break;
1117 else if (res == SHM_MQ_SUCCESS)
1118 {
1119 StringInfoData msg;
1120
1121 initStringInfo(&msg);
1122 appendBinaryStringInfo(&msg, data, nbytes);
1123 ProcessParallelMessage(pcxt, i, &msg);
1124 pfree(msg.data);
1125 }
1126 else
1127 ereport(ERROR,
1129 errmsg("lost connection to parallel worker")));
1130 }
1131 }
1132 }
1133
1134 MemoryContextSwitchTo(oldcontext);
1135
1136 /* Might as well clear the context on our way out */
1138
1140}
static void ProcessParallelMessage(ParallelContext *pcxt, int i, StringInfo msg)
Definition parallel.c:1146
#define dlist_foreach(iter, lhead)
Definition ilist.h:623
#define dlist_container(type, membername, ptr)
Definition ilist.h:593
void MemoryContextReset(MemoryContext context)
Definition mcxt.c:403
const void * data
shm_mq_result shm_mq_receive(shm_mq_handle *mqh, Size *nbytesp, void **datap, bool nowait)
Definition shm_mq.c:574
shm_mq_result
Definition shm_mq.h:39
@ SHM_MQ_SUCCESS
Definition shm_mq.h:40
@ SHM_MQ_WOULD_BLOCK
Definition shm_mq.h:41
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition stringinfo.c:281
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
dlist_node * cur
Definition ilist.h:179

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, appendBinaryStringInfo(), dlist_iter::cur, StringInfoData::data, data, dlist_container, dlist_foreach, ereport, errcode(), errmsg, ERROR, ParallelWorkerInfo::error_mqh, fb(), HOLD_INTERRUPTS, i, initStringInfo(), MemoryContextReset(), MemoryContextSwitchTo(), ParallelContext::nworkers_launched, ParallelMessagePending, pcxt_list, pfree(), ProcessParallelMessage(), RESUME_INTERRUPTS, shm_mq_receive(), SHM_MQ_SUCCESS, SHM_MQ_WOULD_BLOCK, TopMemoryContext, and ParallelContext::worker.

Referenced by ProcessInterrupts().

◆ ReinitializeParallelDSM()

void ReinitializeParallelDSM ( ParallelContext pcxt)

Definition at line 511 of file parallel.c.

512{
513 MemoryContext oldcontext;
515
516 /* We might be running in a very short-lived memory context. */
518
519 /* Wait for any old workers to exit. */
520 if (pcxt->nworkers_launched > 0)
521 {
524 pcxt->nworkers_launched = 0;
525 if (pcxt->known_attached_workers)
526 {
529 pcxt->nknown_attached_workers = 0;
530 }
531 }
532
533 /* Reset a few bits of fixed parallel state to a clean state. */
534 fps = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_FIXED, false);
535 fps->last_xlog_end = InvalidXLogRecPtr;
536
537 /* Recreate error queues (if they exist). */
538 if (pcxt->nworkers > 0)
539 {
540 char *error_queue_space;
541 int i;
542
545 for (i = 0; i < pcxt->nworkers; ++i)
546 {
547 char *start;
548 shm_mq *mq;
549
553 pcxt->worker[i].error_mqh = shm_mq_attach(mq, pcxt->seg, NULL);
554 }
555 }
556
557 /* Restore previous memory context. */
558 MemoryContextSwitchTo(oldcontext);
559}
void WaitForParallelWorkersToFinish(ParallelContext *pcxt)
Definition parallel.c:805

References ParallelWorkerInfo::error_mqh, fb(), i, InvalidXLogRecPtr, ParallelContext::known_attached_workers, MemoryContextSwitchTo(), MyProc, ParallelContext::nknown_attached_workers, ParallelContext::nworkers, ParallelContext::nworkers_launched, PARALLEL_ERROR_QUEUE_SIZE, PARALLEL_KEY_ERROR_QUEUE, PARALLEL_KEY_FIXED, pfree(), ParallelContext::seg, shm_mq_attach(), shm_mq_create(), shm_mq_set_receiver(), shm_toc_lookup(), start, ParallelContext::toc, TopTransactionContext, WaitForParallelWorkersToExit(), WaitForParallelWorkersToFinish(), and ParallelContext::worker.

Referenced by ExecParallelReinitialize(), and parallel_vacuum_process_all_indexes().

◆ ReinitializeParallelWorkers()

void ReinitializeParallelWorkers ( ParallelContext pcxt,
int  nworkers_to_launch 
)

Definition at line 568 of file parallel.c.

569{
570 /*
571 * The number of workers that need to be launched must be less than the
572 * number of workers with which the parallel context is initialized. But
573 * the caller might not know that InitializeParallelDSM reduced nworkers,
574 * so just silently trim the request.
575 */
576 pcxt->nworkers_to_launch = Min(pcxt->nworkers, nworkers_to_launch);
577}

References Min, ParallelContext::nworkers, and ParallelContext::nworkers_to_launch.

Referenced by parallel_vacuum_process_all_indexes().

◆ WaitForParallelWorkersToAttach()

void WaitForParallelWorkersToAttach ( ParallelContext pcxt)

Definition at line 702 of file parallel.c.

703{
704 int i;
705
706 /* Skip this if we have no launched workers. */
707 if (pcxt->nworkers_launched == 0)
708 return;
709
710 for (;;)
711 {
712 /*
713 * This will process any parallel messages that are pending and it may
714 * also throw an error propagated from a worker.
715 */
717
718 for (i = 0; i < pcxt->nworkers_launched; ++i)
719 {
720 BgwHandleStatus status;
721 shm_mq *mq;
722 int rc;
723 pid_t pid;
724
725 if (pcxt->known_attached_workers[i])
726 continue;
727
728 /*
729 * If error_mqh is NULL, then the worker has already exited
730 * cleanly.
731 */
732 if (pcxt->worker[i].error_mqh == NULL)
733 {
734 pcxt->known_attached_workers[i] = true;
736 continue;
737 }
738
739 status = GetBackgroundWorkerPid(pcxt->worker[i].bgwhandle, &pid);
740 if (status == BGWH_STARTED)
741 {
742 /* Has the worker attached to the error queue? */
744 if (shm_mq_get_sender(mq) != NULL)
745 {
746 /* Yes, so it is known to be attached. */
747 pcxt->known_attached_workers[i] = true;
749 }
750 }
751 else if (status == BGWH_STOPPED)
752 {
753 /*
754 * If the worker stopped without attaching to the error queue,
755 * throw an error.
756 */
758 if (shm_mq_get_sender(mq) == NULL)
761 errmsg("parallel worker failed to initialize"),
762 errhint("More details may be available in the server log.")));
763
764 pcxt->known_attached_workers[i] = true;
766 }
767 else
768 {
769 /*
770 * Worker not yet started, so we must wait. The postmaster
771 * will notify us if the worker's state changes. Our latch
772 * might also get set for some other reason, but if so we'll
773 * just end up waiting for the same worker again.
774 */
775 rc = WaitLatch(MyLatch,
778
779 if (rc & WL_LATCH_SET)
781 }
782 }
783
784 /* If all workers are known to have started, we're done. */
786 {
788 break;
789 }
790 }
791}
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition bgworker.c:1165
BgwHandleStatus
Definition bgworker.h:111
@ BGWH_STARTED
Definition bgworker.h:112
@ BGWH_STOPPED
Definition bgworker.h:114
int errhint(const char *fmt,...) pg_attribute_printf(1
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
shm_mq * shm_mq_get_queue(shm_mq_handle *mqh)
Definition shm_mq.c:907
PGPROC * shm_mq_get_sender(shm_mq *mq)
Definition shm_mq.c:259
#define WL_EXIT_ON_PM_DEATH
#define WL_LATCH_SET

References Assert, BGWH_STARTED, BGWH_STOPPED, ParallelWorkerInfo::bgwhandle, CHECK_FOR_INTERRUPTS, ereport, errcode(), errhint(), errmsg, ERROR, ParallelWorkerInfo::error_mqh, fb(), GetBackgroundWorkerPid(), i, ParallelContext::known_attached_workers, MyLatch, ParallelContext::nknown_attached_workers, ParallelContext::nworkers_launched, ResetLatch(), shm_mq_get_queue(), shm_mq_get_sender(), WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and ParallelContext::worker.

Referenced by _brin_begin_parallel(), _bt_begin_parallel(), and _gin_begin_parallel().

◆ WaitForParallelWorkersToExit()

static void WaitForParallelWorkersToExit ( ParallelContext pcxt)
static

Definition at line 919 of file parallel.c.

920{
921 int i;
922
923 /* Wait until the workers actually die. */
924 for (i = 0; i < pcxt->nworkers_launched; ++i)
925 {
926 BgwHandleStatus status;
927
928 if (pcxt->worker == NULL || pcxt->worker[i].bgwhandle == NULL)
929 continue;
930
932
933 /*
934 * If the postmaster kicked the bucket, we have no chance of cleaning
935 * up safely -- we won't be able to tell when our workers are actually
936 * dead. This doesn't necessitate a PANIC since they will all abort
937 * eventually, but we can't safely continue this session.
938 */
939 if (status == BGWH_POSTMASTER_DIED)
942 errmsg("postmaster exited during a parallel transaction")));
943
944 /* Release memory. */
945 pfree(pcxt->worker[i].bgwhandle);
946 pcxt->worker[i].bgwhandle = NULL;
947 }
948}
BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle)
Definition bgworker.c:1265
@ BGWH_POSTMASTER_DIED
Definition bgworker.h:115
#define FATAL
Definition elog.h:41

References BGWH_POSTMASTER_DIED, ParallelWorkerInfo::bgwhandle, ereport, errcode(), errmsg, FATAL, fb(), i, ParallelContext::nworkers_launched, pfree(), WaitForBackgroundWorkerShutdown(), and ParallelContext::worker.

Referenced by DestroyParallelContext(), and ReinitializeParallelDSM().

◆ WaitForParallelWorkersToFinish()

void WaitForParallelWorkersToFinish ( ParallelContext pcxt)

Definition at line 805 of file parallel.c.

806{
807 for (;;)
808 {
809 bool anyone_alive = false;
810 int nfinished = 0;
811 int i;
812
813 /*
814 * This will process any parallel messages that are pending, which may
815 * change the outcome of the loop that follows. It may also throw an
816 * error propagated from a worker.
817 */
819
820 for (i = 0; i < pcxt->nworkers_launched; ++i)
821 {
822 /*
823 * If error_mqh is NULL, then the worker has already exited
824 * cleanly. If we have received a message through error_mqh from
825 * the worker, we know it started up cleanly, and therefore we're
826 * certain to be notified when it exits.
827 */
828 if (pcxt->worker[i].error_mqh == NULL)
829 ++nfinished;
830 else if (pcxt->known_attached_workers[i])
831 {
832 anyone_alive = true;
833 break;
834 }
835 }
836
837 if (!anyone_alive)
838 {
839 /* If all workers are known to have finished, we're done. */
840 if (nfinished >= pcxt->nworkers_launched)
841 {
843 break;
844 }
845
846 /*
847 * We didn't detect any living workers, but not all workers are
848 * known to have exited cleanly. Either not all workers have
849 * launched yet, or maybe some of them failed to start or
850 * terminated abnormally.
851 */
852 for (i = 0; i < pcxt->nworkers_launched; ++i)
853 {
854 pid_t pid;
855 shm_mq *mq;
856
857 /*
858 * If the worker is BGWH_NOT_YET_STARTED or BGWH_STARTED, we
859 * should just keep waiting. If it is BGWH_STOPPED, then
860 * further investigation is needed.
861 */
862 if (pcxt->worker[i].error_mqh == NULL ||
863 pcxt->worker[i].bgwhandle == NULL ||
865 &pid) != BGWH_STOPPED)
866 continue;
867
868 /*
869 * Check whether the worker ended up stopped without ever
870 * attaching to the error queue. If so, the postmaster was
871 * unable to fork the worker or it exited without initializing
872 * properly. We must throw an error, since the caller may
873 * have been expecting the worker to do some work before
874 * exiting.
875 */
877 if (shm_mq_get_sender(mq) == NULL)
880 errmsg("parallel worker failed to initialize"),
881 errhint("More details may be available in the server log.")));
882
883 /*
884 * The worker is stopped, but is attached to the error queue.
885 * Unless there's a bug somewhere, this will only happen when
886 * the worker writes messages and terminates after the
887 * CHECK_FOR_INTERRUPTS() near the top of this function and
888 * before the call to GetBackgroundWorkerPid(). In that case,
889 * our latch should have been set as well and the right things
890 * will happen on the next pass through the loop.
891 */
892 }
893 }
894
898 }
899
900 if (pcxt->toc != NULL)
901 {
903
904 fps = shm_toc_lookup(pcxt->toc, PARALLEL_KEY_FIXED, false);
905 if (fps->last_xlog_end > XactLastRecEnd)
906 XactLastRecEnd = fps->last_xlog_end;
907 }
908}
XLogRecPtr XactLastRecEnd
Definition xlog.c:258

References Assert, BGWH_STOPPED, ParallelWorkerInfo::bgwhandle, CHECK_FOR_INTERRUPTS, ereport, errcode(), errhint(), errmsg, ERROR, ParallelWorkerInfo::error_mqh, fb(), GetBackgroundWorkerPid(), i, ParallelContext::known_attached_workers, MyLatch, ParallelContext::nworkers_launched, PARALLEL_KEY_FIXED, ResetLatch(), shm_mq_get_queue(), shm_mq_get_sender(), shm_toc_lookup(), ParallelContext::toc, WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, ParallelContext::worker, and XactLastRecEnd.

Referenced by _brin_end_parallel(), _bt_end_parallel(), _gin_end_parallel(), ExecParallelFinish(), parallel_vacuum_process_all_indexes(), and ReinitializeParallelDSM().

Variable Documentation

◆ fn_addr

Definition at line 141 of file parallel.c.

Referenced by LookupParallelWorkerFunction().

◆ fn_name

const char* fn_name

Definition at line 140 of file parallel.c.

Referenced by LookupParallelWorkerFunction().

◆ InitializingParallelWorker

◆ [struct]

const struct { ... } InternalParallelWorkers[]
Initial value:
=
{
{
"ParallelQueryMain", ParallelQueryMain
},
{
"_bt_parallel_build_main", _bt_parallel_build_main
},
{
"_brin_parallel_build_main", _brin_parallel_build_main
},
{
"_gin_parallel_build_main", _gin_parallel_build_main
},
{
"parallel_vacuum_main", parallel_vacuum_main
}
}
void _brin_parallel_build_main(dsm_segment *seg, shm_toc *toc)
Definition brin.c:2880
void ParallelQueryMain(dsm_segment *seg, shm_toc *toc)
void _gin_parallel_build_main(dsm_segment *seg, shm_toc *toc)
Definition gininsert.c:2103
void _bt_parallel_build_main(dsm_segment *seg, shm_toc *toc)
Definition nbtsort.c:1743
void parallel_vacuum_main(dsm_segment *seg, shm_toc *toc)

Referenced by LookupParallelWorkerFunction().

◆ MyFixedParallelState

FixedParallelState* MyFixedParallelState
static

Definition at line 126 of file parallel.c.

Referenced by ParallelWorkerMain(), and ParallelWorkerReportLastRecEnd().

◆ ParallelLeaderPid

pid_t ParallelLeaderPid
static

Definition at line 132 of file parallel.c.

Referenced by ParallelWorkerMain(), and ParallelWorkerShutdown().

◆ ParallelMessagePending

volatile sig_atomic_t ParallelMessagePending = false

◆ ParallelWorkerNumber

◆ pcxt_list