Skip to content

Commit de98a7e

Browse files
committed
The default values for shared_buffers and max_connections are now 1000
and 100 respectively, if the platform will allow it. initdb selects values that are not too large to allow the postmaster to start, and places these values in the installed postgresql.conf file. This allows us to continue to start up out-of-the-box on platforms with small SHMMAX, while having somewhat-realistic default settings on platforms with reasonable SHMMAX. Per recent pghackers discussion.
1 parent 8460000 commit de98a7e

File tree

5 files changed

+97
-88
lines changed

5 files changed

+97
-88
lines changed

doc/src/sgml/runtime.sgml

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.192 2003/07/09 08:52:56 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/runtime.sgml,v 1.193 2003/07/14 20:00:22 tgl Exp $
33
-->
44

55
<Chapter Id="runtime">
@@ -608,9 +608,18 @@ SET ENABLE_SEQSCAN TO OFF;
608608
<listitem>
609609
<para>
610610
Determines the maximum number of concurrent connections to the
611-
database server. The default is 32 (unless altered while
612-
building the server). This parameter can only be set at server
613-
start.
611+
database server. The default is typically 100, but may be less
612+
if your kernel settings will not support it (as determined
613+
during <application>initdb</>).
614+
This parameter can only be set at server start.
615+
</para>
616+
617+
<para>
618+
Increasing this parameter may cause <productname>PostgreSQL</>
619+
to request more <systemitem class="osname">System V</> shared
620+
memory or semaphores than your operating system's default configuration
621+
allows. See <xref linkend="sysvipc"> for information on how to
622+
adjust these parameters, if necessary.
614623
</para>
615624
</listitem>
616625
</varlistentry>
@@ -831,13 +840,16 @@ SET ENABLE_SEQSCAN TO OFF;
831840
<listitem>
832841
<para>
833842
Sets the number of shared memory buffers used by the database
834-
server. The default is 64. Each buffer is typically 8192
835-
bytes. This must be greater than 16, as well as at least twice
836-
the value of <varname>MAX_CONNECTIONS</varname>; however, a
837-
higher value can often improve performance.
838-
Values of a few thousand are recommended
839-
for production installations. This option can only be set at
840-
server start.
843+
server. The default is typically 1000, but may be less
844+
if your kernel settings will not support it (as determined
845+
during <application>initdb</>). Each buffer is 8192
846+
bytes, unless a different value of <literal>BLCKSZ</> was chosen
847+
when building the server. This setting must be at least 16,
848+
as well as at least twice the value of
849+
<varname>MAX_CONNECTIONS</varname>; however, settings significantly
850+
higher than the minimum are usually needed for good performance.
851+
Values of a few thousand are recommended for production installations.
852+
This option can only be set at server start.
841853
</para>
842854

843855
<para>
@@ -2796,7 +2808,7 @@ $ <userinput>postmaster -o '-S 1024 -s'</userinput>
27962808
number</quote>, to detect collision with semaphore sets used by
27972809
other applications. The maximum number of semaphores in the system
27982810
is set by <varname>SEMMNS</>, which consequently must be at least
2799-
as high as the connection setting plus one extra for each 16
2811+
as high as <literal>max_connections</> plus one extra for each 16
28002812
allowed connections (see the formula in <xref
28012813
linkend="sysvipc-parameters">). The parameter <varname>SEMMNI</>
28022814
determines the limit on the number of semaphore sets that can

src/backend/port/sysv_shmem.c

Lines changed: 25 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Portions Copyright (c) 1994, Regents of the University of California
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.10 2003/05/08 19:17:07 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.11 2003/07/14 20:00:22 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -45,10 +45,8 @@ void *UsedShmemSegAddr = NULL;
4545
static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size);
4646
static void IpcMemoryDetach(int status, Datum shmaddr);
4747
static void IpcMemoryDelete(int status, Datum shmId);
48-
static void *PrivateMemoryCreate(uint32 size);
49-
static void PrivateMemoryDelete(int status, Datum memaddr);
5048
static PGShmemHeader *PGSharedMemoryAttach(IpcMemoryKey key,
51-
IpcMemoryId *shmid, void *addr);
49+
IpcMemoryId *shmid);
5250

5351

