How to create a nested function in PL / pgSQL?

I would like to create a function in PL / pgSQL with several nested (or internal) functions inside it. This way I can break the problem down into smaller pieces, but not have the smaller parts available outside of this function.

Can this be done in PL / pgSQL? If so, how?

+6
source share
2 answers

Nested functions are not supported by PLpgSQL. Emulation makes no sense and is unproductive.

+2
source

Try:

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(); exits

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

EDIT: if you do not drop the function at the end of the external function will remain visible to the rest of the database.

+7
source

All Articles