diff --git a/packages/pg/lib/native/query.js b/packages/pg/lib/native/query.js index 8cb561979..601f7ea93 100644 --- a/packages/pg/lib/native/query.js +++ b/packages/pg/lib/native/query.js @@ -137,7 +137,8 @@ NativeQuery.prototype.submit = function (client) { // 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]) { + // check for undefined, not falsy: the text can be empty + 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) diff --git a/packages/pg/lib/query.js b/packages/pg/lib/query.js index 6b9214199..50773ca24 100644 --- a/packages/pg/lib/query.js +++ b/packages/pg/lib/query.js @@ -183,7 +183,12 @@ class Query extends EventEmitter { } hasBeenParsed(connection) { - return this.name && (connection.parsedStatements[this.name] || connection.submittedNamedStatements[this.name]) + // check for undefined, not falsy: the text can be empty + return ( + this.name && + (connection.parsedStatements[this.name] !== undefined || + connection.submittedNamedStatements[this.name] !== undefined) + ) } handlePortalSuspended(connection) { diff --git a/packages/pg/test/integration/client/empty-query-tests.js b/packages/pg/test/integration/client/empty-query-tests.js index 61d46512e..45aeabb08 100644 --- a/packages/pg/test/integration/client/empty-query-tests.js +++ b/packages/pg/test/integration/client/empty-query-tests.js @@ -19,3 +19,15 @@ suite.test('callback supported', function (done) { client.end(done) }) }) + +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() + } +})