Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oQ83T-0003PR-Kc for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Aug 2022 14:05:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oQ83R-0001EQ-Uu for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Aug 2022 14:05:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oQ83R-0001EH-LY for pgsql-hackers@lists.postgresql.org; Mon, 22 Aug 2022 14:05:25 +0000 Received: from feynman.df7cb.de ([195.49.152.168]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oQ83K-0001Hs-HK for pgsql-hackers@lists.postgresql.org; Mon, 22 Aug 2022 14:05:25 +0000 Received: from msg.df7cb.de (unknown [IPv6:2003:5b:203b:100:7627:eaff:fe52:8e03]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) by feynman.df7cb.de (Postfix) with ESMTPSA id 4MBDdn51DZz3Dyp; Mon, 22 Aug 2022 16:05:17 +0200 (CEST) Date: Mon, 22 Aug 2022 16:05:16 +0200 From: Christoph Berg To: Michael Paquier Cc: Bharath Rupireddy , Daniel Gustafsson , PostgreSQL Hackers Subject: Re: pg_receivewal and SIGTERM Message-ID: Mail-Followup-To: Christoph Berg , Michael Paquier , Bharath Rupireddy , Daniel Gustafsson , PostgreSQL Hackers References: <15338147-16C0-4D4D-8CAD-460102857BF6@yesql.se> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="Uibv73CNSB3pmcdq" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Uibv73CNSB3pmcdq Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Re: Michael Paquier > While looking at the last patch proposed, it strikes me that > time_to_stop should be sig_atomic_t in pg_receivewal.c, as the safe > type of variable to set in a signal handler. We could change that, > while on it.. Done in the attached patch. > Backpatching this stuff is not an issue here. Do you mean it can, or can not be backpatched? (I'd argue for backpatching since the behaviour is slightly broken at the moment.) Christoph --Uibv73CNSB3pmcdq Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-pg_receivewal-pg_recvlogical-Exit-cleanly-on-SIGTERM.patch" From 3d17d6f77566047fc63e05c2f33106a09b2c9793 Mon Sep 17 00:00:00 2001 From: Christoph Berg Date: Mon, 15 Aug 2022 14:29:43 +0200 Subject: [PATCH] pg_receivewal, pg_recvlogical: Exit cleanly on SIGTERM In pg_receivewal, compressed output is only flushed on clean exits. The reason to support SIGTERM here as well is that pg_receivewal might well be running as a daemon, and systemd's default KillSignal is SIGTERM. Since pg_recvlogical is also supposed to run as a daemon, teach it about SIGTERM as well. While at it, change pg_receivewal's time_to_stop to sig_atomic_t like in pg_recvlogical. --- doc/src/sgml/ref/pg_receivewal.sgml | 8 +++++--- doc/src/sgml/ref/pg_recvlogical.sgml | 7 +++++++ src/bin/pg_basebackup/pg_receivewal.c | 9 +++++---- src/bin/pg_basebackup/pg_recvlogical.c | 7 ++++--- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/doc/src/sgml/ref/pg_receivewal.sgml b/doc/src/sgml/ref/pg_receivewal.sgml index 4fe9e1a874..5f83ba1893 100644 --- a/doc/src/sgml/ref/pg_receivewal.sgml +++ b/doc/src/sgml/ref/pg_receivewal.sgml @@ -118,8 +118,9 @@ PostgreSQL documentation In the absence of fatal errors, pg_receivewal - will run until terminated by the SIGINT signal - (ControlC). + will run until terminated by the SIGINT + (ControlC) + or SIGTERM signal. @@ -457,7 +458,8 @@ PostgreSQL documentation pg_receivewal will exit with status 0 when - terminated by the SIGINT signal. (That is the + terminated by the SIGINT or + SIGTERM signal. (That is the normal way to end it. Hence it is not an error.) For fatal errors or other signals, the exit status will be nonzero. diff --git a/doc/src/sgml/ref/pg_recvlogical.sgml b/doc/src/sgml/ref/pg_recvlogical.sgml index 1a88225409..012ee4468b 100644 --- a/doc/src/sgml/ref/pg_recvlogical.sgml +++ b/doc/src/sgml/ref/pg_recvlogical.sgml @@ -46,6 +46,13 @@ PostgreSQL documentation a slot without consuming it, use pg_logical_slot_peek_changes. + + + In the absence of fatal errors, pg_recvlogical + will run until terminated by the SIGINT + (ControlC) + or SIGTERM signal. + diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c index f064cff4ab..4b37a98fd6 100644 --- a/src/bin/pg_basebackup/pg_receivewal.c +++ b/src/bin/pg_basebackup/pg_receivewal.c @@ -45,7 +45,7 @@ static int verbose = 0; static int compresslevel = 0; static int noloop = 0; static int standby_message_timeout = 10 * 1000; /* 10 sec = default */ -static volatile bool time_to_stop = false; +static volatile sig_atomic_t time_to_stop = false; static bool do_create_slot = false; static bool slot_exists_ok = false; static bool do_drop_slot = false; @@ -673,13 +673,13 @@ StreamLog(void) } /* - * When sigint is called, just tell the system to exit at the next possible + * When SIGINT/SIGTERM are caught, just tell the system to exit at the next possible * moment. */ #ifndef WIN32 static void -sigint_handler(int signum) +sigexit_handler(int signum) { time_to_stop = true; } @@ -932,7 +932,8 @@ main(int argc, char **argv) * if one is needed, in GetConnection.) */ #ifndef WIN32 - pqsignal(SIGINT, sigint_handler); + pqsignal(SIGINT, sigexit_handler); + pqsignal(SIGTERM, sigexit_handler); #endif /* diff --git a/src/bin/pg_basebackup/pg_recvlogical.c b/src/bin/pg_basebackup/pg_recvlogical.c index 2a4c8b130a..363102cf85 100644 --- a/src/bin/pg_basebackup/pg_recvlogical.c +++ b/src/bin/pg_basebackup/pg_recvlogical.c @@ -650,11 +650,11 @@ error: #ifndef WIN32 /* - * When sigint is called, just tell the system to exit at the next possible + * When SIGINT/SIGTERM are caught, just tell the system to exit at the next possible * moment. */ static void -sigint_handler(int signum) +sigexit_handler(int signum) { time_to_abort = true; } @@ -922,7 +922,8 @@ main(int argc, char **argv) * if one is needed, in GetConnection.) */ #ifndef WIN32 - pqsignal(SIGINT, sigint_handler); + pqsignal(SIGINT, sigexit_handler); + pqsignal(SIGTERM, sigexit_handler); pqsignal(SIGHUP, sighup_handler); #endif -- 2.35.1 --Uibv73CNSB3pmcdq--