How do I create a nested function in PL/pgSQL?

后端 未结 2 1602
自闭症患者
自闭症患者 2021-01-18 02:37

I would like to create a function in PL/pgSQL with a couple of nested (or inner) functions within it. This way I can break the problem down into smaller pieces but not have

相关标签:
2条回答
  • 2021-01-18 02:47

    Nested functions are not supported by PLpgSQL. The emulation has not any sense and it is nonproductive.

    0 讨论(0)
  • 2021-01-18 03:01

    Try it:

    CREATE OR REPLACE FUNCTION outer() RETURNS void AS $outer$
    DECLARE s text;
    BEGIN
      CREATE OR REPLACE FUNCTION inner() RETURNS text AS $inner$
      BEGIN
        RETURN 'inner';
      END;
      $inner$ language plpgsql;
    
      SELECT inner() INTO s;
      RAISE NOTICE '%', s;
    
      DROP FUNCTION inner();
    END;
    $outer$ language plpgsql;
    

    In postgres 9.5 SELECT outer(); outputs

     psql:/vagrant/f.sql:14: NOTICE:  inner
    

    EDIT: if you don't drop the inner function at the end of the outer function it will remain visible to the rest of the database.

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