LCOV - code coverage report
Current view: top level - src/backend/storage/ipc - procsignal.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 84.8 % 197 167
Test Date: 2026-04-06 14:16:21 Functions: 92.3 % 13 12
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * procsignal.c
       4              :  *    Routines for interprocess signaling
       5              :  *
       6              :  *
       7              :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       8              :  * Portions Copyright (c) 1994, Regents of the University of California
       9              :  *
      10              :  * IDENTIFICATION
      11              :  *    src/backend/storage/ipc/procsignal.c
      12              :  *
      13              :  *-------------------------------------------------------------------------
      14              :  */
      15              : #include "postgres.h"
      16              : 
      17              : #include <signal.h>
      18              : #include <unistd.h>
      19              : 
      20              : #include "access/parallel.h"
      21              : #include "commands/async.h"
      22              : #include "miscadmin.h"
      23              : #include "pgstat.h"
      24              : #include "port/pg_bitutils.h"
      25              : #include "postmaster/datachecksum_state.h"
      26              : #include "replication/logicalctl.h"
      27              : #include "replication/logicalworker.h"
      28              : #include "replication/walsender.h"
      29              : #include "storage/condition_variable.h"
      30              : #include "storage/ipc.h"
      31              : #include "storage/latch.h"
      32              : #include "storage/proc.h"
      33              : #include "storage/shmem.h"
      34              : #include "storage/sinval.h"
      35              : #include "storage/smgr.h"
      36              : #include "storage/subsystems.h"
      37              : #include "tcop/tcopprot.h"
      38              : #include "utils/memutils.h"
      39              : #include "utils/wait_event.h"
      40              : 
      41              : /*
      42              :  * The SIGUSR1 signal is multiplexed to support signaling multiple event
      43              :  * types. The specific reason is communicated via flags in shared memory.
      44              :  * We keep a boolean flag for each possible "reason", so that different
      45              :  * reasons can be signaled to a process concurrently.  (However, if the same
      46              :  * reason is signaled more than once nearly simultaneously, the process may
      47              :  * observe it only once.)
      48              :  *
      49              :  * Each process that wants to receive signals registers its process ID
      50              :  * in the ProcSignalSlots array. The array is indexed by ProcNumber to make
      51              :  * slot allocation simple, and to avoid having to search the array when you
      52              :  * know the ProcNumber of the process you're signaling.  (We do support
      53              :  * signaling without ProcNumber, but it's a bit less efficient.)
      54              :  *
      55              :  * The fields in each slot are protected by a spinlock, pss_mutex. pss_pid can
      56              :  * also be read without holding the spinlock, as a quick preliminary check
      57              :  * when searching for a particular PID in the array.
      58              :  *
      59              :  * pss_signalFlags are intended to be set in cases where we don't need to
      60              :  * keep track of whether or not the target process has handled the signal,
      61              :  * but sometimes we need confirmation, as when making a global state change
      62              :  * that cannot be considered complete until all backends have taken notice
      63              :  * of it. For such use cases, we set a bit in pss_barrierCheckMask and then
      64              :  * increment the current "barrier generation"; when the new barrier generation
      65              :  * (or greater) appears in the pss_barrierGeneration flag of every process,
      66              :  * we know that the message has been received everywhere.
      67              :  */
      68              : typedef struct
      69              : {
      70              :     pg_atomic_uint32 pss_pid;
      71              :     int         pss_cancel_key_len; /* 0 means no cancellation is possible */
      72              :     uint8       pss_cancel_key[MAX_CANCEL_KEY_LENGTH];
      73              :     volatile sig_atomic_t pss_signalFlags[NUM_PROCSIGNALS];
      74              :     slock_t     pss_mutex;      /* protects the above fields */
      75              : 
      76              :     /* Barrier-related fields (not protected by pss_mutex) */
      77              :     pg_atomic_uint64 pss_barrierGeneration;
      78              :     pg_atomic_uint32 pss_barrierCheckMask;
      79              :     ConditionVariable pss_barrierCV;
      80              : } ProcSignalSlot;
      81              : 
      82              : /*
      83              :  * Information that is global to the entire ProcSignal system can be stored
      84              :  * here.
      85              :  *
      86              :  * psh_barrierGeneration is the highest barrier generation in existence.
      87              :  */
      88              : struct ProcSignalHeader
      89              : {
      90              :     pg_atomic_uint64 psh_barrierGeneration;
      91              :     ProcSignalSlot psh_slot[FLEXIBLE_ARRAY_MEMBER];
      92              : };
      93              : 
      94              : /*
      95              :  * We reserve a slot for each possible ProcNumber, plus one for each
      96              :  * possible auxiliary process type.  (This scheme assumes there is not
      97              :  * more than one of any auxiliary process type at a time, except for
      98              :  * IO workers.)
      99              :  */
     100              : #define NumProcSignalSlots  (MaxBackends + NUM_AUXILIARY_PROCS)
     101              : 
     102              : /* Check whether the relevant type bit is set in the flags. */
     103              : #define BARRIER_SHOULD_CHECK(flags, type) \
     104              :     (((flags) & (((uint32) 1) << (uint32) (type))) != 0)
     105              : 
     106              : /* Clear the relevant type bit from the flags. */
     107              : #define BARRIER_CLEAR_BIT(flags, type) \
     108              :     ((flags) &= ~(((uint32) 1) << (uint32) (type)))
     109              : 
     110              : static void ProcSignalShmemRequest(void *arg);
     111              : static void ProcSignalShmemInit(void *arg);
     112              : 
     113              : const ShmemCallbacks ProcSignalShmemCallbacks = {
     114              :     .request_fn = ProcSignalShmemRequest,
     115              :     .init_fn = ProcSignalShmemInit,
     116              : };
     117              : 
     118              : NON_EXEC_STATIC ProcSignalHeader *ProcSignal = NULL;
     119              : 
     120              : static ProcSignalSlot *MyProcSignalSlot = NULL;
     121              : 
     122              : static bool CheckProcSignal(ProcSignalReason reason);
     123              : static void CleanupProcSignalState(int status, Datum arg);
     124              : static void ResetProcSignalBarrierBits(uint32 flags);
     125              : 
     126              : /*
     127              :  * ProcSignalShmemRequest
     128              :  *      Register ProcSignal's shared memory needs at postmaster startup
     129              :  */
     130              : static void
     131         1233 : ProcSignalShmemRequest(void *arg)
     132              : {
     133              :     Size        size;
     134              : 
     135         1233 :     size = mul_size(NumProcSignalSlots, sizeof(ProcSignalSlot));
     136         1233 :     size = add_size(size, offsetof(ProcSignalHeader, psh_slot));
     137              : 
     138         1233 :     ShmemRequestStruct(.name = "ProcSignal",
     139              :                        .size = size,
     140              :                        .ptr = (void **) &ProcSignal,
     141              :         );
     142         1233 : }
     143              : 
     144              : static void
     145         1230 : ProcSignalShmemInit(void *arg)
     146              : {
     147         1230 :     pg_atomic_init_u64(&ProcSignal->psh_barrierGeneration, 0);
     148              : 
     149       163161 :     for (int i = 0; i < NumProcSignalSlots; ++i)
     150              :     {
     151       161931 :         ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
     152              : 
     153       161931 :         SpinLockInit(&slot->pss_mutex);
     154       161931 :         pg_atomic_init_u32(&slot->pss_pid, 0);
     155       161931 :         slot->pss_cancel_key_len = 0;
     156       809655 :         MemSet(slot->pss_signalFlags, 0, sizeof(slot->pss_signalFlags));
     157       161931 :         pg_atomic_init_u64(&slot->pss_barrierGeneration, PG_UINT64_MAX);
     158       161931 :         pg_atomic_init_u32(&slot->pss_barrierCheckMask, 0);
     159       161931 :         ConditionVariableInit(&slot->pss_barrierCV);
     160              :     }
     161         1230 : }
     162              : 
     163              : /*
     164              :  * ProcSignalInit
     165              :  *      Register the current process in the ProcSignal array
     166              :  */
     167              : void
     168        24798 : ProcSignalInit(const uint8 *cancel_key, int cancel_key_len)
     169              : {
     170              :     ProcSignalSlot *slot;
     171              :     uint64      barrier_generation;
     172              :     uint32      old_pss_pid;
     173              : 
     174              :     Assert(cancel_key_len >= 0 && cancel_key_len <= MAX_CANCEL_KEY_LENGTH);
     175        24798 :     if (MyProcNumber < 0)
     176            0 :         elog(ERROR, "MyProcNumber not set");
     177        24798 :     if (MyProcNumber >= NumProcSignalSlots)
     178            0 :         elog(ERROR, "unexpected MyProcNumber %d in ProcSignalInit (max %d)", MyProcNumber, NumProcSignalSlots);
     179        24798 :     slot = &ProcSignal->psh_slot[MyProcNumber];
     180              : 
     181        24798 :     SpinLockAcquire(&slot->pss_mutex);
     182              : 
     183              :     /* Value used for sanity check below */
     184        24798 :     old_pss_pid = pg_atomic_read_u32(&slot->pss_pid);
     185              : 
     186              :     /* Clear out any leftover signal reasons */
     187       123990 :     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              :      */
     200        24798 :     pg_atomic_write_u32(&slot->pss_barrierCheckMask, 0);
     201              :     barrier_generation =
     202        24798 :         pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration);
     203        24798 :     pg_atomic_write_u64(&slot->pss_barrierGeneration, barrier_generation);
     204              : 
     205        24798 :     if (cancel_key_len > 0)
     206        14556 :         memcpy(slot->pss_cancel_key, cancel_key, cancel_key_len);
     207        24798 :     slot->pss_cancel_key_len = cancel_key_len;
     208        24798 :     pg_atomic_write_u32(&slot->pss_pid, MyProcPid);
     209              : 
     210        24798 :     SpinLockRelease(&slot->pss_mutex);
     211              : 
     212              :     /* Spinlock is released, do the check */
     213        24798 :     if (old_pss_pid != 0)
     214            0 :         elog(LOG, "process %d taking over ProcSignal slot %d, but it's not empty",
     215              :              MyProcPid, MyProcNumber);
     216              : 
     217              :     /* Remember slot location for CheckProcSignal */
     218        24798 :     MyProcSignalSlot = slot;
     219              : 
     220              :     /* Set up to release the slot on process exit */
     221        24798 :     on_shmem_exit(CleanupProcSignalState, (Datum) 0);
     222        24798 : }
     223              : 
     224              : /*
     225              :  * CleanupProcSignalState
     226              :  *      Remove current process from ProcSignal mechanism
     227              :  *
     228              :  * This function is called via on_shmem_exit() during backend shutdown.
     229              :  */
     230              : static void
     231        24798 : CleanupProcSignalState(int status, Datum arg)
     232              : {
     233              :     pid_t       old_pid;
     234        24798 :     ProcSignalSlot *slot = MyProcSignalSlot;
     235              : 
     236              :     /*
     237              :      * Clear MyProcSignalSlot, so that a SIGUSR1 received after this point
     238              :      * won't try to access it after it's no longer ours (and perhaps even
     239              :      * after we've unmapped the shared memory segment).
     240              :      */
     241              :     Assert(MyProcSignalSlot != NULL);
     242        24798 :     MyProcSignalSlot = NULL;
     243              : 
     244              :     /* sanity check */
     245        24798 :     SpinLockAcquire(&slot->pss_mutex);
     246        24798 :     old_pid = pg_atomic_read_u32(&slot->pss_pid);
     247        24798 :     if (old_pid != MyProcPid)
     248              :     {
     249              :         /*
     250              :          * don't ERROR here. We're exiting anyway, and don't want to get into
     251              :          * infinite loop trying to exit
     252              :          */
     253            0 :         SpinLockRelease(&slot->pss_mutex);
     254            0 :         elog(LOG, "process %d releasing ProcSignal slot %d, but it contains %d",
     255              :              MyProcPid, (int) (slot - ProcSignal->psh_slot), (int) old_pid);
     256            0 :         return;                 /* XXX better to zero the slot anyway? */
     257              :     }
     258              : 
     259              :     /* Mark the slot as unused */
     260        24798 :     pg_atomic_write_u32(&slot->pss_pid, 0);
     261        24798 :     slot->pss_cancel_key_len = 0;
     262              : 
     263              :     /*
     264              :      * Make this slot look like it's absorbed all possible barriers, so that
     265              :      * no barrier waits block on it.
     266              :      */
     267        24798 :     pg_atomic_write_u64(&slot->pss_barrierGeneration, PG_UINT64_MAX);
     268              : 
     269        24798 :     SpinLockRelease(&slot->pss_mutex);
     270              : 
     271        24798 :     ConditionVariableBroadcast(&slot->pss_barrierCV);
     272              : }
     273              : 
     274              : /*
     275              :  * SendProcSignal
     276              :  *      Send a signal to a Postgres process
     277              :  *
     278              :  * Providing procNumber is optional, but it will speed up the operation.
     279              :  *
     280              :  * On success (a signal was sent), zero is returned.
     281              :  * On error, -1 is returned, and errno is set (typically to ESRCH or EPERM).
     282              :  *
     283              :  * Not to be confused with ProcSendSignal
     284              :  */
     285              : int
     286         7642 : SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
     287              : {
     288              :     volatile ProcSignalSlot *slot;
     289              : 
     290         7642 :     if (procNumber != INVALID_PROC_NUMBER)
     291              :     {
     292              :         Assert(procNumber < NumProcSignalSlots);
     293         7574 :         slot = &ProcSignal->psh_slot[procNumber];
     294              : 
     295         7574 :         SpinLockAcquire(&slot->pss_mutex);
     296         7574 :         if (pg_atomic_read_u32(&slot->pss_pid) == pid)
     297              :         {
     298              :             /* Atomically set the proper flag */
     299         7574 :             slot->pss_signalFlags[reason] = true;
     300         7574 :             SpinLockRelease(&slot->pss_mutex);
     301              :             /* Send signal */
     302         7574 :             return kill(pid, SIGUSR1);
     303              :         }
     304            0 :         SpinLockRelease(&slot->pss_mutex);
     305              :     }
     306              :     else
     307              :     {
     308              :         /*
     309              :          * procNumber not provided, so search the array using pid.  We search
     310              :          * the array back to front so as to reduce search overhead.  Passing
     311              :          * INVALID_PROC_NUMBER means that the target is most likely an
     312              :          * auxiliary process, which will have a slot near the end of the
     313              :          * array.
     314              :          */
     315              :         int         i;
     316              : 
     317         3070 :         for (i = NumProcSignalSlots - 1; i >= 0; i--)
     318              :         {
     319         3070 :             slot = &ProcSignal->psh_slot[i];
     320              : 
     321         3070 :             if (pg_atomic_read_u32(&slot->pss_pid) == pid)
     322              :             {
     323           68 :                 SpinLockAcquire(&slot->pss_mutex);
     324           68 :                 if (pg_atomic_read_u32(&slot->pss_pid) == pid)
     325              :                 {
     326              :                     /* Atomically set the proper flag */
     327           68 :                     slot->pss_signalFlags[reason] = true;
     328           68 :                     SpinLockRelease(&slot->pss_mutex);
     329              :                     /* Send signal */
     330           68 :                     return kill(pid, SIGUSR1);
     331              :                 }
     332            0 :                 SpinLockRelease(&slot->pss_mutex);
     333              :             }
     334              :         }
     335              :     }
     336              : 
     337            0 :     errno = ESRCH;
     338            0 :     return -1;
     339              : }
     340              : 
     341              : /*
     342              :  * EmitProcSignalBarrier
     343              :  *      Send a signal to every Postgres process
     344              :  *
     345              :  * The return value of this function is the barrier "generation" created
     346              :  * by this operation. This value can be passed to WaitForProcSignalBarrier
     347              :  * to wait until it is known that every participant in the ProcSignal
     348              :  * mechanism has absorbed the signal (or started afterwards).
     349              :  *
     350              :  * Note that it would be a bad idea to use this for anything that happens
     351              :  * frequently, as interrupting every backend could cause a noticeable
     352              :  * performance hit.
     353              :  *
     354              :  * Callers are entitled to assume that this function will not throw ERROR
     355              :  * or FATAL.
     356              :  */
     357              : uint64
     358          663 : EmitProcSignalBarrier(ProcSignalBarrierType type)
     359              : {
     360          663 :     uint32      flagbit = 1 << (uint32) type;
     361              :     uint64      generation;
     362              : 
     363              :     /*
     364              :      * Set all the flags.
     365              :      *
     366              :      * Note that pg_atomic_fetch_or_u32 has full barrier semantics, so this is
     367              :      * totally ordered with respect to anything the caller did before, and
     368              :      * anything that we do afterwards. (This is also true of the later call to
     369              :      * pg_atomic_add_fetch_u64.)
     370              :      */
     371        70820 :     for (int i = 0; i < NumProcSignalSlots; i++)
     372              :     {
     373        70157 :         volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
     374              : 
     375        70157 :         pg_atomic_fetch_or_u32(&slot->pss_barrierCheckMask, flagbit);
     376              :     }
     377              : 
     378              :     /*
     379              :      * Increment the generation counter.
     380              :      */
     381              :     generation =
     382          663 :         pg_atomic_add_fetch_u64(&ProcSignal->psh_barrierGeneration, 1);
     383              : 
     384              :     /*
     385              :      * Signal all the processes, so that they update their advertised barrier
     386              :      * generation.
     387              :      *
     388              :      * Concurrency is not a problem here. Backends that have exited don't
     389              :      * matter, and new backends that have joined since we entered this
     390              :      * function must already have current state, since the caller is
     391              :      * responsible for making sure that the relevant state is entirely visible
     392              :      * before calling this function in the first place. We still have to wake
     393              :      * them up - because we can't distinguish between such backends and older
     394              :      * backends that need to update state - but they won't actually need to
     395              :      * change any state.
     396              :      */
     397        70820 :     for (int i = NumProcSignalSlots - 1; i >= 0; i--)
     398              :     {
     399        70157 :         volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
     400        70157 :         pid_t       pid = pg_atomic_read_u32(&slot->pss_pid);
     401              : 
     402        70157 :         if (pid != 0)
     403              :         {
     404         4080 :             SpinLockAcquire(&slot->pss_mutex);
     405         4080 :             pid = pg_atomic_read_u32(&slot->pss_pid);
     406         4080 :             if (pid != 0)
     407              :             {
     408              :                 /* see SendProcSignal for details */
     409         4080 :                 slot->pss_signalFlags[PROCSIG_BARRIER] = true;
     410         4080 :                 SpinLockRelease(&slot->pss_mutex);
     411         4080 :                 kill(pid, SIGUSR1);
     412              :             }
     413              :             else
     414            0 :                 SpinLockRelease(&slot->pss_mutex);
     415              :         }
     416              :     }
     417              : 
     418          663 :     return generation;
     419              : }
     420              : 
     421              : /*
     422              :  * WaitForProcSignalBarrier - wait until it is guaranteed that all changes
     423              :  * requested by a specific call to EmitProcSignalBarrier() have taken effect.
     424              :  */
     425              : void
     426          647 : WaitForProcSignalBarrier(uint64 generation)
     427              : {
     428              :     Assert(generation <= pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration));
     429              : 
     430          647 :     elog(DEBUG1,
     431              :          "waiting for all backends to process ProcSignalBarrier generation "
     432              :          UINT64_FORMAT,
     433              :          generation);
     434              : 
     435        69674 :     for (int i = NumProcSignalSlots - 1; i >= 0; i--)
     436              :     {
     437        69027 :         ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
     438              :         uint64      oldval;
     439              : 
     440              :         /*
     441              :          * It's important that we check only pss_barrierGeneration here and
     442              :          * not pss_barrierCheckMask. Bits in pss_barrierCheckMask get cleared
     443              :          * before the barrier is actually absorbed, but pss_barrierGeneration
     444              :          * is updated only afterward.
     445              :          */
     446        69027 :         oldval = pg_atomic_read_u64(&slot->pss_barrierGeneration);
     447        72024 :         while (oldval < generation)
     448              :         {
     449         2997 :             if (ConditionVariableTimedSleep(&slot->pss_barrierCV,
     450              :                                             5000,
     451              :                                             WAIT_EVENT_PROC_SIGNAL_BARRIER))
     452            0 :                 ereport(LOG,
     453              :                         (errmsg("still waiting for backend with PID %d to accept ProcSignalBarrier",
     454              :                                 (int) pg_atomic_read_u32(&slot->pss_pid))));
     455         2997 :             oldval = pg_atomic_read_u64(&slot->pss_barrierGeneration);
     456              :         }
     457        69027 :         ConditionVariableCancelSleep();
     458              :     }
     459              : 
     460          647 :     elog(DEBUG1,
     461              :          "finished waiting for all backends to process ProcSignalBarrier generation "
     462              :          UINT64_FORMAT,
     463              :          generation);
     464              : 
     465              :     /*
     466              :      * The caller is probably calling this function because it wants to read
     467              :      * the shared state or perform further writes to shared state once all
     468              :      * backends are known to have absorbed the barrier. However, the read of
     469              :      * pss_barrierGeneration was performed unlocked; insert a memory barrier
     470              :      * to separate it from whatever follows.
     471              :      */
     472          647 :     pg_memory_barrier();
     473          647 : }
     474              : 
     475              : /*
     476              :  * Handle receipt of an interrupt indicating a global barrier event.
     477              :  *
     478              :  * All the actual work is deferred to ProcessProcSignalBarrier(), because we
     479              :  * cannot safely access the barrier generation inside the signal handler as
     480              :  * 64bit atomics might use spinlock based emulation, even for reads. As this
     481              :  * routine only gets called when PROCSIG_BARRIER is sent that won't cause a
     482              :  * lot of unnecessary work.
     483              :  */
     484              : static void
     485         3016 : HandleProcSignalBarrierInterrupt(void)
     486              : {
     487         3016 :     InterruptPending = true;
     488         3016 :     ProcSignalBarrierPending = true;
     489              :     /* latch will be set by procsignal_sigusr1_handler */
     490         3016 : }
     491              : 
     492              : /*
     493              :  * Perform global barrier related interrupt checking.
     494              :  *
     495              :  * Any backend that participates in ProcSignal signaling must arrange to
     496              :  * call this function periodically. It is called from CHECK_FOR_INTERRUPTS(),
     497              :  * which is enough for normal backends, but not necessarily for all types of
     498              :  * background processes.
     499              :  */
     500              : void
     501         3009 : ProcessProcSignalBarrier(void)
     502              : {
     503              :     uint64      local_gen;
     504              :     uint64      shared_gen;
     505              :     volatile uint32 flags;
     506              : 
     507              :     Assert(MyProcSignalSlot);
     508              : 
     509              :     /* Exit quickly if there's no work to do. */
     510         3009 :     if (!ProcSignalBarrierPending)
     511            0 :         return;
     512         3009 :     ProcSignalBarrierPending = false;
     513              : 
     514              :     /*
     515              :      * It's not unlikely to process multiple barriers at once, before the
     516              :      * signals for all the barriers have arrived. To avoid unnecessary work in
     517              :      * response to subsequent signals, exit early if we already have processed
     518              :      * all of them.
     519              :      */
     520         3009 :     local_gen = pg_atomic_read_u64(&MyProcSignalSlot->pss_barrierGeneration);
     521         3009 :     shared_gen = pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration);
     522              : 
     523              :     Assert(local_gen <= shared_gen);
     524              : 
     525         3009 :     if (local_gen == shared_gen)
     526            2 :         return;
     527              : 
     528              :     /*
     529              :      * Get and clear the flags that are set for this backend. Note that
     530              :      * pg_atomic_exchange_u32 is a full barrier, so we're guaranteed that the
     531              :      * read of the barrier generation above happens before we atomically
     532              :      * extract the flags, and that any subsequent state changes happen
     533              :      * afterward.
     534              :      *
     535              :      * NB: In order to avoid race conditions, we must zero
     536              :      * pss_barrierCheckMask first and only afterwards try to do barrier
     537              :      * processing. If we did it in the other order, someone could send us
     538              :      * another barrier of some type right after we called the
     539              :      * barrier-processing function but before we cleared the bit. We would
     540              :      * have no way of knowing that the bit needs to stay set in that case, so
     541              :      * the need to call the barrier-processing function again would just get
     542              :      * forgotten. So instead, we tentatively clear all the bits and then put
     543              :      * back any for which we don't manage to successfully absorb the barrier.
     544              :      */
     545         3007 :     flags = pg_atomic_exchange_u32(&MyProcSignalSlot->pss_barrierCheckMask, 0);
     546              : 
     547              :     /*
     548              :      * If there are no flags set, then we can skip doing any real work.
     549              :      * Otherwise, establish a PG_TRY block, so that we don't lose track of
     550              :      * which types of barrier processing are needed if an ERROR occurs.
     551              :      */
     552         3007 :     if (flags != 0)
     553              :     {
     554         3007 :         bool        success = true;
     555              : 
     556         3007 :         PG_TRY();
     557              :         {
     558              :             /*
     559              :              * Process each type of barrier. The barrier-processing functions
     560              :              * should normally return true, but may return false if the
     561              :              * barrier can't be absorbed at the current time. This should be
     562              :              * rare, because it's pretty expensive.  Every single
     563              :              * CHECK_FOR_INTERRUPTS() will return here until we manage to
     564              :              * absorb the barrier, and that cost will add up in a hurry.
     565              :              *
     566              :              * NB: It ought to be OK to call the barrier-processing functions
     567              :              * unconditionally, but it's more efficient to call only the ones
     568              :              * that might need us to do something based on the flags.
     569              :              */
     570         9021 :             while (flags != 0)
     571              :             {
     572              :                 ProcSignalBarrierType type;
     573         3007 :                 bool        processed = true;
     574              : 
     575         3007 :                 type = (ProcSignalBarrierType) pg_rightmost_one_pos32(flags);
     576         3007 :                 switch (type)
     577              :                 {
     578          829 :                     case PROCSIGNAL_BARRIER_SMGRRELEASE:
     579          829 :                         processed = ProcessBarrierSmgrRelease();
     580          829 :                         break;
     581         1889 :                     case PROCSIGNAL_BARRIER_UPDATE_XLOG_LOGICAL_INFO:
     582         1889 :                         processed = ProcessBarrierUpdateXLogLogicalInfo();
     583         1889 :                         break;
     584              : 
     585          289 :                     case PROCSIGNAL_BARRIER_CHECKSUM_INPROGRESS_ON:
     586              :                     case PROCSIGNAL_BARRIER_CHECKSUM_ON:
     587              :                     case PROCSIGNAL_BARRIER_CHECKSUM_INPROGRESS_OFF:
     588              :                     case PROCSIGNAL_BARRIER_CHECKSUM_OFF:
     589          289 :                         processed = AbsorbDataChecksumsBarrier(type);
     590          289 :                         break;
     591              :                 }
     592              : 
     593              :                 /*
     594              :                  * To avoid an infinite loop, we must always unset the bit in
     595              :                  * flags.
     596              :                  */
     597         3007 :                 BARRIER_CLEAR_BIT(flags, type);
     598              : 
     599              :                 /*
     600              :                  * If we failed to process the barrier, reset the shared bit
     601              :                  * so we try again later, and set a flag so that we don't bump
     602              :                  * our generation.
     603              :                  */
     604         3007 :                 if (!processed)
     605              :                 {
     606            0 :                     ResetProcSignalBarrierBits(((uint32) 1) << type);
     607            0 :                     success = false;
     608              :                 }
     609              :             }
     610              :         }
     611            0 :         PG_CATCH();
     612              :         {
     613              :             /*
     614              :              * If an ERROR occurred, we'll need to try again later to handle
     615              :              * that barrier type and any others that haven't been handled yet
     616              :              * or weren't successfully absorbed.
     617              :              */
     618            0 :             ResetProcSignalBarrierBits(flags);
     619            0 :             PG_RE_THROW();
     620              :         }
     621         3007 :         PG_END_TRY();
     622              : 
     623              :         /*
     624              :          * If some barrier types were not successfully absorbed, we will have
     625              :          * to try again later.
     626              :          */
     627         3007 :         if (!success)
     628            0 :             return;
     629              :     }
     630              : 
     631              :     /*
     632              :      * State changes related to all types of barriers that might have been
     633              :      * emitted have now been handled, so we can update our notion of the
     634              :      * generation to the one we observed before beginning the updates. If
     635              :      * things have changed further, it'll get fixed up when this function is
     636              :      * next called.
     637              :      */
     638         3007 :     pg_atomic_write_u64(&MyProcSignalSlot->pss_barrierGeneration, shared_gen);
     639         3007 :     ConditionVariableBroadcast(&MyProcSignalSlot->pss_barrierCV);
     640              : }
     641              : 
     642              : /*
     643              :  * If it turns out that we couldn't absorb one or more barrier types, either
     644              :  * because the barrier-processing functions returned false or due to an error,
     645              :  * arrange for processing to be retried later.
     646              :  */
     647              : static void
     648            0 : ResetProcSignalBarrierBits(uint32 flags)
     649              : {
     650            0 :     pg_atomic_fetch_or_u32(&MyProcSignalSlot->pss_barrierCheckMask, flags);
     651            0 :     ProcSignalBarrierPending = true;
     652            0 :     InterruptPending = true;
     653            0 : }
     654              : 
     655              : /*
     656              :  * CheckProcSignal - check to see if a particular reason has been
     657              :  * signaled, and clear the signal flag.  Should be called after receiving
     658              :  * SIGUSR1.
     659              :  */
     660              : static bool
     661       111664 : CheckProcSignal(ProcSignalReason reason)
     662              : {
     663       111664 :     volatile ProcSignalSlot *slot = MyProcSignalSlot;
     664              : 
     665       111664 :     if (slot != NULL)
     666              :     {
     667              :         /*
     668              :          * Careful here --- don't clear flag if we haven't seen it set.
     669              :          * pss_signalFlags is of type "volatile sig_atomic_t" to allow us to
     670              :          * read it here safely, without holding the spinlock.
     671              :          */
     672       111520 :         if (slot->pss_signalFlags[reason])
     673              :         {
     674         8345 :             slot->pss_signalFlags[reason] = false;
     675         8345 :             return true;
     676              :         }
     677              :     }
     678              : 
     679       103319 :     return false;
     680              : }
     681              : 
     682              : /*
     683              :  * procsignal_sigusr1_handler - handle SIGUSR1 signal.
     684              :  */
     685              : void
     686        13958 : procsignal_sigusr1_handler(SIGNAL_ARGS)
     687              : {
     688        13958 :     if (CheckProcSignal(PROCSIG_CATCHUP_INTERRUPT))
     689         3139 :         HandleCatchupInterrupt();
     690              : 
     691        13958 :     if (CheckProcSignal(PROCSIG_NOTIFY_INTERRUPT))
     692           11 :         HandleNotifyInterrupt();
     693              : 
     694        13958 :     if (CheckProcSignal(PROCSIG_PARALLEL_MESSAGE))
     695         2088 :         HandleParallelMessageInterrupt();
     696              : 
     697        13958 :     if (CheckProcSignal(PROCSIG_WALSND_INIT_STOPPING))
     698           47 :         HandleWalSndInitStopping();
     699              : 
     700        13958 :     if (CheckProcSignal(PROCSIG_BARRIER))
     701         3016 :         HandleProcSignalBarrierInterrupt();
     702              : 
     703        13958 :     if (CheckProcSignal(PROCSIG_LOG_MEMORY_CONTEXT))
     704           11 :         HandleLogMemoryContextInterrupt();
     705              : 
     706        13958 :     if (CheckProcSignal(PROCSIG_PARALLEL_APPLY_MESSAGE))
     707           15 :         HandleParallelApplyMessageInterrupt();
     708              : 
     709        13958 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT))
     710           18 :         HandleRecoveryConflictInterrupt();
     711              : 
     712        13958 :     SetLatch(MyLatch);
     713        13958 : }
     714              : 
     715              : /*
     716              :  * Send a query cancellation signal to backend.
     717              :  *
     718              :  * Note: This is called from a backend process before authentication.  We
     719              :  * cannot take LWLocks yet, but that's OK; we rely on atomic reads of the
     720              :  * fields in the ProcSignal slots.
     721              :  */
     722              : void
     723           16 : SendCancelRequest(int backendPID, const uint8 *cancel_key, int cancel_key_len)
     724              : {
     725           16 :     if (backendPID == 0)
     726              :     {
     727            0 :         ereport(LOG, (errmsg("invalid cancel request with PID 0")));
     728            0 :         return;
     729              :     }
     730              : 
     731              :     /*
     732              :      * See if we have a matching backend. Reading the pss_pid and
     733              :      * pss_cancel_key fields is racy, a backend might die and remove itself
     734              :      * from the array at any time.  The probability of the cancellation key
     735              :      * matching wrong process is miniscule, however, so we can live with that.
     736              :      * PIDs are reused too, so sending the signal based on PID is inherently
     737              :      * racy anyway, although OS's avoid reusing PIDs too soon.
     738              :      */
     739          276 :     for (int i = 0; i < NumProcSignalSlots; i++)
     740              :     {
     741          276 :         ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
     742              :         bool        match;
     743              : 
     744          276 :         if (pg_atomic_read_u32(&slot->pss_pid) != backendPID)
     745          260 :             continue;
     746              : 
     747              :         /* Acquire the spinlock and re-check */
     748           16 :         SpinLockAcquire(&slot->pss_mutex);
     749           16 :         if (pg_atomic_read_u32(&slot->pss_pid) != backendPID)
     750              :         {
     751            0 :             SpinLockRelease(&slot->pss_mutex);
     752            0 :             continue;
     753              :         }
     754              :         else
     755              :         {
     756           32 :             match = slot->pss_cancel_key_len == cancel_key_len &&
     757           16 :                 timingsafe_bcmp(slot->pss_cancel_key, cancel_key, cancel_key_len) == 0;
     758              : 
     759           16 :             SpinLockRelease(&slot->pss_mutex);
     760              : 
     761           16 :             if (match)
     762              :             {
     763              :                 /* Found a match; signal that backend to cancel current op */
     764           16 :                 ereport(DEBUG2,
     765              :                         (errmsg_internal("processing cancel request: sending SIGINT to process %d",
     766              :                                          backendPID)));
     767              : 
     768              :                 /*
     769              :                  * If we have setsid(), signal the backend's whole process
     770              :                  * group
     771              :                  */
     772              : #ifdef HAVE_SETSID
     773           16 :                 kill(-backendPID, SIGINT);
     774              : #else
     775              :                 kill(backendPID, SIGINT);
     776              : #endif
     777              :             }
     778              :             else
     779              :             {
     780              :                 /* Right PID, wrong key: no way, Jose */
     781            0 :                 ereport(LOG,
     782              :                         (errmsg("wrong key in cancel request for process %d",
     783              :                                 backendPID)));
     784              :             }
     785           16 :             return;
     786              :         }
     787              :     }
     788              : 
     789              :     /* No matching backend */
     790            0 :     ereport(LOG,
     791              :             (errmsg("PID %d in cancel request did not match any process",
     792              :                     backendPID)));
     793              : }
        

Generated by: LCOV version 2.0-1