Repository navigation
♻️ feat: add PG_POOL_PRE_PING/PG_POOL_RECYCLE for dropped remote DB connections - #285
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
PG_POOL_PRE_PINGenv var (defaultTrue). When enabled, the connection pool issues a lightweightSELECT 1before 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.PG_POOL_RECYCLEenv var (default-1, disabled). Set to a positive number of seconds when the server enforces a hard idle/lifetime limit (e.g.1800for a 30-minute cap).get_vector_store→engine_argsonExtendedPgVector/AsyncPgVector.Closes #192, closes #125.
Test plan
tests/services/test_vector_store_factory.py— three new tests for default-enabled pre-ping, explicit disable, andpool_recyclepropagation.Compatibility
Not a breaking change for local docker-compose setups (Postgres on the same host never idles out connections and a
SELECT 1before each checkout is measured in microseconds). Users who for some reason want the old no-pre-ping behavior can setPG_POOL_PRE_PING=False.