11diff --git a/src/backend/storage/ipc/procsignal.c b/src/backend/storage/ipc/procsignal.c
2- index a3d6ac5..07270a9 100644
2+ index a3d6ac5..60732a6 100644
33--- a/src/backend/storage/ipc/procsignal.c
44+++ b/src/backend/storage/ipc/procsignal.c
5- @@ -59,12 +59,17 @@ typedef struct
5+ @@ -26,6 +26,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+ @@ -59,12 +60,17 @@ typedef struct
614 */
715 #define NumProcSignalSlots(MaxBackends + NUM_AUXPROCTYPES)
816
@@ -20,7 +28,7 @@ index a3d6ac5..07270a9 100644
2028 /*
2129 * ProcSignalShmemSize
2230 *Compute space needed for procsignal's shared memory
23- @@ -165,6 +170 ,57 @@ CleanupProcSignalState(int status, Datum arg)
31+ @@ -165,6 +171 ,57 @@ CleanupProcSignalState(int status, Datum arg)
2432 }
2533
2634 /*
@@ -78,7 +86,7 @@ index a3d6ac5..07270a9 100644
7886 * SendProcSignal
7987 *Send a signal to a Postgres process
8088 *
81- @@ -259,7 +315 ,8 @@ CheckProcSignal(ProcSignalReason reason)
89+ @@ -259,7 +316 ,8 @@ CheckProcSignal(ProcSignalReason reason)
8290 void
8391 procsignal_sigusr1_handler(SIGNAL_ARGS)
8492 {
@@ -88,7 +96,7 @@ index a3d6ac5..07270a9 100644
8896
8997 if (CheckProcSignal(PROCSIG_CATCHUP_INTERRUPT))
9098 HandleCatchupInterrupt();
91- @@ -288,9 +345,55 @@ procsignal_sigusr1_handler(SIGNAL_ARGS)
99+ @@ -288,9 +346,88 @@ procsignal_sigusr1_handler(SIGNAL_ARGS)
92100 if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN))
93101 RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
94102
@@ -132,6 +140,32 @@ index a3d6ac5..07270a9 100644
132140+ CheckAndHandleCustomSignals(void)
133141+ {
134142+ 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. It's
151+ + * unclear that recursive entry would be safe, and it doesn't seem useful
152+ + * even if it is safe, so let's block interrupts until done.
153+ + */
154+ + HOLD_INTERRUPTS();
155+ +
156+ + /*
157+ + * Moreover, CurrentMemoryContext might be pointing almost anywhere. We
158+ + * don't want to risk leaking data into long-lived contexts, so let's do
159+ + * our work here in a private context that we can reset on each use.
160+ + */
161+ + if (hcs_context == NULL)/* first time through? */
162+ + hcs_context = AllocSetContextCreate(TopMemoryContext,
163+ + "HandleCustomSignals",
164+ + ALLOCSET_DEFAULT_SIZES);
165+ + else
166+ + MemoryContextReset(hcs_context);
167+ +
168+ + oldcontext = MemoryContextSwitchTo(hcs_context);
135169+
136170+ for (i = 0; i < NUM_CUSTOM_PROCSIGNALS; i++)
137171+ if (CustomSignalPendings[i])
@@ -143,6 +177,13 @@ index a3d6ac5..07270a9 100644
143177+ if (handler)
144178+ handler();
145179+ }
180+ +
181+ + MemoryContextSwitchTo(oldcontext);
182+ +
183+ + /* Might as well clear the context on our way out */
184+ + MemoryContextReset(hcs_context);
185+ +
186+ + RESUME_INTERRUPTS();
146187+ }
147188diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
148189index 98ccbbb..c5d649c 100644