PostgreSQL Tutorial

  • Home
  • Administration
  • Views
  • Triggers
  • Stored Procedures
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
Home / PostgreSQL Stored Procedures / PL/pgSQL Function Parameters

PL/pgSQL Function Parameters

Summary: in this tutorial, we will introduce you to various kinds of PL/pgSQL function parameters: IN, OUT , INOUT and VARIADIC.

PL/pgSQL IN parameters

Let’s start with an example of creating new function called get_sum() as follows:

1
2
3
4
5
6
7
8
9
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 two parameters are NUMERIC. By default, the parameter’s type of any parameter in PostgreSQL is IN parameter. You can pass the IN parameters to the function but you cannot get them back as a part of result.

1
SELECT get_sum(10,20);

PostgreSQL IN Parameters

PL/pgSQL OUT parameters

The OUT parameters is a part of the  function arguments list and you can get the result back as the part of the result. To define OUT parameters, you use OUT keyword. See the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
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 defining a custom type.

The following statement calls the hi_lo function:

1
SELECT hi_lo(10,20,30);

PostgreSQL OUT Parameters

The output of the function is a record, which is a custom type. To make the output separated as columns, you use the following syntax:

1
SELECT * FROM hi_lo(10,20,30);

PostgreSQL OUT Parameters Example

PL/pgSQL INOUT parameters

The INOUT parameter is the combination IN and OUT parameters. It means that the caller can pass the value to the function. The function then changes the argument and passes the value back as a part of the result.

The following example shows you the square function that accepts a number and returns the square of that number.

1
2
3
4
5
6
7
CREATE OR REPLACE FUNCTION square(
INOUT a NUMERIC)
AS $$
BEGIN
a := a * a;
END; $$
LANGUAGE plpgsql;

1
SELECT square(4);

PL/pgSQL VARIADIC parameters

A PostgreSQL function can accept a variable numbers of arguments with one condition that all arguments have the same data type. The arguments are passed to the function as an array. See the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;

The sum_avg() function accepts a list of numbers, calculates the total and average, and returns both values.

1
SELECT * FROM sum_avg(10,20,30);

PostgreSQL VARIADIC Parameters

In this tutorial, you have learned four type of function parameters including IN, OUT, INOUT and VARIADIC.

Previous Tutorial: Developing User-defined Functions Using PostgreSQL CREATE FUNCTION Statement
Next Tutorial: PL/pgSQL Function Overloading

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PL/pgSQL Getting Started

  • Introduction to Stored Procedures
  • PL/pgSQL Block Structure
  • PL/pgSQL Errors and Messages
  • PL/pgSQL Create Function
  • PL/pgSQL Function Parameters
  • PL/pgSQL Function Overloading
  • PL/pgSQL Function That Returns A Table
  • PL/pgSQL Variables
  • PL/pgSQL Constants
  • PL/pgSQL IF Statement
  • PL/pgSQL CASE Statement
  • PL/pgSQL Loop Statements
  • PL/pgSQL Cursor

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples
  • Understanding PostgreSQL Timestamp Data Types

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2016 by PostgreSQL Tutorial Website. All Rights Reserved.