PostgreSQL Source Code git master
Loading...
Searching...
No Matches
procsignal.c File Reference
#include "postgres.h"
#include <signal.h>
#include <unistd.h>
#include "access/parallel.h"
#include "commands/async.h"
#include "commands/repack.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "port/pg_bitutils.h"
#include "postmaster/datachecksum_state.h"
#include "replication/logicalctl.h"
#include "replication/logicalworker.h"
#include "replication/slotsync.h"
#include "replication/walsender.h"
#include "storage/condition_variable.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/proc.h"
#include "storage/shmem.h"
#include "storage/sinval.h"
#include "storage/smgr.h"
#include "storage/subsystems.h"
#include "tcop/tcopprot.h"
#include "utils/memutils.h"
#include "utils/wait_event.h"
Include dependency graph for procsignal.c:

Go to the source code of this file.

Data Structures

struct  ProcSignalSlot
 
struct  ProcSignalHeader
 

Macros

#define NumProcSignalSlots   (MaxBackends + NUM_AUXILIARY_PROCS)
 
#define BARRIER_SHOULD_CHECK(flags, type)    (((flags) & (((uint32) 1) << (uint32) (type))) != 0)
 
#define BARRIER_CLEAR_BIT(flags, type)    ((flags) &= ~(((uint32) 1) << (uint32) (type)))
 

Functions

static void ProcSignalShmemRequest (void *arg)
 
static void ProcSignalShmemInit (void *arg)
 
static bool CheckProcSignal (ProcSignalReason reason)
 
static void CleanupProcSignalState (int status, Datum arg)
 
static void ResetProcSignalBarrierBits (uint32 flags)
 
void ProcSignalInit (const uint8 *cancel_key, int cancel_key_len)
 
int SendProcSignal (pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
 
uint64 EmitProcSignalBarrier (ProcSignalBarrierType type)
 
void WaitForProcSignalBarrier (uint64 generation)
 
static void HandleProcSignalBarrierInterrupt (void)
 
void ProcessProcSignalBarrier (void)
 
void procsignal_sigusr1_handler (SIGNAL_ARGS)
 
void SendCancelRequest (int backendPID, const uint8 *cancel_key, int cancel_key_len)
 

Variables

const ShmemCallbacks ProcSignalShmemCallbacks
 
NON_EXEC_STATIC ProcSignalHeaderProcSignal = NULL
 
static ProcSignalSlotMyProcSignalSlot = NULL
 

Macro Definition Documentation

◆ BARRIER_CLEAR_BIT

#define BARRIER_CLEAR_BIT (   flags,
  type 
)     ((flags) &= ~(((uint32) 1) << (uint32) (type)))

Definition at line 109 of file procsignal.c.

