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 1oNZTG-00013E-B2 for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Aug 2022 12:45:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oNZTF-0003c6-3L for pgsql-hackers@arkaria.postgresql.org; Mon, 15 Aug 2022 12:45:29 +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 1oNZTE-0003bx-QS for pgsql-hackers@lists.postgresql.org; Mon, 15 Aug 2022 12:45:28 +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 1oNZTC-0005MD-Ab for pgsql-hackers@lists.postgresql.org; Mon, 15 Aug 2022 12:45:28 +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 4M5vBs2YY4z3F3x for ; Mon, 15 Aug 2022 14:45:25 +0200 (CEST) Date: Mon, 15 Aug 2022 14:45:24 +0200 From: Christoph Berg To: PostgreSQL Hackers Subject: pg_receivewal and SIGTERM Message-ID: Mail-Followup-To: Christoph Berg , PostgreSQL Hackers MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="lHS+ukWV+WGUMhZr" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --lHS+ukWV+WGUMhZr Content-Type: text/plain; charset=us-ascii Content-Disposition: inline There's a smallish backup tool called pg_backupcluster in Debian's postgresql-common which also ships a systemd service that runs pg_receivewal for wal archiving, and supplies a pg_getwal script for reading the files back on restore, including support for .partial files. So far the machinery was using plain files and relied on compressing the WALs from time to time, but now I wanted to compress the files directly from pg_receivewal --compress=5. Unfortunately this broke the regression tests that include a test for the .partial files where pg_receivewal.service is shut down before the segment is full. The problem was that systemd's default KillSignal is SIGTERM, while pg_receivewal flushes the output compression buffers on SIGINT only. The attached patch makes it do the same for SIGTERM as well. (Most places in PG that install a SIGINT handler also install a SIGTERM handler already.) Christoph --lHS+ukWV+WGUMhZr Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-pg_receivewal-Exit-cleanly-on-SIGTERM.patch" From 8e42bf5458ae00050327da07c09a77649f24e36d Mon Sep 17 00:00:00 2001 From: Christoph Berg Date: Mon, 15 Aug 2022 14:29:43 +0200 Subject: [PATCH] pg_receivewal: Exit cleanly on SIGTERM 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. --- doc/src/sgml/ref/pg_receivewal.sgml | 8 +++++--- src/bin/pg_basebackup/pg_receivewal.c | 1 + 2 files changed, 6 insertions(+), 3 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/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c index f064cff4ab..4acd0654b9 100644 --- a/src/bin/pg_basebackup/pg_receivewal.c +++ b/src/bin/pg_basebackup/pg_receivewal.c @@ -933,6 +933,7 @@ main(int argc, char **argv) */ #ifndef WIN32 pqsignal(SIGINT, sigint_handler); + pqsignal(SIGTERM, sigint_handler); #endif /* -- 2.35.1 --lHS+ukWV+WGUMhZr--