Skip to content

fix(pg): don't poison the statement cache when a named bind fails to serialize - #3868

Open
Cdineshreddy12 wants to merge 1 commit into
brianc:masterfrom
Cdineshreddy12:fix/named-statement-cache-poisoning
Open

Cdineshreddy12 wants to merge 1 commit into
brianc:masterfrom
Cdineshreddy12:fix/named-statement-cache-poisoning

Conversation

@Cdineshreddy12

Copy link
Copy Markdown

Fixes #3867

When a parameter of a named prepared statement fails to serialize, the catch in Query.prepare closes the statement on the server (Close('S', name) + Sync, from #3574) but the client's statement cache was left pointing at it, so every later query reusing that name failed with prepared statement "<name>" does not exist. Full write-up and repro in the issue.

Two cases, both fixed in the same catch:

  • first use of the name — the Parse was already sent, so ParseComplete comes back and _handleParseComplete would record the name right after the Close removed it. A new _statementClosedDueToError flag on the query makes that handler skip the recording.
  • reuse of an already-parsed name — no new Parse is sent, so the existing cache entry just needs removing, which the catch now does for both parsedStatements and submittedNamedStatements.

Unnamed statements are unaffected (if (this.name) guard), so the existing throw-in-bind tests are unchanged.

Note on #3820 / #3835: this is a different bug on the same catch path (cache vs. double-settle), and #3835 keeps sending Close + Sync, so this poisoning is still present with that PR applied. The two fixes are independent; whichever lands first, I'll rebase this onto it — the only shared line is the handleError/defer call, which I left untouched here.

Added an integration test in query-error-handling-prepared-statement-tests.js (uses the promise API so it settles once regardless of #3820); it fails on master with "prepared statement does not exist" and passes with this change. Ran it and the prepared-statement integration suite against PostgreSQL 18, and the client unit tests (the one pre-existing password-callback-tests.js failure is unrelated to this change).

…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.
@Cdineshreddy12
Cdineshreddy12 force-pushed the fix/named-statement-cache-poisoning branch from 2624236 to edd8bb2 Compare November 19, 2026 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Named prepared statement poisons the statement cache when a parameter fails to serialize

1 participant