13

How do I write a simple stored procedure in postgres that doesn't return a value at all? Even with a void return type when I call the stored procedure I get a single row back.

CREATE FUNCTION somefunc(in_id bigint) RETURNS void AS $$
BEGIN
   DELETE from test_table where id = in_id;
END;
$$ LANGUAGE plpgsql;

3 Answers 3

8

It's not the function that returns value, it's the SELECT you used to call it. If it doesn't return any rows, it doesn't run your function.

Sign up to request clarification or add additional context in comments.

Comments

6

You can achieve "nothing returned" by abusing set-returning functions:

Simple function:

create function x () returns setof record as $$
begin
return;
END;
$$ language plpgsql;

Now you can:

# select x();
 x
---
(0 rows)

In case it doesn't work for you (sorry, I'm using 8.5), try with this approach:

# create function x (OUT o1 bool, OUT o2 bool) returns setof record as $$
begin
return;
END;
$$ language plpgsql;
CREATE FUNCTION

The parameters are irrelevant, but:

  • You need > 1 of them
  • They have to be named

And now you can:

# select * from x();
 o1 | o2
----+----
(0 rows)

Comments

5

You are doing just fine. You dont need to add anything else.

The result of the row is null, so it is a void return.

I don't think theres something you can do about that. Checking my void functions all of them are just like yours.

returns void as $$ and no return statement in code block.

4 Comments

But how can that method be called?
SELECT * FROM void_function();
It's perhaps easier to use PERFORM void_function();.
PERFORM only works inside a pl/pgsql function. You use it when you want to discard the result of a SELECT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.