X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 7593E9DC99B for ; Wed, 1 Mar 2006 10:43:33 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 91232-08 for ; Wed, 1 Mar 2006 10:43:37 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from smtp02l.fasthosts.co.uk (smtp.fasthosts.co.uk [213.171.216.50]) by postgresql.org (Postfix) with ESMTP id 5B8E09DC867 for ; Wed, 1 Mar 2006 10:43:30 -0400 (AST) Received: from aurora (host217-35-95-235.in-addr.btopenworld.com [217.35.95.235]) by smtp02l.fasthosts.co.uk (Postfix) with ESMTP id 24E1516A4A3 for ; Wed, 1 Mar 2006 14:43:26 +0000 (GMT) From: "Simon Kinsella" To: Subject: Re: Help with trigger that updates a row prior to a potentially aborted deletion? Date: Wed, 1 Mar 2006 14:43:00 -0000 Organization: Bluefire Systems Ltd MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 Thread-Index: AcY9PMk6NsYwkKB+Q/+j1phtpc/KVwAAD5JAAABY0rA= Message-Id: <20060301144326.24E1516A4A3@smtp02l.fasthosts.co.uk> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.12 required=5 tests=[AWL=0.120] X-Spam-Score: 0.12 X-Spam-Level: X-Archive-Number: 200603/11 X-Sequence-Number: 24357 Yes I originally started working on a function based approach like you suggest, but realised it wouldn't cover the situation where the delete operation is fired as a result of a CASCADE ON DELETE constraint from a parent table, rather than as a manual query. I suppose I could ditch that particular cascading contraint and replace it with a trigger function that calls my custom delete function. Not sure if I like that though ;) Thanks for your feedback, Simon Kinsella -----Original Message----- From: Stephan Szabo [mailto:sszabo@megazone.bigpanda.com] Sent: Wednesday, March 01, 2006 2:31 PM To: Simon Kinsella Cc: pgsql-sql@postgresql.org Subject: Re: [SQL] Help with trigger that updates a row prior to a potentially aborted deletion? On Wed, 1 Mar 2006, Simon Kinsella wrote: > Hi all, > > I have a situation where a DELETE operation may (correctly) fail due > to a RESTRICT FK constraint. If so, I need to set a flag in the row > indicating that it has been marked for deletion so that I can > disregarded in subsequent queries. > > I'm trying to achieve this with a BEFORE DELETE trigger, which would > set the 'marked_for_deletion' field to TRUE before attempting the delete proper. > Then if the DELETE fails the row would still be tagged and I'd be happy. > Problem is, when the DELETE operation fails with a RESTRICT VIOLATION > error the entire operation is rolled back, including the BEFORE > triggers, leaving me back where I started. > > Is there anyway to get the DELETE operation, or more specifically the > FK constraint, to fail silently, i.e. to skip over the failed > operation and not throw an exception? Not with the standard constraint trigger, no. In general, constraint checks happen after the action and as such can't skip over an operation since it's already happened. You might be able to do this within a function however if you do the update and then start an exeption checking block to do the delete.