X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5C6C2D9ADD for ; Wed, 28 Sep 2005 16:46:14 -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 87066-08 for ; Wed, 28 Sep 2005 19:46:06 +0000 (GMT) Received: from qproxy.gmail.com (qproxy.gmail.com [72.14.204.198]) by svr1.postgresql.org (Postfix) with ESMTP id CB99DD9AD6 for ; Wed, 28 Sep 2005 16:46:05 -0300 (ADT) Received: by qproxy.gmail.com with SMTP id c12so400354qbc for ; Wed, 28 Sep 2005 12:46:07 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=F1eYL2mwEjuz7RVhC3g/6ow6p1fSSHv9xf2ZWyEOejaucl4lQrQ5NdAR9emV+9JsI++w1nlvgkAgQsfzH+Rs2PRlbIrcOzt5OWdq1lsOBD8GbqeFbPqbbiFgdyHpYL22F8Y9WdIf8fvC2m6Z31R0yfNZxJFnxfKDT0iibKFrWXU= Received: by 10.65.22.20 with SMTP id z20mr71788qbi; Wed, 28 Sep 2005 12:46:07 -0700 (PDT) Received: by 10.64.180.12 with HTTP; Wed, 28 Sep 2005 12:46:07 -0700 (PDT) Message-ID: Date: Wed, 28 Sep 2005 15:46:07 -0400 From: Ian Meyer Reply-To: Ian Meyer To: pgsql-sql@postgresql.org Subject: Re: Problem with function and trigger... In-Reply-To: <7747.1127929968@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: <7747.1127929968@sss.pgh.pa.us> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.147 required=5 tests=[AWL=-0.916, RCVD_BY_IP=0.024, RCVD_IN_BL_SPAMCOP_NET=1.832, UPPERCASE_25_50=0.207] X-Spam-Level: * X-Archive-Number: 200509/285 X-Sequence-Number: 22832 On 9/28/05, Tom Lane wrote: > Ian Meyer writes: > > IF TG_OP =3D 'DELETE' AND OLD.deleted =3D FALSE THEN > > > ERROR: record "old" is not assigned yet > > DETAIL: The tuple structure of a not-yet-assigned record is indetermin= ate. > > CONTEXT: PL/pgSQL function "thread_sync" line 2 at if > > > What am I failing to understand with this? > > We don't guarantee short-circuit evaluation of boolean expressions. > You'll have to break that into two IFs, ie, > > IF TG_OP =3D 'DELETE' THEN > IF ... test on OLD.something ... > > regards, tom lane > Ah ha.. that makes sense. I fixed it to be: CREATE OR REPLACE FUNCTION thread_sync() RETURNS trigger AS $$ BEGIN IF TG_OP =3D 'DELETE' THEN UPDATE member SET total_threads=3Dtotal_threads-1 WHERE id=3DOLD.member= _id; RETURN OLD; ELSEIF TG_OP =3D 'INSERT' THEN UPDATE member SET total_threads=3Dtotal_threads+1 WHERE id=3DNEW.member= _id; RETURN NEW; ELSEIF TG_OP =3D 'UPDATE' THEN IF NEW.deleted !=3D OLD.deleted THEN IF NEW.deleted =3D TRUE THEN UPDATE member SET total_threads=3Dtotal_threads-1 WHERE id=3DNEW.me= mber_id; RETURN NEW; ELSEIF NEW.deleted =3D FALSE then UPDATE member SET total_threads=3Dtotal_threads+1 WHERE id=3DNEW.me= mber_id; RETURN NEW; END IF; RETURN NULL; END IF; RETURN NULL; END IF; END; $$ LANGUAGE plpgsql; Here's my next question(s)... If i have 2 rows in the thread table and I delete 1 of the rows with: delete from thread where id=3D26; it ends up decrementing the total_threads value by 2, instead of one. bcodev=3D> select id, total_threads from member; id | total_threads ----+--------------- 1 | 2 (1 row) bcodev=3D> delete from thread where id=3D37; DELETE 1 bcodev=3D> select id, total_threads from member; id | total_threads ----+--------------- 1 | 0 (1 row) I'm confused, to say the least. Thanks for all the help. Ian