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)
 

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 (bool cancel_key_valid, int32 cancel_key)
 
int SendProcSignal (pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
 
void SendCancelRequest (int backendPID, int32 cancelAuthCode)
 
uint64 EmitProcSignalBarrier (ProcSignalBarrierType type)
 
void WaitForProcSignalBarrier (uint64 generation)
 
void ProcessProcSignalBarrier (void)
 
void procsignal_sigusr1_handler (SIGNAL_ARGS)
 

Macro Definition Documentation

◆ NUM_PROCSIGNALS

#define NUM_PROCSIGNALS   (PROCSIG_RECOVERY_CONFLICT_LAST + 1)

Definition at line 52 of file procsignal.h.

Typedef Documentation

◆ ProcSignalHeader

Definition at line 74 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 353 of file procsignal.c.

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

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 496 of file procsignal.c.

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

◆ procsignal_sigusr1_handler()

void procsignal_sigusr1_handler ( SIGNAL_ARGS  )

Definition at line 671 of file procsignal.c.

672 {
675 
678 
681 
684 
687 
690 
693 
696 
699 
702 
705 
708 
711 
714 
715  SetLatch(MyLatch);
716 }
void HandleParallelApplyMessageInterrupt(void)
void HandleNotifyInterrupt(void)
Definition: async.c:1804
void HandleParallelMessageInterrupt(void)
Definition: parallel.c:1016
struct Latch * MyLatch
Definition: globals.c:62
void SetLatch(Latch *latch)
Definition: latch.c:632
void HandleLogMemoryContextInterrupt(void)
Definition: mcxt.c:1272
void HandleRecoveryConflictInterrupt(ProcSignalReason reason)
Definition: postgres.c:3068
static bool CheckProcSignal(ProcSignalReason reason)
Definition: procsignal.c:646
static void HandleProcSignalBarrierInterrupt(void)
Definition: procsignal.c:480
void HandleCatchupInterrupt(void)
Definition: sinval.c:154
void HandleWalSndInitStopping(void)
Definition: walsender.c:3532

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(), AutoVacWorkerMain(), BackgroundWorkerMain(), BackgroundWriterMain(), CheckpointerMain(), PgArchiverMain(), PostgresMain(), ReplSlotSyncWorkerMain(), StartupProcessMain(), WalReceiverMain(), WalSndSignals(), WalSummarizerMain(), and WalWriterMain().

◆ ProcSignalInit()

void ProcSignalInit ( bool  cancel_key_valid,
int32  cancel_key 
)

Definition at line 166 of file procsignal.c.

167 {
168  ProcSignalSlot *slot;
169  uint64 barrier_generation;
170 
171  if (MyProcNumber < 0)
172  elog(ERROR, "MyProcNumber not set");
174  elog(ERROR, "unexpected MyProcNumber %d in ProcSignalInit (max %d)", MyProcNumber, NumProcSignalSlots);
175  slot = &ProcSignal->psh_slot[MyProcNumber];
176 
177  /* sanity check */
178  SpinLockAcquire(&slot->pss_mutex);
179  if (pg_atomic_read_u32(&slot->pss_pid) != 0)
180  {
181  SpinLockRelease(&slot->pss_mutex);
182  elog(LOG, "process %d taking over ProcSignal slot %d, but it's not empty",
184  }
185 
186  /* Clear out any leftover signal reasons */
187  MemSet(slot->pss_signalFlags, 0, NUM_PROCSIGNALS * sizeof(sig_atomic_t));
188 
189  /*
190  * Initialize barrier state. Since we're a brand-new process, there
191  * shouldn't be any leftover backend-private state that needs to be
192  * updated. Therefore, we can broadcast the latest barrier generation and
193  * disregard any previously-set check bits.
194  *
195  * NB: This only works if this initialization happens early enough in the
196  * startup sequence that we haven't yet cached any state that might need
197  * to be invalidated. That's also why we have a memory barrier here, to be
198  * sure that any later reads of memory happen strictly after this.
199  */
201  barrier_generation =
203  pg_atomic_write_u64(&slot->pss_barrierGeneration, barrier_generation);
204 
205  slot->pss_cancel_key_valid = cancel_key_valid;
206  slot->pss_cancel_key = cancel_key;
208 
209  SpinLockRelease(&slot->pss_mutex);
210 
211  /* Remember slot location for CheckProcSignal */
212  MyProcSignalSlot = slot;
213 
214  /* Set up to release the slot on process exit */
216 }
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:1011
#define LOG
Definition: elog.h:31
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
int MyProcPid
Definition: globals.c:46
ProcNumber MyProcNumber
Definition: globals.c:89
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:365
uintptr_t Datum
Definition: postgres.h:64
static void CleanupProcSignalState(int status, Datum arg)
Definition: procsignal.c:225
#define NUM_PROCSIGNALS
Definition: procsignal.h:52
int32 pss_cancel_key
Definition: procsignal.c:67
bool pss_cancel_key_valid
Definition: procsignal.c:66

References CleanupProcSignalState(), elog, ERROR, LOG, 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_valid, 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 {
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  {
147  ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
148 
149  SpinLockInit(&slot->pss_mutex);
150  pg_atomic_init_u32(&slot->pss_pid, 0);
151  slot->pss_cancel_key_valid = false;
152  slot->pss_cancel_key = 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:584
size_t Size
Definition: c.h:596
void ConditionVariableInit(ConditionVariable *cv)
Size ProcSignalShmemSize(void)
Definition: procsignal.c:116
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
static pg_noinline void Size size
Definition: slab.c:607
#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, ProcSignalSlot::pss_cancel_key_valid, ProcSignalSlot::pss_mutex, ProcSignalSlot::pss_pid, ProcSignalSlot::pss_signalFlags, ShmemInitStruct(), size, 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(), NumProcSignalSlots, and size.

Referenced by CalculateShmemSize(), and ProcSignalShmemInit().

◆ SendCancelRequest()

void SendCancelRequest ( int  backendPID,
int32  cancelAuthCode 
)

Definition at line 726 of file procsignal.c.

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

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_valid, ProcSignalSlot::pss_mutex, ProcSignalSlot::pss_pid, SpinLockAcquire, and SpinLockRelease.

Referenced by ProcessStartupPacket().

◆ SendProcSignal()

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

Definition at line 281 of file procsignal.c.

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

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