Cycle through backend servers and get their httpchk value

Hi folks,

I’m trying to implement postgres failover/switchover. For this, my lua script needs to know which of the servers is currently primary / replica. I run a script on my postgres servers that is accessed by haproxy’s httpchk. It returns 200 when it’s primary, 206 when it’s replica.

How would I go about a core.register_init function that:

  • cycles through all servers in a named backend
  • runs the httpchk method that’s in the haproxy.cfg
  • depending on the return value decides if this backend server is primary or replica and remember that in some global variable