Search code examples
postgresqllibpq

What's the behavior of libpq when hostname resolves to multiple IP addresses?


I have a pg_autofailover cluster monitored by single monitor node, 1 primary node accepting read-write connections, and 2 standby nodes ingesting WAL from the primary to keep up to date.

Currently I publish all 3 IPs to my client apps, using which they form the Postgresql connection string: postgresql://user:pass@ip1:5432,ip2:5432,ip3:5432/dbname. I want the client apps to always connect to current primary node, so I advise them to use targetServer=primary.

All this is working fine.

Now I have started using TLS based connections, and need all my postgres nodes to share a hostname and certificate for some unrelated and unavoidable reasons. So basically my DNS server will resolve hostname my_awesome_hostname to 3 IP addresses: ip1,ip2 and ip3.

My question is, If I tell my clients to use connection string: postgresql://user:pass@my_awesome_hostname:5432/dbname Then, at the client side, will libpq keep behaving similar to as it is doing now? Will it try to connect to each ip returned from the DNS record 1 by 1 and establish connection with the primary node?


Solution

  • According to my reading of the source, PostgreSQL will store all the IP addresses in its connection object and will treat them just as if you had specified multiple hosts. By the way, the parameter is called target_session_attrs, not targetServer.