Skip to content

Commit 48d1767

Browse files
authored
perf(pg-pool): optimize client retrieval from pool by prioritizing free client with cached prepared statements
see brianc#3397
1 parent 88311c1 commit 48d1767

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

packages/pg-pool/index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class Pool extends EventEmitter {
111111
return this._clients.length >= this.options.max
112112
}
113113

114-
_pulseQueue() {
114+
_pulseQueue(name) {
115115
this.log('pulse queue')
116116
if (this.ended) {
117117
this.log('pulse queue ended')
@@ -142,7 +142,10 @@ class Pool extends EventEmitter {
142142
}
143143
const pendingItem = this._pendingQueue.shift()
144144
if (this._idle.length) {
145-
const idleItem = this._idle.pop()
145+
let idleItem = name ? this._idle.find((item) => item.client.connection.parsedStatements[name]) : null
146+
if (!idleItem) {
147+
idleItem = this._idle.pop()
148+
}
146149
clearTimeout(idleItem.timeoutId)
147150
const client = idleItem.client
148151
client.ref && client.ref()
@@ -168,7 +171,7 @@ class Pool extends EventEmitter {
168171
this.emit('remove', client)
169172
}
170173

171-
connect(cb) {
174+
connect(cb, name) {
172175
if (this.ending) {
173176
const err = new Error('Cannot use a pool after calling end on the pool')
174177
return cb ? cb(err) : this.Promise.reject(err)
@@ -181,7 +184,7 @@ class Pool extends EventEmitter {
181184
if (this._isFull() || this._idle.length) {
182185
// if we have idle clients schedule a pulse immediately
183186
if (this._idle.length) {
184-
process.nextTick(() => this._pulseQueue())
187+
process.nextTick(() => this._pulseQueue(name))
185188
}
186189

187190
if (!this.options.connectionTimeoutMillis) {
@@ -431,7 +434,7 @@ class Pool extends EventEmitter {
431434
client.release(err)
432435
return cb(err)
433436
}
434-
})
437+
}, text.name)
435438
return response.result
436439
}
437440

0 commit comments

Comments
 (0)