FUNCTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
CREATE OR REPLACE FUNCTION 
get_count(schema text, tablename text) RETURNS integer
AS
$body$
DECLARE
  count integer;
  query varchar;
BEGIN
  query := 'SELECT count(1) FROM ' || schema || '.' || tablename;
  EXECUTE query INTO count;
  RETURN count;
END;
$body$
LANGUAGE plpgsql;

Query to get count

Greenplum

1
2
3
4
5
6
7
8
9
10
11
SELECT
     n.nspname as "Schema",
     c.relname as "Table",
     get_count(n.nspname, c.relname)
FROM pg_catalog.pg_class c
     JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
AND c.relstorage != 'x'  -- excludes external tables
AND n.nspname not in ('pg_catalog','information_schema')
AND c.oid not in (SELECT inhrelid from pg_inherits)  --excludes child partition tables
ORDER BY 3 desc;

PostgreSQL

1
2
3
4
5
6
7
8
9
10
SELECT
     n.nspname as "Schema",
     c.relname as "Table",
     get_count(n.nspname, c.relname)
FROM pg_catalog.pg_class c
     JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
AND n.nspname not in ('pg_catalog','information_schema')
AND c.oid not in (SELECT inhrelid from pg_inherits)  --excludes child partition tables
ORDER BY 3 desc;

Result

Schema Table Row count
public ttblsize 10416272
public test_yogesh 11000
public tdbsize 7129
public reorg_history 190
public vacuum_history 23
public test11 20
public test_after_upgrade 5
public test_sravan 3
public test12 1