Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Query Cancellation #61

Open
PNixx opened this issue Oct 29, 2023 · 3 comments
Open

Support Query Cancellation #61

PNixx opened this issue Oct 29, 2023 · 3 comments

Comments

@PNixx
Copy link

PNixx commented Oct 29, 2023

For example:

$connection->query('SELECT count(*) FROM users', new TimeoutCancellation(600));
@trowski
Copy link
Member

trowski commented Dec 9, 2023

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.

@PNixx
Copy link
Author

PNixx commented Dec 9, 2023

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.

@trowski trowski changed the title When will support for canceling a request be available? Support Query Cancellation Dec 9, 2023
@trowski
Copy link
Member

trowski commented Dec 9, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants