From 9eeccb7027288bb1c3ee4c047c191d25f80507f9 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 01:12:11 -0700 Subject: [PATCH 1/3] Test prepared statements after DDL changes their result columns 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 --- test/runtests.jl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index f3c5bb7..24e1e05 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1299,6 +1299,28 @@ include("trim_compile_tests.jl") @test function_value == 42 @test only(DBInterface.execute(held)).val == 42 + # DDL that changes a prepared statement's result columns + # makes PostgreSQL reject it (0A000). That rejection must + # not stick to the cache, and statements prepared again + # must read the server's new column names and order. + DBInterface.execute(conn, "DROP TABLE IF EXISTS stmt_shape") + DBInterface.execute(conn, "CREATE TABLE stmt_shape (a int, b int)") + DBInterface.execute(conn, "INSERT INTO stmt_shape VALUES (1, 2)") + shape_sql = "SELECT * FROM stmt_shape" + shape_handle = DBInterface.prepare(conn, shape_sql) + @test only(Tables.rowtable(DBInterface.execute(shape_handle))) == (a = 1, b = 2) + Postgres.execute_script(conn, """ + ALTER TABLE stmt_shape RENAME COLUMN a TO tmp; + ALTER TABLE stmt_shape RENAME COLUMN b TO a; + ALTER TABLE stmt_shape RENAME COLUMN tmp TO b; + """) + shape_err = try DBInterface.execute(shape_handle) catch err; err end + @test shape_err isa Postgres.API.Error && shape_err.code == "0A000" + @test only(Tables.rowtable(DBInterface.execute(DBInterface.prepare(conn, shape_sql)))) == (b = 1, a = 2) + @test only(Tables.rowtable(DBInterface.execute(shape_handle))) == (b = 1, a = 2) + DBInterface.close!(shape_handle) + DBInterface.execute(conn, "DROP TABLE stmt_shape") + DBInterface.close!(held) DBInterface.close!(mismatch_stmt) DBInterface.close!(failing_stmt) From 79d414b525a3f40d65faee98f2cb2d670901c054 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 01:13:17 -0700 Subject: [PATCH 2/3] Prepare a statement again after PostgreSQL rejects its changed result 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 --- docs/src/manual.md | 6 +++++ src/execute.jl | 56 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/docs/src/manual.md b/docs/src/manual.md index dc7c946..a60e5d7 100644 --- a/docs/src/manual.md +++ b/docs/src/manual.md @@ -177,6 +177,12 @@ Prepared statements can be created explicitly. Postgres.jl caches their named backend statements with LRU eviction while returning an independent handle to each caller. Set `statement_cache_maxsize=0` to disable this cache. +If DDL or a `search_path` change alters a prepared statement's result +columns, PostgreSQL rejects its next execution with SQLSTATE `0A000` ("cached +plan must not change result type"). Postgres.jl then closes that server +statement, so the next execution of the same SQL, through a new or an existing +handle, prepares it again and reads the new columns. + ```julia stmt = DBInterface.prepare(conn, raw"SELECT $1::text AS value") rows = Tables.rowtable(DBInterface.execute(stmt, ("prepared",))) diff --git a/src/execute.jl b/src/execute.jl index de07875..ff7b96c 100644 --- a/src/execute.jl +++ b/src/execute.jl @@ -61,9 +61,11 @@ mutable struct Statement{S <: API.AbstractPostgresStyle} <: DBInterface.Statemen const conn::Connection{Statement{S}, S} name::String const sql::String - const nfields::Int - const names::Vector{Symbol} - const typeIds::Vector{Int} + # The result description of the server statement `name`; replaced + # whenever the SQL is prepared again, since DDL can change it. + nfields::Int + names::Vector{Symbol} + typeIds::Vector{Int} const nparams::Int # holds references to params as strings const params::Vector{Union{String, Missing}} @@ -113,23 +115,53 @@ function checkstmt(stmt::Statement) # reconnect. Keep this caller handle valid as an independent # server statement; do not silently repopulate or overrun the # current cache policy. - stmt.name = API.prepare(stmt.conn.style, stmt.conn.socket, stmt.sql, stmt.conn.debug, - stmt.conn.server_parameters) - stmt.generation = stmt.conn.generation + reprepare!(stmt) stmt.cached = false else touch_statement!(stmt.conn, cached) stmt.last_used = cached.last_used end elseif stmt.generation != stmt.conn.generation - stmt.name = API.prepare(stmt.conn.style, stmt.conn.socket, stmt.sql, stmt.conn.debug, - stmt.conn.server_parameters) - stmt.generation = stmt.conn.generation + reprepare!(stmt) end stmt.cached || touch_statement!(stmt.conn, stmt) return end +# Prepare the SQL as a new server statement and adopt its description: the +# columns can differ from the last prepare after DDL or a search_path change. +function reprepare!(stmt::Statement) + conn = stmt.conn + stmt.name = API.prepare(conn.style, conn.socket, stmt.sql, conn.debug, conn.server_parameters) + _, stmt.names, stmt.typeIds = API.describeprepared(conn.style, conn.socket, stmt.name, conn.debug, + conn.server_parameters) + stmt.nfields = length(stmt.names) + stmt.generation = conn.generation + return +end + +# PostgreSQL rejects every later execution of a prepared statement whose result +# columns changed since it was prepared (SQLSTATE 0A000, "cached plan must not +# change result type"). Close that server statement so the next use prepares +# the SQL again. +function discard_rejected_statement!(stmt::Statement, err) + (err isa API.Error && err.code == "0A000") || return + conn = stmt.conn + cached = get(conn.statements, stmt.sql, nothing) + if cached !== nothing && cached.name == stmt.name + delete!(conn.statements, stmt.sql) + cached.closed = true + end + stmt.generation = -1 # checkstmt prepares again + try + isopen(conn.socket) && API.close_statement(conn.style, conn.socket, stmt.name, conn.debug, + conn.server_parameters) + catch + # the caller is already throwing the server's rejection + end + return +end + function statement_handle(stmt::Statement) params = Union{String, Missing}[missing for _ = 1:stmt.nparams] return Statement{_style_type(stmt.conn)}( @@ -549,6 +581,9 @@ function DBInterface.execute(stmt::Statement, params=nothing, ::Type{T}=Any; deb API.exec(style, stmt.conn.socket, stmt.name, stmt.params, stmt.names, stmt.typeIds, stmt.conn.type_registry, actual_debug, 0, stmt.conn.server_parameters) + catch err + discard_rejected_statement!(stmt, err) + rethrow() finally # Bound strings can contain credentials or personal data. The # vector must be cleared even if local parameter validation or @@ -796,6 +831,9 @@ function cursor(stmt::Statement, params=nothing; fetchsize::Integer=1000, API.writemessages(conn.socket, conn.debug, ('B', portal, stmt.name, Int16(0), API.bind_param_count(length(stmt.params)), API.Params(stmt.params), Int16(0)), ('E', portal, Int32(cursor.fetchsize)), ('S',)) read_portal_batch!(cursor) return cursor + catch err + discard_rejected_statement!(stmt, err) + rethrow() finally fill!(stmt.params, missing) end From f33c9b975b58c8200aac1a0074eb5cda8308502b Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 19 Nov 2026 01:18:49 -0700 Subject: [PATCH 3/3] Keep the rejected-statement handler static under juliac --trim 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 --- src/execute.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/execute.jl b/src/execute.jl index ff7b96c..df9696a 100644 --- a/src/execute.jl +++ b/src/execute.jl @@ -144,8 +144,8 @@ end # columns changed since it was prepared (SQLSTATE 0A000, "cached plan must not # change result type"). Close that server statement so the next use prepares # the SQL again. -function discard_rejected_statement!(stmt::Statement, err) - (err isa API.Error && err.code == "0A000") || return +function discard_rejected_statement!(stmt::Statement, err::API.Error) + err.code == "0A000" || return conn = stmt.conn cached = get(conn.statements, stmt.sql, nothing) if cached !== nothing && cached.name == stmt.name @@ -582,7 +582,7 @@ function DBInterface.execute(stmt::Statement, params=nothing, ::Type{T}=Any; deb stmt.names, stmt.typeIds, stmt.conn.type_registry, actual_debug, 0, stmt.conn.server_parameters) catch err - discard_rejected_statement!(stmt, err) + err isa API.Error && discard_rejected_statement!(stmt, err) rethrow() finally # Bound strings can contain credentials or personal data. The @@ -832,7 +832,7 @@ function cursor(stmt::Statement, params=nothing; fetchsize::Integer=1000, read_portal_batch!(cursor) return cursor catch err - discard_rejected_statement!(stmt, err) + err isa API.Error && discard_rejected_statement!(stmt, err) rethrow() finally fill!(stmt.params, missing)