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 20164538AD for ; Tue, 12 Apr 2005 04:30:59 -0300 (ADT) 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 72636-10 for ; Tue, 12 Apr 2005 07:30:50 +0000 (GMT) Received: from anchor-post-35.mail.demon.net (anchor-post-35.mail.demon.net [194.217.242.85]) by svr1.postgresql.org (Postfix) with ESMTP id 559DE5367B for ; Tue, 12 Apr 2005 04:30:49 -0300 (ADT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-35.mail.demon.net with esmtp (Exim 4.42) id 1DLFob-000KDv-HM; Tue, 12 Apr 2005 07:28:06 +0000 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 3188A1711D; Tue, 12 Apr 2005 08:30:40 +0100 (BST) Message-ID: <425B7920.4040002@archonet.com> Date: Tue, 12 Apr 2005 08:30:40 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Carlos Moreno Cc: pgsql-sql@postgresql.org Subject: Re: Update aborted if trigger function fails? References: <425ADE3F.1090206@mochima.com> In-Reply-To: <425ADE3F.1090206@mochima.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.059 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200504/161 X-Sequence-Number: 21054 Carlos Moreno wrote: > > Hi, > > I just noticed this (odd?) behaviour, and it kind of > scares me. > Isn't this a little fragile? Is there something I > could do to avoid this situation? Should trigger > functions be extremely simple as to guarantee that > an error would never happen? There's nothing else it can do, really. Far better that the whole update fails than you get an inconsistent database. Imagine you have a banking system, and every time you add a row to the transaction-history, you update the "current_balance" table. Which would you prefer, both updates fail, or the two get out of sync? Now, there is room for improved dependency checking, but functions pose certain difficulties. 1. The body of the function is opaque to PostgreSQL - it's only plpgsql that it handles itself. It knows nothing about Perl/Python/PHP/Java/C. 2. Functions can create queries from text - even if PG understood all these languages, it couldn't determine which tables were accessed. So - how do you deal with this? Well, you test. Ideally, you should have a set of tests and re-run them to ensure all your functions work as desired. -- Richard Huxton Archonet Ltd