如何查看数据库对象定义的注释?

How to see defined comments on database objects?

使用 COMMENT ON IS 的主要动机是生成智能文档报告,通过 SQL 进行操作。

指南postgresql.org/docs/functions-info不解释(丑)。我尝试直觉 SELECT obj_description('schemaName.tableName'::regclass) 并且幸运地成功了...但我需要

  1. 对函数名做同样的事情,但是name::regclass对函数不起作用。

  2. 看看catalog_name是什么?对于函数、表和视图。

  3. 如何轻松地(在现在 2018 年)列出架构的所有函数或所有表?

so=# create function t.fn() returns int as
$$
begin return 1; end; $$ language plpgsql;
CREATE FUNCTION
so=# comment on function t.fn() is 'some comment';
COMMENT
so=# select * from obj_description('t.fn'::regproc);
 obj_description
-----------------
 some comment
(1 row)

regclass是为了关系,为了函数使用regproc

更新

https://www.postgresql.org/docs/current/static/functions-info.html#FUNCTIONS-INFO-COMMENT-TABLE

The two-parameter form of obj_description returns the comment for a database object specified by its OID and the name of the containing system catalog. For example, obj_description(123456,'pg_class') would retrieve the comment for the table with OID 123456. The one-parameter form of obj_description requires only the object OID. It is deprecated since there is no guarantee that OIDs are unique across different system catalogs; therefore, the wrong comment might be returned.

函数 oid 存储在 pg_proc 中,表和视图存储在 pg_class 中(分别为 relkind rv),因此:

  1. select * from obj_description('t.fn'::regproc)
  2. pg_class 表和视图,pg_proc 函数
  3. 以下是查询:

对于所有函数(我添加 orderlimit 以显示 UDF 小列表):

so=# select oid::regproc,obj_description(oid,tableoid::regclass::text) 
from pg_proc 
order by oid desc 
limit 2;
 oid  | obj_description
------+-----------------
 t.fn | some comment
 a    | blah
(2 rows) 

对于所有表:

so=# select oid::regclass,obj_description(oid,tableoid::regclass::text) from pg_class where relkind = 'r' order by oid desc limit 1;
      oid      |   obj_description
---------------+---------------------
 t."WeirdMix$" | table with bad name
(1 row)

浏览量分别为:

so=# select oid::regclass,obj_description(oid,tableoid::regclass::text) from pg_class where relkind = 'v' order by oid desc limit 1;
 oid | obj_description
-----+-----------------
 v   | view this is
(1 row)