Skip to content

Bun.sql prepared statement cache not invalidated after DDL — connection permanently fails with cached plan must not change result type (SQLSTATE 0A000) #29484

@yehonatanz

Description

@yehonatanz

What version of Bun is running?

1.3.12+700fc117a

What platform is your computer?

Linux 6.12.* aarch64 unknown

What steps can reproduce the bug?

bun:sql creates server-side named prepared statements and caches them per
connection. When DDL changes the rowtype of a table referenced by a cached
plan, PostgreSQL rejects subsequent executions of that plan with SQLSTATE
0A000 ("cached plan must not change result type"). bun:sql surfaces the
error to the caller but does not evict the stale plan or retry, so every
future execution of the same query on that connection fails forever —
even though a fresh parse would succeed.

Reproduction:
Run the attached repro.ts against any postgres server.

What is the expected behavior?

Sibling drivers (e.g. Porsager's postgres / postgres.js) handle this
transparently: they detect SQLSTATE 0A000 + the specific message, drop
the cached statement, and retry once. Callers never see the error.

I believe that should be Bun's approach as well.

What do you see instead?

2nd call FAILED: PostgresError: cached plan must not change result type
    errno: "0A000",
 severity: "ERROR",
     file: "plancache.c",
  routine: "RevalidateCachedQuery",
     code: "ERR_POSTGRES_SERVER_ERROR"

      at wrapPostgresError (internal:sql/postgres:171:27)
      at onRejectPostgresQuery (internal:sql/postgres:200:33)

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions