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 1tKh2M-00AEzj-Oh for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Dec 2024 16:55:10 +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 1tKh2J-00AGW6-Sz for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Dec 2024 16:55:09 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tKh2J-00AGVx-EJ for pgsql-hackers@lists.postgresql.org; Mon, 09 Dec 2024 16:55:08 +0000 Received: from mail-yb1-xb34.google.com ([2607:f8b0:4864:20::b34]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tKh2H-001vsH-QL for pgsql-hackers@lists.postgresql.org; Mon, 09 Dec 2024 16:55:07 +0000 Received: by mail-yb1-xb34.google.com with SMTP id 3f1490d57ef6-e3a0acba5feso2314005276.2 for ; Mon, 09 Dec 2024 08:55:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733763304; x=1734368104; 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=FGi+HlEEIJA2MwgNzogwTcXiMnlJubgnntOSuLshXwk=; b=d4pw8g+09DFKt8lmGPZLesOYi49wnRDDSAC96VKu4nFg/2rToEqZjQDK8gE2tpccnQ ueQnEIzWF5GdrEQO3OvVWGTBYQuG3d9wAP6vW7WLWZhrLzjht0kXBp4mYAC3hNloPbvw JqJSp2SWh7aHmnErfignhIPTIGBbg+O3B5Eo415MpA7bxGjjvXO9Hj9tZK+/6lzhqw1m J3hfSuUg65/g3oRe11mSXisIW3uNHfLIzgTKGDqiot6SQ2+8bhYRabQDO21W3jpqM5RI 61ReFqiq1py8uAsvH4fsAJQwRLy/t1mfX1mhwo7nWRp5CTgH0FFgw7z7g627zPvdpPZa VApw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733763304; x=1734368104; 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=FGi+HlEEIJA2MwgNzogwTcXiMnlJubgnntOSuLshXwk=; b=xAbhdEhDYkNow2V1ijYhav6VEIklQ3ttfHoXhpU/zi112DtKph0MjMYyjhznTrQW3y NIS4K6AFxb+BjLjUpt1rct5JF7NGFeDVWHGMPOxtAib3pwJbRjyulKcPATmYf+0OmYFK pwiWzOTg2XCUFvs67ch0j6i80Y2IpEmrtqPtj0X2uA+MvRPejT8kS4VlF93ZWdwAU/HG EtDmd0IVtnd3bMjbXVPM+7hbdDmXUX+y50iVvQlbL6oPSAslJ1qFbfLJIvLeGcki4DQQ P3+ViHsP/SBPX9jICoCbV3ysmM1wNR/8kVVEzmM9Q0HbdbJA8wknL3HLtjHR6hGXgxXQ Axrg== X-Forwarded-Encrypted: i=1; AJvYcCWnCedOQo+JkuA1qQBuLyNayvOA4MBmQIC5ywhOz5bTNQL2W4DTDU4kJT1tLeLOzjE0xfB319Uoe+8c6ZdM@lists.postgresql.org X-Gm-Message-State: AOJu0YyR3tziaM84kSG5QCyCw/NmIuvIBQhEoEdW036G77cL8X1kfcuV iioAvOTPXn7QDKH+6M+J7ys7oHywWRQgpUtGLb7C61X2QDOC8yAHgdys0a6xd/4Uxrc1/OJLRVN UXkFHWvPY754mO9y2t6BO/GF1wes= X-Gm-Gg: ASbGnct3sDRVh93Bd7X1SBfjHKNbfhrmHGYVlEzb2ectfSrnBIDIkVHumbGG/scnKck m8rvEJvsHZZN1qs33fcpyPEPsDu4x+80BoXbBsOZHYBT7f6+pWLjxAgzKClgiJb5O X-Google-Smtp-Source: AGHT+IEWT58l2jWy9n9K4cuMiynarUT/5WEG0w1EaPRi/RXr9Q4TfYsIKJC2yPRLZ7MWCH6E+GULz74yoU9+6Ub7t4w= X-Received: by 2002:a05:6902:2c08:b0:e39:7f25:5ed6 with SMTP id 3f1490d57ef6-e3a59beb25amr722486276.53.1733763304571; Mon, 09 Dec 2024 08:55:04 -0800 (PST) MIME-Version: 1.0 References: <3chredgnjcmccym2kczawfih226b4ac6co7p6z4jeofevrcosi@mrsxkx2x2c65> <20241120201313.t4wbhld4ktgielaf@erthalion.local> In-Reply-To: <20241120201313.t4wbhld4ktgielaf@erthalion.local> From: Pavel Stehule Date: Mon, 9 Dec 2024 17:54:26 +0100 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 Content-Type: multipart/alternative; boundary="000000000000e4cd6e0628d93925" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000e4cd6e0628d93925 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi st 20. 11. 2024 v 21:14 odes=C3=ADlatel Dmitry Dolgov <9erthalion6@gmail.co= m> napsal: > > On Tue, Nov 19, 2024 at 08:14:01PM +0100, Pavel Stehule wrote: > > Hi > > > > I wrote POC of VARIABLE(varname) syntax support > > Thanks, the results look good. I'm still opposed the idea of having a > warning, and think it has to be an error -- but it's my subjective > opinion. Lets see if there are more votes on that topic. > Maybe the warning of usage of unfenced variables can be changed (enhanced) to some different mechanism that can be more restrictive (and safer), but I think it can still be user friendly. My idea is based on assumption so users with knowledge of stored procedures know variables and related risks (and know tools how to minimize risks), and for other people the risk is higher and we should force usage of variable fences. I think we can force usage of variable fences at query runtime, when query is not executed from the SPI environment. This behaviour can be enabled by default, but can be optionally disabled. CREATE VARIABLE s.x AS int; -- allowed when user has create right on schema s CREATE VIEW v1 AS SELECT x; -- no problem, the check is dynamic (execution), not static CREATE VIEW v2 AS SELECT VARIABLE(x); -- no problem SELECT x; -- fails on access to unfenced variable SELECT * FROM v1; -- fails on access to unfenced variable SELECT * FROM v2; -- ok but inside pl, this check will not be active, and then with default setting I can write an code like LET var =3D 10; -- fencing is not allowed there, and there is not any collision DO $$ BEGIN RAISE NOTICE 'var=3D%', var; RAISE NOTICE 'var=3D%', (SELECT * FROM v1); --is ok here too END; $$; Outside PL the fencing can be required, inside PL the fencing can be optional. With this proposal we can limit the possible risk usage of unfenced variables only in PL context, and the behaviour can be very similar to PL/SQL or SQL/PSM. This check is only a runtime check, so it has no impact on any DDL statement. It doesn't change the syntax or behavior, so it can be implemented subsequently - it is just a safeguard against unwanted usage of variables in an environment, where users have no possibility to use variables already. I can imagine that this check "allow_unfenced_variables" can have three values (everywhere, pl, nowhere) and the default can be pl. The results of queries don't depend on the current setting of this check. For all values for all possible queries and situations, the result is the same (when it is finished). But sometimes, the check can break the execution - in similar meaning like access rights. All previous proposed warnings can be unchanged. Comments, notes? Regards Pavel --000000000000e4cd6e0628d93925 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi

st 20. 11. 2024 v=C2=A021= :14 odes=C3=ADlatel Dmitry Dolgov <9erthalion6@gmail.com> napsal:
> On Tue, Nov 19, 2024 at 08:14:01PM +0100, Pa= vel Stehule wrote:
> Hi
>
> I wrote POC of VARIABLE(varname) syntax support

Thanks, the results look good. I'm still opposed the idea of having a warning, and think it has to be an error -- but it's my subjective
opinion. Lets see if there are more votes on that topic.

Maybe the warning of usage of unfenced variables can be c= hanged (enhanced) to some different mechanism that can be more restrictive = (and safer), but I think it can still be user friendly.

My idea is based on assumption so users with knowledge of stored proc= edures know=C2=A0 variables and related risks (and know tools how to minimi= ze risks), and for other people the risk is higher and we should force usag= e of variable fences. I think we can force usage of variable fences at quer= y runtime, when query is not executed from the SPI environment. This behavi= our can be enabled by default, but can be optionally disabled.=C2=A0
<= div>
CREATE VARIABLE s.x AS int; -- allowed when user has cre= ate right on schema s
CREATE VIEW v1 AS SELECT x; -- no problem, = the check is dynamic (execution), not static
CREATE VIEW v2 A= S SELECT VARIABLE(x); -- no problem

SELECT x; -- f= ails on access to unfenced variable
SELECT * FROM v1; -- fails on= access to unfenced variable
SELECT * FROM v2; -- ok
but inside pl, this check will not be active, and then with de= fault setting I can write an code like

LET var =3D= 10; -- fencing is not allowed there, and there is not any collision
DO $$
BEGIN
=C2=A0 RAISE NOTICE 'var=3D%= 9;, var;
=C2=A0 RAISE NOTICE 'var=3D%', (SELECT * FROM v1= ); --is ok here too
END;
$$;

Outside PL the fencing can be required, inside PL the fencing can be opt= ional. With this proposal we can limit the possible risk usage of unfenced = variables only in PL context, and the behaviour can be very similar to PL/S= QL or SQL/PSM. This check is only a runtime check, so it has no impact on a= ny DDL statement. It doesn't change the syntax or behavior, so it can b= e implemented subsequently - it is just a safeguard against unwanted usage = of variables in an environment, where users have no possibility to use vari= ables already. I can imagine that this check "allow_unfenced_variables= " can have three values (everywhere, pl, nowhere) and the default can = be pl. The results of queries don't depend on the current setting of th= is check. For all values for all possible queries and situations, the resul= t is the same (when it is finished). But sometimes, the check can break the= execution - in similar meaning like access rights. All previous proposed w= arnings can be unchanged.

Comments, notes?

Regards

Pavel



=C2=A0
--000000000000e4cd6e0628d93925--