获取 PostgreSQL 中表注释的列表

发布于 2024-11-01 03:21:07 字数 216 浏览 0 评论 0 原文

Postgresql 允许向表等对象添加注释。例如,我使用以下 SQL 命令向表“mytable”添加了注释:

COMMENT ON TABLE mytable IS 'This is my table.';

我的问题是: 如果我想使用 SQL 命令获取所有表及其各自的注释 - 我该怎么做?对此合适的查询是什么?

提前致谢! 干杯!

Postgresql allows adding comments to objects such as tables. For example I've added a comment to table "mytable" by using this SQL command:

COMMENT ON TABLE mytable IS 'This is my table.';

My question is:
If I want to use a SQL-command to get all tables along with their respective comment - how would I do this? What would be the appropriate query for this?

Thanks in advance!
Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

匿名的好友 2024-11-08 03:21:07

所有评论都存储在 pg_description

中以获取评论一个表,您需要将其加入 pg_class

作为另一种方法是,您还可以使用函数 obj_description() 来检索此信息:

SELECT obj_description(oid)
FROM pg_class
WHERE relkind = 'r'

编辑

在 psql 中,您只需使用 \d+命令显示所有表格,包括它们的注释。或者使用\dd命令显示系统中所有评论

All comments are stored in pg_description

To get the comments on a table, you need to join it to pg_class

As an alternative you can also use the function obj_description() to retrieve this information:

SELECT obj_description(oid)
FROM pg_class
WHERE relkind = 'r'

Edit

In psql you can simply use the \d+ command to show all tables including their comments. Or use the \dd command to show all comments in the system

深居我梦 2024-11-08 03:21:07

“显示评论”的主要问题是记住特定功能的名称、目录名称等以检索评论......或其在指南上的页面。在这个答案中我们通过两种方式来解决:
通过普通方式(pg方式)的总结来显示评论;
并通过提供快捷功能,减少“记忆问题”。


pg-way

最简单,在psql上,是使用\dt+来显示表注释和\d+显示专栏评论。一些功能注释?

要使用 SQL,对于记住所有参数的人来说,pg 方法是使用 obj_description() 函数(指南)与足够的reg类型结合:

  • 函数: select obj_description('mySchema.myFunction'::regproc, 'pg_proc')

  • 表或视图:
    ("...以及大多数具有列或与表类似的其他内容",指南)
    选择 obj_description( 'mySchema.myClass'::regclass, 'pg_class')

  • 其他 通用:select obj_description('mySchema.myObject'::regName, pg_regName ),其中 regNamedatatype-oid 引用指南,和 pg_regName 相同,用前缀 pg_ 替换前缀 reg

  • 其他特定:类似select obj_description('schema.myObject'::regName, Catalog_name),其中catalog_name更具体有关(95 个中的 1 个)关键字,请参见目录指南。它可以减少一些“命名空间污染”。例如,pg_proc 用于函数,pg_aggregate 用于聚合函数。

  • 获取共享数据库对象的注释,模拟但使用函数shobj_description()(相同页面指南)。

  • select col_description('mySchema.myObject'::regClass, column_number),其中column_number是列的序号位置(位于CREATE TABLE)。
    没有列名称...请参见下面的col_description(table,column_name)补充。

重要提示:使用相同的reg类型和_catalog_name_(例如::regclasspg_class)似乎是多余的,有时obj_description(' obj'::regObj) 工作正常,只有 reg 类型! ...但是,正如指南所说:

它已被弃用,因为不能保证 OID 在不同系统目录中是唯一的;因此,可能会返回错误的评论。

获取注释的快捷函数

如果您发现很难记住所有类型转换和参数,最好是采用一个新的、最简单的函数来检索注释。

 CREATE FUNCTION rel_description(
     p_relname text, p_schemaname text DEFAULT NULL
 ) RETURNS text AS $f$
    SELECT obj_description((CASE 
       WHEN strpos($1, '.')>0 THEN $1
       WHEN $2 IS NULL THEN 'public.'||$1
       ELSE $2||'.'||$1
            END)::regclass, 'pg_class');
 $f$ LANGUAGE SQL;
 -- EXAMPLES OF USE:
 -- SELECT rel_description('mytable'); 
 -- SELECT rel_description('public.mytable'); 
 -- SELECT rel_description('otherschema.mytable'); 
 -- SELECT rel_description('mytable', 'otherschema'); 
 -- PS: rel_description('public.mytable', 'otherschema') is a syntax error, 
 --     but not generates exception: returns the same as ('public.mytable') 

