how to display full stored procedure code?

PostgresqlStored Procedures

Postgresql Problem Overview


How do you view a stored procedure/function?

Say I have an old function without the original definition - I want to see what it is doing in pg/psql but I can't seem to figure out a way to do that.

using Postgres version 8.4.1

Postgresql Solutions


Solution 1 - Postgresql

\df+ <function_name> in psql.

Solution 2 - Postgresql

\ef <function_name> in psql. It will give the whole function with editable text.

Solution 3 - Postgresql

SELECT prosrc FROM pg_proc WHERE proname = 'function_name';

This tells the function handler how to invoke the function. It might be the actual source code of the function for interpreted languages, a link symbol, a file name, or just about anything else, depending on the implementation language/call convention

Solution 4 - Postgresql

use pgAdmin or use pg_proc to get the source of your stored procedures. pgAdmin does the same.

Solution 5 - Postgresql

Use \df to list all the stored procedure in Postgres.

Solution 6 - Postgresql

If anyone wonders how to quickly query catalog tables and make use of the pg_get_functiondef() function here's the sample query:

SELECT n.nspname AS schema
      ,proname AS fname
      ,proargnames AS args
      ,t.typname AS return_type
      ,d.description
      ,pg_get_functiondef(p.oid) as definition
--      ,CASE WHEN NOT p.proisagg THEN pg_get_functiondef(p.oid)
--            ELSE 'pg_get_functiondef() can''t be used with aggregate functions'
--       END as definition
  FROM pg_proc p
  JOIN pg_type t
    ON p.prorettype = t.oid
  LEFT OUTER
  JOIN pg_description d
    ON p.oid = d.objoid
  LEFT OUTER
  JOIN pg_namespace n
    ON n.oid = p.pronamespace
 WHERE NOT p.proisagg
   AND n.nspname~'<$SCHEMA_NAME_PATTERN>'
   AND proname~'<$FUNCTION_NAME_PATTERN>'

Solution 7 - Postgresql

Since PostgreSQL 9.1 \sf is available.

Solution 8 - Postgresql

You can also get by phpPgAdmin if you are configured it in your system,

Step 1: Select your database

Step 2: Click on find button

Step 3: Change search option to functions then click on Find.

You will get the list of defined functions.You can search functions by name also, hope this answer will help others.

Solution 9 - Postgresql

We can use pg_get_functiondef() command to get the full function code of proc. Also creating the macro using the below code we can use it as shortcut key to get the function code. Try this method to display full postgresql fundtion code.

DO
$$
DECLARE	
        v_Inputtext	TEXT;
        v_output	text;
BEGIN
       v_Inputtext 	:= (SELECT lower('procedurename'));
       v_output 	:= (SELECT pg_get_functiondef(oid) FROM pg_catalog.pg_proc WHERE proname = v_Inputtext);
       RAISE NOTICE '%',v_output;
END;
$$

https://www.novicetechie.com/2020/01/how-to-display-full-postgresql-function.html

Solution 10 - Postgresql

SELECT pg_get_functiondef(( SELECT oid 
                            FROM   pg_proc
                            WHERE  proname = 'function_name' ));

Solution 11 - Postgresql

Normally speaking you'd use a DB manager application like pgAdmin, browse to the object you're interested in, and right click your way to "script as create" or similar.

Are you trying to do this... without a management app?

Solution 12 - Postgresql

To see the full code(query) written in stored procedure/ functions, Use below Command:

sp_helptext procedure/function_name

for function name and procedure name don't add prefix 'dbo.' or 'sys.'.

don't add brackets at the end of procedure or function name and also don't pass the parameters.

use sp_helptext keyword and then just pass the procedure/ function name.

use below command to see full code written for Procedure:

sp_helptext ProcedureName

use below command to see full code written for function:

sp_helptext FunctionName

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondarrenView Question on Stackoverflow
Solution 1 - PostgresqlMilen A. RadevView Answer on Stackoverflow
Solution 2 - PostgresqlAsha KoshtiView Answer on Stackoverflow
Solution 3 - PostgresqlMaximView Answer on Stackoverflow
Solution 4 - PostgresqlFrank HeikensView Answer on Stackoverflow
Solution 5 - PostgresqlPuneet PurohitView Answer on Stackoverflow
Solution 6 - PostgresqlmsciwojView Answer on Stackoverflow
Solution 7 - PostgresqlAntonio BardazziView Answer on Stackoverflow
Solution 8 - PostgresqlVeeresh DigasangiView Answer on Stackoverflow
Solution 9 - PostgresqlMuthuView Answer on Stackoverflow
Solution 10 - PostgresqltechStudView Answer on Stackoverflow
Solution 11 - PostgresqlannakataView Answer on Stackoverflow
Solution 12 - PostgresqlDeveloper_SurajView Answer on Stackoverflow