fix(postgres): re-parse unnamed statement after resolving custom-type OIDs - #4361
Open
DoTuanAnh2k1 wants to merge 1 commit into
Open
fix(postgres): re-parse unnamed statement after resolving custom-type OIDs#4361DoTuanAnh2k1 wants to merge 1 commit into
DoTuanAnh2k1 wants to merge 1 commit into
Conversation
… OIDs A non-persistent query (`persistent(false)`) that references a type whose OID is not built-in or cached failed with "unnamed prepared statement does not exist" (SQLSTATE 26000) the first time the type was seen on a connection. Resolving the type OID runs a catalog lookup over the simple query protocol, which Postgres uses to also destroy the unnamed prepared statement. That happened between parsing the unnamed statement and binding it, so the Bind failed. Named (persistent) statements are unaffected. Re-parse the unnamed statement when a catalog lookup ran while resolving its metadata, so the following Bind can reference it. Fixes transact-rs#4305.
DoTuanAnh2k1
force-pushed
the
fix/pg-4305-unnamed-stmt-custom-type
branch
from
August 1, 2026 08:39
5a9b100 to
f5e852f
Compare
|
Just chiming in to say that we are hitting this when trying to upgrade as well! Appreciate this being fixed. |
Author
|
Thanks for the extra data point — good to know it's biting more than one upgrade. Hopefully it gets a review soon. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
A non-persistent query (
persistent(false)) that references a type whose OID is not built-in or cached fails the first time that type is seen on a connection:Thanks to @patrickt's diagnosis in #4305: for
persistent(false)the query is parsed as the unnamed statement. Resolving the OID of a custom result type (resolve_statement_metadata→fill_cache) runs a catalog lookup over the simple query protocol, and per the Postgres protocol a simpleQuerydestroys the unnamed prepared statement. That happens afterParsebut beforeBind, so theBindfails. Named (persistent) statements are unaffected, which is why it only reproduces withpersistent(false)on a cold type-OID cache. It's a regression from 0.8.The module already anticipates exactly this case:
What changed
TypeResolver/ColumnResolver::fill_cacheandresolve_statement_metadatanow report whether they actually ran a catalog lookup. Inprepare, when the statement is unnamed and such a lookup ran, the unnamed statement is re-parsed before returning, so the followingBindcan reference it. The extraParseonly happens on thepersistent(false)+ cold-cache path that already pays for the catalog round-trip; the common path is unchanged.Testing
Added
test_issue_4305: apersistent(false)query returning a custom enum on a fresh connection. It fails with SQLSTATE 26000 on the current code and passes with the fix. The fulltests/postgres/postgres.rssuite passes (57 passed, 0 failed), including the existing custom-type/array/domain tests.cargo fmt --checkandcargo clippy -p sqlx-postgresare clean.Fixes #4305.
Disclosure: prepared with AI assistance.