feat: Add multihost support for native js driver by maxbronnikov10 · Pull Request #3643 · brianc/node-postgres
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.