From: Tom Lane <tgl@sss.pgh.pa.us>
To: Chao Li <li.evan.chao@gmail.com>
Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>
Cc: Kirill Reshke <reshkekirill@gmail.com>
Cc: jian he <jian.universality@gmail.com>
Subject: Re: psql: Fix CREATE SCHEMA scanning of nested routine bodies
Date: Mon, 22 Jun 2026 12:04:42 -0400
Message-ID: <473593.1782144282@sss.pgh.pa.us> (raw)
In-Reply-To: <8E03BB8D-003D-4850-9772-5F8015A5A0C7@gmail.com>
References: <8E03BB8D-003D-4850-9772-5F8015A5A0C7@gmail.com>
Chao Li <li.evan.chao@gmail.com> 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-hackers@postgresql.org
Cc: tgl@sss.pgh.pa.us, li.evan.chao@gmail.com, reshkekirill@gmail.com, jian.universality@gmail.com
Subject: Re: psql: Fix CREATE SCHEMA scanning of nested routine bodies
In-Reply-To: <473593.1782144282@sss.pgh.pa.us>
References: <8E03BB8D-003D-4850-9772-5F8015A5A0C7@gmail.com> <473593.1782144282@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox