Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1a5fb65

Browse files
committed
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.
1 parent 1682c36 commit 1a5fb65

File tree

4 files changed

+89
-18
lines changed

4 files changed

+89
-18
lines changed

src/backend/postmaster/postmaster.c

+20-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.93 1998/07/09 03:28:47 scrappy Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.94 1998/08/25 21:04:36 scrappy Exp $
1414
*
1515
* NOTES
1616
*
@@ -238,6 +238,9 @@ void GetCharSetByHost(char *, int, char *);
238238

239239
#endif
240240

241+
#ifdef USE_ASSERT_CHECKING
242+
int assert_enabled = 1;
243+
#endif
241244

242245
static void
243246
checkDataDir(const char *DataDir, bool *DataDirOK)
@@ -295,8 +298,6 @@ checkDataDir(const char *DataDir, bool *DataDirOK)
295298
}
296299
}
297300

298-
299-
300301
int
301302
PostmasterMain(int argc, char *argv[])
302303
{
@@ -365,10 +366,22 @@ PostmasterMain(int argc, char *argv[])
365366
DataDir = getenv("PGDATA"); /* default value */
366367

367368
opterr = 0;
368-
while ((opt = getopt(nonblank_argc, argv, "a:B:b:D:dim:Mno:p:Ss")) != EOF)
369+
while ((opt = getopt(nonblank_argc, argv,"A:a:B:b:D:dim:Mno:p:Ss")) != EOF)
369370
{
370371
switch (opt)
371372
{
373+
case 'A':
374+
#ifndef USE_ASSERT_CHECKING
375+
fprintf(stderr, "Assert checking is not enabled\n");
376+
#else
377+
/*
378+
* Pass this option also to each backend.
379+
*/
380+
assert_enabled = atoi(optarg);
381+
strcat(ExtraOptions, " -A ");
382+
strcat(ExtraOptions, optarg);
383+
#endif
384+
break;
372385
case 'a':
373386
/* Can no longer set authentication method. */
374387
break;
@@ -566,6 +579,9 @@ static void
566579
usage(const char *progname)
567580
{
568581
fprintf(stderr, "usage: %s [options]\n", progname);
582+
#ifdef USE_ASSERT_CHECKING
583+
fprintf(stderr, "\t-A [1|0]\tenable/disable runtime assert checking\n");
584+
#endif
569585
fprintf(stderr, "\t-B nbufs\tset number of shared buffers\n");
570586
fprintf(stderr, "\t-D datadir\tset data directory\n");
571587
fprintf(stderr, "\t-S \t\tsilent mode (disassociate from tty)\n");

src/backend/tcop/postgres.c

+54-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.84 1998/08/25 15:00:17 thomas Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.85 1998/08/25 21:04:38 scrappy Exp $
1111
*
1212
* NOTES
1313
* this is the "main" module of the postgres backend and
@@ -850,6 +850,9 @@ usage(char *progname)
850850
{
851851
fprintf(stderr,
852852
"Usage: %s [options] [dbname]\n", progname);
853+
#ifdef USE_ASSERT_CHECKING
854+
fprintf(stderr, " A: enable/disable assert checking\n");
855+
#endif
853856
fprintf(stderr, "\t-B buffers\tset number of buffers in buffer pool\n");
854857
fprintf(stderr, "\t-C \t\tsupress version info\n");
855858
fprintf(stderr, "\t-D dir\t\tdata directory\n");
@@ -866,6 +869,7 @@ usage(char *progname)
866869
fprintf(stderr, "\t-o file\t\tsend stdout and stderr to given filename \n");
867870
fprintf(stderr, "\t-s \t\tshow stats after each query\n");
868871
fprintf(stderr, "\t-v version\tset protocol version being used by frontend\n");
872+
fprintf(stderr, "\t-W \t\twait N seconds to allow attach from a debugger\n");
869873
}
870874

871875
/* ----------------------------------------------------------------
@@ -943,10 +947,22 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
943947

944948
optind = 1; /* reset after postmaster usage */
945949

946-
while ((flag = getopt(argc, argv, "B:bCD:d:Eef:iK:Lm:MNo:P:pQS:st:v:x:F"))
950+
while ((flag = getopt(argc, argv,
951+
"A:B:bCD:d:Eef:iK:Lm:MNo:P:pQS:st:v:x:FW:"))
947952
!= EOF)
948953
switch (flag)
949954
{
955+
case 'A':
956+
/* ----------------
957+
* enable/disable assert checking.
958+
* ----------------
959+
*/
960+
#ifdef USE_ASSERT_CHECKING
961+
assert_enabled = atoi(optarg);
962+
#else
963+
fprintf(stderr, "Assert checking is not enabled\n");
964+
#endif
965+
break;
950966

951967
case 'b':
952968
/* ----------------
@@ -955,6 +971,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
955971
*/
956972
BushyPlanFlag = 1;
957973
break;
974+
958975
case 'B':
959976
/* ----------------
960977
* specify the size of buffer pool
@@ -1165,6 +1182,14 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
11651182
FrontendProtocol = (ProtocolVersion) atoi(optarg);
11661183
break;
11671184

1185+
case 'W':
1186+
/* ----------------
1187+
* wait N seconds to allow attach from a debugger
1188+
* ----------------
1189+
*/
1190+
sleep(atoi(optarg));
1191+
break;
1192+
11681193
case 'x':
11691194
#if 0 /* planner/xfunc.h */
11701195

@@ -1390,7 +1415,7 @@ PostgresMain(int argc, char *argv[], int real_argc, char *real_argv[])
13901415
if (!IsUnderPostmaster)
13911416
{
13921417
puts("\nPOSTGRES backend interactive interface");
1393-
puts("$Revision: 1.84 $ $Date: 1998/08/25 15:00:17 $");
1418+
puts("$Revision: 1.85 $ $Date: 1998/08/25 21:04:38 $");
13941419
}
13951420

13961421
/* ----------------
@@ -1631,3 +1656,29 @@ ShowUsage(void)
16311656
PrintBufferUsage(StatFp);
16321657
/* DisplayTupleCount(StatFp); */
16331658
}
1659+
1660+
#ifdef USE_ASSERT_CHECKING
1661+
int
1662+
assertEnable(int val)
1663+
{
1664+
assert_enabled = val;
1665+
return val;
1666+
}
1667+
1668+
#ifdef ASSERT_CHECKING_TEST
1669+
int
1670+
assertTest(int val)
1671+
{
1672+
Assert(val == 0);
1673+
1674+
if (assert_enabled) {
1675+
/* val != 0 should be trapped by previous Assert */
1676+
elog(NOTICE, "Assert test successfull (val = %d)", val);
1677+
} else {
1678+
elog(NOTICE, "Assert checking is disabled (val = %d)", val);
1679+
}
1680+
1681+
return val;
1682+
}
1683+
#endif
1684+
#endif

src/backend/utils/init/enbl.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/utils/init/Attic/enbl.c,v 1.2 1997/09/07 04:53:42 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/utils/init/Attic/enbl.c,v 1.3 1998/08/25 21:04:40 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
14-
#include "c.h"
14+
#include "postgres.h"
1515
#include "utils/module.h" /* where the declarations go */
1616

1717
/*

src/include/c.h

+13-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: c.h,v 1.42 1998/06/23 15:35:46 momjian Exp $
10+
* $Id: c.h,v 1.43 1998/08/25 21:04:41 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -583,12 +583,10 @@ typedef struct Exception
583583
} Exception;
584584

585585
/*
586-
* USE_ASSERT_CHECKING, if defined, turns off all the assertions.
586+
* USE_ASSERT_CHECKING, if defined, turns on all the assertions.
587587
* - plai 9/5/90
588588
*
589-
* It should _NOT_ be undef'ed in releases or in benchmark copies
590-
*
591-
* #undef USE_ASSERT_CHECKING
589+
* It should _NOT_ be defined in releases or in benchmark copies
592590
*/
593591

594592
/*
@@ -597,7 +595,7 @@ typedef struct Exception
597595
*
598596
*/
599597
#define Trap(condition, exception) \
600-
{ if (condition) \
598+
{ if ((assert_enabled) && (condition)) \
601599
ExceptionalCondition(CppAsString(condition), &(exception), \
602600
(char*)NULL, __FILE__, __LINE__); }
603601

@@ -609,7 +607,7 @@ typedef struct Exception
609607
* Isn't CPP fun?
610608
*/
611609
#define TrapMacro(condition, exception) \
612-
((bool) ((! condition) || \
610+
((bool) ((! assert_enabled) || (! condition) || \
613611
(ExceptionalCondition(CppAsString(condition), \
614612
&(exception), \
615613
(char*) NULL, __FILE__, __LINE__))))
@@ -619,6 +617,7 @@ typedef struct Exception
619617
#define AssertMacro(condition) (void)true
620618
#define AssertArg(condition)
621619
#define AssertState(condition)
620+
#define assert_enabled 0
622621
#else
623622
#define Assert(condition) \
624623
Trap(!(condition), FailedAssertion)
@@ -632,6 +631,7 @@ typedef struct Exception
632631
#define AssertState(condition) \
633632
Trap(!(condition), BadState)
634633

634+
extern int assert_enabled;
635635
#endif /* USE_ASSERT_CHECKING */
636636

637637
/*
@@ -640,7 +640,7 @@ typedef struct Exception
640640
*
641641
*/
642642
#define LogTrap(condition, exception, printArgs) \
643-
{ if (condition) \
643+
{ if ((assert_enabled) && (condition)) \
644644
ExceptionalCondition(CppAsString(condition), &(exception), \
645645
form printArgs, __FILE__, __LINE__); }
646646

@@ -650,7 +650,7 @@ typedef struct Exception
650650
* #define foo(x) (LogAssertMacro(x != 0, "yow!") && bar(x))
651651
*/
652652
#define LogTrapMacro(condition, exception, printArgs) \
653-
((bool) ((! condition) || \
653+
((bool) ((! assert_enabled) || (! condition) || \
654654
(ExceptionalCondition(CppAsString(condition), \
655655
&(exception), \
656656
form printArgs, __FILE__, __LINE__))))
@@ -673,6 +673,10 @@ typedef struct Exception
673673
#define LogAssertState(condition, printArgs) \
674674
LogTrap(!(condition), BadState, printArgs)
675675

676+
extern int assertEnable(int val);
677+
#ifdef ASSERT_CHECKING_TEST
678+
extern int assertTest(int val);
679+
#endif
676680
#endif /* USE_ASSERT_CHECKING */
677681

678682
/* ----------------------------------------------------------------

0 commit comments

Comments
 (0)