Fix PostgreSQL connection exhaustion (too many clients) with gevent workers#1408
Draft
Fix PostgreSQL connection exhaustion (too many clients) with gevent workers#1408
Conversation
- Set CONN_MAX_AGE default to 0 (close DB connections after each request) to prevent connection exhaustion when using gevent workers where each greenlet holds its own persistent connection - Fix buggy CONN_MAX_AGE env var logic that used Python `or` operator, which prevented explicitly setting CONN_MAX_AGE=0 - Remove non-functional POOL_OPTIONS (no pooling library installed) - Make gunicorn settings configurable via environment variables - Update .envs/.production/.django with new configuration docs Co-authored-by: robertatakenaka <505143+robertatakenaka@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix django cursor connection issues in production
Fix PostgreSQL connection exhaustion (too many clients) with gevent workers
Mar 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
O que esse PR faz?
Corrige o erro
FATAL: sorry, too many clients alreadyque derruba o scielo.org em produção.Causa raiz: Com gevent (1000 greenlets/worker × 3 workers = até 3000 greenlets),
CONN_MAX_AGE=60mantém uma conexão persistente por greenlet, esgotando omax_connectionsdo PostgreSQL (tipicamente 100).Mudanças:
CONN_MAX_AGEdefault 60→0: Fecha conexões após cada request — correto para gevent onde cada greenlet é um "thread" com sua própria conexãoenv.int("CONN_MAX_AGE", default=0) or env.int("DJANGO_CONN_MAX_AGE", default=60)— oordo Python trata0como falsy, impossibilitando setarCONN_MAX_AGE=0explicitamente via envPOOL_OPTIONS:POOL_SIZE,MAX_OVERFLOW,RECYCLEsão opções SQLAlchemy que não fazem nada comdjango_prometheus.db.backends.postgresql— nenhuma lib de pooling está instaladaGUNICORN_WORKERS,GUNICORN_WORKER_CONNECTIONS,GUNICORN_WORKER_CLASS,GUNICORN_TIMEOUTOnde a revisão poderia começar?
config/settings/production.py— a mudança noCONN_MAX_AGEé o fix principal.Como este poderia ser testado manualmente?
CONN_MAX_AGEefetivo é0viadjango.conf.settings.DATABASES["default"]["CONN_MAX_AGE"]SELECT count(*) FROM pg_stat_activity;— deve se manter dentro domax_connectionsCONN_MAX_AGE=60deve ser respeitado (antes,CONN_MAX_AGE=0era ignorado pelo bug door)GUNICORN_WORKERS=4 GUNICORN_WORKER_CONNECTIONS=500deve alterar o comportamento do gunicornAlgum cenário de contexto que queira dar?
O
POOL_OPTIONSconfigurado dava falsa sensação de que existia connection pooling. Se pooling real for necessário, considerar PgBouncer ou instalardjango-db-connection-poole reconfigurar o engine.Screenshots
N/A — mudanças de configuração de infraestrutura.
Quais são tickets relevantes?
Referências
Original prompt
This section details on the original issue you should resolve
<issue_title>scielo.org com dificuldade de manter up</issue_title>
<issue_description>### Descrição do problema
O log do django está com algumas mensagens em produção como: