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

Commit f9f2582

Browse files
committed
Create a GUC parameter max_files_per_process that is a configurable
upper limit on what we will believe from sysconf(_SC_OPEN_MAX). The default value is 1000, so that under ordinary conditions it won't affect the behavior. But on platforms where the kernel promises far more than it can deliver, this can be used to prevent running out of file descriptors. See numerous past discussions, eg, pgsql-hackers around 23-Dec-2000.
1 parent 40ed132 commit f9f2582

File tree

5 files changed

+73
-18
lines changed

5 files changed

+73
-18
lines changed

doc/src/sgml/runtime.sgml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.85 2001/09/23 21:52:36 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.86 2001/09/30 18:57:45 tgl Exp $
33
-->
44

55
<Chapter Id="runtime">
@@ -1218,6 +1218,26 @@ dynamic_library_path = '/usr/local/lib/postgresql:/home/my_project/lib:$libdir'
12181218
</listitem>
12191219
</varlistentry>
12201220

1221+
<varlistentry>
1222+
<term><varname>MAX_FILES_PER_PROCESS</varname> (<type>integer</type>)</term>
1223+
<listitem>
1224+
<para>
1225+
Sets the maximum number of simultaneously open files in each server
1226+
process. The default is 1000. The limit actually used by the code
1227+
is the smaller of this setting and the result of
1228+
<literal>sysconf(_SC_OPEN_MAX)</literal>.
1229+
Therefore, on systems where sysconf returns a reasonable limit,
1230+
you don't need to worry about this setting. But on some platforms
1231+
(notably, most BSD systems), sysconf returns a value that is much
1232+
larger than the system can really support when a large number of
1233+
processes all try to open that many files. If you find yourself
1234+
seeing <quote>Too many open files</> failures, try reducing this
1235+
setting.
1236+
This option can only be set at server start.
1237+
</para>
1238+
</listitem>
1239+
</varlistentry>
1240+
12211241
<varlistentry>
12221242
<term><varname>MAX_FSM_RELATIONS</varname> (<type>integer</type>)</term>
12231243
<listitem>

src/backend/storage/file/fd.c

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/file/fd.c,v 1.83 2001/08/04 19:42:34 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/file/fd.c,v 1.84 2001/09/30 18:57:45 tgl Exp $
1111
*
1212
* NOTES:
1313
*
@@ -69,7 +69,7 @@
6969
*
7070
* (Even though most dynamic loaders now use dlopen(3) or the
7171
* equivalent, the OS must still open several files to perform the
72-
* dynamic loading. Keep this here.)
72+
* dynamic loading. And stdin/stdout/stderr count too. Keep this here.)
7373
*/
7474
#ifndef RESERVE_FOR_LD
7575
#define RESERVE_FOR_LD 10
@@ -87,6 +87,14 @@
8787
#define FD_MINFREE 10
8888
#endif
8989

90+
/*
91+
* A number of platforms return values for sysconf(_SC_OPEN_MAX) that are
92+
* far beyond what they can really support. This GUC parameter limits what
93+
* we will believe.
94+
*/
95+
int max_files_per_process = 1000;
96+
97+
9098
/* Debugging.... */
9199

92100
#ifdef FDDEBUG
@@ -281,29 +289,46 @@ pg_nofile(void)
281289
{
282290
static long no_files = 0;
283291

292+
/* need do this calculation only once */
284293
if (no_files == 0)
285294
{
286-
/* need do this calculation only once */
287-
#ifndef HAVE_SYSCONF
288-
no_files = (long) NOFILE;
289-
#else
295+
/*
296+
* Ask the system what its files-per-process limit is.
297+
*/
298+
#ifdef HAVE_SYSCONF
290299
no_files = sysconf(_SC_OPEN_MAX);
291-
if (no_files == -1)
300+
if (no_files <= 0)
292301
{
293-
/* tweak for Hurd, which does not support NOFILE */
294302
#ifdef NOFILE
295-
elog(DEBUG, "pg_nofile: Unable to get _SC_OPEN_MAX using sysconf(); using %d", NOFILE);
296303
no_files = (long) NOFILE;
297304
#else
298-
elog(FATAL, "pg_nofile: Unable to get _SC_OPEN_MAX using sysconf() and NOFILE is undefined");
305+
no_files = (long) max_files_per_process;
299306
#endif
307+
elog(DEBUG, "pg_nofile: sysconf(_SC_OPEN_MAX) failed; using %ld",
308+
no_files);
300309
}
310+
#else /* !HAVE_SYSCONF */
311+
#ifdef NOFILE
312+
no_files = (long) NOFILE;
313+
#else
314+
no_files = (long) max_files_per_process;
301315
#endif
316+
#endif /* HAVE_SYSCONF */
317+
318+
/*
319+
* Some platforms return hopelessly optimistic values. Apply a
320+
* configurable upper limit.
321+
*/
322+
if (no_files > (long) max_files_per_process)
323+
no_files = (long) max_files_per_process;
302324

325+
/*
326+
* Make sure we have enough to get by after reserving some for LD.
327+
*/
303328
if ((no_files - RESERVE_FOR_LD) < FD_MINFREE)
304-
elog(FATAL, "pg_nofile: insufficient File Descriptors in postmaster to start backend (%ld).\n"
305-
" O/S allows %ld, Postmaster reserves %d, We need %d (MIN) after that.",
306-
no_files - RESERVE_FOR_LD, no_files, RESERVE_FOR_LD, FD_MINFREE);
329+
elog(FATAL, "pg_nofile: insufficient file descriptors available to start backend.\n"
330+
"\tSystem allows %ld, we need at least %d.",
331+
no_files, RESERVE_FOR_LD + FD_MINFREE);
307332

308333
no_files -= RESERVE_FOR_LD;
309334
}

src/backend/utils/misc/guc.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Support for grand unified configuration scheme, including SET
55
* command, configuration file, and command line options.
66
*
7-
* $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.53 2001/09/29 04:02:25 tgl Exp $
7+
* $Header: /cvsroot/pgsql/src/backend/utils/misc/guc.c,v 1.54 2001/09/30 18:57:45 tgl Exp $
88
*
99
* Copyright 2000 by PostgreSQL Global Development Group
1010
* Written by Peter Eisentraut <peter_e@gmx.net>.
@@ -31,6 +31,7 @@
3131
#include "optimizer/paths.h"
3232
#include "optimizer/planmain.h"
3333
#include "parser/parse_expr.h"
34+
#include "storage/fd.h"
3435
#include "storage/freespace.h"
3536
#include "storage/lock.h"
3637
#include "storage/proc.h"
@@ -302,6 +303,9 @@ static struct config_int
302303
{"vacuum_mem", PGC_USERSET, &VacuumMem,
303304
8192, 1024, INT_MAX, NULL, NULL},
304305

306+
{"max_files_per_process", PGC_BACKEND, &max_files_per_process,
307+
1000, 25, INT_MAX, NULL, NULL},
308+
305309
{"debug_level", PGC_USERSET, &DebugLvl,
306310
0, 0, 16, NULL, NULL},
307311

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,11 @@
176176
#
177177
#dynamic_library_path = '$libdir'
178178
#australian_timezones = false
179-
#authentication_timeout = 60 # min 1, max 600
179+
#authentication_timeout = 60 # min 1, max 600
180180
#deadlock_timeout = 1000
181181
#default_transaction_isolation = 'read committed'
182-
#max_expr_depth = 10000 # min 10
182+
#max_expr_depth = 10000 # min 10
183+
#max_files_per_process = 1000 # min 25
183184
#password_encryption = false
184185
#sql_inheritance = true
185186
#transform_null_equals = false

src/include/storage/fd.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: fd.h,v 1.30 2001/06/11 04:12:29 tgl Exp $
10+
* $Id: fd.h,v 1.31 2001/09/30 18:57:45 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -43,6 +43,11 @@ typedef char *FileName;
4343

4444
typedef int File;
4545

46+
47+
/* GUC parameter */
48+
extern int max_files_per_process;
49+
50+
4651
/*
4752
* prototypes for functions in fd.c
4853
*/

0 commit comments

Comments
 (0)