Connections and Authentication / TCP Settings

Master PostgreSQL TCP connection parameters. Learn optimal settings for keepalives, timeout management, and connection stability for reliable database networking.

client_connection_check_interval

  • What it does: Sets the time interval between checks for client disconnection while queries are running.
  • Why it matters: This parameter is crucial for detecting and cleaning up abandoned connections promptly. Without proper checking, interrupted client connections (due to network issues, client crashes, or firewall timeouts) can leave server processes running unnecessarily, consuming valuable resources and potentially holding locks. This helps prevent resource leaks and ensures that database resources are efficiently utilized.
  • Ideal value & Best Practice: Default 0 (disabled) is not recommended for production. Set to 30 seconds or lower for most environments. For systems with unstable networks or high connection churn, consider 10-15 seconds. Balance between rapid detection and avoiding excessive overhead from frequent checks.

tcp_keepalives_count

  • What it does: Sets the maximum number of TCP keepalive probes that can be sent before considering the connection dead.
  • Why it matters: This parameter works with other keepalive settings to detect broken connections at the TCP level. It determines how persistent the system should be in verifying connection liveness before giving up. Proper configuration ensures that half-open connections are identified and cleaned up efficiently.
  • Ideal value & Best Practice: Default 0 (use system default, typically 5-9). Set to 5 for most environments. Increase to 8-10 for networks with occasional packet loss, but avoid very high values that might keep dead connections alive too long.

tcp_keepalives_idle

  • What it does: Sets the amount of time a connection must be idle before TCP begins sending keepalive probes.
  • Why it matters: Controls how quickly the system starts checking idle connections for liveness. This is particularly important for preventing "half-open" connections where the client has disappeared but the server maintains the connection. Timely detection free up resources and prevents connection pool exhaustion.
  • Ideal value & Best Practice: Default 0 (use system default, often 2 hours). Set to 300 seconds (5 minutes) for most production environments. For applications with strict resource constraints, consider 60-120 seconds. Adjust based on your network stability and application requirements.

tcp_keepalives_interval

  • What it does: Sets the time between individual TCP keepalive probe retransmissions after the initial idle period.
  • Why it matters: Determines how aggressively the system checks for connection responsiveness once keepalive probing begins. Shorter intervals detect dead connections faster but generate more network traffic. Longer intervals reduce overhead but delay detection of connection failures.
  • Ideal value & Best Practice: Default 0 (use system default, typically 75 seconds). Set to 75 seconds for balanced behavior. For critical systems requiring fast failure detection, use 30-60 seconds. In high-latency networks, you might need longer intervals.

tcp_user_timeout

  • What it does: Sets the maximum amount of time that transmitted data may remain unacknowledged before the connection is forcibly closed.
  • Why it matters: This is a critical parameter for handling network partitions and severe connectivity issues. It ensures that the server doesn't wait indefinitely for client responses that will never arrive. Proper timeout settings prevent hung connections and ensure predictable behavior under network failure conditions.
  • Ideal value & Best Practice: Default 0 (use system default). Set to 30000 milliseconds (30 seconds) for most environments. For applications requiring aggressive timeout handling, use 10000-15000ms. In high-latency WAN environments, you might need higher values. Always test timeout behavior to ensure it matches your application's recovery capabilities.

Try pghealth Free Today πŸš€

Start your journey toward a healthier PostgreSQL with pghealth.
You can explore all features immediately with a free trial β€” no installation required.

πŸ‘‰ Start Free Trial