brianc / node-pg-native

Native (C/C++) bindings to PostgreSQL with sync and async options.
247 stars 44 forks source link

Code throws error under pg-native but not pg #49

Open rmzg opened 7 years ago

rmzg commented 7 years ago
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.

abevalle commented 3 years ago

Have you ever found a solution to this?

rmzg commented 3 years ago

No, I haven't tried it since then.

mmahut commented 3 years ago

Happening too, under heavy load.

1000101 commented 3 years ago

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.

wpatrik14 commented 3 years ago

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

1000101 commented 3 years ago

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.

philwhln commented 3 years ago

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