pgbouncer

Download

wget https://repo.percona.com/apt/percona-release_latest.generic_all.deb
sudo dpkg -i percona-release_latest.generic_all.deb
sudo percona-release setup ppg-18
sudo apt install percona-pgbouncer -y

pgbouncer user

-- This file sets up the database role and function for PgBouncer authentication.
CREATE ROLE pgbouncer_auth WITH LOGIN PASSWORD 'REPLACE_ME_LONG_RANDOM5432';

-- Execute the following so the pgbouncer_auth role can access the get_auth function and schema.
GRANT USAGE ON SCHEMA pgbouncer TO pgbouncer_auth;

Get Auth

-- This file sets up the database role and function for PgBouncer authentication.
CREATE SCHEMA IF NOT EXISTS pgbouncer;
GRANT USAGE ON SCHEMA pgbouncer TO pgbouncer_auth;

-- Create the get_auth function that PgBouncer will call to retrieve user credentials.
CREATE OR REPLACE FUNCTION pgbouncer.get_auth(p_usename text)
RETURNS TABLE(username text, password text)
LANGUAGE sql
SECURITY DEFINER
AS $$
  SELECT rolname::text, rolpassword::text
  FROM pg_authid
  WHERE rolname = p_usename;
$$;

-- Execute the following so the pgbouncer_auth role can access the get_auth function and schema.
REVOKE ALL ON FUNCTION pgbouncer.get_auth(text) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION pgbouncer.get_auth(text) TO pgbouncer_auth;

Min pgbouncer.ini

[databases]
aws_dbz_001 = host=172.31.11.248 port=5432 dbname=example auth_user=pgbouncer_auth
aws_dbz_001_ro = host=172.31.9.92 port=5432 dbname=example auth_user=pgbouncer_auth
aws_dbz_sales = host=172.31.11.248 port=5432 dbname=sales auth_user=pgbouncer_auth
aws_dbz_sales_ro = host=172.31.9.92 port=5432 dbname=sales auth_user=pgbouncer_auth


[pgbouncer]
listen_addr = 0.0.0.0
listen_port = 6432

auth_type = scram-sha-256
auth_user = pgbouncer_auth
auth_query = SELECT username, password FROM pgbouncer.get_auth($1)
auth_file  = /etc/pgbouncer/userlist.txt 

; Use session pooling for long-running reports
; Use transaction pooling for short transactions (e.g. BI tools)
pool_mode = transaction

max_client_conn = 200

; Cap true DB concurrency for reporting
default_pool_size = 25
reserve_pool_size = 10
reserve_pool_timeout = 5

logfile = /var/log/postgresql/pgbouncer.log
pidfile = /var/run/postgresql/pgbouncer.pid

Max pgbouncer

[databases]
aurora_reader = host=<aurora-reader-endpoint> port=5432 dbname=<db> auth_user=pgbouncer_auth

[pgbouncer]
listen_addr = 0.0.0.0
listen_port = 6432

auth_type = scram-sha-256
auth_file = /etc/pgbouncer/userlist.txt

; Use session pooling for long-running reports
pool_mode = session

max_client_conn = 2000

; Cap true DB concurrency for reporting
default_pool_size = 25
reserve_pool_size = 10
reserve_pool_timeout = 5

; Don’t kill long queries
query_timeout = 0

; Let users queue (tune to expectations)
query_wait_timeout = 1800   ; 30 minutes

; Keep BI tool connections stable
client_idle_timeout = 3600  ; 60 minutes

; Backend hygiene
server_idle_timeout = 600
server_lifetime = 3600      ; recycle backend conns hourly
server_connect_timeout = 15
server_login_retry = 15

; Logging
log_connections = 1
log_disconnections = 1
stats_period = 60

By Rudy