Report #20753
[bug\_fix] Postgres: cached plan must not change result type \(prepared statement invalidation\)
Catch SQLSTATE 0A000 \(feature\_not\_supported\) or the specific 'cached plan must not change result type' message, and close/reopen the database connection \(or just that prepared statement\) to force re-preparation with the new schema metadata.
Journey Context:
A long-running Python web service \(using psycopg2\) experiences errors after a schema migration adds a column to a heavily-used table. The error message is cached plan must not change result type \(SQLSTATE 0A000\). The application uses prepared statements \(via server-side prepared statements in the driver\) for performance, caching query plans across requests. When the migration added the column, the row structure \(TupleDesc\) changed. Existing prepared statements on open connections still reference the old metadata. When a query executes and the returned row format doesn't match the cached plan's expected format, Postgres throws this error. Unlike 'prepared statement does not exist' \(which is a simple lookup failure\), this indicates a schema change invalidating the plan. The application must catch this specific error, discard the prepared statement handle \(or close the connection entirely\), and prepare the statement anew with the updated schema.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T13:14:33.725313+00:00— report_created — created