Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uHde3-006PHv-5E for pgsql-hackers@arkaria.postgresql.org; Wed, 21 May 2025 07:13:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uHde0-003xxp-GU for pgsql-hackers@arkaria.postgresql.org; Wed, 21 May 2025 07:13:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uHde0-003xxh-5m for pgsql-hackers@lists.postgresql.org; Wed, 21 May 2025 07:13:40 +0000 Received: from mail-yw1-x112d.google.com ([2607:f8b0:4864:20::112d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uHddx-0005Ys-1n for pgsql-hackers@lists.postgresql.org; Wed, 21 May 2025 07:13:39 +0000 Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-70dec158cbcso11550197b3.2 for ; Wed, 21 May 2025 00:13:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747811616; x=1748416416; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=IYhJV/Un1op5/uEYTNHHxaGJh+F7EIvhI9p3TZtyD98=; b=Mg46We3SsIvgweooyuksG/G/NH+e1K5wtTq82whMX37jSpz5m2ACpnG+ZMx4v1VAtO al+4CB0k8RvsKY6zqY8EsYroMopMwT1EwzQTvjMz1L8mLlAH/WhROV3DwqTtuORuPyVu xpykAFUBaShWaFMuA/RivzLw5pJfGqXPVhm0CzYxShaIsyHtmG6RfMBwWjLJgrDQnLSg Hl8yO33lZA5Sp1HWc3kjYEj8WShx6cM7/bM454g7ZRI5j6EQrNlZka5HqcsL7dIdAzuR 1tDN1RlppHbdeLCFfs6vnE5nvLPrttDzrkdzhhFbjUWFIur+LPGg68kpeNZjK/eZ6WMH 9S5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747811616; x=1748416416; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=IYhJV/Un1op5/uEYTNHHxaGJh+F7EIvhI9p3TZtyD98=; b=EpDtoY1JfXNIeHgniR9nEL5LlTmcK1wvAOgb2pGB5SlAFwyFc+fIpN6KLkakFxdxpx ULreFaTUL5+hrmMe1A4fBLGsDnt6OmVu2CLSuK3deSTrfCjoHWqDFwRBfsIlK4XHLH0q FtI4kMSP57wErQVYQVhv/T/by1EKPcg3p4mHh7ooFmtHDwn/UQmg5cqzc5RvfdQbR8Up chQoYY+x9/LgrL4fzQkwxJyPzmP0fEfWYd55O/sFb2IslWnM8o8LGA131GFPCLjpIv/v gUgLnPpduvSWUDLj6HBwi2B1YwyVlv/j798hIYwJLTo2PcGWhkKHMsODLHNuI2VaK1Ld sPrg== X-Forwarded-Encrypted: i=1; AJvYcCXOwF1abW5yQ4SGWnMfbAttwCKpyKF39RpnMvUB5eqOKGtmrWeSshO7KRDxuHN9YhhiyhKVQAIj8tuwTvuQ@lists.postgresql.org X-Gm-Message-State: AOJu0YxOwxRcKlBYRjSMWAUt2g7LNd0YNoDcugtkUMZcm+jll/zKXmG5 N0RFQ7Cmxs+BPtOma1KdnAoV7U0nqBO7JEEhp0KJEZn4Ld63DbqNkXm2LsRa8+Z3uZzJqrPQPw8 rIiXQeRkZ6YCECzYS6nQX/Sff+3ZXg1A= X-Gm-Gg: ASbGnctG+S3eQ8THBOsxVceIe+45Ejx4+UGMbjcCgvvigQKjKYtKVxcJL6F9t0UdAYC fzJHVaIj014O4Bj9EUbxBcVS+zMOwJA9q+rV21Mmu9YrMVOp2+QjKqPpmWLB0wdyUp1A9yZLdyE ZYIWae+sdYbHLqmsCY7PzQJoltUwDCwf46JMA8VJslpMmAAZOylq0c/1J7ikYZ8HDF6Oe3S5PNg aUL X-Google-Smtp-Source: AGHT+IGZ5gAiqW+qCLU2kB1bdPAkylxda8dJanWp5L5KjeUBHE3ltLteK+0eeNJP8kJQybqnC6rxh/YbbZkP32sQJos= X-Received: by 2002:a05:690c:6d84:b0:70d:fc99:15cc with SMTP id 00721157ae682-70dfc993ab4mr1202407b3.36.1747811615662; Wed, 21 May 2025 00:13:35 -0700 (PDT) MIME-Version: 1.0 References: <3chredgnjcmccym2kczawfih226b4ac6co7p6z4jeofevrcosi@mrsxkx2x2c65> In-Reply-To: From: Pavel Stehule Date: Wed, 21 May 2025 09:12:54 +0200 X-Gm-Features: AX0GCFvPNWmT5NGhsPeqlLBS8uFUEmQyv5zqK40s7MmU9CjAIW4gYSCf2RZtD4E Message-ID: Subject: Re: proposal: schema variables To: Dmitry Dolgov <9erthalion6@gmail.com> Cc: Laurenz Albe , Erik Rijkers , Michael Paquier , Amit Kapila , DUVAL REMI , PostgreSQL Hackers , Bruce Momjian , jian he , Alvaro Herrera Content-Type: multipart/alternative; boundary="0000000000007c6a550635a01a79" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000007c6a550635a01a79 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi =C3=BAt 19. 11. 2024 v 20:14 odes=C3=ADlatel Pavel Stehule napsal: > Hi > > I wrote POC of VARIABLE(varname) syntax support > > here is a copy from regress test > > SET session_variables_ambiguity_warning TO on; > SET session_variables_use_fence_warning_guard TO on; > SET search_path TO 'public'; > CREATE VARIABLE a AS int; > LET a =3D 10; > CREATE TABLE test_table(a int, b int); > INSERT INTO test_table VALUES(20, 20); > -- no warning > SELECT a; > a.. > ---- > 10 > (1 row) > > -- warning variable is shadowed > SELECT a, b FROM test_table; > WARNING: session variable "a" is shadowed > LINE 1: SELECT a, b FROM test_table; > ^ > DETAIL: Session variables can be shadowed by columns, routine's variable= s > and routine's arguments with the same name. > a | b.. > ----+---- > 20 | 20 > (1 row) > > -- no warning > SELECT variable(a) FROM test_table; > a.. > ---- > 10 > (1 row) > > ALTER TABLE test_table DROP COLUMN a; > -- warning - variable fence is not used > SELECT a, b FROM test_table; > WARNING: session variable "a" is not used inside variable fence > LINE 1: SELECT a, b FROM test_table; > ^ > DETAIL: The collision of session variable' names and column names is > possible. > a | b.. > ----+---- > 10 | 20 > (1 row) > > -- no warning > SELECT variable(a), b FROM test_table; > a | b.. > ----+---- > 10 | 20 > (1 row) > > DROP VARIABLE a; > DROP TABLE test_table; > SET session_variables_ambiguity_warning TO DEFAULT; > SET session_variables_use_fence_warning_guard TO DEFAULT; > SET search_path TO DEFAULT; > > Last discussion is related to reducing the size of the session variable patch set. I have an idea to use variable's fencing more aggressively from the start, and then we can reduce it in future. This should not break issues with compatibility and doesn't need some like version flags. The real problem of proposed session variables is possible collisions between session variables identifiers and table or columns identifiers. I designed some tools to minimize the risk of unwanted collisions, but these tools increase the size of code and don't reduce the complexity of the patch and tests. The proposed change probably doesn't reduce a lot of code, but can reduce some tests, and mainly possible risk of some unwanted impact - at the end it can be less work for reviewers and less stress for committers - and the implementation can be divided to allone workable following steps. Step 1 =3D=3D=3D=3D=3D So the main change is the hard requirement for usage variable's fence everywhere where collisions are possible - and then in the first step, the collisions will not be possible, and then we don't need it to solve, and we don't need to test it. CREATE VARIABLE public.foo AS int; LET foo =3D 10; SELECT VARIABLE(foo); DO $$ BEGIN RAISE NOTICE '% %', VARIABLE(foo), VARIABLE(public.foo); END; $$; Step 2 =3D=3D=3D=3D=3D Necessity of usage variable fencing in PL/pgSQL can be a problem for migration from PL/SQL. But this can be solved separately by using SPI params hooks - similar to how PL/pgSQL works with PL/pgSQL variables. In this step we can push optimization for fast execution of the LET statement or optimization of usage variables in queries. After this step will be possible: DO $$ BEGIN RAISE NOTICE '% %', foo, VARIABLE(public.foo); END; $$; SELECT VARIABLE(foo); No other visible change in this step. WIth this step the people who do migration form Oracle and PL/pgSQL developers will be very happy. They don't need more. There can be collisions, but the collisions can be limited just to PL/pgSQL scope, and we can use already implemented mechanisms. Step 3 =3D=3D=3D=3D=3D We can talk in future about less requirement of usage variable fencing in queries. This needs to introduce some form of detection collisions and how they should be solved (outside PL/pgSQL). We can talk about other features like temporal, default values, transactional, etc ... This proposal doesn't reduce lines of code, but significantly reduces possible impacts of introducing session variables to other parts of SQL. Moreover, it allows us to separate some work and related discussion into separate blocks - any block can be implemented in different major pg releases. I think a lot of users will be very happy just with step 1 and step 2, and anything else can be discussed in future. Is this plan acceptable? Regards Pavel > Regards > > Pavel > --0000000000007c6a550635a01a79 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi

=C3=BAt 19. 11. 2024 v=C2=A02= 0:14 odes=C3=ADlatel Pavel Stehule <pavel.stehule@gmail.com> napsal:
Hi

I wrote POC of VARIABLE(varname) syntax support

here is a copy from regress test

SET session= _variables_ambiguity_warning TO on;
SET session_variables_use_fence_warn= ing_guard TO on;
SET search_path TO 'public';
CREATE VARIABLE= a AS int;
LET a =3D 10;
CREATE TABLE test_table(a int, b int);
IN= SERT INTO test_table VALUES(20, 20);
-- no warning
SELECT a;
=C2= =A0a..
----
=C2=A010
(1 row)

-- warning variable is shadowe= d
SELECT a, b FROM test_table;
WARNING: =C2=A0session variable "= a" is shadowed
LINE 1: SELECT a, b FROM test_table;
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0^
DETAIL: =C2=A0Session var= iables can be shadowed by columns, routine's variables and routine'= s arguments with the same name.
=C2=A0a =C2=A0| b..
----+----
=C2= =A020 | 20
(1 row)

-- no warning
SELECT variable(a) FROM test_= table;
=C2=A0a..
----
=C2=A010
(1 row)

ALTER TABLE test_= table DROP COLUMN a;
-- warning - variable fence is not used
SELECT a= , b FROM test_table;
WARNING: =C2=A0session variable "a" is no= t used inside variable fence
LINE 1: SELECT a, b FROM test_table;
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0^
DETAIL: =C2=A0The = collision of session variable' names and column names is possible.
= =C2=A0a =C2=A0| b..
----+----
=C2=A010 | 20
(1 row)

-- no w= arning
SELECT variable(a), b FROM test_table;
=C2=A0a =C2=A0| b..
= ----+----
=C2=A010 | 20
(1 row)

DROP VARIABLE a;
DROP TABLE= test_table;
SET session_variables_ambiguity_warning TO DEFAULT;
SET = session_variables_use_fence_warning_guard TO DEFAULT;
SET search_path TO= DEFAULT;


Last d= iscussion is related to reducing the size of the session variable patch set= .

I have an idea to use variable's fencing mor= e aggressively from the start, and then we can reduce it in future. This sh= ould not break issues with compatibility and doesn't need some like ver= sion flags.

The real problem of proposed session v= ariables is possible collisions between session variables identifiers and t= able or columns identifiers. I designed some tools to minimize the risk of = unwanted collisions, but these tools increase the size of code and don'= t reduce the complexity of the patch and tests. The proposed change probabl= y doesn't reduce a lot of code, but can reduce some tests, and mainly p= ossible risk of some unwanted impact - at the end it can be less work for r= eviewers and less stress for committers - and the implementation can be div= ided to allone workable following steps.

Step 1
=3D=3D=3D=3D=3D

So the main change is the = hard requirement for usage variable's fence everywhere where collisions= are possible - and then in the first step, the collisions will not be poss= ible, and then we don't need it to solve, and we don't need to test= it.

CREATE VARIABLE public.foo AS int;
= LET foo =3D 10;
SELECT VARIABLE(foo);

DO= $$
BEGIN
=C2=A0 RAISE NOTICE '% %', VARIABLE(f= oo), VARIABLE(public.foo);
END;
$$;

Step 2
=3D=3D=3D=3D=3D
Necessity of usage variabl= e fencing in PL/pgSQL can be a problem for migration from PL/SQL. But this = can be solved separately by using SPI params hooks - similar to how PL/pgSQ= L works with PL/pgSQL variables. In this step we can push optimization for = fast execution of the LET statement or optimization of usage variables in q= ueries.

After this step will be possible:

DO $$
BEGIN
=C2=A0 RAISE NOTICE '%= %', foo, VARIABLE(public.foo);
END;
$$;
=
SELECT VARIABLE(foo);

No other visi= ble change in this step. WIth this step the people who do migration form Or= acle and PL/pgSQL developers will be very happy. They don't need more. = There can be collisions, but the collisions can be limited just to PL/pgSQL= scope, and we can use already implemented mechanisms.

<= /div>
Step 3
=3D=3D=3D=3D=3D
We can talk in future = about less requirement of usage variable fencing in queries. This needs to = introduce some form of detection collisions and how they should be solved (= outside PL/pgSQL).
We can talk about other features like temporal= , default values, transactional, etc ...

This prop= osal doesn't reduce lines of code, but significantly reduces possible i= mpacts of introducing session variables to other parts of SQL. Moreover, it= allows us to separate some
=C2=A0work and related discussion into sep= arate blocks - any block can be implemented in different major pg releases.=

I think a lot of users will be very = happy just with step 1 and step 2, and anything else can be discussed in fu= ture.

Is this plan acceptable?
Regards

Pavel



=C2=A0
Regards

P= avel
--0000000000007c6a550635a01a79--