ltree's data type from the system information_schema.columns gives incorrect data type "USER-DEFINED"

38 Views Asked by At

I have the following table:

CREATE table contact (
    id bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
    firstname text,
    lastname text,
    birthday timestamp with time zone NOT NULL,
    path ltree
);

I want to query the column names and their data type. I am using this:

SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'contact';

This gives the correct results except for the ltree data type for which is gives USER-DEFINED:

 column_name |        data_type         
-------------+--------------------------
 id          | bigint
 firstname   | text
 lastname    | text
 birthday    | timestamp with time zone
 path        | USER-DEFINED
(5 rows)

How can I get ltree as the data type instead?

1

There are 1 best solutions below

0
sudoExclamationExclamation On

I figured it out:

SELECT column_name, case when data_type = 'USER-DEFINED' then udt_name else data_type end as data_type FROM information_schema.columns WHERE table_name = 'contact';

gives:

 column_name |        data_type         
-------------+--------------------------
 id          | bigint
 firstname   | text
 lastname    | text
 birthday    | timestamp with time zone
 path        | ltree
(5 rows)

I learned about this trick from the docs:

data_type: Data type of the array elements, if it is a built-in type, else USER-DEFINED (in that case, the type is identified in udt_name and associated columns).

https://www.postgresql.org/docs/current/infoschema-element-types.html