node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

pg-cursor makes query promises hang

Open markgrin opened this issue 5 years ago • 2 comments

If a client of pg-cursor is closed mid query then other queries start to hang.

const Cursor = require('pg-cursor');
const {Pool} = require('pg');

const pool = new Pool({
    connectionString: 'postgres://postgres@db:5432/postgres',
    max: 20,
    idleTimeoutMillis: 5000,
    connectionTimeoutMillis: 2000
});

function repeatedQuery(func) {
    console.log('QUERY START');
    // PROBLEM: This is never resolved after calling brokenCursor 
   pool.query('SELECT 1;').then(result => { 
        console.log('QUERY END');
        setTimeout(() => func(func), 250);
    }).catch (error => {
        console.log('QUERY ERROR');
    });
}


function brokenCursor() {
    pool.connect().then(client => {
        const cursor = client.query(new Cursor('SELECT * FROM (VALUES (1), (2), (3), (4), (5) ) as result (data);'));
        cursor.read(1, (err, rows) => {
            console.log(rows);
            client.release();
        });
    }).catch(error => {
        console.log(error);
    });
}

repeatedQuery(repeatedQuery);
setTimeout(brokenCursor, 3000);

If this behavior is expected I think it should be noted in documentation.

markgrin avatar Mar 27 '20 13:03 markgrin

@markgrin did you track down the problem in the meantime? which versions of pg and pg-cursor are you using?

pawelrychlik avatar May 13 '20 06:05 pawelrychlik

Hmmm, I have a similar issue and am not sure how to fix that. When I use pg-cursor together with a connection pool the query promise never resolves. It does work fine when using a simple connection.

Does anyone have any ideas?

pg version 8.7.3, pg-cursor version 2.7.3

mschilde avatar Aug 10 '22 23:08 mschilde