Skip to content

feat: Add multihost support for native js driver#3643

Open
maxbronnikov10 wants to merge 1 commit intobrianc:masterfrom
maxbronnikov10:feat/add-multihost-support
Open

feat: Add multihost support for native js driver#3643
maxbronnikov10 wants to merge 1 commit intobrianc:masterfrom
maxbronnikov10:feat/add-multihost-support

Conversation

@maxbronnikov10
Copy link
Contributor

@maxbronnikov10 maxbronnikov10 commented Mar 10, 2026

Summary

  • Add host/port array support to Connection.connect() in the pure JS driver, with automatic TCP-level failover to the next host on connection error
  • Add targetSessionAttrs config option to Connection and wire it through Client
  • Intercept protocol events during startup to collect in_hot_standby and default_transaction_read_only from ParameterStatus messages and validate them against targetSessionAttrs before signalling readyForQuery to the caller
  • Fall back to issuing SHOW transaction_read_only; SELECT pg_catalog.pg_is_in_recovery() for older servers that don't advertise those params

Behavior Changes

When host and port are arrays, the driver tries each host in order. TCP-level errors before the first successful connect silently trigger a retry on the next host. With targetSessionAttrs set, hosts that connect successfully but don't match the session requirement are also silently skipped. If the list is exhausted without a match, an error is emitted.

Problem

The pure JS driver previously only accepted a single host and had no targetSessionAttrs support, unlike the native libpq driver. This made it impossible to use driver for high-availability Postgres setups where you want to connect to whichever replica is currently the primary, or explicitly route to a read replica.

The lack of multihost support meant users had to implement their own failover logic outside the driver, or switch to the native bindings just to get this feature.

Related issues

#1470

Copilot AI review requested due to automatic review settings March 10, 2026 18:01
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds multihost connection support to the pg native JavaScript driver's Connection class, enabling TCP-level failover across multiple hosts and PostgreSQL target_session_attrs session attribute filtering (e.g., read-write, read-only, primary, standby, prefer-standby).

Changes:

  • Extended Connection.connect() to accept arrays of hosts and ports, with automatic failover to the next host on TCP connection errors
  • Implemented targetSessionAttrs support that checks PostgreSQL backend parameters (in_hot_standby, default_transaction_read_only) after connection, with a fallback to SHOW/SELECT queries when parameters aren't available via ParameterStatus messages
  • Added comprehensive unit tests covering multihost failover, all targetSessionAttrs modes, and the SHOW query fallback mechanism

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.

File Description
packages/pg/lib/connection.js Core multihost and targetSessionAttrs logic in the Connection class, including stream factory support, emit interception for session attribute checking, and the isHostMatchTargetSessionAttrs helper function
packages/pg/lib/client.js Passes targetSessionAttrs config option through to the Connection constructor
packages/pg/test/unit/connection/multihost-tests.js Unit tests for multihost connectivity, all targetSessionAttrs modes, fallback SHOW queries, and edge cases
.gitignore Adds .history directory to gitignore

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@maxbronnikov10 maxbronnikov10 force-pushed the feat/add-multihost-support branch from ffce1e3 to 02f5146 Compare March 10, 2026 18:16
@maxbronnikov10
Copy link
Contributor Author

@brianc @charmander hello! can u please check this PR

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@maxbronnikov10 maxbronnikov10 force-pushed the feat/add-multihost-support branch 2 times, most recently from 59e2fae to 928c27d Compare March 10, 2026 18:49
@maxbronnikov10 maxbronnikov10 force-pushed the feat/add-multihost-support branch from 928c27d to 2778d49 Compare March 10, 2026 18:50
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.

2 participants