for the etp installation, I temporarily do this:
select max(tree_leaf_key_to_int(child.tree_sortkey)) into v_max_value
from cr_items child
where child.parent_id =0;
Worked fine and then back to the original function. But a general solution is needed here.