Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check which function uses a type?

I have a type which I'd like to change but I don't know who else is using it.

How can I check for all functions that return this type?

like image 960
avi Avatar asked May 29 '26 08:05

avi


1 Answers

You can find all dependencies in the system catalog pg_depend.

This returns all functions depending on the type. I.e. not only those with the type in the RETURNS clause, but also those with the type as function parameter:

SELECT objid::regproc                            AS function_name
     , pg_get_functiondef(objid)                 AS function_definition
     , pg_get_function_identity_arguments(objid) AS function_args
     , pg_get_function_result(objid)             AS function_returns
FROM   pg_depend
WHERE  refclassid = 'pg_type'::regclass
AND    refobjid   = 'my_type'::regtype    -- insert your type name here
AND    classid    = 'pg_proc'::regclass;  -- only find functions

This also works for table functions:

...
RETURNS TABLE (foo my_type, bar int)

Using system catalog information functions.

There may be other dependencies (not to functions). Remove the last WHERE condition from my query to test (and adapt the SELECT list, obviously).

And there is still the possibility of the type being used explicitly (in a cast for instance) in queries in the function body or in dynamic SQL. You can only identify such use cases by parsing the text of the function body. There are no explicit dependencies registered in the system.

Related:

  • How to get function parameter lists (so I can drop a function)
  • DROP FUNCTION without knowing the number/type of parameters?
like image 76
Erwin Brandstetter Avatar answered May 30 '26 20:05

Erwin Brandstetter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!