question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how is the way for cancel query?

See original GitHub issue

Can I cancel a query inside query.on(‘row’…?

I want to do something like:

query.on('row',function(row, result){
    if(row.field1=='ok'){
        result.addRow(row);
    }else{
        result.cancel(row, 'bla bla bla'); 
    }
});

The need is to not continue fetching rows if I detect an error condition.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:2
  • Comments:26 (9 by maintainers)

github_iconTop GitHub Comments

8reactions
robhaswellcommented, Apr 13, 2017

You should be able to cancel queries using pg_cancel_backend(). I think the pattern looks like this:

const pool = pg.Pool()

/* Check out a client */
pool.connect()
.then((c) => {
  /* Get my PID */
  c.query('SELECT pg_backend_pid()')
  .then((result) => {
    const pid = result.rows[0][0]

    const badResult = c.query('SELECT pg_sleep(10000000000)')

    /* Kill this after 1 second */
    setTimeout(() => {
      /* NOTE this query is executed by the pool */
      const cancelResult = pool.query('SELECT pg_cancel_backend($1)', [ pid ])
    }, 1000)
  })
})
3reactions
robhaswellcommented, Jan 29, 2020

start the governing connection before starting the cancellable query

This is VERY expensive, especially in a scenario where resources are limited.

A better approach would be to have a reserved pool for cancelling connections:

// My platform has allocated me 20 connections
const pool = new Pool({max: 19})
const adminPool = new Pool({max: 1})

const client = await pool.connect()
client.query('SELECT pg_sleep(1000000)')

const otherClient = await adminPool.connect()
otherClient.cancel(client)
// - or, preferably: -
await adminPool.cancel(client) // like Pool.query

Whether you want the user to instantiate the admin pool, or allow them to specify the number of reserved connections, I think either is fine. I expect the latter would be quite a significant refactor, to have Pool manage two logical pools.

Read more comments on GitHub >

github_iconTop Results From Across the Web

CANCEL QUERY | CockroachDB Docs
The CANCEL QUERY statement cancels a running SQL query. Considerations. Schema changes are treated differently than other SQL queries.
Read more >
Options - Cancel Queries
There are two ways to cancel the query during the first phase: ODBC Driver. This option is available if your ODBC Driver supports...
Read more >
Canceling Statements - Snowflake Documentation
The recommended way to cancel a statement is to use the interface of the application in which the query is running (e.g. the...
Read more >
How to cancel a query and return a subset of records - Ask TOM
How to cancel a query and return a subset of records I have a requirement like this:One of the database table can have...
Read more >
How to CANCEL a query running in another session?
Before we go on, here's how query cancellation (pressing CTRL+C in sqlplus for example) works in Oracle:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found