DROP FUNCTION IF EXISTS akuz.worker_fio CASCADE;

CREATE FUNCTION akuz.worker_fio()
  RETURNS trigger AS
$BODY$
BEGIN
	UPDATE akuz.T_WORKER
	SET FIO = New.LastName || ' ' || LEFT(New.FirstName, 1) || '.' || 
    CASE 
        WHEN NULLIF(TRIM(BOTH FROM New.FatherName), '') IS NOT NULL THEN LEFT(New.FatherName, 1) || '.'
        ELSE ''
    END
	where WorkerId = New.WorkerId;
Return New;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
--ALTER FUNCTION akuz.worker_fio()  OWNER TO postgres;

CREATE TRIGGER worker_insert_update_fio
  AFTER INSERT OR UPDATE OF lastname, firstname, fathername
  ON akuz.t_worker
  FOR EACH ROW
  EXECUTE PROCEDURE akuz.worker_fio();