Skip to content

Commit 9d19785

Browse files
committed
Rearrange handling of MAXBACKENDS a little bit. The default setting
of MAXBACKENDS is now 1024, since all it's costing is about 32 bytes of memory per array slot. configure's --with-maxbackends switch now controls DEF_MAXBACKENDS which is simply the default value of the postmaster's -N switch. Thus, the out-of-the-box configuration will still limit you to 64 backends, but you can go up to 1024 backends simply by restarting the postmaster with a different -N switch --- no rebuild required.
1 parent 75cccd0 commit 9d19785

File tree

10 files changed

+79
-68
lines changed

10 files changed

+79
-68
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 10 additions & 9 deletions
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.102 1999/02/19 06:06:00 tgl Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.103 1999/02/21 01:41:43 tgl Exp $
1414
*
1515
* NOTES
1616
*
@@ -162,14 +162,15 @@ static IpcMemoryKey ipc_key;
162162
* adding to this.
163163
*/
164164

165-
static int MaxBackends = MAXBACKENDS;
165+
static int MaxBackends = DEF_MAXBACKENDS;
166166

167167
/*
168-
* MaxBackends is the actual soft limit on the number of backends
169-
* we will start. It defaults to the hard limit established at compilation
170-
* time, but can be readjusted with postmaster's xxx switch.
171-
* One reason to reduce MaxBackends is to allow startup under a kernel
172-
* that won't let us get MAXBACKENDS semaphores!
168+
* MaxBackends is the actual limit on the number of backends we will start.
169+
* The default is established by configure, but it can be readjusted
170+
* from 1..MAXBACKENDS with the postmaster -N switch.
171+
* Note that a larger MaxBackends value will increase the size of the
172+
* shared memory area as well as cause the postmaster to grab more
173+
* kernel semaphores, even if you never actually use that many backends.
173174
*/
174175

175176
static int NextBackendTag = MAXINT; /* XXX why count down not up? */
@@ -641,8 +642,8 @@ usage(const char *progname)
641642
fprintf(stderr, "\t-b backend\tuse a specific backend server executable\n");
642643
fprintf(stderr, "\t-d [1|2|3]\tset debugging level\n");
643644
fprintf(stderr, "\t-i \t\tlisten on TCP/IP sockets as well as Unix domain socket\n");
644-
fprintf(stderr, "\t-N nprocs\tset max number of backend servers (1..%d)\n",
645-
MAXBACKENDS);
645+
fprintf(stderr, "\t-N nprocs\tset max number of backends (1..%d, default %d)\n",
646+
MAXBACKENDS, DEF_MAXBACKENDS);
646647
fprintf(stderr, "\t-n \t\tdon't reinitialize shared memory after abnormal exit\n");
647648
fprintf(stderr, "\t-o option\tpass 'option' to each backend servers\n");
648649
fprintf(stderr, "\t-p port\tspecify port for postmaster to listen on\n");

src/backend/storage/ipc/ipc.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/ipc.c,v 1.35 1999/02/13 23:18:09 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/ipc.c,v 1.36 1999/02/21 01:41:44 tgl Exp $
1111
*
1212
* NOTES
1313
*
@@ -677,9 +677,10 @@ struct ipcdummy
677677
SLock *free;
678678
int unused;
679679
slock_t memlock;
680-
SLock slocks[NSLOCKS];
680+
SLock slocks[MAX_SPINS + 1];
681681
};
682-
static int SLockMemorySize = sizeof(struct ipcdummy);
682+
683+
#define SLOCKMEMORYSIZE sizeof(struct ipcdummy)
683684

684685
void
685686
CreateAndInitSLockMemory(IPCKey key)
@@ -688,7 +689,7 @@ CreateAndInitSLockMemory(IPCKey key)
688689
SLock *slckP;
689690

690691
SLockMemoryId = IpcMemoryCreate(key,
691-
SLockMemorySize,
692+
SLOCKMEMORYSIZE,
692693
0700);
693694
AttachSLockMemory(key);
694695
*FreeSLockPP = NULL;
@@ -713,7 +714,7 @@ AttachSLockMemory(IPCKey key)
713714
struct ipcdummy *slockM;
714715

715716
if (SLockMemoryId == -1)
716-
SLockMemoryId = IpcMemoryIdGet(key, SLockMemorySize);
717+
SLockMemoryId = IpcMemoryIdGet(key, SLOCKMEMORYSIZE);
717718
if (SLockMemoryId == -1)
718719
elog(FATAL, "SLockMemory not in shared memory");
719720
slockM = (struct ipcdummy *) IpcMemoryAttach(SLockMemoryId);

