LCOV - differential code coverage report
Current view: top level - src/backend/storage/ipc - procsignal.c (source / functions) Coverage Total Hit LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB
Current: Differential Code Coverage HEAD vs 15 Lines: 89.7 % 165 148 4 8 5 1 106 4 37 10 107 1
Current Date: 2023-04-08 17:13:01 Functions: 91.7 % 12 11 1 11 1 11
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 [..60] days: 100.0 % 2 2 2
Legend: Lines: hit not hit (60,120] days: 100.0 % 2 2 2
(240..) days: 89.4 % 161 144 4 8 5 1 106 37 10 102
Function coverage date bins:
(240..) days: 45.8 % 24 11 1 11 1 11

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

Generated by: LCOV version v1.16-55-g56c0a2a