Note that these errors most of the time only happens very briefly at the same time as the ALTER is run. When I did some experiments today the server in total had around 3k req/s with maybe 0.1% of them touching the table being updated, and the error then happens maybe 1-10% of the times I try this operation. If I do the operation on a table with more load the error will happen more frequently.
Also, someone suggested me to try and recreate the functions returning the table as well inside a transaction, but that did not change anything:
BEGIN;
ALTER TABLE...
CREATE OR UPDATE FUNCTION ...
END;
Thanks for your help so far!
/Victor