Попробуйте получить в качестве поля накопительный путь от всех вышестоящих родителей для каждой записи.
Что-то типа такого:
WITH RECURSIVE tree AS (
-- Base case: select the root node(s)
SELECT id, parent_id, time, to_char(parent_id)||'/' as r_path
FROM my_table
WHERE parent_id IS NULL
UNION ALL
-- Recursive case: select the children of the current node
SELECT t.id, t.parent_id, t.time, r.r_path||to_char(parent_id)||'/' as r_path
FROM my_table t
JOIN tree r ON t.parent_id = r.id
)
select * from tree
order by r_path,time