54

我正在 Postgres 数据库上运行一个项目,需要检索数据库中列上的注释以用作表标题等。我已经看到有几个内置函数(pg_descriptioncol_description),但我无法找到有关如何使用它们的示例,并且使用它们被证明是徒劳的。

所以我想知道以前是否有人能够做到这一点,如果可以,怎么做?

4

15 回答 15

61
SELECT c.table_schema,c.table_name,c.column_name,pgd.description
FROM pg_catalog.pg_statio_all_tables as st
  inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid)
  inner join information_schema.columns c on (pgd.objsubid=c.ordinal_position
    and  c.table_schema=st.schemaname and c.table_name=st.relname);
于 2011-02-09T14:38:59.977 回答
18

这一切都由oid工作,

mat=> SELECT c.oid FROM pg_catalog.pg_class c WHERE c.relname = 'customers';
  oid  
-------
 23208
(1 row)

现在,我有那个表的 oid,所以我可以问:

mat=> select pg_catalog.obj_description(23208);
  obj_description  
-------------------
 Customers
(1 row)

然后,我可以询问第四列的描述:

mat=> select pg_catalog.col_description(23208,4);
             col_description             
-----------------------------------------
 Customer codes, CHS, FACTPOST, POWER...
(1 row)

如果您想知道在执行或psql时确实运行了哪些查询,只需使用.\dt+\d+ customers-E

于 2008-12-05T07:51:14.123 回答
14

小心模式,这段代码认为它们:

SELECT
    cols.column_name, (
        SELECT
            pg_catalog.col_description(c.oid, cols.ordinal_position::int)
        FROM
            pg_catalog.pg_class c
        WHERE
            c.oid = (SELECT ('"' || cols.table_name || '"')::regclass::oid)
            AND c.relname = cols.table_name
    ) AS column_comment
FROM
    information_schema.columns cols
WHERE
    cols.table_catalog    = 'your_database'
    AND cols.table_name   = 'your_table'
    AND cols.table_schema = 'your_schema';

参考:

于 2014-03-20T23:55:37.147 回答
11

如果有人需要,就在这里。

这里有很多答案,但没有一个像我希望的那样简单。因此,基于以前的答案和当前的 postgres 9.4,我创建了这个查询:

SELECT 
    obj_description(format('%s.%s',isc.table_schema,isc.table_name)::regclass::oid, 'pg_class') as table_description,
    pg_catalog.col_description(format('%s.%s',isc.table_schema,isc.table_name)::regclass::oid,isc.ordinal_position) as column_description
FROM
    information_schema.columns isc

它获取表和列描述,没有任何令人困惑的连接和丑陋的字符串连接。

于 2015-03-04T07:30:50.077 回答
6

这适用于我使用 PostBooks 3.2.2 DB:

select cols.column_name,
(select pg_catalog.obj_description(oid) from pg_catalog.pg_class c where c.relname=cols.table_name) as table_comment
,(select pg_catalog.col_description(oid,cols.ordinal_position::int) from pg_catalog.pg_class c where c.relname=cols.table_name) as column_comment
from information_schema.columns cols
where cols.table_catalog='postbooks' and cols.table_name='apapply'

问候, 西林斯

于 2009-07-06T19:18:53.163 回答
6

对其他答案之一稍作更改,它只会为您提供对其有评论的列,这将为您提供所有列,无论它们是否有评论。

select c.table_schema, st.relname as TableName, c.column_name, 
pgd.description
from pg_catalog.pg_statio_all_tables as st
inner join information_schema.columns c
on c.table_schema = st.schemaname
and c.table_name = st.relname
left join pg_catalog.pg_description pgd
on pgd.objoid=st.relid
and pgd.objsubid=c.ordinal_position
where st.relname = 'YourTableName';
于 2018-04-23T15:39:14.597 回答
3

Enhance for @Nick and @mat suggestions: use
SELECT obj_description('schemaName.tableName'::regclass, 'pg_class'); when you have string name (not oid).

To avoid to remember 'pg_class' parameter, and to avoid ugly concatenations at the function calls, as (tname||'.'||schema)::regclass, an useful overload for obj_description:

  CREATE FUNCTION obj_description(
      p_rname text, p_schema text DEFAULT NULL, 
      p_catalname text DEFAULT 'pg_class'
  ) RETURNS text AS $f$
     SELECT obj_description((CASE 
        WHEN strpos($1, '.')>0 OR $2 IS NULL OR $2='' THEN $1
        ELSE $2||'.'||$1
     END)::regclass, $3);
  $f$ LANGUAGE SQL IMMUTABLE;
 -- USAGE: obj_description('mytable') 
 --        SELECT obj_description('s.t'); 
 -- PS: obj_description('s.t', 'otherschema') is a syntax error, 
 --     but not generates exception: returns the same as ('s.t') 

Now is easy to use, because the table name (rname parameter) is a varchar and can be expressed with a separated field for schema name, as in the main tables and queries.

See also "Getting list of table comments in PostgreSQL" or the new pg9.3 Guide

于 2012-10-08T10:26:45.313 回答
3

如果您只需要comments在其他数据中显示您的列,您还可以使用:

\d+ my_table
于 2020-06-30T09:26:54.933 回答
2

这个答案有点晚了,但它出现在我为研究这个问题所做的谷歌搜索中。我们只需要表格描述,但列的方法是相同的。列描述也在 pg_description 表中,由 objoid 引用。

添加此视图:


CREATE OR REPLACE VIEW our_tables AS 
 SELECT c.oid, n.nspname AS schemaname, c.relname AS tablename, d.description,
   pg_get_userbyid(c.relowner) AS tableowner, t.spcname AS "tablespace", 
   c.relhasindex AS hasindexes, c.relhasrules AS hasrules, c.reltriggers > 0 AS hastriggers
   FROM pg_class c
   LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
   LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
   LEFT JOIN pg_description d ON c.oid = d.objoid
  WHERE c.relkind = 'r'::"char";

ALTER TABLE our_tables OWNER TO postgres;
GRANT SELECT, UPDATE, INSERT, DELETE, REFERENCES, TRIGGER ON TABLE our_tables TO postgres;
GRANT SELECT ON TABLE our_tables TO public;

然后运行:

SELECT tablename, description FROM our_tables WHERE schemaname = 'public'

该视图是 pg_tables 视图的修改版本,它添加在描述列中。您还可以随意使用视图定义以使其成为单个查询。

于 2009-07-23T18:13:09.450 回答
2

我访问了这样的表格评论:

select c.relname table_name, pg_catalog.obj_description(c.oid) as comment from pg_catalog.pg_class c where c.relname = 'table_name';

因此,专栏评论:

SELECT c.column_name, pgd.description FROM pg_catalog.pg_statio_all_tables as st inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid) inner join information_schema.columns c on (pgd.objsubid=c.ordinal_position and c.table_schema=st.schemaname and c.table_name=st.relname and c.table_name = 'table_name' and c.table_schema = 'public');
于 2017-10-23T14:17:10.617 回答
1

我刚刚在这里找到了这个。它将为您提供一个特定表上的所有类型的元数据(类型、默认值、非空标志、长度、注释、外键名称、主键名称)。它似乎运作良好。

SELECT pg_tables.tablename, pg_attribute.attname AS field, 
    format_type(pg_attribute.atttypid, NULL) AS "type", 
    pg_attribute.atttypmod AS len,
    (SELECT col_description(pg_attribute.attrelid, 
            pg_attribute.attnum)) AS comment, 
    CASE pg_attribute.attnotnull 
        WHEN false THEN 1  ELSE 0  
    END AS "notnull", 
    pg_constraint.conname AS "key", pc2.conname AS ckey, 
    (SELECT pg_attrdef.adsrc FROM pg_attrdef 
        WHERE pg_attrdef.adrelid = pg_class.oid 
        AND pg_attrdef.adnum = pg_attribute.attnum) AS def 
FROM pg_tables, pg_class 
JOIN pg_attribute ON pg_class.oid = pg_attribute.attrelid 
    AND pg_attribute.attnum > 0 
LEFT JOIN pg_constraint ON pg_constraint.contype = 'p'::"char" 
    AND pg_constraint.conrelid = pg_class.oid AND
    (pg_attribute.attnum = ANY (pg_constraint.conkey)) 
LEFT JOIN pg_constraint AS pc2 ON pc2.contype = 'f'::"char" 
    AND pc2.conrelid = pg_class.oid 
    AND (pg_attribute.attnum = ANY (pc2.conkey)) 
WHERE pg_class.relname = pg_tables.tablename  
--    AND pg_tables.tableowner = "current_user"() 
    AND pg_attribute.atttypid <> 0::oid  
    AND tablename='your_table' 
ORDER BY field ASC

来源: http: //golden13.blogspot.de/2012/08/how-to-get-some-information-about_7.html

于 2015-09-22T13:09:26.800 回答
0

上个月我问了一个关于 Postgresql 评论的类似问题。如果你深入研究,你会在我的博客上看到一些 Perl 代码,它可以自动提取评论的过程。

要提取表的列名,可以使用如下内容:

select
     a.attname  as "colname"
    ,a.attrelid as "tableoid"
    ,a.attnum   as "columnoid"
from
    pg_catalog.pg_attribute a
    inner join pg_catalog.pg_class c on a.attrelid = c.oid
where
        c.relname = 'mytable' -- better to use a placeholder
    and a.attnum > 0
    and a.attisdropped is false
    and pg_catalog.pg_table_is_visible(c.oid)
order by a.attnum

然后,您可以使用 tableoid、columnoid 元组来提取每列的注释(请参阅我的问题)。

于 2008-12-08T21:41:17.097 回答
0

好的,所以我解决了这个问题......

选择 col_description(表 id,列号)...

即:选择 col_description(36698,2);

那行得通,但是有没有更简单的方法可以做到这一点,可能会在所有列上添加所有注释并使用表名而不是 oid?

于 2008-12-05T07:45:21.360 回答
0

要显示所有表的所有列的注释:

SELECT
    cols.table_name,
    cols.column_name, (
    SELECT
        pg_catalog.col_description(c.oid, cols.ordinal_position::int)
    FROM
        pg_catalog.pg_class c
    WHERE
            c.oid = (SELECT ('"' || cols.table_name || '"')::regclass::oid)
      AND c.relname = cols.table_name
) AS column_comment
FROM
    information_schema.columns cols
WHERE
  cols.table_name   IN (SELECT cols.table_name FROM information_schema.columns)
  AND cols.table_catalog = 'your_database_name'
  AND cols.table_schema = 'your_schema_name';

您需要在任何架构/目录/数据库之外执行此查询

此查询基于此问题中的另一个答案,该答案仅显示来自一个表的评论

于 2020-12-23T13:34:29.547 回答
0

扩展@amxy 提供的响应;我发现添加模式过滤器在某些环境中会有所帮助。正如我发现@amxy 的解决方案在我通过模式过滤器添加之前不起作用

SELECT 
pg_tables.schemaname,
pg_tables.TABLENAME,
pg_attribute.attname AS field,
format_type(pg_attribute.atttypid, NULL) AS "type",
pg_attribute.atttypmod AS len,
(
SELECT col_description(pg_attribute.attrelid, pg_attribute.attnum)) AS  COMMENT,
CASE pg_attribute.attnotnull
    WHEN FALSE THEN 1
    ELSE 0
END AS "notnull",
pg_constraint.conname AS "key", pc2.conname AS ckey,
(
SELECT pg_attrdef.adsrc
FROM pg_attrdef
WHERE pg_attrdef.adrelid = pg_class.oid
    AND pg_attrdef.adnum = pg_attribute.attnum) AS def
FROM pg_tables, pg_class
JOIN pg_attribute
    ON pg_class.oid = pg_attribute.attrelid
    AND pg_attribute.attnum > 0
LEFT JOIN pg_constraint
    ON pg_constraint.contype = 'p'::"char"
    AND pg_constraint.conrelid = pg_class.oid
    AND
(pg_attribute.attnum = ANY (pg_constraint.conkey))
LEFT JOIN pg_constraint AS pc2
    ON pc2.contype = 'f'::"char"
    AND pc2.conrelid = pg_class.oid
    AND (pg_attribute.attnum = ANY (pc2.conkey))
WHERE pg_class.relname = pg_tables.TABLENAME
AND pg_tables.schemaname IN ('op', 'im', 'cs','usr','li')
-- AND pg_tables.tableowner = "current_user"()
    AND pg_attribute.atttypid <> 0::oid
    ---AND TABLENAME='your_table'
ORDER BY pg_tables.schemaname,
pg_tables.TABLENAME ASC;

结果: 在此处输入图像描述

于 2021-11-14T23:29:42.507 回答