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