|
| 1 | +diff --git a/src/backend/storage/ipc/procsignal.c b/src/backend/storage/ipc/procsignal.c |
| 2 | +index 4ed9ced..6e70892 100644 |
| 3 | +--- a/src/backend/storage/ipc/procsignal.c |
| 4 | ++++ b/src/backend/storage/ipc/procsignal.c |
| 5 | +@@ -6,6 +6,7 @@ |
| 6 | + * |
| 7 | + * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group |
| 8 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 9 | ++ * Portions Copyright (c) 2024, Postgres Professional |
| 10 | + * |
| 11 | + * IDENTIFICATION |
| 12 | + * src/backend/storage/ipc/procsignal.c |
| 13 | +@@ -96,6 +97,13 @@ typedef struct |
| 14 | + #define BARRIER_CLEAR_BIT(flags, type) \ |
| 15 | + ((flags) &= ~(((uint32) 1) << (uint32) (type))) |
| 16 | + |
| 17 | ++#define IsCustomProcSignalReason(reason) \ |
| 18 | ++ ((reason) >= PROCSIG_CUSTOM_1 && (reason) <= PROCSIG_CUSTOM_N) |
| 19 | ++ |
| 20 | ++static bool CustomSignalPendings[NUM_CUSTOM_PROCSIGNALS]; |
| 21 | ++static bool CustomSignalProcessing[NUM_CUSTOM_PROCSIGNALS]; |
| 22 | ++static ProcSignalHandler_type CustomInterruptHandlers[NUM_CUSTOM_PROCSIGNALS]; |
| 23 | ++ |
| 24 | + static ProcSignalHeader *ProcSignal = NULL; |
| 25 | + static ProcSignalSlot *MyProcSignalSlot = NULL; |
| 26 | + |
| 27 | +@@ -103,6 +111,8 @@ static bool CheckProcSignal(ProcSignalReason reason); |
| 28 | + static void CleanupProcSignalState(int status, Datum arg); |
| 29 | + static void ResetProcSignalBarrierBits(uint32 flags); |
| 30 | + |
| 31 | ++static void CheckAndSetCustomSignalInterrupts(void); |
| 32 | ++ |
| 33 | + /* |
| 34 | + * ProcSignalShmemSize |
| 35 | + * Compute space needed for ProcSignal's shared memory |
| 36 | +@@ -242,6 +252,36 @@ CleanupProcSignalState(int status, Datum arg) |
| 37 | + slot->pss_pid = 0; |
| 38 | + } |
| 39 | + |
| 40 | ++/* |
| 41 | ++ * RegisterCustomProcSignalHandler |
| 42 | ++ * Assign specific handler of custom process signal with new |
| 43 | ++ * ProcSignalReason key. |
| 44 | ++ * |
| 45 | ++ * This function has to be called in _PG_init function of extensions at the |
| 46 | ++ * stage of loading shared preloaded libraries. Otherwise it throws fatal error. |
| 47 | ++ * |
| 48 | ++ * Return INVALID_PROCSIGNAL if all slots for custom signals are occupied. |
| 49 | ++ */ |
| 50 | ++ProcSignalReason |
| 51 | ++RegisterCustomProcSignalHandler(ProcSignalHandler_type handler) |
| 52 | ++{ |
| 53 | ++ ProcSignalReason reason; |
| 54 | ++ |
| 55 | ++ if (!process_shared_preload_libraries_in_progress) |
| 56 | ++ ereport(FATAL, (errcode(ERRCODE_INTERNAL_ERROR), |
| 57 | ++ errmsg("cannot register custom signal after startup"))); |
| 58 | ++ |
| 59 | ++ /* Iterate through custom signal slots to find a free one */ |
| 60 | ++ for (reason = PROCSIG_CUSTOM_1; reason <= PROCSIG_CUSTOM_N; reason++) |
| 61 | ++ if (!CustomInterruptHandlers[reason - PROCSIG_CUSTOM_1]) |
| 62 | ++ { |
| 63 | ++ CustomInterruptHandlers[reason - PROCSIG_CUSTOM_1] = handler; |
| 64 | ++ return reason; |
| 65 | ++ } |
| 66 | ++ |
| 67 | ++ return INVALID_PROCSIGNAL; |
| 68 | ++} |
| 69 | ++ |
| 70 | + /* |
| 71 | + * SendProcSignal |
| 72 | + * Send a signal to a Postgres process |
| 73 | +@@ -676,5 +716,70 @@ procsignal_sigusr1_handler(SIGNAL_ARGS) |
| 74 | + if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN)) |
| 75 | + HandleRecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN); |
| 76 | + |
| 77 | ++ CheckAndSetCustomSignalInterrupts(); |
| 78 | ++ |
| 79 | + SetLatch(MyLatch); |
| 80 | + } |
| 81 | ++ |
| 82 | ++/* |
| 83 | ++ * Handle receipt of an interrupt indicating any of custom process signals. |
| 84 | ++ */ |
| 85 | ++static void |
| 86 | ++CheckAndSetCustomSignalInterrupts() |
| 87 | ++{ |
| 88 | ++ ProcSignalReason reason; |
| 89 | ++ |
| 90 | ++ for (reason = PROCSIG_CUSTOM_1; reason <= PROCSIG_CUSTOM_N; reason++) |
| 91 | ++ { |
| 92 | ++ if (CheckProcSignal(reason)) |
| 93 | ++ { |
| 94 | ++ |
| 95 | ++ /* set interrupt flags */ |
| 96 | ++ InterruptPending = true; |
| 97 | ++ CustomSignalPendings[reason - PROCSIG_CUSTOM_1] = true; |
| 98 | ++ } |
| 99 | ++ } |
| 100 | ++ |
| 101 | ++ SetLatch(MyLatch); |
| 102 | ++} |
| 103 | ++ |
| 104 | ++/* |
| 105 | ++ * CheckAndHandleCustomSignals |
| 106 | ++ * Check custom signal flags and call handler assigned to that signal |
| 107 | ++ * if it is not NULL |
| 108 | ++ * |
| 109 | ++ * This function is called within CHECK_FOR_INTERRUPTS if interrupt occurred. |
| 110 | ++ */ |
| 111 | ++void |
| 112 | ++CheckAndHandleCustomSignals(void) |
| 113 | ++{ |
| 114 | ++ int i; |
| 115 | ++ |
| 116 | ++ /* |
| 117 | ++ * This is invoked from ProcessInterrupts(), and since some of the |
| 118 | ++ * functions it calls contain CHECK_FOR_INTERRUPTS(), there is a potential |
| 119 | ++ * for recursive calls if more signals are received while this runs, so |
| 120 | ++ * let's block interrupts until done. |
| 121 | ++ */ |
| 122 | ++ HOLD_INTERRUPTS(); |
| 123 | ++ |
| 124 | ++ /* Check on expiring of custom signals and call its handlers if exist */ |
| 125 | ++ for (i = 0; i < NUM_CUSTOM_PROCSIGNALS; i++) |
| 126 | ++ { |
| 127 | ++ if (!CustomSignalProcessing[i] && CustomSignalPendings[i]) |
| 128 | ++ { |
| 129 | ++ ProcSignalHandler_type handler; |
| 130 | ++ |
| 131 | ++ CustomSignalPendings[i] = false; |
| 132 | ++ handler = CustomInterruptHandlers[i]; |
| 133 | ++ if (handler != NULL) |
| 134 | ++ { |
| 135 | ++ CustomSignalProcessing[i] = true; |
| 136 | ++ handler(); |
| 137 | ++ CustomSignalProcessing[i] = false; |
| 138 | ++ } |
| 139 | ++ } |
| 140 | ++ } |
| 141 | ++ |
| 142 | ++ RESUME_INTERRUPTS(); |
| 143 | ++} |
| 144 | +diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c |
| 145 | +index a750dc8..e1b0be5 100644 |
| 146 | +--- a/src/backend/tcop/postgres.c |
| 147 | ++++ b/src/backend/tcop/postgres.c |
| 148 | +@@ -3492,6 +3492,8 @@ ProcessInterrupts(void) |
| 149 | + if (ParallelMessagePending) |
| 150 | + HandleParallelMessages(); |
| 151 | + |
| 152 | ++ CheckAndHandleCustomSignals(); |
| 153 | ++ |
| 154 | + if (LogMemoryContextPending) |
| 155 | + ProcessLogMemoryContextInterrupt(); |
| 156 | + |
| 157 | +diff --git a/src/include/storage/procsignal.h b/src/include/storage/procsignal.h |
| 158 | +index 7d290ea..f262f0c 100644 |
| 159 | +--- a/src/include/storage/procsignal.h |
| 160 | ++++ b/src/include/storage/procsignal.h |
| 161 | +@@ -6,6 +6,7 @@ |
| 162 | + * |
| 163 | + * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group |
| 164 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 165 | ++ * Portions Copyright (c) 2024, Postgres Professional |
| 166 | + * |
| 167 | + * src/include/storage/procsignal.h |
| 168 | + * |
| 169 | +@@ -17,6 +18,8 @@ |
| 170 | + #include "storage/procnumber.h" |
| 171 | + |
| 172 | + |
| 173 | ++#define NUM_CUSTOM_PROCSIGNALS 64 |
| 174 | ++ |
| 175 | + /* |
| 176 | + * Reasons for signaling a Postgres child process (a backend or an auxiliary |
| 177 | + * process, like checkpointer). We can cope with concurrent signals for different |
| 178 | +@@ -29,6 +32,8 @@ |
| 179 | + */ |
| 180 | + typedef enum |
| 181 | + { |
| 182 | ++ INVALID_PROCSIGNAL = -1, /* Must be first */ |
| 183 | ++ |
| 184 | + PROCSIG_CATCHUP_INTERRUPT, /* sinval catchup interrupt */ |
| 185 | + PROCSIG_NOTIFY_INTERRUPT, /* listen/notify interrupt */ |
| 186 | + PROCSIG_PARALLEL_MESSAGE, /* message from cooperating parallel backend */ |
| 187 | +@@ -37,6 +42,14 @@ typedef enum |
| 188 | + PROCSIG_LOG_MEMORY_CONTEXT, /* ask backend to log the memory contexts */ |
| 189 | + PROCSIG_PARALLEL_APPLY_MESSAGE, /* Message from parallel apply workers */ |
| 190 | + |
| 191 | ++ PROCSIG_CUSTOM_1, |
| 192 | ++ /* |
| 193 | ++ * PROCSIG_CUSTOM_2, |
| 194 | ++ * ..., |
| 195 | ++ * PROCSIG_CUSTOM_N-1, |
| 196 | ++ */ |
| 197 | ++ PROCSIG_CUSTOM_N = PROCSIG_CUSTOM_1 + NUM_CUSTOM_PROCSIGNALS - 1, |
| 198 | ++ |
| 199 | + /* Recovery conflict reasons */ |
| 200 | + PROCSIG_RECOVERY_CONFLICT_FIRST, |
| 201 | + PROCSIG_RECOVERY_CONFLICT_DATABASE = PROCSIG_RECOVERY_CONFLICT_FIRST, |
| 202 | +@@ -56,6 +69,9 @@ typedef enum |
| 203 | + PROCSIGNAL_BARRIER_SMGRRELEASE, /* ask smgr to close files */ |
| 204 | + } ProcSignalBarrierType; |
| 205 | + |
| 206 | ++/* Handler of custom process signal */ |
| 207 | ++typedef void (*ProcSignalHandler_type) (void); |
| 208 | ++ |
| 209 | + /* |
| 210 | + * prototypes for functions in procsignal.c |
| 211 | + */ |
| 212 | +@@ -63,12 +79,15 @@ extern Size ProcSignalShmemSize(void); |
| 213 | + extern void ProcSignalShmemInit(void); |
| 214 | + |
| 215 | + extern void ProcSignalInit(void); |
| 216 | ++extern ProcSignalReason |
| 217 | ++ RegisterCustomProcSignalHandler(ProcSignalHandler_type handler); |
| 218 | + extern int SendProcSignal(pid_t pid, ProcSignalReason reason, |
| 219 | + ProcNumber procNumber); |
| 220 | + |
| 221 | + extern uint64 EmitProcSignalBarrier(ProcSignalBarrierType type); |
| 222 | + extern void WaitForProcSignalBarrier(uint64 generation); |
| 223 | + extern void ProcessProcSignalBarrier(void); |
| 224 | ++extern void CheckAndHandleCustomSignals(void); |
| 225 | + |
| 226 | + extern void procsignal_sigusr1_handler(SIGNAL_ARGS); |
| 227 | + |
0 commit comments