node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

Available clients in pool

Open ranode opened this issue 10 years ago • 3 comments

Is there a way to determine how many clients are currently "available" in the pool? I.e. not being used by the app.

ranode avatar Jul 23 '16 09:07 ranode

new pg.Pool().pool is an instance of generic pool and has the same public interface as described in the readme. You should be able to get available number of clients with pool.pool.availableObjectsCount()

brianc avatar Jul 24 '16 04:07 brianc

Oh boi, I know this is very very old 😅

I've a legacy project I'm trying to upgrade and I've hacky code like this:

    const pool = pg.pools.all['{}']; // default pool
    if (pool) {
      pgsql.allConnections = pool.getPoolSize();
      pgsql.connectionsQueued = pool.waitingClientsCount();
      pgsql.freeConnections = pool.availableObjectsCount();
    }

Now, I read the last comment but it seems that the current pg-pool is not based on node-pool anymore, at least I can't see anything in https://github.com/brianc/node-postgres/blob/f49db313c1a75a7679c467b9f1740ea70047a509/packages/pg-pool/package.json

When I look at the source, would deep-diving be appropriate in this case?

    pgsql.allConnections = pool._clients.length;
    pgsql.connectionsQueued = pool._pendingQueue.length;
    pgsql.freeConnections = pool._idle.length;

The basic idea is to provide some stats for recording metrics how the pool behaves.

Thanks!

mfn avatar Oct 13 '20 13:10 mfn

yup that looks correct! I should make some first class getters to return that info, really...but that'll work for now! Good luck with your upgrade & lmk if you have any other questions.

brianc avatar Oct 13 '20 14:10 brianc