Postgres与OS内核相关的几个参数设置

Stella981
• 阅读 696

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,如

[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)

 

共享内存的使用:

 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

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
Python之time模块的时间戳、时间字符串格式化与转换
Python处理时间和时间戳的内置模块就有time,和datetime两个,本文先说time模块。关于时间戳的几个概念时间戳,根据1970年1月1日00:00:00开始按秒计算的偏移量。时间元组(struct_time),包含9个元素。 time.struct_time(tm_y
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Java服务总在半夜挂,背后的真相竟然是... | 京东云技术团队
最近有用户反馈测试环境Java服务总在凌晨00:00左右挂掉,用户反馈Java服务没有定时任务,也没有流量突增的情况,Jvm配置也合理,莫名其妙就挂了
Python进阶者 Python进阶者
11个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这