From 1a2ae7c50fb4ab17a8d38e755fc871ebc3a5845d Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Thu, 12 Nov 2020 13:25:23 +0900 Subject: [PATCH] Use standard SIGHUP and SIGTERM handlers in walreceiver. Commit 1e53fe0e70 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 --- src/backend/replication/walreceiver.c | 53 +++++---------------------- 1 file changed, 10 insertions(+), 43 deletions(-) diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index bb1d44ccb7a..babee386c4b 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -104,13 +104,6 @@ static int recvFile = -1; static TimeLineID recvFileTLI = 0; static XLogSegNo recvSegNo = 0; -/* - * Flags set by interrupt handlers of walreceiver for later service in the - * main loop. - */ -static volatile sig_atomic_t got_SIGHUP = false; -static volatile sig_atomic_t got_SIGTERM = false; - /* * LogstreamResult indicates the byte positions that we have already * written/fsynced. @@ -135,11 +128,6 @@ static void XLogWalRcvSendReply(bool force, bool requestReply); static void XLogWalRcvSendHSFeedback(bool immed); static void ProcessWalSndrMessage(XLogRecPtr walEnd, TimestampTz sendTime); -/* Signal handlers */ -static void WalRcvSigHupHandler(SIGNAL_ARGS); -static void WalRcvShutdownHandler(SIGNAL_ARGS); - - /* * Process any interrupts the walreceiver process may have received. * This should be called any time the process's latch has become set. @@ -164,7 +152,7 @@ ProcessWalRcvInterrupts(void) */ CHECK_FOR_INTERRUPTS(); - if (got_SIGTERM) + if (ShutdownRequestPending) { ereport(FATAL, (errcode(ERRCODE_ADMIN_SHUTDOWN), @@ -267,9 +255,10 @@ WalReceiverMain(void) on_shmem_exit(WalRcvDie, 0); /* Properly accept or ignore signals the postmaster might send us */ - pqsignal(SIGHUP, WalRcvSigHupHandler); /* set flag to read config file */ + pqsignal(SIGHUP, SignalHandlerForConfigReload); /* set flag to read config + * file */ pqsignal(SIGINT, SIG_IGN); - pqsignal(SIGTERM, WalRcvShutdownHandler); /* request shutdown */ + pqsignal(SIGTERM, SignalHandlerForShutdownRequest); /* request shutdown */ /* SIGQUIT handler was already set up by InitPostmasterChild */ pqsignal(SIGALRM, SIG_IGN); pqsignal(SIGPIPE, SIG_IGN); @@ -441,9 +430,9 @@ WalReceiverMain(void) /* Process any requests or signals received recently */ ProcessWalRcvInterrupts(); - if (got_SIGHUP) + if (ConfigReloadPending) { - got_SIGHUP = false; + ConfigReloadPending = false; ProcessConfigFile(PGC_SIGHUP); XLogWalRcvSendHSFeedback(true); } @@ -510,7 +499,7 @@ WalReceiverMain(void) * avoiding some system calls. */ Assert(wait_fd != PGINVALID_SOCKET); - rc = WaitLatchOrSocket(walrcv->latch, + rc = WaitLatchOrSocket(MyLatch, WL_EXIT_ON_PM_DEATH | WL_SOCKET_READABLE | WL_TIMEOUT | WL_LATCH_SET, wait_fd, @@ -518,7 +507,7 @@ WalReceiverMain(void) WAIT_EVENT_WAL_RECEIVER_MAIN); if (rc & WL_LATCH_SET) { - ResetLatch(walrcv->latch); + ResetLatch(MyLatch); ProcessWalRcvInterrupts(); if (walrcv->force_reply) @@ -669,7 +658,7 @@ WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI) WakeupRecovery(); for (;;) { - ResetLatch(walrcv->latch); + ResetLatch(MyLatch); ProcessWalRcvInterrupts(); @@ -701,7 +690,7 @@ WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI) } SpinLockRelease(&walrcv->mutex); - (void) WaitLatch(walrcv->latch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0, + (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0, WAIT_EVENT_WAL_RECEIVER_WAIT_START); } @@ -806,28 +795,6 @@ WalRcvDie(int code, Datum arg) WakeupRecovery(); } -/* SIGHUP: set flag to re-read config file at next convenient time */ -static void -WalRcvSigHupHandler(SIGNAL_ARGS) -{ - got_SIGHUP = true; -} - - -/* SIGTERM: set flag for ProcessWalRcvInterrupts */ -static void -WalRcvShutdownHandler(SIGNAL_ARGS) -{ - int save_errno = errno; - - got_SIGTERM = true; - - if (WalRcv->latch) - SetLatch(WalRcv->latch); - - errno = save_errno; -} - /* * Accept the message from XLOG stream, and process it. */