Commits on Aug 6, 2026
-
libpq: Add DNS SRV record support for service discovery
Add a new connection parameter "srvhost" that causes libpq to query _postgresql._tcp.<srvhost> SRV records before connecting, replacing the normal host list with the sorted result. This allows a whole PostgreSQL cluster to be addressed by a single DNS name. New connection options: srvhost=<domain> DNS domain for SRV lookup (env: PGSRVHOST) postgresql+srv://<domain>/<db> URI shorthand for srvhost postgres+srv://<domain>/<db> alias SRV records are sorted per RFC 2782 (priority ascending, weight descending) and injected into the existing multi-host machinery before connhost[] is built, so target_session_attrs, load_balance_hosts, and failover all work transparently on the expanded host list. srvhost is mutually exclusive with host and hostaddr. Multiple hosts in a +srv URI are rejected because expansion is DNS's job. On POSIX, resolution uses res_query(3) + dn_expand(3) to parse the DNS wire format directly, avoiding ns_initparse() which is absent on musl. On Windows, DnsQuery() from windns.h is used instead. Platforms without either receive a clear error at connect time. The resolver is called from pqConnectOptions2() before the connhost[] array is allocated, requiring no changes to PQconnectPoll or the connection state machine.