X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 4687D534C2 for ; Fri, 8 Apr 2005 16:29:25 +0100 (BST) 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 52253-07 for ; Fri, 8 Apr 2005 15:29:20 +0000 (GMT) Received: from a2s1.a2webhosting.com (host-180.a2webhosting.com [69.39.89.180]) by svr1.postgresql.org (Postfix) with ESMTP id 9443F53482 for ; Fri, 8 Apr 2005 16:29:20 +0100 (BST) Received: from user-11207sg.dsl.mindspring.com ([66.32.31.144] helo=[192.168.1.10]) by a2s1.a2webhosting.com with esmtpsa (TLSv1:RC4-SHA:128) (Exim 4.44) id 1DJvQ2-0001zx-Es; Fri, 08 Apr 2005 11:29:16 -0400 In-Reply-To: <14244.1112970986@sss.pgh.pa.us> References: <4255AA15.8000508@mochima.com> <14244.1112970986@sss.pgh.pa.us> Mime-Version: 1.0 (Apple Message framework v619) Content-Type: text/plain; charset=US-ASCII; format=flowed Message-Id: Content-Transfer-Encoding: 7bit Cc: Carlos Moreno , Sean Davis , pgsql-sql@postgresql.org From: John DeSoi Subject: Re: Question on triggers and plpgsql Date: Fri, 8 Apr 2005 11:29:12 -0400 To: Tom Lane X-Mailer: Apple Mail (2.619) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - a2s1.a2webhosting.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: by amavisd-new at hub.org X-Spam-Status: No, hits=0.01 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/92 X-Sequence-Number: 20985 Tom, Thanks for setting the record straight. It has been a while since I have written a trigger and I forgot that you can't modify the row in the AFTER trigger. Makes perfect sense. For the record, here is what the docs say: Typically, row before triggers are used for checking or modifying the data that will be inserted or updated. For example, a before trigger might be used to insert the current time into a timestamp column, or to check that two elements of the row are consistent. Row after triggers are most sensibly used to propagate the updates to other tables, or make consistency checks against other tables. The reason for this division of labor is that an after trigger can be certain it is seeing the final value of the row, while a before trigger cannot; there might be other before triggers firing after it. If you have no specific reason to make a trigger before or after, the before case is more efficient, since the information about the operation doesn't have to be saved until end of statement. It might be worth adding a sentence here that explicitly states modifications can only be made in the BEFORE trigger. I did not see that anywhere else in the document. On Apr 8, 2005, at 10:36 AM, Tom Lane wrote: > No, actually Carlos wanted to do > new.last_modified = now(); > so he *must* use a BEFORE trigger --- AFTER is too late to change the > data that will be stored. > > Generalizing freely, I've seen three basic uses for triggers: > 1. Modify the data that will be stored. > 2. Check that data is valid (eg, consistent with another table). > 3. Propagate updates in one place to other places. > Clearly #1 must be done in BEFORE triggers. #2 and #3 could be done > either way. They are often done in AFTER triggers because that way you > *know* that any case-1 triggers have done their work and you are > looking > at the correct final state of the row. But you could do them in a > BEFORE trigger if you were willing to assume that no later-fired > trigger > would make a change that invalidates your check or propagation. AFTER > triggers are relatively expensive (since the triggering event state has > to be saved and then recalled) so I could see making that tradeoff if > performance is critical. John DeSoi, Ph.D. http://pgedit.com/ Power Tools for PostgreSQL