Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions integration/elixir/test/savepoint_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
defmodule Pgdog.SavepointTest do
use ExUnit.Case, async: false

# Regression tests for https://github.com/pgdogdev/pgdog/issues/1314.
#
# Postgrex's mode: :savepoint wraps each query in a savepoint by sending
# Bind, Execute and a simple Query("RELEASE SAVEPOINT postgrex_query") in one batch (no Sync in that batch)
#
# When that Execute errors, the server discards everything until a Sync arrives,
# so the Query's ReadyForQuery is never produced.
#
# PgDog used to keep waiting for it and never read the Sync the client had already sent

setup do
%{conn: Pgdog.connect()}
end

test "query error under mode: :savepoint returns instead of hanging", %{conn: conn} do
{:ok, {:error, error}} =
Postgrex.transaction(
conn,
fn c -> Postgrex.query(c, "SELECT 1/0", [], mode: :savepoint) end,
timeout: 5_000
)

assert %Postgrex.Error{postgres: %{code: :division_by_zero}} = error
end

test "connection survives a savepoint-mode error", %{conn: conn} do
{:ok, {:error, _}} =
Postgrex.transaction(
conn,
fn c -> Postgrex.query(c, "SELECT 1/0", [], mode: :savepoint) end,
timeout: 5_000
)

assert Pgdog.one(Postgrex.query!(conn, "SELECT 1::bigint", [])) == 1
end

test "savepoint-mode query that succeeds still works", %{conn: conn} do
{:ok, {:ok, result}} =
Postgrex.transaction(
conn,
fn c -> Postgrex.query(c, "SELECT 2::bigint", [], mode: :savepoint) end,
timeout: 5_000
)

assert Pgdog.one(result) == 2
end
end
11 changes: 7 additions & 4 deletions pgdog/src/backend/prepared_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ impl PreparedStatements {
}

ProtocolMessage::Sync(_) => {
self.state.add('Z');
self.state.add(ExecutionCode::ReadyForQuerySync);
}

ProtocolMessage::Query(_) => {
self.state.add('Z');
self.state.add(ExecutionCode::ReadyForQuery);
}

ProtocolMessage::Parse(parse) => {
Expand Down Expand Up @@ -252,7 +252,9 @@ impl PreparedStatements {
} else {
self.parses.push_back(name.clone());
self.state.add_ignore('C');
self.state.add_ignore('Z');

// Prepare turns into a Simple Query ('Q') so it expects a regular RFQ back.
self.state.add_ignore(ExecutionCode::ReadyForQuery);
return Ok(HandleResult::Forward);
}
}
Expand All @@ -269,7 +271,8 @@ impl PreparedStatements {
// Fastpath (F): backend responds with FunctionCallResponse (V) + ReadyForQuery (Z).
// V is Untracked and passes through; register Z so the response loop runs.
ProtocolMessage::Fastpath(_) => {
self.state.add('Z');
// If we have an extended error prior, this should be dropped.
self.state.add(ExecutionCode::ReadyForQuery);
}

ProtocolMessage::Other(_) => (),
Expand Down
Loading
Loading