Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rSC4w-00Bzk4-HV for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Jan 2024 08:24:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rSC4v-00Awha-MK for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Jan 2024 08:24:17 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rSC4v-00AwhR-98 for pgsql-hackers@lists.postgresql.org; Tue, 23 Jan 2024 08:24:17 +0000 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rSC4s-002vgr-Li for pgsql-hackers@lists.postgresql.org; Tue, 23 Jan 2024 08:24:16 +0000 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-6d9bbf71bc8so2373932b3a.1 for ; Tue, 23 Jan 2024 00:24:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705998253; x=1706603053; darn=lists.postgresql.org; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:from:subject:cc:to:message-id:date:from:to:cc:subject :date:message-id:reply-to; bh=/hV02dTcdtLokzncGpJlH1y74UGmUrGEf19/W+CzUzo=; b=R2vRUgtH/+EFfTyigJFYQ8VUC2dPEK9ncpxZJrTIyu5khuyJXpCYlFkuAQHgJBlNle JJRW1Ui3fa6ayK0UEyOXjKw4juET+lB3XDOTPtmeyjuzedxVcbdwsat0qeuJBsEYrcTh t/3HhAkgiiIAn+1ljMqILe4NMKeGUpEE28PlLRUA9Or7X0f1T3P6Ch7TxRHFL5MZNtdK s1qrgrn0wm3l5Xjfjq/hMTnoDbLAElSUnx7HRUrLQvKRiGLYc1FOmEt7SCOQYmg3Lrr2 FymVyykmjVVTitP2njJK/JmecjuRkWRhwsUDZ3TFyn6skZzPySI4qJn6lI+LR/L12f4L kTxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705998253; x=1706603053; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:from:subject:cc:to:message-id:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=/hV02dTcdtLokzncGpJlH1y74UGmUrGEf19/W+CzUzo=; b=qTLQenxtXwzY9Ws6hDFHoEVfBVZkbKE11PUeBC22xqN73G3PIUNsAv0KkhKbuo56Cy kvcVvChr8cyDUwrkdqxCtmWYoOa0bvmYI3OLErSEO8YgPzCau3Vx8Si2vZoN+WQZbFaG Gkw3TiFmUGwEBWniqjX5MJOCgmS0yjzwlEzX8cJH4UJzWk+zqKlB1e2DFlhCFIA4VG39 J6doR17q2m/ylbOAL5zHvzjRZ9WM34T/cM3kzhSKHGQrDOlvIFOE8hAEnVE9X4u0RU1L LXD/NLCcr+T4gn5aBxHd5U6+c4zzLHwcX3u1oaR6oEjs8oZnOEddl2Duw+k2AwWsrCy3 SQNQ== X-Gm-Message-State: AOJu0YzyzOFE4blGlkd8nn28ogyMGujyOZx0xC2k3XQucNioQdknitxu dYN9gg3foAszvyy3F6kZgYflLLGwtR/3bwOr/GSJDkcmvnUEfQ02 X-Google-Smtp-Source: AGHT+IERQqV5UHLG7Dou0A+o1p/gbmb9zpQVh40fXOe6+FAiAxgO/U0Ew1VSdCExTGrKO/UB5tPV/Q== X-Received: by 2002:a05:6a20:918a:b0:19c:5215:f7b6 with SMTP id v10-20020a056a20918a00b0019c5215f7b6mr1218994pzd.41.1705998253542; Tue, 23 Jan 2024 00:24:13 -0800 (PST) Received: from localhost (KD036014041111.ppp-bb.dion.ne.jp. [36.14.41.111]) by smtp.gmail.com with ESMTPSA id pq2-20020a17090b3d8200b00290d2922d61sm719165pjb.20.2024.01.23.00.24.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 00:24:12 -0800 (PST) Date: Tue, 23 Jan 2024 17:24:10 +0900 (JST) Message-Id: <20240123.172410.1596193222420636986.horikyota.ntt@gmail.com> To: masao.fujii@gmail.com Cc: andres@anarazel.de, michael@paquier.xyz, hlinnaka@iki.fi, pgsql-hackers@lists.postgresql.org Subject: Re: Network failure may prevent promotion From: Kyotaro Horiguchi In-Reply-To: References: <20240122212910.llghcvr3x4itlett@awork3.anarazel.de> <20240123.132320.1131416933172930185.horikyota.ntt@gmail.com> User-Agent: Mew version 6.8 on Emacs 27.2 Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="--Next_Part(Tue_Jan_23_17_24_10_2024_908)--" Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ----Next_Part(Tue_Jan_23_17_24_10_2024_908)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Thank you for looking this! At Tue, 23 Jan 2024 15:07:10 +0900, Fujii Masao wrote in > Regarding the patch, here are the review comments. > > +/* > + * Is current process a wal receiver? > + */ > +bool > +IsWalReceiver(void) > +{ > + return WalRcv != NULL; > +} > > This looks wrong because WalRcv can be non-NULL in processes other > than walreceiver. Mmm. Sorry for the silly mistake. We can use B_WAL_RECEIVER instead. I'm not sure if the new function IsWalReceiver() is required. The expression "MyBackendType == B_WAL_RECEIVER" is quite descriptive. However, the function does make ProcessInterrupts() more aligned regarding process types. > - pqsignal(SIGTERM, SignalHandlerForShutdownRequest); /* request shutdown */ > + pqsignal(SIGTERM, WalRcvShutdownSignalHandler); /* request shutdown */ > > Can't we just use die(), instead? There was a comment explaining the problems associated with exiting within a signal handler; - * Currently, only SIGTERM is of interest. We can't just exit(1) within the - * SIGTERM signal handler, because the signal might arrive in the middle of - * some critical operation, like while we're holding a spinlock. Instead, the And I think we should keep the considerations it suggests. The patch removes the comment itself, but it does so because it implements our standard process exit procedure, which incorporates points suggested by the now-removed comment. -- Kyotaro Horiguchi NTT Open Source Software Center ----Next_Part(Tue_Jan_23_17_24_10_2024_908)-- Content-Type: Text/X-Patch; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="walrcv_shutdown_deblocking_v2.patch" diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c index 201c36cb22..db779dc6ca 100644 --- a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c +++ b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c @@ -749,7 +749,7 @@ libpqrcv_PQgetResult(PGconn *streamConn) if (rc & WL_LATCH_SET) { ResetLatch(MyLatch); - ProcessWalRcvInterrupts(); + CHECK_FOR_INTERRUPTS(); } /* Consume whatever data is available from the socket */ @@ -1053,7 +1053,7 @@ libpqrcv_processTuples(PGresult *pgres, WalRcvExecResult *walres, { char *cstrs[MaxTupleAttributeNumber]; - ProcessWalRcvInterrupts(); + CHECK_FOR_INTERRUPTS(); /* Do the allocations in temporary context. */ oldcontext = MemoryContextSwitchTo(rowcontext); diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c index 728059518e..e491f7d4c5 100644 --- a/src/backend/replication/walreceiver.c +++ b/src/backend/replication/walreceiver.c @@ -147,39 +147,34 @@ static void XLogWalRcvSendReply(bool force, bool requestReply); static void XLogWalRcvSendHSFeedback(bool immed); static void ProcessWalSndrMessage(XLogRecPtr walEnd, TimestampTz sendTime); static void WalRcvComputeNextWakeup(WalRcvWakeupReason reason, TimestampTz now); +static void WalRcvShutdownSignalHandler(SIGNAL_ARGS); -/* - * Process any interrupts the walreceiver process may have received. - * This should be called any time the process's latch has become set. - * - * Currently, only SIGTERM is of interest. We can't just exit(1) within the - * SIGTERM signal handler, because the signal might arrive in the middle of - * some critical operation, like while we're holding a spinlock. Instead, the - * signal handler sets a flag variable as well as setting the process's latch. - * We must check the flag (by calling ProcessWalRcvInterrupts) anytime the - * latch has become set. Operations that could block for a long time, such as - * reading from a remote server, must pay attention to the latch too; see - * libpqrcv_PQgetResult for example. - */ void -ProcessWalRcvInterrupts(void) +WalRcvShutdownSignalHandler(SIGNAL_ARGS) { - /* - * Although walreceiver interrupt handling doesn't use the same scheme as - * regular backends, call CHECK_FOR_INTERRUPTS() to make sure we receive - * any incoming signals on Win32, and also to make sure we process any - * barrier events. - */ - CHECK_FOR_INTERRUPTS(); + int save_errno = errno; - if (ShutdownRequestPending) + /* Don't joggle the elbow of proc_exit */ + if (!proc_exit_inprogress) { - ereport(FATAL, - (errcode(ERRCODE_ADMIN_SHUTDOWN), - errmsg("terminating walreceiver process due to administrator command"))); + InterruptPending = true; + ProcDiePending = true; } + + SetLatch(MyLatch); + + errno = save_errno; + } +/* + * Is current process a wal receiver? + */ +bool +IsWalReceiver(void) +{ + return MyBackendType == B_WAL_RECEIVER; +} /* Main entry point for walreceiver process */ void @@ -277,7 +272,7 @@ WalReceiverMain(void) pqsignal(SIGHUP, SignalHandlerForConfigReload); /* set flag to read config * file */ pqsignal(SIGINT, SIG_IGN); - pqsignal(SIGTERM, SignalHandlerForShutdownRequest); /* request shutdown */ + pqsignal(SIGTERM, WalRcvShutdownSignalHandler); /* request shutdown */ /* SIGQUIT handler was already set up by InitPostmasterChild */ pqsignal(SIGALRM, SIG_IGN); pqsignal(SIGPIPE, SIG_IGN); @@ -456,7 +451,7 @@ WalReceiverMain(void) errmsg("cannot continue WAL streaming, recovery has already ended"))); /* Process any requests or signals received recently */ - ProcessWalRcvInterrupts(); + CHECK_FOR_INTERRUPTS(); if (ConfigReloadPending) { @@ -552,7 +547,7 @@ WalReceiverMain(void) if (rc & WL_LATCH_SET) { ResetLatch(MyLatch); - ProcessWalRcvInterrupts(); + CHECK_FOR_INTERRUPTS(); if (walrcv->force_reply) { @@ -691,7 +686,7 @@ WalRcvWaitForStartPosition(XLogRecPtr *startpoint, TimeLineID *startpointTLI) { ResetLatch(MyLatch); - ProcessWalRcvInterrupts(); + CHECK_FOR_INTERRUPTS(); SpinLockAcquire(&walrcv->mutex); Assert(walrcv->walRcvState == WALRCV_RESTARTING || diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 1a34bd3715..2ce24d8a9a 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -59,6 +59,7 @@ #include "replication/logicallauncher.h" #include "replication/logicalworker.h" #include "replication/slot.h" +#include "replication/walreceiver.h" #include "replication/walsender.h" #include "rewrite/rewriteHandler.h" #include "storage/bufmgr.h" @@ -3286,6 +3287,10 @@ ProcessInterrupts(void) */ proc_exit(1); } + else if (IsWalReceiver()) + ereport(FATAL, + (errcode(ERRCODE_ADMIN_SHUTDOWN), + errmsg("terminating walreceiver process due to administrator command"))); else if (IsBackgroundWorker) ereport(FATAL, (errcode(ERRCODE_ADMIN_SHUTDOWN), diff --git a/src/include/replication/walreceiver.h b/src/include/replication/walreceiver.h index 0899891cdb..a7684b7bdc 100644 --- a/src/include/replication/walreceiver.h +++ b/src/include/replication/walreceiver.h @@ -456,8 +456,8 @@ walrcv_clear_result(WalRcvExecResult *walres) } /* prototypes for functions in walreceiver.c */ +extern bool IsWalReceiver(void); extern void WalReceiverMain(void) pg_attribute_noreturn(); -extern void ProcessWalRcvInterrupts(void); extern void WalRcvForceReply(void); /* prototypes for functions in walreceiverfuncs.c */ ----Next_Part(Tue_Jan_23_17_24_10_2024_908)----