Use signals for postmaster death on Linux.
authorThomas Munro <tmunro@postgresql.org>
Wed, 11 Jul 2018 00:40:58 +0000 (12:40 +1200)
committerThomas Munro <tmunro@postgresql.org>
Wed, 11 Jul 2018 00:47:06 +0000 (12:47 +1200)
Linux provides a way to ask for a signal when your parent process dies.
Use that to make PostmasterIsAlive() very cheap.

Based on a suggestion from Andres Freund.

Author: Thomas Munro, Heikki Linnakangas
Reviewed-By: Michael Paquier
Discussion: https://postgr.es/m/7261eb39-0369-f2f4-1bb5-62f3b6083b5e%40iki.fi
Discussion: https://postgr.es/m/20180411002643.6buofht4ranhei7k%40alap3.anarazel.de

configure
configure.in
src/backend/storage/ipc/latch.c
src/backend/storage/ipc/pmsignal.c
src/backend/utils/init/miscinit.c
src/include/pg_config.h.in
src/include/pg_config.h.win32
src/include/storage/pmsignal.h

index 1bc465b9429f36c741291ecb9b5d00605a2dba1c..41e0e1cf34ab73c94c04fae218db49f94bf33c84 100755 (executable)
--- a/configure
+++ b/configure
@@ -12494,7 +12494,7 @@ $as_echo "#define HAVE_STDBOOL_H 1" >>confdefs.h
 fi
 
 
-for ac_header in atomic.h crypt.h dld.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h sys/epoll.h sys/ipc.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/sockio.h sys/tas.h sys/un.h termios.h ucred.h utime.h wchar.h wctype.h
+for ac_header in atomic.h crypt.h dld.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h sys/epoll.h sys/ipc.h sys/prctl.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/sockio.h sys/tas.h sys/un.h termios.h ucred.h utime.h wchar.h wctype.h
 do :
   as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
 ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"
index a6b3b88cfab8c7bc59499c8e97eda683d2f4b34e..1e76c9ee46b5ca849ffccf5671cdd192e6a4a5b8 100644 (file)
@@ -1260,7 +1260,7 @@ AC_SUBST(UUID_LIBS)
 
 AC_HEADER_STDBOOL
 
-AC_CHECK_HEADERS([atomic.h crypt.h dld.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h sys/epoll.h sys/ipc.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/sockio.h sys/tas.h sys/un.h termios.h ucred.h utime.h wchar.h wctype.h])
+AC_CHECK_HEADERS([atomic.h crypt.h dld.h fp_class.h getopt.h ieeefp.h ifaddrs.h langinfo.h mbarrier.h poll.h sys/epoll.h sys/ipc.h sys/prctl.h sys/pstat.h sys/resource.h sys/select.h sys/sem.h sys/shm.h sys/sockio.h sys/tas.h sys/un.h termios.h ucred.h utime.h wchar.h wctype.h])
 
 # On BSD, test for net/if.h will fail unless sys/socket.h
 # is included first.
index e6706f7fb8008a03fd310ed752f38ab4e9084c69..f6dda9cc9ac213e3a0662788768c06c06007333f 100644 (file)
@@ -1112,7 +1112,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
             * WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
             * cost much.
             */
