Home > OS >  Npgsql Minimum Pool Size seems to be not take into account
Npgsql Minimum Pool Size seems to be not take into account

Time:12-07

In my .Net application (that runs on multiple pods) I'm using following connection string:

"User ID=;Password=;Host=;Port=;Database=;Pooling=true;Username=;ApplicationName=;Max Auto Prepare=200;Minimum Pool Size=90;Maximum Pool Size=100;Read Buffer Size=18000;Timeout=1;Command Timeout=5;"

(I know that Minimum Pool Size=90 is little too high, but it's just to tackle the issue)

Having the above I am expecting that each pod will have between 90 and 100 active connections to the DB server. However, when I execute following query:

select client_addr, count(*) used FROM pg_stat_activity where datname = 'my_app_name' group by client_addr;

results shows that each pod has max around ~30 connections.

connections per client_addr count

This is how I use connections and execute queries in my C# code:

public async Task<IEnumerable<T>> GetGeneric<T>(string query, DynamicParameters parameters)
{
    await using var connection = DbConnectionHelper.Create(myConnectionString);
    return await connection.QueryAsync<T>(query, parameters);
}
DbConnectionHelper:
public static class DbConnectionHelper
{
    public static NpgsqlConnection Create(string connectionString)
    {
        var connection = new NpgsqlConnection(connectionString);
        return connection;
    }
}

From this documentation I can read:

When a pool is created, multiple connection objects are created and added to the pool so that the minimum pool size requirement is satisfied.

Meaning 90 connections per each pod should be added on app start. Shouldn't it?

This rises following questions:

  1. Why Minimum Pool Size is not used?
  2. Why I don't see numbers between 90 and 100 when executing select client_addr, count(*) used FROM pg_stat_activity ?

CodePudding user response:

The documentation you are referring to is not the one for npgsql and doesn't apply.

While the doc for npgsql isn't very verbose on the Minimum Pool Size, we can look at the code and find that

  1. there is no reference to opening several connections at startup
  2. connections are closed only if there are more than Minimum Pool Size

So the answer is that your clients haven't needed the 90 concurrent connections so they are not open yet.

  • Related