Skip to content

Abort running query #461

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

Closed
twoxfh opened this issue Jun 19, 2021 · 3 comments
Closed

Abort running query #461

twoxfh opened this issue Jun 19, 2021 · 3 comments

Comments

@twoxfh
Copy link

twoxfh commented Jun 19, 2021

Does anyone know how to abort a running query?

I added the sqlite3_interruput to SQL-JS, however since the underlying code is really synchronous, this really does nothing. I found an abort controller, issue with great discussion but without any outcome. There is a pragma busy-timeout which does not seem to abort the query. The only other method I have used is terminating the worker, however, then I have to reload the database.

Any thoughts?

@lovasoa
Copy link
Member

lovasoa commented Jun 19, 2021

It is not possible to abort a running query in the current sql.js. Your best bet for the moment is to kill the worker.

You could try to implement that using emscripten's pthread support and sqlite3_interrupt, but this would probably be a lot of work, make the library slower, require you to serve your pages with custom headers, and not work on all browsers...

@rhashimoto
Copy link

rhashimoto commented Jun 20, 2021

Note that you can interrupt a query between output rows, simply by not continuing to call step() for the next row. The problem is interrupting a query in the middle of a step, e.g. for very large aggregation queries.

I think there might be one way to do that with the current sql.js, though it is pretty limited. If you use create_function to define a custom SQL function, then throwing an exception from that function looks like it will abort the query that uses it. You would have to construct your query to call the function repeatedly, and the function needs to decide synchronously whether or not to throw. So perhaps technically possible for some narrow use cases.

The easiest way to implement and use this interrupting function would be to make it just return its argument:

function interruptible(x) {
  if (checkForInterrupt()) {
    throw new Error('interrupt');
  }
  return x;
}

Then just wrap one of the SELECT expressions in the SQL query:

SELECT SUM(interruptible(x)) FROM my_large_view;

Alternatively, if you're not opposed to forking sql.js and hacking the SQLite C source, you could find a spot in the SQLite code to insert a call to Javascript and conditionally return an error code (somewhere in the pager might work). Basically a similar approach as above but one that works with any query.

@twoxfh
Copy link
Author

twoxfh commented Jun 21, 2021

Thanks @lovasoa and @rhashimoto great insight. I was able to make a sub select as a start time, use time in the main select, calculate the difference in time, then throw the Error which halted the query. Convoluted but worked.

Thanks, hopefully over time it wont get so convoluted.

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

No branches or pull requests

3 participants