Skip to content

Commit 226e9ff

Browse files
committed
Call setrlimit if possible in pg_regress to allow core file generation, and provide a switch for similar behaviour in pg_ctl.
1 parent 138668c commit 226e9ff

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed

doc/src/sgml/ref/pg_ctl-ref.sgml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_ctl-ref.sgml,v 1.35 2006/12/02 00:34:52 petere Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_ctl-ref.sgml,v 1.36 2007/01/05 16:17:54 adunstan Exp $
33
PostgreSQL documentation
44
-->
55

@@ -29,6 +29,7 @@ PostgreSQL documentation
2929
<arg>-l <replaceable>filename</replaceable></arg>
3030
<arg>-o <replaceable>options</replaceable></arg>
3131
<arg>-p <replaceable>path</replaceable></arg>
32+
<arg>-c</arg>
3233
<sbr>
3334
<command>pg_ctl</command>
3435
<arg choice="plain">stop</arg>
@@ -48,6 +49,7 @@ PostgreSQL documentation
4849
<arg>-w</arg>
4950
<arg>-s</arg>
5051
<arg>-D <replaceable>datadir</replaceable></arg>
52+
<arg>-c</arg>
5153
<arg>-m
5254
<group choice="plain">
5355
<arg>s[mart]</arg>
@@ -245,6 +247,19 @@ PostgreSQL documentation
245247
</listitem>
246248
</varlistentry>
247249

250+
<varlistentry>
251+
<term><option>-c</option></term>
252+
<listitem>
253+
<para>
254+
Attempt to allow server crashes to produce core files, on platforms
255+
where this available, by lifting any soft resource limit placed on
256+
them.
257+
This is useful in debugging or diagnosing problems by allowing a
258+
stack trace to be obtained from a failed server process.
259+
</para>
260+
</listitem>
261+
</varlistentry>
262+
248263
<varlistentry>
249264
<term><option>-w</option></term>
250265
<listitem>

src/bin/pg_ctl/pg_ctl.c

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
66
*
7-
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.74 2006/10/12 05:14:49 tgl Exp $
7+
* $PostgreSQL: pgsql/src/bin/pg_ctl/pg_ctl.c,v 1.75 2007/01/05 16:17:55 adunstan Exp $
88
*
99
*-------------------------------------------------------------------------
1010
*/
@@ -26,6 +26,11 @@
2626
#include <sys/stat.h>
2727
#include <unistd.h>
2828

29+
#ifdef HAVE_SYS_RESOURCE_H
30+
#include <sys/time.h>
31+
#include <sys/resource.h>
32+
#endif
33+
2934
#include "libpq/pqsignal.h"
3035
#include "getopt_long.h"
3136

@@ -90,6 +95,7 @@ static char *register_servicename = "PostgreSQL"; /* FIXME: + version ID? */
9095
static char *register_username = NULL;
9196
static char *register_password = NULL;
9297
static char *argv0 = NULL;
98+
static bool allow_core_files = false;
9399

94100
static void
95101
write_stderr(const char *fmt,...)
@@ -132,6 +138,10 @@ static char postopts_file[MAXPGPATH];
132138
static char pid_file[MAXPGPATH];
133139
static char conf_file[MAXPGPATH];
134140

141+
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
142+
static void unlimit_core_size(void);
143+
#endif
144+
135145

136146
#if defined(WIN32) || defined(__CYGWIN__)
137147
static void
@@ -478,6 +488,27 @@ test_postmaster_connection(void)
478488
}
479489

480490

491+
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
492+
static void
493+
unlimit_core_size(void)
494+
{
495+
struct rlimit lim;
496+
getrlimit(RLIMIT_CORE,&lim);
497+
if (lim.rlim_max == 0)
498+
{
499+
write_stderr(_("%s: cannot set core size, disallowed by hard limit.\n"),
500+
progname);
501+
return;
502+
}
503+
else if (lim.rlim_max == RLIM_INFINITY || lim.rlim_cur < lim.rlim_max)
504+
{
505+
lim.rlim_cur = lim.rlim_max;
506+
setrlimit(RLIMIT_CORE,&lim);
507+
}
508+
}
509+
#endif
510+
511+
481512

482513
static void
483514
do_start(void)
@@ -581,6 +612,11 @@ do_start(void)
581612
postgres_path = postmaster_path;
582613
}
583614

615+
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
616+
if (allow_core_files)
617+
unlimit_core_size();
618+
#endif
619+
584620
exitcode = start_postmaster();
585621
if (exitcode != 0)
586622
{
@@ -1401,7 +1437,11 @@ do_help(void)
14011437
printf(_(" -o OPTIONS command line options to pass to postgres\n"
14021438
" (PostgreSQL server executable)\n"));
14031439
printf(_(" -p PATH-TO-POSTGRES normally not necessary\n"));
1404-
1440+
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
1441+
printf(_(" -c, --core-files allow postgres to produce core files\n"));
1442+
#else
1443+
printf(_(" -c, --core-files not applicable on this platform\n"));
1444+
#endif
14051445
printf(_("\nOptions for stop or restart:\n"));
14061446
printf(_(" -m SHUTDOWN-MODE may be \"smart\", \"fast\", or \"immediate\"\n"));
14071447

@@ -1497,6 +1537,7 @@ main(int argc, char **argv)
14971537
{"mode", required_argument, NULL, 'm'},
14981538
{"pgdata", required_argument, NULL, 'D'},
14991539
{"silent", no_argument, NULL, 's'},
1540+
{"core-files", no_argument, NULL, 'c'},
15001541
{NULL, 0, NULL, 0}
15011542
};
15021543

@@ -1561,7 +1602,7 @@ main(int argc, char **argv)
15611602
/* process command-line options */
15621603
while (optind < argc)
15631604
{
1564-
while ((c = getopt_long(argc, argv, "D:l:m:N:o:p:P:sU:wW", long_options, &option_index)) != -1)
1605+
while ((c = getopt_long(argc, argv, "cD:l:m:N:o:p:P:sU:wW", long_options, &option_index)) != -1)
15651606
{
15661607
switch (c)
15671608
{
@@ -1632,6 +1673,9 @@ main(int argc, char **argv)
16321673
do_wait = false;
16331674
wait_set = true;
16341675
break;
1676+
case 'c':
1677+
allow_core_files = true;
1678+
break;
16351679
default:
16361680
/* getopt_long already issued a suitable error message */
16371681
do_advice();

src/test/regress/pg_regress.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
14-
* $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.23 2006/10/04 00:30:14 momjian Exp $
14+
* $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.24 2007/01/05 16:17:55 adunstan Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -24,6 +24,11 @@
2424
#include <signal.h>
2525
#include <unistd.h>
2626

27+
#ifdef HAVE_SYS_RESOURCE_H
28+
#include <sys/time.h>
29+
#include <sys/resource.h>
30+
#endif
31+
2732
#include "getopt_long.h"
2833
#include "pg_config_paths.h"
2934

@@ -122,6 +127,30 @@ psql_command(const char *database, const char *query,...)
122127
the supplied arguments. */
123128
__attribute__((format(printf, 2, 3)));
124129

130+
/*
131+
* allow core files if possible.
132+
*/
133+
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
134+
static void
135+
unlimit_core_size(void)
136+
{
137+
struct rlimit lim;
138+
getrlimit(RLIMIT_CORE,&lim);
139+
if (lim.rlim_max == 0)
140+
{
141+
fprintf(stderr,
142+
_("%s: cannot set core size,: disallowed by hard limit.\n"),
143+
progname);
144+
return;
145+
}
146+
else if (lim.rlim_max == RLIM_INFINITY || lim.rlim_cur < lim.rlim_max)
147+
{
148+
lim.rlim_cur = lim.rlim_max;
149+
setrlimit(RLIMIT_CORE,&lim);
150+
}
151+
}
152+
#endif
153+
125154

126155
/*
127156
* Add an item at the end of a stringlist.
@@ -1459,6 +1488,10 @@ main(int argc, char *argv[])
14591488

14601489
initialize_environment();
14611490

1491+
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
1492+
unlimit_core_size();
1493+
#endif
1494+
14621495
if (temp_install)
14631496
{
14641497
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy