Postgres與OS內核相關的幾個參數設置

Postgres在postgresql.conf裏面的配置參數有幾個是和OS的內核參數緊密相關的,一般默認值是偏小的,但設置過大也會形成Postgres的啓動失敗,官方文檔(Part 17.3)有較詳細的說明,但沒有例子,這裏給出實際示例。

測試環境:
DB: postgres 9.1.3
OS: CentOS 6.2 / Redhat
--內核參數文件位置:
/proc/sys/kernel
/etc/sysctl.conf
[root@localhost ~]# sysctl -a|more
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
kernel.shmmni = 4096
kernel.msgmax = 65536
kernel.msgmni = 2005
kernel.msgmnb = 65536
[postgres@localhost ~]$ cat /proc/sys/kernel/sem
250     32000   32      128
(semmsl  semmns  semopm  semmni)

1.shared_buffers VS shmget
shared_buffers是共享內存的意思,若是該值超過系統的內存值(包括swap),會形成啓動失敗
shmget是get share memory,它是建立一個共享內存的函數
[root@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          1006        872        134          0        100        629
-/+ buffers/cache:        142        863
Swap:         2015         13       2002
[root@localhost ~]# su - postgres
[postgres@localhost ~]$ vi /database/pgdata/postgresql.conf 
shared_buffers = 5GB
[postgres@localhost ~]$ pg_start
server starting
[postgres@localhost ~]$ FATAL:  could not create shared memory segment: Cannot allocate memory
DETAIL:  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,如 sql

[postgres@localhost ~]$ vi /database/pgdata/postgresql.conf
shared_buffers = 222222GB
max_connections = 100
[postgres@localhost ~]$ pg_start
server starting
[postgres@localhost ~]$ FATAL:  invalid value for parameter "shared_buffers": "222222GB"
HINT:  Value exceeds integer range. 
2.max_connections VS semget
max_connections是最大鏈接數,即容許客戶端鏈接的最大鏈接數,增大鏈接能夠容許接入更多的客戶端,但設置過大一樣會形成DB啓動失敗
semget是獲取信號的一個函數,即get semaphore
[postgres@localhost ~]$ vi /database/pgdata/postgresql.conf 
max_connections = 5000
[postgres@localhost ~]$ pg_start
server starting
[postgres@localhost ~]$ FATAL:  could not create semaphores: No space left on device
DETAIL:  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/sem
500     64000   50      150
附參數說明

 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 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 + 4) / 16)
 semmns  Maximum number of semaphores system-wide ceil((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 semaphore at least 1000 (The default is often 32767; do not change unless necessary)















 

 

 

 

 

 

 

共享內存的使用: app

 usage  Approximate shared memory bytes
Connections   (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_buffers
Fixed space requirements 770KB
相關文章
相關標籤/搜索