feat: Add multihost support for native js driver#3643
feat: Add multihost support for native js driver#3643maxbronnikov10 wants to merge 1 commit intobrianc:masterfrom
Conversation
There was a problem hiding this comment.
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
targetSessionAttrssupport that checks PostgreSQL backend parameters (in_hot_standby,default_transaction_read_only) after connection, with a fallback toSHOW/SELECTqueries when parameters aren't available viaParameterStatusmessages - Added comprehensive unit tests covering multihost failover, all
targetSessionAttrsmodes, 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.
ffce1e3 to
02f5146
Compare
|
@brianc @charmander hello! can u please check this PR |
There was a problem hiding this comment.
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.
59e2fae to
928c27d
Compare
928c27d to
2778d49
Compare
Summary
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