1

I have 2 db hosts: host1 and host2. I connect to db using multi host connection string: postgresql://host1:port1,host2:port2/. According to libpq documentation, connection is established with first successful host in the list given. When both hosts are running fine, connection with host1 is established and all queries are served from host1. When host1 becomes down, connection with host2 is established and queries are served from host2. Now, when host1 is recovered and running back again, I want to the connection to be established with host1 again and I want queries to be served from host1 again. But it doesn't happen.

I am using golang's pgx library as driver which supports multi-host connection string feature. Is there mechanism which checks if host1 is up or not and establish connection with it once it is up?

Edit: more context

host1 is read-only replica db and host2 is read-write primary db. I have this multi-connection string setup only for read queries. I want those read queries to be served primarily by read-only db. So I want to switch connection back to host1.

Rohanil
  • 1,717
  • 5
  • 22
  • 47

1 Answers1

0

It will establish connections with the first healthy host in the list when it has to establish a connection. During the failover, all connections to host 1 were lost, so new connections were established to host 2. But as long as host 2 stays up, those connections are still good, so it doesn't need to open new connections (which would go to host 1). You can always fail host 2 to force all those connections to disconnect.

Adrian
  • 42,911
  • 6
  • 107
  • 99