2

for a given table how can i find all functions that uses ANY fields from that table? say i have student table with (studentid, studentname)

i want a list of all functions and triggers that uses studentid and studentname from student.

my question is similar to List stored functions that reference a table in PostgreSQL but the code given there isn't working... n.nspname which is given in the condition there is related to Schema not to table.

4
  • Your table name is in the column prosrc, you have to query that column using a like or regex. This has nothing to do with the schema where the function is located. Commented Jun 3, 2015 at 9:00
  • Tables have columns, not fields... BTW, information_schema.ROUTINE_TABLE_USAGE? Commented Jun 3, 2015 at 9:01
  • what about id jarlh? Commented Jun 3, 2015 at 9:14
  • @jarlh postgresql doesn't have information_schema.routine_table_usage. Commented Jun 3, 2015 at 10:10

1 Answer 1

3

Perhaps is not very precise, but this query should work.

SELECT routine_schema, routine_name 
FROM information_schema.routines 
WHERE routine_definition ~ 'student' 
AND routine_definition ~ 'studentid' 
AND routine_definition ~ 'studentname';

Depending on how you wrote the procedure, can you apply more precise regex expressions. In example, if you always wrote tables and columns in this form: table.column could you use this expression:

... WHERE routine_definition ~ 'student\.studentid' 
AND routine_definition ~ 'student\.studentname'
Sign up to request clarification or add additional context in comments.

1 Comment

Since sql and plpgsql is case insensitive it's wise to use case insensitive operator ~* to search. Also please keep in mind that function can depend on table using dynamic executed sql which may be very difficult to identify without human review.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.