【问题标题】:How to see defined comments on database objects?如何查看对数据库对象定义的注释?
【发布时间】:2018-05-05 16:05:23
【问题描述】:

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

指南postgresql.org/docs/functions-info不解释(很丑)。我尝试直观的SELECT obj_description('schemaName.tableName'::regclass) 并幸运地工作......但我需要

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

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

  3. 如何轻松(在 2018 年)列出模式的所有函数或所有表?

【问题讨论】:

    标签: sql postgresql descriptor


    【解决方案1】:
    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

    obj_description 的两参数形式返回一个注释 由其 OID 和包含的名称指定的数据库对象 系统目录。例如,obj_description(123456,'pg_class') 会 检索具有 OID 123456 的表的注释。一个参数 obj_description 的形式只需要对象 OID。已弃用 因为不能保证 OID 在不同的 系统目录;因此,可能会返回错误的评论。

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

    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)
    

    【讨论】:

    • 谢谢!您解决了第 1 项,并且非常完美。你能解决第 2 项和第 3 项吗?关于第 2 项,问题是指南说 obj_description(object_oid) 已弃用,所以我需要了解 catalog_name 才能使用未弃用的 obj_description(object_oid, catalog_name)
    • @PeterKrauss 我知道混乱来自哪里。并更新了答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 2011-01-10
    相关资源
    最近更新 更多