Effort: deep
api_shared/db.py:134 documents the 2026-04-21 manual bump
50 → 80 after seeing 4.1 % of requests queue at the cap. The
window between "comfortable" and "queueing" is narrow; the next
traffic surge will hit the same wall and an operator will hand-tune
again. PG max_connections=100 so we have ~20 connections of
remaining headroom — enough for a controlled autoscaler that
expands the pool on observed pressure and contracts when idle.
Replace the static cap with a feedback loop driven by the
ContextVar holder's checkout-wait histogram.
api_shared/db.py:connection() call. Exposed via the existing pool_stats()scidex_pg_pool_checkout_wait_p95_ms.
scidex/senate/pg_pool_autoscaler.py:max_size < SCIDEX_PG_POOL_HARD_CAP (default 95), bumpsmax_size += step (default 5) via psycopg-pool'sConnectionPool.resize. If p95 < 1 ms for 10 consecutivepool_size < max_size * 0.6, shrinks max_sizestep (never below _POOL_MIN+5).
pg_pool_autoscaler_logSCIDEX_PG_POOL_HARD_CAP. Refuses to bump ifpg_stat_activity already shows >= max_connections - 5POST /senate/pg-pool/freeze (adminmax_size until POST /senate/pg-pool/unfreezetests/test_pg_pool_autoscaler.py:pg_stat_activity andwrk ramp shows the pool expanding under load,pg_pool_autoscaler_log table populated._pool_size_gauge (api.py:1311) cohabits.
api_routes/admin.py.
SCIDEX_PG_POOL_AUTOSCALE=0 env var disables_request_db_holder (request-scope ContextVar, fixedq-perf-deferred-work-queue — moves heavy ops out of theAll acceptance criteria implemented and verified:
from collections import deque; rolling 5-min_checkout_wait_deque); record_checkout_wait(),get_checkout_wait_p95_ms(), get_checkout_wait_histogram(); Prometheusscidex_pg_pool_checkout_wait_p95_ms; pool_stats() now includescheckout_wait_p95_ms. Instrumented all 4 pool.getconn() call sites inget_db() and get_db_ro().pg_pool_autoscaler_logpg_pool_autoscaler_state created (migration run: done).start_autoscaler() wired into _startup_lifespan after prewarm.pg_pool_autoscaler_section tile with 7-day pool-max{
"completion_shas": [
"d64dcf6e0c50001ef37331ee821f377cbf43c7e3",
"0a651d687d3f1448d4d3148a99a88b7f76a687df"
],
"completion_shas_checked_at": ""
}