pgjdbc-ng icon indicating copy to clipboard operation
pgjdbc-ng copied to clipboard

pgjdbc-ng driver and cluster failover - targetServerType=master

Open vazquesaid opened this issue 5 years ago • 7 comments
trafficstars

i have a postgresql cluster with a master and 2 standby nodes, i need the failover to be managed by the impossibl drive the same as the standard postgresql driver using the targetServerType=master. will the impossibl driver implement a such feature ?

Thanks for your answer.

vazquesaid avatar Jun 10 '20 07:06 vazquesaid

https://impossibl.github.io/pgjdbc-ng/docs/current/user-guide/#connection-fallback

kdubb avatar Sep 21 '20 22:09 kdubb

☝️That is all that is supported with regard to "multiple" servers.

kdubb avatar Sep 21 '20 22:09 kdubb

I have the same issue, does targetServerType parameter supported by pgjdbc-ng ? thanks for feedback

dibwill avatar Apr 15 '21 16:04 dibwill

The pgjdbc-ng does not support the targetServerType. i have to create a proxy driver witch encapsulate the pgjdbc and added the targetType=master as URL property and ping for each connection the server using the (select pg_is_in_recovery()) to elect the master. if you use tomcat or hikari connection pool you can add the validationQuery to help you validate the master connection : select case when not pg_is_in_recovery() then 1 else random() / 0 end doing all above it works like a charm .

vazquesaid avatar Apr 22 '21 12:04 vazquesaid

I also have issues with failover when multiple addresses are specified in connection URL. If the first host is secondary (replica), then my service will successfully connect to it, but it will throw an error when LISTEN command is executed on replica.

Do you have any plans to support proper automatic failover within this driver, something similar like on official Postgres JDBC driver?

bhemar avatar May 04 '21 07:05 bhemar

To fix this failover issue, you need to write a small driver delegate, by writing or overloading the getConnection() method. The new getConnection method will test each native getConnection (from the url lists) against the (if(select pg_is_in_recovery() = false) then return connection; else go to the next url; and so until you find a master node). the target is to return the connection on the pool only if it is from master. Hope that helps.

vazquesaid avatar May 11 '21 10:05 vazquesaid

I can try something like this. thanks!

bhemar avatar May 11 '21 10:05 bhemar