Last active
December 5, 2024 20:58
-
-
Save dincosman/4929e37069c864fb9fe6cb9b09af13f4 to your computer and use it in GitHub Desktop.
Get Row Counts for Each Table in PostgreSQL Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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