How to check which function uses a type?

久未见 提交于 2019-12-24 01:05:28

问题


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?


回答1:


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?



回答2:


As mentioned by Erwin Brandstetter, this only works for functions directly returning the data type.

SELECT * FROM information_schema.routines r 
WHERE r.type_udt_name = 'YOUR_DATA_TYPE' ORDER BY r.routine_name;


来源:https://stackoverflow.com/questions/33546586/how-to-check-which-function-uses-a-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!