114 {
115 .request_fn = ProcSignalShmemRequest,
116 .init_fn = ProcSignalShmemInit,
117};
118
120
122
123static bool CheckProcSignal(ProcSignalReason reason);
124static void CleanupProcSignalState(int status, Datum arg);
125static void ResetProcSignalBarrierBits(uint32 flags);
126
127/*
128 * ProcSignalShmemRequest
129 * Register ProcSignal's shared memory needs at postmaster startup
130 */
131static void
133{
134 Size size;
135
137 size = add_size(size, offsetof(ProcSignalHeader, psh_slot));
138
139 ShmemRequestStruct(.name = "ProcSignal",
140 .size = size,
141 .ptr = (void **) &ProcSignal,
142 );
143}
144
145static void
147{
149
150 for (int i = 0; i < NumProcSignalSlots; ++i)
151 {
153
154 SpinLockInit(&slot->pss_mutex);
155 pg_atomic_init_u32(&slot->pss_pid, 0);
156 slot->pss_cancel_key_len = 0;
157 MemSet(slot->pss_signalFlags, 0, sizeof(slot->pss_signalFlags));
161 }
162}
163
164/*
165 * ProcSignalInit
166 * Register the current process in the ProcSignal array
167 */
168void
170{
171 ProcSignalSlot *slot;
174
176 if (MyProcNumber < 0)
177 elog(ERROR, "MyProcNumber not set");
179 elog(ERROR, "unexpected MyProcNumber %d in ProcSignalInit (max %d)", MyProcNumber, NumProcSignalSlots);
181
183
184 /* Value used for sanity check below */
186
187 /* Clear out any leftover signal reasons */
189
190 /*
191 * Publish the PID before reading the global barrier generation to ensure
192 * that EmitProcSignalBarrier() doesn't skip us while we are grabbing an
193 * older generation. We need a memory barrier here to make sure that the
194 * update of pss_pid is ordered before the subsequent load of
195 * psh_barrierGeneration.
196 */
198
199 /*
200 * Initialize barrier state. Since we're a brand-new process, there
201 * shouldn't be any leftover backend-private state that needs to be
202 * updated. Therefore, we can broadcast the latest barrier generation and
203 * disregard any previously-set check bits.
204 *
205 * NB: This only works if this initialization happens early enough in the
206 * startup sequence that we haven't yet cached any state that might need
207 * to be invalidated. That's also why we have a memory barrier here, to be
208 * sure that any later reads of memory happen strictly after this.
209 */
214
215 if (cancel_key_len > 0)
218
220
221 /* Spinlock is released, do the check */
222 if (old_pss_pid != 0)
223 elog(LOG, "process %d taking over ProcSignal slot %d, but it's not empty",
225
226 /* Remember slot location for CheckProcSignal */
227 MyProcSignalSlot = slot;
228
229 /* Set up to release the slot on process exit */
231}
232
233/*
234 * CleanupProcSignalState
235 * Remove current process from ProcSignal mechanism
236 *
237 * This function is called via on_shmem_exit() during backend shutdown.
238 */
239static void
241{
244
245 /*
246 * Clear MyProcSignalSlot, so that a SIGUSR1 received after this point
247 * won't try to access it after it's no longer ours (and perhaps even
248 * after we've unmapped the shared memory segment).
249 */
252
253 /* sanity check */
256 if (old_pid != MyProcPid)
257 {
258 /*
259 * don't ERROR here. We're exiting anyway, and don't want to get into
260 * infinite loop trying to exit
261 */
263 elog(LOG, "process %d releasing ProcSignal slot %d, but it contains %d",
264 MyProcPid, (int) (slot - ProcSignal->psh_slot), (int) old_pid);
265 return; /* XXX better to zero the slot anyway? */
266 }
267
268 /* Mark the slot as unused */
269 pg_atomic_write_u32(&slot->pss_pid, 0);
270 slot->pss_cancel_key_len = 0;
271
272 /*
273 * Make this slot look like it's absorbed all possible barriers, so that
274 * no barrier waits block on it.
275 */
277
279
281}
282
283/*
284 * SendProcSignal
285 * Send a signal to a Postgres process
286 *
287 * Providing procNumber is optional, but it will speed up the operation.
288 *
289 * On success (a signal was sent), zero is returned.
290 * On error, -1 is returned, and errno is set (typically to ESRCH or EPERM).
291 *
292 * Not to be confused with ProcSendSignal
293 */
294int
295SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
296{
297 volatile ProcSignalSlot *slot;
298
299 if (procNumber != INVALID_PROC_NUMBER)
300 {
301 Assert(procNumber < NumProcSignalSlots);
302 slot = &ProcSignal->psh_slot[procNumber];
303
305 if (pg_atomic_read_u32(&slot->pss_pid) == pid)
306 {
307 /* Atomically set the proper flag */
308 slot->pss_signalFlags[reason] = true;
310 /* Send signal */
311 return kill(pid, SIGUSR1);
312 }
314 }
315 else
316 {
317 /*
318 * procNumber not provided, so search the array using pid. We search
319 * the array back to front so as to reduce search overhead. Passing
320 * INVALID_PROC_NUMBER means that the target is most likely an
321 * auxiliary process, which will have a slot near the end of the
322 * array.
323 */
324 int i;
325
326 for (i = NumProcSignalSlots - 1; i >= 0; i--)
327 {
328 slot = &ProcSignal->psh_slot[i];
329
330 if (pg_atomic_read_u32(&slot->pss_pid) == pid)
331 {
333 if (pg_atomic_read_u32(&slot->pss_pid) == pid)
334 {
335 /* Atomically set the proper flag */
336 slot->pss_signalFlags[reason] = true;
338 /* Send signal */
339 return kill(pid, SIGUSR1);
340 }
342 }
343 }
344 }
345
346 errno = ESRCH;
347 return -1;
348}
349
350/*
351 * EmitProcSignalBarrier
352 * Send a signal to every Postgres process
353 *
354 * The return value of this function is the barrier "generation" created
355 * by this operation. This value can be passed to WaitForProcSignalBarrier
356 * to wait until it is known that every participant in the ProcSignal
357 * mechanism has absorbed the signal (or started afterwards).
358 *
359 * Note that it would be a bad idea to use this for anything that happens
360 * frequently, as interrupting every backend could cause a noticeable
361 * performance hit.
362 *
363 * Callers are entitled to assume that this function will not throw ERROR
364 * or FATAL.
365 */
366uint64
368{
369 uint32 flagbit = 1 << (uint32) type;
370 uint64 generation;
371
372 /*
373 * Set all the flags.
374 *
375 * Note that pg_atomic_fetch_or_u32 has full barrier semantics, so this is
376 * totally ordered with respect to anything the caller did before, and
377 * anything that we do afterwards. (This is also true of the later call to
378 * pg_atomic_add_fetch_u64.)
379 */
380 for (int i = 0; i < NumProcSignalSlots; i++)
381 {
382 volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
383
385 }
386
387 /*
388 * Increment the generation counter.
389 */
390 generation =
392
393 /*
394 * Signal all the processes, so that they update their advertised barrier
395 * generation.
396 *
397 * Concurrency is not a problem here. Backends that have exited don't
398 * matter, and new backends that have joined since we entered this
399 * function must already have current state, since the caller is
400 * responsible for making sure that the relevant state is entirely visible
401 * before calling this function in the first place. We still have to wake
402 * them up - because we can't distinguish between such backends and older
403 * backends that need to update state - but they won't actually need to
404 * change any state.
405 */
406 for (int i = NumProcSignalSlots - 1; i >= 0; i--)
407 {
408 volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
409 pid_t pid = pg_atomic_read_u32(&slot->pss_pid);
410
411 if (pid != 0)
412 {
414 pid = pg_atomic_read_u32(&slot->pss_pid);
415 if (pid != 0)
416 {
417 /* see SendProcSignal for details */
418 slot->pss_signalFlags[PROCSIG_BARRIER] = true;
420 kill(pid, SIGUSR1);
421 }
422 else
424 }
425 }
426
427 return generation;
428}
429
430/*
431 * WaitForProcSignalBarrier - wait until it is guaranteed that all changes
432 * requested by a specific call to EmitProcSignalBarrier() have taken effect.
433 */
434void
436{
438
439 elog(DEBUG1,
440 "waiting for all backends to process ProcSignalBarrier generation "
442 generation);
443
444 for (int i = NumProcSignalSlots - 1; i >= 0; i--)
445 {
448
449 /*
450 * It's important that we check only pss_barrierGeneration here and
451 * not pss_barrierCheckMask. Bits in pss_barrierCheckMask get cleared
452 * before the barrier is actually absorbed, but pss_barrierGeneration
453 * is updated only afterward.
454 */
456 while (oldval < generation)
457 {
459 5000,
461 ereport(LOG,
462 (errmsg("still waiting for backend with PID %d to accept ProcSignalBarrier",
463 (int) pg_atomic_read_u32(&slot->pss_pid))));
465 }
467 }
468
469 elog(DEBUG1,
470 "finished waiting for all backends to process ProcSignalBarrier generation "
472 generation);
473
474 /*
475 * The caller is probably calling this function because it wants to read
476 * the shared state or perform further writes to shared state once all
477 * backends are known to have absorbed the barrier. However, the read of
478 * pss_barrierGeneration was performed unlocked; insert a memory barrier
479 * to separate it from whatever follows.
480 */
482}
483
484/*
485 * Handle receipt of an interrupt indicating a global barrier event.
486 *
487 * All the actual work is deferred to ProcessProcSignalBarrier(), because we
488 * cannot safely access the barrier generation inside the signal handler as
489 * 64bit atomics might use spinlock based emulation, even for reads. As this
490 * routine only gets called when PROCSIG_BARRIER is sent that won't cause a
491 * lot of unnecessary work.
492 */
493static void
495{
496 InterruptPending = true;
498 /* latch will be set by procsignal_sigusr1_handler */
499}
500
501/*
502 * Perform global barrier related interrupt checking.
503 *
504 * Any backend that participates in ProcSignal signaling must arrange to
505 * call this function periodically. It is called from CHECK_FOR_INTERRUPTS(),
506 * which is enough for normal backends, but not necessarily for all types of
507 * background processes.
508 */
509void
511{
514 volatile uint32 flags;
515
517
518 /* Exit quickly if there's no work to do. */
520 return;
522
523 /*
524 * It's not unlikely to process multiple barriers at once, before the
525 * signals for all the barriers have arrived. To avoid unnecessary work in
526 * response to subsequent signals, exit early if we already have processed
527 * all of them.
528 */
531
533
534 if (local_gen == shared_gen)
535 return;
536
537 /*
538 * Get and clear the flags that are set for this backend. Note that
539 * pg_atomic_exchange_u32 is a full barrier, so we're guaranteed that the
540 * read of the barrier generation above happens before we atomically
541 * extract the flags, and that any subsequent state changes happen
542 * afterward.
543 *
544 * NB: In order to avoid race conditions, we must zero
545 * pss_barrierCheckMask first and only afterwards try to do barrier
546 * processing. If we did it in the other order, someone could send us
547 * another barrier of some type right after we called the
548 * barrier-processing function but before we cleared the bit. We would
549 * have no way of knowing that the bit needs to stay set in that case, so
550 * the need to call the barrier-processing function again would just get
551 * forgotten. So instead, we tentatively clear all the bits and then put
552 * back any for which we don't manage to successfully absorb the barrier.
553 */
555
556 /*
557 * If there are no flags set, then we can skip doing any real work.
558 * Otherwise, establish a PG_TRY block, so that we don't lose track of
559 * which types of barrier processing are needed if an ERROR occurs.
560 */
561 if (flags != 0)
562 {
563 bool success = true;
564
565 PG_TRY();
566 {
567 /*
568 * Process each type of barrier. The barrier-processing functions
569 * should normally return true, but may return false if the
570 * barrier can't be absorbed at the current time. This should be
571 * rare, because it's pretty expensive. Every single
572 * CHECK_FOR_INTERRUPTS() will return here until we manage to
573 * absorb the barrier, and that cost will add up in a hurry.
574 *
575 * NB: It ought to be OK to call the barrier-processing functions
576 * unconditionally, but it's more efficient to call only the ones
577 * that might need us to do something based on the flags.
578 */
579 while (flags != 0)
580 {
582 bool processed = true;
583
585 switch (type)
586 {
588 processed = ProcessBarrierSmgrRelease();
589 break;
592 break;
593
598 processed = AbsorbDataChecksumsBarrier(type);
599 break;
600 }
601
602 /*
603 * To avoid an infinite loop, we must always unset the bit in
604 * flags.
605 */
606 BARRIER_CLEAR_BIT(flags, type);
607
608 /*
609 * If we failed to process the barrier, reset the shared bit
610 * so we try again later, and set a flag so that we don't bump
611 * our generation.
612 */
613 if (!processed)
614 {
616 success = false;
617 }
618 }
619 }
620 PG_CATCH();
621 {
622 /*
623 * If an ERROR occurred, we'll need to try again later to handle
624 * that barrier type and any others that haven't been handled yet
625 * or weren't successfully absorbed.
626 */
628 PG_RE_THROW();
629 }
630 PG_END_TRY();
631
632 /*
633 * If some barrier types were not successfully absorbed, we will have
634 * to try again later.
635 */
636 if (!success)
637 return;
638 }
639
640 /*
641 * State changes related to all types of barriers that might have been
642 * emitted have now been handled, so we can update our notion of the
643 * generation to the one we observed before beginning the updates. If
644 * things have changed further, it'll get fixed up when this function is
645 * next called.
646 */
649}
650
651/*
652 * If it turns out that we couldn't absorb one or more barrier types, either
653 * because the barrier-processing functions returned false or due to an error,
654 * arrange for processing to be retried later.
655 */
656static void
658{
661 InterruptPending = true;
662}
663
664/*
665 * CheckProcSignal - check to see if a particular reason has been
666 * signaled, and clear the signal flag. Should be called after receiving
667 * SIGUSR1.
668 */
669static bool
671{
672 volatile ProcSignalSlot *slot = MyProcSignalSlot;
673
674 if (slot != NULL)
675 {
676 /*
677 * Careful here --- don't clear flag if we haven't seen it set.
678 * pss_signalFlags is of type "volatile sig_atomic_t" to allow us to
679 * read it here safely, without holding the spinlock.
680 */
681 if (slot->pss_signalFlags[reason])
682 {
683 slot->pss_signalFlags[reason] = false;
684 return true;
685 }
686 }
687
688 return false;
689}
690
691/*
692 * procsignal_sigusr1_handler - handle SIGUSR1 signal.
693 */
694void
696{
699
702
705
708
711
714
717
720
723
726
728}
729
730/*
731 * Send a query cancellation signal to backend.
732 *
733 * Note: This is called from a backend process before authentication. We
734 * cannot take LWLocks yet, but that's OK; we rely on atomic reads of the
735 * fields in the ProcSignal slots.
736 */
737void
738SendCancelRequest(int backendPID, const uint8 *cancel_key, int cancel_key_len)
739{
740 if (backendPID == 0)
741 {
742 ereport(LOG, (errmsg("invalid cancel request with PID 0")));
743 return;
744 }
745
746 /*
747 * See if we have a matching backend. Reading the pss_pid and
748 * pss_cancel_key fields is racy, a backend might die and remove itself
749 * from the array at any time. The probability of the cancellation key
750 * matching wrong process is miniscule, however, so we can live with that.
751 * PIDs are reused too, so sending the signal based on PID is inherently
752 * racy anyway, although OS's avoid reusing PIDs too soon.
753 */
754 for (int i = 0; i < NumProcSignalSlots; i++)
755 {
757 bool match;
758
759 if (pg_atomic_read_u32(&slot->pss_pid) != backendPID)
760 continue;
761
762 /* Acquire the spinlock and re-check */
764 if (pg_atomic_read_u32(&slot->pss_pid) != backendPID)
765 {
767 continue;
768 }
769 else
770 {
771 match = slot->pss_cancel_key_len == cancel_key_len &&
773
775
776 if (match)
777 {
778 /* Found a match; signal that backend to cancel current op */
780 (errmsg_internal("processing cancel request: sending SIGINT to process %d",
781 backendPID)));
782
783 /*
784 * If we have setsid(), signal the backend's whole process
785 * group
786 */
787#ifdef HAVE_SETSID
788 kill(-backendPID, SIGINT);
789#else
790 kill(backendPID, SIGINT);
791#endif
792 }
793 else
794 {
795 /* Right PID, wrong key: no way, Jose */
796 ereport(LOG,
797 (errmsg("wrong key in cancel request for process %d",
798 backendPID)));
799 }
800 return;
801 }
802 }
803
804 /* No matching backend */
805 ereport(LOG,
806 (errmsg("PID %d in cancel request did not match any process",
807 backendPID)));
808}
void HandleParallelApplyMessageInterrupt(void)
void HandleNotifyInterrupt(void)
Definition async.c:2552
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition atomics.h:485
static uint32 pg_atomic_fetch_or_u32(volatile pg_atomic_uint32 *ptr, uint32 or_)
Definition atomics.h:410
#define pg_memory_barrier()
Definition atomics.h:141
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition atomics.h:219
static void pg_atomic_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition atomics.h:274
static void pg_atomic_write_membarrier_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition atomics.h:315
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition atomics.h:237
static uint64 pg_atomic_add_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 add_)
Definition atomics.h:569
static uint32 pg_atomic_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 newval)
Definition atomics.h:330
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition atomics.h:453
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition atomics.h:467
void HandleParallelMessageInterrupt(void)
Definition parallel.c:1046
uint8_t uint8
Definition c.h:622
#define SIGNAL_ARGS
Definition c.h:1462
#define Assert(condition)
Definition c.h:943
#define UINT64_FORMAT
Definition c.h:635
uint64_t uint64
Definition c.h:625
uint32_t uint32
Definition c.h:624
#define PG_UINT64_MAX
Definition c.h:677
#define MemSet(start, val, len)
Definition c.h:1107
size_t Size
Definition c.h:689
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
bool ConditionVariableCancelSleep(void)
bool ConditionVariableTimedSleep(ConditionVariable *cv, long timeout, uint32 wait_event_info)
void ConditionVariableBroadcast(ConditionVariable *cv)
void ConditionVariableInit(ConditionVariable *cv)
bool AbsorbDataChecksumsBarrier(ProcSignalBarrierType barrier)
Datum arg
Definition elog.c:1323
#define LOG
Definition elog.h:32
#define PG_RE_THROW()
Definition elog.h:407
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define PG_TRY(...)
Definition elog.h:374
#define DEBUG2
Definition elog.h:30
#define PG_END_TRY(...)
Definition elog.h:399
#define DEBUG1
Definition elog.h:31
#define ERROR
Definition elog.h:40
#define PG_CATCH(...)
Definition elog.h:384
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
volatile sig_atomic_t ProcSignalBarrierPending
Definition globals.c:40
volatile sig_atomic_t InterruptPending
Definition globals.c:32
int MyProcPid
Definition globals.c:49
ProcNumber MyProcNumber
Definition globals.c:92
struct Latch * MyLatch
Definition globals.c:65
static bool success
Definition initdb.c:188
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:372
int i
Definition isn.c:77
void SetLatch(Latch *latch)
Definition latch.c:290
bool ProcessBarrierUpdateXLogLogicalInfo(void)
Definition logicalctl.c:184
Size add_size(Size s1, Size s2)
Definition mcxt.c:1733
void HandleLogMemoryContextInterrupt(void)
Definition mcxt.c:1326
Size mul_size(Size s1, Size s2)
Definition mcxt.c:1752
static char * errmsg
static int pg_rightmost_one_pos32(uint32 word)
int timingsafe_bcmp(const void *b1, const void *b2, size_t n)
void HandleRecoveryConflictInterrupt(void)
Definition postgres.c:3098
uint64_t Datum
Definition postgres.h:70
#define NON_EXEC_STATIC
Definition postgres.h:573
static int fb(int x)
#define INVALID_PROC_NUMBER
Definition procnumber.h:26
int ProcNumber
Definition procnumber.h:24
static void CleanupProcSignalState(int status, Datum arg)
Definition procsignal.c:241
int SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
Definition procsignal.c:296
void ProcSignalInit(const uint8 *cancel_key, int cancel_key_len)
Definition procsignal.c:170
static void ProcSignalShmemInit(void *arg)
Definition procsignal.c:147
#define NumProcSignalSlots
Definition procsignal.c:102
static bool CheckProcSignal(ProcSignalReason reason)
Definition procsignal.c:671
void ProcessProcSignalBarrier(void)
Definition procsignal.c:511
void WaitForProcSignalBarrier(uint64 generation)
Definition procsignal.c:436
NON_EXEC_STATIC ProcSignalHeader * ProcSignal
Definition procsignal.c:120
static void ResetProcSignalBarrierBits(uint32 flags)
Definition procsignal.c:658
void SendCancelRequest(int backendPID, const uint8 *cancel_key, int cancel_key_len)
Definition procsignal.c:739
uint64 EmitProcSignalBarrier(ProcSignalBarrierType type)
Definition procsignal.c:368
static void ProcSignalShmemRequest(void *arg)
Definition procsignal.c:133
static void HandleProcSignalBarrierInterrupt(void)
Definition procsignal.c:495
static ProcSignalSlot * MyProcSignalSlot
Definition procsignal.c:122
#define BARRIER_CLEAR_BIT(flags, type)
Definition procsignal.c:109
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition procsignal.c:696
#define NUM_PROCSIGNALS
Definition procsignal.h:46
ProcSignalReason
Definition procsignal.h:31
@ PROCSIG_RECOVERY_CONFLICT
Definition procsignal.h:41
@ PROCSIG_PARALLEL_MESSAGE
Definition procsignal.h:34
@ PROCSIG_CATCHUP_INTERRUPT
Definition procsignal.h:32
@ PROCSIG_SLOTSYNC_MESSAGE
Definition procsignal.h:39
@ PROCSIG_LOG_MEMORY_CONTEXT
Definition procsignal.h:37
@ PROCSIG_BARRIER
Definition procsignal.h:36
@ PROCSIG_REPACK_MESSAGE
Definition procsignal.h:40
@ PROCSIG_WALSND_INIT_STOPPING
Definition procsignal.h:35
@ PROCSIG_PARALLEL_APPLY_MESSAGE
Definition procsignal.h:38
@ PROCSIG_NOTIFY_INTERRUPT
Definition procsignal.h:33
ProcSignalBarrierType
Definition procsignal.h:49
@ PROCSIGNAL_BARRIER_CHECKSUM_INPROGRESS_OFF
Definition procsignal.h:55
@ PROCSIGNAL_BARRIER_SMGRRELEASE
Definition procsignal.h:50
@ PROCSIGNAL_BARRIER_CHECKSUM_INPROGRESS_ON
Definition procsignal.h:54
@ PROCSIGNAL_BARRIER_UPDATE_XLOG_LOGICAL_INFO
Definition procsignal.h:51
@ PROCSIGNAL_BARRIER_CHECKSUM_ON
Definition procsignal.h:56
@ PROCSIGNAL_BARRIER_CHECKSUM_OFF
Definition procsignal.h:53
#define MAX_CANCEL_KEY_LENGTH
Definition procsignal.h:67
void HandleRepackMessageInterrupt(void)
Definition repack.c:3655
#define ShmemRequestStruct(...)
Definition shmem.h:176
void HandleCatchupInterrupt(void)
Definition sinval.c:154
void HandleSlotSyncMessageInterrupt(void)
Definition slotsync.c:1338
bool ProcessBarrierSmgrRelease(void)
Definition smgr.c:1027
static void SpinLockRelease(volatile slock_t *lock)
Definition spin.h:62
static void SpinLockAcquire(volatile slock_t *lock)
Definition spin.h:56
static void SpinLockInit(volatile slock_t *lock)
Definition spin.h:50
ProcSignalSlot psh_slot[FLEXIBLE_ARRAY_MEMBER]
Definition procsignal.c:93
pg_atomic_uint64 psh_barrierGeneration
Definition procsignal.c:92
uint8 pss_cancel_key[MAX_CANCEL_KEY_LENGTH]
Definition procsignal.c:74
ConditionVariable pss_barrierCV
Definition procsignal.c:81
pg_atomic_uint64 pss_barrierGeneration
Definition procsignal.c:79
volatile sig_atomic_t pss_signalFlags[NUM_PROCSIGNALS]
Definition procsignal.c:75
slock_t pss_mutex
Definition procsignal.c:76
pg_atomic_uint32 pss_pid
Definition procsignal.c:72
int pss_cancel_key_len
Definition procsignal.c:73
pg_atomic_uint32 pss_barrierCheckMask
Definition procsignal.c:80
const char * type
const char * name
void HandleWalSndInitStopping(void)
Definition walsender.c:3914
#define kill(pid, sig)
Definition win32_port.h:490
#define SIGUSR1
Definition win32_port.h:170

◆ BARRIER_SHOULD_CHECK

#define BARRIER_SHOULD_CHECK (   flags,
  type 
)     (((flags) & (((uint32) 1) << (uint32) (type))) != 0)

Definition at line 105 of file procsignal.c.

◆ NumProcSignalSlots

#define NumProcSignalSlots   (MaxBackends + NUM_AUXILIARY_PROCS)

Definition at line 102 of file procsignal.c.

Function Documentation

◆ CheckProcSignal()

static bool CheckProcSignal ( ProcSignalReason  reason)
static

Definition at line 671 of file procsignal.c.

672{
673 volatile ProcSignalSlot *slot = MyProcSignalSlot;
674
675 if (slot != NULL)
676 {
677 /*
678 * Careful here --- don't clear flag if we haven't seen it set.
679 * pss_signalFlags is of type "volatile sig_atomic_t" to allow us to
680 * read it here safely, without holding the spinlock.
681 */
682 if (slot->pss_signalFlags[reason])
683 {
684 slot->pss_signalFlags[reason] = false;
685 return true;
686 }
687 }
688
689 return false;
690}

References fb(), MyProcSignalSlot, and ProcSignalSlot::pss_signalFlags.

Referenced by procsignal_sigusr1_handler().

◆ CleanupProcSignalState()

static void CleanupProcSignalState ( int  status,
Datum  arg 
)
static

Definition at line 241 of file procsignal.c.

242{
245
246 /*
247 * Clear MyProcSignalSlot, so that a SIGUSR1 received after this point
248 * won't try to access it after it's no longer ours (and perhaps even
249 * after we've unmapped the shared memory segment).
250 */
253
254 /* sanity check */
257 if (old_pid != MyProcPid)
258 {
259 /*
260 * don't ERROR here. We're exiting anyway, and don't want to get into
261 * infinite loop trying to exit
262 */
264 elog(LOG, "process %d releasing ProcSignal slot %d, but it contains %d",
265 MyProcPid, (int) (slot - ProcSignal->psh_slot), (int) old_pid);
266 return; /* XXX better to zero the slot anyway? */
267 }
268
269 /* Mark the slot as unused */
270 pg_atomic_write_u32(&slot->pss_pid, 0);
271 slot->pss_cancel_key_len = 0;
272
273 /*
274 * Make this slot look like it's absorbed all possible barriers, so that
275 * no barrier waits block on it.
276 */
278
280
282}

References Assert, ConditionVariableBroadcast(), elog, fb(), LOG, MyProcPid, MyProcSignalSlot, pg_atomic_read_u32(), pg_atomic_write_u32(), pg_atomic_write_u64(), PG_UINT64_MAX, ProcSignal, ProcSignalHeader::psh_slot, ProcSignalSlot::pss_barrierCV, ProcSignalSlot::pss_barrierGeneration, ProcSignalSlot::pss_cancel_key_len, ProcSignalSlot::pss_mutex, ProcSignalSlot::pss_pid, SpinLockAcquire(), and SpinLockRelease().

