PostgreSQL - Create and use connection pools

Create and use connection pools in your Public Cloud Databases for PostgreSQL

Last Updated 8th April 2022

Objective

Public Cloud Databases allow you to focus on building and deploying cloud applications while OVHcloud takes care of the database infrastructure and maintenance in operational conditions.

This guide explains how to create and use connection pools.

Requirements

Concept

Connection pooling allows you to maintain very large numbers of connections to a database while minimizing the consumption of server resources.

Why connection pooling?

Eventually a high number of backend connections becomes a problem with PostgreSQL as the resource cost per connection is quite high due to the way PostgreSQL manages client connections. PostgreSQL creates a separate backend process for each connection and the unnecessary memory usage caused by the processes will start hurting the total throughput of the system at some point. Also, if each connection is very active, the performance can be affected by the high number of parallel executing tasks.

It makes sense to have enough connections so that each CPU core on the server has something to do (each connection can only utilize a single CPU core), but a hundred connections per CPU core may be too much. All this is workload specific, but often a good number of connections to have is in the ballpark of 3-5 times the CPU core count.

Connection pooling modes

  • Session pooling: A server connection is assigned to the client application for the life of the client connection. PgBouncer releases the server connection back into the pool once the client application disconnects.

  • Transaction pooling: A server connection is assigned to the client application for the duration of a transaction. When PgBouncer detects the completion of the transaction, it releases the server connection back into the pool.

Several PostgreSQL features, described in the official PgBouncer features page, are known to be broken by the default transaction-based pooling and must not be used by the application when in this mode. You must carefully consider the design of the client applications connecting to PgBouncer, otherwise the application may not work as expected.

  • Statement pooling: A server connection is assigned to the client application for each statement. When the statement completes, the server connection is returned into the pool. Multi-statement transactions are not permitted for this mode.

Instructions

Create a connection pool

To create a new connection pool, log in to your OVHcloud Control Panel and open your Public Cloud project.

Click on Databases in the left-hand navigation bar and select your PostgreSQL instance, then select the Pools tab. Click on Add a pool, and fill the form.

The settings available are:

  • Pool name: Enter a name for your connection pool here.This will also become the "database" or "dbname" connection parameter for your pooled client connections.
  • Database: Choose the database that you want to connect to. Each pool can only connect to a single database.
  • Pool Mode: Select the pooling mode.
  • Pool Size: Select how many PostgreSQL server connections this pool can use at a time.
  • Username: Select the database username that you want to use when connecting to the backend database.

Add a pool

You can edit, delete and access information about the pool by clicking on ... to the right of the pool.

Connect to a connection pool

To establish a connection, get information about the pool:

Pools

Click on Information, then collect the required information.

Pool informations

Checking

We can use the psql command-line client to verify that the pooling works as supposed:

From terminal 1:

$ psql "postgres://avnadmin:xxxxxxxxxxxxxxxxxxxxx@postgresql-b412100d-o2626ab53.database.cloud.ovh.net:20185/pgpool?sslmode=require"

From terminal 2:

$ psql "postgres://avnadmin:xxxxxxxxxxxxxxxxxxxxx@postgresql-b412100d-o2626ab53.database.cloud.ovh.net:20185/pgpool?sslmode=require"

We have two open client connections to the pool. Let's verify that each connection is able to access the database:

Terminal 1:

pgpool=> SELECT 1;
?column?
-------
        1
(1 row)

Terminal 2:

pgpool=> SELECT 1;
?column?
-------
        1
(1 row)

Both connections respond as they should. Now let's check how many connections there are to the PostgreSQL backend database:

Terminal 1:

pgpool=> SELECT COUNT(*) FROM pg_stat_activity WHERE usename = 'avnadmin';
 count
-------
     1
(1 row)

pgstatactivity outputs the two psql sessions, which uses the same PostgreSQL server database connection.

Go further

Visit our dedicated Discord channel: https://discord.gg/PwPqWUpN8G. Ask questions, provide feedback and interact directly with the team that builds our databases services.

Join our community of users on https://community.ovh.com/en/.


Esta documentação foi-lhe útil?

Não hesite em propor-nos sugestões de melhoria para fazer evoluir este manual.

Imagens, conteúdo, estrutura... Não hesite em dizer-nos porquê para evoluirmos em conjunto!

Os seus pedidos de assistência não serão tratados através deste formulário. Para isso, utilize o formulário "Criar um ticket" .

Obrigado. A sua mensagem foi recebida com sucesso.


Estes manuais também podem ser úteis...

OVHcloud Community

Aceda ao seu espaço comunitário. Coloque as suas questões, procure informações e interaja com outros membros do OVHcloud Community.

Discuss with the OVHcloud community

Em conformidade com a alteração à Diretiva 2006/112/CE, os preços com IVA podem variar de acordo com o país de residência do cliente
(por defeito, os preços com IVA apresentados incluem o IVA português em vigor).