Skip to content

Instantly share code, notes, and snippets.

@hbasria
Last active June 21, 2019 06:16
Show Gist options
  • Select an option

  • Save hbasria/bb45da34566946852f86 to your computer and use it in GitHub Desktop.

Select an option

Save hbasria/bb45da34566946852f86 to your computer and use it in GitHub Desktop.
SELECT
pgClass.relname AS tableName,
pgClass.reltuples AS rowCount
FROM
pg_class pgClass
LEFT JOIN
pg_namespace pgNamespace ON (pgNamespace.oid = pgClass.relnamespace)
WHERE
pgNamespace.nspname NOT IN ('pg_catalog', 'information_schema') AND
pgClass.relkind='r'
--OR--
create or replace function
count_rows(schema text, tablename text) returns integer
as
$body$
declare
result integer;
query varchar;
begin
query := 'SELECT count(1) FROM ' || schema || '.' || tablename;
execute query into result;
return result;
end;
$body$
language plpgsql;
--
select
table_schema,
table_name,
count_rows(table_schema, table_name)
from information_schema.tables
where
table_schema not in ('pg_catalog', 'information_schema')
and table_type='BASE TABLE'
order by 3 desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment