Postgres PLpgsql Function Parameters
If you’re planning to use PL/pgSQL, the procedural language for PostgreSQL, it’s important to understand how functions work in this language. One key component of a function is its parameters. In PostgreSQL, you can define both IN and OUT parameters when you create a PL/pgSQL function. This article will take a closer look at Postgres PLpgSQL function parameters and show you some examples to demonstrate how they’re used.
If you’d like to follow along with the examples in this tutorial, you’ll need to have Postgres installed and configured. Simply download the following for your operating system:
Basic PostgreSQL knowledge is also required to get the most out of this tutorial.
PLpgsql Function IN Parameter
In this section, we’ll show how to use the Postgres IN parameter.
If you have any programming experience, the IN parameter will seem familiar– this type of parameter is used to pass a value to a specific function within an application. Keep in mind that the attribute of this parameter is read-only, which means we cannot alter its value within the method.
Shown below is the basic form of the IN parameter:
CREATE [OR REPLACE] FUNCTION <function_name> (
first_param IN datatype, second_param IN datatype ... )
The example below shows how to use the IN parameter in a function:
CREATE OR REPLACE FUNCTION Sum_it_up(
RETURNS NUMERIC AS $$
RETURN value1 + value2 + value3;
There’s quite a bit going on in this example, so let’s take a closer look at it:
- We created a function named
Sum_it_up()that takes three parameters: value1, value2 and value3. The function returns a NUMERIC type as declared. We are allowed to pass the Postgres IN parameters to the function; we then retrieve the sum of these values as part of the result. The syntax shown below demonstrates how to execute the function:
postgres=# SELECT sum_it_up(2,4,6);
After we execute the function, we should see something like this:
We can see that the function accepted our IN parameters and added the values together to return the sum.
PLpgsql Function OUT Parameter
In this section, we’ll show you how to use the Postgres OUT parameter.
Unlike the IN parameter, the OUT parameter is returned either as a component or as part of the result.
Let’s look at the basic form of the OUT parameter:
CREATE [OR REPLACE] FUNCTION <function_name> (parameter_name OUT datatype)
The following example shows us how to use the OUT parameter as part of a function:
CREATE OR REPLACE FUNCTION higher_lower(
OUT higher NUMERIC,
OUT LOWER NUMERIC)
higher := GREATEST(value1,value2,value3);
LOWER := LEAST(value1,value2,value3);
Let’s discuss the above example in further detail:
We created a function that accepts five parameters. Shown below is the breakdown of these parameters:
- We provide three IN parameters: value1, value2 and value3.
- We have two OUT parameters: higher and lower
Notice that we did not specify a return value– by default, the OUT parameters are used to return the multiple values. In this example, the OUT parameters will return the highest and lowest values that are supplied by the IN parameters.
We can execute this function using a
SELECT statement as seen in the following example:
The result should look something like the following:
We can see that our OUT parameters worked as expected, returning the highest and lowest values from the set of IN parameters.
It’s important to understand how functions are defined when you work with PL/pgSQL. Knowing how function parameters work is essential for writing effective PL/pgSQL functions. In this article, we discussed the different types of Postgres PLpgsql function parameters and provided examples of their use. With these examples and instructions to get you started, you’ll be ready to create PL/pgSQL functions that work with your own PostgreSQL database.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.Get Started