From 74bc13551fcdc208d4a5e386a01776def602251e Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Fri, 16 Apr 2021 15:36:43 +1200
Subject: [PATCH] Make postmaster's SIGURG setup more consistent.

Although the only current user of SIGURG isn't at risk of missing an
important signal (latch.c doesn't expect SIGURG until it advertises in
shmem that it's waiting for it and does even that only after checking
that is_set hasn't been set), it's theoretically possible that we could
use SIGURG for other purposes in the future.  Therefore, give it the
same treatment as SIGUSR2, just in case: install a dummy handler before
forking, so that any signals are queued until it's unblocked by each
child.

Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/3739816.1618453573%40sss.pgh.pa.us
---
 src/backend/postmaster/postmaster.c | 2 +-
 src/backend/storage/ipc/latch.c     | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 4a3ca78c1b..a739b461b7 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -660,7 +660,7 @@ PostmasterMain(int argc, char *argv[])
 	pqsignal_pm(SIGCHLD, reaper);	/* handle child termination */
 
 #ifdef SIGURG
-	pqsignal_pm(SIGURG, SIG_IGN);	/* ignored */
+	pqsignal_pm(SIGURG, dummy_handler);		/* unused, reserve for children */
 #endif
 
 	/*
diff --git a/src/backend/storage/ipc/latch.c b/src/backend/storage/ipc/latch.c
index 5f3318fa8f..05b364fa9e 100644
--- a/src/backend/storage/ipc/latch.c
+++ b/src/backend/storage/ipc/latch.c
@@ -268,7 +268,8 @@ InitializeLatchSupport(void)
 #ifdef WAIT_USE_EPOLL
 	sigset_t	signalfd_mask;
 
-	/* Block SIGURG, because we'll receive it through a signalfd. */
+	/* Ignore SIGURG, and keep it permanently blocked. */
+	pqsignal(SIGURG, SIG_IGN);
 	sigaddset(&UnBlockSig, SIGURG);
 
 	/* Set up the signalfd to receive SIGURG notifications. */
-- 
2.30.1

