SlideShare a Scribd company logo
Scaling Connections in PostgreSQL
Postgres Bangalore(PGBLR) Meetup-2, 29th June 2024
- Aakash M
About Me
• PostgreSQL Tech Lead at Mydbops
• Expertise in databases like MySQL, SQL Server
• Performance tuning / Cost Optimization
• Tech Speaker on Open Source Events
• Active Blogger
• 6+ years of Experience
About Us
Our Services
Focus on MySQL, MongoDB, PostgreSQL, TiDB,
Cassandra
Consulting
Services
Consulting
Services
Managed
Support
Services
24*7
DBA Team
Targeted
Engagement
Agenda
• Connection Pooling
• Why pooling is required ?
• Available Connection Poolers
• pgbouncer
• Benchmark Comparison
Connection Pooling
Application
What is Connection Pooling
• A pool of database connections
• Reusing unused connections from the pool
• Once purpose completes, return back to the pool
Connections to the database
Application
Connection
Pooler
What is Connection Pooling
• A pool of database connections
• Reusing unused connections from the pool
• Once purpose completes, return back to the pool
Connections to the Pool Connections to the database
Advantages of Connection Pooling
• Limiting connections to the database
• Overhead of opening/closing connection is reduced
Why pooling is required ?
Why pooling is required ?
• Process based connection
Statistics Collector
WAL Writer
Background Writer
Checkpointer
Autovacuum
Archiver
Shared Memory
Postgres(Server
Process)
Log Writer
Why pooling is required ?
Application
Postgres
Architecture
PostgreSQL Server
Statistics Collector
WAL Writer
Background Writer
Checkpointer
Autovacuum
Archiver
Shared Memory
Postgres(Server
Process)
Log Writer
Why pooling is required ?
Application
Postgres(Backend
Process)
Postgres(Backend
Process)
PostgreSQL Server
Disadvantages
• More Resources (CPU & Memory)
• Slower Context Switching
Impact of Idle Connections
Reference: https://aws.amazon.com/blogs/database/resources-consumed-by-idle-postgresql-connections/
MySQL is handling More Connections ??
Receiver
Thread
Connection Creation in MySQL
Application
MySQL
Server
mysqld process
Connection Requests
Thread Cache
User Threads THDS
Connection Creation in MySQL
• Resource Efficiency (CPU & Memory)
• Faster Context Switching
• Better Scalability
Available Connection Poolers
Connection Pooler Available Since Setup Complexity Github Repository
pgbouncer 2007 Simple
https://github.com/pgbo
uncer/pgbouncer
Pgpool-II 2003 Medium to High
https://github.com/pgpo
ol/pgpool2
PgCat 2020 Medium
https://github.com/postg
resml/pgcat
Odyssey 2017 Medium to High
https://github.com/yand
ex/odyssey
pgagroal 2020 Low to Medium
https://github.com/agroa
l/pgagroal
Available Connection Poolers
pgbouncer
Pgbouncer
• Lightweight Process
• Easy to setup
• Single Threaded
• Same authentication as database
Pgbouncer
Configuration
Pooling Mode
• Session
• Transaction
• Statement
Session Pooling Mode
• Once the client disconnections, Connection will be
released back to the pool
• Client and Server Connection is paired
Application
Connection
Pooler
Session Pooling Mode
• Once the client disconnections, Connection will be
released back to the pool
• Client and Server Connection is paired
Connection 1 Connection 1
Connection 2
Connection 2
Tranaction Pooling Mode
• Server Connection is assigned to Client whenever a new
transaction is started
• Released back to the pool when transaction closes
• Server and client connection is not paired
• No support for session variables and prepared statements
Tranaction Pooling Mode
Application
Connection
Pooler
User1
Trx1:
Begin;
statement
;
commit or
rollback
User2
Trx2:
Begin;
statement
;
Statement Pooling Mode
• Server connection returns to pool immediately after
statement completes
• Multi statement is not supported
• Autocommit is enabled
Statement Pooling Mode
Application
Connection
Pooler
User1
drop table x;
drop table y;
User2
select 1;
select 2;
select 3;
select 1;
select 2;
Authentication Modes
• Authentication with Auth_file
• Authentication with Auth_user
Auth File
• Text file contains username and password
• Location of Auth_file requires
Auth File
• auth_type = md5 or password
• auth_file = /etc/pgbouncer/userlist.txt
cat /etc/pgbouncer/userlist.txt
"appuser" "md5e10adc3949ba59abbe56e057f20f883e" # Password:
123456
"reporting_user" "md5e10adc543ba59abbe56e057f20f8899"
"admin" "md5e10000543ba59abbe56e057f20f8877"
Auth User
• Load user/password directly from the server
• No maintenance overhead
Auth User
• auth_user=”low_priv_user”
• auth_query=”select usename, passwd FROM pg_shadow
WHERE usename=$1”
• auth_file = /etc/pgbouncer/userlist.txt
cat /etc/pgbouncer/userlist.txt
"low_priv_user" "md5e10adc3949ba59abbe56e057f20f883e"
Parameter Description Default
Max_client_conn
Maximum number of client
connections allowed
100
Default_Pool_Size
How many server
connections to allow
20
min_pool_size
Minimum number of server
connections to maintain
0(Disabled)
Configuration
Parameter Description Default
Reserve_pool_size
Additional connections
allowed to the pool
0(Disabled)
Reserve_pool_timeout
If timeout, use additional
connections in the pool
0(Disabled)
Listen_port Port to listen 6432
Configuration
Working of Pgbouncer
Working of pgbouncer
• Max_client_conn=10
• Default_pool_size=3
• Reserve_pool_size=1
• Reserve_pool_timeout=10
• Listen_port=6542
• Pooling_mode=Session
Working of pgbouncer
Application
pgbouncer
Working of pgbouncer
Application
pgbouncer
Pool
Area
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
2 connections
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
2 connections 2 connections
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
2 connections 2 connections
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
2 connections 2 connections
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections 3
connections
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections 3
connections
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections 2 connections
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections 2 connections
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections 3
connections
Application
pgbouncer
Working of pgbouncer
Pool
Area
Reserve
Pool
Area
Waiting Area
4 connections 4 connections
Monitoring Pgbouncer
CLI
• Admin Console
• psql -h127.0.0.1 -Upostgres pgbouncer
root@ip-172-31-3-53:~/# psql -h172.31.3.53 -Umydbops pgbouncer -p6432
psql (16.3 (Ubuntu 16.3-1.pgdg20.04+1), server 1.12.0/bouncer)
pgbouncer=#
CLI
pgbouncer=# show help;
• NOTICE: Console usage
• DETAIL:
• SHOW HELP|CONFIG|DATABASES|POOLS|CLIENTS|SERVERS|USERS|VERSION
• SHOW FDS|SOCKETS|ACTIVE_SOCKETS|LISTS|MEM
• SHOW DNS_HOSTS|DNS_ZONES
• SHOW STATS|STATS_TOTALS|STATS_AVERAGES|TOTALS
• SET key = arg
• RELOAD
• PAUSE [<db>]
• RESUME [<db>]
• DISABLE <db>
• ENABLE <db>
• RECONNECT [<db>]
• KILL <db>
• SUSPEND
• SHUTDOWN
CLI
pgbouncer=# show pools;
-[ RECORD 1 ]-----------
• database | test
• user | mydbops
• cl_active | 50
• cl_waiting | 50
• sv_active | 50
• sv_idle | 0
• sv_used | 0
• sv_tested | 0
• sv_login | 0
• maxwait | 0
• maxwait_us | 19535
• pool_mode | transaction
Grafana
• Open Source Exporters and Dashboards
https://github.com/spreaker/prometheus-pgbouncer-exporter
https://blog.searce.com/grafana-dashboard-for-pgbouncer-and-monitor-with-percona-pmm-
3170d3eb4d14
Grafana
Benchmarking Results
Benchmark
• 50 pool size
• Hosted on Application Server
• Small Instance Size
• Pgbench for benchmarking
• 1, 50, 75, 100, 200, 400, 500 connections
Benchmark
Benchmark
Benchmark
• Direct connection performs good when number of connection is less
• Only for OLTP workload
• Maintain resource usage
Considerations
Few Considerations
• Single Point of Failure
• Single Threaded
• Multiple Instance for More Connections
• OS should be optimised
Production Use Case
Production Use Case
• High CPU usage for last 1 week
• AWS RDS db.r5.2xlarge (8 CPU, 64G RAM)
• No new deployment
Production Use Case
Production Use Case
• No CPU causing queries
• Increased connections
• Killing Idle Connections
Production Use Case
Production Use Case
Production Use Case
Application
Application
Production Use Case
Production Use Case
Pgpool - II
pgpool-II
• multi threaded Architecture
• Connection Pooling
• Read Write Routing (Load Balancing)
• Query Caching
• In-Built HA Architecture
General Considerations
• Complexity
• Higher Resource Usage
• Latency
Pgcat
pgcat
• multi threaded Architecture
• Simple to setup
• Sharding
• Read Write Routing (Load Balancing)
• Query Caching
• In-Built HA Architecture
pgcat vs pgbouncer
Ref: https://tembo.io/blog/postgres-connection-
poolers
pgcat vs pgbouncer
Ref: https://tembo.io/blog/postgres-connection-
poolers
Ref: https://tembo.io/blog/postgres-connection-
poolers
Summary
• pgbouncer works well for simpler setups
• pgpool/pgcat has features and complex settings
• Ensure to test load balancing
• OS tuning is mandatory
• Do not forget HA
• Good for OLTP workload
Reference:
• https://tembo.io/blog/postgres-connection-poolers
• https://www.mydbops.com/blog/getting-started-with-pgbouncer-in-postgresql/
• https://blog.searce.com/grafana-dashboard-for-pgbouncer-and-monitor-with-
percona-pmm-3170d3eb4d14
• https://postgresml.org/docs/product/pgcat/features
• https://www.pgpool.net/mediawiki/index.php/Main_Page
Thank You

More Related Content

Scaling Connections in PostgreSQL Postgres Bangalore(PGBLR) Meetup-2 - Mydbops