From https://node-postgres.com/features/connecting , seems like we can choose between Pool
or Client
to perform query
pool.query('SELECT NOW()', (err, res) => { console.log(err, res) pool.end() })
client.query('SELECT NOW()', (err, res) => { console.log(err, res) client.end() })
Their functionalities look very much the same. But, the documentation doesn't explain much the difference between Pool
and Client
.
May I know, what thing I should consider, before choosing between Pool
or Client
?
You have two options that you can connect to a PostgreSQL server with the node-postgres module. One of the options is to use a single client. The other method is to use a connection pool. However, if your application is using the database very frequently, the pool will be a better option than using a single client.
Client is one static connection. Pool manages a dynamic list/pool of Client objects, with automatic re-connect functionality ;) Normally, you would just create a single Pool object and use it ;) – vitaly-t.
A node pool is a group of nodes within a cluster that all have the same configuration. Node pools use a NodeConfig specification. Each node in the pool has a Kubernetes node label, cloud.google.com/gke-nodepool , which has the node pool's name as its value.
May I know, what thing I should consider, before choosing between Pool or Client?
Use a pool if you have or expect to have multiple concurrent requests. That is literally what it is there for: to provide a pool of re-usable open client
instances (reduces latency whenever a client
can be reused).
In that case you definitely do not want to call pool.end()
when your query completes, you want to reserve that for when your application terminates because pool.end()
disposes of all the open client
instances. (Remember, the point is to keep up to a fixed number of client
instances available.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With