← Back to Pull requests

fix(db): PostgREST para de perder o LISTEN a cada 10 min (idle_session_timeout do authenticator) #3569

Closed
unknown closed this pull request without merging on Nov 14, 2026 04:16 UTC
View on GitHub
unknown unknown commented on Nov 14, 2026 03:57 UTC
Author

📋 Descrição

Causa raiz das quedas periódicas da Data API. O site fica lento ou fora a cada ~10 min, e o E2E e as edges de todas as PRs falham pelo mesmo motivo.

A migration 20260524211024 (colapso fase 2) definiu ALTER ROLE authenticator SET idle_session_timeout = '600000'. A conexão LISTEN do PostgREST no canal pgrst fica ociosa por natureza, então o banco a derruba a cada 10 min. Toda vez que reconecta, o PostgREST recarrega o schema cache inteiro (588 relações, 1.074 funções). Numa instância Small isso leva de 12 a 72 s, e nesse intervalo a API responde 503 e o pool estoura.

Logs do PostgREST (analytics/endpoints/logs, source = 'postgrest_logs'):

03:23:07 FATAL: terminating connection due to idle-session timeout
03:23:07 Failed listening for database notifications on the "pgrst" channel. SSL connection has been closed unexpectedly
03:25:54 Schema cache queried in 47791.6 milliseconds
03:33:26 FATAL: terminating connection due to idle-session timeout
03:33:59 Schema cache queried in 23312.7 milliseconds
03:43:34 FATAL: terminating connection due to idle-session timeout
03:45:40 {"code":"PGRST002", ... "Could not query the database for the schema cache. Retrying."}  (dezenas)
03:46:55 Schema cache queried in 72852.4 milliseconds
03:53:59 {"code":"PGRST003", ... "Timed out acquiring connection from connection pool."}
03:54:44 FATAL: terminating connection due to idle-session timeout
03:55:12 Schema cache queried in 12821.5 milliseconds

Consulta read-only em pg_db_role_setting:

  • authenticator: idle_session_timeout=600000

  • postgres (DATABASE): idle_session_timeout=600000
  • A sonda de saúde registrou rest=UNHEALTHY em 03:11, 03:15, 03:19–03:27 e 03:40–03:51Z.

    Correção (1 linha): ALTER ROLE authenticator SET idle_session_timeout = 0.

  • O = 0 precisa ser explícito: um RESET cairia no nível DATABASE, que também está em 600000.

  • O pool do PostgREST 14.5 já fecha conexões ociosas sozinho (GET /v1/projects/…/postgrest: db_pool_max_idletime=30, db_pool_max_lifetime=1800). Por isso as conexões zumbi do colapso de 2026-05-24 não voltam; só a conexão LISTEN fica viva, como deve ser.

  • service_role, anon, authenticated e o nível DATABASE não mudam.

  • Rollback: volta para 600000.
  • 🎯 Tipo de mudança

  • 🐛 fix — correção de bug

  • 🚨 hotfix — correção urgente em produção
  • 🌐 Sistemas afetados

  • Supabase (DB — configuração de papel)
  • ✅ Autorização

    DDL em produção. O dono deu autorização ampla no chat desta sessão ("Eu aprovo todas! Acelere o CI!" e "Continue até completar o goal com as autorizações que eu lhe concedi"). A aplicação só acontece pelo db-apply-migration.yml depois do merge.

    🧪 Como testar

    node scripts/preflight-migration-apply.mjs --version=20261114040000   → passed (ledger ao vivo, ainda não aplicada)
    GITHUB_BASE_REF=main node scripts/check-migrations-sync-log-gate.mjs   → passed
    node scripts/check-migration-filename-contract.mjs / check-migration-path-references.mjs → ok
    

    Depois de aplicar:

  • select setconfig from pg_db_role_setting ... where rolname = 'authenticator' deve trazer idle_session_timeout=0.

  • Os logs do PostgREST não devem mais trazer idle-session timeout a cada 10 min.

  • A sonda rest deve ficar estável.
  • 🤖 Generated with Claude Code

    https://claude.ai/code/session_01VXkRxXLTPrNUgWhbASkaJg
    <!-- devin-review-badge-begin -->

    ---

    <a href="https://app.devin.ai/review/adm01-debug/promo_gifts_v4/pull/3569" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a>
    <!-- devin-review-badge-end -->

    Diff: 475 lines (+356 / -119) across 5 files Reviewer: adm01-debug Review duration: 18m
    All Vouch review confidence checks passed
    Residual risk 0.32 = Change Risk (0.69) × (1 − Review Confidence (0.54)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.69 · 475 lines diff across 5 files
    0.69
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    54%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.369
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.069
    Real PR Conversation Timeline
    1 comments
    "Review active on #3569"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 54.2% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 68.9% (HIGH) · Higher = larger blast radius
    68.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 54.2% · Lower = rushed or shallow review
    54.2%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 31.5% · Safe to merge
    31.5%