EricBlair1984
EricBlair1984

Reputation: 369

"ERROR: "array_agg" is an aggregate function" while getting function definitions

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?

Upvotes: 8

Views: 2357

Answers (3)

influent
influent

Reputation: 1367

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;

Upvotes: 0

EricBlair1984
EricBlair1984

Reputation: 369

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:

Upvotes: 3

Eric Hanson
Eric Hanson

Reputation: 654

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

Upvotes: 6

Related Questions