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 1sWfOb-00GaCY-TP for pgsql-hackers@arkaria.postgresql.org; Wed, 24 Jul 2024 17:03:22 +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 1sWfNb-006evy-FG for pgsql-hackers@arkaria.postgresql.org; Wed, 24 Jul 2024 17:02:19 +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 1sWfNa-006evp-SG for pgsql-hackers@lists.postgresql.org; Wed, 24 Jul 2024 17:02:19 +0000 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sWfNX-001EeR-LG for pgsql-hackers@lists.postgresql.org; Wed, 24 Jul 2024 17:02:17 +0000 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-427b4c621b9so48116975e9.1 for ; Wed, 24 Jul 2024 10:02:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1721840534; x=1722445334; 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=uDXD9qQsBL6W/9f1UPjSIL6WpIU/dDfN/OVQ9JkdGms=; b=3Ox1wepUf3i0WRZ08McSr60fjo7QIaifW5hNLYK5LcLFKmOYbukKgwLZ5SZuBHimPt CEmikohfH00pOIjaIY/q4B6shzedwPNSH7R6YMjFYuPtK7RiEeN0Fy+zCH4e7UPEGt8s boOotGYGAmUYkVfTog8ZVcEzG6YjCm95ijyrsBlOYS0thS92i1xbZBROTA4GYajkPNvf +rxeZTvzb+kjSoK0iW4G9qxQ2cyYoO5reu0PiWIdDYwb+SrsrMt/sP+/ZjWz5Fq11JtL URWvcBpMW+9tEILf99pahFRTMWMtuydy4KTD6Zz7dH+44J84dXcNsioECEVb2WFEbc08 K6cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721840534; x=1722445334; 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=uDXD9qQsBL6W/9f1UPjSIL6WpIU/dDfN/OVQ9JkdGms=; b=IWsXgOxW9IGB05RWnmK3+MpG6gp3ZnyBy5rDK+GAhzNryEh0ZsYHxnLpMrtBlPHNpa 2Jem1EI5BJGvl5azB2dzK7eHUQeZ4sQpbrzVyBjm0z1dLYLBPbNFEwk8qs9BRJRw9h+5 JA4kSF9zPQWRWwweCrXoMEMY5FGZ3HccsMvUCkXtI27ZMFr5DAsHbZh+5Kmz5gFjj93p WK+TbGzm1oFgIdWPHlO5hwbpKDq355LSL7WR5PMsqVlwF68vbQFsKkCTX6e269C31wh5 cc851YJuPuAR4FbRv3RPyaviWtAp3GK5STeDJzcWc0SblCmzZGcU7NvWrOJqRNbPuTH4 hO7g== X-Forwarded-Encrypted: i=1; AJvYcCWPM/ZQ2P6xQQ0nQpki0m0Otc4/1ayDlDdQf9l3iuWKXxZYsBSTQPRsFWHJ/aQpNQyZo+IEmOB2eA5Anr4B4/+numOaYqFsNY610/ZHHsYfjcId X-Gm-Message-State: AOJu0YyUqmUfbzViOx98y2OWNzbeI7Ngp+b301qtSux87PGn/uS03JBw 4wTgxJ9fpKxdB3uVHMOFAtKcSZ7BdC7+g7+VhiHDCC4p1M3KjnpMR/7WzLaO5fs= X-Google-Smtp-Source: AGHT+IFArT73LFm0NDmahBVDwo0byN8FQR6wKKdcc9xIIZVdnkk2DX1aehcKOjV9aWktbO8VMlAwkw== X-Received: by 2002:a05:600c:3588:b0:426:647b:1bfa with SMTP id 5b1f17b1804b1-427f954244bmr19671535e9.8.1721840533513; Wed, 24 Jul 2024 10:02:13 -0700 (PDT) Received: from dynamic-pd01.res.v6.highway.a1.net ([2001:871:5e:9d94:367c:90f6:8506:4524]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-427f9399b13sm36129455e9.27.2024.07.24.10.02.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jul 2024 10:02:13 -0700 (PDT) Message-ID: Subject: Re: proposal: schema variables From: Laurenz Albe To: Pavel Stehule Cc: Erik Rijkers , Michael Paquier , Zhihong Yu , Amit Kapila , DUVAL REMI , PostgreSQL Hackers Date: Wed, 24 Jul 2024 19:02:11 +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> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.3 (3.52.3-1.fc40) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 2024-07-24 at 17:19 +0200, Pavel Stehule wrote: > > =C2=A0 This is buggy: > >=20 > > =C2=A0 =C2=A0 CREATE VARIABLE str AS text NOT NULL DEFAULT NULL; > >=20 > > =C2=A0 Ugh. > >=20 > > =C2=A0 =C2=A0 SELECT str; > > =C2=A0 =C2=A0 ERROR:=C2=A0 null value is not allowed for NOT NULL sessi= on variable "laurenz.str" > > =C2=A0 =C2=A0 DETAIL:=C2=A0 The result of DEFAULT expression is NULL. > >=20 > > =C2=A0 Perhaps that is a leftover from the previous coding, but I think= there need be > > =C2=A0 no check upon SELECT.=C2=A0 It should be enough to check during = CREATE VARIABLE and > > =C2=A0 LET. >=20 > I think it is correct. When you use SELECT str, then DEFAULT expression i= s > executed, and then the result is assigned to a variable, and there is NOT= NULL > guard, which raises an exception. The variable is not initialized when yo= u run > DDL, but it is initialized when you first read or write from/to the varia= ble. > The DEFAULT expression is not evaluated in DDL time. In this case, I can = detect > the problem in DDL time because the result is transformed to NULL node, b= ut > generally there can be SQL non immutable function, and then I need to wai= t until > the DEFAULT expression will be evaluated - and it is time of first readin= g. > Unfortunately, there is not an available check if some expression is NULL= , > that I can use in DDL time, but I have it in plpgsql_check. That makes sense to me. In that case, I think we can drop the requirement that NOT NULL variables need a default clause. >=20 > > =C2=A0 I can see the usefulness of IMMUTABLE variables, but I am surpri= sed that > > =C2=A0 they are reset by DISCARD.=C2=A0 What is the use case you have i= n mind? > > =C2=A0 The use case I can envision is an application that sets a value = right after > > =C2=A0 authentication, for use with row-level security.=C2=A0 But then = it would be harmful > > =C2=A0 if the user could reset the variable with DISCARD. >=20 > Primary I think about IMMUTABLE variables like about some form of cache. > This cache is protected against unwanted repeated write - and can help wi= th > detection of this situation. We can leave it as it is. The IMMUTABLE feature need not go into the first release, so that can be discussed some more later. Thanks for the new patch set; I'll look at it soon. Yours, Laurenz Albe