Consider the following example function in plpgsql:
create or replace function fn_delete(p_id int)
returns SETOF result_transaction as
$$
declare
related_rows_affected int :=0;
begin
--some previous code that alters some tables--
.
.
.
select count(id1) into related_rows_affected from td_other,
lateral fn_delete_secondary(id1)
where id = p_id;
end
$$ language 'plpgsql';
Consider fn_delete_secondary just deletes some records. I want to rollback any previous changes in fn_delete if fn_delete_secondary throws an exception that prevents it from deleting the appropriated rows. How can I achieve the aforementioned result?
I thought that throwing an exception inside the fn_delete_secondary would rollback fn_delete too, but isn't the case... Note that fn_delete_secondary handles exceptions in the form
EXCEPTION
WHEN OTHERS THEN
If I didn't handle the exceptions inside fn_delete_secondary, would that work? I would prefer to leave it as is right now because I also use this function directly.
Other question is: How can I get the results of fn_delete_secondary when is used inside in a lateral statement?