LCOV - code coverage report
Current view: top level - src/backend/storage/ipc - procsignal.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 84.6 % 201 170
Test Date: 2026-07-22 20:15:44 Functions: 92.3 % 13 12
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 64.7 % 136 88

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

Generated by: LCOV version 2.0-1