I wrote proof concept of schema variables. The patch is not nice, but the functionality is almost complete (for scalars only) and can be good enough for playing with this concept.
I recap a goals (the order is random):
1. feature like PL/SQL package variables (with similar content life cycle)
2. available from any PL used by PostgreSQL, data can be shared between different PL
3. possibility to store short life data in fast secured storage
4. possibility to pass parameters and results to/from anonymous blocks
5. session variables with possibility to process static code check
6. multiple API available from different environments - SQL commands, SQL functions, internal functions
7. data are stored in binary form
Example:
CREATE VARIABLE public.foo AS integer;
LET foo = 10 + 20;
DO $$
declare x int = random() * 1000;
BEGIN
RAISE NOTICE '%', foo;
LET foo = x + 100;
END; $$;
SELECT public.foo + 10;
SELECT * FROM data WHERE col = foo;
All implemented features are described by regress tests
Interesting note - it is running without any modification of plpgsql code.