Referenced by ProcSignalInit().

◆ EmitProcSignalBarrier()

uint64 EmitProcSignalBarrier ( ProcSignalBarrierType  type)

Definition at line 368 of file procsignal.c.

369{
370 uint32 flagbit = 1 << (uint32) type;
371 uint64 generation;
372
373 /*
374 * Set all the flags.
375 *
376 * Note that pg_atomic_fetch_or_u32 has full barrier semantics, so this is
377 * totally ordered with respect to anything the caller did before, and
378 * anything that we do afterwards. (This is also true of the later call to
379 * pg_atomic_add_fetch_u64.)
380 */
381 for (int i = 0; i < NumProcSignalSlots; i++)
382 {
383 volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
384
386 }
387
388 /*
389 * Increment the generation counter.
390 */
391 generation =
393
394 /*
395 * Signal all the processes, so that they update their advertised barrier
396 * generation.
397 *
398 * Concurrency is not a problem here. Backends that have exited don't
399 * matter, and new backends that have joined since we entered this
400 * function must already have current state, since the caller is
401 * responsible for making sure that the relevant state is entirely visible
402 * before calling this function in the first place. We still have to wake
403 * them up - because we can't distinguish between such backends and older
404 * backends that need to update state - but they won't actually need to
405 * change any state.
406 */
407 for (int i = NumProcSignalSlots - 1; i >= 0; i--)
408 {
409 volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
410 pid_t pid = pg_atomic_read_u32(&slot->pss_pid);
411
412 if (pid != 0)
413 {
415 pid = pg_atomic_read_u32(&slot->pss_pid);
416 if (pid != 0)
417 {
418 /* see SendProcSignal for details */
419 slot->pss_signalFlags[PROCSIG_BARRIER] = true;
421 kill(pid, SIGUSR1);
422 }
423 else
425 }
426 }
427
428 return generation;
429}

