LCOV - code coverage report
Current view: top level - src/backend/storage/ipc - procsignal.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 71.4 % 255 182
Test Date: 2026-01-26 10:56:24 Functions: 84.6 % 13 11
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 42.1 % 202 85

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

Generated by: LCOV version 2.3.2-1