0% found this document useful (0 votes)
92 views

PL - PGSQL Function Overloading

This document discusses function overloading in PostgreSQL. It explains that PostgreSQL allows multiple functions to share the same name if they have different arguments. When calling an overloaded function, PostgreSQL selects the best function match based on the arguments. The document provides examples of overloaded get_rental_duration() functions that return rental duration based on customer ID alone or customer ID and date. It notes issues that can occur with overloading and default values.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

PL - PGSQL Function Overloading

This document discusses function overloading in PostgreSQL. It explains that PostgreSQL allows multiple functions to share the same name if they have different arguments. When calling an overloaded function, PostgreSQL selects the best function match based on the arguments. The document provides examples of overloaded get_rental_duration() functions that return rental duration based on customer ID alone or customer ID and date. It notes issues that can occur with overloading and default values.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

7/8/2021 PL/pgSQL Function Overloading

PL/pgSQL Function Overloading

If this PostgreSQL Tutorial saves you


hours of work, please whitelist it in
your ad blocker 😭 and

Donate Now
(https://www.postgresqltutorial.com/donation/)

to help us ❤️pay for the web


hosting fee and CDN to keep the
website running.
Summary: in this tutorial, you will learn about function overloading in PostgreSQL.

Introduction to PL/pgSQL Function Overloading

PostgreSQL allows multiple functions (https://www.postgresqltutorial.com/postgresql-create-function/) to share the


same name as long as they have different arguments.

If two or more functions share the same name, the function names are overloaded.

When you can call an overloading function, PostgreSQL select the best candidate function to execute
based on the the function argument list.

The following get_rental_duration() function returns the total rental days of a specified customer:

create or replace function get_rental_duration(

p_customer_id integer

returns integer

language plpgsql

as $$

declare

https://www.postgresqltutorial.com/plpgsql-function-overloading/ 1/5
7/8/2021 PL/pgSQL Function Overloading

rental_duration integer;

begin

select

sum( extract(day from return_date - rental_date))

into rental_duration

from rental

where customer_id = p_customer_id;

return rental_duration;

end; $$

The get_rental_function function has the p_customer_id as an in parameter.

The following return the number of rental days of the customer id 232:

SELECT get_rental_duration(232);

get_rental_duration

---------------------

90

(1 row)

Suppose that you want to know the rental duration of a customer from a specific date up to now.

To do it, you can add one more parameter p_from_date to the get_retal_duration() function. Or
you can develop a new function with the same name but have two parameters like this:

create or replace function get_rental_duration(

p_customer_id integer,

p_from_date date

returns integer

language plpgsql

as $$

https://www.postgresqltutorial.com/plpgsql-function-overloading/ 2/5
7/8/2021 PL/pgSQL Function Overloading

declare

rental_duration integer;

begin

-- get the rental duration based on customer_id

-- and rental date

select sum( extract( day from return_date + '12:00:00' - rental_date))

into rental_duration

from rental

where customer_id = p_customer_id and

rental_date >= p_from_date;

-- return the rental duration in days

return rental_duration;

end; $$

This function has the same name as the first one except that it has two parameters.

In other words, the get_rental_duration(integer)  function is overloaded by the


get_rental_duration(integer,date) function.

The following statement returns the rental duration of the customer id 232 since July 1st 2005 :

SELECT get_rental_duration(232,'2005-07-01');

get_rental_duration

---------------------

85

(1 row)

Note that if you omit the second argument, PostgreSQL will call the get_rental_duration(integer)
function that has one parameter.

PL/pgSQL function overloading and default values

https://www.postgresqltutorial.com/plpgsql-function-overloading/ 3/5
7/8/2021 PL/pgSQL Function Overloading

In the get_rental_duration(integer,date) function, if you want to set a default value to the second
argument like this:

create or replace function get_rental_duration(

p_customer_id integer,

p_from_date date default '2005-01-01'

returns integer

language plpgsql

as $$

declare

rental_duration integer;

begin

select sum(

extract( day from return_date + '12:00:00' - rental_date)

into rental_duration

from rental

where customer_id= p_customer_id and

rental_date >= p_from_date;

return rental_duration;

end; $$

The following calls the get_rental_duration() function and passes the customer id 232:

SELECT get_rental_duration(232);

PostgreSQL issued an error:

ERROR: function get_rental_duration(integer) is not unique

LINE 1: SELECT get_rental_duration(232);

https://www.postgresqltutorial.com/plpgsql-function-overloading/ 4/5
7/8/2021 PL/pgSQL Function Overloading

HINT: Could not choose the best candidate function. You might need to add explic
SQL state: 42725

Character: 8

In this case, PostgreSQL could not choose the best candidate function to execute.

In this scenario, you have three functions:

get_rental_duration(p_customer_id integer);

get_rental_duration(p_customer_id integer, p_from_date date)

get_rental_duration(p_customer_id integer, p_from_date date default '2005-01-01'

PostgreSQL did not know whether it should execute the first or the third function.

As a rule of thumb, when you overload a function, you should always make their parameter list unique.

Summary
Multiple functions can share the same names as long as they have different arguments. These
function names are overloaded.

Use a unique function argument list to define overloading functions.

https://www.postgresqltutorial.com/plpgsql-function-overloading/ 5/5

You might also like