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