Connecting to multiple endpoints for high availability system #1711
-
Hi, I'm using pgx v4.17.2 and sub package pgxpool and was wondering how I can connect to multiple endpoints in my high availability system. I saw there used to be a supporting function 'ParseConnectionString', but don't see it supported anymore? Or possibly I am unable to find where it is documented with the later versions of pgx.
What is the best practice for connecting to multiple endpoints while using pgxpool? Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
It is still supported in v4. https://pkg.go.dev/github.com/jackc/pgconn#ParseConfig. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the response @jackc. Right, so I see that pgconn.ParseConfig supports multiple hosts separated by comma. And in the documentation it says ' It parses connString with the same behavior as pgx.ParseConfig with the addition of the following variables...' In my testing, I am coming across an error when I try to do this. It doesn't seem to be attempting connection in order of the hosts provided. Am I constructing the connection string correctly here? username/(no pass) is the same for each host. Thanks in advance for your help. ` context := context.Background() error:
|
Beta Was this translation helpful? Give feedback.
-
I've corrected my connection string to the following, but I still don't see pgx cycling through the list of given hosts. It seems to be stuck connecting to the first one: 'test-database-server', which is currently does not exist, so I expect pgx to fail connecting (which it does) and attempt to connect to the next host. Currently I am giving it 5 seconds to connect before I attempt a connection retry. Any ideas what I am possible doing wrong here? Do we know how much time is given to each host before it tries connecting to the next one?
log output: 2023/08/15 04:38:05 Reconnecting to Database... ... 2023/08/15 04:38:14 Reconnecting to Database... |
Beta Was this translation helpful? Give feedback.
-
Here are some example connection strings with multiple hosts from the test suite:
But from your last set of error messages the problem looks like a failure to connect to the DNS server. |
Beta Was this translation helpful? Give feedback.
-
hi @jackc, thanks again for the feedback. Yes, I started digging through pgconn and confirmed it is failing at 'expandWithIPs()' at the 'lookupFn' where I'm assuming it is attempting to resolve the domain names and is coming back with 'hostname resolving error'. I'm running my service on a multiple system configurations, some where the hostname may exist and other where it will not. That is why I am interesting in providing a list of hosts to begin with. Just wanted to confirm with you that what is observed here is expected behavior and not a bug? Thanks again |
Beta Was this translation helpful? Give feedback.
Where feasible we try to match the C library libpq's behavior. My guess is that handles this situation properly. So I would consider the current behavior a bug or at least something that could be improved.