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