References fb(), 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 abort_logical_decoding_activation(), dbase_redo(), DisableLogicalDecoding(), dropdb(), DropTableSpace(), EmitAndWaitDataChecksumsBarrier(), EnableLogicalDecoding(), movedb(), SetDataChecksumsOff(), SetDataChecksumsOn(), SetDataChecksumsOnInProgress(), tblspc_redo(), and UpdateLogicalDecodingStatusEndOfRecovery().

◆ HandleProcSignalBarrierInterrupt()

static void HandleProcSignalBarrierInterrupt ( void  )
static

Definition at line 495 of file procsignal.c.

496{
497 InterruptPending = true;
499 /* latch will be set by procsignal_sigusr1_handler */
500}

References InterruptPending, and ProcSignalBarrierPending.

Referenced by procsignal_sigusr1_handler().

◆ ProcessProcSignalBarrier()

void ProcessProcSignalBarrier ( void  )

Definition at line 511 of file procsignal.c.

512{
515 volatile uint32 flags;
516
518
519 /* Exit quickly if there's no work to do. */
521 return;
523
524 /*
525 * It's not unlikely to process multiple barriers at once, before the
526 * signals for all the barriers have arrived. To avoid unnecessary work in
527 * response to subsequent signals, exit early if we already have processed
528 * all of them.
529 */
532
534
535 if (local_gen == shared_gen)
536 return;
537
538 /*
539 * Get and clear the flags that are set for this backend. Note that
540 * pg_atomic_exchange_u32 is a full barrier, so we're guaranteed that the
541 * read of the barrier generation above happens before we atomically
542 * extract the flags, and that any subsequent state changes happen
543 * afterward.
544 *
545 * NB: In order to avoid race conditions, we must zero
546 * pss_barrierCheckMask first and only afterwards try to do barrier
547 * processing. If we did it in the other order, someone could send us
548 * another barrier of some type right after we called the
549 * barrier-processing function but before we cleared the bit. We would
550 * have no way of knowing that the bit needs to stay set in that case, so
551 * the need to call the barrier-processing function again would just get
552 * forgotten. So instead, we tentatively clear all the bits and then put
553 * back any for which we don't manage to successfully absorb the barrier.
554 */
556
557 /*
558 * If there are no flags set, then we can skip doing any real work.
559 * Otherwise, establish a PG_TRY block, so that we don't lose track of
560 * which types of barrier processing are needed if an ERROR occurs.
561 */
562 if (flags != 0)
563 {
564 bool success = true;
565
566 PG_TRY();
567 {
568 /*
569 * Process each type of barrier. The barrier-processing functions
570 * should normally return true, but may return false if the
571 * barrier can't be absorbed at the current time. This should be
572 * rare, because it's pretty expensive. Every single
573 * CHECK_FOR_INTERRUPTS() will return here until we manage to
574 * absorb the barrier, and that cost will add up in a hurry.
575 *
576 * NB: It ought to be OK to call the barrier-processing functions
577 * unconditionally, but it's more efficient to call only the ones
578 * that might need us to do something based on the flags.
579 */
580 while (flags != 0)
581 {
583 bool processed = true;
584
586 switch (type)
587 {
589 processed = ProcessBarrierSmgrRelease();
590 break;
593 break;
594
599 processed = AbsorbDataChecksumsBarrier(type);
600 break;
601 }
602
603 /*
604 * To avoid an infinite loop, we must always unset the bit in
605 * flags.
606 */
607 BARRIER_CLEAR_BIT(flags, type);
608
609 /*
610 * If we failed to process the barrier, reset the shared bit
611 * so we try again later, and set a flag so that we don't bump
612 * our generation.
613 */
614 if (!processed)
615 {
617 success = false;
618 }
619 }
620 }
621 PG_CATCH();
622 {
623 /*
624 * If an ERROR occurred, we'll need to try again later to handle
625 * that barrier type and any others that haven't been handled yet
626 * or weren't successfully absorbed.
627 */
629 PG_RE_THROW();
630 }
631 PG_END_TRY();
632
633 /*
634 * If some barrier types were not successfully absorbed, we will have
635 * to try again later.
636 */
637 if (!success)
638 return;
639 }
640
641 /*
642 * State changes related to all types of barriers that might have been
643 * emitted have now been handled, so we can update our notion of the
644 * generation to the one we observed before beginning the updates. If
645 * things have changed further, it'll get fixed up when this function is
646 * next called.
647 */
650}