src/backend/storage/ipc/ipci.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/ipci.c,v 1.20 1999/02/19 07:10:47 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/ipc/ipci.c,v 1.21 1999/02/21 01:41:44 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -72,7 +72,7 @@ CreateSharedMemoryAndSemaphores(IPCKey key, int maxBackends)
7272
* ----------------
7373
*/
7474
CreateSpinlocks(IPCKeyGetSpinLockSemaphoreKey(key));
75-
size = BufferShmemSize() + LockShmemSize();
75+
size = BufferShmemSize() + LockShmemSize(maxBackends);
7676

7777
#ifdef STABLE_MEMORY_STORAGE
7878
size += MMShmemSize();
@@ -113,15 +113,13 @@ CreateSharedMemoryAndSemaphores(IPCKey key, int maxBackends)
113113
void
114114
AttachSharedMemoryAndSemaphores(IPCKey key)
115115
{
116-
int size;
117-
118116
/* ----------------
119117
* create rather than attach if using private key
120118
* ----------------
121119
*/
122120
if (key == PrivateIPCKey)
123121
{
124-
CreateSharedMemoryAndSemaphores(key, 1);
122+
CreateSharedMemoryAndSemaphores(key, 16);
125123
return;
126124
}
127125

@@ -136,8 +134,7 @@ AttachSharedMemoryAndSemaphores(IPCKey key)
136134
* attach the buffer manager buffer pool (and semaphore)
137135
* ----------------
138136
*/
139-
size = BufferShmemSize() + LockShmemSize();
140-
InitShmem(key, size);
137+
InitShmem(key, 0);
141138
InitBufferPool(key);
142139

143140
/* ----------------

src/backend/storage/lmgr/lock.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.42 1999/02/19 06:06:06 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/lock.c,v 1.43 1999/02/21 01:41:46 tgl Exp $
1111
*
1212
* NOTES
1313
* Outside modules can create a lock table and acquire/release
@@ -1478,36 +1478,37 @@ LockReleaseAll(LOCKMETHOD lockmethod, SHM_QUEUE *lockQueue)
14781478
}
14791479

14801480
int
1481-
LockShmemSize()
1481+
LockShmemSize(int maxBackends)
14821482
{
14831483
int size = 0;
1484+
int nLockEnts = NLOCKENTS(maxBackends);
14841485
int nLockBuckets,
14851486
nLockSegs;
14861487
int nXidBuckets,
14871488
nXidSegs;
14881489

1489-
nLockBuckets = 1 << (int) my_log2((NLOCKENTS - 1) / DEF_FFACTOR + 1);
1490+
nLockBuckets = 1 << (int) my_log2((nLockEnts - 1) / DEF_FFACTOR + 1);
14901491
nLockSegs = 1 << (int) my_log2((nLockBuckets - 1) / DEF_SEGSIZE + 1);
14911492

14921493
nXidBuckets = 1 << (int) my_log2((NLOCKS_PER_XACT - 1) / DEF_FFACTOR + 1);
14931494
nXidSegs = 1 << (int) my_log2((nLockBuckets - 1) / DEF_SEGSIZE + 1);
14941495

1495-
size += MAXALIGN(MAXBACKENDS * sizeof(PROC)); /* each MyProc */
1496-
size += MAXALIGN(MAXBACKENDS * sizeof(LOCKMETHODCTL)); /* each
1496+
size += MAXALIGN(maxBackends * sizeof(PROC)); /* each MyProc */
1497+
size += MAXALIGN(maxBackends * sizeof(LOCKMETHODCTL)); /* each
14971498
* lockMethodTable->ctl */
14981499
size += MAXALIGN(sizeof(PROC_HDR)); /* ProcGlobal */
14991500

1500-
size += MAXALIGN(my_log2(NLOCKENTS) * sizeof(void *));
1501+
size += MAXALIGN(my_log2(nLockEnts) * sizeof(void *));
15011502
size += MAXALIGN(sizeof(HHDR));
15021503
size += nLockSegs * MAXALIGN(DEF_SEGSIZE * sizeof(SEGMENT));
1503-
size += NLOCKENTS * /* XXX not multiple of BUCKET_ALLOC_INCR? */
1504+
size += nLockEnts * /* XXX not multiple of BUCKET_ALLOC_INCR? */
15041505
(MAXALIGN(sizeof(BUCKET_INDEX)) +
15051506
MAXALIGN(sizeof(LOCK))); /* contains hash key */
15061507

1507-
size += MAXALIGN(my_log2(MAXBACKENDS) * sizeof(void *));
1508+
size += MAXALIGN(my_log2(maxBackends) * sizeof(void *));
15081509
size += MAXALIGN(sizeof(HHDR));
15091510
size += nXidSegs * MAXALIGN(DEF_SEGSIZE * sizeof(SEGMENT));
1510-
size += MAXBACKENDS * /* XXX not multiple of BUCKET_ALLOC_INCR? */
1511+
size += maxBackends * /* XXX not multiple of BUCKET_ALLOC_INCR? */
15111512
(MAXALIGN(sizeof(BUCKET_INDEX)) +
15121513
MAXALIGN(sizeof(XIDLookupEnt))); /* contains hash key */
15131514

@@ -1673,8 +1674,8 @@ DeadLockCheck(SHM_QUEUE *lockQueue, LOCK *findlock, bool skip_check)
16731674
break;
16741675
if (j >= nprocs && lock != findlock)
16751676
{
1677+
Assert(nprocs < MAXBACKENDS);
16761678
checked_procs[nprocs++] = proc;
1677-
Assert(nprocs <= MAXBACKENDS);
16781679

16791680
/*
16801681
* For non-MyProc entries, we are looking only

src/backend/storage/lmgr/proc.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.50 1999/02/19 07:10:48 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.51 1999/02/21 01:41:45 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -46,7 +46,7 @@
4646
* This is so that we can support more backends. (system-wide semaphore
4747
* sets run out pretty fast.) -ay 4/95
4848
*
49-
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.50 1999/02/19 07:10:48 tgl Exp $
49+
* $Header: /cvsroot/pgsql/src/backend/storage/lmgr/proc.c,v 1.51 1999/02/21 01:41:45 tgl Exp $
5050
*/
5151
#include <sys/time.h>
5252
#include <unistd.h>
@@ -154,23 +154,28 @@ InitProcGlobal(IPCKey key, int maxBackends)
154154
*/
155155
on_shmem_exit(ProcFreeAllSemaphores, NULL);
156156

157-
/* Pre-create the semaphores for the first maxBackends processes */
158-
for (i = 0;
159-
i < (maxBackends+PROC_NSEMS_PER_SET-1) / PROC_NSEMS_PER_SET;
160-
i++)
157+
/* Pre-create the semaphores for the first maxBackends processes,
158+
* unless we are running as a standalone backend.
159+
*/
160+
if (key != PrivateIPCKey)
161161
{
162-
IPCKey semKey = ProcGlobal->currKey + i;
163-
int semId;
164-
int semstat;
165-
166-
semId = IpcSemaphoreCreate(semKey,
167-
PROC_NSEMS_PER_SET,
168-
IPCProtection,
169-
IpcSemaphoreDefaultStartValue,
170-
0,
171-
&semstat);
172-
/* mark this sema set allocated */
173-
ProcGlobal->freeSemMap[i] = (1 << PROC_NSEMS_PER_SET);
162+
for (i = 0;
163+
i < (maxBackends+PROC_NSEMS_PER_SET-1) / PROC_NSEMS_PER_SET;
164+
i++)
165+
{
166+
IPCKey semKey = ProcGlobal->currKey + i;
167+
int semId;
168+
int semstat;
169+
170+
semId = IpcSemaphoreCreate(semKey,
171+
PROC_NSEMS_PER_SET,
172+
IPCProtection,
173+
IpcSemaphoreDefaultStartValue,
174+
0,
175+
&semstat);
176+
/* mark this sema set allocated */
177+
ProcGlobal->freeSemMap[i] = (1 << PROC_NSEMS_PER_SET);
178+
}
174179
}
175180
}
176181
}