我们还需要一些不那么难看的东西来显示专栏评论。没有任何一种 pg_get_serial_sequence() 函数可以从名称中获取的序号位置。原生
col_description('mySchema.myObject'::regClass, column_number)
需要补充:

 CREATE FUNCTION col_description(
    p_relname text,  -- table name or schema.table 
    p_colname text,   -- table's column name
    p_database text DEFAULT NULL -- NULL for current
 ) RETURNS text AS $f$
    WITH r AS (
      SELECT CASE WHEN array_length(x,1)=1 THEN array['public',x[1]] ELSE x END
      FROM regexp_split_to_array(p_relname,'\.') t(x)
     ) 
    SELECT col_description(p_relname::regClass, ordinal_position)
    FROM r, information_schema.columns i
    WHERE i.table_catalog = CASE 
        WHEN $3 IS NULL THEN current_database() ELSE $3 
      END and i.table_schema  = r.x[1]
      and i.table_name    = r.x[2]
      and i.column_name = p_colname
 $f$ LANGUAGE SQL;
 -- SELECT col_description('tableName','colName'); 
 -- SELECT col_description('schemaName.tableName','colName','databaseName); 

注意:

  • 按照 这个答案:“如果您想知道当您执行 \dt+ 或 \d+ 客户时 psql 运行哪些查询,只需使用 psql 启动它 - E”。

  • 可以使用任何多行字符串(带有 E\n$$...$$)...
    但是您不能应用 trim() 或使用其他动态方面。必须在 动态 SQL comment.html" rel="noreferrer">COMMENT 子句。

  • 没有评论可看? PostgreSQL 程序员不使用 COMMENT 子句,因为它使用起来很难看:没有语法可以在 CREATE TABLE 或 CREATE FUNCTION 上添加注释;并且没有好的 IDE 来自动化它。

  • 现代的http://postgREST.org/界面在网络上显示评论!

The main problem with "show comments" is to remember the name of specific fucntions, catalog names, etc. to retrieve the comment... Or its pages on the Guide. At this answer we solve in 2 ways:
by a summary of the ordinary way (the pg-way) to show comments;
and by offering shortcut functions, to reduce the "remember problem".


The pg-way

The simplest, on psql, is to use \dt+ to show table comments and \d+ to show column comments. Some for function comments?

To get on SQL, and for people that remember all parameters, the pg-way is to use the obj_description() function (Guide) in conjunction with adequate reg-type:

  • Function: select obj_description('mySchema.myFunction'::regproc, 'pg_proc')

  • Table or View:
    ("... and most everything else that has columns or is otherwise similar to a table",guide)
    select obj_description('mySchema.myClass'::regclass, 'pg_class')

  • other generic: select obj_description('mySchema.myObject'::regName, pg_regName), where regName is 1 in 10 of datatype-oid references Guide, and pg_regName is the same replacing prefix reg by prefix pg_.

  • other specific: similar select obj_description('schema.myObject'::regName, catalog_name), where catalog_name is to be more specific about a (1 in 95) key-word at catalogs Guide. It can reduce some "namespace pollution". For example pg_proc for functions, pg_aggregate for aggregate functions.

  • to get comment for a shared database object, analog but using the function shobj_description() (same page Guide).

  • Column: select col_description('mySchema.myObject'::regClass, column_number), where column_number is the column's ordinal position (at the CREATE TABLE).
    No column-name... See col_description(table,column_name) complement bellow.

IMPORTANT: the use of same reg-type and _catalog_name_ (e. g. ::regclass and pg_class) seems redundant and sometimes obj_description('obj'::regObj) works fine, with only reg-type! ...But, as the Guide say:

it is deprecated since there is no guarantee that OIDs are unique across different system catalogs; therefore, the wrong comment might be returned.

Shortcut functions to get comments

if you are finding it difficult to remember all the type-casts and parameters, the best is to adopt a new and simplest function to retrieve comments.

 CREATE FUNCTION rel_description(
     p_relname text, p_schemaname text DEFAULT NULL
 ) RETURNS text AS $f$
    SELECT obj_description((CASE 
       WHEN strpos($1, '.')>0 THEN $1
       WHEN $2 IS NULL THEN 'public.'||$1
       ELSE $2||'.'||$1
            END)::regclass, 'pg_class');
 $f$ LANGUAGE SQL;
 -- EXAMPLES OF USE:
 -- SELECT rel_description('mytable'); 
 -- SELECT rel_description('public.mytable'); 
 -- SELECT rel_description('otherschema.mytable'); 
 -- SELECT rel_description('mytable', 'otherschema'); 
 -- PS: rel_description('public.mytable', 'otherschema') is a syntax error, 
 --     but not generates exception: returns the same as ('public.mytable') 

We need also something less ugly to show column comments. There are no kind of pg_get_serial_sequence() function to get ordinal position of a column from its name. The native
col_description('mySchema.myObject'::regClass, column_number)
needs a complement:

 CREATE FUNCTION col_description(
    p_relname text,  -- table name or schema.table 
    p_colname text,   -- table's column name
    p_database text DEFAULT NULL -- NULL for current
 ) RETURNS text AS $f$
    WITH r AS (
      SELECT CASE WHEN array_length(x,1)=1 THEN array['public',x[1]] ELSE x END
      FROM regexp_split_to_array(p_relname,'\.') t(x)
     ) 
    SELECT col_description(p_relname::regClass, ordinal_position)
    FROM r, information_schema.columns i
    WHERE i.table_catalog = CASE 
        WHEN $3 IS NULL THEN current_database() ELSE $3 
      END and i.table_schema  = r.x[1]
      and i.table_name    = r.x[2]
      and i.column_name = p_colname
 $f$ LANGUAGE SQL;
 -- SELECT col_description('tableName','colName'); 
 -- SELECT col_description('schemaName.tableName','colName','databaseName); 

NOTES:

  • As recommended by this answer: "If you want to know which queries does psql run when you do \dt+ or \d+ customers, just launche it with psql -E".

  • It is possible to express multiline comment, using any multiline string (with E\n or $$...$$)...
    But you can't apply trim() or use another dynamic aspect. Must use dynamic SQL on COMMENT clause for it.

  • No comments to see? PostgreSQL programmers not use COMMENT clause because it is ugly to use: there are no syntax to add comment on CREATE TABLE or on CREATE FUNCTION; and there are no good IDE to automatize it.

  • The modern http://postgREST.org/ interface show comments on the Web!

浮光之海 2024-11-08 03:21:07

您可以使用 pg_catalog.obj_description 函数和 information_schema.tables 架构视图:

SELECT t.table_name, pg_catalog.obj_description(pgc.oid, 'pg_class')
FROM information_schema.tables t
INNER JOIN pg_catalog.pg_class pgc
ON t.table_name = pgc.relname 
WHERE t.table_type='BASE TABLE'
AND t.table_schema='public';

FUNCTIONS-INFO-COMMENT-TABLE

MySQL、PostgreSQL 中的 INFORMATION_SCHEMA 支持

You can use pg_catalog.obj_description function and information_schema.tables schema view:

SELECT t.table_name, pg_catalog.obj_description(pgc.oid, 'pg_class')
FROM information_schema.tables t
INNER JOIN pg_catalog.pg_class pgc
ON t.table_name = pgc.relname 
WHERE t.table_type='BASE TABLE'
AND t.table_schema='public';

FUNCTIONS-INFO-COMMENT-TABLE

INFORMATION_SCHEMA Support in MySQL, PostgreSQL

德意的啸 2024-11-08 03:21:07

如果您仍然有混合大小写名称的表,您可以使用以下命令获取数据库中所有表及其注释的完整列表,以及目录、模式、表类型等(在 PostGIS - pgAdmin 4 中测试):

select *,
    obj_description((table_schema||'.'||quote_ident(table_name))::regclass)
from information_schema.tables where table_schema <> 'pg_catalog' 

If you still have tables with mixed case names you can use the following to get a complete list of all tables in a database with their comment, as well as catalog, schema, table type, etc. (tested in PostGIS - pgAdmin 4):

select *,
    obj_description((table_schema||'.'||quote_ident(table_name))::regclass)
from information_schema.tables where table_schema <> 'pg_catalog' 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文