Difficulty: Hard
Summary
The src/db/postgres-driver.ts uses the pg library with a basic connection pool but there is no PgBouncer configuration, no pool exhaustion handling, no connection health checks (dead connection detection), and no pool metrics. Under high load, connection exhaustion causes silent request queuing with no observability.
Task
Harden the PostgreSQL driver with proper pool sizing, connection health checks, pool exhaustion timeouts, PgBouncer compatibility (statement-mode), and Prometheus metrics for pool utilization.
Detailed Description
- Pool sizing: Expose
DB_POOL_MIN (default 2) and DB_POOL_MAX (default 10) env vars. Pass these to the pg.Pool constructor.
- Connection health check: Configure
idleTimeoutMillis: 30000 and connectionTimeoutMillis: 5000. On pool error events, log and increment a db_pool_error_total counter.
- Pool metrics: Expose
db_pool_active_connections (gauge) and db_pool_idle_connections (gauge) via the Prometheus endpoint.
- PgBouncer compatibility: Set
options: '--client_encoding=UTF8' and avoid SET commands in transaction scope to support PgBouncer statement mode.
- Connection health probe: Add a
poolHealth() function that checks out a connection, runs SELECT 1, and releases it. Use this in GET /ready instead of a raw query.
- SSL: Respect
DATABASE_SSL env var (true, no-verify, false) — test in tests/db/postgresDriverSsl.test.ts.
Acceptance Criteria
Notes
- See
src/db/postgres-driver.ts for the current pool implementation.
docs/postgres-migration.md has configuration guidance.
- Pool metrics should reuse the existing Prometheus registry from
src/middleware/metrics.ts.
Difficulty: Hard
Summary
The
src/db/postgres-driver.tsuses thepglibrary with a basic connection pool but there is no PgBouncer configuration, no pool exhaustion handling, no connection health checks (dead connection detection), and no pool metrics. Under high load, connection exhaustion causes silent request queuing with no observability.Task
Harden the PostgreSQL driver with proper pool sizing, connection health checks, pool exhaustion timeouts, PgBouncer compatibility (statement-mode), and Prometheus metrics for pool utilization.
Detailed Description
DB_POOL_MIN(default 2) andDB_POOL_MAX(default 10) env vars. Pass these to thepg.Poolconstructor.idleTimeoutMillis: 30000andconnectionTimeoutMillis: 5000. On pool error events, log and increment adb_pool_error_totalcounter.db_pool_active_connections(gauge) anddb_pool_idle_connections(gauge) via the Prometheus endpoint.options: '--client_encoding=UTF8'and avoidSETcommands in transaction scope to support PgBouncer statement mode.poolHealth()function that checks out a connection, runsSELECT 1, and releases it. Use this inGET /readyinstead of a raw query.DATABASE_SSLenv var (true,no-verify,false) — test intests/db/postgresDriverSsl.test.ts.Acceptance Criteria
DB_POOL_MAXconnections queues (not crashes).DB_POOL_MINandDB_POOL_MAXenv vars are documented in README.tests/db/postgresDriverSsl.test.ts.tests/db/postgresDriverClose.test.tsall pass.Notes
src/db/postgres-driver.tsfor the current pool implementation.docs/postgres-migration.mdhas configuration guidance.src/middleware/metrics.ts.