From edd8bb26633090c96db9e579beec6d194348f346 Mon Sep 17 00:00:00 2001 From: Chinta Dinesh Reddy Date: Thu, 19 Nov 2026 12:41:38 +0530 Subject: [PATCH] fix(pg): don't poison the statement cache when a named bind fails to serialize When a parameter of a named prepared statement throws while being serialized, the client sends Close('S', name) + Sync to clean the statement up on the server. But the name stayed in the client's parsed-statement cache: on first use the ParseComplete for the Parse that was already sent recorded it, and on reuse of an already-parsed name it was never removed. Either way the client then believed the statement existed while the server had closed it, so every later query with that name failed with 'prepared statement "name" does not exist' (SQLSTATE 26000). The bind-serialization catch now drops the name from both statement caches and flags the query so the trailing ParseComplete is not recorded. --- packages/pg/lib/client.js | 2 +- packages/pg/lib/query.js | 12 ++++++ ...error-handling-prepared-statement-tests.js | 40 +++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) 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) + }) +}