PQsendQuery returned error code #81

Closed
tmcw opened this Issue Dec 22, 2011 · 7 comments

6 participants

@tmcw

This is more or less a re-open of #48, since the error is still occurring. It seems pretty clear, since binding.cc throws when it gets a error result code. Mainly I guess it's more of a question of API, and this seems like a mistake - since the behavior should be to continue to call the callback with an error parameter, and to use PQerrorMessage to get a more descriptive error message.

I might be able to hack up a pull request fixing this, because the error's pretty reproducible from my code.

@brianc
Owner

If you could provide a simple piece of code to reproduce this I'd gladly fix it.

@attributor

I am constantly encountering this problem and was able to reproduce it pretty reliably - after approx. 40 sec, it either:

  • hangs, or
  • dumps "PQsendQuery returned error code" and the client connection is useless from that point on.

I'm running against postgres 9.1.2 on a RedHat 6.2 system.

Here is the code to reproduce it:

#!/usr/bin/env node

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

var conString = "tcp://someuser:somepassword@localhost/template1";

pg.connect(conString, function(err, client) {
    console.log("start");
    var sql = "SELECT * FROM generate_series(1, 1000)";
    var started = 0;
    setInterval(function() {
        var current = ++started;
        client.query(sql, function(err, result) {
            if (err)
                console.log(err);
            else
                console.log((new Date()).toISOString() + " current=" + current + " started=" + start\
ed + " delta=" + (started - current));
        });
    }, 5);
});

I tried to see if has anything to do with running queries in "parallel" - by adjusting the interval time so that delta > 1 in the printout.
It doesn't matter - even if queries run all in sequence it happens after approx 40 sec on my system.

@kongelaks

I was consistently receiving this error when using pg (native) and the asynchronous connect function. However, when I began using the synchronous connect function and constructor, the error stopped.

var client = new pg.Client(conString);
client.connect();

Instead of

pg.connect(conString, function(err, client) {
});

PostgreSQL 9.0.7 on i486-pc-linux-gnu, compiled by GCC gcc-4.4.real (Ubuntu 4.4.3-4ubuntu5) 4.4.3, 32-bit
Node v0.6.14
pg 0.6.17

@gwil

+1 to the solution kongelaks provided.

@strk

The workaround worked for me too

@strk

I only now realized that the "workaround" actually changed the semantic of the call, whereas pg.connect uses a connection pooler while new pg.Client doesn't. Just in case others fall into this.

@brianc
Owner

yeah you need to use pauseDrain() / resumeDrain() if you're going to use pooled clients in a timeout. They automatically return themselves to the pool when their query queue's drain.

See this for more information:

#137

We're still working on the edge cases of this. The pooling of clients is pretty confusing to use in an callback based system.

@brianc brianc closed this Sep 4, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment