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 1sipUh-009tfE-KA for pgsql-hackers@arkaria.postgresql.org; Tue, 27 Aug 2024 06:15:55 +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 1sipUd-002XY0-RO for pgsql-hackers@arkaria.postgresql.org; Tue, 27 Aug 2024 06:15:52 +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 1sipUd-002XXn-CF for pgsql-hackers@lists.postgresql.org; Tue, 27 Aug 2024 06:15:52 +0000 Received: from mail-lf1-x131.google.com ([2a00:1450:4864:20::131]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sipUZ-001i02-SR for pgsql-hackers@lists.postgresql.org; Tue, 27 Aug 2024 06:15:51 +0000 Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-5343e75c642so3977616e87.2 for ; Mon, 26 Aug 2024 23:15:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1724739347; x=1725344147; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=G2hhgAX7F3XlAi6wo4Dwam1QfwNz4RxQARc4FIXnHys=; b=0KaH+tHpzh3GeYVoJuRw1woD4ObiG/S/90ZpXtpL/HKBDPmza41ncWvqbaWkcv/kl9 gFzHsWPAZDrofJaVm6NvxYey4qJjBSXHKVyROumX6YtncCPUKGKhQ/57SKvj9Utf3IXT 4jFMX117g3QVizOUEi3e+1fJn9t6YZOb4+PZ2b7rSlDpBiVtYST709X08rlprVxC9JtG 9PMyxykXxHLeYjShFfpSazL+Qw9RFx/PZz9fL3xSxbNYNlTw4kPvERFobb+MeBHBChAg BFmTF29OJT7RpP8OGQc5wdRFlgDF+SpPst1+a0O7/ZDHT3RLGQmSGhDbYfmicJTfw7Ho A5vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724739347; x=1725344147; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=G2hhgAX7F3XlAi6wo4Dwam1QfwNz4RxQARc4FIXnHys=; b=NRNJ+c6Yhtwd0pZ+tRv5zu6l2Ul034miW5z7pw8f4kWAAXn2XpD0zO3WV+Qe751yRA 4nFQQLFyeTsDFn76prcoUqzSMe27RWZtcaIUiJPaADbE8fyPROYA0CAcBwu7NhwH4g2C CuU79WXH2PsHt9dPToWY4Q6MjGJ1VWuBHA7dC8l3qTmawtqyav+8iiNBB9XdxUYLm4EW 5G6QPkbjuKm+1jLuCY43XQXie8ewSodxAIHv8eQ974kTr0fvxMJcqxU2IGvODYlA4BS9 jRysnxmaNtUZiGOf4xDZyffUhCr1W+cA++yHMSZ+ci4dkiQBTa1nm7wj7PX6nWVMerTs 7FRA== X-Forwarded-Encrypted: i=1; AJvYcCVUavJc1TIWXSPnGwXj32laRnS427FHQsv45mWB/mZkDQUpOf5EBWQ8DXGMiN0hvcL5SDQdoArEPUkD8sZ/@lists.postgresql.org X-Gm-Message-State: AOJu0YxohrHxDQPXX6JvKsTKNgE+qJNRo5JyYSUa0nePupQk78IYp6TZ weUQAM5bFQclLnNQMohRrPDy051mwHcmEK1FuOb46f5elxOmObOAJxxDbKaBBpY= X-Google-Smtp-Source: AGHT+IFvcPVfDqKE/KEBC7axeed7JcPnGsMjkIRiblKDvAzaxCpHR8YhSx80AN4rjniKm2h2y8kN5g== X-Received: by 2002:a05:6512:3e0d:b0:533:4638:df40 with SMTP id 2adb3069b0e04-5343883b116mr6551455e87.27.1724739346749; Mon, 26 Aug 2024 23:15:46 -0700 (PDT) Received: from [192.168.33.94] ([213.208.157.39]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a86e5878455sm66246966b.160.2024.08.26.23.15.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Aug 2024 23:15:46 -0700 (PDT) Message-ID: <04ec666686e9e21cb515617df06885c66f3d34ce.camel@cybertec.at> Subject: Re: proposal: schema variables From: Laurenz Albe To: Pavel Stehule Cc: Erik Rijkers , Michael Paquier , Amit Kapila , DUVAL REMI , PostgreSQL Hackers Date: Tue, 27 Aug 2024 08:15:42 +0200 In-Reply-To: References: <20200924035637.GF28585@paquier.xyz> <20201001033824.GC8130@paquier.xyz> <51a9a68e8a998d04df17417d45c1dbd4@xs4all.nl> <89817942c99da01cd5e7850fe418436b@xs4all.nl> <56ca532c37eb0b540961f74a7bd5db39@xs4all.nl> <8181bd3abc647bdae5a4f78e71e62478a98c75f4.camel@cybertec.at> <9e67d49deb18270eddb95e602c83f02b98459843.camel@cybertec.at> <3b662dc5b615d4c20a55e8e2fbe6fc00fe00609d.camel@cybertec.at> <6996931e8c9edf3b82223e74e92326a7ed06c1d6.camel@cybertec.at> <67aa68a7e6dfb44c0cbbdf7f97cadfede4269ce5.camel@cybertec.at> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.4 (3.52.4-1.fc40) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 2024-08-15 at 07:55 +0200, Pavel Stehule wrote: > =C3=BAt 30. 7. 2024 v=C2=A021:46 odes=C3=ADlatel Laurenz Albe napsal: > > - A general reminder: single line comments should start with a lower ca= se > > =C2=A0 letter and have to period in the end: >=20 > Should it be "have not to period in the end" ? I made a typo. I mean "have *no* period in the end". > I fixed almost all without parts related to psql and executor - there alm= ost all > current comments break the mentioned rule. So I use the style used in the= se files. > I can fix it (if you like it) - or it can be fixed generally in a separat= ed patch set. Thanks. I also noticed that a lot of existing comments break that rule, so I think that it is fine if you stick with the way that the surrounding code does it. > > - Variable as parameter: > >=20 > > =C2=A0 CREATE VARIABLE var AS date; > > =C2=A0 LET var =3D current_date; > > =C2=A0 PREPARE stmt(date) AS SELECT $1; > > =C2=A0 EXECUTE stmt(var); > > =C2=A0 ERROR:=C2=A0 paramid of PARAM_VARIABLE param is out of range > >=20 > > =C2=A0 Is that working as intended?=C2=A0 I don't understand the error = message. >=20 > fixed in 0002 patch (variables cannot be used as EXECUTE argument) and 00= 14 (enable usage variables as an argument of EXECUTE) Thanks. > > =C2=A0 > --- a/src/backend/catalog/namespace.c > > =C2=A0 > +++ b/src/backend/catalog/namespace.c > > =C2=A0 > [...] > > =C2=A0 > +/* > > =C2=A0 > + * IdentifyVariable - try to find variable identified by list= of names. > > =C2=A0 > [...] > >=20 > > =C2=A0 Perhaps part of the reason why this function is so complicated i= s that > > =C2=A0 you support things like this: > >=20 > > =C2=A0 =C2=A0 CREATE SCHEMA sch; > > =C2=A0 =C2=A0 CREATE TYPE cp AS (a integer, b integer); > > =C2=A0 =C2=A0 CREATE VARIABLE sch.v AS cp; > > =C2=A0 =C2=A0 LET sch.v =3D (1, 2); > > =C2=A0 =C2=A0 SELECT sch.v.b; > > =C2=A0 =C2=A0 =C2=A0b=20 > > =C2=A0 =C2=A0 =E2=95=90=E2=95=90=E2=95=90 > > =C2=A0 =C2=A0 =C2=A02 > > =C2=A0 =C2=A0 (1 row) > >=20 > > =C2=A0 =C2=A0 test=3D# SELECT test.sch.v.b; > > =C2=A0 =C2=A0 =C2=A0b=20 > > =C2=A0 =C2=A0 =E2=95=90=E2=95=90=E2=95=90 > > =C2=A0 =C2=A0 =C2=A02 > > =C2=A0 =C2=A0 (1 row) > >=20 > > =C2=A0 We don't support that for tables: > >=20 > > =C2=A0 =C2=A0 CREATE TABLE tab (c cp); > > =C2=A0 =C2=A0 INSERT INTO tab VALUES (ROW(42, 43)); > > =C2=A0 =C2=A0 SELECT tab.c.b FROM tab; > > =C2=A0 =C2=A0 ERROR:=C2=A0 cross-database references are not implemente= d: tab.c.b > >=20 > > =C2=A0 You have to use extra parentheses: > >=20 > > =C2=A0 =C2=A0 SELECT (tab.c).b FROM tab; > > =C2=A0 =C2=A0 =C2=A0b=C2=A0=20 > > =C2=A0 =C2=A0 =E2=95=90=E2=95=90=E2=95=90=E2=95=90 > > =C2=A0 =C2=A0 =C2=A043 > > =C2=A0 =C2=A0 (1 row) > >=20 > > =C2=A0 I'd say that this should be the same for session variables. > > =C2=A0 What do you think? >=20 > I prefer the current state, but I don't have a very strong opinion about = it. > It can save 115 lines of almost trivial code, but I think the user experi= ence > can be much worse.=C2=A0 Using composite types in tables is not too commo= n a > pattern (and when I read some recommendations for Oracle, it is an antipa= ttern), > but usage of composite variables is common (it can hold a row). Moreover, > we talked long about possible identifier's collisions, and the pattern > schema.variable is very good protection against possible collisions. > Probably the pattern catalog.schema.variable.field is not too interesting > but the support has 50 lines. >=20 > More, the plpgsql supports pattern label.variable.field, so can be little= bit > unfriendly if session variables doesn't support "similar" pattern I see your point, and I'm fine with leaving it as it is. >=20 >=20 > > - src/backend/commands/session_variable.c > >=20 > > =C2=A0 There is one comment that confuses me and that I did not edit: > >=20 > > =C2=A0 > +Datum > > =C2=A0 > +GetSessionVariable(Oid varid, bool *isNull, Oid *typid) > > =C2=A0 > +{ > > =C2=A0 > +=C2=A0 =C2=A0SVariable=C2=A0 =C2=A0svar; > > =C2=A0 > + > > =C2=A0 > +=C2=A0 =C2=A0svar =3D get_session_variable(varid); > > =C2=A0 > + > > =C2=A0 > +=C2=A0 =C2=A0/* > > =C2=A0 > +=C2=A0 =C2=A0 * Although svar is freshly validated in this po= int, the svar->is_valid can > > =C2=A0 > +=C2=A0 =C2=A0 * be false, due possible accepting invalidation= message inside domain > > =C2=A0 > +=C2=A0 =C2=A0 * check. Now, the validation is done after lock= , that can also accept > > =C2=A0 > +=C2=A0 =C2=A0 * invalidation message, so validation should be= trustful. > > =C2=A0 > +=C2=A0 =C2=A0 * > > =C2=A0 > +=C2=A0 =C2=A0 * For now, we don't need to repeat validation. = Only svar should be valid > > =C2=A0 > +=C2=A0 =C2=A0 * pointer. > > =C2=A0 > +=C2=A0 =C2=A0 */ >=20 > This comment is related to assertions. Before I had there `Assert(svar->i= s_valid)`, > because I expected it. But it was not always true. And although it is tru= e, > we don't need to validate a variable, because at this moment, the variabl= e > should be locked, and then we can return content safely. I guess my main problem is the word "trustful". I don't recognize that wor= d. Perhaps you can reword the comment along the lines of your above explanatio= n. >=20 > > - src/backend/executor/execMain.c > >=20 > > =C2=A0 > @@ -196,6 +198,51 @@ standard_ExecutorStart(QueryDesc *queryDe= sc, int eflags) > > =C2=A0 >=C2=A0 =C2=A0 =C2=A0Assert(queryDesc->sourceText !=3D NULL); > > =C2=A0 >=C2=A0 =C2=A0 =C2=A0estate->es_sourceText =3D queryDesc->source= Text; > > =C2=A0 > > > =C2=A0 > +=C2=A0 =C2=A0/* > > =C2=A0 > +=C2=A0 =C2=A0 * The executor doesn't work with session variab= les directly. Values of > > =C2=A0 > +=C2=A0 =C2=A0 * related session variables are copied to dedic= ated array, and this array > > =C2=A0 > +=C2=A0 =C2=A0 * is passed to executor. > > =C2=A0 > +=C2=A0 =C2=A0 */ > >=20 > > =C2=A0 Why?=C2=A0 Is that a performance measure?=C2=A0 The comment shou= ld explain that. >=20 > Session variables are volatile internally - some function that uses LET s= tatements > can be called more times inside a query. This behavior is not consistent = with > plpgsql's variables or external parameters. And if we want to support par= allel > queries, then volatile session variables can be pretty messy (and unusabl= e). > If we want the same behaviour for queries with or without parallel suppor= t, > then the session variables should be "stable" (like stable functions). > Simple implementation is using "snapshot" of values of used session varia= bles > when query is started. This "snapshot" is immutable, so we don't need to = make > a copy more times. >=20 > I changed this comment Thanks. > > - parallel safety > >=20 > > =C2=A0 > --- a/src/backend/optimizer/util/clauses.c > > =C2=A0 > +++ b/src/backend/optimizer/util/clauses.c > > =C2=A0 > @@ -935,6 +936,13 @@ max_parallel_hazard_walker(Node *node, ma= x_parallel_hazard_context *context) > > =C2=A0 >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (param->paramkind =3D=3D P= ARAM_EXTERN) > > =C2=A0 >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return false; > > =C2=A0 > > > =C2=A0 > +=C2=A0 =C2=A0 =C2=A0 =C2=A0/* We doesn't support passing sess= ion variables to workers */ > > =C2=A0 > +=C2=A0 =C2=A0 =C2=A0 =C2=A0if (param->paramkind =3D=3D PARAM_= VARIABLE) > > =C2=A0 > +=C2=A0 =C2=A0 =C2=A0 =C2=A0{ > > =C2=A0 > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (max_parallel_haz= ard_test(PROPARALLEL_RESTRICTED, context)) > > =C2=A0 > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return= true; > > =C2=A0 > +=C2=A0 =C2=A0 =C2=A0 =C2=A0} > >=20 > > =C2=A0 Even if a later patch alleviates that restriction, this patch sh= ould document that > > =C2=A0 session variables imply "parallel restricted".=C2=A0 I have adde= d that to doc/src/sgml/parallel.sgml. > >=20 >=20 > merged (and removed in 0015) Thanks. I hope I can do some more review at some point in the future... I sincerely hope that this patch set gets merged at some point. The big obstacle is that it is so large. That's probably because it is pre= tty feature-complete (but, as we have found, not totally free of bugs). Judging from the amount of time I put into my review so far, I guess that t= his patch set would keep a committer busy for several weeks. Perhaps the only = way to get this done would be to make you a committer... Yours, Laurenz Albe