Skip to content

♻️ feat: add PG_POOL_PRE_PING/PG_POOL_RECYCLE for dropped remote DB connections - #285

Merged
danny-avila merged 3 commits into
mainfrom
claude/pg-pool-pre-ping
Apr 23, 2026
Merged

danny-avila merged 3 commits into
mainfrom
claude/pg-pool-pre-ping

Conversation

@danny-avila

Copy link
Copy Markdown
Collaborator

Summary

Fixes persistent "connection to external DB gets dropped" issues on managed Postgres (Azure Container Apps, RDS behind an NLB, etc.) where the server — or a middlebox load balancer — silently closes idle connections and the next query fails because SQLAlchemy's pool hands out a dead connection.

  • New PG_POOL_PRE_PING env var (default True). When enabled, the connection pool issues a lightweight SELECT 1 before handing out a pooled connection, so stale connections are transparently replaced. The overhead is negligible and this is SQLAlchemy's own recommended default for any deployment that isn't purely local.
  • New PG_POOL_RECYCLE env var (default -1, disabled). Set to a positive number of seconds when the server enforces a hard idle/lifetime limit (e.g. 1800 for a 30-minute cap).
  • Plumbs both settings through get_vector_store → engine_args on ExtendedPgVector/AsyncPgVector.

Closes #192, closes #125.

Test plan

  • tests/services/test_vector_store_factory.py — three new tests for default-enabled pre-ping, explicit disable, and pool_recycle propagation.
  • Manual: deploy against a Postgres reachable over a load balancer with an idle timeout, leave idle > timeout, then issue a query — should succeed instead of raising a dropped-connection error.

Compatibility

Not a breaking change for local docker-compose setups (Postgres on the same host never idles out connections and a SELECT 1 before each checkout is measured in microseconds). Users who for some reason want the old no-pre-ping behavior can set PG_POOL_PRE_PING=False.

Plumbs pool_pre_ping and pool_recycle through get_vector_store into
the engine_args passed to ExtendedPgVector/AsyncPgVector. Default
enables pool_pre_ping so stale/dead connections (dropped by a remote
Postgres server or middlebox idle timeout) are transparently
replaced instead of surfacing as query errors.
Fixes persistent "connection to external DB gets dropped" issues on
managed Postgres and deployments whose connections traverse a load
balancer with an idle timeout (Azure Container Apps, RDS behind an
NLB, etc.). PG_POOL_PRE_PING defaults to True — the negligible
SELECT 1 overhead is the sensible default for remote DBs and is a
no-op for local setups. PG_POOL_RECYCLE is off by default (-1) and
should be set to the server's idle timeout when one is enforced.

Closes #192, closes #125
@danny-avila danny-avila changed the title ✨ feat: add PG_POOL_PRE_PING/PG_POOL_RECYCLE for dropped remote DB connections ♻️ feat: add PG_POOL_PRE_PING/PG_POOL_RECYCLE for dropped remote DB connections Apr 23, 2026
@danny-avila
danny-avila merged commit b723602 into main Apr 23, 2026
1 check 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.

Connection to external DB gets dropped Embedding fails on first upload

1 participant