Re: using pg_tables and tablename in queries

From: solarsail <solarsail(at)gmail(dot)com>
To: Yasir Malik <ymalik(at)cs(dot)stevens(dot)edu>
Cc: PostgreSQL <pgsql-sql(at)postgresql(dot)org>
Subject: Re: using pg_tables and tablename in queries
Date: 2005-10-04 19:50:39
Message-ID: aa44f4a30510041250k3ea4e067r424d66ae2461453@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I managed to make this work as sub query before... I wish I had
written it down somewhere...

Regarding the creation of a function. I do have a function that
almost does that. I'm having a hard time getting it to return a set
of records from the EXECUTE command ( more than one row returned by
the select * ...).

If I generate a temporary table instead of returning the results how
long will that table exist for? Excuse the OOP terminology but would
it be correct to create a 'Singleton' to access the temporary table,
where if it exists and is less than 30 minutes old use that one,
otherwise drop the table and recreate it?

Thanks

-- sample function..

CREATE OR REPLACE FUNCTION testfunc_jointables()
RETURNS SETOF record AS
$BODY$
DECLARE
query TEXT;
BEGIN
query := 'auditrecord';

FOR
atablename IN select * from pg_tables where tablename like 'mytable_%'
LOOP

query := query || ', ' || quote_ident(atablename.tablename);

END LOOP;

EXECUTE ' SELECT * from ' || query;

END;

On 10/4/05, Yasir Malik <ymalik(at)cs(dot)stevens(dot)edu> wrote:
> > The current behavior is by design.
> >
> > We use the table as a logging repository. It can get very large 250 000
> > records. Because of the large number of records that we have in the table we
> > found it was much faster to perform inserts on a smaller table. Our current
> > system rolls the tables over every 12 hours or so, creating a new table with
> > the following behavior:
> >
> > CREATE TABLE mytable_temp {...}
> >
> > ALTER TABLE mytable RENAME TO mytable_back_datetime;
> > ALTER TABLE mytable_temp RENAME TO mytable;
> >
> > I want to join the mytable_back_datetime tables together in order to perform
> > queries against my huge set of data to generate some reports. I'm probably
> > going to create a temporary table with a few indexes to make the reports run
> > faster... however I need to join the tables all together first.
> >
>
> I would create a function that creates a string with a query that includes
> all the tables you need, and call execute on the string. You would loop
> through the all tables from pg_tables and keep on appending the table name
> you need.
>
> Regards,
> Yasir
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq
>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Gregory S. Williamson 2005-10-04 22:33:28 Re: using pg_tables and tablename in queries
Previous Message Jim Buttafuoco 2005-10-04 19:45:07 Re: using pg_tables and tablename in queries