PostgreSQL与操作系统(OS)内核相关的几个参数

来源:互联网 发布:好用的精华液 知乎 编辑:程序博客网 时间:2024/06/05 11:58

一、概述

PostgreSQL在postgresql.conf里面的配置参数有几个是和OS的内核参数紧密相关的,通常默认值是偏小的,但设置过大也会造成Postgres的启动失败。 

二、测试

1.测试环境

DB: postgres 9.1.3(之后版本这几个参数并未变动)
OS: CentOS 6.2 / Redhat(Ubuntu、Debian等linux系统也是一样)

2.内核参数文件位置

/proc/sys/kernel/etc/sysctl.conf[root@localhost ~]# sysctl -a|morekernel.shmmax = 68719476736kernel.shmall = 4294967296kernel.shmmni = 4096kernel.msgmax = 65536kernel.msgmni = 2005kernel.msgmnb = 65536[postgres@localhost ~]$ cat /proc/sys/kernel/sem250     32000   32      128(semmsl  semmns  semopm  semmni)

2.1 shared_buffers VS shmget 

shared_buffers是共享内存的意思,如果该值超过系统的内存值(包括swap),会造成启动失败。

shmget是get share memory,它是创建一个共享内存的函数。

[root@localhost ~]# free -m             total       used       free     shared    buffers     cachedMem:          1006        872        134          0        100        629-/+ buffers/cache:        142        863Swap:         2015         13       2002[root@localhost ~]# su - postgres[postgres@localhost ~]$ vi /database/pgdata/postgresql.conf shared_buffers = 5GB[postgres@localhost ~]$ pg_startserver starting[postgres@localhost ~]$ FATAL:  could not create shared memory segment: Cannot allocate memoryDETAIL:  Failed system call was shmget(key=1949001, size=5609447424, 03600).HINT:  This error usually means that PostgreSQL's request for a shared memory segment exceeded available memory or swap space,     or exceeded your kernel's SHMALL parameter.  You can either reduce the request size or reconfigure the kernel with larger SHMALL.      To reduce the request size (currently 5609447424 bytes), reduce PostgreSQL's shared memory usage, perhaps by reducing shared_buffers or max_connections.    The PostgreSQL documentation contains more information about shared memory configuration.


解决办法是减小shared_buffers、max_connections值,也或者加大shmall值、shmax值、加大内存和swap。但如果设置超大,大过内核值,则直接报错Invalid argument,如:
[postgres@localhost ~]$ vi /database/pgdata/postgresql.confshared_buffers = 222222GBmax_connections = 100[postgres@localhost ~]$ pg_startserver starting[postgres@localhost ~]$ FATAL:  invalid value for parameter "shared_buffers": "222222GB"HINT:  Value exceeds integer range. 


2.2 max_connections VS semget 

max_connections是最大连接数,即允许客户端连接的最大连接数。增大连接可以允许接入更多的客户端,但设置过大同样会造成DB启动失败。 semget是获取信号的一个函数,即get semaphore 。

[postgres@localhost ~]$ vi /database/pgdata/postgresql.conf max_connections = 5000[postgres@localhost ~]$ pg_startserver starting[postgres@localhost ~]$ FATAL:  could not create semaphores: No space left on deviceDETAIL:  Failed system call was semget(1949125, 17, 03600).HINT:  This error does *not* mean that you have run out of disk space.  It occurs when either the system limit for the maximum number of semaphore sets (SEMMNI), or the system wide maximum number of semaphores (SEMMNS), would be exceeded.  You need to raise the respective kernel parameter.  Alternatively, reduce PostgreSQL's consumption of semaphores by reducing its max_connections parameter.    The PostgreSQL documentation contains more information about configuring your system for PostgreSQL.


上述的空间不够不是指的是磁盘空间不够,而是创建semaphores时空间参数不够,系统调用参数semget报错,但是错误信息感觉有些迷惑......

解决办法通常是减小max_connections,或者增大内核参数,如semmni,semmns等,在/proc/sys/kernel/sem里面调整,如:

[root@localhost ~]# sysctl -w kernel.sem="500 64000 50 150"kernel.sem = 500 64000 50 150[root@localhost ~]# cat /proc/sys/kernel/sem500     64000   50      150


2.3 附参数说明:

 Name Desc Reasonable Value 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 processonly 1 segment is needed, but the default is much higher shmmniMaximum number of shared memory segments system-widelike SHMSEG plus room for other applications semmniMaximum number of semaphore identifiers (i.e., sets)at least ceil((max_connections + autovacuum_max_workers + 4) / 16) semmns Maximum number of semaphores system-wideceil((max_connections + autovacuum_max_workers+ 4) / 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 semaphoreat least 1000 (The default is often 32767; do not change unless necessary)

2.4 共享内存的使用:
<span style="font-weight: normal;"> usage Approximate shared memory bytesConnections  (1800 + 270 * max_locks_per_transaction) * max_connections Autovacuum workers (1800 + 270 * max_locks_per_transaction) * autovacuum_max_workers Prepared  transactions (770 + 270 * max_locks_per_transaction) * max_prepared_transactions Shared disk buffers (block_size + 208) * shared_buffers Wal buffers (wal_block_size + 8) * wal_buffersFixed space requirements770KB</span>

0 0
原创粉丝点击