feat: add multi-host HA connection support for PostgreSQL clusters#298
Open
Gowtham Raj Elangovan (gowtham500) wants to merge 1 commit into
Open
Conversation
Add PGEngine.from_hosts() factory method enabling connections to high-availability PostgreSQL clusters with automatic failover via psycopg3's native multi-host support. - New from_hosts() classmethod accepting multiple host addresses - target_session_attrs routing (primary, standby, prefer-standby, etc.) - Flexible port configuration (per-host or single broadcast port) - Input validation for hosts, ports, and URL-safe characters - Special character encoding for user/password/database via quote_plus - Comprehensive unit tests for connection string building - Integration tests for single-host and engine kwargs passthrough
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.
Description
Add
PGEngine.from_hosts()factory method that enables connections to high-availability PostgreSQL clusters with automatic failover, using psycopg3's native multi-host connection support.Motivation
Production PostgreSQL deployments commonly use primary/replica topologies (e.g., Patroni, Citus, streaming replication) where clients need to connect to multiple hosts with automatic failover. Currently,
PGEngineonly supports single-host connection strings viafrom_connection_string()orfrom_engine(). This PR adds first-class support for multi-host HA connections withtarget_session_attrsrouting, enabling users to direct queries to primaries, standbys, or any available node.Changes
from_hosts()classmethod onPGEngineaccepting multiple host addresses, per-host ports, andtarget_session_attrsfor connection routing_build_multi_host_connection_string()static method that constructs psycopg3-compatible multi-host connection URLsTargetSessionAttrsLiteral type for valid routing options (any,read-write,read-only,primary,standby,prefer-standby)urllib.parse.quote_plusfrom_hosts()with table creation and engine kwargs passthroughChecklist
Example Usage
Connecting to an HA PostgreSQL cluster (primary + replicas)