In Postgres 14 a new kind of syntax was added to CREATE FUNCTION
where the body of the function can be specified directly in SQL rather than being contained in a string. The string bodies of functions are easily accessible in pg_proc.prosrc
, while the new function bodies appear to be stored in pg_proc.prosqlbody
.
The prosqlbody
column is of type pg_node_tree
, which would normally get passed to pg_get_expr
in order to turn it back into readable SQL. However, various attempts to call pg_get_expr
on this column either return NULL
or an error ('input is a query, not an expression'). Is this a bug, or is there some alternative to pg_get_expr
that should be used in this case?
The only other alternative I can see is using pg_get_functiondef
to get the entire CREATE FUNCTION
statement, and then scraping past all of the initial function properties to find the function body. This would work, but it feels fragile and is more effort than I would be hoping for.
The context is that we're working on some code to generate update scripts between two Postgres databases - while it would be simpler to just work with the entire CREATE FUNCTION
statement from pg_get_functiondef
and stick that in a script like pg_dump
does, when altering an existing function that may not be an option. So being able to access the function definition as individual parts helps a lot.