PostgreSQL all tables count Row

Fatih Şahin - Nov 10 '22 - - Dev Community
drop table zzz_table_schema_rows_n;

create table zzz_table_schema_rows_n as 
WITH tbl AS
  (SELECT table_schema,
          TABLE_NAME
   FROM information_schema.tables
   WHERE
    1=1
    AND TABLE_NAME not like 'pg_%'
    AND TABLE_TYPE='BASE TABLE'
    AND table_schema in ('uyumsoft'))
SELECT table_schema,
       TABLE_NAME,
       (xpath('/row/c/text()', query_to_xml(format('select count(*) as c from %I.%I', table_schema, TABLE_NAME), FALSE, TRUE, '')))[1]::text::int AS rows_n
FROM tbl
ORDER BY rows_n DESC;


select * from zzz_table_schema_rows_n

Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .