PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
procsignal.h File Reference
Include dependency graph for procsignal.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define NUM_PROCSIGNALS   (PROCSIG_RECOVERY_CONFLICT_LAST + 1)
 
#define MAX_CANCEL_KEY_LENGTH   32
 

Typedefs

typedef struct ProcSignalHeader ProcSignalHeader
 

Enumerations

enum  ProcSignalReason {
  PROCSIG_CATCHUP_INTERRUPT , PROCSIG_NOTIFY_INTERRUPT , PROCSIG_PARALLEL_MESSAGE , PROCSIG_WALSND_INIT_STOPPING ,
  PROCSIG_BARRIER , PROCSIG_LOG_MEMORY_CONTEXT , PROCSIG_GET_MEMORY_CONTEXT , PROCSIG_PARALLEL_APPLY_MESSAGE ,
  PROCSIG_RECOVERY_CONFLICT_FIRST , PROCSIG_RECOVERY_CONFLICT_DATABASE = PROCSIG_RECOVERY_CONFLICT_FIRST , PROCSIG_RECOVERY_CONFLICT_TABLESPACE , PROCSIG_RECOVERY_CONFLICT_LOCK ,
  PROCSIG_RECOVERY_CONFLICT_SNAPSHOT , PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT , PROCSIG_RECOVERY_CONFLICT_BUFFERPIN , PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK ,
  PROCSIG_RECOVERY_CONFLICT_LAST = PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK
}
 
enum  ProcSignalBarrierType { PROCSIGNAL_BARRIER_SMGRRELEASE }
 

Functions

Size ProcSignalShmemSize (void)
 
void ProcSignalShmemInit (void)
 
void ProcSignalInit (char *cancel_key, int cancel_key_len)
 
