PostgreSQL Source Code git master
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_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 (const uint8 *cancel_key, int cancel_key_len)
 
int SendProcSignal (pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
 
void SendCancelRequest (int backendPID, const uint8 *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 67 of file procsignal.h.

◆ NUM_PROCSIGNALS

#define NUM_PROCSIGNALS   (PROCSIG_RECOVERY_CONFLICT_LAST + 1)

Definition at line 52 of file procsignal.h.

Typedef Documentation

◆ ProcSignalHeader

Definition at line 87 of file procsignal.h.

Enumeration Type Documentation

◆ ProcSignalBarrierType

Enumerator
PROCSIGNAL_BARRIER_SMGRRELEASE 

Definition at line 54 of file procsignal.h.

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

◆ ProcSignalReason

Enumerator
PROCSIG_CATCHUP_INTERRUPT 
PROCSIG_NOTIFY_INTERRUPT 
PROCSIG_PARALLEL_MESSAGE 
PROCSIG_WALSND_INIT_STOPPING 
PROCSIG_BARRIER 
PROCSIG_LOG_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_PARALLEL_APPLY_MESSAGE, /* Message from parallel apply workers */
39
40 /* Recovery conflict reasons */
ProcSignalReason
Definition: procsignal.h:31
@ PROCSIG_PARALLEL_MESSAGE
Definition: procsignal.h:34
@ PROCSIG_RECOVERY_CONFLICT_BUFFERPIN
Definition: procsignal.h:47
@ PROCSIG_CATCHUP_INTERRUPT
Definition: procsignal.h:32
@ PROCSIG_RECOVERY_CONFLICT_LOCK
Definition: procsignal.h:44
@ PROCSIG_LOG_MEMORY_CONTEXT
Definition: procsignal.h:37
@ PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT
Definition: procsignal.h:46
@ PROCSIG_BARRIER
Definition: procsignal.h:36
@ PROCSIG_RECOVERY_CONFLICT_DATABASE
Definition: procsignal.h:42
@ PROCSIG_WALSND_INIT_STOPPING
Definition: procsignal.h:35
@ PROCSIG_PARALLEL_APPLY_MESSAGE
Definition: procsignal.h:38
@ PROCSIG_RECOVERY_CONFLICT_SNAPSHOT
Definition: procsignal.h:45
@ PROCSIG_RECOVERY_CONFLICT_LAST
Definition: procsignal.h:49
@ PROCSIG_RECOVERY_CONFLICT_FIRST
Definition: procsignal.h:41
@ PROCSIG_NOTIFY_INTERRUPT
Definition: procsignal.h:33
@ PROCSIG_RECOVERY_CONFLICT_TABLESPACE
Definition: procsignal.h:43
@ PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK
Definition: procsignal.h:48

Function Documentation

◆ EmitProcSignalBarrier()

uint64 EmitProcSignalBarrier ( ProcSignalBarrierType  type)

Definition at line 356 of file procsignal.c.

357{
358 uint32 flagbit = 1 << (uint32) type;
359 uint64 generation;
360
361 /*
362 * Set all the flags.
363 *
364 * Note that pg_atomic_fetch_or_u32 has full barrier semantics, so this is
365 * totally ordered with respect to anything the caller did before, and
366 * anything that we do afterwards. (This is also true of the later call to
367 * pg_atomic_add_fetch_u64.)
368 */
369 for (int i = 0; i < NumProcSignalSlots; i++)
370 {
371 volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
372
374 }
375
376 /*
377 * Increment the generation counter.
378 */
379 generation =
381
382 /*
383 * Signal all the processes, so that they update their advertised barrier
384 * generation.
385 *
386 * Concurrency is not a problem here. Backends that have exited don't
387 * matter, and new backends that have joined since we entered this
388 * function must already have current state, since the caller is
389 * responsible for making sure that the relevant state is entirely visible
390 * before calling this function in the first place. We still have to wake
391 * them up - because we can't distinguish between such backends and older
392 * backends that need to update state - but they won't actually need to
393 * change any state.
394 */
395 for (int i = NumProcSignalSlots - 1; i >= 0; i--)
396 {
397 volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
398 pid_t pid = pg_atomic_read_u32(&slot->pss_pid);
399
400 if (pid != 0)
401 {
403 pid = pg_atomic_read_u32(&slot->pss_pid);
404 if (pid != 0)
405 {
406 /* see SendProcSignal for details */
407 slot->pss_signalFlags[PROCSIG_BARRIER] = true;
409 kill(pid, SIGUSR1);
410 }
411 else
413 }
414 }
415
416 return generation;
417}
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:95
NON_EXEC_STATIC ProcSignalHeader * ProcSignal
Definition: procsignal.c:105
#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 499 of file procsignal.c.

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

675{
678
681
684
687
690
693
696
699
702
705
708
711
714
717
719}
void HandleParallelApplyMessageInterrupt(void)
void HandleNotifyInterrupt(void)
Definition: async.c:1804
void HandleParallelMessageInterrupt(void)
Definition: parallel.c:1037
struct Latch * MyLatch
Definition: globals.c:63
void SetLatch(Latch *latch)
Definition: latch.c:288
void HandleLogMemoryContextInterrupt(void)
Definition: mcxt.c:1276
void HandleRecoveryConflictInterrupt(ProcSignalReason reason)
Definition: postgres.c:3089
static bool CheckProcSignal(ProcSignalReason reason)
Definition: procsignal.c:649
static void HandleProcSignalBarrierInterrupt(void)
Definition: procsignal.c:483
void HandleCatchupInterrupt(void)
Definition: sinval.c:154
void HandleWalSndInitStopping(void)
Definition: walsender.c:3589

References CheckProcSignal(), HandleCatchupInterrupt(), HandleLogMemoryContextInterrupt(), HandleNotifyInterrupt(), HandleParallelApplyMessageInterrupt(), HandleParallelMessageInterrupt(), HandleProcSignalBarrierInterrupt(), HandleRecoveryConflictInterrupt(), HandleWalSndInitStopping(), MyLatch, PROCSIG_BARRIER, PROCSIG_CATCHUP_INTERRUPT, 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 ( const uint8 cancel_key,
int  cancel_key_len 
)

Definition at line 166 of file procsignal.c.

167{
168 ProcSignalSlot *slot;
169 uint64 barrier_generation;
170 uint32 old_pss_pid;
171
172 Assert(cancel_key_len >= 0 && cancel_key_len <= MAX_CANCEL_KEY_LENGTH);
173 if (MyProcNumber < 0)
174 elog(ERROR, "MyProcNumber not set");
176 elog(ERROR, "unexpected MyProcNumber %d in ProcSignalInit (max %d)", MyProcNumber, NumProcSignalSlots);
178
180
181 /* Value used for sanity check below */
182 old_pss_pid = pg_atomic_read_u32(&slot->pss_pid);
183
184 /* Clear out any leftover signal reasons */
185 MemSet(slot->pss_signalFlags, 0, NUM_PROCSIGNALS * sizeof(sig_atomic_t));
186
187 /*
188 * Initialize barrier state. Since we're a brand-new process, there
189 * shouldn't be any leftover backend-private state that needs to be
190 * updated. Therefore, we can broadcast the latest barrier generation and
191 * disregard any previously-set check bits.
192 *
193 * NB: This only works if this initialization happens early enough in the
194 * startup sequence that we haven't yet cached any state that might need
195 * to be invalidated. That's also why we have a memory barrier here, to be
196 * sure that any later reads of memory happen strictly after this.
197 */
199 barrier_generation =
201 pg_atomic_write_u64(&slot->pss_barrierGeneration, barrier_generation);
202
203 if (cancel_key_len > 0)
204 memcpy(slot->pss_cancel_key, cancel_key, cancel_key_len);
205 slot->pss_cancel_key_len = cancel_key_len;
207
209
210 /* Spinlock is released, do the check */
211 if (old_pss_pid != 0)
212 elog(LOG, "process %d taking over ProcSignal slot %d, but it's not empty",
214
215 /* Remember slot location for CheckProcSignal */
216 MyProcSignalSlot = slot;
217
218 /* Set up to release the slot on process exit */
220}
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:225
int MyProcPid
Definition: globals.c:47
ProcNumber MyProcNumber
Definition: globals.c:90
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:229
#define NUM_PROCSIGNALS
Definition: procsignal.h:52
#define MAX_CANCEL_KEY_LENGTH
Definition: procsignal.h:67
uint8 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 131 of file procsignal.c.

132{
133 Size size = ProcSignalShmemSize();
134 bool found;
135
137 ShmemInitStruct("ProcSignal", size, &found);
138
139 /* If we're first, initialize. */
140 if (!found)
141 {
142 int i;
143
145
146 for (i = 0; i < NumProcSignalSlots; ++i)
147 {
149
150 SpinLockInit(&slot->pss_mutex);
151 pg_atomic_init_u32(&slot->pss_pid, 0);
152 slot->pss_cancel_key_len = 0;
153 MemSet(slot->pss_signalFlags, 0, sizeof(slot->pss_signalFlags));
157 }
158 }
159}
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:117
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 117 of file procsignal.c.

118{
119 Size size;
120
122 size = add_size(size, offsetof(ProcSignalHeader, psh_slot));
123 return size;
124}
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,
const uint8 cancel_key,
int  cancel_key_len 
)

Definition at line 729 of file procsignal.c.

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

285{
286 volatile ProcSignalSlot *slot;
287
288 if (procNumber != INVALID_PROC_NUMBER)
289 {
290 Assert(procNumber < NumProcSignalSlots);
291 slot = &ProcSignal->psh_slot[procNumber];
292
294 if (pg_atomic_read_u32(&slot->pss_pid) == pid)
295 {
296 /* Atomically set the proper flag */
297 slot->pss_signalFlags[reason] = true;
299 /* Send signal */
300 return kill(pid, SIGUSR1);
301 }
303 }
304 else
305 {
306 /*
307 * procNumber not provided, so search the array using pid. We search
308 * the array back to front so as to reduce search overhead. Passing
309 * INVALID_PROC_NUMBER means that the target is most likely an
310 * auxiliary process, which will have a slot near the end of the
311 * array.
312 */
313 int i;
314
315 for (i = NumProcSignalSlots - 1; i >= 0; i--)
316 {
317 slot = &ProcSignal->psh_slot[i];
318
319 if (pg_atomic_read_u32(&slot->pss_pid) == pid)
320 {
322 if (pg_atomic_read_u32(&slot->pss_pid) == pid)
323 {
324 /* Atomically set the proper flag */
325 slot->pss_signalFlags[reason] = true;
327 /* Send signal */
328 return kill(pid, SIGUSR1);
329 }
331 }
332 }
333 }
334
335 errno = ESRCH;
336 return -1;
337}
#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_log_backend_memory_contexts(), SICleanupQueue(), SignalBackends(), SignalVirtualTransaction(), and WalSndInitStopping().

◆ WaitForProcSignalBarrier()

void WaitForProcSignalBarrier ( uint64  generation)

Definition at line 424 of file procsignal.c.

425{
427
428 elog(DEBUG1,
429 "waiting for all backends to process ProcSignalBarrier generation "
431 generation);
432
433 for (int i = NumProcSignalSlots - 1; i >= 0; i--)
434 {
436 uint64 oldval;
437
438 /*
439 * It's important that we check only pss_barrierGeneration here and
440 * not pss_barrierCheckMask. Bits in pss_barrierCheckMask get cleared
441 * before the barrier is actually absorbed, but pss_barrierGeneration
442 * is updated only afterward.
443 */
445 while (oldval < generation)
446 {
448 5000,
449 WAIT_EVENT_PROC_SIGNAL_BARRIER))
450 ereport(LOG,
451 (errmsg("still waiting for backend with PID %d to accept ProcSignalBarrier",
452 (int) pg_atomic_read_u32(&slot->pss_pid))));
454 }
456 }
457
458 elog(DEBUG1,
459 "finished waiting for all backends to process ProcSignalBarrier generation "
461 generation);
462
463 /*
464 * The caller is probably calling this function because it wants to read
465 * the shared state or perform further writes to shared state once all
466 * backends are known to have absorbed the barrier. However, the read of
467 * pss_barrierGeneration was performed unlocked; insert a memory barrier
468 * to separate it from whatever follows.
469 */
471}
#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().