有没有一种方法可以显示用户定义的postgresql枚举类型定义?

nvbavucw  于 2023-04-29  发布在  PostgreSQL
关注(0)|答案(5)|浏览(114)

假设我们定义了一个postgresql类型:

CREATE TYPE my_type AS ENUM('foo', 'bar');

是否有任何方法可以在创建后显示类型定义?
我希望“\d my_type”显示“ENUM('foo ',' bar ')”,但它说:

Did not find any relation named "my_type"

pg_type表似乎没有给予足够的信息。

nr9pn0ug

nr9pn0ug1#

看看这个

select enum_range(null::my_type)

我认为这是一个简单得多的解决方案:)。

rsaldnfx

rsaldnfx2#

你要找的是\dT,但它没有以“CREATE”语句的形式给予它。对于域使用\dD。

\dT+ action.action_status
                          List of data types
 Schema |         Name         | Internal name | Size | Elements | Description 
--------+----------------------+---------------+------+----------+-------------
 action | action.action_status | action_status | 4    | pending +| 
        |                      |               |      | live    +| 
        |                      |               |      | done    +| 
        |                      |               |      | notdone  | 
(1 row)
ycl3bljg

ycl3bljg3#

The manual:
enum_rangeanyenum)→ anyarray
在有序数组中返回输入枚举类型的所有值。
要获取模式,请执行以下操作:

SELECT typnamespace::regnamespace AS "schema", typname
     , enum_range(null::my_enum_type)
FROM   pg_catalog.pg_type 
WHERE  typname = 'my_enum_type';

退货:

schema | typname      | enum_labels
--------+--------------+-------------
 public | my_enum_type | {foo,bar}

您还可以从转储文件中获取SQL CREATE语句。使用pg_dump --schema-only ...从仅模式转储中最便宜。
或者简单地使用pgAdmin。在object browser中选择对象,其创建脚本将显示在SQL pane中。

py49o6xq

py49o6xq4#

SELECT t.typname
FROM pg_class c JOIN pg_attribute a ON c.oid = a.attrelid JOIN pg_type t ON a.atttypid = t.oid
WHERE c.relname = 'your_type';

棘手的部分是,简单地从这些视图中选择 *,结果中不会得到OID。

iszxjhcz

iszxjhcz5#

使用this post,我已经在PgAdmin和PgBackup中实现了模仿'CREATE TYPE'的目标

WITH types AS (
    SELECT n.nspname,
            pg_catalog.format_type ( t.oid, NULL ) AS obj_name,
            CASE
                WHEN t.typrelid != 0 THEN CAST ( 'tuple' AS pg_catalog.text )
                WHEN t.typlen < 0 THEN CAST ( 'var' AS pg_catalog.text )
                ELSE CAST ( t.typlen AS pg_catalog.text )
                END AS obj_type,
            coalesce ( pg_catalog.obj_description ( t.oid, 'pg_type' ), '' ) AS description
        FROM pg_catalog.pg_type t
        JOIN pg_catalog.pg_namespace n
            ON n.oid = t.typnamespace
        WHERE ( t.typrelid = 0
                OR ( SELECT c.relkind = 'c'
                        FROM pg_catalog.pg_class c
                        WHERE c.oid = t.typrelid ) )
            AND NOT EXISTS (
                    SELECT 1
                        FROM pg_catalog.pg_type el
                        WHERE el.oid = t.typelem
                        AND el.typarray = t.oid )
            AND n.nspname <> 'pg_catalog'
            AND n.nspname <> 'information_schema'
            AND n.nspname !~ '^pg_toast'
),
cols AS (
    SELECT n.nspname::text AS schema_name,
            pg_catalog.format_type ( t.oid, NULL ) AS obj_name,
            a.attname::text AS column_name,
            pg_catalog.format_type ( a.atttypid, a.atttypmod ) AS data_type,
            a.attnotnull AS is_required,
            a.attnum AS ordinal_position,
            pg_catalog.col_description ( a.attrelid, a.attnum ) AS description
        FROM pg_catalog.pg_attribute a
        JOIN pg_catalog.pg_type t
            ON a.attrelid = t.typrelid
        JOIN pg_catalog.pg_namespace n
            ON ( n.oid = t.typnamespace )
        JOIN types
            ON ( types.nspname = n.nspname
                AND types.obj_name = pg_catalog.format_type ( t.oid, NULL ) )
        WHERE a.attnum > 0
            AND NOT a.attisdropped
)
SELECT 'CREATE TYPE ' || cols.schema_name || '.' || cols.obj_name || E' AS (\n    ' ||
pg_catalog.array_to_string (ARRAY( 
    SELECT cols.column_name || ' ' || cols.data_type AS col_num_typ
    FROM cols
    WHERE cols.obj_name='my_user_data_type'
    ORDER BY cols.schema_name,
            cols.obj_name,
            cols.ordinal_position ), E',\n    '
    ) || E'\n);'
AS cre_typ
FROM cols
WHERE cols.obj_name='my_user_data_type'
LIMIT 1

并在psql下使用此命令运行它,以仅获得SQL代码:
\t\a\g\a\t

相关问题