How to print functions and triggers sourcecode in postgresql? please let me know if any one know the query to display the function, triggers source code.
How to print functions and triggers sourcecode in postgresql? please let me know if any one know the query to display the function, triggers source code.
\sf function_name in psql yields editable source code of a single function.
From https://www.postgresql.org/docs/9.6/static/app-psql.html:
\sf[+] function_description This command fetches and shows the definition of the named function, in the form of a CREATE OR REPLACE FUNCTION command.
If + is appended to the command name, then the output lines are numbered, with the first line of the function body being line 1.
Since Version: psql (9.6.17, server 11.6)
I have tried all of above answer but For me
postgres=> \sf jsonb_extract_path_text
CREATE OR REPLACE FUNCTION pg_catalog.jsonb_extract_path_text(from_json jsonb, VARIADIC path_elems text[])
RETURNS text
LANGUAGE internal
IMMUTABLE PARALLEL SAFE STRICT
AS $function$jsonb_extract_path_text$function$
postgres=> \df+
ERROR: column p.proisagg does not exist
LINE 6: WHEN p.proisagg THEN 'agg'
^
HINT: Perhaps you meant to reference the column "p.prolang".
df seems not working for me.
To list all functions:
select n.nspname as function_schema,
p.proname as function_name,
l.lanname as function_language,
case when l.lanname = 'internal' then p.prosrc
else pg_get_functiondef(p.oid)
end as definition,
pg_get_function_arguments(p.oid) as function_arguments,
t.typname as return_type
from pg_proc p
left join pg_namespace n on p.pronamespace = n.oid
left join pg_language l on p.prolang = l.oid
left join pg_type t on t.oid = p.prorettype
where n.nspname not in ('pg_catalog', 'information_schema')
order by function_schema,
function_name;
Now if you want to search a specific word or text in all function is something like that, make sure you replace your text in the function:
with tbl as (
select n.nspname as function_schema,
p.proname as function_name,
l.lanname as function_language,
case when l.lanname = 'internal' then p.prosrc
else pg_get_functiondef(p.oid)
end as definition,
pg_get_function_arguments(p.oid) as function_arguments,
t.typname as return_type
from pg_proc p
left join pg_namespace n on p.pronamespace = n.oid
left join pg_language l on p.prolang = l.oid
left join pg_type t on t.oid = p.prorettype
where n.nspname not in ('pg_catalog', 'information_schema')
)
select *
from tbl
where definition ilike '%word or text you want to search%'
order by function_schema,
function_name;