0

I have a postgresql script which selects all information about tables and views for specific database. I'm trying to add a additional column which will display comment associated with a table or a view. But I'm getting an error SQL state: 42P01, ERROR: relation "xxx" does not exist.

select tbl.*, (select obj_description(tbl.table_name::regclass::oid, 'pg_class')) as DESCRIPTION 
from INFORMATION_SCHEMA.TABLES tbl 
where tbl.table_schema <> 'pg_catalog' and tbl.table_schema <> 'information_schema' 
order by tbl.table_schema, tbl.table_name

Unfortunately I have no idea what I'm doing wrong.

3
  • 2
    In few words: obj_description((tbl.table_schema || '.' || tbl.table_name)::regclass - use fully qualified names, including the schema name. Commented Aug 8, 2016 at 10:36
  • @Abelisto Thanks, it works. Commented Aug 8, 2016 at 11:08
  • 1
    Actually the right answer is a combination of my comment and the answer of @LaurenzAlbe. Also you can avoid subquery in the select clause so it could be: select tbl.*, obj_description(format('%I.%I',tbl.table_schema, tbl.table_name)::regclass, 'pg_class') as DESCRIPTION from ... Good luck. Commented Aug 8, 2016 at 11:21

1 Answer 1

1

This incorporates Abelisto's comments above.

You should use

obj_description(
   (quote_ident(tbl.table_name) || '.' || quote_ident(tbl.table_name))::regclass::oid,
   'pg_class'
)

instead of

(select obj_description(tbl.table_name::regclass::oid, 'pg_class'))

I guess that some of your table names are not all lower case.

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.