8

I have the following query to retrieve function definitions:

select pg_get_functiondef
from (
   select pp.proname, pl.lanname,pn.nspname, pg_get_functiondef(pp.oid) 
   from pg_proc pp
   inner join pg_namespace pn on (pp.pronamespace = pn.oid)
   inner join pg_language pl on (pp.prolang = pl.oid)
   where pl.lanname = 'plpgsql' and 
   pn.nspname = 'pga'
   ) f
where pg_get_functiondef like '%userid%'

I just need functions containing a certain text in the definition.

When I run it, this error is thrown:

"ERROR:  "array_agg" is an aggregate function"

If I run just the inner query, it returns all the definitions as expected. No error.

I have also tried this where clause:

where pg_get_functiondef(pp.oid) like '%userid%'

But doesn't work either. How to do it?

3 Answers 3

6

pg_get_functiondef() does not work on aggregate functions, and pg_catalog.pg_proc contains a number of aggregate functions. You can filter them out using the pg_proc.proisagg boolean, as follows:

select
    proname as name,
    pronamespace::pg_catalog.regnamespace,
    pg_catalog.pg_get_functiondef(oid)
from pg_catalog.pg_proc
where proisagg is false; -- this filters them out
Sign up to request clarification or add additional context in comments.

1 Comment

Since v11 proisagg was replaced with prokind. I use prokind != 'a'
3

Here's the answer:

SELECT n.nspname AS schema_name
     , p.proname AS function_name
     , pg_get_function_arguments(p.oid) AS args
     , pg_get_functiondef(p.oid) AS func_def
FROM  (SELECT oid, * FROM pg_proc p WHERE NOT p.proisagg) p
JOIN   pg_namespace n ON n.oid = p.pronamespace
WHERE  n.nspname LIKE 'pga%'
AND    pg_get_functiondef(p.oid) LIKE '%userid%';

Taken from this related answer on dba.SE and adapted to my needs:

Comments

0

None of the other solutions here worked for me but I got this working, on Postgres 14.9 on RDS.

SELECT n.nspname AS schema_name
     , p.proname AS function_name
     , pg_get_function_arguments(p.oid) AS args
     , pg_get_functiondef(p.oid) AS func_def
FROM  (SELECT * FROM pg_proc p2 WHERE NOT p2.prokind = 'a') p
JOIN   pg_namespace n ON n.oid = p.pronamespace
where n.nspname != 'pg_catalog'
and n.nspname != 'information_schema'
and pg_get_functiondef(p.oid) like '%searchtext%'
order by 1, 2;

Comments

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.