Re: plpgsql: PERFORM vs. SELECT INTO (PERFORM not setting FOUND variable?)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Marinos Yannikos <mjy(at)geizhals(dot)at>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: plpgsql: PERFORM vs. SELECT INTO (PERFORM not setting FOUND variable?)
Date: 2004-10-25 20:02:27
Message-ID: 23025.1098734547@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Marinos Yannikos <mjy(at)geizhals(dot)at> 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Edmund Bacon 2004-10-25 20:08:41 Re: plpgsql: PERFORM vs. SELECT INTO (PERFORM not setting FOUND
Previous Message Marinos Yannikos 2004-10-25 19:44:49 plpgsql: PERFORM vs. SELECT INTO (PERFORM not setting FOUND variable?)