SQL access comments in postgreSQL

☆樱花仙子☆ 提交于 2020-07-23 05:23:05

问题


I have been trying to find a SQL approach to retrieve comments of my schemas and other database objects in PostgreSQL.

I have seen the following questions in stackoverflow: How to retrieve the comment of a PostgreSQL database? which shows how to fetch comments for databases, with the following code:

SELECT * FROM pg_shdescription JOIN pg_database ON objoid =
pg_database.oid;

Getting list of table comments in PostgreSQL which shows how to get comments for tables, the command is SELECT obj_description('myschema.mytable'::regclass); for a specific table in a schema.

Retrieving Comments from a PostgreSQL DB which shows how to fetch the comments for all the columns in the database, the command is :

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);

These assess the question of fetching comments from a database, tables and columns, but they do not give the answer for other database objects such as schemas.

I have taken a look at obj_description(object_oid, catalog_name) but did not manage to make it work... for schemas, views, etc.

(I am mainly interested in schemas, but since there are other situations that might interest other developers, I would like to have all the solutions in one place).

Any clues?

EDIT: found a way to get the comments from schemas... Seems a bit complicated, could be simplier. Here is the code:

SELECT

CASE 
    WHEN nspname LIKE E'pg\_temp\_%' THEN 1 
    WHEN (nspname LIKE E'pg\_%') THEN 0  
    ELSE 3 
END AS nsptyp, nsp.nspname, nsp.oid, pg_get_userbyid(nspowner) 
    AS namespaceowner, 
    nspacl, description,  
    has_schema_privilege(nsp.oid, 'CREATE') as cancreate 
FROM pg_namespace nsp 
LEFT OUTER JOIN pg_description des ON des.objoid=nsp.oid  
WHERE NOT ((nspname = 'pg_catalog' AND EXISTS 
(SELECT 1 FROM pg_class 
    WHERE relname = 'pg_class' 
    AND relnamespace = nsp.oid LIMIT 1)) OR  
(nspname = 'information_schema' AND 
    EXISTS (SELECT 1 FROM pg_class 
            WHERE relname = 'tables' 
            AND relnamespace = nsp.oid LIMIT 1)) OR  
(nspname LIKE '_%' AND 
    EXISTS (SELECT 1 FROM pg_proc 
            WHERE proname='slonyversion' 
            AND pronamespace = nsp.oid LIMIT 1)) OR  
(nspname = 'dbo' AND 
    EXISTS (SELECT 1 FROM pg_class 
            WHERE relname = 'systables' 
            AND relnamespace = nsp.oid LIMIT 1)) OR  
(nspname = 'sys' AND 
    EXISTS (SELECT 1 FROM pg_class 
            WHERE relname = 'all_tables' 
            AND relnamespace = nsp.oid LIMIT 1))
) 
AND nspname NOT LIKE E'pg\_temp\_%'
AND nspname NOT LIKE E'pg\_toast_temp\_%' 
ORDER BY 1, nspname

As the original code is quite lengthy and I like concise solutions, here is something better:

SELECT * FROM pg_namespace AS nsp LEFT OUTER JOIN pg_description AS des ON des.objoid=nsp.oid;

The long code basically excludes a bunch of schemas from PostgreSQL (though I do not know why it doesn't hide pg_toast on my machine).


回答1:


Ok, after a lot of research, I have come to the conclusion that one can use the following code to get descriptions of objects in PostgreSQL:

SELECT * FROM [pg_catalog_table] AS nsp LEFT OUTER JOIN pg_description AS des ON des.objoid=nsp.oid;

The [pg_catalog_table] should be substituted by a table in the pg_catalog which contains an OID column... For instance (but not limited): pg_type ; pg_namespace ; etc.

And use this code

SELECT * FROM pg_database AS nsp  LEFT OUTER JOIN pg_shdescription AS sdes ON sdes.objoid=nsp.oid;

for descriptions from databases.

For views is a bit more tricky as they are listed in pg_class which also lists the rest of the objects (more work to do here).

There doesn't seem to be comments for servers.



来源:https://stackoverflow.com/questions/38866851/sql-access-comments-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!