Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions packages/pg/lib/native/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ NativeQuery.prototype.submit = function (client) {
const values = (this.values || []).map(utils.prepareValue)

// check if the client has already executed this named query
// if so...just execute it again - skip the planning phase
if (client.namedQueries[this.name]) {
// if so...just execute it again - skip the planning phase. By presence, not truth: a
// named statement with an empty text is prepared all the same
if (client.namedQueries[this.name] !== undefined) {
if (this.text && client.namedQueries[this.name] !== this.text) {
const err = new Error(`Prepared statements must be unique - '${this.name}' was used for a different statement`)
return after(err)
Expand Down
7 changes: 6 additions & 1 deletion packages/pg/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,12 @@ class Query extends EventEmitter {
}

hasBeenParsed(connection) {
return this.name && (connection.parsedStatements[this.name] || connection.submittedNamedStatements[this.name])
// by presence, not truth: a named statement with an empty text is parsed all the same
return (
this.name &&
(connection.parsedStatements[this.name] !== undefined ||
connection.submittedNamedStatements[this.name] !== undefined)
)
}

handlePortalSuspended(connection) {
Expand Down
14 changes: 14 additions & 0 deletions packages/pg/test/integration/client/empty-query-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,17 @@ suite.test('callback supported', function (done) {
client.end(done)
})
})

// the name was recorded as parsed with its empty text, and then read as not parsed at all,
// so the second run prepared it again and the server refused the duplicate
suite.test('a named empty statement can run more than once', async function () {
const client = helper.client()
try {
for (let i = 0; i < 2; i++) {
const result = await client.query({ text: '', name: 'empty' })
assert.empty(result.rows)
}
} finally {
await client.end()
}
})
Loading