Skip to content

feat: add multi-host HA connection support for PostgreSQL clusters#298

Open
Gowtham Raj Elangovan (gowtham500) wants to merge 1 commit into
langchain-ai:mainfrom
gowtham500:feat/multi-host-ha-connection
Open

feat: add multi-host HA connection support for PostgreSQL clusters#298
Gowtham Raj Elangovan (gowtham500) wants to merge 1 commit into
langchain-ai:mainfrom
gowtham500:feat/multi-host-ha-connection

Conversation

@gowtham500
Copy link
Copy Markdown

@gowtham500 Gowtham Raj Elangovan (gowtham500) commented Apr 14, 2026

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, PGEngine only supports single-host connection strings via from_connection_string() or from_engine(). This PR adds first-class support for multi-host HA connections with target_session_attrs routing, enabling users to direct queries to primaries, standbys, or any available node.

Changes

  • New from_hosts() classmethod on PGEngine accepting multiple host addresses, per-host ports, and target_session_attrs for connection routing
  • New _build_multi_host_connection_string() static method that constructs psycopg3-compatible multi-host connection URLs
  • TargetSessionAttrs Literal type for valid routing options (any, read-write, read-only, primary, standby, prefer-standby)
  • Input validation for empty hosts, port ranges (1-65535), host/port length mismatches, and comma-in-host injection
  • URL encoding of user, password, and database via urllib.parse.quote_plus
  • Single-port broadcast (one port applied to all hosts) and default port (5432) support
  • Comprehensive unit tests for connection string building (validation, edge cases, all routing options)
  • Integration tests for from_hosts() with table creation and engine kwargs passthrough

Checklist

  • Code follows project style guidelines
  • Self-review completed
  • Documentation updated
  • Tests added and passing
  • No breaking changes

Example Usage

Connecting to an HA PostgreSQL cluster (primary + replicas)

from langchain_postgres import PGEngine, PGVectorStore
from langchain_core.embeddings import DeterministicFakeEmbedding

# HA cluster with 1 primary + 2 replicas (e.g., Patroni, Citus, streaming replication)
engine = PGEngine.from_hosts(
    hosts=[
        "pg-node1.example.com",
        "pg-node2.example.com",
        "pg-node3.example.com",
    ],
    user="app_user",
    password="secret",
    database="vectordb",
    ports=[5432],  # Same port for all nodes
    target_session_attrs="primary",  # Always route to the current primary
    pool_size=5,
    max_overflow=2,
)

# Use the engine as usual — failover is handled automatically by psycopg3
embedding = DeterministicFakeEmbedding(size=768)
engine.init_vectorstore_table("my_docs", vector_size=768)

store = PGVectorStore.create_sync(
    engine=engine,
    table_name="my_docs",
    embedding_service=embedding,
)

store.add_documents([...])

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
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.

1 participant