CREATE OR REPLACE FUNCTION public.trg_core_messages_md5()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
   NEW.body_md5 := md5(NEW.message_body);
   RETURN NEW;
END
$function$
;

create trigger body_md5_default before
insert
    on
    public.core_messages for each row
    when (((new.body_md5 is null)
        and (new.message_body is not null))) execute function trg_core_messages_md5()
;

ALTER TABLE public.core_messages ALTER COLUMN body_md5 SET DEFAULT NULL::character varying;