Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit138323b

Browse files
Merged versions 9.6 and 10
1 parentca97933 commit138323b

6 files changed

+313
-5
lines changed

‎README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ Using this module there can help in the following things:
1515
To install`pg_query_state`, please apply patches`custom_signal.patch` and`runtime_explain.patch` to the latest stable version of PostgreSQL and rebuild PostgreSQL.
1616

1717
Correspondence branch names to PostgreSQL version numbers:
18-
-_PG9_5_ --- PostgreSQL 9.5
18+
-_PG9_5_--- PostgreSQL 9.5
1919
-_PGPRO9_5_ --- PostgresPro 9.5
20-
-_PGPRO9_6_ --- PostgresPro 9.6
21-
-_PGPRO10_ --- PostgresPro 10
22-
-_PG10_ --- PostgreSQL 10
23-
-_master_ --- development version for the newest version PostgreSQL
20+
-_PGPRO9_6_ ---PostgreSQL 9.6 andPostgresPro 9.6
21+
-_PGPRO10_--- PostgresPro 10
22+
-_PG10_--- PostgreSQL 10
23+
-_master_--- development version for the newest version PostgreSQL
2424

2525
Then execute this in the module's directory:
2626
```

‎patches/custom_signals_10.0.patch

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
diff --git a/src/backend/storage/ipc/procsignal.c b/src/backend/storage/ipc/procsignal.c
2+
index b9302ac..95f7da2 100644
3+
--- a/src/backend/storage/ipc/procsignal.c
4+
+++ b/src/backend/storage/ipc/procsignal.c
5+
@@ -27,6 +27,7 @@
6+
#include "storage/shmem.h"
7+
#include "storage/sinval.h"
8+
#include "tcop/tcopprot.h"
9+
+#include "utils/memutils.h"
10+
11+
12+
/*
13+
@@ -60,12 +61,17 @@ typedef struct
14+
*/
15+
#define NumProcSignalSlots(MaxBackends + NUM_AUXPROCTYPES)
16+
17+
+static bool CustomSignalPendings[NUM_CUSTOM_PROCSIGNALS];
18+
+static ProcSignalHandler_type CustomHandlers[NUM_CUSTOM_PROCSIGNALS];
19+
+
20+
static ProcSignalSlot *ProcSignalSlots = NULL;
21+
static volatile ProcSignalSlot *MyProcSignalSlot = NULL;
22+
23+
static bool CheckProcSignal(ProcSignalReason reason);
24+
static void CleanupProcSignalState(int status, Datum arg);
25+
26+
+static void CustomSignalInterrupt(ProcSignalReason reason);
27+
+
28+
/*
29+
* ProcSignalShmemSize
30+
*Compute space needed for procsignal's shared memory
31+
@@ -166,6 +172,57 @@ CleanupProcSignalState(int status, Datum arg)
32+
}
33+
34+
/*
35+
+ * RegisterCustomProcSignalHandler
36+
+ * Assign specific handler for custom process signal with new ProcSignalReason key.
37+
+ * Return INVALID_PROCSIGNAL if all custom signals have been assigned.
38+
+ */
39+
+ProcSignalReason
40+
+RegisterCustomProcSignalHandler(ProcSignalHandler_type handler)
41+
+{
42+
+ProcSignalReason reason;
43+
+
44+
+/* iterate through custom signal keys to find free spot */
45+
+for (reason = PROCSIG_CUSTOM_1; reason <= PROCSIG_CUSTOM_N; reason++)
46+
+if (!CustomHandlers[reason - PROCSIG_CUSTOM_1])
47+
+{
48+
+CustomHandlers[reason - PROCSIG_CUSTOM_1] = handler;
49+
+return reason;
50+
+}
51+
+return INVALID_PROCSIGNAL;
52+
+}
53+
+
54+
+/*
55+
+ * AssignCustomProcSignalHandler
56+
+ * Assign handler of custom process signal with specific ProcSignalReason key.
57+
+ * Return old ProcSignal handler.
58+
+ * Assume incoming reason is one of custom ProcSignals.
59+
+ */
60+
+ProcSignalHandler_type
61+
+AssignCustomProcSignalHandler(ProcSignalReason reason, ProcSignalHandler_type handler)
62+
+{
63+
+ProcSignalHandler_type old;
64+
+
65+
+AssertArg(reason >= PROCSIG_CUSTOM_1 && reason <= PROCSIG_CUSTOM_N);
66+
+
67+
+old = CustomHandlers[reason - PROCSIG_CUSTOM_1];
68+
+CustomHandlers[reason - PROCSIG_CUSTOM_1] = handler;
69+
+return old;
70+
+}
71+
+
72+
+/*
73+
+ * GetCustomProcSignalHandler
74+
+ * Get handler of custom process signal.
75+
+ *Assume incoming reason is one of custom ProcSignals.
76+
+ */
77+
+ProcSignalHandler_type
78+
+GetCustomProcSignalHandler(ProcSignalReason reason)
79+
+{
80+
+AssertArg(reason >= PROCSIG_CUSTOM_1 && reason <= PROCSIG_CUSTOM_N);
81+
+
82+
+return CustomHandlers[reason - PROCSIG_CUSTOM_1];
83+
+}
84+
+
85+
+/*
86+
* SendProcSignal
87+
*Send a signal to a Postgres process
88+
*
89+
@@ -260,7 +317,8 @@ CheckProcSignal(ProcSignalReason reason)
90+
void
91+
procsignal_sigusr1_handler(SIGNAL_ARGS)
92+
{
93+
-intsave_errno = errno;
94+
+intsave_errno = errno;
95+
+ProcSignalReason reason;
96+
97+
if (CheckProcSignal(PROCSIG_CATCHUP_INTERRUPT))
98+
HandleCatchupInterrupt();
99+
@@ -292,9 +350,87 @@ procsignal_sigusr1_handler(SIGNAL_ARGS)
100+
if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN))
101+
RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
102+
103+
+for (reason = PROCSIG_CUSTOM_1; reason <= PROCSIG_CUSTOM_N; reason++)
104+
+if (CheckProcSignal(reason))
105+
+CustomSignalInterrupt(reason);
106+
+
107+
SetLatch(MyLatch);
108+
109+
latch_sigusr1_handler();
110+
111+
errno = save_errno;
112+
}
113+
+
114+
+/*
115+
+ * Handle receipt of an interrupt indicating a custom process signal.
116+
+ */
117+
+static void
118+
+CustomSignalInterrupt(ProcSignalReason reason)
119+
+{
120+
+intsave_errno = errno;
121+
+
122+
+AssertArg(reason >= PROCSIG_CUSTOM_1 && reason <= PROCSIG_CUSTOM_N);
123+
+
124+
+/* set interrupt flags */
125+
+InterruptPending = true;
126+
+CustomSignalPendings[reason - PROCSIG_CUSTOM_1] = true;
127+
+
128+
+/* make sure the event is processed in due course */
129+
+SetLatch(MyLatch);
130+
+
131+
+errno = save_errno;
132+
+}
133+
+
134+
+/*
135+
+ * CheckAndHandleCustomSignals
136+
+ * Check custom signal flags and call handler assigned to that signal if it is not NULL.
137+
+ * This function is called within CHECK_FOR_INTERRUPTS if interrupt have been occurred.
138+
+ */
139+
+void
140+
+CheckAndHandleCustomSignals(void)
141+
+{
142+
+int i;
143+
+MemoryContext oldcontext;
144+
+
145+
+static MemoryContext hcs_context = NULL;
146+
+
147+
+/*
148+
+ * This is invoked from ProcessInterrupts(), and since some of the
149+
+ * functions it calls contain CHECK_FOR_INTERRUPTS(), there is a potential
150+
+ * for recursive calls if more signals are received while this runs, so
151+
+ * let's block interrupts until done.
152+
+ */
153+
+HOLD_INTERRUPTS();
154+
+
155+
+/*
156+
+ * Moreover, CurrentMemoryContext might be pointing almost anywhere. We
157+
+ * don't want to risk leaking data into long-lived contexts, so let's do
158+
+ * our work here in a private context that we can reset on each use.
159+
+ */
160+
+if (hcs_context == NULL)/* first time through? */
161+
+hcs_context = AllocSetContextCreate(TopMemoryContext,
162+
+"HandleCustomSignals",
163+
+ALLOCSET_DEFAULT_SIZES);
164+
+else
165+
+MemoryContextReset(hcs_context);
166+
+
167+
+oldcontext = MemoryContextSwitchTo(hcs_context);
168+
+
169+
+for (i = 0; i < NUM_CUSTOM_PROCSIGNALS; i++)
170+
+if (CustomSignalPendings[i])
171+
+{
172+
+ProcSignalHandler_type handler;
173+
+
174+
+CustomSignalPendings[i] = false;
175+
+handler = CustomHandlers[i];
176+
+if (handler)
177+
+handler();
178+
+}
179+
+
180+
+MemoryContextSwitchTo(oldcontext);
181+
+
182+
+/* Might as well clear the context on our way out */
183+
+MemoryContextReset(hcs_context);
184+
+
185+
+RESUME_INTERRUPTS();
186+
+}
187+
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
188+
index 63a1994..0dfad33 100644
189+
--- a/src/backend/tcop/postgres.c
190+
+++ b/src/backend/tcop/postgres.c
191+
@@ -3012,6 +3012,8 @@ ProcessInterrupts(void)
192+
193+
if (ParallelMessagePending)
194+
HandleParallelMessages();
195+
+
196+
+CheckAndHandleCustomSignals();
197+
}
198+
199+
200+
diff --git a/src/include/storage/procsignal.h b/src/include/storage/procsignal.h
201+
index 20bb05b..9b16eb3 100644
202+
--- a/src/include/storage/procsignal.h
203+
+++ b/src/include/storage/procsignal.h
204+
@@ -17,6 +17,8 @@
205+
#include "storage/backendid.h"
206+
207+
208+
+#define NUM_CUSTOM_PROCSIGNALS 64
209+
+
210+
/*
211+
* Reasons for signalling a Postgres child process (a backend or an auxiliary
212+
* process, like checkpointer). We can cope with concurrent signals for different
213+
@@ -29,6 +31,8 @@
214+
*/
215+
typedef enum
216+
{
217+
+INVALID_PROCSIGNAL = -1,/* Must be first */
218+
+
219+
PROCSIG_CATCHUP_INTERRUPT,/* sinval catchup interrupt */
220+
PROCSIG_NOTIFY_INTERRUPT,/* listen/notify interrupt */
221+
PROCSIG_PARALLEL_MESSAGE,/* message from cooperating parallel backend */
222+
@@ -42,9 +46,20 @@ typedef enum
223+
PROCSIG_RECOVERY_CONFLICT_BUFFERPIN,
224+
PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK,
225+
226+
+PROCSIG_CUSTOM_1,
227+
+/*
228+
+ * PROCSIG_CUSTOM_2,
229+
+ * ...,
230+
+ * PROCSIG_CUSTOM_N-1,
231+
+ */
232+
+PROCSIG_CUSTOM_N = PROCSIG_CUSTOM_1 + NUM_CUSTOM_PROCSIGNALS - 1,
233+
+
234+
NUM_PROCSIGNALS/* Must be last! */
235+
} ProcSignalReason;
236+
237+
+/* Handler of custom process signal */
238+
+typedef void (*ProcSignalHandler_type) (void);
239+
+
240+
/*
241+
* prototypes for functions in procsignal.c
242+
*/
243+
@@ -52,9 +67,15 @@ extern Size ProcSignalShmemSize(void);
244+
extern void ProcSignalShmemInit(void);
245+
246+
extern void ProcSignalInit(int pss_idx);
247+
+extern ProcSignalReason RegisterCustomProcSignalHandler(ProcSignalHandler_type handler);
248+
+extern ProcSignalHandler_type AssignCustomProcSignalHandler(ProcSignalReason reason,
249+
+ ProcSignalHandler_type handler);
250+
+extern ProcSignalHandler_type GetCustomProcSignalHandler(ProcSignalReason reason);
251+
extern int SendProcSignal(pid_t pid, ProcSignalReason reason,
252+
BackendId backendId);
253+
254+
+extern void CheckAndHandleCustomSignals(void);
255+
+
256+
extern void procsignal_sigusr1_handler(SIGNAL_ARGS);
257+
258+
#endif/* PROCSIGNAL_H */
File renamed without changes.
File renamed without changes.

‎pg_query_state.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ void_PG_fini(void);
5858

5959
/* hooks defined in this module */
6060
staticvoidqs_ExecutorStart(QueryDesc*queryDesc,inteflags);
61+
#ifPG_VERSION_NUM<100000
6162
staticvoidqs_ExecutorRun(QueryDesc*queryDesc,ScanDirectiondirection,uint64count);
63+
#else
64+
staticvoidqs_ExecutorRun(QueryDesc*queryDesc,ScanDirectiondirection,
65+
uint64count,boolexecute_once);
66+
#endif
6267
staticvoidqs_ExecutorFinish(QueryDesc*queryDesc);
6368
staticvoidqs_ExecutorEnd(QueryDesc*queryDesc);
6469

@@ -158,9 +163,15 @@ pg_qs_shmem_startup(void)
158163
{
159164
toc=shm_toc_attach(PG_QS_MODULE_KEY,shmem);
160165

166+
#ifPG_VERSION_NUM<100000
161167
counterpart_userid=shm_toc_lookup(toc,num_toc++);
162168
params=shm_toc_lookup(toc,num_toc++);
163169
mq=shm_toc_lookup(toc,num_toc++);
170+
#else
171+
counterpart_userid=shm_toc_lookup(toc,num_toc++, false);
172+
params=shm_toc_lookup(toc,num_toc++, false);
173+
mq=shm_toc_lookup(toc,num_toc++, false);
174+
#endif
164175
}
165176

166177
if (prev_shmem_startup_hook)
@@ -305,14 +316,25 @@ qs_ExecutorStart(QueryDesc *queryDesc, int eflags)
305316
* Catch any fatal signals
306317
*/
307318
staticvoid
319+
#ifPG_VERSION_NUM<100000
308320
qs_ExecutorRun(QueryDesc*queryDesc,ScanDirectiondirection,uint64count)
321+
#else
322+
qs_ExecutorRun(QueryDesc*queryDesc,ScanDirectiondirection,uint64count,
323+
boolexecute_once)
324+
#endif
309325
{
310326
PG_TRY();
311327
{
312328
if (prev_ExecutorRun)
329+
#ifPG_VERSION_NUM<100000
313330
prev_ExecutorRun(queryDesc,direction,count);
314331
else
315332
standard_ExecutorRun(queryDesc,direction,count);
333+
#else
334+
prev_ExecutorRun(queryDesc,direction,count,execute_once);
335+
else
336+
standard_ExecutorRun(queryDesc,direction,count,execute_once);
337+
#endif
316338
}
317339
PG_CATCH();
318340
{
@@ -707,7 +729,11 @@ GetRemoteBackendUserId(PGPROC *proc)
707729
if (result!=InvalidOid)
708730
break;
709731

732+
#ifPG_VERSION_NUM<100000
710733
WaitLatch(MyLatch,WL_LATCH_SET,0);
734+
#else
735+
WaitLatch(MyLatch,WL_LATCH_SET,0,PG_WAIT_EXTENSION);
736+
#endif
711737
CHECK_FOR_INTERRUPTS();
712738
ResetLatch(MyLatch);
713739
}
@@ -744,7 +770,12 @@ shm_mq_receive_with_timeout(shm_mq_handle *mqh,
744770
if (rc&WL_TIMEOUT||delay <=0)
745771
returnSHM_MQ_WOULD_BLOCK;
746772

773+
#ifPG_VERSION_NUM<100000
747774
rc=WaitLatch(MyLatch,WL_LATCH_SET |WL_TIMEOUT,delay);
775+
#else
776+
rc=WaitLatch(MyLatch,WL_LATCH_SET |WL_TIMEOUT,delay,
777+
PG_WAIT_EXTENSION);
778+
#endif
748779

749780
INSTR_TIME_SET_CURRENT(cur_time);
750781
INSTR_TIME_SUBTRACT(cur_time,start_time);
@@ -869,7 +900,11 @@ GetRemoteBackendWorkers(PGPROC *proc)
869900
result=lcons(proc,result);
870901
}
871902

903+
#ifPG_VERSION_NUM<100000
872904
shm_mq_detach(mq);
905+
#else
906+
shm_mq_detach(mqh);
907+
#endif
873908

874909
returnresult;
875910

@@ -959,7 +994,11 @@ GetRemoteBackendQueryStates(PGPROC *leader,
959994
gotomq_error;
960995
Assert(len==msg->length);
961996
result=lappend(result,copy_msg(msg));
997+
#ifPG_VERSION_NUM<100000
962998
shm_mq_detach(mq);
999+
#else
1000+
shm_mq_detach(mqh);
1001+
#endif
9631002

9641003
/*
9651004
* collect results from all alived parallel workers
@@ -990,7 +1029,11 @@ GetRemoteBackendQueryStates(PGPROC *leader,
9901029
/* aggregate result data */
9911030
result=lappend(result,copy_msg(msg));
9921031

1032+
#ifPG_VERSION_NUM<100000
9931033
shm_mq_detach(mq);
1034+
#else
1035+
shm_mq_detach(mqh);
1036+
#endif
9941037
}
9951038

9961039
returnresult;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp