count the number of rows in tables

From: "A B" <gentosaker(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: count the number of rows in tables
Date: 2008-08-21 13:42:59
Message-ID: dbbf25900808210642v266c7049r2a9b7af7296674bc@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

I'd like to fill this table

foo (name varchar(100), rows integer);

with the number of rows for each table that my account has access to.
But EXECUTE with SELECT ... INTO is not yet implemented.
So how do I re-write this function?

create or replace function countrows() RETURNS void AS $$
DECLARE
rec RECORD;
nr INTEGER;
BEGIN
FOR rec IN SELECT tablename FROM pg_tables WHERE tableowner='myaccount'
LOOP
EXECUTE 'SELECT count(*) INTO nr FROM '||rec.tablename;
EXECUTE 'INSERT INTO foo (name,rows) VALUES ('||rec.tablename||','||nr||')';
END LOOP;
END;
$$ LANGUAGE plpgsql;

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Frank Bax 2008-08-21 14:44:44 Re: count the number of rows in tables
Previous Message Andrej Ricnik-Bay 2008-08-21 06:22:14 Re: Postgres backups - when the backup operator isn't the postgres user ?