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 15:15:32 Functions: 91.7 % 12 11 1 11 1 11
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           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
     112 GIC        4564 : ProcSignalShmemSize(void)
     113 ECB             : {
     114                 :     Size        size;
     115                 : 
     116 GIC        4564 :     size = mul_size(NumProcSignalSlots, sizeof(ProcSignalSlot));
     117 CBC        4564 :     size = add_size(size, offsetof(ProcSignalHeader, psh_slot));
     118            4564 :     return size;
     119 ECB             : }
     120                 : 
     121                 : /*
     122                 :  * ProcSignalShmemInit
     123                 :  *      Allocate and initialize ProcSignal's shared memory
     124                 :  */
     125                 : void
     126 GIC        1826 : ProcSignalShmemInit(void)
     127 ECB             : {
     128 GIC        1826 :     Size        size = ProcSignalShmemSize();
     129 ECB             :     bool        found;
     130                 : 
     131 GIC        1826 :     ProcSignal = (ProcSignalHeader *)
     132 CBC        1826 :         ShmemInitStruct("ProcSignal", size, &found);
     133 ECB             : 
     134                 :     /* If we're first, initialize. */
     135 GIC        1826 :     if (!found)
     136 ECB             :     {
     137                 :         int         i;
     138                 : 
     139 GIC        1826 :         pg_atomic_init_u64(&ProcSignal->psh_barrierGeneration, 0);
     140 ECB             : 
     141 GIC      204159 :         for (i = 0; i < NumProcSignalSlots; ++i)
     142 ECB             :         {
     143 GIC      202333 :             ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
     144 ECB             : 
     145 GIC      202333 :             slot->pss_pid = 0;
     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);
     149          202333 :             ConditionVariableInit(&slot->pss_barrierCV);
     150 ECB             :         }
     151                 :     }
     152 GIC        1826 : }
     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
     162 GIC       13282 : ProcSignalInit(int pss_idx)
     163 ECB             : {
     164                 :     ProcSignalSlot *slot;
     165                 :     uint64      barrier_generation;
     166                 : 
     167 GIC       13282 :     Assert(pss_idx >= 1 && pss_idx <= NumProcSignalSlots);
     168 ECB             : 
     169 GIC       13282 :     slot = &ProcSignal->psh_slot[pss_idx - 1];
     170 ECB             : 
     171                 :     /* sanity check */
     172 GIC       13282 :     if (slot->pss_pid != 0)
     173 LBC           0 :         elog(LOG, "process %d taking over ProcSignal slot %d, but it's not empty",
     174 EUB             :              MyProcPid, pss_idx);
     175                 : 
     176                 :     /* Clear out any leftover signal reasons */
     177 GIC       13282 :     MemSet(slot->pss_signalFlags, 0, NUM_PROCSIGNALS * sizeof(sig_atomic_t));
     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                 :      */
     190 GIC       13282 :     pg_atomic_write_u32(&slot->pss_barrierCheckMask, 0);
     191 ECB             :     barrier_generation =
     192 GIC       13282 :         pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration);
     193 CBC       13282 :     pg_atomic_write_u64(&slot->pss_barrierGeneration, barrier_generation);
     194           13282 :     pg_memory_barrier();
     195 ECB             : 
     196                 :     /* Mark slot with my PID */
     197 GIC       13282 :     slot->pss_pid = MyProcPid;
     198 ECB             : 
     199                 :     /* Remember slot location for CheckProcSignal */
     200 GIC       13282 :     MyProcSignalSlot = slot;
     201 ECB             : 
     202                 :     /* Set up to release the slot on process exit */
     203 GIC       13282 :     on_shmem_exit(CleanupProcSignalState, Int32GetDatum(pss_idx));
     204 CBC       13282 : }
     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
     213 GIC       13282 : CleanupProcSignalState(int status, Datum arg)
     214 ECB             : {
     215 GIC       13282 :     int         pss_idx = DatumGetInt32(arg);
     216 ECB             :     ProcSignalSlot *slot;
     217                 : 
     218 GIC       13282 :     slot = &ProcSignal->psh_slot[pss_idx - 1];
     219 CBC       13282 :     Assert(slot == MyProcSignalSlot);
     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                 :      */
     226 GIC       13282 :     MyProcSignalSlot = NULL;
     227 ECB             : 
     228                 :     /* sanity check */
     229 GIC       13282 :     if (slot->pss_pid != MyProcPid)
     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                 :          */
     235 UIC           0 :         elog(LOG, "process %d releasing ProcSignal slot %d, but it contains %d",
     236 EUB             :              MyProcPid, pss_idx, (int) slot->pss_pid);
     237 UIC           0 :         return;                 /* XXX better to zero the slot anyway? */
     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                 :      */
     244 GIC       13282 :     pg_atomic_write_u64(&slot->pss_barrierGeneration, PG_UINT64_MAX);
     245 CBC       13282 :     ConditionVariableBroadcast(&slot->pss_barrierCV);
     246 ECB             : 
     247 GIC       13282 :     slot->pss_pid = 0;
     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
     262 GIC        6650 : SendProcSignal(pid_t pid, ProcSignalReason reason, BackendId backendId)
     263 ECB             : {
     264                 :     volatile ProcSignalSlot *slot;
     265                 : 
     266 GIC        6650 :     if (backendId != InvalidBackendId)
     267 ECB             :     {
     268 GIC        6600 :         slot = &ProcSignal->psh_slot[backendId - 1];
     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                 :          */
     278 GIC        6600 :         if (slot->pss_pid == pid)
     279 ECB             :         {
     280                 :             /* Atomically set the proper flag */
     281 GIC        6600 :             slot->pss_signalFlags[reason] = true;
     282 ECB             :             /* Send signal */
     283 GIC        6600 :             return kill(pid, SIGUSR1);
     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                 : 
     296 GIC        1654 :         for (i = NumProcSignalSlots - 1; i >= 0; i--)
     297 ECB             :         {
     298 GIC        1644 :             slot = &ProcSignal->psh_slot[i];
     299 ECB             : 
     300 GIC        1644 :             if (slot->pss_pid == pid)
     301 ECB             :             {
     302                 :                 /* the above note about race conditions applies here too */
     303                 : 
     304                 :                 /* Atomically set the proper flag */
     305 GIC          40 :                 slot->pss_signalFlags[reason] = true;
     306 ECB             :                 /* Send signal */
     307 GIC          40 :                 return kill(pid, SIGUSR1);
     308 ECB             :             }
     309                 :         }
     310                 :     }
     311                 : 
     312 GIC          10 :     errno = ESRCH;
     313 CBC          10 :     return -1;
     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
     333 GIC          56 : EmitProcSignalBarrier(ProcSignalBarrierType type)
     334 ECB             : {
     335 GIC          56 :     uint32      flagbit = 1 << (uint32) type;
     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                 :      */
     346 GIC        3706 :     for (int i = 0; i < NumProcSignalSlots; i++)
     347 ECB             :     {
     348 GIC        3650 :         volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
     349 ECB             : 
     350 GIC        3650 :         pg_atomic_fetch_or_u32(&slot->pss_barrierCheckMask, flagbit);
     351 ECB             :     }
     352                 : 
     353                 :     /*
     354                 :      * Increment the generation counter.
     355                 :      */
     356                 :     generation =
     357 GIC          56 :         pg_atomic_add_fetch_u64(&ProcSignal->psh_barrierGeneration, 1);
     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                 :      */
     372 GIC        3706 :     for (int i = NumProcSignalSlots - 1; i >= 0; i--)
     373 ECB             :     {
     374 GIC        3650 :         volatile ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
     375 CBC        3650 :         pid_t       pid = slot->pss_pid;
     376 ECB             : 
     377 GIC        3650 :         if (pid != 0)
     378 ECB             :         {
     379                 :             /* see SendProcSignal for details */
     380 GIC         333 :             slot->pss_signalFlags[PROCSIG_BARRIER] = true;
     381 CBC         333 :             kill(pid, SIGUSR1);
     382 ECB             :         }
     383                 :     }
     384                 : 
     385 GIC          56 :     return generation;
     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
     393 GIC          56 : WaitForProcSignalBarrier(uint64 generation)
     394 ECB             : {
     395 GIC          56 :     Assert(generation <= pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration));
     396 ECB             : 
     397 GIC          56 :     elog(DEBUG1,
     398 ECB             :          "waiting for all backends to process ProcSignalBarrier generation "
     399                 :          UINT64_FORMAT,
     400                 :          generation);
     401                 : 
     402 GIC        3706 :     for (int i = NumProcSignalSlots - 1; i >= 0; i--)
     403 ECB             :     {
     404 GIC        3650 :         ProcSignalSlot *slot = &ProcSignal->psh_slot[i];
     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                 :          */
     413 GIC        3650 :         oldval = pg_atomic_read_u64(&slot->pss_barrierGeneration);
     414 CBC        3827 :         while (oldval < generation)
     415 ECB             :         {
     416 GIC         177 :             if (ConditionVariableTimedSleep(&slot->pss_barrierCV,
     417 ECB             :                                             5000,
     418                 :                                             WAIT_EVENT_PROC_SIGNAL_BARRIER))
     419 UIC           0 :                 ereport(LOG,
     420                 :                         (errmsg("still waiting for backend with PID %d to accept ProcSignalBarrier",
     421                 :                                 (int) slot->pss_pid)));
     422 GIC         177 :             oldval = pg_atomic_read_u64(&slot->pss_barrierGeneration);
     423 ECB             :         }
     424 GIC        3650 :         ConditionVariableCancelSleep();
     425 ECB             :     }
     426                 : 
     427 GIC          56 :     elog(DEBUG1,
     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                 :      */
     439 GIC          56 :     pg_memory_barrier();
     440 CBC          56 : }
     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
     452 GIC         248 : HandleProcSignalBarrierInterrupt(void)
     453 ECB             : {
     454 GIC         248 :     InterruptPending = true;
     455 CBC         248 :     ProcSignalBarrierPending = true;
     456 ECB             :     /* latch will be set by procsignal_sigusr1_handler */
     457 GIC         248 : }
     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
     468 GIC         248 : ProcessProcSignalBarrier(void)
     469 ECB             : {
     470                 :     uint64      local_gen;
     471                 :     uint64      shared_gen;
     472                 :     volatile uint32 flags;
     473                 : 
     474 GIC         248 :     Assert(MyProcSignalSlot);
     475 ECB             : 
     476                 :     /* Exit quickly if there's no work to do. */
     477 GIC         248 :     if (!ProcSignalBarrierPending)
     478 LBC           0 :         return;
     479 GBC         248 :     ProcSignalBarrierPending = false;
     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                 :      */
     487 GIC         248 :     local_gen = pg_atomic_read_u64(&MyProcSignalSlot->pss_barrierGeneration);
     488 CBC         248 :     shared_gen = pg_atomic_read_u64(&ProcSignal->psh_barrierGeneration);
     489 ECB             : 
     490 GIC         248 :     Assert(local_gen <= shared_gen);
     491 ECB             : 
     492 GIC         248 :     if (local_gen == shared_gen)
     493 LBC           0 :         return;
     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                 :      */
     512 GIC         248 :     flags = pg_atomic_exchange_u32(&MyProcSignalSlot->pss_barrierCheckMask, 0);
     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                 :      */
     519 GIC         248 :     if (flags != 0)
     520 ECB             :     {
     521 GIC         248 :         bool        success = true;
     522 ECB             : 
     523 GIC         248 :         PG_TRY();
     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                 :              */
     537 GIC         744 :             while (flags != 0)
     538 ECB             :             {
     539                 :                 ProcSignalBarrierType type;
     540 GIC         248 :                 bool        processed = true;
     541 ECB             : 
     542 GIC         248 :                 type = (ProcSignalBarrierType) pg_rightmost_one_pos32(flags);
     543 CBC         248 :                 switch (type)
     544 ECB             :                 {
     545 GIC         248 :                     case PROCSIGNAL_BARRIER_SMGRRELEASE:
     546 CBC         248 :                         processed = ProcessBarrierSmgrRelease();
     547             248 :                         break;
     548 ECB             :                 }
     549                 : 
     550                 :                 /*
     551                 :                  * To avoid an infinite loop, we must always unset the bit in
     552                 :                  * flags.
     553                 :                  */
     554 GIC         248 :                 BARRIER_CLEAR_BIT(flags, type);
     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                 :                  */
     561 GIC         248 :                 if (!processed)
     562 ECB             :                 {
     563 UIC           0 :                     ResetProcSignalBarrierBits(((uint32) 1) << type);
     564 UBC           0 :                     success = false;
     565 EUB             :                 }
     566                 :             }
     567                 :         }
     568 UIC           0 :         PG_CATCH();
     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                 :              */
     575 UIC           0 :             ResetProcSignalBarrierBits(flags);
     576 UBC           0 :             PG_RE_THROW();
     577 EUB             :         }
     578 GIC         248 :         PG_END_TRY();
     579 ECB             : 
     580                 :         /*
     581                 :          * If some barrier types were not successfully absorbed, we will have
     582                 :          * to try again later.
     583                 :          */
     584 GIC         248 :         if (!success)
     585 LBC           0 :             return;
     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                 :      */
     595 GIC         248 :     pg_atomic_write_u64(&MyProcSignalSlot->pss_barrierGeneration, shared_gen);
     596 CBC         248 :     ConditionVariableBroadcast(&MyProcSignalSlot->pss_barrierCV);
     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
     605 UIC           0 : ResetProcSignalBarrierBits(uint32 flags)
     606 EUB             : {
     607 UIC           0 :     pg_atomic_fetch_or_u32(&MyProcSignalSlot->pss_barrierCheckMask, flags);
     608 UBC           0 :     ProcSignalBarrierPending = true;
     609               0 :     InterruptPending = true;
     610               0 : }
     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
     618 GIC      135394 : CheckProcSignal(ProcSignalReason reason)
     619 ECB             : {
     620 GIC      135394 :     volatile ProcSignalSlot *slot = MyProcSignalSlot;
     621 ECB             : 
     622 GIC      135394 :     if (slot != NULL)
     623 ECB             :     {
     624                 :         /* Careful here --- don't clear flag if we haven't seen it set */
     625 GIC      135296 :         if (slot->pss_signalFlags[reason])
     626 ECB             :         {
     627 GIC        5690 :             slot->pss_signalFlags[reason] = false;
     628 CBC        5690 :             return true;
     629 ECB             :         }
     630                 :     }
     631                 : 
     632 GIC      129704 :     return false;
     633 ECB             : }
     634                 : 
     635                 : /*
     636                 :  * procsignal_sigusr1_handler - handle SIGUSR1 signal.
     637                 :  */
     638                 : void
     639 GIC        9671 : procsignal_sigusr1_handler(SIGNAL_ARGS)
     640 ECB             : {
     641 GIC        9671 :     int         save_errno = errno;
     642 ECB             : 
     643 GIC        9671 :     if (CheckProcSignal(PROCSIG_CATCHUP_INTERRUPT))
     644 CBC        2651 :         HandleCatchupInterrupt();
     645 ECB             : 
     646 GIC        9671 :     if (CheckProcSignal(PROCSIG_NOTIFY_INTERRUPT))
     647 CBC          20 :         HandleNotifyInterrupt();
     648 ECB             : 
     649 GIC        9671 :     if (CheckProcSignal(PROCSIG_PARALLEL_MESSAGE))
     650 CBC        2711 :         HandleParallelMessageInterrupt();
     651 ECB             : 
     652 GIC        9671 :     if (CheckProcSignal(PROCSIG_WALSND_INIT_STOPPING))
     653 CBC          27 :         HandleWalSndInitStopping();
     654 ECB             : 
     655 GIC        9671 :     if (CheckProcSignal(PROCSIG_BARRIER))
     656 CBC         248 :         HandleProcSignalBarrierInterrupt();
     657 ECB             : 
     658 GIC        9671 :     if (CheckProcSignal(PROCSIG_LOG_MEMORY_CONTEXT))
     659 CBC           9 :         HandleLogMemoryContextInterrupt();
     660 ECB             : 
     661 GNC        9671 :     if (CheckProcSignal(PROCSIG_PARALLEL_APPLY_MESSAGE))
     662               5 :         HandleParallelApplyMessageInterrupt();
     663                 : 
     664 GIC        9671 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_DATABASE))
     665 CBC           2 :         RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_DATABASE);
     666 ECB             : 
     667 GIC        9671 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_TABLESPACE))
     668 CBC           1 :         RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_TABLESPACE);
     669 ECB             : 
     670 GIC        9671 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_LOCK))
     671 CBC           1 :         RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_LOCK);
     672 ECB             : 
     673 GIC        9671 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_SNAPSHOT))
     674 CBC           1 :         RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_SNAPSHOT);
     675 ECB             : 
     676 GNC        9671 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT))
     677               5 :         RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_LOGICALSLOT);
     678                 : 
     679 GIC        9671 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK))
     680 CBC           8 :         RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK);
     681 ECB             : 
     682 GIC        9671 :     if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN))
     683 CBC           1 :         RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
     684 ECB             : 
     685 GIC        9671 :     SetLatch(MyLatch);
     686 ECB             : 
     687 CBC        9671 :     errno = save_errno;
     688 GIC        9671 : }
        

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