Skip to content

Latest commit

 

History

History
136 lines (89 loc) · 6.18 KB

File metadata and controls

136 lines (89 loc) · 6.18 KB
title excerpt updated
PostgreSQL - Create and use connection pools
Create and use connection pools in your Public Cloud Databases for PostgreSQL
2022-04-08

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.

Warning

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{.action} project.

Click on Databases{.action} in the left-hand navigation bar and select your PostgreSQL instance, then select the Pools{.action} tab. Click on Add a pool{.action}, 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{.thumbnail}

[!primary]

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

Connect to a connection pool

To establish a connection, get information about the pool:

Pools{.thumbnail}

Click on Information{.action}, then collect the required information.

Pool informations{.thumbnail}

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/ovhcloud. Ask questions, provide feedback and interact directly with the team that builds our databases services.

If you need training or technical assistance to implement our solutions, contact your sales representative or click on this link to get a quote and ask our Professional Services experts for a custom analysis of your project.

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