获取 PostgreSQL 中表注释的列表
Postgresql 允许向表等对象添加注释。例如,我使用以下 SQL 命令向表“mytable”添加了注释:
COMMENT ON TABLE mytable IS 'This is my table.';
我的问题是: 如果我想使用 SQL 命令获取所有表及其各自的注释 - 我该怎么做?对此合适的查询是什么?
提前致谢! 干杯!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所有评论都存储在 pg_description
中以获取评论一个表,您需要将其加入 pg_class
作为另一种方法是,您还可以使用函数
obj_description()
来检索此信息:编辑
在 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: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“显示评论”的主要问题是记住特定功能的名称、目录名称等以检索评论......或其在指南上的页面。在这个答案中我们通过两种方式来解决:
通过普通方式(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 )
,其中regName
是 datatype-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_(例如
::regclass
和pg_class
)似乎是多余的,有时obj_description(' obj'::regObj)
工作正常,只有 reg 类型! ...但是,正如指南所说:获取注释的快捷函数
如果您发现很难记住所有类型转换和参数,最好是采用一个新的、最简单的函数来检索注释。
我们还需要一些不那么难看的东西来显示专栏评论。没有任何一种
pg_get_serial_sequence()
函数可以从名称中获取列的序号位置。原生col_description('mySchema.myObject'::regClass, column_number)
需要补充:
注意:
按照 这个答案:“如果您想知道当您执行 \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)
, whereregName
is 1 in 10 of datatype-oid references Guide, and pg_regName is the same replacing prefixreg
by prefixpg_
.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 examplepg_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
andpg_class
) seems redundant and sometimesobj_description('obj'::regObj)
works fine, with only reg-type! ...But, as the Guide say: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.
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 nativecol_description('mySchema.myObject'::regClass, column_number)
needs a complement:
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!
您可以使用 pg_catalog.obj_description 函数和 information_schema.tables 架构视图:
FUNCTIONS-INFO-COMMENT-TABLE
MySQL、PostgreSQL 中的 INFORMATION_SCHEMA 支持
You can use
pg_catalog.obj_description
function andinformation_schema.tables
schema view:FUNCTIONS-INFO-COMMENT-TABLE
INFORMATION_SCHEMA Support in MySQL, PostgreSQL
如果您仍然有混合大小写名称的表,您可以使用以下命令获取数据库中所有表及其注释的完整列表,以及目录、模式、表类型等(在 PostGIS - pgAdmin 4 中测试):
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):