Skip to content

Cancel transactions #11395

Open
Open

Description

@TheJaredWilcurt

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

Activity

  1. papb commented on Sep 7, 2019

    @papb
    Member

    Putting Sequelize aside for a moment, once a query is sent to a database, how can it be canceled?

  2. added
    status: awaiting response
    on Sep 7, 2019
  3. acarl commented on Sep 10, 2019

    @acarl

    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

  4. mmoczulski commented on Nov 14, 2019

    @mmoczulski

    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.

    const killableQuery = async (query, req, options = {}) => {
      const connection = await sequelize.connectionManager.getConnection();
    
      req.on('close', () => {
        sequelize.query(`SELECT pg_terminate_backend(${connection.processID});`)
          .catch((error) => {
            console.error('Unable to terminate query!', error);
          });
      });
      const result = await sequelize.query(query, {
        ...options,
        transaction: { connection },
      });
    
      await sequelize.connectionManager.releaseConnection(connection);
    
      return result;
    };
    

    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 req object. Once I get the close event 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.

  5. acarl commented on Nov 15, 2019

    @acarl

    That's a really creative solution @mmoczulski.
    There are a couple of other things to consider when using pg_terminate_backend(). (documentation)

    1. It requires superuser access to run the function. Depending on your setup you may not want your application user to have superuser access to your database.
    2. When you call this you are actually killing the postgres server's process for the connection. Every time you do this postgres will need make a new process to replace it. If you make use of this cancel ability a lot it would put extra load on the database server as it has to constantly rebuild these processes. It also puts extra strain on the connection pool as you pointed out.
  6. added
    hard
    performance
    dialect: postgres
    and removed
    status: awaiting response
    on Jan 15, 2020
  7. gutiar-junky commented on Sep 7, 2021

    @gutiar-junky

    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.

  8. github-actions commented on Nov 7, 2021

    @github-actions
    Contributor

    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. 🙂

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    dialect: postgreshardperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions