PgBouncer工具可以用于PostgreSQL和Greenplum数据库连接的连接池。
在Greenplum数据库中使用pgBouncer作为连接池管理器是有益的,尤其是在高并发的场景下。pgBouncer作为一个轻量级的数据库连接池管理器,它可以有效减少数据库服务器建立和销毁连接的开销,提高系统性能和稳定性。
以下是在Greenplum中使用pgBouncer的一些好处:
pgbouncer 官网介绍 http://www.pgbouncer.org/
配置文件:http://www.pgbouncer.org/config.html
1、pgbouncer是 PostgreSQL的轻量的连接池,可以有效降低连接数,提升系统性能。Greenplum默认已经自带pgbouncer工具,无需额外安装,只是多数组织在实践中似乎并未启用此服务,也算是一种资源的浪费了。
2、pgbouncer默认存在于Greenplum数据库的bin目录下。
[gpadmin@mdw ~]$ which pgbouncer
/usr/local/greenplum-db-6.26.3/bin/pgbouncer
3、pgbouncer可以配置在运行在Greenplum数据库的master上或另一台机器上,用户最好运行在Greenplum数据库的master上。
4、若配置了[databases]
的具体值,则使用pgbouncer不能切换到其它数据库中
postgres=# \c db1
connection to server at "192.16.7.163", port 6432 failed: FATAL: no such database: db1
Previous connection kept
postgres=# \l db1
List of databases
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
------+-------+----------+------------+------------+------------+-----------------+-------------------
db1 | xxt | UTF8 | en_US.utf8 | en_US.utf8 | | libc |
(1 row)
若想pgbouncer可以连接任意数据库,则可以在[databases]
下配置* =
5、 欧拉系统openEuler 22.03 LTS需要安装libevent-2.0.21包,否则会报错:/usr/local/greenplum-db/bin/pgbouncer: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
rpm -ivh libevent-2.0.21-4.el7.x86_64.rpm --force
6、报错“pooler error: unsupported startup parameter: search_path”,或“unsupported startup parameter: extra_float_digits”
ignore_startup_parameters = extra_float_digits,search_path
7、端口默认为6432,也可以配置6543端口。
8、我使用auth_query配置后,并不能从数据库中查询密码,可能是版本问题吧,所以只能在创建新用户或更新用户密码后,需要先执行导出密码copy (select '"'||usename ||'" "' || passwd||'"' from pg_shadow order by 1) to '/usr/local/greenplum-db/pgbouncer/users.txt';
failed: FATAL: server login has been failing, try again later (server_login_retry)
failed: FATAL: bouncer config error
9、若报错“failed: FATAL: no pg_hba.conf entry for host "[local]", user "pgbouncer", database "postgres", SSL off”,可以考虑在GreenPlum的master节点的pg_hba.conf文件中配置如下内容:
local all all md5
10、使用pgbouncer的一个缺点是:在GP库中直接查询时,只能看到客户端的IP是pgbouncer的IP。所以,若出现问题,要进行追踪是,增加了难度。但是,可以增加参数application_name_add_host=1
用来记录客户端的真实的IP和端口,然后在的pg_stat_activity.application_name中就可以看到了。如下:
docker rm -f gpdb6270
docker run -itd --name gpdb6270 -h gpdb6270 \
-p 5627:5432 -p 26270:28080 \
-v /sys/fs/cgroup:/sys/fs/cgroup \
--privileged=true lhrbest/greenplum:6.27.0_v2 \
/usr/sbin/init
docker exec -it gpdb6270 bash
su - gpadmin
gpstart -a
gpcc start
gpcc status
gpstate
在mdw服务器上做如下的配置。
create user pgbouncer password 'lhr' superuser;
mkdir -p /usr/local/greenplum-db/pgbouncer
cat > /usr/local/greenplum-db/pgbouncer/pgbouncer.ini <<"EOF"
[databases]
* = host=127.0.0.1 port=5432
[pgbouncer]
pool_mode = session
listen_port = 6432
listen_addr = *
auth_type = md5
auth_user = pgbouncer
auth_file = /usr/local/greenplum-db/pgbouncer/users.txt
logfile = /usr/local/greenplum-db/pgbouncer/pgbouncer.log
pidfile = /usr/local/greenplum-db/pgbouncer/pgbouncer.pid
admin_users = pgbouncer
default_pool_size=60
max_client_conn=2000
ignore_startup_parameters = extra_float_digits,search_path
so_reuseport = 1
application_name_add_host=1
EOF
psql
copy (select '"'||usename ||'" "' || passwd||'"' from pg_shadow order by 1) to '/usr/local/greenplum-db/pgbouncer/users.txt';
/usr/local/greenplum-db/bin/pgbouncer -d /usr/local/greenplum-db/pgbouncer/pgbouncer.ini
/usr/local/greenplum-db/bin/pgbouncer -Rd /usr/local/greenplum-db/pgbouncer/pgbouncer.ini
[gpadmin@gpdb6270 ~]$ psql -U pgbouncer -h127.0.0.1 -p 6432 -d pgbouncer
Password for user pgbouncer:
psql (9.4.26, server 1.21.0/bouncer)
Type "help" for help.
pgbouncer=# show clients;
type | user | database | state | addr | port | local_addr | local_port | connect_time | request_time | wait | wait_us | close_needed | ptr | link | remote_pid | tls | application_name | prepared_statements
------+-----------+-----------+--------+-----------+-------+------------+------------+-------------------------+-------------------------+------+---------+--------------+-----------+------+------------+-----+------------------------+---------------------
C | pgbouncer | pgbouncer | active | 127.0.0.1 | 47030 | 127.0.0.1 | 6432 | 2024-04-19 18:18:34 CST | 2024-04-19 18:18:38 CST | 0 | 0 | 0 | 0x12cfb40 | | 0 | | psql - 127.0.0.1:47030 | 0
(1 row)
pgbouncer=#
普罗米修斯+grafana的详细搭建过程,之前麦老师介绍过很多次了,这里直接使用docker来创建:
docker rm -f lhrprometheus
docker run -d --name lhrprometheus -h lhrprometheus \
-p 2222:22 -p 23389:3389 \
-p 29090:9090 -p 29093:9093 -p 23000:3000 \
-v /sys/fs/cgroup:/sys/fs/cgroup --restart=always \
--privileged=true lhrbest/lhrprometheus:2.0 \
/usr/sbin/init
docker exec -it lhrprometheus bash
目前对pgbouncer的监控有2个工具,分别为:https://github.com/spreaker/prometheus-pgbouncer-exporter 和 https://github.com/prometheus-community/pgbouncer_exporter,下面分别介绍。
缺点:安装麻烦,2022.05.20后再没更新过
参考:https://github.com/spreaker/prometheus-pgbouncer-exporter dashboards使用:13353 https://grafana.com/grafana/dashboards/13353-pgbouncer/
docker地址:https://hub.docker.com/r/spreaker/prometheus-pgbouncer-exporter
安装过程:
-- 1、需要有pg_config
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sed -i 's/gpgcheck=1/gpgcheck=0/g' /etc/yum.repos.d/pgdg-redhat-all.repo
sed -i 's/repo_gpgcheck = 1/repo_gpgcheck = 0/g' /etc/yum.repos.d/pgdg-redhat-all.repo
yum install -y postgresql15 postgresql15-server postgresql15-contrib postgresql15-libs
echo "export PATH=/usr/pgsql-15/bin:$PATH" >> /etc/profile
-- 2、需要先安装依赖包 ,否则报错
./psycopg/psycopg.h:36:22: fatal error: libpq-fe.h: No such file or directory
./psycopg/psycopg.h:35:20: fatal error: Python.h: No such file or directory
yum install -y python3-devel libpq5 libpq5-devel
pip3 install prometheus-pgbouncer-exporter
-- 3、启动
nohup pgbouncer-exporter --config /etc/pgbouncer-exporter/config.yml &
curl http://127.0.0.1:9100/metrics
mkdir -p /etc/pgbouncer-exporter/
cat > /etc/pgbouncer-exporter/config.yml <<EOF
# The host on which the exporter should listen to (defaults to 127.0.0.1)
exporter_host: 0.0.0.0
# The port on which the exporter should listen to (defaults to 9100)
exporter_port: 9100
# The list of pgbouncer instances to monitor
pgbouncers:
- dsn: postgresql://pgbouncer:lhr@192.16.7.163:6543/pgbouncer
exclude_databases:
- pgbouncer
extra_labels:
pool_id: 1
EOF
-- 或者使用docker直接创建也可以
docker run -d \
--name pgbouncer-exporter \
-v /etc/timezone:/etc/timezone \
-v /etc/localtime:/etc/localtime \
-p 9127:9127 \
-v /etc/pgbouncer-exporter/config.yml:/etc/pgbouncer-exporter/config.yml \
--restart=unless-stopped \
spreaker/prometheus-pgbouncer-exporter:2.0.3
-- 4、修改配置文件: vi /usr/local/prometheus/prometheus.yml
- job_name: 'pgbouncer'
static_configs:
- targets: ['localhost:9100']
labels:
instance: 192.16.7.163:6543
node_ip: 192.16.7.163:6543
curl -XPOST http://localhost:9090/-/reload
-- 5、grafana添加dashboards
13353
监控结果:
监控指标:
Metric name | Type | PgBouncer | Description |
---|---|---|---|
pgbouncer_stats_requests_total | counter | <= 1.7 | Total number of requests pooled. Could be transactions or queries, depending on pool mode. (labels: database) |
pgbouncer_stats_queries_total | counter | >= 1.8 | Total number of SQL queries pooled by pgbouncer (labels: database) |
pgbouncer_stats_queries_duration_microseconds | counter | all | Total number of microseconds spent waiting for a server to return a query response. Includes time spent waiting for an available connection. (labels: database) |
pgbouncer_stats_waiting_duration_microseconds | counter | >= 1.8 | Total number of microseconds spent waiting for an available connection. (labels: database) |
pgbouncer_stats_received_bytes_total | counter | all | Total volume in bytes of network traffic received by pgbouncer (labels: database) |
pgbouncer_stats_sent_bytes_total | counter | all | Total volume in bytes of network traffic sent by pgbouncer (labels: database) |
pgbouncer_stats_transactions_total | counter | >= 1.8 | Total number of SQL transactions pooled by pgbouncer (labels: database) |
pgbouncer_stats_transactions_duration_microseconds | counter | >= 1.8 | Total number of microseconds spent in a transaction. Includes time spent waiting for an available connection. (labels: database) |
pgbouncer_pools_client_active_connections | gauge | all | Client connections that are linked to server connection and can process queries (labels: database, user) |
pgbouncer_pools_client_waiting_connections | gauge | all | Client connections have sent queries but have not yet got a server connection (labels: database, user) |
pgbouncer_pools_server_active_connections | gauge | all | Server connections that linked to client (labels: database, user) |
pgbouncer_pools_server_idle_connections | gauge | all | Server connections that unused and immediately usable for client queries (labels: database, user) |
pgbouncer_pools_server_used_connections | gauge | all | Server connections that have been idle more than server_check_delay, so they needs server_check_query to run on it before it can be used (labels: database, user) |
pgbouncer_pools_server_testing_connections | gauge | all | Server connections that are currently running either server_reset_query or server_check_query (labels: database, user) |
pgbouncer_pools_server_login_connections | gauge | all | Server connections currently in logging in process (labels: database, user) |
pgbouncer_pools_client_maxwait_seconds | gauge | all | How long the first (oldest) client in queue has waited, in seconds (labels: database, user) |
pgbouncer_databases_database_pool_size | gauge | all | Configured pool size limit (labels: database, backend_database) |
pgbouncer_databases_database_reserve_pool_size | gauge | all | Configured reserve limit (labels: database, backend_database) |
pgbouncer_databases_database_current_connections | gauge | all | Total number of per-database Database connections count (labels: database, backend_database) |
pgbouncer_databases_database_max_connections | gauge | all | Maximum number of allowed connections per-database (labels: database, backend_database) |
pgbouncer_config_max_client_conn | gauge | all | Configuration of maximum number of allowed client connections |
pgbouncer_config_max_user_connections | gauge | all | Configuration of maximum number of server connections per user |
优点:安装简单,配置也简单。
参考:https://github.com/prometheus-community/pgbouncer_exporter
docker地址:https://hub.docker.com/r/prometheuscommunity/pgbouncer-exporter
-- 1、安装
wget https://github.com/prometheus-community/pgbouncer_exporter/releases/download/v0.8.0/pgbouncer_exporter-0.8.0.linux-amd64.tar.gz
tar -zxvf pgbouncer_exporter-0.8.0.linux-amd64.tar.gz
cd pgbouncer_exporter-0.8.0.linux-amd64
mv pgbouncer_exporter /usr/local/bin/
nohup pgbouncer_exporter --pgBouncer.connectionString="postgres://pgbouncer:lhr@192.16.7.163:6543/pgbouncer?sslmode=disable" &
curl http://127.0.0.1:9127/metrics
-- 2、修改配置文件: vi /usr/local/prometheus/prometheus.yml
- job_name: 'pgbouncer1'
static_configs:
- targets: ['localhost:9127']
labels:
instance: 192.16.7.163:6543
curl -XPOST http://localhost:9090/-/reload
-- 3、grafana添加dashboards
14022
监控结果:
监控指标:
PgBouncer column | Prometheus Metric | Description |
---|---|---|
stats_total_query_count | pgbouncer_stats_queries_pooled_total | Total number of SQL queries pooled |
stats.total_query_time | pgbouncer_stats_queries_duration_seconds_total | Total number of seconds spent by pgbouncer when actively connected to PostgreSQL, executing queries |
stats.total_received | pgbouncer_stats_received_bytes_total | Total volume in bytes of network traffic received by pgbouncer, shown as bytes |
stats.total_requests | pgbouncer_stats_queries_total | Total number of SQL requests pooled by pgbouncer, shown as requests |
stats.total_sent | pgbouncer_stats_sent_bytes_total | Total volume in bytes of network traffic sent by pgbouncer, shown as bytes |
stats.total_wait_time | pgbouncer_stats_client_wait_seconds_total | Time spent by clients waiting for a server in seconds |
stats.total_xact_count | pgbouncer_stats_sql_transactions_pooled_total | Total number of SQL transactions pooled |
stats.total_xact_time | pgbouncer_stats_server_in_transaction_seconds_total | Total number of seconds spent by pgbouncer when connected to PostgreSQL in a transaction, either idle in transaction or executing queries |
pools.cl_active | pgbouncer_pools_client_active_connections | Client connections linked to server connection and able to process queries, shown as connection |
pools.cl_waiting | pgbouncer_pools_client_waiting_connections | Client connections waiting on a server connection, shown as connection |
pools.sv_active | pgbouncer_pools_server_active_connections | Server connections linked to a client connection, shown as connection |
pools.sv_idle | pgbouncer_pools_server_idle_connections | Server connections idle and ready for a client query, shown as connection |
pools.sv_used | pgbouncer_pools_server_used_connections | Server connections idle more than server_check_delay, needing server_check_query, shown as connection |
pools.sv_tested | pgbouncer_pools_server_testing_connections | Server connections currently running either server_reset_query or server_check_query, shown as connection |
pools.sv_login | pgbouncer_pools_server_login_connections | Server connections currently in the process of logging in, shown as connection |
pools.maxwait | pgbouncer_pools_client_maxwait_seconds | Age of oldest unserved client connection, shown as second |
config.max_client_conn | pgbouncer_config_max_client_connections | Configured maximum number of client connections |
config.max_user_connections | pgbouncer_config_max_user_connections | Configured maximum number of server connections per user |
GreenPlum中的pgbouncer连接池参考:https://www.xmmup.com/zaigreenplumzhongshiyongpgbouncerlianjiechi.html
PostgreSQL中的pgbouncer连接池参考:https://www.xmmup.com/pglianjiechigongjuzhipgbouncer.html