X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id E8C02D84A1 for ; Tue, 4 Oct 2005 22:01:14 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 44310-08 for ; Wed, 5 Oct 2005 01:01:11 +0000 (GMT) Received: from amanda.contactbda.com (ipn36372-e65122.cidr.lightship.net [216.204.66.226]) by svr1.postgresql.org (Postfix) with ESMTP id EAD74D6F63 for ; Tue, 4 Oct 2005 22:01:12 -0300 (ADT) Received: from amanda.contactbda.com (amanda.contactbda.com [192.168.1.2]) by amanda.contactbda.com (8.12.11/8.12.11/Debian-3) with ESMTP id j9511DSN004105; Tue, 4 Oct 2005 21:01:13 -0400 From: "Jim Buttafuoco" To: solarsail , Yasir Malik Cc: PostgreSQL Reply-To: jim@contactbda.com Subject: Re: using pg_tables and tablename in queries Date: Tue, 4 Oct 2005 21:01:12 -0400 Message-Id: <20051005005916.M89424@contactbda.com> In-Reply-To: References: <9420.1128445291@sss.pgh.pa.us> X-Mailer: Open WebMail 2.41 20040926 X-OriginatingIP: 192.168.1.1 (jim) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.053 required=5 tests=[AWL=0.003, FORGED_RCVD_HELO=0.05] X-Spam-Level: X-Archive-Number: 200510/18 X-Sequence-Number: 22871 again, do you really want to join the tables or do a UNION ALL. From one of your posts you said the table were the same. you need to do something like select * from table_001 union all select * from table_002 ... select * from table_999 I would do this in a set returning function looping of an EXECUTE. If you need help, post the schema for a couple of your tables and I will help with the function Jim ---------- Original Message ----------- From: solarsail To: Yasir Malik Cc: PostgreSQL Sent: Tue, 4 Oct 2005 15:50:39 -0400 Subject: Re: [SQL] using pg_tables and tablename in queries > 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 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 > > > > ---------------------------(end of broadcast)--------------------------- > TIP 2: Don't 'kill -9' the postmaster ------- End of Original Message -------