From 1a5fb654639ea5d2ecad085cc7c2d5d3d269f620 Mon Sep 17 00:00:00 2001
From: "Marc G. Fournier" <scrappy@hub.org>
Date: Tue, 25 Aug 1998 21:04:41 +0000
Subject: [PATCH] From: Massimo Dal Zotto <dz@cs.unitn.it>

assert.patch

        adds a switch to turn on/off the assert checking if enabled at compile
        time. You can now compile postgres with assert checking and disable it
        at runtime in a production environment.
---
 src/backend/postmaster/postmaster.c | 24 ++++++++++--
 src/backend/tcop/postgres.c         | 57 +++++++++++++++++++++++++++--
 src/backend/utils/init/enbl.c       |  4 +-
 src/include/c.h                     | 22 ++++++-----
 4 files changed, 89 insertions(+), 18 deletions(-)

diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 7f33dfedba3..0525282fd50 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -10,7 +10,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.93 1998/07/09 03:28:47 scrappy Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.94 1998/08/25 21:04:36 scrappy Exp $
  *
  * NOTES
  *
@@ -238,6 +238,9 @@ void GetCharSetByHost(char *, int, char *);
 
 #endif
 
+#ifdef USE_ASSERT_CHECKING
+int assert_enabled = 1;
+#endif
 
 static void
 checkDataDir(const char *DataDir, bool *DataDirOK)
@@ -295,8 +298,6 @@ checkDataDir(const char *DataDir, bool *DataDirOK)
 	}
 }
 
