Re: proposal: schema variables

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>
Cc: Bruce Momjian <bruce(at)momjian(dot)us>, Dmitry Dolgov <9erthalion6(at)gmail(dot)com>, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>, Erik Rijkers <er(at)xs4all(dot)nl>, Michael Paquier <michael(at)paquier(dot)xyz>, Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, DUVAL REMI <REMI(dot)DUVAL(at)cheops(dot)fr>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, jian he <jian(dot)universality(at)gmail(dot)com>, Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>, PegoraroF10 <marcos(at)f10(dot)com(dot)br>
Subject: Re: proposal: schema variables
Date: 2025-11-24 19:59:48
Message-ID: CAFj8pRCACo9_fwfe3ixgrRfThon+Nu26b1phd-ge7v5XCx4=9A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-performance

Hi

I was asked to write the most reduced implementation of session variables.
I thought about possible reductions and there is one - catalog. We
can implement temporary session variables. In this case the catalog is not
necessary. After reduction the total size of the patchset is 134KB (doc +
code + tests). The size of the previous patchset was 390KB.

Unfortunately - without catalog some the dependency check is not available,
and then features that depends on dependencies are not available:

1. no catalog, no dependencies - only buildin types can be used (no custom
types, no domains),
2. no catalog, no dependencies - variables cannot be used in objects that
depend on some objects (views, sql functions),
3. simplified syntax - only scalar types can be used (no arrays, no
composites),
4. no catalog, no access rights, only owner check (variables can be used
just by their creator (owner)),
5. no catalog, no plan cache invalidation support (instead type check
before any usage),
6. no catalog, only temp variables are supported - schema cannot be
specified,
7. without direct access from the expression executor - variables cannot be
a parameter of CALL statement
8. session variables block parallel execution
9. no catalog, no object address - no event triggers for session variables
10. no catalog - DDL for session variables (CREATE TEMP VARIABLE, DROP
VARIABLE) are not transactional. Without catalog, implementation of
transactional DDL can be very difficult.

Although there are lot of strong limits, I think so implemented feature can
be still useful (parametrization of anonymous block and secure
session scope storage)

CREATE TEMP VARIABLE x AS int;
CREATE TEMP VARIABLE y AS int;
LET x = 100;
SELECT VARIABLE(x);
DO $$
BEGIN
RAISE NOTICE '%', VARIABLE(x);
LET y = VARIABLE(x) + 10;
END $$;
SELECT VARIABLE(y);
DROP VARIABLE x;
DROP VARIABLE y;

This simple and reduced implementation doesn't block continuous development
in different directions (without compatibility breaks).

When I worked on this reduced version I found the importance of catalog
based implementation. It is almost impossible to implement variables with
life scope longer than transactions without a catalog. There is a strong
necessity of using dependency mechanisms. With catalog, the plan cache
invalidation can be used (instead typecheck). It is more robust.

Comments, notes?

Regards

Pavel

Attachment Content-Type Size
v20251124-0006-LET-command-assign-a-result-of-expression-to-the-ses.patch text/x-patch 37.0 KB
v20251124-0007-DISCARD-TEMP.patch text/x-patch 4.3 KB
v20251124-0005-svariableReceiver.patch text/x-patch 10.8 KB
v20251124-0004-fill-an-auxiliary-buffer-with-values-of-session-vari.patch text/x-patch 14.4 KB
v20251124-0003-collect-session-variables-used-in-plan-and-assign-pa.patch text/x-patch 15.8 KB
v20251124-0002-parsing-session-variable-fences.patch text/x-patch 18.6 KB
v20251124-0001-CREATE-VARIABLE-DROP-VARIABLE.patch text/x-patch 32.9 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2025-11-24 20:00:01 Re: should we have a fast-path planning for OLTP starjoins?
Previous Message Robert Haas 2025-11-24 19:59:33 Re: another autovacuum scheduling thread

Browse pgsql-performance by date

  From Date Subject
Next Message Pavel Stehule 2025-11-24 21:19:10 Re: proposal: schema variables
Previous Message Peter Geoghegan 2025-11-10 17:00:02 Re: Multicolumn index scan efficiency