Repository navigation
Prepare a statement again after a schema change rejects it - #54
Merged
Merged
Conversation
After DDL changes a prepared statement's result columns, PostgreSQL
rejects the statement with SQLSTATE 0A000 ("cached plan must not change
result type"). The test swaps two column names and checks that the
rejection happens once, and that a new prepare and the old handle both
read the server's new column order.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After DDL or a search_path change alters a prepared statement's result
columns, PostgreSQL rejects every later execution of it with SQLSTATE
0A000 ("cached plan must not change result type"). Two driver
behaviors made that worse:
- The statement cache kept the rejected server statement, so every
DBInterface.prepare of the same SQL returned it and failed again
until the cache was cleared or the connection replaced.
- When a handle prepared its SQL again (after the cache entry was
cleared or evicted, or after reconnect=true opened a new session),
it kept the column names and type OIDs described the first time.
With the columns renamed or reordered, values came back under the
wrong names with no error.
A rejected statement is now closed and dropped from the cache, and the
handle prepares again on its next use. Every prepare also describes the
statement and adopts the server's current columns.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The catch variable is untyped, so passing it straight to discard_rejected_statement! was a dynamic call that the trim verifier reported. Narrow it with isa at the call site. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
After a migration changes the columns a prepared query returns (for example
ALTER TABLE ... ADD COLUMNunderSELECT *), PostgreSQL rejects the old prepared statement with SQLSTATE0A000, "cached plan must not change result type". Two driver behaviors turned that one-time rejection into lasting failures or wrong data:DBInterface.prepareof the same SQL on that connection returned it and failed again, until the cache was cleared or the connection was replaced.reconnect=trueopened a new session), it kept the column names and types from its first prepare. With two columns swapped by name, the query returned(a = 1, b = 2)while the server helda = 2, b = 1. No error was raised.What changed
0A000, the driver closes it, drops it from the cache, and prepares the SQL again on its next use.Scope
DBInterface.executeandPostgres.cursoron prepared statements.DBInterface.execute(conn, sql)uses an unnamed statement each time and was not affected.Tradeoffs
0A000from a prepared statement drops it, not only the cached-plan error. Matching the message text would break with a translatedlc_messages. A false match costs one extra prepare.Blast Radius
Only prepared-statement paths change. The normal path is unchanged. A re-prepare (after reconnect, eviction, or this rejection) now makes two round trips instead of one, as
DBInterface.preparealready does.Verification
Prepared Statements: swaps two column names under a cachedSELECT *. On main the second prepare fails again with0A000. With the fix, the new prepare and the old handle both return(b = 1, a = 2).reconnect=true, withstatement_cache_maxsize=0, and throughPostgres.cursoron a cached statement. All pass, and the connection is not left in a transaction.Pkg.test()locally (PostgreSQL 16): 9,961 passed. The one failure was the JuliaC trim verifier, which flagged the new error handler as a dynamic call. The last commit fixes that, and a rerun of the trim compile reports 0 package verifier errors, the same as main.🤖 Generated with Claude Code