From 3c4ab3f51d927a75c97b59db3c7041ae3e2fa0a5 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Sun, 11 Apr 2004 00:54:45 +0000
Subject: [PATCH] Exit backend from SIGTERM or FATAL by simulating client EOF,
 rather than calling proc_exit() directly.  This should make SIGTERM more
 reliable.

---
 src/backend/tcop/postgres.c    | 10 +++++++---
 src/backend/utils/error/elog.c | 11 +++++++++--
 src/include/tcop/tcopprot.h    |  3 ++-
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 6cece54b30b..09fc57b6ea1 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.398 2004/04/07 05:05:49 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.399 2004/04/11 00:54:44 momjian Exp $
  *
  * NOTES
  *	  this is the "main" module of the postgres backend and
@@ -2938,7 +2938,10 @@ PostgresMain(int argc, char *argv[], const char *username)
 		/*
 		 * (3) read a command (loop blocks here)
 		 */
-		firstchar = ReadCommand(&input_message);
+		 if (!in_fatal_exit)
+			firstchar = ReadCommand(&input_message);
+		else
+			firstchar = EOF;
 
 		/*
 		 * (4) disable async signal conditions again.
@@ -3170,7 +3173,8 @@ PostgresMain(int argc, char *argv[], const char *username)
 				 * Otherwise it will fail to be called during other
 				 * backend-shutdown scenarios.
 				 */
-				proc_exit(0);
+				proc_exit(!in_fatal_exit ? 0 : proc_exit_inprogress ||
+												!IsUnderPostmaster);
 
 			case 'd':			/* copy data */
 			case 'c':			/* copy done */
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 96366333f06..e6f191d5df3 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.132 2004/04/05 03:02:06 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.133 2004/04/11 00:54:45 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -72,6 +72,8 @@ PGErrorVerbosity Log_error_verbosity = PGERROR_VERBOSE;
 char       *Log_line_prefix = NULL; /* format for extra log line info */
 unsigned int Log_destination;
 
+bool in_fatal_exit = false;
+
 #ifdef HAVE_SYSLOG
 char	   *Syslog_facility;	/* openlog() parameters */
 char	   *Syslog_ident;
@@ -442,7 +444,12 @@ errfinish(int dummy,...)
 			 */
 			fflush(stdout);
 			fflush(stderr);
-			proc_exit(proc_exit_inprogress || !IsUnderPostmaster);
+
+			if (in_fatal_exit)
+				ereport(PANIC, (errmsg("fatal error during fatal exit, giving up")));
+
+			/* We will exit the backend by simulating a client EOF */
+			in_fatal_exit = true;
 		}
 
 		/*
diff --git a/src/include/tcop/tcopprot.h b/src/include/tcop/tcopprot.h
index 046fe3e810e..97556ccbf1d 100644
--- a/src/include/tcop/tcopprot.h
+++ b/src/include/tcop/tcopprot.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/tcop/tcopprot.h,v 1.64 2004/04/07 05:05:50 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/tcop/tcopprot.h,v 1.65 2004/04/11 00:54:45 momjian Exp $
  *
  * OLD COMMENTS
  *	  This file was created so that other c files could get the two
@@ -34,6 +34,7 @@ extern bool log_hostname;
 extern DLLIMPORT const char *debug_query_string;
 extern char *rendezvous_name;
 extern int	max_stack_depth;
+extern bool in_fatal_exit;
 
 /* GUC-configurable parameters */
 
-- 
GitLab