PostgreSQL - Function Parameters
Last Updated :
05 Nov, 2024
In PostgreSQL, functions provide an efficient way to encapsulate logic, perform calculations, and handle complex tasks within a database. A thorough understanding of PostgreSQL function parameters is essential for writing flexible and optimized functions.
In this article, we will analyze different types of function parameters, including IN, OUT, INOUT, and VARIADIC parameters, along with examples to illustrate their usage.
What Are Function Parameters in PostgreSQL?
Function parameters in PostgreSQL define how data is passed in and out of a function. Understanding how these parameters work is essential for optimizing function design and enabling flexible functionality. PostgreSQL supports four primary types of function parameters:
- IN Parameter
- OUT Parameter
- INOUT Parameter
- VARIADIC Parameter
1. IN Parameter
The IN parameter is the most common type and is used to pass values into a function. These values are used by the function during execution but cannot be modified or returned by the function. By default, all function parameters in PostgreSQL are of type IN. To better understand these function parameters let’s define a simple function 'get_sum()' that accepts two numbers and returns their sum:
syntax:
CREATE OR REPLACE FUNCTION get_sum(
a NUMERIC,
b NUMERIC)
RETURNS NUMERIC AS $$
BEGIN
RETURN a + b;
END;
$$ LANGUAGE plpgsql;
The 'get_sum()' function accepts two parameters: a, and b, and returns a numeric.
The data types of the two parameters are NUMERIC. By default, the parameter’s type of any parameter in PostgreSQL is IN parameter. We can pass the IN parameters to the function but we cannot get them back as a part of the result.
Example
SELECT get_sum(10, 20);
Output
IN Parameter2. Out Parameter
The OUT parameter allows us to return values from a function as part of the result set. When we define an OUT parameter, we don’t need to include a RETURN statement in the function; the values of the OUT parameters are automatically returned. For better understanding let's define a function 'hi_lo()' that takes three numbers and returns the highest and lowest values:
Syntax
CREATE OR REPLACE FUNCTION hi_lo(
a NUMERIC,
b NUMERIC,
c NUMERIC,
OUT hi NUMERIC,
OUT lo NUMERIC)
AS $$
BEGIN
hi := GREATEST(a, b, c);
lo := LEAST(a, b, c);
END;
$$ LANGUAGE plpgsql;
The 'hi_lo()' function accepts 5 parameters:
- Three IN parameters: a, b, c.
- Two OUT parameters: hi (high) and lo (low).
Inside the function, we get the greatest and least numbers of three IN parameters using GREATEST and LEAST built-in functions. Because we use the OUT parameters, we don’t need to have a RETURN statement. The OUT parameters are useful in a function that needs to return multiple values without the need of defining a custom type.
Example
SELECT hi_lo(10, 20, 30);
Output:
OUT Parameter3. INOUT Parameter
The INOUT parameter is the combination IN and OUT parameters. It means that the caller can pass a value to the function. The function then changes the argument and passes the value back as a part of the result. The following 'square()' function accepts a number and returns the square of that number.
Syntax:
CREATE OR REPLACE FUNCTION square(
INOUT a NUMERIC)
AS $$
BEGIN
a := a * a;
END;
$$ LANGUAGE plpgsql;
Example
SELECT square(4);
Output:
INOUT Parameter4. VARIADIC Parameters
The VARIADIC parameter allows a function to accept a variable number of arguments, provided that all arguments are of the same data type. These arguments are passed to the function as an array, which we can then manipulate within the function. Let’s create a function 'sum_avg()' that accepts a list of numbers, calculates their sum and average, and returns both values:
Syntax:
CREATE OR REPLACE FUNCTION sum_avg(
VARIADIC list NUMERIC[],
OUT total NUMERIC,
OUT average NUMERIC)
AS $$
BEGIN
SELECT INTO total SUM(list[i])
FROM generate_subscripts(list, 1) g(i);
SELECT INTO average AVG(list[i])
FROM generate_subscripts(list, 1) g(i);
END;
$$ LANGUAGE plpgsql;
Example
The 'sum_avg()' function accepts a list of numbers, calculates the total and average, and returns both values.
SELECT * FROM sum_avg(10, 20, 30);
Output:

Best Practices for Using Function Parameters in PostgreSQL
- Keep IN parameters for inputs that do not need to be modified or returned by the function.
- Use OUT parameters when we need to return multiple values without creating a custom type.
- Choose INOUT parameters when the input value needs to be modified and returned as part of the function's result.
- Utilize VARIADIC parameters for functions that need to handle a flexible number of inputs, ensuring they are of the same type.
Conclusion
Understanding PostgreSQL function parameters IN, OUT, INOUT, and VARIADIC enhances our ability to create efficient, optimized, and flexible database functions. These parameters allow for various ways to pass data into and out of functions, accommodating different types of requirements within PostgreSQL. Whether we are dealing with single inputs, multiple return values, or dynamic lists, PostgreSQL's function parameters provide the tools we need to structure and manage our functions.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Introduction of DBMS (Database Management System) A Database Management System (DBMS) is a software solution designed to efficiently manage, organize, and retrieve data in a structured manner. It serves as a critical component in modern computing, enabling organizations to store, manipulate, and secure their data effectively. From small application
8 min read
SQL Query Interview Questions SQL or Structured Query Language, is the standard language for managing and manipulating relational databases such as MySQL, Oracle, and PostgreSQL. It serves as a powerful tool for efficiently handling data whether retrieving specific data points, performing complex analysis, or modifying database
15 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. They enable calculations across a specific set of rows, known as a "window," while retaining the individual rows in the dataset. Unlike traditional aggregate functions that summarize data for the entire group, win
7 min read