Skip to content

Commit 71a1364

Browse files
javierturybrianc
authored andcommitted
Improve Readme.md for not so advanced users (brianc#1235)
* Improve Readme.md for not so advanced users 1. Add brief description about the 3 possible ways of executing queries: passing the query to a pool, borrowing a client from a pool or obtaining an exclusive client. Give examples for the 3 of them. 2. Use the examples to teach how to reuse a pool in all of your project. This should be helpful for not so advanced users and prevents mistakes. 3. Open a troubleshooting section. * Shrink Troubleshooting and Point to Examples 1. Troubleshooting/FAQ section will only contain a reference to the wiki FAQ. I've already moved the content to the wiki. 2. At the end of "Pooling example" point to the wiki example page. Also indicate that there they can find how to use node-postgres with promises and async/await. I've already created that content in the wiki.
1 parent 7504c20 commit 71a1364

File tree

1 file changed

+96
-45
lines changed

1 file changed

+96
-45
lines changed

README.md

+96-45
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,36 @@ $ npm install pg
1515

1616
## Intro & Examples
1717

18-
### Simple example
18+
There are 3 ways of executing queries
1919

20-
```js
21-
var pg = require('pg');
20+
1. Passing the query to a pool
21+
2. Borrowing a client from a pool and executing the query with it
22+
3. Obtaining an exclusive client and executing the query with it
2223

23-
// instantiate a new client
24-
// the client will read connection information from
25-
// the same environment variables used by postgres cli tools
26-
var client = new pg.Client();
24+
It is recommended to pass the query to a pool as often as possible. If that isn't possible, because of long and complex transactions for example, borrow a client from a pool. Just remember to initialize the pool only once in your code so you maximize reusability of connections.
2725

28-
// connect to our database
29-
client.connect(function (err) {
30-
if (err) throw err;
26+
### Why pooling?
3127

32-
// execute a query on our database
33-
client.query('SELECT $1::text as name', ['brianc'], function (err, result) {
34-
if (err) throw err;
28+
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 support only 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.
3529

36-
// just print the result to the console
37-
console.log(result.rows[0]); // outputs: { name: 'brianc' }
30+
With that in mind we can imagine a situation 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!
3831

39-
// disconnect the client
40-
client.end(function (err) {
41-
if (err) throw err;
42-
});
43-
});
44-
});
32+
__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:
4533

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

48-
### Client pooling
36+
It's __highly recommended__ you read the documentation for [pg-pool](https://github.com/brianc/node-pg-pool.git).
4937

50-
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 support only 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.
38+
[Here is an up & running quickly example](https://github.com/brianc/node-postgres/wiki/Example)
5139

52-
With that in mind we can imagine a situation 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!
40+
For more information about `config.ssl` check [TLS (SSL) of nodejs](https://nodejs.org/dist/latest-v4.x/docs/api/tls.html)
5341

54-
__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:
42+
### Pooling example
43+
44+
Let's create a pool in `./lib/db.js` which will be reused across the whole project
5545

5646
```javascript
57-
var pg = require('pg');
47+
const pg = require('pg')
5848

5949
// create a config to configure both pooling behavior
6050
// and client options
@@ -70,18 +60,63 @@ var config = {
7060
idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
7161
};
7262

73-
7463
//this initializes a connection pool
7564
//it will keep idle connections open for 30 seconds
7665
//and set a limit of maximum 10 idle clients
77-
var pool = new pg.Pool(config);
66+
const pool = new pg.Pool(config);
7867

79-
// to run a query we can acquire a client from the pool,
80-
// run a query on the client, and then return the client to the pool
68+
pool.on('error', function (err, client) {
69+
// if an error is encountered by a client while it sits idle in the pool
70+
// the pool itself will emit an error event with both the error and
71+
// the client which emitted the original error
72+
// this is a rare occurrence but can happen if there is a network partition
73+
// between your application and the database, the database restarts, etc.
74+
// and so you might want to handle it and at least log it out
75+
console.error('idle client error', err.message, err.stack)
76+
})
77+
78+
//export the query method for passing queries to the pool
79+
module.exports.query = function (text, values, callback) {
80+
console.log('query:', text, values);
81+
return pool.query(text, values, callback);
82+
};
83+
84+
// the pool also supports checking out a client for
85+
// multiple operations, such as a transaction
86+
module.exports.connect = function (callback) {
87+
return pool.connect(callback);
88+
};
89+
```
90+
91+
Now if in `./foo.js` you want to pass a query to the pool
92+
93+
```js
94+
const pool = require('./lib/db');
95+
96+
//to run a query we just pass it to the pool
97+
//after we're done nothing has to be taken care of
98+
//we don't have to return any client to the pool or close a connection
99+
pool.query('SELECT $1::int AS number', ['2'], function(err, res) {
100+
if(err) {
101+
return console.error('error running query', err);
102+
}
103+
104+
console.log('number:', res.rows[0].number);
105+
});
106+
```
107+
108+
Or if in `./bar.js` you want borrow a client from the pool
109+
110+
```js
111+
const pool = require('./lib/db');
112+
113+
//ask for a client from the pool
81114
pool.connect(function(err, client, done) {
82115
if(err) {
83116
return console.error('error fetching client from pool', err);
84117
}
118+
119+
//use the client for executing the query
85120
client.query('SELECT $1::int AS number', ['1'], function(err, result) {
86121
//call `done(err)` to release the client back to the pool (or destroy it if there is an error)
87122
done(err);
@@ -93,27 +128,39 @@ pool.connect(function(err, client, done) {
93128
//output: 1
94129
});
95130
});
96-
97-
pool.on('error', function (err, client) {
98-
// if an error is encountered by a client while it sits idle in the pool
99-
// the pool itself will emit an error event with both the error and
100-
// the client which emitted the original error
101-
// this is a rare occurrence but can happen if there is a network partition
102-
// between your application and the database, the database restarts, etc.
103-
// and so you might want to handle it and at least log it out
104-
console.error('idle client error', err.message, err.stack)
105-
})
106131
```
107132

108-
node-postgres uses [pg-pool](https://github.com/brianc/node-pg-pool.git) to manage pooling. It bundles it and exports it for convenience. If you want, you can `require('pg-pool')` and use it directly - it's the same as the constructor exported at `pg.Pool`.
133+
For more examples, including how to use a connection pool with promises and async/await see the [example](https://github.com/brianc/node-postgres/wiki/Example) page in the wiki.
109134

110-
It's __highly recommended__ you read the documentation for [pg-pool](https://github.com/brianc/node-pg-pool.git).
135+
### Obtaining an exclusive client, example
111136

137+
```js
138+
var pg = require('pg');
112139

113-
[Here is an up & running quickly example](https://github.com/brianc/node-postgres/wiki/Example)
140+
// instantiate a new client
141+
// the client will read connection information from
142+
// the same environment variables used by postgres cli tools
143+
var client = new pg.Client();
114144

145+
// connect to our database
146+
client.connect(function (err) {
147+
if (err) throw err;
115148

116-
For more information about `config.ssl` check [TLS (SSL) of nodejs](https://nodejs.org/dist/latest-v4.x/docs/api/tls.html)
149+
// execute a query on our database
150+
client.query('SELECT $1::text as name', ['brianc'], function (err, result) {
151+
if (err) throw err;
152+
153+
// just print the result to the console
154+
console.log(result.rows[0]); // outputs: { name: 'brianc' }
155+
156+
// disconnect the client
157+
client.end(function (err) {
158+
if (err) throw err;
159+
});
160+
});
161+
});
162+
163+
```
117164

118165
## [More Documentation](https://github.com/brianc/node-postgres/wiki)
119166

@@ -183,6 +230,10 @@ Information about the testing processes is in the [wiki](https://github.com/bria
183230

184231
Open source belongs to all of us, and we're all invited to participate!
185232

233+
## Troubleshooting and FAQ
234+
235+
The causes and solutions to common errors can be found among the [Frequently Asked Questions(FAQ)](https://github.com/brianc/node-postgres/wiki/FAQ)
236+
186237
## Support
187238

188239
If at all possible when you open an issue please provide

0 commit comments

Comments
 (0)