How to Fix Postgres Error 42723 Duplicate Function

Published
Length
3 min read

Luis Felipe Dias

All posts

You trigger postgres error 42723 duplicate function when you create an existing routine. You fix it by replacing the routine or dropping the old version first. Database migrations fail when scripts try to build the same routine twice. The engine stops the transaction to protect your existing logic. Teams see this error during schema updates or manual database changes. Understanding how the database tracks routines helps you resolve the conflict fast. What causes postgres error 42723 duplicate

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You trigger postgres error 42723 duplicate function when you create an existing routine. You fix it by replacing the routine or dropping the old version first.

Database migrations fail when scripts try to build the same routine twice. The engine stops the transaction to protect your existing logic. Teams see this error during schema updates or manual database changes. Understanding how the database tracks routines helps you resolve the conflict fast.

What causes postgres error 42723 duplicate function

PostgreSQL maps state 42723 to the duplicate_function condition and ERRCODE_DUPLICATE_FUNCTION macro. The database identifies a routine by its schema, name, and input argument types. You can have two routines with the same name in one schema. This works only when their input argument types differ. If the name and input types match, the engine blocks the new definition.

Migration scripts often cause this conflict when they lack replace clauses. A developer might run a creation script twice in the same test database. Another common trigger is changing return types or parameter names. An update operation cannot change a routine return type in place. If a script tries to recreate it without dropping it first, 42723 occurs.

Schema search paths also contribute to confusion during deployments. In PostgreSQL 15 and later, normal users cannot create objects in the public schema. In older versions, an admin revokes CREATE on the public schema from PUBLIC. If a script targets the wrong schema, it may collide with existing objects.

How to diagnose and fix duplicate routines safely

Start by checking your system catalog to inspect existing routines. Look up the routine name, schema, input argument types, and return type. Compare the existing signature in the database with the signature in your script. Check if another database object, like a view or trigger, depends on it.

Once you find the conflict, choose the safest update method for production. If the input types and return type match, use a replace operation. This updates the routine body without breaking dependent views or permissions. If the return type changed, you must drop the old routine first. Wrap the drop and create steps inside one transaction block.

Serverless Function Security + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Be careful with permissions when you drop and recreate database routines. By default, the special role PUBLIC gets EXECUTE privileges on new functions and procedures. Every role on the system inherits privileges granted to PUBLIC. The object owner can revoke these default execute rights after creation. You can also set up group roles to manage permissions for many users. Grant users membership in the group role instead of giving out broad rights.

Preventing schema conflicts in production

Unplanned manual changes in production often lead to state 42723 errors later. When an engineer creates a quick fix by hand, migration tools lose track. The next automated release tries to create the same routine and fails.

Teams prevent this by restricting who can run schema changes in production. Keep everyday accounts limited to read rights on specific tables or columns. The SELECT privilege allows reading from specific columns of a table or view. When someone queries user data, masking rewrites matched fields like emails or phone numbers. Run all routine updates through peer review and automated deployment pipelines. This keeps your schema state clean and predictable across every database instance.

Add query and result controls beside Postgres

Grants and pools govern who logs into PostgreSQL. Yet every query in an open session still runs with that role's full scope. In practice, hoop.dev sits inline in the wire protocol beside your database as a sidecar for your Postgres. For every plain or prepared query an agent or human runs against production, hoop.dev decides to pass, mask, block, or escalate it for review.

When a tool or agent sends SQL, hoop.dev Postgres controls block banned statements before execution or hold risky ones for human approval via an AI Analyzer you configure. Matched sensitive columns in result rows are masked in memory before they reach the caller. Each query and decision is logged per listener, which generates evidence for SOC 2, HIPAA, and GDPR audits. View the binary on GitHub.

Frequently asked questions

Can two routines have the same name in PostgreSQL?

Yes, function overloading allows multiple routines to share a single name. They must have different input argument types so the engine can tell them apart.

Why does replacing a routine sometimes fail with a duplicate error?

Replacing a routine in place fails if you change its return type. You must drop the existing routine first and then create the new version.

What happens to permissions when I drop and recreate a function?

Dropping a function removes its custom access grants. New functions grant EXECUTE to PUBLIC by default until the owner revokes it.

The query runs. The emails stay hidden.