Skip to content

Commit 3002c2c

Browse files
Ekaterina Sokolovasokolcati
authored andcommitted
Add patches for PostgreSQL 18 core
1 parent ca1a4ab commit 3002c2c

File tree

2 files changed

+500
-0
lines changed

2 files changed

+500
-0
lines changed

patches/custom_signals_18.0.patch

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
diff --git a/src/backend/storage/ipc/procsignal.c b/src/backend/storage/ipc/procsignal.c
2+
index 087821311cc..f412ed57fbe 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-2025, PostgreSQL Global Development Group
8+
* Portions Copyright (c) 1994, Regents of the University of California
9+
+ * Portions Copyright (c) 2025, Postgres Professional
10+
*
11+
* IDENTIFICATION
12+
* src/backend/storage/ipc/procsignal.c
13+
@@ -102,6 +103,13 @@ struct ProcSignalHeader
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+
NON_EXEC_STATIC ProcSignalHeader *ProcSignal = NULL;
25+
static ProcSignalSlot *MyProcSignalSlot = NULL;
26+
27+
@@ -109,6 +117,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+
@@ -269,6 +279,36 @@ CleanupProcSignalState(int status, Datum arg)
37+
ConditionVariableBroadcast(&slot->pss_barrierCV);
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+
@@ -715,6 +755,8 @@ 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+
@@ -797,3 +839,66 @@ SendCancelRequest(int backendPID, const uint8 *cancel_key, int cancel_key_len)
83+
(errmsg("PID %d in cancel request did not match any process",
84+
backendPID)));
85+
}
86+
+
87+
+/*
88+
+ * Handle receipt of an interrupt indicating any of custom process signals.
89+
+ */
90+
+static void
91+
+CheckAndSetCustomSignalInterrupts()
92+
+{
93+
+ ProcSignalReason reason;
94+
+
95+
+ for (reason = PROCSIG_CUSTOM_1; reason <= PROCSIG_CUSTOM_N; reason++)
96+
+ {
97+
+ if (CheckProcSignal(reason))
98+
+ {
99+
+
100+
+ /* set interrupt flags */
101+
+ InterruptPending = true;
102+
+ CustomSignalPendings[reason - PROCSIG_CUSTOM_1] = true;
103+
+ }
104+
+ }
105+
+
106+
+ SetLatch(MyLatch);
107+
+}
108+
+
109+
+/*
110+
+ * CheckAndHandleCustomSignals
111+
+ * Check custom signal flags and call handler assigned to that signal
112+
+ * if it is not NULL
113+
+ *
114+
+ * This function is called within CHECK_FOR_INTERRUPTS if interrupt occurred.
115+
+ */
116+
+void
117+
+CheckAndHandleCustomSignals(void)
118+
+{
119+
+ int i;
120+
+
121+
+ /*
122+
+ * This is invoked from ProcessInterrupts(), and since some of the
123+
+ * functions it calls contain CHECK_FOR_INTERRUPTS(), there is a potential
124+
+ * for recursive calls if more signals are received while this runs, so
125+
+ * let's block interrupts until done.
126+
+ */
127+
+ HOLD_INTERRUPTS();
128+
+
129+
+ /* Check on expiring of custom signals and call its handlers if exist */
130+
+ for (i = 0; i < NUM_CUSTOM_PROCSIGNALS; i++)
131+
+ {
132+
+ if (!CustomSignalProcessing[i] && CustomSignalPendings[i])
133+
+ {
134+
+ ProcSignalHandler_type handler;
135+
+
136+
+ CustomSignalPendings[i] = false;
137+
+ handler = CustomInterruptHandlers[i];
138+
+ if (handler != NULL)
139+
+ {
140+
+ CustomSignalProcessing[i] = true;
141+
+ handler();
142+
+ CustomSignalProcessing[i] = false;
143+
+ }
144+
+ }
145+
+ }
146+
+
147+
+ RESUME_INTERRUPTS();
148+
+}
149+
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
150+
index 2f8c3d5f918..7baa3ebc98f 100644
151+
--- a/src/backend/tcop/postgres.c
152+
+++ b/src/backend/tcop/postgres.c
153+
@@ -3530,6 +3530,8 @@ ProcessInterrupts(void)
154+
if (ParallelMessagePending)
155+
ProcessParallelMessages();
156+
157+
+ CheckAndHandleCustomSignals();
158+
+
159+
if (LogMemoryContextPending)
160+
ProcessLogMemoryContextInterrupt();
161+
162+
diff --git a/src/include/storage/procsignal.h b/src/include/storage/procsignal.h
163+
index afeeb1ca019..814825fb839 100644
164+
--- a/src/include/storage/procsignal.h
165+
+++ b/src/include/storage/procsignal.h
166+
@@ -6,6 +6,7 @@
167+
*
168+
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
169+
* Portions Copyright (c) 1994, Regents of the University of California
170+
+ * Portions Copyright (c) 2025, Postgres Professional
171+
*
172+
* src/include/storage/procsignal.h
173+
*
174+
@@ -17,6 +18,8 @@
175+
#include "storage/procnumber.h"
176+
177+
178+
+#define NUM_CUSTOM_PROCSIGNALS 64
179+
+
180+
/*
181+
* Reasons for signaling a Postgres child process (a backend or an auxiliary
182+
* process, like checkpointer). We can cope with concurrent signals for different
183+
@@ -29,6 +32,8 @@
184+
*/
185+
typedef enum
186+
{
187+
+ INVALID_PROCSIGNAL = -1, /* Must be first */
188+
+
189+
PROCSIG_CATCHUP_INTERRUPT, /* sinval catchup interrupt */
190+
PROCSIG_NOTIFY_INTERRUPT, /* listen/notify interrupt */
191+
PROCSIG_PARALLEL_MESSAGE, /* message from cooperating parallel backend */
192+
@@ -37,6 +42,14 @@ typedef enum
193+
PROCSIG_LOG_MEMORY_CONTEXT, /* ask backend to log the memory contexts */
194+
PROCSIG_PARALLEL_APPLY_MESSAGE, /* Message from parallel apply workers */
195+
196+
+ PROCSIG_CUSTOM_1,
197+
+ /*
198+
+ * PROCSIG_CUSTOM_2,
199+
+ * ...,
200+
+ * PROCSIG_CUSTOM_N-1,
201+
+ */
202+
+ PROCSIG_CUSTOM_N = PROCSIG_CUSTOM_1 + NUM_CUSTOM_PROCSIGNALS - 1,
203+
+
204+
/* Recovery conflict reasons */
205+
PROCSIG_RECOVERY_CONFLICT_FIRST,
206+
PROCSIG_RECOVERY_CONFLICT_DATABASE = PROCSIG_RECOVERY_CONFLICT_FIRST,
207+
@@ -56,6 +69,9 @@ typedef enum
208+
PROCSIGNAL_BARRIER_SMGRRELEASE, /* ask smgr to close files */
209+
} ProcSignalBarrierType;
210+
211+
+/* Handler of custom process signal */
212+
+typedef void (*ProcSignalHandler_type) (void);
213+
+
214+
/*
215+
* Length of query cancel keys generated.
216+
*
217+
@@ -73,6 +89,8 @@ extern Size ProcSignalShmemSize(void);
218+
extern void ProcSignalShmemInit(void);
219+
220+
extern void ProcSignalInit(const uint8 *cancel_key, int cancel_key_len);
221+
+extern ProcSignalReason
222+
+ RegisterCustomProcSignalHandler(ProcSignalHandler_type handler);
223+
extern int SendProcSignal(pid_t pid, ProcSignalReason reason,
224+
ProcNumber procNumber);
225+
extern void SendCancelRequest(int backendPID, const uint8 *cancel_key, int cancel_key_len);
226+
@@ -80,6 +98,7 @@ extern void SendCancelRequest(int backendPID, const uint8 *cancel_key, int cance
227+
extern uint64 EmitProcSignalBarrier(ProcSignalBarrierType type);
228+
extern void WaitForProcSignalBarrier(uint64 generation);
229+
extern void ProcessProcSignalBarrier(void);
230+
+extern void CheckAndHandleCustomSignals(void);
231+
232+
extern void procsignal_sigusr1_handler(SIGNAL_ARGS);
233+

0 commit comments

Comments
 (0)