-           if (!PostmasterIsAlive())
+           if (!PostmasterIsAliveInternal())
            {
                occurred_events->fd = PGINVALID_SOCKET;
                occurred_events->events = WL_POSTMASTER_DEATH;
@@ -1230,7 +1230,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
             * WL_POSTMASTER_DEATH event would be painful. Re-checking doesn't
             * cost much.
             */
-           if (!PostmasterIsAlive())
+           if (!PostmasterIsAliveInternal())
            {
                occurred_events->fd = PGINVALID_SOCKET;
                occurred_events->events = WL_POSTMASTER_DEATH;
@@ -1390,7 +1390,7 @@ WaitEventSetWaitBlock(WaitEventSet *set, int cur_timeout,
         * even though there is no known reason to think that the event could
         * be falsely set on Windows.
         */
-       if (!PostmasterIsAlive())
+       if (!PostmasterIsAliveInternal())
        {
            occurred_events->fd = PGINVALID_SOCKET;
            occurred_events->events = WL_POSTMASTER_DEATH;
index be61858fc67456c36da24aba67713b2996577947..ebf027306f7754b55320938d667d2bd943e388b5 100644 (file)
 #include <signal.h>
 #include <unistd.h>
 
+#ifdef HAVE_SYS_PRCTL_H
+#include <sys/prctl.h>
+#endif
+
 #include "miscadmin.h"
 #include "postmaster/postmaster.h"
 #include "replication/walsender.h"
@@ -71,6 +75,35 @@ struct PMSignalData
 
 NON_EXEC_STATIC volatile PMSignalData *PMSignalState = NULL;
 
+/*
+ * Signal handler to be notified if postmaster dies.
+ */
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+volatile sig_atomic_t postmaster_possibly_dead = false;
+
+static void
+postmaster_death_handler(int signo)
+{
+   postmaster_possibly_dead = true;
+}
+
+/*
+ * The available signals depend on the OS.  SIGUSR1 and SIGUSR2 are already
+ * used for other things, so choose another one.
+ *
+ * Currently, we assume that we can always find a signal to use.  That
+ * seems like a reasonable assumption for all platforms that are modern
+ * enough to have a parent-death signaling mechanism.
+ */
+#if defined(SIGINFO)
+#define POSTMASTER_DEATH_SIGNAL SIGINFO
+#elif defined(SIGPWR)
+#define POSTMASTER_DEATH_SIGNAL SIGPWR
+#else
+#error "cannot find a signal to use for postmaster death"
+#endif
+
+#endif                         /* USE_POSTMASTER_DEATH_SIGNAL */
 
 /*
  * PMSignalShmemSize
@@ -266,28 +299,94 @@ MarkPostmasterChildInactive(void)
 
 
 /*
- * PostmasterIsAlive - check whether postmaster process is still alive
+ * PostmasterIsAliveInternal - check whether postmaster process is still alive
+ *
+ * This is the slow path of PostmasterIsAlive(), where the caller has already
+ * checked 'postmaster_possibly_dead'.  (On platforms that don't support
+ * a signal for parent death, PostmasterIsAlive() is just an alias for this.)
  */
 bool
-PostmasterIsAlive(void)
+PostmasterIsAliveInternal(void)
 {
-#ifndef WIN32
-   char        c;
-   ssize_t     rc;
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+   /*
+    * Reset the flag before checking, so that we don't miss a signal if
+    * postmaster dies right after the check.  If postmaster was indeed dead,
+    * we'll re-arm it before returning to caller.
+    */
+   postmaster_possibly_dead = false;
+#endif
 
-   rc = read(postmaster_alive_fds[POSTMASTER_FD_WATCH], &c, 1);
-   if (rc < 0)
+#ifndef WIN32
    {
-       if (errno == EAGAIN || errno == EWOULDBLOCK)
+       char        c;
+       ssize_t     rc;
+
+       rc = read(postmaster_alive_fds[POSTMASTER_FD_WATCH], &c, 1);
+
+       /*
+        * In the usual case, the postmaster is still alive, and there is no
+        * data in the pipe.
+        */
+       if (rc < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
            return true;
        else
-           elog(FATAL, "read on postmaster death monitoring pipe failed: %m");
+       {
+           /*
+            * Postmaster is dead, or something went wrong with the read()
+            * call.
+            */
+
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+           postmaster_possibly_dead = true;
+#endif
+
+           if (rc < 0)
+               elog(FATAL, "read on postmaster death monitoring pipe failed: %m");
+           else if (rc > 0)
+               elog(FATAL, "unexpected data in postmaster death monitoring pipe");
+
+           return false;
+       }
    }
-   else if (rc > 0)
-       elog(FATAL, "unexpected data in postmaster death monitoring pipe");
 
-   return false;
 #else                          /* WIN32 */
-   return (WaitForSingleObject(PostmasterHandle, 0) == WAIT_TIMEOUT);
+   if (WaitForSingleObject(PostmasterHandle, 0) == WAIT_TIMEOUT)
+       return true;
+   else
+   {
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+       postmaster_possibly_dead = true;
+#endif
+       return false;
+   }
 #endif                         /* WIN32 */
 }
+
+/*
+ * PostmasterDeathSignalInit - request signal on postmaster death if possible
+ */
+void
+PostmasterDeathSignalInit(void)
+{
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+   int         signum = POSTMASTER_DEATH_SIGNAL;
+
+   /* Register our signal handler. */
+   pqsignal(signum, postmaster_death_handler);
+
+   /* Request a signal on parent exit. */
+#if defined(PR_SET_PDEATHSIG)
+   if (prctl(PR_SET_PDEATHSIG, signum) < 0)
+       elog(ERROR, "could not request parent death signal: %m");
+#else
+#error "USE_POSTMASTER_DEATH_SIGNAL set, but there is no mechanism to request the signal"
+#endif
+
+   /*
+    * Just in case the parent was gone already and we missed it, we'd better
+    * check the slow way on the first call.
+    */
+   postmaster_possibly_dead = true;
+#endif                         /* USE_POSTMASTER_DEATH_SIGNAL */
+}
index 03b28c3604a9b65ec6f114f268db176f62361354..4bb28938c27b73a90096521f8ad0cbe96dae72eb 100644 (file)
@@ -43,6 +43,7 @@
 #include "storage/ipc.h"
 #include "storage/latch.h"
 #include "storage/pg_shmem.h"
+#include "storage/pmsignal.h"
 #include "storage/proc.h"
 #include "storage/procarray.h"
 #include "utils/builtins.h"
@@ -304,6 +305,9 @@ InitPostmasterChild(void)
    if (setsid() < 0)
        elog(FATAL, "setsid() failed: %m");
 #endif
+
+   /* Request a signal if the postmaster dies, if possible. */
+   PostmasterDeathSignalInit();
 }
 
 /*
index dcb25bb7234d6de4136ddc99ba261da2c9662d9f..4ef53416785062119f007b11d352478934fb8497 100644 (file)
 /* Define to 1 if you have the <sys/ipc.h> header file. */
 #undef HAVE_SYS_IPC_H
 
+/* Define to 1 if you have the <sys/prctl.h> header file. */
+#undef HAVE_SYS_PRCTL_H
+
 /* Define to 1 if you have the <sys/pstat.h> header file. */
 #undef HAVE_SYS_PSTAT_H
 
index 456b5c6b66d5aa0bd3f869441069fa3e7bd3f116..01cf8daf4385927deea726ee25e4d3eba1899ebb 100644 (file)
 /* Define to 1 if you have the <sys/ipc.h> header file. */
 /* #undef HAVE_SYS_IPC_H */
 
+/* Define to 1 if you have the <sys/prctl.h> header file. */
+/* #undef HAVE_SYS_PRCTL_H */
+
 /* Define to 1 if you have the <sys/pstat.h> header file. */
 /* #undef HAVE_SYS_PSTAT_H */
 
index bec162cc16db23b533bdaf9cff7dbe8da5d7da23..54e7108ac0b316176cdaf2631f7e276092ad9195 100644 (file)
 #ifndef PMSIGNAL_H
 #define PMSIGNAL_H
 
+#ifdef HAVE_SYS_PRCTL_H
+#include "sys/prctl.h"
+#endif
+
 /*
  * Reasons for signaling the postmaster.  We can cope with simultaneous
  * signals for different reasons.  If the same reason is signaled multiple
@@ -51,6 +55,33 @@ extern bool IsPostmasterChildWalSender(int slot);
 extern void MarkPostmasterChildActive(void);
 extern void MarkPostmasterChildInactive(void);
 extern void MarkPostmasterChildWalSender(void);
-extern bool PostmasterIsAlive(void);
+extern bool PostmasterIsAliveInternal(void);
+extern void PostmasterDeathSignalInit(void);
+
+
+/*
+ * Do we have a way to ask for a signal on parent death?
+ *
+ * If we do, pmsignal.c will set up a signal handler, that sets a flag when
+ * the parent dies.  Checking the flag first makes PostmasterIsAlive() a lot
+ * cheaper in usual case that the postmaster is alive.
+ */
+#if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_PDEATHSIG)
+#define USE_POSTMASTER_DEATH_SIGNAL
+#endif
+
+#ifdef USE_POSTMASTER_DEATH_SIGNAL
+extern volatile sig_atomic_t postmaster_possibly_dead;
+
+static inline bool
+PostmasterIsAlive(void)
+{
+   if (likely(!postmaster_possibly_dead))
+       return true;
+   return PostmasterIsAliveInternal();
+}
+#else
+#define PostmasterIsAlive() PostmasterIsAliveInternal()
+#endif
 
 #endif                         /* PMSIGNAL_H */