From bfe9224f44a837b9e3304409d4961dadeb4a39d7 Mon Sep 17 00:00:00 2001
From: Dmitrii Dolgov <9erthalion6@gmail.com>
Date: Wed, 9 Jun 2021 10:53:40 +0200
Subject: [PATCH] Adjust per pool by number of pools

default_pool_size, min_pool_size, reserve_pool_size are per pool values,
that's it per (database, user) pair. Which means it's wrong to calculate
them based on the global maxDBConn, otherwise for the cases when there
are too many users it would result in the configuration conflicting
between maxDBConn and min_pool_size. Adjust those values by the number
of known pools.

This not necessarily a perfect solution, because load could be
distributed between databases & users unevenly in which case some of
those parameters will cause rarely used pools being of the same size as
the active ones.
---
 pkg/cluster/connection_pooler.go | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/pkg/cluster/connection_pooler.go b/pkg/cluster/connection_pooler.go
index f579b446..97cbc16c 100644
--- a/pkg/cluster/connection_pooler.go
+++ b/pkg/cluster/connection_pooler.go
@@ -162,7 +162,11 @@ func (c *Cluster) getConnectionPoolerEnvVars() []v1.EnvVar {
 
 	maxDBConn := *effectiveMaxDBConn / *numberOfInstances
 
-	defaultSize := maxDBConn / 2
+	// Following pooler parameters are per pool, which means they have to be
+	// calculated from the global max db connections adjusted by the number of
+	// pairs (database, user).
+	numberOfPools := int32(len(spec.Users) * len(spec.Databases))
+	defaultSize := (maxDBConn / numberOfPools) / 2
 	minSize := defaultSize / 2
 	reserveSize := minSize
 
-- 
GitLab