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..df9696a 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::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 + err isa API.Error && 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 + err isa API.Error && discard_rejected_statement!(stmt, err) + rethrow() finally fill!(stmt.params, missing) end 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)