5452
/*
@@ -243,41 +241,6 @@ PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2)
243241
}
244242

245243

246-
/* ----------------------------------------------------------------
247-
* private memory support
248-
*
249-
* Rather than allocating shmem segments with IPC_PRIVATE key, we
250-
* just malloc() the requested amount of space. This code emulates
251-
* the needed shmem functions.
252-
* ----------------------------------------------------------------
253-
*/
254-
255-
static void *
256-
PrivateMemoryCreate(uint32 size)
257-
{
258-
void *memAddress;
259-
260-
memAddress = malloc(size);
261-
if (!memAddress)
262-
{
263-
fprintf(stderr, "PrivateMemoryCreate: malloc(%u) failed\n", size);
264-
proc_exit(1);
265-
}
266-
MemSet(memAddress, 0, size); /* keep Purify quiet */
267-
268-
/* Register on-exit routine to release storage */
269-
on_shmem_exit(PrivateMemoryDelete, PointerGetDatum(memAddress));
270-
271-
return memAddress;
272-
}
273-
274-
static void
275-
PrivateMemoryDelete(int status, Datum memaddr)
276-
{
277-
free(DatumGetPointer(memaddr));
278-
}
279-
280-
281244
/*
282245
* PGSharedMemoryCreate
283246
*
@@ -289,6 +252,9 @@ PrivateMemoryDelete(int status, Datum memaddr)
289252
* collision with non-Postgres shmem segments. The idea here is to detect and
290253
* re-use keys that may have been assigned by a crashed postmaster or backend.
291254
*
255+
* makePrivate means to always create a new segment, rather than attach to
256+
* or recycle any existing segment.
257+
*
292258
* The port number is passed for possible use as a key (for SysV, we use
293259
* it to generate the starting shmem key). In a standalone backend,
294260
* zero will be passed.
@@ -307,8 +273,7 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
307273
/* Just attach and return the pointer */
308274
if (ExecBackend && UsedShmemSegAddr != NULL && !makePrivate)
309275
{
310-
if ((hdr = (PGShmemHeader *) memAddress = PGSharedMemoryAttach(
311-
UsedShmemSegID, &shmid, UsedShmemSegAddr)) == NULL)
276+
if ((hdr = PGSharedMemoryAttach(UsedShmemSegID, &shmid)) == NULL)
312277
{
313278
fprintf(stderr, "Unable to attach to proper memory at fixed address: shmget(key=%d, addr=%p) failed: %s\n",
314279
(int) UsedShmemSegID, UsedShmemSegAddr, strerror(errno));
@@ -317,34 +282,29 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
317282
return hdr;
318283
}
319284

320-
/* Create shared memory */
321-
322-
NextShmemSegID = port * 1000 + 1;
285+
/* Loop till we find a free IPC key */
286+
NextShmemSegID = port * 1000;
323287

324-
for (;;NextShmemSegID++)
288+
for (NextShmemSegID++;; NextShmemSegID++)
325289
{
326-
/* Special case if creating a private segment --- just malloc() it */
327-
if (makePrivate)
328-
{
329-
memAddress = PrivateMemoryCreate(size);
330-
break;
331-
}
332-
333290
/* Try to create new segment */
334291
memAddress = InternalIpcMemoryCreate(NextShmemSegID, size);
335292
if (memAddress)
336293
break; /* successful create and attach */
337294

338295
/* Check shared memory and possibly remove and recreate */
339-
340-
if ((hdr = (PGShmemHeader *) memAddress = PGSharedMemoryAttach(
341-
NextShmemSegID, &shmid, UsedShmemSegAddr)) == NULL)
296+
297+
if (makePrivate) /* a standalone backend shouldn't do this */
298+
continue;
299+
300+
if ((memAddress = PGSharedMemoryAttach(NextShmemSegID, &shmid)) == NULL)
342301
continue; /* can't attach, not one of mine */
343302

344303
/*
345304
* If I am not the creator and it belongs to an extant process,
346305
* continue.
347306
*/
307+
hdr = (PGShmemHeader *) memAddress;
348308
if (hdr->creatorPID != getpid())
349309
{
350310
if (kill(hdr->creatorPID, 0) == 0 || errno != ESRCH)
@@ -407,22 +367,25 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
407367

408368

409369
/*
410-
* Attach to shared memory and make sure it has a Postgres header
370+
* Attach to shared memory and make sure it has a Postgres header
371+
*
372+
* Returns attach address if OK, else NULL
411373
*/
412374
static PGShmemHeader *
413-
PGSharedMemoryAttach(IpcMemoryKey key, IpcMemoryId *shmid, void *addr)
375+
PGSharedMemoryAttach(IpcMemoryKey key, IpcMemoryId *shmid)
414376
{
415377
PGShmemHeader *hdr;
416378

417379
if ((*shmid = shmget(key, sizeof(PGShmemHeader), 0)) < 0)
418380
return NULL;
419381

420-
hdr = (PGShmemHeader *) shmat(*shmid, UsedShmemSegAddr,
382+
hdr = (PGShmemHeader *) shmat(*shmid,
383+
UsedShmemSegAddr,
421384
#if defined(solaris) && defined(__sparc__)
422-
/* use intimate shared memory on SPARC Solaris */
423-
SHM_SHARE_MMU
385+
/* use intimate shared memory on Solaris */
386+
SHM_SHARE_MMU
424387
#else
425-
0
388+
0
426389
#endif
427390
);
428391

@@ -434,5 +397,6 @@ PGSharedMemoryAttach(IpcMemoryKey key, IpcMemoryId *shmid, void *addr)
434397
shmdt(hdr);
435398
return NULL; /* segment belongs to a non-Postgres app */
436399
}
400+
437401
return hdr;
438402
}

src/backend/utils/init/postinit.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.122 2003/06/27 14:45:30 petere Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.123 2003/07/14 20:00:22 tgl Exp $
1212
*
1313
*
1414
*-------------------------------------------------------------------------
@@ -176,12 +176,8 @@ InitCommunication(void)
176176
{
177177
/*
178178
* We're running a postgres bootstrap process or a standalone backend.
179-
* Create private "shmem" and semaphores. Force MaxBackends to 1 so
180-
* that we don't allocate more resources than necessary.
179+
* Create private "shmem" and semaphores.
181180
*/
182-
SetConfigOption("max_connections", "1",
183-
PGC_POSTMASTER, PGC_S_OVERRIDE);
184-
185181
CreateSharedMemoryAndSemaphores(true, MaxBackends, 0);
186182
}
187183
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@
210210
#client_encoding = sql_ascii # actually, defaults to database encoding
211211

212212
# These settings are initialized by initdb -- they may be changed
213+
#lc_messages = 'C' # locale for system error message strings
214+
#lc_monetary = 'C' # locale for monetary formatting
215+
#lc_numeric = 'C' # locale for number formatting
216+
#lc_time = 'C' # locale for time formatting
213217

214218
# Other Defaults
215219

src/bin/initdb/initdb.sh

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
2828
# Portions Copyright (c) 1994, Regents of the University of California
2929
#
30-
# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.193 2003/07/04 16:41:21 tgl Exp $
30+
# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.194 2003/07/14 20:00:23 tgl Exp $
3131
#
3232
#-------------------------------------------------------------------------
3333

@@ -577,6 +577,40 @@ echo "$short_version" > "$PGDATA/base/1/PG_VERSION" || exit_nicely
577577

578578
echo "ok"
579579

580+
##########################################################################
581+
#
582+
# DETERMINE PLATFORM-SPECIFIC CONFIG SETTINGS
583+
#
584+
# Use reasonable values if kernel will let us, else scale back
585+
586+
cp /dev/null "$PGDATA"/postgresql.conf || exit_nicely
587+
588+
$ECHO_N "selecting default shared_buffers... "$ECHO_C
589+
590+
for nbuffers in 1000 900 800 700 600 500 400 300 200 100 50
591+
do
592+
TEST_OPT="$PGSQL_OPT -c shared_buffers=$nbuffers -c max_connections=5"
593+
if "$PGPATH"/postgres $TEST_OPT template1 </dev/null >/dev/null 2>&1
594+
then
595+
break
596+
fi
597+
done
598+
599+
echo "$nbuffers"
600+
601+
$ECHO_N "selecting default max_connections... "$ECHO_C
602+
603+
for nconns in 100 50 40 30 20 10
604+
do
605+
TEST_OPT="$PGSQL_OPT -c shared_buffers=$nbuffers -c max_connections=$nconns"
606+
if "$PGPATH"/postgres $TEST_OPT template1 </dev/null >/dev/null 2>&1
607+
then
608+
break
609+
fi
610+
done
611+
612+
echo "$nconns"
613+
580614
##########################################################################
581615
#
582616
# CREATE CONFIG FILES
@@ -585,14 +619,13 @@ $ECHO_N "creating configuration files... "$ECHO_C
585619

586620
cp "$PG_HBA_SAMPLE" "$PGDATA"/pg_hba.conf || exit_nicely
587621
cp "$PG_IDENT_SAMPLE" "$PGDATA"/pg_ident.conf || exit_nicely
588-
(
589-
trigger="# These settings are initialized by initdb -- they may be changed"
590-
sed -n "1,/$trigger/p" "$POSTGRESQL_CONF_SAMPLE"
591-
for cat in MESSAGES MONETARY NUMERIC TIME; do
592-
echo "LC_$cat = '`pg_getlocale $cat`'"
593-
done
594-
sed -n "1,/$trigger/!p" "$POSTGRESQL_CONF_SAMPLE"
595-
) > "$PGDATA"/postgresql.conf || exit_nicely
622+
sed -e "s/^#shared_buffers = 64/shared_buffers = $nbuffers/" \
623+
-e "s/^#max_connections = 32/max_connections = $nconns/" \
624+
-e "s/^#lc_messages = 'C'/lc_messages = '`pg_getlocale MESSAGES`'/" \
625+
-e "s/^#lc_monetary = 'C'/lc_monetary = '`pg_getlocale MONETARY`'/" \
626+
-e "s/^#lc_numeric = 'C'/lc_numeric = '`pg_getlocale NUMERIC`'/" \
627+
-e "s/^#lc_time = 'C'/lc_time = '`pg_getlocale TIME`'/" \
628+
"$POSTGRESQL_CONF_SAMPLE" > "$PGDATA"/postgresql.conf || exit_nicely
596629

597630
chmod 0600 "$PGDATA"/pg_hba.conf "$PGDATA"/pg_ident.conf \
598631
"$PGDATA"/postgresql.conf

0 commit comments

Comments
 (0)