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 8893D3A41E6 for ; Mon, 25 Oct 2004 21:02:35 +0100 (BST) 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 60345-02 for ; Mon, 25 Oct 2004 20:02:28 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 19A903A41E1 for ; Mon, 25 Oct 2004 21:02:29 +0100 (BST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id i9PK2R9M023026; Mon, 25 Oct 2004 16:02:27 -0400 (EDT) To: Marinos Yannikos Cc: pgsql-sql@postgresql.org Subject: Re: plpgsql: PERFORM vs. SELECT INTO (PERFORM not setting FOUND variable?) In-reply-to: <417D57B1.8010507@geizhals.at> References: <417D57B1.8010507@geizhals.at> Comments: In-reply-to Marinos Yannikos message dated "Mon, 25 Oct 2004 21:44:49 +0200" Date: Mon, 25 Oct 2004 16:02:27 -0400 Message-ID: <23025.1098734547@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/183 X-Sequence-Number: 19286 Marinos Yannikos writes: > create function blup_unique2 (text,text) returns boolean as 'begin > perform (select 1 from blup where t1=$1 or t1=$2 or t2=$1 or t2=$2 or > $1=$2 limit 1); return NOT FOUND; end' LANGUAGE plpgsql; You've got a syntax problem. PERFORM is syntactically like SELECT, so what you wrote is equivalent to SELECT (SELECT 1 FROM blup ....) In other words, you are evaluating a scalar subquery, which is going to return either "1" or "NULL" depending on whether the WHERE matches, or give an error if the WHERE matches multiple rows (a case you wouldn't hit because of the LIMIT). So the outer SELECT produces exactly one row containing the scalar result, and FOUND always ends up TRUE. So what you want is just PERFORM 1 FROM blup ... and then check the FOUND result from that. (The 8.0 docs hopefully explain this more clearly; PERFORM was certainly not very well documented before.) Note that I'm concerned that the performance of this will suck ... in particular you really ought to test the $1=$2 case separately. regards, tom lane