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?
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
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With