2

My database has 50+ schemas. Each schema has around 100 tables and some views.

I want a query that tells me the schema name, table name, and total number of rows in each table. The below query:

select table_schema, table_name, table_type, count(*) 
from information_schema.tables 
group by table_schema, table_name, table_type

...gives the total number of tables with each name, rather than giving the row count. I am using pgAdmin 3 version 1.10

Paul White
95.4k30 gold badges440 silver badges689 bronze badges
asked Mar 25, 2016 at 14:01
1
  • Normally you get the number of rows in a table by querying the table itself: SELECT COUNT(*) FROM tablename. Unless PostgreSQL stores row counts externally somewhere, you will probably need to use dynamic SQL: generate a SELECT COUNT(*) FROM... query for every table of every schema listed in information_schema.tables and then execute the resulting script. Commented Mar 25, 2016 at 14:30

1 Answer 1

1

This should do the trick:

select nspname, relname, relkind, reltuples from pg_class c
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
where relkind in ('r', 'v');

Note that the reltuples is an estimate of the rows not the actual count at the moment but generally it's quite accurate (depends on how up to date your statistics are).

answered Mar 25, 2016 at 14:32
1
  • Hi. Thanks for proposing a solution but this is not giving the accurate count. Commented Mar 28, 2016 at 9:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.