create function public.set_acting_usr(uid text)
  returns void
  language plpgsql
  volatile
  as $$
begin
  set session "dnim.usr_uid" = uid;
end;
$$;

create function public.get_acting_usr()
  returns public.usr
  language plpgsql
  volatile
as $$
declare
  uid text;
  acting_usr public.usr;
begin
  if nullif(current_setting('dnim.usr_uid', true), '') is null then
    acting_usr := public.usr_root();
  else 
    select u.*
    from public.usr u
    where u.uid = current_setting('dnim.usr_uid', true) :: uuid
    into acting_usr;
  end if;

  return acting_usr;
end;
$$;
