postgresql 9.1 kernel.shm 设置不当导致 FATAL: could not create shared memory segment
老业务系统,os 为 debian 6.0
# lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 6.0.6 (squeeze)
Release: 6.0.6
Codename: squeeze
# free -m
total used free shared buffers cached
Mem: 3966 1078 2887 0 183 509
-/+ buffers/cache: 385 3580
Swap: 7628 0 7628
观察到pgsql 的 shared_buffers = 16MB,于是手贱,调整为 shared_buffers = 512MB,结果启动报错了。
$ /usr/lib/postgresql/9.1/bin/pg_ctl start -D /var/lib/postgresql/9.1/main -o "-c config_file=/etc/postgresql/9.1/main/postgresql.conf"
server starting
2017-12-26 18:09:40 HKT FATAL: could not create shared memory segment: Invalid argument
2017-12-26 18:09:40 HKT DETAIL: Failed system call was shmget(key=5432001, size=41238528, 03600).
2017-12-26 18:09:40 HKT HINT: This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMMAX parameter. You can either reduce the request size or reconfigure the kernel with larger SHMMAX. To reduce the request size (currently 41238528 bytes), reduce PostgreSQL's shared memory usage, perhaps by reducing shared_buffers or max_connections.
If the request size is already small, it's possible that it is less than your kernel's SHMMIN parameter, in which case raising the request size or reconfiguring SHMMIN is called for.
The PostgreSQL documentation contains more information about shared memory configuration.
从报错信息看,并查看 postgresql.conf 其它参数,觉得应该是 SHMMAX 设置的问题。
# sysctl -a | grep -i "kernel.shm"
kernel.shmmax = 33554432
kernel.shmall = 2097152
kernel.shmmni = 4096
shmmax、shmall 都设置的太小了。
shmmax 表示内核所允许的最大共享内存段的大小(bytes)。
缺省设置:33554432
shmall 表示在任何给定时刻,系统上可以使用的共享内存的总数,至少为ceil(shmmax/PAGE_SIZE)。
缺省设置:2097152
shmmni 表示用于整个系统的共享内存段的最大数目(个)
缺省设置:4096
查看pgsql 9.1 的文档
查看 /etc/sysctl.d/30-postgresql-shm.conf
# cat /etc/sysctl.d/30-postgresql-shm.conf
# Shared memory settings for PostgreSQL
# Note that if another program uses shared memory as well, you will have to
# coordinate the size settings between the two.
# Maximum size of shared memory segment in bytes
#kernel.shmmax = 33554432
# Maximum total size of shared memory in pages (normally 4096 bytes)
#kernel.shmall = 2097152
修改 kernel.shm
# vi /etc/sysctl.conf
kernel.shmmax = 4398046511104
kernel.shmall = 4294967296
kernel.shmmni = 4096
# sysctl -p
启动数据库
$ /usr/lib/postgresql/9.1/bin/pg_ctl start -D /var/lib/postgresql/9.1/main -o "-c config_file=/etc/postgresql/9.1/main/postgresql.conf"
$ ps -ef|grep -i postgres
postgres 28912 18210 0 18:08 pts/0 00:00:00 su - postgres
postgres 28913 28912 0 18:08 pts/0 00:00:00 -su
postgres 30976 1 0 18:44 pts/0 00:00:00 /usr/lib/postgresql/9.1/bin/postgres -D /var/lib/postgresql/9.1/main -c config_file=/etc/postgresql/9.1/main/postgresql.conf
postgres 30977 30976 0 18:44 ? 00:00:00 postgres: logger process
postgres 30979 30976 0 18:44 ? 00:00:00 postgres: writer process
postgres 30980 30976 0 18:44 ? 00:00:00 postgres: wal writer process
postgres 30981 30976 0 18:44 ? 00:00:00 postgres: autovacuum launcher process
postgres 30982 30976 0 18:44 ? 00:00:00 postgres: stats collector process
postgres 31173 28913 0 18:47 pts/0 00:00:00 ps -ef
postgres 31174 28913 0 18:47 pts/0 00:00:00 grep -i postgres