Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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?

like image 751
EricBlair1984 Avatar asked Mar 06 '23 22:03

EricBlair1984


2 Answers

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
like image 140
Eric Hanson Avatar answered May 03 '23 00:05

Eric Hanson


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:

  • PostgreSQL: How to list all stored functions that access specific table
like image 34
EricBlair1984 Avatar answered May 03 '23 01:05

EricBlair1984