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

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

  update public.perm
  set owner_user = public.usr_root()
    , owner_group = public.grp_admins()
  where path = '/groups/' || new_grp || '/members'
     or path = '/groups/' || new_grp || '/tag';

  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();
