为什么你的pgbouncer连接这么慢

在PostgreSQL中;说得连接池,立马想到pgbouncer。pgbouncer 的目标是降低因为新建到 PostgreSQL/Greenplum 的连接而导致的性能损失。

在项目中;DBA会劝开发人员使用连接池。这样可以控制连接数。也避免反复建立连接带来的性能消耗。对连接池的优点。这篇文章讲解比较好。

但是在开发人员在使用连接池的过程中,一般开发人员那边也有自己的jdbc的连接池。在启动项目的过程;于是出现下面这段对话

开发人员:这项目启动好慢;原因是连接数据库好慢。

DBA:这项目启动时申请多少数据库连接。

开发人员:没多少;也就50。峰值不会超过400。

DBA:直连数据库会慢吗?

开发人员:不会,启动很快。

连接池pgbouncer是一个针对PostgreSQL数据库的轻量级连接池。但是单线程的。我观察在启动的过程中;连接数是一条一条增加的。并不是你需要多少就给多少

[postgres@bogon pgbouncer]$ ps -ef | grep postgres | grep 127 |wc -l
44
[postgres@bogon pgbouncer]$ ps -ef | grep postgres | grep 127 |wc -l
45
[postgres@bogon pgbouncer]$ ps -ef | grep postgres | grep 127 |wc -l
46
[postgres@bogon pgbouncer]$ ps -ef | grep postgres | grep 127 |wc -l
47
[postgres@bogon pgbouncer]$ ps -ef | grep postgres | grep 127 |wc -l
47
[postgres@bogon pgbouncer]$ ps -ef | grep postgres | grep 127 |wc -l
49
[postgres@bogon pgbouncer]$ ps -ef | grep postgres | grep 127 |wc -l
49
[postgres@bogon pgbouncer]$ ps -ef | grep postgres | grep 127 |wc -l
49
[postgres@bogon pgbouncer]$ ps -ef | grep postgres | grep 127 |wc -l
49
[postgres@bogon pgbouncer]$ ps -ef | grep postgres | grep 127 |wc -l
50

若是这样的话;那我们知道为什么项目通过pgbouncer启动很慢了。
若我们提前申请一批连接资源做为idle。等项目启动的时候直接可以用不久解决了,说干就干。

在pgbouncer的配置文件中设置

min_pool_size = 50
posted @ 2021-01-19 14:38  lottu  阅读(418)  评论(0编辑  收藏  举报