Professional Documents
Culture Documents
Donate Now
(https://www.postgresqltutorial.com/donation/)
IN OUT INOUT
Pass a value to Return a value from a function Pass a value to a function and return
function an updated value.
https://www.postgresqltutorial.com/plpgsql-function-parameters/ 1/5
7/8/2021 PL/pgSQL Function Parameter Modes: IN, OUT, and INOUT
IN OUT INOUT
in parameters act like out parameters act like inout parameters act like an initialized
constants uninitialized variables variables
The IN mode
The following function finds a film by its id and returns the title of the film:
returns varchar
language plpgsql
as $$
declare
film_title film.title%type;
begin
select title
into film_title
from film
end if;
return title;
end;$$
Because we didn’t specify the mode for p_film_id parameter, it takes the in mode by default.
https://www.postgresqltutorial.com/plpgsql-function-parameters/ 2/5
7/8/2021 PL/pgSQL Function Parameter Modes: IN, OUT, and INOUT
The out parameters are defined as a part of the argument list and are returned back as a part of the
result.
The out parameters are very useful in functions that need to return multiple values.
Note that PostgreSQL has supported the out parameters since version 8.1.
To define out parameters, you explicitly precede the parameter name with the out keyword as
follows:
The following example defines the get_film_stat function that has three out parameters:
language plpgsql
as $$
begin
select min(length),
max(length),
avg(length)::numeric(5,1)
from film;
end;$$
In the get_film_stat function, we select the min, max, and average of film length from the film
table using the min , max , and avg aggregate functions and assign the results to the corresponding
https://www.postgresqltutorial.com/plpgsql-function-parameters/ 3/5
7/8/2021 PL/pgSQL Function Parameter Modes: IN, OUT, and INOUT
out parameters.
select get_film_stat();
The output of the function is a record. To make the output separated as columns, you use the
following statement:
It means that the caller can pass an argument to a function. The function changes the argument and
returns the updated value.
The following swap function accepts two integers and their values:
inout x int,
inout y int
language plpgsql
as $$
begin
end; $$;
https://www.postgresqltutorial.com/plpgsql-function-parameters/ 4/5
7/8/2021 PL/pgSQL Function Parameter Modes: IN, OUT, and INOUT
Summary
PL/pgSQL support three parameter modes: in, out, and intout. By default, a parameter takes the
in mode.
Use the out mode if you want to return a value from a function.
Use the inout mode when you want to pass in an initial value, update the value in the function,
and return it updated value back.
https://www.postgresqltutorial.com/plpgsql-function-parameters/ 5/5