I have a function that updates a unique column mytable.value from oldvalue to newvalue.
The problem is that I'd like to store oldvalue along with the old modified time in a separate archive table.
I don't know how to do this using a plain plpgsql function (Postgres 9.6). I think I know how to do it using a trigger, but I want the archive table to be populated only in certain cases, so when the function is explicitly called, rather than every modification to mytable.
create or replace function updatevalue(oldvalue text, newvalue text) returns void
language plpgsql
as $$
begin
update mytable set modified = now(), value = newvalue where value = oldvalue
end
$$;
How can I modify the above function to populate the archive table with the old modified time? I don't want to pass it from the application as an argument to the function because of possible race conditions.