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

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


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.