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 1mxbxy-0006lQ-Kj for pgsql-general@arkaria.postgresql.org; Wed, 15 Dec 2021 21:37:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mxbxx-0002Hi-Gb for pgsql-general@arkaria.postgresql.org; Wed, 15 Dec 2021 21:37:37 +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 1mxbxv-0002HZ-TW for pgsql-general@lists.postgresql.org; Wed, 15 Dec 2021 21:37:37 +0000 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mxbxr-0003AV-8J for pgsql-general@lists.postgresql.org; Wed, 15 Dec 2021 21:37:35 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id A8DB03200974; Wed, 15 Dec 2021 16:37:27 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Wed, 15 Dec 2021 16:37:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= message-id:date:mime-version:subject:to:cc:references:from :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=h JqFDBYnvl9zhZjqizU5jzkMARqdutWj0HQxhobCkus=; b=v5nbRx1mVIIgjeiDx psv9n5S9wi1zSn8PsfOAinWLOfj5F9awYUY23BCHbRhXvLHJ9uqPGDP5e2Zl2AD2 fdrJ99Xp8n089vJXr0xmkSfgczPrP8YquCRMD8AmZy5ruH6KktNsrAc1WJADZhJz w17Rt+FbY5wR73+7tGN9V5BiHAr7fwbO8Fk3WDHLu7q88ZSkAPfHoTRGOghX1fEO bSCkuDx1xwY+TgWYJ8ymUg9En+PJOofzm038aUFGhMxjLGnVQGKOjzeGaSZ1Hn6b dZuBlLlPv1WqEURCQ2frbD2ngWig+lIR7lJxxGrMMk1gurja7o+bDBFe/wgiYdrm UvE8A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=hJqFDBYnvl9zhZjqizU5jzkMARqdutWj0HQxhobCk us=; b=jc9F6rskoubf+ODS3jz7e4nNSVhPMnz+cEiar12c2ZQOPHposyGN+St0l 3g/Q6QWpg52uC3AoGaqosjt4CRQ0qbDZzAcvtABkVe5y77NimnZidBp4/EICwZH1 k0cAgUZTuMUnuXO5FK95ogo8D+DF0q52W+ggcW+HxN7s1G8ApK6OQ+2CSMYJjn4i 7KoUo8vDdTr+WEweNF1q8B2G/Tkgfoe3icAgQNgNMyXxRgr5yS0VkVUAHXhZHR8F XZCB45yBjTM5xb9xqfZPfwHyDtj22e9o+Ja/Ba9ZNXHKHiZg8X7bO2RvFalzKjTV x+0XRFLT6zq0Rcjksi+f1W8xab+yg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvuddrledvgdduheefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvfhfhjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeelkeegjefhkeetheefudevteehiedvudehkeduleet fedvffeuhedtvdegtdeffeenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgne cuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhi rghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 15 Dec 2021 16:37:26 -0500 (EST) Message-ID: <39ae120c-ec62-1833-1104-d45030491b45@aklaver.com> Date: Wed, 15 Dec 2021 13:37:25 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: Why can't I have a "language sql" anonymous block? Content-Language: en-US To: Bryn Llewellyn , Merlin Moncure Cc: pgsql-general list References: <872342.1639428817@sss.pgh.pa.us> <25643215-899C-4FF8-A35F-85A61A8AD0A5@yugabyte.com> From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12/15/21 13:05, Bryn Llewellyn wrote: >> mmoncure@gmail.com wrote: >> >>> Bryn wrote: >>> >>>> david.g.johnston@gmail.com wrote: >>>> >>>>> Bryn wrote: >>>>> >>>>> There must be a reason to prefer a “language sql” procedure over a >>>>> “language plpgsql” procedure—otherwise the former wouldn’t be >>>>> supported. >>>> >>>> I would say that is true for functions.  I wouldn’t assume that for >>>> procedures—it’s probable that because sql already >>>> worked for functions we got that feature for free when implementing >>>> procedures. >>> >>> Interesting. That’s exactly the kind of historical insight I was >>> after. Thanks. >> >> SQL language functions have one clear advantage in that they can >> be inlined in narrow contexts; this can give >> dramatic performance advantages when it occurs. They have a lot of >> disadvantages: >> >> (1) Tables can’t be created then used without turning off function >> body evaluation. >> >> (2) Queries must be parsed and planned upon each evocation (this can >> be construed as advantage in scenarios where you want to float a >> function over schemas). >> >> (3) Generally constrained to basic SQL statements (no variables, logic >> etc). >> >> …simplifies down to, “use SQL functions [only] when inlining”. > > — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — > About your point #1… > > I used a procedure to test this because functions shouldn’t do DDL. I > started with a working “language plpgsql” example: Since procedures are relatively new to Postgres you are going to find more functions doing DDL then procedures. Not sure I follow why one is preferred over the other anyway? > Then I changed it to a “language sql” test: > > *drop table if exists t cascade; > drop procedure if exists p() cascade; > create procedure p() >   language sql > as $body$ >   drop table if exists t cascade; >   create table t(k int primary key, v text not null); > >   -- Causes compilation error: 42P01: relation "t" does not exist >   -- insert into t(k, v) values (1, 'dog'), (2, 'cat'), (3, 'frog'); > $body$; > * > With the “insert” in place, it fails the syntax check with the error > that I mentioned. When it’s commented out, it passes the syntax check > and executes without error and has the expected effect. Which is documented: https://www.postgresql.org/docs/current/xfunc-sql.html " Note The entire body of an SQL function is parsed before any of it is executed. While an SQL function can contain commands that alter the system catalogs (e.g., CREATE TABLE), the effects of such commands will not be visible during parse analysis of later commands in the function. Thus, for example, CREATE TABLE foo (...); INSERT INTO foo VALUES(...); will not work as desired if packaged up into a single SQL function, since foo won't exist yet when the INSERT command is parsed. It's recommended to use PL/pgSQL instead of an SQL function in this type of situation. " > The upshot of this is that I can’t design a test to demonstrate the > effect that I thought you meant. Could you show me a code example, please? To turn off function body evaluation: https://www.postgresql.org/docs/current/runtime-config-client.html "check_function_bodies (boolean) This parameter is normally on. When set to off, it disables validation of the routine body string during CREATE FUNCTION and CREATE PROCEDURE. Disabling validation avoids side effects of the validation process, in particular preventing false positives due to problems such as forward references. Set this parameter to off before loading functions on behalf of other users; pg_dump does so automatically. " -- Adrian Klaver adrian.klaver@aklaver.com