Creating nested functions with plpgsql - Mailing list pgsql-sql

From Jorge Godoy
Subject Creating nested functions with plpgsql
Date
Msg-id 200605021414.49661.jgodoy@gmail.com
Whole thread Raw
Responses Re: Creating nested functions with plpgsql
List pgsql-sql
Hi!


Is it possible to create nested functions using plpgsql as the language?  I'd
like to avoid other dependencies if I can, besides using already written SQL
code...

There are lots of repetitive tasks that "subfunctions" would solve in a very
elegant way, keeping code more readable and concise.

I was thinking something along the lines of:

CREATE FUNCTION outer_function(param1 DATE) RETURNS date AS $$
DECLARE
BEGIN
    CREATE FUNCTION inner_function(OUT output_day DATE) AS $_$
    DECLARE
        output_day DATE;
    BEGIN
        -- do something to calculate output_day
    END;
    $_$ language plpgsql stable strict;
                          -- here I could have other languages or
                          -- even restrict the inner function to the
                          -- same language as the outer function...

                          -- $_$ is different from $$ intentionally.

    -- do something in main function that uses inner function several
    -- times.
END;
$$ language plpgsql;
   

I have some real case examples where this could be useful, if it is needed.
I haven't pasted them here because the smallest one has 176 LOC, after
refactoring with nested functions.

If it is not possible, are there any plans to allow this kind of thing?
(Even with a different syntax it would be good to have it.)


TIA,
--
Jorge Godoy      <jgodoy@gmail.com>



pgsql-sql by date:

Previous
From: Mario Splivalo
Date:
Subject: Re: Counting the rows INSERTed/UPDATEd?
Next
From: "Catalin Pitis"
Date:
Subject: ERROR: plan should not reference subplan's variable