从 Oracle 中的 'start with' 和 'connect by' 到 Postgres

From 'start with' and 'connect by' in Oracle to Postgres

更新:

我想转换这个 Oracle 代码:

select lpad(c4, length(c4) + (level*2)-2 , ' '), lpad(c2, length(c2) + (level*2)-2 , ' ')
from (select root_id c1, root_tab_col_name c2, null c3, 'root_table' c4 from root_table
    union all
    select second_tab_id, second_tab_col_name, root_tab_id, 'second_table' from second_table
    union all
    select third_tab_id, third_tab_col_name, second_tab_id, 'third_table' from third_table)
start with c3 is null
connect by prior c1 = c3;

以 PostgreSQL 形式。有人可以帮我吗?关于。

更新:

我有 3 个表用外键连接:

本次查询的结果:

试试这个:

with request_base as (
       select root_id c1, root_tab_col_name c2, null c3, 'root_table' c4 from root_table
    union all
       select second_tab_id, second_tab_col_name, root_tab_id, 'second_table' from second_table
     union all
    select third_tab_id, third_tab_col_name, second_tab_id, 'third_table' from third_table
)
,req2 as (WITH recursive recurs1(tables_name,texts,n) as (
      select c4,c2,c1 rel from request_base where c3 is null
    UNION 
        select c4,c2,c1 from request_base,recurs1 where c3=n
 )
    select tables_name,texts from recurs1
)
select * from req2