Error
Error Code:
42723
PostgreSQL Error 42723: Duplicate Function Definition
Description
This error indicates an attempt to create a function with a name and argument signature that already exists in the current database schema. PostgreSQL requires unique function signatures to avoid ambiguity when calling functions, treating a new function with an identical signature as a duplicate.
Error Message
duplicate function
Known Causes
4 known causesFunction Already Exists
You tried to create a function using `CREATE FUNCTION` when a function with the exact same name and argument types already exists in the same schema.
Case Sensitivity Mismatch
Attempting to create a function where the name, when interpreted by PostgreSQL (especially without double quotes), clashes with an existing function due to case-insensitivity rules.
Script Reruns Without Check
Executing a SQL script multiple times that attempts to `CREATE FUNCTION` without using `CREATE OR REPLACE` or checking if the function already exists.
Concurrent Development Conflict
Multiple developers attempting to create functions with identical signatures in the same schema simultaneously or without proper coordination.
Solutions
3 solutions available1. Identify and Remove Duplicate Function Definition easy
Locate and delete the redundant function definition that's causing the conflict.
1
Connect to your PostgreSQL database using a client like `psql` or pgAdmin.
2
Query the `pg_proc` catalog to find functions with the same name and parameter types.
SELECT proname, pg_get_function_identity_arguments(oid) FROM pg_proc WHERE proname = 'your_function_name';
3
Examine the output. If you see multiple entries for the same function name and arguments, identify the one you wish to keep and the one(s) to remove. Note the function's name and its arguments.
4
Drop the duplicate function definition. Replace 'your_function_name' and the argument list with the actual details of the duplicate function you want to remove.
DROP FUNCTION your_function_name(arg1_type, arg2_type, ...);
5
If the function has the same name but different argument types, you might need to specify the full signature for the `DROP FUNCTION` command. For example, if you have `my_func(integer)` and `my_func(text)`, you'd drop them individually.
DROP FUNCTION my_func(integer);
DROP FUNCTION my_func(text);
2. Review and Refactor Code Causing Duplication medium
Analyze the scripts or application code that is creating functions to prevent future duplicates.
1
Identify the source of the function creation. This could be SQL scripts, migration files, or application code that dynamically generates SQL.
2
Examine the scripts for any `CREATE FUNCTION` statements that might be executed more than once or that are attempting to create a function that already exists.
3
Add `IF NOT EXISTS` clause to your `CREATE FUNCTION` statements to prevent the error if the function already exists. This is a common practice in idempotent scripts.
CREATE OR REPLACE FUNCTION your_function_name(arg1_type, arg2_type, ...) RETURNS return_type AS $$
-- function body
$$ LANGUAGE plpgsql;
-- Alternatively, for specific cases where you want to ensure it's only created if it doesn't exist:
CREATE FUNCTION IF NOT EXISTS your_function_name(arg1_type, arg2_type, ...) RETURNS return_type AS $$
-- function body
$$ LANGUAGE plpgsql;
4
If you are using a migration tool (e.g., Flyway, Liquibase), ensure that each migration script is designed to be run only once and that it handles potential existing objects gracefully. Check for duplicate migration file names or sequences.
3. Use `CREATE OR REPLACE FUNCTION` for Updates easy
Employ `CREATE OR REPLACE FUNCTION` when intending to update an existing function.
1
When you need to modify an existing function, use `CREATE OR REPLACE FUNCTION` instead of `CREATE FUNCTION`. This command will create the function if it doesn't exist, or replace it if it does, preventing the duplicate definition error.
CREATE OR REPLACE FUNCTION your_function_name(arg1_type, arg2_type, ...) RETURNS return_type AS $$
-- New function body
$$ LANGUAGE plpgsql;
2
Ensure that the function signature (name and argument types) remains consistent if you intend to replace an existing function. If the signature changes, it's effectively a new function and might still lead to an error if the old one isn't dropped first.