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

Generated by: LCOV version 2.0-1