Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mgSUs-0007BR-0b for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Oct 2021 14:04:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mgSUq-0002ju-Vr for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Oct 2021 14:04:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mgSUq-0002jl-NH for pgsql-hackers@lists.postgresql.org; Fri, 29 Oct 2021 14:04:40 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mgSUk-0003WP-6t for pgsql-hackers@postgresql.org; Fri, 29 Oct 2021 14:04:40 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 19TE4VXE626328; Fri, 29 Oct 2021 10:04:32 -0400 From: Tom Lane To: tomas@tuxteam.de cc: pgsql-hackers@postgresql.org Subject: Re: plpgsql: can I use a variable in a DECLARE later whithin the DECLARE? In-reply-to: <20211029121435.GA5414@tuxteam.de> References: <20211029121435.GA5414@tuxteam.de> Comments: In-reply-to message dated "Fri, 29 Oct 2021 14:14:35 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <626326.1635516271.1@sss.pgh.pa.us> Date: Fri, 29 Oct 2021 10:04:31 -0400 Message-ID: <626327.1635516271@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk writes: > In plpgsql, i'm trying to use a variable initialised in a DECLARE in > a later initialisation expression whithin the same DECLARE: > DECLARE > x int := 23; > y int := x + 4 > BEGIN > ... > Experiments suggest that it works, but the docs are silent about that. > Is this a good idea, or should I be more careful and do it in a nested > block? I don't see any reason to foresee that it would break. However, I notice that we don't actually have any regression tests checking this. scope_test() in plpgsql.sql checks an adjacent question, but not exactly this one. I'm a bit inclined now to go add such a test. regards, tom lane