New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
connection pointer is NULL #182
Comments
Is there anymore information you can provide? OS? Postgres version? Some example code of how you are connecting to the database? I have several apps that connect to Postgres manually (by creating a pg.Client object without connection pooling) for long periods of time and I have never had any problems with losing connection as this error seems to indicate. |
my app is running on nodejitsu, so it's on SmartOS with heroku's postgresql (v 9.1.3) code: this.client = new pg.Client(connStr); // connStr is a string like postgres://user:pwd@host/db
this.client.connect(); Thanks a lot :) |
Each time you create a pg.Client object you are creating a new connection to Postgres. If you do this for each request then you will eventually exhaust all your allowable connections to Postgres. You can instead use pg.connect() which will take advantage of connection pooling and reuse connections, by default it will only create a maximum of 10 connections. |
ooh i didn't know that.. thanks a lot :) i'll try and if that works i'll close the issue |
@yawnt please close this as this is not an actual bug/problem EDIT: sorry, didnt notice the last part there 😄 |
yeah the fact is that i don't create a connection for every request, but just once.. looking into pooling anyway @freewil |
uhm it looks like i create the client only once :( , so that's not the problem |
Are you trying to connect over SSL? See #170 |
I've seen this issue before a long time ago though I cannot remember what it was. Could you post a larger code sample? Fortunately, or unfortunately depending on how you look at it, I'm 99% sure it's due to your use of the library. Just to be sure, it does execute some queries before this issue happens, correct? |
yes, it does execute queries.. this is the whole code https://github.com/yawnt/hater/blob/master/lib/builders/postgresql.js |
have you had time to look at the code perhaps @brianc ? thanks |
I haven't had a chance to look at the code in detail within the module you're using. I would guess there is some custom connection pooling within the |
what i do is: i create a single client and then use that for every query i make (via a Query instance). thank you :) |
Perhaps heroku's using some addon to tell postgres to disconnect idle connections after a time? I think it could help to expose the underlying stream's Is there a way you can reproduce this locally on your machine without using the |
I'm seeing this as well. My env: pg: 0.8.6 Here is a small script to reproduce it: var pg = require('pg').native
var pgUrl = process.env.DATABASE_URL;
pg.defaults.poolSize = 5;
pg.defaults.poolIdleTimeout = 30000;
var delay = 1000;
function onConnect(err, client) {
if(err) throw err;
function run() {
console.log('%s Sleeping %sms', new Date(), delay);
setTimeout(function() {
try {
client.query('select * from docs;', [], function(err, docs) {
if(err) {
console.log('%s ASYNC ERROR', new Date(), err);
// Trying to connect to fix things causes a segfault.
pg.connect(pgUrl, onConnect);
} else {
delay = delay * 2;
run();
}
});
} catch(err) {
console.log('%s SYNC ERROR', new Date(), err);
}
}, delay);
}
run();
}
pg.connect(pgUrl, onConnect); This works fine in the beginning, but when the timeout reaches 32s I get an error. The attempt to reconnect causes a segfault.
I know that #61 suggests the use of |
By turning on logging I noticed that the connection is attempted released twice. This might be related to #192. Updated code: var pg = require('pg').native
var pgUrl = process.env.DATABASE_URL;
pg.defaults.poolSize = 5;
pg.defaults.poolIdleTimeout = 1000;
pg.defaults.poolLog = console.log;
var delay = 250;
function onConnect(err, client) {
if(err) throw err;
function run() {
console.log('%s Sleeping %sms', new Date(), delay);
setTimeout(function() {
try {
client.query('select * from docs;', [], function(err, docs) {
if(err) {
console.log('%s ASYNC ERROR', new Date(), err);
// Trying to connect to fix things causes a segfault.
pg.connect(pgUrl, onConnect);
} else {
delay = delay * 2;
run();
}
});
} catch(err) {
console.log('%s SYNC ERROR', new Date(), err);
}
}, delay);
}
run();
}
pg.connect(pgUrl, onConnect); Output:
|
If you want to use a single open client to run all your connections create the client manually via |
Also encountering this randomly on simple connect & insert (both local postgres 9.2 installation and remote heroku; require('pg').native). For subqueries and also this simple insert I'm using pauseDrain() as noted in issue 61. Here are the logs, with a successful connect & insert, then a failed one. On retry it will work. 17:33:01 web.1 | at callbacks (node_modules/express/lib/router/index.js:160:37) As a separate note/request, would it be possible to have a real-life code example, with node.js and express?
|
I've changed the pool substantially to no longer rely on the |
I have a web app that uses node-postgres and after a while i notice this error in the logs
and database calls are (obviously) impossible after that , therefore i need to restart the app.. is there a way to avoid this?
Thanks
The text was updated successfully, but these errors were encountered: