X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id CF38ED1B458 for ; Wed, 26 Nov 2003 15:56:57 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 61845-09 for ; Wed, 26 Nov 2003 11:56:30 -0400 (AST) Received: from hotmail.com (bay9-f41.bay9.hotmail.com [64.4.47.41]) by svr1.postgresql.org (Postfix) with ESMTP id 7CB2BD1B4D4 for ; Wed, 26 Nov 2003 11:56:25 -0400 (AST) Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC; Wed, 26 Nov 2003 07:56:30 -0800 Received: from 134.29.51.143 by by9fd.bay9.hotmail.msn.com with HTTP; Wed, 26 Nov 2003 15:56:30 GMT X-Originating-IP: [134.29.51.143] X-Originating-Email: [cstotes@hotmail.com] Reply-To: cstotesbery@acm.org From: "Clint Stotesbery" To: tgl@sss.pgh.pa.us Cc: pgsql-sql@postgresql.org Subject: Re: trigger conversion advice needed Date: Wed, 26 Nov 2003 15:56:30 +0000 Mime-Version: 1.0 Content-Type: text/plain; format=flowed Message-ID: X-OriginalArrivalTime: 26 Nov 2003 15:56:30.0129 (UTC) FILETIME=[DB41BA10:01C3B435] X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200311/294 X-Sequence-Number: 16109 Thanks for the advice Tom. I figured out why my create trigger statement wouldn't compile. Postgres expects a FOR EACH ROW or FOR EACH STATEMENT clause before the EXECUTE portion. Oracle assumes a statement level trigger unless you specify it to be for each row. In addition Oracle allows for specific columns to be monitored for updates like I had in the Oracle trigger (AFTER UPDATE OR INSERT OF order_date ON orders). Well I ended up adding FOR EACH STATEMENT to the Postgres version after the first email. I got a message back that said statement triggers weren't implemented in Postgres yet even though according to the 7.3 docs it can be used. I looked at the changelog for 7.4 and it said: "Add statement-level triggers (Neil) While this allows a trigger to fire at the end of a statement, it does not allow the trigger to access all rows modified by the statement. This capability is planned for a future release." I wonder if that means that I can specify FOR EACH STATEMENT and have it compile fine but it seems like that I can't acccess new and old though still in 7.4. I'll have to experiment with what you suggested and perhaps look into upgrading to 7.4 as well. Thanks, Clint ----Original Message Follows---- From: Tom Lane To: cstotesbery@acm.org CC: pgsql-sql@postgresql.org Subject: Re: [SQL] trigger conversion advice needed Date: Wed, 26 Nov 2003 10:40:08 -0500 Received: from alias2.acm.org ([199.222.69.92]) by mc11-f24.hotmail.com with Microsoft SMTPSVC(5.0.2195.6713); Wed, 26 Nov 2003 07:40:10 -0800 Received: from sss.pgh.pa.us ([192.204.191.242]) by alias2.acm.org (ACM Email Forwarding Service) with ESMTP id CRY73883 for ; Wed, 26 Nov 2003 10:40:10 -0500 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1])by sss.pgh.pa.us (8.12.10/8.12.10) with ESMTP id hAQFe819015058;Wed, 26 Nov 2003 10:40:08 -0500 (EST) X-Message-Info: JGTYoYF78jE74k1WFZAS8n73gEHv7D0r In-reply-to: References: Comments: In-reply-to "Clint Stotesbery" message dated "Wed, 26 Nov 2003 11:51:42 +0000" Message-ID: <15057.1069861208@sss.pgh.pa.us> Return-Path: tgl@sss.pgh.pa.us X-OriginalArrivalTime: 26 Nov 2003 15:40:10.0631 (UTC) FILETIME=[936E3170:01C3B433] "Clint Stotesbery" writes: > I'm working on converting a simple trigger from Oracle to Postgres and I > have a couple ofl questions that I need some help on please. First here's > the Oracle trigger: > CREATE OR REPLACE TRIGGER t_ship_date > AFTER UPDATE OR INSERT OF order_date ON orders > BEGIN > UPDATE orders > SET ship_date = working_5days(order_date); > END; It looks to me like this trigger implicitly assumes that an UPDATE command would only affect the row it was fired for --- which is not at all how Postgres will interpret such a command. (Alternatively, maybe the trigger actually does result in recomputing every row's ship_date? You would only notice if ship_date had been changed manually in some rows to be different from order_date + 5...) Guessing at what is actually wanted here, my inclination would be to use a BEFORE INSERT OR UPDATE trigger and to detect updates by change from OLD to NEW. The INSERT case would simply do NEW.ship_date := working_5days(NEW.order_date); RETURN NEW; The UPDATE case would look like IF NEW.order_date <> OLD.order_date THEN NEW.ship_date := working_5days(NEW.order_date); END IF; RETURN NEW; Pretty simple when you get the hang of it. > CREATE TRIGGER t_ship_date AFTER UPDATE OR INSERT ON orders > EXECUTE PROCEDURE t_ship_date(); > I always get a parse error at or near execute. You need to say FOR EACH ROW in there too. regards, tom lane _________________________________________________________________ Set yourself up for fun at home! Get tips on home entertainment equipment, video game reviews, and more here. http://special.msn.com/home/homeent.armx