src/configure

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ ac_help="$ac_help
3131
ac_help="$ac_help
3232
--with-pgport=<portnum> change default startup port "
3333
ac_help="$ac_help
34-
--with-maxbackends=<n> set maximum number of server processes "
34+
--with-maxbackends=<n> set default maximum number of server processes "
3535
ac_help="$ac_help
3636
--with-tcl build Tcl interfaces and pgtclsh "
3737
ac_help="$ac_help
@@ -879,18 +879,18 @@ EOF
879879
fi
880880

881881

882-
echo $ac_n "checking setting MAXBACKENDS""... $ac_c" 1>&6
883-
echo "configure:884: checking setting MAXBACKENDS" >&5
882+
echo $ac_n "checking setting DEF_MAXBACKENDS""... $ac_c" 1>&6
883+
echo "configure:884: checking setting DEF_MAXBACKENDS" >&5
884884
# Check whether --with-maxbackends or --without-maxbackends was given.
885885
if test "${with_maxbackends+set}" = set; then
886886
withval="$with_maxbackends"
887887
cat >> confdefs.h <<EOF
888-
#define MAXBACKENDS ${withval}
888+
#define DEF_MAXBACKENDS ${withval}
889889
EOF
890890
echo "$ac_t""$with_maxbackends" 1>&6
891891
else
892892
cat >> confdefs.h <<EOF
893-
#define MAXBACKENDS 64
893+
#define DEF_MAXBACKENDS 64
894894
EOF
895895
echo "$ac_t""64" 1>&6
896896

