X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 0D064D1B4B8 for ; Thu, 4 Mar 2004 15:13:55 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 05156-05 for ; Thu, 4 Mar 2004 11:13:53 -0400 (AST) Received: from anchor-post-30.mail.demon.net (anchor-post-30.mail.demon.net [194.217.242.88]) by svr1.postgresql.org (Postfix) with ESMTP id 8108DD1B470 for ; Thu, 4 Mar 2004 11:13:50 -0400 (AST) Received: from mailgate.bray-healthcare.com ([80.177.250.202] helo=solport.bray-healthcare.com) by anchor-post-30.mail.demon.net with esmtp (Exim 3.35 #1) id 1AyuXp-0007Z4-0U; Thu, 04 Mar 2004 15:13:53 +0000 Received: from braydb.bray-healthcare.com ([192.168.1.18]) by solport.bray-healthcare.com with esmtp (Exim 3.36 #1 (Debian)) id 1AyuXp-0002AF-00; Thu, 04 Mar 2004 15:13:53 +0000 Subject: Re: How to avoid (stop) a endless loop in a trigger From: Oliver Elphick Reply-To: olly@lfix.co.uk To: Rodrigo Sakai Cc: pgsql-sql@postgresql.org In-Reply-To: <20040304145407.32236.qmail@hm10.locaweb.com.br> References: <20040304145407.32236.qmail@hm10.locaweb.com.br> Content-Type: text/plain Message-Id: <1078413233.498.5.camel@braydb> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.5 Date: Thu, 04 Mar 2004 15:13:53 +0000 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200403/37 X-Sequence-Number: 17136 On Thu, 2004-03-04 at 14:54, Rodrigo Sakai wrote: > Hi people, i have a problem here. > I'm doing a trigger that when a update occurs i need to do an update on the same table (target table), but as known, it causes a endless loop whithin infinit updates. So I need to stop the trigger after it does the first update, is there any way????? > I tried to do a return null, but that was a very bad idea because it stops completly the function fired by a trigger and all its computation is in vain... > > The test trigger that i did is like : > > CREATE OR REPLACE FUNCTION public.sp_teste_loop() RETURNS trigger AS ' > begin > raise notice \'Trigger Fired\'; > if (TG_OP = \'INSERT\') then > update teste_trigger > set flg_bool = \'S\' > where codigo=NEW.codigo; > > RETURN NEW; > > elsif (TG_OP = \'UPDATE\') then > update teste_trigger > set flg_bool = \'N\' > where codigo=NEW.codigo; > > RETURN NULL; > end if; > end; > ' LANGUAGE 'plpgsql' VOLATILE; Does this update other records, or only the one you are inserting or updating? If the former, add " AND flg_bool IS NULL OR flg_bool != \'S\'" to the update condition (!=\'N\' for the update case); then records that are already OK will not be touched, so the recursion will stop automatically. If the latter, just change NEW.flg_bool and return NEW > CREATE TRIGGER tr_sp_teste_trigger > BEFORE INSERT OR UPDATE > ON public.teste_trigger > FOR EACH ROW > EXECUTE PROCEDURE public.sp_teste_loop(); > > Thank for any help and regards!!!! > > > ===================== > Rodrigo Sakai > Database Programmer > rodrigo@2bfree.com.br > http://www.2bfree.com.br > Tel: (55) (11) 5083-5577 > Fax: (55) (11) 5549-3598 > ===================== > > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org