diff --git a/packages/pg/lib/connection.js b/packages/pg/lib/connection.js index c426b152c..a99ff6ff0 100644 --- a/packages/pg/lib/connection.js +++ b/packages/pg/lib/connection.js @@ -152,6 +152,52 @@ class Connection extends EventEmitter { this._send(serialize.query(text)) } + // query using single batch of packets + // https://github.com/brianc/node-postgres/issues/3325 + queryWithPacketBatching(query, valueMapper) { + const packets = [] + + if (!query.hasBeenParsed(this)) { + packets.push( + serialize.parse({ + text: query.text, + name: query.name, + types: query.types, + }) + ) + } + + packets.push( + serialize.bind({ + portal: query.portal, + statement: query.name, + values: query.values, + binary: query.binary, + valueMapper: valueMapper, + }) + ) + + packets.push( + serialize.describe({ + type: 'P', + name: query.portal || '', + }) + ) + + packets.push( + serialize.execute({ + portal: query.portal, + rows: query.rows, + }) + ) + + if (!query.rows) { + packets.push(syncBuffer) + } + + this._send(Buffer.concat(packets)) + } + // send parse message parse(query) { this._send(serialize.parse(query)) diff --git a/packages/pg/lib/query.js b/packages/pg/lib/query.js index fbef341bf..cf8f8c75a 100644 --- a/packages/pg/lib/query.js +++ b/packages/pg/lib/query.js @@ -161,7 +161,7 @@ class Query extends EventEmitter { return new Error('Query values must be an array') } if (this.requiresPreparation()) { - this.prepare(connection) + connection.queryWithPacketBatching(this, utils.prepareValue) } else { connection.query(this.text) }