brianc / node-pg-pool

A connection pool for node-postgres
MIT License
180 stars 64 forks source link

Is there a difference between pool.query() VS connect(), query(), release() #120

Closed gajus closed 5 years ago

gajus commented 5 years ago

I have a design problem in Slonik.

The beforeQueryExecution interceptor requires to know the connectionId.

connectionId is being assigned by Slonik at the time of new connection.

The problem is that if query is initiated pool.query() way, then the connection is not yet acquired at the time the beforeQueryExecution is called.

https://github.com/gajus/slonik/blob/f20b1b45a586039c85e178e0f6f5a3e84d2cd48d/src/connectionMethods/query.js#L89

Is there an overhead for doing connect(), query(), release() VS pool.query()?

This would allow me to wrap every pool method with connection logic.

gajus commented 5 years ago

Either way, this looks like the only solution (logically).

https://github.com/gajus/slonik/commit/6a011cbbe39e382904c7131af484eb540869af66

sehrope commented 5 years ago

Internally pool.query(...) calls pool.connect(...) and client.release(...) with a wrapped callback to invoke the user's query upon successful acquisition of a connection: https://github.com/brianc/node-pg-pool/blob/master/index.js#L289-L304

It's a convenience wrapper so no different than you doing it yourself. That'd be the correct approach if you want to do something with the connection prior to executing the actual query (ex: SET ROLE ... or change some server setting).