-
-
 int
 PostmasterMain(int argc, char *argv[])
 {
@@ -365,10 +366,22 @@ PostmasterMain(int argc, char *argv[])
 	DataDir = getenv("PGDATA"); /* default value */
 
 	opterr = 0;
-	while ((opt = getopt(nonblank_argc, argv, "a:B:b:D:dim:Mno:p:Ss")) != EOF)
+	while ((opt = getopt(nonblank_argc, argv,"A:a:B:b:D:dim:Mno:p:Ss")) != EOF)
 	{
 		switch (opt)
 		{
+			case 'A':
+#ifndef USE_ASSERT_CHECKING
+				fprintf(stderr, "Assert checking is not enabled\n");
+#else
+				/*
+				 * Pass this option also to each backend.
+				 */
+				assert_enabled = atoi(optarg);
+				strcat(ExtraOptions, " -A ");
+				strcat(ExtraOptions, optarg);
+#endif
+				break;
 			case 'a':
 				/* Can no longer set authentication method. */
 				break;
@@ -566,6 +579,9 @@ static void
 usage(const char *progname)
 {
 	fprintf(stderr, "usage: %s [options]\n", progname);
+#ifdef USE_ASSERT_CHECKING
+	fprintf(stderr, "\t-A [1|0]\tenable/disable runtime assert checking\n");
+#endif
 	fprintf(stderr, "\t-B nbufs\tset number of shared buffers\n");
 	fprintf(stderr, "\t-D datadir\tset data directory\n");
 	fprintf(stderr, "\t-S \t\tsilent mode (disassociate from tty)\n");
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index f7a80d326c6..838f4756d2a 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.84 1998/08/25 15:00:17 thomas Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.85 1998/08/25 21:04:38 scrappy Exp $
  *
  * NOTES
  *	  this is the "main" module of the postgres backend and
@@ -850,6 +850,9 @@ usage(char *progname)
 {
 	fprintf(stderr,
 			"Usage: %s [options] [dbname]\n", progname);
+#ifdef USE_ASSERT_CHECKING
+	fprintf(stderr, "    A: enable/disable assert checking\n");
+#endif
 	fprintf(stderr, "\t-B buffers\tset number of buffers in buffer pool\n");
 	fprintf(stderr, "\t-C \t\tsupress version info\n");
 	fprintf(stderr, "\t-D dir\t\tdata directory\n");
@@ -866,6 +869,7 @@ usage(char *progname)
 	fprintf(stderr, "\t-o file\t\tsend stdout and stderr to given filename \n");
 	fprintf(stderr, "\t-s \t\tshow stats after each query\n");
 	fprintf(stderr, "\t-v version\tset protocol version being used by frontend\n");
+	fprintf(stderr, "\t-W \t\twait N seconds to allow attach from a debugger\n");
 }
 
 /* ----------------------------------------------------------------
@@ -943,10 +947,22 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
 
     optind = 1; /* reset after postmaster usage */
 	
-	while ((flag = getopt(argc, argv, "B:bCD:d:Eef:iK:Lm:MNo:P:pQS:st:v:x:F"))
+	while ((flag = getopt(argc, argv,
+						  "A:B:bCD:d:Eef:iK:Lm:MNo:P:pQS:st:v:x:FW:"))
 		   != EOF)
 		switch (flag)
 		{
+			case 'A':
+				/* ----------------
+				 *  enable/disable assert checking.
+				 * ----------------
+				 */
+#ifdef USE_ASSERT_CHECKING
+				assert_enabled = atoi(optarg);
+#else
+				fprintf(stderr, "Assert checking is not enabled\n");
+#endif
+				break;
 
 			case 'b':
 				/* ----------------
@@ -955,6 +971,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
 				 */
 				BushyPlanFlag = 1;
 				break;
+
 			case 'B':
 				/* ----------------
 				 *	specify the size of buffer pool
@@ -1165,6 +1182,14 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
 				FrontendProtocol = (ProtocolVersion) atoi(optarg);
 				break;
 
+			case 'W':
+				/* ----------------
+				 *  wait N seconds to allow attach from a debugger
+				 * ----------------
+				 */
+				sleep(atoi(optarg));
+				break;
+
 			case 'x':
 #if 0							/* planner/xfunc.h */
 
@@ -1390,7 +1415,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
 	if (!IsUnderPostmaster)
 	{
 		puts("\nPOSTGRES backend interactive interface");
-		puts("$Revision: 1.84 $ $Date: 1998/08/25 15:00:17 $");
+		puts("$Revision: 1.85 $ $Date: 1998/08/25 21:04:38 $");
 	}
 
 	/* ----------------
@@ -1631,3 +1656,29 @@ ShowUsage(void)
 	PrintBufferUsage(StatFp);
 /*	   DisplayTupleCount(StatFp); */
 }
+
+#ifdef USE_ASSERT_CHECKING
+int
+assertEnable(int val)
+{
+	assert_enabled = val;
+	return val;
+}
+
+#ifdef ASSERT_CHECKING_TEST
+int
+assertTest(int val)
+{
+	Assert(val == 0);
+
+	if (assert_enabled) {
+		/* val != 0 should be trapped by previous Assert */
+		elog(NOTICE, "Assert test successfull (val = %d)", val);
+	} else {
+		elog(NOTICE, "Assert checking is disabled (val = %d)", val);
+	}
+
+	return val;
+}
+#endif
+#endif
diff --git a/src/backend/utils/init/enbl.c b/src/backend/utils/init/enbl.c
index dfcf295e34c..1c27a0dc489 100644
--- a/src/backend/utils/init/enbl.c
+++ b/src/backend/utils/init/enbl.c
@@ -7,11 +7,11 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/init/Attic/enbl.c,v 1.2 1997/09/07 04:53:42 momjian Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/init/Attic/enbl.c,v 1.3 1998/08/25 21:04:40 scrappy Exp $
  *
  *-------------------------------------------------------------------------
  */
-#include "c.h"
+#include "postgres.h"
 #include "utils/module.h"		/* where the declarations go */
 
 /*
diff --git a/src/include/c.h b/src/include/c.h
index 08d5ad1d2e5..65eb4266aea 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -7,7 +7,7 @@
  *
  * Copyright (c) 1994, Regents of the University of California
  *
- * $Id: c.h,v 1.42 1998/06/23 15:35:46 momjian Exp $
+ * $Id: c.h,v 1.43 1998/08/25 21:04:41 scrappy Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -583,12 +583,10 @@ typedef struct Exception
 } Exception;
 
 /*
- * USE_ASSERT_CHECKING, if defined, turns off all the assertions.
+ * USE_ASSERT_CHECKING, if defined, turns on all the assertions.
  * - plai  9/5/90
  *
- * It should _NOT_ be undef'ed in releases or in benchmark copies
- *
- * #undef USE_ASSERT_CHECKING
+ * It should _NOT_ be defined in releases or in benchmark copies
  */
 
 /*
@@ -597,7 +595,7 @@ typedef struct Exception
  *
  */
 #define Trap(condition, exception) \
-		{ if (condition) \
+		{ if ((assert_enabled) && (condition)) \
 				ExceptionalCondition(CppAsString(condition), &(exception), \
 						(char*)NULL, __FILE__, __LINE__); }
 
@@ -609,7 +607,7 @@ typedef struct Exception
  *	Isn't CPP fun?
  */
 #define TrapMacro(condition, exception) \
-	((bool) ((! condition) || \
+	((bool) ((! assert_enabled) || (! condition) || \
 			 (ExceptionalCondition(CppAsString(condition), \
 								  &(exception), \
 								  (char*) NULL, __FILE__, __LINE__))))
@@ -619,6 +617,7 @@ typedef struct Exception
 #define AssertMacro(condition)	(void)true
 #define AssertArg(condition)
 #define AssertState(condition)
+#define assert_enabled 0
 #else
 #define Assert(condition) \
 		Trap(!(condition), FailedAssertion)
@@ -632,6 +631,7 @@ typedef struct Exception
 #define AssertState(condition) \
 		Trap(!(condition), BadState)
 
+extern int assert_enabled;
 #endif							/* USE_ASSERT_CHECKING */
 
 /*
@@ -640,7 +640,7 @@ typedef struct Exception
  *
  */
 #define LogTrap(condition, exception, printArgs) \
-		{ if (condition) \
+		{ if ((assert_enabled) && (condition)) \
 				ExceptionalCondition(CppAsString(condition), &(exception), \
 						form printArgs, __FILE__, __LINE__); }
 
@@ -650,7 +650,7 @@ typedef struct Exception
  *		#define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
  */
 #define LogTrapMacro(condition, exception, printArgs) \
-	((bool) ((! condition) || \
+	((bool) ((! assert_enabled) || (! condition) || \
 			 (ExceptionalCondition(CppAsString(condition), \
 								   &(exception), \
 								   form printArgs, __FILE__, __LINE__))))
@@ -673,6 +673,10 @@ typedef struct Exception
 #define LogAssertState(condition, printArgs) \
 		LogTrap(!(condition), BadState, printArgs)
 
+extern int	assertEnable(int val);
+#ifdef ASSERT_CHECKING_TEST
+extern int	assertTest(int val);
+#endif
 #endif							/* USE_ASSERT_CHECKING */
 
 /* ----------------------------------------------------------------
-- 
GitLab