Skip to content

Instantly share code, notes, and snippets.

@goors
Created January 29, 2026 20:53
Show Gist options
  • Select an option

  • Save goors/34ecb0ee78ff75ad63a2ad7776002862 to your computer and use it in GitHub Desktop.

Select an option

Save goors/34ecb0ee78ff75ad63a2ad7776002862 to your computer and use it in GitHub Desktop.
--- PostgreSQL: Row Count for All Tables in a Schema
DO $$
DECLARE
r RECORD;
cnt BIGINT;
BEGIN
FOR r IN
SELECT tablename
FROM pg_tables
WHERE schemaname = 'table_name'
LOOP
EXECUTE format('SELECT count(*) FROM table_name.%I', r.tablename)
INTO cnt;
RAISE NOTICE '%: % rows', r.tablename, cnt;
END LOOP;
END $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment