7

I want to for selected table in selected schema get desired result like this

column_name  |  data_type   | description
-----------------------------------------
id           | integer      |  id of bill
name         | character    |
address      | character    | adress of buyer

notice that some columns don't have description (column comment).

For now i got only this query which gives me good result but only for columns which got comments (for columns that are in the selected table and don't have comment are not represented in output).

MY query which returns only data for columns that have comment is next

SELECT c.column_name, c.data_type, 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)  
where table_schema = 'public' and table_name = 'some_table';

How to get columns without comment in result?

2
  • just use LEFT JOIN. INNER JOIN gives you an intersection of two tables. If there is no relation from a row in table A to table B, that row won't be included in the output. Commented Jun 12, 2017 at 6:57
  • tried with left join before posting question and still same result Commented Jun 12, 2017 at 6:58

3 Answers 3

11

There is the function col_description(table_oid, column_number)

select 
    column_name, 
    data_type, 
    col_description('public.some_table'::regclass, ordinal_position)
from information_schema.columns
where table_schema = 'public' and table_name = 'some_table';

Test it in db<>fiddle.

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

Comments

4

because information_schema.columns is the table with data for sure, and you reference it not the first, you need right outer join instead:

t=# SELECT c.column_name, c.data_type, pgd.description
from pg_catalog.pg_statio_all_tables as st
inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid)
right outer join information_schema.columns c on (pgd.objsubid=c.ordinal_position and  c.table_schema=st.schemaname and c.table_name=st.relname)
where table_schema = 'public' and table_name = 's150';
  column_name  | data_type | description
---------------+-----------+-------------
 customer_name | text      |
 order_id      | text      |
 order_date    | date      |
(3 rows)

Comments

2

Use col_description() on pg_attribute:

select
  attname as column_name,
  atttypid::regtype as data_type,
  col_description(attrelid, attnum) as description
from pg_attribute
where attrelid = '(myschema.)mytable'::regclass
  and attnum > 0        -- hide system columns
  and not attisdropped  -- hide dead/dropped columns
order by attnum;

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.