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
S1is successful butS2fails, willS1get committed? - is my understanding that, all statements after
BEGINare executed as a single trasaction, correct? - in the absence of an explicit
COMMITbeforeENDand all statements are successful, will the transaction be committed regardless of autocommit = on/off ? - lets say
S1, S2, S3are allINSERTstatements.S1andS2succeed butS3fails, will the inserts inS1, S2be reversed in the absence of an explicitROLLBACKstatement?
Thank you!