给定typname
复合类型,如何递归oids
查找所有组件类型的类型?
例子:
CREATE TYPE t_station AS (x INT,
y INT,
label VARCHAR);
CREATE TYPE t_address AS (city VARCHAR,
street VARCHAR,
no INT,
stations t_station[]);
CREATE TYPE t_employee AS (name VARCHAR,
age INT,
coins INT[],
notes VARCHAR,
address t_address);
我可以得到oids
成员的类型t_employee
:
SELECT
t.typname, t.oid, a.attname, a.atttypid
FROM
pg_attribute a INNER JOIN pg_type t ON a.attrelid = t.typrelid
AND t.typname = 't_employee'
但我需要递归,我想这可以使用WITH RECURSIVE
:
WITH RECURSIVE allattrs(typname, oid, attname, atttypid) AS (
select t.typname, t.oid, a.attname, a.atttypid from pg_attribute a inner join pg_type t on a.attrelid = t.typrelid and t.typname = 't_employee'
union all
select z.* from
(select t.typname, t.oid, a.attname, a.atttypid from pg_attribute a inner join pg_type t on a.attrelid = t.typrelid) z,
allattrs y where y.atttypid = z.oid
)
SELECT * FROM allattrs limit 100
;
但这并没有找到t_station
复合类型的内部数组。