科学地增加postgresql最大连接数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了科学地增加postgresql最大连接数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3252字,纯文字阅读大概需要5分钟。
内容图文

PG配置文件路径
/etc/postgresql/9.3/main/postgresql.conf
首先如何查看最大连接数
This SQL will help you
select max_conn,used,res_for_super,max_conn-used-res_for_super res_for_normal
from
(select count(*) used from pg_stat_activity) t1,
(select setting::int res_for_super from pg_settings where name=$$superuser_reserved_connections$$) t2,
(select setting::int max_conn from pg_settings where name=$$max_connections$$) t3
Result:
max_conn | used | res_for_super | res_for_normal
---------+------+---------------+----------------
100 | 2 | 3 | 95
(1 row)
You can put this in shell:
#!/bin/bash
for (( c=1; c<=3600; c++ ))
do
gsql -U pgdba -W pgdba -p 6432 -c "sql" >> /home/pgdba/res_data.log
sleep 1 # once per second
done
or you can record the results into a table, then execute
postgres=# copy restbl to ‘/home/pgdba/res.csv‘ csv header;
to get result csv file.
其次如何科学设置
https://stackoverflow.com/a/32584211/2803340
Just increasing max_connections
is bad idea. You need to increase shared_buffers
and kernel.shmmax
as well.
Considerations
max_connections
determines the maximum number of concurrent connections to the database server. The default is typically 100 connections.
Before increasing your connection count you might need to scale up your deployment. But before that, you should consider whether you really need an increased connection limit.
Each PostgreSQL connection consumes RAM for managing the connection or the client using it. The more connections you have, the more RAM you will be using that could instead be used to run the database.
A well-written app typically doesn‘t need a large number of connections. If you have an app that does need a large number of connections then consider using a tool such as pg_bouncer which can pool connections for you. As each connection consumes RAM, you should be looking to minimize their use.
How to increase max connections
1. Increase max_connection
and shared_buffers
in /var/lib/pgsql/data/postgresql.conf
change
max_connections = 100
shared_buffers = 24MB
to
max_connections = 300
shared_buffers = 80MB
The shared_buffers
configuration parameter determines how much memory is dedicated to PostgreSQL to use for caching data.
- If you have a system with 1GB or more of RAM, a reasonable starting
value for shared_buffers is 1/4 of the memory in your system. - it‘s unlikely you‘ll find using more than 40% of RAM to work better
than a smaller amount (like 25%) - Be aware that if your system or PostgreSQL build is 32-bit, it might
not be practical to set shared_buffers above 2 ~ 2.5GB. - Note that on Windows, large values for shared_buffers aren‘t as
effective, and you may find better results keeping it relatively low
and using the OS cache more instead. On Windows the useful range is
64MB to 512MB.
2. Change kernel.shmmax
You would need to increase kernel max segment size to be slightly larger
than the shared_buffers
.
In file /etc/sysctl.conf
set the parameter as shown below. It will take effect when postgresql
reboots (The following line makes the kernel max to 96Mb
)
kernel.shmmax=100663296
References
How to increase max connections in postgres
Postgres Max Connections And Shared Buffers
原文:https://www.cnblogs.com/xiangnan/p/10051240.html
内容总结
以上是互联网集市为您收集整理的科学地增加postgresql最大连接数全部内容,希望文章能够帮你解决科学地增加postgresql最大连接数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。