source: node_modules/pg-pool/README.md

main
Last change on this file was 62b2964, checked in by Klimentina Efremova <klimentina08642@…>, 13 days ago

Project Handcraft Marketplace

  • Property mode set to 100644
File size: 12.8 KB
Line 
1# pg-pool
2
3[![Build Status](https://travis-ci.org/brianc/node-pg-pool.svg?branch=master)](https://travis-ci.org/brianc/node-pg-pool)
4
5A connection pool for node-postgres
6
7## install
8
9```sh
10npm i pg-pool pg
11```
12
13## use
14
15### create
16
17to use pg-pool you must first create an instance of a pool
18
19```js
20const Pool = require('pg-pool')
21
22// by default the pool uses the same
23// configuration as whatever `pg` version you have installed
24const pool = new Pool()
25
26// you can pass properties to the pool
27// these properties are passed unchanged to both the node-postgres Client constructor
28// and the pool constructor, allowing you to fully configure the behavior of both
29const pool2 = new Pool({
30 database: 'postgres',
31 user: 'brianc',
32 password: 'secret!',
33 port: 5432,
34 ssl: true,
35 max: 20, // set pool max size to 20
36 idleTimeoutMillis: 1000, // close idle clients after 1 second
37 connectionTimeoutMillis: 1000, // return an error after 1 second if connection could not be established
38 maxUses: 7500, // close (and replace) a connection after it has been used 7500 times (see below for discussion)
39})
40
41// you can supply a custom client constructor
42// if you want to use the native postgres client
43const NativeClient = require('pg').native.Client
44const nativePool = new Pool({ Client: NativeClient })
45
46// you can even pool pg-native clients directly
47const PgNativeClient = require('pg-native')
48const pgNativePool = new Pool({ Client: PgNativeClient })
49```
50
51##### Note:
52
53The Pool constructor does not support passing a Database URL as the parameter. To use pg-pool on heroku, for example, you need to parse the URL into a config object. Here is an example of how to parse a Database URL.
54
55```js
56const Pool = require('pg-pool')
57const url = require('url')
58
59const params = url.parse(process.env.DATABASE_URL)
60const auth = params.auth.split(':')
61
62const config = {
63 user: auth[0],
64 password: auth[1],
65 host: params.hostname,
66 port: params.port,
67 database: params.pathname.split('/')[1],
68 ssl: true,
69}
70
71const pool = new Pool(config)
72
73/*
74 Transforms, 'postgres://DBuser:secret@DBHost:#####/myDB', into
75 config = {
76 user: 'DBuser',
77 password: 'secret',
78 host: 'DBHost',
79 port: '#####',
80 database: 'myDB',
81 ssl: true
82 }
83*/
84```
85
86### acquire clients with a promise
87
88pg-pool supports a fully promise-based api for acquiring clients
89
90```js
91const pool = new Pool()
92pool.connect().then((client) => {
93 client
94 .query('select $1::text as name', ['pg-pool'])
95 .then((res) => {
96 client.release()
97 console.log('hello from', res.rows[0].name)
98 })
99 .catch((e) => {
100 client.release()
101 console.error('query error', e.message, e.stack)
102 })
103})
104```
105
106### plays nice with async/await
107
108this ends up looking much nicer if you're using [co](https://github.com/tj/co) or async/await:
109
110```js
111// with async/await
112;(async () => {
113 const pool = new Pool()
114 const client = await pool.connect()
115 try {
116 const result = await client.query('select $1::text as name', ['brianc'])
117 console.log('hello from', result.rows[0])
118 } finally {
119 client.release()
120 }
121})().catch((e) => console.error(e.message, e.stack))
122
123// with co
124co(function* () {
125 const client = yield pool.connect()
126 try {
127 const result = yield client.query('select $1::text as name', ['brianc'])
128 console.log('hello from', result.rows[0])
129 } finally {
130 client.release()
131 }
132}).catch((e) => console.error(e.message, e.stack))
133```
134
135### your new favorite helper method
136
137because its so common to just run a query and return the client to the pool afterward pg-pool has this built-in:
138
139```js
140const pool = new Pool()
141const time = await pool.query('SELECT NOW()')
142const name = await pool.query('select $1::text as name', ['brianc'])
143console.log(name.rows[0].name, 'says hello at', time.rows[0].now)
144```
145
146you can also use a callback here if you'd like:
147
148```js
149const pool = new Pool()
150pool.query('SELECT $1::text as name', ['brianc'], function (err, res) {
151 console.log(res.rows[0].name) // brianc
152})
153```
154
155**pro tip:** unless you need to run a transaction (which requires a single client for multiple queries) or you
156have some other edge case like [streaming rows](https://github.com/brianc/node-pg-query-stream) or using a [cursor](https://github.com/brianc/node-pg-cursor)
157you should almost always just use `pool.query`. Its easy, it does the right thing :tm:, and wont ever forget to return
158clients back to the pool after the query is done.
159
160### drop-in backwards compatible
161
162pg-pool still and will always support the traditional callback api for acquiring a client. This is the exact API node-postgres has shipped with for years:
163
164```js
165const pool = new Pool()
166pool.connect((err, client, done) => {
167 if (err) return done(err)
168
169 client.query('SELECT $1::text as name', ['pg-pool'], (err, res) => {
170 done()
171 if (err) {
172 return console.error('query error', err.message, err.stack)
173 }
174 console.log('hello from', res.rows[0].name)
175 })
176})
177```
178
179### shut it down
180
181When you are finished with the pool if all the clients are idle the pool will close them after `config.idleTimeoutMillis` and your app
182will shutdown gracefully. If you don't want to wait for the timeout you can end the pool as follows:
183
184```js
185const pool = new Pool()
186const client = await pool.connect()
187console.log(await client.query('select now()'))
188client.release()
189await pool.end()
190```
191
192### a note on instances
193
194The pool should be a **long-lived object** in your application. Generally you'll want to instantiate one pool when your app starts up and use the same instance of the pool throughout the lifetime of your application. If you are frequently creating a new pool within your code you likely don't have your pool initialization code in the correct place. Example:
195
196```js
197// assume this is a file in your program at ./your-app/lib/db.js
198
199// correct usage: create the pool and let it live
200// 'globally' here, controlling access to it through exported methods
201const pool = new pg.Pool()
202
203// this is the right way to export the query method
204module.exports.query = (text, values) => {
205 console.log('query:', text, values)
206 return pool.query(text, values)
207}
208
209// this would be the WRONG way to export the connect method
210module.exports.connect = () => {
211 // notice how we would be creating a pool instance here
212 // every time we called 'connect' to get a new client?
213 // that's a bad thing & results in creating an unbounded
214 // number of pools & therefore connections
215 const aPool = new pg.Pool()
216 return aPool.connect()
217}
218```
219
220### events
221
222Every instance of a `Pool` is an event emitter. These instances emit the following events:
223
224#### error
225
226Emitted whenever an idle client in the pool encounters an error. This is common when your PostgreSQL server shuts down, reboots, or a network partition otherwise causes it to become unavailable while your pool has connected clients.
227
228Example:
229
230```js
231const Pool = require('pg-pool')
232const pool = new Pool()
233
234// attach an error handler to the pool for when a connected, idle client
235// receives an error by being disconnected, etc
236pool.on('error', function (error, client) {
237 // handle this in the same way you would treat process.on('uncaughtException')
238 // it is supplied the error as well as the idle client which received the error
239})
240```
241
242#### connect
243
244Fired whenever the pool creates a **new** `pg.Client` instance and successfully connects it to the backend.
245
246Example:
247
248```js
249const Pool = require('pg-pool')
250const pool = new Pool()
251
252const count = 0
253
254pool.on('connect', (client) => {
255 client.count = count++
256})
257
258pool
259 .connect()
260 .then((client) => {
261 return client
262 .query('SELECT $1::int AS "clientCount"', [client.count])
263 .then((res) => console.log(res.rows[0].clientCount)) // outputs 0
264 .then(() => client)
265 })
266 .then((client) => client.release())
267```
268
269#### acquire
270
271Fired whenever a client is acquired from the pool
272
273Example:
274
275This allows you to count the number of clients which have ever been acquired from the pool.
276
277```js
278const Pool = require('pg-pool')
279const pool = new Pool()
280
281const acquireCount = 0
282pool.on('acquire', function (client) {
283 acquireCount++
284})
285
286const connectCount = 0
287pool.on('connect', function () {
288 connectCount++
289})
290
291for (let i = 0; i < 200; i++) {
292 pool.query('SELECT NOW()')
293}
294
295setTimeout(function () {
296 console.log('connect count:', connectCount) // output: connect count: 10
297 console.log('acquire count:', acquireCount) // output: acquire count: 200
298}, 100)
299```
300
301### environment variables
302
303pg-pool & node-postgres support some of the same environment variables as `psql` supports. The most common are:
304
305```
306PGDATABASE=my_db
307PGUSER=username
308PGPASSWORD="my awesome password"
309PGPORT=5432
310PGSSLMODE=require
311```
312
313Usually I will export these into my local environment via a `.env` file with environment settings or export them in `~/.bash_profile` or something similar. This way I get configurability which works with both the postgres suite of tools (`psql`, `pg_dump`, `pg_restore`) and node, I can vary the environment variables locally and in production, and it supports the concept of a [12-factor app](http://12factor.net/) out of the box.
314
315## maxUses and read-replica autoscaling (e.g. AWS Aurora)
316
317The maxUses config option can help an application instance rebalance load against a replica set that has been auto-scaled after the connection pool is already full of healthy connections.
318
319The mechanism here is that a connection is considered "expended" after it has been acquired and released `maxUses` number of times. Depending on the load on your system, this means there will be an approximate time in which any given connection will live, thus creating a window for rebalancing.
320
321Imagine a scenario where you have 10 app instances providing an API running against a replica cluster of 3 that are accessed via a round-robin DNS entry. Each instance runs a connection pool size of 20. With an ambient load of 50 requests per second, the connection pool will likely fill up in a few minutes with healthy connections.
322
323If you have weekly bursts of traffic which peak at 1,000 requests per second, you might want to grow your replicas to 10 during this period. Without setting `maxUses`, the new replicas will not be adopted by the app servers without an intervention -- namely, restarting each in turn in order to build up new connection pools that are balanced against all the replicas. Adding additional app server instances will help to some extent because they will adopt all the replicas in an even way, but the initial app servers will continue to focus additional load on the original replicas.
324
325This is where the `maxUses` configuration option comes into play. Setting `maxUses` to 7500 will ensure that over a period of 30 minutes or so the new replicas will be adopted as the pre-existing connections are closed and replaced with new ones, thus creating a window for eventual balance.
326
327You'll want to test based on your own scenarios, but one way to make a first guess at `maxUses` is to identify an acceptable window for rebalancing and then solve for the value:
328
329```
330maxUses = rebalanceWindowSeconds * totalRequestsPerSecond / numAppInstances / poolSize
331```
332
333In the example above, assuming we acquire and release 1 connection per request and we are aiming for a 30 minute rebalancing window:
334
335```
336maxUses = rebalanceWindowSeconds * totalRequestsPerSecond / numAppInstances / poolSize
337 7200 = 1800 * 1000 / 10 / 25
338```
339
340## tests
341
342To run tests clone the repo, `npm i` in the working dir, and then run `npm test`
343
344## contributions
345
346I love contributions. Please make sure they have tests, and submit a PR. If you're not sure if the issue is worth it or will be accepted it never hurts to open an issue to begin the conversation. If you're interested in keeping up with node-postgres releated stuff, you can follow me on twitter at [@briancarlson](https://twitter.com/briancarlson) - I generally announce any noteworthy updates there.
347
348## license
349
350The MIT License (MIT)
351Copyright (c) 2016 Brian M. Carlson
352
353Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
354
355The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
356
357THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Note: See TracBrowser for help on using the repository browser.