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 4B0DF52966 for ; Fri, 8 Apr 2005 16:35:54 +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 55496-01 for ; Fri, 8 Apr 2005 15:35:50 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id C2C0D535FD for ; Fri, 8 Apr 2005 16:35:48 +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 j38FZl43014741; Fri, 8 Apr 2005 11:35:47 -0400 (EDT) To: Andrew Sullivan Cc: pgsql-sql@postgresql.org Subject: Re: Question on triggers and plpgsql In-reply-to: <20050408145928.GA27718@phlogiston.dyndns.org> References: <4255AA15.8000508@mochima.com> <14244.1112970986@sss.pgh.pa.us> <20050408145928.GA27718@phlogiston.dyndns.org> Comments: In-reply-to Andrew Sullivan message dated "Fri, 08 Apr 2005 10:59:28 -0400" Date: Fri, 08 Apr 2005 11:35:47 -0400 Message-ID: <14740.1112974547@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/93 X-Sequence-Number: 20986 Andrew Sullivan writes: > On Fri, Apr 08, 2005 at 10:36:26AM -0400, Tom Lane wrote: >> 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. > People who know more about this will no doubt correct me, but isn't > such a case crying out for LISTEN/NOTIFY instead? That is, your > trigger puts the mail content into a table of mails to be sent, and > wakes up the mail-sender client with the NOTIFY; the NOTIFY and the > commit to the mail-it table only happen in that case if the > transaction commits. And since mail is async anyway, the extra few > seconds shouldn't make any difference, right? We do often recommend that, though it occurs to me that this just moves the failure case somewhere else. The hypothetical mail-sending process would presumably want to send mail and then delete the associated record from the table of pending mails ... so what if it fails after sending the mail and before committing the delete? What this does do for you is replace the risk of phantom emails (mail sent but corresponding action inside the database never committed) with the risk of duplicate emails (mail-sender sends you another one after it restarts). In most cases I think I'd prefer the latter. regards, tom lane