References AbsorbDataChecksumsBarrier(), Assert, BARRIER_CLEAR_BIT, ConditionVariableBroadcast(), fb(), 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(), ProcessBarrierUpdateXLogLogicalInfo(), ProcSignal, PROCSIGNAL_BARRIER_CHECKSUM_INPROGRESS_OFF, PROCSIGNAL_BARRIER_CHECKSUM_INPROGRESS_ON, PROCSIGNAL_BARRIER_CHECKSUM_OFF, PROCSIGNAL_BARRIER_CHECKSUM_ON, PROCSIGNAL_BARRIER_SMGRRELEASE, PROCSIGNAL_BARRIER_UPDATE_XLOG_LOGICAL_INFO, 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 696 of file procsignal.c.

References CheckProcSignal(), HandleCatchupInterrupt(), HandleLogMemoryContextInterrupt(), HandleNotifyInterrupt(), HandleParallelApplyMessageInterrupt(), HandleParallelMessageInterrupt(), HandleProcSignalBarrierInterrupt(), HandleRecoveryConflictInterrupt(), HandleRepackMessageInterrupt(), HandleSlotSyncMessageInterrupt(), HandleWalSndInitStopping(), MyLatch, PROCSIG_BARRIER, PROCSIG_CATCHUP_INTERRUPT, PROCSIG_LOG_MEMORY_CONTEXT, PROCSIG_NOTIFY_INTERRUPT, PROCSIG_PARALLEL_APPLY_MESSAGE, PROCSIG_PARALLEL_MESSAGE, PROCSIG_RECOVERY_CONFLICT, PROCSIG_REPACK_MESSAGE, PROCSIG_SLOTSYNC_MESSAGE, PROCSIG_WALSND_INIT_STOPPING, and SetLatch().

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

◆ ProcSignalInit()

void ProcSignalInit ( const uint8 cancel_key,
int  cancel_key_len 
)

Definition at line 170 of file procsignal.c.

171{
172 ProcSignalSlot *slot;
175
177 if (MyProcNumber < 0)
178 elog(ERROR, "MyProcNumber not set");
180 elog(ERROR, "unexpected MyProcNumber %d in ProcSignalInit (max %d)", MyProcNumber, NumProcSignalSlots);
182
184
185 /* Value used for sanity check below */
187
188 /* Clear out any leftover signal reasons */
190
191 /*
192 * Publish the PID before reading the global barrier generation to ensure
193 * that EmitProcSignalBarrier() doesn't skip us while we are grabbing an
194 * older generation. We need a memory barrier here to make sure that the
195 * update of pss_pid is ordered before the subsequent load of
196 * psh_barrierGeneration.
197 */
199
200 /*
201 * Initialize barrier state. Since we're a brand-new process, there
202 * shouldn't be any leftover backend-private state that needs to be
203 * updated. Therefore, we can broadcast the latest barrier generation and
204 * disregard any previously-set check bits.
205 *
206 * NB: This only works if this initialization happens early enough in the
207 * startup sequence that we haven't yet cached any state that might need
208 * to be invalidated. That's also why we have a memory barrier here, to be
209 * sure that any later reads of memory happen strictly after this.
210 */
215
216 if (cancel_key_len > 0)
219
221
222 /* Spinlock is released, do the check */
223 if (old_pss_pid != 0)
224 elog(LOG, "process %d taking over ProcSignal slot %d, but it's not empty",
226
227 /* Remember slot location for CheckProcSignal */
228 MyProcSignalSlot = slot;
229
230 /* Set up to release the slot on process exit */
232}

References Assert, CleanupProcSignalState(), elog, ERROR, fb(), LOG, MAX_CANCEL_KEY_LENGTH, memcpy(), MemSet, MyProcNumber, MyProcPid, MyProcSignalSlot, NUM_PROCSIGNALS, NumProcSignalSlots, on_shmem_exit(), pg_atomic_read_u32(), pg_atomic_read_u64(), pg_atomic_write_membarrier_u32(), 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()

◆ ProcSignalShmemRequest()

static void ProcSignalShmemRequest ( void arg)
static

Definition at line 133 of file procsignal.c.

134{
135 Size size;
136
138 size = add_size(size, offsetof(ProcSignalHeader, psh_slot));
139
140 ShmemRequestStruct(.name = "ProcSignal",
141 .size = size,
142 .ptr = (void **) &ProcSignal,
143 );
144}

References add_size(), fb(), mul_size(), name, NumProcSignalSlots, ProcSignal, and ShmemRequestStruct.

◆ ResetProcSignalBarrierBits()

◆ SendCancelRequest()

void SendCancelRequest ( int  backendPID,
const uint8 cancel_key,
int  cancel_key_len 
)

Definition at line 739 of file procsignal.c.

740{
741 if (backendPID == 0)
742 {
743 ereport(LOG, (errmsg("invalid cancel request with PID 0")));
744 return;
745 }
746
747 /*
748 * See if we have a matching backend. Reading the pss_pid and
749 * pss_cancel_key fields is racy, a backend might die and remove itself
750 * from the array at any time. The probability of the cancellation key
751 * matching wrong process is miniscule, however, so we can live with that.
752 * PIDs are reused too, so sending the signal based on PID is inherently
753 * racy anyway, although OS's avoid reusing PIDs too soon.
754 */
755 for (int i = 0; i < NumProcSignalSlots; i++)
756 {
758 bool match;
759
760 if (pg_atomic_read_u32(&slot->pss_pid) != backendPID)
761 continue;
762
763 /* Acquire the spinlock and re-check */
765 if (pg_atomic_read_u32(&slot->pss_pid) != backendPID)
766 {
768 continue;
769 }
770 else
771 {
772 match = slot->pss_cancel_key_len == cancel_key_len &&
774
776
777 if (match)
778 {
779 /* Found a match; signal that backend to cancel current op */
781 (errmsg_internal("processing cancel request: sending SIGINT to process %d",
782 backendPID)));
783
784 /*
785 * If we have setsid(), signal the backend's whole process
786 * group
787 */
788#ifdef HAVE_SETSID
789 kill(-backendPID, SIGINT);
790#else
791 kill(backendPID, SIGINT);
792#endif
793 }
794 else
795 {
796 /* Right PID, wrong key: no way, Jose */
797 ereport(LOG,
798 (errmsg("wrong key in cancel request for process %d",
799 backendPID)));
800 }
801 return;
802 }
803 }
804
805 /* No matching backend */
806 ereport(LOG,
807 (errmsg("PID %d in cancel request did not match any process",
808 backendPID)));
809}

