| PostgreSQL 8.4.22 Documentation | ||||
|---|---|---|---|---|
| Prev | Fast Backward | Chapter 17. Server Setup and Operation | Fast Forward | Next |
A large PostgreSQL installation can quickly exhaust 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, keep reading.
Shared memory and semaphores are collectively referred to as "System V IPC" (together with message queues, which are not relevant for PostgreSQL). 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. (On Windows, PostgreSQL provides its own replacement implementation of these facilities, and so most of this section can be disregarded.)
The complete lack of these facilities is usually manifested by an Illegal system call error upon server start. In that case there's nothing left to do but to reconfigure your kernel. PostgreSQL won't work without them.
When PostgreSQL exceeds one of the various hard IPC limits, the server will refuse to start and should leave an instructive error message describing the problem encountered and what to do about it. (See also Section 17.3.1.) The relevant kernel parameters are named consistently across different systems; Table 17-1 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, and possibly even recompile the kernel, to change these settings.
Table 17-1. System V IPC parameters
| Name | Description | Reasonable values |
|---|---|---|
| SHMMAX | Maximum size of shared memory segment (bytes) | at least several megabytes (see text) |
| SHMMIN | Minimum size of shared memory segment (bytes) | 1 |
| SHMALL | Total amount of shared memory available (bytes or pages) | if bytes, same as SHMMAX; if pages, ceil(SHMMAX/PAGE_SIZE) |
| 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 plus room for other applications |
| SEMMNI | Maximum number of semaphore identifiers (i.e., sets) | at least ceil((max_connections + autovacuum_max_workers) / 16) |
| SEMMNS | Maximum number of semaphores system-wide | ceil((max_connections + autovacuum_max_workers) / 16) * 17 plus room for other applications |
| SEMMSL | Maximum number of semaphores per set | at least 17 |
| SEMMAP | Number of entries in semaphore map | see text |
| SEMVMX | Maximum value of semaphore | at least 1000 (The default is often 32767, don't change unless forced to) |
The most important
shared memory parameter is SHMMAX, the
maximum size, in bytes, of a shared memory segment. If you get
an error message from shmget like
Invalid argument, it is likely
that this limit has been exceeded. The size of the required
shared memory segment varies depending on several PostgreSQL configuration parameters, as
shown in Table
17-2. (Any error message you might get will include the
exact size of the failed allocation request.) You can, as a
temporary solution, lower some of those settings to avoid the
failure. While it is possible to get PostgreSQL to run with SHMMAX as small as 2 MB, you need considerably
more for acceptable performance. Desirable settings are in the
tens to hundreds of megabytes.
Some systems also have a limit on the total amount of shared memory in the system (SHMALL). Make sure this is large enough for PostgreSQL plus any other applications that are using shared memory segments. (Caution: SHMALL is measured in pages rather than bytes on many systems.)
Less likely to cause problems is the minimum size for shared memory segments (SHMMIN), which should be at most approximately 500 kB for PostgreSQL (it is usually just 1). The maximum number of segments system-wide (SHMMNI) or per-process (SHMSEG) are unlikely to cause a problem unless your system has them set to zero.
PostgreSQL uses one
semaphore per allowed connection (max_connections)
and allowed autovacuum worker process (autovacuum_max_workers),
in sets of 16. Each such set will also contain a 17th semaphore
which contains a "magic 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 max_connections
plus autovacuum_max_workers, plus one
extra for each 16 allowed connections plus workers (see the
formula in Table 17-1). 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 + autovacuum_max_workers) /
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 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 over time lead to fewer 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 PostgreSQL.
Various other settings related to "semaphore undo", such as SEMMNU and SEMUME, are not of concern for PostgreSQL.
At least as of version 5.1, it should not be necessary to do any special configuration for such parameters as SHMMAX, as it appears this is configured to allow all memory to be used as shared memory. That is the sort of configuration commonly used for other databases such as DB/2.
It might, however, be necessary to modify the global ulimit information in /etc/security/limits, as the default hard limits for file sizes (fsize) and numbers of files (nofiles) might be too low.
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. To increase the amount of shared memory supported by your system, add something like the following to your kernel configuration file:
options "SHMALL=8192" options "SHMMAX=\(SHMALL*PAGE_SIZE\)"
SHMALL is measured in 4 kB pages, so a value of 1024 represents 4 MB of shared memory. Therefore the above increases the maximum shared memory area to 32 MB. For those running 4.3 or later, you will probably also need to increase KERNEL_VIRTUAL_MB above the default 248. Once all changes have been made, recompile the kernel, and reboot.
For those running 4.0 and earlier releases, use bpatch to find the sysptsize value in the current kernel. This is computed dynamically at boot time.
$ bpatch -r sysptsize 0x9 = 9
Next, add SYSPTSIZE as a hard-coded value in the kernel configuration file. Increase the value you found using bpatch. Add 1 for every additional 4 MB of shared memory you desire.
options "SYSPTSIZE=16"
sysptsize cannot be changed by sysctl.
Semaphores. You will probably want to increase the number of semaphores as well; the default system total of 60 will only allow about 50 PostgreSQL connections. Set the values you want in your kernel configuration file, e.g.:
options "SEMMNI=40" options "SEMMNS=240"
The default settings are only suitable for small installations (for example, default SHMMAX is 32 MB). Changes can be made via the sysctl or loader interfaces. The following parameters can be set using sysctl:
$ sysctl -w kern.ipc.shmall=32768 $ sysctl -w kern.ipc.shmmax=134217728 $ sysctl -w kern.ipc.semmap=256
To have these settings persist over reboots, modify /etc/sysctl.conf.
The remaining semaphore settings are read-only as far as sysctl is concerned, but can be changed before boot using the loader prompt:
(loader) set kern.ipc.semmni=256 (loader) set kern.ipc.semmns=512 (loader) set kern.ipc.semmnu=256
Similarly these can be saved between reboots in /boot/loader.conf.
You might also want to configure your kernel to lock shared memory into RAM and prevent it from being paged out to swap. This can be accomplished using the sysctl setting kern.ipc.shm_use_phys.
If running in FreeBSD jails by enabling sysctl's security.jail.sysvipc_allowed, postmasters running in different jails should be run by different operating system users. This improves security because it prevents non-root users from interfering with shared memory or semaphores in a different jail, and it allows the PostgreSQL IPC cleanup code to function properly. (In FreeBSD 6.0 and later the IPC cleanup code doesn't properly detect processes in other jails, preventing the running of postmasters on the same port in different jails.)
FreeBSD versions before 4.0 work like OpenBSD (see below).
In NetBSD 5.0 and later, IPC parameters can be adjusted using sysctl, for example:
$ sysctl -w kern.ipc.shmmax=16777216
To have these settings persist over reboots, modify /etc/sysctl.conf.
You might also want to configure your kernel to lock shared memory into RAM and prevent it from being paged out to swap. This can be accomplished using the sysctl setting kern.ipc.shm_use_phys.
NetBSD versions before 5.0 work like OpenBSD (see below), except that parameters should be set with the keyword options not option.
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:
option SYSVSHM option SHMMAXPGS=4096 option SHMSEG=256 option SYSVSEM option SEMMNI=256 option SEMMNS=512 option SEMMNU=256 option SEMMAP=256
You might also want to configure your kernel to lock shared memory into RAM and prevent it from being paged out to swap. This can be accomplished using the sysctl setting kern.ipc.shm_use_phys.
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