LCOV - code coverage report
Current view: top level - src/backend/storage/ipc - procsignal.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 140 161 87.0 %
Date: 2024-04-25 22:10:56 Functions: 11 12 91.7 %
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-2024, 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 "replication/logicalworker.h"
      26             : #include "replication/walsender.h"
      27             : #include "storage/condition_variable.h"
      28             : #include "storage/ipc.h"
      29             : #include "storage/latch.h"
      30             : #include "storage/shmem.h"
      31             : #include "storage/sinval.h"
      32             : #include "storage/smgr.h"
      33             : #include "tcop/tcopprot.h"
      34             : #include "utils/memutils.h"
      35             : 
      36             : /*
      37             :  * The SIGUSR1 signal is multiplexed to support signaling multiple event
      38             :  * types. The specific reason is communicated via flags in shared memory.
      39             :  * We keep a boolean flag for each possible "reason", so that different
      40             :  * reasons can be signaled to a process concurrently.  (However, if the same
      41             :  * reason is signaled more than once nearly simultaneously, the process may
      42             :  * observe it only once.)
      43             :  *
      44             :  * Each process that wants to receive signals registers its process ID
      45             :  * in the ProcSignalSlots array. The array is indexed by ProcNumber to make
      46             :  * slot allocation simple, and to avoid having to search the array when you
      47             :  * know the ProcNumber of the process you're signaling.  (We do support
      48             :  * signaling without ProcNumber, but it's a bit less efficient.)
      49             :  *
      50             :  * The flags are actually declared as "volatile sig_atomic_t" for maximum
      51             :  * portability.  This should ensure that loads and stores of the flag
      52             :  * values are atomic, allowing us to dispense with any explicit locking.
      53             :  *
      54             :  * pss_signalFlags are intended to be set in cases where we don't need to
      55             :  * keep track of whether or not the target process has handled the signal,
      56             :  * but sometimes we need confirmation, as when making a global state change
      57             :  * that cannot be considered complete until all backends have taken notice
      58             :  * of it. For such use cases, we set a bit in pss_barrierCheckMask and then
      59             :  * increment the current "barrier generation"; when the new barrier generation
      60             :  * (or greater) appears in the pss_barrierGeneration flag of every process,
      61             :  * we know that the message has been received everywhere.
      62             :  */
      63             : typedef struct
      64             : {
      65             :     volatile pid_t pss_pid;
      66             :     volatile sig_atomic_t pss_signalFlags[NUM_PROCSIGNALS];
      67             :     pg_atomic_uint64 pss_barrierGeneration;
      68             :     pg_atomic_uint32 pss_barrierCheckMask;
      69             :     ConditionVariable pss_barrierCV;
      70             : } ProcSignalSlot;
      71             : 
      72             : /*
      73             :  * Information that is global to the entire ProcSignal system can be stored
      74             :  * here.
      75             :  *
      76             :  * psh_barrierGeneration is the highest barrier generation in existence.
      77             :  */
      78             : typedef struct
      79             : {
      80             :     pg_atomic_uint64 psh_barrierGeneration;
      81             :     ProcSignalSlot psh_slot[FLEXIBLE_ARRAY_MEMBER];
      82             : } ProcSignalHeader;
      83             : 
      84             : /*
      85             :  * We reserve a slot for each possible ProcNumber, plus one for each
      86             :  * possible auxiliary process type.  (This scheme assumes there is not
      87             :  * more than one of any auxiliary process type at a time.)
      88             :  */
      89             : #define NumProcSignalSlots  (MaxBackends + NUM_AUXILIARY_PROCS)
      90             : 
      91             : /* Check whether the relevant type bit is set in the flags. */
      92             : #define BARRIER_SHOULD_CHECK(flags, type) \
      93             :     (((flags) & (((uint32) 1) << (uint32) (type))) != 0)
      94             : 
      95             : /* Clear the relevant type bit from the flags. */
      96             : #define BARRIER_CLEAR_BIT(flags, type) \
      97             :     ((flags) &= ~(((uint32) 1) << (uint32) (type)))
      98             : 
      99             : static ProcSignalHeader *ProcSignal = NULL;
     100             : static ProcSignalSlot *MyProcSignalSlot = NULL;
     101             : 
     102             : static bool CheckProcSignal(ProcSignalReason reason);
     103             : static void CleanupProcSignalState(int status, Datum arg);
     104             : static void ResetProcSignalBarrierBits(uint32 flags);
     105             : 
     106             : /*
     107             :  * ProcSignalShmemSize
     108             :  *      Compute space needed for ProcSignal's shared memory
     109             :  */
     110             : Size
     111        5066 : ProcSignalShmemSize(void)
     112             : {
     113             :     Size        size;
     114             : 
     115        5066 :     size = mul_size(NumProcSignalSlots, sizeof(ProcSignalSlot));
     116        5066 :     size = add_size(size, offsetof(ProcSignalHeader, psh_slot));
     117        5066 :     return size;
     118             : }
     119             : 
     120             : /*
     121             :  * ProcSignalShmemInit
     122             :  *      Allocate and initialize ProcSignal's shared memory
     123             :  */
     124             : void
     125        1768 : ProcSignalShmemInit(void)
     126             : {
     127        1768 :     Size        size = ProcSignalShmemSize();
     128             :     bool        found;
     129             : 
     130        1768 :     ProcSignal = (ProcSignalHeader *)
     131        1768 :         ShmemInitStruct("ProcSignal", size, &found);
     132             : 
     133             :     /* If we're first, initialize. */
     134        1768 :     if (!found)
     135             :     {
     136             :         int         i;
     137             : 
     138        1768 :         pg_atomic_init_u64(&ProcSignal->psh_barrierGeneration, 0);
     139             : 
     140      158636 :         for (i = 0; i < NumProcSignalSlots; ++i)
     141             :         {
     142      156868 :             ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
     143             : 
     144      156868 :             slot->pss_pid = 0;
     145      156868 :             MemSet(slot->pss_signalFlags, 0, sizeof(slot->pss_signalFlags));
     146      156868 :             pg_atomic_init_u64(&slot->pss_barrierGeneration, PG_UINT64_MAX);
     147      156868 :             pg_atomic_init_u32(&slot->pss_barrierCheckMask, 0);
     148      156868 :             ConditionVariableInit(&slot->pss_barrierCV);
     149             :         }
     150             :     }
     151        1768 : }
     152             : 
     153             : /*
     154             :  * ProcSignalInit
     155             :  *      Register the current process in the ProcSignal array
     156             :  */
     157             : void
     158       29868 : ProcSignalInit(void)
     159             : {
     160             :     ProcSignalSlot *slot;
     161             :     uint64      barrier_generation;
     162             : 
     163       29868 :     if (MyProcNumber < 0)
     164           0 :         elog(ERROR, "MyProcNumber not set");
     165       29868 :     if (MyProcNumber >= NumProcSignalSlots)
     166           0 :         elog(ERROR, "unexpected MyProcNumber %d in ProcSignalInit (max %d)", MyProcNumber, NumProcSignalSlots);
     167       29868 :     slot = &ProcSignal->psh_slot[MyProcNumber];
     168             : 
     169             :     /* sanity check */
     170       29868 :     if (slot->pss_pid != 0)
     171           0 :         elog(LOG, "process %d taking over ProcSignal slot %d, but it's not empty",
     172             :              MyProcPid, MyProcNumber);
     173             : 
     174             :     /* Clear out any leftover signal reasons */
     175       29868 :     MemSet(slot->pss_signalFlags, 0, NUM_PROCSIGNALS * sizeof(sig_atomic_t));
     176             : 
     177             :     /*
     178             :      * Initialize barrier state. Since we're a brand-new process, there
     179             :      * shouldn't be any leftover backend-private state that needs to be
     180             :      * updated. Therefore, we can broadcast the latest barrier generation and
     181             :      * disregard any previously-set check bits.
     182             :      *
     183             :      * NB: This only works if this initialization happens early enough in the
     184             :      * startup sequence that we haven't yet cached any state that might need
     185             :      * to be invalidated. That's also why we have a memory barrier here, to be
     186             :      * sure that any later reads of memory happen strictly after this.
     187             :      */
     188       29868 :     pg_atomic_write_u32(&slot->pss_barrierCheckMask, 0);
     189             :     barrier_generation =
     190       29868 :         pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration);
     191       29868 :     pg_atomic_write_u64(&slot->pss_barrierGeneration, barrier_generation);
     192       29868 :     pg_memory_barrier();
     193             : 
     194             :     /* Mark slot with my PID */
     195       29868 :     slot->pss_pid = MyProcPid;
     196             : 
     197             :     /* Remember slot location for CheckProcSignal */
     198       29868 :     MyProcSignalSlot = slot;
     199             : 
     200             :     /* Set up to release the slot on process exit */
     201       29868 :     on_shmem_exit(CleanupProcSignalState, (Datum) 0);
     202       29868 : }
     203             : 
     204             : /*
     205             :  * CleanupProcSignalState
     206             :  *      Remove current process from ProcSignal mechanism
     207             :  *
     208             :  * This function is called via on_shmem_exit() during backend shutdown.
     209             :  */
     210             : static void
     211       29868 : CleanupProcSignalState(int status, Datum arg)
     212             : {
     213       29868 :     ProcSignalSlot *slot = MyProcSignalSlot;
     214             : 
     215             :     /*
     216             :      * Clear MyProcSignalSlot, so that a SIGUSR1 received after this point
     217             :      * won't try to access it after it's no longer ours (and perhaps even
     218             :      * after we've unmapped the shared memory segment).
     219             :      */
     220             :     Assert(MyProcSignalSlot != NULL);
     221       29868 :     MyProcSignalSlot = NULL;
     222             : 
     223             :     /* sanity check */
     224       29868 :     if (slot->pss_pid != MyProcPid)
     225             :     {
     226             :         /*
     227             :          * don't ERROR here. We're exiting anyway, and don't want to get into
     228             :          * infinite loop trying to exit
     229             :          */
     230           0 :         elog(LOG, "process %d releasing ProcSignal slot %d, but it contains %d",
     231             :              MyProcPid, (int) (slot - ProcSignal->psh_slot), (int) slot->pss_pid);
     232           0 :         return;                 /* XXX better to zero the slot anyway? */
     233             :     }
     234             : 
     235             :     /*
     236             :      * Make this slot look like it's absorbed all possible barriers, so that
     237             :      * no barrier waits block on it.
     238             :      */
     239       29868 :     pg_atomic_write_u64(&slot->pss_barrierGeneration, PG_UINT64_MAX);
     240       29868 :     ConditionVariableBroadcast(&slot->pss_barrierCV);
     241             : 
     242       29868 :     slot->pss_pid = 0;
     243             : }
     244             : 
     245             : /*
     246             :  * SendProcSignal
     247             :  *      Send a signal to a Postgres process
     248             :  *
     249             :  * Providing procNumber is optional, but it will speed up the operation.
     250             :  *
     251             :  * On success (a signal was sent), zero is returned.
     252             :  * On error, -1 is returned, and errno is set (typically to ESRCH or EPERM).
     253             :  *
     254             :  * Not to be confused with ProcSendSignal
     255             :  */
     256             : int
     257       10662 : SendProcSignal(pid_t pid, ProcSignalReason reason, ProcNumber procNumber)
     258             : {
     259             :     volatile ProcSignalSlot *slot;
     260             : 
     261       10662 :     if (procNumber != INVALID_PROC_NUMBER)
     262             :     {
     263       10562 :         slot = &ProcSignal->psh_slot[procNumber];
     264             : 
     265             :         /*
     266             :          * Note: Since there's no locking, it's possible that the target
     267             :          * process detaches from shared memory and exits right after this
     268             :          * test, before we set the flag and send signal. And the signal slot
     269             :          * might even be recycled by a new process, so it's remotely possible
     270             :          * that we set a flag for a wrong process. That's OK, all the signals
     271             :          * are such that no harm is done if they're mistakenly fired.
     272             :          */
     273       10562 :         if (slot->pss_pid == pid)
     274             :         {
     275             :             /* Atomically set the proper flag */
     276       10562 :             slot->pss_signalFlags[reason] = true;
     277             :             /* Send signal */
     278       10562 :             return kill(pid, SIGUSR1);
     279             :         }
     280             :     }
     281             :     else
     282             :     {
     283             :         /*
     284             :          * Pronumber not provided, so search the array using pid.  We search
     285             :          * the array back to front so as to reduce search overhead.  Passing
     286             :          * INVALID_PROC_NUMBER means that the target is most likely an
     287             :          * auxiliary process, which will have a slot near the end of the
     288             :          * array.
     289             :          */
     290             :         int         i;
     291             : 
     292        1216 :         for (i = NumProcSignalSlots - 1; i >= 0; i--)
     293             :         {
     294        1216 :             slot = &ProcSignal->psh_slot[i];
     295             : 
     296        1216 :             if (slot->pss_pid == pid)
     297             :             {
     298             :                 /* the above note about race conditions applies here too */
     299             : 
     300             :                 /* Atomically set the proper flag */
     301         100 :                 slot->pss_signalFlags[reason] = true;
     302             :                 /* Send signal */
     303         100 :                 return kill(pid, SIGUSR1);
     304             :             }
     305             :         }
     306             :     }
     307             : 
     308           0 :     errno = ESRCH;
     309           0 :     return -1;
     310             : }
     311             : 
     312             : /*
     313             :  * EmitProcSignalBarrier
     314             :  *      Send a signal to every Postgres process
     315             :  *
     316             :  * The return value of this function is the barrier "generation" created
     317             :  * by this operation. This value can be passed to WaitForProcSignalBarrier
     318             :  * to wait until it is known that every participant in the ProcSignal
     319             :  * mechanism has absorbed the signal (or started afterwards).
     320             :  *
     321             :  * Note that it would be a bad idea to use this for anything that happens
     322             :  * frequently, as interrupting every backend could cause a noticeable
     323             :  * performance hit.
     324             :  *
     325             :  * Callers are entitled to assume that this function will not throw ERROR
     326             :  * or FATAL.
     327             :  */
     328             : uint64
     329         142 : EmitProcSignalBarrier(ProcSignalBarrierType type)
     330             : {
     331         142 :     uint32      flagbit = 1 << (uint32) type;
     332             :     uint64      generation;
     333             : 
     334             :     /*
     335             :      * Set all the flags.
     336             :      *
     337             :      * Note that pg_atomic_fetch_or_u32 has full barrier semantics, so this is
     338             :      * totally ordered with respect to anything the caller did before, and
     339             :      * anything that we do afterwards. (This is also true of the later call to
     340             :      * pg_atomic_add_fetch_u64.)
     341             :      */
     342        9882 :     for (int i = 0; i < NumProcSignalSlots; i++)
     343             :     {
     344        9740 :         volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
     345             : 
     346        9740 :         pg_atomic_fetch_or_u32(&slot->pss_barrierCheckMask, flagbit);
     347             :     }
     348             : 
     349             :     /*
     350             :      * Increment the generation counter.
     351             :      */
     352             :     generation =
     353         142 :         pg_atomic_add_fetch_u64(&ProcSignal->psh_barrierGeneration, 1);
     354             : 
     355             :     /*
     356             :      * Signal all the processes, so that they update their advertised barrier
     357             :      * generation.
     358             :      *
     359             :      * Concurrency is not a problem here. Backends that have exited don't
     360             :      * matter, and new backends that have joined since we entered this
     361             :      * function must already have current state, since the caller is
     362             :      * responsible for making sure that the relevant state is entirely visible
     363             :      * before calling this function in the first place. We still have to wake
     364             :      * them up - because we can't distinguish between such backends and older
     365             :      * backends that need to update state - but they won't actually need to
     366             :      * change any state.
     367             :      */
     368        9882 :     for (int i = NumProcSignalSlots - 1; i >= 0; i--)
     369             :     {
     370        9740 :         volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
     371        9740 :         pid_t       pid = slot->pss_pid;
     372             : 
     373        9740 :         if (pid != 0)
     374             :         {
     375             :             /* see SendProcSignal for details */
     376         818 :             slot->pss_signalFlags[PROCSIG_BARRIER] = true;
     377         818 :             kill(pid, SIGUSR1);
     378             :         }
     379             :     }
     380             : 
     381         142 :     return generation;
     382             : }
     383             : 
     384             : /*
     385             :  * WaitForProcSignalBarrier - wait until it is guaranteed that all changes
     386             :  * requested by a specific call to EmitProcSignalBarrier() have taken effect.
     387             :  */
     388             : void
     389         142 : WaitForProcSignalBarrier(uint64 generation)
     390             : {
     391             :     Assert(generation <= pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration));
     392             : 
     393         142 :     elog(DEBUG1,
     394             :          "waiting for all backends to process ProcSignalBarrier generation "
     395             :          UINT64_FORMAT,
     396             :          generation);
     397             : 
     398        9882 :     for (int i = NumProcSignalSlots - 1; i >= 0; i--)
     399             :     {
     400        9740 :         ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
     401             :         uint64      oldval;
     402             : 
     403             :         /*
     404             :          * It's important that we check only pss_barrierGeneration here and
     405             :          * not pss_barrierCheckMask. Bits in pss_barrierCheckMask get cleared
     406             :          * before the barrier is actually absorbed, but pss_barrierGeneration
     407             :          * is updated only afterward.
     408             :          */
     409        9740 :         oldval = pg_atomic_read_u64(&slot->pss_barrierGeneration);
     410       10150 :         while (oldval < generation)
     411             :         {
     412         410 :             if (ConditionVariableTimedSleep(&slot->pss_barrierCV,
     413             :                                             5000,
     414             :                                             WAIT_EVENT_PROC_SIGNAL_BARRIER))
     415           0 :                 ereport(LOG,
     416             :                         (errmsg("still waiting for backend with PID %d to accept ProcSignalBarrier",
     417             :                                 (int) slot->pss_pid)));
     418         410 :             oldval = pg_atomic_read_u64(&slot->pss_barrierGeneration);
     419             :         }
     420        9740 :         ConditionVariableCancelSleep();
     421             :     }
     422             : 
     423         142 :     elog(DEBUG1,
     424             :          "finished waiting for all backends to process ProcSignalBarrier generation "
     425             :          UINT64_FORMAT,
     426             :          generation);
     427             : 
     428             :     /*
     429             :      * The caller is probably calling this function because it wants to read
     430             :      * the shared state or perform further writes to shared state once all
     431             :      * backends are known to have absorbed the barrier. However, the read of
     432             :      * pss_barrierGeneration was performed unlocked; insert a memory barrier
     433             :      * to separate it from whatever follows.
     434             :      */
     435         142 :     pg_memory_barrier();
     436         142 : }
     437             : 
     438             : /*
     439             :  * Handle receipt of an interrupt indicating a global barrier event.
     440             :  *
     441             :  * All the actual work is deferred to ProcessProcSignalBarrier(), because we
     442             :  * cannot safely access the barrier generation inside the signal handler as
     443             :  * 64bit atomics might use spinlock based emulation, even for reads. As this
     444             :  * routine only gets called when PROCSIG_BARRIER is sent that won't cause a
     445             :  * lot of unnecessary work.
     446             :  */
     447             : static void
     448         570 : HandleProcSignalBarrierInterrupt(void)
     449             : {
     450         570 :     InterruptPending = true;
     451         570 :     ProcSignalBarrierPending = true;
     452             :     /* latch will be set by procsignal_sigusr1_handler */
     453         570 : }
     454             : 
     455             : /*
     456             :  * Perform global barrier related interrupt checking.
     457             :  *
     458             :  * Any backend that participates in ProcSignal signaling must arrange to
     459             :  * call this function periodically. It is called from CHECK_FOR_INTERRUPTS(),
     460             :  * which is enough for normal backends, but not necessarily for all types of
     461             :  * background processes.
     462             :  */
     463             : void
     464         570 : ProcessProcSignalBarrier(void)
     465             : {
     466             :     uint64      local_gen;
     467             :     uint64      shared_gen;
     468             :     volatile uint32 flags;
     469             : 
     470             :     Assert(MyProcSignalSlot);
     471             : 
     472             :     /* Exit quickly if there's no work to do. */
     473         570 :     if (!ProcSignalBarrierPending)
     474           0 :         return;
     475         570 :     ProcSignalBarrierPending = false;
     476             : 
     477             :     /*
     478             :      * It's not unlikely to process multiple barriers at once, before the
     479             :      * signals for all the barriers have arrived. To avoid unnecessary work in
     480             :      * response to subsequent signals, exit early if we already have processed
     481             :      * all of them.
     482             :      */
     483         570 :     local_gen = pg_atomic_read_u64(&MyProcSignalSlot->pss_barrierGeneration);
     484         570 :     shared_gen = pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration);
     485             : 
     486             :     Assert(local_gen <= shared_gen);
     487             : 
     488         570 :     if (local_gen == shared_gen)
     489           0 :         return;
     490             : 
     491             :     /*
     492             :      * Get and clear the flags that are set for this backend. Note that
     493             :      * pg_atomic_exchange_u32 is a full barrier, so we're guaranteed that the
     494             :      * read of the barrier generation above happens before we atomically
     495             :      * extract the flags, and that any subsequent state changes happen
     496             :      * afterward.
     497             :      *
     498             :      * NB: In order to avoid race conditions, we must zero
     499             :      * pss_barrierCheckMask first and only afterwards try to do barrier
     500             :      * processing. If we did it in the other order, someone could send us
     501             :      * another barrier of some type right after we called the
     502             :      * barrier-processing function but before we cleared the bit. We would
     503             :      * have no way of knowing that the bit needs to stay set in that case, so
     504             :      * the need to call the barrier-processing function again would just get
     505             :      * forgotten. So instead, we tentatively clear all the bits and then put
     506             :      * back any for which we don't manage to successfully absorb the barrier.
     507             :      */
     508         570 :     flags = pg_atomic_exchange_u32(&MyProcSignalSlot->pss_barrierCheckMask, 0);
     509             : 
     510             :     /*
     511             :      * If there are no flags set, then we can skip doing any real work.
     512             :      * Otherwise, establish a PG_TRY block, so that we don't lose track of
     513             :      * which types of barrier processing are needed if an ERROR occurs.
     514             :      */
     515         570 :     if (flags != 0)
     516             :     {
     517         570 :         bool        success = true;
     518             : 
     519         570 :         PG_TRY();
     520             :         {
     521             :             /*
     522             :              * Process each type of barrier. The barrier-processing functions
     523             :              * should normally return true, but may return false if the
     524             :              * barrier can't be absorbed at the current time. This should be
     525             :              * rare, because it's pretty expensive.  Every single
     526             :              * CHECK_FOR_INTERRUPTS() will return here until we manage to
     527             :              * absorb the barrier, and that cost will add up in a hurry.
     528             :              *
     529             :              * NB: It ought to be OK to call the barrier-processing functions
     530             :              * unconditionally, but it's more efficient to call only the ones
     531             :              * that might need us to do something based on the flags.
     532             :              */
     533        1140 :             while (flags != 0)
     534             :             {
     535             :                 ProcSignalBarrierType type;
     536         570 :                 bool        processed = true;
     537             : 
     538         570 :                 type = (ProcSignalBarrierType) pg_rightmost_one_pos32(flags);
     539         570 :                 switch (type)
     540             :                 {
     541         570 :                     case PROCSIGNAL_BARRIER_SMGRRELEASE:
     542         570 :                         processed = ProcessBarrierSmgrRelease();
     543         570 :                         break;
     544             :                 }
     545             : 
     546             :                 /*
     547             :                  * To avoid an infinite loop, we must always unset the bit in
     548             :                  * flags.
     549             :                  */
     550         570 :                 BARRIER_CLEAR_BIT(flags, type);
     551             : 
     552             :                 /*
     553             :                  * If we failed to process the barrier, reset the shared bit
     554             :                  * so we try again later, and set a flag so that we don't bump
     555             :                  * our generation.
     556             :                  */
     557         570 :                 if (!processed)
     558             :                 {
     559           0 :                     ResetProcSignalBarrierBits(((uint32) 1) << type);
     560           0 :                     success = false;
     561             :                 }
     562             :             }
     563             :         }
     564           0 :         PG_CATCH();
     565             :         {
     566             :             /*
     567             :              * If an ERROR occurred, we'll need to try again later to handle
     568             :              * that barrier type and any others that haven't been handled yet
     569             :              * or weren't successfully absorbed.
     570             :              */
     571           0 :             ResetProcSignalBarrierBits(flags);
     572           0 :             PG_RE_THROW();
     573             :         }
     574         570 :         PG_END_TRY();
     575             : 
     576             :         /*
     577             :          * If some barrier types were not successfully absorbed, we will have
     578             :          * to try again later.
     579             :          */
     580         570 :         if (!success)
     581           0 :             return;
     582             :     }
     583             : 
     584             :     /*
     585             :      * State changes related to all types of barriers that might have been
     586             :      * emitted have now been handled, so we can update our notion of the
     587             :      * generation to the one we observed before beginning the updates. If
     588             :      * things have changed further, it'll get fixed up when this function is
     589             :      * next called.
     590             :      */
     591         570 :     pg_atomic_write_u64(&MyProcSignalSlot->pss_barrierGeneration, shared_gen);
     592         570 :     ConditionVariableBroadcast(&MyProcSignalSlot->pss_barrierCV);
     593             : }
     594             : 
     595             : /*
     596             :  * If it turns out that we couldn't absorb one or more barrier types, either
     597             :  * because the barrier-processing functions returned false or due to an error,
     598             :  * arrange for processing to be retried later.
     599             :  */
     600             : static void
     601           0 : ResetProcSignalBarrierBits(uint32 flags)
     602             : {
     603           0 :     pg_atomic_fetch_or_u32(&MyProcSignalSlot->pss_barrierCheckMask, flags);
     604           0 :     ProcSignalBarrierPending = true;
     605           0 :     InterruptPending = true;
     606           0 : }
     607             : 
     608             : /*
     609             :  * CheckProcSignal - check to see if a particular reason has been
     610             :  * signaled, and clear the signal flag.  Should be called after receiving
     611             :  * SIGUSR1.
     612             :  */
     613             : static bool
     614      251104 : CheckProcSignal(ProcSignalReason reason)
     615             : {
     616      251104 :     volatile ProcSignalSlot *slot = MyProcSignalSlot;
     617             : 
     618      251104 :     if (slot != NULL)
     619             :     {
     620             :         /* Careful here --- don't clear flag if we haven't seen it set */
     621      251048 :         if (slot->pss_signalFlags[reason])
     622             :         {
     623        9260 :             slot->pss_signalFlags[reason] = false;
     624        9260 :             return true;
     625             :         }
     626             :     }
     627             : 
     628      241844 :     return false;
     629             : }
     630             : 
     631             : /*
     632             :  * procsignal_sigusr1_handler - handle SIGUSR1 signal.
     633             :  */
     634             : void
     635       17936 : procsignal_sigusr1_handler(SIGNAL_ARGS)
     636             : {
     637       17936 :     if (CheckProcSignal(PROCSIG_CATCHUP_INTERRUPT))
     638        5156 :         HandleCatchupInterrupt();
     639             : 
     640       17936 :     if (CheckProcSignal(PROCSIG_NOTIFY_INTERRUPT))
     641          40 :         HandleNotifyInterrupt();
     642             : 
     643       17936 :     if (CheckProcSignal(PROCSIG_PARALLEL_MESSAGE))
     644        3350 :         HandleParallelMessageInterrupt();
     645             : 
     646       17936 :     if (CheckProcSignal(PROCSIG_WALSND_INIT_STOPPING))
     647          60 :         HandleWalSndInitStopping();
     648             : 
     649       17936 :     if (CheckProcSignal(PROCSIG_BARRIER))
     650         570 :         HandleProcSignalBarrierInterrupt();
     651             : 
     652       17936 :     if (CheckProcSignal(PROCSIG_LOG_MEMORY_CONTEXT))
     653          18 :         HandleLogMemoryContextInterrupt();
     654             : 
     655       17936 :     if (CheckProcSignal(PROCSIG_PARALLEL_APPLY_MESSAGE))
     656          28 :         HandleParallelApplyMessageInterrupt();
     657             : 
     658       17936 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_DATABASE))
     659           4 :         HandleRecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_DATABASE);
     660             : 
     661       17936 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_TABLESPACE))
     662           2 :         HandleRecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_TABLESPACE);
     663             : 
     664       17936 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_LOCK))
     665           2 :         HandleRecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_LOCK);
     666             : 
     667       17936 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_SNAPSHOT))
     668           2 :         HandleRecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_SNAPSHOT);
     669             : 
     670       17936 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT))
     671          10 :         HandleRecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT);
     672             : 
     673       17936 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK))
     674          16 :         HandleRecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK);
     675             : 
     676       17936 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN))
     677           2 :         HandleRecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
     678             : 
     679       17936 :     SetLatch(MyLatch);
     680       17936 : }

Generated by: LCOV version 1.14