References DEBUG2, ereport, errmsg, errmsg_internal(), fb(), 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 296 of file procsignal.c.

297{
298 volatile ProcSignalSlot *slot;
299
300 if (procNumber != INVALID_PROC_NUMBER)
301 {
302 Assert(procNumber < NumProcSignalSlots);
303 slot = &ProcSignal->psh_slot[procNumber];
304
306 if (pg_atomic_read_u32(&slot->pss_pid) == pid)
307 {
308 /* Atomically set the proper flag */
309 slot->pss_signalFlags[reason] = true;
311 /* Send signal */
312 return kill(pid, SIGUSR1);
313 }
315 }
316 else
317 {
318 /*
319 * procNumber not provided, so search the array using pid. We search
320 * the array back to front so as to reduce search overhead. Passing
321 * INVALID_PROC_NUMBER means that the target is most likely an
322 * auxiliary process, which will have a slot near the end of the
323 * array.
324 */
325 int i;
326
327 for (i = NumProcSignalSlots - 1; i >= 0; i--)
328 {
329 slot = &ProcSignal->psh_slot[i];
330
331 if (pg_atomic_read_u32(&slot->pss_pid) == pid)
332 {
334 if (pg_atomic_read_u32(&slot->pss_pid) == pid)
335 {
336 /* Atomically set the proper flag */
337 slot->pss_signalFlags[reason] = true;
339 /* Send signal */
340 return kill(pid, SIGUSR1);
341 }
343 }
344 }
345 }
346
347 errno = ESRCH;
348 return -1;
349}

References Assert, fb(), 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 mq_putmessage(), pa_shutdown(), ParallelWorkerShutdown(), pg_log_backend_memory_contexts(), RepackWorkerShutdown(), ShutDownSlotSync(), SICleanupQueue(), SignalBackends(), SignalRecoveryConflict(), SignalRecoveryConflictWithDatabase(), SignalRecoveryConflictWithVirtualXID(), and WalSndInitStopping().

◆ WaitForProcSignalBarrier()

void WaitForProcSignalBarrier ( uint64  generation)

Definition at line 436 of file procsignal.c.

437{
439
440 elog(DEBUG1,
441 "waiting for all backends to process ProcSignalBarrier generation "
443 generation);
444
445 for (int i = NumProcSignalSlots - 1; i >= 0; i--)
446 {
449
450 /*
451 * It's important that we check only pss_barrierGeneration here and
452 * not pss_barrierCheckMask. Bits in pss_barrierCheckMask get cleared
453 * before the barrier is actually absorbed, but pss_barrierGeneration
454 * is updated only afterward.
455 */
457 while (oldval < generation)
458 {
460 5000,
462 ereport(LOG,
463 (errmsg("still waiting for backend with PID %d to accept ProcSignalBarrier",
464 (int) pg_atomic_read_u32(&slot->pss_pid))));
466 }
468 }
469
470 elog(DEBUG1,
471 "finished waiting for all backends to process ProcSignalBarrier generation "
473 generation);
474
475 /*
476 * The caller is probably calling this function because it wants to read
477 * the shared state or perform further writes to shared state once all
478 * backends are known to have absorbed the barrier. However, the read of
479 * pss_barrierGeneration was performed unlocked; insert a memory barrier
480 * to separate it from whatever follows.
481 */
483}

References Assert, ConditionVariableCancelSleep(), ConditionVariableTimedSleep(), DEBUG1, elog, ereport, errmsg, fb(), 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(), EmitAndWaitDataChecksumsBarrier(), EnableLogicalDecoding(), movedb(), SetDataChecksumsOff(), SetDataChecksumsOn(), SetDataChecksumsOnInProgress(), tblspc_redo(), and UpdateLogicalDecodingStatusEndOfRecovery().

Variable Documentation

◆ MyProcSignalSlot

◆ ProcSignal

◆ ProcSignalShmemCallbacks

const ShmemCallbacks ProcSignalShmemCallbacks
Initial value:
= {
.request_fn = ProcSignalShmemRequest,
.init_fn = ProcSignalShmemInit,
}

Definition at line 115 of file procsignal.c.

115 {
116 .request_fn = ProcSignalShmemRequest,
117 .init_fn = ProcSignalShmemInit,
118};