PL/pgSQL Function Overloading
Summary: in this tutorial, you will learn about function overloading in PostgreSQL.
Introduction to PL/pgSQL Function Overloading
PostgreSQL allows multiple functions to share the same name as long as they have different arguments.
If two or more functions share the same name, they are considered overloaded.
When you call an overloaded function, PostgreSQL selects the best candidate function to execute based on the function argument list.
The following get_rental_duration()
function returns the total rental days of a specified customer:
The get_rental_function
function has the p_customer_id
as an in
parameter.
The following returns the number of rental days of customer id 232:
Output:
Suppose that you want to know the rental duration of a customer from a specific date up to now.
To do that, you can add one more parameter p_from_date
to the get_retal_duration()
function. Alternatively, you can develop a new function with the same name but have two parameters like this:
This function shares the same name as the first one, except 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
:
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
In the get_rental_duration(integer,date)
function, if you want to set a default value to the second argument like this:
The following calls the get_rental_duration()
function and passes the customer id 232:
Error:
In this case, PostgreSQL could not choose the best candidate function to execute.
In this scenario, you have three functions:
PostgreSQL did not know whether it should execute the first or the third ones.
As a rule of thumb, when overloading functions, you should always ensure their parameter lists are 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 overloaded functions.