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.96) (envelope-from ) id 1wbh8o-002lrz-2O for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Jun 2026 16:04:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wbh8l-007NBI-1F for pgsql-hackers@arkaria.postgresql.org; Mon, 22 Jun 2026 16:04:51 +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.96) (envelope-from ) id 1wbh8l-007NB2-0K for pgsql-hackers@lists.postgresql.org; Mon, 22 Jun 2026 16:04:51 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wbh8i-00000001byS-3APh for pgsql-hackers@lists.postgresql.org; Mon, 22 Jun 2026 16:04:50 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 65MG4gC2473594; Mon, 22 Jun 2026 12:04:42 -0400 From: Tom Lane To: Chao Li cc: Postgres hackers , Kirill Reshke , jian he Subject: Re: psql: Fix CREATE SCHEMA scanning of nested routine bodies In-reply-to: <8E03BB8D-003D-4850-9772-5F8015A5A0C7@gmail.com> References: <8E03BB8D-003D-4850-9772-5F8015A5A0C7@gmail.com> Comments: In-reply-to Chao Li message dated "Mon, 22 Jun 2026 17:34:53 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <473592.1782144282.1@sss.pgh.pa.us> Date: Mon, 22 Jun 2026 12:04:42 -0400 Message-ID: <473593.1782144282@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Chao Li writes: > See this repro: > ``` > evantest=# CREATE SCHEMA s CREATE VIEW begin AS SELECT 1; > evantest-# end; > WARNING: there is no transaction in progress > CREATE SCHEMA > COMMIT > ``` Meh. Yeah, you can fool psql by using non-reserved keywords as identifiers. I'm not super excited about adding complexity that resolves specific edge cases of that kind, because there will always be more of them. As an example, this fools both HEAD and the back branches: regression=# create function begin() returns int regression-# begin atomic return 2+2; end; regression-# Is it really likely that anyone will use "begin" as an object name? Having said that, I agree that what psqlscan.l is doing now is quite hokey and maybe we should make it smarter. I don't like the details of your patch too much though. It's too obviously bolted on: the added parsing logic looks nothing like what was there before. Also, I think it can still be fooled by create schema s create function f() ... as $$ function body $$ create view begin as ... because you don't reset create_schema_routine_body unless you see "end". What I'd think about doing, if you want to pursue this, is to have a buffer similar to cur_state->identifiers[] but it tracks identifiers starting from the most recent CREATE sub-clause start within CREATE SCHEMA (ie, a non-nested CREATE keyword), and then applies basically the same logic as in the original code to decide if we're within a CREATE FUNCTION sub-clause. Also, you could avoid cluttering other productions by resetting all the new state when identifier_count is zero, in the same place where we reset cur_state->identifiers[] today. It'd likely be appropriate to pull all this logic out of the {identifier} production and put it in a subroutine, just because it's getting unduly complicated. regards, tom lane