tables meta data collection

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Everyone,

I was trying to collect table metadata with a description; the use case is that I need to show all columns of the tables whether it has the description or not. 

I tried the below query, but it only gives column details that have a description and ignore others if not. 


Postgres 11 | db<>fiddle





create table test(id int);
create table test1(id int Primary key );
comment on column test.id is 'Test descr';


 SELECT c.table_schema,c.table_name,c.column_name,
case 
    when c.domain_name is not null then c.domain_name
    when c.data_type='character varying' THEN 'character varying('||c.character_maximum_length||')'
    when c.data_type='character' THEN 'character('||c.character_maximum_length||')'
    when c.data_type='numeric' THEN 'numeric('||c.numeric_precision||','||c.numeric_scale||')'
    else c.data_type
end as data_type,
c.is_nullable, 
(select 'Y' from information_schema.table_constraints tco
join information_schema.key_column_usage kcu 
     on kcu.constraint_name = tco.constraint_name
     and kcu.constraint_schema = tco.constraint_schema
     and kcu.constraint_schema = c.table_schema
 and kcu.table_name = c.table_name
 and kcu.column_name = c.column_name
where tco.constraint_type = 'PRIMARY KEY' ) as is_in_PK,
(select distinct 'Y' from information_schema.table_constraints tco
join information_schema.key_column_usage kcu 
     on kcu.constraint_name = tco.constraint_name
     and kcu.constraint_schema = tco.constraint_schema
     and kcu.constraint_schema = c.table_schema
 and kcu.table_name = c.table_name
 and kcu.column_name = c.column_name
where tco.constraint_type = 'FOREIGN KEY' ) as is_in_FK,
pgd.description


FROM pg_catalog.pg_statio_all_tables as st
  Left outer join pg_catalog.pg_description pgd on (pgd.objoid=st.relid)
  left 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 c.table_name='test'
order by c.table_schema,c.table_name,c.ordinal_position; 


expected formate is :

table_schematable_namecolumn_namedata_typeis_nullableis_in_pkis_in_fkdescription


any suggestions?

Thanks,
Rj


[Postgresql General]     [Postgresql PHP]     [PHP Users]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Yosemite]

  Powered by Linux