perf(api): AIN-222 C1 · runtime SQLAlchemy pool sized + pooler URL split#60
Merged
Merged
Conversation
Adds DATABASE_POOLER_URL setting. When set, the runtime engine uses the Supabase transaction-mode pooler (:6543); when unset, falls back to database_url (:5432). Alembic always uses database_url since DDL needs session-level state the pooler does not preserve — alembic/env.py already pulls from settings.database_url and uses NullPool, so no change there. Runtime pool sized for warm reuse: pool_size = 10 max_overflow = 20 pool_recycle = 300s pool_pre_ping = True (kept) asyncpg + Supabase transaction-mode pooler requires statement_cache_size=0 — prepared statements break when the pooler hands each transaction a fresh session. Applied only when the pooler URL is set (no-op against the direct port). Single-call TTFB on the live api today: 0.47–1.07s — gate is <150ms. This change is necessary but not sufficient: setting DATABASE_POOLER_URL in Railway production env is the second half. See deliverables for the env var to set. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
AIN-222 [perf] Dashboard slow — DB connection-per-request + sequential waterfall (not the query)
Owner: Aule (exec) · Counsel/audit: CC · Measured 2026-05-22. The DB is not the bottleneck — the connection path is. Evidence (measured live, not theorized)
A 0.13ms query that takes 620ms over the wire = new DB connection per request (TCP+TLS+Postgres-auth handshake every call) and/or cross-region Railway↔Supabase(us-east-1). The dashboard then fires agents+models+audit+billing sequentially → ~2–3s stacked waterfall. That's the "slow dashboard." Fixes — ranked by win-per-effortP0 — Persistent DB connection pool in the api
P0 — Collapse the dashboard waterfall
P1 — Confirm Railway region == Supabase
|
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.
Summary
DATABASE_POOLER_URLsetting. When set, the runtime engine uses the Supabase transaction-mode pooler (:6543); when unset, falls back todatabase_url(:5432). Alembic always usesdatabase_url.pool_size=10, max_overflow=20, pool_recycle=300, pool_pre_ping=True).statement_cache_size=0on asyncpg when the pooler is in use — required for transaction-mode multiplexing.Why
Live api single-call TTFB measured 0.47–1.07s today; the AIN-222 gate is <150ms. Cold connection setup dominates that spend, and
pool_size=5default + no pre-warming means most requests pay it. This change is necessary but not sufficient: settingDATABASE_POOLER_URLin Railway prod env is the second half (Discipline #6 — env var change is a founder action; see deliverables for the URL shape to set).Safety
DATABASE_POOLER_URLset, the engine uses the existing direct URL — no behavior change vs today's deploy.alembic/env.pyreadsdatabase_urlexplicitly and usesNullPool— already correct.Test plan
alembic upgrade head+ seed) all green.DATABASE_POOLER_URLin Railway env./v1/*TTFB drops below 150ms on prod after Railway redeploys.🤖 Generated with Claude Code
Note
Medium Risk
Medium risk because it changes database connection behavior (URL selection, pool sizing, and asyncpg connect args), which can impact runtime stability and performance if misconfigured or if the pooler behaves differently under load.
Overview
Adds an optional
database_pooler_urlsetting so the runtime SQLAlchemy engine can connect via a Supabase transaction-mode pooler when configured, while keeping migrations on the directdatabase_url.Updates DB engine construction to tune pooling (
pool_size,max_overflow,pool_recycle,pool_pre_ping) and conditionally disables asyncpg prepared-statement caching (statement_cache_size=0) when using the pooler.Reviewed by Cursor Bugbot for commit 2897bc7. Bugbot is set up for automated code reviews on this repo. Configure here.