Comments (4)
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.
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.
from epgsql.
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)
- binary parameters HOT 6
- Weird UTF8 issue HOT 1
- Type unsufficient support HOT 2
- epgsql:parse leaves active statement HOT 2
- Add support for tracing with OpenTelemetry HOT 1
- timeout in epgsql_sock:sync_command/3 HOT 6
- Unable to insert UTF-8 HOT 2
- `port_command` in OTP-26 HOT 1
- New release for OTP-26 compat HOT 4
- Always check for errors returned from epgsql_sock:send/3 HOT 3
- {error, sock_closed} on version 26.0.2 HOT 6
- Confusing documentation for epgsql:with_transaction/3 HOT 1
- Error when one of parameters of equery is of cube type HOT 2
- Invalid UUIDs in extended queries crash the driver HOT 3
- Using an invalid UUID in extended query crash the driver HOT 1
- ensure_commited option to with_transaction seems to override query errors? HOT 3
- with_transaction rollback scenario crashing if the transaction fun inside the try block crashes. HOT 4
- internal crash when postgresql is not (yet) running
- return {error,sock_closed} when conn to tembo cloud databases
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from epgsql.