Skip to content

Commit 1bc50f1

Browse files
authored
Update README.md
1 parent 812277f commit 1bc50f1

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

README.md

+19-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var pg = require('pg');
2323
// the same environment varaibles used by postgres cli tools
2424
var client = new pg.Client();
2525

26-
// connect to our PostgreSQL server instance
26+
// connect to our database
2727
client.connect(function (err) {
2828
if (err) throw err;
2929

@@ -45,13 +45,11 @@ client.connect(function (err) {
4545

4646
### Client pooling
4747

48-
If you're working on something like a web application which makes frequent queries you'll want to access the PostgreSQL server through a pool of clients. Why? There is ~20-30 millisecond delay (YMMV) when connecting a new client to the PostgreSQL server because of the startup handshake. Also, PostgreSQL can only support a limited number of clients...it depends on the amount of ram on your database server, but generally more than 100 clients at a time is a bad thing. :tm: Finally
49-
PostgreSQL can only execute 1 query at a time per connected client.
48+
If you're working on something like a web application which makes frequent queries you'll want to access the PostgreSQL server through a pool of clients. Why? For one thing, there is ~20-30 millisecond delay (YMMV) when connecting a new client to the PostgreSQL server because of the startup handshake. Furthermore, PostgreSQL can only support a limited number of clients...it depends on the amount of ram on your database server, but generally more than 100 clients at a time is a __very bad thing__. :tm: Additionally, PostgreSQL can only execute 1 query at a time per connected client, so pipelining all queries for all requests through a single, long-lived client will likely introduce a bottleneck into your application if you need high concurrency.
5049

51-
With that in mind we can imagine a situtation where you have a web server which connects and disconnects a new client for every web request or every query (don't do this!). If you get only 1 request at a time everything will seem to work fine, though it will be a touch slower due to the connection overhead. Once you get >500 simultaneous requests your web server will attempt to open 500 connections to the PostgreSQL backend and :boom: you'll run out of memory on the PostgreSQL server, it will become
52-
unresponsive, your app will seem to hang, and everything will break. Boooo!
50+
With that in mind we can imagine a situtation where you have a web server which connects and disconnects a new client for every web request or every query (don't do this!). If you get only 1 request at a time everything will seem to work fine, though it will be a touch slower due to the connection overhead. Once you get >100 simultaneous requests your web server will attempt to open 100 connections to the PostgreSQL backend and :boom: you'll run out of memory on the PostgreSQL server, your database will become unresponsive, your app will seem to hang, and everything will break. Boooo!
5351

54-
__Good news__: node-postgres ships with built in client pooling.
52+
__Good news__: node-postgres ships with built in client pooling. Client pooling allows your application to use a pool of already connected clients and reuse them for each request to your application. If your app needs to make more queries than there are available clients in the pool the queries will queue instead of overwhelming your database & causing a cascading failure. :thumbsup:
5553

5654
```javascript
5755
var pg = require('pg');
@@ -69,11 +67,14 @@ var config = {
6967
idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
7068
};
7169

72-
var pool = new pg.Pool(config);
7370

7471
//this initializes a connection pool
7572
//it will keep idle connections open for a 30 seconds
7673
//and set a limit of maximum 10 idle clients
74+
var pool = new pg.Pool(config);
75+
76+
// to run a query we can acquire a client from the pool,
77+
// run a query on the client, and then return the client to the pool
7778
pool.connect(function(err, client, done) {
7879
if(err) {
7980
return console.error('error fetching client from pool', err);
@@ -89,9 +90,19 @@ pool.connect(function(err, client, done) {
8990
//output: 1
9091
});
9192
});
93+
94+
pool.on('error', function (err, client) {
95+
// if an error is encountered by a client while it sits idle in the pool
96+
// the pool itself will emit an error event with both the error and
97+
// the client which emitted the original error
98+
// this is a rare occurrence but can happen if there is a network partition
99+
// between your application and the database, the database restarts, etc
100+
// and so you might want to handle it and at least log it out
101+
console.error('idle client error', err.message, err.stack)
102+
})
92103
```
93104

94-
node-postgres uses [pg-pool](https://github.com/brianc/node-pg-pool.git) to manage pooling and includes it and exports it for convienience. If you want, you can `require('pg-pool')` and use it directly - its the same as the constructor exported at `pg.Pool`.
105+
node-postgres uses [pg-pool](https://github.com/brianc/node-pg-pool.git) to manage pooling. It bundles it and exports it for convienience. If you want, you can `require('pg-pool')` and use it directly - its the same as the constructor exported at `pg.Pool`.
95106

96107
It's __highly recommend__ you read the documentation for [pg-pool](https://github.com/brianc/node-pg-pool.git).
97108

0 commit comments

Comments
 (0)