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

Sometimes query returns empty result set

Open aleksandrmelnyk opened this issue 13 years ago • 16 comments

Hello guys,

I am using node-pg inside node-any-db wrapper with pool min: 100 max: 200 connections. Sometimes result set is empty or incomplete (for example it returns 200 rows instead of 1800). Restarting node application fixes it. While it returns empty result set, I tried to run the query from another node.js cli script using same parameters and it works ok. Any ideas what could it be?

Thank you in advance,

Alex.

aleksandrmelnyk avatar Jul 18 '13 08:07 aleksandrmelnyk

and no error messages on logs.

aleksandrmelnyk avatar Jul 18 '13 09:07 aleksandrmelnyk

this behavior was noticed for both: native and JS mode.

aleksandrmelnyk avatar Jul 18 '13 09:07 aleksandrmelnyk

Can you reproduce the issue without using any-db?

brianc avatar Jul 18 '13 14:07 brianc

I have not refactor the code yet as it is production application, but going to try to use pg without any-db and will report here.

aleksandrmelnyk avatar Jul 18 '13 20:07 aleksandrmelnyk

Have tried node-pg directly (without not-any-db) and the issue was not reported for few weeks, I could not replicate it. Seem like it relates to not-any-db.

aleksandrmelnyk avatar Aug 07 '13 04:08 aleksandrmelnyk

Sorry, seems this issue relates to node-pg, it appeared even if I use nodePg.connect(function(){ client.query(); });

What I noticed it was reported only for "heavy" modules, where 3 parallel queries are done more then 300 times. As usual restarting node.js sorts temporary (for few days) Can it be related to memory managing anyhow?

aleksandrmelnyk avatar Aug 19 '13 05:08 aleksandrmelnyk

Can you submit code to reproduce this issue?

brianc avatar Aug 19 '13 22:08 brianc

So far, no. It is part of quite complex program. And just after node.js start it is working alright. I will try to provide additional data if possible.

2013/8/20 Brian C [email protected]

Can you submit code to reproduce this issue?

— Reply to this email directly or view it on GitHubhttps://github.com/brianc/node-postgres/issues/401#issuecomment-22907951 .

aleksandrmelnyk avatar Aug 21 '13 11:08 aleksandrmelnyk

I have the same issue: Here is a code which start 5 select queries from database. I've got error on different usernames at each start. PS. There are integrated recursion for repeat request with invalid responses and sleep for make delay between requests. (I thought that help me, but not...)

function isUserConfirmed(username, cnt) { logger.log('info','IsUserConfirmed check... USERNAME: ', username); //Async sleep 100ms sleep(100, function() { //New connection for each request!!! pg.connect(simplexPgConnectionString, function(err, client, done) { if(err) { logger.log('error', 'Fetching client from pool [%j]', err, {}); } client.query('SELECT username, confirmed from users where username = $1', [username], function(err, result) { //call done() to release the client back to the pool done(); logger.log('info','SELECT response [%j]',result, {}); logger.log('info','SELECT rows [%j]',result.rows, {}); if(err) { return logger.log('error', 'Error running query [%j]', err, {}); } var confirmed = false; if (result.rows[0] != undefined) { logger.log('info', "Confirmed: ", result.rows[0].confirmed); } else { logger.log('warn', 'Undefined!!!'); cnt++; if (cnt<0) // Recursion steps. 0 - disable recursion. { logger.log('warn', 'Hello recursion!!! ['+cnt+']'); isUserConfirmed(username, cnt); }

            }
        });

    });
});

}

function sleep(time, callback) { var stop = new Date().getTime(); while(new Date().getTime() < stop + time) { ; } callback(); }

var test_message = function() { var errarr = [ "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]" ]; for (var i = 0; i < 5; i++) { isUserConfirmed(errarr[i], 0); }; } test_message();

P.P.S: This is bad response: (it might be help) SELECT response [{"command":"SELECT","rowCount":0,"oid":null,"rows":[],"fields":[{"name":"username","tableID":1177594,"columnID":2,"dataTypeID":1043,"dataTypeSize":-1,"dataTypeModifier":-1,"format":"text"},{"name":"confirmed","tableID":1177594,"columnID":12,"dataTypeID":16,"dataTypeSize":1,"dataTypeModifier":-1,"format":"text"}],"_parsers":[null,null],"rowAsArray":false}]

Gamblt avatar Jun 11 '14 20:06 Gamblt

Can you take a little time to properly format the code so I can read it?

brianc avatar Oct 20 '14 03:10 brianc

Did you ever get this figured out? Feel free to reopen if its still causing you problems. Otherwise I'm going to assume you found a fix.

brianc avatar Oct 20 '14 03:10 brianc

I have the same problem (node version 8.11.1, mssql 2016, mssql/tedious driver) Sample output from a process that fetches data with same conditions repeatedly for 500 times as kind of a load test

Rows returned : 13 Rows returned : 13 Rows returned : 13 Rows returned : 13 Rows returned : 0 Rows returned : 13 Rows returned : 0 Rows returned : 13 Rows returned : 13 Rows returned : 13

orshee avatar Jun 08 '18 13:06 orshee

@orshee This is the repository for https://www.npmjs.com/package/pg, a PostgreSQL driver.

charmander avatar Jun 08 '18 20:06 charmander

I have the same problem and it occures very often. Sometimes every 3 to 4 querries. Running ^7.14.0 for NodeJS. I make a count querrie and then a querrie for the data and it occures quiet often that either the count querry or the data querry return empty results.

Edit: It mostly only occures on a system with low performance.

Darcody avatar Aug 03 '20 09:08 Darcody

@Darcody I need the version of node, pg, and a self-contained code example that reproduces the problem if you want me to take a look. If you're saying you're using version 7.14 of node.js then that's an unsupported version of node, btw.

brianc avatar Aug 03 '20 13:08 brianc

Never mind. It was our fault. We have two databases and a consule to which we connect and for some reason the databases were asynchron which resulted to empty results. Sorry for your inconvenience.

Darcody avatar Aug 04 '20 09:08 Darcody