X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 9CAE29FB362 for ; Mon, 31 Jul 2006 12:55:45 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 00827-09 for ; Mon, 31 Jul 2006 12:55:25 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from a2s5.a2hosting.com (host-209-124-50-10.a2webhosting.com [209.124.50.10]) by postgresql.org (Postfix) with ESMTP id D49C99FB36D for ; Mon, 31 Jul 2006 12:54:44 -0300 (ADT) Received: from adsl-155-100-31.tys.bellsouth.net ([72.155.100.31] helo=[192.168.1.10]) by a2s5.a2hosting.com with esmtpsa (TLSv1:RC4-SHA:128) (Exim 4.52) id 1G7a6D-0002w9-5U; Mon, 31 Jul 2006 11:54:33 -0400 In-Reply-To: References: <20060731041644.32247.qmail@web56610.mail.re3.yahoo.com> <7F7548C8-249E-44E2-8205-6C055671DA19@pgedit.com> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: Cc: "Thusitha Kodikara" , pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit From: John DeSoi Subject: Re: Triggers using PL/pgSQL Date: Mon, 31 Jul 2006 11:53:46 -0400 To: Aaron Bono X-Mailer: Apple Mail (2.752.2) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - a2s5.a2hosting.com X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - pgedit.com X-Source: X-Source-Args: X-Source-Dir: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=2.997 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, SPF_HELO_SOFTFAIL, SPF_SOFTFAIL X-Spam-Level: ** X-Archive-Number: 200607/379 X-Sequence-Number: 25953 On Jul 31, 2006, at 10:59 AM, Aaron Bono wrote: > On 7/31/06, John DeSoi wrote: > Is it really necessary to build a SQL string and use execute? It > seems you could just issue the INSERT statement. > > I don't think so but there was some discussion a week or two ago > about mixing variables and using execute. I am curious, does > anyone know what the "best" approach is? I did not test with older versions, but it seems to work fine with 8.1: CREATE OR REPLACE FUNCTION my_table_history_fn () returns trigger as ' BEGIN -- if a trigger insert or update operation occurs IF TG_OP = ''INSERT'' OR TG_OP = ''UPDATE'' THEN insert into my_table_history ( my_table_id, my_value, create_dt ) VALUES ( NEW.my_table_id, NEW.my_value, now() ); RETURN NEW; END IF; END; ' LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER; insert into my_table values (1, 'test1'); insert into my_table values (2, 'test2'); update my_table set my_value = 'test3' where my_table_id = 1; select * from my_table_history; === psql 9 === my_table_id | my_value | create_dt -------------+----------+---------------------------- 1 | test1 | 2006-07-31 11:47:33.080556 2 | test2 | 2006-07-31 11:47:48.221009 1 | test3 | 2006-07-31 11:48:21.029696 (3 rows) John DeSoi, Ph.D. http://pgedit.com/ Power Tools for PostgreSQL