Skip to content

Prepare a statement again after a schema change rejects it - #54

Merged
quinnj-bot[bot] merged 3 commits into
mainfrom
claude/prepared-statement-ddl
Nov 19, 2026
Merged

quinnj-bot[bot] merged 3 commits into
mainfrom
claude/prepared-statement-ddl

Conversation

@quinnj-bot

@quinnj-bot quinnj-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Why

After a migration changes the columns a prepared query returns (for example ALTER TABLE ... ADD COLUMN under SELECT *), PostgreSQL rejects the old prepared statement with SQLSTATE 0A000, "cached plan must not change result type". Two driver behaviors turned that one-time rejection into lasting failures or wrong data:

  • The statement cache kept the rejected statement. Every later DBInterface.prepare of the same SQL on that connection returned it and failed again, until the cache was cleared or the connection was replaced.
  • When a handle prepared its SQL again (after the cache was cleared or the entry evicted, or after reconnect=true opened 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 held a = 2, b = 1. No error was raised.

What changed

  • After the server rejects a statement with 0A000, the driver closes it, drops it from the cache, and prepares the SQL again on its next use.
  • Each new prepare also describes the statement, and the handle uses the server's current column names and types.
  • The manual describes this behavior.

Scope

  • Covers DBInterface.execute and Postgres.cursor on prepared statements. DBInterface.execute(conn, sql) uses an unnamed statement each time and was not affected.
  • The failed execution still throws. The driver does not retry automatically, because the error aborts an open transaction.

Tradeoffs

  • Any 0A000 from a prepared statement drops it, not only the cached-plan error. Matching the message text would break with a translated lc_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.prepare already does.

Verification

  • New test in Prepared Statements: swaps two column names under a cached SELECT *. On main the second prepare fails again with 0A000. With the fix, the new prepare and the old handle both return (b = 1, a = 2).
  • Local checks against PostgreSQL 17 cover the same flow after reconnect=true, with statement_cache_maxsize=0, and through Postgres.cursor on a cached statement. All pass, and the connection is not left in a transaction.
  • Full 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.
  • Self-review: the new code runs while the connection lock is held. The protocol Close message works inside an aborted transaction. A failed Close does not replace the server's error. Other handles that share the dropped statement see the missing cache entry and prepare again.

🤖 Generated with Claude Code

quinnj and others added 3 commits November 19, 2026 01:12
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>
@quinnj-bot
quinnj-bot Bot merged commit bdebf1e into main Nov 19, 2026
16 checks passed
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.

1 participant