int SendProcSignal (pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
 
void SendCancelRequest (int backendPID, char *cancel_key, int cancel_key_len)
 
uint64 EmitProcSignalBarrier (ProcSignalBarrierType type)
 
void WaitForProcSignalBarrier (uint64 generation)
 
void ProcessProcSignalBarrier (void)
 
void procsignal_sigusr1_handler (SIGNAL_ARGS)
 

Macro Definition Documentation

◆ MAX_CANCEL_KEY_LENGTH

#define MAX_CANCEL_KEY_LENGTH   32

Definition at line 68 of file procsignal.h.

◆ NUM_PROCSIGNALS

#define NUM_PROCSIGNALS   (PROCSIG_RECOVERY_CONFLICT_LAST + 1)

Definition at line 53 of file procsignal.h.

Typedef Documentation

◆ ProcSignalHeader

Definition at line 88 of file procsignal.h.

Enumeration Type Documentation

◆ ProcSignalBarrierType

Enumerator
PROCSIGNAL_BARRIER_SMGRRELEASE 

Definition at line 55 of file procsignal.h.

56{
57 PROCSIGNAL_BARRIER_SMGRRELEASE, /* ask smgr to close files */
ProcSignalBarrierType
Definition: procsignal.h:56
@ PROCSIGNAL_BARRIER_SMGRRELEASE
Definition: procsignal.h:57

◆ ProcSignalReason

Enumerator
PROCSIG_CATCHUP_INTERRUPT 
PROCSIG_NOTIFY_INTERRUPT 
PROCSIG_PARALLEL_MESSAGE 
PROCSIG_WALSND_INIT_STOPPING 
PROCSIG_BARRIER 
PROCSIG_LOG_MEMORY_CONTEXT 
PROCSIG_GET_MEMORY_CONTEXT 
PROCSIG_PARALLEL_APPLY_MESSAGE 
PROCSIG_RECOVERY_CONFLICT_FIRST 
PROCSIG_RECOVERY_CONFLICT_DATABASE 
PROCSIG_RECOVERY_CONFLICT_TABLESPACE 
PROCSIG_RECOVERY_CONFLICT_LOCK 
PROCSIG_RECOVERY_CONFLICT_SNAPSHOT 
PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT 
PROCSIG_RECOVERY_CONFLICT_BUFFERPIN 
PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK 
PROCSIG_RECOVERY_CONFLICT_LAST 

Definition at line 30 of file procsignal.h.

31{
32 PROCSIG_CATCHUP_INTERRUPT, /* sinval catchup interrupt */
33 PROCSIG_NOTIFY_INTERRUPT, /* listen/notify interrupt */
34 PROCSIG_PARALLEL_MESSAGE, /* message from cooperating parallel backend */
35 PROCSIG_WALSND_INIT_STOPPING, /* ask walsenders to prepare for shutdown */
36 PROCSIG_BARRIER, /* global barrier interrupt */
37 PROCSIG_LOG_MEMORY_CONTEXT, /* ask backend to log the memory contexts */
38 PROCSIG_GET_MEMORY_CONTEXT, /* ask backend to send the memory contexts */
39 PROCSIG_PARALLEL_APPLY_MESSAGE, /* Message from parallel apply workers */
40
41 /* Recovery conflict reasons */
ProcSignalReason
Definition: procsignal.h:31
@ PROCSIG_GET_MEMORY_CONTEXT
Definition: procsignal.h:38
@ PROCSIG_PARALLEL_MESSAGE
Definition: procsignal.h:34
@ PROCSIG_RECOVERY_CONFLICT_BUFFERPIN
Definition: procsignal.h:48
@ PROCSIG_CATCHUP_INTERRUPT
Definition: procsignal.h:32
@ PROCSIG_RECOVERY_CONFLICT_LOCK
Definition: procsignal.h:45
@ PROCSIG_LOG_MEMORY_CONTEXT
Definition: procsignal.h:37
@ PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT
Definition: procsignal.h:47
@ PROCSIG_BARRIER
Definition: procsignal.h:36
@ PROCSIG_RECOVERY_CONFLICT_DATABASE
Definition: procsignal.h:43
@ PROCSIG_WALSND_INIT_STOPPING
Definition: procsignal.h:35
@ PROCSIG_PARALLEL_APPLY_MESSAGE
Definition: procsignal.h:39
@ PROCSIG_RECOVERY_CONFLICT_SNAPSHOT
Definition: procsignal.h:46
@ PROCSIG_RECOVERY_CONFLICT_LAST
Definition: procsignal.h:50
@ PROCSIG_RECOVERY_CONFLICT_FIRST
Definition: procsignal.h:42
@ PROCSIG_NOTIFY_INTERRUPT
Definition: procsignal.h:33
@ PROCSIG_RECOVERY_CONFLICT_TABLESPACE
Definition: procsignal.h:44
@ PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK
Definition: procsignal.h:49

Function Documentation

◆ EmitProcSignalBarrier()

uint64 EmitProcSignalBarrier ( ProcSignalBarrierType  type)

Definition at line 355 of file procsignal.c.

356{
357 uint32 flagbit = 1 << (uint32) type;
358 uint64 generation;
359
360 /*
361 * Set all the flags.
362 *
363 * Note that pg_atomic_fetch_or_u32 has full barrier semantics, so this is
364 * totally ordered with respect to anything the caller did before, and
365 * anything that we do afterwards. (This is also true of the later call to
366 * pg_atomic_add_fetch_u64.)
367 */
368 for (int i = 0; i < NumProcSignalSlots; i++)
369 {
370 volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
371
373 }
374
375 /*
376 * Increment the generation counter.
377 */
378 generation =
380
381 /*
382 * Signal all the processes, so that they update their advertised barrier
383 * generation.
384 *
385 * Concurrency is not a problem here. Backends that have exited don't
386 * matter, and new backends that have joined since we entered this
387 * function must already have current state, since the caller is
388 * responsible for making sure that the relevant state is entirely visible
389 * before calling this function in the first place. We still have to wake
390 * them up - because we can't distinguish between such backends and older
391 * backends that need to update state - but they won't actually need to
392 * change any state.
393 */
394 for (int i = NumProcSignalSlots - 1; i >= 0; i--)
395 {
396 volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
397 pid_t pid = pg_atomic_read_u32(&slot->pss_pid);
398
399 if (pid != 0)
400 {
402 pid = pg_atomic_read_u32(&slot->pss_pid);
403 if (pid != 0)
404 {
405 /* see SendProcSignal for details */
406 slot->pss_signalFlags[PROCSIG_BARRIER] = true;
408 kill(pid, SIGUSR1);
409 }
410 else
412 }
413 }
414
415 return generation;
416}
static uint32 pg_atomic_fetch_or_u32(volatile pg_atomic_uint32 *ptr, uint32 or_)
Definition: atomics.h:410
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:239
static uint64 pg_atomic_add_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition: atomics.h:559
uint64_t uint64
Definition: c.h:503
uint32_t uint32
Definition: c.h:502
int i
Definition: isn.c:77
#define NumProcSignalSlots
Definition: procsignal.c:94
NON_EXEC_STATIC ProcSignalHeader * ProcSignal
Definition: procsignal.c:104
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
ProcSignalSlot psh_slot[FLEXIBLE_ARRAY_MEMBER]
Definition: procsignal.c:86
pg_atomic_uint64 psh_barrierGeneration
Definition: procsignal.c:85
volatile sig_atomic_t pss_signalFlags[NUM_PROCSIGNALS]
Definition: procsignal.c:68
slock_t pss_mutex
Definition: procsignal.c:69
pg_atomic_uint32 pss_pid
Definition: procsignal.c:65
pg_atomic_uint32 pss_barrierCheckMask
Definition: procsignal.c:73
const char * type
#define kill(pid, sig)
Definition: win32_port.h:493
#define SIGUSR1
Definition: win32_port.h:170

References i, kill, NumProcSignalSlots, pg_atomic_add_fetch_u64(), pg_atomic_fetch_or_u32(), pg_atomic_read_u32(), PROCSIG_BARRIER, ProcSignal, ProcSignalHeader::psh_barrierGeneration, ProcSignalHeader::psh_slot, ProcSignalSlot::pss_barrierCheckMask, ProcSignalSlot::pss_mutex, ProcSignalSlot::pss_pid, ProcSignalSlot::pss_signalFlags, SIGUSR1, SpinLockAcquire, SpinLockRelease, and type.

Referenced by dbase_redo(), dropdb(), DropTableSpace(), movedb(), and tblspc_redo().

◆ ProcessProcSignalBarrier()

void ProcessProcSignalBarrier ( void  )

Definition at line 498 of file procsignal.c.

499{
500 uint64 local_gen;
501 uint64 shared_gen;
502 volatile uint32 flags;
503
505
506 /* Exit quickly if there's no work to do. */
508 return;
510
511 /*
512 * It's not unlikely to process multiple barriers at once, before the
513 * signals for all the barriers have arrived. To avoid unnecessary work in
514 * response to subsequent signals, exit early if we already have processed
515 * all of them.
516 */
519
520 Assert(local_gen <= shared_gen);
521
522 if (local_gen == shared_gen)
523 return;
524
525 /*
526 * Get and clear the flags that are set for this backend. Note that
527 * pg_atomic_exchange_u32 is a full barrier, so we're guaranteed that the
528 * read of the barrier generation above happens before we atomically
529 * extract the flags, and that any subsequent state changes happen
530 * afterward.
531 *
532 * NB: In order to avoid race conditions, we must zero
533 * pss_barrierCheckMask first and only afterwards try to do barrier
534 * processing. If we did it in the other order, someone could send us
535 * another barrier of some type right after we called the
536 * barrier-processing function but before we cleared the bit. We would
537 * have no way of knowing that the bit needs to stay set in that case, so
538 * the need to call the barrier-processing function again would just get
539 * forgotten. So instead, we tentatively clear all the bits and then put
540 * back any for which we don't manage to successfully absorb the barrier.
541 */
543
544 /*
545 * If there are no flags set, then we can skip doing any real work.
546 * Otherwise, establish a PG_TRY block, so that we don't lose track of
547 * which types of barrier processing are needed if an ERROR occurs.
548 */
549 if (flags != 0)
550 {
551 bool success = true;
552
553 PG_TRY();
554 {
555 /*
556 * Process each type of barrier. The barrier-processing functions
557 * should normally return true, but may return false if the
558 * barrier can't be absorbed at the current time. This should be
559 * rare, because it's pretty expensive. Every single
560 * CHECK_FOR_INTERRUPTS() will return here until we manage to
561 * absorb the barrier, and that cost will add up in a hurry.
562 *
563 * NB: It ought to be OK to call the barrier-processing functions
564 * unconditionally, but it's more efficient to call only the ones
565 * that might need us to do something based on the flags.
566 */
567 while (flags != 0)
568 {
570 bool processed = true;
571
573 switch (type)
574 {
576 processed = ProcessBarrierSmgrRelease();
577 break;
578 }
579
580 /*
581 * To avoid an infinite loop, we must always unset the bit in
582 * flags.
583 */
584 BARRIER_CLEAR_BIT(flags, type);
585
586 /*
587 * If we failed to process the barrier, reset the shared bit
588 * so we try again later, and set a flag so that we don't bump
589 * our generation.
590 */
591 if (!processed)
592 {
594 success = false;
595 }
596 }
597 }
598 PG_CATCH();
599 {
600 /*
601 * If an ERROR occurred, we'll need to try again later to handle
602 * that barrier type and any others that haven't been handled yet
603 * or weren't successfully absorbed.
604 */
606 PG_RE_THROW();
607 }
608 PG_END_TRY();
609
610 /*
611 * If some barrier types were not successfully absorbed, we will have
612 * to try again later.
613 */
614 if (!success)
615 return;
616 }
617
618 /*
619 * State changes related to all types of barriers that might have been
620 * emitted have now been handled, so we can update our notion of the
621 * generation to the one we observed before beginning the updates. If
622 * things have changed further, it'll get fixed up when this function is
623 * next called.
624 */
627}
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:485
static uint32 pg_atomic_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 newval)
Definition: atomics.h:330
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:467
void ConditionVariableBroadcast(ConditionVariable *cv)
#define PG_RE_THROW()
Definition: elog.h:405
#define PG_TRY(...)
Definition: elog.h:372
#define PG_END_TRY(...)
Definition: elog.h:397
#define PG_CATCH(...)
Definition: elog.h:382
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:40
Assert(PointerIsAligned(start, uint64))
static bool success
Definition: initdb.c:187
static int pg_rightmost_one_pos32(uint32 word)
Definition: pg_bitutils.h:111
static void ResetProcSignalBarrierBits(uint32 flags)
Definition: procsignal.c:635
static ProcSignalSlot * MyProcSignalSlot
Definition: procsignal.c:105
#define BARRIER_CLEAR_BIT(flags, type)
Definition: procsignal.c:101
bool ProcessBarrierSmgrRelease(void)
Definition: smgr.c:1018
ConditionVariable pss_barrierCV
Definition: procsignal.c:74
pg_atomic_uint64 pss_barrierGeneration
Definition: procsignal.c:72

References Assert(), BARRIER_CLEAR_BIT, ConditionVariableBroadcast(), MyProcSignalSlot, pg_atomic_exchange_u32(), pg_atomic_read_u64(), pg_atomic_write_u64(), PG_CATCH, PG_END_TRY, PG_RE_THROW, pg_rightmost_one_pos32(), PG_TRY, ProcessBarrierSmgrRelease(), ProcSignal, PROCSIGNAL_BARRIER_SMGRRELEASE, ProcSignalBarrierPending, ProcSignalHeader::psh_barrierGeneration, ProcSignalSlot::pss_barrierCheckMask, ProcSignalSlot::pss_barrierCV, ProcSignalSlot::pss_barrierGeneration, ResetProcSignalBarrierBits(), success, and type.

Referenced by BufferSync(), CheckpointWriteDelay(), ProcessAutoVacLauncherInterrupts(), ProcessCheckpointerInterrupts(), ProcessInterrupts(), ProcessMainLoopInterrupts(), ProcessPgArchInterrupts(), ProcessStartupProcInterrupts(), and ProcessWalSummarizerInterrupts().

◆ procsignal_sigusr1_handler()

void procsignal_sigusr1_handler ( SIGNAL_ARGS  )

Definition at line 673 of file procsignal.c.

674{
677
680
683
686
689
692
695
698
701
704
707
710
713
716
719
721}
void HandleParallelApplyMessageInterrupt(void)
void HandleNotifyInterrupt(void)
Definition: async.c:1804
void HandleParallelMessageInterrupt(void)
Definition: parallel.c:1037
struct Latch * MyLatch
Definition: globals.c:64
void SetLatch(Latch *latch)
Definition: latch.c:288
void HandleGetMemoryContextInterrupt(void)
Definition: mcxt.c:1363
void HandleLogMemoryContextInterrupt(void)
Definition: mcxt.c:1347
void HandleRecoveryConflictInterrupt(ProcSignalReason reason)
Definition: postgres.c:3091
static bool CheckProcSignal(ProcSignalReason reason)
Definition: procsignal.c:648
static void HandleProcSignalBarrierInterrupt(void)
Definition: procsignal.c:482
void HandleCatchupInterrupt(void)
Definition: sinval.c:154
void HandleWalSndInitStopping(void)
Definition: walsender.c:3565

References CheckProcSignal(), HandleCatchupInterrupt(), HandleGetMemoryContextInterrupt(), HandleLogMemoryContextInterrupt(), HandleNotifyInterrupt(), HandleParallelApplyMessageInterrupt(), HandleParallelMessageInterrupt(), HandleProcSignalBarrierInterrupt(), HandleRecoveryConflictInterrupt(), HandleWalSndInitStopping(), MyLatch, PROCSIG_BARRIER, PROCSIG_CATCHUP_INTERRUPT, PROCSIG_GET_MEMORY_CONTEXT, PROCSIG_LOG_MEMORY_CONTEXT, PROCSIG_NOTIFY_INTERRUPT, PROCSIG_PARALLEL_APPLY_MESSAGE, PROCSIG_PARALLEL_MESSAGE, PROCSIG_RECOVERY_CONFLICT_BUFFERPIN, PROCSIG_RECOVERY_CONFLICT_DATABASE, PROCSIG_RECOVERY_CONFLICT_LOCK, PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT, PROCSIG_RECOVERY_CONFLICT_SNAPSHOT, PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK, PROCSIG_RECOVERY_CONFLICT_TABLESPACE, PROCSIG_WALSND_INIT_STOPPING, and SetLatch().

Referenced by autoprewarm_main(), AutoVacLauncherMain(), AutoVacWorkerMain(), BackgroundWorkerMain(), BackgroundWriterMain(), CheckpointerMain(), IoWorkerMain(), PgArchiverMain(), PostgresMain(), ReplSlotSyncWorkerMain(), StartupProcessMain(), WalReceiverMain(), WalSndSignals(), WalSummarizerMain(), and WalWriterMain().

◆ ProcSignalInit()

void ProcSignalInit ( char *  cancel_key,
int  cancel_key_len 
)

Definition at line 165 of file procsignal.c.

166{
167 ProcSignalSlot *slot;
168 uint64 barrier_generation;
169 uint32 old_pss_pid;
170
171 Assert(cancel_key_len >= 0 && cancel_key_len <= MAX_CANCEL_KEY_LENGTH);
172 if (MyProcNumber < 0)
173 elog(ERROR, "MyProcNumber not set");
175 elog(ERROR, "unexpected MyProcNumber %d in ProcSignalInit (max %d)", MyProcNumber, NumProcSignalSlots);
177
179
180 /* Value used for sanity check below */
181 old_pss_pid = pg_atomic_read_u32(&slot->pss_pid);
182
183 /* Clear out any leftover signal reasons */
184 MemSet(slot->pss_signalFlags, 0, NUM_PROCSIGNALS * sizeof(sig_atomic_t));
185
186 /*
187 * Initialize barrier state. Since we're a brand-new process, there
188 * shouldn't be any leftover backend-private state that needs to be
189 * updated. Therefore, we can broadcast the latest barrier generation and
190 * disregard any previously-set check bits.
191 *
192 * NB: This only works if this initialization happens early enough in the
193 * startup sequence that we haven't yet cached any state that might need
194 * to be invalidated. That's also why we have a memory barrier here, to be
195 * sure that any later reads of memory happen strictly after this.
196 */
198 barrier_generation =
200 pg_atomic_write_u64(&slot->pss_barrierGeneration, barrier_generation);
201
202 if (cancel_key_len > 0)
203 memcpy(slot->pss_cancel_key, cancel_key, cancel_key_len);
204 slot->pss_cancel_key_len = cancel_key_len;
206
208
209 /* Spinlock is released, do the check */
210 if (old_pss_pid != 0)
211 elog(LOG, "process %d taking over ProcSignal slot %d, but it's not empty",
213
214 /* Remember slot location for CheckProcSignal */
215 MyProcSignalSlot = slot;
216
217 /* Set up to release the slot on process exit */
219}
static void pg_atomic_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:276
#define MemSet(start, val, len)
Definition: c.h:991
#define LOG
Definition: elog.h:31
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
int MyProcPid
Definition: globals.c:48
ProcNumber MyProcNumber
Definition: globals.c:91
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:365
uintptr_t Datum
Definition: postgres.h:69
static void CleanupProcSignalState(int status, Datum arg)
Definition: procsignal.c:228
#define NUM_PROCSIGNALS
Definition: procsignal.h:53
#define MAX_CANCEL_KEY_LENGTH
Definition: procsignal.h:68
char pss_cancel_key[MAX_CANCEL_KEY_LENGTH]
Definition: procsignal.c:67
int pss_cancel_key_len
Definition: procsignal.c:66

References Assert(), CleanupProcSignalState(), elog, ERROR, LOG, MAX_CANCEL_KEY_LENGTH, MemSet, MyProcNumber, MyProcPid, MyProcSignalSlot, NUM_PROCSIGNALS, NumProcSignalSlots, on_shmem_exit(), pg_atomic_read_u32(), pg_atomic_read_u64(), pg_atomic_write_u32(), pg_atomic_write_u64(), ProcSignal, ProcSignalHeader::psh_barrierGeneration, ProcSignalHeader::psh_slot, ProcSignalSlot::pss_barrierCheckMask, ProcSignalSlot::pss_barrierGeneration, ProcSignalSlot::pss_cancel_key, ProcSignalSlot::pss_cancel_key_len, ProcSignalSlot::pss_mutex, ProcSignalSlot::pss_pid, ProcSignalSlot::pss_signalFlags, SpinLockAcquire, and SpinLockRelease.

Referenced by AuxiliaryProcessMainCommon(), and InitPostgres().

◆ ProcSignalShmemInit()

void ProcSignalShmemInit ( void  )

Definition at line 130 of file procsignal.c.

131{
132 Size size = ProcSignalShmemSize();
133 bool found;
134
136 ShmemInitStruct("ProcSignal", size, &found);
137
138 /* If we're first, initialize. */
139 if (!found)
140 {
141 int i;
142
144
145 for (i = 0; i < NumProcSignalSlots; ++i)
146 {
148
149 SpinLockInit(&slot->pss_mutex);
150 pg_atomic_init_u32(&slot->pss_pid, 0);
151 slot->pss_cancel_key_len = 0;
152 MemSet(slot->pss_signalFlags, 0, sizeof(slot->pss_signalFlags));
156 }
157 }
158}
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:221
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:453
#define PG_UINT64_MAX
Definition: c.h:564
size_t Size
Definition: c.h:576
void ConditionVariableInit(ConditionVariable *cv)
Size ProcSignalShmemSize(void)
Definition: procsignal.c:116
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
#define SpinLockInit(lock)
Definition: spin.h:57

References ConditionVariableInit(), i, MemSet, NumProcSignalSlots, pg_atomic_init_u32(), pg_atomic_init_u64(), PG_UINT64_MAX, ProcSignal, ProcSignalShmemSize(), ProcSignalHeader::psh_barrierGeneration, ProcSignalHeader::psh_slot, ProcSignalSlot::pss_barrierCheckMask, ProcSignalSlot::pss_barrierCV, ProcSignalSlot::pss_barrierGeneration, ProcSignalSlot::pss_cancel_key_len, ProcSignalSlot::pss_mutex, ProcSignalSlot::pss_pid, ProcSignalSlot::pss_signalFlags, ShmemInitStruct(), and SpinLockInit.

Referenced by CreateOrAttachShmemStructs().

◆ ProcSignalShmemSize()

Size ProcSignalShmemSize ( void  )

Definition at line 116 of file procsignal.c.

117{
118 Size size;
119
121 size = add_size(size, offsetof(ProcSignalHeader, psh_slot));
122 return size;
123}
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510

References add_size(), mul_size(), and NumProcSignalSlots.

Referenced by CalculateShmemSize(), and ProcSignalShmemInit().

◆ SendCancelRequest()

void SendCancelRequest ( int  backendPID,
char *  cancel_key,
int  cancel_key_len 
)

Definition at line 731 of file procsignal.c.

732{
733 Assert(backendPID != 0);
734
735 /*
736 * See if we have a matching backend. Reading the pss_pid and
737 * pss_cancel_key fields is racy, a backend might die and remove itself
738 * from the array at any time. The probability of the cancellation key
739 * matching wrong process is miniscule, however, so we can live with that.
740 * PIDs are reused too, so sending the signal based on PID is inherently
741 * racy anyway, although OS's avoid reusing PIDs too soon.
742 */
743 for (int i = 0; i < NumProcSignalSlots; i++)
744 {
746 bool match;
747
748 if (pg_atomic_read_u32(&slot->pss_pid) != backendPID)
749 continue;
750
751 /* Acquire the spinlock and re-check */
753 if (pg_atomic_read_u32(&slot->pss_pid) != backendPID)
754 {
756 continue;
757 }
758 else
759 {
760 match = slot->pss_cancel_key_len == cancel_key_len &&
761 timingsafe_bcmp(slot->pss_cancel_key, cancel_key, cancel_key_len) == 0;
762
764
765 if (match)
766 {
767 /* Found a match; signal that backend to cancel current op */
769 (errmsg_internal("processing cancel request: sending SIGINT to process %d",
770 backendPID)));
771
772 /*
773 * If we have setsid(), signal the backend's whole process
774 * group
775 */
776#ifdef HAVE_SETSID
777 kill(-backendPID, SIGINT);
778#else
779 kill(backendPID, SIGINT);
780#endif
781 }
782 else
783 {
784 /* Right PID, wrong key: no way, Jose */
785 ereport(LOG,
786 (errmsg("wrong key in cancel request for process %d",
787 backendPID)));
788 }
789 return;
790 }
791 }
792
793 /* No matching backend */
794 ereport(LOG,
795 (errmsg("PID %d in cancel request did not match any process",
796 backendPID)));
797}
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1158
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define DEBUG2
Definition: elog.h:29
#define ereport(elevel,...)
Definition: elog.h:149
int timingsafe_bcmp(const void *b1, const void *b2, size_t len)

References Assert(), DEBUG2, ereport, errmsg(), errmsg_internal(), i, kill, LOG, NumProcSignalSlots, pg_atomic_read_u32(), ProcSignal, ProcSignalHeader::psh_slot, ProcSignalSlot::pss_cancel_key, ProcSignalSlot::pss_cancel_key_len, ProcSignalSlot::pss_mutex, ProcSignalSlot::pss_pid, SpinLockAcquire, SpinLockRelease, and timingsafe_bcmp().

Referenced by ProcessCancelRequestPacket().

◆ SendProcSignal()

int SendProcSignal ( pid_t  pid,
ProcSignalReason  reason,
ProcNumber  procNumber 
)

Definition at line 283 of file procsignal.c.

284{
285 volatile ProcSignalSlot *slot;
286
287 if (procNumber != INVALID_PROC_NUMBER)
288 {
289 Assert(procNumber < NumProcSignalSlots);
290 slot = &ProcSignal->psh_slot[procNumber];
291
293 if (pg_atomic_read_u32(&slot->pss_pid) == pid)
294 {
295 /* Atomically set the proper flag */
296 slot->pss_signalFlags[reason] = true;
298 /* Send signal */
299 return kill(pid, SIGUSR1);
300 }
302 }
303 else
304 {
305 /*
306 * procNumber not provided, so search the array using pid. We search
307 * the array back to front so as to reduce search overhead. Passing
308 * INVALID_PROC_NUMBER means that the target is most likely an
309 * auxiliary process, which will have a slot near the end of the
310 * array.
311 */
312 int i;
313
314 for (i = NumProcSignalSlots - 1; i >= 0; i--)
315 {
316 slot = &ProcSignal->psh_slot[i];
317
318 if (pg_atomic_read_u32(&slot->pss_pid) == pid)
319 {
321 if (pg_atomic_read_u32(&slot->pss_pid) == pid)
322 {
323 /* Atomically set the proper flag */
324 slot->pss_signalFlags[reason] = true;
326 /* Send signal */
327 return kill(pid, SIGUSR1);
328 }
330 }
331 }
332 }
333
334 errno = ESRCH;
335 return -1;
336}
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26

References Assert(), i, INVALID_PROC_NUMBER, kill, NumProcSignalSlots, pg_atomic_read_u32(), ProcSignal, ProcSignalHeader::psh_slot, ProcSignalSlot::pss_mutex, ProcSignalSlot::pss_pid, ProcSignalSlot::pss_signalFlags, SIGUSR1, SpinLockAcquire, and SpinLockRelease.

Referenced by CancelDBBackends(), InvalidatePossiblyObsoleteSlot(), mq_putmessage(), pa_shutdown(), ParallelWorkerShutdown(), pg_get_process_memory_contexts(), pg_log_backend_memory_contexts(), SICleanupQueue(), SignalBackends(), SignalVirtualTransaction(), and WalSndInitStopping().

◆ WaitForProcSignalBarrier()

void WaitForProcSignalBarrier ( uint64  generation)

Definition at line 423 of file procsignal.c.

424{
426
427 elog(DEBUG1,
428 "waiting for all backends to process ProcSignalBarrier generation "
430 generation);
431
432 for (int i = NumProcSignalSlots - 1; i >= 0; i--)
433 {
435 uint64 oldval;
436
437 /*
438 * It's important that we check only pss_barrierGeneration here and
439 * not pss_barrierCheckMask. Bits in pss_barrierCheckMask get cleared
440 * before the barrier is actually absorbed, but pss_barrierGeneration
441 * is updated only afterward.
442 */
444 while (oldval < generation)
445 {
447 5000,
448 WAIT_EVENT_PROC_SIGNAL_BARRIER))
449 ereport(LOG,
450 (errmsg("still waiting for backend with PID %d to accept ProcSignalBarrier",
451 (int) pg_atomic_read_u32(&slot->pss_pid))));
453 }
455 }
456
457 elog(DEBUG1,
458 "finished waiting for all backends to process ProcSignalBarrier generation "
460 generation);
461
462 /*
463 * The caller is probably calling this function because it wants to read
464 * the shared state or perform further writes to shared state once all
465 * backends are known to have absorbed the barrier. However, the read of
466 * pss_barrierGeneration was performed unlocked; insert a memory barrier
467 * to separate it from whatever follows.
468 */
470}
#define pg_memory_barrier()
Definition: atomics.h:143
#define UINT64_FORMAT
Definition: c.h:521
bool ConditionVariableCancelSleep(void)
bool ConditionVariableTimedSleep(ConditionVariable *cv, long timeout, uint32 wait_event_info)
#define DEBUG1
Definition: elog.h:30

References Assert(), ConditionVariableCancelSleep(), ConditionVariableTimedSleep(), DEBUG1, elog, ereport, errmsg(), i, LOG, NumProcSignalSlots, pg_atomic_read_u32(), pg_atomic_read_u64(), pg_memory_barrier, ProcSignal, ProcSignalHeader::psh_barrierGeneration, ProcSignalHeader::psh_slot, ProcSignalSlot::pss_barrierCV, ProcSignalSlot::pss_barrierGeneration, ProcSignalSlot::pss_pid, and UINT64_FORMAT.

Referenced by dbase_redo(), dropdb(), DropTableSpace(), movedb(), and tblspc_redo().