A large Postgres installation can quickly hit various operating system resource limits. (On some systems, the factory defaults are so low that you don't even need a really "large" installation.) If you have encountered this kind of problem then keep reading.
Shared memory and semaphores are collectively referred to as "System V IPC" (together with message queues, which are not relevant for Postgres). Almost all modern operating systems provide these features, but not all of them have them turned on or sufficiently sized by default, especially systems with BSD heritage. (For the QNX and BeOS ports, Postgres provides its own replacement implementation of these facilities.)
The complete lack of these facilities is usually manifested by an Illegal system call error upon postmaster start. In that case there's nothing left to do but to reconfigure your kernel -- Postgres won't work without them.
When Postgres exceeds one of the various hard limits of the IPC resources then the postmaster will refuse to start up and should leave a marginally instructive error message about which problem was encountered and what needs to be done about it. (See also Section 3.3.1.) The relevant kernel parameters are named consistently across different systems; Table 3-2 gives an overview. The methods to set them, however, vary; suggestions for some platforms are given below. Be warned that it is often necessary to reboot your machine at least, possibly even recompile the kernel, to change these settings.
Table 3-2. System V IPC parameters
Name | Description | Reasonable values |
---|---|---|
SHMMAX | Maximum size of shared memory segment (bytes) | 512 kB + 8192 * buffers + extra ... infinity |
SHMMIN | Minimum size of shared memory segment (bytes) | 1 (at most about 256 kB) |
SHMSEG | Maximum number of shared memory segments per process | only 1 segment is needed, but the default is much higher |
SHMMNI | Maximum number of shared memory segments system-wide | like SHMSEG + room for other applications |
SEMMNI | Maximum number of semaphore identifiers (i.e., sets) | >= ceil(max_connections / 16) |
SEMMNS | Maximum number of semaphores system-wide | ceil(max_connections / 16) * 17 + room for other applications |
SEMMSL | Maximum number of semaphores per set | >= 17 |
SEMMAP | Number of entries in semaphore map | see text |
SEMVMX | Maximum value of semaphore | >= 255 (The default is often 32767, don't change unless asked to.) |
The most important shared memory parameter is SHMMAX, the maximum size, in bytes, that a shared memory segment can have. If you get an error message from shmget along the lines of Invalid argument then it is possible that this limit has been exceeded. The size of the required shared memory segments varies both with the number of requested buffers (-B option) and the number of allowed connections (-N option), although the former is the dominant item. (You can therefore, as a temporary solution, lower these settings to get rid of the failures.) As a rough approximation you can estimate the required segment size as the number of buffers times the block size (8192 kB by default) plus ample overhead (at least half a megabyte). Any error message you might get will contain the size of the failed allocation request.
Less likely to cause problems is the minimum size for shared memory segments (SHMMIN), which should be at most somewhere around 256 kB for Postgres (it is usually just 1). The maximum number of segments system-wide (SHMMNI) or per-process (SHMSEG) should not cause a problem unless your system has them set to zero. Some systems also have a limit on the total amount of shared memory in the system; see the platform-specific instructions below.
Postgres uses one semaphore per allowed connection (-N option), in sets of 16. Each such set will also contain a 17th semaphore which contains a "magic number", to avoid 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 allowed connections (see the formula in Table 3-2). The parameter SEMMNI determines the limit on the number of semaphore sets that can exist on the system at one time. Hence this parameter must be at least ceil(max_connections / 16). Lowering the number of allowed connections is a temporary workaround for failures, which are usually confusingly worded "No space left on device", from the function semget().
In some cases it might also turn out to be necessary to increase SEMMAP to be at least on the order of SEMMNS. This parameter defines the size of the semaphore resource map, in which each contiguous block of available semaphores needs an entry. When a semaphore set is freed it is either added to an existing entry that is adjacent to the freed block or it is registered under a new map entry. If the map is full, the freed semaphores get lost (until reboot). Fragmentation of the semaphore space could therefore over time lead to less available semaphores than there should be.
The SEMMSL parameter, which determines how many semaphores can be in a set, must be at least 17 for Postgres.
Various other settings related to "semaphore undo", such as SEMMNU and SEMUME, are not of concern for Postgres.
Shared Memory. By default, only 4 MB of shared memory is supported. Keep in mind that shared memory is not pageable; it is locked in RAM. The shared memory parameters are:
#define SHMMAX /* max shared memory segment size (bytes) */ #define SHMMIN /* min shared memory segment size (bytes) */ #define SHMMNI /* max number of shared memory identifiers */ #define SHMSEG /* max shared memory segments per process */ #define SHMALL /* max amount of shared memory (pages) */To increase the number of buffers supported by the postmaster, add the following to your kernel config file. A SHMALL value of 1024 represents 4MB of shared memory. Increase it accordingly:
options "SHMALL=4096" options "SHMMAX=\(SHMALL*PAGE_SIZE\)"
For those running 4.1 or later, just recompile the kernel and reboot. For those running earlier releases, use bpatch to find the sysptsize value for the current kernel. This is computed dynamically at bootup.
$ bpatch -r sysptsize 0x9 = 9Next, change SYSPTSIZE to a hard-coded value. Use the bpatch value, plus add 1 for every additional 4 MB of shared memory you desire.
options "SYSPTSIZE=13"sysptsize can not be changed by sysctl on the fly.
Semaphores. You may need to increase the number of semaphores. By default, Postgres allocates 34 semaphores, which is over half the default system total of 60.
The defaults are in /sys/sys/sem.h:
#define SEMMNI 10 /* # of semaphore identifiers */ #define SEMMNS 60 /* # of semaphores in system */ #define SEMUME 10 /* max # of undo entries per process */ #define SEMMNU 30 /* # of undo structures in system */Set the values you want in your kernel config file, e.g.:
options "SEMMNI=40" options "SEMMNS=240" options "SEMUME=40" options "SEMMNU=120"
The options SYSVSHM and SYSVSEM need to be enabled when the kernel is compiled. (They are by default.) The maximum size of shared memory is determined by the option SHMMAXPGS (in pages). The following shows an example of how to set the various parameters:
options SYSVSHM options SHMMAXPGS=4096 options SHMSEG=256 options SYSVSEM options SEMMNI=256 options SEMMNS=512 options SEMMNU=256 options SEMMAP=256
The default settings tend to suffice for normal installations. On HP-UX 10, the factory default for SEMMNS is 128, which might be too low for larger database sites.
IPC parameters can be set in the System Administration Manager (SAM) under Kernel Configuration->Configurable Parameters. Hit Create A New Kernel when you're done.
The default shared memory limit (both SHMMAX and SHMALL) is 32 MB in 2.2 kernels, but it can be changed in the proc file system (without reboot). For example, to allow 128 MB:
$ echo 134217728 >/proc/sys/kernel/shmall $ echo 134217728 >/proc/sys/kernel/shmmaxYou could put these commands into a script run at boot-time.
Alternatively, you can use sysctl, if available, to control these parameters. Look for a file called /etc/sysctl.conf and add lines like the following to it:
kernel.shmall = 134217728 kernel.shmmax = 134217728This file is usually processed at boot time, but sysctl can also be called explicitly later.
Other parameters are sufficiently sized for any application. If you want to see for yourself look into /usr/src/linux/include/asm-xxx/shmparam.h and /usr/src/linux/include/linux/sem.h.
In the default configuration, only 512 kB of shared memory per segment is allowed, which is about enough for -B 24 -N 12. To increase the setting, first change the directory to /etc/conf/cf.d. To display the current value of SHMMAX, in bytes, run
./configure -y SHMMAXTo set a new value for SHMMAX, run:
./configure SHMMAX=valuewhere value is the new value you want to use (in bytes). After setting SHMMAX, rebuild the kernel
./link_unixand reboot.
At least in version 2.6, the maximum size of a shared memory segment is set too low for Postgres. The relevant settings can be changed in /etc/system, for example:
set shmsys:shminfo_shmmax=0x2000000 set shmsys:shminfo_shmmin=1 set shmsys:shminfo_shmmni=256 set shmsys:shminfo_shmseg=256 set semsys:seminfo_semmap=256 set semsys:seminfo_semmni=512 set semsys:seminfo_semmns=512 set semsys:seminfo_semmsl=32You need to reboot to make the changes effective.
See also http://www.sunworld.com/swol-09-1997/swol-09-insidesolaris.html for information on shared memory under Solaris.
On UnixWare 7, the maximum size for shared memory segments is 512 kB in the default configuration. This is enough for about -B 24 -N 12. To display the current value of SHMMAX, run
/etc/conf/bin/idtune -g SHMMAXwhich displays the current, default, minimum, and maximum values, in bytes. To set a new value for SHMMAX, run:
/etc/conf/bin/idtune SHMMAX valuewhere value is the new value you want to use (in bytes). After setting SHMMAX, rebuild the kernel
/etc/conf/bin/idbuild -Band reboot.
Unix-like operating systems enforce various kinds of resource limits that might interfere with the operation of your Postgres server. Of importance are especially the limits on the number of processes per user, the number of open files per process, and the amount of memory available to a process. Each of these have a "hard" and a "soft" limit. The soft limit is what actually counts but it can be changed by the user up to the hard limit. The hard limit can only be changed by the root user. The system call setrlimit is responsible for setting these parameters. The shell's built-in command ulimit (Bourne shells) or limit (csh) is used to control the resource limits from the command line. On BSD-derived systems the file /etc/login.conf controls what values the various resource limits are set to upon login. See login.conf for details. The relevant parameters are maxproc, openfiles, and datasize. For example:
default:\ ... :datasize-cur=256M:\ :maxproc-cur=256:\ :openfiles-cur=256:\ ...(-cur is the soft limit. Append -max to set the hard limit.)
Kernels generally also have an implementation-dependent system-wide limit on some resources.
On Linux /proc/sys/fs/file-max determines the maximum number of files that the kernel will allocate. It can be changed by writing a different number into the file or by adding an assignment in /etc/sysctl.conf. The maximum limit of files per process is fixed at the time the kernel is compiled; see /usr/src/linux/Documentation/proc.txt for more information.
The Postgres server uses one process per connection so you should provide for at least as many processes as allowed connections, in addition to what you need for the rest of your system. This is usually not a problem but if you run several servers on one machine things might get tight.
The factory default limit on open files is often set to "socially friendly" values that allow many users to coexist on a machine without using an inappropriate fraction of the system resources. If you run many servers on a machine this is perhaps what you want, but on dedicated servers you may want to raise this limit.