IP Address is very slow compared to loopback address

Solution 1:

By default on Linux traffic from the system itself to the (public) IP-address of the server will not go to the actual NIC nor out over the wire, when that ip-address is configured on the server itself.

By default Linux uses a single network stack and communication to and from all configured ip-addresses will be in-memory in the Linux kernel's network stack, even when the source and destination ip-addresses are associated with different NIC's.

The network speed when communicating with that IP-address should therefore be the same as the loopback interface and is only limited by how fast the system is, not the wire speed of the network uplink (and will usually exceed the bandwidth of that uplink).

When there is a significant difference in those speeds some notable exceptions to that default behaviour come to mind:

  • The public ip-address of the server (or whatever the DNS name you're using resolves to) is not a configured ip-address of that system.
    Check with ip addr.

    • For instance in many cloud deployments the public IP-address associated with an instance is NAT construct configured and maintained in the providers network and that public ip-address is NOT configured in the server itself. (That is also what allows you to use the management layer to remove that public ip-address from one system and assign it to another.) Traffic from the server to that public ip-address will not, as far as your system is aware, have a local destination. It needs to be transmitted before the external NAT mapping will direct it back.
    • Similar the hostname / public ip-address may be associated with a loadbalancer / reverse-proxy and when the applications tries to address itself the request will be routed via the load balancer back to the node (possibly even to a completely different one).
  • The system can have policy routing enabled. That can force the system to send traffic between different IP-addresses on different NIC's out over the wire to the external network. Check with ip rule list.
  • Network namespaces allow the Linux kernel to set up more than one network stack, each with their own IP and routing settings. Traffic between different network namespaces will usually also be routed outside of the system itself and much slower than the loopback interface. Check with ip netns list

I'm not overly familiar with Postgres but typically you can get the most performance with services that support a unix domain socket in addition to TCP/IP connections by using a socket connection instead of either using the localhost or external IP-address as that takes away the overhead of framing the data in IP packets.

So rather than using the external IP-address or the localhost 127.0.0.1 address, enable and connect to the Postgresql socket.