Skip to content

Conversation

@sanderegg
Copy link
Member

@sanderegg sanderegg commented Sep 5, 2025

What do these changes do?

The interface for the creation of the SqlAlchemy DB connection using asyncpg is slightly different from aiopg. Creating the engine have different arguments that have quite an influence on the connections pool:
asyncpg

  • pool_size --> this is the maximum amount of connections that are kept alive (aka hot)
  • max_overflow --> this is the maximum amount of connections over pool_size that can be created on the fly on demand and that are discarded right away (aka slow)

aiopg

  • min_size --> on start creates this amount of connections
  • max_size --> pool might go up to that amount of connections

We are migrating to asyngp currently.
Since we are setting the ENV POSTGRES_MINSIZE to pool_size that means that the number of fast connection is currently set to 2, which is very low. That might explain why the performance went down.

This PR:

  • add POSTGRES_MAX_POOLSIZE that is defaulted to 10 and represents the sqlalchemy pool max size (without overflow) - e.g. these connections are kept after initial connection to the postgres DB
  • add POSTGRES_MAX_OVERFLOW that is defaulted to 20 and represents the sqlalchemy max overflow size - e.g. these are connection created above POSTGRES_MAX_POOLSIZE and that are discarded after use (slow construction,deletion)
  • all POSTGRES environment variables are now passed in docker-compose.yml via yaml anchors
  • redefines a POSTGRES_MINSIZE of 1 and POSTGRES_MAXSIZE of 50 as basis instead of 1 and 50 (these are now only used with aiopg engine and the webserver login asyncpg-based manual connection).
  • simplify the docker-compose.yml file
  • removes the POSTGRES_ENDPOINT that is not necessary

Related issue/s

How to test

Dev-ops

@sanderegg sanderegg added this to the Cheops milestone Sep 5, 2025
@sanderegg sanderegg self-assigned this Sep 5, 2025
@sanderegg sanderegg added the a:database associated to postgres service and postgres-database package label Sep 5, 2025

This comment was marked as outdated.

@sanderegg sanderegg requested a review from odeimaiz September 5, 2025 13:18
@codecov
Copy link

codecov bot commented Sep 5, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.84%. Comparing base (a58201f) to head (1cad715).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8322      +/-   ##
==========================================
- Coverage   87.85%   87.84%   -0.02%     
==========================================
  Files        1945     1945              
  Lines       75370    75371       +1     
  Branches     1311     1311              
==========================================
- Hits        66218    66208      -10     
- Misses       8757     8768      +11     
  Partials      395      395              
Flag Coverage Δ
integrationtests 64.04% <100.00%> (-0.05%) ⬇️
unittests 86.51% <100.00%> (-0.01%) ⬇️
Components Coverage Δ
pkg_aws_library 93.59% <ø> (ø)
pkg_celery_library 87.37% <ø> (ø)
pkg_dask_task_models_library 79.62% <ø> (ø)
pkg_models_library 93.15% <ø> (ø)
pkg_notifications_library 85.20% <ø> (ø)
pkg_postgres_database 88.02% <ø> (ø)
pkg_service_integration 70.19% <ø> (ø)
pkg_service_library 71.13% <ø> (ø)
pkg_settings_library 90.19% <100.00%> (+0.01%) ⬆️
pkg_simcore_sdk 85.03% <ø> (+0.05%) ⬆️
agent 93.53% <ø> (ø)
api_server 91.91% <ø> (ø)
autoscaling 95.77% <ø> (ø)
catalog 92.34% <ø> (ø)
clusters_keeper 99.13% <ø> (ø)
dask_sidecar 92.37% <ø> (ø)
datcore_adapter 97.94% <ø> (ø)
director 75.81% <ø> (-0.09%) ⬇️
director_v2 91.00% <100.00%> (-0.03%) ⬇️
dynamic_scheduler 96.27% <ø> (ø)
dynamic_sidecar 90.46% <ø> (ø)
efs_guardian 89.62% <ø> (ø)
invitations 91.44% <ø> (ø)
payments 92.61% <ø> (ø)
resource_usage_tracker 92.18% <ø> (+0.10%) ⬆️
storage 86.36% <ø> (-0.21%) ⬇️
webclient ∅ <ø> (∅)
webserver 87.99% <100.00%> (-0.03%) ⬇️

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a58201f...1cad715. Read the comment docs.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mergify
Copy link
Contributor

mergify bot commented Sep 5, 2025

🧪 CI Insights

Here's what we observed from your CI run for 1cad715.

✅ Passed Jobs With Interesting Signals

Pipeline Job Signal Health on master Retries 🔍 CI Insights 📄 Logs
CI integration-tests Base branch is broken, but the job passed. Looks like this might be a real fix 💪 Broken 0 View View
unit-tests Base branch is healthy, but retries were needed. Could be early signs of flakiness 👀 Healthy 1 View View

Copy link
Member

@pcrespov pcrespov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx! Really appreciated!

@sanderegg sanderegg force-pushed the maintenance/fix-asyncpg-usage branch from 7a421f7 to 865ca16 Compare September 8, 2025 06:43
@sanderegg sanderegg requested a review from Copilot September 8, 2025 06:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR reconfigures the PostgreSQL database connection pool settings for the transition from aiopg to asyncpg. The main purpose is to address performance issues that arose after the migration by properly setting up the database engine with appropriate connection pool parameters.

Key changes:

  • Add new environment variables POSTGRES_MAX_POOLSIZE and POSTGRES_MAX_OVERFLOW for asyncpg-specific configuration
  • Redefine default values for POSTGRES_MINSIZE (from 2 to 1) and maintain POSTGRES_MAXSIZE at 50
  • Consolidate PostgreSQL environment variables in docker-compose.yml using YAML anchors for better maintainability

Reviewed Changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
services/docker-compose.yml Introduces x-postgres-settings YAML anchor and applies it across all services, removing duplicate environment variable definitions
packages/settings-library/src/settings_library/postgres.py Adds POSTGRES_MAX_POOLSIZE and POSTGRES_MAX_OVERFLOW fields, updates field descriptions and default values
packages/service-library/src/servicelib/db_asyncpg_utils.py Updates engine creation to use new pool settings and adds orjson serialization
services/web/server/src/simcore_service_webserver/application_settings_utils.py Maps new PostgreSQL settings to application configuration
Multiple test files Updates test configurations with new environment variables and default values

@sanderegg sanderegg force-pushed the maintenance/fix-asyncpg-usage branch from 1550aa5 to 86176b6 Compare September 8, 2025 07:28
@sanderegg sanderegg added the t:maintenance Some planned maintenance work label Sep 8, 2025
Copy link
Member

@mrnicegyu11 mrnicegyu11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good stuff, lgtm thx

@sonarqubecloud
Copy link

sonarqubecloud bot commented Sep 8, 2025

@sanderegg sanderegg merged commit ffd604e into ITISFoundation:master Sep 8, 2025
146 of 148 checks passed
@sanderegg sanderegg deleted the maintenance/fix-asyncpg-usage branch September 8, 2025 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a:database associated to postgres service and postgres-database package t:maintenance Some planned maintenance work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants