Sub topic 10
Replacing, Overloading and Dropping Functions
Assume you can run basic SQL statements and understand PostgreSQL schemas, roles, and data types. This chapter focuses on managing existing functions without accidentally changing the wrong object.
A PostgreSQL function has an identity made from its schema, name, and input argument types. The return type is not part of that identity.
flowchart TD
A["Function call"] --> B["Schema and name"]
B --> C["Input argument types"]
C --> D["One function identity"]
D --> E["Return value and body"]Replacing an existing function
Use CREATE OR REPLACE FUNCTION when you want to change a function's body or selected properties while preserving the function object. Existing ownership and privileges remain attached to the function.
CREATE FUNCTION public.discounted_price(price numeric)
RETURNS numeric
LANGUAGE sql
IMMUTABLE
AS $$
SELECT price * 0.90;
$$;
CREATE OR REPLACE FUNCTION public.discounted_price(price numeric)
RETURNS numeric
LANGUAGE sql
IMMUTABLE
AS $$
SELECT round(price * 0.90, 2);
$$;The replacement must keep the same function name and input argument types. You cannot use CREATE OR REPLACE FUNCTION to change the return type. To make that change, create a new function under a different identity, or drop and recreate the old one after considering its dependencies.
Parameter names are useful inside the body, but PostgreSQL identifies overloads by input types, not parameter names. Therefore, changing a parameter name can affect callers that use named notation even though the function identity stays the same.
Overloading functions
Overloading means defining several functions with the same name but different input argument types or counts. PostgreSQL chooses the best match for the types in a call.
CREATE FUNCTION public.format_id(value integer)
RETURNS text
LANGUAGE sql
IMMUTABLE
AS $$
SELECT 'int:' || value;
$$;
CREATE FUNCTION public.format_id(value text)
RETURNS text
LANGUAGE sql
IMMUTABLE
AS $$
SELECT 'text:' || value;
$$;
SELECT format_id(42);
SELECT format_id('42'::text);The explicit cast makes the intended overload clear. Untyped string literals and implicit casts can sometimes make a call ambiguous.
Default parameters require extra care. A function with one required argument and a function with two arguments where the second has a default may both match a call with one argument. Avoid overlapping signatures unless you have tested every intended call.
| Operation | Object selected by |
|---|---|
| `CREATE OR REPLACE FUNCTION` | Name and input argument types |
| `ALTER FUNCTION` | Name and input argument types |
| `DROP FUNCTION` | Name and input argument types |
| Function call resolution | Name, supplied values, casts, and defaults |
Altering and dropping functions
Use ALTER FUNCTION for properties such as volatility, strictness, security mode, ownership, schema, estimated cost, and parallel safety.
ALTER FUNCTION public.discounted_price(numeric)
COST 5
PARALLEL SAFE;
ALTER FUNCTION public.discounted_price(numeric)
RENAME TO sale_price;The argument list in management commands contains only input argument types. Do not include parameter names or the return type.
Drop a function by specifying its exact identity:
DROP FUNCTION public.sale_price(numeric);CASCADE also removes dependent objects, so use it only after reviewing the dependency impact. Prefer RESTRICT, the default, when you want PostgreSQL to stop instead of removing related objects.
Procedures are separate objects. Manage them with ALTER PROCEDURE and DROP PROCEDURE, and invoke them with CALL.
Exercise
Can CREATE OR REPLACE FUNCTION change a function's return type from numeric to text?
Show answer
No. It can replace the body and compatible properties, but it cannot change the return type. Create a new function or deliberately drop and recreate the old one.