-
Notifications
You must be signed in to change notification settings - Fork 283
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 #783
Comments
+1 to const signal = AbortSignal.timeout(1000); // Aborts after 1 second
const results = await db
.selectFrom('users')
.selectAll()
.execute({ signal }); // Pass the signal If it aborts, it throws an error, and then calls some internal |
This would also require this which is a breaking change. All existing 3rd party dialects would be broken unless we find some backwards compatible way to do it. Once we had the query ID in Query cancellation sounds like a good idea, but breaking all existing dialects needs to be done carefully. |
Broken dialects would just need to upgrade Kysely, right? There are only about 12 of them. It would take an hour to do it. |
Would love to have this, happy to help out in any way. Having |
I also need support for query cancellation +1 |
Hey 👋 Let's talk about cancellation, DB-level. PostgreSQL: MySQL: SQLite: MS SQL Server: |
+1 |
Same here. |
Let me share what I did to work around this in Postgres, by using const query = sql`SELECT * FROM users`;
const timeout = 1_000;
const result = await kysely.transaction().execute((trx) => {
await sql`set local statement_timeout = ${sql.raw(timeout.toString())}`.execute(trx);
return query.execute(trx);
}); What this does:
Using a transaction is necessary, otherwise the timeout would apply to future queries. I created a custom This solved all of my problems. But boy is it ugly. I would really like to be able to cancel queries with AbortSignal. |
Also, AFAIK only MySQL can cancel specific queries. Postgres will actually cancel the whole connection (so it in theory it could result in race conditions...) But anyway, that means the Postgres adapter does not even need query IDs to work. |
OK, #176 got merged. Time to figure this thing out. |
That's amazing @igalklebanov !! 🚀 Thank you! 😁 🙏 |
Thank you @igalklebanov ! |
I have some long-running analytical queries. At some point in the middle of the query running, the end user can express that they're no longer interested in getting more query results, by clicking a 'cancel' button or leaving the page. I'd like, at that point, to stop running the query in the underlying database, to save on compute costs. (For reference, my underlying database is Snowflake, which has pretty good support for cancellation in its driver.)
In some of these cases, I'm reading the query results from Kysely in a streaming fashion; in other cases, I'm just waiting for a promise with the full result.
So, this request is to somehow support query cancellation in kysely, ideally for both streaming and non-streaming queries.
I don't really know what the API should look like for this in kysely, but...
DatabaseConnection
interface could be extended to provide an optionalcancelQuery
methodexecute()
and friends could accept an object argument like{ signal: AbortSignal }
. If/when the signal is aborted before the query finishes, the underlyingcancelQuery
would be called.iterator.return()
), but it would be nice for kysely to cancel the currently-running query at that time (whereas I think all that happens today is that the connection is released after the current batch of data finishes loading). It might also be nice forstream()
to accept a signal too, for convenience, in case the caller already has a signal that they’re using to create one lifecycle for multiple different async operations (which could contain a mix of streaming and non-streaming queries).Thoughts?
NB: edited to suggest AbortSignal instead (brain fart) and to clarify that this isn't limited to streaming.
The text was updated successfully, but these errors were encountered: