Home:ALL Converter>SQL access comments in postgreSQL

SQL access comments in postgreSQL

Ask Time:2016-08-10T15:28:43         Author:benoitespinola

Json Formatter

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

Author:benoitespinola,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/38866851/sql-access-comments-in-postgresql
yy