ChatGPT would be better than whatever wrote this, I had to stop reading.
with affected_rows as (
insert/update/delete into/from ...
returning *
)
insert into my_audit_table
select @op, current_timestamp, @userinfo, * from affected_rows
where @op is bound to one of "insert", "update" or "delete" and @userinfo is the user(name|id|various|etc) of the user that caused the change.