CREATE OR REPLACE FUNCTION rds.update_patient_fio()
    RETURNS TRIGGER
    LANGUAGE plpgsql
AS
$$
BEGIN
    new.fio := REPLACE(
        COALESCE(new.lastname, '') ||
        COALESCE(' ' || new.firstname, ' ') ||
        COALESCE(' ' || new.fathername, ' '),
        'ё', 'е');
    RETURN new;
END;
$$;


CREATE OR REPLACE TRIGGER patient_fio_trigger
    BEFORE INSERT OR UPDATE
    ON rds.t_patient
    FOR EACH ROW
EXECUTE FUNCTION update_patient_fio();
