Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the table row count of all the tables present in particular schema in postgresql 9.5?

how to get the table row count of all the tables present in particular schema in postgresql 9.5? I would like to have the result as table_name | row_count. How this can be done using query?

like image 915
Karthik Avatar asked Dec 01 '22 11:12

Karthik


1 Answers

This can be done with some XML magic:

select table_schema, table_name,
       (xpath('/row/count/text()', query_to_xml('select count(*) from '||format('%I.%I', table_schema, table_name), true, true, '')))[1]::text::int as row_count
from information_schema.tables
where table_schema = 'public'
like image 182
a_horse_with_no_name Avatar answered Dec 06 '22 16:12

a_horse_with_no_name