Skip to content

Batch query packets to avoid additional latency #3340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/pg/lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,44 @@ 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,
}))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have lost the try? I’m not sure if it’s a bug in #1855’s test or if the rewrite to pg-protocol or some other change made the try unnecessary, but it should probably be consistent with prepare.


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)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the Flush message when query.rows is present?


this._send(Buffer.concat(packets))
}

// send parse message
parse(query) {
this._send(serialize.parse(query))
Expand Down
2 changes: 1 addition & 1 deletion packages/pg/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down