I have some questions about how postgres functions and transactions work.
Currently my function looks like this:
CREATE OR REPLACE FUNCTION test_function(some_id character varying)
RETURNS character varying AS
$BODY$
BEGIN
S1;
S2;
S3;
.
.
Sn;
RETURN some_id;
END; $BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
The statements can be INSERT
, UPDATE
or plain SELECT
queries based on some_id
. As I understand from postgre documentation, all statements in this function are executed as a single transaction and committed at the END.
My questions are:
- if lets say
S1
is successful butS2
fails, willS1
get committed? - is my understanding that, all statements after
BEGIN
are executed as a single trasaction, correct? - in the absence of an explicit
COMMIT
beforeEND
and all statements are successful, will the transaction be committed regardless of autocommit = on/off ? - lets say
S1, S2, S3
are allINSERT
statements.S1
andS2
succeed butS3
fails, will the inserts inS1, S2
be reversed in the absence of an explicitROLLBACK
statement?
Thank you!