Giter VIP home page Giter VIP logo

Comments (4)

seriyps avatar seriyps commented on September 15, 2024

Wow, I didn't know both error and "ok" can be returned at the same time.
Are you sure you are not, eg, using the same connection from multiple processes at the same time?

Would you be able to create a reproducible minimal example?

from epgsql.

ehamberg avatar ehamberg commented on September 15, 2024

After some investigation, I think this is related to initially deferred constraint triggers.

I was able to reproduce this with the following table + constraint trigger:

create table my_table (x int);

create or replace function my_check() returns trigger as $$
begin
    if new.x >= 10 then
        raise 'x must be < 10';
    end if;

    return null;
end; $$ language 'plpgsql';

create constraint trigger my_trigger
  after insert or update on my_table
  initially deferred
  for each row execute procedure my_check();

… and the following code:

    {ok, C} = epgsql:connect(#{
        host => "localhost",
        username => "mydb",
        database => "mydb"
    }),
    {error, _} = epgsql:equery(C, "insert into my_table values(11)", []),
    ok = epgsql:close(C).

Which crashes because epgsql_sock:get_results(Sock) returns more than one value:

    {case_clause,
        [{ok,1},
         {error,
             {error,error,<<"P0001">>,raise_exception,
                 <<"x must be < 10">>,
                 [{file,<<"pl_exec.c">>},
                  {line,<<"3876">>},
                  {routine,<<"exec_stmt_raise">>},
                  {severity,<<"ERROR">>},
                  {where,
                      <<"PL/pgSQL function my_check() line 4 at RAISE">>}]}}]}}

However, if I change from equery to squery, it works as expected:

    […]
    {error, _} = epgsql:squery(C, "insert into my_table values(11)"), % matches
    […]

Also, if the constraint trigger is not initially deferred, things work with equery again:

drop trigger my_trigger on my_table;

create constraint trigger my_trigger
  after insert or update on my_table
  -- initially deferred ⚠️ DON'T defer the constraint trigger
  for each row execute procedure my_check();

I guess what's happening is that the {ok,1} comes from the insert which initially succeeds – until the deferred constraint trigger is invoked and raises the exception, which returns an error value. That would explain why two results are returned.

from epgsql.

seriyps avatar seriyps commented on September 15, 2024

from epgsql.

ehamberg avatar ehamberg commented on September 15, 2024

Do you know if the operation then succeeds in the end? Or insert is then rolled back?

The transaction fails and the insert is rolled back, so only the error should be returned.

Also interesting how result would look if there is a RETURNING xxx clause.

If I add a returning clause to the insert (epgsql:equery(C, "insert into my_table values(11) returning 43", [])), it returns {ok,1,Rows}:

   {case_clause,
        [{ok,1,[{column,<<"?column?">>,int4,23,4,-1,1,0,0}],[{43}]},
         {error,
             {error,error,<<"P0001">>,raise_exception,<<"x must be < 10">>,
                 [{file,<<"pl_exec.c">>},
                  {line,<<"3876">>},
                  {routine,<<"exec_stmt_raise">>},
                  {severity,<<"ERROR">>},
                  {where,
                      <<"PL/pgSQL function my_check() line 4 at RAISE">>}]}}]}}

from epgsql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.