postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Support Query Cancellation

Open PNixx opened this issue 2 years ago • 4 comments

For example:

$connection->query('SELECT count(*) FROM users', new TimeoutCancellation(600));

PNixx avatar Oct 29 '23 12:10 PNixx

So far I've not found this to be necessary. Cancelling insert/update/delete queries isn't very practical. Unbuffered select queries can be effectively cancelled by not consuming the entire result set. There is the initial round-trip time to the server where a select query could be cancelled, but would require interface changes in this library as well amphp/sql and amphp/sql-common, which would affect amphp/mysql as well. I'd have to investigate if query cancellation is possible there, or if the connection would just need to be closed.

trowski avatar Dec 09 '23 16:12 trowski

You can use pg_cancel_query. This is necessary, for example, when an HTTP request arrived and it was closed due to a timeout. In this case, the backend does not need to continue to wait for a response from the database; it needs to cancel the request using the database. This immediately reduces the extra load on the database, for example during DDOS attacks. Or when the database is overloaded, there is no point in loading it with more queries, the answers to which no one needs anymore.

PNixx avatar Dec 09 '23 18:12 PNixx

I understand there may be some advantage, particular with buffered select queries. I would be open to implementing this in the future, but first will require some investigation as the best way to go about it. As I mentioned in my prior comment, full support would either 1) require modifying the common components supplied by amphp/sql-common and adding support in amphp/mysql (which I believe requires a second connection to kill a query on the first connection); or 2) not using some common components in amphp/sql-common.

trowski avatar Dec 09 '23 23:12 trowski