LCOV - differential code coverage report
Current view: top level - src/backend/storage/ipc - procsignal.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 87.3 % 165 144 2 19 17 127 5 14
Current Date: 2024-04-14 14:21:10 Functions: 91.7 % 12 11 1 4 7 2
Baseline: 16@8cea358b128 Branches: 61.3 % 124 76 7 41 5 5 66
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 83.3 % 12 10 2 10
(180,240] days: 100.0 % 7 7 7
(240..) days: 87.0 % 146 127 19 127
Function coverage date bins:
[..60] days: 100.0 % 2 2 2
(240..) days: 90.0 % 10 9 1 2 7
Branch coverage date bins:
[..60] days: 41.7 % 12 5 7 5
(240..) days: 63.4 % 112 71 41 5 66

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

Generated by: LCOV version 2.1-beta2-3-g6141622