francistheturd
francistheturd

Reputation: 301

Getting syntax error when running a simple perform

Why am I getting a syntax error on this simple perform call

create or replace function foo ()
returns void
as $$
begin
end;
$$ language 'plpgsql';

perform * from foo ();

I tested it online at ExtendsClass

enter image description here

Upvotes: 0

Views: 35

Answers (1)

AdamKG
AdamKG

Reputation: 14081

PERFORM is a PL/pgSQL statement, not a SQL one. This means it can only be used between the BEGIN and END of a function definition. See below, where I call bar() from foo() using PERFORM.

create or replace function bar ()
returns void
as $$
begin
RAISE NOTICE 'bar() ran';
end;
$$ language 'plpgsql';

create or replace function foo ()
returns void
as $$
begin
perform bar();
end;
$$ language 'plpgsql';

The SQL fiddle site you've linked doesn't show stderr, but if you paste the above into psql and then select * from foo();, you'll see that bar() ran.

testdb=# select * from foo();
NOTICE:  bar() ran
 foo 
-----
 
(1 row)

Upvotes: 1

Related Questions