From de98a7e23a193a9c9a6505540ad03c5b4a57f345 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 14 Jul 2003 20:00:23 +0000 Subject: [PATCH] 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. --- doc/src/sgml/runtime.sgml | 36 +++++--- src/backend/port/sysv_shmem.c | 86 ++++++------------- src/backend/utils/init/postinit.c | 8 +- src/backend/utils/misc/postgresql.conf.sample | 4 + src/bin/initdb/initdb.sh | 51 +++++++++-- 5 files changed, 97 insertions(+), 88 deletions(-) diff --git a/doc/src/sgml/runtime.sgml b/doc/src/sgml/runtime.sgml index bb8a8f9bd3..39057f0652 100644 --- a/doc/src/sgml/runtime.sgml +++ b/doc/src/sgml/runtime.sgml @@ -1,5 +1,5 @@ @@ -608,9 +608,18 @@ SET ENABLE_SEQSCAN TO OFF; Determines the maximum number of concurrent connections to the - database server. The default is 32 (unless altered while - building the server). This parameter can only be set at server - start. + database server. The default is typically 100, but may be less + if your kernel settings will not support it (as determined + during initdb). + This parameter can only be set at server start. + + + + Increasing this parameter may cause PostgreSQL + to request more System V shared + memory or semaphores than your operating system's default configuration + allows. See for information on how to + adjust these parameters, if necessary. @@ -831,13 +840,16 @@ SET ENABLE_SEQSCAN TO OFF; Sets the number of shared memory buffers used by the database - server. The default is 64. Each buffer is typically 8192 - bytes. This must be greater than 16, as well as at least twice - the value of MAX_CONNECTIONS; however, a - higher value can often improve performance. - Values of a few thousand are recommended - for production installations. This option can only be set at - server start. + server. The default is typically 1000, but may be less + if your kernel settings will not support it (as determined + during initdb). Each buffer is 8192 + bytes, unless a different value of BLCKSZ was chosen + when building the server. This setting must be at least 16, + as well as at least twice the value of + MAX_CONNECTIONS; however, settings significantly + higher than the minimum are usually needed for good performance. + Values of a few thousand are recommended for production installations. + This option can only be set at server start. @@ -2796,7 +2808,7 @@ $ postmaster -o '-S 1024 -s' number, to detect collision with semaphore sets used by other applications. The maximum number of semaphores in the system is set by SEMMNS, which consequently must be at least - as high as the connection setting plus one extra for each 16 + as high as max_connections plus one extra for each 16 allowed connections (see the formula in ). The parameter SEMMNI determines the limit on the number of semaphore sets that can diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c index ffe5bfa268..8d6a1814ec 100644 --- a/src/backend/port/sysv_shmem.c +++ b/src/backend/port/sysv_shmem.c @@ -10,7 +10,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.10 2003/05/08 19:17:07 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.11 2003/07/14 20:00:22 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -45,10 +45,8 @@ void *UsedShmemSegAddr = NULL; static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size); static void IpcMemoryDetach(int status, Datum shmaddr); static void IpcMemoryDelete(int status, Datum shmId); -static void *PrivateMemoryCreate(uint32 size); -static void PrivateMemoryDelete(int status, Datum memaddr); static PGShmemHeader *PGSharedMemoryAttach(IpcMemoryKey key, - IpcMemoryId *shmid, void *addr); + IpcMemoryId *shmid); /* @@ -243,41 +241,6 @@ PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2) } -/* ---------------------------------------------------------------- - * private memory support - * - * Rather than allocating shmem segments with IPC_PRIVATE key, we - * just malloc() the requested amount of space. This code emulates - * the needed shmem functions. - * ---------------------------------------------------------------- - */ - -static void * -PrivateMemoryCreate(uint32 size) -{ - void *memAddress; - - memAddress = malloc(size); - if (!memAddress) - { - fprintf(stderr, "PrivateMemoryCreate: malloc(%u) failed\n", size); - proc_exit(1); - } - MemSet(memAddress, 0, size); /* keep Purify quiet */ - - /* Register on-exit routine to release storage */ - on_shmem_exit(PrivateMemoryDelete, PointerGetDatum(memAddress)); - - return memAddress; -} - -static void -PrivateMemoryDelete(int status, Datum memaddr) -{ - free(DatumGetPointer(memaddr)); -} - - /* * PGSharedMemoryCreate * @@ -289,6 +252,9 @@ PrivateMemoryDelete(int status, Datum memaddr) * collision with non-Postgres shmem segments. The idea here is to detect and * re-use keys that may have been assigned by a crashed postmaster or backend. * + * makePrivate means to always create a new segment, rather than attach to + * or recycle any existing segment. + * * The port number is passed for possible use as a key (for SysV, we use * it to generate the starting shmem key). In a standalone backend, * zero will be passed. @@ -307,8 +273,7 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port) /* Just attach and return the pointer */ if (ExecBackend && UsedShmemSegAddr != NULL && !makePrivate) { - if ((hdr = (PGShmemHeader *) memAddress = PGSharedMemoryAttach( - UsedShmemSegID, &shmid, UsedShmemSegAddr)) == NULL) + if ((hdr = PGSharedMemoryAttach(UsedShmemSegID, &shmid)) == NULL) { fprintf(stderr, "Unable to attach to proper memory at fixed address: shmget(key=%d, addr=%p) failed: %s\n", (int) UsedShmemSegID, UsedShmemSegAddr, strerror(errno)); @@ -317,34 +282,29 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port) return hdr; } - /* Create shared memory */ - - NextShmemSegID = port * 1000 + 1; + /* Loop till we find a free IPC key */ + NextShmemSegID = port * 1000; - for (;;NextShmemSegID++) + for (NextShmemSegID++;; NextShmemSegID++) { - /* Special case if creating a private segment --- just malloc() it */ - if (makePrivate) - { - memAddress = PrivateMemoryCreate(size); - break; - } - /* Try to create new segment */ memAddress = InternalIpcMemoryCreate(NextShmemSegID, size); if (memAddress) break; /* successful create and attach */ /* Check shared memory and possibly remove and recreate */ - - if ((hdr = (PGShmemHeader *) memAddress = PGSharedMemoryAttach( - NextShmemSegID, &shmid, UsedShmemSegAddr)) == NULL) + + if (makePrivate) /* a standalone backend shouldn't do this */ + continue; + + if ((memAddress = PGSharedMemoryAttach(NextShmemSegID, &shmid)) == NULL) continue; /* can't attach, not one of mine */ /* * If I am not the creator and it belongs to an extant process, * continue. */ + hdr = (PGShmemHeader *) memAddress; if (hdr->creatorPID != getpid()) { if (kill(hdr->creatorPID, 0) == 0 || errno != ESRCH) @@ -407,22 +367,25 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port) /* - * Attach to shared memory and make sure it has a Postgres header + * Attach to shared memory and make sure it has a Postgres header + * + * Returns attach address if OK, else NULL */ static PGShmemHeader * -PGSharedMemoryAttach(IpcMemoryKey key, IpcMemoryId *shmid, void *addr) +PGSharedMemoryAttach(IpcMemoryKey key, IpcMemoryId *shmid) { PGShmemHeader *hdr; if ((*shmid = shmget(key, sizeof(PGShmemHeader), 0)) < 0) return NULL; - hdr = (PGShmemHeader *) shmat(*shmid, UsedShmemSegAddr, + hdr = (PGShmemHeader *) shmat(*shmid, + UsedShmemSegAddr, #if defined(solaris) && defined(__sparc__) - /* use intimate shared memory on SPARC Solaris */ - SHM_SHARE_MMU + /* use intimate shared memory on Solaris */ + SHM_SHARE_MMU #else - 0 + 0 #endif ); @@ -434,5 +397,6 @@ PGSharedMemoryAttach(IpcMemoryKey key, IpcMemoryId *shmid, void *addr) shmdt(hdr); return NULL; /* segment belongs to a non-Postgres app */ } + return hdr; } diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 48194071e5..3cd3f57929 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.122 2003/06/27 14:45:30 petere Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/init/postinit.c,v 1.123 2003/07/14 20:00:22 tgl Exp $ * * *------------------------------------------------------------------------- @@ -176,12 +176,8 @@ InitCommunication(void) { /* * We're running a postgres bootstrap process or a standalone backend. - * Create private "shmem" and semaphores. Force MaxBackends to 1 so - * that we don't allocate more resources than necessary. + * Create private "shmem" and semaphores. */ - SetConfigOption("max_connections", "1", - PGC_POSTMASTER, PGC_S_OVERRIDE); - CreateSharedMemoryAndSemaphores(true, MaxBackends, 0); } } diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index 7333210692..2dd980cc18 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -210,6 +210,10 @@ #client_encoding = sql_ascii # actually, defaults to database encoding # These settings are initialized by initdb -- they may be changed +#lc_messages = 'C' # locale for system error message strings +#lc_monetary = 'C' # locale for monetary formatting +#lc_numeric = 'C' # locale for number formatting +#lc_time = 'C' # locale for time formatting # Other Defaults diff --git a/src/bin/initdb/initdb.sh b/src/bin/initdb/initdb.sh index 08fe399bfc..0c22560ffe 100644 --- a/src/bin/initdb/initdb.sh +++ b/src/bin/initdb/initdb.sh @@ -27,7 +27,7 @@ # Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group # Portions Copyright (c) 1994, Regents of the University of California # -# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.193 2003/07/04 16:41:21 tgl Exp $ +# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.194 2003/07/14 20:00:23 tgl Exp $ # #------------------------------------------------------------------------- @@ -577,6 +577,40 @@ echo "$short_version" > "$PGDATA/base/1/PG_VERSION" || exit_nicely echo "ok" +########################################################################## +# +# DETERMINE PLATFORM-SPECIFIC CONFIG SETTINGS +# +# Use reasonable values if kernel will let us, else scale back + +cp /dev/null "$PGDATA"/postgresql.conf || exit_nicely + +$ECHO_N "selecting default shared_buffers... "$ECHO_C + +for nbuffers in 1000 900 800 700 600 500 400 300 200 100 50 +do + TEST_OPT="$PGSQL_OPT -c shared_buffers=$nbuffers -c max_connections=5" + if "$PGPATH"/postgres $TEST_OPT template1 /dev/null 2>&1 + then + break + fi +done + +echo "$nbuffers" + +$ECHO_N "selecting default max_connections... "$ECHO_C + +for nconns in 100 50 40 30 20 10 +do + TEST_OPT="$PGSQL_OPT -c shared_buffers=$nbuffers -c max_connections=$nconns" + if "$PGPATH"/postgres $TEST_OPT template1 /dev/null 2>&1 + then + break + fi +done + +echo "$nconns" + ########################################################################## # # CREATE CONFIG FILES @@ -585,14 +619,13 @@ $ECHO_N "creating configuration files... "$ECHO_C cp "$PG_HBA_SAMPLE" "$PGDATA"/pg_hba.conf || exit_nicely cp "$PG_IDENT_SAMPLE" "$PGDATA"/pg_ident.conf || exit_nicely -( - trigger="# These settings are initialized by initdb -- they may be changed" - sed -n "1,/$trigger/p" "$POSTGRESQL_CONF_SAMPLE" - for cat in MESSAGES MONETARY NUMERIC TIME; do - echo "LC_$cat = '`pg_getlocale $cat`'" - done - sed -n "1,/$trigger/!p" "$POSTGRESQL_CONF_SAMPLE" -) > "$PGDATA"/postgresql.conf || exit_nicely +sed -e "s/^#shared_buffers = 64/shared_buffers = $nbuffers/" \ + -e "s/^#max_connections = 32/max_connections = $nconns/" \ + -e "s/^#lc_messages = 'C'/lc_messages = '`pg_getlocale MESSAGES`'/" \ + -e "s/^#lc_monetary = 'C'/lc_monetary = '`pg_getlocale MONETARY`'/" \ + -e "s/^#lc_numeric = 'C'/lc_numeric = '`pg_getlocale NUMERIC`'/" \ + -e "s/^#lc_time = 'C'/lc_time = '`pg_getlocale TIME`'/" \ + "$POSTGRESQL_CONF_SAMPLE" > "$PGDATA"/postgresql.conf || exit_nicely chmod 0600 "$PGDATA"/pg_hba.conf "$PGDATA"/pg_ident.conf \ "$PGDATA"/postgresql.conf