Skip to content

Commit 1a2ae7c

Browse files
committed
Use standard SIGHUP and SIGTERM handlers in walreceiver.
Commit 1e53fe0 changed background processes so that they use standard SIGHUP handler. Like that, this commit makes walreceiver use standard SIGHUP and SIGTERM handlers, to simplify the code. As the side effect of this commit, walreceiver can wake up and process the configuration files promptly when receiving SIGHUP. Because the standard SIGHUP handler sets the latch. On the other hand, previously there could be a time lag between the receipt of SIGHUP and the process of configuration files since the dedicated handler didn't set the latch. Author: Bharath Rupireddy, tweaked by Fujii Masao Reviewed-by: Kyotaro Horiguchi, Fujii Masao Discussion: https://postgr.es/m/CALj2ACXPorUqePswDtOeM_s82v9RW32E1fYmOPZ5NuE+TWKj_A@mail.gmail.com
1 parent 8ca8208 commit 1a2ae7c

File tree

1 file changed

+10
-43
lines changed

1 file changed

+10
-43
lines changed

src/backend/replication/walreceiver.c

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,6 @@ static int recvFile = -1;
104104
static TimeLineID recvFileTLI = 0;
105105
static XLogSegNo recvSegNo = 0;
106106

107-
/*
108-
* Flags set by interrupt handlers of walreceiver for later service in the
109-
* main loop.
110-
*/
111-
static volatile sig_atomic_t got_SIGHUP = false;
112-
static volatile sig_atomic_t got_SIGTERM = false;
113-
114107
/*
115108
* LogstreamResult indicates the byte positions that we have already
116109
* written/fsynced.
@@ -135,11 +128,6 @@ static void XLogWalRcvSendReply(bool force, bool requestReply);
135128
static void XLogWalRcvSendHSFeedback(bool immed);
136129
static void ProcessWalSndrMessage(XLogRecPtr walEnd, TimestampTz sendTime);
137130

138-
/* Signal handlers */
139-
static void WalRcvSigHupHandler(SIGNAL_ARGS);
140-
static void WalRcvShutdownHandler(SIGNAL_ARGS);
141-
142-
143131
/*
144132
* Process any interrupts the walreceiver process may have received.
145133
* This should be called any time the process's latch has become set.
@@ -164,7 +152,7 @@ ProcessWalRcvInterrupts(void)
164152
*/
165153
CHECK_FOR_INTERRUPTS();
166154

167-
if (got_SIGTERM)
155+
if (ShutdownRequestPending)
168156
{
169157
ereport(FATAL,
170158
(errcode(ERRCODE_ADMIN_SHUTDOWN),
@@ -267,9 +255,10 @@ WalReceiverMain(void)
267255
on_shmem_exit(WalRcvDie, 0);
268256

269257
/* Properly accept or ignore signals the postmaster might send us */
270-
pqsignal(SIGHUP, WalRcvSigHupHandler); /* set flag to read config file */
258+
pqsignal(SIGHUP, SignalHandlerForConfigReload); /* set flag to read config
259+
* file */
271260
pqsignal(SIGINT, SIG_IGN);
272-
pqsignal(SIGTERM, WalRcvShutdownHandler); /* request shutdown */
261+
pqsignal(SIGTERM, SignalHandlerForShutdownRequest); /* request shutdown */
273262
/* SIGQUIT handler was already set up by InitPostmasterChild */
274263
pqsignal(SIGALRM, SIG_IGN);
275264
pqsignal(SIGPIPE, SIG_IGN);
@@ -441,9 +430,9 @@ WalReceiverMain(void)
441430
/* Process any requests or signals received recently */
442431
ProcessWalRcvInterrupts();
443432

444-
if (got_SIGHUP)
433+
if (ConfigReloadPending)
445434
{
446-
got_SIGHUP = false;
435+
ConfigReloadPending = false;
447436
ProcessConfigFile(PGC_SIGHUP);
448437
XLogWalRcvSendHSFeedback(true);
449438
}
@@ -510,15 +499,15 @@ WalReceiverMain(void)
510499
* avoiding some system calls.
511500
*/
512501
Assert(wait_fd != PGINVALID_SOCKET);
513-
rc = WaitLatchOrSocket(walrcv->latch,
502+
rc = WaitLatchOrSocket(MyLatch,
514503
WL_EXIT_ON_PM_DEATH | WL_SOCKET_READABLE |
515504
WL_TIMEOUT | WL_LATCH_SET,
516505
wait_fd,
517506
NAPTIME_PER_CYCLE,
518507
WAIT_EVENT_WAL_RECEIVER_MAIN);
519508
if (rc & WL_LATCH_SET)
520509
{
521-
ResetLatch(walrcv->latch);
510+
ResetLatch(MyLatch);
522511
ProcessWalRcvInterrupts();
523512

524513
if (walrcv->force_reply)
@@ -669,7 +658,7 @@ WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI)
669658
WakeupRecovery();
670659
for (;;)
671660
{
672-
ResetLatch(walrcv->latch);
661+
ResetLatch(MyLatch);
673662

674663
ProcessWalRcvInterrupts();
675664

@@ -701,7 +690,7 @@ WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI)
701690
}
702691
SpinLockRelease(&walrcv->mutex);
703692

704-
(void) WaitLatch(walrcv->latch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0,
693+
(void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0,
705694
WAIT_EVENT_WAL_RECEIVER_WAIT_START);
706695
}
707696

@@ -806,28 +795,6 @@ WalRcvDie(int code, Datum arg)
806795
WakeupRecovery();
807796
}
808797

809-
/* SIGHUP: set flag to re-read config file at next convenient time */
810-
static void
811-
WalRcvSigHupHandler(SIGNAL_ARGS)
812-
{
813-
got_SIGHUP = true;
814-
}
815-
816-
817-
/* SIGTERM: set flag for ProcessWalRcvInterrupts */
818-
static void
819-
WalRcvShutdownHandler(SIGNAL_ARGS)
820-
{
821-
int save_errno = errno;
822-
823-
got_SIGTERM = true;
824-
825-
if (WalRcv->latch)
826-
SetLatch(WalRcv->latch);
827-
828-
errno = save_errno;
829-
}
830-
831798
/*
832799
* Accept the message from XLOG stream, and process it.
833800
*/

0 commit comments

Comments
 (0)