Dovresti usare un'espressione di tabella comune ricorsiva che semplicemente "porta" la radice attraverso i livelli di ricorsione:
with recursive fg_tree as (
select fg_id,
fg_id as fg_clasifier_id -- <<< this is the "root"
from fg
where parent_fg_id is null -- <<< this is the "start with" part
union all
select c.fg_id,
p.fg_clasifier_id
from fg c
join fg_tree p on p.fg_id = c.parent_fg_id -- <<< this is the "connect by" part
)
select *
from fg_tree;
Maggiori dettagli sulle espressioni di tabelle comuni ricorsive nel manuale:http://www.postgresql.org/docs/current/static/queries-with.html">http://www.postgresql.org/docs/current/static/queries-with.html www.postgresql.org/docs/current/static/queries-with.html