PostgreSQL – Resolving pg_stat_activity Entries Without Client Process

pg-stat-activitypostgresql

If I see an entry in pg_stat_activity for a client_addr:client_port combination where there is no process bound to that port on the indicated client, what does that mean? Just that the client process exited before the query completed?

Best Answer

You are probably correct that this simply means that the client process exited. If it exits without cleaning up the connection while its back-end server process is working on a query, then the server process will not notice until it tries to send a message to the client and finds that no one is there.

If it takes a long time to return a tuple (because it has to sort or aggregate before it sends the first one, for example) then the server process can be a dead man walking for a long time.

There was discussion about fixing this, but finding a way to do it in a performant and cross-platform way is not easy.