How to display the function, procedure, triggers source code in postgresql?

后端 未结 8 577
别那么骄傲
别那么骄傲 2020-12-04 05:59

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.

相关标签:
8条回答
  • 2020-12-04 06:23

    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.

    0 讨论(0)
  • 2020-12-04 06:30

    For function:

    you can query the pg_proc view , just as the following

    select proname,prosrc from pg_proc where proname= your_function_name; 
    

    Another way is that just execute the commont \df and \ef which can list the functions.

    skytf=> \df           
                                                 List of functions
     Schema |         Name         | Result data type |              Argument data types               |  Type  
    --------+----------------------+------------------+------------------------------------------------+--------
     public | pg_buffercache_pages | SETOF record     |                                                | normal
    
    
    skytf=> \ef  pg_buffercache_pages
    

    It will show the source code of the function.

    For triggers:

    I dont't know if there is a direct way to get the source code. Just know the following way, may be it will help you!

    • step 1 : Get the table oid of the trigger:
        skytf=> select tgrelid from pg_trigger  where tgname='insert_tbl_tmp_trigger';
          tgrelid
        ---------
           26599
        (1 row)
    
    • step 2: Get the table name of the above oid !
        skytf=> select oid,relname  from pg_class where oid=26599;
          oid  |           relname           
        -------+-----------------------------
         26599 | tbl_tmp
        (1 row)
    
    • step 3: list the table information
        skytf=> \d tbl_tmp
    

    It will show you the details of the trigger of the table . Usually a trigger uses a function. So you can get the source code of the trigger function just as the above that I pointed out !

    0 讨论(0)
提交回复
热议问题