Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Postgres stored functions have both a return value AND out parameters?

I know Oracle and PL/SQL

Compared to what I know about Oracle PL/SQL, I'm not very familiar with PostgreSQL's stored procedures and plpgsql. In Oracle, there are two types of callables:

  • Procedures. They can have IN, OUT and IN OUT parameters, but no return values
  • Functions. They can have IN, OUT and IN OUT parameters and they MUST return a value

But I'm new to plpgsql

I understand that in plpgsql, all stored procedures are considered functions. To my understanding, this means, they can (but don't have to) always return a value. Now I see on the documentation page, that I can also declare OUT parameters on functions, a thing that's not possible in Oracle. But I don't see an example or any clear statement about whether OUT parameters can be combined with return values. Neither can I see whether IN OUT parameters are possible.

So these are my questions:

  • Does plpgsql allow IN OUT parameters?
  • Does plpgsql allow OUT parameters to be combined with return values? Is this a common practice? Do you have examples for that?
like image 680
Lukas Eder Avatar asked Jan 10 '11 20:01

Lukas Eder


People also ask

Can we use out and inout parameters in function?

A function can have OUT or IN OUT parameters, but this is bad coding practice. A function should have a return value and no out parameter.

Can stored procedure return value in PostgreSQL?

Procedures do not return a function value; hence CREATE PROCEDURE lacks a RETURNS clause. However, procedures can instead return data to their callers via output parameters. While a function is called as part of a query or DML command, a procedure is called in isolation using the CALL command.

What is stored function in PostgreSQL?

A PostgreSQL function or a stored procedure is a set of SQL and procedural commands such as declarations, assignments, loops, flow-of-control etc. stored on the database server and can be involved using the SQL interface. And it is also known as PostgreSQL stored procedures.

What is the difference between in out and inout parameter?

An INOUT parameter is a combination of IN and OUT parameters. It means that the calling program may pass the argument, and the stored procedure can modify the INOUT parameter and pass the new value back to the calling program.


2 Answers

IN and OUT are basically aliases for older syntax.

old way:

create function test(param int) 
returns integer as 
$$ select 1 $$ 
language sql;

equivalent:

create function test(in param int, out int)
as $$ select 1 $$
langauge sql;

What params do provide is type information which basically creates an anonymous type for your return:

create function test(in param, out int, out int)
as $$ select 1, 2 $$
langauge sql;

now you can write:

 select * from test(1);
 column1 | column2 
---------+---------
       1 |       2

Without the out params you would have have had to create a type or table that had two ints to cast the data to the right type:

create or replace function test(in a int) 
returns record as 
as $$ select 1, 2 $$ 
language sql;
                                ^
select * from test(1);
ERROR:  a column definition list is required 
   for functions returning "record"
like image 188
nate c Avatar answered Sep 21 '22 00:09

nate c


... actually I should have searched a bit more myself. The answer is not far away on the documentations page:

http://www.postgresql.org/docs/current/static/sql-createfunction.html

like image 24
Lukas Eder Avatar answered Sep 23 '22 00:09

Lukas Eder