Given the typname
of a composite type, how do I find the type oids
of all component types recursively?
Example:
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);
I can get the type oids
of the members of 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'
But I need to recurse that, which I guess can be done using 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
;
But that does not find the inner array of t_station
composite type.