diff --git a/packages/pg/lib/client.js b/packages/pg/lib/client.js index 2b13c1de7..eab017cc6 100644 --- a/packages/pg/lib/client.js +++ b/packages/pg/lib/client.js @@ -506,7 +506,7 @@ class Client extends EventEmitter { // if a prepared statement has a name and properly parses // we track that its already been executed so we don't parse // it again on the same client - if (activeQuery.name) { + if (activeQuery.name && !activeQuery._statementClosedDueToError) { this.connection.parsedStatements[activeQuery.name] = activeQuery.text delete this.connection.submittedNamedStatements[activeQuery.name] } diff --git a/packages/pg/lib/query.js b/packages/pg/lib/query.js index 3b93cb5ba..6cd4718c5 100644 --- a/packages/pg/lib/query.js +++ b/packages/pg/lib/query.js @@ -239,6 +239,18 @@ class Query extends EventEmitter { connection.close({ type: 'S', name: this.name }) connection.sync() + if (this.name) { + // The statement is being closed on the server, so the client must not + // keep it in its parsed-statement cache. Drop any existing entry (reuse + // of an already-parsed name) and flag the query so the ParseComplete for + // the Parse we just sent (first use of the name) is not recorded either. + // Otherwise every later use of this name fails with "prepared statement + // does not exist". + this._statementClosedDueToError = true + delete connection.parsedStatements[this.name] + delete connection.submittedNamedStatements[this.name] + } + this.handleError(err, connection) return } diff --git a/packages/pg/test/integration/client/query-error-handling-prepared-statement-tests.js b/packages/pg/test/integration/client/query-error-handling-prepared-statement-tests.js index 13ecf4b53..41ccabf53 100644 --- a/packages/pg/test/integration/client/query-error-handling-prepared-statement-tests.js +++ b/packages/pg/test/integration/client/query-error-handling-prepared-statement-tests.js @@ -124,3 +124,43 @@ suite.test('query killed during query execution of prepared statement', function }) ) }) + +// The client-side prepared-statement cache this guards against only exists in +// the pure-JS driver; pg-native prepares statements through libpq, so this is +// a JS-only test. +if (!helper.args.native) { + suite.test('reusing a named statement after a parameter fails to serialize', function (done) { + // A value that throws while being serialized makes the client close the + // prepared statement on the server. The client must drop it from its + // statement cache too, otherwise every later use of the same name fails + // with "prepared statement does not exist". Uses the promise API so the + // first query settles exactly once regardless of issue #3820. + const client = new Client() + client + .connect() + .then(async () => { + const bad = { + toPostgres() { + throw new Error('serialization failed') + }, + } + + await assert.rejects( + () => client.query({ name: 'reused-after-error', text: 'select $1::int as v', values: [bad] }), + /serialization failed/ + ) + + // the same name, now with a value that serializes fine + const res = await client.query({ + name: 'reused-after-error', + text: 'select $1::int as v', + values: [42], + }) + assert.equal(res.rows[0].v, 42) + + await client.end() + done() + }) + .catch(done) + }) +}