skip to content

In PostgreSQL, when a client host vanishes without closing its TCP connection, how do the tcp_keepalives settings, tcp_user_timeout and client_connection_check_interval get its backend cleaned up?

level: seniorimportance: nice to knowfreq 24%

answer

  1. no FIN, no RST
  2. the backend is waiting or busy
  3. probe timing in seconds
  4. polling the socket mid-query
  5. a timeout as the backstop

basics

~20 s

PostgreSQL turns on TCP keepalives for client sockets; tcp_keepalives_idle, _interval and _count (seconds, 0 = OS default) shorten detection of a silent dead peer, tcp_user_timeout caps unacknowledged sends, and client_connection_check_interval lets a busy query notice the loss.

solid answer

~40 s

If a courier-tracking pod loses power, no FIN or RST ever arrives, so its backend can wait for hours, idle in transaction with row locks held or still running a report. The server always enables `SO_KEEPALIVE` on TCP client sockets, but with `tcp_keepalives_idle`, `tcp_keepalives_interval` and `tcp_keepalives_count` at 0 the operating system's timing applies, often hours. Setting, say, 60, 10 and 6 declares a silent peer dead after about two minutes, and the backend exits, rolling back and releasing its locks. Keepalives only probe an idle link; `tcp_user_timeout` (ms) bounds how long sent data may go unacknowledged. A backend busy executing does not read the socket, so `client_connection_check_interval` (ms, Linux, macOS, illumos, BSD) polls it mid-query. `idle_in_transaction_session_timeout` remains the network-independent backstop.

code

ini · 6 lines
ini
# postgresql.conf on the courier-tracking database
tcp_keepalives_idle = 60          # seconds of silence before the first probe
tcp_keepalives_interval = 10      # seconds between unanswered probes
tcp_keepalives_count = 6          # lost probes before the peer is dead
tcp_user_timeout = 120s           # cap on unacknowledged sent data
client_connection_check_interval = 5s   # poll the socket during queries

go deeper

for a junior

Know that a client that vanishes without closing its connection can leave a server session behind, and that timeouts and keepalives exist to clean it up.

for a middle

Explain the three keepalive settings, their seconds unit, what 0 means, and why a busy query needs client_connection_check_interval.

for a senior

Design layered detection for a fleet behind NAT: keepalive timing, tcp_user_timeout, socket polling and idle_in_transaction_session_timeout as the backstop.

for a principal

Balance fast dead-peer detection against false kills on flaky networks, and align database, pooler and client timeouts so they fail in a predictable order.

## Why a vanished client is a database problem When a client closes its connection normally, or its process dies on a live host, the kernel sends a FIN or RST and the backend sees end-of-file at once. When the **host** disappears, through a power loss, a hard VM stop or a network partition behind a NAT gateway, nothing is sent. The PostgreSQL backend serving it then sits in one of two states: - **Waiting for the client** (idle, or idle in transaction): it is blocked reading the socket and learns nothing until TCP declares the peer dead. An idle-in-transaction backend keeps its row locks on, say, `courier_assignments`, and holds back vacuum cleanup the whole time. - **Busy executing** a long query: it does not touch the socket until it has results to send, so it burns CPU and I/O for a client that is gone. ## Server-side keepalives The PostgreSQL server always enables `SO_KEEPALIVE` on TCP client connections. Three settings control the timing: | Setting | Meaning | Unit | Default | |---|---|---|---| | `tcp_keepalives_idle` | silence before the first probe | seconds | 0 = OS default | | `tcp_keepalives_interval` | wait before re-sending an unanswered probe | seconds | 0 = OS default | | `tcp_keepalives_count` | lost probes before the connection is dead | count | 0 = OS default | The operating system's own timing usually waits hours before probing (kernel defaults are the operating-system topic's business), which is why the default rarely helps. With `60`, `10` and `6`, a silent dead peer is declared dead after roughly 60 + 6 × 10 = 120 seconds. The kernel then fails the pending read, the backend exits, its transaction rolls back and its locks are released. Details worth knowing: - These settings apply only where the platform supports the matching socket option; `tcp_keepalives_count` is unavailable on Windows. - On Unix-domain socket connections they are ignored and always read as zero; a local client that dies has its socket closed by the kernel anyway. - Keepalive probes can also stop a stateful firewall or NAT gateway from silently expiring a long-idle connection, provided they come more often than that device's idle timeout. ## tcp_user_timeout Keepalive probes are meant for an idle connection. If the server has **sent data that is not yet acknowledged**, for example a large result streaming to a dead peer, the kernel's retransmission rules govern instead. `tcp_user_timeout` (milliseconds, default 0 = OS default, not available on Windows) caps how long transmitted data may remain unacknowledged before the kernel forcibly closes the connection. ## Busy backends: client_connection_check_interval A backend in the middle of a long query is not reading the socket, so even a connection the kernel already knows is closed goes unnoticed until the next send. `client_connection_check_interval` (milliseconds, default 0 = off) makes the backend poll the socket at that interval while a query runs and abort the query if the kernel reports the connection closed. It relies on kernel events available on Linux, macOS, illumos and the BSD family, and the manual notes that for reliable detection after a network failure the keepalive settings may need tuning too: the poll can only see what the kernel has concluded. ## A layered setup 1. Set the three keepalive settings so a silent peer is detected in minutes, not hours. 2. Set `tcp_user_timeout` close to that window, so connections with data in flight are bounded too. 3. Set `client_connection_check_interval` to a few seconds so abandoned long queries stop early. 4. Keep `idle_in_transaction_session_timeout` on the application roles: it ends stuck transactions whatever the network did, at the price of also ending a healthy but slow client. 5. Bound runaway statements with `statement_timeout` as before. ## The client side libpq has matching connection parameters: `keepalives` (default 1, on), `keepalives_idle`, `keepalives_interval`, `keepalives_count` and `tcp_user_timeout`, which let the **client** detect a dead server, and `connect_timeout` for the connection attempt itself. They protect the application from hanging on a vanished database host; the server-side settings protect the database from vanished clients. ## Choosing the numbers Shorter detection is not free. Aggressive values turn a brief network hiccup into a killed session and a rolled-back transaction, so pick a window longer than the pauses your network really has (a failover of a load balancer, a VM live migration) and shorter than the lock hold you can tolerate. Keep the server-side window and any pooler or client timeouts in a known order, so the layer you expect to give up first actually does.

  • Why can't keepalives alone stop a 20-minute report whose client host lost power?
    The backend is executing, not reading the socket. Keepalives let the kernel conclude the peer is dead, but the backend only learns of it when it next touches the socket, typically when it sends rows. `client_connection_check_interval` makes it poll during execution and cancel early; without it, `statement_timeout` is the only bound.
  • What do the libpq keepalive parameters protect that the server settings do not?
    They act on the client's end. libpq enables keepalives by default (`keepalives` = 1), and `keepalives_idle`, `keepalives_interval`, `keepalives_count` and `tcp_user_timeout` let an application notice that the database host has vanished instead of hanging on a read. `connect_timeout` bounds the connection attempt itself, per host.

  • With default settings PostgreSQL notices a vanished client host within seconds.
  • PostgreSQL sends no TCP keepalives unless tcp_keepalives_idle is set.
  • client_connection_check_interval works on every platform, Windows included.
  • tcp_keepalives_idle is in milliseconds like the session timeouts.
  • statement_timeout ends an idle-in-transaction session left by a dead client.