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
22 changes: 20 additions & 2 deletions lib/postgrex/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,9 @@ defmodule Postgrex.Protocol do
:ok <- msg_send(s, msgs, buffer) do
reload_closed(s, status, query, oids, buffer)
else
{:error, %Postgrex.Error{} = err, s, buffer} ->
error_ready(s, status, err, buffer)

{:disconnect, _err, _s} = disconnect ->
disconnect
end
Expand All @@ -1975,6 +1978,9 @@ defmodule Postgrex.Protocol do
{:ok, %{buffer: buffer} = s} <- recv_ready(s, status, buffer) do
reload_spawn(%{s | buffer: nil}, status, query, oids, buffer)
else
{:error, %Postgrex.Error{} = err, s, buffer} ->
error_ready(s, status, err, buffer)

{:disconnect, _err, _s} = disconnect ->
disconnect
end
Expand Down Expand Up @@ -3005,6 +3011,9 @@ defmodule Postgrex.Protocol do
%{connection_id: connection_id} = s
{:ok, %Postgrex.Result{command: :close, connection_id: connection_id}, s}
else
{:error, %Postgrex.Error{} = err, s, buffer} ->
error_ready(s, status, err, buffer)

{:disconnect, _err, _s} = disconnect ->
disconnect
end
Expand All @@ -3020,6 +3029,9 @@ defmodule Postgrex.Protocol do
%{connection_id: connection_id} = s
{:ok, %Postgrex.Result{command: :close, connection_id: connection_id}, s}
else
{:error, %Postgrex.Error{} = err, s, buffer} ->
error_ready(s, status, err, buffer)

{:disconnect, _err, _s} = disconnect ->
disconnect
end
Expand Down Expand Up @@ -3138,8 +3150,7 @@ defmodule Postgrex.Protocol do
{:ok, %{s | postgres: postgres, buffer: buffer}}

{:ok, msg_error(fields: fields), buffer} ->
err = Postgrex.Error.exception(postgres: fields)
{:disconnect, err, %{s | buffer: buffer}}
{:error, Postgrex.Error.exception(postgres: fields), s, buffer}

{:ok, msg, buffer} ->
s = handle_msg(s, status, msg)
Expand Down Expand Up @@ -3169,6 +3180,9 @@ defmodule Postgrex.Protocol do
{:ok, msg_ready(status: unexpected), buffer} ->
sync_error(s, unexpected, buffer)

{:ok, msg_error(fields: fields), buffer} ->
{:error, Postgrex.Error.exception(postgres: fields), s, buffer}

{:ok, msg, buffer} ->
s = handle_msg(s, status, msg)
recv_strict_ready(s, status, expected, buffer)
Expand All @@ -3185,6 +3199,9 @@ defmodule Postgrex.Protocol do
{:ok, s} ->
{:error, %{err | connection_id: connection_id}, s}

{:error, err, s, buffer} ->
{:disconnect, err, %{s | buffer: buffer}}

{:disconnect, _, _} = disconnect ->
if err.postgres.severity in ["FATAL", "PANIC"] do
{:disconnect, %{err | connection_id: connection_id}, %{s | buffer: buffer}}
Expand All @@ -3197,6 +3214,7 @@ defmodule Postgrex.Protocol do
defp error_ready(s, status, err, buffer) do
case recv_ready(s, status, buffer) do
{:ok, s} -> {:error, err, s}
{:error, err, s, buffer} -> {:disconnect, err, %{s | buffer: buffer}}
{:disconnect, _, _} = disconnect -> disconnect
end
end
Expand Down
14 changes: 14 additions & 0 deletions test/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,20 @@ defmodule QueryTest do
assert [[42]] = query("SELECT 42", [])
end

test "connection works after failure in syncing state", context do
assert [[connection_id]] = query("SELECT pg_backend_pid()", [])

assert %Postgrex.Error{postgres: %{code: :unique_violation}} =
query("insert into deferred_uniques values (1), (1);", [])

assert %Postgrex.Error{postgres: %{code: :unique_violation}} =
query("insert into deferred_uniques values (1), (1);", [],
cache_statement: "deferred_uniques"
)

assert [[^connection_id]] = query("SELECT pg_backend_pid()", [])
end

test "connection works after failure during transaction", context do
assert :ok = query("BEGIN", [])

Expand Down
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ DROP TYPE IF EXISTS enum1;
CREATE TYPE enum1 AS ENUM ('elixir', 'erlang');

CREATE TABLE uniques (a int UNIQUE);
CREATE TABLE deferred_uniques (a int UNIQUE DEFERRABLE INITIALLY DEFERRED);
CREATE TABLE timestamps (micro timestamp, milli timestamp(3), sec timestamp(0), sec_arr timestamp(0)[]);
CREATE TABLE timestamps_stream (micro timestamp, milli timestamp(3), sec timestamp(0), sec_arr timestamp(0)[]);

Expand Down
10 changes: 10 additions & 0 deletions test/transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ defmodule TransactionTest do
assert [[42]] = query("SELECT 42", [])
end

@tag mode: :transaction
test "connection works after failure during implicit commit", context do
assert [[connection_id]] = query("SELECT pg_backend_pid()", [])

assert %Postgrex.Error{postgres: %{code: :unique_violation}} =
query("insert into deferred_uniques values (1), (1);", [])

assert [[^connection_id]] = query("SELECT pg_backend_pid()", [])
end

@tag mode: :transaction
test "query begin returns error", context do
Process.flag(:trap_exit, true)
Expand Down
Loading