erleans / pgo

Erlang Postgres client and connection pool
Apache License 2.0
80 stars 16 forks source link

Crash when executing `do` statement #69

Closed lpil closed 1 year ago

lpil commented 1 year ago

Hello!

If I execute this statement:

do $$
begin

insert into hex_user
  (username, email, hex_url)
values
  ('one', 'one@one.com', null);

insert into hex_user
  (username, email, hex_url)
values
  ('two', 'two@two.com', null);

end
$$

pgo crashes after successfully running the statement.

exception error: no match of right hand side value do
  in function  pgo_handler:receive_loop0/6 (/Users/louis/src/gleam/packages/build/dev/erlang/pgo/src/pgo_handler.erl, line 462)
  in call from pgo_handler:extended_query/6 (/Users/louis/src/gleam/packages/build/dev/erlang/pgo/src/pgo_handler.erl, line 363)
  in call from otel_tracer_noop:with_span/5 (/Users/louis/src/gleam/packages/build/dev/erlang/opentelemetry_api/src/otel_tracer_noop.erl, line 59)
  in call from pgo:query/3 (/Users/louis/src/gleam/packages/build/dev/erlang/pgo/src/pgo.erl, line 103)
  in call from gleam_pgo_ffi:query/3 (/Users/louis/src/gleam/packages/build/dev/erlang/gleam_pgo/_gleam_artefacts/gleam_pgo_ffi.erl, line 63)
  in call from gleam@pgo:execute/4 (/Users/louis/src/gleam/packages/build/dev/erlang/gleam_pgo/_gleam_artefacts/gleam@pgo.erl, line 946)
  in call from packages:main/0 (/Users/louis/src/gleam/packages/build/dev/erlang/packages/_gleam_artefacts/packages.erl, line 19)⏎ 

The line in question:

https://github.com/erleans/pgo/blob/d1989c44f41ccf1b4e670ccafe46d43f71cb6dfc/src/pgo_handler.erl#L462

The crash happens after the statement runs, so the rows are successfully inserted.

# select * from hex_user;
 id | username |    email    | hex_url 
----+----------+-------------+---------
  1 | one      | one@one.com | 
  2 | two      | two@two.com | 
(2 rows)

Related to #68

Thanks, Louis

tsloughter commented 1 year ago

Huh, it seems to be saying it can't match do to {Command, NumRows} but I don't see how decode_tag couldn't return a tuple..

Need a type checker!

lpil commented 1 year ago

Yeah, weird eh? I couldn't work out what was going on there either.

tsloughter commented 1 year ago

What version are you on? I just added to the test suite and it worked:

?assertMatch(#{command := do},
                 pgo:query("do $$ begin insert into foo_range (id, some_range) values (1, '[1,3]'); insert into foo_range (id, some_range) values (3, '[4,8]'); end $$")
),
lpil commented 1 year ago

Oops, should have included that. I'm on v0.13.0.

My Gleam wrapper is using pgo:query(Sql, Arguments, #{pool => Name}), but otherwise it looks like the same thing. I've got to stop working now and get ready for a wedding I'm attending in the morning, but if you don't find the problem before I'm back I'll try and make a minimal reproduction for you.

tsloughter commented 1 year ago

Oh, this was fixed shortly after 0.13.0 :) https://github.com/erleans/pgo/pull/64/files

I'll make a new release today.

lpil commented 1 year ago

Wicked, thank you!

tsloughter commented 1 year ago

Published 0.14.0