PL - PGSQL Function Overloading
PL - PGSQL Function Overloading
Donate Now
(https://www.postgresqltutorial.com/donation/)
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:
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
into rental_duration
from rental
return rental_duration;
end; $$
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:
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
into rental_duration
from rental
return rental_duration;
end; $$
This function has the same name as the first one except that it has two parameters.
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.
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:
p_customer_id integer,
returns integer
language plpgsql
as $$
declare
rental_duration integer;
begin
select sum(
into rental_duration
from rental
return rental_duration;
end; $$
The following calls the get_rental_duration() function and passes the customer id 232:
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.
get_rental_duration(p_customer_id integer);
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.
https://www.postgresqltutorial.com/plpgsql-function-overloading/ 5/5