gotchasqlMinor
Unexpected result from PostgreSQL information schema
Viewed 0 times
postgresqlresultunexpectedfromschemainformation
Problem
I have written a simple query that should show all table sizes of any given schema in human-readable format:
When I run this query in PgAdmin, I get the following error:
How can this error even be possible? I have not changed the information_schema at all and, if a relation doesn't exist in the first place, why would it be in the information_schema ? Any idea how this could happen?
select table_name, pg_relation_size(quote_ident(table_name))
from information_schema.tables
where table_schema = 'my_schema'
order by 2When I run this query in PgAdmin, I get the following error:
ERROR: relation "my_table" does not exist
SQL state: 42P01How can this error even be possible? I have not changed the information_schema at all and, if a relation doesn't exist in the first place, why would it be in the information_schema ? Any idea how this could happen?
Solution
The only reason I can think of, is if
As you pass a table name without schema qualifying it to the function
Use a fully qualified name instead:
my_schema is not part of your search_path.As you pass a table name without schema qualifying it to the function
pg_relation_size() the table is searched in the default search_path. If it's not found, you'll get that error.Use a fully qualified name instead:
pg_relation_size(format('%I.%I', table_schema, table_name))Code Snippets
pg_relation_size(format('%I.%I', table_schema, table_name))Context
StackExchange Database Administrators Q#285365, answer score: 5
Revisions (0)
No revisions yet.