node-pg-native icon indicating copy to clipboard operation
node-pg-native copied to clipboard

Code throws error under pg-native but not pg

Open rmzg opened this issue 9 years ago • 7 comments

const pg = require('pg').native;

async function main() {
  let client = new pg.Client({host:"/var/run/postgresql"});
  await client.connect();
  client.end();
}

main();

That throws the error:

assert.js:85
  throw new assert.AssertionError({
  ^
AssertionError: Must be connected to start reader
    at PQ.startReader (/home/buu/dev/bs/node_modules/pg-native/node_modules/libpq/index.js:258:3)
    at module.exports.Client._startReading (/home/buu/dev/bs/node_modules/pg-native/index.js:145:11)
    at module.exports.<anonymous> (/home/buu/dev/bs/node_modules/pg-native/index.js:32:10)
    at emitTwo (events.js:106:13)
    at module.exports.emit (events.js:191:7)
    at _addListener (events.js:226:14)
    at module.exports.addListener (events.js:276:10)
    at /home/buu/dev/bs/node_modules/pg/lib/native/index.js:81:19

but if you delete the .native part of the require line, you no longer get an error.

rmzg avatar Jan 24 '17 02:01 rmzg

Have you ever found a solution to this?

abevalle avatar Jan 12 '21 21:01 abevalle

No, I haven't tried it since then.

rmzg avatar Jan 12 '21 21:01 rmzg

Happening too, under heavy load.

mmahut avatar Aug 28 '21 17:08 mmahut

Used this in pg-promise - was able to resolve this bug by removing pgNative: const pgp = pgLib({ pgNative: true }); ->const pgp = pgLib({});

2021-08-28T14:41:24: AssertionError [ERR_ASSERTION]: Must be connected to start reader
    at PQ.startReader (/nix/store/nkn6lgb3s5w34zv93y0a8hs1k3gmpqgz-myapp/libexec/source/node_modules/libpq/index.js:258:3)
    at module.exports.Client._startReading (/nix/store/nkn6lgb3s5w34zv93y0a8hs1k3gmpqgz-myapp/libexec/source/node_modules/pg-native/index.js:245:11)
    at module.exports.<anonymous> (/nix/store/nkn6lgb3s5w34zv93y0a8hs1k3gmpqgz-myapp/libexec/source/node_modules/pg-native/index.js:36:10)
    at module.exports.emit (events.js:315:20)
    at module.exports.EventEmitter.emit (domain.js:467:12)
    at _addListener (events.js:358:14)
    at module.exports.addListener (events.js:406:10)
    at /nix/store/nkn6lgb3s5w34zv93y0a8hs1k3gmpqgz-myapp/libexec/source/node_modules/pg/lib/native/client.js:103:19
App [server:1] exited with code [1] via signal [SIGINT]

the last line is from pm2, as this raises an uncaught exception that causes pm2 to crash and restart.

This happens only under heavy (hundreds of requests per second) load so it might be difficult to debug.

1000101 avatar Aug 29 '21 16:08 1000101

I've managed to reproduce this with pg.Pool The issue occured when I set the connectionTimeoutMillis to a too low number like 1000 and I initiated connection to the database in a for loop... When I increaed it to 60_000, the issue did not come up or if you set to 0 (default), then there is no timeout.

I suggest leaving the pg.Pool connectionTimeoutMillis on 0 (default value) or set a fairly high number like 60_000 (1 minute).

https://node-postgres.com/api/pool

wpatrik14 avatar Sep 21 '21 09:09 wpatrik14

I've managed to reproduce this with pg.Pool The issue occured when I set the connectionTimeoutMillis to a too low number like 1000 and I initiated connection to the database in a for loop... When I increaed it to 60_000, the issue did not come up or if you set to 0 (default), then there is no timeout.

I suggest leaving the pg.Pool connectionTimeoutMillis on 0 (default value) or set a fairly high number like 60_000 (1 minute).

https://node-postgres.com/api/pool

Thanks for the info! My connectionTimeoutMillis setting is currently 5000ms so this might be it. However, my host is remote and I'm worried about connections hanging indefinitely during intermittent network issues / lags.

1000101 avatar Sep 21 '21 11:09 1000101

We hit this as well. I can confirm that uninstalling pg-native or setting connectionTimeoutMillis from 5000 to 60000 (didn't try other values yet) both solved this for us (so far... 🙂 ).

version : 3.0.0

philwhln avatar Nov 10 '21 01:11 philwhln