DELETE helper function
CREATE OR REPLACE FUNCTION fr_person(person, bigint)
RETURNS boolean
LANGUAGE plpgsql
AS $function$
BEGIN
UPDATE person_hst
SET effective_until = (
SELECT timestamp
FROM audit
WHERE audit.audit_id = $2
)
WHERE person_hst.person_id = $1.person_id
AND effective_until = 'infinity';
INSERT INTO person_hst (
effective_until,
effective_from,
removed,
audit_id,
address,
date_of_birth,
person_id,
name
) SELECT
'infinity' AS effective_until,
audit.timestamp,
TRUE,
$2,
$1.address,
$1.date_of_birth,
$1.person_id,
$1.name
FROM audit
WHERE audit.audit_id = $2;
DELETE FROM person
WHERE person.person_id = $1.person_id;
RETURN TRUE;
END;
$function$
;