src/configure.in

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ AC_ARG_WITH(
255255
AC_DEFINE_UNQUOTED(DEF_PGPORT, "5432") AC_MSG_RESULT(5432)
256256
)
257257

258-
dnl MAXBACKENDS can be set by --with-maxbackends. Default value is 64.
259-
AC_MSG_CHECKING(setting MAXBACKENDS)
258+
dnl DEF_MAXBACKENDS can be set by --with-maxbackends. Default value is 64.
259+
AC_MSG_CHECKING(setting DEF_MAXBACKENDS)
260260
AC_ARG_WITH(
261261
maxbackends,
262-
[ --with-maxbackends=<n> set maximum number of server processes ],
263-
AC_DEFINE_UNQUOTED(MAXBACKENDS, ${withval}) AC_MSG_RESULT($with_maxbackends),
264-
AC_DEFINE_UNQUOTED(MAXBACKENDS, 64) AC_MSG_RESULT(64)
262+
[ --with-maxbackends=<n> set default maximum number of server processes ],
263+
AC_DEFINE_UNQUOTED(DEF_MAXBACKENDS, ${withval}) AC_MSG_RESULT($with_maxbackends),
264+
AC_DEFINE_UNQUOTED(DEF_MAXBACKENDS, 64) AC_MSG_RESULT(64)
265265
)
266266

267267
dnl We exclude tcl support unless user says --with-tcl

src/include/config.h.in

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@
99
#define CONFIG_H
1010

1111
/*
12-
* Maximum number of backend server processes per postmaster.
12+
* Default runtime limit on number of backend server processes per postmaster;
13+
* this is just the default setting for the postmaster's -N switch.
1314
* (Actual value is set by configure script.)
1415
*/
15-
#undef MAXBACKENDS
16+
#undef DEF_MAXBACKENDS
17+
18+
/*
19+
* Hard limit on number of backend server processes per postmaster.
20+
* Increasing this costs about 32 bytes per process slot as of v 6.5.
21+
*/
22+
#define MAXBACKENDS (DEF_MAXBACKENDS > 1024 ? DEF_MAXBACKENDS : 1024)
1623

1724
/*
1825
* Size of a disk block --- currently, this limits the size of a tuple.

src/include/storage/ipc.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: ipc.h,v 1.33 1999/02/19 06:06:33 tgl Exp $
9+
* $Id: ipc.h,v 1.34 1999/02/21 01:41:47 tgl Exp $
1010
*
1111
* NOTES
1212
* This file is very architecture-specific. This stuff should actually
@@ -96,7 +96,6 @@ extern void AttachSLockMemory(IPCKey key);
9696

9797
#ifdef HAS_TEST_AND_SET
9898

99-
#define NSLOCKS 2048
10099
#define NOLOCK 0
101100
#define SHAREDLOCK 1
102101
#define EXCLUSIVELOCK 2

src/include/storage/lock.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: lock.h,v 1.22 1999/02/19 06:06:35 tgl Exp $
9+
* $Id: lock.h,v 1.23 1999/02/21 01:41:47 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -28,14 +28,14 @@ typedef int MASK;
2828
/* ----------------------
2929
* The following defines are used to estimate how much shared
3030
* memory the lock manager is going to require.
31+
* See LockShmemSize() in lock.c.
3132
*
32-
* MAXBACKENDS - The max number of concurrently running backends (config.h)
3333
* NLOCKS_PER_XACT - The number of unique locks acquired in a transaction
3434
* NLOCKENTS - The maximum number of lock entries in the lock table.
3535
* ----------------------
3636
*/
37-
#define NLOCKS_PER_XACT 40
38-
#define NLOCKENTS (NLOCKS_PER_XACT*MAXBACKENDS)
37+
#define NLOCKS_PER_XACT 40
38+
#define NLOCKENTS(maxBackends) (NLOCKS_PER_XACT*(maxBackends))
3939

4040
typedef int LOCKMODE;
4141
typedef int LOCKMETHOD;
@@ -242,7 +242,7 @@ extern bool LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag,
242242
LOCKMODE lockmode);
243243
extern void GrantLock(LOCK *lock, LOCKMODE lockmode);
244244
extern bool LockReleaseAll(LOCKMETHOD lockmethod, SHM_QUEUE *lockQueue);
245-
extern int LockShmemSize(void);
245+
extern int LockShmemSize(int maxBackends);
246246
extern bool LockingDisabled(void);
247247
extern bool DeadLockCheck(SHM_QUEUE *lockQueue, LOCK *findlock,
248248
bool skip_check);

0 commit comments

Comments
 (0)