From e009ed9fe41ad84324677c3db74c8aadf16e5740 Mon Sep 17 00:00:00 2001 From: Zach H Date: Mon, 22 Jul 2024 01:29:20 -0400 Subject: [PATCH] fix: always use pgxpool config if MaxOpenConns isn't set (#8328) # Which Problems Are Solved - `pgxpool -> pgx` dependency throws "MaxSize must be >= 1" on init if `postgres.MaxOpenConns` isn't set in the ZItadel config # How the Problems Are Solved Only override the `MaxConns` with the Zitadel configured `MaxOpenConns` if greater than 0 (default value). The default `MaxConns` [is derived by `pgxpool`](https://github.com/jackc/pgx/blob/ea9610f6722e0c6c6073660f14ded3c9683a593e/pgxpool/pool.go#L309-L324) itself in a sensible way, but somewhat undocumented: checks for explicit config in connection url or config and falls back on max(num_cpus, 4). # Additional Changes Applied same check in cockroach config # Additional Context This is likely a regression from the changes in https://github.com/zitadel/zitadel/pull/8325 --- internal/database/cockroach/crdb.go | 6 +++++- internal/database/postgres/pg.go | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/database/cockroach/crdb.go b/internal/database/cockroach/crdb.go index 2aeb295390..2f685f6d92 100644 --- a/internal/database/cockroach/crdb.go +++ b/internal/database/cockroach/crdb.go @@ -81,7 +81,11 @@ func (c *Config) Connect(useAdmin bool, pusherRatio, spoolerRatio float64, purpo if err != nil { return nil, err } - config.MaxConns = int32(connConfig.MaxOpenConns) + + if connConfig.MaxOpenConns != 0 { + config.MaxConns = int32(connConfig.MaxOpenConns) + } + config.MaxConnLifetime = c.MaxConnLifetime config.MaxConnIdleTime = c.MaxConnIdleTime diff --git a/internal/database/postgres/pg.go b/internal/database/postgres/pg.go index 0870d6243c..ecafbe877a 100644 --- a/internal/database/postgres/pg.go +++ b/internal/database/postgres/pg.go @@ -82,7 +82,11 @@ func (c *Config) Connect(useAdmin bool, pusherRatio, spoolerRatio float64, purpo if err != nil { return nil, err } - config.MaxConns = int32(connConfig.MaxOpenConns) + + if connConfig.MaxOpenConns != 0 { + config.MaxConns = int32(connConfig.MaxOpenConns) + } + config.MaxConnLifetime = c.MaxConnLifetime config.MaxConnIdleTime = c.MaxConnIdleTime