create function public.do_usr_create_default_grp()
  returns trigger
  volatile
  language plpgsql
  as $$
declare
  name    public.grp_tag := grp_tag_of_string('usr_' || new.uid);
  new_grp public.grp;
begin
  insert into public.grp
    (tag)
  values
    (name)
  returning * into new_grp;

  perform public.grp_add_member(to_grp => new_grp.id, add_usr => new.id);

  update public.perm
  set owner_user = NEW.id
    , owner_group = (public.grp_admins()).id
    , owner_user_mode = 'r' :: public.perm_mode
  where path = '/groups/' || (new_grp.uid :: text) || '/members'
     or path = '/groups/' || (new_grp.uid :: text) || '/name';

  return null;
end;
$$;

create trigger usr_create_default_grp
after insert on public.usr
for each row
execute function public.do_usr_create_default_grp();
