From 73a7c250b5365fc1e73a80b35d5ebcb14a34875d Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 17 Mar 2026 12:14:18 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7(backend)=20add=20DB=5FPSYCOPG=5FPO?= =?UTF-8?q?OL=5FENABLED=20settings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The psycopg pool config was enabled by default forcing its usage. Using psycopg pool can be difficult, finding the good configuration take time. By default its usage should be disable and the maintainer of the instance should decide to enable it or not. --- .github/workflows/impress.yml | 1 - CHANGELOG.md | 6 ++- docs/env.md | 1 + env.d/development/postgresql | 1 - src/backend/core/tests/test_settings.py | 36 ++++++++++++++++++ src/backend/impress/settings.py | 49 ++++++++++++++++--------- 6 files changed, 73 insertions(+), 21 deletions(-) diff --git a/.github/workflows/impress.yml b/.github/workflows/impress.yml index 4b7b9acc6..6e0d23bf0 100644 --- a/.github/workflows/impress.yml +++ b/.github/workflows/impress.yml @@ -143,7 +143,6 @@ jobs: AWS_S3_ENDPOINT_URL: http://localhost:9000 AWS_S3_ACCESS_KEY_ID: impress AWS_S3_SECRET_ACCESS_KEY: password - DB_PSYCOPG_POOL_MAX_SIZE: 15 steps: - name: Checkout repository diff --git a/CHANGELOG.md b/CHANGELOG.md index efac567b5..6ffcd73f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,12 +6,16 @@ and this project adheres to ## [Unreleased] -## [v4.8.0] - 2026-03-13 +### Added + +- 🔧(backend) add DB_PSYCOPG_POOL_ENABLED settings ### Changed - ⬇️(backend) downgrade django-treebeard to version < 5.0.0 +## [v4.8.0] - 2026-03-13 + ### Added - ✨(backend) add a is_first_connection flag to the User model #1938 diff --git a/docs/env.md b/docs/env.md index 04ee11325..cd31f46d1 100644 --- a/docs/env.md +++ b/docs/env.md @@ -46,6 +46,7 @@ These are the environment variables you can set for the `impress-backend` contai | DB_NAME | Name of the database | impress | | DB_PASSWORD | Password to authenticate with | pass | | DB_PORT | Port of the database | 5432 | +| DB_PSYCOPG_POOL_ENABLED | Enable or not the psycopg pool configuration in the default database options | False | | DB_PSYCOPG_POOL_MIN_SIZE | The psycopg min pool size | 4 | | DB_PSYCOPG_POOL_MAX_SIZE | The psycopg max pool size | None | | DB_PSYCOPG_POOL_TIMEOUT | The default maximum time in seconds that a client can wait to receive a connection from the pool | 3 | diff --git a/env.d/development/postgresql b/env.d/development/postgresql index 4d08ff970..81daebab1 100644 --- a/env.d/development/postgresql +++ b/env.d/development/postgresql @@ -9,4 +9,3 @@ DB_NAME=impress DB_USER=dinum DB_PASSWORD=pass DB_PORT=5432 -DB_PSYCOPG_POOL_MAX_SIZE=15 diff --git a/src/backend/core/tests/test_settings.py b/src/backend/core/tests/test_settings.py index 44c7c8f76..44c2a4ce1 100644 --- a/src/backend/core/tests/test_settings.py +++ b/src/backend/core/tests/test_settings.py @@ -28,3 +28,39 @@ def test_invalid_settings_oidc_email_configuration(): "Both OIDC_FALLBACK_TO_EMAIL_FOR_IDENTIFICATION and " "OIDC_ALLOW_DUPLICATE_EMAILS cannot be set to True simultaneously. " ) + + +def test_settings_psycopg_pool_not_enabled(): + """ + Test that not changing DB_PSYCOPG_POOL_ENABLED should not configure psycopg in the DATABASES + settings. + """ + + class TestSettings(Base): + """Fake test settings without enabling psycopg""" + + TestSettings.post_setup() + + assert TestSettings.DATABASES["default"].get("OPTIONS") == {} + + +def test_settings_psycopg_pool_enabled(monkeypatch): + """ + Test when DB_PSYCOPG_POOL_ENABLED is set to True, the psycopg pool options should be present + in the DATABASES OPTIONS. + """ + + monkeypatch.setenv("DB_PSYCOPG_POOL_ENABLED", "True") + + class TestSettings(Base): + """Fake test settings without enabling psycopg""" + + TestSettings.post_setup() + + assert TestSettings.DATABASES["default"].get("OPTIONS") == { + "pool": { + "min_size": 4, + "max_size": None, + "timeout": 3, + } + } diff --git a/src/backend/impress/settings.py b/src/backend/impress/settings.py index fea120404..b8ef63c91 100755 --- a/src/backend/impress/settings.py +++ b/src/backend/impress/settings.py @@ -99,24 +99,7 @@ class Base(Configuration): "localhost", environ_name="DB_HOST", environ_prefix=None ), "PORT": values.Value(5432, environ_name="DB_PORT", environ_prefix=None), - "OPTIONS": { - # https://www.psycopg.org/psycopg3/docs/api/pool.html#psycopg_pool.ConnectionPool - "pool": { - "min_size": values.IntegerValue( - 4, environ_name="DB_PSYCOPG_POOL_MIN_SIZE", environ_prefix=None - ), - "max_size": values.IntegerValue( - None, - environ_name="DB_PSYCOPG_POOL_MAX_SIZE", - environ_prefix=None, - ), - "timeout": values.IntegerValue( - 3, - environ_name="DB_PSYCOPG_POOL_TIMEOUT", - environ_prefix=None, - ), - } - }, + # Psycopg pool can be configured in the post_setup method } } DEFAULT_AUTO_FIELD = "django.db.models.AutoField" @@ -1017,6 +1000,36 @@ class Base(Configuration): "OIDC_ALLOW_DUPLICATE_EMAILS cannot be set to True simultaneously. " ) + psycopg_pool_enabled = values.BooleanValue( + False, environ_name="DB_PSYCOPG_POOL_ENABLED", environ_prefix="" + ) + + if psycopg_pool_enabled: + cls.DATABASES["default"].update( + { + "OPTIONS": { + # https://www.psycopg.org/psycopg3/docs/api/pool.html#psycopg_pool.ConnectionPool + "pool": { + "min_size": values.IntegerValue( + 4, + environ_name="DB_PSYCOPG_POOL_MIN_SIZE", + environ_prefix=None, + ), + "max_size": values.IntegerValue( + None, + environ_name="DB_PSYCOPG_POOL_MAX_SIZE", + environ_prefix=None, + ), + "timeout": values.IntegerValue( + 3, + environ_name="DB_PSYCOPG_POOL_TIMEOUT", + environ_prefix=None, + ), + } + }, + } + ) + class Build(Base): """Settings used when the application is built.