Skip to content

Instantly share code, notes, and snippets.

@dincosman
Last active December 5, 2024 20:58
Show Gist options
  • Save dincosman/4929e37069c864fb9fe6cb9b09af13f4 to your computer and use it in GitHub Desktop.
Save dincosman/4929e37069c864fb9fe6cb9b09af13f4 to your computer and use it in GitHub Desktop.
Get Row Counts for Each Table in PostgreSQL Database
CREATE FUNCTION pg_temp.get_rows(p_schemaname text, p_tablename text)
RETURNS bigint AS $$
DECLARE
result_rowcount bigint;
BEGIN
EXECUTE format('SELECT COUNT(*) FROM "%I"."%I"', p_schemaname, p_tablename)
INTO result_rowcount;
RETURN result_rowcount;
END;
$$ LANGUAGE plpgsql;
SELECT
schemaname AS schemaname,
tablename AS table_name,
pg_temp.get_rows(schemaname, tablename) AS cnt
FROM
pg_tables
WHERE
schemaname in ('person','sales','purchasing','production','humanresources');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment