pgbouncer是一个PostgreSQL的连接池。
启用之后再应用程序看来它就像它是PostgreSQL服务器一样,然后pgbouncer将创建到实际服务器的连接, 或者它将重用其中一个现有的连接。
使用pgbouncer的很大目的是为了降低打开PostgreSQL新连接时的性能影响。以及实际上建立的隔离作用。
- pgbouncer管理界面
pgbouncer有一个基于虚拟数据库pgbouncer的控制台。
可以使用如下正常连接pgbouncer控制台
psql -h 192.168.5.101 -p 6432 -U postgres pgbouncer
-U 所用用户为启用pgbouncer配置文件内声明的管理用户(特例,auth_mode=any 时任何用户都可以登录控制台)
#查看pgbouncer配置文件
[pg12@node1 pgbouncer]$ ps -ef |grep pgbouncer
zhengxu+ 15560 1 0 02:47 ? 00:00:05 /home/pg12/pgbouncer/bin/pgbouncer -d -v /home/pg12/pgbouncer/pgbouncer.ini
[pg12@node1 pgbouncer]$ cat pgbouncer.ini
[databases]
appdb = host = 192.168.5.101 dbname=postgres port=23527
[users]
[pgbouncer]
logfile = /home/pg12/pgbouncer/pgbouncer.log
pidfile = /home/pg12/pgbouncer/pgbouncer.pid
listen_addr = *
listen_port = 6432
pool_mode = statement
admin_users = postgres
auth_type = trust
auth_file = /home/pg12/pgbouncer/userlist.txt
# 连接pgbouncer控制台:
[pg12@node1 pgbouncer]$ psql -h 192.168.5.101 -p 6432 -U postgres pgbouncer
psql (12.15, server 1.15.0/bouncer)
Type "help" for help.
2023-06-02 09:55:34 pgbouncer=# \l
ERROR: invalid command 'SELECT d.datname as "Name",
pg_catalog.pg_get_userbyid(d.datdba) as "Owner",
pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",
pg_catalog.array_to_string(d.datacl, '\n') AS "Access privileges"
FROM pg_catalog.pg_database d
ORDER BY 1;', use SHOW HELP;
2023-06-02 09:55:44 pgbouncer=# \d
ERROR: failure
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Succeeded.
psql (12.15, server 1.15.0/bouncer)
2023-06-02 09:55:50 pgbouncer=#
# 因为pgbounver只是虚拟数据库, 不是正常数据库不支持基本查询。
2.在管理pgbouncer界面查看连接池信息
在控制台使用show命令查看各种信息。
如
show stats; 显示统计信息。
show servers; 显示连接的pg服务器信息。
show clients; 显示连接到pgbouncer的客户端信息。
show pools; 显示连接池信息。
其中 pool_mode字段即显示正在使用的连接池模式。可以与配置文件相印证。
如下即为运行在statement语句连接池模式。
2023-06-02 10:18:07 pgbouncer=# show pools;
database | user | cl_active | cl_waiting | sv_active | sv_idle | sv_used | sv_tested | sv_login | maxwait | maxwait_us | pool_mode
-----------+-----------+-----------+------------+-----------+---------+---------+-----------+----------+---------+------------+-----------
appdb | postgres | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | statement
pgbouncer | pgbouncer | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | statement
(2 rows)
# 几个解释:
# database 数据库名称
# user 用户名
# cl_active 链接到服务器连接并可以处理查询的客户端连接
# cl_waiting 已发送查询但尚未获得服务器连接的客户端连接
# sv_active 链接到客户端的服务器连接。
# sv_idle 未使用且可立即用于客户机查询的服务器连接。
# sv_login 当前正在登录过程中的服务器连接。
# maxwait 队列中第一个(最老的)客户端已经等待了多长时间(秒)。如果它相对增加,表明当前的连接池处理请求的速度不够快。原因可能是服务器负载过重或pool_size设置过小。
# pool_mode 正在使用的连接池模式。
2023-06-02 10:30:38 pgbouncer=# show stats;
database | total_xact_count | total_query_count | total_received | total_sent | total_xact_time | total_query_time | total_wait_time | avg_xact_count | avg_query_count | avg_recv | avg_sent | avg_xact_t
ime | avg_query_time | avg_wait_time
-----------+------------------+-------------------+----------------+------------+-----------------+------------------+-----------------+----------------+-----------------+----------+----------+-----------
----+----------------+---------------
appdb | 34 | 34 | 490 | 1805 | 18443 | 18443 | 2273688 | 0 | 0 | 0 | 0 | 0 | 0 | 0
pgbouncer | 3 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
(2 rows)
#字段说明:
字段 说明
database 统计信息按数据库组织
total_xact_count SQL事务总数
total_query_count SQL查询总数
total_received 收到的网络流量(字节)
total_sent 发送的网络流量(字节)
total_xact_time 在事务中的总时长
total_query_time 在查询中的总时长
total_wait_time 在等待中的总时长
avg_xact_count 平均事务数(当前)
avg_query_count 平均查询数(当前)
avg_recv 平均每秒收到字节数(当前)
avg_sent 平均每秒发送字节数(当前)
avg_xact_time 平均事务时长(毫秒)
avg_query_time 平均查询时长(毫秒)
avg_wait_time 平均等待时长(毫秒)
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




