Skip to content

Instantly share code, notes, and snippets.

@poacosta
Created August 21, 2024 10:25
Show Gist options
  • Save poacosta/7a4ed5066cf0d6ac583fe8555f84bf65 to your computer and use it in GitHub Desktop.
Save poacosta/7a4ed5066cf0d6ac583fe8555f84bf65 to your computer and use it in GitHub Desktop.
PostgreSQL: How to count rows in tables
DO $$
DECLARE
r RECORD;
row_count BIGINT;
BEGIN
FOR r IN (SELECT relname FROM pg_stat_user_tables) LOOP
EXECUTE 'SELECT COUNT(*) FROM ' || quote_ident(r.relname) INTO row_count;
RAISE NOTICE 'Table: %, Row count: %', r.relname, row_count;
END LOOP;
END $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment