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 1tBUUZ-004DL6-Ml for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Nov 2024 07:42:15 +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 1tBUUW-001ieE-FG for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Nov 2024 07:42:13 +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 1tBUUW-001ie4-3F for pgsql-hackers@lists.postgresql.org; Thu, 14 Nov 2024 07:42:12 +0000 Received: from mail-yw1-x1135.google.com ([2607:f8b0:4864:20::1135]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tBUUO-001qo3-Un for pgsql-hackers@lists.postgresql.org; Thu, 14 Nov 2024 07:42:11 +0000 Received: by mail-yw1-x1135.google.com with SMTP id 00721157ae682-6ea15a72087so3287217b3.1 for ; Wed, 13 Nov 2024 23:42:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731570124; x=1732174924; 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=XPvc8BLbep11rclwzUBKRxBKPvbVBtB657oyZv3Ys6A=; b=O2lm0LabtyofERoL7GoPgTC1dwkcfwe8WUqmoP5xvfTfhy/LGYg1Sablbo/a9/thk/ CZ66sEpSCqDDR/5R1xrSK7f3xP9tdZREOmoApqljROVbMME9ZLxyzlBzV3lYTiksniI0 /9+7FSJRYU+Vkq3+lrtRdO0zhseJo8674N6yWTbg7IkxwI0fRn605kja45j+EFZ+jfow 5RAAW20ogbQ2RjlGPUuaVLBw7U/tOs4Y1M8xF7kJHGTHF/a8EWuZafOtpKOzRK1I4KIj yPLfII3sU2HRAVeu5CDm62YaBeGKigFtw6x6U8Eda7MpmkeqKrnz11KR87+NeGtyMChN ImVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731570124; x=1732174924; 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=XPvc8BLbep11rclwzUBKRxBKPvbVBtB657oyZv3Ys6A=; b=Jj8JfjUlRRuvzd2uNx/RGYPRJrSYNtzcBSx3VaXXn+It7hrrpbIF0fCI8N6HkWDCF5 7ocZ+sV0nNcv85j3siglfDnfoIKkQvJ+5P6MSCubUSJ/dd1SRdnYuUcK6RSkDU2+Ayiq 4T15LrQCWctkpmP+6yvGyLwldNNkuQkuHKShVMv0NZJJMQq4h7lwr4qy9rSX1moe/osV b2Uccw/2nsxax4NEMd1y2ymM/oR3+KP5900DTUmZMKTlz8E9LCEvuHO5QdfJUX2ReG+7 +my4qZFX5SdFplY0jbrZ848LnKxC5zPHb1OafgJ2omBIhi+/U2jWKnp1VOqSrzz8f3Tm XfpA== X-Forwarded-Encrypted: i=1; AJvYcCXijnsXXPROAMPabiK/mUEA0lrSBTsiVgngnx/ewvVqnaeL27Jxyjt1/Jaajo4KemM4Cw3F5Mqxymgf5HhB@lists.postgresql.org X-Gm-Message-State: AOJu0YxuiAzTqUjnqLlnN7cUcSEJizNANPNnU/RuohVoTg5UNXwDvHsu vNQM6+7VvgFIBS5uaQppuAb23cAG7BjrbimQ4Ny3Hs8yFxrDne/SQnsv/fZesvZ8wb2Mtv5qx2u veMQI4WxV5WnjJ4KABfzMoRtnBbM= X-Google-Smtp-Source: AGHT+IFWooEnrMSOE0VL96bJzYcx+EpbRHIyh9t/1cl6ydFT6EzuGNrTuwm3hFPvgV+Jj0N4QQMZHQk5TWl65pc1oz0= X-Received: by 2002:a05:690c:61c8:b0:6db:d217:895e with SMTP id 00721157ae682-6ecb348203bmr67920247b3.36.1731570123882; Wed, 13 Nov 2024 23:42:03 -0800 (PST) MIME-Version: 1.0 References: <3chredgnjcmccym2kczawfih226b4ac6co7p6z4jeofevrcosi@mrsxkx2x2c65> In-Reply-To: <3chredgnjcmccym2kczawfih226b4ac6co7p6z4jeofevrcosi@mrsxkx2x2c65> From: Pavel Stehule Date: Thu, 14 Nov 2024 08:41: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="00000000000023480b0626da961a" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000023480b0626da961a Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable st 13. 11. 2024 v 17:35 odes=C3=ADlatel Dmitry Dolgov <9erthalion6@gmail.co= m> napsal: > > On Sun, Nov 10, 2024 at 06:51:40PM GMT, Pavel Stehule wrote: > > ne 10. 11. 2024 v 17:19 odes=C3=ADlatel Pavel Stehule < > pavel.stehule@gmail.com> > > napsal: > > I thought a lot of time about better solutions for identifier collision= s > > and I really don't think so there is some consistent user friendly > syntax. > > Personally I think there is an easy already implemented solution - > > convention - just use a dedicated schema for variables and this schema > > should not be in the search path. Or use secondary convention - like > using > > prefix "__" for session variables. Common convention is using "_" for > > PLpgSQL variables. I searched how this issue is solved in other > databases, > > or in standard, and I found nothing special. The Oracle and SQL/PSM has= a > > concept of visibility - the variables are not visible outside packages = or > > modules, but Postgres has nothing similar. It can be emulated by a > > dedicated schema without inserting a search path, but it is less strong= . > > > > I think we can introduce an alternative syntax, that will not be user > > friendly or readable friendly, but it can be without collisions - or ca= n > > decrease possible risks. > > > > It is nothing new - SQL does it with old, "new" syntax of inner joins, = or > > in Postgres we can > > > > where salary < 40000 > > > > or > > > > where pg_catalog.int4lt(salary, 40000); > > > > > > or some like we use for operators OPERATOR(*schema*.*operatorname*) > > > > So introducing VARIABLE(schema.variablename) syntax as an alternative > > syntax for accessing variables I really like. I strongly prefer to use > this > > as only alternative (secondary) syntax, because I don't think it is > > friendly syntax or writing friendly, but it is safe, and I can imagine > > tools that can replace generic syntax to this special, or that detects > > generic syntax and shows some warning. Then users can choose what they > > prefer. Two syntaxes - generic and special can be good enough for all - > and > > this can be perfectly consistent with current Postgres. > > As far as I recall, last time this topic was discussed in hackers, two > options were proposed: the one with VARIABLE(name), what you mention > here; and another one with adding variables to the FROM clause. The > VARIABLE(...) syntax didn't get much negative feedback, so I guess why > not -- if you find it fitting, it would be interesting to see the > implementation. > > I'm afraid it should not be just an alternative syntax, but the only one > allowed, because otherwise I don't see how scenarious like "drop a > column with the same name" could be avoided. As in the previous thread: > > -- we've got a variable b at the same time > SELECT a, b FROM table1; > > Then dropping the column b, but everything still works beause the > variable b got silently picked up. But if it would be required to say > VARIABLE(b), then all fine. > In this scenario you will get a warning related to variable shadowing (before you drop a column). I think this issue can be partially similar to creating two equally named tables in different schemas (both schemas are in search path). When you drop one table, the query will work, but the result is different. It is the same issue. The SQL has no concept of shadowing and on the base line it is not necessary. But when you integrate SQL with some procedural code then you should solve this issue (or accept). This issue is real, and it is in every procedural enhancement of SQL that I know with the same syntax. On the other hand I doubt this is a real issue. The changes of system catalogue are tested before production - so probably you will read a warning about a shadowed variable, and probably you will get different results, because variable b has the same value for all rows, and probably will have different value than column b. I can imagine the necessity of disabling this warning on production systems. Shadowing by self is not an issue, probably, but it is a signal of code quality problems. But this scenario is real, and then it is a question if the warning about shadowed variables should be only optional and if it can be disabled. Maybe not. Generally the shadowing is a strange concept - it is safeguard against serious issues, but it should not be used generally and everywhere the developer should rename the conflict identifiers. Regards Pavel > And to make sure we're on the same page, could you post couple of > examples from curretly existing tests in the patch, how are they going > to look like with this proposal? > > About adding variables to the FROM clause. Looks like this option was > quite popular, and you've mentioned some technical challenges > implementing that. If you'd like to go with another approach, it would > be great to elaborate on that -- maybe even with a PoC, to make a > convincing point here. > --00000000000023480b0626da961a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
st 13. 11. 2024 v=C2=A017:35 odes=C3= =ADlatel Dmitry Dolgov <9erthal= ion6@gmail.com> napsal:
> On Sun, Nov 10, 2024 at 06:51:40PM GMT, Pavel Stehule w= rote:
> ne 10. 11. 2024 v 17:19 odes=C3=ADlatel Pavel Stehule <pavel.stehule@gmail.com>
> napsal:
> I thought a lot of time about better solutions for identifier collisio= ns
> and I really don't think so there is some consistent user friendly= syntax.
> Personally I think there is an easy already implemented solution -
> convention - just use a dedicated schema for variables and this schema=
> should not be in the search path. Or use secondary convention - like u= sing
> prefix "__" for session variables. Common convention is usin= g "_" for
> PLpgSQL variables. I searched how this issue is solved in other databa= ses,
> or in standard, and I found nothing special. The Oracle and SQL/PSM ha= s a
> concept of visibility - the variables are not visible outside packages= or
> modules, but Postgres has nothing similar. It can be emulated by a
> dedicated schema without inserting a search path, but it is less stron= g.
>
> I think we can introduce an alternative syntax, that will not be user<= br> > friendly or readable friendly, but it can be without collisions - or c= an
> decrease possible risks.
>
> It is nothing new - SQL does it with old, "new" syntax of in= ner joins, or
> in Postgres we can
>
> where salary < 40000
>
> or
>
> where pg_catalog.int4lt(salary, 40000);
>
>
> or some like we use for operators OPERATOR(*schema*.*operatorname*) >
> So introducing VARIABLE(schema.variablename) syntax as an alternative<= br> > syntax for accessing variables I really like. I strongly prefer to use= this
> as only alternative (secondary) syntax, because I don't think it i= s
> friendly syntax or writing friendly, but it is safe, and I can imagine=
> tools that can replace generic syntax to this special, or that detects=
> generic syntax and shows some warning. Then users can choose what they=
> prefer. Two syntaxes - generic and special can be good enough for all = - and
> this can be perfectly consistent with current Postgres.

As far as I recall, last time this topic was discussed in hackers, two
options were proposed: the one with VARIABLE(name), what you mention
here; and another one with adding variables to the FROM clause. The
VARIABLE(...) syntax didn't get much negative feedback, so I guess why<= br> not -- if you find it fitting, it would be interesting to see the
implementation.

I'm afraid it should not be just an alternative syntax, but the only on= e
allowed, because otherwise I don't see how scenarious like "drop a=
column with the same name" could be avoided. As in the previous thread= :

=C2=A0 =C2=A0 -- we've got a variable b at the same time
=C2=A0 =C2=A0 SELECT a, b FROM table1;

Then dropping the column b, but everything still works beause the
variable b got silently picked up. But if it would be required to say
VARIABLE(b), then all fine.


I think this issue can be partia= lly similar to creating two equally named tables in different schemas (both= schemas are in search path). When you drop one table, the query will work,= but the result is different. It is the same issue. The SQL has no concept = of shadowing and on the base line it is not necessary. But when you integra= te SQL with some procedural code then you should solve this issue (or accep= t). This issue is real, and it is in every procedural enhancement of SQL th= at I know with the same syntax.=C2=A0 On the other hand I doubt this is a r= eal issue. The changes of system catalogue are tested before production - s= o probably you will read a warning about a shadowed variable, and probably = you will get different results, because variable b has the same value for a= ll rows, and probably will have different value than column b. I can imagin= e the necessity of disabling this warning on production systems. Shadowing = by self is not an issue, probably, but it is a signal of code quality probl= ems.





And to make sure we're on the same page, could you post couple of
examples from curretly existing tests in the patch, how are they going
to look like with this proposal?

About adding variables to the FROM clause. Looks like this option was
quite popular, and you've mentioned some technical challenges
implementing that. If you'd like to go with another approach, it would<= br> be great to elaborate on that -- maybe even with a PoC, to make a
convincing point here.
--00000000000023480b0626da961a--