Member-only story
PostgreSQL Functions: Part Two — Parameter Modes
SQL Functions Can Utilize Parameter Modes Called In, Out, and In-Out.
This is part two of my series on PostgreSQL functions (functions from here on). You can read part one here. In this series installment, I will cover what parameter modes are and their use case.
Be sure to follow me for part three of this series!
In, Out, and In-Out Parameters
PostgreSQL functions have three parameter modes: IN, OUT, and IN-OUT. Let’s look at definitions and examples for each mode.
IN Parameters
IN is the default mode of function arguments. They behave exactly as you would expect: an IN parameter is passed to a function when called, and its value can be referenced in the function body:
-- user_id is an IN parameter create or replace function get_user(user_id text)
returns table(id text, name text, title text) as
$$
begin
return query
select u.id, u.name, u.title
from users u
where u.id = user_id;
end
$$
language plpgsql;
OUT Parameters
OUT parameters represent values that a function returns. Because parameters default to IN, OUT parameters must be specified in the…