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 1lot7H-0001Vp-C7 for pgsql-general@arkaria.postgresql.org; Thu, 03 Jun 2021 19:34:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lot7G-0002uu-CE for pgsql-general@arkaria.postgresql.org; Thu, 03 Jun 2021 19:34:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lot7G-0002um-3F for pgsql-general@lists.postgresql.org; Thu, 03 Jun 2021 19:34:54 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lot79-0006Pu-OJ for pgsql-general@lists.postgresql.org; Thu, 03 Jun 2021 19:34:53 +0000 Received: from bruce by momjian.us with local (Exim 4.92) (envelope-from ) id 1lot76-0003kW-KP; Thu, 03 Jun 2021 15:34:44 -0400 Date: Thu, 3 Jun 2021 15:34:44 -0400 From: Bruce Momjian To: Tom Lane Cc: Adrian Klaver , Marc Millas , "pgsql-general@lists.postgresql.org" Subject: Re: syntax question Message-ID: <20210603193444.GD22019@momjian.us> References: <20210603190108.GC22019@momjian.us> <49061.1622748075@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <49061.1622748075@sss.pgh.pa.us> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Jun 3, 2021 at 03:21:15PM -0400, Tom Lane wrote: > Adrian Klaver writes: > > On 6/3/21 12:01 PM, Bruce Momjian wrote: > >> On Thu, Jun 3, 2021 at 08:58:03PM +0200, Marc Millas wrote: > >>> within a function, I want to create another function. > > >> You can't create functions inside of functions; same for procedures. > > > Sure you can: > > Yeah. The actual problem here is that Marc is expecting variable > substitution to occur within a utility (DDL) statement, which it > doesn't. The workaround is to build the command as a string and > use EXECUTE, as Adrian illustrated: > > > EXECUTE 'create function ' || bidule || '() RETURNS void language > > plpgsql AS $fnc$ BEGIN END; $fnc$ '; > > This is not terribly well explained in the existing docs. I tried > to improve the explanation awhile ago in HEAD: > > https://www.postgresql.org/docs/devel/plpgsql-statements.html#PLPGSQL-STATEMENTS-GENERAL-SQL Oh, I thought he wanted to declare a function inside the function that could be called only by that function, like private functions in Oracle packages can do. Yes, you can create a function that defines a function that can be called later. I guess you could also create a function that _conditionally_ creates a function that it can call itself too. My point is that you can't create a function that has function scope --- they all have schema scope. -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com If only the physical world exists, free will is an illusion.