I have an PostgreSQL function which takes around 30 minutes to complete. When running that function I want to insert a row to a separate table called running_status
at the start of the function and update that record after completing the function.
CREATE OR REPLACE FUNCTION fun1(arg1 numeric, arg2 numeric)
RETURNS numeric
LANGUAGE plpgsql
AS $$
DECLARE
-- <declare variabls>
BEGIN
BEGIN
-- <insert status record>
END;
-- <function body>
BEGIN
-- <update status record>
END;
END;
$$;
But the problem is, since the function is defined within transaction block (BEGIN, END block) it will be executed as a transaction. So this status record is not inserted to the database until this whole block completed.
So my question is how to insert this record to the database before start the function body?
Start Function -> insert record to db -> commit -> run function body -> commit -> End function