Skip to content

feat(db): configurable Postgres statement_timeout on pool connections#1200

Merged
nicoloboschi merged 3 commits into
mainfrom
feat/db-statement-timeout
Apr 22, 2026
Merged

feat(db): configurable Postgres statement_timeout on pool connections#1200
nicoloboschi merged 3 commits into
mainfrom
feat/db-statement-timeout

Conversation

@nicoloboschi

Copy link
Copy Markdown
Collaborator

Summary

  • Adds HINDSIGHT_API_DB_STATEMENT_TIMEOUT (default 600s, 0 disables), applied via the asyncpg pool init hook so every runtime connection gets a server-side safety net for runaway queries.
  • Migrations are not affected — Alembic runs through a separate SQLAlchemy/psycopg2 engine (hindsight_api/migrations.py + alembic/env.py), which bypasses the asyncpg pool.
  • Fixes the final-ANN chunk path in retain/orchestrator.py to restore the pool's configured statement_timeout in a finally block instead of RESET. RESET falls back to the Postgres server default, which would silently drop the safety net on that pooled connection after the ANN tightening to 300s.

Test plan

  • ./scripts/hooks/lint.sh passes
  • HindsightConfig.from_env() reads db_statement_timeout=600 by default and honors HINDSIGHT_API_DB_STATEMENT_TIMEOUT=0 to disable
  • Manual: start API, verify SHOW statement_timeout; on a pool connection returns 10min
  • Manual: set HINDSIGHT_API_DB_STATEMENT_TIMEOUT=0, verify pool connections report 0 (unlimited)
  • Manual: confirm Alembic migrations still run without the timeout (long DDL like CREATE INDEX CONCURRENTLY unaffected)

…ctions

Adds HINDSIGHT_API_DB_STATEMENT_TIMEOUT (default 600s, set 0 to disable).
Applied via the asyncpg pool init hook, so it only affects runtime
queries — Alembic migrations run on a separate psycopg2 engine and are
untouched.

Also fixes the ANN chunk path in the retain orchestrator to restore the
pool's configured statement_timeout rather than RESET, which would fall
back to the server default and silently drop the safety net on that
pooled connection.
…_ann

Now that the asyncpg pool applies a Postgres statement_timeout to every
connection (HINDSIGHT_API_DB_STATEMENT_TIMEOUT, default 600s), the ANN
path can be treated like any other query — no need for the 300s asyncpg
per-query timeout or the orchestrator's SET/restore dance around the
pool's default.
…hange

Re-runs scripts/generate-docs-skill.sh so the skill reference mirror
matches the HINDSIGHT_API_DB_STATEMENT_TIMEOUT row added in
hindsight-docs/docs/developer/configuration.md.

Also picks up unrelated drift (openapi.json version bump, changelog
index) that had accumulated on main.
@nicoloboschi
nicoloboschi merged commit bdb3a55 into main Apr 22, 2026
53 of 54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant