postgresql pgsql connection pool pgBouncer (details)

Applying connection pooling can effectively reduce the loss caused by repeated connections
. Updated on 2023.9.28, demo: version 1.20.1

If pgsql is not installed, please refer to: pgsql compilation and installation

1. Compile and install

Package installation is easier, but you can’t choose the version

1.pgBouncer download address

Link: github project address
Link: pgbouncer official website

2.Download

Enter directory

cd /usr/local

官网下载

wget https://www.pgbouncer.org/downloads/files/1.20.1/pgbouncer-1.20.1.tar.gz

or
github下载

wget https://github.com/pgbouncer/pgbouncer/releases/download/pgbouncer_1_20_1/pgbouncer-1.20.1.tar.gz

3. Unzip

tar -zxvf pgbouncer-1.20.1.tar.gz && cd pgbouncer-1.20.1

4. Depend on installation

apt-get install libevent-dev libsystemd-dev

5. Compile and install

For more parameters, please refer to the official documentation: Official documentation

./configure --prefix=/usr/local --with-systemd --with-pam
make && make install

After the installation is complete, the relevant file locations are prompted as follows:
Insert image description here

2. Patterns and Principles

(1) Session mode

More people actually use the second level

1.Session pooling/session connection pool

This method is similar to not using a connection pool, and the effect is not obvious.
Session-level connection. During the life cycle of the client connection, the connection pool allocates a connection to it. The allocated connection will not return to the connection until the client disconnects. in the pool.

2.Transaction pooling/transaction connection pool (recommended)

Server connections are only granted to clients within a transaction. When PgBouncer notices the end of the transaction, the server will be put back into the connection pool. This is a hack because it breaks the application's view of the backend connection. This connection method should only be used if the application is compatible with such usage patterns and if the use would not disrupt the usage patterns. See subscript retrieval for features that break this pattern.

3.Statement pooling/statement connection pool

The most aggressive mode. This is a twisted variant of transactional connection pooling - multi-statement transactions are not allowed. This means forcing "autocomit" mode on the client, mainly for PL/Proxy.

3. Configuration and management

(1) Configuration file

1. Create a configuration file

Personal habit is to store configuration files in the /etc directory

mkdir /etc/pgbouncer

Copy configuration file

cp /usr/local/share/doc/pgbouncer/pgbouncer.ini /etc/pgbouncer

2. Create the required directories

创建日志目录和日志文件

mkdir -p /var/pgbouncer

Transfer ownership to the postgres user (linux pgsql, this user is customarily used)

chown postgres:postgres /var/pgbouncer

3. Edit configuration file

vi /etc/pgbouncer/pgbouncer.ini

添加数据库连接[database]下
The account password is the account password of pgsql, which is used to connect to the existing pgsql database. The
front name main_pg here is the postgres library of the associated source database. Main_pg is used as the virtual database when connecting, instead of localhost:5432. The postgres database is equivalent to an alias. When the time comes to connect, it will connect to the main_pg virtual database.

main_pg = host=localhost port=5432 dbname=postgres user=postgres password=abc123456 

修改log和pid地址

Insert image description here
允许外部访问
listen_addr = *

修改admin_users
If there is no authentication, this parameter will be invalid.

admin_users = postgres

修改每个池最大连接数
Find max_client_conn, change it to 1000, and delete the previous one;

max_client_conn = 1000

修改连接池数
Find default_pool_size, change it to 200, and delete the previous one;

default_pool_size = 200

修改连接池模式
Find pool_mode = session, change it to the following, and delete the previous one;

pool_mode = transaction

设置ignore_startup
Delete the previous one;

ignore_startup_parameters = extra_float_digits

(2) Connection file configuration

1. Copy the certification file

cp /usr/local/share/doc/pgbouncer/userlist.txt /etc/pgbouncer

2. Edit the database account password file

vi /etc/pgbouncer/userlist.txt

Change it to a form similar to the following. The front is the user name of pgsql, postgres is the default account name, and the password is a clear text password, which
looks like the following part

"postgres" "abc123456"

This password is the password to connect to pgbouncer. It can be inconsistent with the password of the pgsql source library. This is equivalent to an extra layer of protection.

(3) systemctl management

1. Copy the systemctl file

cp /usr/local/share/doc/pgbouncer/pgbouncer.service /lib/systemd/system

2. Edit files

vi /lib/systemd/system/pgbouncer.service

Modify the ExecStart address (you will be prompted after compilation is completed).
According to my directory, it is /usr/local/bin.

Insert image description here

3. Start the program

systemctl daemon-reload && systemctl start pgbouncer

4. Check status

systemctl status pgbouncer

(4) Connection test

1. Enter pgBouncer’s psql

su postgres
psql -p 6432 pgbouncer

The password is the password in the connection file set above

2.Commands that come with pgbouncer

显示所有配置

show config; 

显示所有客户连接

show clients;

显示所有的源数据库

show servers;

4. Use

1. Connection principle

Use psql to connect to the main_pg database of pgbouncer (pgbouncer is connected to the source database of main_pg at this time).
In this way, the pool of pgsql is implemented.

psql -h localhost -p 6432 -U postgres -d main_pg

At this time, enter the command in the source database to query the pgbouncer connection.

select * from pg_stat_activity;

2. Called in the back-end program

In this case, the backend connects to the main_pg database on port 6432, which can correspond to the original postgres database on port 5432. You
only need to modify the connection address, and there is no need to adjust the backend logic (unless it changes to the third mode, which is not recommended)

Guess you like

Origin blog.csdn.net/ziqibit/article/details/133361307