Re: Clients disconnect but query still runs - Mailing list pgsql-general

From Tom Lane
Subject Re: Clients disconnect but query still runs
Date
Msg-id 3415.1248745744@sss.pgh.pa.us
Whole thread Raw
In response to Clients disconnect but query still runs  (Robert James <srobertjames@gmail.com>)
Responses Re: Clients disconnect but query still runs
Re: Clients disconnect but query still runs
List pgsql-general
Robert James <srobertjames@gmail.com> writes:
> Hi.  I noticed that when clients (both psql and pgAdmin) disconnect or
> cancel, queries are often still running on the server.  A few questions:
> 1) Is there a way to reconnect and get the results?

No.

> 2) Is there a way to tell postgres to automatically stop all queries when
> the client who queried them disconnects?

No.

> 3) Is there a way to see all queries whose clients have disconnected?

No.

> 4) And finally: Why is this the behavior?

It's not easy to tell whether a client has disconnected (particularly if
the network stack is unhelpful, which is depressingly often true).
Postgres will cancel a query if it gets told that the connection's been
dropped, but it will only discover this when an attempt to output to the
client fails.  It does not spend cycles looking aside to see if the
connection has dropped when it is doing something that doesn't involve
output to the client.

If your client code is polite enough to send a cancel request before
disconnecting, that should terminate the query reasonably promptly.
But just "yanking the plug" doesn't do that.

            regards, tom lane

pgsql-general by date:

Previous
From: Jeff Davis
Date:
Subject: Re: Relational Algebra and Aggregate Functions
Next
From: Tom Lane
Date:
Subject: Re: A question about the permissions