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

PDF
PostgreSQL 15 and its Major Features -(Aakash M - Mydbops) - Mydbops Opensour...
PDF
(ATS4-PLAT08) Server Pool Management
PDF
Connection Pooling in PostgreSQL using pgbouncer
PPTX
Deep Dive - Usage of on premises data gateway for hybrid integration scenarios
PPTX
What you need to know for postgresql operation
PDF
Why we love pgpool-II and why we hate it!
PDF
How to monitor NGINX
PDF
MySQL Performance for DevOps
PostgreSQL 15 and its Major Features -(Aakash M - Mydbops) - Mydbops Opensour...
(ATS4-PLAT08) Server Pool Management
Connection Pooling in PostgreSQL using pgbouncer
Deep Dive - Usage of on premises data gateway for hybrid integration scenarios
What you need to know for postgresql operation
Why we love pgpool-II and why we hate it!
How to monitor NGINX
MySQL Performance for DevOps

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

PPTX
Versioning challenges in micro services of Gwent
PPTX
SharePoint 2013 Performance Analysis - Robi Vončina
PPTX
"Spin-up pgbouncer for fun and profit", Vitaliy Kharytonskiy
PPTX
Best And Worst Practices Deploying IBM Connections
PPTX
Autoscaled Distributed Automation using AWS at Selenium London MeetUp
PDF
Postgres connections at scale
PPTX
SharePoint 2016 Platform Adoption Lessons Learned and Advanced Troubleshooting
PDF
SPSUtah 2014 SharePoint 2013 Performance (Admin)
PPTX
Autoscaled Distributed Automation Expedia Know How
PPTX
Disaster Recovery Site Implementation with MySQL
PDF
MySQL 5.6 Replication Webinar
PDF
Scaling PostgreSQL with Skytools
PPTX
Getting started with postgresql
PPT
Implementing SharePoint on Azure, Lessons Learnt from a Real World Project
PPTX
Cognos Performance Tuning Tips & Tricks
PPTX
Era of server less computing
PDF
NY Meetup: Scaling MariaDB with Maxscale
PDF
be the captain of your connections deployment
PPTX
What’s New in NGINX Plus R15?
PDF
Postgres in Amazon RDS
Versioning challenges in micro services of Gwent
SharePoint 2013 Performance Analysis - Robi Vončina
"Spin-up pgbouncer for fun and profit", Vitaliy Kharytonskiy
Best And Worst Practices Deploying IBM Connections
Autoscaled Distributed Automation using AWS at Selenium London MeetUp
Postgres connections at scale
SharePoint 2016 Platform Adoption Lessons Learned and Advanced Troubleshooting
SPSUtah 2014 SharePoint 2013 Performance (Admin)
Autoscaled Distributed Automation Expedia Know How
Disaster Recovery Site Implementation with MySQL
MySQL 5.6 Replication Webinar
Scaling PostgreSQL with Skytools
Getting started with postgresql
Implementing SharePoint on Azure, Lessons Learnt from a Real World Project
Cognos Performance Tuning Tips & Tricks
Era of server less computing
NY Meetup: Scaling MariaDB with Maxscale
be the captain of your connections deployment
What’s New in NGINX Plus R15?
Postgres in Amazon RDS
Ad

More from Mydbops (20)

PDF
Scaling TiDB for Large-Scale Application
PDF
AWS MySQL Showdown - RDS vs RDS Multi AZ vs Aurora vs Serverless - Mydbops...
PDF
Mastering Vector Search with MongoDB Atlas - Manosh Malai - Mydbops MyWebinar 39
PDF
Migration Journey To TiDB - Kabilesh PR - Mydbops MyWebinar 38
PDF
AWS Blue Green Deployment for Databases - Mydbops
PDF
What's New In MySQL 8.4 LTS Mydbops MyWebinar Edition 36
PDF
What's New in PostgreSQL 17? - Mydbops MyWebinar Edition 35
PDF
What's New in MongoDB 8.0 - Mydbops MyWebinar Edition 34
PDF
Read/Write Splitting using MySQL Router - Mydbops Meetup16
PDF
TiDB - From Data to Discovery: Exploring the Intersection of Distributed Dat...
PDF
MySQL InnoDB Storage Engine: Deep Dive - Mydbops
PDF
Demystifying Real time Analytics with TiDB
PDF
Must Know Postgres Extension for DBA and Developer during Migration
PDF
Efficient MySQL Indexing and what's new in MySQL Explain
PDF
Scale your database traffic with Read & Write split using MySQL Router
PDF
PostgreSQL Schema Changes with pg-osc - Mydbops @ PGConf India 2024
PDF
Choosing the Right Database: Exploring MySQL Alternatives for Modern Applicat...
PDF
Mastering Aurora PostgreSQL Clusters for Disaster Recovery
PDF
Navigating Transactions: ACID Complexity in Modern Databases- Mydbops Open So...
PDF
AWS RDS in MySQL 2023 Vinoth Kanna @ Mydbops OpenSource Database Meetup 15
Scaling TiDB for Large-Scale Application
AWS MySQL Showdown - RDS vs RDS Multi AZ vs Aurora vs Serverless - Mydbops...
Mastering Vector Search with MongoDB Atlas - Manosh Malai - Mydbops MyWebinar 39
Migration Journey To TiDB - Kabilesh PR - Mydbops MyWebinar 38
AWS Blue Green Deployment for Databases - Mydbops
What's New In MySQL 8.4 LTS Mydbops MyWebinar Edition 36
What's New in PostgreSQL 17? - Mydbops MyWebinar Edition 35
What's New in MongoDB 8.0 - Mydbops MyWebinar Edition 34
Read/Write Splitting using MySQL Router - Mydbops Meetup16
TiDB - From Data to Discovery: Exploring the Intersection of Distributed Dat...
MySQL InnoDB Storage Engine: Deep Dive - Mydbops
Demystifying Real time Analytics with TiDB
Must Know Postgres Extension for DBA and Developer during Migration
Efficient MySQL Indexing and what's new in MySQL Explain
Scale your database traffic with Read & Write split using MySQL Router
PostgreSQL Schema Changes with pg-osc - Mydbops @ PGConf India 2024
Choosing the Right Database: Exploring MySQL Alternatives for Modern Applicat...
Mastering Aurora PostgreSQL Clusters for Disaster Recovery
Navigating Transactions: ACID Complexity in Modern Databases- Mydbops Open So...
AWS RDS in MySQL 2023 Vinoth Kanna @ Mydbops OpenSource Database Meetup 15
Ad

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
KodekX | Application Modernization Development
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Cloud computing and distributed systems.
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PDF
Modernizing your data center with Dell and AMD
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
The Rise and Fall of 3GPP – Time for a Sabbatical?
20250228 LYD VKU AI Blended-Learning.pptx
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
KodekX | Application Modernization Development
Understanding_Digital_Forensics_Presentation.pptx
cuic standard and advanced reporting.pdf
MYSQL Presentation for SQL database connectivity
GamePlan Trading System Review: Professional Trader's Honest Take
Diabetes mellitus diagnosis method based random forest with bat algorithm
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Spectral efficient network and resource selection model in 5G networks
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Cloud computing and distributed systems.
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Modernizing your data center with Dell and AMD
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....

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