Pg-promise: Time taken to acquire connection from connection pool

Created on 10 Jun 2016  路  5Comments  路  Source: vitaly-t/pg-promise

I'm looking for a way to log the amount of time it takes for a connection to be acquired from the connection pool so that I can get an idea of whether my pool size needs to be increased or not.

I was looking at the connect event, but there doesn't seem to be any properties that I can use there. Is there another way I can determine this?

question

All 5 comments

There is no special support for this. You can only do it as a separate test:

var start = Date.now();
db.connect()
    .then(obj=> {
        var duration = Date.now() - start;
        console.log(duration);
        obj.done();
    });

Ah, yeah that's what I was doing in the meantime. Would this be difficult to implement? Possible feature request for a future version?

It is possible, but nobody asked for it yet.

You know, you can always hack into the API directly, by overriding pgp.pg.connect and provide your version that would log the duration ;)

@RyanMcDonald

Here's how you can hack into the pg connection pool used by the library, to log the duration:

var prevConnect = pgp.pg.connect;

pgp.pg.connect = function (cn, cb) {
    var start = Date.now();
    return prevConnect.call(this, cn, function (err, client, done) {
        if (!err) {
            var duration = Date.now() - start;
            console.log(duration);
        }
        cb(err, client, done);
    });
};

@vitaly-t Beautiful, thank you! 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

leemhenson picture leemhenson  路  5Comments

ForbesLindesay picture ForbesLindesay  路  3Comments

illarionvk picture illarionvk  路  3Comments

realcarbonneau picture realcarbonneau  路  4Comments

cmelone picture cmelone  路  3Comments