Search 'grep-alike' through PostgreSql functions
Asked Answered
Z

2

15

When refactoring PostgreSql functions (and more specific: while searching for 'unused' functions) it would be handy to have a function available to search for a specific string within the function definitions.

Does anyone know if this is the best approach (trying to 'grep'-search the function definitions) or are there alternative methods available?

How would one implement this functionality?

Zeiler answered 15/12, 2010 at 10:26 Comment(0)
E
30
SELECT 
  proname AS functionname, 
  prosrc AS source 
FROM 
  pg_proc 
WHERE 
  prosrc ~* 'your_string';

Details about how to use a regex in PostgreSQL, can be found in the manual.

Enrapture answered 15/12, 2010 at 10:42 Comment(1)
+1 (did not realize it could be this simple ;-) I'll wait a little while to see if other approaches pop up, but this is working perfectly fine!Zeiler
C
5

Frank's answer is great. Use this enhanced version to additionally list the schema which helps to locate the function:

SELECT
  proname AS functionname,
  prosrc AS source,
  nspname AS schema
FROM
  pg_proc
INNER JOIN pg_namespace ON (pg_proc.pronamespace = pg_namespace.oid)
WHERE
  prosrc ~* 'your_string';
Copro answered 11/1, 2017 at 8:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.