Repository navigation
Open
Labels
dialect: postgresFor issues and PRs. Things that involve PostgreSQL (and do not involve all dialects).hardFor issues and PRs.performanceFor issues and PRs. Things that affect the performance of Sequelize.
Description
Issue Description
Is your feature request related to a problem? Please describe.
We have a large paginated table on the frontend that the users can type into to filter results. As they type we send new requests and cancel the previous ones using Axios.
This will kill the request on the frontend, meaning the browser stops trying to get the data, and sends a message to the server to cancel it. Express can receive this signal.
Describe the solution you'd like
When a request was cancelled by the frontend, I'd like to stop any related queries, since some requests may result in accessing a lot related backend tables is PostgreSQL.
Why should this be in Sequelize
Seems like an extremely common need and could dramatically boost performance by freeing up resources.
Describe alternatives/workarounds you've considered
¯\_(ツ)_/¯
Additional context
Issue Template Checklist
Is this issue dialect-specific?
- No. This issue is relevant to Sequelize as a whole.
- Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ
- Ummmm, it effects whichever ones allow canceling
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don't know how to start, I would need guidance.
- No, I don't have the time, although I believe I could do it if I had the time...
- No, I basically just do frontend and have no idea how any of this works
Reacted by Michał Moczulski, Rahmat Ali, yasser, gutiar-junky, Niraj, The Jared Wilcurt and Edmundo Santos
Metadata
Metadata
Assignees
Labels
dialect: postgresFor issues and PRs. Things that involve PostgreSQL (and do not involve all dialects).hardFor issues and PRs.performanceFor issues and PRs. Things that affect the performance of Sequelize.
Activity
papb commentedon Sep 7, 2019
Putting Sequelize aside for a moment, once a query is sent to a database, how can it be canceled?
acarl commentedon Sep 10, 2019
Canceling a running query is definitely possible in at least postgres.
There is an existing issue to support this for the node-postgres package.
brianc/node-postgres#753
mmoczulski commentedon Nov 14, 2019
I found a workaround for killing / terminating queries. I think it's not an ideal solution and uses the knowledge of sequelize internals and its connection manager. Keep in mind, I'm still using sequelize v3, so I'm not sure if this will work automatically for newer versions. Apart of that I also use express and pg.
What I do here? First, I obtain the connection with underlying connection manager (and generic-pool). Then I execute the query with fake transaction object. I know that sequelize re-uses same connection if executing with transaction objects. So I know that my connection is gonna be used within the query. In the meantime I create event handler for closed
reqobject. Once I get thecloseevent I try to kill the connection process (with a new connection in fact). In the end, if the query is executed successfully I need to release the connection and return the result of query.Why this solution is not ideal? First it requires at least one available connection in the underlying pool. If we use only one connection at a time, this just won't work. If our server is busy and is using all connections within pool, it won't execute until at least one connection is released. It would be better if we could somehow kill the connection from app side. Second, it depends on the knowledge of internal implementation of sequelize and connection manager.
With the transaction object obtained with
sequelize.transaction()it's even easier - we already have the connection and we don't need to release it afterwards.acarl commentedon Nov 15, 2019
That's a really creative solution @mmoczulski.
There are a couple of other things to consider when using pg_terminate_backend(). (documentation)
gutiar-junky commentedon Sep 7, 2021
Is there any hope that this will be implemented in the near future?
I'm facing exactly the same problem but with mysql.
mmoczulski's solution is a great workaround but I would really prefer a native solution.
github-actions commentedon Nov 7, 2021
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