Re: Seeking PL/PGSQL example

From: Chris Travers <chris(at)travelamericas(dot)com>
To: John Wells <jb(at)sourceillustrated(dot)com>, pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Seeking PL/PGSQL example
Date: 2005-08-12 23:13:04
Message-ID: 42FD2D00.9030600@travelamericas.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

John Wells wrote:

>Guys,
>
>I'm getting started with PL/PGSQL but want to understand if the following
>is even possible:
>
>With PL/PGSQL, how would I roll through all tables in my database and
>print the table name, along with the row count of that table?
>
>
I would assume you would do something like (this may need to be
incomplete but should give you a place to start).

I would use a FOR loop to get the names of the tables (with a select
statement like SELECT table_name FROM information_schema.tables where
table_schema = 'public')

Then I would have to have a for in execute loop which would return next
for each of SELECT table_name as table_name, count(*) from table_name

And run this nested loop this way.

Something like:
CREATE FUNCTION rowcounts() RETURNS SETOF record AS '
DECLARE
tablename varchar();
BEGIN
FOR tname IN SELECT table_name FROM information_schema.tables
LOOP
FOR IN EXECUTE ''SELECT '' || tname || ''AS table_name, count(*)
from '' ||tname;
LOOP
RETURN NEXT;
END LOOP;
END LOOP;
END;
' LANGUAGE PLPGSQL;

Not saying this will work but it might be a good start.

Best Wishes,
Chris Travers
Metatron Technology Consulting

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ed L. 2005-08-12 23:20:49 Re: insert performance riddle
Previous Message Michael Fuhr 2005-08-12 22:46:10 Re: Seeking PL/PGSQL example