PostgreSQL - Create and use connection pools
Create and use connection pools in your Public Cloud Databases for PostgreSQL
Create and use connection pools in your Public Cloud Databases for PostgreSQL
Last Updated 8th April 2022
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.
Connection pooling allows you to maintain very large numbers of connections to a database while minimizing the consumption of server resources.
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.
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.
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:
You can edit, delete and access information about the pool by clicking on ...
to the right of the pool.
To establish a connection, get information about the pool:
Click on Information
, then collect the required information.
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.
Visit our dedicated Discord channel: https://discord.gg/ovhcloud. 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/.
Si lo desea, también puede enviarnos sus sugerencias para ayudarnos a mejorar nuestra documentación.
Imágenes, contenido, estructura...: ayúdenos a mejorar nuestra documentación con sus sugerencias.
No podemos tratar sus solicitudes de asistencia a través de este formulario. Para ello, haga clic en "Crear un tíquet" .
¡Gracias! Tendremos en cuenta su opinión.
¡Acceda al espacio de la OVHcloud Community! Resuelva sus dudas, busque información, publique contenido e interactúe con otros miembros de la comunidad.
Discuss with the OVHcloud community