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 34C9A53234 for ; Fri, 8 Apr 2005 15:36:29 +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 35210-03 for ; Fri, 8 Apr 2005 14:36:25 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 4649F535DC for ; Fri, 8 Apr 2005 15:36:24 +0100 (BST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id j38EaQRN014245; Fri, 8 Apr 2005 10:36:26 -0400 (EDT) To: Sean Davis Cc: John DeSoi , Carlos Moreno , pgsql-sql@postgresql.org Subject: Re: Question on triggers and plpgsql In-reply-to: References: <4255AA15.8000508@mochima.com> Comments: In-reply-to Sean Davis message dated "Fri, 08 Apr 2005 09:41:57 -0400" Date: Fri, 08 Apr 2005 10:36:26 -0400 Message-ID: <14244.1112970986@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.006 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/89 X-Sequence-Number: 20982 Sean Davis writes: > Just one detail, but in the form of a question. In the original > posting, I think the trigger was doing the logging for something > happening on a table as a before insert or update--I may be wrong on > that detail. I would think of doing such actions AFTER the > update/insert. In the world of transaction-safe operations, is there > ANY danger in doing the logging as a BEFORE trigger rather than an > AFTER trigger? 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. AFAICS the only way that you could get into a can't-roll-back situation is if the trigger tries to propagate the update outside the database. For instance, the proverbial trigger to send mail: once sent you can't cancel it. But really this is dangerous even in an AFTER trigger --- the transaction could still be rolled back after the AFTER trigger fires. regards, tom lane