Skip to content

sql: report RevalidateCachedQuery as the cached-plan error's routine - #173374

Open
demostheneslld wants to merge 1 commit into
cockroachdb:masterfrom
demostheneslld:pin-cached-plan-routine
Open

sql: report RevalidateCachedQuery as the cached-plan error's routine#173374
demostheneslld wants to merge 1 commit into
cockroachdb:masterfrom
demostheneslld:pin-cached-plan-routine

Conversation

@demostheneslld

Copy link
Copy Markdown

During the Bind phase of the extended pgwire protocol (and Execute for the simple protocol), the "cached plan must not change result type" error is detected by clients via its Routine field (PG_DIAG_SOURCE_FUNCTION). PostgreSQL reports RevalidateCachedQuery, its plancache.c function. CockroachDB reported whichever internal function constructed the error - runExecBuilder until #164406 moved the extended-protocol check to Bind, which changed the value to execBind and silently broke clients matching the old name (e.g. activerecord-cockroachdb-adapter's is_cached_plan_failure?, which stopped translating the error into ActiveRecord's evict-and-retry recovery; we hit this in production).

This PR constructs the error in a dedicated function named RevalidateCachedQuery, so the reported routine is stable across refactors and identical to PostgreSQL's - detection heuristics written against PostgreSQL (ActiveRecord upstream matches exactly this name) work unchanged. The pgtest datadriven test now asserts Code, Message, and Routine (new keepErrRoutine option), making the fields clients key on a tested contract rather than an accident of the call stack. The assertion also holds when the suite runs against real PostgreSQL.

Compatibility note: this is itself a wire-behavior change, and it breaks two client classes once. (1) Clients that adapted to 26.2 by matching execBind stop matching when the pin ships. (2) Simple-protocol clients (SQL-level PREPARE/EXECUTE, no wire Bind) matching runExecBuilder work on every released version and break only here. Every alternative to freezing the accidental value breaks one of these groups eventually; the pin breaks them onto the value PostgreSQL has reported since 8.3, making it the last such break. Flagged in the release note; deferring to you on which release it rides.

Companion adapter fix: cockroachdb/activerecord-cockroachdb-adapter#403 switches detection to the error message, which works on pre- and post-#164406 servers. The two are complementary: the adapter fix covers existing versions, this makes the field reliable going forward.

Resolves the detection half of the issue behind #164406's driver-compat work.

Clients detect "cached plan must not change result type" via the error's
Routine field (PG_DIAG_SOURCE_FUNCTION). CockroachDB reported whichever
internal function raised the error, so cockroachdb#164406 silently changed the value
from "runExecBuilder" to "execBind" and broke ActiveRecord's stale-plan
recovery. Construct the error in a dedicated function named
RevalidateCachedQuery so the field is stable and matches what PostgreSQL
reports, and assert it in pgtest.

Epic: none

Release note (bug fix): The "cached plan must not change result type"
error now reports "RevalidateCachedQuery" as its source function,
matching PostgreSQL, so client libraries that detect stale prepared
statements via that field recover correctly.
@demostheneslld
demostheneslld requested review from a team as code owners August 14, 2026 19:33
@demostheneslld
demostheneslld requested review from bghal and yuzefovich and removed request for a team August 14, 2026 19:33
@blathers-crl

blathers-crl Bot commented Aug 14, 2026

Copy link
Copy Markdown

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Aug 14, 2026
@cockroachlabs-cla-agent

cockroachlabs-cla-agent Bot commented Aug 14, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@blathers-crl

blathers-crl Bot commented Aug 15, 2026

Copy link
Copy Markdown

Detected infrastructure failure (matched: self-hosted runner lost communication with the server). Automatically rerunning failed jobs. (run link)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant