I keep all my functions in a text file with 'CREATE OR REPLACE FUNCTION somefunction'
.
So if I add or change some function I just feed the file to psql.
Now if I add or remove parameters to an existing function, it creates an overload with the same name and to delete the original I need type in all the parameter types in the exact order which is kind of tedious.
Is there some kind of wildcard I can use to DROP all functions with a given name so I can just add DROP FUNCTION
lines to the top of my file?
Improving original answer in order to take
schema
into account, ie.schema.my_function_name
,Basic query
This query creates all necessary DDL statements (simplified with cast to
regprocedure
):Output:
Execute the commands (after a plausibility check).
The function name is case-sensitive and with no added double-quotes when passed as
text
parameter to match againstpg_proc.proname
.The cast to the object identifier type
regprocedure
(oid::regprocedure
) makes all identifiers safe against SQL injection (by way of maliciously malformed identifiers). When converting totext
, the function name is double-quoted and schema-qualified according to the currentsearch_path
automatically where needed.pg_function_is_visible(oid)
restricts the selection to functions in the currentsearch_path
. You may or may not want that. With the conditionpg_function_is_visible(oid)
in place, the function is guaranteed to be visible.If you have multiple functions of the same name in multiple schemas, or overloaded functions with various function arguments, all of those will be listed separately. You may want to restrict to specific schema(s) or specific function parameter(s) after all.
Related:
Function
You can build a
plpgsql
function around this to execute the statements immediately withEXECUTE
. For Postgres 9.1 or later: Careful! It drops your functions!Call:
Or just:
This way you don't get the column name
func_dropped
for the result column. May not matter to you.The function returns the number of functions found and dropped (no exception raised) -
0
if none were found.It assumes a (default)
search_path
wherepg_catalog
has not been moved around.More in these related answers:
For Postgres versions older than 9.1 or older variants of the function using
regproc
andpg_get_function_identity_arguments(oid)
check the edit history of this answer.As of Postgres 10 you can drop functions by name only, as long as the names are unique to their schema. Just place the following declaration at the top of your function file:
Documentation here.
You would need to write a function that took the function name, and looked up each overload with its parameter types from
information_schema
, then built and executed aDROP
for each one.EDIT: This turned out to be a lot harder than I thought. It looks like
information_schema
doesn't keep the necessary parameter information in itsroutines
catalog. So you need to use PostgreSQL's supplementary tablespg_proc
andpg_type
:I successfully tested this on an overloaded function. It was thrown together pretty fast, but works fine as a utility function. I would recommend testing more before using it in practice, in case I overlooked something.
Slightly enhanced version of Erwin's answer. Additionally supports following
Code for copy/paste:
Here is the query I built on top of @Сухой27 solution that generates sql statements for dropping all the stored functions in a schema: