Thread: Find if a function is being referenced in another function in the database

Find if a function is being referenced in another function in the database

From
Sumeet Shukla
Date:
Can someone guide me on how can I find if a given function is being referenced by other functions in the same database. If being referenced then a list of functions that are referencing it?

Any help would be highly appreciated. 

Please let me know if you need more details from me to answer this.


Thanks & Regards,
Sumeet Shukla
Ph. No. 962 323 4700
Sumeet Shukla <sumeet.k.shukla@gmail.com> writes:
> Can someone guide me on how can I find if a given function is being
> referenced by other functions in the same database. If being referenced
> then a list of functions that are referencing it?

Postgres does not track this in any way, because function bodies are for
the most part just black boxes to the core database.

As a first approximation, you could grep (LIKE/regexp search) the
pg_proc.prosrc column for the names of functions you're interested in.
This would have some problems with overloaded functions, and it's
certainly possible to hide calls from it if you want to, but it's a start.
Something like

select s.proname as caller, t.proname as callee
from pg_proc t, pg_proc s
where s.prosrc ~ ('\W' || t.proname || '\(')
  and s.prolang > 13 and t.prolang > 13;

(The prolang conditions exclude C/internal functions; this query will
run quite a long time if you include those.)

            regards, tom lane


Re: Find if a function is being referenced in another function in the database

From
Sumeet Shukla
Date:
Thank You for answering the query :-)

Thanks & Regards,
Sumeet Shukla
Ph. No. 962 323 4700

On Wed, Dec 2, 2015 at 8:35 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Sumeet Shukla <sumeet.k.shukla@gmail.com> writes:
> Can someone guide me on how can I find if a given function is being
> referenced by other functions in the same database. If being referenced
> then a list of functions that are referencing it?

Postgres does not track this in any way, because function bodies are for
the most part just black boxes to the core database.

As a first approximation, you could grep (LIKE/regexp search) the
pg_proc.prosrc column for the names of functions you're interested in.
This would have some problems with overloaded functions, and it's
certainly possible to hide calls from it if you want to, but it's a start.
Something like

select s.proname as caller, t.proname as callee
from pg_proc t, pg_proc s
where s.prosrc ~ ('\W' || t.proname || '\(')
  and s.prolang > 13 and t.prolang > 13;

(The prolang conditions exclude C/internal functions; this query will
run quite a long time if you include those.)

                        regards, tom lane