X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (developer.postgresql.org [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id 259CEB4389B for ; Wed, 9 Jul 2003 10:13:49 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 29630-09 for ; Wed, 9 Jul 2003 07:13:36 -0300 (ADT) Received: from water.oasis.net.au (water.oasis.net.au [210.8.139.2]) by svr1.postgresql.org (Postfix) with ESMTP id 31034B43161 for ; Wed, 9 Jul 2003 07:13:34 -0300 (ADT) Received: from oasis.net.au (localhost [127.0.0.1]) by water.oasis.net.au (8.9.3/8.9.3/Debian 8.9.3-21) with ESMTP id UAA12102 for ; Wed, 9 Jul 2003 20:32:32 +1000 X-Authentication-Warning: water.oasis.net.au: Host localhost [127.0.0.1] claimed to be oasis.net.au From: "Rudi Starcevic" To: pgsql-sql@postgresql.org Subject: Re: Logging select statements Date: Wed, 9 Jul 2003 20:32:32 +1000 Message-Id: <20030709103232.M72964@oasis.net.au> In-Reply-To: <9424A5E8-B1F3-11D7-BB21-000393B3A702@canprint.com.au> References: <3F0BBC7D.7060303@oasis.net.au> <9424A5E8-B1F3-11D7-BB21-000393B3A702@canprint.com.au> X-Mailer: Open WebMail X-OriginatingIP: 203.220.112.140 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/102 X-Sequence-Number: 14216 Matthew, Gee thanks .. I just read over Stephan's Set Returning Function last night .. I was trying to see how I could use it. > Hope that is what you were after! Indeed it is. Your 'rough and ready solution' solution is a mighty fine place to begin. Thanks aplenty to you and Achilleus for taking the time to look at this for me - another reason why I love PostgreSQL ! :-) Best regards Rudi. > Hi Rudi, > > You can't trigger on a SELECT, but you could wrap your SQL in a set > returning function... > > http://techdocs.postgresql.org/guides/SetReturningFunctions > > Here is a rough and ready solution: > > CREATE TABLE access_log ( id int not null ); > > CREATE TABLE datatable ( > id int not null primary key, > somedata varchar(255) not null > ); > > INSERT INTO datatable VALUES( 1, 'apple' ); > INSERT INTO datatable VALUES( 2, 'orange' ); > INSERT INTO datatable VALUES( 3, 'banana' ); > > CREATE OR REPLACE FUNCTION get_rows_and_log( varchar ) RETURNS SETOF > record AS > ' > DECLARE > r record; > BEGIN > FOR r IN EXECUTE ''SELECT * FROM '' || $1 LOOP > INSERT INTO access_log VALUES( r.id ); > RETURN NEXT r; > END LOOP; > RETURN; > END; > ' > LANGUAGE 'plpgsql'; > > Now, as an example, do: > > SELECT * FROM get_rows_and_log( 'datatable' ) AS data( id int, > somedata varchar); > > You'll get the data returned, and the log entries will be made. > > You can put your WHERE clause in the parameter: > > SELECT * FROM get_rows_and_log( 'datatable WHERE somedata LIKE > ''%e''' ) AS data( id int, somedata varchar); > > Hope that is what you were after! > > Cheers > > Matthew. > > On Wednesday, July 9, 2003, at 04:55 PM, Rudi Starcevic wrote: > > > Thanks Achilleus, > > > > I know there's a couple of ways I could do this. > > > > In my first email I can see a senario of 1 select plus 100 inserts. > > > > Another may be 1 select plus 1 insert. > > For example; > > In a table of 3000 rows a user submits a query which returns 100 rows. > > I could loop through the result set and build a string of id's ( > > 1,2,5,7,8,9,44,22 etc ) and > > make one insert into a logging table of the entire string.