paurkedal / ocaml-caqti

Cooperative-threaded access to relational data
https://paurkedal.github.io/ocaml-caqti/index.html
GNU Lesser General Public License v3.0
299 stars 36 forks source link

is there a way to cancel a query? #74

Closed pw374 closed 2 years ago

pw374 commented 2 years ago

Say there's a query that's taking too much time, and I want to kill it so that I can reuse that connection for other queries. Is it possible?

I thought naively that Lwt.cancel could do the job but it doesn't work and I end up with Invalid concurrent usage of PostgreSQL connection detected.

paurkedal commented 2 years ago

I will take a look whether Lwt.cancel can be supported, but some options for the current version may be to set statement_timeout on the connection before issuing the long-running statements.

pw374 commented 2 years ago

I will take a look whether Lwt.cancel can be supported, but some options for the current version may be to set statement_timeout on the connection before issuing the long-running statements.

I think that trick will do for now. Thanks!

paurkedal commented 2 years ago

I tried to get Lwt.cancel to work, but it seems tricky since Lwt.catch does not intercept the timeout exception, probably because it is itself cancelled during the propagation. It's probably possible to make it work with the correct cancellation primitives around the Lwt.catch, but I can see from the manual page that "cancelation has proved difficult to understand, explain, and maintain, so use of these functions is discouraged in new code." So, I think the better solution is to use the statement timeout.

I'm pushing a commit now which adds generic support for setting the statement timeout for a MariaDB and PostgreSQL connection. I couldn't find a way to do the same for SQLite3, so that's left as a no-op.

pw374 commented 2 years ago

Thank you! 😊