X-Original-To: pgsql-admin@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id CBE01474E61 for ; Wed, 19 Feb 2003 21:37:43 -0500 (EST) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with SMTP id 28116-04 for ; Wed, 19 Feb 2003 21:37:33 -0500 (EST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id F087E474E4F for ; Wed, 19 Feb 2003 21:37:32 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id CE552D628; Wed, 19 Feb 2003 18:37:36 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id C40EE5C03; Wed, 19 Feb 2003 18:37:36 -0800 (PST) Date: Wed, 19 Feb 2003 18:37:36 -0800 (PST) From: Stephan Szabo To: Yudha Setiawan Cc: Subject: Re: How to avoid the Mutating Triggers ?? In-Reply-To: <004c01c2d887$d801b3d0$ea00a8c0@yudha> Message-ID: <20030219183512.P40199-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new X-Archive-Number: 200302/326 X-Sequence-Number: 7524 On Thu, 20 Feb 2003, Yudha Setiawan wrote: > Dear Szabo, You were wrote; > > > Well, what have you tried? For the simple > > case (pointer is not changing), a not terribly > > efficient version might be something like: > > > > create or replace function dorecalcupd() returns trigger as > > 'declare > > last int; > > begin > > SELECT INTO last qty FROM foo where pointer < NEW."pointer" ORDER BY > > pointer desc LIMIT 1; > > NEW.qty := last + NEW."in" - NEW."out"; > > if (NEW.pointer = OLD.POINTER AND (NEW."in" != OLD."in" or NEW."out" != > > OLD."out")) then > > UPDATE foo set qty = (select sum(f."in") - sum(f."out") from foo f where > > f.pointer > where pointer>NEW.pointer; > > end if; > > return NEW; > > end;' language 'plpgsql'; > > > > In this case the trigger does cause requests to the same trigger for the > > later rows, but since only qty is changing it does nothing. > > > > It's more complicated when you deal with the pointer moving case. > > Thank's a Lot for all milist especialy for dear Stephan Szabo. I'm Sorry > "Pointer" on my ilustration "IS NOT a FIELD". it's just information for > me, to show you where is exactly the position of record / Pointer that > I want to Change. > Perhaps you have another best one, please...?? Well, what's the key that you're ordering by? Use that instead of pointer in the above. If you have no ordering key or the key isn't guaranteed to be unique, the original question doesn't have an answer because there is no implict order to rows in a table.