Skip to content

rakibulhossain/pgBouncerStuff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Why Connection Pooling?

PostgreSQL has a rather heavyweight connection handling architecture. For each incoming connection, the postmaster (the main Postgres daemon) forks out a new process (conventionally called a backend) to handle it. While this design provides better stability and isolation, it does not make it particularly efficient at handling short-lived connections. A new Postgres client connection involves TCP setup, process creation and backend initialization – all of which are costly in terms of time and system resources.

This of course is only a problem if connections are created too often and discarded without reuse. Unfortunately, it’s not uncommon to have a cluster of web nodes running applications written in PHP or other such languages that need to connect to the database once per page load. Batch jobs that rapidly make a bunch of connections in quick succession are also common. Employing connection pooling in such scenarios can drastically reduce the load on your PostgreSQL server and dramatically improve the query latencies.

With connection pooling, the clients connect to a proxy server which maintains a set of direct connections to the real PostgreSQL server. Typically, the clients do not (and should not) realize that they are connected to a proxy server rather than the actual server. The proxy may run on the same node as the client (example, on each web node), in which case the clients can connect to the proxy via Unix domain sockets which have very low connection overhead. Even if the proxy is on another node and the client needs a TCP connection to reach the proxy, the overhead of a new Postgres backend can be avoided.

Problem That a Proxy Solve:

plot

What is PgBouncer?

PgBouncer is an open-source, lightweight, single-binary connection pooler for PostgreSQL. It can pool connections to one or more databases (on possibly different servers) and serve clients over TCP and Unix domain sockets.

PgBouncer maintains a pool of connections for each unique user, database pair. It’s typically configured to hand out one of these connections to a new incoming client connection, and return it back in to the pool when the client disconnects. You can configure PgBouncer to pool more aggressively, so that it can pick up and return the connection to the pool at transaction or statement boundaries rather than connection boundaries. There are some potentially undesirable consequences to those, however.

Installation:

PgBouncer depends on few things to get compiled:

sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

sudo apt-get update

sudo apt-get install pgbouncer

PG-Bouncer tutorial:

https://www.pgbouncer.org/usage.html

what is Pgpool-II?

Pgpool-II is a middleware that works between PostgreSQL servers and a PostgreSQL database client.

Pgpool Wiki

  • Installation:

sudo apt-get update
sudo apt-get install pgpool2
  • Configuration:
cd /etc/pgpool2/

change the access permissions to edit configuration file or login as a user. (prefered to login as a user)

chmod 666 pgpool.conf
code pgpool.conf
code pcp.conf
code pool_hba.conf
  • Pgpool on K8s

https://www.pgpool.net/docs/42/en/html/example-kubernetes.html

https://github.com/pgpool/pgpool2_on_k8s

About

Learning stuff for PostgreSQL Connection Pooling.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published