Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: brianc/node-postgres
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v6.1.6
Choose a base ref
...
head repository: brianc/node-postgres
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v6.2.2
Choose a head ref
  • 11 commits
  • 8 files changed
  • 7 contributors

Commits on Mar 28, 2017

  1. Fix README.md (#1250)

    iRockyZhou authored and brianc committed Mar 28, 2017
    Copy the full SHA
    7504c20 View commit details
  2. Improve Readme.md for not so advanced users (#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.
    javiertury authored and brianc committed Mar 28, 2017
    Copy the full SHA
    71a1364 View commit details

Commits on Apr 17, 2017

  1. Add semicolons to Pooling example in README.md (#1266)

    Kenneth Schnall authored and brianc committed Apr 17, 2017
    Copy the full SHA
    0e2625b View commit details

Commits on Apr 19, 2017

  1. support ssl params for pg-native (#1169)

    Make pg-native able to pass sslmode, sslca, sslkey and sslcert params to libpq
    arypurnomoz authored and brianc committed Apr 19, 2017
    Copy the full SHA
    4505ae9 View commit details

Commits on Apr 24, 2017

  1. Support for logical streaming replication (#1271)

    * Support for logical streaming replication
    
    * Wrong compare expr in getLibpqConnectionString
    
    * Simplify codes for replication parameter
    Kibae Shin authored and brianc committed Apr 24, 2017
    Copy the full SHA
    4f790de View commit details
  2. Copy the full SHA
    80d136a View commit details
  3. Bump version

    brianc committed Apr 24, 2017
    Copy the full SHA
    f42924b View commit details

Commits on May 15, 2017

  1. Upgrade packet reader (#1287)

    brianc authored May 15, 2017
    Copy the full SHA
    db5f4ae View commit details
  2. Bump version

    brianc committed May 15, 2017
    Copy the full SHA
    4659d5d View commit details
  3. Libpq connection string escaping (#1285)

    * Fix escaping of libpq connection string properties
    
    Fix handlings of libpq connection properties to properly escape single
    quotes and backslashes. Previously the values were surrounded in single
    quotes which handled whitespace within the property value, but internal
    single quotes and backslashes would cause invalid connection strings to
    be generated.
    
    * Update expected output in test to be quoted
    
    Update the expect host output in the connection parameter test
    to expect it to be surrounded by single quotes.
    
    * Add test for configs with quotes and backslashes
    sehrope authored and brianc committed May 15, 2017
    Copy the full SHA
    ee81936 View commit details
  4. Bump version

    brianc committed May 15, 2017
    Copy the full SHA
    c32316d View commit details
Showing with 156 additions and 57 deletions.
  1. +4 −0 CHANGELOG.md
  2. +97 −46 README.md
  3. +4 −0 lib/client.js
  4. +20 −5 lib/connection-parameters.js
  5. +3 −0 lib/connection.js
  6. +2 −2 package.json
  7. +19 −3 test/unit/connection-parameters/creation-tests.js
  8. +7 −1 test/unit/connection/inbound-parser-tests.js
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,10 @@ For richer information consult the commit log on github with referenced pull req

We do not include break-fix version release in this file.

### v6.2.0

- Add support for [parsing `replicationStart` messages](https://github.com/brianc/node-postgres/pull/1271/files).

### v6.1.0

- Add optional callback parameter to the pure JavaScript `client.end` method. The native client already supported this.
143 changes: 97 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#node-postgres
# node-postgres

[![Build Status](https://secure.travis-ci.org/brianc/node-postgres.svg?branch=master)](http://travis-ci.org/brianc/node-postgres)
[![Dependency Status](https://david-dm.org/brianc/node-postgres.svg)](https://david-dm.org/brianc/node-postgres)
@@ -15,46 +15,36 @@ $ npm install pg

## Intro & Examples

### Simple example
There are 3 ways of executing queries

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

// instantiate a new client
// the client will read connection information from
// the same environment variables used by postgres cli tools
var client = new pg.Client();
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.

// connect to our database
client.connect(function (err) {
if (err) throw err;
### Why pooling?

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

// just print the result to the console
console.log(result.rows[0]); // outputs: { name: 'brianc' }
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!

// disconnect the client
client.end(function (err) {
if (err) throw err;
});
});
});
__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:

```
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`.

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

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

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!
For more information about `config.ssl` check [TLS (SSL) of nodejs](https://nodejs.org/dist/latest-v4.x/docs/api/tls.html)

__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:
### Pooling example

Let's create a pool in `./lib/db.js` which will be reused across the whole project

```javascript
var pg = require('pg');
const pg = require('pg');

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


//this initializes a connection pool
//it will keep idle connections open for 30 seconds
//and set a limit of maximum 10 idle clients
var pool = new pg.Pool(config);
const pool = new pg.Pool(config);

pool.on('error', function (err, client) {
// if an error is encountered by a client while it sits idle in the pool
// the pool itself will emit an error event with both the error and
// the client which emitted the original error
// this is a rare occurrence but can happen if there is a network partition
// between your application and the database, the database restarts, etc.
// and so you might want to handle it and at least log it out
console.error('idle client error', err.message, err.stack);
});

//export the query method for passing queries to the pool
module.exports.query = function (text, values, callback) {
console.log('query:', text, values);
return pool.query(text, values, callback);
};

// the pool also supports checking out a client for
// multiple operations, such as a transaction
module.exports.connect = function (callback) {
return pool.connect(callback);
};
```

Now if in `./foo.js` you want to pass a query to the pool

// to run a query we can acquire a client from the pool,
// run a query on the client, and then return the client to the pool
```js
const pool = require('./lib/db');

//to run a query we just pass it to the pool
//after we're done nothing has to be taken care of
//we don't have to return any client to the pool or close a connection
pool.query('SELECT $1::int AS number', ['2'], function(err, res) {
if(err) {
return console.error('error running query', err);
}

console.log('number:', res.rows[0].number);
});
```

Or if in `./bar.js` you want borrow a client from the pool

```js
const pool = require('./lib/db');

//ask for a client from the pool
pool.connect(function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}

//use the client for executing the query
client.query('SELECT $1::int AS number', ['1'], function(err, result) {
//call `done(err)` to release the client back to the pool (or destroy it if there is an error)
done(err);
@@ -93,27 +128,39 @@ pool.connect(function(err, client, done) {
//output: 1
});
});

pool.on('error', function (err, client) {
// if an error is encountered by a client while it sits idle in the pool
// the pool itself will emit an error event with both the error and
// the client which emitted the original error
// this is a rare occurrence but can happen if there is a network partition
// between your application and the database, the database restarts, etc.
// and so you might want to handle it and at least log it out
console.error('idle client error', err.message, err.stack)
})
```

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`.
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.

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

```js
var pg = require('pg');

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

// connect to our database
client.connect(function (err) {
if (err) throw err;

// execute a query on our database
client.query('SELECT $1::text as name', ['brianc'], function (err, result) {
if (err) throw err;

For more information about `config.ssl` check [TLS (SSL) of nodejs](https://nodejs.org/dist/latest-v4.x/docs/api/tls.html)
// just print the result to the console
console.log(result.rows[0]); // outputs: { name: 'brianc' }

// disconnect the client
client.end(function (err) {
if (err) throw err;
});
});
});

```

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

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

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

## Troubleshooting and FAQ

The causes and solutions to common errors can be found among the [Frequently Asked Questions(FAQ)](https://github.com/brianc/node-postgres/wiki/FAQ)

## Support

If at all possible when you open an issue please provide
4 changes: 4 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ var Client = function(config) {
this.port = this.connectionParameters.port;
this.host = this.connectionParameters.host;
this.password = this.connectionParameters.password;
this.replication = this.connectionParameters.replication;

var c = config || {};

@@ -222,6 +223,9 @@ Client.prototype.getStartupConf = function() {
if (appName) {
data.application_name = appName;
}
if (params.replication) {
data.replication = '' + params.replication;
}

return data;
};
25 changes: 20 additions & 5 deletions lib/connection-parameters.js
Original file line number Diff line number Diff line change
@@ -57,17 +57,23 @@ var ConnectionParameters = function(config) {
this.binary = val('binary', config);
this.ssl = typeof config.ssl === 'undefined' ? useSsl() : config.ssl;
this.client_encoding = val("client_encoding", config);
this.replication = val("replication", config);
//a domain socket begins with '/'
this.isDomainSocket = (!(this.host||'').indexOf('/'));

this.application_name = val('application_name', config, 'PGAPPNAME');
this.fallback_application_name = val('fallback_application_name', config, false);
};

// Convert arg to a string, surround in single quotes, and escape single quotes and backslashes
var quoteParamValue = function(value) {
return "'" + ('' + value).replace(/\\/g, "\\\\").replace(/'/g, "\\'") + "'";
};

var add = function(params, config, paramName) {
var value = config[paramName];
if(value) {
params.push(paramName+"='"+value+"'");
params.push(paramName + "=" + quoteParamValue(value));
}
};

@@ -79,21 +85,30 @@ ConnectionParameters.prototype.getLibpqConnectionString = function(cb) {
add(params, this, 'application_name');
add(params, this, 'fallback_application_name');

var ssl = typeof this.ssl === 'object' ? this.ssl : {sslmode: this.ssl};
add(params, ssl, 'sslmode');
add(params, ssl, 'sslca');
add(params, ssl, 'sslkey');
add(params, ssl, 'sslcert');

if(this.database) {
params.push("dbname='" + this.database + "'");
params.push("dbname=" + quoteParamValue(this.database));
}
if(this.replication) {
params.push("replication=" + quoteParamValue(this.replication));
}
if(this.host) {
params.push("host=" + this.host);
params.push("host=" + quoteParamValue(this.host));
}
if(this.isDomainSocket) {
return cb(null, params.join(' '));
}
if(this.client_encoding) {
params.push("client_encoding='" + this.client_encoding + "'");
params.push("client_encoding=" + quoteParamValue(this.client_encoding));
}
dns.lookup(this.host, function(err, address) {
if(err) return cb(err, null);
params.push("hostaddr=" + address);
params.push("hostaddr=" + quoteParamValue(address));
return cb(null, params.join(' '));
});
};
3 changes: 3 additions & 0 deletions lib/connection.js
Original file line number Diff line number Diff line change
@@ -425,6 +425,9 @@ Connection.prototype.parseMessage = function(buffer) {
case 0x48: //H
return this.parseH(buffer, length);

case 0x57: //W
return new Message('replicationStart', length);

case 0x63: //c
return new Message('copyDone', length);

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg",
"version": "6.1.5",
"version": "6.2.2",
"description": "PostgreSQL client - pure javascript & libpq with the same API",
"keywords": [
"postgres",
@@ -19,7 +19,7 @@
"main": "./lib",
"dependencies": {
"buffer-writer": "1.0.1",
"packet-reader": "0.2.0",
"packet-reader": "0.3.1",
"pg-connection-string": "0.1.3",
"pg-pool": "1.*",
"pg-types": "1.*",
22 changes: 19 additions & 3 deletions test/unit/connection-parameters/creation-tests.js
Original file line number Diff line number Diff line change
@@ -126,7 +126,7 @@ test('libpq connection string building', function() {
checkForPart(parts, "user='brian'");
checkForPart(parts, "password='xyz'");
checkForPart(parts, "port='888'");
checkForPart(parts, "hostaddr=127.0.0.1");
checkForPart(parts, "hostaddr='127.0.0.1'");
checkForPart(parts, "dbname='bam'");
}));
});
@@ -143,7 +143,7 @@ test('libpq connection string building', function() {
assert.isNull(err);
var parts = constring.split(" ");
checkForPart(parts, "user='brian'");
checkForPart(parts, "hostaddr=127.0.0.1");
checkForPart(parts, "hostaddr='127.0.0.1'");
}));
});

@@ -173,7 +173,23 @@ test('libpq connection string building', function() {
assert.isNull(err);
var parts = constring.split(" ");
checkForPart(parts, "user='brian'");
checkForPart(parts, "host=/tmp/");
checkForPart(parts, "host='/tmp/'");
}));
});

test('config contains quotes and backslashes', function() {
var config = {
user: 'not\\brian',
password: 'bad\'chars',
port: 5432,
host: '/tmp/'
};
var subject = new ConnectionParameters(config);
subject.getLibpqConnectionString(assert.calls(function(err, constring) {
assert.isNull(err);
var parts = constring.split(" ");
checkForPart(parts, "user='not\\\\brian'");
checkForPart(parts, "password='bad\\'chars'");
}));
});

8 changes: 7 additions & 1 deletion test/unit/connection/inbound-parser-tests.js
Original file line number Diff line number Diff line change
@@ -347,6 +347,13 @@ test('Connection', function() {
name: 'portalSuspended'
});
});

test('parses replication start message', function() {
testForMessage(new Buffer([0x57, 0x00, 0x00, 0x00, 0x04]), {
name: 'replicationStart',
length: 4
});
});
});

//since the data message on a stream can randomly divide the incomming
@@ -465,5 +472,4 @@ test('split buffer, multiple message parsing', function() {
splitAndVerifyTwoMessages(1);
});
});

});