Skip to content

P0: Configure Postgres pool limits + statement_timeout (#165)#182

Open
dkijania wants to merge 1 commit into
mainfrom
feat/pg-pool-timeouts
Open

P0: Configure Postgres pool limits + statement_timeout (#165)#182
dkijania wants to merge 1 commit into
mainfrom
feat/pg-pool-timeouts

Conversation

@dkijania

Copy link
Copy Markdown
Contributor

What & why

Part of the production-readiness epic (#163). Closes #165.

The archive-node Postgres client was created with postgres(connectionString) and no pool sizing or timeouts. Once the API is publicly reachable this is a DoS risk — a single expensive query can hold a connection open indefinitely, exhaust the pool, and cascade into an outage.

Changes

  • New src/db/archive-node-adapter/postgres-options.ts — builds the postgres() options from conservative, env-tunable defaults, isolated so it's unit-testable without a DB.
  • Adapter now calls postgres(connectionString, buildPostgresOptions()).
Env var Default Meaning
PG_MAX_CONNECTIONS 10 Max pooled connections per host
PG_IDLE_TIMEOUT 30 Seconds before an idle connection is closed
PG_CONNECT_TIMEOUT 30 Seconds to wait for a connection before failing
PG_STATEMENT_TIMEOUT 30000 Server-side query cap (ms); longer queries are cancelled by Postgres. 0 disables

Malformed values fall back to defaults rather than throwing, so a stray typo can never silently disable a safety limit (e.g. max → 0). statement_timeout is sent as a startup connection parameter, so it applies to every query on every connection.

Docs (getting-started.md), .env.example.compose, and envionment.d.ts updated; new unit tests cover parsing, fallbacks, clamping, and the options shape.

Testing

  • npm run build — clean
  • npm run test:unit — all pass (6 new assertions in postgres-options.test.ts)
  • npm run lint — clean
  • npx prettier --debug-check . — exit 0

Tests construct their own postgres clients directly, so the new adapter defaults don't affect the integration/live-network suites.

🤖 Generated with Claude Code

@dkijania dkijania added production-readiness Work toward making the API production-ready / publicly available P0 Blocker for public availability labels Jun 28, 2026
The archive-node Postgres client was created with `postgres(connectionString)`
and no pool sizing or timeouts. With the API exposed publicly this is a denial-
of-service risk: one expensive query can hold a connection open indefinitely,
exhausting the pool and cascading into an outage.

Add a small, unit-testable `postgres-options` module that builds the client
options from conservative, env-tunable defaults:

- PG_MAX_CONNECTIONS  (max pooled connections, default 10)
- PG_IDLE_TIMEOUT     (seconds, default 30)
- PG_CONNECT_TIMEOUT  (seconds, default 30)
- PG_STATEMENT_TIMEOUT(ms server-side query cap, default 30000; 0 disables)

Malformed values fall back to defaults rather than throwing, so a stray typo
can never silently disable a safety limit. Docs, env example, and env type
declarations updated; unit tests cover parsing, fallbacks, and the options shape.

Also anchor the `db/` and `data/` .gitignore rules to the repo root (`/db/`,
`/data/`). The unanchored `db/` matched `src/db/` anywhere in the tree, which
silently ignored the new module file.

Closes #165.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QSuak9smCHbp4N17xjjLF6
@dkijania dkijania force-pushed the feat/pg-pool-timeouts branch from 58c7d49 to dfcad11 Compare June 28, 2026 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P0 Blocker for public availability production-readiness Work toward making the API production-ready / publicly available

Projects

None yet

Development

Successfully merging this pull request may close these issues.

P0: Configure Postgres pool limits + statement_timeout

1 participant