← Back to Pull requests

[AAP-103723] perf(db): enable CONN_MAX_AGE and CONN_HEALTH_CHECKS on default database #553

Closed
unknown closed this pull request without merging on Nov 26, 2026 16:06 UTC
View on GitHub
unknown unknown commented on Nov 26, 2026 14:29 UTC
Author

Description


  • What is being changed?

  • - Sets CONN_MAX_AGE = 60 and CONN_HEALTH_CHECKS = True on DATABASES["default"] in apps/settings/defaults.py.
    - Sets DATABASES__default__CONN_MAX_AGE = 0 in apps/settings/test.py so pytest database teardown/reuse does not encounter lingering connections.
    - Leaves DATABASES["awx"] untouched (its raw psycopg3 connection and SELECT 1 probe are already managed in apps/tasks/utils.py::get_db_connection()).
    - Adds unit tests in tests/unit/test_database_connection_settings.py covering defaults, awx isolation, and test.py overrides.
  • Why is this change needed?

  • - Without CONN_MAX_AGE, Django defaults to 0, closing and reopening the PostgreSQL connection on every HTTP request. Each request pays a full TCP/TLS handshake, authentication round-trip, and PostgreSQL backend fork.
  • How does this change address the issue?

  • - Each Gunicorn worker reuses its warm PostgreSQL connection for up to 60 seconds across requests.
    - CONN_HEALTH_CHECKS = True validates reused connections before executing queries and transparently reconnects if the database or network closed the session.
    - Background paths (UnifiedTaskScheduler and run_with_lock) already call close_old_connections(), so connection lifetimes remain bounded there as well.

    Type of Change


  • Bug fix (non-breaking change which fixes an issue)

  • New feature (non-breaking change which adds functionality)

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

  • Documentation update

  • Test update

  • Refactoring (no functional changes)

  • Development environment change

  • Configuration change
  • Self-Review Checklist


  • I have performed a self-review of my code

  • I have added relevant comments to complex code sections

  • I have updated documentation where needed

  • I have considered the security impact of these changes

  • I have considered performance implications

  • I have thought about error handling and edge cases

  • I have tested the changes in my local environment
  • Testing Instructions


    Prerequisites


  • Local PostgreSQL instance running (e.g. make compose or local test DB)
  • Steps to Test


    1. Run lint and format checks:
       uv run ruff format --check apps/settings/defaults.py apps/settings/test.py tests/unit/test_database_connection_settings.py
       uv run ruff check apps/settings/defaults.py apps/settings/test.py tests/unit/test_database_connection_settings.py
       

    2. Run settings and validator unit tests:
       METRICS_SERVICE_MODE=test uv run pytest \
         tests/unit/test_database_connection_settings.py \
         tests/unit/test_dynaconf_settings.py \
         tests/unit/test_settings_post_hooks.py \
         tests/unit/test_settings_segment_key.py
       

    Expected Results


  • All 98 tests pass (CONN_MAX_AGE == 60 and CONN_HEALTH_CHECKS is True in defaults.py, awx unchanged, CONN_MAX_AGE == 0 under METRICS_SERVICE_MODE=test, and all production Dynaconf validators pass).
  • Additional Context


    Paired Benchmark Results


    Measured on a containerized reference deployment (x86_64, RHEL 9.6, Python 3.12.16, Django 5.2.19, remote PostgreSQL 15.14) using 3 alternating (baseline, candidate) pairs in separate fresh 1-worker Gunicorn processes, timing 200 sequential HTTP GET /health/ requests per run (with identical SHA-256 HTTP response digests across all 6 runs):

    | Metric | Baseline (CONN_MAX_AGE=0) | Candidate (CONN_MAX_AGE=60, CONN_HEALTH_CHECKS=True) | Delta |
    |---|---:|---:|---:|
    | Median wall time (200 reqs) | 1.358 s | 0.342 s | 3.97× faster (-74.8%) |
    | Median per-request latency | 6.62 ms | 1.68 ms | -4.94 ms/req |
    | New PostgreSQL sessions (pg_stat_database.sessions) | 200 | 0 | Eliminated per-request churn |

    Operational note: With the default GUNICORN_WORKERS=4, persistent connections hold at most 1 idle connection per active Gunicorn worker for up to 60 seconds (up to +4 connections per web container). Operators can still override via METRICS_SERVICE_DATABASES__default__CONN_MAX_AGE.

    Diff: 521 lines (+390 / -131) across 6 files Reviewer: redhat-chai-bot Review duration: 97m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 521 lines diff across 6 files
    0.48
    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%
    60%
    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.374
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #553"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    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: 47.8% (MEDIUM) · Higher = larger blast radius
    47.8%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 18.9% · Safe to merge
    18.9%