2

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
  • I wonder why something like COMMENT isn't/wasn't part of the SQL standard, or why there is a command to add a comment but not retrieve them? Seems really useful to have... Commented Nov 1, 2018 at 16:53

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.