0001-Create-a-new-session-in-postmaster-by-calling-setsid.patch

application/octet-stream

Filename: 0001-Create-a-new-session-in-postmaster-by-calling-setsid.patch
Type: application/octet-stream
Part: 0
Message: [Patch] Create a new session in postmaster by calling setsid()

Patch

Format: format-patch
Series: patch 0001
Subject: Create a new session in postmaster by calling setsid().
File+
src/backend/postmaster/postmaster.c 5 0
From ee5aa1753d48429de0338401039bfcc45a3c5c60 Mon Sep 17 00:00:00 2001
From: Paul Guo <pguo@pivotal.io>
Date: Fri, 27 Jul 2018 18:11:31 +0800
Subject: [PATCH] Create a new session in postmaster by calling setsid().

postmaster children have done this by calling InitPostmasterChild(),
but postmaster itself does not. This could lead to some issues.
For example, running the script below and press ctrl+c to
terminate the pg_sleep() sql could lead to postmaster down.

$ cat test.sh
pg_ctl -D ./data -l stop.log stop
sleep 2 -- wait for pg down.
pg_ctl -D ./data -l start.log start
sleep 2 -- wait for pg up.
echo "pg_sleep()-ing"
psql -d postgres -c 'select pg_sleep(1000)'  -- press ctrl+c, then you will see postmaster and its children are all gone.
---
 src/backend/postmaster/postmaster.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index a4b53b33cd..2648ef1092 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -941,6 +941,11 @@ PostmasterMain(int argc, char *argv[])
 				(errmsg_internal("-----------------------------------------")));
 	}
 
+#ifdef HAVE_SETSID
+	if (setsid() < 0)
+		elog(FATAL, "setsid() failed: %m");
+#endif
+
 	/*
 	 * Create lockfile for data directory.
 	 *
-- 
2.14.3