HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlModerate

how do I execute a void-returning procedure in pgsql 9.1?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
procedurereturningpgsqlhowvoidexecute

Problem

If I execute the following in a file with the pgsql command, it complains that the procedure needs to be called with 'perform'. But when I try to use perform, it tells me that perform is not defined. What's the solution?

create or replace function waitris() returns void as 
$
declare
    cnt integer;
begin
    loop
        select count(*) into cnt from taskflag where taskid = 'rdfdb' and state != 2;
        if cnt = 0 then   
            exit;
        end if;
        select pg_sleep(1);
    end loop;
end;
$ 
language plpgsql;

select waitris();

Solution

There is no problem with how you call your function. The error you get comes from executing it if and only if cnt is greater than zero. The error you get is

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function "waitris" line 9 at SQL statement


and it clearly states that the problem lies in the SELECT pg_sleep(1); line. That is the place you should replace SELECT with PERFORM.

Otherwise your loop is futile without a CONTINUE. And even if you add that, behaviour of the loop is hard to follow. You'd better use a WHILE loop instead. And still one more: as you see, I changed the variable name count to cnt for better readability.

Code Snippets

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function "waitris" line 9 at SQL statement

Context

StackExchange Database Administrators Q#18523, answer score: 10

Revisions (0)

No revisions yet.