1

Need to extract the source of a PostgreSQL function using SQL.

I am seeing this odd behavior with one of the function, all other functions (around 200+ ) work absolutely fine.

When I run the following statement, it works:

select prosrc  from pg_proc where proname= 'accounts_count';

However when I run the following, it returns an empty string:

select  routine_definition
from    information_schema.routines
where   specific_name = 'accounts_count_66243'

PostgreSQL version 8.3. I have tried using both pgAdmin III and psql.

Not a show stopper, but would be useful to know why this might be.

Any ideas anyone?

2
  • What does select length(routine_definition)... return? Commented Sep 29, 2012 at 21:08
  • Since 8.4, there is pg_get_functiondef(). Maybe another reason to update to a more up-to-date version? Commented Sep 30, 2012 at 9:07

1 Answer 1

2

if you look on definition of information_schema.routines, then you can find following filter:

FROM pg_namespace n, pg_proc p, pg_language l, pg_type t, pg_namespace nt
 WHERE n.oid = p.pronamespace AND p.prolang = l.oid AND p.prorettype = t.oid 
   AND t.typnamespace = nt.oid AND (pg_has_role(p.proowner, 'USAGE'::text) 
    OR has_function_privilege(p.oid, 'EXECUTE'::text));

so my theory:

  • there are some issue in rights and ownership of related function (probably) - try to use different account for validation of this theory (postgres is best)
Sign up to request clarification or add additional context in comments.

1 Comment

Well done. It turned out to be a rights issue. The function owner was set to a different user. It appears if the function is not owned by the logged in user, information_schema.routines does not surface the source while pg_proc does. Odd.

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.